Skip to content

Commit 7a23769

Browse files
committed
feat(diagnostics): allocation-site profiler for the #581 analysis harness
The census could say which POOL held memory but never which allocation put it there, so each hypothesis needed a code change plus an eight-minute soak to test and the investigation could only ask one yes/no question at a time. Records allocations at or above a threshold (default 32 KiB, since arena fragmentation is driven by large transient runs) against their captured stack, into fixed tables whose exhaustion is counted and reported rather than silently dropped. Emitted alongside every census sample, so pool totals and attribution always describe the same instant, plus an explicit unattributed remainder so incomplete coverage announces itself. scripts/memlab-report.py ranks sites by retained-byte growth using a linear fit and can diff two platforms' profiles against each other. Signed-off-by: Martin Vogel <[email protected]>
1 parent 5929f6a commit 7a23769

6 files changed

Lines changed: 525 additions & 4 deletions

File tree

‎Makefile.cbm‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ FOUNDATION_SRCS = \
140140
src/foundation/compat_fs.c \
141141
src/foundation/compat_regex.c \
142142
src/foundation/mem.c \
143+
src/foundation/mem_profile.c \
143144
src/foundation/diagnostics.c \
144145
src/foundation/profile.c \
145146
src/foundation/dump_verify.c \

‎scripts/memlab-report.py‎

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
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())

‎src/foundation/mem.c‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "platform.h"
1111
#include "log.h"
1212
#include "compat_thread.h"
13+
#include "mem_profile.h"
1314

1415
#include "foundation/constants.h"
1516

@@ -858,7 +859,24 @@ void cbm_mem_census_log(const char *tag) {
858859
(void)snprintf(dc_err, sizeof(dc_err), "n/a");
859860
(void)snprintf(dc_mb, sizeof(dc_mb), "n/a");
860861
#endif
861-
cbm_log_info("mem.census", "at", tag ? tag : "?", "priv_kb", priv_kb, "crt_kb", crt_kb,
862+
/* Attribution alongside the pool totals: the census says which pool holds
863+
* the memory, the profile says which call sites put it there. Emitting
864+
* both from one place keeps the two views on the same sample. */
865+
cbm_mem_profile_totals_t profile;
866+
cbm_mem_profile_totals(&profile);
867+
char prof_live_kb[CBM_SZ_32];
868+
char prof_sites[CBM_SZ_32];
869+
char prof_lost[CBM_SZ_32];
870+
(void)snprintf(prof_live_kb, sizeof(prof_live_kb), "%zu", profile.live_bytes / CBM_SZ_1K);
871+
(void)snprintf(prof_sites, sizeof(prof_sites), "%zu", profile.sites);
872+
(void)snprintf(prof_lost, sizeof(prof_lost), "%zu",
873+
profile.site_table_full + profile.pointer_table_full);
874+
char profile_path[CBM_SZ_1K];
875+
if (cbm_safe_getenv("CBM_MEM_PROFILE_OUT", profile_path, sizeof(profile_path), NULL) != NULL) {
876+
(void)cbm_mem_profile_dump(profile_path, tag ? tag : "?");
877+
}
878+
cbm_log_info("mem.census", "at", tag ? tag : "?",
879+
"prof_live_kb", prof_live_kb, "prof_sites", prof_sites, "prof_lost", prof_lost, "priv_kb", priv_kb, "crt_kb", crt_kb,
862880
"crt_busy_kb", busy_kb, "mapped_kb", mapped_kb, "rss_kb", rss_kb, "biggest_kb",
863881
biggest_kb, "regions", regions, "big_regions", big_regions, "heaps", heaps,
864882
"decommit_calls", dc_calls, "decommit_fails", dc_fail, "decommit_err", dc_err,

‎src/foundation/mem_override_win.c‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
* mimalloc is always safe.
4343
*/
4444
#include "foundation/mem.h"
45+
#include "foundation/mem_profile.h"
4546

4647
#include <mimalloc.h>
4748
#include <stddef.h>
@@ -69,11 +70,15 @@ static inline bool mem_override_is_ours(const void *block) {
6970
}
7071

7172
void *__wrap_malloc(size_t size) {
72-
return mi_malloc(size);
73+
void *block = mi_malloc(size);
74+
cbm_mem_profile_alloc(block, size);
75+
return block;
7376
}
7477

7578
void *__wrap_calloc(size_t count, size_t size) {
76-
return mi_calloc(count, size);
79+
void *block = mi_calloc(count, size);
80+
cbm_mem_profile_alloc(block, count * size);
81+
return block;
7782
}
7883

7984
char *__wrap_strdup(const char *text) {
@@ -95,6 +100,7 @@ void __wrap_free(void *block) {
95100
return;
96101
}
97102
if (mem_override_is_ours(block)) {
103+
cbm_mem_profile_free(block);
98104
mi_free(block);
99105
return;
100106
}
@@ -120,7 +126,10 @@ void *__wrap_realloc(void *block, size_t size) {
120126
return mi_malloc(size);
121127
}
122128
if (mem_override_is_ours(block)) {
123-
return mi_realloc(block, size);
129+
cbm_mem_profile_free(block);
130+
void *grown = mi_realloc(block, size);
131+
cbm_mem_profile_alloc(grown, size);
132+
return grown;
124133
}
125134
/* A CRT block cannot be handed to mi_realloc, and the CRT's realloc must
126135
* not be asked to grow into mimalloc's space either. Copy across the

0 commit comments

Comments
 (0)