refactor TimesWithContext

don't make assumptions on which CPUs are online and wich aren't based
on hw.smt and hw.ncpuonline.  Rather, use KERN_CPUSTATS to get the CPU
statistics, which includes a flag field that can tell us if that CPU
is online or not.
pull/1244/head
Omar Polo 3 years ago
parent 16cc7d7d73
commit 57d5711d44

@ -7,7 +7,6 @@ import (
"context" "context"
"fmt" "fmt"
"runtime" "runtime"
"syscall"
"unsafe" "unsafe"
"github.com/shirou/gopsutil/v3/internal/common" "github.com/shirou/gopsutil/v3/internal/common"
@ -26,13 +25,14 @@ const (
cpIntr = 4 cpIntr = 4
cpIdle = 5 cpIdle = 5
cpuStates = 6 cpuStates = 6
cpuOnline = 0x0001 // CPUSTATS_ONLINE
// sys/sysctl.h // sys/sysctl.h
ctlKern = 1 // "high kernel": proc, limits ctlKern = 1 // "high kernel": proc, limits
ctlHw = 6 // CTL_HW ctlHw = 6 // CTL_HW
smt = 24 // HW_SMT smt = 24 // HW_SMT
kernCptime = 40 // KERN_CPTIME kernCpTime = 40 // KERN_CPTIME
kernCptime2 = 71 // KERN_CPTIME2 kernCPUStats = 85 // KERN_CPUSTATS
) )
var ClocksPerSec = float64(128) var ClocksPerSec = float64(128)
@ -45,87 +45,66 @@ func init() {
} }
} }
func smtEnabled() (bool, error) {
mib := []int32{ctlHw, smt}
buf, _, err := common.CallSyscall(mib)
if err != nil {
return false, err
}
smt := *(*uint32)(unsafe.Pointer(&buf[0]))
return smt == 1, nil
}
func Times(percpu bool) ([]TimesStat, error) { func Times(percpu bool) ([]TimesStat, error) {
return TimesWithContext(context.Background(), percpu) return TimesWithContext(context.Background(), percpu)
} }
func TimesWithContext(ctx context.Context, percpu bool) ([]TimesStat, error) { func cpsToTS(cpuTimes [cpuStates]uint64, name string) TimesStat {
var ret []TimesStat return TimesStat{
CPU: name,
var ncpu int User: float64(cpuTimes[cpUser]) / ClocksPerSec,
if percpu { Nice: float64(cpuTimes[cpNice]) / ClocksPerSec,
ncpu, _ = Counts(true) System: float64(cpuTimes[cpSys]) / ClocksPerSec,
} else { Idle: float64(cpuTimes[cpIdle]) / ClocksPerSec,
ncpu = 1 Irq: float64(cpuTimes[cpIntr]) / ClocksPerSec,
} }
smt, err := smtEnabled()
if err == syscall.EOPNOTSUPP {
// if hw.smt is not applicable for this platform (e.g. i386),
// pretend it's enabled
smt = true
} else if err != nil {
return nil, err
} }
for i := 0; i < ncpu; i++ { func TimesWithContext(ctx context.Context, percpu bool) (ret []TimesStat, err error) {
j := i cpuTimes := [cpuStates]uint64{}
if !smt {
j *= 2
}
var mib []int32 if !percpu {
if percpu { mib := []int32{ctlKern, kernCpTime}
mib = []int32{ctlKern, kernCptime2, int32(j)}
} else {
mib = []int32{ctlKern, kernCptime}
}
buf, _, err := common.CallSyscall(mib) buf, _, err := common.CallSyscall(mib)
if err != nil { if err != nil {
return ret, err return ret, err
} }
var cpuTimes [cpuStates]uint64
if percpu {
// could use unsafe.Slice but it's only for go1.17+
var x []uint64
x = (*[cpuStates]uint64)(unsafe.Pointer(&buf[0]))[:]
for i := range x {
cpuTimes[i] = x[i]
}
} else {
// KERN_CPTIME yields long[CPUSTATES] and `long' is
// platform dependent
var x []C.long var x []C.long
// could use unsafe.Slice but it's only for go1.17+
x = (*[cpuStates]C.long)(unsafe.Pointer(&buf[0]))[:] x = (*[cpuStates]C.long)(unsafe.Pointer(&buf[0]))[:]
for i := range x { for i := range x {
cpuTimes[i] = uint64(x[i]) cpuTimes[i] = uint64(x[i])
} }
c := cpsToTS(cpuTimes, "cpu-total")
return []TimesStat{c}, nil
} }
c := TimesStat{ ncpu, err := unix.SysctlUint32("hw.ncpu")
User: float64(cpuTimes[cpUser]) / ClocksPerSec, if err != nil {
Nice: float64(cpuTimes[cpNice]) / ClocksPerSec, return
System: float64(cpuTimes[cpSys]) / ClocksPerSec, }
Idle: float64(cpuTimes[cpIdle]) / ClocksPerSec,
Irq: float64(cpuTimes[cpIntr]) / ClocksPerSec, var i uint32
for i = 0; i < ncpu; i++ {
mib := []int32{ctlKern, kernCPUStats, int32(i)}
buf, _, err := common.CallSyscall(mib)
if err != nil {
return ret, err
}
data := unsafe.Pointer(&buf[0])
fptr := unsafe.Pointer(uintptr(data) + uintptr(8*cpuStates))
flags := *(*uint64)(fptr)
if (flags & cpuOnline) == 0 {
continue
} }
if percpu {
c.CPU = fmt.Sprintf("cpu%d", j) var x []uint64
} else { x = (*[cpuStates]uint64)(data)[:]
c.CPU = "cpu-total" for i := range x {
cpuTimes[i] = x[i]
} }
c := cpsToTS(cpuTimes, fmt.Sprintf("cpu%d", i))
ret = append(ret, c) ret = append(ret, c)
} }

Loading…
Cancel
Save