forked from opencontainers/runc
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
4 changed files
with
81 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters