remove wmi to get number of physical cores

pull/1786/head
Stefano Balzarotti 4 months ago
parent 90e59961ca
commit 37a1761106
No known key found for this signature in database
GPG Key ID: 7E7FC24DA4B9F8C9

@ -79,24 +79,27 @@ func TestTimes(t *testing.T) {
} }
func TestCounts(t *testing.T) { func TestCounts(t *testing.T) {
v, err := Counts(true) logicalCount, err := Counts(true)
skipIfNotImplementedErr(t, err) skipIfNotImplementedErr(t, err)
if err != nil { if err != nil {
t.Errorf("error %v", err) t.Errorf("error %v", err)
} }
if v == 0 { if logicalCount == 0 {
t.Errorf("could not get logical CPU counts: %v", v) t.Errorf("could not get logical CPU counts: %v", logicalCount)
} }
t.Logf("logical cores: %d", v) t.Logf("logical cores: %d", logicalCount)
v, err = Counts(false) physicalCount, err := Counts(false)
skipIfNotImplementedErr(t, err) skipIfNotImplementedErr(t, err)
if err != nil { if err != nil {
t.Errorf("error %v", err) t.Errorf("error %v", err)
} }
if v == 0 { if physicalCount == 0 {
t.Errorf("could not get physical CPU counts: %v", v) t.Errorf("could not get physical CPU counts: %v", physicalCount)
}
t.Logf("physical cores: %d", physicalCount)
if physicalCount > logicalCount {
t.Errorf("physical cpu cannot be more than logical cpu: %v > %v", physicalCount, logicalCount)
} }
t.Logf("physical cores: %d", v)
} }
func TestTimeStat_String(t *testing.T) { func TestTimeStat_String(t *testing.T) {

@ -8,6 +8,7 @@ import (
"fmt" "fmt"
"unsafe" "unsafe"
cpuid "github.com/klauspost/cpuid/v2"
"github.com/shirou/gopsutil/v4/internal/common" "github.com/shirou/gopsutil/v4/internal/common"
"github.com/yusufpapurcu/wmi" "github.com/yusufpapurcu/wmi"
"golang.org/x/sys/windows" "golang.org/x/sys/windows"
@ -198,8 +199,7 @@ type systemInfo struct {
wProcessorRevision uint16 wProcessorRevision uint16
} }
func CountsWithContext(ctx context.Context, logical bool) (int, error) { func getNumberOfLogicalCores() (int, error) {
if logical {
// https://github.com/giampaolo/psutil/blob/d01a9eaa35a8aadf6c519839e987a49d8be2d891/psutil/_psutil_windows.c#L97 // https://github.com/giampaolo/psutil/blob/d01a9eaa35a8aadf6c519839e987a49d8be2d891/psutil/_psutil_windows.c#L97
ret := windows.GetActiveProcessorCount(windows.ALL_PROCESSOR_GROUPS) ret := windows.GetActiveProcessorCount(windows.ALL_PROCESSOR_GROUPS)
if ret != 0 { if ret != 0 {
@ -210,18 +210,23 @@ func CountsWithContext(ctx context.Context, logical bool) (int, error) {
if systemInfo.dwNumberOfProcessors == 0 { if systemInfo.dwNumberOfProcessors == 0 {
return 0, err return 0, err
} }
return int(systemInfo.dwNumberOfProcessors), nil
numberOfLogicalProcessors := systemInfo.dwNumberOfProcessors
return int(numberOfLogicalProcessors), nil
} }
// physical cores https://github.com/giampaolo/psutil/blob/d01a9eaa35a8aadf6c519839e987a49d8be2d891/psutil/_psutil_windows.c#L499
// for the time being, try with unreliable and slow WMI call… func CountsWithContext(ctx context.Context, logical bool) (int, error) {
var dst []win32_Processor numberOfLogicalProcessors, err := getNumberOfLogicalCores()
q := wmi.CreateQuery(&dst, "") if err != nil {
if err := common.WMIQueryWithContext(ctx, q, &dst); err != nil {
return 0, err return 0, err
} }
var count uint32 if logical {
for _, d := range dst { return int(numberOfLogicalProcessors), nil
count += d.NumberOfCores
} }
return int(count), nil
logicalCoresOfCurrentCPU := cpuid.CPU.LogicalCores // in a system with more cpu, we assume all cpu have the same number of cores
numberOfCPU := int(numberOfLogicalProcessors) / logicalCoresOfCurrentCPU
currentCPUPhysicalCores := numberOfCPU * cpuid.CPU.PhysicalCores // cpu get info about the processor where the code is running
return int(currentCPUPhysicalCores), nil
} }

@ -16,6 +16,7 @@ require (
require ( require (
github.com/davecgh/go-spew v1.1.1 // indirect github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect github.com/go-ole/go-ole v1.2.6 // indirect
github.com/klauspost/cpuid/v2 v2.2.9 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/tklauser/numcpus v0.6.1 // indirect github.com/tklauser/numcpus v0.6.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect

@ -7,6 +7,8 @@ github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiU
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/klauspost/cpuid/v2 v2.2.9 h1:66ze0taIn2H33fBvCkXuv9BmCwDfafmiIVpKV9kKGuY=
github.com/klauspost/cpuid/v2 v2.2.9/go.mod h1:rqkxqrZ1EhYM9G+hXH7YdowN5R5RGN6NK4QwQ3WMXF8=
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 h1:6E+4a0GO5zZEnZ81pIr0yLvtUWk2if982qA3F3QD6H4= github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 h1:6E+4a0GO5zZEnZ81pIr0yLvtUWk2if982qA3F3QD6H4=
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0/go.mod h1:zJYVVT2jmtg6P3p1VtQj7WsuWi/y4VnjVBn7F8KPB3I= github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0/go.mod h1:zJYVVT2jmtg6P3p1VtQj7WsuWi/y4VnjVBn7F8KPB3I=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=

Loading…
Cancel
Save