mimalloc-stats.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 1 // 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_COUNT(reset) /* reset bytes */ \
  29. MI_STAT_COUNT(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) /* searches for a fresh page */ \
  52. /* only on v1 and v2 */ \
  53. MI_STAT_COUNT(segments) \
  54. MI_STAT_COUNT(segments_abandoned) \
  55. MI_STAT_COUNT(segments_cache) \
  56. MI_STAT_COUNT(_segments_reserved) \
  57. /* only on v3 */ \
  58. MI_STAT_COUNTER(pages_reclaim_on_alloc) \
  59. MI_STAT_COUNTER(pages_reclaim_on_free) \
  60. MI_STAT_COUNTER(pages_reabandon_full) \
  61. MI_STAT_COUNTER(pages_unabandon_busy_wait) \
  62. // Define the statistics structure
  63. #define MI_BIN_HUGE (73U) // see types.h
  64. #define MI_STAT_COUNT(stat) mi_stat_count_t stat;
  65. #define MI_STAT_COUNTER(stat) mi_stat_counter_t stat;
  66. typedef struct mi_stats_s
  67. {
  68. int version;
  69. MI_STAT_FIELDS()
  70. // future extension
  71. mi_stat_count_t _stat_reserved[4];
  72. mi_stat_counter_t _stat_counter_reserved[4];
  73. // size segregated statistics
  74. mi_stat_count_t malloc_bins[MI_BIN_HUGE+1]; // allocation per size bin
  75. mi_stat_count_t page_bins[MI_BIN_HUGE+1]; // pages allocated per size bin
  76. } mi_stats_t;
  77. #undef MI_STAT_COUNT
  78. #undef MI_STAT_COUNTER
  79. // Exported definitions
  80. #ifdef __cplusplus
  81. extern "C" {
  82. #endif
  83. mi_decl_export void mi_stats_get( size_t stats_size, mi_stats_t* stats ) mi_attr_noexcept;
  84. 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
  85. #ifdef __cplusplus
  86. }
  87. #endif
  88. #endif // MIMALLOC_STATS_H
  89. #else
  90. #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
  91. #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)