2018-02-05 16:05:59 -05:00
|
|
|
package sysinfo // import "github.com/docker/docker/pkg/sysinfo"
|
2016-06-25 10:26:00 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"runtime"
|
2017-05-23 10:22:32 -04:00
|
|
|
|
|
|
|
"golang.org/x/sys/unix"
|
2016-06-25 10:26:00 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// numCPU queries the system for the count of threads available
|
|
|
|
// for use to this process.
|
|
|
|
//
|
|
|
|
// Issues two syscalls.
|
|
|
|
// Returns 0 on errors. Use |runtime.NumCPU| in that case.
|
|
|
|
func numCPU() int {
|
|
|
|
// Gets the affinity mask for a process: The very one invoking this function.
|
2019-06-18 13:26:56 -04:00
|
|
|
pid := unix.Getpid()
|
2016-06-25 10:26:00 -04:00
|
|
|
|
2019-06-18 13:26:56 -04:00
|
|
|
var mask unix.CPUSet
|
|
|
|
err := unix.SchedGetaffinity(pid, &mask)
|
|
|
|
if err != nil {
|
2016-06-25 10:26:00 -04:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2019-06-18 13:26:56 -04:00
|
|
|
return mask.Count()
|
2016-06-25 10:26:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// NumCPU returns the number of CPUs which are currently online
|
|
|
|
func NumCPU() int {
|
|
|
|
if ncpu := numCPU(); ncpu > 0 {
|
|
|
|
return ncpu
|
|
|
|
}
|
|
|
|
return runtime.NumCPU()
|
|
|
|
}
|