virt-handler: cgroup: add helper to retrieve threads of a cgroup
Add function GetCgroupThreads which returns a list of pids attached
to a cgroup.
package cgroup
import (
+ "bufio"
"fmt"
"os"
"path/filepath"
@@ -179,3 +180,38 @@ func detectVMIsolation(vm *v1.VirtualMachineInstance, socket string) (isolationR
return isolationRes, nil
}
+
+func GetCgroupThreads(manager Manager) (tIds []int, err error) {
+ tIds = make([]int, 0, 10)
+ fName := "tasks"
+ if runc_cgroups.IsCgroup2UnifiedMode() {
+ fName = "cgroup.threads"
+ }
+
+ subSysPath, err := manager.GetBasePathToHostSubsystem("cpuset")
+ if err != nil {
+ return nil, err
+ }
+
+ fh, err := os.Open(filepath.Join(subSysPath, fName))
+ if err != nil {
+ return nil, err
+ }
+ defer fh.Close()
+
+ scanner := bufio.NewScanner(fh)
+ for scanner.Scan() {
+ line := scanner.Text()
+ intVal, err := strconv.Atoi(line)
+ if err != nil {
+ log.Log.Errorf("error converting %s: %v", line, err)
+ return nil, err
+ }
+ tIds = append(tIds, intVal)
+ }
+ if err := scanner.Err(); err != nil {
+ log.Log.Errorf("error reading %s: %v", fName, err)
+ return nil, err
+ }
+ return tIds, nil
+}
