Skip to content

Commit

Permalink
util: support systems using the new cgroup v2 structure
Browse files Browse the repository at this point in the history
With cgroup v2, the location of the pids.max file changed and so did the
/proc/self/cgroup file

new /proc/self/cgroup file
`
0::/user.slice/user-500.slice/session-14.scope
`

old file:
`
11:pids:/user.slice/user-500.slice/session-2.scope
10:blkio:/user.slice
9:net_cls,net_prio:/
8:perf_event:/
...
`

There is no directory per subsystem (e.g. /sys/fs/cgroup/pids) any more, all
files are now in one directory.

fixes: ceph#3085

Signed-off-by: Marcus Röder <m.roeder@yieldlab.de>
  • Loading branch information
Marcus Röder authored and humblec committed May 9, 2022
1 parent 4011626 commit ebaf19f
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions internal/util/pidlimit.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,32 +27,44 @@ import (
)

const (
procCgroup = "/proc/self/cgroup"
sysPidsMaxFmt = "/sys/fs/cgroup/pids%s/pids.max"
procCgroup = "/proc/self/cgroup"
sysPidsMaxFmtCgroupV1 = "/sys/fs/cgroup/pids%s/pids.max"
sysPidsMaxFmtCgroupV2 = "/sys/fs/cgroup%s/pids.max"
)

// return the cgouprs "pids.max" file of the current process
//
// find the line containing the pids group from the /proc/self/cgroup file
// $ grep 'pids' /proc/self/cgroup
// getCgroupPidsFile return the cgroups "pids.max" file of the
// current process
// For cgroup v1, find the line containing the pids group from the /proc/self/cgroup file
// $ grep ':pids:' /proc/self/cgroup
// 7:pids:/kubepods.slice/kubepods-besteffort.slice/....scope
// $ cat /sys/fs/cgroup/pids + *.scope + /pids.max.
// The entry for cgroup v2 is always in the format "0::...scope", no subsystem given.
// (see https://www.kernel.org/doc/Documentation/cgroup-v2.txt)
func getCgroupPidsFile() (string, error) {
cgroup, err := os.Open(procCgroup)
if err != nil {
return "", err
}
defer cgroup.Close() // #nosec: error on close is not critical here

pidsMax := ""
scanner := bufio.NewScanner(cgroup)
var slice string
for scanner.Scan() {
parts := strings.SplitN(scanner.Text(), ":", 3)
if parts == nil || len(parts) < 3 {
continue
}
// No cgroup subsystem given, then it is cgroupv2
if parts[0] == "0" && parts[1] == "" {
slice = parts[2]
pidsMax = fmt.Sprintf(sysPidsMaxFmtCgroupV2, slice)

break
}
if parts[1] == "pids" {
slice = parts[2]
pidsMax = fmt.Sprintf(sysPidsMaxFmtCgroupV1, slice)

break
}
Expand All @@ -61,8 +73,6 @@ func getCgroupPidsFile() (string, error) {
return "", fmt.Errorf("could not find a cgroup for 'pids'")
}

pidsMax := fmt.Sprintf(sysPidsMaxFmt, slice)

return pidsMax, nil
}

Expand Down

0 comments on commit ebaf19f

Please sign in to comment.