1 /*
2 * Copyright (C) 2022 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 #include "src/trace_processor/importers/proto/translation_table_module.h"
17
18 #include "src/trace_processor/importers/common/args_translation_table.h"
19 #include "src/trace_processor/importers/common/slice_translation_table.h"
20
21 #include "protos/perfetto/trace/trace_packet.pbzero.h"
22 #include "protos/perfetto/trace/translation/translation_table.pbzero.h"
23
24 namespace perfetto {
25 namespace trace_processor {
26
27 using perfetto::protos::pbzero::TracePacket;
28
TranslationTableModule(TraceProcessorContext * context)29 TranslationTableModule::TranslationTableModule(TraceProcessorContext* context)
30 : context_(context) {
31 RegisterForField(TracePacket::kTranslationTableFieldNumber, context);
32 }
33
34 TranslationTableModule::~TranslationTableModule() = default;
35
ParsePacket(const TracePacket::Decoder & decoder,const TimestampedTracePiece &,uint32_t field_id)36 void TranslationTableModule::ParsePacket(const TracePacket::Decoder& decoder,
37 const TimestampedTracePiece& /*ttp*/,
38 uint32_t field_id) {
39 if (field_id != TracePacket::kTranslationTableFieldNumber) {
40 return;
41 }
42 const auto translation_table =
43 protos::pbzero::TranslationTable::Decoder(decoder.translation_table());
44 if (translation_table.has_chrome_histogram()) {
45 ParseChromeHistogramRules(translation_table.chrome_histogram());
46 } else if (translation_table.has_chrome_user_event()) {
47 ParseChromeUserEventRules(translation_table.chrome_user_event());
48 } else if (translation_table.has_chrome_performance_mark()) {
49 ParseChromePerformanceMarkRules(
50 translation_table.chrome_performance_mark());
51 } else if (translation_table.has_slice_name()) {
52 ParseSliceNameRules(translation_table.slice_name());
53 }
54 }
55
ParseChromeHistogramRules(protozero::ConstBytes bytes)56 void TranslationTableModule::ParseChromeHistogramRules(
57 protozero::ConstBytes bytes) {
58 const auto chrome_histogram =
59 protos::pbzero::ChromeHistorgramTranslationTable::Decoder(bytes);
60 for (auto it = chrome_histogram.hash_to_name(); it; ++it) {
61 protos::pbzero::ChromeHistorgramTranslationTable::HashToNameEntry::Decoder
62 entry(*it);
63 context_->args_translation_table->AddChromeHistogramTranslationRule(
64 entry.key(), entry.value());
65 }
66 }
67
ParseChromeUserEventRules(protozero::ConstBytes bytes)68 void TranslationTableModule::ParseChromeUserEventRules(
69 protozero::ConstBytes bytes) {
70 const auto chrome_user_event =
71 protos::pbzero::ChromeUserEventTranslationTable::Decoder(bytes);
72 for (auto it = chrome_user_event.action_hash_to_name(); it; ++it) {
73 protos::pbzero::ChromeUserEventTranslationTable::ActionHashToNameEntry::
74 Decoder entry(*it);
75 context_->args_translation_table->AddChromeUserEventTranslationRule(
76 entry.key(), entry.value());
77 }
78 }
79
ParseChromePerformanceMarkRules(protozero::ConstBytes bytes)80 void TranslationTableModule::ParseChromePerformanceMarkRules(
81 protozero::ConstBytes bytes) {
82 const auto chrome_performance_mark =
83 protos::pbzero::ChromePerformanceMarkTranslationTable::Decoder(bytes);
84 for (auto it = chrome_performance_mark.site_hash_to_name(); it; ++it) {
85 protos::pbzero::ChromePerformanceMarkTranslationTable::SiteHashToNameEntry::
86 Decoder entry(*it);
87 context_->args_translation_table
88 ->AddChromePerformanceMarkSiteTranslationRule(entry.key(),
89 entry.value());
90 }
91 for (auto it = chrome_performance_mark.mark_hash_to_name(); it; ++it) {
92 protos::pbzero::ChromePerformanceMarkTranslationTable::MarkHashToNameEntry::
93 Decoder entry(*it);
94 context_->args_translation_table
95 ->AddChromePerformanceMarkMarkTranslationRule(entry.key(),
96 entry.value());
97 }
98 }
99
ParseSliceNameRules(protozero::ConstBytes bytes)100 void TranslationTableModule::ParseSliceNameRules(protozero::ConstBytes bytes) {
101 const auto slice_name =
102 protos::pbzero::SliceNameTranslationTable::Decoder(bytes);
103 for (auto it = slice_name.raw_to_deobfuscated_name(); it; ++it) {
104 protos::pbzero::SliceNameTranslationTable::RawToDeobfuscatedNameEntry::
105 Decoder entry(*it);
106 context_->slice_translation_table->AddNameTranslationRule(entry.key(),
107 entry.value());
108 }
109 }
110
111 } // namespace trace_processor
112 } // namespace perfetto
113