#!/usr/bin/env bash # Profile-guided optimization, as an optional build: instrument md-bench, run the # four benchmark workloads, merge the profile, rebuild with it. The binaries land in # target/pgo-use/release/; `cargo build --release` is unchanged, because a .profdata # is tied to one compiler version. Also builds target/pgo-plain/ with the same # toolchain and no profile, the fair base for scripts/ab.sh (BIN_A, BIN_B). # # Needs llvm-profdata from the toolchain's own LLVM (rustup's llvm-tools component). # Usage: scripts/pgo.sh env: TOOLCHAIN (default 1.93.1, which has llvm-tools here), # FRAMES (training frames per workload, default 900), CPU (default 30) set -euo pipefail ROOT=$(cd "$(dirname "$0")/.." && pwd) TOOLCHAIN=${TOOLCHAIN:-1.93.1} FRAMES=${FRAMES:-900} CPU=${CPU:-30} sysroot=$(rustup run "$TOOLCHAIN" rustc --print sysroot) PROFDATA=${PROFDATA:-"$sysroot/lib/rustlib/x86_64-unknown-linux-gnu/bin/llvm-profdata"} [ -x "$PROFDATA" ] || { echo "llvm-profdata not found at $PROFDATA" >&2; exit 2; } profiles="$ROOT/target/pgo-profiles/$(date +%s)" mkdir -p "$profiles" build() { # target-dir rustflags RUSTFLAGS="$2" CARGO_TARGET_DIR="$ROOT/target/$1" \ nice rustup run "$TOOLCHAIN" cargo build --release -q --manifest-path "$ROOT/Cargo.toml" \ --bin md-bench --bin md-trace } echo "plain and instrumented builds ($TOOLCHAIN)" build pgo-plain "" build pgo-gen "-C profile-generate=$profiles" echo "training: scripts/bench.sh, $FRAMES frames each" BENCH_BIN="$ROOT/target/pgo-gen/release/md-bench" taskset -c "$CPU" "$ROOT/scripts/bench.sh" --frames "$FRAMES" "$PROFDATA" merge -o "$profiles/merged.profdata" "$profiles"/*.profraw echo "optimized build" build pgo-use "-C profile-use=$profiles/merged.profdata" echo "built target/pgo-use/release/{md-bench,md-trace} from $(git -C "$ROOT" rev-parse --short HEAD)"