@@ -49,7 +49,7 @@ func Times(percpu bool) ([]TimesStat, error) {
49
49
return TimesWithContext (context .Background (), percpu )
50
50
}
51
51
52
- func cpsToTS (cpuTimes [cpuStates ]uint64 , name string ) TimesStat {
52
+ func cpsToTS (cpuTimes []uint64 , name string ) TimesStat {
53
53
return TimesStat {
54
54
CPU : name ,
55
55
User : float64 (cpuTimes [cpUser ]) / ClocksPerSec ,
@@ -61,8 +61,6 @@ func cpsToTS(cpuTimes [cpuStates]uint64, name string) TimesStat {
61
61
}
62
62
63
63
func TimesWithContext (ctx context.Context , percpu bool ) (ret []TimesStat , err error ) {
64
- cpuTimes := [cpuStates ]uint64 {}
65
-
66
64
if ! percpu {
67
65
mib := []int32 {ctlKern , kernCpTime }
68
66
buf , _ , err := common .CallSyscall (mib )
@@ -72,10 +70,11 @@ func TimesWithContext(ctx context.Context, percpu bool) (ret []TimesStat, err er
72
70
var x []C.long
73
71
// could use unsafe.Slice but it's only for go1.17+
74
72
x = (* [cpuStates ]C.long )(unsafe .Pointer (& buf [0 ]))[:]
73
+ cpuTimes := [cpuStates ]uint64 {}
75
74
for i := range x {
76
75
cpuTimes [i ] = uint64 (x [i ])
77
76
}
78
- c := cpsToTS (cpuTimes , "cpu-total" )
77
+ c := cpsToTS (cpuTimes [:] , "cpu-total" )
79
78
return []TimesStat {c }, nil
80
79
}
81
80
@@ -101,10 +100,7 @@ func TimesWithContext(ctx context.Context, percpu bool) (ret []TimesStat, err er
101
100
102
101
var x []uint64
103
102
x = (* [cpuStates ]uint64 )(data )[:]
104
- for i := range x {
105
- cpuTimes [i ] = x [i ]
106
- }
107
- c := cpsToTS (cpuTimes , fmt .Sprintf ("cpu%d" , i ))
103
+ c := cpsToTS (x , fmt .Sprintf ("cpu%d" , i ))
108
104
ret = append (ret , c )
109
105
}
110
106
0 commit comments