# shellcheck shell=bash # Helpers shared by bench.sh, bench-compile.sh, stress.sh and ci.sh. # Sourced, not run; the caller has `cd`'d to the repository root. # Faster links with mold when it is installed (runners may lack it). # Target-scoped, never a global RUSTFLAGS: a global one makes cargo ignore # CARGO_TARGET__RUSTFLAGS, which the aarch64 lane's lld/sysroot # flags need (found by WS-BC). LOUISE_NO_MOLD=1 opts out. # Scripts that set their own global RUSTFLAGS for a --target build # (runtime.sh's lto archive, tsan.sh, the loom lane) go through # without_mold: the scoped var would shadow their flags otherwise. LOUISE_MOLD_FLAGS="" if [ "${LOUISE_NO_MOLD:-0}" != 1 ] && command -v mold >/dev/null; then LOUISE_MOLD_FLAGS="-C link-arg=-fuse-ld=mold" LOUISE_MOLD_VAR="CARGO_TARGET_$(rustc -vV | sed -n 's/^host: //p' | tr 'a-z-.' 'A-Z__')_RUSTFLAGS" if [ -z "${!LOUISE_MOLD_VAR:-}" ]; then export "$LOUISE_MOLD_VAR=$LOUISE_MOLD_FLAGS"; fi export LOUISE_MOLD_VAR fi export LOUISE_MOLD_FLAGS # without_mold CMD... — CMD builds with its own RUSTFLAGS for a --target: # run it with the scoped mold var cleared (its own flags then apply). without_mold() { if [ -n "${LOUISE_MOLD_VAR:-}" ]; then env -u "$LOUISE_MOLD_VAR" "$@"; else "$@"; fi } # heavy CMD... — on a shared host (LOUISE_CI_SHARED_HOST=1), run heavy lanes # one at a time per machine, niced; ci.sh uses this, and standalone scripts # source lib.sh to get it. heavy() { if [ "${LOUISE_CI_SHARED_HOST:-0}" = 1 ]; then flock /tmp/louise-ci-heavy.lock nice -n 10 "$@"; else "$@"; fi } # The compiler and runtime the scripts build programs with, in the Cargo # profile LOUISE_PROFILE names: `release` (the default) or `dist`, the # toolchain's release builds (issue #120). `louise` finds the runtime next # to itself, so both come from one profile. LOUISE_PROFILE="${LOUISE_PROFILE:-release}" # A prebuilt compiler (a container mounts one), with its runtime archive # next to it: set LOUISE_BIN to it and the build is skipped. louise_prebuilt=0 if [ -n "${LOUISE_BIN:-}" ]; then louise_prebuilt=1 else LOUISE_BIN="target/$LOUISE_PROFILE/louise" fi build_release_louise() { [ "$louise_prebuilt" = 1 ] && return cargo build --quiet --profile "$LOUISE_PROFILE" -p louise -p louise_rt # The `fast` archive release builds link (ADR 0031). scripts/runtime.sh "$LOUISE_PROFILE" } # The output a test program expects: its `//>` lines (format: # tests/README.md). expected_output() { sed -n 's|^//> \{0,1\}||p' "$1" } # A table of results, one row per measurement in milliseconds, that the # benchmarks save (`--save FILE`) or compare with saved results # (`--compare FILE`): # # table_start MODE FILE WIDTH TITLE RESULTS # print the header # table_row NAME MS # print a row, record it in RESULTS # table_end # with --save, copy RESULTS to FILE table_start() { table_mode=$1 table_file=$2 table_width=$3 table_results=$5 declare -gA table_baseline=() if [ "$table_mode" = --compare ]; then while read -r name ms; do table_baseline[$name]=$ms; done < "$table_file" fi : > "$table_results" printf "%-${table_width}s %9s" "$4" ms if [ "$table_mode" = --compare ]; then printf ' %9s %8s' before change; fi printf '\n' } table_row() { local name=$1 best=$2 echo "$name $best" >> "$table_results" printf "%-${table_width}s %9d" "$name" "$best" if [ "$table_mode" = --compare ] && [ -n "${table_baseline[$name]:-}" ]; then local before=${table_baseline[$name]} printf ' %9d %7d%%' "$before" $(((best - before) * 100 / (before > 0 ? before : 1))) fi printf '\n' } table_end() { if [ "$table_mode" = --save ]; then cp "$table_results" "$table_file"; fi }