OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/trace_event/memory_profiler_heap_dump_writer.h" |
| 6 |
| 7 #include <numeric> |
| 8 |
| 9 #include "base/format_macros.h" |
| 10 #include "base/strings/stringprintf.h" |
| 11 #include "base/trace_event/memory_profiler_allocation_register.h" |
| 12 #include "base/trace_event/trace_event_argument.h" |
| 13 |
| 14 namespace base { |
| 15 namespace trace_event { |
| 16 |
| 17 namespace { |
| 18 |
| 19 template <typename T> |
| 20 bool PairSizeGt(const std::pair<T, size_t>& lhs, |
| 21 const std::pair<T, size_t>& rhs) { |
| 22 return lhs.second > rhs.second; |
| 23 } |
| 24 |
| 25 template <typename T> |
| 26 size_t PairSizeAdd(size_t acc, const std::pair<T, size_t>& rhs) { |
| 27 return acc + rhs.second; |
| 28 } |
| 29 |
| 30 } // namespace |
| 31 |
| 32 HeapDumpWriter::HeapDumpWriter(StackFrameDeduplicator* stack_frame_deduplicator) |
| 33 : traced_value_(new TracedValue()), |
| 34 stack_frame_deduplicator_(stack_frame_deduplicator) {} |
| 35 |
| 36 HeapDumpWriter::~HeapDumpWriter() {} |
| 37 |
| 38 void HeapDumpWriter::InsertAllocation(const AllocationContext& context, |
| 39 size_t size) { |
| 40 bytes_by_backtrace_[context.backtrace] += size; |
| 41 } |
| 42 |
| 43 scoped_refptr<TracedValue> HeapDumpWriter::WriteHeapDump() { |
| 44 // Sort the backtraces by size in descending order. |
| 45 std::vector<std::pair<Backtrace, size_t>> sorted_by_backtrace; |
| 46 |
| 47 std::copy(bytes_by_backtrace_.begin(), bytes_by_backtrace_.end(), |
| 48 std::back_inserter(sorted_by_backtrace)); |
| 49 std::sort(sorted_by_backtrace.begin(), sorted_by_backtrace.end(), |
| 50 PairSizeGt<Backtrace>); |
| 51 |
| 52 traced_value_->BeginArray("entries"); |
| 53 |
| 54 // The global size, no column specified. |
| 55 { |
| 56 size_t total_size = |
| 57 std::accumulate(sorted_by_backtrace.begin(), sorted_by_backtrace.end(), |
| 58 size_t(0), PairSizeAdd<Backtrace>); |
| 59 traced_value_->BeginDictionary(); |
| 60 WriteSize(total_size); |
| 61 traced_value_->EndDictionary(); |
| 62 } |
| 63 |
| 64 // Size per backtrace. |
| 65 for (auto it = sorted_by_backtrace.begin(); |
| 66 it != sorted_by_backtrace.end(); it++) { |
| 67 traced_value_->BeginDictionary(); |
| 68 // Insert a forward reference to the backtrace that will be written to the |
| 69 // |stackFrames| dictionary later on. |
| 70 WriteStackFrameIndex(stack_frame_deduplicator_->Insert(it->first)); |
| 71 WriteSize(it->second); |
| 72 traced_value_->EndDictionary(); |
| 73 } |
| 74 |
| 75 traced_value_->EndArray(); // "entries" |
| 76 |
| 77 return traced_value_; |
| 78 } |
| 79 |
| 80 void HeapDumpWriter::WriteStackFrameIndex(int index) { |
| 81 if (index == -1) { |
| 82 // An empty backtrace (which will have index -1) is represented by the empty |
| 83 // string, because there is no leaf frame to reference in |stackFrames|. |
| 84 traced_value_->SetString("bt", ""); |
| 85 } else { |
| 86 // Format index of the leaf frame as a string, because |stackFrames| is a |
| 87 // dictionary, not an array. |
| 88 SStringPrintf(&buffer_, "%i", index); |
| 89 traced_value_->SetString("bt", buffer_); |
| 90 } |
| 91 } |
| 92 |
| 93 void HeapDumpWriter::WriteSize(size_t size) { |
| 94 // Format size as hexadecimal string into |buffer_|. |
| 95 SStringPrintf(&buffer_, "%" PRIx64, static_cast<uint64_t>(size)); |
| 96 traced_value_->SetString("size", buffer_); |
| 97 } |
| 98 |
| 99 } // namespace trace_event |
| 100 } // namespace base |
OLD | NEW |