strawberry/scripts/profile_capture.sh
trvon 11d3614348
profiling: pinned Tracy client, headless capture script, scan bench, tray + scan-stage zones
- third_party/tracy.wrap pins the Tracy client to v0.15.0 (sha256-checked), matching
  the Arch tracy 0.15.0 capture/export tools (client and tools must match exactly);
  built with tracy_enable + on_demand, so nothing is recorded or paid for unless a
  capture is connected. -Denable_profiling=true picks it up; default builds are
  unchanged (macros are no-ops).
- scripts/profile_capture.sh: headless (no GUI, no root) `run OUT -- CMD` or
  `attach OUT SECONDS` -> OUT.tracy + OUT.zones.csv + a top-zones table.
- strawberry-scan-bench (dev tool, not installed): the real scan_on_demand path over
  files with the real governor and rule set, so the scan path can be profiled
  without root or the daemon.
- Zones: scan_on_demand, image.read, image.sha256 (next to yara_scan/scan_image/
  assess_exec), tray.* (poll, on_reply, refresh_evidence, update_status/_policy,
  refresh_findings, notify) and evidence/database reloads.

Baseline (6 yams ASan binaries, 668 MiB, idle host): YARA 86%, read 9%, sha256 5%,
~61 MiB/s; PT_LOAD segments are 35-48% of those files. DEVELOPER.md: profiling
section, and the balanced row corrected (64 MiB image cap, 768 MiB budget).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 18:29:57 -07:00

59 lines
2.7 KiB
Bash
Executable file

#!/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