| 1550 | const char *repo_path); |
| 1551 | |
| 1552 | int cbm_parallel_extract_ex(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t *files, int file_count, |
| 1553 | CBMFileResult **result_cache, _Atomic int64_t *shared_ids, |
| 1554 | int worker_count, const cbm_parallel_extract_opts_t *opts) { |
| 1555 | cbm_parallel_extract_opts_t resolved_opts = cbm_parallel_extract_resolve_opts(opts); |
| 1556 | |
| 1557 | if (file_count == 0) { |
| 1558 | return 0; |
| 1559 | } |
| 1560 | |
| 1561 | cbm_log_info("parallel.extract.start", "files", itoa_log(file_count), "workers", |
| 1562 | itoa_log(worker_count)); |
| 1563 | { |
| 1564 | size_t mb = (size_t)CBM_SZ_1K * CBM_SZ_1K; |
| 1565 | cbm_log_info("parallel.extract.retention", "retain_sources", |
| 1566 | resolved_opts.retain_sources ? "true" : "false", "total_mb", |
| 1567 | itoa_log((int)(resolved_opts.retain_total_budget_bytes / mb)), "per_file_mb", |
| 1568 | itoa_log((int)(resolved_opts.retain_per_file_max_bytes / mb))); |
| 1569 | } |
| 1570 | |
| 1571 | /* Log per-worker memory budget */ |
| 1572 | if (cbm_mem_budget() > 0) { |
| 1573 | size_t worker_budget = cbm_mem_worker_budget(worker_count); |
| 1574 | cbm_log_info("parallel.mem.budget", "total_mb", |
| 1575 | itoa_log((int)(cbm_mem_budget() / ((size_t)CBM_SZ_1K * CBM_SZ_1K))), |
| 1576 | "per_worker_mb", |
| 1577 | itoa_log((int)(worker_budget / ((size_t)CBM_SZ_1K * CBM_SZ_1K)))); |
| 1578 | } |
| 1579 | |
| 1580 | /* Sub-phase: Ensure extraction library is initialized */ |
| 1581 | CBM_PROF_START(t_init); |
| 1582 | cbm_init(); |
| 1583 | |
| 1584 | /* Slab allocator for tree-sitter (thread-safe via TLS). Destroy any |
| 1585 | * parser this thread still holds BEFORE switching the global ts |
| 1586 | * allocator: a parser created in the mimalloc epoch (sequential run, |
| 1587 | * watcher tick) must be freed by the allocator that created it, or its |
| 1588 | * teardown after the switch routes mi pointers into plain free() |
| 1589 | * (#773). */ |
| 1590 | cbm_destroy_thread_parser(); |
| 1591 | cbm_slab_install(); |
| 1592 | CBM_PROF_END("parallel_extract", "1_init_libs", t_init); |
| 1593 | |
| 1594 | /* Sub-phase: Sort files by descending size for tail-latency reduction */ |
| 1595 | CBM_PROF_START(t_sort); |
| 1596 | file_sort_entry_t *sorted = malloc((size_t)file_count * sizeof(file_sort_entry_t)); |
| 1597 | if (!sorted) { |
| 1598 | return CBM_NOT_FOUND; |
| 1599 | } |
| 1600 | for (int i = 0; i < file_count; i++) { |
| 1601 | sorted[i].idx = i; |
| 1602 | sorted[i].size = files[i].size; |
| 1603 | } |
| 1604 | qsort(sorted, file_count, sizeof(file_sort_entry_t), compare_by_size_desc); |
| 1605 | CBM_PROF_END_N("parallel_extract", "2_sort_files", t_sort, file_count); |
| 1606 | |
| 1607 | /* Allocate per-worker state (cache-line aligned via posix_memalign) */ |
| 1608 | extract_worker_state_t *workers = NULL; |
| 1609 | if (cbm_aligned_alloc((void **)&workers, CBM_CACHE_LINE, |