OLD | NEW |
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> |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
67 | 67 |
68 // Comparison operator to order buckets by their size. | 68 // Comparison operator to order buckets by their size. |
69 bool operator<(const Bucket& lhs, const Bucket& rhs) { | 69 bool operator<(const Bucket& lhs, const Bucket& rhs) { |
70 return lhs.size < rhs.size; | 70 return lhs.size < rhs.size; |
71 } | 71 } |
72 | 72 |
73 // Groups the allocations in the bucket by |breakBy|. The buckets in the | 73 // Groups the allocations in the bucket by |breakBy|. The buckets in the |
74 // returned list will have |backtrace_cursor| advanced or | 74 // returned list will have |backtrace_cursor| advanced or |
75 // |is_broken_down_by_type_name| set depending on the property to group by. | 75 // |is_broken_down_by_type_name| set depending on the property to group by. |
76 std::vector<Bucket> GetSubbuckets(const Bucket& bucket, BreakDownMode breakBy) { | 76 std::vector<Bucket> GetSubbuckets(const Bucket& bucket, BreakDownMode breakBy) { |
77 base::hash_map<const void*, Bucket> breakdown; | 77 base::hash_map<const char*, Bucket> breakdown; |
78 | 78 |
79 if (breakBy == BreakDownMode::kByBacktrace) { | 79 if (breakBy == BreakDownMode::kByBacktrace) { |
80 for (const auto& context_and_metrics : bucket.metrics_by_context) { | 80 for (const auto& context_and_metrics : bucket.metrics_by_context) { |
81 const Backtrace& backtrace = context_and_metrics.first->backtrace; | 81 const Backtrace& backtrace = context_and_metrics.first->backtrace; |
82 const StackFrame* begin = std::begin(backtrace.frames); | 82 const char* const* begin = std::begin(backtrace.frames); |
83 const StackFrame* end = begin + backtrace.frame_count; | 83 const char* const* end = std::end(backtrace.frames); |
84 const StackFrame* cursor = begin + bucket.backtrace_cursor; | 84 const char* const* cursor = begin + bucket.backtrace_cursor; |
| 85 |
| 86 // 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 |
| 88 // last non-null frame. |
| 89 while (begin != end && *(end - 1) == nullptr) |
| 90 end--; |
85 | 91 |
86 DCHECK_LE(cursor, end); | 92 DCHECK_LE(cursor, end); |
87 | 93 |
88 if (cursor != end) { | 94 if (cursor != end) { |
89 Bucket& subbucket = breakdown[cursor->value]; | 95 Bucket& subbucket = breakdown[*cursor]; |
90 subbucket.size += context_and_metrics.second.size; | 96 subbucket.size += context_and_metrics.second.size; |
91 subbucket.count += context_and_metrics.second.count; | 97 subbucket.count += context_and_metrics.second.count; |
92 subbucket.metrics_by_context.push_back(context_and_metrics); | 98 subbucket.metrics_by_context.push_back(context_and_metrics); |
93 subbucket.backtrace_cursor = bucket.backtrace_cursor + 1; | 99 subbucket.backtrace_cursor = bucket.backtrace_cursor + 1; |
94 subbucket.is_broken_down_by_type_name = | 100 subbucket.is_broken_down_by_type_name = |
95 bucket.is_broken_down_by_type_name; | 101 bucket.is_broken_down_by_type_name; |
96 DCHECK_GT(subbucket.size, 0u); | 102 DCHECK_GT(subbucket.size, 0u); |
97 DCHECK_GT(subbucket.count, 0u); | 103 DCHECK_GT(subbucket.count, 0u); |
98 } | 104 } |
99 } | 105 } |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
182 HeapDumpWriter::~HeapDumpWriter() {} | 188 HeapDumpWriter::~HeapDumpWriter() {} |
183 | 189 |
184 bool HeapDumpWriter::AddEntryForBucket(const Bucket& bucket) { | 190 bool HeapDumpWriter::AddEntryForBucket(const Bucket& bucket) { |
185 // The contexts in the bucket are all different, but the [begin, cursor) range | 191 // The contexts in the bucket are all different, but the [begin, cursor) range |
186 // is equal for all contexts in the bucket, and the type names are the same if | 192 // is equal for all contexts in the bucket, and the type names are the same if |
187 // |is_broken_down_by_type_name| is set. | 193 // |is_broken_down_by_type_name| is set. |
188 DCHECK(!bucket.metrics_by_context.empty()); | 194 DCHECK(!bucket.metrics_by_context.empty()); |
189 | 195 |
190 const AllocationContext* context = bucket.metrics_by_context.front().first; | 196 const AllocationContext* context = bucket.metrics_by_context.front().first; |
191 | 197 |
192 const StackFrame* backtrace_begin = std::begin(context->backtrace.frames); | 198 const char* const* backtrace_begin = std::begin(context->backtrace.frames); |
193 const StackFrame* backtrace_end = backtrace_begin + bucket.backtrace_cursor; | 199 const char* const* backtrace_end = backtrace_begin + bucket.backtrace_cursor; |
194 DCHECK_LE(bucket.backtrace_cursor, arraysize(context->backtrace.frames)); | 200 DCHECK_LE(bucket.backtrace_cursor, arraysize(context->backtrace.frames)); |
195 | 201 |
196 Entry entry; | 202 Entry entry; |
197 entry.stack_frame_id = stack_frame_deduplicator_->Insert( | 203 entry.stack_frame_id = |
198 backtrace_begin, backtrace_end); | 204 stack_frame_deduplicator_->Insert(backtrace_begin, backtrace_end); |
199 | 205 |
200 // Deduplicate the type name, or use ID -1 if type name is not set. | 206 // Deduplicate the type name, or use ID -1 if type name is not set. |
201 entry.type_id = bucket.is_broken_down_by_type_name | 207 entry.type_id = bucket.is_broken_down_by_type_name |
202 ? type_name_deduplicator_->Insert(context->type_name) | 208 ? type_name_deduplicator_->Insert(context->type_name) |
203 : -1; | 209 : -1; |
204 | 210 |
205 entry.size = bucket.size; | 211 entry.size = bucket.size; |
206 entry.count = bucket.count; | 212 entry.count = bucket.count; |
207 | 213 |
208 auto position_and_inserted = entries_.insert(entry); | 214 auto position_and_inserted = entries_.insert(entry); |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
300 const hash_map<AllocationContext, AllocationMetrics>& metrics_by_context, | 306 const hash_map<AllocationContext, AllocationMetrics>& metrics_by_context, |
301 StackFrameDeduplicator* stack_frame_deduplicator, | 307 StackFrameDeduplicator* stack_frame_deduplicator, |
302 TypeNameDeduplicator* type_name_deduplicator) { | 308 TypeNameDeduplicator* type_name_deduplicator) { |
303 internal::HeapDumpWriter writer(stack_frame_deduplicator, | 309 internal::HeapDumpWriter writer(stack_frame_deduplicator, |
304 type_name_deduplicator); | 310 type_name_deduplicator); |
305 return Serialize(writer.Summarize(metrics_by_context)); | 311 return Serialize(writer.Summarize(metrics_by_context)); |
306 } | 312 } |
307 | 313 |
308 } // namespace trace_event | 314 } // namespace trace_event |
309 } // namespace base | 315 } // namespace base |
OLD | NEW |