Skip to content

Commit

Permalink
avoid cgo for cpu_openbsd
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
omar-polo committed Feb 24, 2022
1 parent 3c3c017 commit 73f9c8d
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 37 deletions.
69 changes: 32 additions & 37 deletions cpu/cpu_openbsd.go
Expand Up @@ -14,17 +14,8 @@ import (
"golang.org/x/sys/unix"
)

import "C"

const (
// sys/sched.h
cpUser = 0
cpNice = 1
cpSys = 2
cpSpin = 3
cpIntr = 4
cpIdle = 5
cpuStates = 6
cpuOnline = 0x0001 // CPUSTATS_ONLINE

// sys/sysctl.h
Expand All @@ -37,6 +28,19 @@ const (

var ClocksPerSec = float64(128)

type cpuStats struct {
// cs_time[CPUSTATES]
User uint64
Nice uint64
Sys uint64
Spin uint64
Intr uint64
Idle uint64

// cs_flags
Flags uint64
}

func init() {
clkTck, err := sysconf.Sysconf(sysconf.SC_CLK_TCK)
// ignore errors
Expand All @@ -49,33 +53,23 @@ func Times(percpu bool) ([]TimesStat, error) {
return TimesWithContext(context.Background(), percpu)
}

func cpsToTS(cpuTimes []uint64, name string) TimesStat {
return TimesStat{
CPU: name,
User: float64(cpuTimes[cpUser]) / ClocksPerSec,
Nice: float64(cpuTimes[cpNice]) / ClocksPerSec,
System: float64(cpuTimes[cpSys]) / ClocksPerSec,
Idle: float64(cpuTimes[cpIdle]) / ClocksPerSec,
Irq: float64(cpuTimes[cpIntr]) / ClocksPerSec,
}
}

func TimesWithContext(ctx context.Context, percpu bool) (ret []TimesStat, err error) {
if !percpu {
mib := []int32{ctlKern, kernCpTime}
buf, _, err := common.CallSyscall(mib)
if err != nil {
return ret, err
}
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])
times := (*cpuTimes)(unsafe.Pointer(&buf[0]))
stat := TimesStat{
CPU: "cpu-total",
User: float64(times.User) / ClocksPerSec,
Nice: float64(times.Nice) / ClocksPerSec,
System: float64(times.Sys) / ClocksPerSec,
Idle: float64(times.Idle) / ClocksPerSec,
Irq: float64(times.Intr) / ClocksPerSec,
}
c := cpsToTS(cpuTimes[:], "cpu-total")
return []TimesStat{c}, nil
return []TimesStat{stat}, nil
}

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

data := unsafe.Pointer(&buf[0])
fptr := unsafe.Pointer(uintptr(data) + uintptr(8*cpuStates))
flags := *(*uint64)(fptr)
if (flags & cpuOnline) == 0 {
stats := (*cpuStats)(unsafe.Pointer(&buf[0]))
if (stats.Flags & cpuOnline) == 0 {
continue
}

var x []uint64
x = (*[cpuStates]uint64)(data)[:]
c := cpsToTS(x, fmt.Sprintf("cpu%d", i))
ret = append(ret, c)
ret = append(ret, TimesStat{
CPU: fmt.Sprintf("cpu%d", i),
User: float64(stats.User) / ClocksPerSec,
Nice: float64(stats.Nice) / ClocksPerSec,
System: float64(stats.Sys) / ClocksPerSec,
Idle: float64(stats.Idle) / ClocksPerSec,
Irq: float64(stats.Intr) / ClocksPerSec,
})
}

return ret, nil
Expand Down
10 changes: 10 additions & 0 deletions cpu/cpu_openbsd_386.go
@@ -0,0 +1,10 @@
package cpu

type cpuTimes struct {
User uint32
Nice uint32
Sys uint32
Spin uint32
Intr uint32
Idle uint32
}
10 changes: 10 additions & 0 deletions cpu/cpu_openbsd_amd64.go
@@ -0,0 +1,10 @@
package cpu

type cpuTimes struct {
User uint64
Nice uint64
Sys uint64
Spin uint64
Intr uint64
Idle uint64
}

0 comments on commit 73f9c8d

Please sign in to comment.