1
0
mirror of https://github.com/shirou/gopsutil.git synced 2025-04-29 13:49:21 +08:00

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.
This commit is contained in:
Omar Polo 2022-02-24 14:59:20 +01:00
parent 3c3c017f23
commit 73f9c8dfd5
3 changed files with 52 additions and 37 deletions

View File

@ -14,17 +14,8 @@ import (
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
) )
import "C"
const ( const (
// sys/sched.h // sys/sched.h
cpUser = 0
cpNice = 1
cpSys = 2
cpSpin = 3
cpIntr = 4
cpIdle = 5
cpuStates = 6
cpuOnline = 0x0001 // CPUSTATS_ONLINE cpuOnline = 0x0001 // CPUSTATS_ONLINE
// sys/sysctl.h // sys/sysctl.h
@ -37,6 +28,19 @@ const (
var ClocksPerSec = float64(128) 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() { func init() {
clkTck, err := sysconf.Sysconf(sysconf.SC_CLK_TCK) clkTck, err := sysconf.Sysconf(sysconf.SC_CLK_TCK)
// ignore errors // ignore errors
@ -49,17 +53,6 @@ func Times(percpu bool) ([]TimesStat, error) {
return TimesWithContext(context.Background(), percpu) 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) { func TimesWithContext(ctx context.Context, percpu bool) (ret []TimesStat, err error) {
if !percpu { if !percpu {
mib := []int32{ctlKern, kernCpTime} mib := []int32{ctlKern, kernCpTime}
@ -67,15 +60,16 @@ func TimesWithContext(ctx context.Context, percpu bool) (ret []TimesStat, err er
if err != nil { if err != nil {
return ret, err return ret, err
} }
var x []C.long times := (*cpuTimes)(unsafe.Pointer(&buf[0]))
// could use unsafe.Slice but it's only for go1.17+ stat := TimesStat{
x = (*[cpuStates]C.long)(unsafe.Pointer(&buf[0]))[:] CPU: "cpu-total",
cpuTimes := [cpuStates]uint64{} User: float64(times.User) / ClocksPerSec,
for i := range x { Nice: float64(times.Nice) / ClocksPerSec,
cpuTimes[i] = uint64(x[i]) System: float64(times.Sys) / ClocksPerSec,
Idle: float64(times.Idle) / ClocksPerSec,
Irq: float64(times.Intr) / ClocksPerSec,
} }
c := cpsToTS(cpuTimes[:], "cpu-total") return []TimesStat{stat}, nil
return []TimesStat{c}, nil
} }
ncpu, err := unix.SysctlUint32("hw.ncpu") ncpu, err := unix.SysctlUint32("hw.ncpu")
@ -91,17 +85,18 @@ func TimesWithContext(ctx context.Context, percpu bool) (ret []TimesStat, err er
return ret, err return ret, err
} }
data := unsafe.Pointer(&buf[0]) stats := (*cpuStats)(unsafe.Pointer(&buf[0]))
fptr := unsafe.Pointer(uintptr(data) + uintptr(8*cpuStates)) if (stats.Flags & cpuOnline) == 0 {
flags := *(*uint64)(fptr)
if (flags & cpuOnline) == 0 {
continue continue
} }
ret = append(ret, TimesStat{
var x []uint64 CPU: fmt.Sprintf("cpu%d", i),
x = (*[cpuStates]uint64)(data)[:] User: float64(stats.User) / ClocksPerSec,
c := cpsToTS(x, fmt.Sprintf("cpu%d", i)) Nice: float64(stats.Nice) / ClocksPerSec,
ret = append(ret, c) System: float64(stats.Sys) / ClocksPerSec,
Idle: float64(stats.Idle) / ClocksPerSec,
Irq: float64(stats.Intr) / ClocksPerSec,
})
} }
return ret, nil return ret, nil

10
cpu/cpu_openbsd_386.go Normal file
View File

@ -0,0 +1,10 @@
package cpu
type cpuTimes struct {
User uint32
Nice uint32
Sys uint32
Spin uint32
Intr uint32
Idle uint32
}

10
cpu/cpu_openbsd_amd64.go Normal file
View File

@ -0,0 +1,10 @@
package cpu
type cpuTimes struct {
User uint64
Nice uint64
Sys uint64
Spin uint64
Intr uint64
Idle uint64
}