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