Skip to content

Commit 73f9c8d

Browse files
committed
avoid cgo for cpu_openbsd
Even thought OpenBSD often breaks the ABI compatibility and doesn't make *any* promise of "stability", this project aims to be "pure go" so avoid doing inter-op at the cost of artificially reducing the number of supported architectures down to amd64 and i386. To add support for another architecture (e.g. arm), add another file cpu_openbsd_${arch}.go like done for 386 and amd64. The fields are declared as `long' in C, so pick the appropriate size when declaring the struct.
1 parent 3c3c017 commit 73f9c8d

File tree

3 files changed

+52
-37
lines changed

3 files changed

+52
-37
lines changed

cpu/cpu_openbsd.go

+32-37
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,8 @@ import (
1414
"golang.org/x/sys/unix"
1515
)
1616

17-
import "C"
18-
1917
const (
2018
// sys/sched.h
21-
cpUser = 0
22-
cpNice = 1
23-
cpSys = 2
24-
cpSpin = 3
25-
cpIntr = 4
26-
cpIdle = 5
27-
cpuStates = 6
2819
cpuOnline = 0x0001 // CPUSTATS_ONLINE
2920

3021
// sys/sysctl.h
@@ -37,6 +28,19 @@ const (
3728

3829
var ClocksPerSec = float64(128)
3930

31+
type cpuStats struct {
32+
// cs_time[CPUSTATES]
33+
User uint64
34+
Nice uint64
35+
Sys uint64
36+
Spin uint64
37+
Intr uint64
38+
Idle uint64
39+
40+
// cs_flags
41+
Flags uint64
42+
}
43+
4044
func init() {
4145
clkTck, err := sysconf.Sysconf(sysconf.SC_CLK_TCK)
4246
// ignore errors
@@ -49,33 +53,23 @@ func Times(percpu bool) ([]TimesStat, error) {
4953
return TimesWithContext(context.Background(), percpu)
5054
}
5155

52-
func cpsToTS(cpuTimes []uint64, name string) TimesStat {
53-
return TimesStat{
54-
CPU: name,
55-
User: float64(cpuTimes[cpUser]) / ClocksPerSec,
56-
Nice: float64(cpuTimes[cpNice]) / ClocksPerSec,
57-
System: float64(cpuTimes[cpSys]) / ClocksPerSec,
58-
Idle: float64(cpuTimes[cpIdle]) / ClocksPerSec,
59-
Irq: float64(cpuTimes[cpIntr]) / ClocksPerSec,
60-
}
61-
}
62-
6356
func TimesWithContext(ctx context.Context, percpu bool) (ret []TimesStat, err error) {
6457
if !percpu {
6558
mib := []int32{ctlKern, kernCpTime}
6659
buf, _, err := common.CallSyscall(mib)
6760
if err != nil {
6861
return ret, err
6962
}
70-
var x []C.long
71-
// could use unsafe.Slice but it's only for go1.17+
72-
x = (*[cpuStates]C.long)(unsafe.Pointer(&buf[0]))[:]
73-
cpuTimes := [cpuStates]uint64{}
74-
for i := range x {
75-
cpuTimes[i] = uint64(x[i])
63+
times := (*cpuTimes)(unsafe.Pointer(&buf[0]))
64+
stat := TimesStat{
65+
CPU: "cpu-total",
66+
User: float64(times.User) / ClocksPerSec,
67+
Nice: float64(times.Nice) / ClocksPerSec,
68+
System: float64(times.Sys) / ClocksPerSec,
69+
Idle: float64(times.Idle) / ClocksPerSec,
70+
Irq: float64(times.Intr) / ClocksPerSec,
7671
}
77-
c := cpsToTS(cpuTimes[:], "cpu-total")
78-
return []TimesStat{c}, nil
72+
return []TimesStat{stat}, nil
7973
}
8074

8175
ncpu, err := unix.SysctlUint32("hw.ncpu")
@@ -91,17 +85,18 @@ func TimesWithContext(ctx context.Context, percpu bool) (ret []TimesStat, err er
9185
return ret, err
9286
}
9387

94-
data := unsafe.Pointer(&buf[0])
95-
fptr := unsafe.Pointer(uintptr(data) + uintptr(8*cpuStates))
96-
flags := *(*uint64)(fptr)
97-
if (flags & cpuOnline) == 0 {
88+
stats := (*cpuStats)(unsafe.Pointer(&buf[0]))
89+
if (stats.Flags & cpuOnline) == 0 {
9890
continue
9991
}
100-
101-
var x []uint64
102-
x = (*[cpuStates]uint64)(data)[:]
103-
c := cpsToTS(x, fmt.Sprintf("cpu%d", i))
104-
ret = append(ret, c)
92+
ret = append(ret, TimesStat{
93+
CPU: fmt.Sprintf("cpu%d", i),
94+
User: float64(stats.User) / ClocksPerSec,
95+
Nice: float64(stats.Nice) / ClocksPerSec,
96+
System: float64(stats.Sys) / ClocksPerSec,
97+
Idle: float64(stats.Idle) / ClocksPerSec,
98+
Irq: float64(stats.Intr) / ClocksPerSec,
99+
})
105100
}
106101

107102
return ret, nil

cpu/cpu_openbsd_386.go

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package cpu
2+
3+
type cpuTimes struct {
4+
User uint32
5+
Nice uint32
6+
Sys uint32
7+
Spin uint32
8+
Intr uint32
9+
Idle uint32
10+
}

cpu/cpu_openbsd_amd64.go

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package cpu
2+
3+
type cpuTimes struct {
4+
User uint64
5+
Nice uint64
6+
Sys uint64
7+
Spin uint64
8+
Intr uint64
9+
Idle uint64
10+
}

0 commit comments

Comments
 (0)