blob: 49d2f02d452fd36e6e8584eaa3b65a707e45380c [file] [log] [blame]
Eric Secklerc165b872019-11-04 14:26:25 +00001/*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "src/trace_processor/importers/proto/heap_graph_module.h"
18
19#include "src/trace_processor/importers/proto/heap_graph_tracker.h"
20#include "src/trace_processor/process_tracker.h"
21#include "src/trace_processor/trace_processor_context.h"
22#include "src/trace_processor/trace_storage.h"
23
24#include "protos/perfetto/trace/profiling/heap_graph.pbzero.h"
25
26namespace perfetto {
27namespace trace_processor {
28
29namespace {
30
31const char* HeapGraphRootTypeToString(int32_t type) {
32 switch (type) {
33 case protos::pbzero::HeapGraphRoot::ROOT_UNKNOWN:
34 return "ROOT_UNKNOWN";
35 case protos::pbzero::HeapGraphRoot::ROOT_JNI_GLOBAL:
36 return "ROOT_JNI_GLOBAL";
37 case protos::pbzero::HeapGraphRoot::ROOT_JNI_LOCAL:
38 return "ROOT_JNI_LOCAL";
39 case protos::pbzero::HeapGraphRoot::ROOT_JAVA_FRAME:
40 return "ROOT_JAVA_FRAME";
41 case protos::pbzero::HeapGraphRoot::ROOT_NATIVE_STACK:
42 return "ROOT_NATIVE_STACK";
43 case protos::pbzero::HeapGraphRoot::ROOT_STICKY_CLASS:
44 return "ROOT_STICKY_CLASS";
45 case protos::pbzero::HeapGraphRoot::ROOT_THREAD_BLOCK:
46 return "ROOT_THREAD_BLOCK";
47 case protos::pbzero::HeapGraphRoot::ROOT_MONITOR_USED:
48 return "ROOT_MONITOR_USED";
49 case protos::pbzero::HeapGraphRoot::ROOT_THREAD_OBJECT:
50 return "ROOT_THREAD_OBJECT";
51 case protos::pbzero::HeapGraphRoot::ROOT_INTERNED_STRING:
52 return "ROOT_INTERNED_STRING";
53 case protos::pbzero::HeapGraphRoot::ROOT_FINALIZING:
54 return "ROOT_FINALIZING";
55 case protos::pbzero::HeapGraphRoot::ROOT_DEBUGGER:
56 return "ROOT_DEBUGGER";
57 case protos::pbzero::HeapGraphRoot::ROOT_REFERENCE_CLEANUP:
58 return "ROOT_REFERENCE_CLEANUP";
59 case protos::pbzero::HeapGraphRoot::ROOT_VM_INTERNAL:
60 return "ROOT_VM_INTERNAL";
61 case protos::pbzero::HeapGraphRoot::ROOT_JNI_MONITOR:
62 return "ROOT_JNI_MONITOR";
63 default:
64 return "ROOT_UNKNOWN";
65 }
66}
67
Florian Mayer905d1112019-11-15 16:47:03 +000068// Iterate over a repeated field of varints, independent of whether it is
69// packed or not.
70template <int32_t field_no, typename T, typename F>
71bool ForEachVarInt(const T& decoder, F fn) {
72 auto field = decoder.template at<field_no>();
73 bool parse_error = false;
74 if (field.type() == protozero::proto_utils::ProtoWireType::kLengthDelimited) {
75 // packed repeated
76 auto it = decoder.template GetPackedRepeated<
77 ::protozero::proto_utils::ProtoWireType::kVarInt, uint64_t>(
78 field_no, &parse_error);
79 for (; it; ++it)
80 fn(*it);
81 } else {
82 // non-packed repeated
83 auto it = decoder.template GetRepeated<uint64_t>(field_no);
84 for (; it; ++it)
85 fn(*it);
86 }
87 return parse_error;
88}
89
Eric Secklerc165b872019-11-04 14:26:25 +000090} // namespace
91
Mikhail Khokhlovddba3e62019-12-09 11:06:28 +000092using perfetto::protos::pbzero::TracePacket;
93
94HeapGraphModule::HeapGraphModule(TraceProcessorContext* context)
Mikhail Khokhlov6146a582019-12-17 17:33:47 +000095 : context_(context), heap_graph_tracker_(context) {
Mikhail Khokhlovddba3e62019-12-09 11:06:28 +000096 RegisterForField(TracePacket::kHeapGraphFieldNumber, context);
97 RegisterForField(TracePacket::kDeobfuscationMappingFieldNumber, context);
98}
99
100void HeapGraphModule::ParsePacket(
101 const protos::pbzero::TracePacket::Decoder& decoder,
102 const TimestampedTracePiece& ttp,
103 uint32_t field_id) {
104 switch (field_id) {
105 case TracePacket::kHeapGraphFieldNumber:
Florian Mayer1690fc52019-12-11 15:46:34 +0000106 ParseHeapGraph(decoder.trusted_packet_sequence_id(), ttp.timestamp,
107 decoder.heap_graph());
Mikhail Khokhlovddba3e62019-12-09 11:06:28 +0000108 return;
109 case TracePacket::kDeobfuscationMappingFieldNumber:
110 ParseDeobfuscationMapping(decoder.deobfuscation_mapping());
111 return;
112 }
113}
114
Florian Mayer1690fc52019-12-11 15:46:34 +0000115void HeapGraphModule::ParseHeapGraph(uint32_t seq_id,
116 int64_t ts,
117 protozero::ConstBytes blob) {
Eric Secklerc165b872019-11-04 14:26:25 +0000118 protos::pbzero::HeapGraph::Decoder heap_graph(blob.data, blob.size);
119 UniquePid upid = context_->process_tracker->GetOrCreateProcess(
120 static_cast<uint32_t>(heap_graph.pid()));
Mikhail Khokhlov6146a582019-12-17 17:33:47 +0000121 heap_graph_tracker_.SetPacketIndex(seq_id, heap_graph.index());
Eric Secklerc165b872019-11-04 14:26:25 +0000122 for (auto it = heap_graph.objects(); it; ++it) {
123 protos::pbzero::HeapGraphObject::Decoder object(*it);
124 HeapGraphTracker::SourceObject obj;
125 obj.object_id = object.id();
126 obj.self_size = object.self_size();
127 obj.type_id = object.type_id();
Florian Mayer905d1112019-11-15 16:47:03 +0000128
129 std::vector<uint64_t> field_ids;
130 std::vector<uint64_t> object_ids;
131
132 bool parse_error = ForEachVarInt<
133 protos::pbzero::HeapGraphObject::kReferenceFieldIdFieldNumber>(
134 object, [&field_ids](uint64_t value) { field_ids.push_back(value); });
135
136 if (!parse_error) {
137 parse_error = ForEachVarInt<
138 protos::pbzero::HeapGraphObject::kReferenceObjectIdFieldNumber>(
139 object,
140 [&object_ids](uint64_t value) { object_ids.push_back(value); });
Florian Mayera9669012019-11-15 14:53:22 +0000141 }
Florian Mayeracb44472019-11-15 16:46:03 +0000142
Florian Mayer905d1112019-11-15 16:47:03 +0000143 if (parse_error) {
144 context_->storage->IncrementIndexedStats(
145 stats::heap_graph_malformed_packet, static_cast<int>(upid));
146 break;
147 }
148 if (field_ids.size() != object_ids.size()) {
149 context_->storage->IncrementIndexedStats(
150 stats::heap_graph_malformed_packet, static_cast<int>(upid));
Florian Mayeracb44472019-11-15 16:46:03 +0000151 continue;
152 }
Florian Mayer905d1112019-11-15 16:47:03 +0000153 for (size_t i = 0; i < field_ids.size(); ++i) {
154 HeapGraphTracker::SourceObject::Reference ref;
155 ref.field_name_id = field_ids[i];
156 ref.owned_object_id = object_ids[i];
157 obj.references.emplace_back(std::move(ref));
158 }
Mikhail Khokhlov6146a582019-12-17 17:33:47 +0000159 heap_graph_tracker_.AddObject(seq_id, upid, ts, std::move(obj));
Eric Secklerc165b872019-11-04 14:26:25 +0000160 }
161 for (auto it = heap_graph.type_names(); it; ++it) {
162 protos::pbzero::InternedString::Decoder entry(*it);
163 const char* str = reinterpret_cast<const char*>(entry.str().data);
164 auto str_view = base::StringView(str, entry.str().size);
165
Mikhail Khokhlov6146a582019-12-17 17:33:47 +0000166 heap_graph_tracker_.AddInternedTypeName(
Florian Mayer1690fc52019-12-11 15:46:34 +0000167 seq_id, entry.iid(), context_->storage->InternString(str_view));
Eric Secklerc165b872019-11-04 14:26:25 +0000168 }
169 for (auto it = heap_graph.field_names(); it; ++it) {
170 protos::pbzero::InternedString::Decoder entry(*it);
171 const char* str = reinterpret_cast<const char*>(entry.str().data);
172 auto str_view = base::StringView(str, entry.str().size);
173
Mikhail Khokhlov6146a582019-12-17 17:33:47 +0000174 heap_graph_tracker_.AddInternedFieldName(
Florian Mayer1690fc52019-12-11 15:46:34 +0000175 seq_id, entry.iid(), context_->storage->InternString(str_view));
Eric Secklerc165b872019-11-04 14:26:25 +0000176 }
177 for (auto it = heap_graph.roots(); it; ++it) {
178 protos::pbzero::HeapGraphRoot::Decoder entry(*it);
179 const char* str = HeapGraphRootTypeToString(entry.root_type());
180 auto str_view = base::StringView(str);
181
182 HeapGraphTracker::SourceRoot src_root;
183 src_root.root_type = context_->storage->InternString(str_view);
Florian Mayer905d1112019-11-15 16:47:03 +0000184 bool parse_error =
185 ForEachVarInt<protos::pbzero::HeapGraphRoot::kObjectIdsFieldNumber>(
186 entry, [&src_root](uint64_t value) {
187 src_root.object_ids.emplace_back(value);
188 });
189 if (parse_error) {
190 context_->storage->IncrementIndexedStats(
191 stats::heap_graph_malformed_packet, static_cast<int>(upid));
192 break;
193 }
Mikhail Khokhlov6146a582019-12-17 17:33:47 +0000194 heap_graph_tracker_.AddRoot(seq_id, upid, ts, std::move(src_root));
Eric Secklerc165b872019-11-04 14:26:25 +0000195 }
196 if (!heap_graph.continued()) {
Mikhail Khokhlov6146a582019-12-17 17:33:47 +0000197 heap_graph_tracker_.FinalizeProfile(seq_id);
Eric Secklerc165b872019-11-04 14:26:25 +0000198 }
199}
200
Florian Mayere0f2b0e2019-11-26 14:20:36 +0000201void HeapGraphModule::ParseDeobfuscationMapping(protozero::ConstBytes blob) {
202 // TODO(fmayer): Support multiple profiles in the same trace.
203 protos::pbzero::DeobfuscationMapping::Decoder deobfuscation_mapping(
204 blob.data, blob.size);
205 for (auto class_it = deobfuscation_mapping.obfuscated_classes(); class_it;
206 ++class_it) {
207 protos::pbzero::ObfuscatedClass::Decoder cls(*class_it);
208 auto obfuscated_class_name_id =
209 context_->storage->string_pool().GetId(cls.obfuscated_name());
210 if (!obfuscated_class_name_id) {
211 PERFETTO_DLOG("Class string %s not found",
212 cls.obfuscated_name().ToStdString().c_str());
213 } else {
214 const std::vector<int64_t>* cls_objects =
Mikhail Khokhlov6146a582019-12-17 17:33:47 +0000215 heap_graph_tracker_.RowsForType(*obfuscated_class_name_id);
Florian Mayere0f2b0e2019-11-26 14:20:36 +0000216
217 if (cls_objects) {
218 auto interned_deobfuscated_name =
219 context_->storage->InternString(cls.deobfuscated_name());
220 for (int64_t row : *cls_objects) {
221 context_->storage->mutable_heap_graph_object_table()
222 ->mutable_deobfuscated_type_name()
223 ->Set(static_cast<uint32_t>(row), interned_deobfuscated_name);
224 }
225 } else {
226 PERFETTO_DLOG("Class %s not found",
227 cls.obfuscated_name().ToStdString().c_str());
228 }
229 }
230 for (auto member_it = cls.obfuscated_members(); member_it; ++member_it) {
231 protos::pbzero::ObfuscatedMember::Decoder member(*member_it);
232
233 std::string merged_obfuscated = cls.obfuscated_name().ToStdString() +
234 "." +
235 member.obfuscated_name().ToStdString();
236 std::string merged_deobfuscated =
237 cls.deobfuscated_name().ToStdString() + "." +
238 member.deobfuscated_name().ToStdString();
239
240 auto obfuscated_field_name_id = context_->storage->string_pool().GetId(
241 base::StringView(merged_obfuscated));
242 if (!obfuscated_field_name_id) {
243 PERFETTO_DLOG("Field string %s not found", merged_obfuscated.c_str());
244 continue;
245 }
246
247 const std::vector<int64_t>* field_references =
Mikhail Khokhlov6146a582019-12-17 17:33:47 +0000248 heap_graph_tracker_.RowsForField(*obfuscated_field_name_id);
Florian Mayere0f2b0e2019-11-26 14:20:36 +0000249 if (field_references) {
250 auto interned_deobfuscated_name = context_->storage->InternString(
251 base::StringView(merged_deobfuscated));
252 for (int64_t row : *field_references) {
253 context_->storage->mutable_heap_graph_reference_table()
254 ->mutable_deobfuscated_field_name()
255 ->Set(static_cast<uint32_t>(row), interned_deobfuscated_name);
256 }
257 } else {
258 PERFETTO_DLOG("Field %s not found", merged_obfuscated.c_str());
259 }
260 }
261 }
262}
263
Eric Secklerc165b872019-11-04 14:26:25 +0000264} // namespace trace_processor
265} // namespace perfetto