Skip to content

Commit e789e13

Browse files
authored
Merge pull request ceph#56865 from zmc/cmake-sccache-max-jobs
cmake: Intelligently set job limits for sccache Reviewed-by: Kefu Chai <[email protected]>
2 parents 720f8e9 + be70946 commit e789e13

File tree

2 files changed

+54
-27
lines changed

2 files changed

+54
-27
lines changed

CMakeLists.txt

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -85,17 +85,36 @@ endif(WITH_CCACHE)
8585

8686
option(WITH_SCCACHE "Build with sccache.")
8787
if(WITH_SCCACHE)
88-
if(CMAKE_C_COMPILER_LAUNCHER OR CMAKE_CXX_COMPILER_LAUNCHER)
89-
message(WARNING "Compiler launcher already set. stop configuring sccache")
90-
else()
91-
find_program(SCCACHE_EXECUTABLE sccache)
92-
if(NOT SCCACHE_EXECUTABLE)
93-
message(FATAL_ERROR "Can't find sccache. Is it installed?")
88+
find_program(SCCACHE_EXECUTABLE sccache)
89+
if(NOT SCCACHE_EXECUTABLE)
90+
message(FATAL_ERROR "Can't find sccache. Is it installed?")
91+
endif()
92+
if(NOT NINJA_MAX_COMPILE_JOBS)
93+
if(CMAKE_VERSION VERSION_GREATER_EQUAL "3.19")
94+
execute_process(
95+
COMMAND "sccache" "--dist-status"
96+
OUTPUT_VARIABLE sccache_dist_status
97+
)
98+
string(
99+
JSON sccache_cores
100+
ERROR_VARIABLE sccache_dist_status_error
101+
GET "${sccache_dist_status}" SchedulerStatus 1 num_cpus
102+
)
103+
string(FIND "${sccache_dist_status}" "disabled" find_result)
104+
if(find_result EQUAL -1)
105+
message(STATUS "Using sccache with distributed compilation. Effective cores: ${sccache_cores}")
106+
set(NINJA_MAX_COMPILE_JOBS ${sccache_cores})
107+
set(NINJA_MAX_LINK_JOBS ${sccache_cores})
108+
else()
109+
message(WARNING "Using sccache, but it is not configured for distributed complilation")
110+
endif()
111+
else()
112+
message(WARNING "Using sccache, but cannot determine maximum job value since cmake version is <3.19")
94113
endif()
95-
message(STATUS "Building with sccache: ${SCCACHE_EXECUTABLE}, SCCACHE_CONF=$ENV{SCCACHE_CONF}")
96-
set(CMAKE_C_COMPILER_LAUNCHER ${SCCACHE_EXECUTABLE})
97-
set(CMAKE_CXX_COMPILER_LAUNCHER ${SCCACHE_EXECUTABLE})
98114
endif()
115+
message(STATUS "Building with sccache: ${SCCACHE_EXECUTABLE}, SCCACHE_CONF=$ENV{SCCACHE_CONF}")
116+
set(CMAKE_C_COMPILER_LAUNCHER ${SCCACHE_EXECUTABLE})
117+
set(CMAKE_CXX_COMPILER_LAUNCHER ${SCCACHE_EXECUTABLE})
99118
endif(WITH_SCCACHE)
100119

101120
option(WITH_MANPAGE "Build man pages." ON)

cmake/modules/LimitJobs.cmake

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,20 @@ set(MAX_LINK_MEM 4500 CACHE INTERNAL "maximum memory used by each linking job (i
44
cmake_host_system_information(RESULT _num_cores QUERY NUMBER_OF_LOGICAL_CORES)
55
cmake_host_system_information(RESULT _total_mem QUERY TOTAL_PHYSICAL_MEMORY)
66

7-
math(EXPR _avg_compile_jobs "${_total_mem} / ${MAX_COMPILE_MEM}")
8-
if(_avg_compile_jobs EQUAL 0)
9-
set(_avg_compile_jobs 1)
10-
endif()
11-
if(_num_cores LESS _avg_compile_jobs)
12-
set(_avg_compile_jobs ${_num_cores})
7+
if(NINJA_MAX_COMPILE_JOBS)
8+
set(_avg_compile_jobs "${NINJA_MAX_COMPILE_JOBS}")
9+
else()
10+
math(EXPR _avg_compile_jobs "${_total_mem} / ${MAX_COMPILE_MEM}")
11+
if(_avg_compile_jobs EQUAL 0)
12+
set(_avg_compile_jobs 1)
13+
endif()
14+
if(_num_cores LESS _avg_compile_jobs)
15+
set(_avg_compile_jobs "${_num_cores}")
16+
endif()
17+
set(NINJA_MAX_COMPILE_JOBS "${_avg_compile_jobs}" CACHE STRING
18+
"The maximum number of concurrent compilation jobs, for Ninja build system." FORCE)
19+
mark_as_advanced(NINJA_MAX_COMPILE_JOBS)
1320
endif()
14-
set(NINJA_MAX_COMPILE_JOBS "${_avg_compile_jobs}" CACHE STRING
15-
"The maximum number of concurrent compilation jobs, for Ninja build system." FORCE)
16-
mark_as_advanced(NINJA_MAX_COMPILE_JOBS)
1721
if(NINJA_MAX_COMPILE_JOBS)
1822
math(EXPR _heavy_compile_jobs "${_avg_compile_jobs} / 2")
1923
if(_heavy_compile_jobs EQUAL 0)
@@ -25,16 +29,20 @@ if(NINJA_MAX_COMPILE_JOBS)
2529
set(CMAKE_JOB_POOL_COMPILE avg_compile_job_pool)
2630
endif()
2731

28-
math(EXPR _avg_link_jobs "${_total_mem} / ${MAX_LINK_MEM}")
29-
if(_avg_link_jobs EQUAL 0)
30-
set(_avg_link_jobs 1)
31-
endif()
32-
if(_num_cores LESS _avg_link_jobs)
33-
set(_avg_link_jobs ${_num_cores})
32+
if(NINJA_MAX_LINK_JOBS)
33+
set(_avg_link_jobs "${NINJA_MAX_LINK_JOBS}")
34+
else()
35+
math(EXPR _avg_link_jobs "${_total_mem} / ${MAX_LINK_MEM}")
36+
if(_avg_link_jobs EQUAL 0)
37+
set(_avg_link_jobs 1)
38+
endif()
39+
if(_num_cores LESS _avg_link_jobs)
40+
set(_avg_link_jobs "${_num_cores}")
41+
endif()
42+
set(NINJA_MAX_LINK_JOBS "${_avg_link_jobs}" CACHE STRING
43+
"The maximum number of concurrent link jobs, for Ninja build system." FORCE)
44+
mark_as_advanced(NINJA_MAX_LINK_JOBS)
3445
endif()
35-
set(NINJA_MAX_LINK_JOBS "${_avg_link_jobs}" CACHE STRING
36-
"The maximum number of concurrent link jobs, for Ninja build system." FORCE)
37-
mark_as_advanced(NINJA_MAX_LINK_JOBS)
3846
if(NINJA_MAX_LINK_JOBS)
3947
math(EXPR _heavy_link_jobs "${_avg_link_jobs} / 2")
4048
if(_heavy_link_jobs EQUAL 0)

0 commit comments

Comments
 (0)