• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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, int64_t correlation_id);
39 
40   // Interns a given GPU track into the storage.
41   TrackId InternGpuTrack(const tables::GpuTrackTable::Row& row);
42 
43   // Interns a legacy Chrome async event track into the storage.
44   TrackId InternLegacyChromeAsyncTrack(StringId name,
45                                        uint32_t upid,
46                                        int64_t source_id,
47                                        bool source_id_is_process_scoped,
48                                        StringId source_scope);
49 
50   // Interns a Android async track into the storage.
51   TrackId InternAndroidAsyncTrack(StringId name,
52                                   UniquePid upid,
53                                   int64_t cookie);
54 
55   // Interns a track for perf event stack samples, with process-wide grouping.
56   TrackId InternPerfStackTrack(UniquePid upid);
57 
58   // Interns a track for legacy Chrome process-scoped instant events into the
59   // storage.
60   TrackId InternLegacyChromeProcessInstantTrack(UniquePid upid);
61 
62   // Lazily creates the track for legacy Chrome global instant events.
63   TrackId GetOrCreateLegacyChromeGlobalInstantTrack();
64 
65   // Associate a TrackDescriptor track identified by the given |uuid| with a
66   // process's |pid|. This is called during tokenization. If a reservation for
67   // the same |uuid| already exists, verifies that the present reservation
68   // matches the new one.
69   //
70   // The track will be resolved to the process track (see InternProcessTrack())
71   // upon the first call to GetDescriptorTrack() with the same |uuid|. At this
72   // time, |pid| will also be resolved to a |upid|.
73   void ReserveDescriptorProcessTrack(uint64_t uuid,
74                                      StringId name,
75                                      uint32_t pid,
76                                      int64_t timestamp);
77 
78   // Associate a TrackDescriptor track identified by the given |uuid| with a
79   // thread's |pid| and |tid|. This is called during tokenization. If a
80   // reservation for the same |uuid| already exists, verifies that the present
81   // reservation matches the new one.
82   //
83   // The track will be resolved to the thread track (see InternThreadTrack())
84   // upon the first call to GetDescriptorTrack() with the same |uuid|. At this
85   // time, |pid| will also be resolved to a |upid|.
86   void ReserveDescriptorThreadTrack(uint64_t uuid,
87                                     uint64_t parent_uuid,
88                                     StringId name,
89                                     uint32_t pid,
90                                     uint32_t tid,
91                                     int64_t timestamp);
92 
93   // Associate a TrackDescriptor track identified by the given |uuid| with a
94   // parent track (usually a process- or thread-associated track). This is
95   // called during tokenization. If a reservation for the same |uuid| already
96   // exists, will attempt to update it.
97   //
98   // The track will be created upon the first call to GetDescriptorTrack() with
99   // the same |uuid|. If |parent_uuid| is 0, the track will become a global
100   // track. Otherwise, it will become a new track of the same type as its parent
101   // track.
102   void ReserveDescriptorChildTrack(uint64_t uuid,
103                                    uint64_t parent_uuid,
104                                    StringId name);
105 
106   // Associate a counter-type TrackDescriptor track identified by the given
107   // |uuid| with a parent track (usually a process or thread track). This is
108   // called during tokenization. If a reservation for the same |uuid| already
109   // exists, will attempt to update it. The provided |category| will be stored
110   // into the track's args.
111   //
112   // If |is_incremental| is true, the counter will only be valid on the packet
113   // sequence identified by |packet_sequence_id|. |unit_multiplier| is an
114   // optional multiplication factor applied to counter values. Values for the
115   // counter will be translated during tokenization via
116   // ConvertToAbsoluteCounterValue().
117   //
118   // The track will be created upon the first call to GetDescriptorTrack() with
119   // the same |uuid|. If |parent_uuid| is 0, the track will become a global
120   // track. Otherwise, it will become a new counter track for the same
121   // process/thread as its parent track.
122   void ReserveDescriptorCounterTrack(uint64_t uuid,
123                                      uint64_t parent_uuid,
124                                      StringId name,
125                                      StringId category,
126                                      int64_t unit_multiplier,
127                                      bool is_incremental,
128                                      uint32_t packet_sequence_id);
129 
130   // Returns the ID of the track for the TrackDescriptor with the given |uuid|.
131   // This is called during parsing. The first call to GetDescriptorTrack() for
132   // each |uuid| resolves and inserts the track (and its parent tracks,
133   // following the parent_uuid chain recursively) based on reservations made for
134   // the |uuid|. Returns nullopt if no track for a descriptor with this |uuid|
135   // has been reserved.
136   base::Optional<TrackId> GetDescriptorTrack(uint64_t uuid);
137 
138   // Returns the ID of the implicit trace-global default TrackDescriptor track.
139   TrackId GetOrCreateDefaultDescriptorTrack();
140 
141   // Returns the ID of the implicit trace-global default track for triggers
142   // received by the service.
143   TrackId GetOrCreateTriggerTrack();
144 
145   // Interns a global counter track into the storage.
146   TrackId InternGlobalCounterTrack(StringId name);
147 
148   // Interns a counter track associated with a cpu into the storage.
149   TrackId InternCpuCounterTrack(StringId name, uint32_t cpu);
150 
151   // Interns a counter track associated with a thread into the storage.
152   TrackId InternThreadCounterTrack(StringId name, UniqueTid utid);
153 
154   // Interns a counter track associated with a process into the storage.
155   TrackId InternProcessCounterTrack(StringId name, UniquePid upid);
156 
157   // Interns a counter track associated with an irq into the storage.
158   TrackId InternIrqCounterTrack(StringId name, int32_t irq);
159 
160   // Interns a counter track associated with an softirq into the storage.
161   TrackId InternSoftirqCounterTrack(StringId name, int32_t softirq);
162 
163   // Interns a counter track associated with a GPU into the storage.
164   TrackId InternGpuCounterTrack(StringId name, uint32_t gpu_id);
165 
166   // Creates a counter track associated with a GPU into the storage.
167   TrackId CreateGpuCounterTrack(StringId name,
168                                 uint32_t gpu_id,
169                                 StringId description = StringId::Null(),
170                                 StringId unit = StringId::Null());
171 
172   // Converts the given counter value to an absolute value in the unit of the
173   // counter, applying incremental delta encoding or unit multipliers as
174   // necessary. If the counter uses incremental encoding, |packet_sequence_id|
175   // must match the one in its track reservation. Returns base::nullopt if the
176   // counter track is unknown or an invalid |packet_sequence_id| was passed.
177   base::Optional<int64_t> ConvertToAbsoluteCounterValue(
178       uint64_t counter_track_uuid,
179       uint32_t packet_sequence_id,
180       int64_t value);
181 
182   // Called by ProtoTraceTokenizer whenever incremental state is cleared on a
183   // packet sequence. Resets counter values for any incremental counters of
184   // the sequence identified by |packet_sequence_id|.
185   void OnIncrementalStateCleared(uint32_t packet_sequence_id);
186 
187  private:
188   struct GpuTrackTuple {
189     StringId track_name;
190     StringId scope;
191     int64_t context_id;
192 
193     friend bool operator<(const GpuTrackTuple& l, const GpuTrackTuple& r) {
194       return std::tie(l.track_name, l.scope, l.context_id) <
195              std::tie(r.track_name, r.scope, r.context_id);
196     }
197   };
198   struct ChromeTrackTuple {
199     base::Optional<int64_t> upid;
200     int64_t source_id = 0;
201     StringId source_scope = StringId::Null();
202 
203     friend bool operator<(const ChromeTrackTuple& l,
204                           const ChromeTrackTuple& r) {
205       return std::tie(l.source_id, l.upid, l.source_scope) <
206              std::tie(r.source_id, r.upid, r.source_scope);
207     }
208   };
209   struct AndroidAsyncTrackTuple {
210     UniquePid upid;
211     int64_t cookie;
212     StringId name;
213 
214     friend bool operator<(const AndroidAsyncTrackTuple& l,
215                           const AndroidAsyncTrackTuple& r) {
216       return std::tie(l.upid, l.cookie, l.name) <
217              std::tie(r.upid, r.cookie, r.name);
218     }
219   };
220   struct DescriptorTrackReservation {
221     uint64_t parent_uuid = 0;
222     base::Optional<uint32_t> pid;
223     base::Optional<uint32_t> tid;
224     int64_t min_timestamp = 0;  // only set if |pid| and/or |tid| is set.
225     StringId name = kNullStringId;
226 
227     // For counter tracks.
228     bool is_counter = false;
229     StringId category = kNullStringId;
230     int64_t unit_multiplier = 1;
231     bool is_incremental = false;
232     uint32_t packet_sequence_id = 0;
233     int64_t latest_value = 0;
234 
235     // Whether |other| is a valid descriptor for this track reservation. A track
236     // should always remain nested underneath its original parent.
IsForSameTrackDescriptorTrackReservation237     bool IsForSameTrack(const DescriptorTrackReservation& other) {
238       // Note that |min_timestamp|, |latest_value|, and |name| are ignored for
239       // this comparison.
240       return std::tie(parent_uuid, pid, tid, is_counter, category,
241                       unit_multiplier, is_incremental, packet_sequence_id) ==
242              std::tie(other.parent_uuid, pid, tid, is_counter, category,
243                       unit_multiplier, is_incremental, packet_sequence_id);
244     }
245   };
246 
247   base::Optional<TrackId> GetDescriptorTrackImpl(
248       uint64_t uuid,
249       std::vector<uint64_t>* descendent_uuids = nullptr);
250   TrackId ResolveDescriptorTrack(uint64_t uuid,
251                                  const DescriptorTrackReservation&,
252                                  std::vector<uint64_t>* descendent_uuids);
253 
254   static constexpr uint64_t kDefaultDescriptorTrackUuid = 0u;
255 
256   std::map<UniqueTid, TrackId> thread_tracks_;
257   std::map<UniquePid, TrackId> process_tracks_;
258   std::map<int64_t /* correlation_id */, TrackId> fuchsia_async_tracks_;
259   std::map<GpuTrackTuple, TrackId> gpu_tracks_;
260   std::map<ChromeTrackTuple, TrackId> chrome_tracks_;
261   std::map<AndroidAsyncTrackTuple, TrackId> android_async_tracks_;
262   std::map<UniquePid, TrackId> chrome_process_instant_tracks_;
263   base::Optional<TrackId> chrome_global_instant_track_id_;
264   std::map<uint64_t /* uuid */, DescriptorTrackReservation>
265       reserved_descriptor_tracks_;
266   std::map<uint64_t /* uuid */, TrackId> resolved_descriptor_tracks_;
267   std::map<UniquePid, TrackId> perf_stack_tracks_;
268 
269   std::map<StringId, TrackId> global_counter_tracks_by_name_;
270   std::map<std::pair<StringId, uint32_t>, TrackId> cpu_counter_tracks_;
271   std::map<std::pair<StringId, UniqueTid>, TrackId> utid_counter_tracks_;
272   std::map<std::pair<StringId, UniquePid>, TrackId> upid_counter_tracks_;
273   std::map<std::pair<StringId, int32_t>, TrackId> irq_counter_tracks_;
274   std::map<std::pair<StringId, int32_t>, TrackId> softirq_counter_tracks_;
275   std::map<std::pair<StringId, uint32_t>, TrackId> gpu_counter_tracks_;
276 
277   // Stores the descriptor uuid used for the primary process/thread track
278   // for the given upid / utid. Used for pid/tid reuse detection.
279   std::map<UniquePid, uint64_t /*uuid*/> descriptor_uuids_by_upid_;
280   std::map<UniqueTid, uint64_t /*uuid*/> descriptor_uuids_by_utid_;
281 
282   base::Optional<TrackId> trigger_track_id_;
283 
284   const StringId source_key_ = kNullStringId;
285   const StringId source_id_key_ = kNullStringId;
286   const StringId source_id_is_process_scoped_key_ = kNullStringId;
287   const StringId source_scope_key_ = kNullStringId;
288   const StringId parent_track_id_key_ = kNullStringId;
289   const StringId category_key_ = kNullStringId;
290 
291   const StringId fuchsia_source_ = kNullStringId;
292   const StringId chrome_source_ = kNullStringId;
293   const StringId android_source_ = kNullStringId;
294   const StringId descriptor_source_ = kNullStringId;
295 
296   const StringId default_descriptor_track_name_ = kNullStringId;
297 
298   TraceProcessorContext* const context_;
299 };
300 
301 }  // namespace trace_processor
302 }  // namespace perfetto
303 
304 #endif  // SRC_TRACE_PROCESSOR_IMPORTERS_COMMON_TRACK_TRACKER_H_
305