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_PROTO_INCREMENTAL_STATE_H_
18 #define SRC_TRACE_PROCESSOR_IMPORTERS_PROTO_PROTO_INCREMENTAL_STATE_H_
19 
20 #include <stdint.h>
21 
22 #include <map>
23 
24 #include "src/trace_processor/importers/proto/packet_sequence_state.h"
25 
26 namespace perfetto {
27 namespace trace_processor {
28 
29 class TraceProcessorContext;
30 
31 // Stores per-packet-sequence incremental state during trace parsing, such as
32 // reference timestamps for delta timestamp calculation and interned messages.
33 class ProtoIncrementalState {
34  public:
ProtoIncrementalState(TraceProcessorContext * context)35   ProtoIncrementalState(TraceProcessorContext* context) : context_(context) {}
36 
37   // Returns the PacketSequenceState for the packet sequence with the given id.
38   // If this is a new sequence which we haven't tracked before, initializes and
39   // inserts a new PacketSequenceState into the state map.
GetOrCreateStateForPacketSequence(uint32_t sequence_id)40   PacketSequenceState* GetOrCreateStateForPacketSequence(uint32_t sequence_id) {
41     auto& ptr = packet_sequence_states_[sequence_id];
42     if (!ptr)
43       ptr.reset(new PacketSequenceState(context_));
44     return ptr.get();
45   }
46 
47  private:
48   // Stores unique_ptrs to ensure that pointers to a PacketSequenceState remain
49   // valid even if the map rehashes.
50   std::map<uint32_t, std::unique_ptr<PacketSequenceState>>
51       packet_sequence_states_;
52 
53   TraceProcessorContext* context_;
54 };
55 
56 }  // namespace trace_processor
57 }  // namespace perfetto
58 
59 #endif  // SRC_TRACE_PROCESSOR_IMPORTERS_PROTO_PROTO_INCREMENTAL_STATE_H_
60