|
|
use std::{ |
|
|
cell::UnsafeCell, |
|
|
ptr::NonNull, |
|
|
sync::atomic::{AtomicUsize, Ordering}, |
|
|
}; |
|
|
|
|
|
use crate::AllocationCounters; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static ALLOCATED: AtomicUsize = AtomicUsize::new(0); |
|
|
const KB: usize = 1024; |
|
|
|
|
|
|
|
|
const TARGET_BUFFER: usize = 100 * KB; |
|
|
|
|
|
|
|
|
const MAX_BUFFER: usize = 200 * KB; |
|
|
|
|
|
#[derive(Default)] |
|
|
struct ThreadLocalCounter { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
buffer: usize, |
|
|
allocation_counters: AllocationCounters, |
|
|
} |
|
|
|
|
|
impl ThreadLocalCounter { |
|
|
const fn new() -> Self { |
|
|
Self { |
|
|
buffer: 0, |
|
|
allocation_counters: AllocationCounters::new(), |
|
|
} |
|
|
} |
|
|
fn add(&mut self, size: usize) { |
|
|
self.allocation_counters.allocations += size; |
|
|
self.allocation_counters.allocation_count += 1; |
|
|
if self.buffer >= size { |
|
|
self.buffer -= size; |
|
|
} else { |
|
|
let offset = size - self.buffer + TARGET_BUFFER; |
|
|
self.buffer = TARGET_BUFFER; |
|
|
ALLOCATED.fetch_add(offset, Ordering::Relaxed); |
|
|
} |
|
|
} |
|
|
|
|
|
fn remove(&mut self, size: usize) { |
|
|
self.allocation_counters.deallocations += size; |
|
|
self.allocation_counters.deallocation_count += 1; |
|
|
self.buffer += size; |
|
|
if self.buffer > MAX_BUFFER { |
|
|
let offset = self.buffer - TARGET_BUFFER; |
|
|
self.buffer = TARGET_BUFFER; |
|
|
ALLOCATED.fetch_sub(offset, Ordering::Relaxed); |
|
|
} |
|
|
} |
|
|
|
|
|
fn update(&mut self, old_size: usize, new_size: usize) { |
|
|
self.allocation_counters.deallocations += old_size; |
|
|
self.allocation_counters.deallocation_count += 1; |
|
|
self.allocation_counters.allocations += new_size; |
|
|
self.allocation_counters.allocation_count += 1; |
|
|
match old_size.cmp(&new_size) { |
|
|
std::cmp::Ordering::Equal => {} |
|
|
std::cmp::Ordering::Less => { |
|
|
let size = new_size - old_size; |
|
|
if self.buffer >= size { |
|
|
self.buffer -= size; |
|
|
} else { |
|
|
let offset = size - self.buffer + TARGET_BUFFER; |
|
|
self.buffer = TARGET_BUFFER; |
|
|
ALLOCATED.fetch_add(offset, Ordering::Relaxed); |
|
|
} |
|
|
} |
|
|
std::cmp::Ordering::Greater => { |
|
|
let size = old_size - new_size; |
|
|
self.buffer += size; |
|
|
if self.buffer > MAX_BUFFER { |
|
|
let offset = self.buffer - TARGET_BUFFER; |
|
|
self.buffer = TARGET_BUFFER; |
|
|
ALLOCATED.fetch_sub(offset, Ordering::Relaxed); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
fn unload(&mut self) { |
|
|
if self.buffer > 0 { |
|
|
ALLOCATED.fetch_sub(self.buffer, Ordering::Relaxed); |
|
|
self.buffer = 0; |
|
|
} |
|
|
self.allocation_counters = AllocationCounters::default(); |
|
|
} |
|
|
} |
|
|
|
|
|
thread_local! { |
|
|
static LOCAL_COUNTER: UnsafeCell<ThreadLocalCounter> = const {UnsafeCell::new(ThreadLocalCounter::new())}; |
|
|
} |
|
|
|
|
|
pub fn get() -> usize { |
|
|
ALLOCATED.load(Ordering::Relaxed) |
|
|
} |
|
|
|
|
|
pub fn allocation_counters() -> AllocationCounters { |
|
|
with_local_counter(|local| local.allocation_counters.clone()) |
|
|
} |
|
|
|
|
|
pub fn reset_allocation_counters(start: AllocationCounters) { |
|
|
with_local_counter(|local| local.allocation_counters = start); |
|
|
} |
|
|
|
|
|
fn with_local_counter<T>(f: impl FnOnce(&mut ThreadLocalCounter) -> T) -> T { |
|
|
LOCAL_COUNTER.with(|local| { |
|
|
let ptr = local.get(); |
|
|
|
|
|
let mut local = unsafe { NonNull::new_unchecked(ptr) }; |
|
|
f(unsafe { local.as_mut() }) |
|
|
}) |
|
|
} |
|
|
|
|
|
|
|
|
pub fn add(size: usize) { |
|
|
with_local_counter(|local| local.add(size)); |
|
|
} |
|
|
|
|
|
|
|
|
pub fn remove(size: usize) { |
|
|
with_local_counter(|local| local.remove(size)); |
|
|
} |
|
|
|
|
|
|
|
|
pub fn update(old_size: usize, new_size: usize) { |
|
|
with_local_counter(|local| local.update(old_size, new_size)); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub fn flush() { |
|
|
with_local_counter(|local| local.unload()); |
|
|
} |
|
|
|
|
|
#[cfg(test)] |
|
|
mod tests { |
|
|
use super::*; |
|
|
|
|
|
#[test] |
|
|
fn counting() { |
|
|
let mut expected = get(); |
|
|
add(100); |
|
|
|
|
|
expected += TARGET_BUFFER + 100; |
|
|
assert_eq!(get(), expected); |
|
|
add(100); |
|
|
|
|
|
assert_eq!(get(), expected); |
|
|
add(MAX_BUFFER); |
|
|
|
|
|
expected += 100 + MAX_BUFFER; |
|
|
assert_eq!(get(), expected); |
|
|
remove(100); |
|
|
|
|
|
|
|
|
assert_eq!(get(), expected); |
|
|
remove(MAX_BUFFER); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
expected -= MAX_BUFFER + 100; |
|
|
assert_eq!(get(), expected); |
|
|
} |
|
|
} |
|
|
|