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(
59 "counter event (ts: %" PRId64 ") out of order by %.4f ms, skipping",
60 timestamp, static_cast<double>(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
PushCounter(int64_t timestamp,double value,TrackId track_id,SetArgsCallback args_callback)70 base::Optional<CounterId> EventTracker::PushCounter(
71 int64_t timestamp,
72 double value,
73 TrackId track_id,
74 SetArgsCallback args_callback) {
75 auto maybe_counter_id = PushCounter(timestamp, value, track_id);
76 if (maybe_counter_id) {
77 auto inserter = context_->args_tracker->AddArgsTo(*maybe_counter_id);
78 args_callback(&inserter);
79 }
80 return maybe_counter_id;
81 }
82
PushInstant(int64_t timestamp,StringId name_id,int64_t ref,RefType ref_type,bool resolve_utid_to_upid)83 InstantId EventTracker::PushInstant(int64_t timestamp,
84 StringId name_id,
85 int64_t ref,
86 RefType ref_type,
87 bool resolve_utid_to_upid) {
88 auto* instants = context_->storage->mutable_instant_table();
89 InstantId id;
90 if (resolve_utid_to_upid) {
91 auto ref_type_id = context_->storage->InternString(
92 GetRefTypeStringMap()[static_cast<size_t>(RefType::kRefUpid)]);
93 auto id_and_row = instants->Insert({timestamp, name_id, 0, ref_type_id});
94 id = id_and_row.id;
95 PendingUpidResolutionInstant pending;
96 pending.row = id_and_row.row;
97 pending.utid = static_cast<UniqueTid>(ref);
98 pending_upid_resolution_instant_.emplace_back(pending);
99 } else {
100 auto ref_type_id = context_->storage->InternString(
101 GetRefTypeStringMap()[static_cast<size_t>(ref_type)]);
102 id = instants->Insert({timestamp, name_id, ref, ref_type_id}).id;
103 }
104 return id;
105 }
106
FlushPendingEvents()107 void EventTracker::FlushPendingEvents() {
108 const auto& thread_table = context_->storage->thread_table();
109 for (const auto& pending_counter : pending_upid_resolution_counter_) {
110 // TODO(lalitm): having upid == 0 is probably not the correct approach here
111 // but it's unclear what may be better.
112 UniqueTid utid = pending_counter.utid;
113 UniquePid upid = thread_table.upid()[utid].value_or(0);
114 TrackId id = context_->track_tracker->InternProcessCounterTrack(
115 pending_counter.name_id, upid);
116 context_->storage->mutable_counter_table()->mutable_track_id()->Set(
117 pending_counter.row, id);
118 }
119
120 for (const auto& pending_instant : pending_upid_resolution_instant_) {
121 UniqueTid utid = pending_instant.utid;
122 // TODO(lalitm): having upid == 0 is probably not the correct approach here
123 // but it's unclear what may be better.
124 UniquePid upid = thread_table.upid()[utid].value_or(0);
125 context_->storage->mutable_instant_table()->mutable_ref()->Set(
126 pending_instant.row, upid);
127 }
128
129 pending_upid_resolution_counter_.clear();
130 pending_upid_resolution_instant_.clear();
131 }
132
133 } // namespace trace_processor
134 } // namespace perfetto
135