Skip to content

Commit

Permalink
cgroups: fix controller filesystem path in cgroup v2
Browse files Browse the repository at this point in the history
- Fixes page demotion in cgroup v2. This was failing because
  GetProcesses() and GetCgroupDir() did not work.
  • Loading branch information
askervin committed Dec 15, 2022
1 parent 759f717 commit bb6e162
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
8 changes: 6 additions & 2 deletions pkg/cgroups/cgroupcontrol.go
Expand Up @@ -30,7 +30,7 @@ type Controller int
// Group represents a control group.
type Group string

//nolint
// nolint
const (
// UnkownController represents a controller of unknown type.
UnknownController Controller = iota
Expand Down Expand Up @@ -104,6 +104,10 @@ func (c Controller) String() string {

// Path returns the absolute path of the given controller.
func (c Controller) Path() string {
DetectSystemCgroupVersion()
if systemCgroupVersion == 2 {
return GetMountDir()
}
return path.Join(mountDir, c.String())
}

Expand All @@ -114,7 +118,7 @@ func (c Controller) RelPath() string {

// Group returns the given group for the controller.
func (c Controller) Group(group string) Group {
return Group(path.Join(mountDir, c.String(), group))
return Group(path.Join(c.Path(), group))
}

// AsGroup returns the group for the given absolute directory path.
Expand Down
18 changes: 17 additions & 1 deletion pkg/cgroups/cgrouppath.go
Expand Up @@ -16,11 +16,12 @@ package cgroups

import (
"flag"
"os"
"path"
"path/filepath"
)

//nolint
// nolint
const (
// Tasks is a cgroup's "tasks" entry.
Tasks = "tasks"
Expand All @@ -36,6 +37,8 @@ const (
CpusetCpus = "cpuset.cpus"
// CpusetMems is the cpuset controller's cpuset.mems entry.
CpusetMems = "cpuset.mems"
// Controllers is the cgroup v2 controllers file
Controllers = "cgroup.controllers"
)

var (
Expand All @@ -45,6 +48,8 @@ var (
v2Dir = path.Join(mountDir, "unified")
// KubeletRoot is the --cgroup-root option the kubelet is running with.
KubeletRoot = ""
// detected system cgroup version, 0 is undetected
systemCgroupVersion = 0
)

// GetMountDir returns the common mount point for cgroup v1 controllers.
Expand Down Expand Up @@ -83,3 +88,14 @@ func init() {
flag.StringVar(&KubeletRoot, "kubelet-cgroup-root", KubeletRoot,
"--cgroup-root options the kubelet is running with")
}

func DetectSystemCgroupVersion() int {
if systemCgroupVersion == 0 {
if _, err := os.Stat(path.Join(GetMountDir(), Controllers)); err == nil {
systemCgroupVersion = 2
} else {
systemCgroupVersion = 1
}
}
return systemCgroupVersion
}

0 comments on commit bb6e162

Please sign in to comment.