mirror of
https://github.com/shirou/gopsutil.git
synced 2025-04-29 13:49:21 +08:00
Fix cpu stats when hw.smt is enabled
When hw.smt is enabled, and it's enabled by default from 6.4, the number of cpus given by `runtime.NumCPU()` is half of the total: only the cpuN with N = 0,2,4,... are used by the system. We need to detect that and ask for the correct stats.
This commit is contained in:
parent
12d92847cf
commit
932f2f6049
@ -29,6 +29,7 @@ var (
|
|||||||
const (
|
const (
|
||||||
CTLKern = 1 // "high kernel": proc, limits
|
CTLKern = 1 // "high kernel": proc, limits
|
||||||
CTLHw = 6 // CTL_HW
|
CTLHw = 6 // CTL_HW
|
||||||
|
SMT = 24 // HW_SMT
|
||||||
NCpuOnline = 25 // HW_NCPUONLINE
|
NCpuOnline = 25 // HW_NCPUONLINE
|
||||||
KernCptime = 40 // KERN_CPTIME
|
KernCptime = 40 // KERN_CPTIME
|
||||||
KernCptime2 = 71 // KERN_CPTIME2
|
KernCptime2 = 71 // KERN_CPTIME2
|
||||||
@ -69,6 +70,22 @@ func init() {
|
|||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func smt() (bool, error) {
|
||||||
|
mib := []int32{CTLHw, SMT}
|
||||||
|
buf, _, err := common.CallSyscall(mib)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var ret bool
|
||||||
|
br := bytes.NewReader(buf)
|
||||||
|
if err := binary.Read(br, binary.LittleEndian, ret); err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret, nil
|
||||||
|
}
|
||||||
|
|
||||||
func Times(percpu bool) ([]TimesStat, error) {
|
func Times(percpu bool) ([]TimesStat, error) {
|
||||||
return TimesWithContext(context.Background(), percpu)
|
return TimesWithContext(context.Background(), percpu)
|
||||||
}
|
}
|
||||||
@ -83,11 +100,21 @@ func TimesWithContext(ctx context.Context, percpu bool) ([]TimesStat, error) {
|
|||||||
ncpu = 1
|
ncpu = 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
smt, err := smt()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
for i := 0; i < ncpu; i++ {
|
for i := 0; i < ncpu; i++ {
|
||||||
|
j := i
|
||||||
|
if !smt {
|
||||||
|
j *= 2
|
||||||
|
}
|
||||||
|
|
||||||
var cpuTimes = make([]int64, CPUStates)
|
var cpuTimes = make([]int64, CPUStates)
|
||||||
var mib []int32
|
var mib []int32
|
||||||
if percpu {
|
if percpu {
|
||||||
mib = []int32{CTLKern, KernCptime2, int32(i)}
|
mib = []int32{CTLKern, KernCptime2, int32(j)}
|
||||||
} else {
|
} else {
|
||||||
mib = []int32{CTLKern, KernCptime}
|
mib = []int32{CTLKern, KernCptime}
|
||||||
}
|
}
|
||||||
@ -109,7 +136,7 @@ func TimesWithContext(ctx context.Context, percpu bool) ([]TimesStat, error) {
|
|||||||
Irq: float64(cpuTimes[CPIntr]) / ClocksPerSec,
|
Irq: float64(cpuTimes[CPIntr]) / ClocksPerSec,
|
||||||
}
|
}
|
||||||
if percpu {
|
if percpu {
|
||||||
c.CPU = fmt.Sprintf("cpu%d", i)
|
c.CPU = fmt.Sprintf("cpu%d", j)
|
||||||
} else {
|
} else {
|
||||||
c.CPU = "cpu-total"
|
c.CPU = "cpu-total"
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user