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_COMMON_EVENT_TRACKER_H_ 18 #define SRC_TRACE_PROCESSOR_IMPORTERS_COMMON_EVENT_TRACKER_H_ 19 20 #include <array> 21 #include <limits> 22 23 #include "perfetto/ext/base/string_view.h" 24 #include "perfetto/ext/base/utils.h" 25 #include "src/trace_processor/importers/common/args_tracker.h" 26 #include "src/trace_processor/storage/trace_storage.h" 27 28 namespace perfetto { 29 namespace trace_processor { 30 31 class TraceProcessorContext; 32 33 // Tracks sched events, instants, and counters. 34 class EventTracker { 35 public: 36 using SetArgsCallback = std::function<void(ArgsTracker::BoundInserter*)>; 37 38 explicit EventTracker(TraceProcessorContext*); 39 EventTracker(const EventTracker&) = delete; 40 EventTracker& operator=(const EventTracker&) = delete; 41 virtual ~EventTracker(); 42 43 // Adds a counter event to the counters table returning the index of the 44 // newly added row. 45 virtual std::optional<CounterId> PushCounter(int64_t timestamp, 46 double value, 47 TrackId track_id); 48 49 // Adds a counter event with args to the counters table returning the index of 50 // the newly added row. 51 std::optional<CounterId> PushCounter(int64_t timestamp, 52 double value, 53 TrackId track_id, 54 SetArgsCallback args_callback); 55 56 // Adds a counter event to the counters table for counter events which 57 // should be associated with a process but only have a thread context 58 // (e.g. rss_stat events). 59 // 60 // This function will resolve the utid to a upid when the events are 61 // flushed (see |FlushPendingEvents()|). 62 virtual std::optional<CounterId> PushProcessCounterForThread( 63 int64_t timestamp, 64 double value, 65 StringId name_id, 66 UniqueTid utid); 67 68 // Called at the end of trace to flush any events which are pending to the 69 // storage. 70 void FlushPendingEvents(); 71 72 // For SchedEventTracker. max_timestamp()73 int64_t max_timestamp() const { return max_timestamp_; } UpdateMaxTimestamp(int64_t ts)74 void UpdateMaxTimestamp(int64_t ts) { 75 max_timestamp_ = std::max(ts, max_timestamp_); 76 } 77 78 private: 79 // Represents a counter event which is currently pending upid resolution. 80 struct PendingUpidResolutionCounter { 81 uint32_t row = 0; 82 StringId name_id = kNullStringId; 83 UniqueTid utid = 0; 84 }; 85 86 // Store the rows in the counters table which need upids resolved. 87 std::vector<PendingUpidResolutionCounter> pending_upid_resolution_counter_; 88 89 // Timestamp of the previous event. Used to discard events arriving out 90 // of order. 91 int64_t max_timestamp_ = 0; 92 93 TraceProcessorContext* const context_; 94 }; 95 } // namespace trace_processor 96 } // namespace perfetto 97 98 #endif // SRC_TRACE_PROCESSOR_IMPORTERS_COMMON_EVENT_TRACKER_H_ 99