• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_H_
18 #define SRC_TRACE_PROCESSOR_IMPORTERS_PROTO_PACKET_SEQUENCE_STATE_H_
19 
20 #include <stdint.h>
21 
22 #include <unordered_map>
23 #include <vector>
24 
25 #include "perfetto/base/compiler.h"
26 #include "src/trace_processor/importers/proto/packet_sequence_state_generation.h"
27 #include "src/trace_processor/importers/proto/stack_profile_tracker.h"
28 #include "src/trace_processor/storage/trace_storage.h"
29 #include "src/trace_processor/types/trace_processor_context.h"
30 #include "src/trace_processor/util/interned_message_view.h"
31 
32 namespace perfetto {
33 namespace trace_processor {
34 
35 class PacketSequenceState {
36  public:
PacketSequenceState(TraceProcessorContext * context)37   explicit PacketSequenceState(TraceProcessorContext* context)
38       : context_(context), sequence_stack_profile_tracker_(context) {
39     current_generation_.reset(
40         new PacketSequenceStateGeneration(this, generation_index_++));
41   }
42 
IncrementAndGetTrackEventTimeNs(int64_t delta_ns)43   int64_t IncrementAndGetTrackEventTimeNs(int64_t delta_ns) {
44     PERFETTO_DCHECK(track_event_timestamps_valid());
45     track_event_timestamp_ns_ += delta_ns;
46     return track_event_timestamp_ns_;
47   }
48 
IncrementAndGetTrackEventThreadTimeNs(int64_t delta_ns)49   int64_t IncrementAndGetTrackEventThreadTimeNs(int64_t delta_ns) {
50     PERFETTO_DCHECK(track_event_timestamps_valid());
51     track_event_thread_timestamp_ns_ += delta_ns;
52     return track_event_thread_timestamp_ns_;
53   }
54 
IncrementAndGetTrackEventThreadInstructionCount(int64_t delta)55   int64_t IncrementAndGetTrackEventThreadInstructionCount(int64_t delta) {
56     PERFETTO_DCHECK(track_event_timestamps_valid());
57     track_event_thread_instruction_count_ += delta;
58     return track_event_thread_instruction_count_;
59   }
60 
61   // Intern a message into the current generation.
InternMessage(uint32_t field_id,TraceBlobView message)62   void InternMessage(uint32_t field_id, TraceBlobView message) {
63     current_generation_->InternMessage(field_id, std::move(message));
64   }
65 
66   // Set the trace packet defaults for the current generation. If the current
67   // generation already has defaults set, starts a new generation without
68   // invalidating other incremental state (such as interned data).
UpdateTracePacketDefaults(TraceBlobView defaults)69   void UpdateTracePacketDefaults(TraceBlobView defaults) {
70     if (!current_generation_->GetTracePacketDefaultsView()) {
71       current_generation_->SetTracePacketDefaults(std::move(defaults));
72       return;
73     }
74 
75     // The new defaults should only apply to subsequent messages on the
76     // sequence. Add a new generation with the updated defaults but the
77     // current generation's interned data state.
78     current_generation_.reset(new PacketSequenceStateGeneration(
79         this, generation_index_++, current_generation_->interned_data_,
80         std::move(defaults)));
81   }
82 
SetThreadDescriptor(int32_t pid,int32_t tid,int64_t timestamp_ns,int64_t thread_timestamp_ns,int64_t thread_instruction_count)83   void SetThreadDescriptor(int32_t pid,
84                            int32_t tid,
85                            int64_t timestamp_ns,
86                            int64_t thread_timestamp_ns,
87                            int64_t thread_instruction_count) {
88     track_event_timestamps_valid_ = true;
89     pid_and_tid_valid_ = true;
90     pid_ = pid;
91     tid_ = tid;
92     track_event_timestamp_ns_ = timestamp_ns;
93     track_event_thread_timestamp_ns_ = thread_timestamp_ns;
94     track_event_thread_instruction_count_ = thread_instruction_count;
95   }
96 
OnPacketLoss()97   void OnPacketLoss() {
98     packet_loss_ = true;
99     track_event_timestamps_valid_ = false;
100   }
101 
102   // Starts a new generation with clean-slate incremental state and defaults.
OnIncrementalStateCleared()103   void OnIncrementalStateCleared() {
104     packet_loss_ = false;
105     current_generation_.reset(
106         new PacketSequenceStateGeneration(this, generation_index_++));
107   }
108 
IsIncrementalStateValid()109   bool IsIncrementalStateValid() const { return !packet_loss_; }
110 
sequence_stack_profile_tracker()111   SequenceStackProfileTracker& sequence_stack_profile_tracker() {
112     return sequence_stack_profile_tracker_;
113   }
114 
115   // Returns a ref-counted ptr to the current generation.
current_generation()116   RefPtr<PacketSequenceStateGeneration> current_generation() const {
117     return current_generation_;
118   }
119 
track_event_timestamps_valid()120   bool track_event_timestamps_valid() const {
121     return track_event_timestamps_valid_;
122   }
123 
pid_and_tid_valid()124   bool pid_and_tid_valid() const { return pid_and_tid_valid_; }
125 
pid()126   int32_t pid() const { return pid_; }
tid()127   int32_t tid() const { return tid_; }
128 
context()129   TraceProcessorContext* context() const { return context_; }
130 
131  private:
132   TraceProcessorContext* context_;
133 
134   size_t generation_index_ = 0;
135 
136   // If true, incremental state on the sequence is considered invalid until we
137   // see the next packet with incremental_state_cleared. We assume that we
138   // missed some packets at the beginning of the trace.
139   bool packet_loss_ = true;
140 
141   // We can only consider TrackEvent delta timestamps to be correct after we
142   // have observed a thread descriptor (since the last packet loss).
143   bool track_event_timestamps_valid_ = false;
144 
145   // |pid_| and |tid_| are only valid after we parsed at least one
146   // ThreadDescriptor packet on the sequence.
147   bool pid_and_tid_valid_ = false;
148 
149   // Process/thread ID of the packet sequence set by a ThreadDescriptor
150   // packet. Used as default values for TrackEvents that don't specify a
151   // pid/tid override. Only valid after |pid_and_tid_valid_| is set to true.
152   int32_t pid_ = 0;
153   int32_t tid_ = 0;
154 
155   // Current wall/thread timestamps/counters used as reference for the next
156   // TrackEvent delta timestamp.
157   int64_t track_event_timestamp_ns_ = 0;
158   int64_t track_event_thread_timestamp_ns_ = 0;
159   int64_t track_event_thread_instruction_count_ = 0;
160 
161   RefPtr<PacketSequenceStateGeneration> current_generation_;
162   SequenceStackProfileTracker sequence_stack_profile_tracker_;
163 };
164 
165 template <uint32_t FieldId, typename MessageType>
166 typename MessageType::Decoder*
LookupInternedMessage(uint64_t iid)167 PacketSequenceStateGeneration::LookupInternedMessage(uint64_t iid) {
168   auto* interned_message_view = GetInternedMessageView(FieldId, iid);
169   if (!interned_message_view)
170     return nullptr;
171 
172   return interned_message_view->template GetOrCreateDecoder<MessageType>();
173 }
174 
175 }  // namespace trace_processor
176 }  // namespace perfetto
177 
178 #endif  // SRC_TRACE_PROCESSOR_IMPORTERS_PROTO_PACKET_SEQUENCE_STATE_H_
179