github.com/Prakhar-Agarwal-byte/moby@v0.0.0-20231027092010-a14e3e8ab87e/pkg/sysinfo/numcpu_windows.go (about) 1 package sysinfo // import "github.com/Prakhar-Agarwal-byte/moby/pkg/sysinfo" 2 3 import ( 4 "unsafe" 5 6 "golang.org/x/sys/windows" 7 ) 8 9 var ( 10 kernel32 = windows.NewLazySystemDLL("kernel32.dll") 11 getCurrentProcess = kernel32.NewProc("GetCurrentProcess") 12 getProcessAffinityMask = kernel32.NewProc("GetProcessAffinityMask") 13 ) 14 15 // Returns bit count of 1, used by NumCPU 16 func popcnt(x uint64) (n byte) { 17 x -= (x >> 1) & 0x5555555555555555 18 x = (x>>2)&0x3333333333333333 + x&0x3333333333333333 19 x += x >> 4 20 x &= 0x0f0f0f0f0f0f0f0f 21 x *= 0x0101010101010101 22 return byte(x >> 56) 23 } 24 25 func numCPU() int { 26 // Gets the affinity mask for a process 27 var mask, sysmask uintptr 28 currentProcess, _, _ := getCurrentProcess.Call() 29 ret, _, _ := getProcessAffinityMask.Call(currentProcess, uintptr(unsafe.Pointer(&mask)), uintptr(unsafe.Pointer(&sysmask))) 30 if ret == 0 { 31 return 0 32 } 33 // For every available thread a bit is set in the mask. 34 ncpu := int(popcnt(uint64(mask))) 35 return ncpu 36 }