#!/usr/bin/env python3 """Replay: python3 cadence.py.txt RUN_DIR Presentation cadence during injected contact, from the client's own journal (shell-ui-journal.txt, `NNNms frame-done` / `NNNms commit` rows on its monotonic millisecond clock): distinct_presented_fps frame-done rows more than 3 ms apart (one compositor frame can answer several pending callbacks) callback_to_commit_ms frame-done -> the next commit (client draw time) commits_per_presented rendered frames per distinct presented frame With shell-journal.txt (compositor benchmark armed), also sway's own repaint: sway_render_ms repaint-timing start_ns -> commit_start_ns damage_px frame-damage pixels per repaint """ import collections, json, re, sys from pathlib import Path run = Path(sys.argv[1]); meta = json.loads((run / 'meta.json').read_text()) def contact(t): return any(g['start'] <= t <= g['end'] for g in meta['gestures']) def q(v, p): v = sorted(v); return round(v[min(len(v) - 1, int(len(v) * p))], 2) if v else None rows = [] for line in (run / 'shell-ui-journal.txt').read_text().splitlines(): m = re.match(r'^(\d+\.\d+) .* (\d+)ms (frame-done|commit)$', line) if m: rows.append((float(m[1]), int(m[2]), m[3])) inside = [r for r in rows if contact(r[0])] done = [r[1] for r in inside if r[2] == 'frame-done'] distinct = [t for i, t in enumerate(done) if i == 0 or t - done[i - 1] > 3] seconds = sum(g['end'] - g['start'] for g in meta['gestures']) gaps = [b - a for a, b in zip(distinct, distinct[1:]) if b - a < 500] lat = [b[1] - a[1] for a, b in zip(rows, rows[1:]) if a[2] == 'frame-done' and b[2] == 'commit' and contact(a[0])] out = {'contact_seconds': round(seconds, 2), 'distinct_presented_fps': round(len(distinct) / seconds, 2), 'distinct_interval_ms': {'median': q(gaps, .5), 'p95': q(gaps, .95)}, 'commits_per_presented': round(sum(r[2] == 'commit' for r in inside) / max(1, len(distinct)), 2), 'callback_to_commit_ms': {'median': q(lat, .5), 'p10': q(lat, .1), 'p90': q(lat, .9), 'n': len(lat)}} bench = run / 'shell-journal.txt' if bench.exists(): text = bench.read_text() timing = [(int(a), int(b)) for a, b in re.findall(r'repaint-timing .*?start_ns=(\d+) commit_start_ns=(\d+)', text)] render = [(b - a) / 1e6 for a, b in timing] out['sway_render_ms'] = {'median': q(render, .5), 'p10': q(render, .1), 'p90': q(render, .9), 'n': len(render)} out['damage_px'] = collections.Counter(int(d) for d in re.findall(r'frame-damage .*?damage_px=(\d+)', text)).most_common(4) print(json.dumps(out, indent=1))