Skip to content

Commit 3c3c017

Browse files
committed
avoid copying kernCPUStats
1 parent 57d5711 commit 3c3c017

File tree

1 file changed

+4
-8
lines changed

1 file changed

+4
-8
lines changed

cpu/cpu_openbsd.go

+4-8
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func Times(percpu bool) ([]TimesStat, error) {
4949
return TimesWithContext(context.Background(), percpu)
5050
}
5151

52-
func cpsToTS(cpuTimes [cpuStates]uint64, name string) TimesStat {
52+
func cpsToTS(cpuTimes []uint64, name string) TimesStat {
5353
return TimesStat{
5454
CPU: name,
5555
User: float64(cpuTimes[cpUser]) / ClocksPerSec,
@@ -61,8 +61,6 @@ func cpsToTS(cpuTimes [cpuStates]uint64, name string) TimesStat {
6161
}
6262

6363
func TimesWithContext(ctx context.Context, percpu bool) (ret []TimesStat, err error) {
64-
cpuTimes := [cpuStates]uint64{}
65-
6664
if !percpu {
6765
mib := []int32{ctlKern, kernCpTime}
6866
buf, _, err := common.CallSyscall(mib)
@@ -72,10 +70,11 @@ func TimesWithContext(ctx context.Context, percpu bool) (ret []TimesStat, err er
7270
var x []C.long
7371
// could use unsafe.Slice but it's only for go1.17+
7472
x = (*[cpuStates]C.long)(unsafe.Pointer(&buf[0]))[:]
73+
cpuTimes := [cpuStates]uint64{}
7574
for i := range x {
7675
cpuTimes[i] = uint64(x[i])
7776
}
78-
c := cpsToTS(cpuTimes, "cpu-total")
77+
c := cpsToTS(cpuTimes[:], "cpu-total")
7978
return []TimesStat{c}, nil
8079
}
8180

@@ -101,10 +100,7 @@ func TimesWithContext(ctx context.Context, percpu bool) (ret []TimesStat, err er
101100

102101
var x []uint64
103102
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))
108104
ret = append(ret, c)
109105
}
110106

0 commit comments

Comments
 (0)