mod-alloc 0.9.2

Allocation profiling for Rust. Counters, peak resident, and call-site grouping with inline backtrace capture. Zero external dependencies in the hot path. Lean dhat replacement targeting MSRV 1.75.
Documentation
//! Integration test: symbolicate the test binary's own
//! allocations.
//!
//! Builds a known-named call chain ending in an allocation, then
//! fetches the symbolicated report and asserts the function name
//! appears. If the test binary was built without debug info (e.g.
//! `RUSTFLAGS="-C strip=symbols"`), the symbolicator silently
//! returns unresolved frames and the test downgrades to a
//! shape-only check.

#![cfg(feature = "symbolicate")]

use mod_alloc::ModAlloc;

#[global_allocator]
static GLOBAL: ModAlloc = ModAlloc::new();

#[inline(never)]
fn mod_alloc_test_known_callsite() {
    let v: Vec<u64> = Vec::with_capacity(1024);
    std::hint::black_box(&v);
}

#[test]
fn symbolicates_the_test_binary_itself() {
    for _ in 0..50 {
        mod_alloc_test_known_callsite();
    }

    let report = GLOBAL.symbolicated_report();
    assert!(!report.is_empty(), "expected at least one call site");

    let total: u64 = report.iter().map(|s| s.count).sum();
    assert!(total >= 50, "expected >= 50 aggregated events, got {total}");

    let any_function_resolved = report
        .iter()
        .flat_map(|s| s.frames.iter())
        .any(|f| f.function.is_some());

    if any_function_resolved {
        let target = "mod_alloc_test_known_callsite";
        let found = report.iter().flat_map(|s| s.frames.iter()).any(|f| {
            f.function
                .as_deref()
                .map(|name| name.contains(target))
                .unwrap_or(false)
        });
        if !found {
            // Print the report for diagnosis if our target is not
            // visible. The walker on stock-FP std builds often
            // captures only the immediate caller of
            // `GlobalAlloc::alloc`, which lives in std/alloc
            // crates, not in this test binary. That is a known
            // limitation documented in v0.9.1's release notes; we
            // accept the looser check and only assert that SOME
            // function name resolved.
            for site in &report {
                for f in &site.frames {
                    eprintln!("  resolved: {:#018x}  -> {:?}", f.address, f.function);
                }
            }
        }
    } else {
        // Stripped binary or PDB missing on Windows. Shape check
        // only.
        for site in &report {
            assert!(
                !site.frames.is_empty(),
                "every site should report at least one frame, even if unresolved"
            );
        }
    }
}