Path: blob/main/system/lib/mimalloc/include/mimalloc-stats.h
14369 views
/* ----------------------------------------------------------------------------1Copyright (c) 2024-2025, Microsoft Research, Daan Leijen2This is free software; you can redistribute it and/or modify it under the3terms of the MIT license. A copy of the license can be found in the file4"LICENSE" at the root of this distribution.5-----------------------------------------------------------------------------*/6#pragma once7#ifndef MIMALLOC_STATS_H8#define MIMALLOC_STATS_H910#include <mimalloc.h>11#include <stdint.h>1213#define MI_STAT_VERSION 5 // increased on every backward incompatible change1415// alignment for atomic fields16#if defined(_MSC_VER)17#define mi_decl_align(a) __declspec(align(a))18#elif defined(__GNUC__)19#define mi_decl_align(a) __attribute__((aligned(a)))20#elif __cplusplus >= 201103L21#define mi_decl_align(a) alignas(a)22#else23#define mi_decl_align(a)24#endif252627// count allocation over time28typedef struct mi_stat_count_s {29int64_t total; // total allocated30int64_t peak; // peak allocation31int64_t current; // current allocation32} mi_stat_count_t;3334// counters only increase35typedef struct mi_stat_counter_s {36int64_t total; // total count37} mi_stat_counter_t;3839#define MI_STAT_FIELDS() \40MI_STAT_COUNT(pages) /* count of mimalloc pages */ \41MI_STAT_COUNT(reserved) /* reserved memory bytes */ \42MI_STAT_COUNT(committed) /* committed bytes */ \43MI_STAT_COUNTER(reset) /* reset bytes */ \44MI_STAT_COUNTER(purged) /* purged bytes */ \45MI_STAT_COUNT(page_committed) /* committed memory inside pages */ \46MI_STAT_COUNT(pages_abandoned) /* abandoned pages count */ \47MI_STAT_COUNT(threads) /* number of threads */ \48MI_STAT_COUNT(malloc_normal) /* allocated bytes <= MI_LARGE_OBJ_SIZE_MAX */ \49MI_STAT_COUNT(malloc_huge) /* allocated bytes in huge pages */ \50MI_STAT_COUNT(malloc_requested) /* malloc requested bytes */ \51\52MI_STAT_COUNTER(mmap_calls) \53MI_STAT_COUNTER(commit_calls) \54MI_STAT_COUNTER(reset_calls) \55MI_STAT_COUNTER(purge_calls) \56MI_STAT_COUNTER(arena_count) /* number of memory arena's */ \57MI_STAT_COUNTER(malloc_normal_count) /* number of blocks <= MI_LARGE_OBJ_SIZE_MAX */ \58MI_STAT_COUNTER(malloc_huge_count) /* number of huge bloks */ \59MI_STAT_COUNTER(malloc_guarded_count) /* number of allocations with guard pages */ \60\61/* internal statistics */ \62MI_STAT_COUNTER(arena_rollback_count) \63MI_STAT_COUNTER(arena_purges) \64MI_STAT_COUNTER(pages_extended) /* number of page extensions */ \65MI_STAT_COUNTER(pages_retire) /* number of pages that are retired */ \66MI_STAT_COUNTER(page_searches) /* total pages searched for a fresh page */ \67MI_STAT_COUNTER(page_searches_count) /* searched count for a fresh page */ \68/* only on v1 and v2 */ \69MI_STAT_COUNT(segments) \70MI_STAT_COUNT(segments_abandoned) \71MI_STAT_COUNT(segments_cache) \72MI_STAT_COUNT(_segments_reserved) \73/* only on v3 */ \74MI_STAT_COUNT(heaps) \75MI_STAT_COUNT(theaps) \76MI_STAT_COUNTER(pages_reclaim_on_alloc) \77MI_STAT_COUNTER(pages_reclaim_on_free) \78MI_STAT_COUNTER(pages_reabandon_full) \79MI_STAT_COUNTER(pages_unabandon_busy_wait) \80MI_STAT_COUNTER(heaps_delete_wait)8182// Size bins for chunks83typedef enum mi_chunkbin_e {84MI_CBIN_SMALL, // slice_count == 185MI_CBIN_OTHER, // slice_count: any other from the other bins, and 1 <= slice_count <= MI_BCHUNK_BITS86MI_CBIN_MEDIUM, // slice_count == 887MI_CBIN_LARGE, // slice_count == MI_SIZE_BITS (only used if MI_ENABLE_LARGE_PAGES is 1)88MI_CBIN_HUGE, // slice_count > MI_BCHUNK_BITS89MI_CBIN_NONE, // no bin assigned yet (the chunk is completely free)90MI_CBIN_COUNT91} mi_chunkbin_t;929394// Define the statistics structure95#define MI_BIN_HUGE (73U) // see types.h96#define MI_STAT_COUNT(stat) mi_stat_count_t stat;97#define MI_STAT_COUNTER(stat) mi_stat_counter_t stat;9899typedef struct mi_stats_s100{101size_t size; // size of the mi_stats_t structure102size_t version;103104mi_decl_align(8) MI_STAT_FIELDS()105106// future extension107mi_stat_count_t _stat_reserved[4];108mi_stat_counter_t _stat_counter_reserved[4];109110// size segregated statistics111mi_stat_count_t malloc_bins[MI_BIN_HUGE+1]; // allocation per size bin112mi_stat_count_t page_bins[MI_BIN_HUGE+1]; // pages allocated per size bin113mi_stat_count_t chunk_bins[MI_CBIN_COUNT]; // chunks per page sizes114} mi_stats_t;115116#undef MI_STAT_COUNT117#undef MI_STAT_COUNTER118119// helper120#if __cplusplus121#define MI_STATS_ZERO_INIT { } /* empty initializer to prevent running the constructor (with msvc) */122#else123#define MI_STATS_ZERO_INIT { 0 } /* C zero initialize */124#endif125126#define mi_stats_t_decl(name) mi_stats_t name = MI_STATS_ZERO_INIT; name.size = sizeof(mi_stats_t); name.version = MI_STAT_VERSION;127128// Exported definitions129#ifdef __cplusplus130extern "C" {131#endif132133// stats from a heap134mi_decl_export bool mi_heap_stats_get(mi_heap_t* heap, mi_stats_t* stats) mi_attr_noexcept;135mi_decl_export char* mi_heap_stats_get_json(mi_heap_t* heap, size_t buf_size, char* buf) mi_attr_noexcept; // use mi_free to free the result if the input buf == NULL136mi_decl_export void mi_heap_stats_print_out(mi_heap_t* heap, mi_output_fun* out, void* arg) mi_attr_noexcept;137138// stats from a subprocess and its heaps aggregated139mi_decl_export bool mi_subproc_stats_get(mi_subproc_id_t subproc_id, mi_stats_t* stats) mi_attr_noexcept;140mi_decl_export char* mi_subproc_stats_get_json(mi_subproc_id_t subproc_id, size_t buf_size, char* buf) mi_attr_noexcept; // use mi_free to free the result if the input buf == NULL141mi_decl_export void mi_subproc_stats_print_out(mi_subproc_id_t subproc_id, mi_output_fun* out, void* arg) mi_attr_noexcept;142// print subprocess and all its heap stats segregated143mi_decl_export void mi_subproc_heap_stats_print_out(mi_subproc_id_t subproc_id, mi_output_fun* out, void* arg) mi_attr_noexcept;144145// stats aggregated for the current subprocess and all its heaps.146mi_decl_export bool mi_stats_get(mi_stats_t* stats) mi_attr_noexcept;147mi_decl_export char* mi_stats_get_json(size_t buf_size, char* buf) mi_attr_noexcept; // use mi_free to free the result if the input buf == NULL148mi_decl_export void mi_stats_print_out(mi_output_fun* out, void* arg) mi_attr_noexcept;149150// add the stats of the heap to the subprocess and clear the heap stats151mi_decl_export void mi_heap_stats_merge_to_subproc(mi_heap_t* heap);152153// stats from the subprocess without aggregating its heaps154mi_decl_export bool mi_subproc_stats_get_exclusive(mi_subproc_id_t subproc_id, mi_stats_t* stats) mi_attr_noexcept;155156mi_decl_export char* mi_stats_as_json(mi_stats_t* stats, size_t buf_size, char* buf) mi_attr_noexcept; // use mi_free to free the result if the input buf == NULL157mi_decl_export size_t mi_stats_get_bin_size(size_t bin) mi_attr_noexcept;158159#ifdef __cplusplus160}161#endif162163#endif // MIMALLOC_STATS_H164165166