mi-malloc 3.7/2.5/1.15
Loading...
Searching...
No Matches
Profiling

Profiling support is experimental and may still change. It is enabled by default, including in release builds, and adds overhead only when activated. The profiling API in <mimalloc-profile.h> supports both custom profiling hooks, the built-in pprof-compatible profiler, and USDT probes for tracers like bpftrace.

Programmatic Profiling

Explicit profiler APIs remain available in secure builds and when environment activation is disabled. Build with profiling support (-DMI_PROFILE=ON), then create a profiler, attach it, and start sampling:

#include <mimalloc.h>
#include <mimalloc-profile.h>
int main(void) {
512 * 1024, // sample roughly every 512 KiB
"myprofile", // output basename; protobuf format
0, 0, 0); // no automatic snapshots
if (prof == NULL) return 1;
if (!mi_profile(prof)) {
return 1;
}
// Run your workload here; join worker threads before cleanup.
mi_profile_detach(prof); // detach before deleting
return 0;
}
mi_profiler_t * mi_pprof_profiler_new(size_t initial_threshold, const char *base_file_name, size_t alloc_interval_size, size_t inuse_interval_size, size_t time_interval_secs)
experimental: Create the built-in pprof-compatible profiler.
bool mi_profile(mi_profiler_t *profiler)
experimental: Attach a profiler to the current and future heaps of the main subprocess.
bool mi_profiler_start(mi_profiler_t *profiler)
experimental: Start sampling with an attached profiler.
struct mi_profiler_s mi_profiler_t
Profiler type. Custom profiler fields and callback types are defined in <mimalloc-profile....
Definition mimalloc-doc.h:1640
void mi_profiler_snapshot(mi_profiler_t *profiler)
experimental: Take a snapshot of the current profile.
bool mi_profiler_stop(mi_profiler_t *profiler)
experimental: Stop sampling.
bool mi_profile_detach(mi_profiler_t *profiler)
experimental: Detach a profiler from the main subprocess and its heaps.
void mi_pprof_profiler_delete(mi_profiler_t *profiler)
experimental: Delete a built-in profiler created by mi_pprof_profiler_new().

Stopping sampling does not detach the profiler: its on_free callback can still run for sampled allocations. Detach it from all associated heaps and subprocesses, and ensure no other thread can still invoke its callbacks before deleting it. See Heap Profiling for the complete API.

Environment-based Profiling

The built-in profiler works with Google's pprof tools. When both MI_SECURE=0 and MI_SECURE_ENV=0, and the process is not elevated, it can also be activated through the environment:

  • MIMALLOC_PROFILE=<basename> activates profiling for the main subprocess and writes pprof-compatible <basename>.<seq>.pb files. A .Heaps or .text extension selects the text heap-profile format instead. For a basename of profile, view the results in a browser with pprof -http=:8080 ./myprogram profile.*.
  • MIMALLOC_PROFILE_ALLOC_INTERVAL=N sets the approximate number of allocated bytes between automatic snapshots (default: 1GiB; 0 disables).
  • MIMALLOC_PROFILE_INUSE_INTERVAL=N takes a snapshot when in-use bytes grow by that amount (default: 0, disabled).
  • MIMALLOC_PROFILE_TIME_INTERVAL=N sets seconds between automatic snapshots, checked on allocation events (default: 0, disabled).
  • MIMALLOC_PROFILE_SAMPLE_RATE=N sets the average number of allocated bytes between samples, using a Poisson distribution (default: 512KiB).

USDT Profiling

On Linux (x64 and arm64), mimalloc also has USDT (user statically-defined tracing) probes on sampled allocations. Sampling starts automatically once a tracer attaches to one of the probes (and stops when it detaches), except in secure mode or in elevated (setuid/setgid) processes. This allows external tools, like bpftrace or eBPF based profilers, to capture stack traces of a running program with little overhead. There are two probes, where weight is the number of bytes allocated since the previous sample (using the MIMALLOC_PROFILE_SAMPLE_RATE):

  • mimalloc:sample_alloc(ptr, size, weight): a sampled allocation.
  • mimalloc:sample_free(ptr, size, weight): a sampled allocation is freed.

For example (where arg2 is the weight):

> sudo bpftrace -p <pid> -e 'usdt:/usr/lib/libmimalloc.so:mimalloc:sample_alloc { @[ustack] = sum(arg2); }'

The USDT profiler can also be attached (to the process or a specific heap) and started programmatically using mi_usdt_profiler_new(), which disables automatic activation.