blob: 8f8e79a26b38b48ee5b771dcfa13dfb98915b756 [file] [log] [blame]
Lalit Maganticaed37e2018-06-01 03:03:08 +01001/*
2 * Copyright (C) 2017 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#ifndef SRC_TRACE_PROCESSOR_TRACE_STORAGE_H_
18#define SRC_TRACE_PROCESSOR_TRACE_STORAGE_H_
19
Lalit Maganti35622b72018-06-06 12:03:11 +010020#include <array>
Lalit Maganticaed37e2018-06-01 03:03:08 +010021#include <deque>
Isabelle Taylor47328cf2018-06-12 14:33:59 +010022#include <map>
Lalit Maganticaed37e2018-06-01 03:03:08 +010023#include <string>
24#include <unordered_map>
Ioannis Ilkosb8b11102019-01-29 17:56:55 +000025#include <utility>
Lalit Maganticaed37e2018-06-01 03:03:08 +010026#include <vector>
27
Lalit Maganti35622b72018-06-06 12:03:11 +010028#include "perfetto/base/logging.h"
Lalit Maganti770886a2018-11-16 17:40:21 +000029#include "perfetto/base/optional.h"
Primiano Tucci2da5d2e2018-08-10 14:23:31 +010030#include "perfetto/base/string_view.h"
Hector Dearmanc8339932018-08-10 11:22:46 +010031#include "perfetto/base/utils.h"
Lalit Magantib0b53ee2019-01-24 17:53:39 +000032#include "src/trace_processor/ftrace_utils.h"
Primiano Tucci0e38a142019-01-07 20:51:09 +000033#include "src/trace_processor/stats.h"
Lalit Maganti35622b72018-06-06 12:03:11 +010034
Lalit Maganticaed37e2018-06-01 03:03:08 +010035namespace perfetto {
36namespace trace_processor {
37
Isabelle Taylora0a22972018-08-03 12:06:12 +010038// UniquePid is an offset into |unique_processes_|. This is necessary because
39// Unix pids are reused and thus not guaranteed to be unique over a long
40// period of time.
41using UniquePid = uint32_t;
Primiano Tucci0d72a312018-08-07 14:42:45 +010042
Isabelle Taylora0a22972018-08-03 12:06:12 +010043// UniqueTid is an offset into |unique_threads_|. Necessary because tids can
44// be reused.
45using UniqueTid = uint32_t;
46
Primiano Tucci0d72a312018-08-07 14:42:45 +010047// StringId is an offset into |string_pool_|.
Lalit Maganti85ca4a82018-12-07 17:28:02 +000048using StringId = uint32_t;
Primiano Tucci0d72a312018-08-07 14:42:45 +010049
Lalit Maganti5ea9e932018-11-30 14:19:39 +000050// Identifiers for all the tables in the database.
51enum TableId : uint8_t {
52 // Intentionally don't have TableId == 0 so that RowId == 0 can refer to an
53 // invalid row id.
54 kCounters = 1,
Lalit Maganti1d915a62019-01-07 12:10:42 +000055 kRawEvents = 2,
Lalit Maganti66ed7ad2019-01-11 16:47:26 +000056 kInstants = 3,
Isabelle Taylorb9222c32019-01-31 10:58:37 +000057 kSched = 4,
Lalit Maganti5ea9e932018-11-30 14:19:39 +000058};
59
60// The top 8 bits are set to the TableId and the bottom 32 to the row of the
61// table.
Lalit Maganti85ca4a82018-12-07 17:28:02 +000062using RowId = int64_t;
Lalit Maganti5ea9e932018-11-30 14:19:39 +000063static const RowId kInvalidRowId = 0;
64
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +000065using ArgSetId = uint32_t;
66static const ArgSetId kInvalidArgSetId = 0;
67
Isabelle Taylora97c5f52018-10-23 17:36:12 +010068enum RefType {
Primiano Tucci5403e4f2018-11-27 10:07:03 +000069 kRefNoRef = 0,
70 kRefUtid = 1,
71 kRefCpuId = 2,
72 kRefIrq = 3,
73 kRefSoftIrq = 4,
74 kRefUpid = 5,
75 kRefUtidLookupUpid = 6,
76 kRefMax
Isabelle Taylora97c5f52018-10-23 17:36:12 +010077};
Isabelle Taylor14674d42018-09-07 11:33:11 +010078
Lalit Maganticaed37e2018-06-01 03:03:08 +010079// Stores a data inside a trace file in a columnar form. This makes it efficient
80// to read or search across a single field of the trace (e.g. all the thread
81// names for a given CPU).
82class TraceStorage {
83 public:
Isabelle Taylor47328cf2018-06-12 14:33:59 +010084 TraceStorage();
Isabelle Taylora0a22972018-08-03 12:06:12 +010085 TraceStorage(const TraceStorage&) = delete;
86
87 virtual ~TraceStorage();
Isabelle Taylor47328cf2018-06-12 14:33:59 +010088
Isabelle Taylora0a22972018-08-03 12:06:12 +010089 // Information about a unique process seen in a trace.
90 struct Process {
Primiano Tuccib75dcee2018-08-08 12:21:36 +010091 explicit Process(uint32_t p) : pid(p) {}
Lalit Maganti85ca4a82018-12-07 17:28:02 +000092 int64_t start_ns = 0;
93 int64_t end_ns = 0;
Isabelle Taylora0a22972018-08-03 12:06:12 +010094 StringId name_id = 0;
Primiano Tuccib75dcee2018-08-08 12:21:36 +010095 uint32_t pid = 0;
Isabelle Taylora0a22972018-08-03 12:06:12 +010096 };
97
98 // Information about a unique thread seen in a trace.
99 struct Thread {
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100100 explicit Thread(uint32_t t) : tid(t) {}
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000101 int64_t start_ns = 0;
102 int64_t end_ns = 0;
Isabelle Taylora0a22972018-08-03 12:06:12 +0100103 StringId name_id = 0;
Lalit Maganti770886a2018-11-16 17:40:21 +0000104 base::Optional<UniquePid> upid;
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100105 uint32_t tid = 0;
Isabelle Taylora0a22972018-08-03 12:06:12 +0100106 };
Isabelle Taylor3dd366c2018-06-22 16:21:41 +0100107
Lalit Maganti6e9c55e2018-11-29 12:00:39 +0000108 // Generic key value storage which can be referenced by other tables.
109 class Args {
110 public:
Lalit Maganti1d915a62019-01-07 12:10:42 +0000111 // Variadic type representing the possible values for the args table.
112 struct Variadic {
Lalit Maganti6e9c55e2018-11-29 12:00:39 +0000113 enum Type { kInt, kString, kReal };
114
Lalit Maganti1d915a62019-01-07 12:10:42 +0000115 static Variadic Integer(int64_t int_value) {
116 Variadic variadic;
117 variadic.type = Type::kInt;
118 variadic.int_value = int_value;
119 return variadic;
120 }
121
122 static Variadic String(StringId string_id) {
123 Variadic variadic;
124 variadic.type = Type::kString;
125 variadic.string_value = string_id;
126 return variadic;
127 }
128
129 static Variadic Real(double real_value) {
130 Variadic variadic;
131 variadic.type = Type::kReal;
132 variadic.real_value = real_value;
133 return variadic;
134 }
Lalit Maganti6e9c55e2018-11-29 12:00:39 +0000135
136 Type type;
137 union {
138 int64_t int_value;
139 StringId string_value;
140 double real_value;
141 };
142 };
143
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000144 struct Arg {
145 StringId flat_key = 0;
146 StringId key = 0;
147 Variadic value = Variadic::Integer(0);
148
149 // This is only used by the arg tracker and so is not part of the hash.
150 RowId row_id = 0;
151 };
152
153 struct ArgHasher {
154 uint64_t operator()(const Arg& arg) const noexcept {
155 uint64_t hash = kFnv1a64OffsetBasis;
156 hash ^= static_cast<decltype(hash)>(arg.key);
157 hash *= 1099511628211; // FNV-1a-64 prime.
158 // We don't hash arg.flat_key because it's a subsequence of arg.key.
159 switch (arg.value.type) {
160 case Variadic::Type::kInt:
161 hash ^= static_cast<uint64_t>(arg.value.int_value);
162 break;
163 case Variadic::Type::kString:
164 hash ^= static_cast<uint64_t>(arg.value.string_value);
165 break;
166 case Variadic::Type::kReal:
167 hash ^= static_cast<uint64_t>(arg.value.real_value);
168 break;
169 }
170 hash *= kFnv1a64Prime;
171 return hash;
172 }
173 };
174
175 const std::deque<ArgSetId>& set_ids() const { return set_ids_; }
Lalit Maganti5ea9e932018-11-30 14:19:39 +0000176 const std::deque<StringId>& flat_keys() const { return flat_keys_; }
177 const std::deque<StringId>& keys() const { return keys_; }
Lalit Maganti1d915a62019-01-07 12:10:42 +0000178 const std::deque<Variadic>& arg_values() const { return arg_values_; }
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000179 uint32_t args_count() const {
180 return static_cast<uint32_t>(set_ids_.size());
Lalit Maganti79472be2018-12-04 13:41:27 +0000181 }
Lalit Maganti6e9c55e2018-11-29 12:00:39 +0000182
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000183 ArgSetId AddArgSet(const std::vector<Arg>& args,
184 uint32_t begin,
185 uint32_t end) {
186 ArgSetHash hash = kFnv1a64OffsetBasis;
187 for (uint32_t i = begin; i < end; i++) {
188 hash ^= ArgHasher()(args[i]);
189 hash *= kFnv1a64Prime;
190 }
Lalit Maganti5ea9e932018-11-30 14:19:39 +0000191
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000192 auto it = arg_row_for_hash_.find(hash);
193 if (it != arg_row_for_hash_.end()) {
194 return set_ids_[it->second];
195 }
196
197 // The +1 ensures that nothing has an id == kInvalidArgSetId == 0.
198 ArgSetId id = static_cast<uint32_t>(arg_row_for_hash_.size()) + 1;
199 arg_row_for_hash_.emplace(hash, args_count());
200 for (uint32_t i = begin; i < end; i++) {
201 const auto& arg = args[i];
202 set_ids_.emplace_back(id);
203 flat_keys_.emplace_back(arg.flat_key);
204 keys_.emplace_back(arg.key);
205 arg_values_.emplace_back(arg.value);
206 }
207 return id;
Lalit Maganti6e9c55e2018-11-29 12:00:39 +0000208 }
209
Lalit Maganti5ea9e932018-11-30 14:19:39 +0000210 private:
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000211 using ArgSetHash = uint64_t;
212
213 static constexpr uint64_t kFnv1a64OffsetBasis = 0xcbf29ce484222325;
214 static constexpr uint64_t kFnv1a64Prime = 0xcbf29ce484222325;
215
216 std::deque<ArgSetId> set_ids_;
Lalit Maganti5ea9e932018-11-30 14:19:39 +0000217 std::deque<StringId> flat_keys_;
218 std::deque<StringId> keys_;
Lalit Maganti1d915a62019-01-07 12:10:42 +0000219 std::deque<Variadic> arg_values_;
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000220
221 std::unordered_map<ArgSetHash, uint32_t> arg_row_for_hash_;
Lalit Maganti6e9c55e2018-11-29 12:00:39 +0000222 };
223
Lalit Magantiff69c112018-09-24 12:07:47 +0100224 class Slices {
Lalit Maganti35622b72018-06-06 12:03:11 +0100225 public:
Lalit Magantifde29042018-10-04 13:28:52 +0100226 inline size_t AddSlice(uint32_t cpu,
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000227 int64_t start_ns,
228 int64_t duration_ns,
Lalit Magantib0b53ee2019-01-24 17:53:39 +0000229 UniqueTid utid,
230 ftrace_utils::TaskState end_state,
231 int32_t priority) {
Lalit Magantiff69c112018-09-24 12:07:47 +0100232 cpus_.emplace_back(cpu);
Lalit Maganti35622b72018-06-06 12:03:11 +0100233 start_ns_.emplace_back(start_ns);
234 durations_.emplace_back(duration_ns);
Isabelle Taylora0a22972018-08-03 12:06:12 +0100235 utids_.emplace_back(utid);
Lalit Magantib0b53ee2019-01-24 17:53:39 +0000236 end_states_.emplace_back(end_state);
237 priorities_.emplace_back(priority);
Lalit Maganti58638932019-01-31 10:48:26 +0000238
239 if (utid >= rows_for_utids_.size())
240 rows_for_utids_.resize(utid + 1);
241 rows_for_utids_[utid].emplace_back(slice_count() - 1);
Lalit Magantifde29042018-10-04 13:28:52 +0100242 return slice_count() - 1;
243 }
244
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000245 void set_duration(size_t index, int64_t duration_ns) {
Lalit Magantifde29042018-10-04 13:28:52 +0100246 durations_[index] = duration_ns;
Lalit Maganti35622b72018-06-06 12:03:11 +0100247 }
248
Lalit Magantib0b53ee2019-01-24 17:53:39 +0000249 void set_end_state(size_t index, ftrace_utils::TaskState end_state) {
250 end_states_[index] = end_state;
251 }
252
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100253 size_t slice_count() const { return start_ns_.size(); }
Lalit Maganti35622b72018-06-06 12:03:11 +0100254
Lalit Magantiff69c112018-09-24 12:07:47 +0100255 const std::deque<uint32_t>& cpus() const { return cpus_; }
256
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000257 const std::deque<int64_t>& start_ns() const { return start_ns_; }
Lalit Maganti35622b72018-06-06 12:03:11 +0100258
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000259 const std::deque<int64_t>& durations() const { return durations_; }
Lalit Maganti35622b72018-06-06 12:03:11 +0100260
Isabelle Taylor68e42192018-06-19 16:19:31 +0100261 const std::deque<UniqueTid>& utids() const { return utids_; }
262
Lalit Magantib0b53ee2019-01-24 17:53:39 +0000263 const std::deque<ftrace_utils::TaskState>& end_state() const {
264 return end_states_;
265 }
266
267 const std::deque<int32_t>& priorities() const { return priorities_; }
268
Lalit Maganti58638932019-01-31 10:48:26 +0000269 const std::deque<std::vector<uint32_t>>& rows_for_utids() const {
Lalit Magantif0f09c32019-01-18 17:29:31 +0000270 return rows_for_utids_;
271 }
272
Lalit Maganti35622b72018-06-06 12:03:11 +0100273 private:
Hector Dearman947f12a2018-09-11 16:50:36 +0100274 // Each deque below has the same number of entries (the number of slices
Lalit Maganti35622b72018-06-06 12:03:11 +0100275 // in the trace for the CPU).
Lalit Magantiff69c112018-09-24 12:07:47 +0100276 std::deque<uint32_t> cpus_;
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000277 std::deque<int64_t> start_ns_;
278 std::deque<int64_t> durations_;
Isabelle Taylor68e42192018-06-19 16:19:31 +0100279 std::deque<UniqueTid> utids_;
Lalit Magantib0b53ee2019-01-24 17:53:39 +0000280 std::deque<ftrace_utils::TaskState> end_states_;
281 std::deque<int32_t> priorities_;
Lalit Maganti58638932019-01-31 10:48:26 +0000282
283 // One row per utid.
284 std::deque<std::vector<uint32_t>> rows_for_utids_;
Primiano Tucci0d72a312018-08-07 14:42:45 +0100285 };
Isabelle Taylor68e42192018-06-19 16:19:31 +0100286
Primiano Tucci0d72a312018-08-07 14:42:45 +0100287 class NestableSlices {
288 public:
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000289 inline size_t AddSlice(int64_t start_ns,
290 int64_t duration_ns,
Lalit Maganti7b37bbf2018-11-09 15:58:54 +0000291 UniqueTid utid,
292 StringId cat,
293 StringId name,
294 uint8_t depth,
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000295 int64_t stack_id,
296 int64_t parent_stack_id) {
Primiano Tucci0d72a312018-08-07 14:42:45 +0100297 start_ns_.emplace_back(start_ns);
298 durations_.emplace_back(duration_ns);
299 utids_.emplace_back(utid);
300 cats_.emplace_back(cat);
301 names_.emplace_back(name);
302 depths_.emplace_back(depth);
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100303 stack_ids_.emplace_back(stack_id);
304 parent_stack_ids_.emplace_back(parent_stack_id);
Lalit Maganti7b37bbf2018-11-09 15:58:54 +0000305 return slice_count() - 1;
306 }
307
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000308 void set_duration(size_t index, int64_t duration_ns) {
Lalit Maganti7b37bbf2018-11-09 15:58:54 +0000309 durations_[index] = duration_ns;
310 }
311
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000312 void set_stack_id(size_t index, int64_t stack_id) {
Lalit Maganti7b37bbf2018-11-09 15:58:54 +0000313 stack_ids_[index] = stack_id;
Primiano Tucci0d72a312018-08-07 14:42:45 +0100314 }
315
316 size_t slice_count() const { return start_ns_.size(); }
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000317 const std::deque<int64_t>& start_ns() const { return start_ns_; }
318 const std::deque<int64_t>& durations() const { return durations_; }
Primiano Tucci0d72a312018-08-07 14:42:45 +0100319 const std::deque<UniqueTid>& utids() const { return utids_; }
320 const std::deque<StringId>& cats() const { return cats_; }
321 const std::deque<StringId>& names() const { return names_; }
322 const std::deque<uint8_t>& depths() const { return depths_; }
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000323 const std::deque<int64_t>& stack_ids() const { return stack_ids_; }
324 const std::deque<int64_t>& parent_stack_ids() const {
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100325 return parent_stack_ids_;
326 }
Primiano Tucci0d72a312018-08-07 14:42:45 +0100327
328 private:
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000329 std::deque<int64_t> start_ns_;
330 std::deque<int64_t> durations_;
Primiano Tucci0d72a312018-08-07 14:42:45 +0100331 std::deque<UniqueTid> utids_;
332 std::deque<StringId> cats_;
333 std::deque<StringId> names_;
334 std::deque<uint8_t> depths_;
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000335 std::deque<int64_t> stack_ids_;
336 std::deque<int64_t> parent_stack_ids_;
Lalit Maganti35622b72018-06-06 12:03:11 +0100337 };
338
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100339 class Counters {
340 public:
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000341 inline size_t AddCounter(int64_t timestamp,
Lalit Magantifde29042018-10-04 13:28:52 +0100342 StringId name_id,
343 double value,
Lalit Magantifde29042018-10-04 13:28:52 +0100344 int64_t ref,
345 RefType type) {
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100346 timestamps_.emplace_back(timestamp);
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100347 name_ids_.emplace_back(name_id);
348 values_.emplace_back(value);
349 refs_.emplace_back(ref);
350 types_.emplace_back(type);
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000351 arg_set_ids_.emplace_back(kInvalidArgSetId);
Lalit Magantifde29042018-10-04 13:28:52 +0100352 return counter_count() - 1;
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100353 }
Lalit Magantifde29042018-10-04 13:28:52 +0100354
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000355 void set_arg_set_id(uint32_t row, ArgSetId id) { arg_set_ids_[row] = id; }
356
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100357 size_t counter_count() const { return timestamps_.size(); }
358
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000359 const std::deque<int64_t>& timestamps() const { return timestamps_; }
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100360
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100361 const std::deque<StringId>& name_ids() const { return name_ids_; }
362
363 const std::deque<double>& values() const { return values_; }
364
365 const std::deque<int64_t>& refs() const { return refs_; }
366
367 const std::deque<RefType>& types() const { return types_; }
368
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000369 const std::deque<ArgSetId>& arg_set_ids() const { return arg_set_ids_; }
370
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100371 private:
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000372 std::deque<int64_t> timestamps_;
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100373 std::deque<StringId> name_ids_;
374 std::deque<double> values_;
375 std::deque<int64_t> refs_;
376 std::deque<RefType> types_;
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000377 std::deque<ArgSetId> arg_set_ids_;
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100378 };
379
Primiano Tucci5cb84f82018-10-31 21:46:36 -0700380 class SqlStats {
381 public:
382 static constexpr size_t kMaxLogEntries = 100;
383 void RecordQueryBegin(const std::string& query,
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000384 int64_t time_queued,
385 int64_t time_started);
386 void RecordQueryEnd(int64_t time_ended);
Primiano Tucci5cb84f82018-10-31 21:46:36 -0700387 size_t size() const { return queries_.size(); }
388 const std::deque<std::string>& queries() const { return queries_; }
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000389 const std::deque<int64_t>& times_queued() const { return times_queued_; }
390 const std::deque<int64_t>& times_started() const { return times_started_; }
391 const std::deque<int64_t>& times_ended() const { return times_ended_; }
Primiano Tucci5cb84f82018-10-31 21:46:36 -0700392
393 private:
394 std::deque<std::string> queries_;
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000395 std::deque<int64_t> times_queued_;
396 std::deque<int64_t> times_started_;
397 std::deque<int64_t> times_ended_;
Primiano Tucci5cb84f82018-10-31 21:46:36 -0700398 };
399
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000400 class Instants {
401 public:
Lalit Maganti66ed7ad2019-01-11 16:47:26 +0000402 inline uint32_t AddInstantEvent(int64_t timestamp,
403 StringId name_id,
404 double value,
405 int64_t ref,
406 RefType type) {
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000407 timestamps_.emplace_back(timestamp);
408 name_ids_.emplace_back(name_id);
409 values_.emplace_back(value);
410 refs_.emplace_back(ref);
411 types_.emplace_back(type);
Lalit Maganti1c21d172019-02-07 10:48:24 +0000412 arg_set_ids_.emplace_back(kInvalidArgSetId);
Lalit Maganti66ed7ad2019-01-11 16:47:26 +0000413 return static_cast<uint32_t>(instant_count() - 1);
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000414 }
415
Lalit Maganti1c21d172019-02-07 10:48:24 +0000416 void set_arg_set_id(uint32_t row, ArgSetId id) { arg_set_ids_[row] = id; }
417
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000418 size_t instant_count() const { return timestamps_.size(); }
419
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000420 const std::deque<int64_t>& timestamps() const { return timestamps_; }
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000421
422 const std::deque<StringId>& name_ids() const { return name_ids_; }
423
424 const std::deque<double>& values() const { return values_; }
425
426 const std::deque<int64_t>& refs() const { return refs_; }
427
428 const std::deque<RefType>& types() const { return types_; }
429
Lalit Maganti1c21d172019-02-07 10:48:24 +0000430 const std::deque<ArgSetId>& arg_set_ids() const { return arg_set_ids_; }
431
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000432 private:
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000433 std::deque<int64_t> timestamps_;
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000434 std::deque<StringId> name_ids_;
435 std::deque<double> values_;
436 std::deque<int64_t> refs_;
437 std::deque<RefType> types_;
Lalit Maganti1c21d172019-02-07 10:48:24 +0000438 std::deque<ArgSetId> arg_set_ids_;
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000439 };
440
Lalit Maganti1d915a62019-01-07 12:10:42 +0000441 class RawEvents {
442 public:
443 inline RowId AddRawEvent(int64_t timestamp,
444 StringId name_id,
Lalit Magantie87cc812019-01-10 15:20:06 +0000445 uint32_t cpu,
Lalit Maganti1d915a62019-01-07 12:10:42 +0000446 UniqueTid utid) {
447 timestamps_.emplace_back(timestamp);
448 name_ids_.emplace_back(name_id);
Lalit Magantie87cc812019-01-10 15:20:06 +0000449 cpus_.emplace_back(cpu);
Lalit Maganti1d915a62019-01-07 12:10:42 +0000450 utids_.emplace_back(utid);
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000451 arg_set_ids_.emplace_back(kInvalidArgSetId);
Lalit Maganti1d915a62019-01-07 12:10:42 +0000452 return CreateRowId(TableId::kRawEvents,
453 static_cast<uint32_t>(raw_event_count() - 1));
454 }
455
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000456 void set_arg_set_id(uint32_t row, ArgSetId id) { arg_set_ids_[row] = id; }
457
Lalit Maganti1d915a62019-01-07 12:10:42 +0000458 size_t raw_event_count() const { return timestamps_.size(); }
459
460 const std::deque<int64_t>& timestamps() const { return timestamps_; }
461
462 const std::deque<StringId>& name_ids() const { return name_ids_; }
463
Lalit Magantie87cc812019-01-10 15:20:06 +0000464 const std::deque<uint32_t>& cpus() const { return cpus_; }
465
Lalit Maganti1d915a62019-01-07 12:10:42 +0000466 const std::deque<UniqueTid>& utids() const { return utids_; }
467
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000468 const std::deque<ArgSetId>& arg_set_ids() const { return arg_set_ids_; }
469
Lalit Maganti1d915a62019-01-07 12:10:42 +0000470 private:
471 std::deque<int64_t> timestamps_;
472 std::deque<StringId> name_ids_;
Lalit Magantie87cc812019-01-10 15:20:06 +0000473 std::deque<uint32_t> cpus_;
Lalit Maganti1d915a62019-01-07 12:10:42 +0000474 std::deque<UniqueTid> utids_;
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000475 std::deque<ArgSetId> arg_set_ids_;
Lalit Maganti1d915a62019-01-07 12:10:42 +0000476 };
477
Primiano Tucci2c761ef2019-01-07 20:20:46 +0000478 class AndroidLogs {
479 public:
480 inline size_t AddLogEvent(int64_t timestamp,
481 UniqueTid utid,
482 uint8_t prio,
483 StringId tag_id,
484 StringId msg_id) {
485 timestamps_.emplace_back(timestamp);
486 utids_.emplace_back(utid);
487 prios_.emplace_back(prio);
488 tag_ids_.emplace_back(tag_id);
489 msg_ids_.emplace_back(msg_id);
490 return size() - 1;
491 }
492
493 size_t size() const { return timestamps_.size(); }
494
495 const std::deque<int64_t>& timestamps() const { return timestamps_; }
496 const std::deque<UniqueTid>& utids() const { return utids_; }
497 const std::deque<uint8_t>& prios() const { return prios_; }
498 const std::deque<StringId>& tag_ids() const { return tag_ids_; }
499 const std::deque<StringId>& msg_ids() const { return msg_ids_; }
500
501 private:
502 std::deque<int64_t> timestamps_;
503 std::deque<UniqueTid> utids_;
504 std::deque<uint8_t> prios_;
505 std::deque<StringId> tag_ids_;
506 std::deque<StringId> msg_ids_;
507 };
508
Primiano Tucci0e38a142019-01-07 20:51:09 +0000509 struct Stats {
510 using IndexMap = std::map<int, int64_t>;
511 int64_t value = 0;
512 IndexMap indexed_values;
513 };
514 using StatsMap = std::array<Stats, stats::kNumKeys>;
515
Isabelle Taylora0a22972018-08-03 12:06:12 +0100516 void ResetStorage();
Lalit Maganti35622b72018-06-06 12:03:11 +0100517
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100518 UniqueTid AddEmptyThread(uint32_t tid) {
519 unique_threads_.emplace_back(tid);
Isabelle Taylora0a22972018-08-03 12:06:12 +0100520 return static_cast<UniqueTid>(unique_threads_.size() - 1);
521 }
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100522
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100523 UniquePid AddEmptyProcess(uint32_t pid) {
524 unique_processes_.emplace_back(pid);
Isabelle Taylora0a22972018-08-03 12:06:12 +0100525 return static_cast<UniquePid>(unique_processes_.size() - 1);
526 }
Isabelle Taylor3dd366c2018-06-22 16:21:41 +0100527
Isabelle Taylora0a22972018-08-03 12:06:12 +0100528 // Return an unqiue identifier for the contents of each string.
529 // The string is copied internally and can be destroyed after this called.
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100530 // Virtual for testing.
531 virtual StringId InternString(base::StringView);
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100532
Isabelle Taylora0a22972018-08-03 12:06:12 +0100533 Process* GetMutableProcess(UniquePid upid) {
Lalit Maganti676658b2018-11-20 18:24:31 +0000534 PERFETTO_DCHECK(upid < unique_processes_.size());
Isabelle Taylora0a22972018-08-03 12:06:12 +0100535 return &unique_processes_[upid];
536 }
Isabelle Taylor3dd366c2018-06-22 16:21:41 +0100537
Isabelle Taylora0a22972018-08-03 12:06:12 +0100538 Thread* GetMutableThread(UniqueTid utid) {
Florian Mayera1aaec22018-09-19 17:02:26 +0100539 PERFETTO_DCHECK(utid < unique_threads_.size());
Isabelle Taylora0a22972018-08-03 12:06:12 +0100540 return &unique_threads_[utid];
541 }
Isabelle Taylor3dd366c2018-06-22 16:21:41 +0100542
Primiano Tucci0e38a142019-01-07 20:51:09 +0000543 // Example usage: SetStats(stats::android_log_num_failed, 42);
544 void SetStats(size_t key, int64_t value) {
545 PERFETTO_DCHECK(key < stats::kNumKeys);
546 PERFETTO_DCHECK(stats::kTypes[key] == stats::kSingle);
547 stats_[key].value = value;
548 }
549
550 // Example usage: IncrementStats(stats::android_log_num_failed, -1);
551 void IncrementStats(size_t key, int64_t increment = 1) {
552 PERFETTO_DCHECK(key < stats::kNumKeys);
553 PERFETTO_DCHECK(stats::kTypes[key] == stats::kSingle);
554 stats_[key].value += increment;
555 }
556
557 // Example usage: SetIndexedStats(stats::cpu_failure, 1, 42);
558 void SetIndexedStats(size_t key, int index, int64_t value) {
559 PERFETTO_DCHECK(key < stats::kNumKeys);
560 PERFETTO_DCHECK(stats::kTypes[key] == stats::kIndexed);
561 stats_[key].indexed_values[index] = value;
562 }
563
Lalit Maganticaed37e2018-06-01 03:03:08 +0100564 // Reading methods.
Isabelle Taylora0a22972018-08-03 12:06:12 +0100565 const std::string& GetString(StringId id) const {
566 PERFETTO_DCHECK(id < string_pool_.size());
567 return string_pool_[id];
568 }
569
Lalit Magantie9d40532018-06-29 13:15:06 +0100570 const Process& GetProcess(UniquePid upid) const {
Lalit Maganti676658b2018-11-20 18:24:31 +0000571 PERFETTO_DCHECK(upid < unique_processes_.size());
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100572 return unique_processes_[upid];
573 }
574
Lalit Magantie9d40532018-06-29 13:15:06 +0100575 const Thread& GetThread(UniqueTid utid) const {
Lalit Maganti1f64cfa2018-09-18 13:20:02 +0100576 // Allow utid == 0 for idle thread retrieval.
Florian Mayera1aaec22018-09-19 17:02:26 +0100577 PERFETTO_DCHECK(utid < unique_threads_.size());
Isabelle Taylor68e42192018-06-19 16:19:31 +0100578 return unique_threads_[utid];
579 }
580
Lalit Maganti5ea9e932018-11-30 14:19:39 +0000581 static RowId CreateRowId(TableId table, uint32_t row) {
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000582 return (static_cast<RowId>(table) << kRowIdTableShift) | row;
Lalit Maganti5ea9e932018-11-30 14:19:39 +0000583 }
584
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000585 static std::pair<int8_t /*table*/, uint32_t /*row*/> ParseRowId(RowId rowid) {
586 auto id = static_cast<uint64_t>(rowid);
587 auto table_id = static_cast<uint8_t>(id >> kRowIdTableShift);
588 auto row = static_cast<uint32_t>(id & ((1ull << kRowIdTableShift) - 1));
589 return std::make_pair(table_id, row);
590 }
591
Lalit Magantiff69c112018-09-24 12:07:47 +0100592 const Slices& slices() const { return slices_; }
Lalit Magantifde29042018-10-04 13:28:52 +0100593 Slices* mutable_slices() { return &slices_; }
594
Primiano Tucci0d72a312018-08-07 14:42:45 +0100595 const NestableSlices& nestable_slices() const { return nestable_slices_; }
596 NestableSlices* mutable_nestable_slices() { return &nestable_slices_; }
597
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100598 const Counters& counters() const { return counters_; }
599 Counters* mutable_counters() { return &counters_; }
Isabelle Taylor14674d42018-09-07 11:33:11 +0100600
Primiano Tucci5cb84f82018-10-31 21:46:36 -0700601 const SqlStats& sql_stats() const { return sql_stats_; }
602 SqlStats* mutable_sql_stats() { return &sql_stats_; }
603
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000604 const Instants& instants() const { return instants_; }
605 Instants* mutable_instants() { return &instants_; }
606
Primiano Tucci2c761ef2019-01-07 20:20:46 +0000607 const AndroidLogs& android_logs() const { return android_log_; }
608 AndroidLogs* mutable_android_log() { return &android_log_; }
609
Primiano Tucci0e38a142019-01-07 20:51:09 +0000610 const StatsMap& stats() const { return stats_; }
Lalit Maganti05e8c132018-11-09 18:16:12 +0000611
Lalit Maganti6e9c55e2018-11-29 12:00:39 +0000612 const Args& args() const { return args_; }
613 Args* mutable_args() { return &args_; }
614
Lalit Maganti1d915a62019-01-07 12:10:42 +0000615 const RawEvents& raw_events() const { return raw_events_; }
616 RawEvents* mutable_raw_events() { return &raw_events_; }
617
Lalit Magantiacda68b2018-10-29 15:23:25 +0000618 const std::deque<std::string>& string_pool() const { return string_pool_; }
619
Isabelle Taylore7003fb2018-07-17 11:39:01 +0100620 // |unique_processes_| always contains at least 1 element becuase the 0th ID
621 // is reserved to indicate an invalid process.
Lalit Maganti9c6395b2019-01-17 00:25:09 +0000622 size_t process_count() const { return unique_processes_.size(); }
Primiano Tucci0d72a312018-08-07 14:42:45 +0100623
Isabelle Taylore7003fb2018-07-17 11:39:01 +0100624 // |unique_threads_| always contains at least 1 element becuase the 0th ID
625 // is reserved to indicate an invalid thread.
Lalit Maganti9c6395b2019-01-17 00:25:09 +0000626 size_t thread_count() const { return unique_threads_.size(); }
Isabelle Taylore7003fb2018-07-17 11:39:01 +0100627
Hector Dearman12323362018-08-09 16:09:28 +0100628 // Number of interned strings in the pool. Includes the empty string w/ ID=0.
629 size_t string_count() const { return string_pool_.size(); }
630
Ioannis Ilkosb8b11102019-01-29 17:56:55 +0000631 // Start / end ts (in nanoseconds) across the parsed trace events.
632 // Returns (0, 0) if the trace is empty.
633 std::pair<int64_t, int64_t> GetTraceTimestampBoundsNs() const;
634
Lalit Maganticaed37e2018-06-01 03:03:08 +0100635 private:
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000636 static constexpr uint8_t kRowIdTableShift = 32;
Isabelle Taylora0a22972018-08-03 12:06:12 +0100637
Primiano Tucci2da5d2e2018-08-10 14:23:31 +0100638 using StringHash = uint64_t;
Lalit Maganticaed37e2018-06-01 03:03:08 +0100639
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000640 TraceStorage& operator=(const TraceStorage&) = default;
641
Lalit Maganti05e8c132018-11-09 18:16:12 +0000642 // Stats about parsing the trace.
Primiano Tucci0e38a142019-01-07 20:51:09 +0000643 StatsMap stats_{};
Lalit Maganti35622b72018-06-06 12:03:11 +0100644
Lalit Maganticaed37e2018-06-01 03:03:08 +0100645 // One entry for each CPU in the trace.
Lalit Magantiff69c112018-09-24 12:07:47 +0100646 Slices slices_;
Lalit Maganticaed37e2018-06-01 03:03:08 +0100647
Lalit Maganti6e9c55e2018-11-29 12:00:39 +0000648 // Args for all other tables.
649 Args args_;
650
Lalit Maganticaed37e2018-06-01 03:03:08 +0100651 // One entry for each unique string in the trace.
Lalit Maganti35622b72018-06-06 12:03:11 +0100652 std::deque<std::string> string_pool_;
Lalit Maganticaed37e2018-06-01 03:03:08 +0100653
654 // One entry for each unique string in the trace.
Lalit Maganti35622b72018-06-06 12:03:11 +0100655 std::unordered_map<StringHash, StringId> string_index_;
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100656
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100657 // One entry for each UniquePid, with UniquePid as the index.
Isabelle Taylor3dd366c2018-06-22 16:21:41 +0100658 std::deque<Process> unique_processes_;
Isabelle Taylor68e42192018-06-19 16:19:31 +0100659
Isabelle Taylor68e42192018-06-19 16:19:31 +0100660 // One entry for each UniqueTid, with UniqueTid as the index.
Isabelle Taylor3dd366c2018-06-22 16:21:41 +0100661 std::deque<Thread> unique_threads_;
Primiano Tucci0d72a312018-08-07 14:42:45 +0100662
663 // Slices coming from userspace events (e.g. Chromium TRACE_EVENT macros).
664 NestableSlices nestable_slices_;
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100665
666 // Counter events from the trace. This includes CPU frequency events as well
667 // systrace trace_marker counter events.
668 Counters counters_;
Primiano Tucci5cb84f82018-10-31 21:46:36 -0700669
670 SqlStats sql_stats_;
Lalit Maganti6e9c55e2018-11-29 12:00:39 +0000671
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000672 // These are instantaneous events in the trace. They have no duration
673 // and do not have a value that make sense to track over time.
674 // e.g. signal events
675 Instants instants_;
Lalit Maganti1d915a62019-01-07 12:10:42 +0000676
677 // Raw events are every ftrace event in the trace. The raw event includes
678 // the timestamp and the pid. The args for the raw event will be in the
679 // args table. This table can be used to generate a text version of the
680 // trace.
681 RawEvents raw_events_;
Primiano Tucci2c761ef2019-01-07 20:20:46 +0000682 AndroidLogs android_log_;
Lalit Maganticaed37e2018-06-01 03:03:08 +0100683};
684
685} // namespace trace_processor
686} // namespace perfetto
687
688#endif // SRC_TRACE_PROCESSOR_TRACE_STORAGE_H_