cgroup: add pids.peak for cgroup v2 kernel >= 6.1

This commit is contained in:
criyle 2025-02-20 01:51:42 +00:00
parent 4154f44d83
commit 0b6b557947
5 changed files with 23 additions and 2 deletions

View File

@ -183,8 +183,9 @@ type Environment interface {
## Kernel Versions
- 6.1: `pids.peak` in cgroup v2
- 5.19: `memory.peak` in cgroup v2
- 4.15: cgroup v2
- 4.15: cgroup v2 (also need support in the Linux distribution)
- 4.14: SECCOMP_RET_KILL_PROCESS
- 4.6: CLONE_NEWCGROUP
- 3.19: execveat()

View File

@ -417,7 +417,11 @@ func start() (*runner.Result, error) {
if err != nil && !errors.Is(err, os.ErrNotExist) {
return nil, fmt.Errorf("cgroup memory: %v", err)
}
debug("cgroup: cpu: ", cpu, " memory: ", memory)
procPeak, err := cg.ProcessPeak()
if err != nil && !errors.Is(err, os.ErrNotExist) {
return nil, fmt.Errorf("cgroup pid: %v", err)
}
debug("cgroup: cpu: ", cpu, " memory: ", memory, " procPeak: ", procPeak)
rt.Time = time.Duration(cpu)
if memory > 0 {
rt.Memory = runner.Size(memory)

View File

@ -33,6 +33,9 @@ type Cgroup interface {
// MemoryMaxUsageInBytes reads max total memory usage. Not exist in cgroup v2 with kernel version < 5.19
MemoryMaxUsage() (uint64, error)
// ProcessPeak reads maximum number of process ever existed in cgroup. Not exist in cgroup v1 or kernel < 6.1
ProcessPeak() (uint64, error)
// SetCPUBandwidth sets the cpu bandwidth. Times in ns
SetCPUBandwidth(quota, period uint64) error

View File

@ -181,6 +181,11 @@ func (c *V1) MemoryMaxUsage() (uint64, error) {
return c.memory.ReadUint("memory.max_usage_in_bytes")
}
// ProcessPeak implements Cgroup.
func (c *V1) ProcessPeak() (uint64, error) {
return 0, ErrNotInitialized
}
// SetMemoryLimit write memory.limit_in_bytes
func (c *V1) SetMemoryLimit(i uint64) error {
return c.memory.WriteUint("memory.limit_in_bytes", i)

View File

@ -155,6 +155,14 @@ func (c *V2) MemoryMaxUsage() (uint64, error) {
return c.ReadUint("memory.peak")
}
// ProcessPeak reads pids.peak
func (c *V2) ProcessPeak() (uint64, error) {
if !c.control.Pids {
return 0, ErrNotInitialized
}
return c.ReadUint("pids.peak")
}
// SetCPUBandwidth set cpu.max quota period
func (c *V2) SetCPUBandwidth(quota, period uint64) error {
if !c.control.CPU {