mirror of
https://github.com/shirou/gopsutil.git
synced 2025-05-02 22:17:08 +08:00
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.
This commit is contained in:
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,
|
||||||
|
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,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var ncpu int
|
func TimesWithContext(ctx context.Context, percpu bool) (ret []TimesStat, err error) {
|
||||||
if percpu {
|
cpuTimes := [cpuStates]uint64{}
|
||||||
ncpu, _ = Counts(true)
|
|
||||||
} else {
|
if !percpu {
|
||||||
ncpu = 1
|
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]))[:]
|
||||||
|
for i := range x {
|
||||||
|
cpuTimes[i] = uint64(x[i])
|
||||||
|
}
|
||||||
|
c := cpsToTS(cpuTimes, "cpu-total")
|
||||||
|
return []TimesStat{c}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
smt, err := smtEnabled()
|
ncpu, err := unix.SysctlUint32("hw.ncpu")
|
||||||
if err == syscall.EOPNOTSUPP {
|
if err != nil {
|
||||||
// if hw.smt is not applicable for this platform (e.g. i386),
|
return
|
||||||
// pretend it's enabled
|
|
||||||
smt = true
|
|
||||||
} else if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := 0; i < ncpu; i++ {
|
var i uint32
|
||||||
j := i
|
for i = 0; i < ncpu; i++ {
|
||||||
if !smt {
|
mib := []int32{ctlKern, kernCPUStats, int32(i)}
|
||||||
j *= 2
|
|
||||||
}
|
|
||||||
|
|
||||||
var mib []int32
|
|
||||||
if percpu {
|
|
||||||
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
|
data := unsafe.Pointer(&buf[0])
|
||||||
if percpu {
|
fptr := unsafe.Pointer(uintptr(data) + uintptr(8*cpuStates))
|
||||||
// could use unsafe.Slice but it's only for go1.17+
|
flags := *(*uint64)(fptr)
|
||||||
var x []uint64
|
if (flags & cpuOnline) == 0 {
|
||||||
x = (*[cpuStates]uint64)(unsafe.Pointer(&buf[0]))[:]
|
continue
|
||||||
for i := range x {
|
|
||||||
cpuTimes[i] = x[i]
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// KERN_CPTIME yields long[CPUSTATES] and `long' is
|
|
||||||
// platform dependent
|
|
||||||
var x []C.long
|
|
||||||
x = (*[cpuStates]C.long)(unsafe.Pointer(&buf[0]))[:]
|
|
||||||
for i := range x {
|
|
||||||
cpuTimes[i] = uint64(x[i])
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
c := TimesStat{
|
var x []uint64
|
||||||
User: float64(cpuTimes[cpUser]) / ClocksPerSec,
|
x = (*[cpuStates]uint64)(data)[:]
|
||||||
Nice: float64(cpuTimes[cpNice]) / ClocksPerSec,
|
for i := range x {
|
||||||
System: float64(cpuTimes[cpSys]) / ClocksPerSec,
|
cpuTimes[i] = x[i]
|
||||||
Idle: float64(cpuTimes[cpIdle]) / ClocksPerSec,
|
|
||||||
Irq: float64(cpuTimes[cpIntr]) / ClocksPerSec,
|
|
||||||
}
|
|
||||||
if percpu {
|
|
||||||
c.CPU = fmt.Sprintf("cpu%d", j)
|
|
||||||
} else {
|
|
||||||
c.CPU = "cpu-total"
|
|
||||||
}
|
}
|
||||||
|
c := cpsToTS(cpuTimes, fmt.Sprintf("cpu%d", i))
|
||||||
ret = append(ret, c)
|
ret = append(ret, c)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user