github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/client/lib/cgutil/cpuset_manager.go (about) 1 package cgutil 2 3 import ( 4 "context" 5 "path/filepath" 6 "strings" 7 8 "github.com/hashicorp/nomad/lib/cpuset" 9 "github.com/hashicorp/nomad/nomad/structs" 10 ) 11 12 const ( 13 // CgroupRoot is hard-coded in the cgroups specification. 14 // It only applies to linux but helpers have references to it in driver(s). 15 CgroupRoot = "/sys/fs/cgroup" 16 ) 17 18 // CpusetManager is used to setup cpuset cgroups for each task. 19 type CpusetManager interface { 20 // Init should be called before the client starts running allocations. This 21 // is where the cpuset manager should start doing background operations. 22 Init() 23 24 // AddAlloc adds an allocation to the manager 25 AddAlloc(alloc *structs.Allocation) 26 27 // RemoveAlloc removes an alloc by ID from the manager 28 RemoveAlloc(allocID string) 29 30 // CgroupPathFor returns a callback for getting the cgroup path and any error that may have occurred during 31 // cgroup initialization. The callback will block if the cgroup has not been created 32 CgroupPathFor(allocID, taskName string) CgroupPathGetter 33 } 34 35 type NoopCpusetManager struct{} 36 37 func (n NoopCpusetManager) Init() { 38 } 39 40 func (n NoopCpusetManager) AddAlloc(alloc *structs.Allocation) { 41 } 42 43 func (n NoopCpusetManager) RemoveAlloc(allocID string) { 44 } 45 46 func (n NoopCpusetManager) CgroupPathFor(allocID, task string) CgroupPathGetter { 47 return func(context.Context) (string, error) { return "", nil } 48 } 49 50 // CgroupPathGetter is a function which returns the cgroup path and any error which 51 // occurred during cgroup initialization. 52 // 53 // It should block until the cgroup has been created or an error is reported. 54 type CgroupPathGetter func(context.Context) (path string, err error) 55 56 type TaskCgroupInfo struct { 57 CgroupPath string 58 RelativeCgroupPath string 59 Cpuset cpuset.CPUSet 60 Error error 61 } 62 63 // SplitPath determines the parent and cgroup from p. 64 // p must contain at least 2 elements (parent + cgroup). 65 // 66 // Handles the cgroup root if present. 67 func SplitPath(p string) (string, string) { 68 p = strings.TrimPrefix(p, CgroupRoot) 69 p = strings.Trim(p, "/") 70 parts := strings.Split(p, "/") 71 return parts[0], "/" + filepath.Join(parts[1:]...) 72 }