Skip to content

Commit c539116

Browse files
kolyshkinPaweł Szulik
authored andcommitted
container: GetCgroupSubsystems: simplify
Instead of creating an intermediate map to remove specific mounts based on MetricSet, amend the supportedSubsystems map to have information about which subsystem reports which metrics. Based on that info, the check whether the subsystem is needed or not becomes much simpler. Signed-off-by: Kir Kolyshkin <[email protected]>
1 parent 3888dda commit c539116

File tree

2 files changed

+33
-53
lines changed

2 files changed

+33
-53
lines changed

container/libcontainer/helpers.go

Lines changed: 32 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -39,38 +39,7 @@ func GetCgroupSubsystems(includedMetrics container.MetricSet) (map[string]string
3939
return nil, err
4040
}
4141

42-
disableCgroups := map[string]struct{}{}
43-
44-
if !includedMetrics.Has(container.DiskIOMetrics) {
45-
disableCgroups["blkio"] = struct{}{}
46-
disableCgroups["io"] = struct{}{}
47-
}
48-
49-
if !includedMetrics.Has(container.CpuUsageMetrics) {
50-
disableCgroups["cpu"] = struct{}{}
51-
}
52-
53-
if !includedMetrics.Has(container.CPUSetMetrics) {
54-
disableCgroups["cpuset"] = struct{}{}
55-
}
56-
57-
if !includedMetrics.Has(container.HugetlbUsageMetrics) {
58-
disableCgroups["hugetlb"] = struct{}{}
59-
}
60-
61-
if !includedMetrics.Has(container.MemoryUsageMetrics) {
62-
disableCgroups["memory"] = struct{}{}
63-
}
64-
65-
if !includedMetrics.Has(container.PerfMetrics) {
66-
disableCgroups["perf_event"] = struct{}{}
67-
}
68-
69-
if !includedMetrics.Has(container.ProcessMetrics) {
70-
disableCgroups["pids"] = struct{}{}
71-
}
72-
73-
return getCgroupSubsystemsHelper(allCgroups, disableCgroups)
42+
return getCgroupSubsystemsHelper(allCgroups, includedMetrics)
7443
}
7544

7645
// Get information about all the cgroup subsystems.
@@ -81,11 +50,10 @@ func GetAllCgroupSubsystems() (map[string]string, error) {
8150
return nil, err
8251
}
8352

84-
emptyDisableCgroups := map[string]struct{}{}
85-
return getCgroupSubsystemsHelper(allCgroups, emptyDisableCgroups)
53+
return getCgroupSubsystemsHelper(allCgroups, nil)
8654
}
8755

88-
func getCgroupSubsystemsHelper(allCgroups []cgroups.Mount, disableCgroups map[string]struct{}) (map[string]string, error) {
56+
func getCgroupSubsystemsHelper(allCgroups []cgroups.Mount, includedMetrics container.MetricSet) (map[string]string, error) {
8957
if len(allCgroups) == 0 {
9058
return nil, fmt.Errorf("failed to find cgroup mounts")
9159
}
@@ -94,11 +62,7 @@ func getCgroupSubsystemsHelper(allCgroups []cgroups.Mount, disableCgroups map[st
9462
mountPoints := make(map[string]string, len(allCgroups))
9563
for _, mount := range allCgroups {
9664
for _, subsystem := range mount.Subsystems {
97-
if _, exists := disableCgroups[subsystem]; exists {
98-
continue
99-
}
100-
if _, ok := supportedSubsystems[subsystem]; !ok {
101-
// Unsupported subsystem
65+
if !needSubsys(subsystem, includedMetrics) {
10266
continue
10367
}
10468
if _, ok := mountPoints[subsystem]; ok {
@@ -113,18 +77,34 @@ func getCgroupSubsystemsHelper(allCgroups []cgroups.Mount, disableCgroups map[st
11377
return mountPoints, nil
11478
}
11579

116-
// Cgroup subsystems we support listing (should be the minimal set we need stats from).
117-
var supportedSubsystems map[string]struct{} = map[string]struct{}{
118-
"cpu": {},
119-
"cpuacct": {},
120-
"memory": {},
121-
"hugetlb": {},
122-
"pids": {},
123-
"cpuset": {},
124-
"blkio": {},
125-
"io": {},
126-
"devices": {},
127-
"perf_event": {},
80+
// A map of cgroup subsystems we support listing (should be the minimal set
81+
// we need stats from) to a respective MetricKind.
82+
var supportedSubsystems = map[string]container.MetricKind{
83+
"cpu": container.CpuUsageMetrics,
84+
"cpuacct": container.CpuUsageMetrics,
85+
"memory": container.MemoryUsageMetrics,
86+
"hugetlb": container.HugetlbUsageMetrics,
87+
"pids": container.ProcessMetrics,
88+
"cpuset": container.CPUSetMetrics,
89+
"blkio": container.DiskIOMetrics,
90+
"io": container.DiskIOMetrics,
91+
"devices": "",
92+
"perf_event": container.PerfMetrics,
93+
}
94+
95+
// Check if this cgroup subsystem/controller is of use.
96+
func needSubsys(name string, metrics container.MetricSet) bool {
97+
// Check if supported.
98+
metric, supported := supportedSubsystems[name]
99+
if !supported {
100+
return false
101+
}
102+
// Check if needed.
103+
if metrics == nil || metric == "" {
104+
return true
105+
}
106+
107+
return metrics.Has(metric)
128108
}
129109

130110
func diskStatsCopy0(major, minor uint64) *info.PerDiskStats {

container/libcontainer/helpers_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func TestGetCgroupSubsystems(t *testing.T) {
9090
}
9191

9292
for i, testCase := range testCases {
93-
subSystems, err := getCgroupSubsystemsHelper(testCase.mounts, map[string]struct{}{})
93+
subSystems, err := getCgroupSubsystemsHelper(testCase.mounts, nil)
9494
if testCase.err {
9595
if err == nil {
9696
t.Fatalf("[case %d] Expected error but didn't get one", i)

0 commit comments

Comments
 (0)