Skip to main content

decl_lang/
allocation_diagnostics.rs

1//! Rust requested-allocation counters for separate diagnostic helpers.
2//! Counts requests to MiMalloc, not its capacity, C allocations or RSS.
3//! This module is compiled only with `runtime-diagnostics`; feature-empty native
4//! builds do not include these counters.
5//!
6//! Counters are cumulative across all threads and all [`CountingAllocator`]
7//! instances; they are not reset by taking a snapshot. Each field is read with
8//! relaxed atomics, so snapshots are not transactions across concurrent calls.
9//! Compare ledger identities only at endpoints with other allocator users idle.
10use serde::Serialize;
11use std::alloc::{GlobalAlloc, Layout};
12use std::sync::atomic::{AtomicU64, Ordering::Relaxed};
13static ALLOCS: AtomicU64 = AtomicU64::new(0);
14static REALLOCS: AtomicU64 = AtomicU64::new(0);
15static FREES: AtomicU64 = AtomicU64::new(0);
16static ALLOCATED: AtomicU64 = AtomicU64::new(0);
17static FREED: AtomicU64 = AtomicU64::new(0);
18static LIVE: AtomicU64 = AtomicU64::new(0);
19static PEAK: AtomicU64 = AtomicU64::new(0);
20static FAILURES: AtomicU64 = AtomicU64::new(0);
21fn grow(bytes: u64) {
22    let n = LIVE.fetch_add(bytes, Relaxed) + bytes;
23    PEAK.fetch_max(n, Relaxed);
24}
25/// Requested-allocation ledger read from the process-wide counters.
26///
27/// A default value is an all-zero value, not a read of the current counters.
28/// Individual fields can reflect different instants while allocations continue.
29#[derive(Clone, Copy, Debug, Default, Serialize)]
30pub struct Snapshot {
31    /// Successful `alloc` and `alloc_zeroed` calls; excludes reallocations.
32    pub alloc_calls: u64,
33    /// Successful reallocations, including those that retain the same address.
34    pub realloc_calls: u64,
35    /// Explicit `dealloc` calls; excludes the old side of a reallocation.
36    pub free_calls: u64,
37    /// Cumulative requested bytes of successful allocations and new realloc sizes.
38    pub allocated_bytes: u64,
39    /// Cumulative explicit deallocation sizes and old sizes of successful reallocations.
40    pub freed_bytes: u64,
41    /// Net requested bytes still accounted as live; excludes allocator rounding.
42    pub live_bytes: u64,
43    /// Largest net live-byte count recorded since counting began, across all threads.
44    /// This is not a phase-local peak or the allocator's transient old/new
45    /// storage overlap during a reallocation.
46    pub global_peak_bytes: u64,
47    /// Allocation or reallocation calls that returned a null pointer.
48    /// Such calls do not update the successful-call or byte counters.
49    pub failures: u64,
50}
51/// Read the cumulative ledger without allocation or resetting any counter.
52///
53/// Loads are individually atomic but not a coherent multi-field transaction.
54/// With allocator activity quiescent, `allocated_bytes - freed_bytes` equals
55/// `live_bytes`. Concurrent calls can temporarily violate that identity in a read.
56pub fn snapshot() -> Snapshot {
57    Snapshot {
58        alloc_calls: ALLOCS.load(Relaxed),
59        realloc_calls: REALLOCS.load(Relaxed),
60        free_calls: FREES.load(Relaxed),
61        allocated_bytes: ALLOCATED.load(Relaxed),
62        freed_bytes: FREED.load(Relaxed),
63        live_bytes: LIVE.load(Relaxed),
64        global_peak_bytes: PEAK.load(Relaxed),
65        failures: FAILURES.load(Relaxed),
66    }
67}
68/// MiMalloc delegate that records successful Rust allocation requests.
69///
70/// A diagnostic executable must install this value as its global allocator for
71/// ordinary Rust allocations to be counted. Merely enabling the diagnostics
72/// feature does not replace an executable's allocator. Direct C allocations and
73/// MiMalloc's internal capacity are outside this ledger.
74///
75/// Successful reallocations add the complete new and old requested sizes to
76/// the gross byte counters, while changing live bytes only by their difference.
77/// This avoids treating internal realloc overlap as a requested live-byte peak.
78pub struct CountingAllocator;
79unsafe impl GlobalAlloc for CountingAllocator {
80    unsafe fn alloc(&self, l: Layout) -> *mut u8 {
81        let p = unsafe { mimalloc::MiMalloc.alloc(l) };
82        if p.is_null() {
83            FAILURES.fetch_add(1, Relaxed);
84        } else {
85            ALLOCS.fetch_add(1, Relaxed);
86            ALLOCATED.fetch_add(l.size() as u64, Relaxed);
87            grow(l.size() as u64);
88        }
89        p
90    }
91    unsafe fn alloc_zeroed(&self, l: Layout) -> *mut u8 {
92        let p = unsafe { mimalloc::MiMalloc.alloc_zeroed(l) };
93        if p.is_null() {
94            FAILURES.fetch_add(1, Relaxed);
95        } else {
96            ALLOCS.fetch_add(1, Relaxed);
97            ALLOCATED.fetch_add(l.size() as u64, Relaxed);
98            grow(l.size() as u64);
99        }
100        p
101    }
102    unsafe fn dealloc(&self, p: *mut u8, l: Layout) {
103        unsafe { mimalloc::MiMalloc.dealloc(p, l) };
104        FREES.fetch_add(1, Relaxed);
105        FREED.fetch_add(l.size() as u64, Relaxed);
106        LIVE.fetch_sub(l.size() as u64, Relaxed);
107    }
108    unsafe fn realloc(&self, p: *mut u8, l: Layout, n: usize) -> *mut u8 {
109        let q = unsafe { mimalloc::MiMalloc.realloc(p, l, n) };
110        if q.is_null() {
111            FAILURES.fetch_add(1, Relaxed);
112        } else {
113            REALLOCS.fetch_add(1, Relaxed);
114            ALLOCATED.fetch_add(n as u64, Relaxed);
115            FREED.fetch_add(l.size() as u64, Relaxed);
116            if n >= l.size() {
117                grow((n - l.size()) as u64)
118            } else {
119                LIVE.fetch_sub((l.size() - n) as u64, Relaxed);
120            }
121        }
122        q
123    }
124}