Skip to content

Commit

Permalink
Merge pull request #1353 from haircommander/cpuset-preconfigure
Browse files Browse the repository at this point in the history
preconfigure cpuset with required resources
  • Loading branch information
giuseppe authored Nov 15, 2023
2 parents 39fa25c + 08d9fea commit f9e3792
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/libcrun/cgroup-internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ convert_shares_to_weight (uint64_t shares)
}

int initialize_cpuset_subsystem (const char *path, libcrun_error_t *err);
int initialize_cpuset_subsystem_resources (const char *path, runtime_spec_schema_config_linux_resources *resources, libcrun_error_t *err);

int write_cpuset_resources (int dirfd_cpuset, int cgroup2, runtime_spec_schema_config_linux_resources_cpu *cpu, libcrun_error_t *err);

Expand Down
26 changes: 23 additions & 3 deletions src/libcrun/cgroup-setup.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
#include <libgen.h>

static int
initialize_cpuset_subsystem_rec (char *path, size_t path_len, char *cpus, char *mems, libcrun_error_t *err)
initialize_cpuset_subsystem_rec (char *path, size_t path_len, char *cpus, char *mems, runtime_spec_schema_config_linux_resources *resources, libcrun_error_t *err)
{
cleanup_close int dirfd = -1;
cleanup_close int mems_fd = -1;
Expand Down Expand Up @@ -95,7 +95,7 @@ initialize_cpuset_subsystem_rec (char *path, size_t path_len, char *cpus, char *
return 0;

path[parent_path_len] = '\0';
ret = initialize_cpuset_subsystem_rec (path, parent_path_len, cpus, mems, err);
ret = initialize_cpuset_subsystem_rec (path, parent_path_len, cpus, mems, resources, err);
path[parent_path_len] = '/';
if (UNLIKELY (ret < 0))
{
Expand All @@ -104,6 +104,15 @@ initialize_cpuset_subsystem_rec (char *path, size_t path_len, char *cpus, char *
}
}

/* If we know the resources, use them, instead of initializing with the full set, only to revert it later. */
if (resources && resources->cpu)
{
if (resources->cpu->cpus)
cpus = xstrdup (resources->cpu->cpus);
if (resources->cpu->mems)
mems = xstrdup (resources->cpu->mems);
}

if (cpus_fd >= 0)
{
b_len = TEMP_FAILURE_RETRY (write (cpus_fd, cpus, strlen (cpus)));
Expand All @@ -129,7 +138,18 @@ initialize_cpuset_subsystem (const char *path, libcrun_error_t *err)
char mems_buf[257];

cpus_buf[0] = mems_buf[0] = '\0';
return initialize_cpuset_subsystem_rec (tmp_path, strlen (tmp_path), cpus_buf, mems_buf, err);
return initialize_cpuset_subsystem_rec (tmp_path, strlen (tmp_path), cpus_buf, mems_buf, NULL, err);
}

int
initialize_cpuset_subsystem_resources (const char *path, runtime_spec_schema_config_linux_resources *resources, libcrun_error_t *err)
{
cleanup_free char *tmp_path = xstrdup (path);
char cpus_buf[257];
char mems_buf[257];

cpus_buf[0] = mems_buf[0] = '\0';
return initialize_cpuset_subsystem_rec (tmp_path, strlen (tmp_path), cpus_buf, mems_buf, resources, err);
}

static int
Expand Down
2 changes: 1 addition & 1 deletion src/libcrun/cgroup-systemd.c
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ setup_cpuset_for_systemd_v1 (runtime_spec_schema_config_linux_resources *resourc
if (UNLIKELY (ret < 0))
return ret;

ret = initialize_cpuset_subsystem (cgroup_path, err);
ret = initialize_cpuset_subsystem_resources (cgroup_path, resources, err);
if (UNLIKELY (ret < 0))
return ret;

Expand Down

0 comments on commit f9e3792

Please sign in to comment.