Skip to content

Commit

Permalink
avoid copying kernCPUStats
Browse files Browse the repository at this point in the history
  • Loading branch information
omar-polo committed Feb 8, 2022
1 parent 57d5711 commit 3c3c017
Showing 1 changed file with 4 additions and 8 deletions.
12 changes: 4 additions & 8 deletions cpu/cpu_openbsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func Times(percpu bool) ([]TimesStat, error) {
return TimesWithContext(context.Background(), percpu)
}

func cpsToTS(cpuTimes [cpuStates]uint64, name string) TimesStat {
func cpsToTS(cpuTimes []uint64, name string) TimesStat {
return TimesStat{
CPU: name,
User: float64(cpuTimes[cpUser]) / ClocksPerSec,
Expand All @@ -61,8 +61,6 @@ func cpsToTS(cpuTimes [cpuStates]uint64, name string) TimesStat {
}

func TimesWithContext(ctx context.Context, percpu bool) (ret []TimesStat, err error) {
cpuTimes := [cpuStates]uint64{}

if !percpu {
mib := []int32{ctlKern, kernCpTime}
buf, _, err := common.CallSyscall(mib)
Expand All @@ -72,10 +70,11 @@ func TimesWithContext(ctx context.Context, percpu bool) (ret []TimesStat, err er
var x []C.long
// could use unsafe.Slice but it's only for go1.17+
x = (*[cpuStates]C.long)(unsafe.Pointer(&buf[0]))[:]
cpuTimes := [cpuStates]uint64{}
for i := range x {
cpuTimes[i] = uint64(x[i])
}
c := cpsToTS(cpuTimes, "cpu-total")
c := cpsToTS(cpuTimes[:], "cpu-total")
return []TimesStat{c}, nil
}

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

var x []uint64
x = (*[cpuStates]uint64)(data)[:]
for i := range x {
cpuTimes[i] = x[i]
}
c := cpsToTS(cpuTimes, fmt.Sprintf("cpu%d", i))
c := cpsToTS(x, fmt.Sprintf("cpu%d", i))
ret = append(ret, c)
}

Expand Down

0 comments on commit 3c3c017

Please sign in to comment.