• 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/common/trace_blob_view.h"
26 #include "src/trace_processor/importers/proto/proto_incremental_state.h"
27 #include "src/trace_processor/importers/proto/proto_trace_tokenizer.h"
28 
29 namespace protozero {
30 struct ConstBytes;
31 }
32 
33 namespace perfetto {
34 
35 namespace protos {
36 namespace pbzero {
37 class TracePacket_Decoder;
38 class TraceConfig_Decoder;
39 }  // namespace pbzero
40 }  // namespace protos
41 
42 namespace trace_processor {
43 
44 class PacketSequenceState;
45 class TraceProcessorContext;
46 class TraceSorter;
47 class TraceStorage;
48 
49 // Implementation of ChunkedTraceReader for proto traces. Tokenizes a proto
50 // trace into packets, handles parsing of any packets which need to be
51 // handled in trace-order and passes the remainder to TraceSorter to sort
52 // into timestamp order.
53 class ProtoTraceReader : public ChunkedTraceReader {
54  public:
55   // |reader| is the abstract method of getting chunks of size |chunk_size_b|
56   // from a trace file with these chunks parsed into |trace|.
57   explicit ProtoTraceReader(TraceProcessorContext*);
58   ~ProtoTraceReader() override;
59 
60   // ChunkedTraceReader implementation.
61   util::Status Parse(std::unique_ptr<uint8_t[]>, size_t size) override;
62   void NotifyEndOfFile() override;
63 
64  private:
65   using ConstBytes = protozero::ConstBytes;
66   util::Status ParsePacket(TraceBlobView);
67   util::Status ParseServiceEvent(int64_t ts, ConstBytes);
68   util::Status ParseClockSnapshot(ConstBytes blob, uint32_t seq_id);
69   void HandleIncrementalStateCleared(
70       const protos::pbzero::TracePacket_Decoder&);
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   base::Optional<StringId> GetBuiltinClockNameOrNull(uint64_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 
101 }  // namespace trace_processor
102 }  // namespace perfetto
103 
104 #endif  // SRC_TRACE_PROCESSOR_IMPORTERS_PROTO_PROTO_TRACE_READER_H_
105