Skip to content

Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.

diag: attribute committed-memory growth to a code path, and name #581's

Martin Vogelf292cd20498b
diff --git a/src/foundation/diagnostics.c b/src/foundation/diagnostics.c
index 337531442..e88d1f90c 100644
--- a/src/foundation/diagnostics.c
+++ b/src/foundation/diagnostics.c
@@ -467,7 +467,14 @@ static void write_diagnostics(void) {
bucket_length += written;
}
- char snapshot[CBM_SZ_2K];
+ /* Phase attribution: which bracketed code path the committed bytes stayed
+ * in. Empty unless CBM_MEM_PHASES=1. */
+ char phases[CBM_SZ_1K];
+ if (cbm_mem_phase_report_json(phases, sizeof(phases)) <= 0) {
+ phases[0] = '\0';
+ }
+
+ char snapshot[CBM_SZ_4K];
int length = snprintf(snapshot, sizeof(snapshot),
"{\n"
" \"uptime_s\": %ld,\n"
@@ -487,11 +494,12 @@ static void write_diagnostics(void) {
" \"mem_area_committed_bytes\": %zu,\n"
" \"mem_residual_bytes\": %zu,\n"
" \"mem_buckets\": [%s],\n"
+ " \"mem_phases\": [%s],\n"
" \"pid\": %d\n"
"}\n",
uptime, current_rss, peak_rss, current_commit, peak_commit, page_faults,
fds, qcount, qerrors, qtime, qavg, qmax, map.live_bytes, map.live_blocks,
- map.area_committed_bytes, residual, buckets, (int)getpid());
+ map.area_committed_bytes, residual, buckets, phases, (int)getpid());
if (length <= 0 || (size_t)length >= sizeof(snapshot) ||
!diag_write_file(DIAG_SNAPSHOT_TMP_NAME, snapshot, (size_t)length, false) ||
!diag_native_rename(DIAG_SNAPSHOT_TMP_NAME, DIAG_SNAPSHOT_NAME)) {
diff --git a/src/foundation/mem.c b/src/foundation/mem.c
index 6828e1bdb..617a2fe21 100644
--- a/src/foundation/mem.c
+++ b/src/foundation/mem.c
@@ -9,6 +9,7 @@
#include "mem.h"
#include "platform.h"
#include "log.h"
+#include "compat_thread.h"
#include "foundation/constants.h"
@@ -416,3 +417,131 @@ bool cbm_mem_map_collect(cbm_mem_map_t *out) {
(void)mi_theap_visit_blocks(mi_theap_get_default(), false, mem_map_visit_area, out);
return true;
}
+
+/* ── Phase map (see mem.h for how to read it, and its limits) ──────── */
+
+enum { MEM_PHASE_SLOTS = 24 };
+
+typedef struct {
+ const char *label; /* static string, compared by pointer then by text */
+ int64_t bytes;
+ int64_t hits;
+} mem_phase_slot_t;
+
+static mem_phase_slot_t g_mem_phases[MEM_PHASE_SLOTS];
+static int g_mem_phase_count;
+static const char *g_mem_phase_open;
+static size_t g_mem_phase_baseline;
+static cbm_mutex_t g_mem_phase_mutex;
+static atomic_int g_mem_phase_enabled = ATOMIC_VAR_INIT(-1); /* -1 = undecided */
+
+static size_t mem_phase_committed(void) {
+ size_t elapsed_ms = 0;
+ size_t user_ms = 0;
+ size_t sys_ms = 0;
+ size_t rss = 0;
+ size_t peak_rss = 0;
+ size_t commit = 0;
+ size_t peak_commit = 0;
+ size_t faults = 0;
+ mi_process_info(&elapsed_ms, &user_ms, &sys_ms, &rss, &peak_rss, &commit, &peak_commit,
+ &faults);
+ return commit;
+}
+
+static bool mem_phase_enabled(void) {
+ int state = atomic_load_explicit(&g_mem_phase_enabled, memory_order_acquire);
+ if (state >= 0) {
+ return state == 1;
+ }
+ char buf[CBM_SZ_16];
+ bool on = cbm_safe_getenv("CBM_MEM_PHASES", buf, sizeof(buf), NULL) != NULL && buf[0] == '1';
+ if (on) {
+ cbm_mutex_init(&g_mem_phase_mutex);
+ }
+ atomic_store_explicit(&g_mem_phase_enabled, on ? 1 : 0, memory_order_release);
+ return on;
+}
+
+void cbm_mem_phase_mark(const char *label) {
+ if (!mem_phase_enabled()) {
+ return;
+ }
+ size_t now = mem_phase_committed();
+ cbm_mutex_lock(&g_mem_phase_mutex);
+ if (g_mem_phase_open) {
+ int slot = -1;
+ for (int i = 0; i < g_mem_phase_count; i++) {
+ if (g_mem_phases[i].label == g_mem_phase_open ||
+ strcmp(g_mem_phases[i].label, g_mem_phase_open) == 0) {
+ slot = i;
+ break;
+ }
+ }
+ if (slot < 0 && g_mem_phase_count < MEM_PHASE_SLOTS) {
+ slot = g_mem_phase_count++;
+ g_mem_phases[slot].label = g_mem_phase_open;
+ g_mem_phases[slot].bytes = 0;
+ g_mem_phases[slot].hits = 0;
+ }
+ if (slot >= 0) {
+ g_mem_phases[slot].bytes += (int64_t)now - (int64_t)g_mem_phase_baseline;
+ g_mem_phases[slot].hits++;
+ }
+ }
+ g_mem_phase_open = label;
+ g_mem_phase_baseline = now;
+ cbm_mutex_unlock(&g_mem_phase_mutex);
+}
+
+void cbm_mem_phase_reset(void) {
+ if (!mem_phase_enabled()) {
+ return;
+ }
+ cbm_mutex_lock(&g_mem_phase_mutex);
+ memset(g_mem_phases, 0, sizeof(g_mem_phases));
+ g_mem_phase_count = 0;
+ g_mem_phase_open = NULL;
+ g_mem_phase_baseline = mem_phase_committed();
+ cbm_mutex_unlock(&g_mem_phase_mutex);
+}
+
+int cbm_mem_phase_report_json(char *out, size_t size) {
+ if (!out || size == 0) {
+ return 0;
+ }
+ out[0] = '\0';
+ if (!mem_phase_enabled()) {
+ return 0;
+ }
+ mem_phase_slot_t copy[MEM_PHASE_SLOTS];
+ int count = 0;
+ cbm_mutex_lock(&g_mem_phase_mutex);
+ count = g_mem_phase_count;
+ memcpy(copy, g_mem_phases, sizeof(copy));
+ cbm_mutex_unlock(&g_mem_phase_mutex);
+
+ /* Biggest retainer first — insertion sort, count <= MEM_PHASE_SLOTS. */
+ for (int i = 1; i < count; i++) {
+ mem_phase_slot_t key = copy[i];
+ int j = i - 1;
+ while (j >= 0 && copy[j].bytes < key.bytes) {
+ copy[j + 1] = copy[j];
+ j--;
+ }
+ copy[j + 1] = key;
+ }
+
+ int length = 0;
+ for (int i = 0; i < count && length >= 0 && (size_t)length < size; i++) {
+ int written = snprintf(out + length, size - (size_t)length,
+ "%s{\"label\": \"%s\", \"bytes\": %lld, \"hits\": %lld}",
+ i == 0 ? "" : ", ", copy[i].label ? copy[i].label : "?",
+ (long long)copy[i].bytes, (long long)copy[i].hits);
+ if (written < 0) {
+ break;
+ }
+ length += written;
+ }
+ return length > 0 && (size_t)length < size ? length : 0;
+}
diff --git a/src/foundation/mem.h b/src/foundation/mem.h
index f0d73f801..43f52237f 100644
--- a/src/foundation/mem.h
+++ b/src/foundation/mem.h
@@ -125,4 +125,31 @@ size_t cbm_mem_map_bucket_limit(int bucket);
* that the walk contributed nothing rather than reading 0 as "no leak". */
bool cbm_mem_map_collect(cbm_mem_map_t *out);
+/* ── Phase map: WHICH code path does the memory stay in? ─────────────
+ *
+ * The allocator walk answers "how much and what size"; it cannot answer "who".
+ * When the growth does not route through the allocator at all — the Windows
+ * case in #581 — attribution has to come from the one metric that does see it:
+ * OS committed bytes. Marks bracket a critical path, and each mark attributes
+ * the committed-bytes delta since the previous mark to the previous label. Over
+ * many requests the label whose total climbs monotonically is where memory
+ * stays.
+ *
+ * Deliberately coarse and honest about it:
+ * - OS committed is process-wide, so a concurrently-busy thread lands noise
+ * in whichever label is open. Signal comes from ACCUMULATION over many
+ * passes, never from a single delta.
+ * - Marks must bracket the WHOLE path with no unlabelled gaps, or the leak
+ * hides in the gap. Label the tail explicitly.
+ * - Off unless CBM_MEM_PHASES=1, so the hot path pays one atomic load.
+ */
+void cbm_mem_phase_mark(const char *label);
+
+/* Drop all accumulated phase totals (call once at the start of a measurement). */
+void cbm_mem_phase_reset(void);
+
+/* Write the phase table as a JSON array of {label, bytes, hits}, biggest total
+ * first. Returns bytes written (0 when disabled or empty). */
+int cbm_mem_phase_report_json(char *out, size_t size);
+
#endif /* CBM_MEM_H */
diff --git a/src/mcp/mcp.c b/src/mcp/mcp.c
index fb5a750ec..36afeb6f8 100644
--- a/src/mcp/mcp.c
+++ b/src/mcp/mcp.c
@@ -3365,8 +3365,14 @@ static void emit_semantic_results_toon(cbm_sb_t *sb, const cbm_vector_result_t *
}
static char *handle_search_graph(cbm_mcp_server_t *srv, const char *args) {
+ /* Inner phase split: every tool leaks the same ~4 MB per request, so the
+ * retainer is in what the handlers share -- store resolution or the query
+ * itself. These marks separate the two. */
+ cbm_mem_phase_mark("handler.args");
char *project = get_project_arg(args);
+ cbm_mem_phase_mark("handler.resolve_store");
cbm_store_t *store = resolve_store(srv, project);
+ cbm_mem_phase_mark("handler.body");
REQUIRE_STORE(store, project);
char *not_indexed = verify_project_indexed(store, project);
@@ -10416,16 +10422,26 @@ static void release_request_store(cbm_mcp_server_t *srv) {
}
char *cbm_mcp_handle_tool(cbm_mcp_server_t *srv, const char *tool_name, const char *args_json) {
+ /* Phase marks bracket the WHOLE request with no unlabelled gap, so growth
+ * cannot hide between them (CBM_MEM_PHASES=1; see foundation/mem.h). The
+ * "idle" label owns everything outside a request, which is what makes a
+ * request-path retainer distinguishable from background growth. */
+ cbm_mem_phase_mark("request.scope_begin");
bool request_scope = !srv || cbm_mcp_server_request_scope_begin(srv);
if (!request_scope) {
release_request_store(srv);
+ cbm_mem_phase_mark("idle");
return cbm_mcp_text_result("request cancellation scope unavailable", true);
}
+ cbm_mem_phase_mark("request.dispatch_tool");
char *result = dispatch_tool(srv, tool_name, args_json);
+ cbm_mem_phase_mark("request.scope_end");
if (srv) {
cbm_mcp_server_request_scope_end(srv);
}
+ cbm_mem_phase_mark("request.release_store");
release_request_store(srv);
+ cbm_mem_phase_mark("idle");
return result;
}