From 06d7c1d2616052974ebb39a403a31e4ff95b9a20 Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Wed, 20 May 2020 01:37:03 -0700 Subject: [PATCH] systemd+cgroupv1: fix updating CPUQuotaPerSecUSec 1. do not allow to set quota without period or period without quota, as we won't be able to calculate new value for CPUQuotaPerSecUSec otherwise. 2. do not ignore setting quota to -1 when a period is not set. 3. update the test case accordingly. Note that systemd value checks will be added in the next commit. Signed-off-by: Kir Kolyshkin --- libcontainer/cgroups/systemd/v1.go | 14 +++++++++++++- tests/integration/update.bats | 25 +++++++++++++++++-------- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/libcontainer/cgroups/systemd/v1.go b/libcontainer/cgroups/systemd/v1.go index d5bc17139..a805f72c2 100644 --- a/libcontainer/cgroups/systemd/v1.go +++ b/libcontainer/cgroups/systemd/v1.go @@ -4,6 +4,7 @@ package systemd import ( "errors" + "fmt" "io/ioutil" "math" "os" @@ -89,7 +90,18 @@ func genV1ResourcesProperties(c *configs.Cgroup) ([]systemdDbus.Property, error) } // cpu.cfs_quota_us and cpu.cfs_period_us are controlled by systemd. - if c.Resources.CpuQuota != 0 && c.Resources.CpuPeriod != 0 { + if c.Resources.CpuQuota != 0 || c.Resources.CpuPeriod != 0 { + if c.Resources.CpuQuota < -1 { + return nil, fmt.Errorf("Invalid CPU quota value: %d", c.Resources.CpuQuota) + } + if c.Resources.CpuQuota != -1 { + if c.Resources.CpuQuota == 0 || c.Resources.CpuPeriod == 0 { + return nil, errors.New("CPU quota and period should both be set") + } + if c.Resources.CpuPeriod < 0 { + return nil, fmt.Errorf("Invalid CPU period value: %d", c.Resources.CpuPeriod) + } + } // corresponds to USEC_INFINITY in systemd // if USEC_INFINITY is provided, CPUQuota is left unbound by systemd // always setting a property value ensures we can apply a quota and remove it later diff --git a/tests/integration/update.bats b/tests/integration/update.bats index fab94abb5..265941745 100644 --- a/tests/integration/update.bats +++ b/tests/integration/update.bats @@ -252,15 +252,24 @@ EOF check_cgroup_value "cpu.cfs_quota_us" 500000 check_cgroup_value "cpu.shares" 100 - # update cpu-period - runc update test_update --cpu-period 900000 - [ "$status" -eq 0 ] - check_cgroup_value "cpu.cfs_period_us" 900000 + # systemd driver does not allow to update quota and period separately + if [ -z "$RUNC_USE_SYSTEMD" ]; then + # update cpu period + runc update test_update --cpu-period 900000 + [ "$status" -eq 0 ] + check_cgroup_value "cpu.cfs_period_us" 900000 - # update cpu-quota - runc update test_update --cpu-quota 600000 - [ "$status" -eq 0 ] - check_cgroup_value "cpu.cfs_quota_us" 600000 + # update cpu quota + runc update test_update --cpu-quota 600000 + [ "$status" -eq 0 ] + check_cgroup_value "cpu.cfs_quota_us" 600000 + else + # update cpu quota and period together + runc update test_update --cpu-period 900000 --cpu-quota 600000 + [ "$status" -eq 0 ] + check_cgroup_value "cpu.cfs_period_us" 900000 + check_cgroup_value "cpu.cfs_quota_us" 600000 + fi # update cpu-shares runc update test_update --cpu-share 200