Skip to content

Commit 7d4bac6

Browse files
committed
merge branch 'pr-3133'
Kir Kolyshkin (3): libct/cg: GetAllPids: optimize for go 1.16+ libct/cg: improve GetAllPids and readProcsFile libct/cg: move GetAllPids out of utils.go LGTMs: AkihiroSuda cyphar Closes opencontainers#3133
2 parents 8772c4d + d0c3bc4 commit 7d4bac6

File tree

4 files changed

+81
-25
lines changed

4 files changed

+81
-25
lines changed

libcontainer/cgroups/getallpids.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// +build linux,go1.16
2+
3+
package cgroups
4+
5+
import (
6+
"io/fs"
7+
"path/filepath"
8+
)
9+
10+
// GetAllPids returns all pids from the cgroup identified by path, and all its
11+
// sub-cgroups.
12+
func GetAllPids(path string) ([]int, error) {
13+
var pids []int
14+
err := filepath.WalkDir(path, func(p string, d fs.DirEntry, iErr error) error {
15+
if iErr != nil {
16+
return iErr
17+
}
18+
if !d.IsDir() {
19+
return nil
20+
}
21+
cPids, err := readProcsFile(p)
22+
if err != nil {
23+
return err
24+
}
25+
pids = append(pids, cPids...)
26+
return nil
27+
})
28+
return pids, err
29+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// +build linux,!go1.16
2+
3+
package cgroups
4+
5+
import (
6+
"os"
7+
"path/filepath"
8+
)
9+
10+
// GetAllPids returns all pids, that were added to cgroup at path and to all its
11+
// subcgroups.
12+
func GetAllPids(path string) ([]int, error) {
13+
var pids []int
14+
// collect pids from all sub-cgroups
15+
err := filepath.Walk(path, func(p string, info os.FileInfo, iErr error) error {
16+
if iErr != nil {
17+
return iErr
18+
}
19+
if !info.IsDir() {
20+
return nil
21+
}
22+
cPids, err := readProcsFile(p)
23+
if err != nil {
24+
return err
25+
}
26+
pids = append(pids, cPids...)
27+
return nil
28+
})
29+
return pids, err
30+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// +build linux
2+
3+
package cgroups
4+
5+
import (
6+
"testing"
7+
)
8+
9+
func BenchmarkGetAllPids(b *testing.B) {
10+
total := 0
11+
for i := 0; i < b.N; i++ {
12+
i, err := GetAllPids("/sys/fs/cgroup")
13+
if err != nil {
14+
b.Fatal(err)
15+
}
16+
total += len(i)
17+
}
18+
b.Logf("iter: %d, total: %d", b.N, total)
19+
}

libcontainer/cgroups/utils.go

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@ func GetAllSubsystems() ([]string, error) {
118118
return subsystems, nil
119119
}
120120

121-
func readProcsFile(file string) ([]int, error) {
122-
f, err := os.Open(file)
121+
func readProcsFile(dir string) ([]int, error) {
122+
f, err := OpenFile(dir, CgroupProcesses, os.O_RDONLY)
123123
if err != nil {
124124
return nil, err
125125
}
@@ -336,29 +336,7 @@ func getHugePageSizeFromFilenames(fileNames []string) ([]string, error) {
336336

337337
// GetPids returns all pids, that were added to cgroup at path.
338338
func GetPids(dir string) ([]int, error) {
339-
return readProcsFile(filepath.Join(dir, CgroupProcesses))
340-
}
341-
342-
// GetAllPids returns all pids, that were added to cgroup at path and to all its
343-
// subcgroups.
344-
func GetAllPids(path string) ([]int, error) {
345-
var pids []int
346-
// collect pids from all sub-cgroups
347-
err := filepath.Walk(path, func(p string, info os.FileInfo, iErr error) error {
348-
if iErr != nil {
349-
return iErr
350-
}
351-
if info.IsDir() || info.Name() != CgroupProcesses {
352-
return nil
353-
}
354-
cPids, err := readProcsFile(p)
355-
if err != nil {
356-
return err
357-
}
358-
pids = append(pids, cPids...)
359-
return nil
360-
})
361-
return pids, err
339+
return readProcsFile(dir)
362340
}
363341

364342
// WriteCgroupProc writes the specified pid into the cgroup's cgroup.procs file

0 commit comments

Comments
 (0)