use std::sync::atomic::{AtomicU64, Ordering};
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct HeapStats {
pub total_blocks: u64,
pub total_bytes: u64,
pub curr_blocks: usize,
pub curr_bytes: usize,
pub max_blocks: usize,
pub max_bytes: usize,
}
impl HeapStats {
pub fn get() -> Self {
let snap = crate::current_snapshot_or_zeros();
Self {
total_blocks: snap.alloc_count,
total_bytes: snap.total_bytes,
curr_blocks: snap.live_count as usize,
curr_bytes: snap.current_bytes as usize,
max_blocks: snap.peak_live_count as usize,
max_bytes: snap.peak_bytes as usize,
}
}
}
#[derive(Clone, Debug, PartialEq, Eq, Default)]
pub struct AdHocStats {
pub total_events: u64,
pub total_units: u64,
}
impl AdHocStats {
pub fn get() -> Self {
Self {
total_events: AD_HOC_EVENTS.load(Ordering::Relaxed),
total_units: AD_HOC_UNITS.load(Ordering::Relaxed),
}
}
}
pub(crate) static AD_HOC_EVENTS: AtomicU64 = AtomicU64::new(0);
pub(crate) static AD_HOC_UNITS: AtomicU64 = AtomicU64::new(0);
pub fn ad_hoc_event(weight: usize) {
AD_HOC_EVENTS.fetch_add(1, Ordering::Relaxed);
AD_HOC_UNITS.fetch_add(weight as u64, Ordering::Relaxed);
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn ad_hoc_event_accumulates() {
let before = AdHocStats::get();
ad_hoc_event(10);
ad_hoc_event(5);
let after = AdHocStats::get();
assert_eq!(after.total_events - before.total_events, 2);
assert_eq!(after.total_units - before.total_units, 15);
}
}