avoid copying kernCPUStats

pull/1244/head
Omar Polo 3 years ago
parent 57d5711d44
commit 3c3c017f23

@ -49,7 +49,7 @@ func Times(percpu bool) ([]TimesStat, error) {
return TimesWithContext(context.Background(), percpu) return TimesWithContext(context.Background(), percpu)
} }
func cpsToTS(cpuTimes [cpuStates]uint64, name string) TimesStat { func cpsToTS(cpuTimes []uint64, name string) TimesStat {
return TimesStat{ return TimesStat{
CPU: name, CPU: name,
User: float64(cpuTimes[cpUser]) / ClocksPerSec, User: float64(cpuTimes[cpUser]) / ClocksPerSec,
@ -61,8 +61,6 @@ func cpsToTS(cpuTimes [cpuStates]uint64, name string) TimesStat {
} }
func TimesWithContext(ctx context.Context, percpu bool) (ret []TimesStat, err error) { func TimesWithContext(ctx context.Context, percpu bool) (ret []TimesStat, err error) {
cpuTimes := [cpuStates]uint64{}
if !percpu { if !percpu {
mib := []int32{ctlKern, kernCpTime} mib := []int32{ctlKern, kernCpTime}
buf, _, err := common.CallSyscall(mib) buf, _, err := common.CallSyscall(mib)
@ -72,10 +70,11 @@ func TimesWithContext(ctx context.Context, percpu bool) (ret []TimesStat, err er
var x []C.long var x []C.long
// could use unsafe.Slice but it's only for go1.17+ // could use unsafe.Slice but it's only for go1.17+
x = (*[cpuStates]C.long)(unsafe.Pointer(&buf[0]))[:] x = (*[cpuStates]C.long)(unsafe.Pointer(&buf[0]))[:]
cpuTimes := [cpuStates]uint64{}
for i := range x { for i := range x {
cpuTimes[i] = uint64(x[i]) cpuTimes[i] = uint64(x[i])
} }
c := cpsToTS(cpuTimes, "cpu-total") c := cpsToTS(cpuTimes[:], "cpu-total")
return []TimesStat{c}, nil return []TimesStat{c}, nil
} }
@ -101,10 +100,7 @@ func TimesWithContext(ctx context.Context, percpu bool) (ret []TimesStat, err er
var x []uint64 var x []uint64
x = (*[cpuStates]uint64)(data)[:] x = (*[cpuStates]uint64)(data)[:]
for i := range x { c := cpsToTS(x, fmt.Sprintf("cpu%d", i))
cpuTimes[i] = x[i]
}
c := cpsToTS(cpuTimes, fmt.Sprintf("cpu%d", i))
ret = append(ret, c) ret = append(ret, c)
} }

Loading…
Cancel
Save