| 316 | } |
| 317 | |
| 318 | int cbm_default_worker_count(bool initial) { |
| 319 | /* CBM_WORKERS env override (clamped to [1, CBM_WORKERS_MAX]). |
| 320 | * Useful inside containers where sysconf(_SC_NPROCESSORS_ONLN) |
| 321 | * reports host CPUs rather than the cgroup's effective CPU quota. |
| 322 | * Same precedence shape as other CBM_* env overrides: |
| 323 | * explicit override > implicit detection. */ |
| 324 | char buf[CBM_SZ_32]; |
| 325 | if (cbm_safe_getenv("CBM_WORKERS", buf, sizeof(buf), NULL) != NULL) { |
| 326 | long n = strtol(buf, NULL, CBM_DECIMAL_BASE); |
| 327 | if (n >= MIN_WORKERS && n <= CBM_WORKERS_MAX) { |
| 328 | return (int)n; |
| 329 | } |
| 330 | cbm_log_warn("workers.env.invalid", "value", buf, "fallback", "sysconf"); |
| 331 | } |
| 332 | |
| 333 | cbm_system_info_t info = cbm_system_info(); |
| 334 | if (initial) { |
| 335 | /* Use all cores for initial indexing — user is waiting */ |
| 336 | return info.total_cores; |
| 337 | } |
| 338 | /* Incremental: leave headroom for user's apps */ |
| 339 | int workers = info.perf_cores - SKIP_ONE; |
| 340 | return workers > 0 ? workers : MIN_WORKERS; |
| 341 | } |
| 342 | |
| 343 | /* -- Available RAM -------------------------------------------------- |
| 344 | * |