oxedyne/fe2o3/fe2o3_austenite/tools/bench/corpus/rec.py
6.8 KiB, 1 run, executable
created by r1870400018:72514, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | #!/usr/bin/env python3 |
| 2 | """Folds one document's run directory into one JSONL record (called by sweep.sh). |
| 3 | |
| 4 | The directory holds, for run i, `a<i>.{time,out,err,json,load,rc}` (Austenite) and `t<i>.{time,err,load,rc}` |
| 5 | (typst), `t.pages`, optionally `tt.json` and `tt.time` (one typst run under `--timings`, for its stage split) |
| 6 | and `w.out` (watch_edit.sh's line). Stages are put side by side under one set of names: |
| 7 | |
| 8 | load Austenite run.load; typst's wall time outside its trace plus parsing and font discovery |
| 9 | eval run.eval; typst `eval` |
| 10 | realise the passes' realise; typst `realize` and the show rules under it (each span takes the |
| 11 | stage of its own name, else of its nearest named ancestor) |
| 12 | layout the passes' flow, place, decorate, settle and probe (line breaking is inside flow); typst's |
| 13 | layout iterations less their realize |
| 14 | emit sink, finish and write; typst `pdf` |
| 15 | |
| 16 | Typst's stages are read from the trace run and fitted to its median wall time (see typ_stages), so the |
| 17 | trace's own cost does not count. Usage: rec.py <dir> <runs> <meta-json> |
| 18 | """ |
| 19 | |
| 20 | import json |
| 21 | import os |
| 22 | import re |
| 23 | import statistics |
| 24 | import sys |
| 25 | |
| 26 | STAGES = ("load", "eval", "realise", "layout", "emit") |
| 27 | # Typst's layout spans that are not named for it; a show rule's own span under `realize` stays realise. |
| 28 | LAYOUT = {"block", "collect", "prepare", "finalize", "page run", "introspect pages", "check stabilized", |
| 29 | "grid_to_cellgrid"} |
| 30 | |
| 31 | |
| 32 | def read(path, default=""): |
| 33 | try: |
| 34 | with open(path, encoding="utf-8", errors="replace") as f: |
| 35 | return f.read() |
| 36 | except OSError: |
| 37 | return default |
| 38 | |
| 39 | |
| 40 | def classify(rc, err): |
| 41 | if rc == 0: |
| 42 | return "ok" |
| 43 | if rc == 124: |
| 44 | return "timeout" |
| 45 | if rc in (137, 9): |
| 46 | return "killed" |
| 47 | if "panicked at" in err or rc == 101: |
| 48 | return "panic" |
| 49 | if rc > 128: |
| 50 | return f"signal{rc - 128}" |
| 51 | return "error" |
| 52 | |
| 53 | |
| 54 | def message(err): |
| 55 | lines = [l.strip() for l in err.splitlines() if l.strip()] |
| 56 | pan = [l for l in lines if "panicked at" in l] |
| 57 | if pan: |
| 58 | i = lines.index(pan[0]) |
| 59 | return " | ".join(lines[i:i + 2])[:400] |
| 60 | errs = [l for l in lines if l.startswith("error") or "Err" in l or "rror:" in l] |
| 61 | pick = errs[-2:] if errs else lines[-2:] |
| 62 | return " | ".join(pick)[:400] |
| 63 | |
| 64 | |
| 65 | def engine(d, tag, runs): |
| 66 | out = {"runs": []} |
| 67 | worst = None |
| 68 | for i in range(1, runs + 1): |
| 69 | rc_s = read(os.path.join(d, f"{tag}{i}.rc")).strip() |
| 70 | if not rc_s: |
| 71 | break |
| 72 | rc = int(rc_s) |
| 73 | err = read(os.path.join(d, f"{tag}{i}.err")) |
| 74 | st = classify(rc, err) |
| 75 | r = {"rc": rc, "status": st, "load": read(os.path.join(d, f"{tag}{i}.load")).strip()} |
| 76 | t = read(os.path.join(d, f"{tag}{i}.time")).split() |
| 77 | if st == "ok" and len(t) >= 4: |
| 78 | r.update(wall=float(t[0]), user=float(t[1]), sys=float(t[2]), rss_kb=int(t[3])) |
| 79 | w = read(os.path.join(d, f"{tag}{i}.wall")).strip() |
| 80 | if w: |
| 81 | r["wall"] = float(w) |
| 82 | out["runs"].append(r) |
| 83 | if st != "ok" and worst is None: |
| 84 | worst = (st, message(err)) |
| 85 | if not out["runs"]: |
| 86 | out["status"] = "absent" |
| 87 | return out |
| 88 | if worst: |
| 89 | out["status"], out["msg"] = worst |
| 90 | else: |
| 91 | out["status"] = "ok" |
| 92 | walls = [r["wall"] for r in out["runs"] if "wall" in r] |
| 93 | out["wall"] = statistics.median(walls) if walls else None |
| 94 | out["cpu"] = statistics.median([r["user"] + r["sys"] for r in out["runs"] if "user" in r]) if walls else None |
| 95 | out["rss_kb"] = max(r.get("rss_kb", 0) for r in out["runs"]) |
| 96 | return out |
| 97 | |
| 98 | |
| 99 | def aus_stages(d, runs, wall): |
| 100 | # The run whose wall is the median's, else the last. |
| 101 | best, best_gap = None, None |
| 102 | for i in range(1, runs + 1): |
| 103 | t = (read(os.path.join(d, f"a{i}.wall")) or read(os.path.join(d, f"a{i}.time"))).split() |
| 104 | p = os.path.join(d, f"a{i}.json") |
| 105 | if not t or not os.path.exists(p): |
| 106 | continue |
| 107 | gap = abs(float(t[0]) - wall) if wall is not None else 0 |
| 108 | if best_gap is None or gap < best_gap: |
| 109 | best, best_gap = p, gap |
| 110 | if best is None: |
| 111 | return None |
| 112 | try: |
| 113 | j = json.loads(read(best)) |
| 114 | except ValueError: |
| 115 | return None |
| 116 | ns = lambda node, k: (node.get(k) or {}).get("ns", 0) / 1e9 |
| 117 | run = j.get("run", {}) |
| 118 | s = dict.fromkeys(STAGES, 0.0) |
| 119 | s["load"] = ns(run, "load") |
| 120 | s["eval"] = ns(run, "eval") |
| 121 | s["emit"] = ns(run, "finish") + ns(run, "write") |
| 122 | flow = 0.0 |
| 123 | for p in j.get("pass", []): |
| 124 | s["realise"] += ns(p, "realise") |
| 125 | f = ns(p, "flow") |
| 126 | flow += f |
| 127 | s["layout"] += f + ns(p, "place") + ns(p, "decorate") + ns(p, "settle") + ns(p, "probe") |
| 128 | s["emit"] += ns(p, "sink") |
| 129 | s["flow"] = flow |
| 130 | s["total"] = j.get("total", 0) / 1e9 |
| 131 | s["passes"] = j.get("passes") |
| 132 | return s |
| 133 | |
| 134 | |
| 135 | def typ_stages(d, wall): |
| 136 | p = os.path.join(d, "tt.json") |
| 137 | if not os.path.exists(p) or wall is None: |
| 138 | return None |
| 139 | try: |
| 140 | ev = json.loads(read(p)) |
| 141 | except ValueError: |
| 142 | return None |
| 143 | cat_of = {"eval": "eval", "create source": "load", "parse": "load", "discover fonts": "load", |
| 144 | "realize": "realise", "layout document": "layout", "pdf": "emit"} |
| 145 | acc = dict.fromkeys(STAGES, 0.0) |
| 146 | stack = [] # (name, start, category, child time) |
| 147 | for e in ev: |
| 148 | ph = e.get("ph") |
| 149 | if ph == "B": |
| 150 | name = e.get("name", "") |
| 151 | cat = cat_of.get(name) |
| 152 | if cat is None and (name.startswith(("iter (", "layout", "linebreak", "math ")) or name in LAYOUT): |
| 153 | cat = "layout" |
| 154 | if cat is None and stack: |
| 155 | cat = stack[-1][2] |
| 156 | stack.append([name, e["ts"], cat, 0.0]) |
| 157 | elif ph == "E" and stack: |
| 158 | name, start, cat, child = stack.pop() |
| 159 | dur = e["ts"] - start |
| 160 | if cat: |
| 161 | acc[cat] += (dur - child) / 1e6 |
| 162 | if stack: |
| 163 | stack[-1][3] += dur if cat else 0.0 |
| 164 | # The trace run's own wall is noisy (the trace costs time), so the median wall less what was traced is |
| 165 | # the load; a trace that adds up to more than the median wall is scaled down to it. |
| 166 | traced = sum(acc.values()) |
| 167 | if traced > wall: |
| 168 | return {k: v / traced * wall for k, v in acc.items()} |
| 169 | acc["load"] += wall - traced |
| 170 | return acc |
| 171 | |
| 172 | |
| 173 | def main(): |
| 174 | d, runs, meta = sys.argv[1], int(sys.argv[2]), json.loads(sys.argv[3]) |
| 175 | rec = dict(meta) |
| 176 | a = engine(d, "a", runs) |
| 177 | t = engine(d, "t", runs) |
| 178 | m = re.search(r"-> (\d+) page\(s\)", read(os.path.join(d, "a1.out"))) |
| 179 | if m: |
| 180 | rec["pages"] = int(m.group(1)) |
| 181 | tp = read(os.path.join(d, "t.pages")).strip() |
| 182 | if tp.isdigit(): |
| 183 | rec["typst_pages"] = int(tp) |
| 184 | if a.get("status") == "ok": |
| 185 | a["stages"] = aus_stages(d, runs, a.get("wall")) |
| 186 | if t.get("status") == "ok": |
| 187 | t["stages"] = typ_stages(d, t.get("wall")) |
| 188 | rec["aus"], rec["typ"] = a, t |
| 189 | if a.get("wall") and t.get("wall"): |
| 190 | rec["ratio"] = round(a["wall"] / t["wall"], 3) |
| 191 | w = read(os.path.join(d, "w.out")) |
| 192 | if w: |
| 193 | m = re.search(r"mode=(\S*) compile_s=(\S*) passes=(\S*) save_to_view_s=(\S*)", w) |
| 194 | rc = read(os.path.join(d, "w.rc")).strip() |
| 195 | if m: |
| 196 | rec["warm"] = {"mode": m.group(1), "compile_s": float(m.group(2) or "nan"), |
| 197 | "passes": int(m.group(3)) if m.group(3).isdigit() else None, |
| 198 | "save_to_view_s": float(m.group(4) or "nan")} |
| 199 | else: |
| 200 | rec["warm"] = {"status": "fail", "rc": int(rc) if rc.lstrip("-").isdigit() else None, |
| 201 | "msg": message(read(os.path.join(d, "w.err")) + "\n" + w)} |
| 202 | print(json.dumps(rec, separators=(",", ":"))) |
| 203 | |
| 204 | |
| 205 | if __name__ == "__main__": |
| 206 | main() |