• 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 HandlePreviousPacketDropped(const protos::pbzero::TracePacket_Decoder&);
71   void ParseTracePacketDefaults(const protos::pbzero::TracePacket_Decoder&,
72                                 TraceBlobView trace_packet_defaults);
73   void ParseInternedData(const protos::pbzero::TracePacket_Decoder&,
74                          TraceBlobView interned_data);
75   void ParseTraceConfig(ConstBytes);
76 
77   base::Optional<StringId> GetBuiltinClockNameOrNull(uint64_t clock_id);
78 
GetIncrementalStateForPacketSequence(uint32_t sequence_id)79   PacketSequenceState* GetIncrementalStateForPacketSequence(
80       uint32_t sequence_id) {
81     if (!incremental_state)
82       incremental_state.reset(new ProtoIncrementalState(context_));
83     return incremental_state->GetOrCreateStateForPacketSequence(sequence_id);
84   }
85   util::Status ParseExtensionDescriptor(ConstBytes descriptor);
86 
87   TraceProcessorContext* context_;
88 
89   ProtoTraceTokenizer tokenizer_;
90 
91   // Temporary. Currently trace packets do not have a timestamp, so the
92   // timestamp given is latest_timestamp_.
93   int64_t latest_timestamp_ = 0;
94 
95   // Stores incremental state and references to interned data, e.g. for track
96   // event protos.
97   std::unique_ptr<ProtoIncrementalState> incremental_state;
98 };
99 
100 }  // namespace trace_processor
101 }  // namespace perfetto
102 
103 #endif  // SRC_TRACE_PROCESSOR_IMPORTERS_PROTO_PROTO_TRACE_READER_H_
104