#!/usr/bin/env bash # The making benchmark: how long the runtime takes to make each render pipeline state of a set, # cold, on the GPU this host's loader picks, and what each making costs. # # scripts/making-bench/making-bench --bin PATH --set DIR --out DIR # [--combos "FORMAT:MODE ..."] [--repeats N] [--timeout SECONDS] [--ids "ID ..."] # [--queue-first] [--policy NAMES] # # The set is a `corpus-draw-ab` draws set (`crates/metal2vulkan-validation/src/bin/corpus-draw-ab.rs` documents it): # `DIR/draws.jsonl`, one state per line (`{"id", "vertex", "fragment", "template"}`), and # `DIR/src/.bc` with `DIR/src/.entry`. Nothing in this script names a shader: # the set is the caller's, and a set of guest data stays on the host that holds it (never # committed). `--bin` is a release `corpus-draw-ab` built at the sha being measured. # # Each state is timed once per combo (`corpus-draw-ab time --combo FORMAT:MODE`, default # `RGBA8Unorm:linear BGRA8Unorm:nearest`), each in a process of its own that starts cold: a fresh # `HOME`, a fresh runtime shader cache, and the drivers' disk caches off (`__GL_SHADER_DISK_CACHE=0`, # `MESA_SHADER_CACHE_DISABLE=true`). A run's creation makes the state's predicted key, and its # first draw makes the key of the combo's bound textures and samplers (another specialization), so # each run times two makings. `METAL2VULKAN_LOG_PIPELINES=1` reports every making's wall time and # the SPIR-V words of its largest module. # # `--queue-first`: each run makes its command queue before the state # (`corpus-draw-ab time --queue-first yes`), as a Metal program and the consumer's guest make # theirs long before any pipeline, so the room the device keeps for its encodings # is kept by the queue's creation, not inside the state's: the creation is timed cold as such a # program sees it, and the queue's own creation is reported apart (`queue_first_ms`). Off by # default, so runs without it stay comparable with earlier ones. # # `--policy NAMES`: each run opens its device with those `DevicePolicy` switches at their accurate # forms (`corpus-draw-ab time --policy NAMES`: `exact-answers` and field names such as `sampling`, # comma-separated), so a state is timed as an application under those forms makes it. Off by # default (the default policy). # # Writes, under `--out`: # - `makings.tsv`: `id combo pipeline at ms waited_ms words`, one line per `VkPipeline` made # (`at` is `creation` for one made inside the state's creation, else `draw`); # - `runs.tsv`: `id combo rc created_ms first_draw_ms gpu_ms_median gpu_ms_min maxrss_kb # creation_making_ms`, one line per run (`created_ms` less `creation_making_ms` is the # creation's translation and setup, outside the driver); with `--queue-first`, a last column # `queue_first_ms`, the queue's creation before the state's; # - `host.txt`: the host, the GPU, the driver and the binary's sha256. # Only times, counts and words: no shader text. `summarize.py` ranks them. # # The scoreboard (this project's fleet; the wrapper is the orchestrator's, outside the tree): # ~/.cache/m2v-XCR/bench.sh [--tag T] [5080] [3090] # builds a release `corpus-draw-ab` at on the RTX 5080 Laptop and the RTX 3090, each through `rev-bg.sh --gpu nvidia`, and runs this script there over the # consumer's heaviest states (Xgc from its captures 14 and 15 under the BGRA8, RGBA8 and RGBA16Float # states, and A2Xghfc's two states), a guest set kept at ~/.cache/m2v-bench-guest/set on those two # hosts only (its checksums in ../set.sums; never copied elsewhere, never committed). Wait with # `~/.cache/m2v-orch/rev-wait.sh XCR-mb-[T-]-`, which prints `summarize.py`'s table; # the outputs stay on the host under ~/.cache/m2v-XCR/XCR-mb-[T-]-/. Rerun it after # each batch of merges that changes making time, and compare with an earlier run on the same host: # python3 scripts/making-bench/summarize.py /out --base /out set -euo pipefail bin='' set_dir='' out='' combos='RGBA8Unorm:linear BGRA8Unorm:nearest' repeats=10 timeout=1800 ids='' queue_first='' policy='' while [ $# -gt 0 ]; do case "$1" in --bin) bin=$2; shift 2 ;; --set) set_dir=$2; shift 2 ;; --out) out=$2; shift 2 ;; --combos) combos=$2; shift 2 ;; --repeats) repeats=$2; shift 2 ;; --timeout) timeout=$2; shift 2 ;; --ids) ids=$2; shift 2 ;; --queue-first) queue_first=yes; shift ;; --policy) policy=$2; shift 2 ;; *) echo "making-bench: unknown argument $1" >&2; exit 64 ;; esac done [ -x "$bin" ] || { echo "making-bench: no binary at '$bin'" >&2; exit 66; } [ -f "$set_dir/draws.jsonl" ] || { echo "making-bench: no $set_dir/draws.jsonl" >&2; exit 66; } [ -n "$out" ] || { echo "making-bench: --out is required" >&2; exit 64; } [ -x /usr/bin/time ] || { echo "making-bench: needs /usr/bin/time" >&2; exit 69; } mkdir -p "$out" scratch=$(mktemp -d "${TMPDIR:-/tmp}/making-bench.XXXXXX") trap 'rm -rf "$scratch"' EXIT trap 'exit 130' INT TERM HUP if [ -z "$ids" ]; then ids=$(python3 -I -c 'import json, sys for line in open(sys.argv[1]): if line.strip(): print(json.loads(line)["id"])' "$set_dir/draws.jsonl") fi { echo "host $(hostname) $(nproc) CPUs, $(date -u +%Y-%m-%dT%H:%M:%SZ)" vulkaninfo --summary 2>/dev/null | grep -m 1 -A 9 '^GPU0:' | grep -E 'deviceName|driverInfo' || true echo "binary sha256 $(sha256sum "$bin" | cut -c1-16)" echo "combos $combos; repeats $repeats; timeout ${timeout}s${queue_first:+; queue first}${policy:+; policy $policy}" } > "$out/host.txt" printf 'id\tcombo\tpipeline\tat\tms\twaited_ms\twords\n' > "$out/makings.tsv" qf_column='' [ -z "$queue_first" ] || qf_column=$(printf '\tqueue_first_ms') printf 'id\tcombo\trc\tcreated_ms\tfirst_draw_ms\tgpu_ms_median\tgpu_ms_min\tmaxrss_kb\tcreation_making_ms%s\n' \ "$qf_column" > "$out/runs.tsv" # A line of `corpus-draw-ab time`'s table is `idnamevalue...`: the value of `name`, or # its `column`th field (3, the first value, by default). field() { # file, name[, column] awk -F'\t' -v n="$2" -v c="${3:-3}" '$2 == n { print $c; exit }' "$1" 2>/dev/null } for id in $ids; do for combo in $combos; do run=$scratch/run rm -rf "$run" mkdir -p "$run/home" "$run/cache" "$run/out" rc=0 env -i PATH="$PATH" HOME="$run/home" TMPDIR="$run" \ ${VK_DRIVER_FILES:+VK_DRIVER_FILES="$VK_DRIVER_FILES"} \ ${VK_ICD_FILENAMES:+VK_ICD_FILENAMES="$VK_ICD_FILENAMES"} \ ${VK_LOADER_DRIVERS_SELECT:+VK_LOADER_DRIVERS_SELECT="$VK_LOADER_DRIVERS_SELECT"} \ ${VK_INSTANCE_LAYERS:+VK_INSTANCE_LAYERS="$VK_INSTANCE_LAYERS"} \ ${DISPLAY:+DISPLAY="$DISPLAY"} ${XDG_RUNTIME_DIR:+XDG_RUNTIME_DIR="$XDG_RUNTIME_DIR"} \ __GL_SHADER_DISK_CACHE=0 MESA_SHADER_CACHE_DISABLE=true METAL2VULKAN_LOG_PIPELINES=1 \ /usr/bin/time -f 'maxrss %M KB' timeout --kill-after=10 "$timeout" \ "$bin" time --repeats "$repeats" --combo "$combo" --draws "$set_dir/draws.jsonl" --id "$id" \ --sources "$set_dir/src" --device-type gpu --shader-cache "$run/cache" --out "$run/out" \ ${queue_first:+--queue-first yes} ${policy:+--policy "$policy"} \ > "$run/stdout.txt" 2> "$run/stderr.txt" || rc=$? t=$run/out/$id.time.tsv python3 -I - "$id" "$combo" "$run/stderr.txt" >> "$out/makings.tsv" <<'PY' import re, sys ident, combo, path = sys.argv[1:4] line_re = re.compile(r'pipeline creation: .*? pipeline (\d+): (?:made|failed) in ([0-9.]+) ms' r'(?: after waiting ([0-9.]+) ms for its turn)?; largest module (\d+) words(.*)') for line in open(path, errors='replace'): m = line_re.search(line) if m: at = 'creation' if "made at its state's creation" in m.group(5) else 'draw' print('\t'.join([ident, combo, m.group(1), at, m.group(2), m.group(3) or '0', m.group(4)])) PY creation=$(awk -F'\t' -v i="$id" -v c="$combo" '$1 == i && $2 == c && $4 == "creation" { s += $5 } END { printf "%.1f", s }' "$out/makings.tsv") qf='' [ -z "$queue_first" ] || qf=$(printf '\t%s' "$(field "$t" queue_first_ms)") printf '%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s%s\n' "$id" "$combo" "$rc" \ "$(field "$t" created_ms)" "$(field "$t" first_draw_ms)" \ "$(field "$t" gpu_ms_median_min)" "$(field "$t" gpu_ms_median_min 4)" \ "$(grep -oE 'maxrss [0-9]+' "$run/stderr.txt" | tail -1 | cut -d' ' -f2)" "$creation" \ "$qf" >> "$out/runs.tsv" if [ "$rc" -ne 0 ]; then # The reason, without shader text: the harness's own error lines. grep -E '^(error|Error|corpus-draw-ab)|REFUSED|SKIP|panicked' "$run/stderr.txt" | cut -c1-240 | head -4 | sed "s/^/$id $combo rc $rc: /" >> "$out/failures.txt" || true fi rm -rf "$run" done done