Skip to content

Commit

Permalink
feat: add cpu max burst support
Browse files Browse the repository at this point in the history
Signed-off-by: Lucas Jacques <[email protected]>
  • Loading branch information
lucas-jacques committed Dec 5, 2024
1 parent d4e976d commit 5ed586d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
20 changes: 16 additions & 4 deletions cgroup2/cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,16 @@ func NewCPUMax(quota *int64, period *uint64) CPUMax {
return CPUMax(strings.Join([]string{max, strconv.FormatUint(*period, 10)}, " "))
}

func NewCPUMaxBurst(burst uint64) string {
return strconv.FormatUint(burst, 10)
}

type CPU struct {
Weight *uint64
Max CPUMax
Cpus string
Mems string
Weight *uint64
Max CPUMax
MaxBurst string
Cpus string
Mems string
}

func (c CPUMax) extractQuotaAndPeriod() (int64, uint64) {
Expand Down Expand Up @@ -79,5 +84,12 @@ func (r *CPU) Values() (o []Value) {
value: r.Mems,
})
}
if r.MaxBurst != "" {
o = append(o, Value{
filename: "cpu.max.burst",
value: r.MaxBurst,
})
}

return o
}
11 changes: 7 additions & 4 deletions cgroup2/cpuv2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,19 @@ func TestCgroupv2CpuStats(t *testing.T) {
group := "/cpu-test-cg"
groupPath := fmt.Sprintf("%s-%d", group, os.Getpid())
var (
burst uint64 = 1000
quota int64 = 10000
period uint64 = 8000
weight uint64 = 100
)

c, err := NewManager(defaultCgroup2Path, groupPath, &Resources{
CPU: &CPU{
Weight: &weight,
Max: NewCPUMax(&quota, &period),
Cpus: "0",
Mems: "0",
Weight: &weight,
Max: NewCPUMax(&quota, &period),
Cpus: "0",
Mems: "0",
MaxBurst: NewCPUMaxBurst(burst),
},
})
require.NoError(t, err, "failed to init new cgroup manager")
Expand All @@ -54,6 +56,7 @@ func TestCgroupv2CpuStats(t *testing.T) {
checkFileContent(t, c.path, "cpu.max", "10000 8000")
checkFileContent(t, c.path, "cpuset.cpus", "0")
checkFileContent(t, c.path, "cpuset.mems", "0")
checkFileContent(t, c.path, "cpu.max.burst", strconv.FormatUint(burst, 10))
}

func TestSystemdCgroupCpuController(t *testing.T) {
Expand Down
3 changes: 3 additions & 0 deletions cgroup2/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,9 @@ func ToResources(spec *specs.LinuxResources) *Resources {
if period := cpu.Period; period != nil {
resources.CPU.Max = NewCPUMax(cpu.Quota, period)
}
if burst := cpu.Burst; burst != nil {
resources.CPU.MaxBurst = NewCPUMaxBurst(*burst)
}
}
if mem := spec.Memory; mem != nil {
resources.Memory = &Memory{}
Expand Down

0 comments on commit 5ed586d

Please sign in to comment.