mi-malloc 3.6/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().
bool mi_profile (mi_profiler_t *profiler)
 experimental: Attach a profiler to the current and future heaps of the main subprocess.
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_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.
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)) {
mi_profile(NULL);
return 1;
}
// Run your workload here; join worker threads before cleanup.
mi_profile(NULL); // 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:1597
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.
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.

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.

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

◆ 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. Attaching stops sampling; use mi_profiler_start() to start it.

Returns
true on success.

◆ 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. Attaching stops sampling.

Returns
true on success.