From: Zack Cerza Date: Thu, 4 Apr 2024 19:01:05 +0000 (-0600) Subject: cmake: Intelligently set job limits for sccache X-Git-Tag: v20.0.0~1990^2 X-Git-Url: http://git.apps.os.sepia.ceph.com/?a=commitdiff_plain;h=be709462ad41c445001d103b4c3fbf4aa4673947;p=ceph.git cmake: Intelligently set job limits for sccache If we are correctly configured for distributed mode, use the cluster's CPU count instead of ours. If we are configured for sccache but without distributed mode, inform the user but continue with normal job limits. Signed-off-by: Zack Cerza --- diff --git a/CMakeLists.txt b/CMakeLists.txt index b2a9334a5cfef..5f989e0a8401d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -85,17 +85,36 @@ endif(WITH_CCACHE) option(WITH_SCCACHE "Build with sccache.") if(WITH_SCCACHE) - if(CMAKE_C_COMPILER_LAUNCHER OR CMAKE_CXX_COMPILER_LAUNCHER) - message(WARNING "Compiler launcher already set. stop configuring sccache") - else() - find_program(SCCACHE_EXECUTABLE sccache) - if(NOT SCCACHE_EXECUTABLE) - message(FATAL_ERROR "Can't find sccache. Is it installed?") + find_program(SCCACHE_EXECUTABLE sccache) + if(NOT SCCACHE_EXECUTABLE) + message(FATAL_ERROR "Can't find sccache. Is it installed?") + endif() + if(NOT NINJA_MAX_COMPILE_JOBS) + if(CMAKE_VERSION VERSION_GREATER_EQUAL "3.19") + execute_process( + COMMAND "sccache" "--dist-status" + OUTPUT_VARIABLE sccache_dist_status + ) + string( + JSON sccache_cores + ERROR_VARIABLE sccache_dist_status_error + GET "${sccache_dist_status}" SchedulerStatus 1 num_cpus + ) + string(FIND "${sccache_dist_status}" "disabled" find_result) + if(find_result EQUAL -1) + message(STATUS "Using sccache with distributed compilation. Effective cores: ${sccache_cores}") + set(NINJA_MAX_COMPILE_JOBS ${sccache_cores}) + set(NINJA_MAX_LINK_JOBS ${sccache_cores}) + else() + message(WARNING "Using sccache, but it is not configured for distributed complilation") + endif() + else() + message(WARNING "Using sccache, but cannot determine maximum job value since cmake version is <3.19") endif() - message(STATUS "Building with sccache: ${SCCACHE_EXECUTABLE}, SCCACHE_CONF=$ENV{SCCACHE_CONF}") - set(CMAKE_C_COMPILER_LAUNCHER ${SCCACHE_EXECUTABLE}) - set(CMAKE_CXX_COMPILER_LAUNCHER ${SCCACHE_EXECUTABLE}) endif() + message(STATUS "Building with sccache: ${SCCACHE_EXECUTABLE}, SCCACHE_CONF=$ENV{SCCACHE_CONF}") + set(CMAKE_C_COMPILER_LAUNCHER ${SCCACHE_EXECUTABLE}) + set(CMAKE_CXX_COMPILER_LAUNCHER ${SCCACHE_EXECUTABLE}) endif(WITH_SCCACHE) option(WITH_MANPAGE "Build man pages." ON) diff --git a/cmake/modules/LimitJobs.cmake b/cmake/modules/LimitJobs.cmake index 591a9321b668d..2dcad24a806ad 100644 --- a/cmake/modules/LimitJobs.cmake +++ b/cmake/modules/LimitJobs.cmake @@ -4,16 +4,20 @@ set(MAX_LINK_MEM 4500 CACHE INTERNAL "maximum memory used by each linking job (i cmake_host_system_information(RESULT _num_cores QUERY NUMBER_OF_LOGICAL_CORES) cmake_host_system_information(RESULT _total_mem QUERY TOTAL_PHYSICAL_MEMORY) -math(EXPR _avg_compile_jobs "${_total_mem} / ${MAX_COMPILE_MEM}") -if(_avg_compile_jobs EQUAL 0) - set(_avg_compile_jobs 1) -endif() -if(_num_cores LESS _avg_compile_jobs) - set(_avg_compile_jobs ${_num_cores}) +if(NINJA_MAX_COMPILE_JOBS) + set(_avg_compile_jobs "${NINJA_MAX_COMPILE_JOBS}") +else() + math(EXPR _avg_compile_jobs "${_total_mem} / ${MAX_COMPILE_MEM}") + if(_avg_compile_jobs EQUAL 0) + set(_avg_compile_jobs 1) + endif() + if(_num_cores LESS _avg_compile_jobs) + set(_avg_compile_jobs "${_num_cores}") + endif() + set(NINJA_MAX_COMPILE_JOBS "${_avg_compile_jobs}" CACHE STRING + "The maximum number of concurrent compilation jobs, for Ninja build system." FORCE) + mark_as_advanced(NINJA_MAX_COMPILE_JOBS) endif() -set(NINJA_MAX_COMPILE_JOBS "${_avg_compile_jobs}" CACHE STRING - "The maximum number of concurrent compilation jobs, for Ninja build system." FORCE) -mark_as_advanced(NINJA_MAX_COMPILE_JOBS) if(NINJA_MAX_COMPILE_JOBS) math(EXPR _heavy_compile_jobs "${_avg_compile_jobs} / 2") if(_heavy_compile_jobs EQUAL 0) @@ -25,16 +29,20 @@ if(NINJA_MAX_COMPILE_JOBS) set(CMAKE_JOB_POOL_COMPILE avg_compile_job_pool) endif() -math(EXPR _avg_link_jobs "${_total_mem} / ${MAX_LINK_MEM}") -if(_avg_link_jobs EQUAL 0) - set(_avg_link_jobs 1) -endif() -if(_num_cores LESS _avg_link_jobs) - set(_avg_link_jobs ${_num_cores}) +if(NINJA_MAX_LINK_JOBS) + set(_avg_link_jobs "${NINJA_MAX_LINK_JOBS}") +else() + math(EXPR _avg_link_jobs "${_total_mem} / ${MAX_LINK_MEM}") + if(_avg_link_jobs EQUAL 0) + set(_avg_link_jobs 1) + endif() + if(_num_cores LESS _avg_link_jobs) + set(_avg_link_jobs "${_num_cores}") + endif() + set(NINJA_MAX_LINK_JOBS "${_avg_link_jobs}" CACHE STRING + "The maximum number of concurrent link jobs, for Ninja build system." FORCE) + mark_as_advanced(NINJA_MAX_LINK_JOBS) endif() -set(NINJA_MAX_LINK_JOBS "${_avg_link_jobs}" CACHE STRING - "The maximum number of concurrent link jobs, for Ninja build system." FORCE) -mark_as_advanced(NINJA_MAX_LINK_JOBS) if(NINJA_MAX_LINK_JOBS) math(EXPR _heavy_link_jobs "${_avg_link_jobs} / 2") if(_heavy_link_jobs EQUAL 0)