Skip to content

Commit 14970f4

Browse files
committed
support cpu idle and burst for cgroupv2
Signed-off-by: zouyee <zouyee1989@gmail.com>
1 parent 1a0f8bf commit 14970f4

File tree

7 files changed

+55
-7
lines changed

7 files changed

+55
-7
lines changed

cgroup2/cpu.go

+14
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ func NewCPUMax(quota *int64, period *uint64) CPUMax {
3434

3535
type CPU struct {
3636
Weight *uint64
37+
Idle *uint64
38+
Burst *uint64
3739
Max CPUMax
3840
Cpus string
3941
Mems string
@@ -61,12 +63,24 @@ func (r *CPU) Values() (o []Value) {
6163
value: *r.Weight,
6264
})
6365
}
66+
if r.Idle != nil {
67+
o = append(o, Value{
68+
filename: "cpu.idle",
69+
value: *r.Idle,
70+
})
71+
}
6472
if r.Max != "" {
6573
o = append(o, Value{
6674
filename: "cpu.max",
6775
value: r.Max,
6876
})
6977
}
78+
if r.Burst != nil {
79+
o = append(o, Value{
80+
filename: "cpu.max.burst",
81+
value: *r.Burst,
82+
})
83+
}
7084
if r.Cpus != "" {
7185
o = append(o, Value{
7286
filename: "cpuset.cpus",

cgroup2/cpuv2_test.go

+26
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,14 @@ func TestCgroupv2CpuStats(t *testing.T) {
3535
quota int64 = 10000
3636
period uint64 = 8000
3737
weight uint64 = 100
38+
// The burst in the range [0, $quota].
39+
burst uint64 = 1000
3840
)
3941
max := "10000 8000"
4042
res := Resources{
4143
CPU: &CPU{
4244
Weight: &weight,
45+
Burst: &burst,
4346
Max: NewCPUMax(&quota, &period),
4447
Cpus: "0",
4548
Mems: "0",
@@ -53,10 +56,33 @@ func TestCgroupv2CpuStats(t *testing.T) {
5356

5457
checkFileContent(t, c.path, "cpu.weight", strconv.FormatUint(weight, 10))
5558
checkFileContent(t, c.path, "cpu.max", max)
59+
checkFileContent(t, c.path, "cpu.max.burst", strconv.FormatUint(burst, 10))
5660
checkFileContent(t, c.path, "cpuset.cpus", "0")
5761
checkFileContent(t, c.path, "cpuset.mems", "0")
5862
}
5963

64+
func TestCgroupv2CpuIdle(t *testing.T) {
65+
checkCgroupMode(t)
66+
group := "/cpu-test-cg-idle"
67+
groupPath := fmt.Sprintf("%s-%d", group, os.Getpid())
68+
var (
69+
idle uint64 = 1
70+
)
71+
res := Resources{
72+
CPU: &CPU{
73+
Idle: &idle,
74+
},
75+
}
76+
c, err := NewManager(defaultCgroup2Path, groupPath, &res)
77+
require.NoError(t, err, "failed to init new cgroup manager")
78+
t.Cleanup(func() {
79+
os.Remove(c.path)
80+
})
81+
82+
checkFileContent(t, c.path, "cpu.weight", "0")
83+
checkFileContent(t, c.path, "cpu.idle", "1")
84+
}
85+
6086
func TestSystemdCgroupCpuController(t *testing.T) {
6187
checkCgroupMode(t)
6288
group := fmt.Sprintf("testing-cpu-%d.scope", os.Getpid())

cgroup2/manager.go

+10-3
Original file line numberDiff line numberDiff line change
@@ -849,9 +849,16 @@ func NewSystemd(slice, group string, pid int, resources *Resources) (*Manager, e
849849
newSystemdProperty("MemoryMax", uint64(*resources.Memory.Max)))
850850
}
851851

852-
if resources.CPU != nil && resources.CPU.Weight != nil && *resources.CPU.Weight != 0 {
853-
properties = append(properties,
854-
newSystemdProperty("CPUWeight", *resources.CPU.Weight))
852+
if resources.CPU != nil {
853+
// Do not add duplicate CPUWeight property
854+
if resources.CPU.Idle != nil && *resources.CPU.Weight != 0 {
855+
properties = append(properties,
856+
newSystemdProperty("CPUWeight", uint64(0)))
857+
}
858+
if resources.CPU.Weight != nil && *resources.CPU.Weight != 0 {
859+
properties = append(properties,
860+
newSystemdProperty("CPUWeight", *resources.CPU.Weight))
861+
}
855862
}
856863

857864
if resources.CPU != nil && resources.CPU.Max != "" {

cmd/go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ require (
1515
github.com/coreos/go-systemd/v22 v22.3.2 // indirect
1616
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d // indirect
1717
github.com/godbus/dbus/v5 v5.0.4 // indirect
18-
github.com/opencontainers/runtime-spec v1.0.2 // indirect
18+
github.com/opencontainers/runtime-spec v1.1.1-0.20230823135140-4fec88fd00a4 // indirect
1919
github.com/russross/blackfriday/v2 v2.0.1 // indirect
2020
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
2121
golang.org/x/exp v0.0.0-20230224173230-c95f2b4c22f2 // indirect

cmd/go.sum

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
1818
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
1919
github.com/opencontainers/runtime-spec v1.0.2 h1:UfAcuLBJB9Coz72x1hgl8O5RVzTdNiaglX6v2DM6FI0=
2020
github.com/opencontainers/runtime-spec v1.0.2/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/C5hL+LL3ko9hs6T5lQ0=
21+
github.com/opencontainers/runtime-spec v1.1.1-0.20230823135140-4fec88fd00a4/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/C5hL+LL3ko9hs6T5lQ0=
2122
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
2223
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
2324
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ require (
77
github.com/coreos/go-systemd/v22 v22.3.2
88
github.com/docker/go-units v0.4.0
99
github.com/godbus/dbus/v5 v5.0.4
10-
github.com/opencontainers/runtime-spec v1.0.2
10+
github.com/opencontainers/runtime-spec v1.1.1-0.20230823135140-4fec88fd00a4
1111
github.com/sirupsen/logrus v1.9.0
1212
github.com/stretchr/testify v1.8.0
1313
go.uber.org/goleak v1.1.12

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
1818
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
1919
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
2020
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
21-
github.com/opencontainers/runtime-spec v1.0.2 h1:UfAcuLBJB9Coz72x1hgl8O5RVzTdNiaglX6v2DM6FI0=
22-
github.com/opencontainers/runtime-spec v1.0.2/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/C5hL+LL3ko9hs6T5lQ0=
21+
github.com/opencontainers/runtime-spec v1.1.1-0.20230823135140-4fec88fd00a4 h1:EctkgBjZ1y4q+sibyuuIgiKpa0QSd2elFtSSdNvBVow=
22+
github.com/opencontainers/runtime-spec v1.1.1-0.20230823135140-4fec88fd00a4/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/C5hL+LL3ko9hs6T5lQ0=
2323
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
2424
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
2525
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=

0 commit comments

Comments
 (0)