Introduce CPU budget and dynamic thread limiting
Release / create-release (push) Successful in 3m4s
ci.yml / build (pull_request) Successful in 4m17s
Release / build-linux-x86_64 (push) Successful in 8m11s
Release / build-macos-arm64 (push) Successful in 2m2s

This change implements mechanisms to enforce resource limits based on the available CPU budget, including setting a hard CPU ceiling and calculating effective thread counts for CLI, query, and superkmer operations.
This commit is contained in:
Eric Coissac committed 2026-09-10 17:08:32 +02:00
1 parent b954032faa
commit b66a488277
9 files changed
+79 -10

No files matched your search

+4 -1
View File
@@ -12,5 +12,8 @@ pub use budget::MemoryBudget;
pub use lock::DirLock;
pub use numa::PartitionRunner;
pub use progress::{Progress, TracedBar, progress_bar, spinner};
pub use resources::{CpuSample, IoSample, available_memory_bytes, effective_parallelism, peak_rss_bytes};
pub use resources::{
CpuSample, IoSample, available_memory_bytes, cpu_budget, effective_parallelism, peak_rss_bytes,
set_cpu_cap,
};
pub use stage::{Reporter, Stage, StageStats};
+27
View File
@@ -1,3 +1,4 @@
use std::sync::OnceLock;
use std::time::Instant;
use libc::{RUSAGE_SELF, getrusage, rusage, timeval};
@@ -120,6 +121,32 @@ pub fn effective_parallelism() -> usize {
host
}
// ── Process-wide CPU budget (hard ceiling) ───────────────────────────────────
static CPU_CAP: OnceLock<usize> = OnceLock::new();
/// Install a hard ceiling on the number of cores the process may use.
///
/// The value is clamped to `[1, effective_parallelism()]` — a cap can only
/// lower the budget, never raise it above what the process is actually
/// allowed to run on. First call wins; later calls are ignored.
///
/// Call this once at startup (before sizing any worker pool or the global
/// rayon pool) when the user passes an explicit `--cpu-max`.
pub fn set_cpu_cap(n: usize) {
let capped = n.clamp(1, effective_parallelism());
let _ = CPU_CAP.set(capped);
}
/// The process-wide CPU budget: the ceiling set via [`set_cpu_cap`] if any,
/// otherwise [`effective_parallelism`].
///
/// Every worker pool and the global rayon pool must size themselves against
/// this value rather than calling [`effective_parallelism`] directly.
pub fn cpu_budget() -> usize {
CPU_CAP.get().copied().unwrap_or_else(effective_parallelism)
}
/// cgroup v2 (unified hierarchy): reads `cpu.max` ("<quota> <period>", or
/// "max <period>" when unlimited) for the current process's cgroup, rounded
/// up to whole cores. Returns `None` if unlimited or on any parse error.