#!/usr/bin/env python3 """Offline fit of the TSV written by the `leak` scenario of the memory smoke (docs/memory-safety.md, section 5.6). leak_fit.py RUN.tsv [--warmup-pct 25] [--tmax SECONDS] [--label NAME] leak_fit.py --compare control.tsv:NAME arm.tsv:NAME ... [--tmax 2700] [--warmup-pct 25] Per series (RSS anon/file/shmem, swap, VRAM, prefix store, glibc heap in use / free / arena): slope in MiB per 1000 requests after the warm-up with a 95% interval (OLS, samples taken as independent: a lower bound of the uncertainty) and the Theil-Sen slope; `--compare` prints the level (median of the last third) of the series that fragmentation moves, for arms run with different allocator settings. Offline analysis only; the engine does not need Python. """ import argparse import math import statistics SERIES = ["rss", "rss_anon", "rss_file", "rss_shmem", "swap", "vram_used", "vram_acct", "prefix_store", "malloc_arena", "malloc_in_use", "malloc_free", "malloc_mmapped", "avail"] MIB = 1048576.0 def load(path, tmax=None): with open(path) as f: header = f.readline().rstrip("\n").split("\t") rows = [dict(zip(header, map(float, line.split("\t")))) for line in f if line.strip()] if tmax is not None: rows = [r for r in rows if r["t_s"] <= tmax] return rows def ols(points): n = len(points) mx = sum(p[0] for p in points) / n my = sum(p[1] for p in points) / n sxx = sum((p[0] - mx) ** 2 for p in points) if sxx == 0: return None slope = sum((p[0] - mx) * (p[1] - my) for p in points) / sxx icpt = my - slope * mx sse = sum((p[1] - icpt - slope * p[0]) ** 2 for p in points) se = math.sqrt(sse / (n - 2) / sxx) return slope, slope - 1.96 * se, slope + 1.96 * se def theil_sen(points): slopes = [(b[1] - a[1]) / (b[0] - a[0]) for i, a in enumerate(points) for b in points[i + 1:] if b[0] != a[0]] return statistics.median(slopes) if slopes else 0.0 def report(rows, warm_pct, label): skip = int(len(rows) * warm_pct / 100) tail = rows[skip:] print(f"== {label}: {len(rows)} samples, {int(rows[-1]['req'])} requests, {rows[-1]['t_s']:.0f} s; fit on {len(tail)} samples after {warm_pct}% warm-up") print(f"{'series':<15}{'slope MiB/1000req':>20}{'95% interval':>28}{'theil-sen':>12}{'first MiB':>12}{'last MiB':>12}") for name in SERIES: if name not in tail[0]: continue pts = [(r["req"], r[name]) for r in tail] if len(pts) < 8: print(f"{name:<15} too few samples") continue fit = ols(pts) if fit is None: continue s, lo, hi = (v * 1000 / MIB for v in fit) zero = " zero inside" if lo <= 0 <= hi else "" print(f"{name:<15}{s:>+20.3f}{'[%+.3f, %+.3f]' % (lo, hi):>28}{theil_sen(pts) * 1000 / MIB:>+12.3f}{pts[0][1] / MIB:>12.0f}{pts[-1][1] / MIB:>12.0f}{zero}") def level(rows, name): last = rows[len(rows) * 2 // 3:] return statistics.median(r[name] for r in last) / MIB def main(): ap = argparse.ArgumentParser() ap.add_argument("files", nargs="*") ap.add_argument("--warmup-pct", type=float, default=25) ap.add_argument("--tmax", type=float, default=None) ap.add_argument("--label", default=None) ap.add_argument("--compare", nargs="*") a = ap.parse_args() if a.compare: arms = [] for spec in a.compare: path, _, name = spec.partition(":") rows = load(path, a.tmax) arms.append((name or path, rows)) report(rows, a.warmup_pct, name or path) print("\nlevel after warm-up (median of the last third of each arm), MiB") print(f"{'arm':<24}" + "".join(f"{n:>16}" for n in ["rss_anon", "malloc_in_use", "malloc_free", "malloc_arena", "rss"])) for name, rows in arms: print(f"{name:<24}" + "".join(f"{level(rows, n):>16.0f}" for n in ["rss_anon", "malloc_in_use", "malloc_free", "malloc_arena", "rss"])) return for path in a.files: report(load(path, a.tmax), a.warmup_pct, a.label or path) if __name__ == "__main__": main()