Skip to content

Commit 349d80c

Browse files
committed
perf(index): run supervised index workers at below-normal CPU priority
A full index saturates every core in the parallel extraction pass, and interactive CLI/MCP queries (CPU-bound at start-up) were measured taking twice their wall time behind it with flat CPU time: pure run-queue contention. The index-worker role now lowers its own priority once, before any indexing thread is created: nice 10 on POSIX, BELOW_NORMAL_PRIORITY_CLASS on Windows. It never raises priority: a process already nicer than 10 (or in the IDLE / BELOW_NORMAL class) is left alone. Plain nice only, not macOS background QoS, which would pin indexing to efficiency cores and throttle its I/O. Failure is logged and ignored. The test runs the helper in a forked child and checks both legs: a normal-priority child ends at nice >= 10, and a child started nicer than that is left where it was. Refs #2834 Signed-off-by: Shane McCarron <[email protected]>
1 parent a3831c9 commit 349d80c

4 files changed

Lines changed: 91 additions & 0 deletions

File tree

‎src/foundation/platform.c‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,3 +729,28 @@ const char *cbm_resolve_cache_dir(void) {
729729
}
730730
return buf;
731731
}
732+
733+
/* ── CPU scheduling priority ────────────────── */
734+
735+
#ifndef _WIN32
736+
#include <sys/resource.h>
737+
#endif
738+
739+
bool cbm_lower_cpu_priority(void) {
740+
#ifdef _WIN32
741+
HANDLE self = GetCurrentProcess();
742+
DWORD current = GetPriorityClass(self);
743+
if (current == IDLE_PRIORITY_CLASS || current == BELOW_NORMAL_PRIORITY_CLASS) {
744+
return true; /* already at least this low: never raise priority */
745+
}
746+
return SetPriorityClass(self, BELOW_NORMAL_PRIORITY_CLASS) != 0;
747+
#else
748+
/* getpriority can legitimately return -1, so errno is the only error signal. */
749+
errno = 0;
750+
int current = getpriority(PRIO_PROCESS, 0);
751+
if (errno == 0 && current >= CBM_LOW_CPU_PRIORITY_NICE) {
752+
return true; /* already at least this nice: never raise priority */
753+
}
754+
return setpriority(PRIO_PROCESS, 0, CBM_LOW_CPU_PRIORITY_NICE) == 0;
755+
#endif
756+
}

‎src/foundation/platform.h‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,4 +215,17 @@ int64_t cbm_file_size(const char *path);
215215
* On POSIX, this is a no-op. Returns the input pointer. */
216216
char *cbm_normalize_path_sep(char *path);
217217

218+
/* ── CPU scheduling priority ───────────────────────────────────── */
219+
220+
enum { CBM_LOW_CPU_PRIORITY_NICE = 10 };
221+
222+
/* Lower this process's CPU scheduling priority so background work yields the
223+
* cores to interactive processes. POSIX: nice CBM_LOW_CPU_PRIORITY_NICE (never
224+
* raises an already-nicer process). Plain nice only, deliberately not macOS
225+
* background QoS: that pins work to efficiency cores and throttles I/O.
226+
* Windows: BELOW_NORMAL_PRIORITY_CLASS. On Linux nice is per thread, so call
227+
* this before creating threads; later threads inherit it.
228+
* Returns false when the priority could not be changed. */
229+
bool cbm_lower_cpu_priority(void);
230+
218231
#endif /* CBM_PLATFORM_H */

