Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(129)

Side by Side Diff: base/trace_event/heap_profiler_heap_dump_writer.cc

Issue 1911643002: Add configurable limit to allocations in heap profiler. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixed naming in protocol.json Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/trace_event/heap_profiler_heap_dump_writer.h" 5 #include "base/trace_event/heap_profiler_heap_dump_writer.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 8
9 #include <algorithm> 9 #include <algorithm>
10 #include <iterator> 10 #include <iterator>
11 #include <tuple> 11 #include <tuple>
12 #include <utility> 12 #include <utility>
13 #include <vector> 13 #include <vector>
14 14
15 #include "base/format_macros.h" 15 #include "base/format_macros.h"
16 #include "base/logging.h" 16 #include "base/logging.h"
17 #include "base/macros.h" 17 #include "base/macros.h"
18 #include "base/strings/stringprintf.h" 18 #include "base/strings/stringprintf.h"
19 #include "base/trace_event/heap_profiler_stack_frame_deduplicator.h" 19 #include "base/trace_event/heap_profiler_stack_frame_deduplicator.h"
20 #include "base/trace_event/heap_profiler_type_name_deduplicator.h" 20 #include "base/trace_event/heap_profiler_type_name_deduplicator.h"
21 #include "base/trace_event/memory_dump_session_state.h"
22 #include "base/trace_event/trace_config.h"
21 #include "base/trace_event/trace_event_argument.h" 23 #include "base/trace_event/trace_event_argument.h"
24 #include "base/trace_event/trace_log.h"
22 25
23 // Most of what the |HeapDumpWriter| does is aggregating detailed information 26 // Most of what the |HeapDumpWriter| does is aggregating detailed information
24 // about the heap and deciding what to dump. The Input to this process is a list 27 // about the heap and deciding what to dump. The Input to this process is a list
25 // of |AllocationContext|s and size pairs. 28 // of |AllocationContext|s and size pairs.
26 // 29 //
27 // The pairs are grouped into |Bucket|s. A bucket is a group of (context, size) 30 // The pairs are grouped into |Bucket|s. A bucket is a group of (context, size)
28 // pairs where the properties of the contexts share a prefix. (Type name is 31 // pairs where the properties of the contexts share a prefix. (Type name is
29 // considered a list of length one here.) First all pairs are put into one 32 // considered a list of length one here.) First all pairs are put into one
30 // bucket that represents the entire heap. Then this bucket is recursively 33 // bucket that represents the entire heap. Then this bucket is recursively
31 // broken down into smaller buckets. Each bucket keeps track of whether further 34 // broken down into smaller buckets. Each bucket keeps track of whether further
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
63 66
64 // When true, the type name for all elements in this bucket must be equal. 67 // When true, the type name for all elements in this bucket must be equal.
65 bool is_broken_down_by_type_name; 68 bool is_broken_down_by_type_name;
66 }; 69 };
67 70
68 // Comparison operator to order buckets by their size. 71 // Comparison operator to order buckets by their size.
69 bool operator<(const Bucket& lhs, const Bucket& rhs) { 72 bool operator<(const Bucket& lhs, const Bucket& rhs) {
70 return lhs.size < rhs.size; 73 return lhs.size < rhs.size;
71 } 74 }
72 75
73 // Groups the allocations in the bucket by |breakBy|. The buckets in the 76 // Groups the allocations in the bucket by |break_by|. The buckets in the
74 // returned list will have |backtrace_cursor| advanced or 77 // returned list will have |backtrace_cursor| advanced or
75 // |is_broken_down_by_type_name| set depending on the property to group by. 78 // |is_broken_down_by_type_name| set depending on the property to group by.
76 std::vector<Bucket> GetSubbuckets(const Bucket& bucket, BreakDownMode breakBy) { 79 std::vector<Bucket> GetSubbuckets(const Bucket& bucket,
80 BreakDownMode break_by) {
77 base::hash_map<const char*, Bucket> breakdown; 81 base::hash_map<const char*, Bucket> breakdown;
78 82
79 if (breakBy == BreakDownMode::kByBacktrace) { 83 if (break_by == BreakDownMode::kByBacktrace) {
80 for (const auto& context_and_metrics : bucket.metrics_by_context) { 84 for (const auto& context_and_metrics : bucket.metrics_by_context) {
81 const Backtrace& backtrace = context_and_metrics.first->backtrace; 85 const Backtrace& backtrace = context_and_metrics.first->backtrace;
82 const char* const* begin = std::begin(backtrace.frames); 86 const char* const* begin = std::begin(backtrace.frames);
83 const char* const* end = std::end(backtrace.frames); 87 const char* const* end = std::end(backtrace.frames);
84 const char* const* cursor = begin + bucket.backtrace_cursor; 88 const char* const* cursor = begin + bucket.backtrace_cursor;
85 89
86 // The backtrace in the context is padded with null pointers, but these 90 // The backtrace in the context is padded with null pointers, but these
87 // should not be considered for breakdown. Adjust end to point past the 91 // should not be considered for breakdown. Adjust end to point past the
88 // last non-null frame. 92 // last non-null frame.
89 while (begin != end && *(end - 1) == nullptr) 93 while (begin != end && *(end - 1) == nullptr)
90 end--; 94 end--;
91 95
92 DCHECK_LE(cursor, end); 96 DCHECK_LE(cursor, end);
93 97
94 if (cursor != end) { 98 if (cursor != end) {
95 Bucket& subbucket = breakdown[*cursor]; 99 Bucket& subbucket = breakdown[*cursor];
96 subbucket.size += context_and_metrics.second.size; 100 subbucket.size += context_and_metrics.second.size;
97 subbucket.count += context_and_metrics.second.count; 101 subbucket.count += context_and_metrics.second.count;
98 subbucket.metrics_by_context.push_back(context_and_metrics); 102 subbucket.metrics_by_context.push_back(context_and_metrics);
99 subbucket.backtrace_cursor = bucket.backtrace_cursor + 1; 103 subbucket.backtrace_cursor = bucket.backtrace_cursor + 1;
100 subbucket.is_broken_down_by_type_name = 104 subbucket.is_broken_down_by_type_name =
101 bucket.is_broken_down_by_type_name; 105 bucket.is_broken_down_by_type_name;
102 DCHECK_GT(subbucket.size, 0u); 106 DCHECK_GT(subbucket.size, 0u);
103 DCHECK_GT(subbucket.count, 0u); 107 DCHECK_GT(subbucket.count, 0u);
104 } 108 }
105 } 109 }
106 } else if (breakBy == BreakDownMode::kByTypeName) { 110 } else if (break_by == BreakDownMode::kByTypeName) {
107 if (!bucket.is_broken_down_by_type_name) { 111 if (!bucket.is_broken_down_by_type_name) {
108 for (const auto& context_and_metrics : bucket.metrics_by_context) { 112 for (const auto& context_and_metrics : bucket.metrics_by_context) {
109 const AllocationContext* context = context_and_metrics.first; 113 const AllocationContext* context = context_and_metrics.first;
110 Bucket& subbucket = breakdown[context->type_name]; 114 Bucket& subbucket = breakdown[context->type_name];
111 subbucket.size += context_and_metrics.second.size; 115 subbucket.size += context_and_metrics.second.size;
112 subbucket.count += context_and_metrics.second.count; 116 subbucket.count += context_and_metrics.second.count;
113 subbucket.metrics_by_context.push_back(context_and_metrics); 117 subbucket.metrics_by_context.push_back(context_and_metrics);
114 subbucket.backtrace_cursor = bucket.backtrace_cursor; 118 subbucket.backtrace_cursor = bucket.backtrace_cursor;
115 subbucket.is_broken_down_by_type_name = true; 119 subbucket.is_broken_down_by_type_name = true;
116 DCHECK_GT(subbucket.size, 0u); 120 DCHECK_GT(subbucket.size, 0u);
117 DCHECK_GT(subbucket.count, 0u); 121 DCHECK_GT(subbucket.count, 0u);
118 } 122 }
119 } 123 }
120 } 124 }
121 125
122 std::vector<Bucket> buckets; 126 std::vector<Bucket> buckets;
123 buckets.reserve(breakdown.size()); 127 buckets.reserve(breakdown.size());
124 for (auto key_bucket : breakdown) 128 for (auto key_bucket : breakdown)
125 buckets.push_back(key_bucket.second); 129 buckets.push_back(key_bucket.second);
126 130
127 return buckets; 131 return buckets;
128 } 132 }
129 133
130 // Breaks down the bucket by |breakBy|. Returns only buckets that contribute 134 // Breaks down the bucket by |break_by|. Returns only buckets that contribute
131 // significantly to the total size. The long tail is omitted. 135 // more than |min_size_bytes| to the total size. The long tail is omitted.
132 std::vector<Bucket> BreakDownBy(const Bucket& bucket, BreakDownMode breakBy) { 136 std::vector<Bucket> BreakDownBy(const Bucket& bucket,
133 std::vector<Bucket> buckets = GetSubbuckets(bucket, breakBy); 137 BreakDownMode break_by,
138 size_t min_size_bytes) {
139 std::vector<Bucket> buckets = GetSubbuckets(bucket, break_by);
134 140
135 // Ensure that |buckets| is a max-heap (the data structure, not memory heap), 141 // Ensure that |buckets| is a max-heap (the data structure, not memory heap),
136 // so its front contains the largest bucket. Buckets should be iterated 142 // so its front contains the largest bucket. Buckets should be iterated
137 // ordered by size, but sorting the vector is overkill because the long tail 143 // ordered by size, but sorting the vector is overkill because the long tail
138 // of small buckets will be discarded. By using a max-heap, the optimal case 144 // of small buckets will be discarded. By using a max-heap, the optimal case
139 // where all but the first bucket are discarded is O(n). The worst case where 145 // where all but the first bucket are discarded is O(n). The worst case where
140 // no bucket is discarded is doing a heap sort, which is O(n log n). 146 // no bucket is discarded is doing a heap sort, which is O(n log n).
141 std::make_heap(buckets.begin(), buckets.end()); 147 std::make_heap(buckets.begin(), buckets.end());
142 148
143 // Keep including buckets until adding one would increase the number of 149 // Keep including buckets until adding one would increase the number of
144 // bytes accounted for by less than 0.8% (1/125). This simple heuristic works 150 // bytes accounted for by |min_size_bytes|. The large buckets end up in
145 // quite well. The large buckets end up in [it, end()), [begin(), it) is the 151 // [it, end()), [begin(), it) is the part that contains the max-heap
146 // part that contains the max-heap of small buckets. 152 // of small buckets.
147 size_t accounted_for = 0;
148 std::vector<Bucket>::iterator it; 153 std::vector<Bucket>::iterator it;
149 for (it = buckets.end(); it != buckets.begin(); --it) { 154 for (it = buckets.end(); it != buckets.begin(); --it) {
150 accounted_for += buckets.front().size; 155 if (buckets.front().size < min_size_bytes)
151 if (buckets.front().size < (accounted_for / 125))
152 break; 156 break;
153 157
154 // Put the largest bucket in [begin, it) at |it - 1| and max-heapify 158 // Put the largest bucket in [begin, it) at |it - 1| and max-heapify
155 // [begin, it - 1). This puts the next largest bucket at |buckets.front()|. 159 // [begin, it - 1). This puts the next largest bucket at |buckets.front()|.
156 std::pop_heap(buckets.begin(), it); 160 std::pop_heap(buckets.begin(), it);
157 } 161 }
158 162
159 // At this point, |buckets| looks like this (numbers are bucket sizes): 163 // At this point, |buckets| looks like this (numbers are bucket sizes):
160 // 164 //
161 // <-- max-heap of small buckets ---> 165 // <-- max-heap of small buckets --->
(...skipping 12 matching lines...) Expand all
174 } // namespace 178 } // namespace
175 179
176 bool operator<(Entry lhs, Entry rhs) { 180 bool operator<(Entry lhs, Entry rhs) {
177 // There is no need to compare |size|. If the backtrace and type name are 181 // There is no need to compare |size|. If the backtrace and type name are
178 // equal then the sizes must be equal as well. 182 // equal then the sizes must be equal as well.
179 return std::tie(lhs.stack_frame_id, lhs.type_id) < 183 return std::tie(lhs.stack_frame_id, lhs.type_id) <
180 std::tie(rhs.stack_frame_id, rhs.type_id); 184 std::tie(rhs.stack_frame_id, rhs.type_id);
181 } 185 }
182 186
183 HeapDumpWriter::HeapDumpWriter(StackFrameDeduplicator* stack_frame_deduplicator, 187 HeapDumpWriter::HeapDumpWriter(StackFrameDeduplicator* stack_frame_deduplicator,
184 TypeNameDeduplicator* type_name_deduplicator) 188 TypeNameDeduplicator* type_name_deduplicator,
189 uint32_t breakdown_threshold_bytes)
185 : stack_frame_deduplicator_(stack_frame_deduplicator), 190 : stack_frame_deduplicator_(stack_frame_deduplicator),
186 type_name_deduplicator_(type_name_deduplicator) {} 191 type_name_deduplicator_(type_name_deduplicator),
192 breakdown_threshold_bytes_(breakdown_threshold_bytes) {
193 }
187 194
188 HeapDumpWriter::~HeapDumpWriter() {} 195 HeapDumpWriter::~HeapDumpWriter() {}
189 196
190 bool HeapDumpWriter::AddEntryForBucket(const Bucket& bucket) { 197 bool HeapDumpWriter::AddEntryForBucket(const Bucket& bucket) {
191 // The contexts in the bucket are all different, but the [begin, cursor) range 198 // The contexts in the bucket are all different, but the [begin, cursor) range
192 // is equal for all contexts in the bucket, and the type names are the same if 199 // is equal for all contexts in the bucket, and the type names are the same if
193 // |is_broken_down_by_type_name| is set. 200 // |is_broken_down_by_type_name| is set.
194 DCHECK(!bucket.metrics_by_context.empty()); 201 DCHECK(!bucket.metrics_by_context.empty());
195 202
196 const AllocationContext* context = bucket.metrics_by_context.front().first; 203 const AllocationContext* context = bucket.metrics_by_context.front().first;
(...skipping 12 matching lines...) Expand all
209 : -1; 216 : -1;
210 217
211 entry.size = bucket.size; 218 entry.size = bucket.size;
212 entry.count = bucket.count; 219 entry.count = bucket.count;
213 220
214 auto position_and_inserted = entries_.insert(entry); 221 auto position_and_inserted = entries_.insert(entry);
215 return position_and_inserted.second; 222 return position_and_inserted.second;
216 } 223 }
217 224
218 void HeapDumpWriter::BreakDown(const Bucket& bucket) { 225 void HeapDumpWriter::BreakDown(const Bucket& bucket) {
219 auto by_backtrace = BreakDownBy(bucket, BreakDownMode::kByBacktrace); 226 auto by_backtrace = BreakDownBy(bucket,
220 auto by_type_name = BreakDownBy(bucket, BreakDownMode::kByTypeName); 227 BreakDownMode::kByBacktrace,
228 breakdown_threshold_bytes_);
229 auto by_type_name = BreakDownBy(bucket,
230 BreakDownMode::kByTypeName,
231 breakdown_threshold_bytes_);
221 232
222 // Insert entries for the buckets. If a bucket was not present before, it has 233 // Insert entries for the buckets. If a bucket was not present before, it has
223 // not been broken down before, so recursively continue breaking down in that 234 // not been broken down before, so recursively continue breaking down in that
224 // case. There might be multiple routes to the same entry (first break down 235 // case. There might be multiple routes to the same entry (first break down
225 // by type name, then by backtrace, or first by backtrace and then by type), 236 // by type name, then by backtrace, or first by backtrace and then by type),
226 // so a set is used to avoid dumping and breaking down entries more than once. 237 // so a set is used to avoid dumping and breaking down entries more than once.
227 238
228 for (const Bucket& subbucket : by_backtrace) 239 for (const Bucket& subbucket : by_backtrace)
229 if (AddEntryForBucket(subbucket)) 240 if (AddEntryForBucket(subbucket))
230 BreakDown(subbucket); 241 BreakDown(subbucket);
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
297 } 308 }
298 309
299 traced_value->EndArray(); // "entries" 310 traced_value->EndArray(); // "entries"
300 return traced_value; 311 return traced_value;
301 } 312 }
302 313
303 } // namespace internal 314 } // namespace internal
304 315
305 std::unique_ptr<TracedValue> ExportHeapDump( 316 std::unique_ptr<TracedValue> ExportHeapDump(
306 const hash_map<AllocationContext, AllocationMetrics>& metrics_by_context, 317 const hash_map<AllocationContext, AllocationMetrics>& metrics_by_context,
307 StackFrameDeduplicator* stack_frame_deduplicator, 318 const MemoryDumpSessionState& session_state) {
308 TypeNameDeduplicator* type_name_deduplicator) { 319 internal::HeapDumpWriter writer(
309 internal::HeapDumpWriter writer(stack_frame_deduplicator, 320 session_state.stack_frame_deduplicator(),
310 type_name_deduplicator); 321 session_state.type_name_deduplicator(),
322 session_state.memory_dump_config().heap_profiler_options
323 .breakdown_threshold_bytes);
311 return Serialize(writer.Summarize(metrics_by_context)); 324 return Serialize(writer.Summarize(metrics_by_context));
312 } 325 }
313 326
314 } // namespace trace_event 327 } // namespace trace_event
315 } // namespace base 328 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698