diff --git a/ci3/memsuspend_limit b/ci3/memsuspend_limit new file mode 100755 index 000000000000..12e16e70c55f --- /dev/null +++ b/ci3/memsuspend_limit @@ -0,0 +1,26 @@ +#!/bin/bash +set -eu + +# Get the hardware memory in GB, useful for setting the memsuspend limit. +# General rule of thumb: +# - Allocate 1/4 of total hardware memory for the build. +# - Max out at 64GB +if [ -n "${MEMSUSPEND:-}" ]; then + echo $MEMSUSPEND + exit +fi +os=$(uname -s) +total_mem_gb=64 # Default to 64GB + +if [[ "$os" == "Darwin" ]]; then + total_mem_bytes=$(sysctl -n hw.memsize) + total_mem_gb=$((total_mem_bytes / 1024 / 1024 / 1024)) +elif [[ "$os" == "Linux" ]]; then + total_mem_gb=$(free -g | awk '/^Mem:/ {print $2}') +fi + +# Allocate 1/4 of total hardware memory for the build. +total_mem_gb=$((total_mem_gb / 4)) + +# Max out at 64GB +echo $(( total_mem_gb < 64 ? total_mem_gb : 64 ))G diff --git a/ci3/parallelise b/ci3/parallelise index 5fde397e88c9..9c27140d7b7f 100755 --- a/ci3/parallelise +++ b/ci3/parallelise @@ -5,7 +5,7 @@ NO_CD=1 source $(git rev-parse --show-toplevel)/ci3/source cd $root jobs=$(get_num_cpus_max ${1:-}) -parallel_args="-j$jobs --memsuspend ${MEMSUSPEND:-64G} --line-buffer --joblog joblog.txt" +parallel_args="-j$jobs --memsuspend $(memsuspend_limit) --line-buffer --joblog joblog.txt" # If not in CI, fail fast. if [ "$CI" -eq 0 ]; then diff --git a/noir-projects/noir-contracts/bootstrap.sh b/noir-projects/noir-contracts/bootstrap.sh index 57a6900e2c53..790043591cdf 100755 --- a/noir-projects/noir-contracts/bootstrap.sh +++ b/noir-projects/noir-contracts/bootstrap.sh @@ -39,7 +39,7 @@ trap on_exit EXIT mkdir -p $tmp_dir # Set common flags for parallel. -export PARALLEL_FLAGS="-j${PARALLELISM:-16} --halt now,fail=1 --memsuspend ${MEMSUSPEND:-64G}" +export PARALLEL_FLAGS="-j${PARALLELISM:-16} --halt now,fail=1 --memsuspend $(memsuspend_limit)" # This computes a vk and adds it to the input function json if it's private, else returns same input. # stdin has the function json. diff --git a/noir-projects/noir-protocol-circuits/bootstrap.sh b/noir-projects/noir-protocol-circuits/bootstrap.sh index 668dcde7b7d3..7bc5f830fae1 100755 --- a/noir-projects/noir-protocol-circuits/bootstrap.sh +++ b/noir-projects/noir-protocol-circuits/bootstrap.sh @@ -157,7 +157,7 @@ function build { echo "$(basename $dir)" fi done | \ - parallel -v --line-buffer --tag --halt now,fail=1 --memsuspend ${MEMSUSPEND:-64G} \ + parallel -v --line-buffer --tag --halt now,fail=1 --memsuspend $(memsuspend_limit) \ --joblog joblog.txt compile {} code=$? cat joblog.txt