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