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   // Creates and inserts a global async track into the storage.
56   TrackId CreateGlobalAsyncTrack(StringId name);
57 
58   // Creates and inserts a Android async track into the storage.
59   TrackId CreateAndroidAsyncTrack(StringId name, UniquePid upid);
60 
61   // Creates and inserts a FrameTimeline async track into the storage.
62   TrackId CreateFrameTimelineAsyncTrack(StringId name, UniquePid upid);
63 
64   // Interns a track for legacy Chrome process-scoped instant events into the
65   // storage.
66   TrackId InternLegacyChromeProcessInstantTrack(UniquePid upid);
67 
68   // Lazily creates the track for legacy Chrome global instant events.
69   TrackId GetOrCreateLegacyChromeGlobalInstantTrack();
70 
71   // Returns the ID of the implicit trace-global default track for triggers
72   // received by the service.
73   TrackId GetOrCreateTriggerTrack();
74 
75   // Interns a global counter track into the storage.
76   TrackId InternGlobalCounterTrack(StringId name,
77                                    StringId unit = kNullStringId,
78                                    StringId description = kNullStringId);
79 
80   // Interns a counter track associated with a cpu into the storage.
81   TrackId InternCpuCounterTrack(StringId name, uint32_t cpu);
82 
83   // Interns a counter track associated with a thread into the storage.
84   TrackId InternThreadCounterTrack(StringId name, UniqueTid utid);
85 
86   // Interns a counter track associated with a process into the storage.
87   TrackId InternProcessCounterTrack(StringId name,
88                                     UniquePid upid,
89                                     StringId unit = kNullStringId,
90                                     StringId description = kNullStringId);
91 
92   // Interns a counter track associated with an irq into the storage.
93   TrackId InternIrqCounterTrack(StringId name, int32_t irq);
94 
95   // Interns a counter track associated with an softirq into the storage.
96   TrackId InternSoftirqCounterTrack(StringId name, int32_t softirq);
97 
98   // Interns a counter track associated with a GPU into the storage.
99   TrackId InternGpuCounterTrack(StringId name, uint32_t gpu_id);
100 
101   // Creates a counter track associated with a GPU into the storage.
102   TrackId CreateGpuCounterTrack(StringId name,
103                                 uint32_t gpu_id,
104                                 StringId description = StringId::Null(),
105                                 StringId unit = StringId::Null());
106 
107   // Creaates a counter track for values within perf samples.
108   // The tracks themselves are managed by PerfSampleTracker.
109   TrackId CreatePerfCounterTrack(StringId name,
110                                  uint32_t perf_session_id,
111                                  uint32_t cpu,
112                                  bool is_timebase);
113 
114  private:
115   struct GpuTrackTuple {
116     StringId track_name;
117     StringId scope;
118     int64_t context_id;
119 
120     friend bool operator<(const GpuTrackTuple& l, const GpuTrackTuple& r) {
121       return std::tie(l.track_name, l.scope, l.context_id) <
122              std::tie(r.track_name, r.scope, r.context_id);
123     }
124   };
125   struct ChromeTrackTuple {
126     base::Optional<int64_t> upid;
127     int64_t source_id = 0;
128     StringId source_scope = StringId::Null();
129 
130     friend bool operator<(const ChromeTrackTuple& l,
131                           const ChromeTrackTuple& r) {
132       return std::tie(l.source_id, l.upid, l.source_scope) <
133              std::tie(r.source_id, r.upid, r.source_scope);
134     }
135   };
136 
137   std::map<UniqueTid, TrackId> thread_tracks_;
138   std::map<UniquePid, TrackId> process_tracks_;
139   std::map<int64_t /* correlation_id */, TrackId> fuchsia_async_tracks_;
140 
141   std::map<std::pair<StringId, uint32_t /* cpu */>, TrackId> cpu_tracks_;
142 
143   std::map<GpuTrackTuple, TrackId> gpu_tracks_;
144   std::map<ChromeTrackTuple, TrackId> chrome_tracks_;
145   std::map<UniquePid, TrackId> chrome_process_instant_tracks_;
146 
147   std::map<StringId, TrackId> global_counter_tracks_by_name_;
148   std::map<std::pair<StringId, uint32_t>, TrackId> cpu_counter_tracks_;
149   std::map<std::pair<StringId, UniqueTid>, TrackId> utid_counter_tracks_;
150   std::map<std::pair<StringId, UniquePid>, TrackId> upid_counter_tracks_;
151   std::map<std::pair<StringId, int32_t>, TrackId> irq_counter_tracks_;
152   std::map<std::pair<StringId, int32_t>, TrackId> softirq_counter_tracks_;
153   std::map<std::pair<StringId, uint32_t>, TrackId> gpu_counter_tracks_;
154 
155   base::Optional<TrackId> chrome_global_instant_track_id_;
156   base::Optional<TrackId> trigger_track_id_;
157 
158   const StringId source_key_ = kNullStringId;
159   const StringId source_id_key_ = kNullStringId;
160   const StringId source_id_is_process_scoped_key_ = kNullStringId;
161   const StringId source_scope_key_ = kNullStringId;
162   const StringId category_key_ = kNullStringId;
163 
164   const StringId fuchsia_source_ = kNullStringId;
165   const StringId chrome_source_ = kNullStringId;
166   const StringId android_source_ = kNullStringId;
167 
168   TraceProcessorContext* const context_;
169 };
170 
171 }  // namespace trace_processor
172 }  // namespace perfetto
173 
174 #endif  // SRC_TRACE_PROCESSOR_IMPORTERS_COMMON_TRACK_TRACKER_H_
175