asan_memory_profile.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. //===-- asan_memory_profile.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 AddressSanitizer, an address sanity checker.
  10. //
  11. // This file implements __sanitizer_print_memory_profile.
  12. //===----------------------------------------------------------------------===//
  13. #include "sanitizer_common/sanitizer_common.h"
  14. #include "sanitizer_common/sanitizer_stackdepot.h"
  15. #include "sanitizer_common/sanitizer_stacktrace.h"
  16. #include "sanitizer_common/sanitizer_stoptheworld.h"
  17. #include "lsan/lsan_common.h"
  18. #include "asan/asan_allocator.h"
  19. #if CAN_SANITIZE_LEAKS
  20. namespace __asan {
  21. struct AllocationSite {
  22. u32 id;
  23. uptr total_size;
  24. uptr count;
  25. };
  26. class HeapProfile {
  27. public:
  28. HeapProfile() { allocations_.reserve(1024); }
  29. void ProcessChunk(const AsanChunkView &cv) {
  30. if (cv.IsAllocated()) {
  31. total_allocated_user_size_ += cv.UsedSize();
  32. total_allocated_count_++;
  33. u32 id = cv.GetAllocStackId();
  34. if (id)
  35. Insert(id, cv.UsedSize());
  36. } else if (cv.IsQuarantined()) {
  37. total_quarantined_user_size_ += cv.UsedSize();
  38. total_quarantined_count_++;
  39. } else {
  40. total_other_count_++;
  41. }
  42. }
  43. void Print(uptr top_percent, uptr max_number_of_contexts) {
  44. Sort(allocations_.data(), allocations_.size(),
  45. [](const AllocationSite &a, const AllocationSite &b) {
  46. return a.total_size > b.total_size;
  47. });
  48. CHECK(total_allocated_user_size_);
  49. uptr total_shown = 0;
  50. Printf("Live Heap Allocations: %zd bytes in %zd chunks; quarantined: "
  51. "%zd bytes in %zd chunks; %zd other chunks; total chunks: %zd; "
  52. "showing top %zd%% (at most %zd unique contexts)\n",
  53. total_allocated_user_size_, total_allocated_count_,
  54. total_quarantined_user_size_, total_quarantined_count_,
  55. total_other_count_, total_allocated_count_ +
  56. total_quarantined_count_ + total_other_count_, top_percent,
  57. max_number_of_contexts);
  58. for (uptr i = 0; i < Min(allocations_.size(), max_number_of_contexts);
  59. i++) {
  60. auto &a = allocations_[i];
  61. Printf("%zd byte(s) (%zd%%) in %zd allocation(s)\n", a.total_size,
  62. a.total_size * 100 / total_allocated_user_size_, a.count);
  63. StackDepotGet(a.id).Print();
  64. total_shown += a.total_size;
  65. if (total_shown * 100 / total_allocated_user_size_ > top_percent)
  66. break;
  67. }
  68. }
  69. private:
  70. uptr total_allocated_user_size_ = 0;
  71. uptr total_allocated_count_ = 0;
  72. uptr total_quarantined_user_size_ = 0;
  73. uptr total_quarantined_count_ = 0;
  74. uptr total_other_count_ = 0;
  75. InternalMmapVector<AllocationSite> allocations_;
  76. void Insert(u32 id, uptr size) {
  77. // Linear lookup will be good enough for most cases (although not all).
  78. for (uptr i = 0; i < allocations_.size(); i++) {
  79. if (allocations_[i].id == id) {
  80. allocations_[i].total_size += size;
  81. allocations_[i].count++;
  82. return;
  83. }
  84. }
  85. allocations_.push_back({id, size, 1});
  86. }
  87. };
  88. static void ChunkCallback(uptr chunk, void *arg) {
  89. reinterpret_cast<HeapProfile*>(arg)->ProcessChunk(
  90. FindHeapChunkByAllocBeg(chunk));
  91. }
  92. static void MemoryProfileCB(const SuspendedThreadsList &suspended_threads_list,
  93. void *argument) {
  94. HeapProfile hp;
  95. __lsan::ForEachChunk(ChunkCallback, &hp);
  96. uptr *Arg = reinterpret_cast<uptr*>(argument);
  97. hp.Print(Arg[0], Arg[1]);
  98. if (Verbosity())
  99. __asan_print_accumulated_stats();
  100. }
  101. } // namespace __asan
  102. #endif // CAN_SANITIZE_LEAKS
  103. extern "C" {
  104. SANITIZER_INTERFACE_ATTRIBUTE
  105. void __sanitizer_print_memory_profile(uptr top_percent,
  106. uptr max_number_of_contexts) {
  107. #if CAN_SANITIZE_LEAKS
  108. uptr Arg[2];
  109. Arg[0] = top_percent;
  110. Arg[1] = max_number_of_contexts;
  111. __sanitizer::StopTheWorld(__asan::MemoryProfileCB, Arg);
  112. #endif // CAN_SANITIZE_LEAKS
  113. }
  114. } // extern "C"