mimalloc-stats.h 4.0 KB

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