Skip to content

Commit 71c591b

Browse files
committed
[lto] When determining the number of lto threads to use, take the minimum of ${BUILD_JOBS} and a heuristic that scales with the memory size of the machine.
This ensures that even if we have a machine with a huge amount of ram, we respect ${BUILD_JOBS}.
1 parent f9b306e commit 71c591b

File tree

1 file changed

+28
-5
lines changed

1 file changed

+28
-5
lines changed

utils/build-script-impl

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -224,14 +224,37 @@ function set_lldb_build_mode() {
224224
LLDB_BUILD_MODE="CustomSwift-${LLDB_BUILD_TYPE}"
225225
}
226226

227+
function system_memory_in_bytes() {
228+
case "$(uname -s -m)" in
229+
Darwin\ x86_64)
230+
sysctl hw.memsize | cut -f 2 -d " "
231+
;;
232+
*)
233+
echo "Unknown operating system"
234+
exit 1
235+
;;
236+
esac
237+
}
238+
239+
function float_min() {
240+
local LHS=$1
241+
local RHS=$2
242+
if (( $(echo "${LHS} < ${RHS}" | bc -l) )); then
243+
echo ${LHS}
244+
else
245+
echo ${RHS}
246+
fi
247+
}
248+
227249
function num_llvm_parallel_lto_link_jobs() {
228250
case "$(uname -s -m)" in
229251
Darwin\ x86_64)
230252
# Currently with -gline-tables-only clang is ~3.5GB on Darwin. Use
231253
# the formula GB Memory/3.5GB to get the number of parallel link
232254
# threads we can support.
233-
echo $(sysctl hw.memsize | cut -f 2 -d " ")/1000000000.0/3.5 | bc
234-
;;
255+
local NUM_MEMORY_THREADS=$(echo $(system_memory_in_bytes)/1000000000.0/3.5 | bc)
256+
echo $(float_min "${NUM_MEMORY_THREADS}" "${BUILD_JOBS}")
257+
;;
235258
*)
236259
echo "Unknown operating system"
237260
exit 1
@@ -245,16 +268,16 @@ function num_swift_parallel_lto_link_jobs() {
245268
# Currently with -gline-tables-only swift is ~5-6GB on Darwin. Use
246269
# the formula GB Memory/6GB to get the number of parallel link
247270
# threads we can support.
248-
echo $(sysctl hw.memsize | cut -f 2 -d " ")/1000000000/6.0 | bc
249-
;;
271+
local NUM_MEMORY_THREADS=$(echo $(system_memory_in_bytes)/1000000000/6.0 | bc)
272+
echo $(float_min "${NUM_MEMORY_THREADS}" "${BUILD_JOBS}")
273+
;;
250274
*)
251275
echo "Unknown operating system"
252276
exit 1
253277
;;
254278
esac
255279
}
256280

257-
258281
function set_deployment_target_based_options() {
259282
llvm_cmake_options=()
260283
swift_cmake_options=()

0 commit comments

Comments
 (0)