From ab80eb32d29fac03b5e9b04058a5a481491824e8 Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Tue, 3 Nov 2020 15:15:30 -0800 Subject: [PATCH] libct/cg/sd/v2: support cpu.max unified resource Signed-off-by: Kir Kolyshkin --- libcontainer/cgroups/systemd/v2.go | 31 ++++++++++++++++++++++++++++-- tests/integration/cgroups.bats | 10 +++++++++- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/libcontainer/cgroups/systemd/v2.go b/libcontainer/cgroups/systemd/v2.go index 09d8200be..a2cd70d6d 100644 --- a/libcontainer/cgroups/systemd/v2.go +++ b/libcontainer/cgroups/systemd/v2.go @@ -45,7 +45,9 @@ func NewUnifiedManager(config *configs.Cgroup, path string, rootless bool) cgrou // For the list of keys, see https://www.kernel.org/doc/Documentation/cgroup-v2.txt // // For the list of systemd unit properties, see systemd.resource-control(5). -func unifiedResToSystemdProps(res map[string]string) (props []systemdDbus.Property, _ error) { +func unifiedResToSystemdProps(conn *systemdDbus.Conn, res map[string]string) (props []systemdDbus.Property, _ error) { + var err error + for k, v := range res { if strings.Contains(k, "/") { return nil, fmt.Errorf("unified resource %q must be a file name (no slashes)", k) @@ -57,7 +59,32 @@ func unifiedResToSystemdProps(res map[string]string) (props []systemdDbus.Proper // Kernel is quite forgiving to extra whitespace // around the value, and so should we. v = strings.TrimSpace(v) + // Please keep cases in alphabetical order. switch k { + case "cpu.max": + // value: quota [period] + quota := int64(0) // 0 means "unlimited" for addCpuQuota, if period is set + period := defCPUQuotaPeriod + sv := strings.Fields(v) + if len(sv) < 1 || len(sv) > 2 { + return nil, fmt.Errorf("unified resource %q value invalid: %q", k, v) + } + // quota + if sv[0] != "max" { + quota, err = strconv.ParseInt(sv[0], 10, 64) + if err != nil { + return nil, fmt.Errorf("unified resource %q period value conversion error: %w", k, err) + } + } + // period + if len(sv) == 2 { + period, err = strconv.ParseUint(sv[1], 10, 64) + if err != nil { + return nil, fmt.Errorf("unified resource %q quota value conversion error: %w", k, err) + } + } + addCpuQuota(conn, &props, quota, period) + case "pids.max": num := uint64(math.MaxUint64) if v != "max" { @@ -131,7 +158,7 @@ func genV2ResourcesProperties(c *configs.Cgroup, conn *systemdDbus.Conn) ([]syst // convert Resources.Unified map to systemd properties if r.Unified != nil { - unifiedProps, err := unifiedResToSystemdProps(r.Unified) + unifiedProps, err := unifiedResToSystemdProps(conn, r.Unified) if err != nil { return nil, err } diff --git a/tests/integration/cgroups.bats b/tests/integration/cgroups.bats index bbd39ba0f..54dea5257 100644 --- a/tests/integration/cgroups.bats +++ b/tests/integration/cgroups.bats @@ -218,6 +218,7 @@ function setup() { echo "$output" | grep -q '^cpu.max:10000 100000$' check_systemd_value "TasksMax" 99 + check_cpu_quota 10000 100000 "100ms" } @test "runc run (cgroup v2 resources.unified override)" { @@ -226,10 +227,15 @@ function setup() { set_cgroups_path "$BUSYBOX_BUNDLE" update_config ' .linux.resources.memory |= {"limit": 33554432} | .linux.resources.memorySwap |= {"limit": 33554432} + | .linux.resources.cpu |= { + "quota": 40000, + "period": 100000 + } | .linux.resources.unified |= { "memory.min": "131072", "memory.max": "10485760", - "pids.max": "42" + "pids.max": "42", + "cpu.max": "5000 50000" }' "$BUSYBOX_BUNDLE" runc run -d --console-socket "$CONSOLE_SOCKET" test_cgroups_unified @@ -247,4 +253,6 @@ function setup() { [ "$status" -eq 0 ] [ "$output" = '42' ] check_systemd_value "TasksMax" 42 + + check_cpu_quota 5000 50000 "100ms" }