MCPcopy Create free account
hub / github.com/DeusData/codebase-memory-mcp / extract_worker

Function extract_worker

src/pipeline/pass_parallel.c:715–973  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

713}
714
715static void extract_worker(int worker_id, void *ctx_ptr) {
716 extract_ctx_t *ec = ctx_ptr;
717 extract_worker_state_t *ws = &ec->workers[worker_id];
718
719 /* Lazy gbuf creation */
720 if (!ws->local_gbuf) {
721 ws->local_gbuf = cbm_gbuf_new_shared_ids(ec->project_name, ec->repo_path, ec->shared_ids);
722 }
723
724 /* Pull files from shared atomic counter */
725 while (SKIP_ONE) {
726 int sort_pos =
727 atomic_fetch_add_explicit(&ec->next_file_idx, SKIP_ONE, memory_order_relaxed);
728 if (sort_pos >= ec->file_count) {
729 break;
730 }
731 if (atomic_load_explicit(ec->cancelled, memory_order_relaxed)) {
732 break;
733 }
734
735 /* Memory back-pressure (large repos): if the process is over its RSS
736 * budget, reclaim this thread's freed pages and nap so peer workers can
737 * finish their current file and return memory before this worker adds
738 * another parse working set. Caps the concurrent extraction transient
739 * near the budget instead of letting all workers parse their biggest
740 * files at once. Self-disabling when the budget is unset (tests) or RSS
741 * is under budget; bounded spins avoid deadlock when the resident graph
742 * is itself near budget (then proceed with a soft overshoot).
743 *
744 * Futility latch: when a FULL nap cycle ends still over budget, the
745 * resident floor — not transients — holds the memory; napping again on
746 * the next pull cannot reclaim it and only idles workers (linux kernel:
747 * one full cycle per pull ≈ 390 s at 79% avg CPU). Latch bp_futile and
748 * proceed with the soft overshoot; the over-budget probe below re-arms
749 * the gate as soon as RSS drains under budget. */
750 if (cbm_mem_budget() > 0) {
751 bool over = cbm_mem_over_budget();
752 bool futile = atomic_load_explicit(&ec->bp_futile, memory_order_relaxed) != 0;
753 if (over && !futile) {
754 cbm_mem_collect();
755 atomic_fetch_add_explicit(&g_bp_nap_cycles, SKIP_ONE, memory_order_relaxed);
756 int bp = 0;
757 for (; bp < PP_BACKPRESSURE_MAX_SPINS && cbm_mem_over_budget() &&
758 !atomic_load_explicit(ec->cancelled, memory_order_relaxed);
759 bp++) {
760 struct timespec nap = {0, PP_BACKPRESSURE_NAP_NS};
761 cbm_nanosleep(&nap, NULL);
762 }
763 if (bp == PP_BACKPRESSURE_MAX_SPINS && cbm_mem_over_budget()) {
764 /* Log only the 0→1 transition: all workers race into the
765 * gate before anyone latches, so a plain store would WARN
766 * once per worker (12 lines per latch event). */
767 if (atomic_exchange_explicit(&ec->bp_futile, 1, memory_order_relaxed) == 0) {
768 cbm_log_warn("mem.backpressure.futile", "action", "soft_overshoot");
769 }
770 }
771 } else if (!over && futile) {
772 atomic_store_explicit(&ec->bp_futile, 0, memory_order_relaxed);

Callers

nothing calls this directly

Calls 15

cbm_gbuf_new_shared_idsFunction · 0.85
cbm_mem_budgetFunction · 0.85
cbm_mem_over_budgetFunction · 0.85
cbm_mem_collectFunction · 0.85
cbm_nanosleepFunction · 0.85
cbm_index_is_quarantinedFunction · 0.85
pp_err_addFunction · 0.85
cbm_max_file_bytesFunction · 0.85
extract_now_nsFunction · 0.85
cbm_extract_file_exFunction · 0.85

Tested by

no test coverage detected