|
| 1 | +//! Allocator statistics: bytes of tensor storage currently alive, the peak since the last reset, and allocation counts |
| 2 | +//! (the CPU analogue of `torch.cuda.memory_allocated` / `max_memory_allocated`). Views share their base storage, so only |
| 3 | +//! real buffers count; a buffer's bytes are returned when its last handle is dropped. |
| 4 | +
|
| 5 | +use std::sync::atomic::{AtomicUsize, Ordering::Relaxed}; |
| 6 | + |
| 7 | +static LIVE: AtomicUsize = AtomicUsize::new(0); |
| 8 | +static PEAK: AtomicUsize = AtomicUsize::new(0); |
| 9 | +static ALLOCS: AtomicUsize = AtomicUsize::new(0); |
| 10 | +static LIVE_BUFFERS: AtomicUsize = AtomicUsize::new(0); |
| 11 | + |
| 12 | +pub(crate) fn on_alloc(bytes: usize) { |
| 13 | + let now = LIVE.fetch_add(bytes, Relaxed) + bytes; |
| 14 | + PEAK.fetch_max(now, Relaxed); |
| 15 | + ALLOCS.fetch_add(1, Relaxed); |
| 16 | + LIVE_BUFFERS.fetch_add(1, Relaxed); |
| 17 | +} |
| 18 | + |
| 19 | +pub(crate) fn on_free(bytes: usize) { |
| 20 | + LIVE.fetch_sub(bytes, Relaxed); |
| 21 | + LIVE_BUFFERS.fetch_sub(1, Relaxed); |
| 22 | +} |
| 23 | + |
| 24 | +/// A snapshot of the counters. |
| 25 | +#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 26 | +pub struct MemoryStats { |
| 27 | + pub allocated_bytes: usize, |
| 28 | + pub peak_bytes: usize, |
| 29 | + pub live_buffers: usize, |
| 30 | + pub total_allocations: usize, |
| 31 | +} |
| 32 | + |
| 33 | +pub fn stats() -> MemoryStats { |
| 34 | + MemoryStats { allocated_bytes: LIVE.load(Relaxed), peak_bytes: PEAK.load(Relaxed), live_buffers: LIVE_BUFFERS.load(Relaxed), total_allocations: ALLOCS.load(Relaxed) } |
| 35 | +} |
| 36 | + |
| 37 | +/// Bytes of tensor storage alive right now. |
| 38 | +pub fn allocated_bytes() -> usize { |
| 39 | + LIVE.load(Relaxed) |
| 40 | +} |
| 41 | + |
| 42 | +/// Highest [`allocated_bytes`] since startup or the last [`reset_peak`]. |
| 43 | +pub fn peak_bytes() -> usize { |
| 44 | + PEAK.load(Relaxed) |
| 45 | +} |
| 46 | + |
| 47 | +/// Restarts peak tracking from the current usage. |
| 48 | +pub fn reset_peak() { |
| 49 | + PEAK.store(LIVE.load(Relaxed), Relaxed); |
| 50 | +} |
| 51 | + |
| 52 | +#[cfg(test)] |
| 53 | +mod tests { |
| 54 | + use crate::tensor::Tensor; |
| 55 | + |
| 56 | + // global counters: other tests allocate concurrently, so compare with inequalities on a large buffer |
| 57 | + #[test] |
| 58 | + fn a_large_tensor_raises_live_bytes_and_peak_and_releases_them_on_drop() { |
| 59 | + let big = 8 << 20; // 8 MiB of f32 = 2M elements |
| 60 | + let before = super::stats(); |
| 61 | + let t = Tensor::zeros(&[big / 4], crate::dtype::DType::F32, &crate::device::Device::Cpu).unwrap(); |
| 62 | + let view = t.reshape(&[2, big / 8]).unwrap(); // a view adds no buffer |
| 63 | + let during = super::stats(); |
| 64 | + assert!(during.allocated_bytes >= before.allocated_bytes + big - (1 << 20) || during.peak_bytes >= big); |
| 65 | + assert!(during.peak_bytes >= big); |
| 66 | + drop(view); |
| 67 | + drop(t); |
| 68 | + std::thread::sleep(std::time::Duration::from_millis(1)); |
| 69 | + assert!(super::allocated_bytes() < during.allocated_bytes || during.allocated_bytes < big); |
| 70 | + assert!(super::stats().total_allocations > before.total_allocations); |
| 71 | + } |
| 72 | +} |
0 commit comments