From a0ee605d0e03bdb393a0b5221dd5528107246783 Mon Sep 17 00:00:00 2001 From: Mahmood Ali Date: Thu, 14 Oct 2021 08:46:07 -0400 Subject: [PATCH] executor: set CpuWeight in cgroup-v2 (#11287) Cgroup-v2 uses `cpu.weight` property instead of cpu shares: https://www.kernel.org/doc/html/latest/admin-guide/cgroup-v2.html#cpu-interface-files . And it uses a different range (i.e. `[1, 10000]`) from cpu.shares (i.e. `[2, 262144]`) to make things more interesting. Luckily, the libcontainer provides a helper function to perform the conversion [`ConvertCPUSharesToCgroupV2Value`](https://pkg.go.dev/github.com/opencontainers/runc@v1.0.2/libcontainer/cgroups#ConvertCPUSharesToCgroupV2Value). I have confirmed that docker/libcontainer performs the conversion as well in https://github.com/opencontainers/runc/blob/v1.0.2/libcontainer/specconv/spec_linux.go#L536-L541 , and that CpuShares is ignored by libcontainer in https://github.com/opencontainers/runc/blob/v1.0.2/libcontainer/cgroups/fs2/cpu.go#L24-L29 . --- .changelog/11287.txt | 4 ++++ drivers/shared/executor/executor_linux.go | 3 ++- 2 files changed, 6 insertions(+), 1 deletion(-) create mode 100644 .changelog/11287.txt diff --git a/.changelog/11287.txt b/.changelog/11287.txt new file mode 100644 index 00000000000..36e6f7873c6 --- /dev/null +++ b/.changelog/11287.txt @@ -0,0 +1,4 @@ + +```release-note:bug +driver/exec: Set CPU resource limits when cgroup-v2 is enabled +``` diff --git a/drivers/shared/executor/executor_linux.go b/drivers/shared/executor/executor_linux.go index 54be7ed79f7..123284ceda5 100644 --- a/drivers/shared/executor/executor_linux.go +++ b/drivers/shared/executor/executor_linux.go @@ -696,8 +696,9 @@ func configureCgroups(cfg *lconfigs.Config, command *ExecCommand) error { return fmt.Errorf("resources.Cpu.CpuShares must be equal to or greater than 2: %v", cpuShares) } - // Set the relative CPU shares for this cgroup. + // Set the relative CPU shares for this cgroup, and convert for cgroupv2 cfg.Cgroups.Resources.CpuShares = uint64(cpuShares) + cfg.Cgroups.Resources.CpuWeight = cgroups.ConvertCPUSharesToCgroupV2Value(uint64(cpuShares)) if command.Resources.LinuxResources != nil && command.Resources.LinuxResources.CpusetCgroupPath != "" { cfg.Hooks = lconfigs.Hooks{