Merge branch '21535-multi-wf-delete'
[arvados.git] / lib / crunchrun / cgroup.go
index 48ec93b8768c0a117f0af60d8433bb30d8f4467c..a722e5f1423b715ebc8f65e9cc1afb1cdb847f2e 100644 (file)
@@ -7,13 +7,16 @@ package crunchrun
 import (
        "bytes"
        "fmt"
-       "io/ioutil"
+       "io/fs"
 )
 
 // Return the current process's cgroup for the given subsystem.
-func findCgroup(subsystem string) (string, error) {
+//
+// If the host has cgroups v2 and not v1 (i.e., unified mode), return
+// the current process's cgroup.
+func findCgroup(fsys fs.FS, subsystem string) (string, error) {
        subsys := []byte(subsystem)
-       cgroups, err := ioutil.ReadFile("/proc/self/cgroup")
+       cgroups, err := fs.ReadFile(fsys, "proc/self/cgroup")
        if err != nil {
                return "", err
        }
@@ -22,7 +25,20 @@ func findCgroup(subsystem string) (string, error) {
                if len(toks) < 3 {
                        continue
                }
+               if len(toks[1]) == 0 && string(toks[0]) == "0" {
+                       // cgroups v2: "0::$PATH"
+                       //
+                       // In "hybrid" mode, this entry is last, so we
+                       // use it when the specified subsystem doesn't
+                       // match a cgroups v1 entry.
+                       //
+                       // In "unified" mode, this is the only entry,
+                       // so we use it regardless of which subsystem
+                       // was specified.
+                       return string(toks[2]), nil
+               }
                for _, s := range bytes.Split(toks[1], []byte(",")) {
+                       // cgroups v1: "7:cpu,cpuacct:/user.slice"
                        if bytes.Compare(s, subsys) == 0 {
                                return string(toks[2]), nil
                        }