diff --git a/libcontainer/cgroups/systemd/v2.go b/libcontainer/cgroups/systemd/v2.go index a2cd70d6d..a2a2836cb 100644 --- a/libcontainer/cgroups/systemd/v2.go +++ b/libcontainer/cgroups/systemd/v2.go @@ -85,6 +85,14 @@ func unifiedResToSystemdProps(conn *systemdDbus.Conn, res map[string]string) (pr } addCpuQuota(conn, &props, quota, period) + case "cpu.weight": + num, err := strconv.ParseUint(v, 10, 64) + if err != nil { + return nil, fmt.Errorf("unified resource %q value conversion error: %w", k, err) + } + props = append(props, + newProp("CPUWeight", num)) + case "pids.max": num := uint64(math.MaxUint64) if v != "max" { diff --git a/tests/integration/cgroups.bats b/tests/integration/cgroups.bats index 54dea5257..aac507de8 100644 --- a/tests/integration/cgroups.bats +++ b/tests/integration/cgroups.bats @@ -200,7 +200,8 @@ function setup() { "memory.high": "5242880", "memory.max": "10485760", "pids.max": "99", - "cpu.max": "10000 100000" + "cpu.max": "10000 100000", + "cpu.weight": "42" }' "$BUSYBOX_BUNDLE" runc run -d --console-socket "$CONSOLE_SOCKET" test_cgroups_unified @@ -219,15 +220,18 @@ function setup() { check_systemd_value "TasksMax" 99 check_cpu_quota 10000 100000 "100ms" + check_cpu_weight 42 } @test "runc run (cgroup v2 resources.unified override)" { requires root cgroups_v2 set_cgroups_path "$BUSYBOX_BUNDLE" + # CPU shares of 3333 corresponds to CPU weight of 128. update_config ' .linux.resources.memory |= {"limit": 33554432} | .linux.resources.memorySwap |= {"limit": 33554432} | .linux.resources.cpu |= { + "shares": 3333, "quota": 40000, "period": 100000 } @@ -235,7 +239,8 @@ function setup() { "memory.min": "131072", "memory.max": "10485760", "pids.max": "42", - "cpu.max": "5000 50000" + "cpu.max": "5000 50000", + "cpu.weight": "42" }' "$BUSYBOX_BUNDLE" runc run -d --console-socket "$CONSOLE_SOCKET" test_cgroups_unified @@ -255,4 +260,6 @@ function setup() { check_systemd_value "TasksMax" 42 check_cpu_quota 5000 50000 "100ms" + + check_cpu_weight 42 } diff --git a/tests/integration/helpers.bash b/tests/integration/helpers.bash index 244c86f01..c4c13b00c 100644 --- a/tests/integration/helpers.bash +++ b/tests/integration/helpers.bash @@ -235,19 +235,27 @@ function check_cpu_quota() { check_systemd_value "CPUQuotaPeriodUSec" $sd_period $sd_infinity } +# Works for cgroup v1 and v2, accepts v1 shares as an argument. function check_cpu_shares() { local shares=$1 if [ "$CGROUP_UNIFIED" = "yes" ]; then local weight=$((1 + ((shares - 2) * 9999) / 262142)) - check_cgroup_value "cpu.weight" $weight - check_systemd_value "CPUWeight" $weight + check_cpu_weight "$weight" else check_cgroup_value "cpu.shares" "$shares" check_systemd_value "CPUShares" "$shares" fi } +# Works only for cgroup v2, accept v2 weight. +function check_cpu_weight() { + local weight=$1 + + check_cgroup_value "cpu.weight" $weight + check_systemd_value "CPUWeight" $weight +} + # Helper function to set a resources limit function set_resources_limit() { bundle="${1:-.}" diff --git a/tests/integration/update.bats b/tests/integration/update.bats index eda2b7ad2..f98130a39 100644 --- a/tests/integration/update.bats +++ b/tests/integration/update.bats @@ -372,12 +372,15 @@ EOF # check that initial values were properly set check_cpu_quota 500000 1000000 "500ms" + # initial cpu shares of 100 corresponds to weight of 4 + check_cpu_weight 4 check_systemd_value "TasksMax" 20 runc update -r - test_update <