]>
Commit | Line | Data |
---|---|---|
10189819 MO |
1 | //===-- asan_memory_profile.cc.cc -----------------------------------------===// |
2 | // | |
3 | // This file is distributed under the University of Illinois Open Source | |
4 | // License. See LICENSE.TXT for details. | |
5 | // | |
6 | //===----------------------------------------------------------------------===// | |
7 | // | |
8 | // This file is a part of AddressSanitizer, an address sanity checker. | |
9 | // | |
10 | // This file implements __sanitizer_print_memory_profile. | |
11 | //===----------------------------------------------------------------------===// | |
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 | ||
20 | #if CAN_SANITIZE_LEAKS | |
21 | ||
22 | namespace __asan { | |
23 | ||
24 | struct AllocationSite { | |
25 | u32 id; | |
26 | uptr total_size; | |
27 | uptr count; | |
28 | }; | |
29 | ||
30 | class HeapProfile { | |
31 | public: | |
32 | HeapProfile() : allocations_(1024) {} | |
5d3805fc JJ |
33 | |
34 | void ProcessChunk(const AsanChunkView& cv) { | |
35 | if (cv.IsAllocated()) { | |
36 | total_allocated_user_size_ += cv.UsedSize(); | |
37 | total_allocated_count_++; | |
38 | u32 id = cv.GetAllocStackId(); | |
39 | if (id) | |
40 | Insert(id, cv.UsedSize()); | |
41 | } else if (cv.IsQuarantined()) { | |
42 | total_quarantined_user_size_ += cv.UsedSize(); | |
43 | total_quarantined_count_++; | |
44 | } else { | |
45 | total_other_count_++; | |
10189819 | 46 | } |
10189819 MO |
47 | } |
48 | ||
5d3805fc | 49 | void Print(uptr top_percent, uptr max_number_of_contexts) { |
10189819 MO |
50 | InternalSort(&allocations_, allocations_.size(), |
51 | [](const AllocationSite &a, const AllocationSite &b) { | |
52 | return a.total_size > b.total_size; | |
53 | }); | |
5d3805fc | 54 | CHECK(total_allocated_user_size_); |
10189819 | 55 | uptr total_shown = 0; |
5d3805fc JJ |
56 | Printf("Live Heap Allocations: %zd bytes in %zd chunks; quarantined: " |
57 | "%zd bytes in %zd chunks; %zd other chunks; total chunks: %zd; " | |
58 | "showing top %zd%% (at most %zd unique contexts)\n", | |
59 | total_allocated_user_size_, total_allocated_count_, | |
60 | total_quarantined_user_size_, total_quarantined_count_, | |
61 | total_other_count_, total_allocated_count_ + | |
62 | total_quarantined_count_ + total_other_count_, top_percent, | |
63 | max_number_of_contexts); | |
64 | for (uptr i = 0; i < Min(allocations_.size(), max_number_of_contexts); | |
65 | i++) { | |
10189819 MO |
66 | auto &a = allocations_[i]; |
67 | Printf("%zd byte(s) (%zd%%) in %zd allocation(s)\n", a.total_size, | |
5d3805fc | 68 | a.total_size * 100 / total_allocated_user_size_, a.count); |
10189819 MO |
69 | StackDepotGet(a.id).Print(); |
70 | total_shown += a.total_size; | |
5d3805fc | 71 | if (total_shown * 100 / total_allocated_user_size_ > top_percent) |
10189819 MO |
72 | break; |
73 | } | |
74 | } | |
75 | ||
76 | private: | |
5d3805fc JJ |
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; | |
10189819 | 82 | InternalMmapVector<AllocationSite> allocations_; |
5d3805fc JJ |
83 | |
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++; | |
90 | return; | |
91 | } | |
92 | } | |
93 | allocations_.push_back({id, size, 1}); | |
94 | } | |
10189819 MO |
95 | }; |
96 | ||
97 | static void ChunkCallback(uptr chunk, void *arg) { | |
5d3805fc JJ |
98 | reinterpret_cast<HeapProfile*>(arg)->ProcessChunk( |
99 | FindHeapChunkByAllocBeg(chunk)); | |
10189819 MO |
100 | } |
101 | ||
102 | static void MemoryProfileCB(const SuspendedThreadsList &suspended_threads_list, | |
103 | void *argument) { | |
104 | HeapProfile hp; | |
105 | __lsan::ForEachChunk(ChunkCallback, &hp); | |
5d3805fc JJ |
106 | uptr *Arg = reinterpret_cast<uptr*>(argument); |
107 | hp.Print(Arg[0], Arg[1]); | |
108 | ||
109 | if (Verbosity()) | |
110 | __asan_print_accumulated_stats(); | |
10189819 MO |
111 | } |
112 | ||
113 | } // namespace __asan | |
114 | ||
5d3805fc JJ |
115 | #endif // CAN_SANITIZE_LEAKS |
116 | ||
10189819 MO |
117 | extern "C" { |
118 | SANITIZER_INTERFACE_ATTRIBUTE | |
5d3805fc JJ |
119 | void __sanitizer_print_memory_profile(uptr top_percent, |
120 | uptr max_number_of_contexts) { | |
121 | #if CAN_SANITIZE_LEAKS | |
122 | uptr Arg[2]; | |
123 | Arg[0] = top_percent; | |
124 | Arg[1] = max_number_of_contexts; | |
125 | __sanitizer::StopTheWorld(__asan::MemoryProfileCB, Arg); | |
126 | #endif // CAN_SANITIZE_LEAKS | |
10189819 MO |
127 | } |
128 | } // extern "C" |