• 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/storage/trace_storage.h"
18 
19 #include <string.h>
20 #include <algorithm>
21 #include <limits>
22 
23 #include "perfetto/ext/base/no_destructor.h"
24 
25 namespace perfetto {
26 namespace trace_processor {
27 
28 namespace {
29 
DbTableMaybeUpdateMinMax(const TypedColumn<int64_t> & ts_col,int64_t * min_value,int64_t * max_value,const TypedColumn<int64_t> * dur_col=nullptr)30 void DbTableMaybeUpdateMinMax(const TypedColumn<int64_t>& ts_col,
31                               int64_t* min_value,
32                               int64_t* max_value,
33                               const TypedColumn<int64_t>* dur_col = nullptr) {
34   if (ts_col.row_map().empty())
35     return;
36 
37   int64_t col_min = ts_col.Min()->AsLong();
38   int64_t col_max = ts_col.Max()->AsLong();
39 
40   if (dur_col) {
41     PERFETTO_CHECK(ts_col.IsSorted());
42     PERFETTO_CHECK(dur_col->row_map().size() == ts_col.row_map().size());
43     for (uint32_t i = 0; i < dur_col->row_map().size(); i++) {
44       col_max =
45           std::max(ts_col.Get(i).AsLong() + dur_col->Get(i).AsLong(), col_max);
46     }
47   }
48 
49   *min_value = std::min(*min_value, col_min);
50   *max_value = std::max(*max_value, col_max);
51 }
52 
CreateRefTypeStringMap()53 std::vector<NullTermStringView> CreateRefTypeStringMap() {
54   std::vector<NullTermStringView> map(static_cast<size_t>(RefType::kRefMax));
55   map[static_cast<size_t>(RefType::kRefNoRef)] = NullTermStringView();
56   map[static_cast<size_t>(RefType::kRefUtid)] = "utid";
57   map[static_cast<size_t>(RefType::kRefCpuId)] = "cpu";
58   map[static_cast<size_t>(RefType::kRefGpuId)] = "gpu";
59   map[static_cast<size_t>(RefType::kRefIrq)] = "irq";
60   map[static_cast<size_t>(RefType::kRefSoftIrq)] = "softirq";
61   map[static_cast<size_t>(RefType::kRefUpid)] = "upid";
62   map[static_cast<size_t>(RefType::kRefTrack)] = "track";
63   return map;
64 }
65 
66 }  // namespace
67 
GetRefTypeStringMap()68 const std::vector<NullTermStringView>& GetRefTypeStringMap() {
69   static const base::NoDestructor<std::vector<NullTermStringView>> map(
70       CreateRefTypeStringMap());
71   return map.ref();
72 }
73 
TraceStorage(const Config &)74 TraceStorage::TraceStorage(const Config&) {
75   // Reserve utid/upid 0. These are special as embedders (e.g. Perfetto UI)
76   // exclude them by filtering them out. If the parsed trace contains ftrace
77   // data, ProcessTracker::SetPidZeroIgnoredForIdleProcess will create a mapping
78   // to these rows for tid/pid 0.
79   tables::ThreadTable::Row thread_row;
80   thread_row.tid = 0;
81   thread_table_.Insert(thread_row);
82 
83   tables::ProcessTable::Row process_row;
84   process_row.pid = 0;
85   process_table_.Insert(process_row);
86 
87   for (uint32_t i = 0; i < variadic_type_ids_.size(); ++i) {
88     variadic_type_ids_[i] = InternString(Variadic::kTypeNames[i]);
89   }
90 }
91 
~TraceStorage()92 TraceStorage::~TraceStorage() {}
93 
RecordQueryBegin(const std::string & query,int64_t time_queued,int64_t time_started)94 uint32_t TraceStorage::SqlStats::RecordQueryBegin(const std::string& query,
95                                                   int64_t time_queued,
96                                                   int64_t time_started) {
97   if (queries_.size() >= kMaxLogEntries) {
98     queries_.pop_front();
99     times_queued_.pop_front();
100     times_started_.pop_front();
101     times_first_next_.pop_front();
102     times_ended_.pop_front();
103     popped_queries_++;
104   }
105   queries_.push_back(query);
106   times_queued_.push_back(time_queued);
107   times_started_.push_back(time_started);
108   times_first_next_.push_back(0);
109   times_ended_.push_back(0);
110   return static_cast<uint32_t>(popped_queries_ + queries_.size() - 1);
111 }
112 
RecordQueryFirstNext(uint32_t row,int64_t time_first_next)113 void TraceStorage::SqlStats::RecordQueryFirstNext(uint32_t row,
114                                                   int64_t time_first_next) {
115   // This means we've popped this query off the queue of queries before it had
116   // a chance to finish. Just silently drop this number.
117   if (popped_queries_ > row)
118     return;
119   uint32_t queue_row = row - popped_queries_;
120   PERFETTO_DCHECK(queue_row < queries_.size());
121   times_first_next_[queue_row] = time_first_next;
122 }
123 
RecordQueryEnd(uint32_t row,int64_t time_ended)124 void TraceStorage::SqlStats::RecordQueryEnd(uint32_t row, int64_t time_ended) {
125   // This means we've popped this query off the queue of queries before it had
126   // a chance to finish. Just silently drop this number.
127   if (popped_queries_ > row)
128     return;
129   uint32_t queue_row = row - popped_queries_;
130   PERFETTO_DCHECK(queue_row < queries_.size());
131   times_ended_[queue_row] = time_ended;
132 }
133 
GetTraceTimestampBoundsNs() const134 std::pair<int64_t, int64_t> TraceStorage::GetTraceTimestampBoundsNs() const {
135   int64_t start_ns = std::numeric_limits<int64_t>::max();
136   int64_t end_ns = std::numeric_limits<int64_t>::min();
137 
138   DbTableMaybeUpdateMinMax(raw_table_.ts(), &start_ns, &end_ns);
139   DbTableMaybeUpdateMinMax(sched_slice_table_.ts(), &start_ns, &end_ns,
140                            &sched_slice_table_.dur());
141   DbTableMaybeUpdateMinMax(counter_table_.ts(), &start_ns, &end_ns);
142   DbTableMaybeUpdateMinMax(slice_table_.ts(), &start_ns, &end_ns,
143                            &slice_table_.dur());
144   DbTableMaybeUpdateMinMax(heap_profile_allocation_table_.ts(), &start_ns,
145                            &end_ns);
146   DbTableMaybeUpdateMinMax(instant_table_.ts(), &start_ns, &end_ns);
147   DbTableMaybeUpdateMinMax(android_log_table_.ts(), &start_ns, &end_ns);
148   DbTableMaybeUpdateMinMax(heap_graph_object_table_.graph_sample_ts(),
149                            &start_ns, &end_ns);
150 
151   if (start_ns == std::numeric_limits<int64_t>::max()) {
152     return std::make_pair(0, 0);
153   }
154   if (start_ns == end_ns) {
155     end_ns += 1;
156   }
157   return std::make_pair(start_ns, end_ns);
158 }
159 
160 }  // namespace trace_processor
161 }  // namespace perfetto
162