Skip to content

Commit d14c8d9

Browse files
authored
chore(build): limit concurrency based on available memory (#25480)
Our automatic build concurrency calculation only uses number of available CPUs as a limiting heuristic. Also add a dimension to limit based on available memory, because my machine runs out of memory every time it builds. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent ed3962a commit d14c8d9

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

build.sh

+13-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ check_prereqs="true"
88
check_compat="true"
99
ci="false"
1010
scope=""
11+
concurrency=""
1112
while [[ "${1:-}" != "" ]]; do
1213
case $1 in
1314
-h|--help)
@@ -32,6 +33,10 @@ while [[ "${1:-}" != "" ]]; do
3233
--ci)
3334
ci=true
3435
;;
36+
-c|--concurrency)
37+
concurrency="$2"
38+
shift
39+
;;
3540
*)
3641
echo "Unrecognized parameter: $1"
3742
exit 1
@@ -93,8 +98,14 @@ if [ "$run_tests" == "true" ]; then
9398
runtarget="$runtarget,test"
9499
fi
95100

96-
# Limit top-level concurrency to available CPUs - 1 to limit CPU load.
97-
concurrency=$(node -p 'Math.max(1, require("os").cpus().length - 1)')
101+
if [[ "$concurrency" == "" ]]; then
102+
# Auto-limit top-level concurrency to:
103+
# - available CPUs - 1 to limit CPU load
104+
# - total memory / 4GB (N.B: constant here may need to be tweaked, configurable with $CDKBUILD_MEM_PER_PROCESS)
105+
mem_per_process=${CDKBUILD_MEM_PER_PROCESS:-4_000_000_000}
106+
concurrency=$(node -p "Math.max(1, Math.min(require('os').cpus().length - 1, Math.round(require('os').totalmem() / $mem_per_process)))")
107+
echo "Concurrency: $concurrency"
108+
fi
98109

99110
flags=""
100111
if [ "$ci" == "true" ]; then

0 commit comments

Comments
 (0)