From 5406833a65e7b0160265bce5df25def19b9aa823 Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Thu, 26 Mar 2020 13:35:51 -0700 Subject: [PATCH] cgroupv2/systemd: add getv2Path Function getSubsystemPath(), while works for v2 unified case, is suboptimal, as it does a few unnecessary calls. Add a simplified version of getSubsystemPath(), called getv2Path(), and use it. Signed-off-by: Kir Kolyshkin --- .../cgroups/systemd/unified_hierarchy.go | 23 ++++++++++++++----- 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/libcontainer/cgroups/systemd/unified_hierarchy.go b/libcontainer/cgroups/systemd/unified_hierarchy.go index 795581566..76004e655 100644 --- a/libcontainer/cgroups/systemd/unified_hierarchy.go +++ b/libcontainer/cgroups/systemd/unified_hierarchy.go @@ -4,7 +4,6 @@ package systemd import ( "bytes" - "fmt" "io/ioutil" "math" "os" @@ -135,7 +134,7 @@ func (m *UnifiedManager) Apply(pid int) error { return err } - path, err := getSubsystemPath(m.Cgroups, "") + path, err := getv2Path(m.Cgroups) if err != nil { return err } @@ -189,14 +188,26 @@ func (m *UnifiedManager) GetUnifiedPath() (string, error) { } return unifiedPath, nil } + +func getv2Path(c *configs.Cgroup) (string, error) { + slice := "system.slice" + if c.Parent != "" { + slice = c.Parent + } + + slice, err := ExpandSlice(slice) + if err != nil { + return "", err + } + + return filepath.Join(fs2.UnifiedMountpoint, slice, getUnitName(c)), nil +} + func createCgroupsv2Path(path string) (Err error) { content, err := ioutil.ReadFile("/sys/fs/cgroup/cgroup.controllers") if err != nil { return err } - if !filepath.HasPrefix(path, "/sys/fs/cgroup") { - return fmt.Errorf("invalid cgroup path %s", path) - } ctrs := bytes.Fields(content) res := append([]byte("+"), bytes.Join(ctrs, []byte(" +"))...) @@ -229,7 +240,7 @@ func createCgroupsv2Path(path string) (Err error) { } func joinCgroupsV2(c *configs.Cgroup, pid int) error { - path, err := getSubsystemPath(c, "memory") + path, err := getv2Path(c) if err != nil { return err }