Skip to content

Commit

Permalink
cgroup: honor cpu burst
Browse files Browse the repository at this point in the history
Signed-off-by: Giuseppe Scrivano <[email protected]>
  • Loading branch information
giuseppe committed Oct 26, 2023
1 parent 8b44699 commit 979b658
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/libcrun/cgroup-internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,6 @@ int initialize_cpuset_subsystem (const char *path, 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);

int write_cpu_burst (int cpu_dirfd, bool cgroup2, runtime_spec_schema_config_linux_resources_cpu *cpu, libcrun_error_t *err);

#endif
18 changes: 17 additions & 1 deletion src/libcrun/cgroup-resources.c
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,21 @@ write_memory_resources (int dirfd, bool cgroup2, runtime_spec_schema_config_linu
return 0;
}

int
write_cpu_burst (int cpu_dirfd, bool cgroup2, runtime_spec_schema_config_linux_resources_cpu *cpu,
libcrun_error_t *err)
{
char fmt_buf[32];
size_t len;
int ret;

if (! cpu->burst_present)
return 0;

len = sprintf (fmt_buf, "%" PRIi64, cpu->burst);
return write_cgroup_file (cpu_dirfd, cgroup2 ? "cpu.max.burst" : "cpu.cfs_burst_us", fmt_buf, len, err);
}

static int
write_pids_resources (int dirfd, bool cgroup2, runtime_spec_schema_config_linux_resources_pids *pids,
libcrun_error_t *err)
Expand Down Expand Up @@ -1013,7 +1028,8 @@ write_cpu_resources (int dirfd_cpu, bool cgroup2, runtime_spec_schema_config_lin
if (UNLIKELY (ret < 0))
return ret;
}
return 0;

return write_cpu_burst (dirfd_cpu, cgroup2, cpu, err);
}

int
Expand Down
43 changes: 43 additions & 0 deletions src/libcrun/cgroup-systemd.c
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,41 @@ setup_rt_runtime (runtime_spec_schema_config_linux_resources *resources,
return 0;
}

static int
setup_missing_cpu_options_for_systemd (runtime_spec_schema_config_linux_resources *resources, bool cgroup2, const char *path, libcrun_error_t *err)
{
cleanup_free char *cgroup_path = NULL;
cleanup_close int dirfd = -1;
int parent;
int ret;

if (resources->cpu == NULL)
return 0;

if (! cpu->burst_present)
return 0;

for (parent = 0; parent < 2; parent++)
{
if (cgroup2)
ret = append_paths (&cgroup_path, err, CGROUP_ROOT, path ? path : "", (parent ? ".." : NULL), NULL);
else
ret = append_paths (&cgroup_path, err, CGROUP_ROOT, "/cpu", path ? path : "", (parent ? ".." : NULL), NULL);
if (UNLIKELY (ret < 0))
return ret;

dirfd = open (cgroup_path, O_DIRECTORY | O_CLOEXEC);
if (UNLIKELY (dirfd < 0))
return crun_make_error (err, errno, "open `%s`", cgroup_path);

ret = write_cpu_burst (dirfd, cgroup2, resources->cpu, err);
if (UNLIKELY (ret < 0))
return ret;
}

return 0;
}

static int
setup_cpuset_for_systemd_v1 (runtime_spec_schema_config_linux_resources *resources, const char *path, libcrun_error_t *err)
{
Expand Down Expand Up @@ -381,6 +416,10 @@ systemd_finalize (struct libcrun_cgroup_args *args, char **path_out,
return crun_make_error (err, 0, "invalid cgroup mode `%d`", cgroup_mode);
}

ret = setup_missing_cpu_options_for_systemd (resources, cgroup_mode == CGROUP_MODE_UNIFIED, path, err);
if (UNLIKELY (ret < 0))
return ret;

*path_out = path;
path = NULL;

Expand Down Expand Up @@ -1381,6 +1420,10 @@ libcrun_update_resources_systemd (struct libcrun_cgroup_status *cgroup_status,
goto exit;
}

ret = setup_missing_cpu_options_for_systemd (resources, cgroup_mode == CGROUP_MODE_UNIFIED, cgroup_status->path, err);
if (UNLIKELY (ret < 0))
goto exit;

ret = 0;

exit:
Expand Down

0 comments on commit 979b658

Please sign in to comment.