|
| 1 | +/* ---------------------------------------------------------------------------- |
| 2 | +Copyright (c) 2018-2025, 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_STATS_H |
| 9 | +#define MIMALLOC_STATS_H |
| 10 | + |
| 11 | +#include <mimalloc.h> |
| 12 | +#include <stdint.h> |
| 13 | + |
| 14 | +#define MI_STAT_VERSION 4 // increased on every backward incompatible change |
| 15 | + |
| 16 | +// count allocation over time |
| 17 | +typedef struct mi_stat_count_s { |
| 18 | + int64_t total; // total allocated |
| 19 | + int64_t peak; // peak allocation |
| 20 | + int64_t current; // current allocation |
| 21 | +} mi_stat_count_t; |
| 22 | + |
| 23 | +// counters only increase |
| 24 | +typedef struct mi_stat_counter_s { |
| 25 | + int64_t total; // total count |
| 26 | +} mi_stat_counter_t; |
| 27 | + |
| 28 | +#define MI_STAT_FIELDS() \ |
| 29 | + MI_STAT_COUNT(pages) /* count of mimalloc pages */ \ |
| 30 | + MI_STAT_COUNT(reserved) /* reserved memory bytes */ \ |
| 31 | + MI_STAT_COUNT(committed) /* committed bytes */ \ |
| 32 | + MI_STAT_COUNTER(reset) /* reset bytes */ \ |
| 33 | + MI_STAT_COUNTER(purged) /* purged bytes */ \ |
| 34 | + MI_STAT_COUNT(page_committed) /* committed memory inside pages */ \ |
| 35 | + MI_STAT_COUNT(pages_abandoned) /* abandoned pages count */ \ |
| 36 | + MI_STAT_COUNT(threads) /* number of threads */ \ |
| 37 | + MI_STAT_COUNT(malloc_normal) /* allocated bytes <= MI_LARGE_OBJ_SIZE_MAX */ \ |
| 38 | + MI_STAT_COUNT(malloc_huge) /* allocated bytes in huge pages */ \ |
| 39 | + MI_STAT_COUNT(malloc_requested) /* malloc requested bytes */ \ |
| 40 | + \ |
| 41 | + MI_STAT_COUNTER(mmap_calls) \ |
| 42 | + MI_STAT_COUNTER(commit_calls) \ |
| 43 | + MI_STAT_COUNTER(reset_calls) \ |
| 44 | + MI_STAT_COUNTER(purge_calls) \ |
| 45 | + MI_STAT_COUNTER(arena_count) /* number of memory arena's */ \ |
| 46 | + MI_STAT_COUNTER(malloc_normal_count) /* number of blocks <= MI_LARGE_OBJ_SIZE_MAX */ \ |
| 47 | + MI_STAT_COUNTER(malloc_huge_count) /* number of huge bloks */ \ |
| 48 | + MI_STAT_COUNTER(malloc_guarded_count) /* number of allocations with guard pages */ \ |
| 49 | + \ |
| 50 | + /* internal statistics */ \ |
| 51 | + MI_STAT_COUNTER(arena_rollback_count) \ |
| 52 | + MI_STAT_COUNTER(arena_purges) \ |
| 53 | + MI_STAT_COUNTER(pages_extended) /* number of page extensions */ \ |
| 54 | + MI_STAT_COUNTER(pages_retire) /* number of pages that are retired */ \ |
| 55 | + MI_STAT_COUNTER(page_searches) /* total pages searched for a fresh page */ \ |
| 56 | + MI_STAT_COUNTER(page_searches_count) /* searched count for a fresh page */ \ |
| 57 | + /* only on v1 and v2 */ \ |
| 58 | + MI_STAT_COUNT(segments) \ |
| 59 | + MI_STAT_COUNT(segments_abandoned) \ |
| 60 | + MI_STAT_COUNT(segments_cache) \ |
| 61 | + MI_STAT_COUNT(_segments_reserved) \ |
| 62 | + /* only on v3 */ \ |
| 63 | + MI_STAT_COUNTER(pages_reclaim_on_alloc) \ |
| 64 | + MI_STAT_COUNTER(pages_reclaim_on_free) \ |
| 65 | + MI_STAT_COUNTER(pages_reabandon_full) \ |
| 66 | + MI_STAT_COUNTER(pages_unabandon_busy_wait) \ |
| 67 | + |
| 68 | + |
| 69 | +// Define the statistics structure |
| 70 | +#define MI_BIN_HUGE (73U) // see types.h |
| 71 | +#define MI_STAT_COUNT(stat) mi_stat_count_t stat; |
| 72 | +#define MI_STAT_COUNTER(stat) mi_stat_counter_t stat; |
| 73 | + |
| 74 | +typedef struct mi_stats_s |
| 75 | +{ |
| 76 | + size_t size; // size of the mi_stats_t structure |
| 77 | + size_t version; |
| 78 | + |
| 79 | + MI_STAT_FIELDS() |
| 80 | + |
| 81 | + // future extension |
| 82 | + mi_stat_count_t _stat_reserved[4]; |
| 83 | + mi_stat_counter_t _stat_counter_reserved[4]; |
| 84 | + |
| 85 | + // size segregated statistics |
| 86 | + mi_stat_count_t malloc_bins[MI_BIN_HUGE+1]; // allocation per size bin |
| 87 | + mi_stat_count_t page_bins[MI_BIN_HUGE+1]; // pages allocated per size bin |
| 88 | +} mi_stats_t; |
| 89 | + |
| 90 | +#undef MI_STAT_COUNT |
| 91 | +#undef MI_STAT_COUNTER |
| 92 | + |
| 93 | +// helper |
| 94 | +#define mi_stats_t_decl(name) mi_stats_t name = { 0 }; name.size = sizeof(mi_stats_t); name.version = MI_STAT_VERSION; |
| 95 | + |
| 96 | +// Exported definitions |
| 97 | +#ifdef __cplusplus |
| 98 | +extern "C" { |
| 99 | +#endif |
| 100 | + |
| 101 | +mi_decl_export bool mi_stats_get( mi_stats_t* stats ) mi_attr_noexcept; |
| 102 | +mi_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 == NULL |
| 103 | + |
| 104 | +#ifdef __cplusplus |
| 105 | +} |
| 106 | +#endif |
| 107 | + |
| 108 | +#endif // MIMALLOC_STATS_H |
0 commit comments