#pragma once
#ifndef MI_PPROF_H
#define MI_PPROF_H
#include "mimalloc.h"
#include "mimalloc/profile/pprof.h"
#ifndef MI_PROFILE_USE_BUILTIN
#define MI_PROFILE_USE_BUILTIN (MI_PROFILE)
#endif
typedef struct mi_location_s mi_location_t;
typedef struct mi_locations_s mi_locations_t;
typedef struct mi_callstack_s mi_callstack_t;
#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;
#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
size_t _mi_pprof_backtrace(void** buffer, size_t max_depth, size_t* hash); int64_t _mi_pprof_time_secs(void);
void _mi_pprof_profiler_snapshot(mi_pprof_profiler_t* prof);
#if MI_PROFILE_USE_BUILTIN
typedef struct {
uintptr_t start;
uintptr_t end;
uint64_t file_offset;
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;
typedef struct mi_pprof_file_s mi_pprof_file_t; mi_pprof_file_t* _mi_pprof_fopen(mi_heap_t* heap, const char* fname, bool binary);
void _mi_pprof_fwrite(mi_pprof_file_t* f, const void* data, size_t size);
void _mi_pprof_fputs(mi_pprof_file_t* f, const char* s);
void _mi_pprof_fprintf(mi_pprof_file_t* f, const char* fmt, ...);
bool _mi_pprof_fclose(mi_pprof_file_t* f); void _mi_pprof_collect_modules(mi_heap_t* heap, mi_pprof_modules_t* mods);
void _mi_pprof_modules_done(mi_pprof_modules_t* mods);
void _mi_pprof_write_mapped_libraries(mi_pprof_file_t* f);
#endif
#endif