#!/usr/bin/env python3
"""GPU Trace (ngfx --auto-export) GPUTRACE_REGIMES.xls: for each range whose name matches,
the hardware-unit metrics (mean over the traced frames), highest first.
python3 -I tools/perf/gputrace_regimes.py OUTDIR/BASE/GPUTRACE_REGIMES.xls "Render Depth Pre-Pass" [TOP]
pct_of_peak metrics say WHICH unit is the limiter (one near 100% = bound by it; all low = a
latency / wait / under-filled GPU problem, not a throughput one).
"""
import collections, csv, sys
path, needle = sys.argv[1], sys.argv[2]
top = int(sys.argv[3]) if len(sys.argv) > 3 else 25
with open(path, newline="") as f:
rows = list(csv.reader(f, delimiter="\t"))
hdr = rows[0]
for r in rows[1:]:
if not r or needle not in r[0]:
continue
acc = collections.defaultdict(list)
for name, v in zip(hdr[1:], r[1:]):
try:
acc[name].append(float(v))
except ValueError:
pass
means = {k: sum(v) / len(v) for k, v in acc.items() if v}
print(f"== {r[0]} ({len(means)} metrics)")
for k, v in sorted(means.items(), key=lambda kv: -kv[1] if "pct" in kv[0] else 0)[:top]:
print(f" {v:12.3f} {k}")