Eric Seckler | c165b87 | 2019-11-04 14:26:25 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 26 | namespace perfetto { |
| 27 | namespace trace_processor { |
| 28 | |
| 29 | namespace { |
| 30 | |
| 31 | const 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 Mayer | 905d111 | 2019-11-15 16:47:03 +0000 | [diff] [blame] | 68 | // Iterate over a repeated field of varints, independent of whether it is |
| 69 | // packed or not. |
| 70 | template <int32_t field_no, typename T, typename F> |
| 71 | bool 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 Seckler | c165b87 | 2019-11-04 14:26:25 +0000 | [diff] [blame] | 90 | } // namespace |
| 91 | |
| 92 | void HeapGraphModule::ParseHeapGraph(int64_t ts, protozero::ConstBytes blob) { |
| 93 | protos::pbzero::HeapGraph::Decoder heap_graph(blob.data, blob.size); |
| 94 | UniquePid upid = context_->process_tracker->GetOrCreateProcess( |
| 95 | static_cast<uint32_t>(heap_graph.pid())); |
| 96 | context_->heap_graph_tracker->SetPacketIndex(heap_graph.index()); |
| 97 | for (auto it = heap_graph.objects(); it; ++it) { |
| 98 | protos::pbzero::HeapGraphObject::Decoder object(*it); |
| 99 | HeapGraphTracker::SourceObject obj; |
| 100 | obj.object_id = object.id(); |
| 101 | obj.self_size = object.self_size(); |
| 102 | obj.type_id = object.type_id(); |
Florian Mayer | 905d111 | 2019-11-15 16:47:03 +0000 | [diff] [blame] | 103 | |
| 104 | std::vector<uint64_t> field_ids; |
| 105 | std::vector<uint64_t> object_ids; |
| 106 | |
| 107 | bool parse_error = ForEachVarInt< |
| 108 | protos::pbzero::HeapGraphObject::kReferenceFieldIdFieldNumber>( |
| 109 | object, [&field_ids](uint64_t value) { field_ids.push_back(value); }); |
| 110 | |
| 111 | if (!parse_error) { |
| 112 | parse_error = ForEachVarInt< |
| 113 | protos::pbzero::HeapGraphObject::kReferenceObjectIdFieldNumber>( |
| 114 | object, |
| 115 | [&object_ids](uint64_t value) { object_ids.push_back(value); }); |
Florian Mayer | a966901 | 2019-11-15 14:53:22 +0000 | [diff] [blame] | 116 | } |
Florian Mayer | acb4447 | 2019-11-15 16:46:03 +0000 | [diff] [blame] | 117 | |
Florian Mayer | 905d111 | 2019-11-15 16:47:03 +0000 | [diff] [blame] | 118 | if (parse_error) { |
| 119 | context_->storage->IncrementIndexedStats( |
| 120 | stats::heap_graph_malformed_packet, static_cast<int>(upid)); |
| 121 | break; |
| 122 | } |
| 123 | if (field_ids.size() != object_ids.size()) { |
| 124 | context_->storage->IncrementIndexedStats( |
| 125 | stats::heap_graph_malformed_packet, static_cast<int>(upid)); |
Florian Mayer | acb4447 | 2019-11-15 16:46:03 +0000 | [diff] [blame] | 126 | continue; |
| 127 | } |
Florian Mayer | 905d111 | 2019-11-15 16:47:03 +0000 | [diff] [blame] | 128 | for (size_t i = 0; i < field_ids.size(); ++i) { |
| 129 | HeapGraphTracker::SourceObject::Reference ref; |
| 130 | ref.field_name_id = field_ids[i]; |
| 131 | ref.owned_object_id = object_ids[i]; |
| 132 | obj.references.emplace_back(std::move(ref)); |
| 133 | } |
Eric Seckler | c165b87 | 2019-11-04 14:26:25 +0000 | [diff] [blame] | 134 | context_->heap_graph_tracker->AddObject(upid, ts, std::move(obj)); |
| 135 | } |
| 136 | for (auto it = heap_graph.type_names(); it; ++it) { |
| 137 | protos::pbzero::InternedString::Decoder entry(*it); |
| 138 | const char* str = reinterpret_cast<const char*>(entry.str().data); |
| 139 | auto str_view = base::StringView(str, entry.str().size); |
| 140 | |
| 141 | context_->heap_graph_tracker->AddInternedTypeName( |
| 142 | entry.iid(), context_->storage->InternString(str_view)); |
| 143 | } |
| 144 | for (auto it = heap_graph.field_names(); it; ++it) { |
| 145 | protos::pbzero::InternedString::Decoder entry(*it); |
| 146 | const char* str = reinterpret_cast<const char*>(entry.str().data); |
| 147 | auto str_view = base::StringView(str, entry.str().size); |
| 148 | |
| 149 | context_->heap_graph_tracker->AddInternedFieldName( |
| 150 | entry.iid(), context_->storage->InternString(str_view)); |
| 151 | } |
| 152 | for (auto it = heap_graph.roots(); it; ++it) { |
| 153 | protos::pbzero::HeapGraphRoot::Decoder entry(*it); |
| 154 | const char* str = HeapGraphRootTypeToString(entry.root_type()); |
| 155 | auto str_view = base::StringView(str); |
| 156 | |
| 157 | HeapGraphTracker::SourceRoot src_root; |
| 158 | src_root.root_type = context_->storage->InternString(str_view); |
Florian Mayer | 905d111 | 2019-11-15 16:47:03 +0000 | [diff] [blame] | 159 | bool parse_error = |
| 160 | ForEachVarInt<protos::pbzero::HeapGraphRoot::kObjectIdsFieldNumber>( |
| 161 | entry, [&src_root](uint64_t value) { |
| 162 | src_root.object_ids.emplace_back(value); |
| 163 | }); |
| 164 | if (parse_error) { |
| 165 | context_->storage->IncrementIndexedStats( |
| 166 | stats::heap_graph_malformed_packet, static_cast<int>(upid)); |
| 167 | break; |
| 168 | } |
Eric Seckler | c165b87 | 2019-11-04 14:26:25 +0000 | [diff] [blame] | 169 | context_->heap_graph_tracker->AddRoot(upid, ts, std::move(src_root)); |
| 170 | } |
| 171 | if (!heap_graph.continued()) { |
| 172 | context_->heap_graph_tracker->FinalizeProfile(); |
| 173 | } |
| 174 | } |
| 175 | |
Florian Mayer | e0f2b0e | 2019-11-26 14:20:36 +0000 | [diff] [blame] | 176 | void HeapGraphModule::ParseDeobfuscationMapping(protozero::ConstBytes blob) { |
| 177 | // TODO(fmayer): Support multiple profiles in the same trace. |
| 178 | protos::pbzero::DeobfuscationMapping::Decoder deobfuscation_mapping( |
| 179 | blob.data, blob.size); |
| 180 | for (auto class_it = deobfuscation_mapping.obfuscated_classes(); class_it; |
| 181 | ++class_it) { |
| 182 | protos::pbzero::ObfuscatedClass::Decoder cls(*class_it); |
| 183 | auto obfuscated_class_name_id = |
| 184 | context_->storage->string_pool().GetId(cls.obfuscated_name()); |
| 185 | if (!obfuscated_class_name_id) { |
| 186 | PERFETTO_DLOG("Class string %s not found", |
| 187 | cls.obfuscated_name().ToStdString().c_str()); |
| 188 | } else { |
| 189 | const std::vector<int64_t>* cls_objects = |
| 190 | context_->heap_graph_tracker->RowsForType(*obfuscated_class_name_id); |
| 191 | |
| 192 | if (cls_objects) { |
| 193 | auto interned_deobfuscated_name = |
| 194 | context_->storage->InternString(cls.deobfuscated_name()); |
| 195 | for (int64_t row : *cls_objects) { |
| 196 | context_->storage->mutable_heap_graph_object_table() |
| 197 | ->mutable_deobfuscated_type_name() |
| 198 | ->Set(static_cast<uint32_t>(row), interned_deobfuscated_name); |
| 199 | } |
| 200 | } else { |
| 201 | PERFETTO_DLOG("Class %s not found", |
| 202 | cls.obfuscated_name().ToStdString().c_str()); |
| 203 | } |
| 204 | } |
| 205 | for (auto member_it = cls.obfuscated_members(); member_it; ++member_it) { |
| 206 | protos::pbzero::ObfuscatedMember::Decoder member(*member_it); |
| 207 | |
| 208 | std::string merged_obfuscated = cls.obfuscated_name().ToStdString() + |
| 209 | "." + |
| 210 | member.obfuscated_name().ToStdString(); |
| 211 | std::string merged_deobfuscated = |
| 212 | cls.deobfuscated_name().ToStdString() + "." + |
| 213 | member.deobfuscated_name().ToStdString(); |
| 214 | |
| 215 | auto obfuscated_field_name_id = context_->storage->string_pool().GetId( |
| 216 | base::StringView(merged_obfuscated)); |
| 217 | if (!obfuscated_field_name_id) { |
| 218 | PERFETTO_DLOG("Field string %s not found", merged_obfuscated.c_str()); |
| 219 | continue; |
| 220 | } |
| 221 | |
| 222 | const std::vector<int64_t>* field_references = |
| 223 | context_->heap_graph_tracker->RowsForField(*obfuscated_field_name_id); |
| 224 | if (field_references) { |
| 225 | auto interned_deobfuscated_name = context_->storage->InternString( |
| 226 | base::StringView(merged_deobfuscated)); |
| 227 | for (int64_t row : *field_references) { |
| 228 | context_->storage->mutable_heap_graph_reference_table() |
| 229 | ->mutable_deobfuscated_field_name() |
| 230 | ->Set(static_cast<uint32_t>(row), interned_deobfuscated_name); |
| 231 | } |
| 232 | } else { |
| 233 | PERFETTO_DLOG("Field %s not found", merged_obfuscated.c_str()); |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | |
Eric Seckler | c165b87 | 2019-11-04 14:26:25 +0000 | [diff] [blame] | 239 | } // namespace trace_processor |
| 240 | } // namespace perfetto |