1 /* 2 * Copyright (C) 2024 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_TRACK_EVENT_SEQUENCE_STATE_H_ 18 #define SRC_TRACE_PROCESSOR_IMPORTERS_PROTO_TRACK_EVENT_SEQUENCE_STATE_H_ 19 20 #include <utility> 21 22 #include "protos/perfetto/trace/track_event/thread_descriptor.pbzero.h" 23 24 namespace perfetto { 25 namespace trace_processor { 26 27 class TrackEventSequenceState { 28 public: CreateFirst()29 static TrackEventSequenceState CreateFirst() { 30 return TrackEventSequenceState(PersistentState()); 31 } 32 OnIncrementalStateCleared()33 TrackEventSequenceState OnIncrementalStateCleared() { 34 return TrackEventSequenceState(persistent_state_); 35 } 36 OnPacketLoss()37 void OnPacketLoss() { timestamps_valid_ = false; } 38 pid_and_tid_valid()39 bool pid_and_tid_valid() const { return persistent_state_.pid_and_tid_valid; } 40 pid()41 int32_t pid() const { return persistent_state_.pid; } tid()42 int32_t tid() const { return persistent_state_.tid; } 43 timestamps_valid()44 bool timestamps_valid() const { return timestamps_valid_; } 45 IncrementAndGetTrackEventTimeNs(int64_t delta_ns)46 int64_t IncrementAndGetTrackEventTimeNs(int64_t delta_ns) { 47 PERFETTO_DCHECK(timestamps_valid()); 48 timestamp_ns_ += delta_ns; 49 return timestamp_ns_; 50 } 51 IncrementAndGetTrackEventThreadTimeNs(int64_t delta_ns)52 int64_t IncrementAndGetTrackEventThreadTimeNs(int64_t delta_ns) { 53 PERFETTO_DCHECK(timestamps_valid()); 54 thread_timestamp_ns_ += delta_ns; 55 return thread_timestamp_ns_; 56 } 57 IncrementAndGetTrackEventThreadInstructionCount(int64_t delta)58 int64_t IncrementAndGetTrackEventThreadInstructionCount(int64_t delta) { 59 PERFETTO_DCHECK(timestamps_valid()); 60 thread_instruction_count_ += delta; 61 return thread_instruction_count_; 62 } 63 64 void SetThreadDescriptor(const protos::pbzero::ThreadDescriptor::Decoder&); 65 66 private: 67 // State that is never cleared. 68 struct PersistentState { 69 // |pid_| and |tid_| are only valid after we parsed at least one 70 // ThreadDescriptor packet on the sequence. 71 bool pid_and_tid_valid = false; 72 73 // Process/thread ID of the packet sequence set by a ThreadDescriptor 74 // packet. Used as default values for TrackEvents that don't specify a 75 // pid/tid override. Only valid after |pid_and_tid_valid_| is set to true. 76 int32_t pid = 0; 77 int32_t tid = 0; 78 }; 79 TrackEventSequenceState(PersistentState persistent_state)80 explicit TrackEventSequenceState(PersistentState persistent_state) 81 : persistent_state_(std::move(persistent_state)) {} 82 83 // We can only consider TrackEvent delta timestamps to be correct after we 84 // have observed a thread descriptor (since the last packet loss). 85 bool timestamps_valid_ = false; 86 87 // Current wall/thread timestamps/counters used as reference for the next 88 // TrackEvent delta timestamp. 89 int64_t timestamp_ns_ = 0; 90 int64_t thread_timestamp_ns_ = 0; 91 int64_t thread_instruction_count_ = 0; 92 93 PersistentState persistent_state_; 94 }; 95 96 } // namespace trace_processor 97 } // namespace perfetto 98 99 #endif // SRC_TRACE_PROCESSOR_IMPORTERS_PROTO_TRACK_EVENT_SEQUENCE_STATE_H_ 100