Skip to content

Commit

Permalink
merge branch 'pr-3133'
Browse files Browse the repository at this point in the history
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
  • Loading branch information
cyphar committed Aug 10, 2021
2 parents 8772c4d + d0c3bc4 commit 7d4bac6
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 25 deletions.
29 changes: 29 additions & 0 deletions libcontainer/cgroups/getallpids.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// +build linux,go1.16

package cgroups

import (
"io/fs"
"path/filepath"
)

// GetAllPids returns all pids from the cgroup identified by path, and all its
// sub-cgroups.
func GetAllPids(path string) ([]int, error) {
var pids []int
err := filepath.WalkDir(path, func(p string, d fs.DirEntry, iErr error) error {
if iErr != nil {
return iErr
}
if !d.IsDir() {
return nil
}
cPids, err := readProcsFile(p)
if err != nil {
return err
}
pids = append(pids, cPids...)
return nil
})
return pids, err
}
30 changes: 30 additions & 0 deletions libcontainer/cgroups/getallpids_go115.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// +build linux,!go1.16

package cgroups

import (
"os"
"path/filepath"
)

// GetAllPids returns all pids, that were added to cgroup at path and to all its
// subcgroups.
func GetAllPids(path string) ([]int, error) {
var pids []int
// collect pids from all sub-cgroups
err := filepath.Walk(path, func(p string, info os.FileInfo, iErr error) error {
if iErr != nil {
return iErr
}
if !info.IsDir() {
return nil
}
cPids, err := readProcsFile(p)
if err != nil {
return err
}
pids = append(pids, cPids...)
return nil
})
return pids, err
}
19 changes: 19 additions & 0 deletions libcontainer/cgroups/getallpids_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// +build linux

package cgroups

import (
"testing"
)

func BenchmarkGetAllPids(b *testing.B) {
total := 0
for i := 0; i < b.N; i++ {
i, err := GetAllPids("/sys/fs/cgroup")
if err != nil {
b.Fatal(err)
}
total += len(i)
}
b.Logf("iter: %d, total: %d", b.N, total)
}
28 changes: 3 additions & 25 deletions libcontainer/cgroups/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ func GetAllSubsystems() ([]string, error) {
return subsystems, nil
}

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

// GetPids returns all pids, that were added to cgroup at path.
func GetPids(dir string) ([]int, error) {
return readProcsFile(filepath.Join(dir, CgroupProcesses))
}

// GetAllPids returns all pids, that were added to cgroup at path and to all its
// subcgroups.
func GetAllPids(path string) ([]int, error) {
var pids []int
// collect pids from all sub-cgroups
err := filepath.Walk(path, func(p string, info os.FileInfo, iErr error) error {
if iErr != nil {
return iErr
}
if info.IsDir() || info.Name() != CgroupProcesses {
return nil
}
cPids, err := readProcsFile(p)
if err != nil {
return err
}
pids = append(pids, cPids...)
return nil
})
return pids, err
return readProcsFile(dir)
}

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

0 comments on commit 7d4bac6

Please sign in to comment.