Skip to content

Commit a2f29b3

Browse files
committed
fix(mem): the 15 GB budget is met by its own metric, end to end
The 15 GB kernel run completed with every phase mark under budget, but the budget metric itself -- the charged footprint the extract gate reads on every file pull -- reached 16.8 GB. Two defects, found with a probe at the gate (one line per 256 MB of charge after the spill latch, with the class table; mem.charge.probe, CBM_MEM_PHASES=1, stays in). Worker graph buffers kept a dense id array over the global id space. The charge climbed gradually from the 14.06 GB latch to 14.96 GB, then jumped about 1 GB between two consecutive gate reads, and the only class that moved in that step was gbuf_index. Worker buffers draw ids from the shared counter, so each of the 18 kept an id -> node array spanning the whole global id space -- 8.5M ids x 8 B, next power of two -- almost entirely empty, and when the global id crossed 2^23 all eighteen doubled in the same instant. Nothing asks a worker buffer by id before the merge (every cbm_gbuf_find_by_id caller runs on the main buffer); the worker constructor now drops the array the way it already drops the five secondary indexes, and the main buffer answers by id after the merge. Spill mode did not park what was already cached. The sweeps ran only on an over-budget observation; a run that latched early (near_budget) and then stayed under budget through extraction -- which is exactly what the first fix produced: 14,949 MB at the extract mark -- carried the 44,797 results cached before the latch (8 GB) into registry build and resolve, which cannot park, and resolve ran at a charged 21.8 GB against the 15 GB budget with nothing to stop it. The earlier runs only fitted because the id-array burst tripped the sweeps by accident. Extraction now ends with a final sweep whenever spill mode is on: results belong on disk before the phases that cannot park inherit them. Kernel, CBM_MEM_BUDGET_MB=15000, before -> after: charged high-water 16,771-16,818 MB -> 14,582 and 14,732 MB in two runs (97-98% of the budget); the final sweep parks ~45k results and takes the charge from 14.6 to 4.3 GB before resolve, which peaks at 11.5 GB committed; wall 234 -> 224-230 s; nodes identical (8,529,729), edges inside the run-to-run band. RSS high-water 18.7 GB is pages already purged and not yet reclaimed: the footprint never exceeds 7.3 GB at a mark. peak_charged_mb (7b77fd4) is what made this measurable: the kernel at the default budget reads charged 22.3 GB against 24.6 GB while ps reports 29 GB RSS, and the 15 GB run read 16.8 GB where every mark was under. Tests: gbuf_worker_buffer_keeps_no_by_id_array (graph_buffer: one node at id 2M in a worker buffer must not grow the index class by even 1 MB where the dense array costs 16 MB; the worker answers by QN, the main buffer by id after the merge) -- RED on the dense array, GREEN without it. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 7b77fd4 commit a2f29b3

3 files changed

Lines changed: 107 additions & 13 deletions

File tree

