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 | |
13 namespace base { | |
14 namespace trace_event { | |
15 | |
16 namespace { | |
17 | |
18 template <typename T> | |
19 bool PairSizeGt(const std::pair<T, size_t>& lhs, | |
20 const std::pair<T, size_t>& rhs) { | |
21 return lhs.second > rhs.second; | |
22 } | |
23 | |
24 template <typename T> | |
25 size_t PairSizeAdd(size_t acc, const std::pair<T, size_t>& rhs) { | |
26 return acc + rhs.second; | |
27 } | |
28 | |
29 } // namespace | |
30 | |
31 HeapDumpWriter::HeapDumpWriter(StackFrameDeduplicator* stack_frame_deduplicator) | |
32 : traced_value_(new TracedValue()), | |
33 stack_frame_deduplicator_(stack_frame_deduplicator) {} | |
34 HeapDumpWriter::~HeapDumpWriter() {} | |
Primiano Tucci (use gerrit)
2015/10/26 20:57:57
add a blank line before this
Ruud van Asseldonk
2015/10/27 10:50:18
Done.
| |
35 | |
36 void HeapDumpWriter::InsertAllocation(const AllocationContext& context, | |
37 size_t size) { | |
38 bytes_by_backtrace_[context.backtrace] += size; | |
39 } | |
40 | |
41 scoped_refptr<TracedValue> HeapDumpWriter::WriteHeapDump() { | |
42 // Sort the backtraces by size in descending order. | |
43 std::vector<std::pair<Backtrace, size_t>> sorted_by_backtrace; | |
44 | |
45 std::copy(bytes_by_backtrace_.begin(), bytes_by_backtrace_.end(), | |
46 std::back_inserter(sorted_by_backtrace)); | |
47 std::sort(sorted_by_backtrace.begin(), sorted_by_backtrace.end(), | |
48 PairSizeGt<Backtrace>); | |
49 | |
50 traced_value_->BeginArray("heap"); | |
51 | |
52 // The global size, no column specified. | |
53 { | |
54 size_t total_size = | |
55 std::accumulate(sorted_by_backtrace.begin(), sorted_by_backtrace.end(), | |
56 size_t(0), PairSizeAdd<Backtrace>); | |
57 traced_value_->BeginDictionary(); | |
58 WriteSize(total_size); | |
59 traced_value_->EndDictionary(); | |
60 } | |
61 | |
62 // Size per backtrace. | |
63 for (auto bt_sz = sorted_by_backtrace.begin(); | |
Primiano Tucci (use gerrit)
2015/10/26 20:57:57
I typically see this as "auto it" in the codebase,
Ruud van Asseldonk
2015/10/27 10:50:18
Done. I called it |bt_sz| because iterator derefer
| |
64 bt_sz != sorted_by_backtrace.end(); bt_sz++) { | |
65 traced_value_->BeginDictionary(); | |
66 // Insert a forward reference to the backtrace that will be written to the | |
67 // |stackFrames| dictionary later on. | |
68 int bt_index = stack_frame_deduplicator_->Insert(bt_sz->first); | |
69 traced_value_->SetInteger("bt", bt_index); | |
Primiano Tucci (use gerrit)
2015/10/26 20:57:57
were you not planning to make the indexes strings
Ruud van Asseldonk
2015/10/27 10:50:18
Yes, I didn't update this one yet. Fixed.
| |
70 WriteSize(bt_sz->second); | |
71 traced_value_->EndDictionary(); | |
72 } | |
73 | |
74 traced_value_->EndArray(); // heap | |
75 | |
76 return traced_value_; | |
77 } | |
78 | |
79 void HeapDumpWriter::WriteSize(size_t size) { | |
80 // Format size as hexadecimal string into |buffer_|. | |
81 SStringPrintf(&buffer_, "%" PRIx64, static_cast<uint64_t>(size)); | |
82 traced_value_->SetString("size", buffer_); | |
83 } | |
84 | |
85 } // namespace trace_event | |
86 } // namespace base | |
OLD | NEW |