diff --git a/AGENTS.md b/AGENTS.md index 27e71f9e2..e6c6fad33 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -143,6 +143,13 @@ has the commands. - Remove scratch files under the OS temp dir as soon as the tool is done with them; no long-lived dumps under fixed `/tmp` paths. FALLBACK repro bundles under `$TMPDIR/metal2vulkan-repros` (or `METAL2VULKAN_REPRO_DIR`) may stay. +- Keep disk writes down (CONTRIBUTING.md, "Many trees on one machine"): + - make worktrees and source snapshots with `scripts/lean-tree`, leaving the observations out + unless the tree runs validation; + - start each new target directory from a seed with `scripts/seed-target`; + - set `CARGO_INCREMENTAL=0` for one-shot builds; + - record Instruments traces through `scripts/xctrace-run`, and delete the `.trace` once + exported. - `pgrep -f` and `ps | grep` match their own command line. Always bracket the first character: `pgrep -f '[c]orpus-refresh'`. diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 5d77cf28c..0d97a2710 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -65,6 +65,24 @@ To build the command: cargo build --release -p metal2vulkan --features cli ``` +A debugger session that needs variables builds with `--profile debugger` (full debug info for the +workspace and its dependencies). Dev builds keep only line tables. + +### Many trees on one machine + +Parallel worktrees and one-shot builds write a lot to disk. Three scripts keep that down: + +- `scripts/lean-tree worktree PATH BRANCH START` and `scripts/lean-tree snapshot REV DIR` make a + tree without `crates/metal2vulkan-validation/corpus/observations` (1.5 GiB of the 1.6 GiB tracked + tree). Pass `--with-observations` for a tree that runs validation. +- `scripts/seed-target new TARGET SEED WORKTREE` starts a target directory as a copy-on-write clone + of a seed, so only the workspace's crates compile. `scripts/seed-target save TARGET SEED WORKTREE` + makes the seed from an idle warm target. Keep one seed per set of `RUSTFLAGS`. +- `scripts/xctrace-run TRACE ARGS...` records with Instruments and removes the raw + `instruments*.ktrace` files a recording leaves in the temp dir, an aborted one included. + +Set `CARGO_INCREMENTAL=0` for a build that won't be edited and rebuilt. + ## Where tests go - A test that names an internal item is a unit test of its crate: under `src/tests/` (or its diff --git a/scripts/lean-tree b/scripts/lean-tree new file mode 100755 index 000000000..998003ec7 --- /dev/null +++ b/scripts/lean-tree @@ -0,0 +1,51 @@ +#!/usr/bin/env bash +# Source trees of this repo without crates/metal2vulkan-validation/corpus/observations: 1.5 GiB of +# the 1.6 GiB tracked tree, read only by validation runs. Pass --with-observations for a run that +# validates. +# +# scripts/lean-tree snapshot REV DEST_DIR [--with-observations] +# scripts/lean-tree worktree PATH NEW_BRANCH START_REV [--with-observations] +# +# snapshot extracts `git archive REV` into DEST_DIR with `tar -xm`, so every file carries the +# extraction time and a target built before it can't take these sources as Fresh. +# +# worktree adds a git worktree on a new branch whose sparse checkout leaves the observations out. +# +# Both act on the repository this script is in, from any of its worktrees. +set -euo pipefail + +die() { + echo "lean-tree: $*" >&2 + exit 2 +} + +repo=$(git -C "$(dirname "$0")" rev-parse --show-toplevel) +observations=crates/metal2vulkan-validation/corpus/observations + +case "${1:-}" in +snapshot) + [ $# -ge 3 ] || die "usage: lean-tree snapshot REV DEST_DIR [--with-observations]" + rev=$2 + dest=$3 + mkdir -p "$dest" + if [ "${4:-}" = --with-observations ]; then + git -C "$repo" archive --format=tar "$rev" | tar -xm -C "$dest" + else + git -C "$repo" archive --format=tar "$rev" -- . ":(exclude)$observations" | tar -xm -C "$dest" + fi + ;; +worktree) + [ $# -ge 4 ] || die "usage: lean-tree worktree PATH NEW_BRANCH START_REV [--with-observations]" + path=$2 + branch=$3 + start=$4 + if [ "${5:-}" = --with-observations ]; then + git -C "$repo" worktree add -b "$branch" "$path" "$start" + else + git -C "$repo" worktree add --no-checkout -b "$branch" "$path" "$start" + git -C "$path" sparse-checkout set --no-cone '/*' "!/$observations/" + git -C "$path" checkout + fi + ;; +*) die "usage: lean-tree snapshot|worktree ..." ;; +esac diff --git a/scripts/seed-target b/scripts/seed-target new file mode 100755 index 000000000..e0d518415 --- /dev/null +++ b/scripts/seed-target @@ -0,0 +1,82 @@ +#!/usr/bin/env bash +# Seeded cargo target directories: a new target starts as a copy-on-write clone of a warm one, so +# only the workspace's own crates compile and the third-party ones are reused. On APFS (and on +# Linux file systems with reflinks) the clone writes nothing until a file in it changes. +# +# scripts/seed-target save WARM_TARGET SEED_DIR WORKTREE +# scripts/seed-target new NEW_TARGET SEED_DIR WORKTREE +# +# save clones an idle WARM_TARGET (no build running in it) into SEED_DIR, replacing any seed there. +# It then removes the workspace members' artifacts from the clone, for every profile directory in +# it (`cargo clean -p`). A worktree whose sources are older than the seed can then never take +# another tree's build as Fresh. WORKTREE's Cargo.lock is kept beside the seed: the lockfile is +# not tracked, and equal dependency versions are what keep the dependencies Fresh. +# +# new clones SEED_DIR into NEW_TARGET, which must not exist. When WORKTREE has no Cargo.lock, it +# copies in the seed's. +# +# Keep one seed per set of build flags (RUSTFLAGS, such as `--cfg m2v_gpu`): different flags +# rebuild every crate. cargo runs under the caller's toolchain (RUSTUP_TOOLCHAIN picks one). +set -euo pipefail + +die() { + echo "seed-target: $*" >&2 + exit 2 +} + +clone() { + if [ "$(uname)" = Darwin ]; then + cp -c -p -R "$1" "$2" + else + cp -a --reflink=auto "$1" "$2" + fi +} + +[ $# -eq 4 ] || die "usage: seed-target save|new TARGET SEED_DIR WORKTREE" +action=$1 +target=${2%/} +seed=${3%/} +worktree=$4 +[ -f "$worktree/Cargo.toml" ] || die "$worktree has no Cargo.toml" + +case "$action" in +save) + [ -d "$target" ] || die "$target does not exist" + new="$seed.new.$$" + clone "$target" "$new" + members=$(cd "$worktree" && cargo metadata -q --no-deps --format-version 1 | + python3 -c 'import json, sys; print(" ".join(p["name"] for p in json.load(sys.stdin)["packages"]))') + for profile_dir in "$new"/*/; do + name=$(basename "$profile_dir") + case "$name" in + debug) profile=dev ;; + release | debugger) profile=$name ;; + *) continue ;; + esac + for member in $members; do + (cd "$worktree" && CARGO_TARGET_DIR="$new" cargo clean -q -p "$member" --profile "$profile") + done + rm -rf "$profile_dir/incremental" + done + if [ -f "$worktree/Cargo.lock" ]; then + cp "$worktree/Cargo.lock" "$new/Cargo.lock.seed" + fi + if [ -d "$seed" ]; then + mv -f "$seed" "$seed.old.$$" + rm -rf "$seed.old.$$" + fi + mv -f "$new" "$seed" + echo "seeded $seed from $target (members cleaned: $members)" + ;; +new) + [ -d "$seed" ] || die "no seed at $seed: build one target, then run: seed-target save TARGET $seed WORKTREE" + [ ! -e "$target" ] || die "$target already exists" + clone "$seed" "$target" + rm -f "$target/Cargo.lock.seed" + if [ ! -f "$worktree/Cargo.lock" ] && [ -f "$seed/Cargo.lock.seed" ]; then + cp "$seed/Cargo.lock.seed" "$worktree/Cargo.lock" + fi + echo "cloned $seed into $target" + ;; +*) die "unknown action $action (save or new)" ;; +esac diff --git a/scripts/xctrace-run b/scripts/xctrace-run new file mode 100755 index 000000000..279db7328 --- /dev/null +++ b/scripts/xctrace-run @@ -0,0 +1,32 @@ +#!/usr/bin/env bash +# An Instruments recording that leaves no raw trace behind (macOS). +# +# scripts/xctrace-run TRACE_PATH XCTRACE_RECORD_ARGS... +# +# Runs `xcrun xctrace record --output TRACE_PATH XCTRACE_RECORD_ARGS...`. On exit, including an +# aborted recording, it deletes the instruments*.ktrace files that appeared in the user temp dir +# since it started and that no process holds open. Those are Instruments' raw recordings: a +# finished recording folds its own into the .trace, and an aborted one leaves it behind, often +# gigabytes. Export what you need from TRACE_PATH (`xcrun xctrace export`), then delete it. +set -uo pipefail + +[ $# -ge 2 ] || { + echo "usage: xctrace-run TRACE_PATH XCTRACE_RECORD_ARGS..." >&2 + exit 2 +} +trace=$1 +shift +tmp=$(getconf DARWIN_USER_TEMP_DIR) +marker=$(mktemp "${tmp}xctrace-run.XXXXXX") + +cleanup() { + for f in "$tmp"instruments*.ktrace; do + [ -f "$f" ] && [ "$f" -nt "$marker" ] || continue + lsof -t -- "$f" >/dev/null 2>&1 || rm -f -- "$f" + done + rm -f -- "$marker" +} +trap cleanup EXIT +trap 'exit 130' INT TERM + +xcrun xctrace record --output "$trace" "$@"