#!/usr/bin/env bash # A/B a base revision against the working trees under several code layouts. # # Two builds of the same source can differ by +-5 % on this machine from where the linker puts # the hot blocks alone, so one build per side measures layout luck, not the change (z80-rust-fast, # m68000-rust-fast). Each side is built under each layout (RUSTFLAGS) and timed with ab.py # (alternating, best of ROUNDS); a change is kept when it wins on every layout and on the best # over layouts. # # scripts/ab.sh [BASE_REV [APU_BASE_REV]] (defaults: HEAD, and gb-apu-rust-fast's HEAD) # env: ROUNDS (default 6), SLICES (300), CPU (31), LAYOUTS ("default nf5 nf6") # # The new side is this working tree with ../gb-apu-rust-fast's working tree; the base side is a # worktree of each at the base revisions (target/ab-base, ../gb-apu-rust-fast/target/ab-base), # its Cargo.toml pointed at them, running its own gameboy-bench.rs. set -euo pipefail ROOT=$(cd "$(dirname "$0")/.." && pwd) APU=$(cd "$ROOT/../gb-apu-rust-fast" && pwd) SM83=$(cd "$ROOT/../sm83-rust" && pwd) BASE=${1:-HEAD} APU_BASE=${2:-HEAD} ROUNDS=${ROUNDS:-6} SLICES=${SLICES:-300} CPU=${CPU:-31} LAYOUTS=${LAYOUTS:-"default nf5 nf6"} declare -A FLAGS=( [default]="" [nf5]="-C llvm-args=-align-all-nofallthru-blocks=5" [nf6]="-C llvm-args=-align-all-nofallthru-blocks=6" ) worktree() { # repo dir rev local repo=$1 dir=$2 rev=$3 [ -d "$dir" ] || git -C "$repo" worktree add -q --detach "$dir" "$rev" git -C "$dir" checkout -q -f --detach "$rev" } rev=$(git -C "$ROOT" rev-parse --short "$BASE") apu_rev=$(git -C "$APU" rev-parse --short "$APU_BASE") worktree "$ROOT" "$ROOT/target/ab-base" "$rev" worktree "$APU" "$APU/target/ab-base" "$apu_rev" B="$ROOT/target/ab-base" UPSTREAM_APU=$(cd "$ROOT/../gb-apu-rust" && pwd) python3 - "$B/Cargo.toml" "$APU/target/ab-base" "$SM83" "$UPSTREAM_APU" <<'EOF' import sys path, apu, sm83, upstream = sys.argv[1:] text = open(path).read() # a base on the APU fork uses its base revision; one from before it, upstream gb-apu-rust (whose # code is the fork point's) text = text.replace('path = "../gb-apu-rust-fast"', f'path = "{apu}"') text = text.replace('path = "../gb-apu-rust"', f'path = "{upstream}"') text = text.replace('path = "../sm83-rust"', f'path = "{sm83}"') open(path, "w").write(text) EOF # a base keeps its own bench (it may predate an API the working tree's uses); the checksums # still have to agree [ -f "$B/src/bin/gameboy-bench.rs" ] || cp "$ROOT/src/bin/gameboy-bench.rs" "$B/src/bin/" build() { # tree layout -> binary local tree=$1 layout=$2 target="$1/target/ab-$2" RUSTFLAGS="${FLAGS[$layout]}" CARGO_TARGET_DIR="$target" \ cargo build --release -q --manifest-path "$tree/Cargo.toml" --bin gameboy-bench >&2 || return 1 echo "$target/release/gameboy-bench" } echo "base $rev (APU $apu_rev) vs working trees; $ROUNDS rounds, $SLICES slices, cpu $CPU, load $(cut -d' ' -f1 /proc/loadavg)" for layout in $LAYOUTS; do a=$(build "$B" "$layout") || { echo "base build failed"; exit 1; } b=$(build "$ROOT" "$layout") || { echo "working-tree build failed"; exit 1; } line=$(nice python3 "$ROOT/scripts/ab.py" "$a" "$b" --rounds "$ROUNDS" --slices "$SLICES" --cpu "$CPU" | tail -1) echo "$layout: $line" done