|
| 1 | +/* ---------------------------------------------------------------------------- |
| 2 | +Copyright (c) 2018-2024, Microsoft Research, Daan Leijen |
| 3 | +This is free software; you can redistribute it and/or modify it under the |
| 4 | +terms of the MIT license. A copy of the license can be found in the file |
| 5 | +"LICENSE" at the root of this distribution. |
| 6 | +-----------------------------------------------------------------------------*/ |
| 7 | +#pragma once |
| 8 | +#ifndef MIMALLOC_PROFILE_H |
| 9 | +#define MIMALLOC_PROFILE_H |
| 10 | + |
| 11 | +#include <stddef.h> |
| 12 | +#include <stdint.h> |
| 13 | +#include <stdbool.h> |
| 14 | +#include "mimalloc/atomic.h" |
| 15 | + |
| 16 | +// Forward declarations — full types come from types.h / internal.h. |
| 17 | +typedef struct mi_page_s mi_page_t; |
| 18 | +typedef struct mi_block_s mi_block_t; |
| 19 | +typedef struct mi_heap_s mi_heap_t; |
| 20 | + |
| 21 | +// ------------------------------------------------------------------ |
| 22 | +// Allocation record: one node per sampled live allocation, stored in |
| 23 | +// a singly-linked list at page->metadata. Opaque to callers; the |
| 24 | +// callbacks receive user_data directly. |
| 25 | +// |
| 26 | +// `ptr` — the sampled user pointer; used internally to match frees. |
| 27 | +// `user_data` — flexible array member for caller-owned metadata. |
| 28 | +// The number of bytes available is |
| 29 | +// _mi_profiler.record_extra_bytes, set at |
| 30 | +// mi_profiler_enable() time. Typical uses: a captured |
| 31 | +// stack trace, allocation size and weight for on_free, |
| 32 | +// a pointer to an external profiler node, or a timestamp. |
| 33 | +// The profiler does not initialize this region and never |
| 34 | +// reads it. |
| 35 | +// ------------------------------------------------------------------ |
| 36 | +typedef struct mi_alloc_record_s { |
| 37 | + void* ptr; |
| 38 | + struct mi_alloc_record_s* next; |
| 39 | + char user_data[]; // length = _mi_profiler.record_extra_bytes |
| 40 | +} mi_alloc_record_t; |
| 41 | + |
| 42 | +// ------------------------------------------------------------------ |
| 43 | +// User-supplied callbacks. |
| 44 | +// |
| 45 | +// on_alloc: called when a sample is taken. |
| 46 | +// `user_data` — caller-owned region (record_extra_bytes bytes); |
| 47 | +// may write anything here for use in on_free. |
| 48 | +// NULL if on_free is not set (record_extra_bytes must |
| 49 | +// be 0 in that case) or if record_extra_bytes is 0. |
| 50 | +// `ptr` — the sampled user pointer. |
| 51 | +// `requested_size` — size passed by the caller to malloc/calloc/etc. |
| 52 | +// `usable_size` — actual usable bytes after size-class rounding; |
| 53 | +// reflects true memory consumption. |
| 54 | +// `threshold` — the threshold (bytes) that triggered this sample. |
| 55 | +// `bytes_since_last_sample` — bytes accumulated since the last sample; the |
| 56 | +// statistical weight of this sample. |
| 57 | +// `heap_tag` — tag of the heap that made the allocation, set via |
| 58 | +// mi_heap_new_ex(). Zero for the default heap. |
| 59 | +// Returns the number of bytes to accumulate before the next sample. |
| 60 | +// Returning 0 causes the next allocation to be sampled immediately. |
| 61 | +// |
| 62 | +// on_free: called when a sampled allocation is freed. |
| 63 | +// `user_data` — the same region written during on_alloc. |
| 64 | +// `ptr` — the freed user pointer. |
| 65 | +// May be NULL if free-time notification is not needed. |
| 66 | +// ------------------------------------------------------------------ |
| 67 | +typedef size_t (*mi_profiler_alloc_cb)(void* user_data, void* ptr, size_t requested_size, size_t usable_size, size_t threshold, size_t bytes_since_last_sample, uint8_t heap_tag); |
| 68 | +typedef void (*mi_profiler_free_cb)(void* user_data, void* ptr); |
| 69 | + |
| 70 | +// ------------------------------------------------------------------ |
| 71 | +// Global profiler configuration. |
| 72 | +// |
| 73 | +// Profiling is one-way: once enabled it cannot be disabled. |
| 74 | +// |
| 75 | +// `enabled` is _Atomic(bool) so that mi_profiler_enable() can be called |
| 76 | +// from any thread. The store uses release order; reads in the inline |
| 77 | +// fast-path hooks (in internal.h) use relaxed order (sufficient to decide |
| 78 | +// whether to do any work); the slow path uses acquire order to ensure |
| 79 | +// on_alloc, on_free, and record_extra_bytes are visible before they are read. |
| 80 | +// ------------------------------------------------------------------ |
| 81 | +typedef struct mi_profiler_s { |
| 82 | + _Atomic(bool) enabled; |
| 83 | + mi_profiler_alloc_cb on_alloc; // non-NULL when enabled=true |
| 84 | + mi_profiler_free_cb on_free; // may be NULL |
| 85 | + size_t record_extra_bytes; // bytes allocated after each mi_alloc_record_t for user_data |
| 86 | +} mi_profiler_t; |
| 87 | + |
| 88 | +extern mi_profiler_t _mi_profiler; |
| 89 | + |
| 90 | +// ------------------------------------------------------------------ |
| 91 | +// Public API — must be called at most once. May be called from any |
| 92 | +// thread, before or after other threads have started. Each thread |
| 93 | +// samples its first allocation immediately; the on_alloc callback |
| 94 | +// controls all subsequent thresholds. |
| 95 | +// ------------------------------------------------------------------ |
| 96 | +void mi_profiler_enable(size_t record_extra_bytes, mi_profiler_alloc_cb on_alloc, mi_profiler_free_cb on_free); |
| 97 | + |
| 98 | +// ------------------------------------------------------------------ |
| 99 | +// Slow-path implementations (defined in profile.c). |
| 100 | +// The inline fast-path wrappers are in internal.h so they have |
| 101 | +// access to the full type definitions they need. |
| 102 | +// ------------------------------------------------------------------ |
| 103 | +void _mi_profiler_on_alloc_slow(mi_heap_t* heap, mi_page_t* page, void* ptr, size_t size); |
| 104 | +void _mi_profiler_on_free_local_slow(mi_page_t* page, void* ptr); |
| 105 | +void _mi_profiler_on_free_collected_slow(mi_page_t* page, mi_block_t* head); |
| 106 | + |
| 107 | +#endif // MIMALLOC_PROFILE_H |
0 commit comments