diff --git a/cpu/cpu_test.go b/cpu/cpu_test.go index 2e4dd30..586e0e2 100644 --- a/cpu/cpu_test.go +++ b/cpu/cpu_test.go @@ -79,24 +79,27 @@ func TestTimes(t *testing.T) { } func TestCounts(t *testing.T) { - v, err := Counts(true) + logicalCount, err := Counts(true) skipIfNotImplementedErr(t, err) if err != nil { t.Errorf("error %v", err) } - if v == 0 { - t.Errorf("could not get logical CPU counts: %v", v) + if logicalCount == 0 { + t.Errorf("could not get logical CPU counts: %v", logicalCount) } - t.Logf("logical cores: %d", v) - v, err = Counts(false) + t.Logf("logical cores: %d", logicalCount) + physicalCount, err := Counts(false) skipIfNotImplementedErr(t, err) if err != nil { t.Errorf("error %v", err) } - if v == 0 { - t.Errorf("could not get physical CPU counts: %v", v) + if physicalCount == 0 { + 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) { diff --git a/cpu/cpu_windows.go b/cpu/cpu_windows.go index 4476b91..7cecd7e 100644 --- a/cpu/cpu_windows.go +++ b/cpu/cpu_windows.go @@ -8,6 +8,7 @@ import ( "fmt" "unsafe" + cpuid "github.com/klauspost/cpuid/v2" "github.com/shirou/gopsutil/v4/internal/common" "github.com/yusufpapurcu/wmi" "golang.org/x/sys/windows" @@ -198,30 +199,34 @@ type systemInfo struct { wProcessorRevision uint16 } -func CountsWithContext(ctx context.Context, logical bool) (int, error) { - if logical { - // https://github.com/giampaolo/psutil/blob/d01a9eaa35a8aadf6c519839e987a49d8be2d891/psutil/_psutil_windows.c#L97 - ret := windows.GetActiveProcessorCount(windows.ALL_PROCESSOR_GROUPS) - if ret != 0 { - return int(ret), nil - } - var systemInfo systemInfo - _, _, err := procGetNativeSystemInfo.Call(uintptr(unsafe.Pointer(&systemInfo))) - if systemInfo.dwNumberOfProcessors == 0 { - return 0, err - } - return int(systemInfo.dwNumberOfProcessors), nil +func getNumberOfLogicalCores() (int, error) { + // https://github.com/giampaolo/psutil/blob/d01a9eaa35a8aadf6c519839e987a49d8be2d891/psutil/_psutil_windows.c#L97 + ret := windows.GetActiveProcessorCount(windows.ALL_PROCESSOR_GROUPS) + if ret != 0 { + return int(ret), 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… - var dst []win32_Processor - q := wmi.CreateQuery(&dst, "") - if err := common.WMIQueryWithContext(ctx, q, &dst); err != nil { + var systemInfo systemInfo + _, _, err := procGetNativeSystemInfo.Call(uintptr(unsafe.Pointer(&systemInfo))) + if systemInfo.dwNumberOfProcessors == 0 { return 0, err } - var count uint32 - for _, d := range dst { - count += d.NumberOfCores + + numberOfLogicalProcessors := systemInfo.dwNumberOfProcessors + return int(numberOfLogicalProcessors), nil +} + +func CountsWithContext(ctx context.Context, logical bool) (int, error) { + numberOfLogicalProcessors, err := getNumberOfLogicalCores() + if err != nil { + return 0, err } - return int(count), nil + if logical { + return int(numberOfLogicalProcessors), 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 } diff --git a/go.mod b/go.mod index 12df66f..7645122 100644 --- a/go.mod +++ b/go.mod @@ -16,6 +16,7 @@ require ( require ( github.com/davecgh/go-spew v1.1.1 // 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/tklauser/numcpus v0.6.1 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect diff --git a/go.sum b/go.sum index 3d6558c..0374b38 100644 --- a/go.sum +++ b/go.sum @@ -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.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= 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/go.mod h1:zJYVVT2jmtg6P3p1VtQj7WsuWi/y4VnjVBn7F8KPB3I= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=