Skip to content

Commit

Permalink
support cpu idle and burst for cgroupv2
Browse files Browse the repository at this point in the history
Signed-off-by: zouyee <[email protected]>
  • Loading branch information
zouyee committed Jan 5, 2024
1 parent 1a0f8bf commit 871ed34
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 7 deletions.
14 changes: 14 additions & 0 deletions cgroup2/cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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",
Expand Down
26 changes: 26 additions & 0 deletions cgroup2/cpuv2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(&quota, &period),
Cpus: "0",
Mems: "0",
Expand All @@ -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())
Expand Down
13 changes: 10 additions & 3 deletions cgroup2/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "" {
Expand Down
2 changes: 1 addition & 1 deletion cmd/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions cmd/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ 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/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=
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit 871ed34

Please sign in to comment.