#!/usr/bin/env python3 """Independent re-derivation of every PROBE-RESULTS.md figure from the raw run files. Reads strace.log directly (not result.json's parsed copy), the listener logs in result.json, marks.json, and a read-only (immutable) open of each queue DB copy. Prints one line per run plus aggregate ranges. """ import collections import glob import hashlib import json import os import re import sqlite3 import sys COLLECTOR = "mobile.events.data.microsoft.com" LINE = re.compile(r"^(\d+)\s+(\d+\.\d+)\s+(\w+)\((.*)$") def raw_strace(path, t0): out = collections.Counter() c443, c53, other, unix, sockets, pids_dns = [], [], [], [], 0, set() for line in open(path, errors="replace"): m = LINE.match(line) if not m: continue pid, ts, call, rest = int(m.group(1)), float(m.group(2)), m.group(3), m.group(4) t = ts - t0 out[call] += 1 if call == "socket": sockets += 1 if call not in ("connect", "sendto", "sendmsg", "sendmmsg", "bind"): continue if "AF_UNIX" in rest: unix.append((round(t, 3), pid, call)) continue a = re.search(r'sin_port=htons\((\d+)\), sin_addr=inet_addr\("([\d.]+)"\)', rest) a6 = re.search(r'sin6_port=htons\((\d+)\).*?inet_pton\(AF_INET6, "([^"]+)"', rest) if a: dst = f"{a.group(2)}:{a.group(1)}" elif a6: dst = f"[{a6.group(2)}]:{a6.group(1)}" else: continue # sendto on a connected socket (NULL address) if call == "connect" and dst == "127.0.0.2:443": c443.append(round(t, 3)) elif call == "connect" and dst == "127.0.0.1:53": c53.append(round(t, 3)); pids_dns.add(pid) else: other.append((round(t, 3), call, dst)) last = open(path, errors="replace").read().strip().splitlines()[-1] return dict(calls=dict(out), c443=c443, c53=c53, other=other, unix=unix, sockets=sockets, pids_dns=sorted(pids_dns), last=last) def queue(run_dir, deviceid, machine_id, ses_id): db = os.path.join(run_dir, "queue", "onnxruntime.db") if not os.path.exists(db): return None con = sqlite3.connect(f"file:{db}?mode=ro&immutable=1", uri=True) rows = con.execute("SELECT retry_count, latency, payload FROM events ORDER BY timestamp, record_id").fetchall() con.close() dev_hash = ("c:" + hashlib.sha256(deviceid.encode()).hexdigest().upper()) if deviceid else None info = [] for retry, lat, payload in rows: text = bytes(payload).decode("latin-1") info.append(dict(retry=retry, latency=lat, name=re.findall(r"(ProcessInfo|RegisterEpLibrary(?:Start|End)|ModelLoad(?:Start|End)|" r"SessionCreation(?:Start|End)?|EpDeviceUsage|RuntimePerf|SystemMetrics)", text), devhash=bool(dev_hash and dev_hash in text), ses=bool(ses_id and ses_id in text), interp="/probe/work/venvs/" in text, raw_dev=bool(deviceid and deviceid in text), raw_mid=bool(machine_id and machine_id in text), host="ortprobe-host" in text, canaries=[c for c in ("probe_graph_canary", "probe-kit", "probe_meta_key", "probe_meta_value_canary") if c in text])) return info def main(runs_dir): agg = collections.defaultdict(list) for rj in sorted(glob.glob(os.path.join(runs_dir, "*-r*", "result.json"))): d = os.path.dirname(rj) r = json.load(open(rj)) t0 = r["t_launch"] st = raw_strace(os.path.join(d, "strace.log"), t0) dns = [(round(e["t"] - t0, 3), e["name"], e["qtype"]) for e in r["dns"]] tls = [(round(e["t_accept"] - t0, 3), e.get("sni"), e["bytes"], tuple(e.get("alpn") or []), tuple(e.get("versions") or [])) for e in r["tls"]] m = json.load(open(os.path.join(d, "marks.json"))) imp = m.get("t_import_done", t0) - t0 # pair each TLS accept with the nearest strace connect to :443 pair = [min((abs(a - t[0]) for a in st["c443"]), default=None) for t in tls] # each attempt's A/AAAA spread by_attempt = collections.defaultdict(list) for t, n, q in dns: by_attempt[round(t, 1)].append((t, q)) spread = max((max(x for x, _ in v) - min(x for x, _ in v) for v in by_attempt.values()), default=0) files = {os.path.basename(f["path"]): f["mtime_rel_s"] for f in r["files_after"]} dev = open(os.path.join(d, "deviceid")).read().strip() if os.path.exists(os.path.join(d, "deviceid")) else None mid = open(os.path.join(d, "machine-id")).read().strip() ses = (r.get("tmp_ses") or {}).get("content") ses_id = ses.split()[-1] if ses else None q = queue(d, dev, mid, ses_id) name = os.path.basename(d) print(f"{name}: rc={r['subject_rc']} selftest={r['selftest']['pass']} void={r.get('void')} " f"dns={len(dns)} names={sorted({n for _, n, _ in dns})} tls={len(tls)} http={len(r['http'])} " f"tls_t={[t[0] for t in tls]} strace443={st['c443']} strace53={len(st['c53'])} " f"other_inet={st['other']} unix={len(st['unix'])}@{[u[0] for u in st['unix']]} sockets={st['sockets']} " f"calls={st['calls']} pair_maxdiff={max([p for p in pair if p is not None], default=None)} " f"AAAA_spread={spread:.4f} dns_pids={st['pids_dns']} last='{st['last'][:40]}'") print(f" hello={sorted({(t[2], t[3], t[4]) for t in tls})} import={imp:.3f} " f"first-import={(tls[0][0] - imp) if tls else None} exit_minus_mark={r['t_exit_rel_s'] - (m['t_exit'] - t0):.3f} " f"files={files} outside_window={len(r['events_outside_window'])} listener_errors={len(r['listener_errors'])}") if q is not None: names = [tuple(x["name"]) for x in q] print(f" queue rows={len(q)} names={names}") print(f" retry={sorted({x['retry'] for x in q})} latency={sorted({x['latency'] for x in q})} " f"devhash_all={all(x['devhash'] for x in q)} ses_all={all(x['ses'] for x in q)} " f"interp_all={all(x['interp'] for x in q)} raw_dev={any(x['raw_dev'] for x in q)} " f"raw_mid={any(x['raw_mid'] for x in q)} host={any(x['host'] for x in q)} " f"canary_rows={[(x['name'], x['canaries']) for x in q if x['canaries']]}") arm = r["arm"] agg[arm].append(dict(tls=[t[0] for t in tls], imp=imp, files=files, exitgap=r['t_exit_rel_s'] - (m['t_exit'] - t0))) print("\n== aggregates") ort = [x for a in ("ort130-default", "ort130-apioff", "ort129-default") for x in agg.get(a, [])] if ort: for i in range(4): g = [x["tls"][i + 1] - x["tls"][i] for x in ort if len(x["tls"]) > i + 1] print(f"gap{i + 1}: {min(g):.2f}..{max(g):.2f}") print("first attempt:", sorted(round(x["tls"][0], 3) for x in ort)) print("first-import:", sorted(round(x["tls"][0] - x["imp"], 3) for x in ort)) print("last attempt:", min(x["tls"][-1] for x in ort), max(x["tls"][-1] for x in ort)) for k in ("deviceid", ".ses", "onnxruntime.db", "mat-debug-15.log"): v = [x["files"].get(k) for x in ort] print(k, "mtime_rel range", min(v), max(v)) allx = [x for v in agg.values() for x in v] print("exit gap range", round(min(x["exitgap"] for x in allx), 3), round(max(x["exitgap"] for x in allx), 3)) if __name__ == "__main__": main(sys.argv[1])