• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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/profile_module.h"
18 #include <string>
19 
20 #include "perfetto/base/logging.h"
21 #include "perfetto/ext/base/flat_hash_map.h"
22 #include "perfetto/ext/base/string_utils.h"
23 #include "src/trace_processor/importers/common/args_translation_table.h"
24 #include "src/trace_processor/importers/common/clock_tracker.h"
25 #include "src/trace_processor/importers/common/deobfuscation_mapping_table.h"
26 #include "src/trace_processor/importers/common/event_tracker.h"
27 #include "src/trace_processor/importers/common/mapping_tracker.h"
28 #include "src/trace_processor/importers/common/process_tracker.h"
29 #include "src/trace_processor/importers/common/stack_profile_tracker.h"
30 #include "src/trace_processor/importers/proto/packet_sequence_state_generation.h"
31 #include "src/trace_processor/importers/proto/perf_sample_tracker.h"
32 #include "src/trace_processor/importers/proto/profile_packet_sequence_state.h"
33 #include "src/trace_processor/importers/proto/profile_packet_utils.h"
34 #include "src/trace_processor/importers/proto/stack_profile_sequence_state.h"
35 #include "src/trace_processor/sorter/trace_sorter.h"
36 #include "src/trace_processor/storage/stats.h"
37 #include "src/trace_processor/storage/trace_storage.h"
38 #include "src/trace_processor/tables/profiler_tables_py.h"
39 #include "src/trace_processor/types/trace_processor_context.h"
40 #include "src/trace_processor/util/build_id.h"
41 #include "src/trace_processor/util/profiler_util.h"
42 
43 #include "protos/perfetto/common/builtin_clock.pbzero.h"
44 #include "protos/perfetto/common/perf_events.pbzero.h"
45 #include "protos/perfetto/trace/profiling/deobfuscation.pbzero.h"
46 #include "protos/perfetto/trace/profiling/profile_common.pbzero.h"
47 #include "protos/perfetto/trace/profiling/profile_packet.pbzero.h"
48 #include "protos/perfetto/trace/profiling/smaps.pbzero.h"
49 
50 namespace perfetto {
51 namespace trace_processor {
52 
53 using perfetto::protos::pbzero::TracePacket;
54 using protozero::ConstBytes;
55 
ProfileModule(TraceProcessorContext * context)56 ProfileModule::ProfileModule(TraceProcessorContext* context)
57     : context_(context) {
58   RegisterForField(TracePacket::kStreamingProfilePacketFieldNumber, context);
59   RegisterForField(TracePacket::kPerfSampleFieldNumber, context);
60   RegisterForField(TracePacket::kProfilePacketFieldNumber, context);
61   RegisterForField(TracePacket::kModuleSymbolsFieldNumber, context);
62   // note: deobfuscation mappings also handled by HeapGraphModule.
63   RegisterForField(TracePacket::kDeobfuscationMappingFieldNumber, context);
64   RegisterForField(TracePacket::kSmapsPacketFieldNumber, context);
65 }
66 
67 ProfileModule::~ProfileModule() = default;
68 
TokenizePacket(const TracePacket::Decoder & decoder,TraceBlobView * packet,int64_t,RefPtr<PacketSequenceStateGeneration> state,uint32_t field_id)69 ModuleResult ProfileModule::TokenizePacket(
70     const TracePacket::Decoder& decoder,
71     TraceBlobView* packet,
72     int64_t /*packet_timestamp*/,
73     RefPtr<PacketSequenceStateGeneration> state,
74     uint32_t field_id) {
75   switch (field_id) {
76     case TracePacket::kStreamingProfilePacketFieldNumber:
77       return TokenizeStreamingProfilePacket(std::move(state), packet,
78                                             decoder.streaming_profile_packet());
79   }
80   return ModuleResult::Ignored();
81 }
82 
ParseTracePacketData(const protos::pbzero::TracePacket::Decoder & decoder,int64_t ts,const TracePacketData & data,uint32_t field_id)83 void ProfileModule::ParseTracePacketData(
84     const protos::pbzero::TracePacket::Decoder& decoder,
85     int64_t ts,
86     const TracePacketData& data,
87     uint32_t field_id) {
88   switch (field_id) {
89     case TracePacket::kStreamingProfilePacketFieldNumber:
90       ParseStreamingProfilePacket(ts, data.sequence_state.get(),
91                                   decoder.streaming_profile_packet());
92       return;
93     case TracePacket::kPerfSampleFieldNumber:
94       ParsePerfSample(ts, data.sequence_state.get(), decoder);
95       return;
96     case TracePacket::kProfilePacketFieldNumber:
97       ParseProfilePacket(ts, data.sequence_state.get(),
98                          decoder.profile_packet());
99       return;
100     case TracePacket::kModuleSymbolsFieldNumber:
101       ParseModuleSymbols(decoder.module_symbols());
102       return;
103     case TracePacket::kDeobfuscationMappingFieldNumber:
104       ParseDeobfuscationMapping(ts, data.sequence_state.get(),
105                                 decoder.trusted_packet_sequence_id(),
106                                 decoder.deobfuscation_mapping());
107       return;
108     case TracePacket::kSmapsPacketFieldNumber:
109       ParseSmapsPacket(ts, decoder.smaps_packet());
110       return;
111   }
112 }
113 
TokenizeStreamingProfilePacket(RefPtr<PacketSequenceStateGeneration> sequence_state,TraceBlobView * packet,ConstBytes streaming_profile_packet)114 ModuleResult ProfileModule::TokenizeStreamingProfilePacket(
115     RefPtr<PacketSequenceStateGeneration> sequence_state,
116     TraceBlobView* packet,
117     ConstBytes streaming_profile_packet) {
118   protos::pbzero::StreamingProfilePacket::Decoder decoder(
119       streaming_profile_packet.data, streaming_profile_packet.size);
120 
121   // We have to resolve the reference timestamp of a StreamingProfilePacket
122   // during tokenization. If we did this during parsing instead, the
123   // tokenization of a subsequent ThreadDescriptor with a new reference
124   // timestamp would cause us to later calculate timestamps based on the wrong
125   // reference value during parsing. Since StreamingProfilePackets only need to
126   // be sorted correctly with respect to process/thread metadata events (so that
127   // pid/tid are resolved correctly during parsing), we forward the packet as a
128   // whole through the sorter, using the "root" timestamp of the packet, i.e.
129   // the current timestamp of the packet sequence.
130   auto packet_ts =
131       sequence_state->IncrementAndGetTrackEventTimeNs(/*delta_ns=*/0);
132   base::StatusOr<int64_t> trace_ts = context_->clock_tracker->ToTraceTime(
133       protos::pbzero::BUILTIN_CLOCK_MONOTONIC, packet_ts);
134   if (trace_ts.ok())
135     packet_ts = *trace_ts;
136 
137   // Increment the sequence's timestamp by all deltas.
138   for (auto timestamp_it = decoder.timestamp_delta_us(); timestamp_it;
139        ++timestamp_it) {
140     sequence_state->IncrementAndGetTrackEventTimeNs(*timestamp_it * 1000);
141   }
142 
143   context_->sorter->PushTracePacket(packet_ts, std::move(sequence_state),
144                                     std::move(*packet), context_->machine_id());
145   return ModuleResult::Handled();
146 }
147 
ParseStreamingProfilePacket(int64_t timestamp,PacketSequenceStateGeneration * sequence_state,ConstBytes streaming_profile_packet)148 void ProfileModule::ParseStreamingProfilePacket(
149     int64_t timestamp,
150     PacketSequenceStateGeneration* sequence_state,
151     ConstBytes streaming_profile_packet) {
152   protos::pbzero::StreamingProfilePacket::Decoder packet(
153       streaming_profile_packet.data, streaming_profile_packet.size);
154 
155   ProcessTracker* procs = context_->process_tracker.get();
156   TraceStorage* storage = context_->storage.get();
157   StackProfileSequenceState& stack_profile_sequence_state =
158       *sequence_state->GetCustomState<StackProfileSequenceState>();
159 
160   uint32_t pid = static_cast<uint32_t>(sequence_state->pid());
161   uint32_t tid = static_cast<uint32_t>(sequence_state->tid());
162   const UniqueTid utid = procs->UpdateThread(tid, pid);
163   const UniquePid upid = procs->GetOrCreateProcess(pid);
164 
165   // Iterate through timestamps and callstacks simultaneously.
166   auto timestamp_it = packet.timestamp_delta_us();
167   for (auto callstack_it = packet.callstack_iid(); callstack_it;
168        ++callstack_it, ++timestamp_it) {
169     if (!timestamp_it) {
170       context_->storage->IncrementStats(stats::stackprofile_parser_error);
171       PERFETTO_ELOG(
172           "StreamingProfilePacket has less callstack IDs than timestamps!");
173       break;
174     }
175 
176     auto opt_cs_id =
177         stack_profile_sequence_state.FindOrInsertCallstack(upid, *callstack_it);
178     if (!opt_cs_id) {
179       context_->storage->IncrementStats(stats::stackprofile_parser_error);
180       continue;
181     }
182 
183     // Resolve the delta timestamps based on the packet's root timestamp.
184     timestamp += *timestamp_it * 1000;
185 
186     tables::CpuProfileStackSampleTable::Row sample_row{
187         timestamp, *opt_cs_id, utid, packet.process_priority()};
188     storage->mutable_cpu_profile_stack_sample_table()->Insert(sample_row);
189   }
190 }
191 
ParsePerfSample(int64_t ts,PacketSequenceStateGeneration * sequence_state,const TracePacket::Decoder & decoder)192 void ProfileModule::ParsePerfSample(
193     int64_t ts,
194     PacketSequenceStateGeneration* sequence_state,
195     const TracePacket::Decoder& decoder) {
196   using PerfSample = protos::pbzero::PerfSample;
197   const auto& sample_raw = decoder.perf_sample();
198   PerfSample::Decoder sample(sample_raw.data, sample_raw.size);
199 
200   uint32_t seq_id = decoder.trusted_packet_sequence_id();
201   PerfSampleTracker::SamplingStreamInfo sampling_stream =
202       context_->perf_sample_tracker->GetSamplingStreamInfo(
203           seq_id, sample.cpu(), sequence_state->GetTracePacketDefaults());
204 
205   // Not a sample, but an indication of data loss in the ring buffer shared with
206   // the kernel.
207   if (sample.kernel_records_lost() > 0) {
208     PERFETTO_DCHECK(sample.pid() == 0);
209 
210     context_->storage->IncrementIndexedStats(
211         stats::perf_cpu_lost_records, static_cast<int>(sample.cpu()),
212         static_cast<int64_t>(sample.kernel_records_lost()));
213     return;
214   }
215 
216   // Sample that looked relevant for the tracing session, but had to be skipped.
217   // Either we failed to look up the procfs file descriptors necessary for
218   // remote stack unwinding (not unexpected in most cases), or the unwind queue
219   // was out of capacity (producer lost data on its own).
220   if (sample.has_sample_skipped_reason()) {
221     context_->storage->IncrementStats(stats::perf_samples_skipped);
222 
223     if (sample.sample_skipped_reason() ==
224         PerfSample::PROFILER_SKIP_UNWIND_ENQUEUE)
225       context_->storage->IncrementStats(stats::perf_samples_skipped_dataloss);
226 
227     return;
228   }
229 
230   // Not a sample, but an event from the producer.
231   // TODO(rsavitski): this stat is indexed by the session id, but the older
232   // stats (see above) aren't. The indexing is relevant if a trace contains more
233   // than one profiling data source. So the older stats should be changed to
234   // being indexed as well.
235   if (sample.has_producer_event()) {
236     PerfSample::ProducerEvent::Decoder producer_event(sample.producer_event());
237     if (producer_event.source_stop_reason() ==
238         PerfSample::ProducerEvent::PROFILER_STOP_GUARDRAIL) {
239       context_->storage->SetIndexedStats(
240           stats::perf_guardrail_stop_ts,
241           static_cast<int>(sampling_stream.perf_session_id.value), ts);
242     }
243     return;
244   }
245 
246   // Proper sample, populate the |perf_sample| table with everything except the
247   // recorded counter values, which go to |counter|.
248   context_->event_tracker->PushCounter(
249       ts, static_cast<double>(sample.timebase_count()),
250       sampling_stream.timebase_track_id);
251 
252   const UniqueTid utid =
253       context_->process_tracker->UpdateThread(sample.tid(), sample.pid());
254   const UniquePid upid =
255       context_->process_tracker->GetOrCreateProcess(sample.pid());
256 
257   StackProfileSequenceState& stack_profile_sequence_state =
258       *sequence_state->GetCustomState<StackProfileSequenceState>();
259   uint64_t callstack_iid = sample.callstack_iid();
260   std::optional<CallsiteId> cs_id =
261       stack_profile_sequence_state.FindOrInsertCallstack(upid, callstack_iid);
262 
263   // A failed lookup of the interned callstack can mean either:
264   // (a) This is a counter-only profile without callstacks. Due to an
265   //     implementation quirk, these packets still set callstack_iid
266   //     corresponding to a callstack with no frames. To reliably identify this
267   //     case (without resorting to config parsing) we further need to rely on
268   //     the fact that the implementation (callstack_trie.h) always assigns this
269   //     callstack the id "1". Such callstacks should not occur outside of
270   //     counter-only profiles, as there should always be at least a synthetic
271   //     error frame if the unwinding completely failed.
272   // (b) This is a ring-buffer profile where some of the referenced internings
273   //     have been overwritten, and the build predates perf_sample_defaults and
274   //     SEQ_NEEDS_INCREMENTAL_STATE sequence flag in perf_sample packets.
275   //     Such packets should be discarded.
276   if (!cs_id && callstack_iid != 1) {
277     PERFETTO_DLOG("Discarding perf_sample since callstack_iid [%" PRIu64
278                   "] references a missing/partially lost interning according "
279                   "to stack_profile_tracker",
280                   callstack_iid);
281     return;
282   }
283 
284   using protos::pbzero::Profiling;
285   TraceStorage* storage = context_->storage.get();
286 
287   auto cpu_mode = static_cast<Profiling::CpuMode>(sample.cpu_mode());
288   StringPool::Id cpu_mode_id =
289       storage->InternString(ProfilePacketUtils::StringifyCpuMode(cpu_mode));
290 
291   std::optional<StringPool::Id> unwind_error_id;
292   if (sample.has_unwind_error()) {
293     auto unwind_error =
294         static_cast<Profiling::StackUnwindError>(sample.unwind_error());
295     unwind_error_id = storage->InternString(
296         ProfilePacketUtils::StringifyStackUnwindError(unwind_error));
297   }
298   tables::PerfSampleTable::Row sample_row(ts, utid, sample.cpu(), cpu_mode_id,
299                                           cs_id, unwind_error_id,
300                                           sampling_stream.perf_session_id);
301   context_->storage->mutable_perf_sample_table()->Insert(sample_row);
302 }
303 
ParseProfilePacket(int64_t ts,PacketSequenceStateGeneration * sequence_state,ConstBytes blob)304 void ProfileModule::ParseProfilePacket(
305     int64_t ts,
306     PacketSequenceStateGeneration* sequence_state,
307     ConstBytes blob) {
308   ProfilePacketSequenceState& profile_packet_sequence_state =
309       *sequence_state->GetCustomState<ProfilePacketSequenceState>();
310   protos::pbzero::ProfilePacket::Decoder packet(blob.data, blob.size);
311   profile_packet_sequence_state.SetProfilePacketIndex(packet.index());
312 
313   for (auto it = packet.strings(); it; ++it) {
314     protos::pbzero::InternedString::Decoder entry(*it);
315     const char* str = reinterpret_cast<const char*>(entry.str().data);
316     auto str_view = base::StringView(str, entry.str().size);
317     profile_packet_sequence_state.AddString(entry.iid(), str_view);
318   }
319 
320   for (auto it = packet.mappings(); it; ++it) {
321     protos::pbzero::Mapping::Decoder entry(*it);
322     profile_packet_sequence_state.AddMapping(
323         entry.iid(), ProfilePacketUtils::MakeSourceMapping(entry));
324   }
325 
326   for (auto it = packet.frames(); it; ++it) {
327     protos::pbzero::Frame::Decoder entry(*it);
328     profile_packet_sequence_state.AddFrame(
329         entry.iid(), ProfilePacketUtils::MakeSourceFrame(entry));
330   }
331 
332   for (auto it = packet.callstacks(); it; ++it) {
333     protos::pbzero::Callstack::Decoder entry(*it);
334     profile_packet_sequence_state.AddCallstack(
335         entry.iid(), ProfilePacketUtils::MakeSourceCallstack(entry));
336   }
337 
338   for (auto it = packet.process_dumps(); it; ++it) {
339     protos::pbzero::ProfilePacket::ProcessHeapSamples::Decoder entry(*it);
340 
341     base::StatusOr<int64_t> maybe_timestamp =
342         context_->clock_tracker->ToTraceTime(
343             protos::pbzero::BUILTIN_CLOCK_MONOTONIC_COARSE,
344             static_cast<int64_t>(entry.timestamp()));
345 
346     // ToTraceTime() increments the clock_sync_failure error stat in this case.
347     if (!maybe_timestamp.ok())
348       continue;
349 
350     int64_t timestamp = *maybe_timestamp;
351 
352     int pid = static_cast<int>(entry.pid());
353     context_->storage->SetIndexedStats(stats::heapprofd_last_profile_timestamp,
354                                        pid, ts);
355 
356     if (entry.disconnected())
357       context_->storage->IncrementIndexedStats(
358           stats::heapprofd_client_disconnected, pid);
359     if (entry.buffer_corrupted())
360       context_->storage->IncrementIndexedStats(
361           stats::heapprofd_buffer_corrupted, pid);
362     if (entry.buffer_overran() ||
363         entry.client_error() ==
364             protos::pbzero::ProfilePacket::ProcessHeapSamples::
365                 CLIENT_ERROR_HIT_TIMEOUT) {
366       context_->storage->IncrementIndexedStats(stats::heapprofd_buffer_overran,
367                                                pid);
368     }
369     if (entry.client_error()) {
370       context_->storage->SetIndexedStats(stats::heapprofd_client_error, pid,
371                                          entry.client_error());
372     }
373     if (entry.rejected_concurrent())
374       context_->storage->IncrementIndexedStats(
375           stats::heapprofd_rejected_concurrent, pid);
376     if (entry.hit_guardrail())
377       context_->storage->IncrementIndexedStats(stats::heapprofd_hit_guardrail,
378                                                pid);
379     if (entry.orig_sampling_interval_bytes()) {
380       context_->storage->SetIndexedStats(
381           stats::heapprofd_sampling_interval_adjusted, pid,
382           static_cast<int64_t>(entry.sampling_interval_bytes()) -
383               static_cast<int64_t>(entry.orig_sampling_interval_bytes()));
384     }
385 
386     protos::pbzero::ProfilePacket::ProcessStats::Decoder stats(entry.stats());
387     context_->storage->IncrementIndexedStats(
388         stats::heapprofd_unwind_time_us, static_cast<int>(entry.pid()),
389         static_cast<int64_t>(stats.total_unwinding_time_us()));
390     context_->storage->IncrementIndexedStats(
391         stats::heapprofd_unwind_samples, static_cast<int>(entry.pid()),
392         static_cast<int64_t>(stats.heap_samples()));
393     context_->storage->IncrementIndexedStats(
394         stats::heapprofd_client_spinlock_blocked, static_cast<int>(entry.pid()),
395         static_cast<int64_t>(stats.client_spinlock_blocked_us()));
396 
397     // orig_sampling_interval_bytes was introduced slightly after a bug with
398     // self_max_count was fixed in the producer. We use this as a proxy
399     // whether or not we are getting this data from a fixed producer or not.
400     bool trustworthy_max_count = entry.orig_sampling_interval_bytes() > 0;
401 
402     for (auto sample_it = entry.samples(); sample_it; ++sample_it) {
403       protos::pbzero::ProfilePacket::HeapSample::Decoder sample(*sample_it);
404 
405       ProfilePacketSequenceState::SourceAllocation src_allocation;
406       src_allocation.pid = entry.pid();
407       if (entry.heap_name().size != 0) {
408         src_allocation.heap_name =
409             context_->storage->InternString(entry.heap_name());
410       } else {
411         // After aosp/1348782 there should be a heap name associated with all
412         // allocations - absence of one is likely a bug (for traces captured
413         // in older builds, this was the native heap profiler (libc.malloc)).
414         src_allocation.heap_name = context_->storage->InternString("unknown");
415       }
416       src_allocation.timestamp = timestamp;
417       src_allocation.callstack_id = sample.callstack_id();
418       if (sample.has_self_max()) {
419         src_allocation.self_allocated = sample.self_max();
420         if (trustworthy_max_count)
421           src_allocation.alloc_count = sample.self_max_count();
422       } else {
423         src_allocation.self_allocated = sample.self_allocated();
424         src_allocation.self_freed = sample.self_freed();
425         src_allocation.alloc_count = sample.alloc_count();
426         src_allocation.free_count = sample.free_count();
427       }
428 
429       profile_packet_sequence_state.StoreAllocation(src_allocation);
430     }
431   }
432   if (!packet.continued()) {
433     profile_packet_sequence_state.FinalizeProfile();
434   }
435 }
436 
ParseModuleSymbols(ConstBytes blob)437 void ProfileModule::ParseModuleSymbols(ConstBytes blob) {
438   protos::pbzero::ModuleSymbols::Decoder module_symbols(blob.data, blob.size);
439   BuildId build_id = BuildId::FromRaw(module_symbols.build_id());
440 
441   auto mappings =
442       context_->mapping_tracker->FindMappings(module_symbols.path(), build_id);
443   if (mappings.empty()) {
444     context_->storage->IncrementStats(stats::stackprofile_invalid_mapping_id);
445     return;
446   }
447   for (auto addr_it = module_symbols.address_symbols(); addr_it; ++addr_it) {
448     protos::pbzero::AddressSymbols::Decoder address_symbols(*addr_it);
449 
450     uint32_t symbol_set_id = context_->storage->symbol_table().row_count();
451 
452     bool has_lines = false;
453     // Taking the last (i.e. the least interned) location if there're several.
454     ArgsTranslationTable::SourceLocation last_location;
455     for (auto line_it = address_symbols.lines(); line_it; ++line_it) {
456       protos::pbzero::Line::Decoder line(*line_it);
457       context_->storage->mutable_symbol_table()->Insert(
458           {symbol_set_id, context_->storage->InternString(line.function_name()),
459            context_->storage->InternString(line.source_file_name()),
460            line.line_number()});
461       last_location = ArgsTranslationTable::SourceLocation{
462           line.source_file_name().ToStdString(),
463           line.function_name().ToStdString(), line.line_number()};
464       has_lines = true;
465     }
466     if (!has_lines) {
467       continue;
468     }
469     bool frame_found = false;
470     for (VirtualMemoryMapping* mapping : mappings) {
471       context_->args_translation_table->AddNativeSymbolTranslationRule(
472           mapping->mapping_id(), address_symbols.address(), last_location);
473       std::vector<FrameId> frame_ids =
474           mapping->FindFrameIds(address_symbols.address());
475 
476       for (const FrameId frame_id : frame_ids) {
477         auto* frames = context_->storage->mutable_stack_profile_frame_table();
478         uint32_t frame_row = *frames->id().IndexOf(frame_id);
479         frames->mutable_symbol_set_id()->Set(frame_row, symbol_set_id);
480         frame_found = true;
481       }
482     }
483 
484     if (!frame_found) {
485       context_->storage->IncrementStats(stats::stackprofile_invalid_frame_id);
486       continue;
487     }
488   }
489 }
490 
ParseDeobfuscationMapping(int64_t,PacketSequenceStateGeneration *,uint32_t,ConstBytes blob)491 void ProfileModule::ParseDeobfuscationMapping(int64_t,
492                                               PacketSequenceStateGeneration*,
493                                               uint32_t /* seq_id */,
494                                               ConstBytes blob) {
495   DeobfuscationMappingTable deobfuscation_mapping_table;
496   protos::pbzero::DeobfuscationMapping::Decoder deobfuscation_mapping(
497       blob.data, blob.size);
498   if (deobfuscation_mapping.package_name().size == 0)
499     return;
500 
501   auto opt_package_name_id = context_->storage->string_pool().GetId(
502       deobfuscation_mapping.package_name());
503   auto opt_memfd_id = context_->storage->string_pool().GetId("memfd");
504   if (!opt_package_name_id && !opt_memfd_id)
505     return;
506 
507   for (auto class_it = deobfuscation_mapping.obfuscated_classes(); class_it;
508        ++class_it) {
509     protos::pbzero::ObfuscatedClass::Decoder cls(*class_it);
510     base::FlatHashMap<StringId, StringId> obfuscated_to_deobfuscated_members;
511     for (auto member_it = cls.obfuscated_methods(); member_it; ++member_it) {
512       protos::pbzero::ObfuscatedMember::Decoder member(*member_it);
513       std::string merged_obfuscated = cls.obfuscated_name().ToStdString() +
514                                       "." +
515                                       member.obfuscated_name().ToStdString();
516       auto merged_obfuscated_id = context_->storage->string_pool().GetId(
517           base::StringView(merged_obfuscated));
518       if (!merged_obfuscated_id)
519         continue;
520       std::string merged_deobfuscated =
521           FullyQualifiedDeobfuscatedName(cls, member);
522 
523       std::vector<tables::StackProfileFrameTable::Id> frames;
524       if (opt_package_name_id) {
525         const std::vector<tables::StackProfileFrameTable::Id> pkg_frames =
526             context_->stack_profile_tracker->JavaFramesForName(
527                 {*merged_obfuscated_id, *opt_package_name_id});
528         frames.insert(frames.end(), pkg_frames.begin(), pkg_frames.end());
529       }
530       if (opt_memfd_id) {
531         const std::vector<tables::StackProfileFrameTable::Id> memfd_frames =
532             context_->stack_profile_tracker->JavaFramesForName(
533                 {*merged_obfuscated_id, *opt_memfd_id});
534         frames.insert(frames.end(), memfd_frames.begin(), memfd_frames.end());
535       }
536 
537       for (tables::StackProfileFrameTable::Id frame_id : frames) {
538         auto* frames_tbl =
539             context_->storage->mutable_stack_profile_frame_table();
540         frames_tbl->mutable_deobfuscated_name()->Set(
541             *frames_tbl->id().IndexOf(frame_id),
542             context_->storage->InternString(
543                 base::StringView(merged_deobfuscated)));
544       }
545       obfuscated_to_deobfuscated_members[context_->storage->InternString(
546           member.obfuscated_name())] =
547           context_->storage->InternString(member.deobfuscated_name());
548     }
549     // Members can contain a class name (e.g "ClassA.FunctionF")
550     deobfuscation_mapping_table.AddClassTranslation(
551         DeobfuscationMappingTable::PackageId{
552             deobfuscation_mapping.package_name().ToStdString(),
553             deobfuscation_mapping.version_code()},
554         context_->storage->InternString(cls.obfuscated_name()),
555         context_->storage->InternString(cls.deobfuscated_name()),
556         std::move(obfuscated_to_deobfuscated_members));
557   }
558   context_->args_translation_table->AddDeobfuscationMappingTable(
559       std::move(deobfuscation_mapping_table));
560 }
561 
ParseSmapsPacket(int64_t ts,ConstBytes blob)562 void ProfileModule::ParseSmapsPacket(int64_t ts, ConstBytes blob) {
563   protos::pbzero::SmapsPacket::Decoder sp(blob.data, blob.size);
564   auto upid = context_->process_tracker->GetOrCreateProcess(sp.pid());
565 
566   for (auto it = sp.entries(); it; ++it) {
567     protos::pbzero::SmapsEntry::Decoder e(*it);
568     context_->storage->mutable_profiler_smaps_table()->Insert(
569         {upid, ts, context_->storage->InternString(e.path()),
570          static_cast<int64_t>(e.size_kb()),
571          static_cast<int64_t>(e.private_dirty_kb()),
572          static_cast<int64_t>(e.swap_kb()),
573          context_->storage->InternString(e.file_name()),
574          static_cast<int64_t>(e.start_address()),
575          static_cast<int64_t>(e.module_timestamp()),
576          context_->storage->InternString(e.module_debugid()),
577          context_->storage->InternString(e.module_debug_path()),
578          static_cast<int32_t>(e.protection_flags()),
579          static_cast<int64_t>(e.private_clean_resident_kb()),
580          static_cast<int64_t>(e.shared_dirty_resident_kb()),
581          static_cast<int64_t>(e.shared_clean_resident_kb()),
582          static_cast<int64_t>(e.locked_kb()),
583          static_cast<int64_t>(e.proportional_resident_kb())});
584   }
585 }
586 
NotifyEndOfFile()587 void ProfileModule::NotifyEndOfFile() {
588   for (auto it = context_->storage->stack_profile_mapping_table().IterateRows();
589        it; ++it) {
590     NullTermStringView path = context_->storage->GetString(it.name());
591     NullTermStringView build_id = context_->storage->GetString(it.build_id());
592 
593     if (path.StartsWith("/data/local/tmp/") && build_id.empty()) {
594       context_->storage->IncrementStats(
595           stats::symbolization_tmp_build_id_not_found);
596     }
597   }
598 }
599 
600 }  // namespace trace_processor
601 }  // namespace perfetto
602