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 <tuple> 23 #include <utility> 24 25 #include "perfetto/ext/base/flat_hash_map.h" 26 #include "src/trace_processor/importers/common/chunked_trace_reader.h" 27 #include "src/trace_processor/importers/proto/multi_machine_trace_manager.h" 28 #include "src/trace_processor/importers/proto/packet_sequence_state_builder.h" 29 #include "src/trace_processor/importers/proto/proto_trace_tokenizer.h" 30 #include "src/trace_processor/storage/trace_storage.h" 31 32 namespace protozero { 33 struct ConstBytes; 34 } 35 36 namespace perfetto { 37 38 namespace protos { 39 namespace pbzero { 40 class TracePacket_Decoder; 41 class TraceConfig_Decoder; 42 } // namespace pbzero 43 } // namespace protos 44 45 namespace trace_processor { 46 47 class PacketSequenceState; 48 class TraceProcessorContext; 49 class TraceSorter; 50 class TraceStorage; 51 52 // Implementation of ChunkedTraceReader for proto traces. Tokenizes a proto 53 // trace into packets, handles parsing of any packets which need to be 54 // handled in trace-order and passes the remainder to TraceSorter to sort 55 // into timestamp order. 56 class ProtoTraceReader : public ChunkedTraceReader { 57 public: 58 // |reader| is the abstract method of getting chunks of size |chunk_size_b| 59 // from a trace file with these chunks parsed into |trace|. 60 explicit ProtoTraceReader(TraceProcessorContext*); 61 ~ProtoTraceReader() override; 62 63 // ChunkedTraceReader implementation. 64 util::Status Parse(TraceBlobView) override; 65 void NotifyEndOfFile() override; 66 67 private: 68 using ConstBytes = protozero::ConstBytes; 69 util::Status ParsePacket(TraceBlobView); 70 util::Status ParseServiceEvent(int64_t ts, ConstBytes); 71 util::Status ParseClockSnapshot(ConstBytes blob, uint32_t seq_id); 72 void HandleIncrementalStateCleared( 73 const protos::pbzero::TracePacket_Decoder&); 74 void HandleFirstPacketOnSequence(uint32_t packet_sequence_id); 75 void HandlePreviousPacketDropped(const protos::pbzero::TracePacket_Decoder&); 76 void ParseTracePacketDefaults(const protos::pbzero::TracePacket_Decoder&, 77 TraceBlobView trace_packet_defaults); 78 void ParseInternedData(const protos::pbzero::TracePacket_Decoder&, 79 TraceBlobView interned_data); 80 void ParseTraceConfig(ConstBytes); 81 void ParseTraceStats(ConstBytes); 82 83 std::optional<StringId> GetBuiltinClockNameOrNull(int64_t clock_id); 84 GetIncrementalStateForPacketSequence(uint32_t sequence_id)85 PacketSequenceStateBuilder* GetIncrementalStateForPacketSequence( 86 uint32_t sequence_id) { 87 auto* builder = packet_sequence_state_builders_.Find(sequence_id); 88 if (builder == nullptr) { 89 builder = packet_sequence_state_builders_ 90 .Insert(sequence_id, PacketSequenceStateBuilder(context_)) 91 .first; 92 } 93 return builder; 94 } 95 util::Status ParseExtensionDescriptor(ConstBytes descriptor); 96 97 TraceProcessorContext* context_; 98 99 ProtoTraceTokenizer tokenizer_; 100 101 // Temporary. Currently trace packets do not have a timestamp, so the 102 // timestamp given is latest_timestamp_. 103 int64_t latest_timestamp_ = 0; 104 105 base::FlatHashMap<uint32_t, PacketSequenceStateBuilder> 106 packet_sequence_state_builders_; 107 108 base::FlatHashMap<uint32_t, size_t> packet_sequence_data_loss_; 109 110 StringId skipped_packet_key_id_; 111 StringId invalid_incremental_state_key_id_; 112 }; 113 114 } // namespace trace_processor 115 } // namespace perfetto 116 117 #endif // SRC_TRACE_PROCESSOR_IMPORTERS_PROTO_PROTO_TRACE_READER_H_ 118