#!/usr/bin/env bash # SPDX-License-Identifier: MIT # A/B the benchmark: a base revision against the working tree, alternately, on one core, # best of ROUNDS each, under each code layout (megadrive-rust-fast's scripts/ab.sh, the # same method). # # Why: the machine is shared, and separate runs move by tens of percent with other # people's load. Two builds timed alternately on one core see the same load, and # interference only ever slows a sample, so best-of recovers the undisturbed rate. The # ratio is the result. Both sides run the working tree's s32x-bench, so only the board # differs; both must give the same checks. # # The base is a worktree under target/ab-base whose external/ is this repository's (a # link), so it builds against the same dependency trees: a step inside a dependency # (a commit on megadrive-rust-fast's sockets-32x or sh2-rust-fast's board-context) is # measured with BIN_A and BIN_B instead. The fork point (5b1bd11) builds against # upstream's clones, as sega32x-rust does. # # Usage: scripts/ab.sh [BASE (default HEAD)] # env: CPU (default 28), BUILD_CPUS (default 24-27), ROUNDS (default 3), # LAYOUTS (default "default nf5"), FRAMES (per workload, default 900), # WORKLOADS (default "chaotix vrd swa doom"), TOOLCHAIN (default stable), # BIN_A and BIN_B (two s32x-bench binaries to compare instead of building) set -euo pipefail ROOT=$(cd "$(dirname "$0")/.." && pwd) BASE=${1:-HEAD} CPU=${CPU:-28} BUILD_CPUS=${BUILD_CPUS:-24-27} ROUNDS=${ROUNDS:-3} LAYOUTS=${LAYOUTS:-"default nf5"} FRAMES=${FRAMES:-900} WORKLOADS=${WORKLOADS:-"chaotix vrd swa doom"} TOOLCHAIN=${TOOLCHAIN:-stable} declare -A FLAGS=( [default]="" [nf5]="-C llvm-args=-align-all-nofallthru-blocks=5" [nf6]="-C llvm-args=-align-all-nofallthru-blocks=6" ) BASE_TREE="$ROOT/target/ab-base" rev=$(git -C "$ROOT" rev-parse --short "$BASE") if [ -n "${BIN_A:-}" ]; then rev=$BIN_A; LAYOUTS=given else if [ ! -d "$BASE_TREE" ]; then git -C "$ROOT" worktree add -q --detach "$BASE_TREE" "$rev" ln -s ../../external "$BASE_TREE/external" fi git -C "$BASE_TREE" checkout -q -f --detach "$rev" cp "$ROOT/src/bin/s32x-bench.rs" "$BASE_TREE/src/bin/" fi build() { # tree layout -> binary path local tree=$1 layout=$2 target target="$tree/target/ab-$layout" RUSTFLAGS="${FLAGS[$layout]}" CARGO_TARGET_DIR="$target" nice taskset -c "$BUILD_CPUS" \ rustup run "$TOOLCHAIN" cargo build --release -q --offline -j 4 --manifest-path "$tree/Cargo.toml" --bin s32x-bench >&2 || return 1 echo "$target/release/s32x-bench" } echo "base $rev vs ${BIN_B:-working tree}, cpu $CPU, $ROUNDS rounds, $FRAMES frames, load $(cut -d' ' -f1-3 /proc/loadavg)" best_base_all=""; best_new_all=""; checks="" for layout in $LAYOUTS; do # A failed build must stop the run: timing a stale binary is worse than no number. if [ -n "${BIN_A:-}" ]; then a=$BIN_A; b=$BIN_B else a=$(build "$BASE_TREE" "$layout") || { echo "base build failed" >&2; exit 1; } b=$(build "$ROOT" "$layout") || { echo "working-tree build failed" >&2; exit 1; } fi declare -A best=() for _ in $(seq "$ROUNDS"); do for w in $WORKLOADS; do for side in a b; do bin=$a; [ $side = b ] && bin=$b out=$(BENCH_BIN=$bin nice taskset -c "$CPU" "$ROOT/scripts/bench.sh" --frames "$FRAMES" --only "$w") t=$(sed -nE 's/^total ([0-9.]+) s$/\1/p' <<<"$out") checks+="$side $w $(grep -o 'check [0-9a-f]*' <<<"$out")"$'\n' key="$side $w" if [ -z "${best[$key]:-}" ] || awk "BEGIN{exit !($t < ${best[$key]})}"; then best[$key]=$t; fi done done done best_a=0; best_b=0 for w in $WORKLOADS; do best_a=$(awk "BEGIN{print $best_a + ${best[a $w]}}") best_b=$(awk "BEGIN{print $best_b + ${best[b $w]}}") echo " $w: base ${best[a $w]} s, new ${best[b $w]} s, x$(awk "BEGIN{printf \"%.3f\", ${best[a $w]}/${best[b $w]}}")" done unset best echo "$layout: base $best_a s, new $best_b s, x$(awk "BEGIN{printf \"%.3f\", $best_a/$best_b}") (load $(cut -d' ' -f1 /proc/loadavg))" [ -z "$best_base_all" ] || awk "BEGIN{exit !($best_a < $best_base_all)}" && best_base_all=$best_a [ -z "$best_new_all" ] || awk "BEGIN{exit !($best_b < $best_new_all)}" && best_new_all=$best_b done # Both sides must make the same sound and picture. if [ "$(cut -d' ' -f2- <<<"$checks" | sort -u | grep -c .)" != "$(wc -w <<<"$WORKLOADS")" ]; then echo "CHECKSUMS DIFFER:"; sort -u <<<"$checks"; exit 1 fi echo "min over layouts: base $best_base_all s, new $best_new_all s, x$(awk "BEGIN{printf \"%.3f\", $best_base_all/$best_new_all}")"