OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
Primiano Tucci (use gerrit)
2017/03/09 11:47:44
Stop living in the past :P (2015 -> 2017)
DmitrySkiba
2017/03/14 22:12:47
Done.
| |
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/heap_profiler_event_writer.h" | |
6 | |
7 #include <stdint.h> | |
8 | |
9 #include <tuple> | |
10 | |
11 #include "base/containers/hash_tables.h" | |
Primiano Tucci (use gerrit)
2017/03/09 11:47:44
I think this is really just for legacy code, this
DmitrySkiba
2017/03/14 22:12:47
Done.
| |
12 #include "base/hash.h" | |
13 #include "base/memory/ptr_util.h" | |
14 #include "base/numerics/safe_conversions.h" | |
15 #include "base/trace_event/heap_profiler_allocation_register.h" | |
16 #include "base/trace_event/heap_profiler_stack_frame_deduplicator.h" | |
17 #include "base/trace_event/heap_profiler_string_deduplicator.h" | |
18 #include "base/trace_event/heap_profiler_type_name_deduplicator.h" | |
19 #include "base/trace_event/memory_dump_session_state.h" | |
20 #include "base/trace_event/trace_event_argument.h" | |
21 | |
22 namespace base { | |
23 namespace trace_event { | |
24 | |
25 namespace { | |
26 | |
27 struct AggregationKey { | |
Primiano Tucci (use gerrit)
2017/03/09 11:47:44
I think you can just:
struct AggregationKey : publ
DmitrySkiba
2017/03/14 22:12:47
I started with that (using tuple), but I didn't li
| |
28 int backtrace_id; | |
29 int type_id; | |
30 | |
31 struct Hasher { | |
32 size_t operator()(const AggregationKey& key) const { | |
33 return base::HashInts(key.backtrace_id, key.type_id); | |
34 } | |
35 }; | |
36 | |
37 bool operator==(const AggregationKey& other) const { | |
38 return backtrace_id == other.backtrace_id && type_id == other.type_id; | |
39 } | |
40 }; | |
41 | |
42 } // namespace | |
43 | |
44 std::unique_ptr<TracedValue> ExportHeapDump( | |
45 const AllocationRegister& allocation_register, | |
46 const MemoryDumpSessionState& session_state) { | |
47 // Aggregate allocations by {backtrace_id, type_id} key. | |
48 hash_map<AggregationKey, AllocationMetrics, AggregationKey::Hasher> | |
Primiano Tucci (use gerrit)
2017/03/09 11:47:44
The comment on hash_map says // Use std::unordered
DmitrySkiba
2017/03/14 22:12:47
Done.
| |
49 metrics_by_key; | |
50 for (const auto& allocation : allocation_register) { | |
51 int backtrace_id = session_state.stack_frame_deduplicator()->Insert( | |
52 std::begin(allocation.context.backtrace.frames), | |
53 std::begin(allocation.context.backtrace.frames) + | |
54 allocation.context.backtrace.frame_count); | |
55 | |
56 int type_id = session_state.type_name_deduplicator()->Insert( | |
57 allocation.context.type_name); | |
58 | |
59 AggregationKey key = {backtrace_id, type_id}; | |
60 AllocationMetrics& metrics = metrics_by_key[key]; | |
61 metrics.size += allocation.size; | |
62 metrics.count += 1; | |
63 } | |
64 | |
65 auto traced_value = MakeUnique<TracedValue>(); | |
66 | |
67 traced_value->BeginArray("nodes"); | |
68 for (const auto& key_and_metrics : metrics_by_key) | |
69 traced_value->AppendInteger(key_and_metrics.first.backtrace_id); | |
70 traced_value->EndArray(); | |
71 | |
72 traced_value->BeginArray("types"); | |
73 for (const auto& key_and_metrics : metrics_by_key) | |
74 traced_value->AppendInteger(key_and_metrics.first.type_id); | |
75 traced_value->EndArray(); | |
76 | |
77 traced_value->BeginArray("counts"); | |
78 for (const auto& key_and_metrics : metrics_by_key) | |
79 traced_value->AppendInteger( | |
80 saturated_cast<int>(key_and_metrics.second.count)); | |
Primiano Tucci (use gerrit)
2017/03/09 11:47:44
nice touch of class :)
| |
81 traced_value->EndArray(); | |
82 | |
83 traced_value->BeginArray("sizes"); | |
84 for (const auto& key_and_metrics : metrics_by_key) | |
85 traced_value->AppendInteger( | |
86 saturated_cast<int>(key_and_metrics.second.size)); | |
87 traced_value->EndArray(); | |
88 | |
89 return traced_value; | |
90 } | |
91 | |
92 std::unique_ptr<TracedValue> ExportHeapProfileEventData( | |
93 const ExportedHeapDumpsMap& heap_dumps, | |
94 const MemoryDumpSessionState& session_state) { | |
95 auto traced_value = MakeUnique<TracedValue>(); | |
96 | |
97 // See brief description of the format in the header file. | |
98 traced_value->SetInteger("version", 1); | |
99 | |
100 traced_value->BeginDictionary("allocators"); | |
101 for (const auto& name_and_dump : heap_dumps) { | |
102 traced_value->SetValueWithCopiedName(name_and_dump.first.c_str(), | |
103 *name_and_dump.second); | |
104 } | |
105 traced_value->EndDictionary(); | |
106 | |
107 traced_value->BeginDictionary("maps"); | |
108 | |
109 if (auto* deduplicator = session_state.stack_frame_deduplicator()) { | |
110 traced_value->BeginArray("nodes"); | |
111 deduplicator->ExportIncrementally(&*traced_value); | |
112 traced_value->EndArray(); | |
113 } | |
114 | |
115 if (auto* deduplicator = session_state.type_name_deduplicator()) { | |
116 traced_value->BeginArray("types"); | |
117 deduplicator->ExportIncrementally(&*traced_value); | |
118 traced_value->EndArray(); | |
119 } | |
120 | |
121 if (auto* deduplicator = session_state.string_deduplicator()) { | |
122 traced_value->BeginArray("strings"); | |
123 deduplicator->ExportIncrementally(&*traced_value); | |
124 traced_value->EndArray(); | |
125 } | |
126 | |
127 traced_value->EndDictionary(); | |
128 | |
129 return traced_value; | |
130 } | |
131 | |
132 } // namespace trace_event | |
133 } // namespace base | |
OLD | NEW |