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_COMMON_TRACK_TRACKER_H_ 18 #define SRC_TRACE_PROCESSOR_IMPORTERS_COMMON_TRACK_TRACKER_H_ 19 20 #include <optional> 21 #include "src/trace_processor/importers/common/args_tracker.h" 22 #include "src/trace_processor/storage/trace_storage.h" 23 #include "src/trace_processor/types/trace_processor_context.h" 24 25 namespace perfetto { 26 namespace trace_processor { 27 28 // Tracks and stores tracks based on track types, ids and scopes. 29 class TrackTracker { 30 public: 31 // Enum which groups global tracks to avoid an explosion of tracks at the top 32 // level. 33 // Try and keep members of this enum high level as every entry here 34 // corresponds to ~1 extra UI track. 35 enum class Group : uint32_t { 36 kMemory = 0, 37 kIo, 38 kVirtio, 39 kNetwork, 40 kPower, 41 kDeviceState, 42 kThermals, 43 kClockFrequency, 44 45 // Keep this last. 46 kSizeSentinel, 47 }; 48 using SetArgsCallback = std::function<void(ArgsTracker::BoundInserter&)>; 49 50 explicit TrackTracker(TraceProcessorContext*); 51 52 // Interns a thread track into the storage. 53 TrackId InternThreadTrack(UniqueTid utid); 54 55 // Interns a process track into the storage. 56 TrackId InternProcessTrack(UniquePid upid); 57 58 // Interns a Fuchsia async track into the storage. 59 TrackId InternFuchsiaAsyncTrack(StringId name, 60 uint32_t upid, 61 int64_t correlation_id); 62 63 // Interns a global track keyed by CPU + name into the storage. 64 TrackId InternCpuTrack(StringId name, uint32_t cpu); 65 66 // Interns a given GPU track into the storage. 67 TrackId InternGpuTrack(const tables::GpuTrackTable::Row& row); 68 69 // Interns a legacy Chrome async event track into the storage. 70 TrackId InternLegacyChromeAsyncTrack(StringId name, 71 uint32_t upid, 72 int64_t source_id, 73 bool source_id_is_process_scoped, 74 StringId source_scope); 75 76 // Interns a track for legacy Chrome process-scoped instant events into the 77 // storage. 78 TrackId InternLegacyChromeProcessInstantTrack(UniquePid upid); 79 80 // Lazily creates the track for legacy Chrome global instant events. 81 TrackId GetOrCreateLegacyChromeGlobalInstantTrack(); 82 83 // Returns the ID of the implicit trace-global default track for triggers 84 // received by the service. 85 TrackId GetOrCreateTriggerTrack(); 86 87 // Interns a global counter track into the storage. 88 TrackId InternGlobalCounterTrack(Group group, 89 StringId name, 90 SetArgsCallback = {}, 91 StringId unit = kNullStringId, 92 StringId description = kNullStringId); 93 94 // Interns a counter track associated with a cpu into the storage. 95 TrackId InternCpuCounterTrack(StringId name, uint32_t cpu); 96 97 // Interns a counter track associated with a thread into the storage. 98 TrackId InternThreadCounterTrack(StringId name, UniqueTid utid); 99 100 // Interns a counter track associated with a process into the storage. 101 TrackId InternProcessCounterTrack(StringId name, 102 UniquePid upid, 103 StringId unit = kNullStringId, 104 StringId description = kNullStringId); 105 106 // Interns a counter track associated with an irq into the storage. 107 TrackId InternIrqCounterTrack(StringId name, int32_t irq); 108 109 // Interns a counter track associated with an softirq into the storage. 110 TrackId InternSoftirqCounterTrack(StringId name, int32_t softirq); 111 112 // Interns a counter track associated with a GPU into the storage. 113 TrackId InternGpuCounterTrack(StringId name, uint32_t gpu_id); 114 115 // Interns energy counter track associated with a 116 // Energy breakdown into the storage. 117 TrackId InternEnergyCounterTrack(StringId name, 118 int32_t consumer_id, 119 StringId consumer_type, 120 int32_t ordinal); 121 // Interns a per process energy counter track associated with a 122 // Energy into the storage. 123 TrackId InternUidCounterTrack(StringId name, int32_t uid); 124 125 // Interns a per process energy consumer counter track associated with a 126 // Energy Uid into the storage. 127 TrackId InternEnergyPerUidCounterTrack(StringId name, 128 int32_t consumer_id, 129 int32_t uid); 130 131 // Creates a counter track associated with a GPU into the storage. 132 TrackId CreateGpuCounterTrack(StringId name, 133 uint32_t gpu_id, 134 StringId description = StringId::Null(), 135 StringId unit = StringId::Null()); 136 137 // Creates a counter track for values within perf samples. 138 // The tracks themselves are managed by PerfSampleTracker. 139 TrackId CreatePerfCounterTrack(StringId name, 140 uint32_t perf_session_id, 141 uint32_t cpu, 142 bool is_timebase); 143 144 // NOTE: 145 // The below method should only be called by AsyncTrackSetTracker 146 147 // Creates and inserts a global async track into the storage. 148 TrackId CreateGlobalAsyncTrack(StringId name, StringId source); 149 150 // Creates and inserts a Android async track into the storage. 151 TrackId CreateProcessAsyncTrack(StringId name, 152 UniquePid upid, 153 StringId source); 154 155 private: 156 struct GpuTrackTuple { 157 StringId track_name; 158 StringId scope; 159 int64_t context_id; 160 161 friend bool operator<(const GpuTrackTuple& l, const GpuTrackTuple& r) { 162 return std::tie(l.track_name, l.scope, l.context_id) < 163 std::tie(r.track_name, r.scope, r.context_id); 164 } 165 }; 166 struct ChromeTrackTuple { 167 std::optional<int64_t> upid; 168 int64_t source_id = 0; 169 StringId source_scope = StringId::Null(); 170 171 friend bool operator<(const ChromeTrackTuple& l, 172 const ChromeTrackTuple& r) { 173 return std::tie(l.source_id, l.upid, l.source_scope) < 174 std::tie(r.source_id, r.upid, r.source_scope); 175 } 176 }; 177 static constexpr size_t kGroupCount = 178 static_cast<uint32_t>(Group::kSizeSentinel); 179 180 TrackId InternTrackForGroup(Group group); 181 182 std::array<std::optional<TrackId>, kGroupCount> group_track_ids_; 183 184 std::map<UniqueTid, TrackId> thread_tracks_; 185 std::map<UniquePid, TrackId> process_tracks_; 186 std::map<int64_t /* correlation_id */, TrackId> fuchsia_async_tracks_; 187 188 std::map<std::pair<StringId, uint32_t /* cpu */>, TrackId> cpu_tracks_; 189 190 std::map<GpuTrackTuple, TrackId> gpu_tracks_; 191 std::map<ChromeTrackTuple, TrackId> chrome_tracks_; 192 std::map<UniquePid, TrackId> chrome_process_instant_tracks_; 193 194 std::map<StringId, TrackId> global_counter_tracks_by_name_; 195 std::map<std::pair<StringId, uint32_t>, TrackId> cpu_counter_tracks_; 196 std::map<std::pair<StringId, UniqueTid>, TrackId> utid_counter_tracks_; 197 std::map<std::pair<StringId, UniquePid>, TrackId> upid_counter_tracks_; 198 std::map<std::pair<StringId, int32_t>, TrackId> irq_counter_tracks_; 199 std::map<std::pair<StringId, int32_t>, TrackId> softirq_counter_tracks_; 200 std::map<std::pair<StringId, uint32_t>, TrackId> gpu_counter_tracks_; 201 std::map<std::pair<StringId, int32_t>, TrackId> energy_counter_tracks_; 202 std::map<std::pair<StringId, int32_t>, TrackId> uid_counter_tracks_; 203 std::map<std::pair<StringId, int32_t>, TrackId> 204 energy_per_uid_counter_tracks_; 205 206 std::optional<TrackId> chrome_global_instant_track_id_; 207 std::optional<TrackId> trigger_track_id_; 208 209 const StringId source_key_ = kNullStringId; 210 const StringId source_id_key_ = kNullStringId; 211 const StringId source_id_is_process_scoped_key_ = kNullStringId; 212 const StringId source_scope_key_ = kNullStringId; 213 const StringId category_key_ = kNullStringId; 214 215 const StringId fuchsia_source_ = kNullStringId; 216 const StringId chrome_source_ = kNullStringId; 217 218 TraceProcessorContext* const context_; 219 }; 220 221 } // namespace trace_processor 222 } // namespace perfetto 223 224 #endif // SRC_TRACE_PROCESSOR_IMPORTERS_COMMON_TRACK_TRACKER_H_ 225