Skip to content

Commit d0ec231

Browse files
authored
Merge pull request DeusData#2394 from DeusData/fix/issue-2184-post-extraction-budget
fix(pipeline): spill before the post-extraction phases when they would cross the budget (DeusData#2184)
2 parents 6d19350 + 78a2ec3 commit d0ec231

4 files changed

Lines changed: 246 additions & 1 deletion

File tree

‎src/foundation/mem.c‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,15 @@ size_t cbm_mem_allocator_committed(void) {
594594
}
595595

596596
static _Atomic size_t g_peak_charged;
597+
static _Atomic size_t g_charged_for_tests; /* 0 = live reading */
598+
void cbm_mem_set_charged_for_tests(size_t bytes) {
599+
atomic_store_explicit(&g_charged_for_tests, bytes, memory_order_relaxed);
600+
}
597601
size_t cbm_mem_charged(void) {
602+
size_t pinned = atomic_load_explicit(&g_charged_for_tests, memory_order_relaxed);
603+
if (pinned != 0) {
604+
return pinned;
605+
}
598606
/* The OS number (phys_footprint on macOS, RSS elsewhere) is the charge for
599607
* everything the process maps, and the memory core's own live bytes are its
600608
* floor: macOS was measured under-reporting the OS number after

‎src/foundation/mem.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,14 @@ size_t cbm_mem_budget(void);
9393
* Never call from production code. */
9494
void cbm_mem_set_budget_for_tests(size_t bytes);
9595

96+
/* TEST HOOK: pin cbm_mem_charged() (and so cbm_mem_over_budget()) to `bytes`;
97+
* 0 restores the live reading. The charge is the process footprint, which a
98+
* test cannot steer, so a gate keyed to it ("extraction ended just under the
99+
* spill latch", #2184) is otherwise untestable. A pinned reading does not move
100+
* cbm_mem_peak_charged(). Callers restore 0 before their assertions. Never call
101+
* from production code. */
102+
void cbm_mem_set_charged_for_tests(size_t bytes);
103+
96104
/* Returns true if current RSS exceeds the budget. */
97105
bool cbm_mem_over_budget(void);
98106

‎src/pipeline/pass_parallel.c‎

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -942,6 +942,88 @@ static int pp_spill_sweep(extract_ctx_t *ec, int worker_id) {
942942
return parked;
943943
}
944944

945+
/* ── Post-extraction projection (#2184) ───────────────────────────────
946+
* Spill is entered DURING extraction, when the charge crosses the latch below.
947+
* The phases after it -- registry build, cross-LSP prepare (all_defs, the
948+
* per-language registries, surface rows, the module index) and resolve (edges
949+
* into the graph buffer, cross-LSP appends into the cached results) -- cannot
950+
* spill, and they grow the charge by a sizeable fraction of what extraction
951+
* left. A run that ended extraction just under the latch therefore kept every
952+
* result in memory and went over the budget later: openclaw, 8 workers,
953+
* 4079 MB budget: 3678 MB charged at extraction end, 5829 MB in resolve.
954+
*
955+
* So extraction end projects that growth from the result counts and spills
956+
* before handing over when charged + growth would cross the latch. A pure
957+
* function of counts (O9): the same repo decides the same way on every run.
958+
* Per-unit costs fitted 2026-09-25 (M5 Pro, release build, 8 workers) on the
959+
* charge growth from the parallel_extract mark to the parallel_resolve mark:
960+
*
961+
* corpus files defs calls+usages growth model
962+
* go 21882 735895 5,504,282 1307 MB 1909 MB
963+
* django 4169 75419 591,350 231 MB 215 MB
964+
* kotlin 5247 49669 421,788 129 MB 166 MB
965+
* rust 818 23717 265,957 79 MB 76 MB
966+
* php 2435 14691 137,969 47 MB 58 MB
967+
* openclaw 48203 748829 >= 7,402,832 2151 MB >= 2419 MB
968+
*
969+
* It never under-reads by more than 7% (inside the budget/16 margin) and
970+
* over-reads Go by 46% -- the safe side: an unneeded spill costs disk reads,
971+
* never graph content (spilled and in-memory runs build the same graph). */
972+
enum {
973+
PP_POST_BYTES_PER_DEF = 1280, /* registry entry + LSP def + cross registries */
974+
PP_POST_BYTES_PER_REF = 160, /* per call / usage: resolved edge + appends */
975+
PP_POST_BYTES_PER_FILE = 8192, /* per-file tables: modules, surfaces, imports */
976+
};
977+
978+
static size_t pp_post_extract_growth(const extract_ctx_t *ec, int64_t *defs_out,
979+
int64_t *refs_out) {
980+
int64_t defs = 0;
981+
int64_t refs = 0;
982+
for (int i = 0; i < ec->file_count; i++) {
983+
const CBMFileResult *r = ec->result_cache[i];
984+
if (r) {
985+
defs += r->defs.count;
986+
refs += (int64_t)r->calls.count + r->usages.count;
987+
}
988+
}
989+
*defs_out = defs;
990+
*refs_out = refs;
991+
return (size_t)defs * PP_POST_BYTES_PER_DEF + (size_t)refs * PP_POST_BYTES_PER_REF +
992+
(size_t)ec->file_count * PP_POST_BYTES_PER_FILE;
993+
}
994+
995+
/* Enter spill mode at extraction end when the projected post-extraction growth
996+
* would carry the charge over the latch; the final sweep then parks every
997+
* cached result before registry build. No-op when spill is already on, not
998+
* allowed for this owner, or no budget is set. */
999+
static void pp_spill_if_projected_over(extract_ctx_t *ec) {
1000+
size_t budget = cbm_mem_budget();
1001+
if (budget == 0 || !ec->pctx || !ec->pctx->spill_allowed || !pp_spill_allowed(ec) ||
1002+
pp_spill_active(ec) || atomic_load_explicit(&ec->over_budget_abort, memory_order_relaxed)) {
1003+
return;
1004+
}
1005+
int64_t defs = 0;
1006+
int64_t refs = 0;
1007+
size_t growth = pp_post_extract_growth(ec, &defs, &refs);
1008+
size_t charged = cbm_mem_charged();
1009+
size_t line = budget - budget / PP_SPILL_EARLY_DIV;
1010+
bool spill = growth > line || charged > line - growth;
1011+
const size_t mb = (size_t)1024 * 1024;
1012+
char v[6][CBM_SZ_32];
1013+
snprintf(v[0], sizeof(v[0]), "%zu", charged / mb);
1014+
snprintf(v[1], sizeof(v[1]), "%zu", growth / mb);
1015+
snprintf(v[2], sizeof(v[2]), "%zu", line / mb);
1016+
snprintf(v[3], sizeof(v[3]), "%lld", (long long)defs);
1017+
snprintf(v[4], sizeof(v[4]), "%lld", (long long)refs);
1018+
snprintf(v[5], sizeof(v[5]), "%d", ec->file_count);
1019+
cbm_log_info("mem.post_extract.projection", "charged_mb", v[0], "growth_mb", v[1], "line_mb",
1020+
v[2], "defs", v[3], "refs", v[4], "files", v[5], "decision",
1021+
spill ? "spill" : "keep");
1022+
if (spill) {
1023+
pp_spill_enter(ec, "post_extract_projection");
1024+
}
1025+
}
1026+
9451027
/* Diagnostic (CBM_MEM_PHASES=1): where does the charge go between the
9461028
* near-budget latch and the first over-budget observation? One line per
9471029
* 256 MB step of the charge above its last logged value while spill mode is
@@ -1487,6 +1569,7 @@ int cbm_parallel_extract_ex(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t *file
14871569
cbm_parallel_for_opts_t parallel_opts = {.max_workers = worker_count, .force_pthreads = false};
14881570
cbm_scale_begin(&ec.scale, "parallel_extract", (long)file_count);
14891571
cbm_parallel_for(worker_count, extract_worker, &ec, parallel_opts);
1572+
pp_spill_if_projected_over(&ec);
14901573
if (pp_spill_active(&ec) &&
14911574
!atomic_load_explicit(&ec.over_budget_abort, memory_order_relaxed)) {
14921575
/* Spill mode was entered, so results belong on disk: park every
@@ -1495,7 +1578,9 @@ int cbm_parallel_extract_ex(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t *file
14951578
* run only on an over-budget observation; a run that latched early
14961579
* and then stayed under budget through extraction (kernel, 15 GB,
14971580
* 2026-09-14: 14,949 MB at this point, 44,797 results = 8 GB still
1498-
* cached) reached resolve with no headroom and aborted there. */
1581+
* cached) reached resolve with no headroom and aborted there.
1582+
* The projection above enters spill mode here too when the phases
1583+
* after extraction would carry the charge over the latch (#2184). */
14991584
int parked = pp_spill_sweep(&ec, 0);
15001585
cbm_log_info("mem.spill.final_sweep", "parked", itoa_log(parked), "charged_mb",
15011586
itoa_log((int)(cbm_mem_charged() / ((size_t)1024 * 1024))));

‎tests/test_parallel.c‎

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "discover/discover.h"
1919
#include "foundation/platform.h"
2020
#include "foundation/log.h"
21+
#include "foundation/mem.h"
2122
#include "cbm.h"
2223
#include "result_spill.h"
2324

@@ -616,6 +617,148 @@ TEST(parallel_spill_mode_builds_the_same_graph) {
616617
PASS();
617618
}
618619

620+
/* ── #2184: the post-extraction phases are charged before they run ─── */
621+
622+
/* Order-independent fingerprint of a whole graph: every node (label, QN, file,
623+
* lines, properties) and every edge (type, source QN, target QN, properties)
624+
* hashed and folded with a sum and an xor. Two graphs that differ in any field
625+
* of any node or edge -- not just in a per-type count -- disagree here. */
626+
typedef struct {
627+
const cbm_gbuf_t *gb;
628+
uint64_t sum;
629+
uint64_t xr;
630+
long count;
631+
} graph_fp_t;
632+
633+
static uint64_t fp_mix(uint64_t h, const char *s) {
634+
for (const unsigned char *p = (const unsigned char *)(s ? s : "\x01"); *p; p++) {
635+
h = (h ^ *p) * 1099511628211ULL; /* FNV-1a */
636+
}
637+
return (h ^ 0xffU) * 1099511628211ULL; /* field separator */
638+
}
639+
640+
static uint64_t fp_mix_int(uint64_t h, long long v) {
641+
char buf[32];
642+
snprintf(buf, sizeof(buf), "%lld", v);
643+
return fp_mix(h, buf);
644+
}
645+
646+
static void fp_fold(graph_fp_t *fp, uint64_t h) {
647+
fp->sum += h;
648+
fp->xr ^= h * 0x9E3779B97F4A7C15ULL;
649+
fp->count++;
650+
}
651+
652+
static void fp_visit_node(const cbm_gbuf_node_t *n, void *ud) {
653+
uint64_t h = fp_mix(1469598103934665603ULL, "N");
654+
h = fp_mix(h, n->label);
655+
h = fp_mix(h, n->qualified_name);
656+
h = fp_mix(h, n->file_path);
657+
h = fp_mix_int(h, n->start_line);
658+
h = fp_mix_int(h, n->end_line);
659+
h = fp_mix(h, n->properties_json);
660+
fp_fold(ud, h);
661+
}
662+
663+
static void fp_visit_edge(const cbm_gbuf_edge_t *e, void *ud) {
664+
graph_fp_t *fp = ud;
665+
const cbm_gbuf_node_t *src = cbm_gbuf_find_by_id(fp->gb, e->source_id);
666+
const cbm_gbuf_node_t *dst = cbm_gbuf_find_by_id(fp->gb, e->target_id);
667+
uint64_t h = fp_mix(1469598103934665603ULL, "E");
668+
h = fp_mix(h, e->type);
669+
h = fp_mix(h, src ? src->qualified_name : NULL);
670+
h = fp_mix(h, dst ? dst->qualified_name : NULL);
671+
h = fp_mix(h, e->properties_json);
672+
fp_fold(fp, h);
673+
}
674+
675+
static graph_fp_t graph_fingerprint(const cbm_gbuf_t *gb) {
676+
graph_fp_t fp = {.gb = gb};
677+
cbm_gbuf_foreach_node(gb, fp_visit_node, &fp);
678+
cbm_gbuf_foreach_edge(gb, fp_visit_edge, &fp);
679+
return fp;
680+
}
681+
682+
/* Mutator hook: runs between extraction and registry build, i.e. exactly where
683+
* the post-extraction phases start. Records how many results are still held
684+
* in memory there. */
685+
static void count_cached_results(CBMFileResult **cache, int file_count, void *ud) {
686+
int n = 0;
687+
for (int i = 0; i < file_count; i++) {
688+
n += cache[i] != NULL;
689+
}
690+
*(int *)ud = n;
691+
}
692+
693+
/* #2184: spill was entered only DURING extraction, when the charge crossed
694+
* budget - budget/16. A run that ended extraction just under that line kept
695+
* every result in memory, and the phases that cannot spill (registry build,
696+
* cross-LSP prepare, resolve) then grew the process past the budget -- openclaw
697+
* at 8 workers, 4079 MB budget: 3678 MB at extraction end, 5829 MB in resolve.
698+
* The extraction end now projects that growth from the result counts and
699+
* spills first when budget - budget/16 would be crossed.
700+
*
701+
* The charge is pinned through the test seam (it is the process footprint
702+
* otherwise): run A ends extraction ONE BYTE under the latch -- the extraction
703+
* gate never fires, only the projection can spill; run B has the whole budget
704+
* free -- the projection fits and nothing may spill. Both graphs must be
705+
* identical, field for field, to each other and to the plain in-memory run. */
706+
TEST(parallel_post_extract_projection_spills_before_resolve) {
707+
if (ensure_parity_setup() != 0)
708+
FAIL("setup failed");
709+
cbm_discover_opts_t opts = {.mode = CBM_MODE_FULL};
710+
cbm_file_info_t *files = NULL;
711+
int file_count = 0;
712+
ASSERT_EQ(cbm_discover(g_par_tmpdir, &opts, &files, &file_count), 0);
713+
ASSERT_GT(file_count, 0);
714+
715+
const size_t saved_budget = cbm_mem_budget();
716+
const size_t budget = (size_t)1024 * 1024 * 1024;
717+
const size_t latch = budget - budget / 16; /* spill latches on charged > latch */
718+
cbm_mem_set_budget_for_tests(budget);
719+
g_harness_spill = true;
720+
721+
cbm_mem_set_charged_for_tests(latch);
722+
int cached_near = -1;
723+
cbm_gbuf_t *gb_near =
724+
run_parallel_with_extract_opts_and_mutator("par-test", g_par_tmpdir, files, file_count, 2,
725+
NULL, count_cached_results, &cached_near, false);
726+
int64_t parked_near = g_harness_parked;
727+
728+
cbm_mem_set_charged_for_tests((size_t)1);
729+
int cached_roomy = -1;
730+
cbm_gbuf_t *roomy = run_parallel_with_extract_opts_and_mutator(
731+
"par-test", g_par_tmpdir, files, file_count, 2, NULL, count_cached_results, &cached_roomy,
732+
false);
733+
int64_t parked_roomy = g_harness_parked;
734+
735+
cbm_mem_set_charged_for_tests(0);
736+
g_harness_spill = false;
737+
cbm_mem_set_budget_for_tests(saved_budget);
738+
cbm_discover_free(files, file_count);
739+
ASSERT(gb_near != NULL);
740+
ASSERT(roomy != NULL);
741+
742+
/* A: every result went to disk before registry build. */
743+
ASSERT_EQ((int)parked_near, file_count);
744+
ASSERT_EQ(cached_near, 0);
745+
/* B: the projection fits, so no store was opened and results stay cached. */
746+
ASSERT_EQ((int)parked_roomy, -1);
747+
ASSERT_GT(cached_roomy, 0);
748+
749+
graph_fp_t fp_near = graph_fingerprint(gb_near);
750+
graph_fp_t fp_roomy = graph_fingerprint(roomy);
751+
graph_fp_t fp_mem = graph_fingerprint(g_par_gbuf);
752+
ASSERT_GT(fp_mem.count, 0);
753+
ASSERT_EQ(fp_near.count, fp_mem.count);
754+
ASSERT_EQ(fp_roomy.count, fp_mem.count);
755+
ASSERT_TRUE(fp_near.sum == fp_mem.sum && fp_near.xr == fp_mem.xr);
756+
ASSERT_TRUE(fp_roomy.sum == fp_mem.sum && fp_roomy.xr == fp_mem.xr);
757+
cbm_gbuf_free(gb_near);
758+
cbm_gbuf_free(roomy);
759+
PASS();
760+
}
761+
619762
/* ── Empty file list ──────────────────────────────────────────────── */
620763

621764
TEST(parallel_empty_files) {
@@ -4472,6 +4615,7 @@ SUITE(parallel) {
44724615
RUN_TEST(parallel_semantic_fixture_expected_counts);
44734616
RUN_TEST(parallel_total_edges);
44744617
RUN_TEST(parallel_spill_mode_builds_the_same_graph);
4618+
RUN_TEST(parallel_post_extract_projection_spills_before_resolve);
44754619
RUN_TEST(parallel_empty_files);
44764620
RUN_TEST(parallel_args_json_no_overflow);
44774621

0 commit comments

Comments
 (0)