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

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

Issue 2650863003: [tracing] Switch to new heap dump format. (Closed)
Patch Set: Rebase Created 3 years, 6 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
(Empty)
1 // Copyright 2017 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/heap_profiler_event_writer.h"
6
7 #include <stdint.h>
8
9 #include <tuple>
10 #include <unordered_map>
11
12 #include "base/bind.h"
13 #include "base/memory/ptr_util.h"
14 #include "base/numerics/safe_conversions.h"
15 #include "base/trace_event/heap_profiler_serialization_state.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/sharded_allocation_register.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 {
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> SerializeHeapDump(
45 const ShardedAllocationRegister& allocation_register,
46 HeapProfilerSerializationState* serialization_state) {
47 // Aggregate allocations by {backtrace_id, type_id} key.
48 using MetricsMap = std::unordered_map<AggregationKey, AllocationMetrics,
49 AggregationKey::Hasher>;
50 MetricsMap metrics_by_key;
51
52 auto visit_allocation =
53 [](HeapProfilerSerializationState* serialization_state,
54 MetricsMap* metrics_by_key,
55 const AllocationRegister::Allocation& allocation) {
56 int backtrace_id =
57 serialization_state->stack_frame_deduplicator()->Insert(
58 std::begin(allocation.context.backtrace.frames),
59 std::begin(allocation.context.backtrace.frames) +
60 allocation.context.backtrace.frame_count);
61
62 int type_id = serialization_state->type_name_deduplicator()->Insert(
63 allocation.context.type_name);
64
65 AggregationKey key = {backtrace_id, type_id};
66 AllocationMetrics& metrics = (*metrics_by_key)[key];
67 metrics.size += allocation.size;
68 metrics.count += 1;
69 };
70 allocation_register.VisitAllocations(base::BindRepeating(
71 visit_allocation, base::Unretained(serialization_state),
72 base::Unretained(&metrics_by_key)));
73
74 auto traced_value = MakeUnique<TracedValue>();
75
76 traced_value->BeginArray("nodes");
77 for (const auto& key_and_metrics : metrics_by_key)
78 traced_value->AppendInteger(key_and_metrics.first.backtrace_id);
79 traced_value->EndArray();
80
81 traced_value->BeginArray("types");
82 for (const auto& key_and_metrics : metrics_by_key)
83 traced_value->AppendInteger(key_and_metrics.first.type_id);
84 traced_value->EndArray();
85
86 traced_value->BeginArray("counts");
87 for (const auto& key_and_metrics : metrics_by_key)
88 traced_value->AppendInteger(
89 saturated_cast<int>(key_and_metrics.second.count));
90 traced_value->EndArray();
91
92 traced_value->BeginArray("sizes");
93 for (const auto& key_and_metrics : metrics_by_key)
94 traced_value->AppendInteger(
95 saturated_cast<int>(key_and_metrics.second.size));
96 traced_value->EndArray();
97
98 return traced_value;
99 }
100
101 std::unique_ptr<TracedValue> SerializeHeapProfileEventData(
102 const SerializedHeapDumpsMap& heap_dumps,
103 HeapProfilerSerializationState* serialization_state) {
104 auto traced_value = MakeUnique<TracedValue>();
105
106 // See brief description of the format in the header file.
107 traced_value->SetInteger("version", 1);
108
109 traced_value->BeginDictionary("allocators");
110 for (const auto& name_and_dump : heap_dumps) {
111 traced_value->SetValueWithCopiedName(name_and_dump.first.c_str(),
112 *name_and_dump.second);
113 }
114 traced_value->EndDictionary();
115
116 traced_value->BeginDictionary("maps");
117
118 if (auto* deduplicator = serialization_state->stack_frame_deduplicator()) {
119 traced_value->BeginArray("nodes");
120 deduplicator->SerializeIncrementally(&*traced_value);
121 traced_value->EndArray();
122 }
123
124 if (auto* deduplicator = serialization_state->type_name_deduplicator()) {
125 traced_value->BeginArray("types");
126 deduplicator->SerializeIncrementally(&*traced_value);
127 traced_value->EndArray();
128 }
129
130 if (auto* deduplicator = serialization_state->string_deduplicator()) {
131 traced_value->BeginArray("strings");
132 deduplicator->SerializeIncrementally(&*traced_value);
133 traced_value->EndArray();
134 }
135
136 traced_value->EndDictionary();
137
138 return traced_value;
139 }
140
141 } // namespace trace_event
142 } // namespace base
OLDNEW
« no previous file with comments | « base/trace_event/heap_profiler_event_writer.h ('k') | base/trace_event/heap_profiler_event_writer_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698