mimalloc-stats.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
  2. /* ----------------------------------------------------------------------------
  3. Copyright (c) 2018-2025, Microsoft Research, Daan Leijen
  4. This is free software; you can redistribute it and/or modify it under the
  5. terms of the MIT license. A copy of the license can be found in the file
  6. "LICENSE" at the root of this distribution.
  7. -----------------------------------------------------------------------------*/
  8. #pragma once
  9. #ifndef MIMALLOC_STATS_H
  10. #define MIMALLOC_STATS_H
  11. #include <mimalloc.h>
  12. #include <stdint.h>
  13. #define MI_STAT_VERSION 3 // increased on every backward incompatible change
  14. // count allocation over time
  15. typedef struct mi_stat_count_s {
  16. int64_t total; // total allocated
  17. int64_t peak; // peak allocation
  18. int64_t current; // current allocation
  19. } mi_stat_count_t;
  20. // counters only increase
  21. typedef struct mi_stat_counter_s {
  22. int64_t total; // total count
  23. } mi_stat_counter_t;
  24. #define MI_STAT_FIELDS() \
  25. MI_STAT_COUNT(pages) /* count of mimalloc pages */ \
  26. MI_STAT_COUNT(reserved) /* reserved memory bytes */ \
  27. MI_STAT_COUNT(committed) /* committed bytes */ \
  28. MI_STAT_COUNTER(reset) /* reset bytes */ \
  29. MI_STAT_COUNTER(purged) /* purged bytes */ \
  30. MI_STAT_COUNT(page_committed) /* committed memory inside pages */ \
  31. MI_STAT_COUNT(pages_abandoned) /* abandonded pages count */ \
  32. MI_STAT_COUNT(threads) /* number of threads */ \
  33. MI_STAT_COUNT(malloc_normal) /* allocated bytes <= MI_LARGE_OBJ_SIZE_MAX */ \
  34. MI_STAT_COUNT(malloc_huge) /* allocated bytes in huge pages */ \
  35. MI_STAT_COUNT(malloc_requested) /* malloc requested bytes */ \
  36. \
  37. MI_STAT_COUNTER(mmap_calls) \
  38. MI_STAT_COUNTER(commit_calls) \
  39. MI_STAT_COUNTER(reset_calls) \
  40. MI_STAT_COUNTER(purge_calls) \
  41. MI_STAT_COUNTER(arena_count) /* number of memory arena's */ \
  42. MI_STAT_COUNTER(malloc_normal_count) /* number of blocks <= MI_LARGE_OBJ_SIZE_MAX */ \
  43. MI_STAT_COUNTER(malloc_huge_count) /* number of huge bloks */ \
  44. MI_STAT_COUNTER(malloc_guarded_count) /* number of allocations with guard pages */ \
  45. \
  46. /* internal statistics */ \
  47. MI_STAT_COUNTER(arena_rollback_count) \
  48. MI_STAT_COUNTER(arena_purges) \
  49. MI_STAT_COUNTER(pages_extended) /* number of page extensions */ \
  50. MI_STAT_COUNTER(pages_retire) /* number of pages that are retired */ \
  51. MI_STAT_COUNTER(page_searches) /* total pages searched for a fresh page */ \
  52. MI_STAT_COUNTER(page_searches_count) /* searched count for a fresh page */ \
  53. /* only on v1 and v2 */ \
  54. MI_STAT_COUNT(segments) \
  55. MI_STAT_COUNT(segments_abandoned) \
  56. MI_STAT_COUNT(segments_cache) \
  57. MI_STAT_COUNT(_segments_reserved) \
  58. /* only on v3 */ \
  59. MI_STAT_COUNTER(pages_reclaim_on_alloc) \
  60. MI_STAT_COUNTER(pages_reclaim_on_free) \
  61. MI_STAT_COUNTER(pages_reabandon_full) \
  62. MI_STAT_COUNTER(pages_unabandon_busy_wait) \
  63. // Define the statistics structure
  64. #define MI_BIN_HUGE (73U) // see types.h
  65. #define MI_STAT_COUNT(stat) mi_stat_count_t stat;
  66. #define MI_STAT_COUNTER(stat) mi_stat_counter_t stat;
  67. typedef struct mi_stats_s
  68. {
  69. int version;
  70. MI_STAT_FIELDS()
  71. // future extension
  72. mi_stat_count_t _stat_reserved[4];
  73. mi_stat_counter_t _stat_counter_reserved[4];
  74. // size segregated statistics
  75. mi_stat_count_t malloc_bins[MI_BIN_HUGE+1]; // allocation per size bin
  76. mi_stat_count_t page_bins[MI_BIN_HUGE+1]; // pages allocated per size bin
  77. } mi_stats_t;
  78. #undef MI_STAT_COUNT
  79. #undef MI_STAT_COUNTER
  80. // Exported definitions
  81. #ifdef __cplusplus
  82. extern "C" {
  83. #endif
  84. mi_decl_export void mi_stats_get( size_t stats_size, mi_stats_t* stats ) mi_attr_noexcept;
  85. 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
  86. #ifdef __cplusplus
  87. }
  88. #endif
  89. #endif // MIMALLOC_STATS_H
  90. #else
  91. #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
  92. #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)