numcpu_linux.go 696 B

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