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/ftrace/ftrace_module_impl.h"
18 #include "perfetto/base/build_config.h"
19 #include "src/trace_processor/importers/common/trace_blob_view.h"
20 #include "src/trace_processor/importers/ftrace/ftrace_parser.h"
21 #include "src/trace_processor/importers/ftrace/ftrace_tokenizer.h"
22 #include "src/trace_processor/timestamped_trace_piece.h"
23 
24 #include "protos/perfetto/trace/trace_packet.pbzero.h"
25 
26 namespace perfetto {
27 namespace trace_processor {
28 
29 using perfetto::protos::pbzero::TracePacket;
30 
FtraceModuleImpl(TraceProcessorContext * context)31 FtraceModuleImpl::FtraceModuleImpl(TraceProcessorContext* context)
32     : tokenizer_(context), parser_(context) {
33   RegisterForField(TracePacket::kFtraceEventsFieldNumber, context);
34   RegisterForField(TracePacket::kFtraceStatsFieldNumber, context);
35 }
36 
TokenizePacket(const protos::pbzero::TracePacket::Decoder & decoder,TraceBlobView * packet,int64_t,PacketSequenceState * seq_state,uint32_t field_id)37 ModuleResult FtraceModuleImpl::TokenizePacket(
38     const protos::pbzero::TracePacket::Decoder& decoder,
39     TraceBlobView* packet,
40     int64_t /*packet_timestamp*/,
41     PacketSequenceState* seq_state,
42     uint32_t field_id) {
43   if (field_id == TracePacket::kFtraceEventsFieldNumber) {
44     auto ftrace_field = decoder.ftrace_events();
45     const size_t fld_off = packet->offset_of(ftrace_field.data);
46     tokenizer_.TokenizeFtraceBundle(packet->slice(fld_off, ftrace_field.size),
47                                     seq_state);
48     return ModuleResult::Handled();
49   }
50   return ModuleResult::Ignored();
51 }
52 
ParsePacket(const protos::pbzero::TracePacket::Decoder & decoder,const TimestampedTracePiece &,uint32_t field_id)53 void FtraceModuleImpl::ParsePacket(
54     const protos::pbzero::TracePacket::Decoder& decoder,
55     const TimestampedTracePiece&,
56     uint32_t field_id) {
57   if (field_id == TracePacket::kFtraceStatsFieldNumber) {
58     parser_.ParseFtraceStats(decoder.ftrace_stats());
59   }
60 }
61 
ParseFtracePacket(uint32_t cpu,const TimestampedTracePiece & ttp)62 void FtraceModuleImpl::ParseFtracePacket(uint32_t cpu,
63                                          const TimestampedTracePiece& ttp) {
64   util::Status res = parser_.ParseFtraceEvent(cpu, ttp);
65   if (!res.ok()) {
66     PERFETTO_ELOG("%s", res.message().c_str());
67   }
68 }
69 
70 }  // namespace trace_processor
71 }  // namespace perfetto
72