github.com/docker/docker@v299999999.0.0-20200612211812-aaf470eca7b5+incompatible/pkg/sysinfo/numcpu_linux.go (about)

     1  package sysinfo // import "github.com/docker/docker/pkg/sysinfo"
     2  
     3  import (
     4  	"runtime"
     5  
     6  	"golang.org/x/sys/unix"
     7  )
     8  
     9  // numCPU queries the system for the count of threads available
    10  // for use to this process.
    11  //
    12  // Issues two syscalls.
    13  // Returns 0 on errors. Use |runtime.NumCPU| in that case.
    14  func numCPU() int {
    15  	// Gets the affinity mask for a process: The very one invoking this function.
    16  	pid := unix.Getpid()
    17  
    18  	var mask unix.CPUSet
    19  	err := unix.SchedGetaffinity(pid, &mask)
    20  	if err != nil {
    21  		return 0
    22  	}
    23  
    24  	return mask.Count()
    25  }
    26  
    27  // NumCPU returns the number of CPUs which are currently online
    28  func NumCPU() int {
    29  	if ncpu := numCPU(); ncpu > 0 {
    30  		return ncpu
    31  	}
    32  	return runtime.NumCPU()
    33  }