Skip to content

Commit c985f28

Browse files
kolyshkinPaweł Szulik
authored andcommitted
container/libcontainer: simplify for cgroup v2
In case of cgroup v2, there are no separate per-subsystem mount points, and so it does not make sense to have a subsystem to mountpoint map. Yet for the unification of code between v1 and v2, we still use this map, although a single element is enough. For cgroup v2, the key is "", the value is the mount point (or the path to a specific cgroup). This commit - simplifes GetCgroupSubsystems for cgroup v2 (trivial) case; - modifies GetCgroupPath() methods to use "" as the key for cgroup v2; - fixes NewCgroupManager() to use "" as the key for cgroup v2 case. Signed-off-by: Kir Kolyshkin <[email protected]>
1 parent a397f0e commit c985f28

File tree

5 files changed

+30
-5
lines changed

5 files changed

+30
-5
lines changed

container/containerd/handler.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"time"
2323

2424
"github.com/containerd/containerd/errdefs"
25+
"github.com/opencontainers/runc/libcontainer/cgroups"
2526
"golang.org/x/net/context"
2627

2728
specs "github.com/opencontainers/runtime-spec/specs-go"
@@ -216,7 +217,11 @@ func (h *containerdContainerHandler) ListContainers(listType container.ListType)
216217
}
217218

218219
func (h *containerdContainerHandler) GetCgroupPath(resource string) (string, error) {
219-
path, ok := h.cgroupPaths[resource]
220+
var res string
221+
if !cgroups.IsCgroup2UnifiedMode() {
222+
res = resource
223+
}
224+
path, ok := h.cgroupPaths[res]
220225
if !ok {
221226
return "", fmt.Errorf("could not find path for resource %q for container %q", resource, h.reference.Name)
222227
}

container/crio/handler.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,11 @@ func (h *crioContainerHandler) ListContainers(listType container.ListType) ([]in
328328
}
329329

330330
func (h *crioContainerHandler) GetCgroupPath(resource string) (string, error) {
331-
path, ok := h.cgroupPaths[resource]
331+
var res string
332+
if !cgroups.IsCgroup2UnifiedMode() {
333+
res = resource
334+
}
335+
path, ok := h.cgroupPaths[res]
332336
if !ok {
333337
return "", fmt.Errorf("could not find path for resource %q for container %q", resource, h.reference.Name)
334338
}

container/docker/handler.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"github.com/google/cadvisor/fs"
3232
info "github.com/google/cadvisor/info/v1"
3333
"github.com/google/cadvisor/zfs"
34+
"github.com/opencontainers/runc/libcontainer/cgroups"
3435

3536
dockercontainer "github.com/docker/docker/api/types/container"
3637
docker "github.com/docker/docker/client"
@@ -484,7 +485,11 @@ func (h *dockerContainerHandler) ListContainers(listType container.ListType) ([]
484485
}
485486

486487
func (h *dockerContainerHandler) GetCgroupPath(resource string) (string, error) {
487-
path, ok := h.cgroupPaths[resource]
488+
var res string
489+
if !cgroups.IsCgroup2UnifiedMode() {
490+
res = resource
491+
}
492+
path, ok := h.cgroupPaths[res]
488493
if !ok {
489494
return "", fmt.Errorf("could not find path for resource %q for container %q", resource, h.reference.Name)
490495
}

container/libcontainer/helpers.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,13 @@ import (
3636
// The incudeMetrics arguments specifies which metrics are requested,
3737
// and is used to filter out some cgroups and their mounts. If nil,
3838
// all supported cgroup subsystems are included.
39+
//
40+
// For cgroup v2, includedMetrics argument is unused, the only map key is ""
41+
// (empty string), and the value is the unified cgroup mount point.
3942
func GetCgroupSubsystems(includedMetrics container.MetricSet) (map[string]string, error) {
43+
if cgroups.IsCgroup2UnifiedMode() {
44+
return map[string]string{"": fs2.UnifiedMountpoint}, nil
45+
}
4046
// Get all cgroup mounts.
4147
allCgroups, err := cgroups.GetCgroupMounts(true)
4248
if err != nil {
@@ -152,7 +158,7 @@ func diskStatsCopy(blkioStats []cgroups.BlkioStatEntry) (stat []info.PerDiskStat
152158

153159
func NewCgroupManager(name string, paths map[string]string) (cgroups.Manager, error) {
154160
if cgroups.IsCgroup2UnifiedMode() {
155-
path := paths["cpu"]
161+
path := paths[""]
156162
return fs2.NewManager(nil, path, false)
157163
}
158164

container/raw/handler.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/google/cadvisor/fs"
2525
info "github.com/google/cadvisor/info/v1"
2626
"github.com/google/cadvisor/machine"
27+
"github.com/opencontainers/runc/libcontainer/cgroups"
2728

2829
"k8s.io/klog/v2"
2930
)
@@ -244,7 +245,11 @@ func (h *rawContainerHandler) GetStats() (*info.ContainerStats, error) {
244245
}
245246

246247
func (h *rawContainerHandler) GetCgroupPath(resource string) (string, error) {
247-
path, ok := h.cgroupPaths[resource]
248+
var res string
249+
if !cgroups.IsCgroup2UnifiedMode() {
250+
res = resource
251+
}
252+
path, ok := h.cgroupPaths[res]
248253
if !ok {
249254
return "", fmt.Errorf("could not find path for resource %q for container %q", resource, h.name)
250255
}

0 commit comments

Comments
 (0)