#!/usr/bin/env python3 """Capture native scrolling, lenses and live stepping with an opt-in Tracy build.""" import argparse import csv import json from pathlib import Path import re import subprocess as sp ROOT = Path(__file__).resolve().parents[2] p = argparse.ArgumentParser(description=__doc__) p.add_argument('--tracy', type=Path, required=True, help='Tracy checkout with build-capture/ and build-csvexport/') p.add_argument('--gui', type=Path, default=ROOT / 'gui/zig-out/bin/ztrace-gui') p.add_argument('--out', type=Path, required=True) p.add_argument('--size', default='2240x1260') p.add_argument('--zoom', type=int, default=100) a = p.parse_args() a.out = a.out.resolve() a.out.mkdir(parents=True, exist_ok=True) for tool in (a.gui, a.tracy / "build-capture/tracy-capture", a.tracy / "build-csvexport/tracy-csvexport"): assert tool.is_file(), f"Missing executable: {tool}" target = a.out / 'segments' sp.run(['cc', '-g', '-O0', '-no-pie', str(ROOT / 'gui/targets/segments.c'), '-o', str(target)], check=True) symbols = dict((name, int(address, 16)) for address, name in re.findall(r'^([0-9a-f]+) \w (\w+)$', sp.check_output(['nm', '-n', str(target)], text=True), re.M)) script = f'''console launch {target} await stops 1 console break checkpoint console continue await execution 1 console set zoom {a.zoom} frames 100 console goto memory {hex(symbols['head'])} console set focus memory frames 30 ''' for _ in range(6): for key in ('j', 'k'): script += (f'key {key}\nframes 2\n' * 8) for _ in range(6): script += f'console lens {hex(symbols["head"])}\nframes 15\nkey escape\nframes 3\n' for i in range(20): script += f'key s\nawait execution {i + 2}\nframes 6\n' script += 'frames 60\nexpect execution 21\nexpect status stopped\n' (a.out / 'input.script').write_text(script) with (a.out / 'gui.log').open('w') as log, (a.out / 'capture.log').open('w') as cap: app = sp.Popen([str(a.gui.resolve()), '--fresh', '--size', a.size, '--state', str(a.out / 'gui.state'), '--script', str(a.out / 'input.script')], stdout=log, stderr=log) capture = sp.Popen([str(a.tracy / 'build-capture/tracy-capture'), '-o', str(a.out / 'frames.tracy'), '-f', '-s', '25'], stdout=cap, stderr=cap) try: assert app.wait(timeout=90) == 0, (a.out / 'gui.log').read_text() assert capture.wait(timeout=30) == 0, (a.out / 'capture.log').read_text() finally: for proc in (app, capture): if proc.poll() is None: proc.terminate() proc.wait() with (a.out / 'events.csv').open('w') as out: sp.run([str(a.tracy / 'build-csvexport/tracy-csvexport'), '-u', str(a.out / 'frames.tracy')], stdout=out, check=True) values = {} starts = [] with (a.out / 'events.csv').open() as f: for row in csv.DictReader(f): at = int(row['ns_since_start']) / 1e9 if at < 2: continue # Cold font/source loading is separate from warm interaction. name = row['name'] if name not in ('frame', 'draw', 'paint commands', 'present', 'trace refresh'): continue values.setdefault(name, []).append(int(row['exec_time_ns']) / 1e6) if name == 'frame': starts.append(at) starts.sort() values['frame interval'] = [(b - a) * 1000 for a, b in zip(starts, starts[1:])] result = {} for name, v in values.items(): v.sort() assert v, 'No Tracy frames: rebuild with -Dtracy=CHECKOUT' result[name] = dict(samples=len(v), **{label: v[min(len(v)-1, int(len(v)*q))] for label, q in [('median_ms', .5), ('p95_ms', .95), ('p99_ms', .99), ('max_ms', 1)]}, over_20ms=sum(x > 20 for x in v)) (a.out / 'results.json').write_text(json.dumps(result, indent=2) + '\n') print(json.dumps(result, indent=2))