Skip to content

Commit

Permalink
Merge pull request #312 from Rouzip/cgroup2_thread
Browse files Browse the repository at this point in the history
feat: add Threads for cgroupv2
  • Loading branch information
AkihiroSuda authored Dec 29, 2023
2 parents 00f6296 + 475935f commit 5e7266a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
23 changes: 16 additions & 7 deletions cgroup2/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"context"
"errors"
"fmt"
"io/fs"
"math"
"os"
"path/filepath"
Expand Down Expand Up @@ -478,9 +479,9 @@ func (c *Manager) Delete() error {
return remove(c.path)
}

func (c *Manager) Procs(recursive bool) ([]uint64, error) {
var processes []uint64
err := filepath.Walk(c.path, func(p string, info os.FileInfo, err error) error {
func (c *Manager) getTasks(recursive bool, tType string) ([]uint64, error) {
var tasks []uint64
err := filepath.Walk(c.path, func(p string, info fs.FileInfo, err error) error {
if err != nil {
return err
}
Expand All @@ -491,17 +492,25 @@ func (c *Manager) Procs(recursive bool) ([]uint64, error) {
return filepath.SkipDir
}
_, name := filepath.Split(p)
if name != cgroupProcs {
if name != tType {
return nil
}
procs, err := parseCgroupProcsFile(p)
curTasks, err := parseCgroupTasksFile(p)
if err != nil {
return err
}
processes = append(processes, procs...)
tasks = append(tasks, curTasks...)
return nil
})
return processes, err
return tasks, err
}

func (c *Manager) Procs(recursive bool) ([]uint64, error) {
return c.getTasks(recursive, cgroupProcs)
}

func (c *Manager) Threads(recursive bool) ([]uint64, error) {
return c.getTasks(recursive, cgroupThreads)
}

func (c *Manager) MoveTo(destination *Manager) error {
Expand Down
3 changes: 3 additions & 0 deletions cgroup2/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,9 @@ func TestKill(t *testing.T) {
pids, err := manager.Procs(true)
require.NoError(t, err)
require.Len(t, pids, numProcs, "pid count unexpected")
threads, err := manager.Threads(true)
require.NoError(t, err)
require.Len(t, threads, numProcs, "pid count unexpected")

// Now run kill, and check that nothing is running after.
err = manager.Kill()
Expand Down
5 changes: 3 additions & 2 deletions cgroup2/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,9 @@ func remove(path string) error {
return fmt.Errorf("cgroups: unable to remove path %q: %w", path, err)
}

// parseCgroupProcsFile parses /sys/fs/cgroup/$GROUPPATH/cgroup.procs
func parseCgroupProcsFile(path string) ([]uint64, error) {
// parseCgroupTasksFile parses /sys/fs/cgroup/$GROUPPATH/cgroup.procs or
// /sys/fs/cgroup/$GROUPPATH/cgroup.threads
func parseCgroupTasksFile(path string) ([]uint64, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
Expand Down

0 comments on commit 5e7266a

Please sign in to comment.