blob: 71af139e5e276b1846e4a02a1fe549c9b3f2f9ce [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"
Primiano Tucci2c5488f2019-06-01 03:27:28 +010029#include "perfetto/ext/base/hash.h"
30#include "perfetto/ext/base/optional.h"
31#include "perfetto/ext/base/string_view.h"
32#include "perfetto/ext/base/time.h"
33#include "perfetto/ext/base/utils.h"
Lalit Magantib0b53ee2019-01-24 17:53:39 +000034#include "src/trace_processor/ftrace_utils.h"
Mikhail Khokhlove466c002019-05-23 13:33:33 +010035#include "src/trace_processor/metadata.h"
Primiano Tucci0e38a142019-01-07 20:51:09 +000036#include "src/trace_processor/stats.h"
Lalit Maganti5c454312019-04-08 12:11:17 +010037#include "src/trace_processor/string_pool.h"
Mikhail Khokhlov85a0dd02019-05-17 14:22:28 +010038#include "src/trace_processor/variadic.h"
Lalit Maganti35622b72018-06-06 12:03:11 +010039
Lalit Maganticaed37e2018-06-01 03:03:08 +010040namespace perfetto {
41namespace trace_processor {
42
Isabelle Taylora0a22972018-08-03 12:06:12 +010043// UniquePid is an offset into |unique_processes_|. This is necessary because
44// Unix pids are reused and thus not guaranteed to be unique over a long
45// period of time.
46using UniquePid = uint32_t;
Primiano Tucci0d72a312018-08-07 14:42:45 +010047
Isabelle Taylora0a22972018-08-03 12:06:12 +010048// UniqueTid is an offset into |unique_threads_|. Necessary because tids can
49// be reused.
50using UniqueTid = uint32_t;
51
Primiano Tucci0d72a312018-08-07 14:42:45 +010052// StringId is an offset into |string_pool_|.
Lalit Maganti5c454312019-04-08 12:11:17 +010053using StringId = StringPool::Id;
Primiano Tucci0d72a312018-08-07 14:42:45 +010054
Lalit Maganti5ea9e932018-11-30 14:19:39 +000055// Identifiers for all the tables in the database.
56enum TableId : uint8_t {
57 // Intentionally don't have TableId == 0 so that RowId == 0 can refer to an
58 // invalid row id.
Lalit Maganti8320e6d2019-03-14 18:49:33 +000059 kCounterValues = 1,
Lalit Maganti1d915a62019-01-07 12:10:42 +000060 kRawEvents = 2,
Lalit Maganti66ed7ad2019-01-11 16:47:26 +000061 kInstants = 3,
Isabelle Taylorb9222c32019-01-31 10:58:37 +000062 kSched = 4,
Eric Seckler70cc4422019-05-28 16:00:23 +010063 kNestableSlices = 5,
Lalit Maganti5ea9e932018-11-30 14:19:39 +000064};
65
66// The top 8 bits are set to the TableId and the bottom 32 to the row of the
67// table.
Lalit Maganti85ca4a82018-12-07 17:28:02 +000068using RowId = int64_t;
Lalit Maganti5ea9e932018-11-30 14:19:39 +000069static const RowId kInvalidRowId = 0;
70
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +000071using ArgSetId = uint32_t;
72static const ArgSetId kInvalidArgSetId = 0;
73
Isabelle Taylora97c5f52018-10-23 17:36:12 +010074enum RefType {
Primiano Tucci5403e4f2018-11-27 10:07:03 +000075 kRefNoRef = 0,
76 kRefUtid = 1,
77 kRefCpuId = 2,
78 kRefIrq = 3,
79 kRefSoftIrq = 4,
80 kRefUpid = 5,
Sidath Senanayake1f5f93a2019-06-06 22:24:15 +010081 kRefGpuId = 6,
Eric Seckler87fd11a2019-06-07 15:52:28 +010082 kRefGlobalAsyncTrack = 7,
83 kRefProcessAsyncTrack = 8,
Primiano Tucci5403e4f2018-11-27 10:07:03 +000084 kRefMax
Isabelle Taylora97c5f52018-10-23 17:36:12 +010085};
Isabelle Taylor14674d42018-09-07 11:33:11 +010086
Eric Seckler972225e2019-04-18 11:07:12 +010087const std::vector<const char*>& GetRefTypeStringMap();
88
Lalit Maganticaed37e2018-06-01 03:03:08 +010089// Stores a data inside a trace file in a columnar form. This makes it efficient
90// to read or search across a single field of the trace (e.g. all the thread
91// names for a given CPU).
92class TraceStorage {
93 public:
Isabelle Taylor47328cf2018-06-12 14:33:59 +010094 TraceStorage();
Isabelle Taylora0a22972018-08-03 12:06:12 +010095
96 virtual ~TraceStorage();
Isabelle Taylor47328cf2018-06-12 14:33:59 +010097
Isabelle Taylora0a22972018-08-03 12:06:12 +010098 // Information about a unique process seen in a trace.
99 struct Process {
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100100 explicit Process(uint32_t p) : pid(p) {}
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000101 int64_t start_ns = 0;
Lalit Maganti637589a2019-07-04 17:25:29 +0100102 int64_t end_ns = 0;
Isabelle Taylora0a22972018-08-03 12:06:12 +0100103 StringId name_id = 0;
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100104 uint32_t pid = 0;
Lalit Maganti08884242019-02-19 12:28:32 +0000105 base::Optional<UniquePid> pupid;
Isabelle Taylora0a22972018-08-03 12:06:12 +0100106 };
107
108 // Information about a unique thread seen in a trace.
109 struct Thread {
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100110 explicit Thread(uint32_t t) : tid(t) {}
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000111 int64_t start_ns = 0;
Lalit Magantib5bd2332019-06-06 14:20:47 +0100112 int64_t end_ns = 0;
Isabelle Taylora0a22972018-08-03 12:06:12 +0100113 StringId name_id = 0;
Lalit Maganti770886a2018-11-16 17:40:21 +0000114 base::Optional<UniquePid> upid;
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100115 uint32_t tid = 0;
Isabelle Taylora0a22972018-08-03 12:06:12 +0100116 };
Isabelle Taylor3dd366c2018-06-22 16:21:41 +0100117
Lalit Maganti6e9c55e2018-11-29 12:00:39 +0000118 // Generic key value storage which can be referenced by other tables.
119 class Args {
120 public:
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000121 struct Arg {
122 StringId flat_key = 0;
123 StringId key = 0;
124 Variadic value = Variadic::Integer(0);
125
126 // This is only used by the arg tracker and so is not part of the hash.
127 RowId row_id = 0;
128 };
129
130 struct ArgHasher {
131 uint64_t operator()(const Arg& arg) const noexcept {
Lalit Maganti1f464742019-02-28 13:49:31 +0000132 base::Hash hash;
133 hash.Update(arg.key);
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000134 // We don't hash arg.flat_key because it's a subsequence of arg.key.
135 switch (arg.value.type) {
136 case Variadic::Type::kInt:
Lalit Maganti1f464742019-02-28 13:49:31 +0000137 hash.Update(arg.value.int_value);
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000138 break;
Eric Secklerc93823e2019-06-03 16:49:19 +0100139 case Variadic::Type::kUint:
140 hash.Update(arg.value.uint_value);
141 break;
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000142 case Variadic::Type::kString:
Lalit Maganti1f464742019-02-28 13:49:31 +0000143 hash.Update(arg.value.string_value);
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000144 break;
145 case Variadic::Type::kReal:
Lalit Maganti1f464742019-02-28 13:49:31 +0000146 hash.Update(arg.value.real_value);
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000147 break;
Eric Secklerc93823e2019-06-03 16:49:19 +0100148 case Variadic::Type::kPointer:
149 hash.Update(arg.value.pointer_value);
150 break;
151 case Variadic::Type::kBool:
152 hash.Update(arg.value.bool_value);
153 break;
154 case Variadic::Type::kJson:
155 hash.Update(arg.value.json_value);
156 break;
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000157 }
Lalit Maganti1f464742019-02-28 13:49:31 +0000158 return hash.digest();
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000159 }
160 };
161
162 const std::deque<ArgSetId>& set_ids() const { return set_ids_; }
Lalit Maganti5ea9e932018-11-30 14:19:39 +0000163 const std::deque<StringId>& flat_keys() const { return flat_keys_; }
164 const std::deque<StringId>& keys() const { return keys_; }
Lalit Maganti1d915a62019-01-07 12:10:42 +0000165 const std::deque<Variadic>& arg_values() const { return arg_values_; }
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000166 uint32_t args_count() const {
167 return static_cast<uint32_t>(set_ids_.size());
Lalit Maganti79472be2018-12-04 13:41:27 +0000168 }
Lalit Maganti6e9c55e2018-11-29 12:00:39 +0000169
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000170 ArgSetId AddArgSet(const std::vector<Arg>& args,
171 uint32_t begin,
172 uint32_t end) {
Lalit Maganti1f464742019-02-28 13:49:31 +0000173 base::Hash hash;
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000174 for (uint32_t i = begin; i < end; i++) {
Lalit Maganti1f464742019-02-28 13:49:31 +0000175 hash.Update(ArgHasher()(args[i]));
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000176 }
Lalit Maganti5ea9e932018-11-30 14:19:39 +0000177
Lalit Maganti1f464742019-02-28 13:49:31 +0000178 ArgSetHash digest = hash.digest();
179 auto it = arg_row_for_hash_.find(digest);
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000180 if (it != arg_row_for_hash_.end()) {
181 return set_ids_[it->second];
182 }
183
184 // The +1 ensures that nothing has an id == kInvalidArgSetId == 0.
185 ArgSetId id = static_cast<uint32_t>(arg_row_for_hash_.size()) + 1;
Lalit Maganti1f464742019-02-28 13:49:31 +0000186 arg_row_for_hash_.emplace(digest, args_count());
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000187 for (uint32_t i = begin; i < end; i++) {
188 const auto& arg = args[i];
189 set_ids_.emplace_back(id);
190 flat_keys_.emplace_back(arg.flat_key);
191 keys_.emplace_back(arg.key);
192 arg_values_.emplace_back(arg.value);
193 }
194 return id;
Lalit Maganti6e9c55e2018-11-29 12:00:39 +0000195 }
196
Lalit Maganti5ea9e932018-11-30 14:19:39 +0000197 private:
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000198 using ArgSetHash = uint64_t;
199
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000200 std::deque<ArgSetId> set_ids_;
Lalit Maganti5ea9e932018-11-30 14:19:39 +0000201 std::deque<StringId> flat_keys_;
202 std::deque<StringId> keys_;
Lalit Maganti1d915a62019-01-07 12:10:42 +0000203 std::deque<Variadic> arg_values_;
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000204
205 std::unordered_map<ArgSetHash, uint32_t> arg_row_for_hash_;
Lalit Maganti6e9c55e2018-11-29 12:00:39 +0000206 };
207
Lalit Magantiff69c112018-09-24 12:07:47 +0100208 class Slices {
Lalit Maganti35622b72018-06-06 12:03:11 +0100209 public:
Lalit Magantifde29042018-10-04 13:28:52 +0100210 inline size_t AddSlice(uint32_t cpu,
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000211 int64_t start_ns,
212 int64_t duration_ns,
Lalit Magantib0b53ee2019-01-24 17:53:39 +0000213 UniqueTid utid,
214 ftrace_utils::TaskState end_state,
215 int32_t priority) {
Lalit Magantiff69c112018-09-24 12:07:47 +0100216 cpus_.emplace_back(cpu);
Lalit Maganti35622b72018-06-06 12:03:11 +0100217 start_ns_.emplace_back(start_ns);
218 durations_.emplace_back(duration_ns);
Isabelle Taylora0a22972018-08-03 12:06:12 +0100219 utids_.emplace_back(utid);
Lalit Magantib0b53ee2019-01-24 17:53:39 +0000220 end_states_.emplace_back(end_state);
221 priorities_.emplace_back(priority);
Lalit Maganti58638932019-01-31 10:48:26 +0000222
223 if (utid >= rows_for_utids_.size())
224 rows_for_utids_.resize(utid + 1);
225 rows_for_utids_[utid].emplace_back(slice_count() - 1);
Lalit Magantifde29042018-10-04 13:28:52 +0100226 return slice_count() - 1;
227 }
228
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000229 void set_duration(size_t index, int64_t duration_ns) {
Lalit Magantifde29042018-10-04 13:28:52 +0100230 durations_[index] = duration_ns;
Lalit Maganti35622b72018-06-06 12:03:11 +0100231 }
232
Lalit Magantib0b53ee2019-01-24 17:53:39 +0000233 void set_end_state(size_t index, ftrace_utils::TaskState end_state) {
234 end_states_[index] = end_state;
235 }
236
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100237 size_t slice_count() const { return start_ns_.size(); }
Lalit Maganti35622b72018-06-06 12:03:11 +0100238
Lalit Magantiff69c112018-09-24 12:07:47 +0100239 const std::deque<uint32_t>& cpus() const { return cpus_; }
240
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000241 const std::deque<int64_t>& start_ns() const { return start_ns_; }
Lalit Maganti35622b72018-06-06 12:03:11 +0100242
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000243 const std::deque<int64_t>& durations() const { return durations_; }
Lalit Maganti35622b72018-06-06 12:03:11 +0100244
Isabelle Taylor68e42192018-06-19 16:19:31 +0100245 const std::deque<UniqueTid>& utids() const { return utids_; }
246
Lalit Magantib0b53ee2019-01-24 17:53:39 +0000247 const std::deque<ftrace_utils::TaskState>& end_state() const {
248 return end_states_;
249 }
250
251 const std::deque<int32_t>& priorities() const { return priorities_; }
252
Lalit Maganti58638932019-01-31 10:48:26 +0000253 const std::deque<std::vector<uint32_t>>& rows_for_utids() const {
Lalit Magantif0f09c32019-01-18 17:29:31 +0000254 return rows_for_utids_;
255 }
256
Lalit Maganti35622b72018-06-06 12:03:11 +0100257 private:
Hector Dearman947f12a2018-09-11 16:50:36 +0100258 // Each deque below has the same number of entries (the number of slices
Lalit Maganti35622b72018-06-06 12:03:11 +0100259 // in the trace for the CPU).
Lalit Magantiff69c112018-09-24 12:07:47 +0100260 std::deque<uint32_t> cpus_;
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000261 std::deque<int64_t> start_ns_;
262 std::deque<int64_t> durations_;
Isabelle Taylor68e42192018-06-19 16:19:31 +0100263 std::deque<UniqueTid> utids_;
Lalit Magantib0b53ee2019-01-24 17:53:39 +0000264 std::deque<ftrace_utils::TaskState> end_states_;
265 std::deque<int32_t> priorities_;
Lalit Maganti58638932019-01-31 10:48:26 +0000266
267 // One row per utid.
268 std::deque<std::vector<uint32_t>> rows_for_utids_;
Primiano Tucci0d72a312018-08-07 14:42:45 +0100269 };
Isabelle Taylor68e42192018-06-19 16:19:31 +0100270
Primiano Tucci0d72a312018-08-07 14:42:45 +0100271 class NestableSlices {
272 public:
Eric Seckler70cc4422019-05-28 16:00:23 +0100273 inline uint32_t AddSlice(int64_t start_ns,
274 int64_t duration_ns,
275 int64_t ref,
276 RefType type,
277 StringId cat,
278 StringId name,
279 uint8_t depth,
280 int64_t stack_id,
281 int64_t parent_stack_id) {
Primiano Tucci0d72a312018-08-07 14:42:45 +0100282 start_ns_.emplace_back(start_ns);
283 durations_.emplace_back(duration_ns);
Eric Seckler972225e2019-04-18 11:07:12 +0100284 refs_.emplace_back(ref);
285 types_.emplace_back(type);
Primiano Tucci0d72a312018-08-07 14:42:45 +0100286 cats_.emplace_back(cat);
287 names_.emplace_back(name);
288 depths_.emplace_back(depth);
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100289 stack_ids_.emplace_back(stack_id);
290 parent_stack_ids_.emplace_back(parent_stack_id);
Eric Seckler70cc4422019-05-28 16:00:23 +0100291 arg_set_ids_.emplace_back(kInvalidArgSetId);
Lalit Maganti7b37bbf2018-11-09 15:58:54 +0000292 return slice_count() - 1;
293 }
294
Eric Seckler70cc4422019-05-28 16:00:23 +0100295 void set_duration(uint32_t index, int64_t duration_ns) {
Lalit Maganti7b37bbf2018-11-09 15:58:54 +0000296 durations_[index] = duration_ns;
297 }
298
Eric Seckler70cc4422019-05-28 16:00:23 +0100299 void set_stack_id(uint32_t index, int64_t stack_id) {
Lalit Maganti7b37bbf2018-11-09 15:58:54 +0000300 stack_ids_[index] = stack_id;
Primiano Tucci0d72a312018-08-07 14:42:45 +0100301 }
302
Eric Seckler70cc4422019-05-28 16:00:23 +0100303 void set_arg_set_id(uint32_t index, ArgSetId id) {
304 arg_set_ids_[index] = id;
305 }
306
307 uint32_t slice_count() const {
308 return static_cast<uint32_t>(start_ns_.size());
309 }
310
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000311 const std::deque<int64_t>& start_ns() const { return start_ns_; }
312 const std::deque<int64_t>& durations() const { return durations_; }
Eric Seckler972225e2019-04-18 11:07:12 +0100313 const std::deque<int64_t>& refs() const { return refs_; }
314 const std::deque<RefType>& types() const { return types_; }
Primiano Tucci0d72a312018-08-07 14:42:45 +0100315 const std::deque<StringId>& cats() const { return cats_; }
316 const std::deque<StringId>& names() const { return names_; }
317 const std::deque<uint8_t>& depths() const { return depths_; }
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000318 const std::deque<int64_t>& stack_ids() const { return stack_ids_; }
319 const std::deque<int64_t>& parent_stack_ids() const {
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100320 return parent_stack_ids_;
321 }
Eric Seckler70cc4422019-05-28 16:00:23 +0100322 const std::deque<ArgSetId>& arg_set_ids() const { return arg_set_ids_; }
Primiano Tucci0d72a312018-08-07 14:42:45 +0100323
324 private:
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000325 std::deque<int64_t> start_ns_;
326 std::deque<int64_t> durations_;
Eric Seckler972225e2019-04-18 11:07:12 +0100327 std::deque<int64_t> refs_;
328 std::deque<RefType> types_;
Eric Seckler651ca312019-06-05 16:41:51 +0100329 // TODO(eseckler): Remove this column and store the category in the args
330 // table instead, similar to what we do for instant events.
Primiano Tucci0d72a312018-08-07 14:42:45 +0100331 std::deque<StringId> cats_;
332 std::deque<StringId> names_;
333 std::deque<uint8_t> depths_;
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000334 std::deque<int64_t> stack_ids_;
335 std::deque<int64_t> parent_stack_ids_;
Eric Seckler70cc4422019-05-28 16:00:23 +0100336 std::deque<ArgSetId> arg_set_ids_;
Lalit Maganti35622b72018-06-06 12:03:11 +0100337 };
338
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000339 class CounterDefinitions {
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100340 public:
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000341 using Id = uint32_t;
Lalit Maganti521d97b2019-04-29 13:47:03 +0100342 static constexpr Id kInvalidId = std::numeric_limits<Id>::max();
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000343
344 inline Id AddCounterDefinition(StringId name_id,
345 int64_t ref,
346 RefType type) {
347 base::Hash hash;
348 hash.Update(name_id);
349 hash.Update(ref);
350 hash.Update(type);
351
352 // TODO(lalitm): this is a perf bottleneck and likely we can do something
353 // quite a bit better here.
354 uint64_t digest = hash.digest();
355 auto it = hash_to_row_idx_.find(digest);
356 if (it != hash_to_row_idx_.end())
357 return it->second;
358
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100359 name_ids_.emplace_back(name_id);
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100360 refs_.emplace_back(ref);
361 types_.emplace_back(type);
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000362 hash_to_row_idx_.emplace(digest, size() - 1);
363 return size() - 1;
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100364 }
Lalit Magantifde29042018-10-04 13:28:52 +0100365
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000366 uint32_t size() const { return static_cast<uint32_t>(name_ids_.size()); }
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100367
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100368 const std::deque<StringId>& name_ids() const { return name_ids_; }
369
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100370 const std::deque<int64_t>& refs() const { return refs_; }
371
372 const std::deque<RefType>& types() const { return types_; }
373
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000374 private:
375 std::deque<StringId> name_ids_;
376 std::deque<int64_t> refs_;
377 std::deque<RefType> types_;
378
379 std::unordered_map<uint64_t, uint32_t> hash_to_row_idx_;
380 };
381
382 class CounterValues {
383 public:
384 inline uint32_t AddCounterValue(CounterDefinitions::Id counter_id,
385 int64_t timestamp,
386 double value) {
387 counter_ids_.emplace_back(counter_id);
388 timestamps_.emplace_back(timestamp);
389 values_.emplace_back(value);
390 arg_set_ids_.emplace_back(kInvalidArgSetId);
Ioannis Ilkosf129c262019-04-25 14:53:51 +0100391
Lalit Maganti521d97b2019-04-29 13:47:03 +0100392 if (counter_id != CounterDefinitions::kInvalidId) {
393 if (counter_id >= rows_for_counter_id_.size()) {
394 rows_for_counter_id_.resize(counter_id + 1);
395 }
396 rows_for_counter_id_[counter_id].emplace_back(size() - 1);
397 }
398 return size() - 1;
399 }
400
401 void set_counter_id(uint32_t index, CounterDefinitions::Id counter_id) {
402 PERFETTO_DCHECK(counter_ids_[index] == CounterDefinitions::kInvalidId);
403
404 counter_ids_[index] = counter_id;
Ioannis Ilkosf129c262019-04-25 14:53:51 +0100405 if (counter_id >= rows_for_counter_id_.size()) {
406 rows_for_counter_id_.resize(counter_id + 1);
407 }
Lalit Maganti521d97b2019-04-29 13:47:03 +0100408
409 auto* new_rows = &rows_for_counter_id_[counter_id];
410 new_rows->insert(
411 std::upper_bound(new_rows->begin(), new_rows->end(), index), index);
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000412 }
413
414 void set_arg_set_id(uint32_t row, ArgSetId id) { arg_set_ids_[row] = id; }
415
416 uint32_t size() const { return static_cast<uint32_t>(counter_ids_.size()); }
417
418 const std::deque<CounterDefinitions::Id>& counter_ids() const {
419 return counter_ids_;
420 }
421
422 const std::deque<int64_t>& timestamps() const { return timestamps_; }
423
424 const std::deque<double>& values() const { return values_; }
425
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000426 const std::deque<ArgSetId>& arg_set_ids() const { return arg_set_ids_; }
427
Ioannis Ilkosf129c262019-04-25 14:53:51 +0100428 const std::deque<std::vector<uint32_t>>& rows_for_counter_id() const {
429 return rows_for_counter_id_;
430 }
431
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100432 private:
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000433 std::deque<CounterDefinitions::Id> counter_ids_;
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000434 std::deque<int64_t> timestamps_;
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100435 std::deque<double> values_;
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000436 std::deque<ArgSetId> arg_set_ids_;
Ioannis Ilkosf129c262019-04-25 14:53:51 +0100437
438 // Indexed by counter_id value and contains the row numbers corresponding to
439 // it.
440 std::deque<std::vector<uint32_t>> rows_for_counter_id_;
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100441 };
442
Primiano Tucci5cb84f82018-10-31 21:46:36 -0700443 class SqlStats {
444 public:
445 static constexpr size_t kMaxLogEntries = 100;
Lalit Magantiaac2f652019-04-30 12:16:21 +0100446 uint32_t RecordQueryBegin(const std::string& query,
447 int64_t time_queued,
448 int64_t time_started);
449 void RecordQueryFirstNext(uint32_t row, int64_t time_first_next);
450 void RecordQueryEnd(uint32_t row, int64_t time_end);
Primiano Tucci5cb84f82018-10-31 21:46:36 -0700451 size_t size() const { return queries_.size(); }
452 const std::deque<std::string>& queries() const { return queries_; }
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000453 const std::deque<int64_t>& times_queued() const { return times_queued_; }
454 const std::deque<int64_t>& times_started() const { return times_started_; }
Lalit Magantiaac2f652019-04-30 12:16:21 +0100455 const std::deque<int64_t>& times_first_next() const {
456 return times_first_next_;
457 }
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000458 const std::deque<int64_t>& times_ended() const { return times_ended_; }
Primiano Tucci5cb84f82018-10-31 21:46:36 -0700459
460 private:
Lalit Magantiaac2f652019-04-30 12:16:21 +0100461 uint32_t popped_queries_ = 0;
462
Primiano Tucci5cb84f82018-10-31 21:46:36 -0700463 std::deque<std::string> queries_;
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000464 std::deque<int64_t> times_queued_;
465 std::deque<int64_t> times_started_;
Lalit Magantiaac2f652019-04-30 12:16:21 +0100466 std::deque<int64_t> times_first_next_;
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000467 std::deque<int64_t> times_ended_;
Primiano Tucci5cb84f82018-10-31 21:46:36 -0700468 };
469
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000470 class Instants {
471 public:
Lalit Maganti66ed7ad2019-01-11 16:47:26 +0000472 inline uint32_t AddInstantEvent(int64_t timestamp,
473 StringId name_id,
474 double value,
475 int64_t ref,
476 RefType type) {
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000477 timestamps_.emplace_back(timestamp);
478 name_ids_.emplace_back(name_id);
479 values_.emplace_back(value);
480 refs_.emplace_back(ref);
481 types_.emplace_back(type);
Lalit Maganti1c21d172019-02-07 10:48:24 +0000482 arg_set_ids_.emplace_back(kInvalidArgSetId);
Lalit Maganti66ed7ad2019-01-11 16:47:26 +0000483 return static_cast<uint32_t>(instant_count() - 1);
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000484 }
485
Lalit Maganti521d97b2019-04-29 13:47:03 +0100486 void set_ref(uint32_t row, int64_t ref) { refs_[row] = ref; }
487
Lalit Maganti1c21d172019-02-07 10:48:24 +0000488 void set_arg_set_id(uint32_t row, ArgSetId id) { arg_set_ids_[row] = id; }
489
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000490 size_t instant_count() const { return timestamps_.size(); }
491
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000492 const std::deque<int64_t>& timestamps() const { return timestamps_; }
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000493
494 const std::deque<StringId>& name_ids() const { return name_ids_; }
495
496 const std::deque<double>& values() const { return values_; }
497
498 const std::deque<int64_t>& refs() const { return refs_; }
499
500 const std::deque<RefType>& types() const { return types_; }
501
Lalit Maganti1c21d172019-02-07 10:48:24 +0000502 const std::deque<ArgSetId>& arg_set_ids() const { return arg_set_ids_; }
503
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000504 private:
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000505 std::deque<int64_t> timestamps_;
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000506 std::deque<StringId> name_ids_;
507 std::deque<double> values_;
508 std::deque<int64_t> refs_;
509 std::deque<RefType> types_;
Lalit Maganti1c21d172019-02-07 10:48:24 +0000510 std::deque<ArgSetId> arg_set_ids_;
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000511 };
512
Lalit Maganti1d915a62019-01-07 12:10:42 +0000513 class RawEvents {
514 public:
515 inline RowId AddRawEvent(int64_t timestamp,
516 StringId name_id,
Lalit Magantie87cc812019-01-10 15:20:06 +0000517 uint32_t cpu,
Lalit Maganti1d915a62019-01-07 12:10:42 +0000518 UniqueTid utid) {
519 timestamps_.emplace_back(timestamp);
520 name_ids_.emplace_back(name_id);
Lalit Magantie87cc812019-01-10 15:20:06 +0000521 cpus_.emplace_back(cpu);
Lalit Maganti1d915a62019-01-07 12:10:42 +0000522 utids_.emplace_back(utid);
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000523 arg_set_ids_.emplace_back(kInvalidArgSetId);
Lalit Maganti1d915a62019-01-07 12:10:42 +0000524 return CreateRowId(TableId::kRawEvents,
525 static_cast<uint32_t>(raw_event_count() - 1));
526 }
527
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000528 void set_arg_set_id(uint32_t row, ArgSetId id) { arg_set_ids_[row] = id; }
529
Lalit Maganti1d915a62019-01-07 12:10:42 +0000530 size_t raw_event_count() const { return timestamps_.size(); }
531
532 const std::deque<int64_t>& timestamps() const { return timestamps_; }
533
534 const std::deque<StringId>& name_ids() const { return name_ids_; }
535
Lalit Magantie87cc812019-01-10 15:20:06 +0000536 const std::deque<uint32_t>& cpus() const { return cpus_; }
537
Lalit Maganti1d915a62019-01-07 12:10:42 +0000538 const std::deque<UniqueTid>& utids() const { return utids_; }
539
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000540 const std::deque<ArgSetId>& arg_set_ids() const { return arg_set_ids_; }
541
Lalit Maganti1d915a62019-01-07 12:10:42 +0000542 private:
543 std::deque<int64_t> timestamps_;
544 std::deque<StringId> name_ids_;
Lalit Magantie87cc812019-01-10 15:20:06 +0000545 std::deque<uint32_t> cpus_;
Lalit Maganti1d915a62019-01-07 12:10:42 +0000546 std::deque<UniqueTid> utids_;
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000547 std::deque<ArgSetId> arg_set_ids_;
Lalit Maganti1d915a62019-01-07 12:10:42 +0000548 };
549
Primiano Tucci2c761ef2019-01-07 20:20:46 +0000550 class AndroidLogs {
551 public:
552 inline size_t AddLogEvent(int64_t timestamp,
553 UniqueTid utid,
554 uint8_t prio,
555 StringId tag_id,
556 StringId msg_id) {
557 timestamps_.emplace_back(timestamp);
558 utids_.emplace_back(utid);
559 prios_.emplace_back(prio);
560 tag_ids_.emplace_back(tag_id);
561 msg_ids_.emplace_back(msg_id);
562 return size() - 1;
563 }
564
565 size_t size() const { return timestamps_.size(); }
566
567 const std::deque<int64_t>& timestamps() const { return timestamps_; }
568 const std::deque<UniqueTid>& utids() const { return utids_; }
569 const std::deque<uint8_t>& prios() const { return prios_; }
570 const std::deque<StringId>& tag_ids() const { return tag_ids_; }
571 const std::deque<StringId>& msg_ids() const { return msg_ids_; }
572
573 private:
574 std::deque<int64_t> timestamps_;
575 std::deque<UniqueTid> utids_;
576 std::deque<uint8_t> prios_;
577 std::deque<StringId> tag_ids_;
578 std::deque<StringId> msg_ids_;
579 };
580
Primiano Tucci0e38a142019-01-07 20:51:09 +0000581 struct Stats {
582 using IndexMap = std::map<int, int64_t>;
583 int64_t value = 0;
584 IndexMap indexed_values;
585 };
586 using StatsMap = std::array<Stats, stats::kNumKeys>;
587
Mikhail Khokhlove466c002019-05-23 13:33:33 +0100588 using MetadataMap = std::array<std::vector<Variadic>, metadata::kNumKeys>;
589
Florian Mayer438b5ab2019-05-02 11:18:06 +0100590 class HeapProfileFrames {
591 public:
Florian Mayerbee52132019-05-02 13:59:56 +0100592 struct Row {
593 StringId name_id;
594 int64_t mapping_row;
595 int64_t rel_pc;
Florian Mayer438b5ab2019-05-02 11:18:06 +0100596
Florian Mayerbee52132019-05-02 13:59:56 +0100597 bool operator==(const Row& other) const {
598 return std::tie(name_id, mapping_row, rel_pc) ==
599 std::tie(other.name_id, other.mapping_row, other.rel_pc);
600 }
601 };
602
Lalit Maganti9b2d52b2019-05-07 14:32:15 +0100603 uint32_t size() const { return static_cast<uint32_t>(names_.size()); }
604
Florian Mayerbee52132019-05-02 13:59:56 +0100605 int64_t Insert(const Row& row) {
606 names_.emplace_back(row.name_id);
607 mappings_.emplace_back(row.mapping_row);
608 rel_pcs_.emplace_back(row.rel_pc);
Florian Mayer438b5ab2019-05-02 11:18:06 +0100609 return static_cast<int64_t>(names_.size()) - 1;
610 }
611
612 const std::deque<StringId>& names() const { return names_; }
613 const std::deque<int64_t>& mappings() const { return mappings_; }
614 const std::deque<int64_t>& rel_pcs() const { return rel_pcs_; }
615
616 private:
617 std::deque<StringId> names_;
618 std::deque<int64_t> mappings_;
619 std::deque<int64_t> rel_pcs_;
620 };
621
622 class HeapProfileCallsites {
623 public:
Florian Mayerbee52132019-05-02 13:59:56 +0100624 struct Row {
625 int64_t depth;
626 int64_t parent_id;
627 int64_t frame_row;
628
629 bool operator==(const Row& other) const {
630 return std::tie(depth, parent_id, frame_row) ==
631 std::tie(other.depth, other.parent_id, other.frame_row);
Florian Mayer438b5ab2019-05-02 11:18:06 +0100632 }
Florian Mayerbee52132019-05-02 13:59:56 +0100633 };
634
Lalit Maganti9b2d52b2019-05-07 14:32:15 +0100635 uint32_t size() const { return static_cast<uint32_t>(frame_ids_.size()); }
636
Florian Mayerbee52132019-05-02 13:59:56 +0100637 int64_t Insert(const Row& row) {
638 frame_depths_.emplace_back(row.depth);
639 parent_callsite_ids_.emplace_back(row.parent_id);
640 frame_ids_.emplace_back(row.frame_row);
Florian Mayer438b5ab2019-05-02 11:18:06 +0100641 return static_cast<int64_t>(frame_depths_.size()) - 1;
642 }
643
644 const std::deque<int64_t>& frame_depths() const { return frame_depths_; }
645 const std::deque<int64_t>& parent_callsite_ids() const {
646 return parent_callsite_ids_;
647 }
648 const std::deque<int64_t>& frame_ids() const { return frame_ids_; }
649
650 private:
651 std::deque<int64_t> frame_depths_;
652 std::deque<int64_t> parent_callsite_ids_;
653 std::deque<int64_t> frame_ids_;
654 };
655
656 class HeapProfileMappings {
657 public:
Florian Mayerbee52132019-05-02 13:59:56 +0100658 struct Row {
659 StringId build_id;
Florian Mayer12655732019-07-02 15:08:26 +0100660 int64_t exact_offset;
661 int64_t start_offset;
Florian Mayerbee52132019-05-02 13:59:56 +0100662 int64_t start;
663 int64_t end;
664 int64_t load_bias;
665 StringId name_id;
Florian Mayer438b5ab2019-05-02 11:18:06 +0100666
Florian Mayerbee52132019-05-02 13:59:56 +0100667 bool operator==(const Row& other) const {
Florian Mayer12655732019-07-02 15:08:26 +0100668 return std::tie(build_id, exact_offset, start_offset, start, end,
669 load_bias, name_id) ==
670 std::tie(other.build_id, other.exact_offset, other.start_offset,
671 other.start, other.end, other.load_bias, other.name_id);
Florian Mayerbee52132019-05-02 13:59:56 +0100672 }
673 };
674
Lalit Maganti9b2d52b2019-05-07 14:32:15 +0100675 uint32_t size() const { return static_cast<uint32_t>(names_.size()); }
676
Florian Mayerbee52132019-05-02 13:59:56 +0100677 int64_t Insert(const Row& row) {
678 build_ids_.emplace_back(row.build_id);
Florian Mayer12655732019-07-02 15:08:26 +0100679 exact_offsets_.emplace_back(row.exact_offset);
680 start_offsets_.emplace_back(row.start_offset);
Florian Mayerbee52132019-05-02 13:59:56 +0100681 starts_.emplace_back(row.start);
682 ends_.emplace_back(row.end);
683 load_biases_.emplace_back(row.load_bias);
684 names_.emplace_back(row.name_id);
Florian Mayer438b5ab2019-05-02 11:18:06 +0100685 return static_cast<int64_t>(build_ids_.size()) - 1;
686 }
687
688 const std::deque<StringId>& build_ids() const { return build_ids_; }
Florian Mayer12655732019-07-02 15:08:26 +0100689 const std::deque<int64_t>& exact_offsets() const { return exact_offsets_; }
690 const std::deque<int64_t>& start_offsets() const { return start_offsets_; }
Florian Mayer438b5ab2019-05-02 11:18:06 +0100691 const std::deque<int64_t>& starts() const { return starts_; }
692 const std::deque<int64_t>& ends() const { return ends_; }
693 const std::deque<int64_t>& load_biases() const { return load_biases_; }
694 const std::deque<StringId>& names() const { return names_; }
695
696 private:
697 std::deque<StringId> build_ids_;
Florian Mayer12655732019-07-02 15:08:26 +0100698 std::deque<int64_t> exact_offsets_;
699 std::deque<int64_t> start_offsets_;
Florian Mayer438b5ab2019-05-02 11:18:06 +0100700 std::deque<int64_t> starts_;
701 std::deque<int64_t> ends_;
702 std::deque<int64_t> load_biases_;
703 std::deque<StringId> names_;
704 };
705
706 class HeapProfileAllocations {
707 public:
Florian Mayerbee52132019-05-02 13:59:56 +0100708 struct Row {
709 int64_t timestamp;
Florian Mayerdf1968c2019-05-13 16:39:35 +0100710 UniquePid upid;
Florian Mayerbee52132019-05-02 13:59:56 +0100711 int64_t callsite_id;
712 int64_t count;
713 int64_t size;
714 };
715
Lalit Maganti9b2d52b2019-05-07 14:32:15 +0100716 uint32_t size() const { return static_cast<uint32_t>(timestamps_.size()); }
717
Florian Mayerbee52132019-05-02 13:59:56 +0100718 void Insert(const Row& row) {
719 timestamps_.emplace_back(row.timestamp);
Florian Mayerdf1968c2019-05-13 16:39:35 +0100720 upids_.emplace_back(row.upid);
Florian Mayerbee52132019-05-02 13:59:56 +0100721 callsite_ids_.emplace_back(row.callsite_id);
722 counts_.emplace_back(row.count);
723 sizes_.emplace_back(row.size);
Florian Mayer438b5ab2019-05-02 11:18:06 +0100724 }
725
726 const std::deque<int64_t>& timestamps() const { return timestamps_; }
Florian Mayerdf1968c2019-05-13 16:39:35 +0100727 const std::deque<UniquePid>& upids() const { return upids_; }
Florian Mayer438b5ab2019-05-02 11:18:06 +0100728 const std::deque<int64_t>& callsite_ids() const { return callsite_ids_; }
729 const std::deque<int64_t>& counts() const { return counts_; }
730 const std::deque<int64_t>& sizes() const { return sizes_; }
731
732 private:
733 std::deque<int64_t> timestamps_;
Florian Mayerdf1968c2019-05-13 16:39:35 +0100734 std::deque<UniquePid> upids_;
Florian Mayer438b5ab2019-05-02 11:18:06 +0100735 std::deque<int64_t> callsite_ids_;
736 std::deque<int64_t> counts_;
737 std::deque<int64_t> sizes_;
738 };
739
Isabelle Taylora0a22972018-08-03 12:06:12 +0100740 void ResetStorage();
Lalit Maganti35622b72018-06-06 12:03:11 +0100741
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100742 UniqueTid AddEmptyThread(uint32_t tid) {
743 unique_threads_.emplace_back(tid);
Isabelle Taylora0a22972018-08-03 12:06:12 +0100744 return static_cast<UniqueTid>(unique_threads_.size() - 1);
745 }
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100746
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100747 UniquePid AddEmptyProcess(uint32_t pid) {
748 unique_processes_.emplace_back(pid);
Isabelle Taylora0a22972018-08-03 12:06:12 +0100749 return static_cast<UniquePid>(unique_processes_.size() - 1);
750 }
Isabelle Taylor3dd366c2018-06-22 16:21:41 +0100751
Isabelle Taylora0a22972018-08-03 12:06:12 +0100752 // Return an unqiue identifier for the contents of each string.
753 // The string is copied internally and can be destroyed after this called.
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100754 // Virtual for testing.
Lalit Maganti5c454312019-04-08 12:11:17 +0100755 virtual StringId InternString(base::StringView str) {
756 return string_pool_.InternString(str);
757 }
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100758
Isabelle Taylora0a22972018-08-03 12:06:12 +0100759 Process* GetMutableProcess(UniquePid upid) {
Lalit Maganti676658b2018-11-20 18:24:31 +0000760 PERFETTO_DCHECK(upid < unique_processes_.size());
Isabelle Taylora0a22972018-08-03 12:06:12 +0100761 return &unique_processes_[upid];
762 }
Isabelle Taylor3dd366c2018-06-22 16:21:41 +0100763
Isabelle Taylora0a22972018-08-03 12:06:12 +0100764 Thread* GetMutableThread(UniqueTid utid) {
Florian Mayera1aaec22018-09-19 17:02:26 +0100765 PERFETTO_DCHECK(utid < unique_threads_.size());
Isabelle Taylora0a22972018-08-03 12:06:12 +0100766 return &unique_threads_[utid];
767 }
Isabelle Taylor3dd366c2018-06-22 16:21:41 +0100768
Primiano Tucci0e38a142019-01-07 20:51:09 +0000769 // Example usage: SetStats(stats::android_log_num_failed, 42);
770 void SetStats(size_t key, int64_t value) {
771 PERFETTO_DCHECK(key < stats::kNumKeys);
772 PERFETTO_DCHECK(stats::kTypes[key] == stats::kSingle);
773 stats_[key].value = value;
774 }
775
776 // Example usage: IncrementStats(stats::android_log_num_failed, -1);
777 void IncrementStats(size_t key, int64_t increment = 1) {
778 PERFETTO_DCHECK(key < stats::kNumKeys);
779 PERFETTO_DCHECK(stats::kTypes[key] == stats::kSingle);
780 stats_[key].value += increment;
781 }
782
Florian Mayer438b5ab2019-05-02 11:18:06 +0100783 // Example usage: IncrementIndexedStats(stats::cpu_failure, 1);
784 void IncrementIndexedStats(size_t key, int index, int64_t increment = 1) {
785 PERFETTO_DCHECK(key < stats::kNumKeys);
786 PERFETTO_DCHECK(stats::kTypes[key] == stats::kIndexed);
787 stats_[key].indexed_values[index] += increment;
788 }
789
Primiano Tucci0e38a142019-01-07 20:51:09 +0000790 // Example usage: SetIndexedStats(stats::cpu_failure, 1, 42);
791 void SetIndexedStats(size_t key, int index, int64_t value) {
792 PERFETTO_DCHECK(key < stats::kNumKeys);
793 PERFETTO_DCHECK(stats::kTypes[key] == stats::kIndexed);
794 stats_[key].indexed_values[index] = value;
795 }
796
Mikhail Khokhlove466c002019-05-23 13:33:33 +0100797 // Example usage:
798 // SetMetadata(metadata::benchmark_name,
799 // Variadic::String(storage->InternString("foo"));
Mikhail Khokhlovb1fe42a2019-05-23 13:58:05 +0100800 // Virtual for testing.
801 virtual void SetMetadata(size_t key, Variadic value) {
Mikhail Khokhlove466c002019-05-23 13:33:33 +0100802 PERFETTO_DCHECK(key < metadata::kNumKeys);
803 PERFETTO_DCHECK(metadata::kKeyTypes[key] == metadata::kSingle);
804 PERFETTO_DCHECK(value.type == metadata::kValueTypes[key]);
805 metadata_[key] = {value};
806 }
807
808 // Example usage:
809 // AppendMetadata(metadata::benchmark_story_tags,
810 // Variadic::String(storage->InternString("bar"));
Mikhail Khokhlovb1fe42a2019-05-23 13:58:05 +0100811 // Virtual for testing.
812 virtual void AppendMetadata(size_t key, Variadic value) {
Mikhail Khokhlove466c002019-05-23 13:33:33 +0100813 PERFETTO_DCHECK(key < metadata::kNumKeys);
814 PERFETTO_DCHECK(metadata::kKeyTypes[key] == metadata::kMulti);
815 PERFETTO_DCHECK(value.type == metadata::kValueTypes[key]);
816 metadata_[key].push_back(value);
817 }
818
Eric Seckler77b52782019-05-02 15:18:57 +0100819 class ScopedStatsTracer {
820 public:
821 ScopedStatsTracer(TraceStorage* storage, size_t key)
822 : storage_(storage), key_(key), start_ns_(base::GetWallTimeNs()) {}
823
824 ~ScopedStatsTracer() {
825 if (!storage_)
826 return;
827 auto delta_ns = base::GetWallTimeNs() - start_ns_;
828 storage_->IncrementStats(key_, delta_ns.count());
829 }
830
831 ScopedStatsTracer(ScopedStatsTracer&& other) noexcept { MoveImpl(&other); }
832
833 ScopedStatsTracer& operator=(ScopedStatsTracer&& other) {
834 MoveImpl(&other);
835 return *this;
836 }
837
838 private:
839 ScopedStatsTracer(const ScopedStatsTracer&) = delete;
840 ScopedStatsTracer& operator=(const ScopedStatsTracer&) = delete;
841
842 void MoveImpl(ScopedStatsTracer* other) {
843 storage_ = other->storage_;
844 key_ = other->key_;
845 start_ns_ = other->start_ns_;
846 other->storage_ = nullptr;
847 }
848
849 TraceStorage* storage_;
850 size_t key_;
851 base::TimeNanos start_ns_;
852 };
853
854 ScopedStatsTracer TraceExecutionTimeIntoStats(size_t key) {
855 return ScopedStatsTracer(this, key);
856 }
857
Lalit Maganticaed37e2018-06-01 03:03:08 +0100858 // Reading methods.
Eric Secklerc93823e2019-06-03 16:49:19 +0100859 // Virtual for testing.
860 virtual NullTermStringView GetString(StringId id) const {
Lalit Maganti5c454312019-04-08 12:11:17 +0100861 return string_pool_.Get(id);
Isabelle Taylora0a22972018-08-03 12:06:12 +0100862 }
863
Lalit Magantie9d40532018-06-29 13:15:06 +0100864 const Process& GetProcess(UniquePid upid) const {
Lalit Maganti676658b2018-11-20 18:24:31 +0000865 PERFETTO_DCHECK(upid < unique_processes_.size());
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100866 return unique_processes_[upid];
867 }
868
Lalit Magantie9d40532018-06-29 13:15:06 +0100869 const Thread& GetThread(UniqueTid utid) const {
Lalit Maganti1f64cfa2018-09-18 13:20:02 +0100870 // Allow utid == 0 for idle thread retrieval.
Florian Mayera1aaec22018-09-19 17:02:26 +0100871 PERFETTO_DCHECK(utid < unique_threads_.size());
Isabelle Taylor68e42192018-06-19 16:19:31 +0100872 return unique_threads_[utid];
873 }
874
Lalit Maganti5ea9e932018-11-30 14:19:39 +0000875 static RowId CreateRowId(TableId table, uint32_t row) {
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000876 return (static_cast<RowId>(table) << kRowIdTableShift) | row;
Lalit Maganti5ea9e932018-11-30 14:19:39 +0000877 }
878
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000879 static std::pair<int8_t /*table*/, uint32_t /*row*/> ParseRowId(RowId rowid) {
880 auto id = static_cast<uint64_t>(rowid);
881 auto table_id = static_cast<uint8_t>(id >> kRowIdTableShift);
882 auto row = static_cast<uint32_t>(id & ((1ull << kRowIdTableShift) - 1));
883 return std::make_pair(table_id, row);
884 }
885
Lalit Magantiff69c112018-09-24 12:07:47 +0100886 const Slices& slices() const { return slices_; }
Lalit Magantifde29042018-10-04 13:28:52 +0100887 Slices* mutable_slices() { return &slices_; }
888
Primiano Tucci0d72a312018-08-07 14:42:45 +0100889 const NestableSlices& nestable_slices() const { return nestable_slices_; }
890 NestableSlices* mutable_nestable_slices() { return &nestable_slices_; }
891
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000892 const CounterDefinitions& counter_definitions() const {
893 return counter_definitions_;
894 }
895 CounterDefinitions* mutable_counter_definitions() {
896 return &counter_definitions_;
897 }
898
899 const CounterValues& counter_values() const { return counter_values_; }
900 CounterValues* mutable_counter_values() { return &counter_values_; }
Isabelle Taylor14674d42018-09-07 11:33:11 +0100901
Primiano Tucci5cb84f82018-10-31 21:46:36 -0700902 const SqlStats& sql_stats() const { return sql_stats_; }
903 SqlStats* mutable_sql_stats() { return &sql_stats_; }
904
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000905 const Instants& instants() const { return instants_; }
906 Instants* mutable_instants() { return &instants_; }
907
Primiano Tucci2c761ef2019-01-07 20:20:46 +0000908 const AndroidLogs& android_logs() const { return android_log_; }
909 AndroidLogs* mutable_android_log() { return &android_log_; }
910
Primiano Tucci0e38a142019-01-07 20:51:09 +0000911 const StatsMap& stats() const { return stats_; }
Lalit Maganti05e8c132018-11-09 18:16:12 +0000912
Mikhail Khokhlove466c002019-05-23 13:33:33 +0100913 const MetadataMap& metadata() const { return metadata_; }
914
Lalit Maganti6e9c55e2018-11-29 12:00:39 +0000915 const Args& args() const { return args_; }
916 Args* mutable_args() { return &args_; }
917
Lalit Maganti1d915a62019-01-07 12:10:42 +0000918 const RawEvents& raw_events() const { return raw_events_; }
919 RawEvents* mutable_raw_events() { return &raw_events_; }
920
Florian Mayer438b5ab2019-05-02 11:18:06 +0100921 const HeapProfileMappings& heap_profile_mappings() const {
922 return heap_profile_mappings_;
923 }
924 HeapProfileMappings* mutable_heap_profile_mappings() {
925 return &heap_profile_mappings_;
926 }
927
928 const HeapProfileFrames& heap_profile_frames() const {
929 return heap_profile_frames_;
930 }
931 HeapProfileFrames* mutable_heap_profile_frames() {
932 return &heap_profile_frames_;
933 }
934
935 const HeapProfileCallsites& heap_profile_callsites() const {
936 return heap_profile_callsites_;
937 }
938 HeapProfileCallsites* mutable_heap_profile_callsites() {
939 return &heap_profile_callsites_;
940 }
941
942 const HeapProfileAllocations& heap_profile_allocations() const {
943 return heap_profile_allocations_;
944 }
945 HeapProfileAllocations* mutable_heap_profile_allocations() {
946 return &heap_profile_allocations_;
947 }
948
Lalit Maganti5c454312019-04-08 12:11:17 +0100949 const StringPool& string_pool() const { return string_pool_; }
Lalit Magantiacda68b2018-10-29 15:23:25 +0000950
Isabelle Taylore7003fb2018-07-17 11:39:01 +0100951 // |unique_processes_| always contains at least 1 element becuase the 0th ID
952 // is reserved to indicate an invalid process.
Lalit Maganti9c6395b2019-01-17 00:25:09 +0000953 size_t process_count() const { return unique_processes_.size(); }
Primiano Tucci0d72a312018-08-07 14:42:45 +0100954
Isabelle Taylore7003fb2018-07-17 11:39:01 +0100955 // |unique_threads_| always contains at least 1 element becuase the 0th ID
956 // is reserved to indicate an invalid thread.
Lalit Maganti9c6395b2019-01-17 00:25:09 +0000957 size_t thread_count() const { return unique_threads_.size(); }
Isabelle Taylore7003fb2018-07-17 11:39:01 +0100958
Hector Dearman12323362018-08-09 16:09:28 +0100959 // Number of interned strings in the pool. Includes the empty string w/ ID=0.
960 size_t string_count() const { return string_pool_.size(); }
961
Ioannis Ilkosb8b11102019-01-29 17:56:55 +0000962 // Start / end ts (in nanoseconds) across the parsed trace events.
963 // Returns (0, 0) if the trace is empty.
964 std::pair<int64_t, int64_t> GetTraceTimestampBoundsNs() const;
965
Lalit Maganticaed37e2018-06-01 03:03:08 +0100966 private:
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000967 static constexpr uint8_t kRowIdTableShift = 32;
Isabelle Taylora0a22972018-08-03 12:06:12 +0100968
Primiano Tucci2da5d2e2018-08-10 14:23:31 +0100969 using StringHash = uint64_t;
Lalit Maganticaed37e2018-06-01 03:03:08 +0100970
Lalit Maganti5c454312019-04-08 12:11:17 +0100971 TraceStorage(const TraceStorage&) = delete;
972 TraceStorage& operator=(const TraceStorage&) = delete;
973
974 TraceStorage(TraceStorage&&) = default;
975 TraceStorage& operator=(TraceStorage&&) = default;
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000976
Lalit Maganti05e8c132018-11-09 18:16:12 +0000977 // Stats about parsing the trace.
Primiano Tucci0e38a142019-01-07 20:51:09 +0000978 StatsMap stats_{};
Lalit Maganti35622b72018-06-06 12:03:11 +0100979
Mikhail Khokhlove466c002019-05-23 13:33:33 +0100980 // Trace metadata from chrome and benchmarking infrastructure.
981 MetadataMap metadata_{};
982
Lalit Maganticaed37e2018-06-01 03:03:08 +0100983 // One entry for each CPU in the trace.
Lalit Magantiff69c112018-09-24 12:07:47 +0100984 Slices slices_;
Lalit Maganticaed37e2018-06-01 03:03:08 +0100985
Lalit Maganti6e9c55e2018-11-29 12:00:39 +0000986 // Args for all other tables.
987 Args args_;
988
Lalit Maganticaed37e2018-06-01 03:03:08 +0100989 // One entry for each unique string in the trace.
Lalit Maganti5c454312019-04-08 12:11:17 +0100990 StringPool string_pool_;
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100991
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100992 // One entry for each UniquePid, with UniquePid as the index.
Ioannis Ilkos9c03cbd2019-03-12 18:30:43 +0000993 // Never hold on to pointers to Process, as vector resize will
994 // invalidate them.
995 std::vector<Process> unique_processes_;
Isabelle Taylor68e42192018-06-19 16:19:31 +0100996
Isabelle Taylor68e42192018-06-19 16:19:31 +0100997 // One entry for each UniqueTid, with UniqueTid as the index.
Isabelle Taylor3dd366c2018-06-22 16:21:41 +0100998 std::deque<Thread> unique_threads_;
Primiano Tucci0d72a312018-08-07 14:42:45 +0100999
1000 // Slices coming from userspace events (e.g. Chromium TRACE_EVENT macros).
1001 NestableSlices nestable_slices_;
Isabelle Taylor15314ea2018-09-19 11:35:19 +01001002
Hector Dearman2442d612019-06-04 18:05:23 +01001003 // The type of counters in the trace. Can be thought of as the "metadata".
Lalit Maganti8320e6d2019-03-14 18:49:33 +00001004 CounterDefinitions counter_definitions_;
1005
1006 // The values from the Counter events from the trace. This includes CPU
1007 // frequency events as well systrace trace_marker counter events.
1008 CounterValues counter_values_;
Primiano Tucci5cb84f82018-10-31 21:46:36 -07001009
1010 SqlStats sql_stats_;
Lalit Maganti6e9c55e2018-11-29 12:00:39 +00001011
Isabelle Taylorc8c11202018-11-05 11:36:22 +00001012 // These are instantaneous events in the trace. They have no duration
1013 // and do not have a value that make sense to track over time.
1014 // e.g. signal events
1015 Instants instants_;
Lalit Maganti1d915a62019-01-07 12:10:42 +00001016
1017 // Raw events are every ftrace event in the trace. The raw event includes
1018 // the timestamp and the pid. The args for the raw event will be in the
1019 // args table. This table can be used to generate a text version of the
1020 // trace.
1021 RawEvents raw_events_;
Primiano Tucci2c761ef2019-01-07 20:20:46 +00001022 AndroidLogs android_log_;
Florian Mayer438b5ab2019-05-02 11:18:06 +01001023
1024 HeapProfileMappings heap_profile_mappings_;
1025 HeapProfileFrames heap_profile_frames_;
1026 HeapProfileCallsites heap_profile_callsites_;
1027 HeapProfileAllocations heap_profile_allocations_;
Lalit Maganticaed37e2018-06-01 03:03:08 +01001028};
1029
1030} // namespace trace_processor
1031} // namespace perfetto
1032
Florian Mayerbee52132019-05-02 13:59:56 +01001033namespace std {
1034
1035template <>
1036struct hash<::perfetto::trace_processor::TraceStorage::HeapProfileFrames::Row> {
1037 using argument_type =
1038 ::perfetto::trace_processor::TraceStorage::HeapProfileFrames::Row;
1039 using result_type = size_t;
1040
1041 result_type operator()(const argument_type& r) const {
1042 return std::hash<::perfetto::trace_processor::StringId>{}(r.name_id) ^
1043 std::hash<int64_t>{}(r.mapping_row) ^ std::hash<int64_t>{}(r.rel_pc);
1044 }
1045};
1046
1047template <>
1048struct hash<
1049 ::perfetto::trace_processor::TraceStorage::HeapProfileCallsites::Row> {
1050 using argument_type =
1051 ::perfetto::trace_processor::TraceStorage::HeapProfileCallsites::Row;
1052 using result_type = size_t;
1053
1054 result_type operator()(const argument_type& r) const {
1055 return std::hash<int64_t>{}(r.depth) ^ std::hash<int64_t>{}(r.parent_id) ^
1056 std::hash<int64_t>{}(r.frame_row);
1057 }
1058};
1059
1060template <>
1061struct hash<
1062 ::perfetto::trace_processor::TraceStorage::HeapProfileMappings::Row> {
1063 using argument_type =
1064 ::perfetto::trace_processor::TraceStorage::HeapProfileMappings::Row;
1065 using result_type = size_t;
1066
1067 result_type operator()(const argument_type& r) const {
1068 return std::hash<::perfetto::trace_processor::StringId>{}(r.build_id) ^
Florian Mayer12655732019-07-02 15:08:26 +01001069 std::hash<int64_t>{}(r.exact_offset) ^
1070 std::hash<int64_t>{}(r.start_offset) ^
1071 std::hash<int64_t>{}(r.start) ^ std::hash<int64_t>{}(r.end) ^
1072 std::hash<int64_t>{}(r.load_bias) ^
Florian Mayerbee52132019-05-02 13:59:56 +01001073 std::hash<::perfetto::trace_processor::StringId>{}(r.name_id);
1074 }
1075};
1076
1077} // namespace std
1078
Lalit Maganticaed37e2018-06-01 03:03:08 +01001079#endif // SRC_TRACE_PROCESSOR_TRACE_STORAGE_H_