#!/usr/bin/env bash # Headless Tracy capture + per-zone summary (no GUI, no root). # # Needs a build configured with -Denable_profiling=true (Tracy client pinned in third_party/tracy.wrap) # and the matching tools (Arch `tracy` 0.15.0: tracy-capture, tracy-csvexport). # # scripts/profile_capture.sh run OUT -- COMMAND [ARGS...] # launch a program and capture all of it # scripts/profile_capture.sh attach OUT SECONDS [PORT] # capture an already-running profiled # # process (daemon, tray) for SECONDS # Writes OUT.tracy (open in tracy-profiler), OUT.zones.csv (tracy-csvexport) and prints the top # zones by total time. Builds use Tracy on-demand mode: nothing is recorded (and nothing costs) # unless a capture is connected. Every run here is niced. set -euo pipefail mode=${1:?usage: profile_capture.sh run OUT -- CMD... | attach OUT SECONDS [PORT]} out=${2:?output prefix} shift 2 command -v tracy-capture >/dev/null || { echo "tracy-capture not found (install the tracy tools)" >&2; exit 2; } command -v tracy-csvexport >/dev/null || { echo "tracy-csvexport not found" >&2; exit 2; } mkdir -p "$(dirname "$out")" case "$mode" in run) [ "${1:-}" = "--" ] || { echo "run: put the command after --" >&2; exit 2; } shift [ $# -gt 0 ] || { echo "run: no command" >&2; exit 2; } port=${TRACY_PORT:-8086} # Capture first (it waits for the client), then the program; TRACY_NO_EXIT keeps the program # alive at exit until all data is sent. nice -n 19 tracy-capture -o "$out.tracy" -f -a 127.0.0.1 -p "$port" >"$out.capture.log" 2>&1 & cap=$! sleep 0.5 TRACY_NO_EXIT=1 TRACY_PORT=$port nice -n 19 "$@" || echo "note: command exited $?" >&2 wait "$cap" || true ;; attach) secs=${1:?attach: seconds} port=${2:-8086} nice -n 19 tracy-capture -o "$out.tracy" -f -a 127.0.0.1 -p "$port" -s "$secs" >"$out.capture.log" 2>&1 ;; *) echo "unknown mode $mode" >&2 exit 2 ;; esac [ -s "$out.tracy" ] || { echo "no capture written (see $out.capture.log)" >&2; tail -5 "$out.capture.log" >&2; exit 1; } tracy-csvexport "$out.tracy" >"$out.zones.csv" echo "capture: $out.tracy zones: $out.zones.csv" # CSV columns: name,src_file,src_line,total_ns,total_perc,counts,mean_ns,min_ns,max_ns,std_ns python3 -I - "$out.zones.csv" <<'PY' import csv, sys rows = list(csv.DictReader(open(sys.argv[1]))) rows.sort(key=lambda r: -int(r["total_ns"])) print(f"{'zone':32} {'count':>8} {'total ms':>11} {'mean ms':>9} {'max ms':>9}") for r in rows[:20]: print(f"{r['name'][:32]:32} {int(r['counts']):8d} {int(r['total_ns'])/1e6:11.1f} " f"{int(r['mean_ns'])/1e6:9.2f} {int(r['max_ns'])/1e6:9.1f}") PY