use std::alloc::{GlobalAlloc, Layout};
use std::sync::atomic::{AtomicI64, AtomicU64, Ordering::Relaxed};
pub static ALLOCS: AtomicU64 = AtomicU64::new(0);
pub static FREES: AtomicU64 = AtomicU64::new(0);
pub static BYTES: AtomicU64 = AtomicU64::new(0);
pub static LIVE: AtomicI64 = AtomicI64::new(0);
pub static PEAK: AtomicU64 = AtomicU64::new(0);
pub static REALLOCS: AtomicU64 = AtomicU64::new(0);
const fn zero_atomic() -> AtomicU64 {
AtomicU64::new(0)
}
pub static SIZE_HIST: [AtomicU64; 64] = [const { zero_atomic() }; 64];
#[derive(Debug, Clone)]
pub struct AllocStats {
pub allocs: u64,
pub frees: u64,
pub bytes: u64,
pub live: i64,
pub peak: u64,
pub reallocs: u64,
pub size_hist: [u64; 64],
}
impl AllocStats {
pub fn delta(&self, before: &Self) -> Self {
Self {
allocs: self.allocs - before.allocs,
frees: self.frees - before.frees,
bytes: self.bytes - before.bytes,
live: self.live - before.live,
peak: self.peak,
reallocs: self.reallocs - before.reallocs,
size_hist: {
let mut h = [0u64; 64];
for (i, slot) in h.iter_mut().enumerate() {
*slot = self.size_hist[i] - before.size_hist[i];
}
h
},
}
}
}
pub fn snapshot() -> AllocStats {
let mut hist = [0u64; 64];
for (i, s) in SIZE_HIST.iter().enumerate() {
hist[i] = s.load(Relaxed);
}
AllocStats {
allocs: ALLOCS.load(Relaxed),
frees: FREES.load(Relaxed),
bytes: BYTES.load(Relaxed),
live: LIVE.load(Relaxed),
peak: PEAK.load(Relaxed),
reallocs: REALLOCS.load(Relaxed),
size_hist: hist,
}
}
pub fn reset() {
ALLOCS.store(0, Relaxed);
FREES.store(0, Relaxed);
BYTES.store(0, Relaxed);
LIVE.store(0, Relaxed);
PEAK.store(0, Relaxed);
REALLOCS.store(0, Relaxed);
for s in SIZE_HIST.iter() {
s.store(0, Relaxed);
}
}
pub struct Counting<A>(pub A);
unsafe impl<A: GlobalAlloc> GlobalAlloc for Counting<A> {
#[inline]
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
ALLOCS.fetch_add(1, Relaxed);
BYTES.fetch_add(layout.size() as u64, Relaxed);
let idx = (63 - layout.size().max(1).leading_zeros()) as usize;
if idx < 64 {
SIZE_HIST[idx].fetch_add(1, Relaxed);
}
let live = LIVE.fetch_add(layout.size() as i64, Relaxed) + layout.size() as i64;
PEAK.fetch_max(live as u64, Relaxed);
self.0.alloc(layout)
}
#[inline]
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
FREES.fetch_add(1, Relaxed);
LIVE.fetch_sub(layout.size() as i64, Relaxed);
self.0.dealloc(ptr, layout)
}
#[inline]
unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
REALLOCS.fetch_add(1, Relaxed);
let old_size = layout.size();
let size_diff = new_size as i64 - old_size as i64;
LIVE.fetch_add(size_diff, Relaxed);
let live = LIVE.load(Relaxed);
PEAK.fetch_max(live as u64, Relaxed);
let idx = (63 - new_size.max(1).leading_zeros()) as usize;
if idx < 64 {
SIZE_HIST[idx].fetch_add(1, Relaxed);
}
BYTES.fetch_add(new_size as u64, Relaxed);
self.0.realloc(ptr, layout, new_size)
}
}
pub fn print_stats(label: &str, stats: &AllocStats) {
println!("=== {label} ===");
println!(
" allocs: {:>12} frees: {:>12} reallocs: {:>8}",
stats.allocs, stats.frees, stats.reallocs
);
println!(
" bytes: {:>12} live: {:>12} peak: {:>12}",
stats.bytes, stats.live, stats.peak
);
println!(" size histogram (log2 buckets):");
for (i, &count) in stats.size_hist.iter().enumerate() {
if count > 0 {
let lo = 1u64 << i;
let hi = if i < 63 { 1u64 << (i + 1) } else { u64::MAX };
println!(" [{:>10} .. {:<10}): {:>10}", lo, hi, count);
}
}
}