• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <optional>
21 
22 #include "perfetto/base/logging.h"
23 #include "perfetto/ext/base/utils.h"
24 #include "src/trace_processor/importers/common/args_tracker.h"
25 #include "src/trace_processor/importers/common/process_tracker.h"
26 #include "src/trace_processor/importers/common/track_tracker.h"
27 #include "src/trace_processor/storage/stats.h"
28 #include "src/trace_processor/types/trace_processor_context.h"
29 #include "src/trace_processor/types/variadic.h"
30 
31 namespace perfetto {
32 namespace trace_processor {
33 
EventTracker(TraceProcessorContext * context)34 EventTracker::EventTracker(TraceProcessorContext* context)
35     : context_(context) {}
36 
37 EventTracker::~EventTracker() = default;
38 
PushProcessCounterForThread(int64_t timestamp,double value,StringId name_id,UniqueTid utid)39 std::optional<CounterId> EventTracker::PushProcessCounterForThread(
40     int64_t timestamp,
41     double value,
42     StringId name_id,
43     UniqueTid utid) {
44   auto opt_id = PushCounter(timestamp, value, kInvalidTrackId);
45   if (opt_id) {
46     PendingUpidResolutionCounter pending;
47     pending.row = *context_->storage->counter_table().id().IndexOf(*opt_id);
48     pending.utid = utid;
49     pending.name_id = name_id;
50     pending_upid_resolution_counter_.emplace_back(pending);
51   }
52   return opt_id;
53 }
54 
PushCounter(int64_t timestamp,double value,TrackId track_id)55 std::optional<CounterId> EventTracker::PushCounter(int64_t timestamp,
56                                                    double value,
57                                                    TrackId track_id) {
58   if (timestamp < max_timestamp_) {
59     PERFETTO_DLOG(
60         "counter event (ts: %" PRId64 ") out of order by %.4f ms, skipping",
61         timestamp, static_cast<double>(max_timestamp_ - timestamp) / 1e6);
62     context_->storage->IncrementStats(stats::counter_events_out_of_order);
63     return std::nullopt;
64   }
65   max_timestamp_ = timestamp;
66 
67   auto* counter_values = context_->storage->mutable_counter_table();
68   return counter_values->Insert({timestamp, track_id, value}).id;
69 }
70 
PushCounter(int64_t timestamp,double value,TrackId track_id,SetArgsCallback args_callback)71 std::optional<CounterId> EventTracker::PushCounter(
72     int64_t timestamp,
73     double value,
74     TrackId track_id,
75     SetArgsCallback args_callback) {
76   auto maybe_counter_id = PushCounter(timestamp, value, track_id);
77   if (maybe_counter_id) {
78     auto inserter = context_->args_tracker->AddArgsTo(*maybe_counter_id);
79     args_callback(&inserter);
80   }
81   return maybe_counter_id;
82 }
83 
FlushPendingEvents()84 void EventTracker::FlushPendingEvents() {
85   const auto& thread_table = context_->storage->thread_table();
86   for (const auto& pending_counter : pending_upid_resolution_counter_) {
87     UniqueTid utid = pending_counter.utid;
88     std::optional<UniquePid> upid = thread_table.upid()[utid];
89 
90     TrackId track_id = kInvalidTrackId;
91     if (upid.has_value()) {
92       track_id = context_->track_tracker->InternProcessCounterTrack(
93           pending_counter.name_id, *upid);
94     } else {
95       // If we still don't know which process this thread belongs to, fall back
96       // onto creating a thread counter track. It's too late to drop data
97       // because the counter values have already been inserted.
98       track_id = context_->track_tracker->InternThreadCounterTrack(
99           pending_counter.name_id, utid);
100     }
101     context_->storage->mutable_counter_table()->mutable_track_id()->Set(
102         pending_counter.row, track_id);
103   }
104   pending_upid_resolution_counter_.clear();
105 }
106 
107 }  // namespace trace_processor
108 }  // namespace perfetto
109