diff --git a/cgroup2/cpu.go b/cgroup2/cpu.go index dcb253d..74d50a1 100644 --- a/cgroup2/cpu.go +++ b/cgroup2/cpu.go @@ -34,6 +34,8 @@ func NewCPUMax(quota *int64, period *uint64) CPUMax { type CPU struct { Weight *uint64 + Idle *uint64 + Burst *uint64 Max CPUMax Cpus string Mems string @@ -61,12 +63,24 @@ func (r *CPU) Values() (o []Value) { value: *r.Weight, }) } + if r.Idle != nil { + o = append(o, Value{ + filename: "cpu.idle", + value: *r.Idle, + }) + } if r.Max != "" { o = append(o, Value{ filename: "cpu.max", value: r.Max, }) } + if r.Burst != nil { + o = append(o, Value{ + filename: "cpu.max.burst", + value: *r.Burst, + }) + } if r.Cpus != "" { o = append(o, Value{ filename: "cpuset.cpus", diff --git a/cgroup2/cpuv2_test.go b/cgroup2/cpuv2_test.go index 30450f3..65a19fb 100644 --- a/cgroup2/cpuv2_test.go +++ b/cgroup2/cpuv2_test.go @@ -35,11 +35,14 @@ func TestCgroupv2CpuStats(t *testing.T) { quota int64 = 10000 period uint64 = 8000 weight uint64 = 100 + // The burst in the range [0, $quota]. + burst uint64 = 1000 ) max := "10000 8000" res := Resources{ CPU: &CPU{ Weight: &weight, + Burst: &burst, Max: NewCPUMax("a, &period), Cpus: "0", Mems: "0", @@ -53,10 +56,33 @@ func TestCgroupv2CpuStats(t *testing.T) { checkFileContent(t, c.path, "cpu.weight", strconv.FormatUint(weight, 10)) checkFileContent(t, c.path, "cpu.max", max) + checkFileContent(t, c.path, "cpu.max.burst", strconv.FormatUint(burst, 10)) checkFileContent(t, c.path, "cpuset.cpus", "0") checkFileContent(t, c.path, "cpuset.mems", "0") } +func TestCgroupv2CpuIdle(t *testing.T) { + checkCgroupMode(t) + group := "/cpu-test-cg-idle" + groupPath := fmt.Sprintf("%s-%d", group, os.Getpid()) + var ( + idle uint64 = 1 + ) + res := Resources{ + CPU: &CPU{ + Idle: &idle, + }, + } + c, err := NewManager(defaultCgroup2Path, groupPath, &res) + require.NoError(t, err, "failed to init new cgroup manager") + t.Cleanup(func() { + os.Remove(c.path) + }) + + checkFileContent(t, c.path, "cpu.weight", "0") + checkFileContent(t, c.path, "cpu.idle", "1") +} + func TestSystemdCgroupCpuController(t *testing.T) { checkCgroupMode(t) group := fmt.Sprintf("testing-cpu-%d.scope", os.Getpid()) diff --git a/cgroup2/manager.go b/cgroup2/manager.go index 8bd2612..671564b 100644 --- a/cgroup2/manager.go +++ b/cgroup2/manager.go @@ -849,9 +849,16 @@ func NewSystemd(slice, group string, pid int, resources *Resources) (*Manager, e newSystemdProperty("MemoryMax", uint64(*resources.Memory.Max))) } - if resources.CPU != nil && resources.CPU.Weight != nil && *resources.CPU.Weight != 0 { - properties = append(properties, - newSystemdProperty("CPUWeight", *resources.CPU.Weight)) + if resources.CPU != nil { + // Do not add duplicate CPUWeight property + if resources.CPU.Idle != nil && *resources.CPU.Weight != 0 { + properties = append(properties, + newSystemdProperty("CPUWeight", uint64(0))) + } + if resources.CPU.Weight != nil && *resources.CPU.Weight != 0 { + properties = append(properties, + newSystemdProperty("CPUWeight", *resources.CPU.Weight)) + } } if resources.CPU != nil && resources.CPU.Max != "" { diff --git a/cmd/go.mod b/cmd/go.mod index 3425402..a68a834 100644 --- a/cmd/go.mod +++ b/cmd/go.mod @@ -15,7 +15,7 @@ require ( github.com/coreos/go-systemd/v22 v22.3.2 // indirect github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d // indirect github.com/godbus/dbus/v5 v5.0.4 // indirect - github.com/opencontainers/runtime-spec v1.0.2 // indirect + github.com/opencontainers/runtime-spec v1.1.1-0.20230823135140-4fec88fd00a4 // indirect github.com/russross/blackfriday/v2 v2.0.1 // indirect github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect golang.org/x/exp v0.0.0-20230224173230-c95f2b4c22f2 // indirect diff --git a/cmd/go.sum b/cmd/go.sum index 9cc1ec4..40f7c7a 100644 --- a/cmd/go.sum +++ b/cmd/go.sum @@ -16,8 +16,8 @@ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= -github.com/opencontainers/runtime-spec v1.0.2 h1:UfAcuLBJB9Coz72x1hgl8O5RVzTdNiaglX6v2DM6FI0= -github.com/opencontainers/runtime-spec v1.0.2/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/C5hL+LL3ko9hs6T5lQ0= +github.com/opencontainers/runtime-spec v1.1.1-0.20230823135140-4fec88fd00a4 h1:EctkgBjZ1y4q+sibyuuIgiKpa0QSd2elFtSSdNvBVow= +github.com/opencontainers/runtime-spec v1.1.1-0.20230823135140-4fec88fd00a4/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/C5hL+LL3ko9hs6T5lQ0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8= diff --git a/go.mod b/go.mod index 0290cda..9086e0e 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,7 @@ require ( github.com/coreos/go-systemd/v22 v22.3.2 github.com/docker/go-units v0.4.0 github.com/godbus/dbus/v5 v5.0.4 - github.com/opencontainers/runtime-spec v1.0.2 + github.com/opencontainers/runtime-spec v1.1.1-0.20230823135140-4fec88fd00a4 github.com/sirupsen/logrus v1.9.0 github.com/stretchr/testify v1.8.0 go.uber.org/goleak v1.1.12 diff --git a/go.sum b/go.sum index 2d8a793..fa779cf 100644 --- a/go.sum +++ b/go.sum @@ -18,8 +18,8 @@ github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= -github.com/opencontainers/runtime-spec v1.0.2 h1:UfAcuLBJB9Coz72x1hgl8O5RVzTdNiaglX6v2DM6FI0= -github.com/opencontainers/runtime-spec v1.0.2/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/C5hL+LL3ko9hs6T5lQ0= +github.com/opencontainers/runtime-spec v1.1.1-0.20230823135140-4fec88fd00a4 h1:EctkgBjZ1y4q+sibyuuIgiKpa0QSd2elFtSSdNvBVow= +github.com/opencontainers/runtime-spec v1.1.1-0.20230823135140-4fec88fd00a4/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/C5hL+LL3ko9hs6T5lQ0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=