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 #include "src/trace_processor/importers/proto/track_event_module.h"
17 
18 #include "perfetto/base/build_config.h"
19 #include "perfetto/ext/base/string_utils.h"
20 #include "src/trace_processor/importers/common/track_tracker.h"
21 #include "src/trace_processor/importers/proto/track_event_tracker.h"
22 #include "src/trace_processor/timestamped_trace_piece.h"
23 #include "src/trace_processor/types/trace_processor_context.h"
24 
25 #include "protos/perfetto/config/data_source_config.pbzero.h"
26 #include "protos/perfetto/config/trace_config.pbzero.h"
27 #include "protos/perfetto/trace/trace_packet.pbzero.h"
28 
29 namespace perfetto {
30 namespace trace_processor {
31 
32 using perfetto::protos::pbzero::TracePacket;
33 
TrackEventModule(TraceProcessorContext * context)34 TrackEventModule::TrackEventModule(TraceProcessorContext* context)
35     : track_event_tracker_(new TrackEventTracker(context)),
36       tokenizer_(context, track_event_tracker_.get()),
37       parser_(context, track_event_tracker_.get()) {
38   RegisterForField(TracePacket::kTrackEventFieldNumber, context);
39   RegisterForField(TracePacket::kTrackDescriptorFieldNumber, context);
40   RegisterForField(TracePacket::kThreadDescriptorFieldNumber, context);
41   RegisterForField(TracePacket::kProcessDescriptorFieldNumber, context);
42 }
43 
44 TrackEventModule::~TrackEventModule() = default;
45 
TokenizePacket(const TracePacket::Decoder & decoder,TraceBlobView * packet,int64_t packet_timestamp,PacketSequenceState * state,uint32_t field_id)46 ModuleResult TrackEventModule::TokenizePacket(
47     const TracePacket::Decoder& decoder,
48     TraceBlobView* packet,
49     int64_t packet_timestamp,
50     PacketSequenceState* state,
51     uint32_t field_id) {
52   switch (field_id) {
53     case TracePacket::kTrackDescriptorFieldNumber:
54       return tokenizer_.TokenizeTrackDescriptorPacket(state, decoder,
55                                                       packet_timestamp);
56     case TracePacket::kTrackEventFieldNumber:
57       tokenizer_.TokenizeTrackEventPacket(state, decoder, packet,
58                                           packet_timestamp);
59       return ModuleResult::Handled();
60     case TracePacket::kThreadDescriptorFieldNumber:
61       // TODO(eseckler): Remove once Chrome has switched to TrackDescriptors.
62       return tokenizer_.TokenizeThreadDescriptorPacket(state, decoder);
63   }
64   return ModuleResult::Ignored();
65 }
66 
ParsePacket(const TracePacket::Decoder & decoder,const TimestampedTracePiece & ttp,uint32_t field_id)67 void TrackEventModule::ParsePacket(const TracePacket::Decoder& decoder,
68                                    const TimestampedTracePiece& ttp,
69                                    uint32_t field_id) {
70   switch (field_id) {
71     case TracePacket::kTrackDescriptorFieldNumber:
72       PERFETTO_DCHECK(ttp.type == TimestampedTracePiece::Type::kTracePacket);
73       parser_.ParseTrackDescriptor(decoder.track_descriptor());
74       break;
75     case TracePacket::kTrackEventFieldNumber:
76       PERFETTO_DCHECK(ttp.type == TimestampedTracePiece::Type::kTrackEvent);
77       parser_.ParseTrackEvent(ttp.timestamp, ttp.track_event_data.get(),
78                               decoder.track_event());
79       break;
80     case TracePacket::kProcessDescriptorFieldNumber:
81       // TODO(eseckler): Remove once Chrome has switched to TrackDescriptors.
82       PERFETTO_DCHECK(ttp.type == TimestampedTracePiece::Type::kTracePacket);
83       parser_.ParseProcessDescriptor(decoder.process_descriptor());
84       break;
85     case TracePacket::kThreadDescriptorFieldNumber:
86       // TODO(eseckler): Remove once Chrome has switched to TrackDescriptors.
87       PERFETTO_DCHECK(ttp.type == TimestampedTracePiece::Type::kTracePacket);
88       parser_.ParseThreadDescriptor(decoder.thread_descriptor());
89       break;
90   }
91 }
92 
OnIncrementalStateCleared(uint32_t packet_sequence_id)93 void TrackEventModule::OnIncrementalStateCleared(uint32_t packet_sequence_id) {
94   track_event_tracker_->OnIncrementalStateCleared(packet_sequence_id);
95 }
96 
97 }  // namespace trace_processor
98 }  // namespace perfetto
99