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

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

Issue 2650863003: [tracing] Switch to new heap dump format. (Closed)
Patch Set: Fix StringDeduplicator::Insert Created 3 years, 9 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 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/heap_profiler_event_writer.h"
6
7 #include <stdint.h>
8
9 #include <tuple>
10
11 #include "base/containers/hash_tables.h"
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 {
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>
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));
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");
ssid 2017/03/13 03:38:00 Have you checked if we don't regress on the trace
DmitrySkiba 2017/03/14 22:12:48 String table actually improves trace size. 40s Lin
ssid 2017/03/16 02:09:20 That's awesome!
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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698