• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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/metadata_module.h"
18 
19 #include "perfetto/ext/base/base64.h"
20 #include "perfetto/ext/base/uuid.h"
21 #include "src/trace_processor/importers/common/metadata_tracker.h"
22 #include "src/trace_processor/importers/common/slice_tracker.h"
23 #include "src/trace_processor/importers/common/track_tracker.h"
24 #include "src/trace_processor/importers/proto/config.descriptor.h"
25 #include "src/trace_processor/util/descriptors.h"
26 #include "src/trace_processor/util/protozero_to_text.h"
27 
28 #include "protos/perfetto/config/trace_config.pbzero.h"
29 #include "protos/perfetto/trace/trace_packet.pbzero.h"
30 #include "protos/perfetto/trace/trace_uuid.pbzero.h"
31 #include "protos/perfetto/trace/trigger.pbzero.h"
32 
33 namespace perfetto {
34 namespace trace_processor {
35 
36 using perfetto::protos::pbzero::TracePacket;
37 
MetadataModule(TraceProcessorContext * context)38 MetadataModule::MetadataModule(TraceProcessorContext* context)
39     : context_(context),
40       producer_name_key_id_(context_->storage->InternString("producer_name")),
41       trusted_producer_uid_key_id_(
42           context_->storage->InternString("trusted_producer_uid")) {
43   RegisterForField(TracePacket::kUiStateFieldNumber, context);
44   RegisterForField(TracePacket::kTriggerFieldNumber, context);
45   RegisterForField(TracePacket::kTraceUuidFieldNumber, context);
46 }
47 
TokenizePacket(const protos::pbzero::TracePacket::Decoder & decoder,TraceBlobView *,int64_t,PacketSequenceState *,uint32_t field_id)48 ModuleResult MetadataModule::TokenizePacket(
49     const protos::pbzero::TracePacket::Decoder& decoder,
50     TraceBlobView*,
51     int64_t,
52     PacketSequenceState*,
53     uint32_t field_id) {
54   switch (field_id) {
55     case TracePacket::kUiStateFieldNumber: {
56       auto ui_state = decoder.ui_state();
57       std::string base64 = base::Base64Encode(ui_state.data, ui_state.size);
58       StringId id = context_->storage->InternString(base::StringView(base64));
59       context_->metadata_tracker->SetMetadata(metadata::ui_state,
60                                               Variadic::String(id));
61       return ModuleResult::Handled();
62     }
63     case TracePacket::kTraceUuidFieldNumber: {
64       // If both the TraceUuid packet and TraceConfig.trace_uuid_msb/lsb are
65       // set, the former (which is emitted first) takes precedence. This is
66       // because the UUID can change throughout the lifecycle of a tracing
67       // session if gap-less snapshots are used. Each trace file has at most one
68       // TraceUuid packet (i has if it comes from an older version of the
69       // tracing service < v32)
70       protos::pbzero::TraceUuid::Decoder uuid_packet(decoder.trace_uuid());
71       if (uuid_packet.msb() != 0 || uuid_packet.lsb() != 0) {
72         base::Uuid uuid(uuid_packet.lsb(), uuid_packet.msb());
73         std::string str = uuid.ToPrettyString();
74         StringId id = context_->storage->InternString(base::StringView(str));
75         context_->metadata_tracker->SetMetadata(metadata::trace_uuid,
76                                                 Variadic::String(id));
77         context_->uuid_found_in_trace = true;
78       }
79       return ModuleResult::Handled();
80     }
81   }
82   return ModuleResult::Ignored();
83 }
84 
ParseTracePacketData(const protos::pbzero::TracePacket::Decoder & decoder,int64_t ts,const TracePacketData &,uint32_t field_id)85 void MetadataModule::ParseTracePacketData(
86     const protos::pbzero::TracePacket::Decoder& decoder,
87     int64_t ts,
88     const TracePacketData&,
89     uint32_t field_id) {
90   if (field_id == TracePacket::kTriggerFieldNumber) {
91     // We handle triggers at parse time rather at tokenization because
92     // we add slices to tables which need to happen post-sorting.
93     ParseTrigger(ts, decoder.trigger());
94   }
95 }
96 
ParseTrigger(int64_t ts,ConstBytes blob)97 void MetadataModule::ParseTrigger(int64_t ts, ConstBytes blob) {
98   protos::pbzero::Trigger::Decoder trigger(blob.data, blob.size);
99   StringId cat_id = kNullStringId;
100   TrackId track_id = context_->track_tracker->GetOrCreateTriggerTrack();
101   StringId name_id = context_->storage->InternString(trigger.trigger_name());
102   context_->slice_tracker->Scoped(
103       ts, track_id, cat_id, name_id,
104       /* duration = */ 0,
105       [&trigger, this](ArgsTracker::BoundInserter* args_table) {
106         StringId producer_name =
107             context_->storage->InternString(trigger.producer_name());
108         if (!producer_name.is_null()) {
109           args_table->AddArg(producer_name_key_id_,
110                              Variadic::String(producer_name));
111         }
112         if (trigger.has_trusted_producer_uid()) {
113           args_table->AddArg(trusted_producer_uid_key_id_,
114                              Variadic::Integer(trigger.trusted_producer_uid()));
115         }
116       });
117 }
118 
ParseTraceUuid(ConstBytes blob)119 void MetadataModule::ParseTraceUuid(ConstBytes blob) {
120   // If both the TraceUuid packet and TraceConfig.trace_uuid_msb/lsb are set,
121   // the former (which is emitted first) takes precedence. This is because the
122   // UUID can change throughout the lifecycle of a tracing session if gap-less
123   // snapshots are used. Each trace file has at most one TraceUuid packet (i
124   // has if it comes from an older version of the tracing service < v32)
125   protos::pbzero::TraceUuid::Decoder uuid_packet(blob.data, blob.size);
126   if (uuid_packet.msb() != 0 || uuid_packet.lsb() != 0) {
127     base::Uuid uuid(uuid_packet.lsb(), uuid_packet.msb());
128     std::string str = uuid.ToPrettyString();
129     StringId id = context_->storage->InternString(base::StringView(str));
130     context_->metadata_tracker->SetMetadata(metadata::trace_uuid,
131                                             Variadic::String(id));
132     context_->uuid_found_in_trace = true;
133   }
134 }
135 
ParseTraceConfig(const protos::pbzero::TraceConfig_Decoder & trace_config)136 void MetadataModule::ParseTraceConfig(
137     const protos::pbzero::TraceConfig_Decoder& trace_config) {
138   int64_t uuid_msb = trace_config.trace_uuid_msb();
139   int64_t uuid_lsb = trace_config.trace_uuid_lsb();
140   if (!context_->uuid_found_in_trace && (uuid_msb != 0 || uuid_lsb != 0)) {
141     base::Uuid uuid(uuid_lsb, uuid_msb);
142     std::string str = uuid.ToPrettyString();
143     StringId id = context_->storage->InternString(base::StringView(str));
144     context_->metadata_tracker->SetMetadata(metadata::trace_uuid,
145                                             Variadic::String(id));
146     context_->uuid_found_in_trace = true;
147   }
148 
149   if (trace_config.has_unique_session_name()) {
150     StringId id = context_->storage->InternString(
151         base::StringView(trace_config.unique_session_name()));
152     context_->metadata_tracker->SetMetadata(metadata::unique_session_name,
153                                             Variadic::String(id));
154   }
155 
156   DescriptorPool pool;
157   pool.AddFromFileDescriptorSet(kConfigDescriptor.data(),
158                                 kConfigDescriptor.size());
159 
160   std::string text = protozero_to_text::ProtozeroToText(
161       pool, ".perfetto.protos.TraceConfig",
162       protozero::ConstBytes{
163           trace_config.begin(),
164           static_cast<uint32_t>(trace_config.end() - trace_config.begin())},
165       protozero_to_text::kIncludeNewLines);
166   StringId id = context_->storage->InternString(base::StringView(text));
167   context_->metadata_tracker->SetMetadata(metadata::trace_config_pbtxt,
168                                           Variadic::String(id));
169 }
170 
171 }  // namespace trace_processor
172 }  // namespace perfetto
173