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