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"
|
|
|
|
"unsafe"
|
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.
|
2017-05-23 10:22:32 -04:00
|
|
|
pid, _, _ := unix.RawSyscall(unix.SYS_GETPID, 0, 0, 0)
|
2016-06-25 10:26:00 -04:00
|
|
|
|
|
|
|
var mask [1024 / 64]uintptr
|
2017-05-23 10:22:32 -04:00
|
|
|
_, _, err := unix.RawSyscall(unix.SYS_SCHED_GETAFFINITY, pid, uintptr(len(mask)*8), uintptr(unsafe.Pointer(&mask[0])))
|
2016-06-25 10:26:00 -04:00
|
|
|
if err != 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// For every available thread a bit is set in the mask.
|
|
|
|
ncpu := 0
|
|
|
|
for _, e := range mask {
|
|
|
|
if e == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ncpu += int(popcnt(uint64(e)))
|
|
|
|
}
|
|
|
|
return ncpu
|
|
|
|
}
|
|
|
|
|
|
|
|
// NumCPU returns the number of CPUs which are currently online
|
|
|
|
func NumCPU() int {
|
|
|
|
if ncpu := numCPU(); ncpu > 0 {
|
|
|
|
return ncpu
|
|
|
|
}
|
|
|
|
return runtime.NumCPU()
|
|
|
|
}
|