use std::sync::atomic::{AtomicBool, Ordering};
static RUNNING: AtomicBool = AtomicBool::new(true);
const MAX_SAMPLES: usize = 16384;
pub fn run_probe() {
ctrlc::set_handler(move || {
RUNNING.store(false, Ordering::Relaxed);
})
.ok();
let mut samples: Vec<i64> = Vec::with_capacity(MAX_SAMPLES);
let target_ns: i64 = 10_000_000; let req = libc::timespec {
tv_sec: 0,
tv_nsec: target_ns,
};
while RUNNING.load(Ordering::Relaxed) && samples.len() < MAX_SAMPLES {
let mut t0 = libc::timespec {
tv_sec: 0,
tv_nsec: 0,
};
let mut t1 = libc::timespec {
tv_sec: 0,
tv_nsec: 0,
};
unsafe {
libc::clock_gettime(libc::CLOCK_MONOTONIC, &mut t0);
libc::nanosleep(&req, std::ptr::null_mut());
libc::clock_gettime(libc::CLOCK_MONOTONIC, &mut t1);
}
let elapsed_ns = (t1.tv_sec - t0.tv_sec) * 1_000_000_000 + (t1.tv_nsec - t0.tv_nsec);
let overshoot_us = (elapsed_ns - target_ns).max(0) / 1000;
samples.push(overshoot_us);
}
use std::io::Write;
let stdout = std::io::stdout();
let mut handle = stdout.lock();
for s in &samples {
let _ = writeln!(handle, "{}", s);
}
}