‎src/graph_buffer/graph_buffer.c‎

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ struct cbm_gbuf {
9898
* strings at kernel scale, plus a snprintf+strdup+hash on every one of
9999
* the ~18 hot find_by_id call sites. */
100100
cbm_gbuf_node_t **by_id;
101+
/* Worker buffers (cbm_gbuf_new_worker) keep no by_id array: their ids
102+
* come from the shared counter, so a dense array would span the whole
103+
* global id space in every worker and double in lockstep. */
104+
bool by_id_off;
101105
int64_t by_id_cap;
102106

103107
/* Secondary node indexes */
@@ -485,22 +489,24 @@ static void cascade_delete_edges(cbm_gbuf_t *gb, CBMHashTable *deleted_set) {
485489
static void register_node_in_indexes(cbm_gbuf_t *gb, cbm_gbuf_node_t *node) {
486490
cbm_ht_set(gb->node_by_qn, node->qualified_name, node);
487491

488-
if (node->id >= gb->by_id_cap) {
489-
int64_t nc = gb->by_id_cap > 0 ? gb->by_id_cap : CBM_SZ_1K;
490-
while (nc <= node->id) {
491-
nc *= 2;
492+
if (!gb->by_id_off) {
493+
if (node->id >= gb->by_id_cap) {
494+
int64_t nc = gb->by_id_cap > 0 ? gb->by_id_cap : CBM_SZ_1K;
495+
while (nc <= node->id) {
496+
nc *= 2;
497+
}
498+
cbm_gbuf_node_t **grown =
499+
cbm_realloc(CBM_MEM_CLASS_GBUF_INDEX, gb->by_id, (size_t)nc * sizeof(*grown));
500+
if (grown) {
501+
memset(grown + gb->by_id_cap, 0, (size_t)(nc - gb->by_id_cap) * sizeof(*grown));
502+
gb->by_id = grown;
503+
gb->by_id_cap = nc;
504+
}
492505
}
493-
cbm_gbuf_node_t **grown =
494-
cbm_realloc(CBM_MEM_CLASS_GBUF_INDEX, gb->by_id, (size_t)nc * sizeof(*grown));
495-
if (grown) {
496-
memset(grown + gb->by_id_cap, 0, (size_t)(nc - gb->by_id_cap) * sizeof(*grown));
497-
gb->by_id = grown;
498-
gb->by_id_cap = nc;
506+
if (node->id >= 0 && node->id < gb->by_id_cap) {
507+
gb->by_id[node->id] = node;
499508
}
500509
}
501-
if (node->id >= 0 && node->id < gb->by_id_cap) {
502-
gb->by_id[node->id] = node;
503-
}
504510

505511
node_ptr_array_t *by_label =
506512
get_or_create_node_array(gb->nodes_by_label, node->label ? node->label : "");
@@ -652,6 +658,16 @@ cbm_gbuf_t *cbm_gbuf_new_worker(const char *project, const char *root_path,
652658
gb->edges_by_source_type = NULL;
653659
gb->edges_by_target_type = NULL;
654660
gb->edges_by_type = NULL;
661+
/* Nor by id: with ids from the shared counter, a dense id -> node array
662+
* in every worker spans the whole global id space (8.5M ids x 8 B x 18
663+
* workers on the kernel) and all of them double in the same instant --
664+
* a 1 GB step between two reads of the memory gate, which is where the
665+
* 15 GB budget was missed (2026-09-14). Nothing asks a worker buffer by
666+
* id before the merge; the main buffer answers after it. */
667+
cbm_free(CBM_MEM_CLASS_GBUF_INDEX, gb->by_id);
668+
gb->by_id = NULL;
669+
gb->by_id_cap = 0;
670+
gb->by_id_off = true;
655671
return gb;
656672
}
657673

‎src/pipeline/pass_parallel.c‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ enum { PP_CSHARP_M_PREFIX_LEN = 2 };
8080
#include "foundation/compat.h"
8181
#include "foundation/compat_fs.h"
8282
#include "foundation/compat_thread.h"
83+
#include "foundation/mem_core.h"
8384
#include "graph_buffer/graph_buffer.h"
8485
#include "service_patterns.h"
8586
#include "foundation/platform.h"
@@ -931,6 +932,36 @@ static int pp_spill_sweep(extract_ctx_t *ec, int worker_id) {
931932
return parked;
932933
}
933934

935+
/* Diagnostic (CBM_MEM_PHASES=1): where does the charge go between the
936+
* near-budget latch and the first over-budget observation? One line per
937+
* 256 MB step of the charge above its last logged value while spill mode is
938+
* on, and one at the first over-budget observation, each with the
939+
* footprint / commit / tracked breakdown and the class table. */
940+
static _Atomic size_t g_probe_last_mb = 0;
941+
static _Atomic int g_probe_over_logged = 0;
942+
static void pp_charge_probe(extract_ctx_t *ec, bool over) {
943+
if (!cbm_mem_phases_enabled() || !pp_spill_active(ec)) {
944+
return;
945+
}
946+
enum { PROBE_STEP_MB = 256, PROBE_MB = 1024 * 1024 };
947+
size_t charged_mb = cbm_mem_charged() / PROBE_MB;
948+
size_t last = atomic_load_explicit(&g_probe_last_mb, memory_order_relaxed);
949+
bool step = charged_mb >= last + PROBE_STEP_MB &&
950+
atomic_compare_exchange_strong_explicit(&g_probe_last_mb, &last, charged_mb,
951+
memory_order_relaxed, memory_order_relaxed);
952+
bool first_over =
953+
over && atomic_exchange_explicit(&g_probe_over_logged, 1, memory_order_relaxed) == 0;
954+
if (!step && !first_over) {
955+
return;
956+
}
957+
cbm_log_info("mem.charge.probe", "event", first_over ? "first_over" : "step", "charged_mb",
958+
itoa_log((int)charged_mb), "footprint_mb",
959+
itoa_log((int)(cbm_mem_footprint() / PROBE_MB)), "commit_mb",
960+
itoa_log((int)(cbm_mem_allocator_committed() / PROBE_MB)), "tracked_mb",
961+
itoa_log((int)(cbm_mem_tracked_live_bytes() / PROBE_MB)));
962+
cbm_mem_class_log(first_over ? "charge.first_over" : "charge.step");
963+
}
964+
934965
static void extract_worker(int worker_id, void *ctx_ptr) {
935966
extract_ctx_t *ec = ctx_ptr;
936967
extract_worker_state_t *ws = &ec->workers[worker_id];
@@ -974,6 +1005,7 @@ static void extract_worker(int worker_id, void *ctx_ptr) {
9741005
* and the previously serving index keeps answering. */
9751006
if (cbm_mem_budget() > 0) {
9761007
bool over = cbm_mem_over_budget();
1008+
pp_charge_probe(ec, over);
9771009
/* Anticipation: the gate sees the crossing per file pull, and the
9781010
* workers' in-flight files carry the charge past the line before
9791011
* the first sweep lands (kernel, 15 GB budget: high-water 15.65
@@ -1401,6 +1433,19 @@ int cbm_parallel_extract_ex(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t *file
14011433
cbm_parallel_for_opts_t parallel_opts = {.max_workers = worker_count, .force_pthreads = false};
14021434
cbm_scale_begin(&ec.scale, "parallel_extract", (long)file_count);
14031435
cbm_parallel_for(worker_count, extract_worker, &ec, parallel_opts);
1436+
if (pp_spill_active(&ec) &&
1437+
!atomic_load_explicit(&ec.over_budget_abort, memory_order_relaxed)) {
1438+
/* Spill mode was entered, so results belong on disk: park every
1439+
* result still cached before the phases that cannot park (registry
1440+
* build, resolve, the semantic pass) inherit them. The sweeps above
1441+
* run only on an over-budget observation; a run that latched early
1442+
* and then stayed under budget through extraction (kernel, 15 GB,
1443+
* 2026-09-14: 14,949 MB at this point, 44,797 results = 8 GB still
1444+
* cached) reached resolve with no headroom and aborted there. */
1445+
int parked = pp_spill_sweep(&ec, 0);
1446+
cbm_log_info("mem.spill.final_sweep", "parked", itoa_log(parked), "charged_mb",
1447+
itoa_log((int)(cbm_mem_charged() / ((size_t)1024 * 1024))));
1448+
}
14041449
if (ctx->spill) {
14051450
int64_t parked = 0;
14061451
int64_t bytes = 0;

‎tests/test_graph_buffer.c‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
*/
77
#include "test_framework.h"
88
#include "graph_buffer/graph_buffer.h"
9+
#include "foundation/mem_core.h"
10+
#include <stdatomic.h>
911
#include "store/store.h"
1012
#include <string.h>
1113

@@ -1120,7 +1122,38 @@ TEST(gbuf_flush_skips_orphan_edges) {
11201122

11211123
/* ── Suite ─────────────────────────────────────────────────────── */
11221124

1125+
/* A worker buffer draws ids from the shared counter, so a dense id -> node
1126+
* array in it spans the whole global id space: 18 workers x (next power of
1127+
* two above the highest id) x 8 B, doubling in lockstep -- a 1 GB step
1128+
* inside one gate interval on the kernel at 8M ids (2026-09-14). A worker
1129+
* buffer is never asked by id before the merge, so it keeps no such array;
1130+
* the main buffer it merges into still answers by id. One node at id 2M
1131+
* would cost a 16 MB array; the index class must not grow by even 1 MB. */
1132+
TEST(gbuf_worker_buffer_keeps_no_by_id_array) {
1133+
_Atomic int64_t ids;
1134+
atomic_init(&ids, (int64_t)1 << 21);
1135+
size_t before = cbm_mem_class_live_bytes(CBM_MEM_CLASS_GBUF_INDEX);
1136+
cbm_gbuf_t *w = cbm_gbuf_new_worker("p", "/r", &ids);
1137+
ASSERT_NOT_NULL(w);
1138+
int64_t id = cbm_gbuf_upsert_node(w, "Function", "f", "p.f", "a.c", 1, 2, "{}");
1139+
ASSERT_TRUE(id >= ((int64_t)1 << 21));
1140+
size_t after = cbm_mem_class_live_bytes(CBM_MEM_CLASS_GBUF_INDEX);
1141+
size_t grown = after > before ? after - before : 0;
1142+
ASSERT_TRUE(grown < ((size_t)1 << 20));
1143+
ASSERT_TRUE(cbm_gbuf_find_by_id(w, id) == NULL);
1144+
ASSERT_NOT_NULL(cbm_gbuf_find_by_qn(w, "p.f"));
1145+
1146+
cbm_gbuf_t *main_gb = cbm_gbuf_new("p", "/r");
1147+
ASSERT_NOT_NULL(main_gb);
1148+
cbm_gbuf_merge(main_gb, w);
1149+
ASSERT_NOT_NULL(cbm_gbuf_find_by_id(main_gb, id));
1150+
cbm_gbuf_free(w);
1151+
cbm_gbuf_free(main_gb);
1152+
PASS();
1153+
}
1154+
11231155
SUITE(graph_buffer) {
1156+
RUN_TEST(gbuf_worker_buffer_keeps_no_by_id_array);
11241157
/* Original tests */
11251158
RUN_TEST(gbuf_create_free);
11261159
RUN_TEST(gbuf_free_null);

0 commit comments

Comments
 (0)