numcpu_windows.go 978 B

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