Skip to main content

mimalloc_pprof/
sys.rs

1//! Raw bindings to the in-tree mimalloc static amalgamation.
2
3use core::ffi::c_char;
4use core::ffi::c_int;
5use core::ffi::c_void;
6
7pub type MiProfWriteFun = unsafe extern "C" fn(*mut c_void, *const c_char, usize);
8
9/// Mirrors `MI_PROF_STAT_VERSION` in `include/mimalloc/profile.h`.
10pub const MI_PROF_STAT_VERSION: c_int = 3;
11
12/// Mirrors `mi_prof_stats_t` (include/mimalloc/profile.h) field-for-field.
13#[repr(C)]
14#[allow(non_camel_case_types)]
15pub struct mi_prof_stats_t {
16    pub size: usize,
17    pub version: c_int,
18    pub enabled: bool,
19    pub accum: bool,
20    pub sample_rate: usize,
21    pub live_samples: usize,
22    pub live_bytes: usize,
23    pub accum_samples: usize,
24    pub accum_bytes: usize,
25    pub unique_stacks: usize,
26    pub arena_committed: usize,
27    pub stack_table_overflows: usize,
28    /// v2. Mirrors `mi_prof_stats_t.dropped_samples`: count of ALL dropped
29    /// samples (record-alloc failure, stack-intern failure, including the
30    /// `MI_PROF_STACK_CAP` cap); `stack_table_overflows` is a subset, so
31    /// `dropped_samples >= stack_table_overflows` always.
32    pub dropped_samples: usize,
33    /// v3. Allocator-level ("ground truth") counters read from `mi_stats_get()`.
34    /// Unlike every field above, these are exact rather than sampled.
35    pub heap_committed: usize,
36    pub heap_reserved: usize,
37    pub heap_malloc_requested: usize,
38    pub heap_pages: usize,
39    pub heap_pages_abandoned: usize,
40    pub heap_count: usize,
41    /// Live thread-local heaps. The main thread's statically-initialized theap
42    /// is not counted, so a single-threaded process reports 0.
43    pub theap_count: usize,
44    pub heap_purged: usize,
45    /// True when the C library was built with `MI_STAT >= 2`. `heap_malloc_requested`
46    /// is only maintained at that level; a default release build reports 0.
47    pub heap_stats_detailed: bool,
48}
49
50/// Mirrors `MI_PROF_CONFIG_VERSION` in `include/mimalloc/profile.h`.
51pub const MI_PROF_CONFIG_VERSION: c_int = 1;
52
53/// Mirrors `MI_PROF_FORMAT_TEXT` / `MI_PROF_FORMAT_PROTO` (include/mimalloc/profile.h).
54pub const MI_PROF_FORMAT_TEXT: c_int = 0;
55pub const MI_PROF_FORMAT_PROTO: c_int = 1;
56
57/// Mirrors `mi_prof_config_mode_t`'s `MI_PROF_CONFIG_FALLBACK` /
58/// `MI_PROF_CONFIG_OVERRIDE` (include/mimalloc/profile.h).
59pub const MI_PROF_CONFIG_FALLBACK: c_int = 0;
60pub const MI_PROF_CONFIG_OVERRIDE: c_int = 1;
61
62/// Mirrors `mi_prof_config_t` (include/mimalloc/profile.h) field-for-field.
63#[repr(C)]
64#[allow(non_camel_case_types)]
65pub struct mi_prof_config_t {
66    pub size: usize,
67    pub version: c_int,
68    pub mode: c_int, // mi_prof_config_mode_t
69    pub sample_interval: usize,
70    pub max_profiler_bytes: usize,
71    pub seed: u64,
72    pub accum: bool,
73    pub max_stack_depth: usize,
74    pub dump_at_exit: *const c_char,
75    pub dump_format: c_int,
76}
77
78/// Mirrors `mi_prof_sample_info_t` (include/mimalloc/profile.h) field-for-field.
79#[repr(C)]
80#[allow(non_camel_case_types)]
81pub struct mi_prof_sample_info_t {
82    pub stack: *const *const c_void,
83    pub depth: usize,
84    pub live_objects: usize,
85    pub live_bytes: usize,
86    pub accum_objects: usize,
87    pub accum_bytes: usize,
88}
89
90#[allow(non_camel_case_types)]
91pub type mi_prof_visit_fun =
92    unsafe extern "C" fn(info: *const mi_prof_sample_info_t, arg: *mut c_void) -> bool;
93
94/// Opaque handle for `mi_prof_snapshot_t`; the profiler never hands out a
95/// value, only a pointer, so this type is never constructed on the Rust side.
96#[allow(non_camel_case_types)]
97pub enum mi_prof_snapshot_t {}
98
99/// Mirrors `mi_prof_module_info_t` (include/mimalloc/profile.h) field-for-field.
100#[repr(C)]
101#[allow(non_camel_case_types)]
102pub struct mi_prof_module_info_t {
103    pub path: *const c_char,
104    pub base: usize,
105    pub size: usize,
106}
107
108#[allow(non_camel_case_types)]
109pub type mi_prof_module_visit_fun =
110    unsafe extern "C" fn(info: *const mi_prof_module_info_t, arg: *mut c_void) -> bool;
111
112unsafe extern "C" {
113    pub fn mi_malloc(size: usize) -> *mut c_void;
114    pub fn mi_zalloc(size: usize) -> *mut c_void;
115    pub fn mi_calloc(count: usize, size: usize) -> *mut c_void;
116    pub fn mi_realloc(p: *mut c_void, newsize: usize) -> *mut c_void;
117    pub fn mi_free(p: *mut c_void);
118    pub fn mi_malloc_aligned(size: usize, alignment: usize) -> *mut c_void;
119    pub fn mi_zalloc_aligned(size: usize, alignment: usize) -> *mut c_void;
120    pub fn mi_realloc_aligned(p: *mut c_void, newsize: usize, alignment: usize) -> *mut c_void;
121    // Zeroing reallocation (issue #83). These grow a block AND zero the new tail, which
122    // `GlobalAlloc` cannot express -- it has no `grow_zeroed` -- so a Rust caller would
123    // otherwise grow and `memset` by hand, redoing work mimalloc has already done.
124    //
125    // What they zero is NOT [old_requested, new): mimalloc measures from the block's
126    // old *usable* size, so the slack between requested and usable is left as-is. A
127    // block requested at 64 bytes may be usable to 80, and growing to 70 is served in
128    // place with nothing zeroed. See `prof::rezalloc` for the safe-wrapper docs.
129    pub fn mi_rezalloc(p: *mut c_void, newsize: usize) -> *mut c_void;
130    pub fn mi_recalloc(p: *mut c_void, newcount: usize, size: usize) -> *mut c_void;
131    pub fn mi_rezalloc_aligned(p: *mut c_void, newsize: usize, alignment: usize) -> *mut c_void;
132    pub fn mi_recalloc_aligned(
133        p: *mut c_void,
134        newcount: usize,
135        size: usize,
136        alignment: usize,
137    ) -> *mut c_void;
138    /// Grow in place only; returns NULL if the block cannot be extended without moving.
139    pub fn mi_expand(p: *mut c_void, newsize: usize) -> *mut c_void;
140    pub fn mi_usable_size(p: *const c_void) -> usize;
141    pub fn mi_prof_start(sample_rate: usize) -> bool;
142    pub fn mi_prof_start_seeded(sample_rate: usize, seed: u64) -> bool;
143    pub fn mi_prof_start_ex(config: *const mi_prof_config_t) -> bool;
144    pub fn mi_prof_stop();
145    pub fn mi_prof_is_enabled() -> bool;
146    pub fn mi_prof_dump(path: *const c_char) -> bool;
147    pub fn mi_prof_dump_writer(write: Option<MiProfWriteFun>, arg: *mut c_void) -> bool;
148    /// profile.proto (google/pprof) writer: same sample/period/mapping semantics as
149    /// `mi_prof_dump`/`mi_prof_dump_writer` but encoded as an uncompressed, binary
150    /// pprof Profile message instead of the legacy "heap profile:" text.
151    pub fn mi_prof_dump_proto(path: *const c_char) -> bool;
152    pub fn mi_prof_dump_proto_writer(write: Option<MiProfWriteFun>, arg: *mut c_void) -> bool;
153    pub fn mi_prof_reset();
154    pub fn mi_prof_debug_stats(records: *mut usize, bytes: *mut usize, unique_stacks: *mut usize);
155    pub fn mi_prof_stats_get(stats: *mut mi_prof_stats_t) -> bool;
156    pub fn mi_prof_visit(visitor: mi_prof_visit_fun, arg: *mut c_void) -> bool;
157    pub fn mi_prof_snapshot_new() -> *mut mi_prof_snapshot_t;
158    pub fn mi_prof_snapshot_visit(
159        snap: *const mi_prof_snapshot_t,
160        visitor: mi_prof_visit_fun,
161        arg: *mut c_void,
162    ) -> bool;
163    pub fn mi_prof_snapshot_free(snap: *mut mi_prof_snapshot_t);
164    /// Structured module (mapping) enumeration, e.g. to build pprof Mapping entries
165    /// yourself. No profiler lock is taken: module lists are OS-owned, not part of
166    /// the sampled-allocation table. `info` (and `info->path`) are valid only for
167    /// the duration of the callback.
168    pub fn mi_prof_modules_visit(visitor: mi_prof_module_visit_fun, arg: *mut c_void) -> bool;
169
170    /// Mirrors `mi_unwrapped_malloc` (include/mimalloc/memory-events.h): backed
171    /// directly by the raw OS layer, never by the hooked `mi_malloc` family.
172    /// See that header's "Stable public unwrapped instrumentation allocation
173    /// path" comment for the full contract.
174    pub fn mi_unwrapped_malloc(size: usize, alignment: usize) -> *mut c_void;
175    /// Mirrors `mi_unwrapped_free` (include/mimalloc/memory-events.h).
176    pub fn mi_unwrapped_free(p: *mut c_void);
177    /// Mirrors `mi_unwrapped_realloc` (include/mimalloc/memory-events.h).
178    pub fn mi_unwrapped_realloc(p: *mut c_void, new_size: usize, alignment: usize) -> *mut c_void;
179}