asan_memory_profile.cpp 4.0 KB

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