github.com/sams1990/dockerrepo@v17.12.1-ce-rc2+incompatible/pkg/sysinfo/numcpu_windows.go (about)

     1  // +build windows
     2  
     3  package sysinfo
     4  
     5  import (
     6  	"runtime"
     7  	"unsafe"
     8  
     9  	"golang.org/x/sys/windows"
    10  )
    11  
    12  var (
    13  	kernel32               = windows.NewLazySystemDLL("kernel32.dll")
    14  	getCurrentProcess      = kernel32.NewProc("GetCurrentProcess")
    15  	getProcessAffinityMask = kernel32.NewProc("GetProcessAffinityMask")
    16  )
    17  
    18  func numCPU() int {
    19  	// Gets the affinity mask for a process
    20  	var mask, sysmask uintptr
    21  	currentProcess, _, _ := getCurrentProcess.Call()
    22  	ret, _, _ := getProcessAffinityMask.Call(currentProcess, uintptr(unsafe.Pointer(&mask)), uintptr(unsafe.Pointer(&sysmask)))
    23  	if ret == 0 {
    24  		return 0
    25  	}
    26  	// For every available thread a bit is set in the mask.
    27  	ncpu := int(popcnt(uint64(mask)))
    28  	return ncpu
    29  }
    30  
    31  // NumCPU returns the number of CPUs which are currently online
    32  func NumCPU() int {
    33  	if ncpu := numCPU(); ncpu > 0 {
    34  		return ncpu
    35  	}
    36  	return runtime.NumCPU()
    37  }