1 /*
2 * Copyright (C) 2018 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/event_tracker.h"
18
19 #include <math.h>
20
21 #include "perfetto/base/logging.h"
22 #include "perfetto/ext/base/utils.h"
23 #include "src/trace_processor/importers/common/args_tracker.h"
24 #include "src/trace_processor/importers/common/process_tracker.h"
25 #include "src/trace_processor/importers/common/track_tracker.h"
26 #include "src/trace_processor/storage/stats.h"
27 #include "src/trace_processor/types/trace_processor_context.h"
28 #include "src/trace_processor/types/variadic.h"
29
30 namespace perfetto {
31 namespace trace_processor {
32
EventTracker(TraceProcessorContext * context)33 EventTracker::EventTracker(TraceProcessorContext* context)
34 : context_(context) {}
35
36 EventTracker::~EventTracker() = default;
37
PushProcessCounterForThread(int64_t timestamp,double value,StringId name_id,UniqueTid utid)38 base::Optional<CounterId> EventTracker::PushProcessCounterForThread(
39 int64_t timestamp,
40 double value,
41 StringId name_id,
42 UniqueTid utid) {
43 auto opt_id = PushCounter(timestamp, value, kInvalidTrackId);
44 if (opt_id) {
45 PendingUpidResolutionCounter pending;
46 pending.row = *context_->storage->counter_table().id().IndexOf(*opt_id);
47 pending.utid = utid;
48 pending.name_id = name_id;
49 pending_upid_resolution_counter_.emplace_back(pending);
50 }
51 return opt_id;
52 }
53
PushCounter(int64_t timestamp,double value,TrackId track_id)54 base::Optional<CounterId> EventTracker::PushCounter(int64_t timestamp,
55 double value,
56 TrackId track_id) {
57 if (timestamp < max_timestamp_) {
58 PERFETTO_DLOG("counter event (ts: %" PRId64
59 ") out of order by %.4f ms, skipping",
60 timestamp, (max_timestamp_ - timestamp) / 1e6);
61 context_->storage->IncrementStats(stats::counter_events_out_of_order);
62 return base::nullopt;
63 }
64 max_timestamp_ = timestamp;
65
66 auto* counter_values = context_->storage->mutable_counter_table();
67 return counter_values->Insert({timestamp, track_id, value}).id;
68 }
69
PushInstant(int64_t timestamp,StringId name_id,int64_t ref,RefType ref_type,bool resolve_utid_to_upid)70 InstantId EventTracker::PushInstant(int64_t timestamp,
71 StringId name_id,
72 int64_t ref,
73 RefType ref_type,
74 bool resolve_utid_to_upid) {
75 auto* instants = context_->storage->mutable_instant_table();
76 InstantId id;
77 if (resolve_utid_to_upid) {
78 auto ref_type_id = context_->storage->InternString(
79 GetRefTypeStringMap()[static_cast<size_t>(RefType::kRefUpid)]);
80 auto id_and_row = instants->Insert({timestamp, name_id, 0, ref_type_id});
81 id = id_and_row.id;
82 PendingUpidResolutionInstant pending;
83 pending.row = id_and_row.row;
84 pending.utid = static_cast<UniqueTid>(ref);
85 pending_upid_resolution_instant_.emplace_back(pending);
86 } else {
87 auto ref_type_id = context_->storage->InternString(
88 GetRefTypeStringMap()[static_cast<size_t>(ref_type)]);
89 id = instants->Insert({timestamp, name_id, ref, ref_type_id}).id;
90 }
91 return id;
92 }
93
FlushPendingEvents()94 void EventTracker::FlushPendingEvents() {
95 const auto& thread_table = context_->storage->thread_table();
96 for (const auto& pending_counter : pending_upid_resolution_counter_) {
97 // TODO(lalitm): having upid == 0 is probably not the correct approach here
98 // but it's unclear what may be better.
99 UniqueTid utid = pending_counter.utid;
100 UniquePid upid = thread_table.upid()[utid].value_or(0);
101 TrackId id = context_->track_tracker->InternProcessCounterTrack(
102 pending_counter.name_id, upid);
103 context_->storage->mutable_counter_table()->mutable_track_id()->Set(
104 pending_counter.row, id);
105 }
106
107 for (const auto& pending_instant : pending_upid_resolution_instant_) {
108 UniqueTid utid = pending_instant.utid;
109 // TODO(lalitm): having upid == 0 is probably not the correct approach here
110 // but it's unclear what may be better.
111 UniquePid upid = thread_table.upid()[utid].value_or(0);
112 context_->storage->mutable_instant_table()->mutable_ref()->Set(
113 pending_instant.row, upid);
114 }
115
116 pending_upid_resolution_counter_.clear();
117 pending_upid_resolution_instant_.clear();
118 }
119
120 } // namespace trace_processor
121 } // namespace perfetto
122