‎src/main.c‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -902,6 +902,12 @@ static int run_cli(int argc, char **argv, cbm_project_lock_manager_t *project_lo
902902
* own here, and only here (cbm_sqlite_dedicated_heap). */
903903
cbm_sqlite_dedicated_heap(true);
904904
cbm_memev_process_role("worker");
905+
/* A full index saturates every core; run it below normal priority so
906+
* interactive CLI/MCP queries are not starved. Before any indexing
907+
* thread exists, so they all inherit it. Failure is harmless. */
908+
if (!cbm_lower_cpu_priority()) {
909+
cbm_log_info("index.worker.priority", "lowered", "false");
910+
}
905911
}
906912
cbm_index_set_worker_role_options(index_worker, response_out, worker_single_thread,
907913
worker_marker, worker_quarantine,

‎tests/test_platform.c‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
#include <unistd.h>
1717
#ifndef _WIN32
1818
#include <sys/stat.h> /* chmod for the symlink-policy mode legs */
19+
#include <errno.h>
20+
#include <sys/resource.h> /* getpriority for the worker-priority leg */
21+
#include <sys/wait.h>
1922
#endif
2023

2124
#ifdef __linux__
@@ -1442,6 +1445,49 @@ TEST(regex_estimate_counts_brackets_and_escapes_literally) {
14421445
PASS();
14431446
}
14441447

1448+
/* An index worker lowers its own priority (#2834). A non-root process cannot
1449+
* raise it back, so the helper runs in a forked child that reports its nice. */
1450+
TEST(platform_lower_cpu_priority_nices_the_process) {
1451+
#ifdef _WIN32
1452+
SKIP_PLATFORM("POSIX nice contract");
1453+
#else
1454+
int fds[2];
1455+
ASSERT_EQ(pipe(fds), 0);
1456+
pid_t child = fork();
1457+
if (child == 0) {
1458+
(void)close(fds[0]);
1459+
/* report: lowered ok, nice after; then, from a nicer start, ok, target, nice after */
1460+
int report[5] = {cbm_lower_cpu_priority() ? 1 : 0, 0, 0, 0, 0};
1461+
errno = 0;
1462+
report[1] = getpriority(PRIO_PROCESS, 0);
1463+
if (errno != 0) {
1464+
report[0] = 0;
1465+
}
1466+
/* Never-raise leg: start nicer than the helper's level; it must leave it. */
1467+
report[3] = report[1] >= 15 ? 19 : 15;
1468+
if (setpriority(PRIO_PROCESS, 0, report[3]) == 0) {
1469+
report[2] = cbm_lower_cpu_priority() ? 1 : 0;
1470+
errno = 0;
1471+
report[4] = getpriority(PRIO_PROCESS, 0);
1472+
}
1473+
_exit(write(fds[1], report, sizeof(report)) == (ssize_t)sizeof(report) ? 0 : 1);
1474+
}
1475+
ASSERT_TRUE(child > 0);
1476+
(void)close(fds[1]);
1477+
int report[5] = {0, 0, 0, 0, 0};
1478+
ssize_t got = read(fds[0], report, sizeof(report));
1479+
(void)close(fds[0]);
1480+
int status = 0;
1481+
(void)waitpid(child, &status, 0);
1482+
ASSERT_EQ(got, (ssize_t)sizeof(report));
1483+
ASSERT_EQ(report[0], 1);
1484+
ASSERT_TRUE(report[1] >= CBM_LOW_CPU_PRIORITY_NICE);
1485+
ASSERT_EQ(report[2], 1);
1486+
ASSERT_EQ(report[4], report[3]);
1487+
PASS();
1488+
#endif
1489+
}
1490+
14451491
SUITE(platform) {
14461492
RUN_TEST(platform_file_apis_survive_max_path_overflow);
14471493
RUN_TEST(platform_mkdir_p_follows_own_symlink_only_when_opted_in);
@@ -1477,6 +1523,7 @@ SUITE(platform) {
14771523
RUN_TEST(platform_env_long_refuses_what_it_cannot_read);
14781524
RUN_TEST(platform_env_dup_honours_the_settings_resolver);
14791525
RUN_TEST(platform_system_info);
1526+
RUN_TEST(platform_lower_cpu_priority_nices_the_process);
14801527
RUN_TEST(regex_compile_refuses_oversized_expansion);
14811528
RUN_TEST(regex_compile_accepts_expansion_under_budget);
14821529
RUN_TEST(regex_compile_refuses_overlong_pattern);

0 commit comments

Comments
 (0)