Skip to content

Commit

Permalink
executor: set CpuWeight in cgroup-v2 (#11287)
Browse files Browse the repository at this point in the history
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
.
  • Loading branch information
Mahmood Ali authored and lgfa29 committed Nov 15, 2021
1 parent 4b52ea3 commit 618e5a8
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
4 changes: 4 additions & 0 deletions .changelog/11287.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

```release-note:bug
driver/exec: Set CPU resource limits when cgroup-v2 is enabled
```
3 changes: 2 additions & 1 deletion drivers/shared/executor/executor_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -687,8 +687,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))

return nil
}
Expand Down

0 comments on commit 618e5a8

Please sign in to comment.