It would be great if peak_alloc had an option to capture a histogram of memory usage at a configurable interval (e.g. every millisecond). Would that be a welcome addition to this crate?
Here's an example implementation which is bolted on top of peak_alloc:
use peak_alloc::PeakAlloc;
use std::thread;
use std::time::Duration;
use std::sync::{Arc, Mutex};
use hdrhistogram::Histogram;
#[global_allocator]
static PEAK_ALLOC: PeakAlloc = PeakAlloc;
struct MemoryTracker {
// Histogram to store 1ms samples (in bytes)
samples: Arc<Mutex<Histogram<u64>>>,
}
impl MemoryTracker {
fn new() -> Self {
// Histogram covering 1B to 100GB with 3 significant figures
let hist = Histogram::<u64>::new_with_bounds(1, 100_000_000_000, 3).unwrap();
Self {
samples: Arc::new(Mutex::new(hist)),
}
}
fn start_monitoring(&self) {
let samples = Arc::clone(&self.samples);
thread::spawn(move || {
loop {
// Sample the current usage from peak_alloc
let current_mem = PEAK_ALLOC.current_usage() as u64;
if let Ok(mut h) = samples.lock() {
let _ = h.record(current_mem);
}
thread::sleep(Duration::from_millis(1));
}
});
}
fn get_percentile(&self, p: f64) -> u64 {
if let Ok(h) = self.samples.lock() {
h.value_at_quantile(p / 100.0)
} else {
0
}
}
}
fn main() {
let tracker = MemoryTracker::new();
tracker.start_monitoring();
// Your application logic here
let mut data = Vec::with_capacity(1_000_000);
for i in 0..1_000_000 { data.push(i); }
// Retrieve the 99th percentile usage
println!("P99 Memory Usage: {} bytes", tracker.get_percentile(99.0));
}
It would be great if peak_alloc had an option to capture a histogram of memory usage at a configurable interval (e.g. every millisecond). Would that be a welcome addition to this crate?
Here's an example implementation which is bolted on top of peak_alloc: