use std::alloc::{GlobalAlloc, Layout, System};
use std::cell::Cell;
use std::sync::atomic::{AtomicU64, Ordering};
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct AllocCounts {
pub allocations: u64,
pub deallocations: u64,
pub bytes: u64,
}
impl AllocCounts {
#[must_use]
pub fn since(self, earlier: AllocCounts) -> AllocCounts {
AllocCounts {
allocations: self.allocations.wrapping_sub(earlier.allocations),
deallocations: self.deallocations.wrapping_sub(earlier.deallocations),
bytes: self.bytes.wrapping_sub(earlier.bytes),
}
}
}
pub fn thread_alloc_counts() -> AllocCounts {
THREAD.try_with(Cell::get).unwrap_or_default()
}
pub fn process_alloc_counts() -> AllocCounts {
AllocCounts {
allocations: PROCESS_ALLOCATIONS.load(Ordering::Relaxed),
deallocations: PROCESS_DEALLOCATIONS.load(Ordering::Relaxed),
bytes: PROCESS_BYTES.load(Ordering::Relaxed),
}
}
const ZERO: AllocCounts = AllocCounts {
allocations: 0,
deallocations: 0,
bytes: 0,
};
thread_local! {
static THREAD: Cell<AllocCounts> = const { Cell::new(ZERO) };
}
static PROCESS_ALLOCATIONS: AtomicU64 = AtomicU64::new(0);
static PROCESS_DEALLOCATIONS: AtomicU64 = AtomicU64::new(0);
static PROCESS_BYTES: AtomicU64 = AtomicU64::new(0);
fn count_allocation(bytes: usize) {
let bytes = bytes as u64;
PROCESS_ALLOCATIONS.fetch_add(1, Ordering::Relaxed);
PROCESS_BYTES.fetch_add(bytes, Ordering::Relaxed);
let _ = THREAD.try_with(|counts| {
let mut thread = counts.get();
thread.allocations = thread.allocations.wrapping_add(1);
thread.bytes = thread.bytes.wrapping_add(bytes);
counts.set(thread);
});
}
fn count_deallocation() {
PROCESS_DEALLOCATIONS.fetch_add(1, Ordering::Relaxed);
let _ = THREAD.try_with(|counts| {
let mut thread = counts.get();
thread.deallocations = thread.deallocations.wrapping_add(1);
counts.set(thread);
});
}
struct Counting;
#[global_allocator]
static ALLOCATOR: Counting = Counting;
unsafe impl GlobalAlloc for Counting {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
let pointer = unsafe { System.alloc(layout) };
if !pointer.is_null() {
count_allocation(layout.size());
}
pointer
}
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
let pointer = unsafe { System.alloc_zeroed(layout) };
if !pointer.is_null() {
count_allocation(layout.size());
}
pointer
}
unsafe fn realloc(&self, pointer: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
let moved = unsafe { System.realloc(pointer, layout, new_size) };
if !moved.is_null() {
count_allocation(new_size);
}
moved
}
unsafe fn dealloc(&self, pointer: *mut u8, layout: Layout) {
unsafe { System.dealloc(pointer, layout) };
count_deallocation();
}
}
#[cfg(test)]
mod tests {
use std::hint::black_box;
use std::thread;
use super::*;
#[test]
fn a_box_counts_one_allocation_of_its_size_and_one_deallocation() {
let start = thread_alloc_counts();
let boxed = black_box(Box::new([7u8; 100]));
let allocated = thread_alloc_counts().since(start);
drop(boxed);
let freed = thread_alloc_counts().since(start);
assert_eq!(
allocated,
AllocCounts {
allocations: 1,
deallocations: 0,
bytes: 100
}
);
assert_eq!(
freed,
AllocCounts {
allocations: 1,
deallocations: 1,
bytes: 100
}
);
}
#[test]
fn a_vec_counts_its_growth_and_not_its_reuse() {
let start = thread_alloc_counts();
let mut numbers: Vec<u64> = Vec::with_capacity(4);
for number in 0..1000 {
numbers.push(black_box(number));
}
let grown = thread_alloc_counts().since(start);
assert!((2..=12).contains(&grown.allocations), "{grown:?}");
assert_eq!(
grown.deallocations, 0,
"a reallocation is not a deallocation"
);
numbers.clear();
let refilled = thread_alloc_counts();
for number in 0..1000 {
numbers.push(black_box(number));
}
assert_eq!(
thread_alloc_counts().since(refilled),
AllocCounts::default()
);
}
#[test]
fn another_threads_allocations_count_for_the_process_and_not_here() {
const MIB: u64 = 1 << 20;
let (here, process) = (thread_alloc_counts(), process_alloc_counts());
let theirs = thread::spawn(|| {
let start = thread_alloc_counts();
drop(black_box(vec![1u8; MIB as usize]));
thread_alloc_counts().since(start)
})
.join()
.expect("the thread runs");
assert_eq!(theirs.allocations, 1);
assert_eq!(theirs.bytes, MIB);
let mine = thread_alloc_counts().since(here);
assert!(mine.bytes < MIB, "{mine:?}");
assert!(process_alloc_counts().since(process).bytes >= MIB);
}
}