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

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.

Detailed Description

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:

#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 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.

  • MIMALLOC_PROFILE_SAMPLE_RATE: approximate number of allocated bytes between samples (default: 512KiB).
  • MIMALLOC_PROFILE_ALLOC_INTERVAL: allocated bytes between automatic snapshots (default: 1GiB; 0 disables).
  • MIMALLOC_PROFILE_INUSE_INTERVAL: growth in in-use bytes between automatic snapshots (default: 0, disabled).
  • MIMALLOC_PROFILE_TIME_INTERVAL: seconds between automatic snapshots, checked on allocation events (default: 0, disabled).

Snapshots use protobuf format by default. A basename ending in .Heaps or .text selects the text heap-profile format instead.

Typedef Documentation

◆ mi_profiler_t

typedef struct mi_profiler_s mi_profiler_t

Profiler type. Custom profiler fields and callback types are defined in <mimalloc-profile.h>.

Function Documentation

◆ mi_heap_profile()

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.

Returns
true on success; fails if the profiler is already attached, or if no more profilers can be attached.

◆ mi_heap_profile_detach()

bool mi_heap_profile_detach ( mi_heap_t * heap,
mi_profiler_t * profiler )

experimental: Detach a profiler from a particular heap.

Parameters
heapThe heap.
profilerThe profiler to detach, or NULL to detach all profilers.
Returns
true if the profiler was attached (or if profiler is NULL).

◆ mi_heap_profile_disable()

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.

◆ mi_pprof_profiler_delete()

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_pprof_profiler_new()

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.

Parameters
initial_thresholdApproximate number of allocated bytes between samples (for example, 512 * 1024).
base_file_nameOutput basename. A .Heaps or .text extension selects text format; any other extension, or none, selects protobuf format. A filename extension is stripped.
alloc_interval_sizeAllocated bytes between automatic snapshots, or 0 to disable.
inuse_interval_sizeGrowth in in-use bytes between automatic snapshots, or 0 to disable.
time_interval_secsSeconds between automatic snapshots, or 0 to disable. Time intervals are checked on allocation events, not by a background timer.
Returns
A profiler, or NULL on failure. The returned profiler must be attached and started before it samples allocations.
See also
mi_profile(), mi_profiler_start(), mi_pprof_profiler_delete()

◆ mi_profile()

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.

Returns
true on success; fails if the profiler is already attached, or if no more profilers can be attached.
See also
mi_heap_profile()

◆ mi_profile_detach()

bool mi_profile_detach ( mi_profiler_t * profiler)

experimental: Detach a profiler from the main subprocess and its heaps.

Parameters
profilerThe profiler to detach, or NULL to detach all profilers.
Returns
true if the profiler was attached (or if profiler is NULL).

◆ mi_profiler_snapshot()

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.

◆ mi_profiler_start()

bool mi_profiler_start ( mi_profiler_t * profiler)

experimental: Start sampling with an attached profiler.

Returns
Whether the profiler was already running; false on the first start is normal.

◆ mi_profiler_stop()

bool mi_profiler_stop ( mi_profiler_t * profiler)

experimental: Stop sampling.

The on_free callback can still run until the profiler is detached.

Returns
Whether the profiler was running before this call.

◆ mi_subproc_profile()

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.

Returns
true on success; fails if the profiler is already attached, or if no more profilers can be attached.

◆ mi_subproc_profile_detach()

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.

Parameters
subproc_idThe subprocess.
profilerThe profiler to detach, or NULL to detach all profilers.
Returns
true if the profiler was attached (or if profiler is NULL).

◆ mi_usdt_profiler_delete()

void mi_usdt_profiler_delete ( mi_profiler_t * profiler)

experimental: Delete a USDT profiler.

Parameters
profilerThe 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_usdt_profiler_new()

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:

#include <mimalloc.h>
#include <mimalloc-profile.h>
int main(void) {
mi_profiler_t* prof = mi_usdt_profiler_new(0); // 0: use `MIMALLOC_PROFILE_SAMPLE_RATE` (512 KiB by default)
if (prof == NULL) return 1; // USDT probes are not supported
if (!mi_profile(prof)) { // fails if no more profilers can be attached
return 1;
}
// Run your workload here; join worker threads before cleanup.
mi_profile_detach(prof); // detach before deleting
return 0;
}
void mi_usdt_profiler_delete(mi_profiler_t *profiler)
experimental: Delete a USDT profiler.
mi_profiler_t * mi_usdt_profiler_new(size_t sample_rate)
experimental: Create a new USDT profiler (on Linux x64 and arm64).
Parameters
sample_rateThe mean sample rate in bytes, or 0 to use the MIMALLOC_PROFILE_SAMPLE_RATE option (512 KiB by default).
Returns
The USDT profiler, or NULL if USDT probes are not supported (or out of memory).
See also
Profiling