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 #ifndef SRC_TRACE_PROCESSOR_IMPORTERS_PROTO_PACKET_SEQUENCE_STATE_BUILDER_H_ 18 #define SRC_TRACE_PROCESSOR_IMPORTERS_PROTO_PACKET_SEQUENCE_STATE_BUILDER_H_ 19 20 #include <cstdint> 21 22 #include "perfetto/trace_processor/ref_counted.h" 23 #include "src/trace_processor/importers/proto/packet_sequence_state_generation.h" 24 #include "src/trace_processor/types/trace_processor_context.h" 25 26 namespace perfetto { 27 namespace trace_processor { 28 29 // Helper class to generate a stream of PacketSequenceStateGeneration as we 30 // receive packets for a sequence. This class deals with various events that 31 // incrementally build up state that can be accessed by packet handling code 32 // (tokenization nad parsing). An example of such state are interned messages or 33 // trace packet defaults. 34 class PacketSequenceStateBuilder { 35 public: PacketSequenceStateBuilder(TraceProcessorContext * context)36 explicit PacketSequenceStateBuilder(TraceProcessorContext* context) { 37 generation_ = PacketSequenceStateGeneration::CreateFirst(context); 38 } 39 40 // Intern a message into the current generation. InternMessage(uint32_t field_id,TraceBlobView message)41 void InternMessage(uint32_t field_id, TraceBlobView message) { 42 generation_->InternMessage(field_id, std::move(message)); 43 } 44 45 // Set the trace packet defaults for the current generation. If the current 46 // generation already has defaults set, starts a new generation without 47 // invalidating other incremental state (such as interned data). UpdateTracePacketDefaults(TraceBlobView defaults)48 void UpdateTracePacketDefaults(TraceBlobView defaults) { 49 generation_ = generation_->OnNewTracePacketDefaults(std::move(defaults)); 50 } 51 OnPacketLoss()52 void OnPacketLoss() { 53 generation_ = generation_->OnPacketLoss(); 54 packet_loss_ = true; 55 } 56 57 // Starts a new generation with clean-slate incremental state and defaults. OnIncrementalStateCleared()58 void OnIncrementalStateCleared() { 59 packet_loss_ = false; 60 generation_ = generation_->OnIncrementalStateCleared(); 61 } 62 IsIncrementalStateValid()63 bool IsIncrementalStateValid() const { return !packet_loss_; } 64 65 // Returns a ref-counted ptr to the current generation. current_generation()66 RefPtr<PacketSequenceStateGeneration> current_generation() const { 67 return generation_; 68 } 69 70 private: 71 // If true, incremental state on the sequence is considered invalid until we 72 // see the next packet with incremental_state_cleared. We assume that we 73 // missed some packets at the beginning of the trace. 74 bool packet_loss_ = true; 75 76 RefPtr<PacketSequenceStateGeneration> generation_; 77 }; 78 79 } // namespace trace_processor 80 } // namespace perfetto 81 82 #endif // SRC_TRACE_PROCESSOR_IMPORTERS_PROTO_PACKET_SEQUENCE_STATE_BUILDER_H_ 83