#include "mimalloc.h"
#include "mimalloc/prim/prim-tls.h"
#include "mimalloc/util/libc.h"
#include "mimalloc/util/options.h"
#include "mimalloc/util/stats.h"
#include "mimalloc/profile/pprof.h"
#include "mimalloc/heap/process.h"
typedef struct mi_location_s mi_location_t;
typedef struct mi_locations_s mi_locations_t;
typedef struct mi_callstack_s mi_callstack_t;
static bool mi_locations_init(mi_heap_t* heap, mi_locations_t* locations);
static void mi_locations_done(mi_heap_t* heap, mi_locations_t* locations);
static mi_location_t* mi_locations_find_or_insert(mi_heap_t* heap, mi_locations_t* locations, mi_thread_id_t thread_id, const mi_callstack_t* callstack, size_t callstack_hash);
#define MI_MAX_BACKTRACE_DEPTH (100)
#define MI_BACKTRACE_SKIP_FRAMES (3)
#ifndef MI_PROFILE_COUNTDOWN_THRESHOLD
#define MI_PROFILE_COUNTDOWN_THRESHOLD (8*MI_KiB)
#endif
#define MI_PROFILE_COUNTDOWN_MIN_INTERVAL (8*MI_PROFILE_COUNTDOWN_THRESHOLD)
struct mi_callstack_s {
size_t count;
void** frames;
};
struct mi_location_s {
_Atomic(struct mi_location_s*) next; mi_thread_id_t thread_id;
mi_callstack_t callstack;
size_t hash; int64_t alloc_count; int64_t alloc_bytes;
_Atomic(size_t) inuse_count; _Atomic(size_t) inuse_bytes; };
struct mi_locations_s {
size_t bucket_count; _Atomic(mi_location_t*)* buckets; _Atomic(size_t) count; };
typedef struct {
mi_profiler_t profiler; mi_heap_t* profile_heap; mi_locations_t locations; size_t sample_threshold; size_t snapshot_count; char* base_file_name; bool format_text; size_t alloc_interval_size; _Atomic(mi_ssize_t) alloc_interval_countdown; size_t inuse_interval_size; _Atomic(size_t) inuse_bytes; _Atomic(size_t) inuse_interval_threshold; size_t time_interval_secs; _Atomic(mi_msecs_t) time_interval_deadline; int64_t start_time_secs; mi_msecs_t start_clock; } mi_pprof_profiler_t;
static inline mi_pprof_profiler_t* downcast( mi_profiler_t* prof ) {
return (mi_pprof_profiler_t*)prof;
}
static void mi_pprof_profiler_snapshot(mi_pprof_profiler_t* prof);
static size_t mi_prim_backtrace(void** buffer, size_t max_depth, size_t* hash); static int64_t mi_prim_time_secs(void);
#ifndef MI_PROFILE_USE_BUILTIN
#define MI_PROFILE_USE_BUILTIN (MI_PROFILE)
#endif
#if MI_PROFILE_USE_BUILTIN
static bool mi_is_elevated_process(void);
void _mi_pprof_profiler_init(void) {
#if MI_PROFILE
if (mi_is_elevated_process()) return; char fname[1024];
if (_mi_getenv("mimalloc_profile", fname, sizeof(fname)) != 0 || fname[0] == 0) return; const size_t sample_rate = mi_option_get_size(mi_option_profile_sample_rate); const size_t alloc_interval_size = mi_option_get_size(mi_option_profile_alloc_interval); const size_t inuse_interval_size = mi_option_get_size(mi_option_profile_inuse_interval); const size_t time_interval_secs = mi_option_get_clamp(mi_option_profile_time_interval,0,24*60*60L); mi_profiler_t* profiler = mi_pprof_profiler_new(sample_rate, fname, alloc_interval_size, inuse_interval_size, time_interval_secs);
if (profiler == NULL) return;
_mi_process.profile_env_profiler = profiler;
_mi_verbose_message("pprof profiler initialized with base file name: %s\n", fname);
mi_profile(profiler); mi_profiler_start(profiler);
#endif
}
void _mi_pprof_profiler_done(void) {
#if MI_PROFILE
mi_profiler_t* profiler = _mi_process.profile_env_profiler;
if (profiler == NULL) return;
_mi_process.profile_env_profiler = NULL;
mi_profiler_stop(profiler);
_mi_verbose_message("pprof profiler done\n");
mi_profiler_snapshot(profiler); mi_profile(NULL); mi_pprof_profiler_delete(profiler);
#endif
}
#else
void _mi_pprof_profiler_init(void) {
}
void _mi_pprof_profiler_done(void) {
}
#endif
static mi_decl_noinline mi_location_t* mi_location_get(mi_pprof_profiler_t* prof) {
void* frames[MI_MAX_BACKTRACE_DEPTH + 1 + MI_BACKTRACE_SKIP_FRAMES]; size_t callstack_hash = 0;
const size_t depth = mi_prim_backtrace(frames, MI_MAX_BACKTRACE_DEPTH, &callstack_hash);
if (depth==0) return NULL;
mi_callstack_t callstack = { depth, frames };
const mi_thread_id_t thread_id = _mi_thread_id();
mi_location_t* loc = mi_locations_find_or_insert(prof->profile_heap, &prof->locations, thread_id, &callstack, callstack_hash);
return loc;
}
static size_t mi_exp_sample(mi_theap_t* theap, size_t scale) {
if (scale == 0) return 0; #if MI_PROFILE==2
if (scale == 1) return 1; #endif
const size_t r = _mi_theap_random_next(theap) | 1;
const size_t lz = mi_clz(r); const size_t frac_q8 = (lz >= MI_SIZE_BITS - 1 ? 0 : (r << (lz + 1)) >> (MI_SIZE_BITS - 8));
const size_t bits_q8 = (lz << 8) | frac_q8; const size_t ln_q8 = (bits_q8 * 177) >> 8; size_t next = ((ln_q8 * scale) >> 8) + 1; const size_t cap = scale * 32; return (next > cap ? cap : next);
}
static bool mi_interval_update(uint64_t ubytes_since_last_sample, size_t alloc_interval_size, _Atomic(mi_ssize_t)* alloc_interval_countdown, mi_ssize_t* interval_pending) {
mi_ssize_t sampled_bytes = 0;
mi_ssize_t bytes_since_last_sample= (ubytes_since_last_sample > MI_SSIZE_MAX ? MI_SSIZE_MAX : (mi_ssize_t)ubytes_since_last_sample);
if (alloc_interval_size < MI_PROFILE_COUNTDOWN_MIN_INTERVAL || MI_SSIZE_MAX - *interval_pending < bytes_since_last_sample ) {
sampled_bytes = bytes_since_last_sample;
}
else {
*interval_pending += bytes_since_last_sample;
if (*interval_pending >= (mi_ssize_t)MI_PROFILE_COUNTDOWN_THRESHOLD) {
sampled_bytes = *interval_pending;
*interval_pending = 0;
}
}
if (sampled_bytes > 0) {
const mi_ssize_t new_countdown = mi_atomic_addi_relaxed(alloc_interval_countdown, -sampled_bytes) - sampled_bytes;
if (new_countdown <= 0 && new_countdown + sampled_bytes > 0) {
mi_atomic_storess_relaxed(alloc_interval_countdown, (mi_ssize_t)alloc_interval_size);
return true;
}
}
return false;
}
static bool mi_inuse_threshold_update(size_t new_total, size_t inuse_interval_size, _Atomic(size_t)* inuse_interval_threshold) {
size_t threshold = mi_atomic_load_relaxed(inuse_interval_threshold);
while (new_total >= threshold) {
size_t next_threshold = threshold + inuse_interval_size;
while (next_threshold <= new_total) { next_threshold += inuse_interval_size; } if (mi_atomic_cas_weak_relaxed(inuse_interval_threshold, &threshold, next_threshold)) {
return true;
}
}
return false;
}
static bool mi_time_threshold_update(mi_msecs_t now, size_t time_interval_secs, _Atomic(mi_msecs_t)* time_interval_deadline) {
const mi_msecs_t interval_msecs = (mi_msecs_t)time_interval_secs * 1000;
mi_msecs_t deadline = mi_atomic_loadi64_relaxed(time_interval_deadline);
while (now >= deadline) {
mi_msecs_t next_deadline = deadline + interval_msecs;
while (next_deadline <= now) { next_deadline += interval_msecs; } if (mi_atomic_casi64_strong_acq_rel(time_interval_deadline, &deadline, next_deadline)) {
return true;
}
}
return false;
}
static size_t mi_cdecl on_alloc(mi_profiler_t* profiler, mi_profiler_sample_data_t* data, void* ptr, size_t requested_size, size_t threshold, uint64_t bytes_since_last_sample, mi_heap_t* heap)
{
MI_UNUSED(threshold); MI_UNUSED(ptr);
mi_pprof_profiler_t* prof = downcast(profiler);
mi_theap_t* theap = _mi_heap_theap(heap);
mi_location_t* loc = mi_location_get(prof); const size_t new_threshold = mi_exp_sample(theap, prof->sample_threshold); const size_t alloc_size = requested_size;
if (data!=NULL) {
mi_assert(data->user_data_size >= 2*sizeof(void*));
data->user_data[0] = loc;
data->user_data[1] = (void*)((uintptr_t)alloc_size);
}
if (loc!=NULL) {
loc->alloc_bytes += (int64_t)alloc_size;
loc->alloc_count += 1;
mi_atomic_add_relaxed(&loc->inuse_bytes, alloc_size);
mi_atomic_increment_relaxed(&loc->inuse_count);
}
if (prof->alloc_interval_size > 0) { mi_ssize_t* pinterval_pending = (mi_ssize_t*)&theap->profiler_reserved1;
if (mi_interval_update(bytes_since_last_sample, prof->alloc_interval_size, &prof->alloc_interval_countdown, pinterval_pending)) {
mi_profiler_snapshot(profiler);
}
}
if (prof->inuse_interval_size > 0) { const size_t total_inuse_bytes = mi_atomic_add_relaxed(&prof->inuse_bytes, alloc_size) + alloc_size;
if (mi_inuse_threshold_update(total_inuse_bytes, prof->inuse_interval_size, &prof->inuse_interval_threshold)) {
mi_profiler_snapshot(profiler);
}
}
if (prof->time_interval_secs > 0) { if (mi_time_threshold_update(_mi_clock_now(), prof->time_interval_secs, &prof->time_interval_deadline)) {
mi_profiler_snapshot(profiler);
}
}
return new_threshold;
}
static void mi_cdecl on_free(mi_profiler_t* profiler, mi_profiler_sample_data_t* data, void* ptr, mi_heap_t* heap)
{
MI_UNUSED(heap); MI_UNUSED(ptr);
mi_pprof_profiler_t* prof = downcast(profiler);
if (data!=NULL) {
mi_assert(data->user_data_size >= 2*sizeof(void*));
mi_location_t* loc = (mi_location_t*)data->user_data[0];
const size_t freed_bytes = (size_t)((uintptr_t)data->user_data[1]);
if (loc!=NULL) {
mi_atomic_sub_relaxed(&loc->inuse_bytes, freed_bytes);
mi_atomic_decrement_relaxed(&loc->inuse_count);
}
if (prof->inuse_interval_size > 0) { mi_atomic_sub_relaxed(&prof->inuse_bytes, freed_bytes);
}
}
}
static void mi_cdecl on_snapshot(mi_profiler_t* profiler) {
mi_pprof_profiler_t* prof = downcast(profiler);
mi_pprof_profiler_snapshot(prof);
}
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) {
mi_heap_t* heap = mi_heap_new();
if (heap == NULL) return NULL;
mi_heap_profile_disable(heap);
mi_pprof_profiler_t* prof = mi_heap_zalloc_tp(mi_pprof_profiler_t,heap);
if (prof == NULL) { mi_heap_delete(heap); return NULL; }
prof->sample_threshold = initial_threshold;
prof->profile_heap = heap;
prof->base_file_name = (base_file_name != NULL ? mi_heap_strndup(heap, base_file_name, 1024) : NULL);
prof->format_text = false; if (prof->base_file_name != NULL) {
const char* start = prof->base_file_name;
const char* sep = _mi_strrchr(start, '/');
if (sep != NULL) start = sep + 1;
sep = _mi_strrchr(start, '\\');
if (sep != NULL) start = sep + 1;
const char* dot = _mi_strrchr(start, '.');
if (dot != NULL) {
if (_mi_streq(dot + 1, "heap") || _mi_streq(dot + 1, "text")) {
prof->format_text = true;
}
prof->base_file_name[dot - prof->base_file_name] = 0; }
}
prof->alloc_interval_size = (alloc_interval_size > (size_t)MI_SSIZE_MAX ? (size_t)MI_SSIZE_MAX : alloc_interval_size); mi_atomic_storess_relaxed(&prof->alloc_interval_countdown, (mi_ssize_t)prof->alloc_interval_size); prof->inuse_interval_size = inuse_interval_size;
mi_atomic_store_relaxed(&prof->inuse_interval_threshold, prof->inuse_interval_size); prof->time_interval_secs = time_interval_secs;
prof->start_time_secs = mi_prim_time_secs();
prof->start_clock = _mi_clock_now();
mi_atomic_storei64_relaxed(&prof->time_interval_deadline, prof->start_clock + (mi_msecs_t)prof->time_interval_secs * 1000); if (!mi_locations_init(heap, &prof->locations)) {
mi_heap_destroy(heap);
return NULL;
}
prof->profiler.initial_sample_rate = initial_threshold;
prof->profiler.sample_data_size = 2*sizeof(void*); prof->profiler.on_alloc = &on_alloc;
prof->profiler.on_free = &on_free;
prof->profiler.on_snapshot = &on_snapshot;
return &prof->profiler;
}
void mi_pprof_profiler_delete(mi_profiler_t* profiler) {
if (profiler == NULL) return;
mi_pprof_profiler_t* prof = downcast(profiler);
mi_heap_t* heap = prof->profile_heap;
mi_locations_done(heap, &prof->locations);
mi_free(prof->base_file_name);
mi_free(prof);
mi_heap_delete(heap);
}
#define MI_LOCATIONS_BUCKET_COUNT (16 * 1024 - 3)
#if SIZE_MAX > UINT32_MAX
#define MI_FNV_OFFSET_BASIS ((size_t)0xcbf29ce484222325ULL)
#define MI_FNV_PRIME ((size_t)0x100000001b3ULL)
#else
#define MI_FNV_OFFSET_BASIS ((size_t)0x811c9dc5UL)
#define MI_FNV_PRIME ((size_t)0x01000193UL)
#endif
static size_t mi_location_hash(mi_thread_id_t thread_id, size_t callstack_hash) {
size_t hash = MI_FNV_OFFSET_BASIS;
const size_t prime = MI_FNV_PRIME;
hash = (hash ^ (size_t)thread_id) * prime;
hash = (hash ^ callstack_hash) * prime;
return hash;
}
static bool mi_locations_init(mi_heap_t* heap, mi_locations_t* locations) {
locations->bucket_count = MI_LOCATIONS_BUCKET_COUNT;
locations->buckets = (_Atomic(mi_location_t*)*)mi_heap_zalloc(heap, locations->bucket_count * sizeof(*locations->buckets));
mi_atomic_store_relaxed(&locations->count, (size_t)0);
return (locations->buckets != NULL);
}
static void mi_locations_done(mi_heap_t* heap, mi_locations_t* locations) {
MI_UNUSED(heap);
if (locations->buckets == NULL) return;
for (size_t i = 0; i < locations->bucket_count; i++) {
mi_location_t* loc = mi_atomic_load_ptr_relaxed(mi_location_t, &locations->buckets[i]);
while (loc != NULL) {
mi_location_t* next = mi_atomic_load_ptr_relaxed(mi_location_t, &loc->next);
if (loc->callstack.frames != NULL) {
mi_free(loc->callstack.frames);
}
mi_free(loc);
loc = next;
}
}
mi_free(locations->buckets);
locations->buckets = NULL;
mi_atomic_store_relaxed(&locations->count, (size_t)0);
}
static bool mi_location_matches(const mi_location_t* loc, size_t hash, mi_thread_id_t thread_id, const mi_callstack_t* callstack) {
return (loc->hash == hash && loc->thread_id == thread_id &&
loc->callstack.count == callstack->count &&
(callstack->count == 0 || mi_memcmp(loc->callstack.frames, callstack->frames, callstack->count * sizeof(void*)) == 0));
}
static mi_location_t* mi_locations_find_or_insert(mi_heap_t* heap, mi_locations_t* locations, mi_thread_id_t thread_id, const mi_callstack_t* callstack, size_t callstack_hash) {
if (locations->buckets == NULL) return NULL;
const size_t hash = mi_location_hash(thread_id, callstack_hash);
const size_t idx = hash % locations->bucket_count;
_Atomic(mi_location_t*)* bucket = &locations->buckets[idx];
for (mi_location_t* loc = mi_atomic_load_ptr_relaxed(mi_location_t, bucket); loc != NULL;
loc = mi_atomic_load_ptr_relaxed(mi_location_t, &loc->next))
{
if (mi_location_matches(loc, hash, thread_id, callstack)) return loc;
}
mi_location_t* loc = mi_heap_zalloc_tp(mi_location_t,heap);
if (loc == NULL) return NULL;
if (callstack->count > 0) {
loc->callstack.frames = (void**)mi_heap_mallocn(heap, callstack->count, sizeof(void*));
if (loc->callstack.frames == NULL) { mi_free(loc); return NULL; } mi_memcpy(loc->callstack.frames, callstack->frames, callstack->count * sizeof(void*));
}
loc->callstack.count = callstack->count;
loc->thread_id = thread_id;
loc->hash = hash;
mi_location_t* head = mi_atomic_load_ptr_relaxed(mi_location_t, bucket);
do {
mi_atomic_store_ptr_relaxed(mi_location_t, &loc->next, head); } while (!mi_atomic_cas_ptr_weak_release(mi_location_t, bucket, &head, loc)); mi_atomic_increment_relaxed(&locations->count); return loc;
}
#if !MI_PROFILE_USE_BUILTIN
static size_t mi_prim_backtrace(void** buffer, size_t max_depth, size_t* hash) {
MI_UNUSED(buffer);
MI_UNUSED(max_depth);
*hash = 0;
return 0;
}
static int64_t mi_prim_time_secs(void) {
return 0;
}
void mi_pprof_profiler_snapshot(mi_pprof_profiler_t* profiler) {
MI_UNUSED(profiler);
}
#else
#include <stdio.h>
#include <time.h>
#if defined(_WIN32)
#include <fcntl.h>
#include <io.h>
#include <share.h>
#include <sys/stat.h>
#elif MI_HAS_UNISTDH
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#endif
static void mi_pprof_write_mapped_libraries(FILE* f);
static bool mi_pprof_snapshot_text(mi_pprof_profiler_t* prof, const char* fname);
static bool mi_pprof_snapshot_proto(mi_pprof_profiler_t* prof, const char* fname);
#if defined(_WIN32)
static bool mi_is_elevated_process(void) {
return false; }
#elif MI_HAS_UNISTDH && (defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DragonFly__))
static bool mi_is_elevated_process(void) {
return (issetugid() != 0);
}
#elif MI_HAS_UNISTDH
static bool mi_is_elevated_process(void) {
return (getuid() != geteuid() || getgid() != getegid());
}
#else
static bool mi_is_elevated_process(void) {
return false; }
#endif
static int64_t mi_prim_time_secs(void) {
const time_t t = time(NULL);
return (t == (time_t)-1 ? 0 : (int64_t)t);
}
static FILE* mi_pprof_fopen(const char* fname, bool binary) {
#if defined(_WIN32)
int oflag = _O_CREAT | _O_TRUNC | _O_WRONLY | (binary ? _O_BINARY : _O_TEXT);
int fd = -1;
if (_sopen_s(&fd, fname, oflag, _SH_DENYNO, _S_IREAD | _S_IWRITE) != 0 || fd < 0) return NULL;
FILE* f = _fdopen(fd, binary ? "wb" : "w");
if (f == NULL) { _close(fd); }
return f;
#elif MI_HAS_UNISTDH
int oflag = O_CREAT | O_TRUNC | O_WRONLY;
#if defined(O_NOFOLLOW)
oflag |= O_NOFOLLOW; #endif
const int fd = open(fname, oflag, S_IRUSR | S_IWUSR); if (fd < 0) return NULL;
FILE* f = fdopen(fd, binary ? "wb" : "w");
if (f == NULL) { close(fd); }
return f;
#else
return fopen(fname, binary ? "wb" : "w"); #endif
}
static void mi_pprof_profiler_snapshot(mi_pprof_profiler_t* prof) {
if (prof == NULL) return;
bool was_running = mi_profiler_stop(&prof->profiler);
mi_locations_t* locations = &prof->locations;
if (locations->buckets != NULL && prof->base_file_name != NULL) {
const size_t seq = ++prof->snapshot_count;
char fname[1024];
bool ok;
if (!prof->format_text) {
_mi_snprintf(fname, sizeof(fname), "%s.%04zu.pb", prof->base_file_name, seq);
ok = mi_pprof_snapshot_proto(prof, fname);
}
else {
_mi_snprintf(fname, sizeof(fname), "%s.%04zu.heap", prof->base_file_name, seq);
ok = mi_pprof_snapshot_text(prof, fname);
}
if (ok) {
_mi_verbose_message("pprof profiler snapshot: %s\n", fname);
}
else {
_mi_verbose_message("pprof profiler: unable to write snapshot (out of memory?)\n");
}
}
if (was_running) { mi_profiler_start(&prof->profiler); }
}
#if MI_HAS_EXECINFOH || MI_HAS_UNWINDH
static size_t mi_backtrace_hash(void** frames, size_t depth) {
size_t hash = MI_FNV_OFFSET_BASIS;
for (size_t i = 0; i < depth; i++) {
hash = (hash ^ (size_t)((uintptr_t)frames[i])) * MI_FNV_PRIME;
}
return hash;
}
#endif
#if _WIN32
#include <windows.h>
static mi_decl_noinline size_t mi_prim_backtrace(void** buffer, size_t max_depth, size_t* hash) {
if (max_depth > ULONG_MAX) { max_depth = ULONG_MAX; }
const size_t skip = 1 + MI_BACKTRACE_SKIP_FRAMES; const size_t win_limit = 62; if (skip + max_depth >= win_limit) { max_depth = (skip <= win_limit ? win_limit - skip : 0); }
if (max_depth == 0) { *hash = 0; return 0; }
DWORD win_hash = 0;
const size_t depth = (size_t)CaptureStackBackTrace((ULONG)skip, (ULONG)max_depth, buffer, &win_hash);
*hash = (size_t)win_hash; return depth;
}
#elif MI_HAS_EXECINFOH
#include <execinfo.h>
static mi_decl_noinline size_t mi_prim_backtrace(void** buffer, size_t max_depth, size_t* hash) {
const size_t skip = 1 + MI_BACKTRACE_SKIP_FRAMES; if (max_depth <= skip) { *hash = 0; return 0; }
if (max_depth > INT_MAX) { max_depth = INT_MAX; }
const size_t raw_depth = (size_t)backtrace(buffer, (int)(max_depth - skip));
if (raw_depth <= skip) { *hash = 0; return 0; }
const size_t depth = raw_depth - skip;
memmove(buffer, buffer + skip, depth * sizeof(void*));
*hash = mi_backtrace_hash(buffer, depth); return depth;
}
#elif MI_HAS_UNWINDH
#include <unwind.h>
typedef struct mi_unwind_state_s {
void** buffer;
size_t max_depth;
size_t skip;
size_t depth;
} mi_unwind_state_t;
static _Unwind_Reason_Code mi_unwind_frame(struct _Unwind_Context* ctx, void* arg) {
mi_unwind_state_t* const st = (mi_unwind_state_t*)arg;
const uintptr_t ip = (uintptr_t)_Unwind_GetIP(ctx);
if (ip == 0) return _URC_END_OF_STACK;
if (st->skip > 0) { st->skip--; return _URC_NO_REASON; }
st->buffer[st->depth++] = (void*)ip;
return (st->depth >= st->max_depth ? _URC_END_OF_STACK : _URC_NO_REASON);
}
static mi_decl_noinline size_t mi_prim_backtrace(void** buffer, size_t max_depth, size_t* hash) {
mi_unwind_state_t st = { buffer, max_depth, 1 + MI_BACKTRACE_SKIP_FRAMES , 0 };
if (max_depth > 0) { _Unwind_Backtrace(&mi_unwind_frame, &st); }
*hash = mi_backtrace_hash(buffer, st.depth); return st.depth;
}
#else
static size_t mi_prim_backtrace(void** buffer, size_t max_depth, size_t* hash) {
MI_UNUSED(buffer);
MI_UNUSED(max_depth);
*hash = 0;
return 0;
}
#endif
typedef struct {
uintptr_t start;
uintptr_t end;
char* path; } mi_pprof_module_t;
typedef struct {
mi_pprof_module_t* modules;
size_t count;
size_t capacity;
bool failed; } mi_pprof_modules_t;
mi_decl_maybe_unused static void mi_pprof_modules_add(mi_heap_t* heap, mi_pprof_modules_t* mods, uintptr_t start, uintptr_t end, const char* path) {
if (mods->failed) return;
if (mods->count == mods->capacity) {
const size_t new_capacity = (mods->capacity == 0 ? 16 : mods->capacity * 2);
mi_pprof_module_t* const new_modules = (mi_pprof_module_t*)mi_heap_realloc(heap, mods->modules, new_capacity * sizeof(*mods->modules));
if (new_modules == NULL) { mods->failed = true; return; }
mods->modules = new_modules;
mods->capacity = new_capacity;
}
char* const copy = (path != NULL ? mi_heap_strndup(heap, path, 1024) : NULL);
if (path != NULL && copy == NULL) { mods->failed = true; return; }
mi_pprof_module_t* m = &mods->modules[mods->count++];
m->start = start;
m->end = end;
m->path = copy;
}
static void mi_pprof_modules_done(mi_pprof_modules_t* mods) {
for (size_t i = 0; i < mods->count; i++) { mi_free(mods->modules[i].path); }
mi_free(mods->modules);
mods->modules = NULL;
mods->count = 0;
mods->capacity = 0;
}
static void mi_fprintf(FILE* f, const char* fmt, ...) {
char buf[512];
va_list args;
va_start(args, fmt);
_mi_vsnprintf(buf, sizeof(buf), fmt, args);
va_end(args);
fputs(buf, f);
}
#if defined(_WIN32)
#include <windows.h>
static void mi_walk_modules_win32(void (*on_module)(void* ctx, uintptr_t base, uintptr_t end, const char* path), void* ctx) {
SYSTEM_INFO si;
GetSystemInfo(&si);
uint8_t* addr = (uint8_t*)si.lpMinimumApplicationAddress;
uint8_t* const addr_max = (uint8_t*)si.lpMaximumApplicationAddress;
void* cur_base = NULL;
uintptr_t cur_end = 0;
while (addr < addr_max) {
MEMORY_BASIC_INFORMATION mbi;
const SIZE_T n = VirtualQuery(addr, &mbi, sizeof(mbi));
if (n == 0) break; if (mbi.Type == MEM_IMAGE && mbi.State != MEM_FREE) {
if (mbi.AllocationBase == cur_base) {
cur_end = (uintptr_t)mbi.BaseAddress + mbi.RegionSize;
}
else {
if (cur_base != NULL) {
char path[MAX_PATH];
const DWORD len = GetModuleFileNameA((HMODULE)cur_base, path, (DWORD)sizeof(path));
on_module(ctx, (uintptr_t)cur_base, cur_end, (len > 0 ? path : ""));
}
cur_base = mbi.AllocationBase;
cur_end = (uintptr_t)mbi.BaseAddress + mbi.RegionSize;
}
}
else if (cur_base != NULL) {
char path[MAX_PATH];
const DWORD len = GetModuleFileNameA((HMODULE)cur_base, path, (DWORD)sizeof(path));
on_module(ctx, (uintptr_t)cur_base, cur_end, (len > 0 ? path : ""));
cur_base = NULL;
}
const uint8_t* next = (const uint8_t*)mbi.BaseAddress + mbi.RegionSize;
if (next <= addr) break; addr = (uint8_t*)next;
}
if (cur_base != NULL) {
char path[MAX_PATH];
const DWORD len = GetModuleFileNameA((HMODULE)cur_base, path, (DWORD)sizeof(path));
on_module(ctx, (uintptr_t)cur_base, cur_end, (len > 0 ? path : ""));
}
}
static void mi_write_mapped_libraries_on_module_win32(void* ctx, uintptr_t base, uintptr_t end, const char* path) {
FILE* f = (FILE*)ctx;
mi_fprintf(f, "%tx-%tx r-xp 00000000 00:00 0 %s\n", base, end, path);
}
static void mi_write_mapped_libraries_win32(FILE* f) {
mi_walk_modules_win32(&mi_write_mapped_libraries_on_module_win32, f);
}
typedef struct { mi_heap_t* heap; mi_pprof_modules_t* mods; } mi_pprof_collect_modules_win32_ctx_t;
static void mi_pprof_collect_modules_win32_on_module(void* ctx, uintptr_t base, uintptr_t end, const char* path) {
mi_pprof_collect_modules_win32_ctx_t* c = (mi_pprof_collect_modules_win32_ctx_t*)ctx;
mi_pprof_modules_add(c->heap, c->mods, base, end, (path[0] != 0 ? path : NULL));
}
static void mi_pprof_collect_modules_win32(mi_heap_t* heap, mi_pprof_modules_t* mods) {
mi_pprof_collect_modules_win32_ctx_t ctx = { heap, mods };
mi_walk_modules_win32(&mi_pprof_collect_modules_win32_on_module, &ctx);
}
#elif defined(__APPLE__)
#include <mach-o/dyld.h>
#include <mach-o/loader.h>
static bool mi_macho_image_range(const struct mach_header* mh, intptr_t slide, uintptr_t* start, uintptr_t* end) {
const bool is64 = (mh->magic == MH_MAGIC_64 || mh->magic == MH_CIGAM_64);
const uint8_t* cmd_ptr = (const uint8_t*)mh + (is64 ? sizeof(struct mach_header_64) : sizeof(struct mach_header));
for (uint32_t c = 0; c < mh->ncmds; c++) {
const struct load_command* lc = (const struct load_command*)cmd_ptr;
if (is64 && lc->cmd == LC_SEGMENT_64) {
const struct segment_command_64* seg = (const struct segment_command_64*)lc;
if (_mi_streq(seg->segname, SEG_TEXT)) {
*start = (uintptr_t)seg->vmaddr + (uintptr_t)slide;
*end = *start + (uintptr_t)seg->vmsize;
return true;
}
}
else if (!is64 && lc->cmd == LC_SEGMENT) {
const struct segment_command* seg = (const struct segment_command*)lc;
if (_mi_streq(seg->segname, SEG_TEXT)) {
*start = (uintptr_t)seg->vmaddr + (uintptr_t)slide;
*end = *start + (uintptr_t)seg->vmsize;
return true;
}
}
cmd_ptr += lc->cmdsize;
}
return false; }
static void mi_write_mapped_libraries_macos(FILE* f) {
const uint32_t count = _dyld_image_count();
for (uint32_t i = 0; i < count; i++) {
const struct mach_header* mh = _dyld_get_image_header(i);
const char* name = _dyld_get_image_name(i);
if (mh == NULL || name == NULL) continue;
const intptr_t slide = _dyld_get_image_vmaddr_slide(i);
uintptr_t start = 0;
uintptr_t end = 0;
if (!mi_macho_image_range(mh, slide, &start, &end)) continue;
mi_fprintf(f, "%tx-%tx r-xp 00000000 00:00 0 %s\n", start, end, name);
}
}
static void mi_pprof_collect_modules_macos(mi_heap_t* heap, mi_pprof_modules_t* mods) {
const uint32_t count = _dyld_image_count();
for (uint32_t i = 0; i < count; i++) {
const struct mach_header* mh = _dyld_get_image_header(i);
const char* name = _dyld_get_image_name(i);
if (mh == NULL || name == NULL) continue;
const intptr_t slide = _dyld_get_image_vmaddr_slide(i);
uintptr_t start = 0;
uintptr_t end = 0;
if (!mi_macho_image_range(mh, slide, &start, &end)) continue;
mi_pprof_modules_add(heap, mods, start, end, name);
}
}
#elif defined(__linux__)
#include <stdlib.h>
static void mi_write_mapped_libraries_linux(FILE* f) {
FILE* maps = fopen("/proc/self/maps", "r");
if (maps != NULL) {
char line[512];
while (fgets(line, sizeof(line), maps) != NULL) {
fputs(line, f);
}
fclose(maps);
}
}
static void mi_pprof_collect_modules_linux(mi_heap_t* heap, mi_pprof_modules_t* mods) {
FILE* maps = fopen("/proc/self/maps", "r");
if (maps == NULL) return;
char line[512];
while (fgets(line, sizeof(line), maps) != NULL) {
char* end_ptr = NULL;
const uintptr_t start = (uintptr_t)strtoull(line, &end_ptr, 16);
if (end_ptr == NULL || *end_ptr != '-') continue;
const uintptr_t end = (uintptr_t)strtoull(end_ptr + 1, &end_ptr, 16);
const char* path = strrchr(line, ' ');
if (path == NULL) continue;
while (*path == ' ') path++;
size_t len = strlen(path);
while (len > 0 && (path[len-1] == '\n' || path[len-1] == '\r')) { len--; }
if (len == 0 || path[0] == '[') continue; char pathbuf[512];
if (len >= sizeof(pathbuf)) { len = sizeof(pathbuf) - 1; }
memcpy(pathbuf, path, len);
pathbuf[len] = 0;
mi_pprof_modules_add(heap, mods, start, end, pathbuf);
}
fclose(maps);
}
#elif defined(__FreeBSD__) || defined(__DragonFly__)
#include <sys/user.h>
#include <libutil.h>
#include <unistd.h>
#include <stdlib.h>
static void mi_write_mapped_libraries_bsd(FILE* f) {
int cnt = 0;
struct kinfo_vmentry* vmmap = kinfo_getvmmap(getpid(), &cnt);
if (vmmap == NULL) return;
for (int i = 0; i < cnt; i++) {
const struct kinfo_vmentry* kve = &vmmap[i];
if (kve->kve_path[0] == 0) continue; mi_fprintf(f, "%l8x-%l8x r-xp 00000000 00:00 0 %s\n", (uint64_t)kve->kve_start, (uint64_t)kve->kve_end, kve->kve_path);
}
free(vmmap);
}
static void mi_pprof_collect_modules_bsd(mi_heap_t* heap, mi_pprof_modules_t* mods) {
int cnt = 0;
struct kinfo_vmentry* vmmap = kinfo_getvmmap(getpid(), &cnt);
if (vmmap == NULL) return;
for (int i = 0; i < cnt; i++) {
const struct kinfo_vmentry* kve = &vmmap[i];
if (kve->kve_path[0] == 0) continue; mi_pprof_modules_add(heap, mods, (uintptr_t)kve->kve_start, (uintptr_t)kve->kve_end, kve->kve_path);
}
free(vmmap);
}
#endif
static void mi_pprof_collect_modules(mi_heap_t* heap, mi_pprof_modules_t* mods) {
mods->modules = NULL;
mods->count = 0;
mods->capacity = 0;
mods->failed = false;
#if defined(__linux__)
mi_pprof_collect_modules_linux(heap, mods);
#elif defined(_WIN32)
mi_pprof_collect_modules_win32(heap, mods);
#elif defined(__APPLE__)
mi_pprof_collect_modules_macos(heap, mods);
#elif defined(__FreeBSD__) || defined(__DragonFly__)
mi_pprof_collect_modules_bsd(heap, mods);
#else
MI_UNUSED(heap);
#endif
}
static void mi_pprof_write_mapped_libraries(FILE* f) {
fprintf(f, "MAPPED_LIBRARIES:\n");
#if defined(__linux__)
mi_write_mapped_libraries_linux(f);
#elif defined(_WIN32)
mi_write_mapped_libraries_win32(f);
#elif defined(__APPLE__)
mi_write_mapped_libraries_macos(f);
#elif defined(__FreeBSD__) || defined(__DragonFly__)
mi_write_mapped_libraries_bsd(f);
#endif
}
static bool mi_pprof_snapshot_text(mi_pprof_profiler_t* prof, const char* fname) {
mi_locations_t* locations = &prof->locations;
FILE* f = mi_pprof_fopen(fname, false);
if (f == NULL) return false;
uint64_t total_inuse_objects = 0;
uint64_t total_inuse_bytes = 0;
uint64_t total_alloc_objects = 0;
uint64_t total_alloc_bytes = 0;
for (size_t i = 0; i < locations->bucket_count; i++) {
for (mi_location_t* loc = mi_atomic_load_ptr_relaxed(mi_location_t, &locations->buckets[i]); loc != NULL; loc = mi_atomic_load_ptr_relaxed(mi_location_t, &loc->next)) {
total_inuse_objects += (uint64_t)mi_atomic_load_relaxed(&loc->inuse_count);
total_inuse_bytes += (uint64_t)mi_atomic_load_relaxed(&loc->inuse_bytes);
total_alloc_objects += (uint64_t)loc->alloc_count;
total_alloc_bytes += (uint64_t)loc->alloc_bytes;
}
}
mi_fprintf(f, "heap profile: %6l8d: %8l8d [%6l8d: %8l8d] @ heapprofile\n",
total_inuse_objects, total_inuse_bytes, total_alloc_objects, total_alloc_bytes);
for (size_t i = 0; i < locations->bucket_count; i++) {
for (mi_location_t* loc = mi_atomic_load_ptr_relaxed(mi_location_t, &locations->buckets[i]); loc != NULL; loc = mi_atomic_load_ptr_relaxed(mi_location_t, &loc->next)) {
const size_t inuse_count = mi_atomic_load_relaxed(&loc->inuse_count);
const size_t inuse_bytes = mi_atomic_load_relaxed(&loc->inuse_bytes);
mi_fprintf(f, "%6l8d: %8l8d [%6l8d: %8l8d] @",
(uint64_t)inuse_count, (uint64_t)inuse_bytes,
(uint64_t)loc->alloc_count, (uint64_t)loc->alloc_bytes);
for (size_t j = 0; j < loc->callstack.count; j++) {
mi_fprintf(f, " 0x%tx", (size_t)(uintptr_t)loc->callstack.frames[j]);
}
fprintf(f, "\n");
}
}
mi_pprof_write_mapped_libraries(f);
const bool ok = (ferror(f) == 0);
if (fclose(f) != 0) return false;
return ok;
}
typedef struct {
mi_heap_t* heap;
uint8_t* data;
size_t size;
size_t capacity;
bool failed;
} mi_pb_buf_t;
static void mi_pb_init(mi_pb_buf_t* b, mi_heap_t* heap) {
b->heap = heap;
b->data = NULL;
b->size = 0;
b->capacity = 0;
b->failed = false;
}
static void mi_pb_done(mi_pb_buf_t* b) {
mi_free(b->data);
b->data = NULL;
b->size = 0;
b->capacity = 0;
b->failed = false;
}
static bool mi_pb_reserve(mi_pb_buf_t* b, size_t extra) {
if (b->failed) return false;
if (b->size + extra <= b->capacity) return true;
size_t new_capacity = (b->capacity == 0 ? 4096 : b->capacity * 2);
while (new_capacity < b->size + extra) { new_capacity *= 2; }
uint8_t* const new_data = (uint8_t*)mi_heap_realloc(b->heap, b->data, new_capacity);
if (new_data == NULL) { b->failed = true; return false; } b->data = new_data;
b->capacity = new_capacity;
return true;
}
static void mi_pb_bytes(mi_pb_buf_t* b, const void* p, size_t n) {
if (n == 0) return; if (!mi_pb_reserve(b, n)) return;
memcpy(b->data + b->size, p, n);
b->size += n;
}
static void mi_pb_byte(mi_pb_buf_t* b, uint8_t byte) {
mi_pb_bytes(b, &byte, 1);
}
static void mi_pb_varint(mi_pb_buf_t* b, uint64_t v) {
do {
uint8_t byte = (uint8_t)(v & 0x7F);
v >>= 7;
if (v != 0) { byte |= 0x80; }
mi_pb_byte(b, byte);
} while (v != 0);
}
static void mi_pb_tag(mi_pb_buf_t* b, uint32_t field, uint32_t wiretype) {
mi_pb_varint(b, ((uint64_t)field << 3) | wiretype);
}
static void mi_pb_uint64_field(mi_pb_buf_t* b, uint32_t field, uint64_t v) {
if (v == 0) return;
mi_pb_tag(b, field, 0);
mi_pb_varint(b, v);
}
static void mi_pb_int64_field(mi_pb_buf_t* b, uint32_t field, int64_t v) {
if (v == 0) return;
mi_pb_tag(b, field, 0);
mi_pb_varint(b, (uint64_t)v); }
static void mi_pb_len_field(mi_pb_buf_t* b, uint32_t field, const void* data, size_t len) {
mi_pb_tag(b, field, 2);
mi_pb_varint(b, len);
mi_pb_bytes(b, data, len);
}
static void mi_pb_message_field(mi_pb_buf_t* b, uint32_t field, const mi_pb_buf_t* msg) {
if (msg->failed) { b->failed = true; return; } mi_pb_len_field(b, field, msg->data, msg->size);
}
static void mi_pb_packed_uint64_field(mi_pb_buf_t* b, uint32_t field, const uint64_t* values, size_t count) {
if (count == 0) return;
mi_pb_buf_t tmp;
mi_pb_init(&tmp, b->heap);
for (size_t i = 0; i < count; i++) { mi_pb_varint(&tmp, values[i]); }
mi_pb_message_field(b, field, &tmp);
mi_pb_done(&tmp);
}
static void mi_pb_packed_int64_field(mi_pb_buf_t* b, uint32_t field, const int64_t* values, size_t count) {
mi_pb_packed_uint64_field(b, field, (const uint64_t*)values, count); }
typedef struct {
char* str;
int64_t index;
} mi_pb_string_entry_t;
typedef struct {
mi_heap_t* heap;
mi_pb_string_entry_t* entries;
size_t count;
size_t capacity;
bool failed; mi_pb_buf_t table; } mi_pb_strings_t;
static void mi_pb_strings_init(mi_pb_strings_t* strings, mi_heap_t* heap) {
strings->heap = heap;
strings->entries = NULL;
strings->count = 0;
strings->capacity = 0;
strings->failed = false;
mi_pb_init(&strings->table, heap);
mi_pb_len_field(&strings->table, 6, "", 0); }
static void mi_pb_strings_done(mi_pb_strings_t* strings) {
for (size_t i = 0; i < strings->count; i++) { mi_free(strings->entries[i].str); }
mi_free(strings->entries);
mi_pb_done(&strings->table);
}
static int64_t mi_pb_strings_intern(mi_pb_strings_t* strings, const char* s) {
if (strings->failed) return 0;
if (s == NULL) { s = ""; }
for (size_t i = 0; i < strings->count; i++) {
if (_mi_streq(strings->entries[i].str, s)) { return strings->entries[i].index; }
}
if (strings->count == strings->capacity) {
const size_t new_capacity = (strings->capacity == 0 ? 32 : strings->capacity * 2);
mi_pb_string_entry_t* const new_entries = (mi_pb_string_entry_t*)mi_heap_realloc(strings->heap, strings->entries, new_capacity * sizeof(*strings->entries));
if (new_entries == NULL) { strings->failed = true; return 0; }
strings->entries = new_entries;
strings->capacity = new_capacity;
}
char* const copy = mi_heap_strndup(strings->heap, s, 1024);
if (copy == NULL) { strings->failed = true; return 0; }
const int64_t index = (int64_t)(strings->count + 1); strings->entries[strings->count].str = copy;
strings->entries[strings->count].index = index;
strings->count++;
mi_pb_len_field(&strings->table, 6, s, strlen(s));
if (strings->table.failed) { strings->failed = true; return 0; }
return index;
}
typedef struct mi_pb_addr_entry_s {
struct mi_pb_addr_entry_s* next;
uintptr_t addr;
uint64_t id;
} mi_pb_addr_entry_t;
typedef struct {
mi_heap_t* heap;
size_t bucket_count;
mi_pb_addr_entry_t** buckets;
bool failed; } mi_pb_addr_map_t;
static void mi_pb_addr_map_init(mi_pb_addr_map_t* map, mi_heap_t* heap, size_t bucket_count) {
map->heap = heap;
map->bucket_count = (bucket_count == 0 ? 1 : bucket_count);
map->buckets = (mi_pb_addr_entry_t**)mi_heap_zalloc(heap, map->bucket_count * sizeof(*map->buckets));
map->failed = (map->buckets == NULL);
}
static void mi_pb_addr_map_done(mi_pb_addr_map_t* map) {
if (map->buckets == NULL) return;
for (size_t i = 0; i < map->bucket_count; i++) {
mi_pb_addr_entry_t* e = map->buckets[i];
while (e != NULL) {
mi_pb_addr_entry_t* const next = e->next;
mi_free(e);
e = next;
}
}
mi_free(map->buckets);
map->buckets = NULL;
}
static uint64_t mi_pb_addr_map_find_or_insert(mi_pb_addr_map_t* map, uintptr_t addr, uint64_t next_id, bool* is_new) {
if (map->failed || map->buckets == NULL) { map->failed = true; *is_new = false; return 0; }
const size_t idx = (size_t)(addr ^ (addr >> 16)) % map->bucket_count;
for (mi_pb_addr_entry_t* e = map->buckets[idx]; e != NULL; e = e->next) {
if (e->addr == addr) { *is_new = false; return e->id; }
}
mi_pb_addr_entry_t* e = mi_heap_malloc_tp(mi_pb_addr_entry_t, map->heap);
if (e == NULL) { map->failed = true; *is_new = false; return 0; }
e->addr = addr;
e->id = next_id;
e->next = map->buckets[idx];
map->buckets[idx] = e;
*is_new = true;
return next_id;
}
static uint64_t mi_pprof_find_mapping_id(const mi_pprof_modules_t* mods, uintptr_t addr) {
for (size_t i = 0; i < mods->count; i++) {
if (addr >= mods->modules[i].start && addr < mods->modules[i].end) { return (uint64_t)(i + 1); }
}
return 0;
}
static bool mi_pprof_build_proto(mi_pprof_profiler_t* prof, mi_pb_buf_t* profile, mi_pb_strings_t* strings,
mi_pprof_modules_t* mods, mi_pb_addr_map_t* addr_map)
{
mi_heap_t* heap = prof->profile_heap;
mi_locations_t* locations = &prof->locations;
const int64_t str_count = mi_pb_strings_intern(strings, "count");
const int64_t str_bytes = mi_pb_strings_intern(strings, "bytes");
const int64_t str_alloc_objects = mi_pb_strings_intern(strings, "alloc_objects");
const int64_t str_alloc_bytes = mi_pb_strings_intern(strings, "alloc_bytes");
const int64_t str_inuse_objects = mi_pb_strings_intern(strings, "inuse_objects");
const int64_t str_inuse_bytes = mi_pb_strings_intern(strings, "inuse_bytes");
const int64_t str_space = mi_pb_strings_intern(strings, "space");
const int64_t str_thread = mi_pb_strings_intern(strings, "thread");
if (strings->failed) return false;
{
const int64_t types[4] = { str_alloc_objects, str_alloc_bytes, str_inuse_objects, str_inuse_bytes };
const int64_t units[4] = { str_count, str_bytes, str_count, str_bytes };
for (size_t i = 0; i < 4; i++) {
mi_pb_buf_t vt;
mi_pb_init(&vt, heap);
mi_pb_int64_field(&vt, 1, types[i]);
mi_pb_int64_field(&vt, 2, units[i]);
mi_pb_message_field(profile, 1, &vt);
mi_pb_done(&vt);
}
}
mi_pprof_collect_modules(heap, mods);
if (mods->failed) return false;
for (size_t i = 0; i < mods->count; i++) {
const mi_pprof_module_t* m = &mods->modules[i];
mi_pb_buf_t mapping;
mi_pb_init(&mapping, heap);
mi_pb_uint64_field(&mapping, 1, (uint64_t)(i + 1)); mi_pb_uint64_field(&mapping, 2, (uint64_t)m->start); mi_pb_uint64_field(&mapping, 3, (uint64_t)m->end); if (m->path != NULL) {
mi_pb_int64_field(&mapping, 5, mi_pb_strings_intern(strings, m->path)); }
mi_pb_message_field(profile, 3, &mapping);
mi_pb_done(&mapping);
if (profile->failed || strings->failed) return false;
}
mi_pb_addr_map_init(addr_map, heap, locations->bucket_count);
if (addr_map->failed) return false;
uint64_t next_location_id = 1;
for (size_t i = 0; i < locations->bucket_count; i++) {
for (mi_location_t* loc = mi_atomic_load_ptr_relaxed(mi_location_t, &locations->buckets[i]); loc != NULL; loc = mi_atomic_load_ptr_relaxed(mi_location_t, &loc->next)) {
const size_t inuse_count = mi_atomic_load_relaxed(&loc->inuse_count);
const size_t inuse_bytes = mi_atomic_load_relaxed(&loc->inuse_bytes);
const size_t frame_count = (loc->callstack.count > MI_MAX_BACKTRACE_DEPTH ? MI_MAX_BACKTRACE_DEPTH : loc->callstack.count);
uint64_t location_ids[MI_MAX_BACKTRACE_DEPTH];
for (size_t j = 0; j < frame_count; j++) {
const uintptr_t addr = (uintptr_t)loc->callstack.frames[j];
bool is_new = false;
const uint64_t id = mi_pb_addr_map_find_or_insert(addr_map, addr, next_location_id, &is_new);
if (addr_map->failed) return false;
location_ids[j] = id;
if (is_new) {
next_location_id++;
mi_pb_buf_t location;
mi_pb_init(&location, heap);
mi_pb_uint64_field(&location, 1, id); mi_pb_uint64_field(&location, 2, mi_pprof_find_mapping_id(mods, addr)); mi_pb_uint64_field(&location, 3, (uint64_t)addr); mi_pb_message_field(profile, 4, &location);
mi_pb_done(&location);
}
}
mi_pb_buf_t sample;
mi_pb_init(&sample, heap);
mi_pb_packed_uint64_field(&sample, 1, location_ids, frame_count); const int64_t values[4] = {
loc->alloc_count, loc->alloc_bytes,
(int64_t)inuse_count, (int64_t)inuse_bytes
};
mi_pb_packed_int64_field(&sample, 2, values, 4); {
mi_pb_buf_t label;
mi_pb_init(&label, heap);
mi_pb_int64_field(&label, 1, str_thread); mi_pb_int64_field(&label, 3, (int64_t)loc->thread_id); mi_pb_message_field(&sample, 3, &label);
mi_pb_done(&label);
}
mi_pb_message_field(profile, 2, &sample);
mi_pb_done(&sample);
if (profile->failed) return false;
}
}
{
mi_pb_buf_t period_type;
mi_pb_init(&period_type, heap);
mi_pb_int64_field(&period_type, 1, str_space);
mi_pb_int64_field(&period_type, 2, str_bytes);
mi_pb_message_field(profile, 11, &period_type);
mi_pb_done(&period_type);
}
mi_pb_int64_field(profile, 12, (int64_t)prof->sample_threshold); const int64_t elapsed_nanos = (int64_t)(_mi_clock_now() - prof->start_clock) * 1000000LL; mi_pb_int64_field(profile, 9, prof->start_time_secs * 1000000000LL + elapsed_nanos); mi_pb_int64_field(profile, 10, elapsed_nanos);
if (strings->failed) return false;
mi_pb_bytes(profile, strings->table.data, strings->table.size);
if (profile->failed) return false;
return true;
}
static bool mi_pprof_write_file(const char* fname, const mi_pb_buf_t* profile) {
FILE* f = mi_pprof_fopen(fname, true);
if (f == NULL) return false;
bool ok = (fwrite(profile->data, 1, profile->size, f) == profile->size);
if (fclose(f) != 0) { ok = false; }
return ok;
}
static bool mi_pprof_snapshot_proto(mi_pprof_profiler_t* prof, const char* fname) {
mi_heap_t* heap = prof->profile_heap;
mi_pb_buf_t profile;
mi_pb_init(&profile, heap);
mi_pb_strings_t strings;
mi_pb_strings_init(&strings, heap);
mi_pprof_modules_t mods;
mods.modules = NULL;
mods.count = 0;
mods.capacity = 0;
mods.failed = false;
mi_pb_addr_map_t addr_map;
addr_map.heap = heap;
addr_map.bucket_count = 0;
addr_map.buckets = NULL;
addr_map.failed = false;
bool ok = mi_pprof_build_proto(prof, &profile, &strings, &mods, &addr_map);
mi_pb_addr_map_done(&addr_map);
mi_pprof_modules_done(&mods);
mi_pb_strings_done(&strings);
if (ok) { ok = mi_pprof_write_file(fname, &profile); }
mi_pb_done(&profile);
return ok;
}
#endif