Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add Threads for cgroupv2 #312

Merged
merged 2 commits into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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