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 <kolyshkin@gmail.com>
This commit is contained in:
Kir Kolyshkin 2020-03-26 13:35:51 -07:00
parent ec1f957b23
commit 5406833a65
1 changed files with 17 additions and 6 deletions

View File

@ -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
}