mirror of
https://github.com/shirou/gopsutil.git
synced 2025-04-29 13:49:21 +08:00
cpu: use windows.GetActiveProcessorCount
Use GetActiveProcessorCount and the ALL_PROCESSOR_GROUPS const provided in golang.org/x/sys/windows. The function is available on Windows 7 and later. Go requires Windows 7, see https://go.dev/doc/go1.11#ports
This commit is contained in:
parent
3b918ebd29
commit
f2bf9a480a
@ -14,7 +14,6 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
procGetActiveProcessorCount = common.Modkernel32.NewProc("GetActiveProcessorCount")
|
|
||||||
procGetNativeSystemInfo = common.Modkernel32.NewProc("GetNativeSystemInfo")
|
procGetNativeSystemInfo = common.Modkernel32.NewProc("GetNativeSystemInfo")
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -204,15 +203,12 @@ type systemInfo struct {
|
|||||||
func CountsWithContext(ctx context.Context, logical bool) (int, error) {
|
func CountsWithContext(ctx context.Context, logical bool) (int, error) {
|
||||||
if logical {
|
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
|
||||||
err := procGetActiveProcessorCount.Find()
|
ret := windows.GetActiveProcessorCount(windows.ALL_PROCESSOR_GROUPS)
|
||||||
if err == nil { // Win7+
|
|
||||||
ret, _, _ := procGetActiveProcessorCount.Call(uintptr(0xffff)) // ALL_PROCESSOR_GROUPS is 0xffff according to Rust's winapi lib https://docs.rs/winapi/*/x86_64-pc-windows-msvc/src/winapi/shared/ntdef.rs.html#120
|
|
||||||
if ret != 0 {
|
if ret != 0 {
|
||||||
return int(ret), nil
|
return int(ret), nil
|
||||||
}
|
}
|
||||||
}
|
|
||||||
var systemInfo systemInfo
|
var systemInfo systemInfo
|
||||||
_, _, err = procGetNativeSystemInfo.Call(uintptr(unsafe.Pointer(&systemInfo)))
|
_, _, err := procGetNativeSystemInfo.Call(uintptr(unsafe.Pointer(&systemInfo)))
|
||||||
if systemInfo.dwNumberOfProcessors == 0 {
|
if systemInfo.dwNumberOfProcessors == 0 {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user