Introduce CPU budget and dynamic thread limiting
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:
1 parent
b954032faa
commit
b66a488277
9 files changed
+79
-10
No files matched your search
@@ -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};
|
||||
@@ -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.
|
||||
|
||||
Reference in new issue
Block a user