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 for (uint32_t i = 0; i < variadic_type_ids_.size(); ++i) {
76 variadic_type_ids_[i] = InternString(Variadic::kTypeNames[i]);
77 }
78 }
79
~TraceStorage()80 TraceStorage::~TraceStorage() {}
81
RecordQueryBegin(const std::string & query,int64_t time_queued,int64_t time_started)82 uint32_t TraceStorage::SqlStats::RecordQueryBegin(const std::string& query,
83 int64_t time_queued,
84 int64_t time_started) {
85 if (queries_.size() >= kMaxLogEntries) {
86 queries_.pop_front();
87 times_queued_.pop_front();
88 times_started_.pop_front();
89 times_first_next_.pop_front();
90 times_ended_.pop_front();
91 popped_queries_++;
92 }
93 queries_.push_back(query);
94 times_queued_.push_back(time_queued);
95 times_started_.push_back(time_started);
96 times_first_next_.push_back(0);
97 times_ended_.push_back(0);
98 return static_cast<uint32_t>(popped_queries_ + queries_.size() - 1);
99 }
100
RecordQueryFirstNext(uint32_t row,int64_t time_first_next)101 void TraceStorage::SqlStats::RecordQueryFirstNext(uint32_t row,
102 int64_t time_first_next) {
103 // This means we've popped this query off the queue of queries before it had
104 // a chance to finish. Just silently drop this number.
105 if (popped_queries_ > row)
106 return;
107 uint32_t queue_row = row - popped_queries_;
108 PERFETTO_DCHECK(queue_row < queries_.size());
109 times_first_next_[queue_row] = time_first_next;
110 }
111
RecordQueryEnd(uint32_t row,int64_t time_ended)112 void TraceStorage::SqlStats::RecordQueryEnd(uint32_t row, int64_t time_ended) {
113 // This means we've popped this query off the queue of queries before it had
114 // a chance to finish. Just silently drop this number.
115 if (popped_queries_ > row)
116 return;
117 uint32_t queue_row = row - popped_queries_;
118 PERFETTO_DCHECK(queue_row < queries_.size());
119 times_ended_[queue_row] = time_ended;
120 }
121
GetTraceTimestampBoundsNs() const122 std::pair<int64_t, int64_t> TraceStorage::GetTraceTimestampBoundsNs() const {
123 int64_t start_ns = std::numeric_limits<int64_t>::max();
124 int64_t end_ns = std::numeric_limits<int64_t>::min();
125
126 DbTableMaybeUpdateMinMax(raw_table_.ts(), &start_ns, &end_ns);
127 DbTableMaybeUpdateMinMax(sched_slice_table_.ts(), &start_ns, &end_ns,
128 &sched_slice_table_.dur());
129 DbTableMaybeUpdateMinMax(counter_table_.ts(), &start_ns, &end_ns);
130 DbTableMaybeUpdateMinMax(slice_table_.ts(), &start_ns, &end_ns,
131 &slice_table_.dur());
132 DbTableMaybeUpdateMinMax(heap_profile_allocation_table_.ts(), &start_ns,
133 &end_ns);
134 DbTableMaybeUpdateMinMax(instant_table_.ts(), &start_ns, &end_ns);
135 DbTableMaybeUpdateMinMax(android_log_table_.ts(), &start_ns, &end_ns);
136 DbTableMaybeUpdateMinMax(heap_graph_object_table_.graph_sample_ts(),
137 &start_ns, &end_ns);
138 DbTableMaybeUpdateMinMax(perf_sample_table_.ts(), &start_ns, &end_ns);
139
140 if (start_ns == std::numeric_limits<int64_t>::max()) {
141 return std::make_pair(0, 0);
142 }
143 if (start_ns == end_ns) {
144 end_ns += 1;
145 }
146 return std::make_pair(start_ns, end_ns);
147 }
148
149 } // namespace trace_processor
150 } // namespace perfetto
151