"""The long run: totals per measurement and the time line, from a format 5 save. python tools/soak.py SAVE summary: every measurement's n, mean, sd, min, max python tools/soak.py SAVE --series the time line, one row a kept cycle python tools/soak.py SAVE --csv OUT.csv the time line as CSV python tools/soak.py SAVE --ref the summary beside the console's reference ranges Units: counts as counted; DMA, DMP*, DMB0 and VPOS in MCLK (the cart keeps H half steps); YMDK as the share of reads that kept the status (630.k, 631.k: the k-th wait); YMFT in lines. """ import csv import json import math import struct import sys from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parent)) import decode ROOT = Path(__file__).resolve().parent.parent HALF = 3420 / 420 # MCLK in a half H step KINDS = {1: "count", 2: "pair", 3: "pos", 4: "set", 5: "pair H32", 6: "pos H32"} STATE = {0: "none", 1: "running", 2: "done", 3: "stopped"} STATS = 64 TICK_SCALE = 256 # fallback; build.py has the real one def tick_scale(): for line in (ROOT / "build.py").read_text().splitlines(): if line.startswith("TICK_SCALE"): return int(line.split("=")[1]) return TICK_SCALE def series_key(k): first = [640, 13, 23, 14, 24, 1, 711, 700, 6] if k < 9: return first[k] k -= 9 return 1200 + 20 * (k >> 1) + 10 * (k & 1) def name(tag, key): if tag == "YMDK": return f"YMDK {630 + (key - 20000) // 32}.{(key - 20000) % 32}" return f"{tag} {key}" def scale(tag, kind): if tag == "YMDK": return 1 if kind in (5, 6): return 20 # H32: an H step is 20 MCLK return HALF if kind in (2, 3) else 1 def load(path): r = decode.parse(path) s = r["raw"] area = decode.soak_area(s) if not area or s[area[0]:area[0] + 4] != b"SOAK": raise SystemExit(f"{path}: no long run in this save") a = s[area[0]:area[0] + area[1]] target, done = struct.unpack_from(">HH", a, 4) head = dict(target=target, done=done, pause=a[8], state=STATE.get(a[9], a[9]), merging=a[10], ticks=struct.unpack_from(">I", a, 12)[0], first_run=struct.unpack_from(">I", a, 16)[0], build=a[20:28].decode("ascii", "replace").strip(), count=struct.unpack_from(">H", a, 28)[0], stride=struct.unpack_from(">H", a, 30)[0], boot=struct.unpack_from(">I", a, 36)[0], nser=struct.unpack_from(">H", a, 40)[0], entry=struct.unpack_from(">H", a, 42)[0]) fmt = struct.unpack_from(">H", s, 4)[0] if fmt >= 6: cap, STAT, nstats = struct.unpack_from(">H", a, 44)[0], a[46], struct.unpack_from(">H", a, 48)[0] else: cap, STAT, nstats = 176, 48, a[11] SERIES = STATS + cap * STAT stats = [] for i in range(nstats): e = a[STATS + i * STAT:STATS + (i + 1) * STAT] tag = e[:4].decode("ascii", "replace") key, kind, nv = struct.unpack_from(">HBB", e, 4) n = struct.unpack_from(">I", e, 8)[0] st = dict(tag=tag, key=key, kind=kind, n=n, name=name(tag, key)) if kind == 4: vals = [struct.unpack_from(">IH", e, 12 + j * 6) for j in range(nv)] st["values"] = vals st["other"] = n - sum(c for _, c in vals) elif STAT == 32: base, lo, hi, s1 = struct.unpack_from(">ihhi", e, 12) lo, hi = base + lo, base + hi sq = struct.unpack_from(">I", e, 24)[0] << 32 | struct.unpack_from(">I", e, 28)[0] else: base, lo, hi, s1 = struct.unpack_from(">iiii", e, 12) sq = struct.unpack_from(">I", e, 28)[0] << 32 | struct.unpack_from(">I", e, 32)[0] if kind != 4: k = scale(tag, kind) mean_d = s1 / n if n else 0 var = max(sq / n - mean_d ** 2, 0) if n else 0 st.update(mean=(base + mean_d) * k, sd=math.sqrt(var) * k, min=lo * k, max=hi * k) stats.append(st) rows = [] for i in range(head["count"]): e = a[SERIES + i * head["entry"]:SERIES + (i + 1) * head["entry"]] cyc, _, t = struct.unpack_from(">HHI", e, 0) vals = struct.unpack_from(f">{head['nser']}i", e, 8) rows.append(dict(cycle=cyc, ticks=t, values=[None if v == -0x80000000 else v / 8 for v in vals])) return head, stats, rows def series_labels(stats, nser): kinds = {s["key"]: (s["tag"], s["kind"]) for s in stats} out = [] for k in range(nser): key = series_key(k) tag, kind = kinds.get(key, ("?", 1)) out.append((f"{tag} {key}", scale(tag, kind))) return out def main(): path = sys.argv[1] head, stats, rows = load(path) ts = tick_scale() minutes = head["ticks"] * ts / 256 / 60 / 60 print(f"long run {head['state']}: {head['done']} of {head['target']} cycles, about {minutes:.1f} min; " f"build {head['build']}, from run {head['first_run']}, boot {head['boot']}; " f"time line {head['count']} entries, one every {head['stride']} cycle(s)" + ("; MERGE CUT SHORT, the last cycle may be counted in part" if head["merging"] else "")) if "--series" in sys.argv or "--csv" in sys.argv: labels = series_labels(stats, head["nser"]) table = [[r["cycle"], round(r["ticks"] * ts / 256 / 3600, 2)] + [None if v is None else round(v * k, 2) for v, (_, k) in zip(r["values"], labels)] for r in rows] header = ["cycle", "minutes"] + [l for l, _ in labels] if "--csv" in sys.argv: out = sys.argv[sys.argv.index("--csv") + 1] with open(out, "w", newline="") as f: csv.writer(f).writerows([header] + table) print(out, len(table), "rows") else: show = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] print(" ".join(f"{h:>10}" for h in [header[i] for i in show])) for t in table: print(" ".join(f"{('' if v is None else v):>10}" for v in [t[i] for i in show])) return ref = json.loads((ROOT / "hardware" / "reference.json").read_text())["metrics"] if "--ref" in sys.argv else {} print(f"{'measurement':16}{'n':>7}{'mean':>14}{'sd':>10}{'min':>12}{'max':>12}") for s in stats: if s["kind"] == 4: vals = " ".join(f"{v:X}x{c}" for v, c in s["values"]) + (f" +{s['other']} other" if s["other"] else "") print(f"{s['name']:16}{s['n']:>7} {vals}") continue line = f"{s['name']:16}{s['n']:>7}{s['mean']:>14.2f}{s['sd']:>10.2f}{s['min']:>12.1f}{s['max']:>12.1f}" m = ref.get(f"{s['tag']} {s['key']}") or ref.get(f"{s['tag']} {s['key']} mclk") or ref.get(f"{s['tag']} {s['key']} steps") if m and m.get("kind") == "range": line += f" console {m['min']}-{m['max']}" print(line) if __name__ == "__main__": main()