memprof_stats.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. //===-- memprof_stats.cpp ------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file is a part of MemProfiler, a memory profiler.
  10. //
  11. // Code related to statistics collected by MemProfiler.
  12. //===----------------------------------------------------------------------===//
  13. #include "memprof_stats.h"
  14. #include "memprof_interceptors.h"
  15. #include "memprof_internal.h"
  16. #include "memprof_thread.h"
  17. #include "sanitizer_common/sanitizer_allocator_interface.h"
  18. #include "sanitizer_common/sanitizer_mutex.h"
  19. #include "sanitizer_common/sanitizer_stackdepot.h"
  20. namespace __memprof {
  21. MemprofStats::MemprofStats() { Clear(); }
  22. void MemprofStats::Clear() {
  23. if (REAL(memset))
  24. return (void)REAL(memset)(this, 0, sizeof(MemprofStats));
  25. internal_memset(this, 0, sizeof(MemprofStats));
  26. }
  27. static void PrintMallocStatsArray(const char *prefix,
  28. uptr (&array)[kNumberOfSizeClasses]) {
  29. Printf("%s", prefix);
  30. for (uptr i = 0; i < kNumberOfSizeClasses; i++) {
  31. if (!array[i])
  32. continue;
  33. Printf("%zu:%zu; ", i, array[i]);
  34. }
  35. Printf("\n");
  36. }
  37. void MemprofStats::Print() {
  38. Printf("Stats: %zuM malloced (%zuM for overhead) by %zu calls\n",
  39. malloced >> 20, malloced_overhead >> 20, mallocs);
  40. Printf("Stats: %zuM realloced by %zu calls\n", realloced >> 20, reallocs);
  41. Printf("Stats: %zuM freed by %zu calls\n", freed >> 20, frees);
  42. Printf("Stats: %zuM really freed by %zu calls\n", really_freed >> 20,
  43. real_frees);
  44. Printf("Stats: %zuM (%zuM-%zuM) mmaped; %zu maps, %zu unmaps\n",
  45. (mmaped - munmaped) >> 20, mmaped >> 20, munmaped >> 20, mmaps,
  46. munmaps);
  47. PrintMallocStatsArray(" mallocs by size class: ", malloced_by_size);
  48. Printf("Stats: malloc large: %zu\n", malloc_large);
  49. }
  50. void MemprofStats::MergeFrom(const MemprofStats *stats) {
  51. uptr *dst_ptr = reinterpret_cast<uptr *>(this);
  52. const uptr *src_ptr = reinterpret_cast<const uptr *>(stats);
  53. uptr num_fields = sizeof(*this) / sizeof(uptr);
  54. for (uptr i = 0; i < num_fields; i++)
  55. dst_ptr[i] += src_ptr[i];
  56. }
  57. static Mutex print_lock;
  58. static MemprofStats unknown_thread_stats(LINKER_INITIALIZED);
  59. static MemprofStats dead_threads_stats(LINKER_INITIALIZED);
  60. static Mutex dead_threads_stats_lock;
  61. // Required for malloc_zone_statistics() on OS X. This can't be stored in
  62. // per-thread MemprofStats.
  63. static uptr max_malloced_memory;
  64. static void MergeThreadStats(ThreadContextBase *tctx_base, void *arg) {
  65. MemprofStats *accumulated_stats = reinterpret_cast<MemprofStats *>(arg);
  66. MemprofThreadContext *tctx = static_cast<MemprofThreadContext *>(tctx_base);
  67. if (MemprofThread *t = tctx->thread)
  68. accumulated_stats->MergeFrom(&t->stats());
  69. }
  70. static void GetAccumulatedStats(MemprofStats *stats) {
  71. stats->Clear();
  72. {
  73. ThreadRegistryLock l(&memprofThreadRegistry());
  74. memprofThreadRegistry().RunCallbackForEachThreadLocked(MergeThreadStats,
  75. stats);
  76. }
  77. stats->MergeFrom(&unknown_thread_stats);
  78. {
  79. Lock lock(&dead_threads_stats_lock);
  80. stats->MergeFrom(&dead_threads_stats);
  81. }
  82. // This is not very accurate: we may miss allocation peaks that happen
  83. // between two updates of accumulated_stats_. For more accurate bookkeeping
  84. // the maximum should be updated on every malloc(), which is unacceptable.
  85. if (max_malloced_memory < stats->malloced) {
  86. max_malloced_memory = stats->malloced;
  87. }
  88. }
  89. void FlushToDeadThreadStats(MemprofStats *stats) {
  90. Lock lock(&dead_threads_stats_lock);
  91. dead_threads_stats.MergeFrom(stats);
  92. stats->Clear();
  93. }
  94. MemprofStats &GetCurrentThreadStats() {
  95. MemprofThread *t = GetCurrentThread();
  96. return (t) ? t->stats() : unknown_thread_stats;
  97. }
  98. static void PrintAccumulatedStats() {
  99. MemprofStats stats;
  100. GetAccumulatedStats(&stats);
  101. // Use lock to keep reports from mixing up.
  102. Lock lock(&print_lock);
  103. stats.Print();
  104. StackDepotStats stack_depot_stats = StackDepotGetStats();
  105. Printf("Stats: StackDepot: %zd ids; %zdM allocated\n",
  106. stack_depot_stats.n_uniq_ids, stack_depot_stats.allocated >> 20);
  107. PrintInternalAllocatorStats();
  108. }
  109. } // namespace __memprof
  110. // ---------------------- Interface ---------------- {{{1
  111. using namespace __memprof;
  112. uptr __sanitizer_get_current_allocated_bytes() {
  113. MemprofStats stats;
  114. GetAccumulatedStats(&stats);
  115. uptr malloced = stats.malloced;
  116. uptr freed = stats.freed;
  117. // Return sane value if malloced < freed due to racy
  118. // way we update accumulated stats.
  119. return (malloced > freed) ? malloced - freed : 1;
  120. }
  121. uptr __sanitizer_get_heap_size() {
  122. MemprofStats stats;
  123. GetAccumulatedStats(&stats);
  124. return stats.mmaped - stats.munmaped;
  125. }
  126. uptr __sanitizer_get_free_bytes() {
  127. MemprofStats stats;
  128. GetAccumulatedStats(&stats);
  129. uptr total_free = stats.mmaped - stats.munmaped + stats.really_freed;
  130. uptr total_used = stats.malloced;
  131. // Return sane value if total_free < total_used due to racy
  132. // way we update accumulated stats.
  133. return (total_free > total_used) ? total_free - total_used : 1;
  134. }
  135. uptr __sanitizer_get_unmapped_bytes() { return 0; }
  136. void __memprof_print_accumulated_stats() { PrintAccumulatedStats(); }