architecture_windows.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package platform
  2. import (
  3. "fmt"
  4. "syscall"
  5. "unsafe"
  6. "golang.org/x/sys/windows"
  7. )
  8. var (
  9. modkernel32 = windows.NewLazySystemDLL("kernel32.dll")
  10. procGetSystemInfo = modkernel32.NewProc("GetSystemInfo")
  11. )
  12. // see http://msdn.microsoft.com/en-us/library/windows/desktop/ms724958(v=vs.85).aspx
  13. type systeminfo struct {
  14. wProcessorArchitecture uint16
  15. wReserved uint16
  16. dwPageSize uint32
  17. lpMinimumApplicationAddress uintptr
  18. lpMaximumApplicationAddress uintptr
  19. dwActiveProcessorMask uintptr
  20. dwNumberOfProcessors uint32
  21. dwProcessorType uint32
  22. dwAllocationGranularity uint32
  23. wProcessorLevel uint16
  24. wProcessorRevision uint16
  25. }
  26. // Constants
  27. const (
  28. ProcessorArchitecture64 = 9 // PROCESSOR_ARCHITECTURE_AMD64
  29. ProcessorArchitectureIA64 = 6 // PROCESSOR_ARCHITECTURE_IA64
  30. ProcessorArchitecture32 = 0 // PROCESSOR_ARCHITECTURE_INTEL
  31. ProcessorArchitectureArm = 5 // PROCESSOR_ARCHITECTURE_ARM
  32. )
  33. // runtimeArchitecture gets the name of the current architecture (x86, x86_64, …)
  34. func runtimeArchitecture() (string, error) {
  35. var sysinfo systeminfo
  36. syscall.Syscall(procGetSystemInfo.Addr(), 1, uintptr(unsafe.Pointer(&sysinfo)), 0, 0)
  37. switch sysinfo.wProcessorArchitecture {
  38. case ProcessorArchitecture64, ProcessorArchitectureIA64:
  39. return "x86_64", nil
  40. case ProcessorArchitecture32:
  41. return "i686", nil
  42. case ProcessorArchitectureArm:
  43. return "arm", nil
  44. default:
  45. return "", fmt.Errorf("Unknown processor architecture")
  46. }
  47. }
  48. // NumProcs returns the number of processors on the system
  49. func NumProcs() uint32 {
  50. var sysinfo systeminfo
  51. syscall.Syscall(procGetSystemInfo.Addr(), 1, uintptr(unsafe.Pointer(&sysinfo)), 0, 0)
  52. return sysinfo.dwNumberOfProcessors
  53. }