feat(numa): introduce I/O sampling to prevent activation stalls
Replaces the monolithic CPU scaling threshold with separate CPU and I/O spawn thresholds. Introduces an `IoSample` struct with platform-specific byte reading and a relative throughput growth heuristic. Adds a 0.1s wall-clock guard to `CpuSample` to suppress artificial efficiency spikes, and updates `maybe_activate` to trigger worker scaling when either resource indicates headroom. Bumps `obikmer` to v1.1.33 and updates architecture documentation.
This commit is contained in:
1 parent
6378734e1c
commit
f84dd539bf
5 files changed
+225
-22
No files matched your search
+94
-1
@@ -266,9 +266,15 @@ impl CpuSample {
|
||||
}
|
||||
|
||||
pub fn do_i_activate(&mut self, threshold: f64) -> bool {
|
||||
let delta_wall = self.wall.elapsed().as_secs_f64();
|
||||
if delta_wall < 0.1 {
|
||||
// Window too short to be meaningful — leave state untouched so it
|
||||
// keeps accumulating until a real sample can be taken.
|
||||
return false;
|
||||
}
|
||||
|
||||
let n = CpuSample::now();
|
||||
let delta_ru = (n.user_secs - self.user_secs) + (n.sys_secs - self.sys_secs);
|
||||
let delta_wall = self.wall.elapsed().as_secs_f64();
|
||||
|
||||
let efficiency = delta_ru / delta_wall;
|
||||
let activate = 0f64.max(efficiency - self.previous) >= threshold;
|
||||
@@ -289,6 +295,93 @@ impl CpuSample {
|
||||
}
|
||||
}
|
||||
|
||||
// ── IoSample ──────────────────────────────────────────────────────────────────
|
||||
|
||||
/// Snapshot of process-wide block I/O (bytes read + written) + wall clock.
|
||||
///
|
||||
/// Same activation protocol as [`CpuSample`], but the growth check in
|
||||
/// [`do_i_activate`](Self::do_i_activate) is *relative* rather than absolute:
|
||||
/// raw I/O throughput has no portable scale across storage devices, unlike a
|
||||
/// core count.
|
||||
pub struct IoSample {
|
||||
wall: Instant,
|
||||
bytes: u64,
|
||||
previous_rate: f64,
|
||||
}
|
||||
|
||||
impl IoSample {
|
||||
pub fn now() -> Self {
|
||||
Self {
|
||||
wall: Instant::now(),
|
||||
bytes: Self::read_bytes(),
|
||||
previous_rate: 0.0,
|
||||
}
|
||||
}
|
||||
|
||||
/// Bytes actually submitted to the block layer (read + write), summed
|
||||
/// process-wide. Returns 0 if unavailable — degrades gracefully to a
|
||||
/// signal that never triggers activation (CPU-only heuristic).
|
||||
#[cfg(target_os = "linux")]
|
||||
fn read_bytes() -> u64 {
|
||||
let Ok(io) = std::fs::read_to_string("/proc/self/io") else {
|
||||
return 0;
|
||||
};
|
||||
io.lines()
|
||||
.filter_map(|l| {
|
||||
l.strip_prefix("read_bytes: ")
|
||||
.or_else(|| l.strip_prefix("write_bytes: "))
|
||||
})
|
||||
.filter_map(|v| v.trim().parse::<u64>().ok())
|
||||
.sum()
|
||||
}
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
fn read_bytes() -> u64 {
|
||||
use libc::{RUSAGE_INFO_V4, getpid, proc_pid_rusage, rusage_info_v4};
|
||||
let mut info: rusage_info_v4 = unsafe { std::mem::zeroed() };
|
||||
let ret =
|
||||
unsafe { proc_pid_rusage(getpid(), RUSAGE_INFO_V4, &mut info as *mut _ as *mut _) };
|
||||
if ret != 0 {
|
||||
return 0;
|
||||
}
|
||||
info.ri_diskio_bytesread + info.ri_diskio_byteswritten
|
||||
}
|
||||
|
||||
#[cfg(not(any(target_os = "linux", target_os = "macos")))]
|
||||
fn read_bytes() -> u64 {
|
||||
0
|
||||
}
|
||||
|
||||
/// Same protocol as [`CpuSample::do_i_activate`] (0.1 s minimum window,
|
||||
/// state untouched on early return), but growth is measured relative to
|
||||
/// the previous rate. `threshold` is a fraction, e.g. `0.2` for a 20 %
|
||||
/// increase in throughput since the last real sample.
|
||||
pub fn do_i_activate(&mut self, threshold: f64) -> bool {
|
||||
let elapsed = self.wall.elapsed().as_secs_f64();
|
||||
if elapsed < 0.1 {
|
||||
return false;
|
||||
}
|
||||
|
||||
let n = Self::read_bytes();
|
||||
let rate = n.saturating_sub(self.bytes) as f64 / elapsed;
|
||||
let activate = if self.previous_rate == 0.0 {
|
||||
rate > 0.0 // bootstrap: any measured throughput is signal enough
|
||||
} else {
|
||||
(rate - self.previous_rate) / self.previous_rate >= threshold
|
||||
};
|
||||
|
||||
debug!(
|
||||
"Do I activate (I/O) : {} -> {} Activate: {}",
|
||||
self.previous_rate, rate, activate
|
||||
);
|
||||
self.previous_rate = rate;
|
||||
self.bytes = n;
|
||||
self.wall = Instant::now();
|
||||
|
||||
activate
|
||||
}
|
||||
}
|
||||
|
||||
// ── public API ────────────────────────────────────────────────────────────────
|
||||
|
||||
/// Snapshot taken at the start of a pipeline stage.
|
||||
|
||||
Reference in new issue
Block a user