Merge pull request #2273 from kolyshkin/v2-untangle

cgroup v2 cleanups
This commit is contained in:
Mrunal Patel 2020-03-27 11:21:36 -07:00 committed by GitHub
commit 75ff40cd28
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 24 deletions

View File

@ -4,7 +4,6 @@ package systemd
import (
"bytes"
"fmt"
"io/ioutil"
"math"
"os"
@ -38,14 +37,6 @@ func (m *UnifiedManager) Apply(pid int) error {
if c.Paths != nil {
paths := make(map[string]string)
for name, path := range c.Paths {
_, err := getSubsystemPath(m.Cgroups, name)
if err != nil {
// Don't fail if a cgroup hierarchy was not found, just skip this subsystem
if cgroups.IsNotFound(err) {
continue
}
return err
}
paths[name] = path
}
m.Paths = paths
@ -139,12 +130,11 @@ func (m *UnifiedManager) Apply(pid int) error {
return err
}
if err := joinCgroupsV2(c, pid); err != nil {
path, err := getv2Path(m.Cgroups)
if err != nil {
return err
}
path, err := getSubsystemPath(m.Cgroups, "")
if err != nil {
if err := createCgroupsv2Path(path); err != nil {
return err
}
m.Paths = map[string]string{
@ -197,14 +187,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(" +"))...)
@ -236,14 +238,6 @@ func createCgroupsv2Path(path string) (Err error) {
return nil
}
func joinCgroupsV2(c *configs.Cgroup, pid int) error {
path, err := getSubsystemPath(c, "memory")
if err != nil {
return err
}
return createCgroupsv2Path(path)
}
func (m *UnifiedManager) fsManager() (cgroups.Manager, error) {
path, err := m.GetUnifiedPath()
if err != nil {

View File

@ -4,6 +4,7 @@ package cgroups
import (
"bufio"
"errors"
"fmt"
"io"
"io/ioutil"
@ -28,6 +29,8 @@ const (
var (
isUnifiedOnce sync.Once
isUnified bool
errUnified = errors.New("not implemented for cgroup v2 unified hierarchy")
)
// HugePageSizeUnitList is a list of the units used by the linux kernel when
@ -307,6 +310,9 @@ func GetAllSubsystems() ([]string, error) {
// GetOwnCgroup returns the relative path to the cgroup docker is running in.
func GetOwnCgroup(subsystem string) (string, error) {
if IsCgroup2UnifiedMode() {
return "", errUnified
}
cgroups, err := ParseCgroupFile("/proc/self/cgroup")
if err != nil {
return "", err
@ -325,6 +331,9 @@ func GetOwnCgroupPath(subsystem string) (string, error) {
}
func GetInitCgroup(subsystem string) (string, error) {
if IsCgroup2UnifiedMode() {
return "", errUnified
}
cgroups, err := ParseCgroupFile("/proc/1/cgroup")
if err != nil {
return "", err