Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support cpu idle and burst for cgroupv2 #323

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
)

c, err := NewManager(defaultCgroup2Path, groupPath, &Resources{
CPU: &CPU{
Weight: &weight,
Burst: &burst,
Max: NewCPUMax(&quota, &period),
Cpus: "0",
Mems: "0",
Expand All @@ -51,11 +54,34 @@ func TestCgroupv2CpuStats(t *testing.T) {
})

checkFileContent(t, c.path, "cpu.weight", strconv.FormatUint(weight, 10))
checkFileContent(t, c.path, "cpu.max.burst", strconv.FormatUint(burst, 10))
checkFileContent(t, c.path, "cpu.max", "10000 8000")
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
17 changes: 14 additions & 3 deletions cgroup2/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -869,9 +869,20 @@ 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 {
// Systemd v252 added support for setting cgroup v2 cpu.idle in systemd/systemd#23299
// The way it works is
// if CPUWeight == 0, cpu.idle is set to 1;
// if CPUWeight != 0, cpu.idle is set to 0.
// Do not add duplicate CPUWeight property
if resources.CPU.Idle != nil && resources.CPU.Weight != nil && *resources.CPU.Weight != 0 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not following this logic.
Right now if resources.CPU.Idle is set and resources.CPU.Weight is set to a non-zero value then we end up with CPUWeight being set twice.

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