2023-07-17 17:09:13 +00:00
# Limit compiler/linker job concurrency to avoid OOMs on subtrees where compilation/linking is memory-intensive.
#
2023-07-17 10:33:09 +00:00
# Usage from CMake:
2023-07-17 17:09:13 +00:00
# set (MAX_COMPILER_MEMORY 2000 CACHE INTERNAL "") # megabyte
# set (MAX_LINKER_MEMORY 3500 CACHE INTERNAL "") # megabyte
# include (cmake/limit_jobs.cmake)
#
# (bigger values mean fewer jobs)
2018-12-23 14:19:11 +00:00
2023-07-17 11:52:00 +00:00
cmake_host_system_information ( RESULT TOTAL_PHYSICAL_MEMORY QUERY TOTAL_PHYSICAL_MEMORY )
2019-01-11 12:40:19 +00:00
cmake_host_system_information ( RESULT NUMBER_OF_LOGICAL_CORES QUERY NUMBER_OF_LOGICAL_CORES )
2018-12-23 14:19:11 +00:00
2023-07-17 10:33:09 +00:00
# Set to disable the automatic job-limiting
option ( PARALLEL_COMPILE_JOBS "Maximum number of concurrent compilation jobs" OFF )
option ( PARALLEL_LINK_JOBS "Maximum number of concurrent link jobs" OFF )
2020-09-17 15:37:23 +00:00
2023-07-17 11:52:00 +00:00
if ( NOT PARALLEL_COMPILE_JOBS AND MAX_COMPILER_MEMORY )
2022-08-13 04:39:20 +00:00
math ( EXPR PARALLEL_COMPILE_JOBS ${ TOTAL_PHYSICAL_MEMORY } / ${ MAX_COMPILER_MEMORY } )
2020-09-17 15:37:23 +00:00
2018-12-23 14:19:11 +00:00
if ( NOT PARALLEL_COMPILE_JOBS )
set ( PARALLEL_COMPILE_JOBS 1 )
endif ( )
2023-07-17 11:53:12 +00:00
if ( PARALLEL_COMPILE_JOBS LESS NUMBER_OF_LOGICAL_CORES )
2023-11-19 14:43:09 +00:00
message ( "The auto-calculated compile jobs limit (${PARALLEL_COMPILE_JOBS}) underutilizes CPU cores (${NUMBER_OF_LOGICAL_CORES}). Set PARALLEL_COMPILE_JOBS to override." )
2023-07-02 14:51:43 +00:00
endif ( )
2018-12-23 14:19:11 +00:00
endif ( )
2020-09-17 15:37:23 +00:00
2023-07-17 11:52:00 +00:00
if ( NOT PARALLEL_LINK_JOBS AND MAX_LINKER_MEMORY )
2022-08-13 04:39:20 +00:00
math ( EXPR PARALLEL_LINK_JOBS ${ TOTAL_PHYSICAL_MEMORY } / ${ MAX_LINKER_MEMORY } )
2020-09-17 15:37:23 +00:00
2018-12-23 14:19:11 +00:00
if ( NOT PARALLEL_LINK_JOBS )
set ( PARALLEL_LINK_JOBS 1 )
endif ( )
2023-07-17 11:53:12 +00:00
if ( PARALLEL_LINK_JOBS LESS NUMBER_OF_LOGICAL_CORES )
2023-11-19 14:43:09 +00:00
message ( "The auto-calculated link jobs limit (${PARALLEL_LINK_JOBS}) underutilizes CPU cores (${NUMBER_OF_LOGICAL_CORES}). Set PARALLEL_LINK_JOBS to override." )
2023-07-02 14:51:43 +00:00
endif ( )
2018-12-23 14:19:11 +00:00
endif ( )
2020-09-17 15:37:23 +00:00
2020-12-10 18:37:12 +00:00
# ThinLTO provides its own parallel linking
2020-12-24 19:51:34 +00:00
# (which is enabled only for RELWITHDEBINFO)
#
2020-12-10 18:37:12 +00:00
# But use 2 parallel jobs, since:
# - this is what llvm does
# - and I've verfied that lld-11 does not use all available CPU time (in peak) while linking one binary
2020-12-24 19:51:34 +00:00
if ( CMAKE_BUILD_TYPE_UC STREQUAL "RELWITHDEBINFO" AND ENABLE_THINLTO AND PARALLEL_LINK_JOBS GREATER 2 )
2020-12-10 18:37:12 +00:00
message ( STATUS "ThinLTO provides its own parallel linking - limiting parallel link jobs to 2." )
set ( PARALLEL_LINK_JOBS 2 )
endif ( )
2023-09-11 03:35:15 +00:00
message ( STATUS "Building sub-tree with ${PARALLEL_COMPILE_JOBS} compile jobs and ${PARALLEL_LINK_JOBS} linker jobs (system: ${NUMBER_OF_LOGICAL_CORES} cores, ${TOTAL_PHYSICAL_MEMORY} MB RAM, 'OFF' means the native core count)." )
2018-12-23 14:19:11 +00:00
2023-07-17 12:01:17 +00:00
if ( PARALLEL_COMPILE_JOBS LESS NUMBER_OF_LOGICAL_CORES )
2023-07-17 10:33:09 +00:00
set ( CMAKE_JOB_POOL_COMPILE compile_job_pool ${ CMAKE_CURRENT_SOURCE_DIR } )
string ( REGEX REPLACE "[^a-zA-Z0-9]+" "_" CMAKE_JOB_POOL_COMPILE ${ CMAKE_JOB_POOL_COMPILE } )
set_property ( GLOBAL APPEND PROPERTY JOB_POOLS ${ CMAKE_JOB_POOL_COMPILE } = ${ PARALLEL_COMPILE_JOBS } )
endif ( )
2023-07-17 12:01:17 +00:00
if ( PARALLEL_LINK_JOBS LESS NUMBER_OF_LOGICAL_CORES )
2023-07-17 10:33:09 +00:00
set ( CMAKE_JOB_POOL_LINK link_job_pool ${ CMAKE_CURRENT_SOURCE_DIR } )
string ( REGEX REPLACE "[^a-zA-Z0-9]+" "_" CMAKE_JOB_POOL_LINK ${ CMAKE_JOB_POOL_LINK } )
set_property ( GLOBAL APPEND PROPERTY JOB_POOLS ${ CMAKE_JOB_POOL_LINK } = ${ PARALLEL_LINK_JOBS } )
endif ( )