Skip to content

Commit 075cea3

Browse files
committed
libcontainer/cgroups/fs: some refactoring
Remove extra global constants that are only used in a single place and make it harder to read the code. Rename nanosecondsInSecond -> nsInSec. This code is tested by unit tests. Signed-off-by: Kir Kolyshkin <[email protected]>
1 parent 4271ecf commit 075cea3

File tree

2 files changed

+15
-21
lines changed

2 files changed

+15
-21
lines changed

libcontainer/cgroups/fs/cpuacct.go

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,7 @@ import (
1111
)
1212

1313
const (
14-
cgroupCpuacctStat = "cpuacct.stat"
15-
cgroupCpuacctUsageAll = "cpuacct.usage_all"
16-
17-
nanosecondsInSecond = 1000000000
18-
19-
userModeColumn = 1
20-
kernelModeColumn = 2
21-
cuacctUsageAllColumnsNumber = 3
14+
nsInSec = 1000000000
2215

2316
// The value comes from `C.sysconf(C._SC_CLK_TCK)`, and
2417
// on Linux it's a constant which is safe to be hard coded,
@@ -80,7 +73,7 @@ func getCpuUsageBreakdown(path string) (uint64, uint64, error) {
8073
const (
8174
userField = "user"
8275
systemField = "system"
83-
file = cgroupCpuacctStat
76+
file = "cpuacct.stat"
8477
)
8578

8679
// Expected format:
@@ -102,7 +95,7 @@ func getCpuUsageBreakdown(path string) (uint64, uint64, error) {
10295
return 0, 0, &parseError{Path: path, File: file, Err: err}
10396
}
10497

105-
return (userModeUsage * nanosecondsInSecond) / clockTicks, (kernelModeUsage * nanosecondsInSecond) / clockTicks, nil
98+
return (userModeUsage * nsInSec) / clockTicks, (kernelModeUsage * nsInSec) / clockTicks, nil
10699
}
107100

108101
func getPercpuUsage(path string) ([]uint64, error) {
@@ -125,7 +118,7 @@ func getPercpuUsage(path string) ([]uint64, error) {
125118
func getPercpuUsageInModes(path string) ([]uint64, []uint64, error) {
126119
usageKernelMode := []uint64{}
127120
usageUserMode := []uint64{}
128-
const file = cgroupCpuacctUsageAll
121+
const file = "cpuacct.usage_all"
129122

130123
fd, err := cgroups.OpenFile(path, file, os.O_RDONLY)
131124
if os.IsNotExist(err) {
@@ -139,22 +132,23 @@ func getPercpuUsageInModes(path string) ([]uint64, []uint64, error) {
139132
scanner.Scan() // skipping header line
140133

141134
for scanner.Scan() {
142-
lineFields := strings.SplitN(scanner.Text(), " ", cuacctUsageAllColumnsNumber+1)
143-
if len(lineFields) != cuacctUsageAllColumnsNumber {
135+
// Each line is: cpu user system
136+
fields := strings.SplitN(scanner.Text(), " ", 3)
137+
if len(fields) != 3 {
144138
continue
145139
}
146140

147-
usageInKernelMode, err := strconv.ParseUint(lineFields[kernelModeColumn], 10, 64)
141+
user, err := strconv.ParseUint(fields[1], 10, 64)
148142
if err != nil {
149143
return nil, nil, &parseError{Path: path, File: file, Err: err}
150144
}
151-
usageKernelMode = append(usageKernelMode, usageInKernelMode)
145+
usageUserMode = append(usageUserMode, user)
152146

153-
usageInUserMode, err := strconv.ParseUint(lineFields[userModeColumn], 10, 64)
147+
kernel, err := strconv.ParseUint(fields[2], 10, 64)
154148
if err != nil {
155149
return nil, nil, &parseError{Path: path, File: file, Err: err}
156150
}
157-
usageUserMode = append(usageUserMode, usageInUserMode)
151+
usageKernelMode = append(usageKernelMode, kernel)
158152
}
159153
if err := scanner.Err(); err != nil {
160154
return nil, nil, &parseError{Path: path, File: file, Err: err}

libcontainer/cgroups/fs/cpuacct_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ func TestCpuacctStats(t *testing.T) {
5353
962250696038415, 981956408513304, 1002658817529022, 994937703492523,
5454
874843781648690, 872544369885276, 870104915696359, 870202363887496,
5555
},
56-
UsageInKernelmode: (uint64(291429664) * nanosecondsInSecond) / clockTicks,
57-
UsageInUsermode: (uint64(452278264) * nanosecondsInSecond) / clockTicks,
56+
UsageInKernelmode: (uint64(291429664) * nsInSec) / clockTicks,
57+
UsageInUsermode: (uint64(452278264) * nsInSec) / clockTicks,
5858
}
5959

6060
if !reflect.DeepEqual(expectedStats, actualStats.CpuStats.CpuUsage) {
@@ -86,8 +86,8 @@ func TestCpuacctStatsWithoutUsageAll(t *testing.T) {
8686
},
8787
PercpuUsageInKernelmode: []uint64{},
8888
PercpuUsageInUsermode: []uint64{},
89-
UsageInKernelmode: (uint64(291429664) * nanosecondsInSecond) / clockTicks,
90-
UsageInUsermode: (uint64(452278264) * nanosecondsInSecond) / clockTicks,
89+
UsageInKernelmode: (uint64(291429664) * nsInSec) / clockTicks,
90+
UsageInUsermode: (uint64(452278264) * nsInSec) / clockTicks,
9191
}
9292

9393
if !reflect.DeepEqual(expectedStats, actualStats.CpuStats.CpuUsage) {

0 commit comments

Comments
 (0)