#!/usr/bin/env python3 """Census of the `fpu_cw` field the shim's template hooks record on every call. Each hook snapshots the x87 control word as it finds it, so a run's trace carries independent samples of the word from *inside* the turn pipeline -- phase 4 (MoveFleet), phase 6 (ServerSystem::ProcessTurn) and phase 8 (ComputeBudget / ProcessResearch). That is the evidence that a control word forced at the turn gate actually held for the duration of the turn, rather than being silently restored partway through (which would produce identical saves and a completely false "nothing depends on precision" conclusion). Usage: cw_census.py [...] """ import gzip import json import sys from collections import Counter, defaultdict def _open(path): op = gzip.open if path.endswith(".gz") else open return op(path, "rt", encoding="utf-8", errors="replace") def census(path): per_hook = defaultdict(Counter) first_last = {} n = 0 with _open(path) as f: for line in f: line = line.strip() if not line.startswith("{"): continue try: rec = json.loads(line) except json.JSONDecodeError: continue hook = rec.get("hook") if not hook: continue n += 1 for arg in rec.get("args", []) or []: if arg.get("n") == "fpu_cw": cw = int(arg["v"]) per_hook[hook][cw] += 1 key = (hook, cw) if key not in first_last: first_last[key] = [rec.get("call_id"), rec.get("call_id")] else: first_last[key][1] = rec.get("call_id") return n, per_hook, first_last def main(): for path in sys.argv[1:]: n, per_hook, first_last = census(path) print(f"== {path} ({n} hook records)") total = Counter() for hook in sorted(per_hook): for cw, count in sorted(per_hook[hook].items()): lo, hi = first_last[(hook, cw)] total[cw] += count print(f" {hook:<42} cw=0x{cw:04x} x{count:<5} call_id {lo}..{hi}") if not per_hook: print(" (no hook recorded an fpu_cw field)") else: vals = ", ".join(f"0x{cw:04x} x{c}" for cw, c in sorted(total.items())) print(f" ALL SAMPLES: {vals}") print() if __name__ == "__main__": main()