| 281 | } |
| 282 | |
| 283 | int cbm_default_worker_count(bool initial) { |
| 284 | /* CBM_WORKERS env override (clamped to [1, CBM_WORKERS_MAX]). |
| 285 | * Useful inside containers where sysconf(_SC_NPROCESSORS_ONLN) |
| 286 | * reports host CPUs rather than the cgroup's effective CPU quota. |
| 287 | * Same precedence shape as other CBM_* env overrides: |
| 288 | * explicit override > implicit detection. */ |
| 289 | char buf[CBM_SZ_32]; |
| 290 | if (cbm_safe_getenv("CBM_WORKERS", buf, sizeof(buf), NULL) != NULL) { |
| 291 | long n = strtol(buf, NULL, CBM_DECIMAL_BASE); |
| 292 | if (n >= MIN_WORKERS && n <= CBM_WORKERS_MAX) { |
| 293 | return (int)n; |
| 294 | } |
| 295 | cbm_log_warn("workers.env.invalid", "value", buf, "fallback", "sysconf"); |
| 296 | } |
| 297 | |
| 298 | cbm_system_info_t info = cbm_system_info(); |
| 299 | if (initial) { |
| 300 | /* Use all cores for initial indexing — user is waiting */ |
| 301 | return info.total_cores; |
| 302 | } |
| 303 | /* Incremental: leave headroom for user's apps */ |
| 304 | int workers = info.perf_cores - SKIP_ONE; |
| 305 | return workers > 0 ? workers : MIN_WORKERS; |
| 306 | } |
no test coverage detected