Expand description
Opt-in allocation-change accounting and callbacks (include/mimalloc/memory-events.h).
Independent of the sampled profiler, and opt-in at compile time since 0.12.0
(#414): the C side is built only with the memory-events cargo feature (which dhat
implies). Without it the per-allocation hook sites are compiled out – they were
measured at 9-13 instructions per malloc/free pair, 18-26% of the pair, even with
tracking disabled – and every function here links but reports the subsystem off
([set_enabled] returns false, [snapshot] returns None). With the feature on,
tracking is still off until [set_enabled] or MIMALLOC_MEMORY_EVENTS=1.
use mimalloc_pprof::{memory_events, MiMalloc};
// The counters only move for allocations that actually reach mimalloc, so this
// example is only meaningful once mimalloc is the global allocator.
#[global_allocator]
static ALLOCATOR: MiMalloc = MiMalloc;
fn main() {
if !memory_events::set_enabled(true) {
return; // built without the `memory-events` feature: the API is a stub
}
let before = memory_events::snapshot().expect("snapshot").accum_count;
let v = vec![0_u8; 4096];
std::hint::black_box(&v);
let after = memory_events::snapshot().expect("snapshot").accum_count;
assert!(after > before);
memory_events::set_enabled(false);
}Structs§
- Callbacks
- Handlers to install with
set_callbacks, one perChangeKind. - Change
- One observed allocation change, copied out of
mi_memory_change_t. - Snapshot
- Running totals maintained while tracking is enabled.
Enums§
- Change
Kind - Which kind of change a
Changedescribes.
Functions§
- clear_
callbacks - Remove every installed callback. Accounting (and
snapshot) keeps working. - is_
enabled - Whether tracking is on.
- set_
callbacks - Install
callbacks, replacing any previous table. Returnsfalseif the C library refused the table. - set_
enabled - Enable or disable tracking; returns the previous state.
- snapshot
- Read the running totals.
- visit_
live_ ⚠allocations - Walk the live allocations this thread may safely observe, calling
visitorwith each one’s address and usable size. Returnfalsefromvisitorto stop early.