#!/usr/bin/env python3 """Summary of the `leleka-serve pool-layer` lines of an engine log (offline analysis, stdlib only; never part of serving). pool_layer_report.py ENGINE_LOG [--skip N] [--split 16] [--tokens 1,2,3,4] [--by-experts] The engine writes one line per layer window of the CPU expert pool when it runs with `LELEKA_POOL_TIMING=layers` (`pool_layer_trace.rs`). Per token count T and layer band (layers < SPLIT, layers >= SPLIT) this prints the percentiles of the group wall (the pool's dispatch as the engine saw it) and, for each phase (m5 gate/up, m6 down), the percentiles of: wall publication to the last task done wake the slowest thread's publication-to-first-task (a thread that was not parked in its spin is late) last the last thread's end of work; "tail" = last - median thread's last (a straggler) busy median thread's compute time; task = p50 / p90 / max of single tasks idle threads that took no task, host = tasks the host took itself and the median host stage split of the group (`POOL_TIMING_NAMES`, microseconds). The first SKIP lines are dropped (warm-up). --by-experts prints one line per (T, number of experts in the window) instead of the layer bands: the group wall percentiles and the median single-task time of the gate/up (m5) and down (m6) phase, so two logs that cover different expert counts can be compared like for like. """ import json import sys STAGES = ["pre", "gu_park0", "gu_publish", "gu_drain", "gu_wait_done", "gu_park1", "requant", "dn_park0", "dn_publish", "dn_drain", "dn_wait_done", "dn_park1", "post"] def q(values, p): if not values: return 0.0 v = sorted(values) return v[min(len(v) - 1, int(round((len(v) - 1) * p)))] def pct(values): return "%6.1f %6.1f %6.1f" % (q(values, 0.5), q(values, 0.75), q(values, 0.9)) def load(path, skip): rows = [] for line in open(path, errors="replace"): i = line.find("leleka-serve pool-layer ") if i >= 0: try: rows.append(json.loads(line[i + len("leleka-serve pool-layer "):])) except ValueError: pass return rows[skip:] def phase_rows(group, mode): return [p for g in group for p in g["ph"] if p["m"] == mode] def report_phase(group, mode): ph = phase_rows(group, mode) if not ph: return tail = [p["last"][3] - p["last"][1] for p in ph] print(" m%d wall p50/p75/p90 %s" % (mode, pct([p["wall"] for p in ph]))) print(" wake max p50/p75/p90 %s" % pct([p["wake"][3] for p in ph])) print(" last max p50/p75/p90 %s tail(last-p50 thread) %s" % (pct([p["last"][3] for p in ph]), pct(tail))) print(" busy p50 p50/p75/p90 %s task p50/p90/max us %.1f/%.1f/%.1f" % ( pct([p["busy"][1] for p in ph]), q([p["task"][1] for p in ph], 0.5), q([p["task"][2] for p in ph], 0.5), q([p["task"][3] for p in ph], 0.5))) print(" tasks %d idle threads p50 %d host tasks p50 %d" % ( ph[0]["tasks"], q([p["idle"] for p in ph], 0.5), q([p["host"] for p in ph], 0.5))) def report_band(rows, label): if not rows: return print(" %s n=%d experts p50 %d entries p50 %d" % ( label, len(rows), q([g["experts"] for g in rows], 0.5), q([g["entries"] for g in rows], 0.5))) print(" group wall p50/p75/p90 %s us" % pct([g["wall"] for g in rows])) for mode in (5, 6): report_phase(rows, mode) st = [g["stages"] for g in rows if len(g["stages"]) == len(STAGES)] if st: print(" stage p50 us: " + " ".join("%s %.1f" % (n, q([s[i] for s in st], 0.5)) for i, n in enumerate(STAGES))) def report_by_experts(rows, tokens): print(" T experts windows | group wall p50/p75/p90 us | m5 task p50 | m6 task p50") for t in tokens: for k in sorted({g["experts"] for g in rows if g["tok"] == t}): sel = [g for g in rows if g["tok"] == t and g["experts"] == k] if len(sel) < 20: continue m5 = [p["task"][1] for p in phase_rows(sel, 5)] m6 = [p["task"][1] for p in phase_rows(sel, 6)] print(" %d %7d %8d | %s | %9.1f | %9.1f" % (t, k, len(sel), pct([g["wall"] for g in sel]), q(m5, 0.5), q(m6, 0.5))) def main(): args = sys.argv[1:] if not args: print(__doc__) return 2 skip = int(args[args.index("--skip") + 1]) if "--skip" in args else 0 split = int(args[args.index("--split") + 1]) if "--split" in args else 16 tokens = [int(x) for x in args[args.index("--tokens") + 1].split(",")] if "--tokens" in args else [1, 2, 3, 4] rows = load(args[0], skip) print("%s: %d pool-layer lines (after skipping %d); workers %s" % ( args[0], len(rows), skip, sorted({g["workers"] for g in rows}))) if "--by-experts" in args: report_by_experts(rows, tokens) return 0 for t in tokens: sel = [g for g in rows if g["tok"] == t] print("T=%d groups %d" % (t, len(sel))) report_band([g for g in sel if g["layer"] < split], "layers 0-%d" % (split - 1)) report_band([g for g in sel if g["layer"] >= split], "layers %d-47" % split) return 0 if __name__ == "__main__": sys.exit(main())