ProcessorCount: Return the container CPU count instead of the host count

On Linux containers (tested with LXC and Docker) getconf returns the
host CPU count.
Use nproc with a higher priority if available to get the container's
allocated CPUs instead of the non-accessible host count.
This commit is contained in:
Sylvain Joubert
2019-02-27 12:47:07 +01:00
parent b3191a0f57
commit f20eab9cdc

View File

@@ -69,6 +69,20 @@ function(ProcessorCount var)
endif()
endif()
if(NOT count)
# Linux (systems with nproc):
# Prefer nproc to getconf if available as getconf may return the host CPU count in Linux containers
find_program(ProcessorCount_cmd_nproc nproc)
mark_as_advanced(ProcessorCount_cmd_nproc)
if(ProcessorCount_cmd_nproc)
execute_process(COMMAND ${ProcessorCount_cmd_nproc}
ERROR_QUIET
OUTPUT_STRIP_TRAILING_WHITESPACE
OUTPUT_VARIABLE count)
#message("ProcessorCount: trying nproc '${ProcessorCount_cmd_nproc}'")
endif()
endif()
if(NOT count)
# Linux (systems with getconf):
find_program(ProcessorCount_cmd_getconf getconf)