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 #include "src/trace_processor/importers/common/track_tracker.h"
18
19 #include "src/trace_processor/importers/common/args_tracker.h"
20 #include "src/trace_processor/importers/common/process_tracker.h"
21
22 namespace perfetto {
23 namespace trace_processor {
24
25 // static
26 constexpr uint64_t TrackTracker::kDefaultDescriptorTrackUuid;
27
TrackTracker(TraceProcessorContext * context)28 TrackTracker::TrackTracker(TraceProcessorContext* context)
29 : source_key_(context->storage->InternString("source")),
30 source_id_key_(context->storage->InternString("source_id")),
31 source_id_is_process_scoped_key_(
32 context->storage->InternString("source_id_is_process_scoped")),
33 source_scope_key_(context->storage->InternString("source_scope")),
34 parent_track_id_key_(context->storage->InternString("parent_track_id")),
35 category_key_(context->storage->InternString("category")),
36 fuchsia_source_(context->storage->InternString("fuchsia")),
37 chrome_source_(context->storage->InternString("chrome")),
38 android_source_(context->storage->InternString("android")),
39 descriptor_source_(context->storage->InternString("descriptor")),
40 default_descriptor_track_name_(
41 context->storage->InternString("Default Track")),
42 context_(context) {}
43
InternThreadTrack(UniqueTid utid)44 TrackId TrackTracker::InternThreadTrack(UniqueTid utid) {
45 auto it = thread_tracks_.find(utid);
46 if (it != thread_tracks_.end())
47 return it->second;
48
49 tables::ThreadTrackTable::Row row;
50 row.utid = utid;
51 auto id = context_->storage->mutable_thread_track_table()->Insert(row).id;
52 thread_tracks_[utid] = id;
53 return id;
54 }
55
InternProcessTrack(UniquePid upid)56 TrackId TrackTracker::InternProcessTrack(UniquePid upid) {
57 auto it = process_tracks_.find(upid);
58 if (it != process_tracks_.end())
59 return it->second;
60
61 tables::ProcessTrackTable::Row row;
62 row.upid = upid;
63 auto id = context_->storage->mutable_process_track_table()->Insert(row).id;
64 process_tracks_[upid] = id;
65 return id;
66 }
67
InternFuchsiaAsyncTrack(StringId name,int64_t correlation_id)68 TrackId TrackTracker::InternFuchsiaAsyncTrack(StringId name,
69 int64_t correlation_id) {
70 auto it = fuchsia_async_tracks_.find(correlation_id);
71 if (it != fuchsia_async_tracks_.end())
72 return it->second;
73
74 tables::TrackTable::Row row(name);
75 auto id = context_->storage->mutable_track_table()->Insert(row).id;
76 fuchsia_async_tracks_[correlation_id] = id;
77
78 context_->args_tracker->AddArgsTo(id)
79 .AddArg(source_key_, Variadic::String(fuchsia_source_))
80 .AddArg(source_id_key_, Variadic::Integer(correlation_id));
81
82 return id;
83 }
84
InternGpuTrack(const tables::GpuTrackTable::Row & row)85 TrackId TrackTracker::InternGpuTrack(const tables::GpuTrackTable::Row& row) {
86 GpuTrackTuple tuple{row.name, row.scope, row.context_id.value_or(0)};
87
88 auto it = gpu_tracks_.find(tuple);
89 if (it != gpu_tracks_.end())
90 return it->second;
91
92 auto id = context_->storage->mutable_gpu_track_table()->Insert(row).id;
93 gpu_tracks_[tuple] = id;
94 return id;
95 }
96
InternLegacyChromeAsyncTrack(StringId name,uint32_t upid,int64_t source_id,bool source_id_is_process_scoped,StringId source_scope)97 TrackId TrackTracker::InternLegacyChromeAsyncTrack(
98 StringId name,
99 uint32_t upid,
100 int64_t source_id,
101 bool source_id_is_process_scoped,
102 StringId source_scope) {
103 ChromeTrackTuple tuple;
104 if (source_id_is_process_scoped)
105 tuple.upid = upid;
106 tuple.source_id = source_id;
107 tuple.source_scope = source_scope;
108
109 auto it = chrome_tracks_.find(tuple);
110 if (it != chrome_tracks_.end())
111 return it->second;
112
113 // Legacy async tracks are always drawn in the context of a process, even if
114 // the ID's scope is global.
115 tables::ProcessTrackTable::Row track(name);
116 track.upid = upid;
117 TrackId id =
118 context_->storage->mutable_process_track_table()->Insert(track).id;
119 chrome_tracks_[tuple] = id;
120
121 context_->args_tracker->AddArgsTo(id)
122 .AddArg(source_key_, Variadic::String(chrome_source_))
123 .AddArg(source_id_key_, Variadic::Integer(source_id))
124 .AddArg(source_id_is_process_scoped_key_,
125 Variadic::Boolean(source_id_is_process_scoped))
126 .AddArg(source_scope_key_, Variadic::String(source_scope));
127
128 return id;
129 }
130
InternAndroidAsyncTrack(StringId name,UniquePid upid,int64_t cookie)131 TrackId TrackTracker::InternAndroidAsyncTrack(StringId name,
132 UniquePid upid,
133 int64_t cookie) {
134 AndroidAsyncTrackTuple tuple{upid, cookie, name};
135
136 auto it = android_async_tracks_.find(tuple);
137 if (it != android_async_tracks_.end())
138 return it->second;
139
140 tables::ProcessTrackTable::Row row(name);
141 row.upid = upid;
142 auto id = context_->storage->mutable_process_track_table()->Insert(row).id;
143 android_async_tracks_[tuple] = id;
144
145 context_->args_tracker->AddArgsTo(id)
146 .AddArg(source_key_, Variadic::String(android_source_))
147 .AddArg(source_id_key_, Variadic::Integer(cookie));
148
149 return id;
150 }
151
InternPerfStackTrack(UniquePid upid)152 TrackId TrackTracker::InternPerfStackTrack(UniquePid upid) {
153 auto it = perf_stack_tracks_.find(upid);
154 if (it != perf_stack_tracks_.end())
155 return it->second;
156
157 StringId name = context_->storage->InternString("Stack samples");
158 tables::ProcessTrackTable::Row row(name);
159 row.upid = upid;
160 auto id = context_->storage->mutable_process_track_table()->Insert(row).id;
161 perf_stack_tracks_[upid] = id;
162 return id;
163 }
164
InternLegacyChromeProcessInstantTrack(UniquePid upid)165 TrackId TrackTracker::InternLegacyChromeProcessInstantTrack(UniquePid upid) {
166 auto it = chrome_process_instant_tracks_.find(upid);
167 if (it != chrome_process_instant_tracks_.end())
168 return it->second;
169
170 tables::ProcessTrackTable::Row row;
171 row.upid = upid;
172 auto id = context_->storage->mutable_process_track_table()->Insert(row).id;
173 chrome_process_instant_tracks_[upid] = id;
174
175 context_->args_tracker->AddArgsTo(id).AddArg(
176 source_key_, Variadic::String(chrome_source_));
177
178 return id;
179 }
180
GetOrCreateLegacyChromeGlobalInstantTrack()181 TrackId TrackTracker::GetOrCreateLegacyChromeGlobalInstantTrack() {
182 if (!chrome_global_instant_track_id_) {
183 chrome_global_instant_track_id_ =
184 context_->storage->mutable_track_table()->Insert({}).id;
185
186 context_->args_tracker->AddArgsTo(*chrome_global_instant_track_id_)
187 .AddArg(source_key_, Variadic::String(chrome_source_));
188 }
189 return *chrome_global_instant_track_id_;
190 }
191
ReserveDescriptorProcessTrack(uint64_t uuid,StringId name,uint32_t pid,int64_t timestamp)192 void TrackTracker::ReserveDescriptorProcessTrack(uint64_t uuid,
193 StringId name,
194 uint32_t pid,
195 int64_t timestamp) {
196 DescriptorTrackReservation reservation;
197 reservation.min_timestamp = timestamp;
198 reservation.pid = pid;
199 reservation.name = name;
200
201 std::map<uint64_t, DescriptorTrackReservation>::iterator it;
202 bool inserted;
203 std::tie(it, inserted) =
204 reserved_descriptor_tracks_.insert(std::make_pair<>(uuid, reservation));
205
206 if (inserted)
207 return;
208
209 if (!it->second.IsForSameTrack(reservation)) {
210 // Process tracks should not be reassigned to a different pid later (neither
211 // should the type of the track change).
212 PERFETTO_DLOG("New track reservation for process track with uuid %" PRIu64
213 " doesn't match earlier one",
214 uuid);
215 context_->storage->IncrementStats(stats::track_event_tokenizer_errors);
216 return;
217 }
218
219 it->second.min_timestamp = std::min(it->second.min_timestamp, timestamp);
220 }
221
ReserveDescriptorThreadTrack(uint64_t uuid,uint64_t parent_uuid,StringId name,uint32_t pid,uint32_t tid,int64_t timestamp)222 void TrackTracker::ReserveDescriptorThreadTrack(uint64_t uuid,
223 uint64_t parent_uuid,
224 StringId name,
225 uint32_t pid,
226 uint32_t tid,
227 int64_t timestamp) {
228 DescriptorTrackReservation reservation;
229 reservation.min_timestamp = timestamp;
230 reservation.parent_uuid = parent_uuid;
231 reservation.pid = pid;
232 reservation.tid = tid;
233 reservation.name = name;
234
235 std::map<uint64_t, DescriptorTrackReservation>::iterator it;
236 bool inserted;
237 std::tie(it, inserted) =
238 reserved_descriptor_tracks_.insert(std::make_pair<>(uuid, reservation));
239
240 if (inserted)
241 return;
242
243 if (!it->second.IsForSameTrack(reservation)) {
244 // Thread tracks should not be reassigned to a different pid/tid later
245 // (neither should the type of the track change).
246 PERFETTO_DLOG("New track reservation for thread track with uuid %" PRIu64
247 " doesn't match earlier one",
248 uuid);
249 context_->storage->IncrementStats(stats::track_event_tokenizer_errors);
250 return;
251 }
252
253 it->second.min_timestamp = std::min(it->second.min_timestamp, timestamp);
254 }
255
ReserveDescriptorCounterTrack(uint64_t uuid,uint64_t parent_uuid,StringId name,StringId category,int64_t unit_multiplier,bool is_incremental,uint32_t packet_sequence_id)256 void TrackTracker::ReserveDescriptorCounterTrack(uint64_t uuid,
257 uint64_t parent_uuid,
258 StringId name,
259 StringId category,
260 int64_t unit_multiplier,
261 bool is_incremental,
262 uint32_t packet_sequence_id) {
263 DescriptorTrackReservation reservation;
264 reservation.parent_uuid = parent_uuid;
265 reservation.is_counter = true;
266 reservation.name = name;
267 reservation.category = category;
268 reservation.unit_multiplier = unit_multiplier;
269 reservation.is_incremental = is_incremental;
270 // Incrementally encoded counters are only valid on a single sequence.
271 if (is_incremental)
272 reservation.packet_sequence_id = packet_sequence_id;
273
274 std::map<uint64_t, DescriptorTrackReservation>::iterator it;
275 bool inserted;
276 std::tie(it, inserted) =
277 reserved_descriptor_tracks_.insert(std::make_pair<>(uuid, reservation));
278
279 if (inserted || it->second.IsForSameTrack(reservation))
280 return;
281
282 // Counter tracks should not be reassigned to a different parent track later
283 // (neither should the type of the track change).
284 PERFETTO_DLOG("New track reservation for counter track with uuid %" PRIu64
285 " doesn't match earlier one",
286 uuid);
287 context_->storage->IncrementStats(stats::track_event_tokenizer_errors);
288 }
289
ReserveDescriptorChildTrack(uint64_t uuid,uint64_t parent_uuid,StringId name)290 void TrackTracker::ReserveDescriptorChildTrack(uint64_t uuid,
291 uint64_t parent_uuid,
292 StringId name) {
293 DescriptorTrackReservation reservation;
294 reservation.parent_uuid = parent_uuid;
295 reservation.name = name;
296
297 std::map<uint64_t, DescriptorTrackReservation>::iterator it;
298 bool inserted;
299 std::tie(it, inserted) =
300 reserved_descriptor_tracks_.insert(std::make_pair<>(uuid, reservation));
301
302 if (inserted || it->second.IsForSameTrack(reservation))
303 return;
304
305 // Child tracks should not be reassigned to a different parent track later
306 // (neither should the type of the track change).
307 PERFETTO_DLOG("New track reservation for child track with uuid %" PRIu64
308 " doesn't match earlier one",
309 uuid);
310 context_->storage->IncrementStats(stats::track_event_tokenizer_errors);
311 }
312
GetDescriptorTrack(uint64_t uuid)313 base::Optional<TrackId> TrackTracker::GetDescriptorTrack(uint64_t uuid) {
314 return GetDescriptorTrackImpl(uuid);
315 }
316
GetDescriptorTrackImpl(uint64_t uuid,std::vector<uint64_t> * descendent_uuids)317 base::Optional<TrackId> TrackTracker::GetDescriptorTrackImpl(
318 uint64_t uuid,
319 std::vector<uint64_t>* descendent_uuids) {
320 auto it = resolved_descriptor_tracks_.find(uuid);
321 if (it == resolved_descriptor_tracks_.end()) {
322 auto reservation_it = reserved_descriptor_tracks_.find(uuid);
323 if (reservation_it == reserved_descriptor_tracks_.end())
324 return base::nullopt;
325 TrackId track_id =
326 ResolveDescriptorTrack(uuid, reservation_it->second, descendent_uuids);
327 resolved_descriptor_tracks_[uuid] = track_id;
328 return track_id;
329 }
330 return it->second;
331 }
332
ResolveDescriptorTrack(uint64_t uuid,const DescriptorTrackReservation & reservation,std::vector<uint64_t> * descendent_uuids)333 TrackId TrackTracker::ResolveDescriptorTrack(
334 uint64_t uuid,
335 const DescriptorTrackReservation& reservation,
336 std::vector<uint64_t>* descendent_uuids) {
337 auto set_track_name_and_return = [this, &reservation](TrackId track_id) {
338 // Initialize the track name here, so that, if a name was given in the
339 // reservation, it is set immediately after resolution takes place.
340 if (reservation.name != kNullStringId) {
341 auto* tracks = context_->storage->mutable_track_table();
342 tracks->mutable_name()->Set(*tracks->id().IndexOf(track_id),
343 reservation.name);
344 }
345 return track_id;
346 };
347
348 static constexpr size_t kMaxAncestors = 10;
349
350 // Try to resolve any parent tracks recursively, too.
351 base::Optional<TrackId> parent_track_id;
352 if (reservation.parent_uuid) {
353 // Input data may contain loops or extremely long ancestor track chains. To
354 // avoid stack overflow in these situations, we keep track of the ancestors
355 // seen in the recursion.
356 std::unique_ptr<std::vector<uint64_t>> owned_descendent_uuids;
357 if (!descendent_uuids) {
358 owned_descendent_uuids.reset(new std::vector<uint64_t>());
359 descendent_uuids = owned_descendent_uuids.get();
360 }
361 descendent_uuids->push_back(uuid);
362
363 if (descendent_uuids->size() > kMaxAncestors) {
364 PERFETTO_ELOG(
365 "Too many ancestors in parent_track_uuid hierarchy at track %" PRIu64
366 " with parent %" PRIu64,
367 uuid, reservation.parent_uuid);
368 } else if (std::find(descendent_uuids->begin(), descendent_uuids->end(),
369 reservation.parent_uuid) != descendent_uuids->end()) {
370 PERFETTO_ELOG(
371 "Loop detected in parent_track_uuid hierarchy at track %" PRIu64
372 " with parent %" PRIu64,
373 uuid, reservation.parent_uuid);
374 } else {
375 parent_track_id =
376 GetDescriptorTrackImpl(reservation.parent_uuid, descendent_uuids);
377 if (!parent_track_id) {
378 PERFETTO_ELOG("Unknown parent track %" PRIu64 " for track %" PRIu64,
379 reservation.parent_uuid, uuid);
380 }
381 }
382
383 descendent_uuids->pop_back();
384 if (owned_descendent_uuids)
385 descendent_uuids = nullptr;
386 }
387
388 if (reservation.tid) {
389 UniqueTid utid = context_->process_tracker->UpdateThread(*reservation.tid,
390 *reservation.pid);
391 auto it_and_inserted =
392 descriptor_uuids_by_utid_.insert(std::make_pair<>(utid, uuid));
393 if (!it_and_inserted.second) {
394 // We already saw a another track with a different uuid for this thread.
395 // Since there should only be one descriptor track for each thread, we
396 // assume that its tid was reused. So, start a new thread.
397 uint64_t old_uuid = it_and_inserted.first->second;
398 PERFETTO_DCHECK(old_uuid != uuid); // Every track is only resolved once.
399
400 PERFETTO_DLOG("Detected tid reuse (pid: %" PRIu32 " tid: %" PRIu32
401 ") from track descriptors (old uuid: %" PRIu64
402 " new uuid: %" PRIu64 " timestamp: %" PRId64 ")",
403 *reservation.pid, *reservation.tid, old_uuid, uuid,
404 reservation.min_timestamp);
405
406 utid = context_->process_tracker->StartNewThread(
407 base::nullopt, *reservation.tid, kNullStringId);
408
409 // Associate the new thread with its process.
410 PERFETTO_CHECK(context_->process_tracker->UpdateThread(
411 *reservation.tid, *reservation.pid) == utid);
412
413 descriptor_uuids_by_utid_[utid] = uuid;
414 }
415 return set_track_name_and_return(InternThreadTrack(utid));
416 }
417
418 if (reservation.pid) {
419 UniquePid upid =
420 context_->process_tracker->GetOrCreateProcess(*reservation.pid);
421 auto it_and_inserted =
422 descriptor_uuids_by_upid_.insert(std::make_pair<>(upid, uuid));
423 if (!it_and_inserted.second) {
424 // We already saw a another track with a different uuid for this process.
425 // Since there should only be one descriptor track for each process, we
426 // assume that its pid was reused. So, start a new process.
427 uint64_t old_uuid = it_and_inserted.first->second;
428 PERFETTO_DCHECK(old_uuid != uuid); // Every track is only resolved once.
429
430 PERFETTO_DLOG("Detected pid reuse (pid: %" PRIu32
431 ") from track descriptors (old uuid: %" PRIu64
432 " new uuid: %" PRIu64 " timestamp: %" PRId64 ")",
433 *reservation.pid, old_uuid, uuid,
434 reservation.min_timestamp);
435
436 upid = context_->process_tracker->StartNewProcess(
437 base::nullopt, base::nullopt, *reservation.pid, kNullStringId);
438
439 descriptor_uuids_by_upid_[upid] = uuid;
440 }
441 return set_track_name_and_return(InternProcessTrack(upid));
442 }
443
444 base::Optional<TrackId> track_id;
445 if (parent_track_id) {
446 // If parent is a thread track, create another thread-associated track.
447 auto* thread_tracks = context_->storage->mutable_thread_track_table();
448 base::Optional<uint32_t> thread_track_index =
449 thread_tracks->id().IndexOf(*parent_track_id);
450 if (thread_track_index) {
451 if (reservation.is_counter) {
452 // Thread counter track.
453 auto* thread_counter_tracks =
454 context_->storage->mutable_thread_counter_track_table();
455 tables::ThreadCounterTrackTable::Row row;
456 row.utid = thread_tracks->utid()[*thread_track_index];
457 track_id = thread_counter_tracks->Insert(row).id;
458 } else {
459 // Thread slice track.
460 tables::ThreadTrackTable::Row row;
461 row.utid = thread_tracks->utid()[*thread_track_index];
462 track_id = thread_tracks->Insert(row).id;
463 }
464 } else {
465 // If parent is a process track, create another process-associated track.
466 auto* process_tracks = context_->storage->mutable_process_track_table();
467 base::Optional<uint32_t> process_track_index =
468 process_tracks->id().IndexOf(*parent_track_id);
469 if (process_track_index) {
470 if (reservation.is_counter) {
471 // Process counter track.
472 auto* thread_counter_tracks =
473 context_->storage->mutable_process_counter_track_table();
474 tables::ProcessCounterTrackTable::Row row;
475 row.upid = process_tracks->upid()[*process_track_index];
476 track_id = thread_counter_tracks->Insert(row).id;
477 } else {
478 // Process slice track.
479 tables::ProcessTrackTable::Row row;
480 row.upid = process_tracks->upid()[*process_track_index];
481 track_id = process_tracks->Insert(row).id;
482 }
483 }
484 }
485 }
486
487 // Otherwise create a global track.
488 if (!track_id) {
489 if (reservation.is_counter) {
490 // Global counter track.
491 tables::CounterTrackTable::Row row;
492 track_id =
493 context_->storage->mutable_counter_track_table()->Insert(row).id;
494 } else {
495 // Global slice track.
496 tables::TrackTable::Row row;
497 track_id = context_->storage->mutable_track_table()->Insert(row).id;
498 }
499 // The global track with no uuid is the default global track (e.g. for
500 // global instant events). Any other global tracks are considered children
501 // of the default track.
502 if (!parent_track_id && uuid) {
503 // Detect loops where the default track has a parent that itself is a
504 // global track (and thus should be parent of the default track).
505 if (descendent_uuids &&
506 std::find(descendent_uuids->begin(), descendent_uuids->end(),
507 kDefaultDescriptorTrackUuid) != descendent_uuids->end()) {
508 PERFETTO_ELOG(
509 "Loop detected in parent_track_uuid hierarchy at track %" PRIu64
510 " with parent %" PRIu64,
511 uuid, kDefaultDescriptorTrackUuid);
512 } else {
513 parent_track_id = GetOrCreateDefaultDescriptorTrack();
514 }
515 }
516 }
517
518 auto args = context_->args_tracker->AddArgsTo(*track_id);
519 args.AddArg(source_key_, Variadic::String(descriptor_source_))
520 .AddArg(source_id_key_, Variadic::Integer(static_cast<int64_t>(uuid)));
521 if (parent_track_id) {
522 args.AddArg(parent_track_id_key_,
523 Variadic::Integer(parent_track_id->value));
524 }
525 if (reservation.category != kNullStringId) {
526 args.AddArg(category_key_, Variadic::String(reservation.category));
527 }
528 return set_track_name_and_return(*track_id);
529 }
530
GetOrCreateDefaultDescriptorTrack()531 TrackId TrackTracker::GetOrCreateDefaultDescriptorTrack() {
532 // If the default track was already reserved (e.g. because a producer emitted
533 // a descriptor for it) or created, resolve and return it.
534 base::Optional<TrackId> track_id =
535 GetDescriptorTrack(kDefaultDescriptorTrackUuid);
536 if (track_id)
537 return *track_id;
538
539 // Otherwise reserve a new track and resolve it.
540 ReserveDescriptorChildTrack(kDefaultDescriptorTrackUuid, /*parent_uuid=*/0,
541 default_descriptor_track_name_);
542 return *GetDescriptorTrack(kDefaultDescriptorTrackUuid);
543 }
544
GetOrCreateTriggerTrack()545 TrackId TrackTracker::GetOrCreateTriggerTrack() {
546 if (trigger_track_id_) {
547 return *trigger_track_id_;
548 }
549 tables::TrackTable::Row row;
550 row.name = context_->storage->InternString("Trace Triggers");
551 trigger_track_id_ = context_->storage->mutable_track_table()->Insert(row).id;
552 return *trigger_track_id_;
553 }
554
InternGlobalCounterTrack(StringId name)555 TrackId TrackTracker::InternGlobalCounterTrack(StringId name) {
556 auto it = global_counter_tracks_by_name_.find(name);
557 if (it != global_counter_tracks_by_name_.end()) {
558 return it->second;
559 }
560
561 tables::CounterTrackTable::Row row(name);
562 TrackId track =
563 context_->storage->mutable_counter_track_table()->Insert(row).id;
564 global_counter_tracks_by_name_[name] = track;
565 return track;
566 }
567
InternCpuCounterTrack(StringId name,uint32_t cpu)568 TrackId TrackTracker::InternCpuCounterTrack(StringId name, uint32_t cpu) {
569 auto it = cpu_counter_tracks_.find(std::make_pair(name, cpu));
570 if (it != cpu_counter_tracks_.end()) {
571 return it->second;
572 }
573
574 tables::CpuCounterTrackTable::Row row(name);
575 row.cpu = cpu;
576
577 TrackId track =
578 context_->storage->mutable_cpu_counter_track_table()->Insert(row).id;
579 cpu_counter_tracks_[std::make_pair(name, cpu)] = track;
580 return track;
581 }
582
InternThreadCounterTrack(StringId name,UniqueTid utid)583 TrackId TrackTracker::InternThreadCounterTrack(StringId name, UniqueTid utid) {
584 auto it = utid_counter_tracks_.find(std::make_pair(name, utid));
585 if (it != utid_counter_tracks_.end()) {
586 return it->second;
587 }
588
589 tables::ThreadCounterTrackTable::Row row(name);
590 row.utid = utid;
591
592 TrackId track =
593 context_->storage->mutable_thread_counter_track_table()->Insert(row).id;
594 utid_counter_tracks_[std::make_pair(name, utid)] = track;
595 return track;
596 }
597
InternProcessCounterTrack(StringId name,UniquePid upid)598 TrackId TrackTracker::InternProcessCounterTrack(StringId name, UniquePid upid) {
599 auto it = upid_counter_tracks_.find(std::make_pair(name, upid));
600 if (it != upid_counter_tracks_.end()) {
601 return it->second;
602 }
603
604 tables::ProcessCounterTrackTable::Row row(name);
605 row.upid = upid;
606
607 TrackId track =
608 context_->storage->mutable_process_counter_track_table()->Insert(row).id;
609 upid_counter_tracks_[std::make_pair(name, upid)] = track;
610 return track;
611 }
612
InternIrqCounterTrack(StringId name,int32_t irq)613 TrackId TrackTracker::InternIrqCounterTrack(StringId name, int32_t irq) {
614 auto it = irq_counter_tracks_.find(std::make_pair(name, irq));
615 if (it != irq_counter_tracks_.end()) {
616 return it->second;
617 }
618
619 tables::IrqCounterTrackTable::Row row(name);
620 row.irq = irq;
621
622 TrackId track =
623 context_->storage->mutable_irq_counter_track_table()->Insert(row).id;
624 irq_counter_tracks_[std::make_pair(name, irq)] = track;
625 return track;
626 }
627
InternSoftirqCounterTrack(StringId name,int32_t softirq)628 TrackId TrackTracker::InternSoftirqCounterTrack(StringId name,
629 int32_t softirq) {
630 auto it = softirq_counter_tracks_.find(std::make_pair(name, softirq));
631 if (it != softirq_counter_tracks_.end()) {
632 return it->second;
633 }
634
635 tables::SoftirqCounterTrackTable::Row row(name);
636 row.softirq = softirq;
637
638 TrackId track =
639 context_->storage->mutable_softirq_counter_track_table()->Insert(row).id;
640 softirq_counter_tracks_[std::make_pair(name, softirq)] = track;
641 return track;
642 }
643
InternGpuCounterTrack(StringId name,uint32_t gpu_id)644 TrackId TrackTracker::InternGpuCounterTrack(StringId name, uint32_t gpu_id) {
645 auto it = gpu_counter_tracks_.find(std::make_pair(name, gpu_id));
646 if (it != gpu_counter_tracks_.end()) {
647 return it->second;
648 }
649 TrackId track = CreateGpuCounterTrack(name, gpu_id);
650 gpu_counter_tracks_[std::make_pair(name, gpu_id)] = track;
651 return track;
652 }
653
CreateGpuCounterTrack(StringId name,uint32_t gpu_id,StringId description,StringId unit)654 TrackId TrackTracker::CreateGpuCounterTrack(StringId name,
655 uint32_t gpu_id,
656 StringId description,
657 StringId unit) {
658 tables::GpuCounterTrackTable::Row row(name);
659 row.gpu_id = gpu_id;
660 row.description = description;
661 row.unit = unit;
662
663 return context_->storage->mutable_gpu_counter_track_table()->Insert(row).id;
664 }
665
ConvertToAbsoluteCounterValue(uint64_t counter_track_uuid,uint32_t packet_sequence_id,int64_t value)666 base::Optional<int64_t> TrackTracker::ConvertToAbsoluteCounterValue(
667 uint64_t counter_track_uuid,
668 uint32_t packet_sequence_id,
669 int64_t value) {
670 auto reservation_it = reserved_descriptor_tracks_.find(counter_track_uuid);
671 if (reservation_it == reserved_descriptor_tracks_.end()) {
672 PERFETTO_DLOG("Unknown counter track with uuid %" PRIu64,
673 counter_track_uuid);
674 return base::nullopt;
675 }
676
677 DescriptorTrackReservation& reservation = reservation_it->second;
678 if (!reservation.is_counter) {
679 PERFETTO_DLOG("Track with uuid %" PRIu64 " is not a counter track",
680 counter_track_uuid);
681 return base::nullopt;
682 }
683
684 if (reservation.unit_multiplier > 0)
685 value *= reservation.unit_multiplier;
686
687 if (reservation.is_incremental) {
688 if (reservation.packet_sequence_id != packet_sequence_id) {
689 PERFETTO_DLOG(
690 "Incremental counter track with uuid %" PRIu64
691 " was updated from the wrong packet sequence (expected: %" PRIu32
692 " got:%" PRIu32 ")",
693 counter_track_uuid, reservation.packet_sequence_id,
694 packet_sequence_id);
695 return base::nullopt;
696 }
697
698 reservation.latest_value += value;
699 value = reservation.latest_value;
700 }
701
702 return value;
703 }
704
OnIncrementalStateCleared(uint32_t packet_sequence_id)705 void TrackTracker::OnIncrementalStateCleared(uint32_t packet_sequence_id) {
706 // TODO(eseckler): Improve on the runtime complexity of this. At O(hundreds)
707 // of packet sequences, incremental state clearing at O(trace second), and
708 // total number of tracks in O(thousands), a linear scan through all tracks
709 // here might not be fast enough.
710 for (auto& entry : reserved_descriptor_tracks_) {
711 DescriptorTrackReservation& reservation = entry.second;
712 // Only consider incremental counter tracks for current sequence.
713 if (!reservation.is_counter || !reservation.is_incremental ||
714 reservation.packet_sequence_id != packet_sequence_id) {
715 continue;
716 }
717 // Reset their value to 0, see CounterDescriptor's |is_incremental|.
718 reservation.latest_value = 0;
719 }
720 }
721
722 } // namespace trace_processor
723 } // namespace perfetto
724