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 "src/trace_processor/storage/trace_storage.h" 21 #include "src/trace_processor/types/trace_processor_context.h" 22 23 namespace perfetto { 24 namespace trace_processor { 25 26 // Tracks and stores tracks based on track types, ids and scopes. 27 class TrackTracker { 28 public: 29 explicit TrackTracker(TraceProcessorContext*); 30 31 // Interns a thread track into the storage. 32 TrackId InternThreadTrack(UniqueTid utid); 33 34 // Interns a process track into the storage. 35 TrackId InternProcessTrack(UniquePid upid); 36 37 // Interns a Fuchsia async track into the storage. 38 TrackId InternFuchsiaAsyncTrack(StringId name, 39 uint32_t upid, 40 int64_t correlation_id); 41 42 // Interns a global track keyed by CPU + name into the storage. 43 TrackId InternCpuTrack(StringId name, uint32_t cpu); 44 45 // Interns a given GPU track into the storage. 46 TrackId InternGpuTrack(const tables::GpuTrackTable::Row& row); 47 48 // Interns a legacy Chrome async event track into the storage. 49 TrackId InternLegacyChromeAsyncTrack(StringId name, 50 uint32_t upid, 51 int64_t source_id, 52 bool source_id_is_process_scoped, 53 StringId source_scope); 54 55 // Interns a track for legacy Chrome process-scoped instant events into the 56 // storage. 57 TrackId InternLegacyChromeProcessInstantTrack(UniquePid upid); 58 59 // Lazily creates the track for legacy Chrome global instant events. 60 TrackId GetOrCreateLegacyChromeGlobalInstantTrack(); 61 62 // Returns the ID of the implicit trace-global default track for triggers 63 // received by the service. 64 TrackId GetOrCreateTriggerTrack(); 65 66 // Interns a global counter track into the storage. 67 TrackId InternGlobalCounterTrack(StringId name, 68 StringId unit = kNullStringId, 69 StringId description = kNullStringId); 70 71 // Interns a counter track associated with a cpu into the storage. 72 TrackId InternCpuCounterTrack(StringId name, uint32_t cpu); 73 74 // Interns a counter track associated with a thread into the storage. 75 TrackId InternThreadCounterTrack(StringId name, UniqueTid utid); 76 77 // Interns a counter track associated with a process into the storage. 78 TrackId InternProcessCounterTrack(StringId name, 79 UniquePid upid, 80 StringId unit = kNullStringId, 81 StringId description = kNullStringId); 82 83 // Interns a counter track associated with an irq into the storage. 84 TrackId InternIrqCounterTrack(StringId name, int32_t irq); 85 86 // Interns a counter track associated with an softirq into the storage. 87 TrackId InternSoftirqCounterTrack(StringId name, int32_t softirq); 88 89 // Interns a counter track associated with a GPU into the storage. 90 TrackId InternGpuCounterTrack(StringId name, uint32_t gpu_id); 91 92 // Creates a counter track associated with a GPU into the storage. 93 TrackId CreateGpuCounterTrack(StringId name, 94 uint32_t gpu_id, 95 StringId description = StringId::Null(), 96 StringId unit = StringId::Null()); 97 98 // Creates a counter track for values within perf samples. 99 // The tracks themselves are managed by PerfSampleTracker. 100 TrackId CreatePerfCounterTrack(StringId name, 101 uint32_t perf_session_id, 102 uint32_t cpu, 103 bool is_timebase); 104 105 // NOTE: 106 // The below method should only be called by AsyncTrackSetTracker 107 108 // Creates and inserts a global async track into the storage. 109 TrackId CreateGlobalAsyncTrack(StringId name, StringId source); 110 111 // Creates and inserts a Android async track into the storage. 112 TrackId CreateProcessAsyncTrack(StringId name, 113 UniquePid upid, 114 StringId source); 115 116 private: 117 struct GpuTrackTuple { 118 StringId track_name; 119 StringId scope; 120 int64_t context_id; 121 122 friend bool operator<(const GpuTrackTuple& l, const GpuTrackTuple& r) { 123 return std::tie(l.track_name, l.scope, l.context_id) < 124 std::tie(r.track_name, r.scope, r.context_id); 125 } 126 }; 127 struct ChromeTrackTuple { 128 base::Optional<int64_t> upid; 129 int64_t source_id = 0; 130 StringId source_scope = StringId::Null(); 131 132 friend bool operator<(const ChromeTrackTuple& l, 133 const ChromeTrackTuple& r) { 134 return std::tie(l.source_id, l.upid, l.source_scope) < 135 std::tie(r.source_id, r.upid, r.source_scope); 136 } 137 }; 138 139 std::map<UniqueTid, TrackId> thread_tracks_; 140 std::map<UniquePid, TrackId> process_tracks_; 141 std::map<int64_t /* correlation_id */, TrackId> fuchsia_async_tracks_; 142 143 std::map<std::pair<StringId, uint32_t /* cpu */>, TrackId> cpu_tracks_; 144 145 std::map<GpuTrackTuple, TrackId> gpu_tracks_; 146 std::map<ChromeTrackTuple, TrackId> chrome_tracks_; 147 std::map<UniquePid, TrackId> chrome_process_instant_tracks_; 148 149 std::map<StringId, TrackId> global_counter_tracks_by_name_; 150 std::map<std::pair<StringId, uint32_t>, TrackId> cpu_counter_tracks_; 151 std::map<std::pair<StringId, UniqueTid>, TrackId> utid_counter_tracks_; 152 std::map<std::pair<StringId, UniquePid>, TrackId> upid_counter_tracks_; 153 std::map<std::pair<StringId, int32_t>, TrackId> irq_counter_tracks_; 154 std::map<std::pair<StringId, int32_t>, TrackId> softirq_counter_tracks_; 155 std::map<std::pair<StringId, uint32_t>, TrackId> gpu_counter_tracks_; 156 157 base::Optional<TrackId> chrome_global_instant_track_id_; 158 base::Optional<TrackId> trigger_track_id_; 159 160 const StringId source_key_ = kNullStringId; 161 const StringId source_id_key_ = kNullStringId; 162 const StringId source_id_is_process_scoped_key_ = kNullStringId; 163 const StringId source_scope_key_ = kNullStringId; 164 const StringId category_key_ = kNullStringId; 165 166 const StringId fuchsia_source_ = kNullStringId; 167 const StringId chrome_source_ = kNullStringId; 168 169 TraceProcessorContext* const context_; 170 }; 171 172 } // namespace trace_processor 173 } // namespace perfetto 174 175 #endif // SRC_TRACE_PROCESSOR_IMPORTERS_COMMON_TRACK_TRACKER_H_ 176