v3: Sample allocations using the built-in pprof-compatible profiler or custom profiling hooks. More...
Typedefs | |
| typedef struct mi_profiler_s | mi_profiler_t |
| Profiler type. Custom profiler fields and callback types are defined in <mimalloc-profile.h>. | |
Functions | |
| 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. | |
| void | mi_pprof_profiler_delete (mi_profiler_t *profiler) |
| experimental: Delete a built-in profiler created by mi_pprof_profiler_new(). | |
| mi_profiler_t * | mi_usdt_profiler_new (size_t sample_rate) |
| experimental: Create a new USDT profiler (on Linux x64 and arm64). | |
| void | mi_usdt_profiler_delete (mi_profiler_t *profiler) |
| experimental: Delete a USDT profiler. | |
| bool | mi_profile (mi_profiler_t *profiler) |
| experimental: Attach a profiler to the current and future heaps of the main subprocess. | |
| bool | mi_profile_detach (mi_profiler_t *profiler) |
| experimental: Detach a profiler from the main subprocess and its heaps. | |
| bool | mi_subproc_profile (mi_subproc_id_t subproc_id, mi_profiler_t *profiler) |
| experimental: Attach a profiler to the current and future heaps of a particular subprocess. | |
| bool | mi_subproc_profile_detach (mi_subproc_id_t subproc_id, mi_profiler_t *profiler) |
| experimental: Detach a profiler from a particular subprocess and its heaps. | |
| bool | mi_heap_profile (mi_heap_t *heap, mi_profiler_t *profiler) |
| experimental: Attach a profiler to a particular heap, or pass NULL as the profiler to detach all profilers. | |
| bool | mi_heap_profile_detach (mi_heap_t *heap, mi_profiler_t *profiler) |
| experimental: Detach a profiler from a particular heap. | |
| void | mi_heap_profile_disable (mi_heap_t *heap) |
| experimental: Exclude a heap from profiling, even when a profiler is subsequently attached to its subprocess. | |
| bool | mi_profiler_start (mi_profiler_t *profiler) |
| experimental: Start sampling with an attached profiler. | |
| bool | mi_profiler_stop (mi_profiler_t *profiler) |
| experimental: Stop sampling. | |
| void | mi_profiler_snapshot (mi_profiler_t *profiler) |
| experimental: Take a snapshot of the current profile. | |
v3: Sample allocations using the built-in pprof-compatible profiler or custom profiling hooks.
Include <mimalloc-profile.h> and build with profiling support (-DMI_PROFILE=ON). The profiling API is experimental and may still change.
Programmatic profiling
Explicit profiler APIs remain available in secure builds and when environment-based activation is disabled. Create a profiler, attach it, and then start sampling:
Stopping sampling does not detach the profiler: its on_free callback can still be invoked for sampled allocations. Before deleting a profiler, detach it from all associated heaps and subprocesses, and ensure no other thread can still invoke its callbacks.
Environment-based profiling
When both MI_SECURE=0 and MI_SECURE_ENV=0, MIMALLOC_PROFILE=<basename> activates the built-in profiler for the main subprocess. Elevated processes ignore this environment variable regardless of build settings.
Snapshots use protobuf format by default. A basename ending in .Heaps or .text selects the text heap-profile format instead.
| typedef struct mi_profiler_s mi_profiler_t |
Profiler type. Custom profiler fields and callback types are defined in <mimalloc-profile.h>.
| bool mi_heap_profile | ( | mi_heap_t * | heap, |
| mi_profiler_t * | profiler ) |
experimental: Attach a profiler to a particular heap, or pass NULL as the profiler to detach all profilers.
Multiple profilers (up to 4 by default, see MI_PROFILERS_MAX) can be attached at the same time, where each profiler samples at its own rate (and an allocation can be sampled by multiple profilers). Attaching stops sampling. The on_free callback is only invoked for allocations sampled by this profiler since it was last attached.
| bool mi_heap_profile_detach | ( | mi_heap_t * | heap, |
| mi_profiler_t * | profiler ) |
experimental: Detach a profiler from a particular heap.
| heap | The heap. |
| profiler | The profiler to detach, or NULL to detach all profilers. |
| void mi_heap_profile_disable | ( | mi_heap_t * | heap | ) |
experimental: Exclude a heap from profiling, even when a profiler is subsequently attached to its subprocess.
Useful for a heap used by a custom profiler to allocate its own metadata.
| void mi_pprof_profiler_delete | ( | mi_profiler_t * | profiler | ) |
experimental: Delete a built-in profiler created by mi_pprof_profiler_new().
First detach the profiler and ensure no other thread can still invoke its callbacks. Deleting a profiler does not take a final snapshot; use mi_profiler_snapshot() explicitly. Other profilers are not deleted (and a warning is emitted).
| 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.
| initial_threshold | Approximate number of allocated bytes between samples (for example, 512 * 1024). |
| base_file_name | Output basename. A .Heaps or .text extension selects text format; any other extension, or none, selects protobuf format. A filename extension is stripped. |
| alloc_interval_size | Allocated bytes between automatic snapshots, or 0 to disable. |
| inuse_interval_size | Growth in in-use bytes between automatic snapshots, or 0 to disable. |
| time_interval_secs | Seconds between automatic snapshots, or 0 to disable. Time intervals are checked on allocation events, not by a background timer. |
| bool mi_profile | ( | mi_profiler_t * | profiler | ) |
experimental: Attach a profiler to the current and future heaps of the main subprocess.
Heaps excluded by mi_heap_profile_disable() remain excluded. Pass NULL to detach all profilers. Attaching stops sampling; use mi_profiler_start() to start it.
| bool mi_profile_detach | ( | mi_profiler_t * | profiler | ) |
experimental: Detach a profiler from the main subprocess and its heaps.
| profiler | The profiler to detach, or NULL to detach all profilers. |
| void mi_profiler_snapshot | ( | mi_profiler_t * | profiler | ) |
experimental: Take a snapshot of the current profile.
May be called while sampling is stopped. Concurrent calls may skip a snapshot if another snapshot callback is already running.
| bool mi_profiler_start | ( | mi_profiler_t * | profiler | ) |
experimental: Start sampling with an attached profiler.
| bool mi_profiler_stop | ( | mi_profiler_t * | profiler | ) |
experimental: Stop sampling.
The on_free callback can still run until the profiler is detached.
| bool mi_subproc_profile | ( | mi_subproc_id_t | subproc_id, |
| mi_profiler_t * | profiler ) |
experimental: Attach a profiler to the current and future heaps of a particular subprocess.
Heaps excluded by mi_heap_profile_disable() remain excluded. Pass NULL as the profiler to detach all profilers. Attaching stops sampling.
| bool mi_subproc_profile_detach | ( | mi_subproc_id_t | subproc_id, |
| mi_profiler_t * | profiler ) |
experimental: Detach a profiler from a particular subprocess and its heaps.
| subproc_id | The subprocess. |
| profiler | The profiler to detach, or NULL to detach all profilers. |
| void mi_usdt_profiler_delete | ( | mi_profiler_t * | profiler | ) |
experimental: Delete a USDT profiler.
| profiler | The profiler returned by mi_usdt_profiler_new() (or NULL). Detach the profiler first (using mi_profile_detach() or mi_heap_profile_detach()), and ensure no other threads are still allocating with it. Other profilers are not deleted (and a warning is emitted). |
| mi_profiler_t * mi_usdt_profiler_new | ( | size_t | sample_rate | ) |
experimental: Create a new USDT profiler (on Linux x64 and arm64).
It fires the mimalloc:sample_alloc(ptr,size,weight) and mimalloc:sample_free(ptr,size,weight) USDT probes on sampled allocations. Sampling starts automatically when a tracer attaches to one of the probes (and stops when it detaches), except in secure mode or elevated processes. Calling this function disables automatic activation (ending an automatically started session) such that the profiler can be attached to the process (or a specific heap using mi_heap_profile()) and started explicitly:
| sample_rate | The mean sample rate in bytes, or 0 to use the MIMALLOC_PROFILE_SAMPLE_RATE option (512 KiB by default). |