platform_windows.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package platform // import "github.com/docker/docker/pkg/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 https://learn.microsoft.com/en-gb/windows/win32/api/sysinfoapi/ns-sysinfoapi-system_info
  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. // Windows processor architectures.
  27. //
  28. // see https://github.com/microsoft/go-winio/blob/v0.6.0/wim/wim.go#L48-L65
  29. // see https://learn.microsoft.com/en-gb/windows/win32/api/sysinfoapi/ns-sysinfoapi-system_info
  30. const (
  31. processorArchitecture64 = 9 // PROCESSOR_ARCHITECTURE_AMD64
  32. processorArchitectureIA64 = 6 // PROCESSOR_ARCHITECTURE_IA64
  33. processorArchitecture32 = 0 // PROCESSOR_ARCHITECTURE_INTEL
  34. processorArchitectureArm = 5 // PROCESSOR_ARCHITECTURE_ARM
  35. processorArchitectureArm64 = 12 // PROCESSOR_ARCHITECTURE_ARM64
  36. )
  37. // runtimeArchitecture gets the name of the current architecture (x86, x86_64, …)
  38. func runtimeArchitecture() (string, error) {
  39. // TODO(thaJeztah): rewrite this to use "GetNativeSystemInfo" instead.
  40. // See: https://learn.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getsysteminfo
  41. // See: https://github.com/shirou/gopsutil/blob/v3.23.3/host/host_windows.go#L267-L297
  42. // > To retrieve accurate information for an application running on WOW64,
  43. // > call the GetNativeSystemInfo function.
  44. var sysinfo systeminfo
  45. _, _, _ = syscall.SyscallN(procGetSystemInfo.Addr(), uintptr(unsafe.Pointer(&sysinfo)))
  46. switch sysinfo.wProcessorArchitecture {
  47. case processorArchitecture64, processorArchitectureIA64:
  48. return "x86_64", nil
  49. case processorArchitecture32:
  50. return "i686", nil
  51. case processorArchitectureArm:
  52. return "arm", nil
  53. case processorArchitectureArm64:
  54. return "arm64", nil
  55. default:
  56. return "", fmt.Errorf("unknown processor architecture %+v", sysinfo.wProcessorArchitecture)
  57. }
  58. }
  59. // NumProcs returns the number of processors on the system
  60. func NumProcs() uint32 {
  61. var sysinfo systeminfo
  62. _, _, _ = syscall.SyscallN(procGetSystemInfo.Addr(), uintptr(unsafe.Pointer(&sysinfo)))
  63. return sysinfo.dwNumberOfProcessors
  64. }