@@ -11,14 +11,7 @@ import (
1111)
1212
1313const (
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
108101func getPercpuUsage (path string ) ([]uint64 , error ) {
@@ -112,7 +105,6 @@ func getPercpuUsage(path string) ([]uint64, error) {
112105 if err != nil {
113106 return percpuUsage , err
114107 }
115- // TODO: use strings.SplitN instead.
116108 for _ , value := range strings .Fields (data ) {
117109 value , err := strconv .ParseUint (value , 10 , 64 )
118110 if err != nil {
@@ -126,7 +118,7 @@ func getPercpuUsage(path string) ([]uint64, error) {
126118func getPercpuUsageInModes (path string ) ([]uint64 , []uint64 , error ) {
127119 usageKernelMode := []uint64 {}
128120 usageUserMode := []uint64 {}
129- const file = cgroupCpuacctUsageAll
121+ const file = "cpuacct.usage_all"
130122
131123 fd , err := cgroups .OpenFile (path , file , os .O_RDONLY )
132124 if os .IsNotExist (err ) {
@@ -140,22 +132,23 @@ func getPercpuUsageInModes(path string) ([]uint64, []uint64, error) {
140132 scanner .Scan () // skipping header line
141133
142134 for scanner .Scan () {
143- lineFields := strings .SplitN (scanner .Text (), " " , cuacctUsageAllColumnsNumber + 1 )
144- if len (lineFields ) != cuacctUsageAllColumnsNumber {
135+ // Each line is: cpu user system
136+ fields := strings .SplitN (scanner .Text (), " " , 3 )
137+ if len (fields ) != 3 {
145138 continue
146139 }
147140
148- usageInKernelMode , err := strconv .ParseUint (lineFields [ kernelModeColumn ], 10 , 64 )
141+ user , err := strconv .ParseUint (fields [ 1 ], 10 , 64 )
149142 if err != nil {
150143 return nil , nil , & parseError {Path : path , File : file , Err : err }
151144 }
152- usageKernelMode = append (usageKernelMode , usageInKernelMode )
145+ usageUserMode = append (usageUserMode , user )
153146
154- usageInUserMode , err := strconv .ParseUint (lineFields [ userModeColumn ], 10 , 64 )
147+ kernel , err := strconv .ParseUint (fields [ 2 ], 10 , 64 )
155148 if err != nil {
156149 return nil , nil , & parseError {Path : path , File : file , Err : err }
157150 }
158- usageUserMode = append (usageUserMode , usageInUserMode )
151+ usageKernelMode = append (usageKernelMode , kernel )
159152 }
160153 if err := scanner .Err (); err != nil {
161154 return nil , nil , & parseError {Path : path , File : file , Err : err }
0 commit comments