From f51d2d2e8b01a657df682e0ab9930170ecb866ea Mon Sep 17 00:00:00 2001 From: Pavlo Ratushnyi Date: Fri, 23 Oct 2026 19:39:39 +0200 Subject: [PATCH] docs+scripts(memory): leak soak jobs and offline slope fit (queued) --- docs/memory-safety.md | 13 +++++ scripts/memory/leak_fit.py | 98 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+) create mode 100644 scripts/memory/leak_fit.py diff --git a/docs/memory-safety.md b/docs/memory-safety.md index 4247f3c8..add9e0e4 100644 --- a/docs/memory-safety.md +++ b/docs/memory-safety.md @@ -233,6 +233,19 @@ The server profile: `auto`, 4096/2048/1024 MiB, lock `try`, heal on. The laptop from the page cache and they were read back once; that is the mapped bank, not the memfd). `memfd_swapped_mib` = 0 on every request, `pswpin/out` 0 (one page of swap-in in B), `MemAvailable` 62.0-62.3 GiB throughout. Same conclusion as job 910: with the host not short of memory a page-cache reader does not move the memfd; the conditions that do are in 5.3. +### 5.6 Does the daemon RSS leak? The long soak (queued: jobs 919, 920, 921, 922; NOT YET RUN) + +What exists: the `leak` scenario of the memory smoke (time-bounded mixed workload: shared growing sessions, unshared 8k-30k sessions, 100k/180k prompts every 120 requests, 8% cancels, 4% over-long refusals, prefix cache `auto` under constant churn), +sampled every 30 s: `VmRSS` split into `RssAnon` / `RssFile` / `RssShmem`, `VmSwap`, `MemAvailable`, `Cached`, `Shmem`, active/inactive file, VRAM used and accounted, the prefix store, `mallinfo2` (arena, in use, free, mmapped), threads, fds; a TSV per run +(`~/work/gpu-runs/mem-logs/*.tsv`, outside Git); at the end it prints, per series, the slope in MiB per 1000 requests after a 25% warm-up with its 95% interval and the Theil-Sen slope (`MEM-LEAK-FIT` lines); +`scripts/memory/leak_fit.py RUN.tsv` re-fits any run offline and `--compare` lines up arms. Jobs: `919` 7-minute check of the scenario, `920` the 3 h 03 min run (default allocator, `malloc_trim` after every eviction), `921` 45 min with `MALLOC_ARENA_MAX=2`, +`922` 45 min with `LELEKA_MEMORY_NO_TRIM=1` (what the trim buys); compare 921 and 922 with the first 45 min of 920 (`leak_fit.py --compare 920.tsv:default 921.tsv:arena2 922.tsv:notrim --tmax 2700`). File-mapped experts, so `MemAvailable` stays ~58 GiB (the 15 GiB rule); +an anonymous or shmem leak shows the same as with the resident bank. + +They were submitted at 14:20 and sit behind twelve other agents' jobs (holder FIFO; 9b1 running at 17:40, the 3 h run could start many hours later). Nothing in this section is measured. **What the evidence says today** is only the CRM session of section 8: +RSS growth = prefix-store growth (6,647 vs 6,748 MiB), then a plateau (+23 MiB in the last 200 s, 8 requests), shmem constant; that is consistent with no leak and does not prove it. The bisect (prefix cache off, CPU pool lookahead off, seed off, K/V imports) is +prepared as environment switches of the smoke (`LELEKA_MEM_PREFIX_MIB=0`, `LELEKA_ORIGINAL_SERVE_SEED=off`) and has to wait for a slope to bisect. + ### 5.3 Swap A/B under real pressure (WSL2 laptop, cgroup v1 memory controller, 9 GiB limit; sealed memfd 6 GiB, anonymous hog 5 GiB, a reader streaming a 12 GiB file; kernel 5.15) | arm | memfd swapped out | notes | diff --git a/scripts/memory/leak_fit.py b/scripts/memory/leak_fit.py new file mode 100644 index 00000000..c81c13c4 --- /dev/null +++ b/scripts/memory/leak_fit.py @@ -0,0 +1,98 @@ +#!/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()