• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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 #ifndef SRC_TRACE_PROCESSOR_IMPORTERS_PROTO_PROTO_TRACE_READER_H_
18 #define SRC_TRACE_PROCESSOR_IMPORTERS_PROTO_PROTO_TRACE_READER_H_
19 
20 #include <stdint.h>
21 
22 #include <memory>
23 
24 #include "src/trace_processor/importers/common/chunked_trace_reader.h"
25 #include "src/trace_processor/importers/proto/proto_incremental_state.h"
26 #include "src/trace_processor/importers/proto/proto_trace_tokenizer.h"
27 
28 namespace protozero {
29 struct ConstBytes;
30 }
31 
32 namespace perfetto {
33 
34 namespace protos {
35 namespace pbzero {
36 class TracePacket_Decoder;
37 class TraceConfig_Decoder;
38 }  // namespace pbzero
39 }  // namespace protos
40 
41 namespace trace_processor {
42 
43 class PacketSequenceState;
44 class TraceProcessorContext;
45 class TraceSorter;
46 class TraceStorage;
47 
48 // Implementation of ChunkedTraceReader for proto traces. Tokenizes a proto
49 // trace into packets, handles parsing of any packets which need to be
50 // handled in trace-order and passes the remainder to TraceSorter to sort
51 // into timestamp order.
52 class ProtoTraceReader : public ChunkedTraceReader {
53  public:
54   // |reader| is the abstract method of getting chunks of size |chunk_size_b|
55   // from a trace file with these chunks parsed into |trace|.
56   explicit ProtoTraceReader(TraceProcessorContext*);
57   ~ProtoTraceReader() override;
58 
59   // ChunkedTraceReader implementation.
60   util::Status Parse(TraceBlobView) override;
61   void NotifyEndOfFile() override;
62 
63  private:
64   using ConstBytes = protozero::ConstBytes;
65   util::Status ParsePacket(TraceBlobView);
66   util::Status ParseServiceEvent(int64_t ts, ConstBytes);
67   util::Status ParseClockSnapshot(ConstBytes blob, uint32_t seq_id);
68   void HandleIncrementalStateCleared(
69       const protos::pbzero::TracePacket_Decoder&);
70   void HandleFirstPacketOnSequence(uint32_t packet_sequence_id);
71   void HandlePreviousPacketDropped(const protos::pbzero::TracePacket_Decoder&);
72   void ParseTracePacketDefaults(const protos::pbzero::TracePacket_Decoder&,
73                                 TraceBlobView trace_packet_defaults);
74   void ParseInternedData(const protos::pbzero::TracePacket_Decoder&,
75                          TraceBlobView interned_data);
76   void ParseTraceConfig(ConstBytes);
77 
78   std::optional<StringId> GetBuiltinClockNameOrNull(int64_t clock_id);
79 
GetIncrementalStateForPacketSequence(uint32_t sequence_id)80   PacketSequenceState* GetIncrementalStateForPacketSequence(
81       uint32_t sequence_id) {
82     if (!incremental_state)
83       incremental_state.reset(new ProtoIncrementalState(context_));
84     return incremental_state->GetOrCreateStateForPacketSequence(sequence_id);
85   }
86   util::Status ParseExtensionDescriptor(ConstBytes descriptor);
87 
88   TraceProcessorContext* context_;
89 
90   ProtoTraceTokenizer tokenizer_;
91 
92   // Temporary. Currently trace packets do not have a timestamp, so the
93   // timestamp given is latest_timestamp_.
94   int64_t latest_timestamp_ = 0;
95 
96   // Stores incremental state and references to interned data, e.g. for track
97   // event protos.
98   std::unique_ptr<ProtoIncrementalState> incremental_state;
99 
100   StringId skipped_packet_key_id_;
101   StringId invalid_incremental_state_key_id_;
102 };
103 
104 }  // namespace trace_processor
105 }  // namespace perfetto
106 
107 #endif  // SRC_TRACE_PROCESSOR_IMPORTERS_PROTO_PROTO_TRACE_READER_H_
108