1 //===-- asan_memory_profile.cc.cc -----------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is a part of AddressSanitizer, an address sanity checker.
12 // This file implements __sanitizer_print_memory_profile.
13 //===----------------------------------------------------------------------===//
15 #include "sanitizer_common/sanitizer_common.h"
16 #include "sanitizer_common/sanitizer_stackdepot.h"
17 #include "sanitizer_common/sanitizer_stacktrace.h"
18 #include "sanitizer_common/sanitizer_stoptheworld.h"
19 #include "lsan/lsan_common.h"
20 #include "asan/asan_allocator.h"
22 #if CAN_SANITIZE_LEAKS
26 struct AllocationSite {
34 HeapProfile() : allocations_(1024) {}
36 void ProcessChunk(const AsanChunkView& cv) {
37 if (cv.IsAllocated()) {
38 total_allocated_user_size_ += cv.UsedSize();
39 total_allocated_count_++;
40 u32 id = cv.GetAllocStackId();
42 Insert(id, cv.UsedSize());
43 } else if (cv.IsQuarantined()) {
44 total_quarantined_user_size_ += cv.UsedSize();
45 total_quarantined_count_++;
51 void Print(uptr top_percent) {
52 InternalSort(&allocations_, allocations_.size(),
53 [](const AllocationSite &a, const AllocationSite &b) {
54 return a.total_size > b.total_size;
56 CHECK(total_allocated_user_size_);
58 Printf("Live Heap Allocations: %zd bytes in %zd chunks; quarantined: "
59 "%zd bytes in %zd chunks; %zd other chunks; total chunks: %zd; "
60 "showing top %zd%%\n",
61 total_allocated_user_size_, total_allocated_count_,
62 total_quarantined_user_size_, total_quarantined_count_,
63 total_other_count_, total_allocated_count_ +
64 total_quarantined_count_ + total_other_count_, top_percent);
65 for (uptr i = 0; i < allocations_.size(); i++) {
66 auto &a = allocations_[i];
67 Printf("%zd byte(s) (%zd%%) in %zd allocation(s)\n", a.total_size,
68 a.total_size * 100 / total_allocated_user_size_, a.count);
69 StackDepotGet(a.id).Print();
70 total_shown += a.total_size;
71 if (total_shown * 100 / total_allocated_user_size_ > top_percent)
77 uptr total_allocated_user_size_ = 0;
78 uptr total_allocated_count_ = 0;
79 uptr total_quarantined_user_size_ = 0;
80 uptr total_quarantined_count_ = 0;
81 uptr total_other_count_ = 0;
82 InternalMmapVector<AllocationSite> allocations_;
84 void Insert(u32 id, uptr size) {
85 // Linear lookup will be good enough for most cases (although not all).
86 for (uptr i = 0; i < allocations_.size(); i++) {
87 if (allocations_[i].id == id) {
88 allocations_[i].total_size += size;
89 allocations_[i].count++;
93 allocations_.push_back({id, size, 1});
97 static void ChunkCallback(uptr chunk, void *arg) {
98 reinterpret_cast<HeapProfile*>(arg)->ProcessChunk(
99 FindHeapChunkByAllocBeg(chunk));
102 static void MemoryProfileCB(const SuspendedThreadsList &suspended_threads_list,
105 __lsan::ForEachChunk(ChunkCallback, &hp);
106 hp.Print(reinterpret_cast<uptr>(argument));
109 } // namespace __asan
112 SANITIZER_INTERFACE_ATTRIBUTE
113 void __sanitizer_print_memory_profile(uptr top_percent) {
114 __sanitizer::StopTheWorld(__asan::MemoryProfileCB, (void*)top_percent);
118 #endif // CAN_SANITIZE_LEAKS