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.

Add memory back-pressure to parallel extraction

Martin Vogel7b67cf9a220c
diff --git a/src/pipeline/pass_parallel.c b/src/pipeline/pass_parallel.c
index 73a998b4b..daec7a661 100644
--- a/src/pipeline/pass_parallel.c
+++ b/src/pipeline/pass_parallel.c
@@ -22,6 +22,12 @@ enum {
PP_LOG_THRESH = 24,
PP_LOG_INTERVAL = 10,
PP_TIMER_THRESH = 1000,
+ /* Extraction memory back-pressure: when the process is over its RSS budget,
+ * a worker reclaims + naps before pulling another file so peers can finish
+ * and return pages. Bounded spins avoid deadlock when the resident graph
+ * itself is near budget (then proceed with a soft overshoot). */
+ PP_BACKPRESSURE_MAX_SPINS = 40,
+ PP_BACKPRESSURE_NAP_NS = 3000000, /* 3 ms */
};
#define PP_NSEC_PER_SEC 1000000000ULL
#define PP_USEC_PER_MS 1000000ULL
@@ -500,6 +506,24 @@ static void extract_worker(int worker_id, void *ctx_ptr) {
break;
}
+ /* Memory back-pressure (large repos): if the process is over its RSS
+ * budget, reclaim this thread's freed pages and nap so peer workers can
+ * finish their current file and return memory before this worker adds
+ * another parse working set. Caps the concurrent extraction transient
+ * near the budget instead of letting all workers parse their biggest
+ * files at once. Self-disabling when the budget is unset (tests) or RSS
+ * is under budget; bounded spins avoid deadlock when the resident graph
+ * is itself near budget (then proceed with a soft overshoot). */
+ if (cbm_mem_budget() > 0 && cbm_mem_over_budget()) {
+ cbm_mem_collect();
+ for (int bp = 0; bp < PP_BACKPRESSURE_MAX_SPINS && cbm_mem_over_budget() &&
+ !atomic_load_explicit(ec->cancelled, memory_order_relaxed);
+ bp++) {
+ struct timespec nap = {0, PP_BACKPRESSURE_NAP_NS};
+ cbm_nanosleep(&nap, NULL);
+ }
+ }
+
int file_idx = ec->sorted[sort_pos].idx;
const cbm_file_info_t *fi = &ec->files[file_idx];
diff --git a/src/pipeline/pipeline.c b/src/pipeline/pipeline.c
index dea45dd0f..13a78684c 100644
--- a/src/pipeline/pipeline.c
+++ b/src/pipeline/pipeline.c
@@ -117,6 +117,14 @@ static const char *itoa_buf(int val) {
return bufs[i];
}
+/* Log current + peak RSS at a pipeline phase boundary (memory profiling). */
+static void log_phase_mem(const char *phase) {
+ enum { PL_BYTES_PER_MB = 1024 * 1024 };
+ cbm_log_info("mem.phase", "phase", phase, "rss_mb",
+ itoa_buf((int)(cbm_mem_rss() / PL_BYTES_PER_MB)), "peak_mb",
+ itoa_buf((int)(cbm_mem_peak_rss() / PL_BYTES_PER_MB)));
+}
+
/* ── Lifecycle ──────────────────────────────────────────────────── */
cbm_pipeline_t *cbm_pipeline_new(const char *repo_path, const char *db_path,
@@ -584,6 +592,7 @@ static int run_parallel_pipeline(cbm_pipeline_t *p, cbm_pipeline_ctx_t *ctx,
rc = cbm_build_registry_from_cache(ctx, files, file_count, cache);
cbm_log_info("pass.timing", "pass", "registry_build", "elapsed_ms",
itoa_buf((int)elapsed_ms(*t)));
+ log_phase_mem("registry_build");
if (rc != 0 || check_cancel(p)) {
for (int i = 0; i < file_count; i++) {
if (cache[i]) {
@@ -651,11 +660,13 @@ static int run_parallel_pipeline(cbm_pipeline_t *p, cbm_pipeline_ctx_t *ctx,
}
cbm_log_info("pass.timing", "pass", "lsp_cross_prepare", "elapsed_ms",
itoa_buf((int)elapsed_ms(*t)));
+ log_phase_mem("lsp_cross_prepare");
cbm_clock_gettime(CLOCK_MONOTONIC, t);
rc = cbm_parallel_resolve(ctx, files, file_count, cache, &shared_ids, worker_count, all_defs,
def_count, def_modules, module_def_index, &cross_registries);
cbm_log_info("pass.timing", "pass", "parallel_resolve", "elapsed_ms",
itoa_buf((int)elapsed_ms(*t)));
+ log_phase_mem("parallel_resolve");
cbm_pxc_free_module_def_index(module_def_index);
cbm_arena_destroy(&cross_lsp_arena); /* releases all per-lang registries */
free(all_defs);