microsoft/qdk
Publicmirrored fromhttps://github.com/microsoft/qdkAvailable
source/allocator/mimalloc-sys/mimalloc/include/mimalloc-stats.h
103lines · modecode
| 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 1 // 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_COUNT(reset) /* reset bytes */ \ |
| 33 | MI_STAT_COUNT(purged) /* purged bytes */ \ |
| 34 | MI_STAT_COUNT(page_committed) /* committed memory inside pages */ \ |
| 35 | MI_STAT_COUNT(pages_abandoned) /* abandonded 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) /* searches for a fresh page */ \ |
| 56 | /* only on v1 and v2 */ \ |
| 57 | MI_STAT_COUNT(segments) \ |
| 58 | MI_STAT_COUNT(segments_abandoned) \ |
| 59 | MI_STAT_COUNT(segments_cache) \ |
| 60 | MI_STAT_COUNT(_segments_reserved) \ |
| 61 | /* only on v3 */ \ |
| 62 | MI_STAT_COUNTER(pages_reclaim_on_alloc) \ |
| 63 | MI_STAT_COUNTER(pages_reclaim_on_free) \ |
| 64 | MI_STAT_COUNTER(pages_reabandon_full) \ |
| 65 | MI_STAT_COUNTER(pages_unabandon_busy_wait) \ |
| 66 | |
| 67 | |
| 68 | // Define the statistics structure |
| 69 | #define MI_BIN_HUGE (73U) // see types.h |
| 70 | #define MI_STAT_COUNT(stat) mi_stat_count_t stat; |
| 71 | #define MI_STAT_COUNTER(stat) mi_stat_counter_t stat; |
| 72 | |
| 73 | typedef struct mi_stats_s |
| 74 | { |
| 75 | int version; |
| 76 | |
| 77 | MI_STAT_FIELDS() |
| 78 | |
| 79 | // future extension |
| 80 | mi_stat_count_t _stat_reserved[4]; |
| 81 | mi_stat_counter_t _stat_counter_reserved[4]; |
| 82 | |
| 83 | // size segregated statistics |
| 84 | mi_stat_count_t malloc_bins[MI_BIN_HUGE+1]; // allocation per size bin |
| 85 | mi_stat_count_t page_bins[MI_BIN_HUGE+1]; // pages allocated per size bin |
| 86 | } mi_stats_t; |
| 87 | |
| 88 | #undef MI_STAT_COUNT |
| 89 | #undef MI_STAT_COUNTER |
| 90 | |
| 91 | // Exported definitions |
| 92 | #ifdef __cplusplus |
| 93 | extern "C" { |
| 94 | #endif |
| 95 | |
| 96 | mi_decl_export void mi_stats_get( size_t stats_size, mi_stats_t* stats ) mi_attr_noexcept; |
| 97 | 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 |
| 98 | |
| 99 | #ifdef __cplusplus |
| 100 | } |
| 101 | #endif |
| 102 | |
| 103 | #endif // MIMALLOC_STATS_H |
| 104 | |