|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Analyse #581 memory-lab output: attribute growth to allocation sites. |
| 3 | +
|
| 4 | +Reads the JSONL the profiler appends (CBM_MEM_PROFILE_OUT) and the mem.census |
| 5 | +lines from a daemon log, and prints: |
| 6 | +
|
| 7 | + * retention per request, as a linear fit rather than first-vs-last, so noise |
| 8 | + does not masquerade as a trend |
| 9 | + * the call sites whose live bytes grow across the run, ranked by growth |
| 10 | + * an explicit unattributed remainder: process growth the profiler could not |
| 11 | + account for. That number is the honesty check — a report that attributed |
| 12 | + everything while tracking only >=32 KiB allocations would be lying. |
| 13 | +
|
| 14 | +Usage: |
| 15 | + memlab-report.py <profile.jsonl> [--census <daemon.log>] [--top N] |
| 16 | + memlab-report.py <a.jsonl> --diff <b.jsonl> # platform comparison |
| 17 | +""" |
| 18 | +import argparse |
| 19 | +import json |
| 20 | +import re |
| 21 | +import sys |
| 22 | +from collections import defaultdict |
| 23 | + |
| 24 | + |
| 25 | +def load_profile(path): |
| 26 | + """Return (summaries, site_series). Records are appended over time, so the |
| 27 | + Nth occurrence of a site is its Nth sample.""" |
| 28 | + summaries, series = [], defaultdict(list) |
| 29 | + with open(path, "r", errors="replace") as handle: |
| 30 | + for line in handle: |
| 31 | + line = line.strip() |
| 32 | + if not line.startswith("{"): |
| 33 | + continue |
| 34 | + try: |
| 35 | + record = json.loads(line) |
| 36 | + except json.JSONDecodeError: |
| 37 | + continue |
| 38 | + if "site" in record: |
| 39 | + series[record["site"]].append(record) |
| 40 | + else: |
| 41 | + summaries.append(record) |
| 42 | + return summaries, series |
| 43 | + |
| 44 | + |
| 45 | +def linear_slope(values): |
| 46 | + """Least-squares slope over sample index. Resistant to the single-outlier |
| 47 | + reading that a first-vs-last delta would treat as the whole trend.""" |
| 48 | + n = len(values) |
| 49 | + if n < 2: |
| 50 | + return 0.0 |
| 51 | + mean_x = (n - 1) / 2.0 |
| 52 | + mean_y = sum(values) / n |
| 53 | + num = sum((i - mean_x) * (v - mean_y) for i, v in enumerate(values)) |
| 54 | + den = sum((i - mean_x) ** 2 for i in range(n)) |
| 55 | + return num / den if den else 0.0 |
| 56 | + |
| 57 | + |
| 58 | +def parse_census(path): |
| 59 | + """Pull the pool totals out of mem.census log lines.""" |
| 60 | + fields = ("priv_kb", "rss_kb", "crt_kb", "biggest_kb", "prof_live_kb") |
| 61 | + rows = [] |
| 62 | + pattern = re.compile(r"(\w+)=(\S+)") |
| 63 | + with open(path, "r", errors="replace") as handle: |
| 64 | + for line in handle: |
| 65 | + if "mem.census" not in line: |
| 66 | + continue |
| 67 | + kv = dict(pattern.findall(line)) |
| 68 | + if not any(f in kv for f in fields): |
| 69 | + continue |
| 70 | + rows.append({f: int(kv[f]) for f in fields if f in kv and kv[f].isdigit()}) |
| 71 | + return rows |
| 72 | + |
| 73 | + |
| 74 | +def report(path, census_path, top): |
| 75 | + summaries, series = load_profile(path) |
| 76 | + if not summaries and not series: |
| 77 | + print(f"{path}: no profiler records — was CBM_MEM_PROFILE=1 set?") |
| 78 | + return |
| 79 | + last = summaries[-1] if summaries else {} |
| 80 | + print(f"=== {path} ===") |
| 81 | + if last: |
| 82 | + print(f"threshold : {last.get('threshold', 0)} bytes " |
| 83 | + f"(allocations below this are invisible by design)") |
| 84 | + print(f"sites seen : {last.get('sites', 0)}") |
| 85 | + print(f"attributed live: {last.get('live_bytes', 0)/1048576:.1f} MB " |
| 86 | + f"in {last.get('live_blocks', 0)} blocks") |
| 87 | + lost = last.get("site_table_full", 0) + last.get("pointer_table_full", 0) |
| 88 | + if lost: |
| 89 | + print(f"RECORDS LOST : {lost} — tables exhausted, treat totals as a floor") |
| 90 | + |
| 91 | + growth = [] |
| 92 | + for site, samples in series.items(): |
| 93 | + live = [s["live_bytes"] for s in samples] |
| 94 | + slope = linear_slope(live) |
| 95 | + if slope <= 0 and live[-1] <= live[0]: |
| 96 | + continue |
| 97 | + growth.append((slope, live[-1] - live[0], live[-1], samples[-1])) |
| 98 | + growth.sort(key=lambda row: row[0], reverse=True) |
| 99 | + |
| 100 | + print(f"\n--- sites whose retained bytes GROW (top {top}) ---") |
| 101 | + if not growth: |
| 102 | + print("none — no tracked site retains more over time.") |
| 103 | + for slope, delta, live, sample in growth[:top]: |
| 104 | + frames = " <- ".join(sample.get("frames", [])[:4]) |
| 105 | + print(f" +{slope/1024:8.1f} KB/sample total +{delta/1048576:6.2f} MB " |
| 106 | + f"live {live/1048576:6.2f} MB") |
| 107 | + print(f" {frames}") |
| 108 | + |
| 109 | + if census_path: |
| 110 | + rows = parse_census(census_path) |
| 111 | + if rows: |
| 112 | + priv = [r["priv_kb"] for r in rows if "priv_kb" in r] |
| 113 | + prof = [r["prof_live_kb"] for r in rows if "prof_live_kb" in r] |
| 114 | + print(f"\n--- pools vs attribution ({len(rows)} samples) ---") |
| 115 | + if priv: |
| 116 | + print(f" private committed : {priv[0]/1024:.1f} -> {priv[-1]/1024:.1f} MB " |
| 117 | + f"({linear_slope(priv):+.1f} KB/sample)") |
| 118 | + if prof and priv: |
| 119 | + unattributed = (priv[-1] - priv[0]) - (prof[-1] - prof[0]) |
| 120 | + print(f" attributed growth : {(prof[-1]-prof[0])/1024:+.1f} MB") |
| 121 | + print(f" UNATTRIBUTED : {unattributed/1024:+.1f} MB " |
| 122 | + "<- below-threshold allocations, allocator metadata, or fragmentation") |
| 123 | + |
| 124 | + |
| 125 | +def diff(path_a, path_b, top): |
| 126 | + """Same sites, two platforms: what does one retain that the other does not?""" |
| 127 | + _, series_a = load_profile(path_a) |
| 128 | + _, series_b = load_profile(path_b) |
| 129 | + rows = [] |
| 130 | + for site, samples in series_a.items(): |
| 131 | + a_live = samples[-1]["live_bytes"] |
| 132 | + b_live = series_b[site][-1]["live_bytes"] if site in series_b else 0 |
| 133 | + if a_live - b_live > 0: |
| 134 | + rows.append((a_live - b_live, a_live, b_live, samples[-1])) |
| 135 | + rows.sort(key=lambda row: row[0], reverse=True) |
| 136 | + print(f"=== {path_a} minus {path_b} (top {top}) ===") |
| 137 | + for delta, a_live, b_live, sample in rows[:top]: |
| 138 | + print(f" +{delta/1048576:7.2f} MB A={a_live/1048576:.2f} MB B={b_live/1048576:.2f} MB") |
| 139 | + print(f" {' <- '.join(sample.get('frames', [])[:4])}") |
| 140 | + |
| 141 | + |
| 142 | +def main(): |
| 143 | + parser = argparse.ArgumentParser() |
| 144 | + parser.add_argument("profile") |
| 145 | + parser.add_argument("--census") |
| 146 | + parser.add_argument("--diff") |
| 147 | + parser.add_argument("--top", type=int, default=12) |
| 148 | + args = parser.parse_args() |
| 149 | + if args.diff: |
| 150 | + diff(args.profile, args.diff, args.top) |
| 151 | + else: |
| 152 | + report(args.profile, args.census, args.top) |
| 153 | + return 0 |
| 154 | + |
| 155 | + |
| 156 | +if __name__ == "__main__": |
| 157 | + sys.exit(main()) |
0 commit comments