blob: 6f313688aec39d229610fe2031e373e8a330e726 [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,
Primiano Tucci5403e4f2018-11-27 10:07:03 +000082 kRefMax
Isabelle Taylora97c5f52018-10-23 17:36:12 +010083};
Isabelle Taylor14674d42018-09-07 11:33:11 +010084
Eric Seckler972225e2019-04-18 11:07:12 +010085const std::vector<const char*>& GetRefTypeStringMap();
86
Lalit Maganticaed37e2018-06-01 03:03:08 +010087// Stores a data inside a trace file in a columnar form. This makes it efficient
88// to read or search across a single field of the trace (e.g. all the thread
89// names for a given CPU).
90class TraceStorage {
91 public:
Isabelle Taylor47328cf2018-06-12 14:33:59 +010092 TraceStorage();
Isabelle Taylora0a22972018-08-03 12:06:12 +010093
94 virtual ~TraceStorage();
Isabelle Taylor47328cf2018-06-12 14:33:59 +010095
Isabelle Taylora0a22972018-08-03 12:06:12 +010096 // Information about a unique process seen in a trace.
97 struct Process {
Primiano Tuccib75dcee2018-08-08 12:21:36 +010098 explicit Process(uint32_t p) : pid(p) {}
Lalit Maganti85ca4a82018-12-07 17:28:02 +000099 int64_t start_ns = 0;
Isabelle Taylora0a22972018-08-03 12:06:12 +0100100 StringId name_id = 0;
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100101 uint32_t pid = 0;
Lalit Maganti08884242019-02-19 12:28:32 +0000102 base::Optional<UniquePid> pupid;
Isabelle Taylora0a22972018-08-03 12:06:12 +0100103 };
104
105 // Information about a unique thread seen in a trace.
106 struct Thread {
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100107 explicit Thread(uint32_t t) : tid(t) {}
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000108 int64_t start_ns = 0;
Lalit Magantib5bd2332019-06-06 14:20:47 +0100109 int64_t end_ns = 0;
Isabelle Taylora0a22972018-08-03 12:06:12 +0100110 StringId name_id = 0;
Lalit Maganti770886a2018-11-16 17:40:21 +0000111 base::Optional<UniquePid> upid;
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100112 uint32_t tid = 0;
Isabelle Taylora0a22972018-08-03 12:06:12 +0100113 };
Isabelle Taylor3dd366c2018-06-22 16:21:41 +0100114
Lalit Maganti6e9c55e2018-11-29 12:00:39 +0000115 // Generic key value storage which can be referenced by other tables.
116 class Args {
117 public:
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000118 struct Arg {
119 StringId flat_key = 0;
120 StringId key = 0;
121 Variadic value = Variadic::Integer(0);
122
123 // This is only used by the arg tracker and so is not part of the hash.
124 RowId row_id = 0;
125 };
126
127 struct ArgHasher {
128 uint64_t operator()(const Arg& arg) const noexcept {
Lalit Maganti1f464742019-02-28 13:49:31 +0000129 base::Hash hash;
130 hash.Update(arg.key);
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000131 // We don't hash arg.flat_key because it's a subsequence of arg.key.
132 switch (arg.value.type) {
133 case Variadic::Type::kInt:
Lalit Maganti1f464742019-02-28 13:49:31 +0000134 hash.Update(arg.value.int_value);
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000135 break;
Eric Secklerc93823e2019-06-03 16:49:19 +0100136 case Variadic::Type::kUint:
137 hash.Update(arg.value.uint_value);
138 break;
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000139 case Variadic::Type::kString:
Lalit Maganti1f464742019-02-28 13:49:31 +0000140 hash.Update(arg.value.string_value);
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000141 break;
142 case Variadic::Type::kReal:
Lalit Maganti1f464742019-02-28 13:49:31 +0000143 hash.Update(arg.value.real_value);
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000144 break;
Eric Secklerc93823e2019-06-03 16:49:19 +0100145 case Variadic::Type::kPointer:
146 hash.Update(arg.value.pointer_value);
147 break;
148 case Variadic::Type::kBool:
149 hash.Update(arg.value.bool_value);
150 break;
151 case Variadic::Type::kJson:
152 hash.Update(arg.value.json_value);
153 break;
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000154 }
Lalit Maganti1f464742019-02-28 13:49:31 +0000155 return hash.digest();
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000156 }
157 };
158
159 const std::deque<ArgSetId>& set_ids() const { return set_ids_; }
Lalit Maganti5ea9e932018-11-30 14:19:39 +0000160 const std::deque<StringId>& flat_keys() const { return flat_keys_; }
161 const std::deque<StringId>& keys() const { return keys_; }
Lalit Maganti1d915a62019-01-07 12:10:42 +0000162 const std::deque<Variadic>& arg_values() const { return arg_values_; }
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000163 uint32_t args_count() const {
164 return static_cast<uint32_t>(set_ids_.size());
Lalit Maganti79472be2018-12-04 13:41:27 +0000165 }
Lalit Maganti6e9c55e2018-11-29 12:00:39 +0000166
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000167 ArgSetId AddArgSet(const std::vector<Arg>& args,
168 uint32_t begin,
169 uint32_t end) {
Lalit Maganti1f464742019-02-28 13:49:31 +0000170 base::Hash hash;
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000171 for (uint32_t i = begin; i < end; i++) {
Lalit Maganti1f464742019-02-28 13:49:31 +0000172 hash.Update(ArgHasher()(args[i]));
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000173 }
Lalit Maganti5ea9e932018-11-30 14:19:39 +0000174
Lalit Maganti1f464742019-02-28 13:49:31 +0000175 ArgSetHash digest = hash.digest();
176 auto it = arg_row_for_hash_.find(digest);
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000177 if (it != arg_row_for_hash_.end()) {
178 return set_ids_[it->second];
179 }
180
181 // The +1 ensures that nothing has an id == kInvalidArgSetId == 0.
182 ArgSetId id = static_cast<uint32_t>(arg_row_for_hash_.size()) + 1;
Lalit Maganti1f464742019-02-28 13:49:31 +0000183 arg_row_for_hash_.emplace(digest, args_count());
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000184 for (uint32_t i = begin; i < end; i++) {
185 const auto& arg = args[i];
186 set_ids_.emplace_back(id);
187 flat_keys_.emplace_back(arg.flat_key);
188 keys_.emplace_back(arg.key);
189 arg_values_.emplace_back(arg.value);
190 }
191 return id;
Lalit Maganti6e9c55e2018-11-29 12:00:39 +0000192 }
193
Lalit Maganti5ea9e932018-11-30 14:19:39 +0000194 private:
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000195 using ArgSetHash = uint64_t;
196
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000197 std::deque<ArgSetId> set_ids_;
Lalit Maganti5ea9e932018-11-30 14:19:39 +0000198 std::deque<StringId> flat_keys_;
199 std::deque<StringId> keys_;
Lalit Maganti1d915a62019-01-07 12:10:42 +0000200 std::deque<Variadic> arg_values_;
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000201
202 std::unordered_map<ArgSetHash, uint32_t> arg_row_for_hash_;
Lalit Maganti6e9c55e2018-11-29 12:00:39 +0000203 };
204
Lalit Magantiff69c112018-09-24 12:07:47 +0100205 class Slices {
Lalit Maganti35622b72018-06-06 12:03:11 +0100206 public:
Lalit Magantifde29042018-10-04 13:28:52 +0100207 inline size_t AddSlice(uint32_t cpu,
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000208 int64_t start_ns,
209 int64_t duration_ns,
Lalit Magantib0b53ee2019-01-24 17:53:39 +0000210 UniqueTid utid,
211 ftrace_utils::TaskState end_state,
212 int32_t priority) {
Lalit Magantiff69c112018-09-24 12:07:47 +0100213 cpus_.emplace_back(cpu);
Lalit Maganti35622b72018-06-06 12:03:11 +0100214 start_ns_.emplace_back(start_ns);
215 durations_.emplace_back(duration_ns);
Isabelle Taylora0a22972018-08-03 12:06:12 +0100216 utids_.emplace_back(utid);
Lalit Magantib0b53ee2019-01-24 17:53:39 +0000217 end_states_.emplace_back(end_state);
218 priorities_.emplace_back(priority);
Lalit Maganti58638932019-01-31 10:48:26 +0000219
220 if (utid >= rows_for_utids_.size())
221 rows_for_utids_.resize(utid + 1);
222 rows_for_utids_[utid].emplace_back(slice_count() - 1);
Lalit Magantifde29042018-10-04 13:28:52 +0100223 return slice_count() - 1;
224 }
225
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000226 void set_duration(size_t index, int64_t duration_ns) {
Lalit Magantifde29042018-10-04 13:28:52 +0100227 durations_[index] = duration_ns;
Lalit Maganti35622b72018-06-06 12:03:11 +0100228 }
229
Lalit Magantib0b53ee2019-01-24 17:53:39 +0000230 void set_end_state(size_t index, ftrace_utils::TaskState end_state) {
231 end_states_[index] = end_state;
232 }
233
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100234 size_t slice_count() const { return start_ns_.size(); }
Lalit Maganti35622b72018-06-06 12:03:11 +0100235
Lalit Magantiff69c112018-09-24 12:07:47 +0100236 const std::deque<uint32_t>& cpus() const { return cpus_; }
237
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000238 const std::deque<int64_t>& start_ns() const { return start_ns_; }
Lalit Maganti35622b72018-06-06 12:03:11 +0100239
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000240 const std::deque<int64_t>& durations() const { return durations_; }
Lalit Maganti35622b72018-06-06 12:03:11 +0100241
Isabelle Taylor68e42192018-06-19 16:19:31 +0100242 const std::deque<UniqueTid>& utids() const { return utids_; }
243
Lalit Magantib0b53ee2019-01-24 17:53:39 +0000244 const std::deque<ftrace_utils::TaskState>& end_state() const {
245 return end_states_;
246 }
247
248 const std::deque<int32_t>& priorities() const { return priorities_; }
249
Lalit Maganti58638932019-01-31 10:48:26 +0000250 const std::deque<std::vector<uint32_t>>& rows_for_utids() const {
Lalit Magantif0f09c32019-01-18 17:29:31 +0000251 return rows_for_utids_;
252 }
253
Lalit Maganti35622b72018-06-06 12:03:11 +0100254 private:
Hector Dearman947f12a2018-09-11 16:50:36 +0100255 // Each deque below has the same number of entries (the number of slices
Lalit Maganti35622b72018-06-06 12:03:11 +0100256 // in the trace for the CPU).
Lalit Magantiff69c112018-09-24 12:07:47 +0100257 std::deque<uint32_t> cpus_;
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000258 std::deque<int64_t> start_ns_;
259 std::deque<int64_t> durations_;
Isabelle Taylor68e42192018-06-19 16:19:31 +0100260 std::deque<UniqueTid> utids_;
Lalit Magantib0b53ee2019-01-24 17:53:39 +0000261 std::deque<ftrace_utils::TaskState> end_states_;
262 std::deque<int32_t> priorities_;
Lalit Maganti58638932019-01-31 10:48:26 +0000263
264 // One row per utid.
265 std::deque<std::vector<uint32_t>> rows_for_utids_;
Primiano Tucci0d72a312018-08-07 14:42:45 +0100266 };
Isabelle Taylor68e42192018-06-19 16:19:31 +0100267
Primiano Tucci0d72a312018-08-07 14:42:45 +0100268 class NestableSlices {
269 public:
Eric Seckler70cc4422019-05-28 16:00:23 +0100270 inline uint32_t AddSlice(int64_t start_ns,
271 int64_t duration_ns,
272 int64_t ref,
273 RefType type,
274 StringId cat,
275 StringId name,
276 uint8_t depth,
277 int64_t stack_id,
278 int64_t parent_stack_id) {
Primiano Tucci0d72a312018-08-07 14:42:45 +0100279 start_ns_.emplace_back(start_ns);
280 durations_.emplace_back(duration_ns);
Eric Seckler972225e2019-04-18 11:07:12 +0100281 refs_.emplace_back(ref);
282 types_.emplace_back(type);
Primiano Tucci0d72a312018-08-07 14:42:45 +0100283 cats_.emplace_back(cat);
284 names_.emplace_back(name);
285 depths_.emplace_back(depth);
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100286 stack_ids_.emplace_back(stack_id);
287 parent_stack_ids_.emplace_back(parent_stack_id);
Eric Seckler70cc4422019-05-28 16:00:23 +0100288 arg_set_ids_.emplace_back(kInvalidArgSetId);
Lalit Maganti7b37bbf2018-11-09 15:58:54 +0000289 return slice_count() - 1;
290 }
291
Eric Seckler70cc4422019-05-28 16:00:23 +0100292 void set_duration(uint32_t index, int64_t duration_ns) {
Lalit Maganti7b37bbf2018-11-09 15:58:54 +0000293 durations_[index] = duration_ns;
294 }
295
Eric Seckler70cc4422019-05-28 16:00:23 +0100296 void set_stack_id(uint32_t index, int64_t stack_id) {
Lalit Maganti7b37bbf2018-11-09 15:58:54 +0000297 stack_ids_[index] = stack_id;
Primiano Tucci0d72a312018-08-07 14:42:45 +0100298 }
299
Eric Seckler70cc4422019-05-28 16:00:23 +0100300 void set_arg_set_id(uint32_t index, ArgSetId id) {
301 arg_set_ids_[index] = id;
302 }
303
304 uint32_t slice_count() const {
305 return static_cast<uint32_t>(start_ns_.size());
306 }
307
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000308 const std::deque<int64_t>& start_ns() const { return start_ns_; }
309 const std::deque<int64_t>& durations() const { return durations_; }
Eric Seckler972225e2019-04-18 11:07:12 +0100310 const std::deque<int64_t>& refs() const { return refs_; }
311 const std::deque<RefType>& types() const { return types_; }
Primiano Tucci0d72a312018-08-07 14:42:45 +0100312 const std::deque<StringId>& cats() const { return cats_; }
313 const std::deque<StringId>& names() const { return names_; }
314 const std::deque<uint8_t>& depths() const { return depths_; }
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000315 const std::deque<int64_t>& stack_ids() const { return stack_ids_; }
316 const std::deque<int64_t>& parent_stack_ids() const {
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100317 return parent_stack_ids_;
318 }
Eric Seckler70cc4422019-05-28 16:00:23 +0100319 const std::deque<ArgSetId>& arg_set_ids() const { return arg_set_ids_; }
Primiano Tucci0d72a312018-08-07 14:42:45 +0100320
321 private:
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000322 std::deque<int64_t> start_ns_;
323 std::deque<int64_t> durations_;
Eric Seckler972225e2019-04-18 11:07:12 +0100324 std::deque<int64_t> refs_;
325 std::deque<RefType> types_;
Eric Seckler651ca312019-06-05 16:41:51 +0100326 // TODO(eseckler): Remove this column and store the category in the args
327 // table instead, similar to what we do for instant events.
Primiano Tucci0d72a312018-08-07 14:42:45 +0100328 std::deque<StringId> cats_;
329 std::deque<StringId> names_;
330 std::deque<uint8_t> depths_;
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000331 std::deque<int64_t> stack_ids_;
332 std::deque<int64_t> parent_stack_ids_;
Eric Seckler70cc4422019-05-28 16:00:23 +0100333 std::deque<ArgSetId> arg_set_ids_;
Lalit Maganti35622b72018-06-06 12:03:11 +0100334 };
335
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000336 class CounterDefinitions {
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100337 public:
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000338 using Id = uint32_t;
Lalit Maganti521d97b2019-04-29 13:47:03 +0100339 static constexpr Id kInvalidId = std::numeric_limits<Id>::max();
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000340
341 inline Id AddCounterDefinition(StringId name_id,
342 int64_t ref,
343 RefType type) {
344 base::Hash hash;
345 hash.Update(name_id);
346 hash.Update(ref);
347 hash.Update(type);
348
349 // TODO(lalitm): this is a perf bottleneck and likely we can do something
350 // quite a bit better here.
351 uint64_t digest = hash.digest();
352 auto it = hash_to_row_idx_.find(digest);
353 if (it != hash_to_row_idx_.end())
354 return it->second;
355
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100356 name_ids_.emplace_back(name_id);
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100357 refs_.emplace_back(ref);
358 types_.emplace_back(type);
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000359 hash_to_row_idx_.emplace(digest, size() - 1);
360 return size() - 1;
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100361 }
Lalit Magantifde29042018-10-04 13:28:52 +0100362
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000363 uint32_t size() const { return static_cast<uint32_t>(name_ids_.size()); }
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100364
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100365 const std::deque<StringId>& name_ids() const { return name_ids_; }
366
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100367 const std::deque<int64_t>& refs() const { return refs_; }
368
369 const std::deque<RefType>& types() const { return types_; }
370
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000371 private:
372 std::deque<StringId> name_ids_;
373 std::deque<int64_t> refs_;
374 std::deque<RefType> types_;
375
376 std::unordered_map<uint64_t, uint32_t> hash_to_row_idx_;
377 };
378
379 class CounterValues {
380 public:
381 inline uint32_t AddCounterValue(CounterDefinitions::Id counter_id,
382 int64_t timestamp,
383 double value) {
384 counter_ids_.emplace_back(counter_id);
385 timestamps_.emplace_back(timestamp);
386 values_.emplace_back(value);
387 arg_set_ids_.emplace_back(kInvalidArgSetId);
Ioannis Ilkosf129c262019-04-25 14:53:51 +0100388
Lalit Maganti521d97b2019-04-29 13:47:03 +0100389 if (counter_id != CounterDefinitions::kInvalidId) {
390 if (counter_id >= rows_for_counter_id_.size()) {
391 rows_for_counter_id_.resize(counter_id + 1);
392 }
393 rows_for_counter_id_[counter_id].emplace_back(size() - 1);
394 }
395 return size() - 1;
396 }
397
398 void set_counter_id(uint32_t index, CounterDefinitions::Id counter_id) {
399 PERFETTO_DCHECK(counter_ids_[index] == CounterDefinitions::kInvalidId);
400
401 counter_ids_[index] = counter_id;
Ioannis Ilkosf129c262019-04-25 14:53:51 +0100402 if (counter_id >= rows_for_counter_id_.size()) {
403 rows_for_counter_id_.resize(counter_id + 1);
404 }
Lalit Maganti521d97b2019-04-29 13:47:03 +0100405
406 auto* new_rows = &rows_for_counter_id_[counter_id];
407 new_rows->insert(
408 std::upper_bound(new_rows->begin(), new_rows->end(), index), index);
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000409 }
410
411 void set_arg_set_id(uint32_t row, ArgSetId id) { arg_set_ids_[row] = id; }
412
413 uint32_t size() const { return static_cast<uint32_t>(counter_ids_.size()); }
414
415 const std::deque<CounterDefinitions::Id>& counter_ids() const {
416 return counter_ids_;
417 }
418
419 const std::deque<int64_t>& timestamps() const { return timestamps_; }
420
421 const std::deque<double>& values() const { return values_; }
422
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000423 const std::deque<ArgSetId>& arg_set_ids() const { return arg_set_ids_; }
424
Ioannis Ilkosf129c262019-04-25 14:53:51 +0100425 const std::deque<std::vector<uint32_t>>& rows_for_counter_id() const {
426 return rows_for_counter_id_;
427 }
428
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100429 private:
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000430 std::deque<CounterDefinitions::Id> counter_ids_;
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000431 std::deque<int64_t> timestamps_;
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100432 std::deque<double> values_;
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000433 std::deque<ArgSetId> arg_set_ids_;
Ioannis Ilkosf129c262019-04-25 14:53:51 +0100434
435 // Indexed by counter_id value and contains the row numbers corresponding to
436 // it.
437 std::deque<std::vector<uint32_t>> rows_for_counter_id_;
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100438 };
439
Primiano Tucci5cb84f82018-10-31 21:46:36 -0700440 class SqlStats {
441 public:
442 static constexpr size_t kMaxLogEntries = 100;
Lalit Magantiaac2f652019-04-30 12:16:21 +0100443 uint32_t RecordQueryBegin(const std::string& query,
444 int64_t time_queued,
445 int64_t time_started);
446 void RecordQueryFirstNext(uint32_t row, int64_t time_first_next);
447 void RecordQueryEnd(uint32_t row, int64_t time_end);
Primiano Tucci5cb84f82018-10-31 21:46:36 -0700448 size_t size() const { return queries_.size(); }
449 const std::deque<std::string>& queries() const { return queries_; }
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000450 const std::deque<int64_t>& times_queued() const { return times_queued_; }
451 const std::deque<int64_t>& times_started() const { return times_started_; }
Lalit Magantiaac2f652019-04-30 12:16:21 +0100452 const std::deque<int64_t>& times_first_next() const {
453 return times_first_next_;
454 }
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000455 const std::deque<int64_t>& times_ended() const { return times_ended_; }
Primiano Tucci5cb84f82018-10-31 21:46:36 -0700456
457 private:
Lalit Magantiaac2f652019-04-30 12:16:21 +0100458 uint32_t popped_queries_ = 0;
459
Primiano Tucci5cb84f82018-10-31 21:46:36 -0700460 std::deque<std::string> queries_;
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000461 std::deque<int64_t> times_queued_;
462 std::deque<int64_t> times_started_;
Lalit Magantiaac2f652019-04-30 12:16:21 +0100463 std::deque<int64_t> times_first_next_;
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000464 std::deque<int64_t> times_ended_;
Primiano Tucci5cb84f82018-10-31 21:46:36 -0700465 };
466
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000467 class Instants {
468 public:
Lalit Maganti66ed7ad2019-01-11 16:47:26 +0000469 inline uint32_t AddInstantEvent(int64_t timestamp,
470 StringId name_id,
471 double value,
472 int64_t ref,
473 RefType type) {
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000474 timestamps_.emplace_back(timestamp);
475 name_ids_.emplace_back(name_id);
476 values_.emplace_back(value);
477 refs_.emplace_back(ref);
478 types_.emplace_back(type);
Lalit Maganti1c21d172019-02-07 10:48:24 +0000479 arg_set_ids_.emplace_back(kInvalidArgSetId);
Lalit Maganti66ed7ad2019-01-11 16:47:26 +0000480 return static_cast<uint32_t>(instant_count() - 1);
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000481 }
482
Lalit Maganti521d97b2019-04-29 13:47:03 +0100483 void set_ref(uint32_t row, int64_t ref) { refs_[row] = ref; }
484
Lalit Maganti1c21d172019-02-07 10:48:24 +0000485 void set_arg_set_id(uint32_t row, ArgSetId id) { arg_set_ids_[row] = id; }
486
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000487 size_t instant_count() const { return timestamps_.size(); }
488
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000489 const std::deque<int64_t>& timestamps() const { return timestamps_; }
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000490
491 const std::deque<StringId>& name_ids() const { return name_ids_; }
492
493 const std::deque<double>& values() const { return values_; }
494
495 const std::deque<int64_t>& refs() const { return refs_; }
496
497 const std::deque<RefType>& types() const { return types_; }
498
Lalit Maganti1c21d172019-02-07 10:48:24 +0000499 const std::deque<ArgSetId>& arg_set_ids() const { return arg_set_ids_; }
500
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000501 private:
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000502 std::deque<int64_t> timestamps_;
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000503 std::deque<StringId> name_ids_;
504 std::deque<double> values_;
505 std::deque<int64_t> refs_;
506 std::deque<RefType> types_;
Lalit Maganti1c21d172019-02-07 10:48:24 +0000507 std::deque<ArgSetId> arg_set_ids_;
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000508 };
509
Lalit Maganti1d915a62019-01-07 12:10:42 +0000510 class RawEvents {
511 public:
512 inline RowId AddRawEvent(int64_t timestamp,
513 StringId name_id,
Lalit Magantie87cc812019-01-10 15:20:06 +0000514 uint32_t cpu,
Lalit Maganti1d915a62019-01-07 12:10:42 +0000515 UniqueTid utid) {
516 timestamps_.emplace_back(timestamp);
517 name_ids_.emplace_back(name_id);
Lalit Magantie87cc812019-01-10 15:20:06 +0000518 cpus_.emplace_back(cpu);
Lalit Maganti1d915a62019-01-07 12:10:42 +0000519 utids_.emplace_back(utid);
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000520 arg_set_ids_.emplace_back(kInvalidArgSetId);
Lalit Maganti1d915a62019-01-07 12:10:42 +0000521 return CreateRowId(TableId::kRawEvents,
522 static_cast<uint32_t>(raw_event_count() - 1));
523 }
524
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000525 void set_arg_set_id(uint32_t row, ArgSetId id) { arg_set_ids_[row] = id; }
526
Lalit Maganti1d915a62019-01-07 12:10:42 +0000527 size_t raw_event_count() const { return timestamps_.size(); }
528
529 const std::deque<int64_t>& timestamps() const { return timestamps_; }
530
531 const std::deque<StringId>& name_ids() const { return name_ids_; }
532
Lalit Magantie87cc812019-01-10 15:20:06 +0000533 const std::deque<uint32_t>& cpus() const { return cpus_; }
534
Lalit Maganti1d915a62019-01-07 12:10:42 +0000535 const std::deque<UniqueTid>& utids() const { return utids_; }
536
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000537 const std::deque<ArgSetId>& arg_set_ids() const { return arg_set_ids_; }
538
Lalit Maganti1d915a62019-01-07 12:10:42 +0000539 private:
540 std::deque<int64_t> timestamps_;
541 std::deque<StringId> name_ids_;
Lalit Magantie87cc812019-01-10 15:20:06 +0000542 std::deque<uint32_t> cpus_;
Lalit Maganti1d915a62019-01-07 12:10:42 +0000543 std::deque<UniqueTid> utids_;
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000544 std::deque<ArgSetId> arg_set_ids_;
Lalit Maganti1d915a62019-01-07 12:10:42 +0000545 };
546
Primiano Tucci2c761ef2019-01-07 20:20:46 +0000547 class AndroidLogs {
548 public:
549 inline size_t AddLogEvent(int64_t timestamp,
550 UniqueTid utid,
551 uint8_t prio,
552 StringId tag_id,
553 StringId msg_id) {
554 timestamps_.emplace_back(timestamp);
555 utids_.emplace_back(utid);
556 prios_.emplace_back(prio);
557 tag_ids_.emplace_back(tag_id);
558 msg_ids_.emplace_back(msg_id);
559 return size() - 1;
560 }
561
562 size_t size() const { return timestamps_.size(); }
563
564 const std::deque<int64_t>& timestamps() const { return timestamps_; }
565 const std::deque<UniqueTid>& utids() const { return utids_; }
566 const std::deque<uint8_t>& prios() const { return prios_; }
567 const std::deque<StringId>& tag_ids() const { return tag_ids_; }
568 const std::deque<StringId>& msg_ids() const { return msg_ids_; }
569
570 private:
571 std::deque<int64_t> timestamps_;
572 std::deque<UniqueTid> utids_;
573 std::deque<uint8_t> prios_;
574 std::deque<StringId> tag_ids_;
575 std::deque<StringId> msg_ids_;
576 };
577
Primiano Tucci0e38a142019-01-07 20:51:09 +0000578 struct Stats {
579 using IndexMap = std::map<int, int64_t>;
580 int64_t value = 0;
581 IndexMap indexed_values;
582 };
583 using StatsMap = std::array<Stats, stats::kNumKeys>;
584
Mikhail Khokhlove466c002019-05-23 13:33:33 +0100585 using MetadataMap = std::array<std::vector<Variadic>, metadata::kNumKeys>;
586
Florian Mayer438b5ab2019-05-02 11:18:06 +0100587 class HeapProfileFrames {
588 public:
Florian Mayerbee52132019-05-02 13:59:56 +0100589 struct Row {
590 StringId name_id;
591 int64_t mapping_row;
592 int64_t rel_pc;
Florian Mayer438b5ab2019-05-02 11:18:06 +0100593
Florian Mayerbee52132019-05-02 13:59:56 +0100594 bool operator==(const Row& other) const {
595 return std::tie(name_id, mapping_row, rel_pc) ==
596 std::tie(other.name_id, other.mapping_row, other.rel_pc);
597 }
598 };
599
Lalit Maganti9b2d52b2019-05-07 14:32:15 +0100600 uint32_t size() const { return static_cast<uint32_t>(names_.size()); }
601
Florian Mayerbee52132019-05-02 13:59:56 +0100602 int64_t Insert(const Row& row) {
603 names_.emplace_back(row.name_id);
604 mappings_.emplace_back(row.mapping_row);
605 rel_pcs_.emplace_back(row.rel_pc);
Florian Mayer438b5ab2019-05-02 11:18:06 +0100606 return static_cast<int64_t>(names_.size()) - 1;
607 }
608
609 const std::deque<StringId>& names() const { return names_; }
610 const std::deque<int64_t>& mappings() const { return mappings_; }
611 const std::deque<int64_t>& rel_pcs() const { return rel_pcs_; }
612
613 private:
614 std::deque<StringId> names_;
615 std::deque<int64_t> mappings_;
616 std::deque<int64_t> rel_pcs_;
617 };
618
619 class HeapProfileCallsites {
620 public:
Florian Mayerbee52132019-05-02 13:59:56 +0100621 struct Row {
622 int64_t depth;
623 int64_t parent_id;
624 int64_t frame_row;
625
626 bool operator==(const Row& other) const {
627 return std::tie(depth, parent_id, frame_row) ==
628 std::tie(other.depth, other.parent_id, other.frame_row);
Florian Mayer438b5ab2019-05-02 11:18:06 +0100629 }
Florian Mayerbee52132019-05-02 13:59:56 +0100630 };
631
Lalit Maganti9b2d52b2019-05-07 14:32:15 +0100632 uint32_t size() const { return static_cast<uint32_t>(frame_ids_.size()); }
633
Florian Mayerbee52132019-05-02 13:59:56 +0100634 int64_t Insert(const Row& row) {
635 frame_depths_.emplace_back(row.depth);
636 parent_callsite_ids_.emplace_back(row.parent_id);
637 frame_ids_.emplace_back(row.frame_row);
Florian Mayer438b5ab2019-05-02 11:18:06 +0100638 return static_cast<int64_t>(frame_depths_.size()) - 1;
639 }
640
641 const std::deque<int64_t>& frame_depths() const { return frame_depths_; }
642 const std::deque<int64_t>& parent_callsite_ids() const {
643 return parent_callsite_ids_;
644 }
645 const std::deque<int64_t>& frame_ids() const { return frame_ids_; }
646
647 private:
648 std::deque<int64_t> frame_depths_;
649 std::deque<int64_t> parent_callsite_ids_;
650 std::deque<int64_t> frame_ids_;
651 };
652
653 class HeapProfileMappings {
654 public:
Florian Mayerbee52132019-05-02 13:59:56 +0100655 struct Row {
656 StringId build_id;
657 int64_t offset;
658 int64_t start;
659 int64_t end;
660 int64_t load_bias;
661 StringId name_id;
Florian Mayer438b5ab2019-05-02 11:18:06 +0100662
Florian Mayerbee52132019-05-02 13:59:56 +0100663 bool operator==(const Row& other) const {
664 return std::tie(build_id, offset, start, end, load_bias, name_id) ==
665 std::tie(other.build_id, other.offset, other.start, other.end,
666 other.load_bias, other.name_id);
667 }
668 };
669
Lalit Maganti9b2d52b2019-05-07 14:32:15 +0100670 uint32_t size() const { return static_cast<uint32_t>(names_.size()); }
671
Florian Mayerbee52132019-05-02 13:59:56 +0100672 int64_t Insert(const Row& row) {
673 build_ids_.emplace_back(row.build_id);
674 offsets_.emplace_back(row.offset);
675 starts_.emplace_back(row.start);
676 ends_.emplace_back(row.end);
677 load_biases_.emplace_back(row.load_bias);
678 names_.emplace_back(row.name_id);
Florian Mayer438b5ab2019-05-02 11:18:06 +0100679 return static_cast<int64_t>(build_ids_.size()) - 1;
680 }
681
682 const std::deque<StringId>& build_ids() const { return build_ids_; }
683 const std::deque<int64_t>& offsets() const { return offsets_; }
684 const std::deque<int64_t>& starts() const { return starts_; }
685 const std::deque<int64_t>& ends() const { return ends_; }
686 const std::deque<int64_t>& load_biases() const { return load_biases_; }
687 const std::deque<StringId>& names() const { return names_; }
688
689 private:
690 std::deque<StringId> build_ids_;
691 std::deque<int64_t> offsets_;
692 std::deque<int64_t> starts_;
693 std::deque<int64_t> ends_;
694 std::deque<int64_t> load_biases_;
695 std::deque<StringId> names_;
696 };
697
698 class HeapProfileAllocations {
699 public:
Florian Mayerbee52132019-05-02 13:59:56 +0100700 struct Row {
701 int64_t timestamp;
Florian Mayerdf1968c2019-05-13 16:39:35 +0100702 UniquePid upid;
Florian Mayerbee52132019-05-02 13:59:56 +0100703 int64_t callsite_id;
704 int64_t count;
705 int64_t size;
706 };
707
Lalit Maganti9b2d52b2019-05-07 14:32:15 +0100708 uint32_t size() const { return static_cast<uint32_t>(timestamps_.size()); }
709
Florian Mayerbee52132019-05-02 13:59:56 +0100710 void Insert(const Row& row) {
711 timestamps_.emplace_back(row.timestamp);
Florian Mayerdf1968c2019-05-13 16:39:35 +0100712 upids_.emplace_back(row.upid);
Florian Mayerbee52132019-05-02 13:59:56 +0100713 callsite_ids_.emplace_back(row.callsite_id);
714 counts_.emplace_back(row.count);
715 sizes_.emplace_back(row.size);
Florian Mayer438b5ab2019-05-02 11:18:06 +0100716 }
717
718 const std::deque<int64_t>& timestamps() const { return timestamps_; }
Florian Mayerdf1968c2019-05-13 16:39:35 +0100719 const std::deque<UniquePid>& upids() const { return upids_; }
Florian Mayer438b5ab2019-05-02 11:18:06 +0100720 const std::deque<int64_t>& callsite_ids() const { return callsite_ids_; }
721 const std::deque<int64_t>& counts() const { return counts_; }
722 const std::deque<int64_t>& sizes() const { return sizes_; }
723
724 private:
725 std::deque<int64_t> timestamps_;
Florian Mayerdf1968c2019-05-13 16:39:35 +0100726 std::deque<UniquePid> upids_;
Florian Mayer438b5ab2019-05-02 11:18:06 +0100727 std::deque<int64_t> callsite_ids_;
728 std::deque<int64_t> counts_;
729 std::deque<int64_t> sizes_;
730 };
731
Isabelle Taylora0a22972018-08-03 12:06:12 +0100732 void ResetStorage();
Lalit Maganti35622b72018-06-06 12:03:11 +0100733
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100734 UniqueTid AddEmptyThread(uint32_t tid) {
735 unique_threads_.emplace_back(tid);
Isabelle Taylora0a22972018-08-03 12:06:12 +0100736 return static_cast<UniqueTid>(unique_threads_.size() - 1);
737 }
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100738
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100739 UniquePid AddEmptyProcess(uint32_t pid) {
740 unique_processes_.emplace_back(pid);
Isabelle Taylora0a22972018-08-03 12:06:12 +0100741 return static_cast<UniquePid>(unique_processes_.size() - 1);
742 }
Isabelle Taylor3dd366c2018-06-22 16:21:41 +0100743
Isabelle Taylora0a22972018-08-03 12:06:12 +0100744 // Return an unqiue identifier for the contents of each string.
745 // The string is copied internally and can be destroyed after this called.
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100746 // Virtual for testing.
Lalit Maganti5c454312019-04-08 12:11:17 +0100747 virtual StringId InternString(base::StringView str) {
748 return string_pool_.InternString(str);
749 }
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100750
Isabelle Taylora0a22972018-08-03 12:06:12 +0100751 Process* GetMutableProcess(UniquePid upid) {
Lalit Maganti676658b2018-11-20 18:24:31 +0000752 PERFETTO_DCHECK(upid < unique_processes_.size());
Isabelle Taylora0a22972018-08-03 12:06:12 +0100753 return &unique_processes_[upid];
754 }
Isabelle Taylor3dd366c2018-06-22 16:21:41 +0100755
Isabelle Taylora0a22972018-08-03 12:06:12 +0100756 Thread* GetMutableThread(UniqueTid utid) {
Florian Mayera1aaec22018-09-19 17:02:26 +0100757 PERFETTO_DCHECK(utid < unique_threads_.size());
Isabelle Taylora0a22972018-08-03 12:06:12 +0100758 return &unique_threads_[utid];
759 }
Isabelle Taylor3dd366c2018-06-22 16:21:41 +0100760
Primiano Tucci0e38a142019-01-07 20:51:09 +0000761 // Example usage: SetStats(stats::android_log_num_failed, 42);
762 void SetStats(size_t key, int64_t value) {
763 PERFETTO_DCHECK(key < stats::kNumKeys);
764 PERFETTO_DCHECK(stats::kTypes[key] == stats::kSingle);
765 stats_[key].value = value;
766 }
767
768 // Example usage: IncrementStats(stats::android_log_num_failed, -1);
769 void IncrementStats(size_t key, int64_t increment = 1) {
770 PERFETTO_DCHECK(key < stats::kNumKeys);
771 PERFETTO_DCHECK(stats::kTypes[key] == stats::kSingle);
772 stats_[key].value += increment;
773 }
774
Florian Mayer438b5ab2019-05-02 11:18:06 +0100775 // Example usage: IncrementIndexedStats(stats::cpu_failure, 1);
776 void IncrementIndexedStats(size_t key, int index, int64_t increment = 1) {
777 PERFETTO_DCHECK(key < stats::kNumKeys);
778 PERFETTO_DCHECK(stats::kTypes[key] == stats::kIndexed);
779 stats_[key].indexed_values[index] += increment;
780 }
781
Primiano Tucci0e38a142019-01-07 20:51:09 +0000782 // Example usage: SetIndexedStats(stats::cpu_failure, 1, 42);
783 void SetIndexedStats(size_t key, int index, int64_t value) {
784 PERFETTO_DCHECK(key < stats::kNumKeys);
785 PERFETTO_DCHECK(stats::kTypes[key] == stats::kIndexed);
786 stats_[key].indexed_values[index] = value;
787 }
788
Mikhail Khokhlove466c002019-05-23 13:33:33 +0100789 // Example usage:
790 // SetMetadata(metadata::benchmark_name,
791 // Variadic::String(storage->InternString("foo"));
Mikhail Khokhlovb1fe42a2019-05-23 13:58:05 +0100792 // Virtual for testing.
793 virtual void SetMetadata(size_t key, Variadic value) {
Mikhail Khokhlove466c002019-05-23 13:33:33 +0100794 PERFETTO_DCHECK(key < metadata::kNumKeys);
795 PERFETTO_DCHECK(metadata::kKeyTypes[key] == metadata::kSingle);
796 PERFETTO_DCHECK(value.type == metadata::kValueTypes[key]);
797 metadata_[key] = {value};
798 }
799
800 // Example usage:
801 // AppendMetadata(metadata::benchmark_story_tags,
802 // Variadic::String(storage->InternString("bar"));
Mikhail Khokhlovb1fe42a2019-05-23 13:58:05 +0100803 // Virtual for testing.
804 virtual void AppendMetadata(size_t key, Variadic value) {
Mikhail Khokhlove466c002019-05-23 13:33:33 +0100805 PERFETTO_DCHECK(key < metadata::kNumKeys);
806 PERFETTO_DCHECK(metadata::kKeyTypes[key] == metadata::kMulti);
807 PERFETTO_DCHECK(value.type == metadata::kValueTypes[key]);
808 metadata_[key].push_back(value);
809 }
810
Eric Seckler77b52782019-05-02 15:18:57 +0100811 class ScopedStatsTracer {
812 public:
813 ScopedStatsTracer(TraceStorage* storage, size_t key)
814 : storage_(storage), key_(key), start_ns_(base::GetWallTimeNs()) {}
815
816 ~ScopedStatsTracer() {
817 if (!storage_)
818 return;
819 auto delta_ns = base::GetWallTimeNs() - start_ns_;
820 storage_->IncrementStats(key_, delta_ns.count());
821 }
822
823 ScopedStatsTracer(ScopedStatsTracer&& other) noexcept { MoveImpl(&other); }
824
825 ScopedStatsTracer& operator=(ScopedStatsTracer&& other) {
826 MoveImpl(&other);
827 return *this;
828 }
829
830 private:
831 ScopedStatsTracer(const ScopedStatsTracer&) = delete;
832 ScopedStatsTracer& operator=(const ScopedStatsTracer&) = delete;
833
834 void MoveImpl(ScopedStatsTracer* other) {
835 storage_ = other->storage_;
836 key_ = other->key_;
837 start_ns_ = other->start_ns_;
838 other->storage_ = nullptr;
839 }
840
841 TraceStorage* storage_;
842 size_t key_;
843 base::TimeNanos start_ns_;
844 };
845
846 ScopedStatsTracer TraceExecutionTimeIntoStats(size_t key) {
847 return ScopedStatsTracer(this, key);
848 }
849
Lalit Maganticaed37e2018-06-01 03:03:08 +0100850 // Reading methods.
Eric Secklerc93823e2019-06-03 16:49:19 +0100851 // Virtual for testing.
852 virtual NullTermStringView GetString(StringId id) const {
Lalit Maganti5c454312019-04-08 12:11:17 +0100853 return string_pool_.Get(id);
Isabelle Taylora0a22972018-08-03 12:06:12 +0100854 }
855
Lalit Magantie9d40532018-06-29 13:15:06 +0100856 const Process& GetProcess(UniquePid upid) const {
Lalit Maganti676658b2018-11-20 18:24:31 +0000857 PERFETTO_DCHECK(upid < unique_processes_.size());
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100858 return unique_processes_[upid];
859 }
860
Lalit Magantie9d40532018-06-29 13:15:06 +0100861 const Thread& GetThread(UniqueTid utid) const {
Lalit Maganti1f64cfa2018-09-18 13:20:02 +0100862 // Allow utid == 0 for idle thread retrieval.
Florian Mayera1aaec22018-09-19 17:02:26 +0100863 PERFETTO_DCHECK(utid < unique_threads_.size());
Isabelle Taylor68e42192018-06-19 16:19:31 +0100864 return unique_threads_[utid];
865 }
866
Lalit Maganti5ea9e932018-11-30 14:19:39 +0000867 static RowId CreateRowId(TableId table, uint32_t row) {
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000868 return (static_cast<RowId>(table) << kRowIdTableShift) | row;
Lalit Maganti5ea9e932018-11-30 14:19:39 +0000869 }
870
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000871 static std::pair<int8_t /*table*/, uint32_t /*row*/> ParseRowId(RowId rowid) {
872 auto id = static_cast<uint64_t>(rowid);
873 auto table_id = static_cast<uint8_t>(id >> kRowIdTableShift);
874 auto row = static_cast<uint32_t>(id & ((1ull << kRowIdTableShift) - 1));
875 return std::make_pair(table_id, row);
876 }
877
Lalit Magantiff69c112018-09-24 12:07:47 +0100878 const Slices& slices() const { return slices_; }
Lalit Magantifde29042018-10-04 13:28:52 +0100879 Slices* mutable_slices() { return &slices_; }
880
Primiano Tucci0d72a312018-08-07 14:42:45 +0100881 const NestableSlices& nestable_slices() const { return nestable_slices_; }
882 NestableSlices* mutable_nestable_slices() { return &nestable_slices_; }
883
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000884 const CounterDefinitions& counter_definitions() const {
885 return counter_definitions_;
886 }
887 CounterDefinitions* mutable_counter_definitions() {
888 return &counter_definitions_;
889 }
890
891 const CounterValues& counter_values() const { return counter_values_; }
892 CounterValues* mutable_counter_values() { return &counter_values_; }
Isabelle Taylor14674d42018-09-07 11:33:11 +0100893
Primiano Tucci5cb84f82018-10-31 21:46:36 -0700894 const SqlStats& sql_stats() const { return sql_stats_; }
895 SqlStats* mutable_sql_stats() { return &sql_stats_; }
896
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000897 const Instants& instants() const { return instants_; }
898 Instants* mutable_instants() { return &instants_; }
899
Primiano Tucci2c761ef2019-01-07 20:20:46 +0000900 const AndroidLogs& android_logs() const { return android_log_; }
901 AndroidLogs* mutable_android_log() { return &android_log_; }
902
Primiano Tucci0e38a142019-01-07 20:51:09 +0000903 const StatsMap& stats() const { return stats_; }
Lalit Maganti05e8c132018-11-09 18:16:12 +0000904
Mikhail Khokhlove466c002019-05-23 13:33:33 +0100905 const MetadataMap& metadata() const { return metadata_; }
906
Lalit Maganti6e9c55e2018-11-29 12:00:39 +0000907 const Args& args() const { return args_; }
908 Args* mutable_args() { return &args_; }
909
Lalit Maganti1d915a62019-01-07 12:10:42 +0000910 const RawEvents& raw_events() const { return raw_events_; }
911 RawEvents* mutable_raw_events() { return &raw_events_; }
912
Florian Mayer438b5ab2019-05-02 11:18:06 +0100913 const HeapProfileMappings& heap_profile_mappings() const {
914 return heap_profile_mappings_;
915 }
916 HeapProfileMappings* mutable_heap_profile_mappings() {
917 return &heap_profile_mappings_;
918 }
919
920 const HeapProfileFrames& heap_profile_frames() const {
921 return heap_profile_frames_;
922 }
923 HeapProfileFrames* mutable_heap_profile_frames() {
924 return &heap_profile_frames_;
925 }
926
927 const HeapProfileCallsites& heap_profile_callsites() const {
928 return heap_profile_callsites_;
929 }
930 HeapProfileCallsites* mutable_heap_profile_callsites() {
931 return &heap_profile_callsites_;
932 }
933
934 const HeapProfileAllocations& heap_profile_allocations() const {
935 return heap_profile_allocations_;
936 }
937 HeapProfileAllocations* mutable_heap_profile_allocations() {
938 return &heap_profile_allocations_;
939 }
940
Lalit Maganti5c454312019-04-08 12:11:17 +0100941 const StringPool& string_pool() const { return string_pool_; }
Lalit Magantiacda68b2018-10-29 15:23:25 +0000942
Isabelle Taylore7003fb2018-07-17 11:39:01 +0100943 // |unique_processes_| always contains at least 1 element becuase the 0th ID
944 // is reserved to indicate an invalid process.
Lalit Maganti9c6395b2019-01-17 00:25:09 +0000945 size_t process_count() const { return unique_processes_.size(); }
Primiano Tucci0d72a312018-08-07 14:42:45 +0100946
Isabelle Taylore7003fb2018-07-17 11:39:01 +0100947 // |unique_threads_| always contains at least 1 element becuase the 0th ID
948 // is reserved to indicate an invalid thread.
Lalit Maganti9c6395b2019-01-17 00:25:09 +0000949 size_t thread_count() const { return unique_threads_.size(); }
Isabelle Taylore7003fb2018-07-17 11:39:01 +0100950
Hector Dearman12323362018-08-09 16:09:28 +0100951 // Number of interned strings in the pool. Includes the empty string w/ ID=0.
952 size_t string_count() const { return string_pool_.size(); }
953
Ioannis Ilkosb8b11102019-01-29 17:56:55 +0000954 // Start / end ts (in nanoseconds) across the parsed trace events.
955 // Returns (0, 0) if the trace is empty.
956 std::pair<int64_t, int64_t> GetTraceTimestampBoundsNs() const;
957
Lalit Maganticaed37e2018-06-01 03:03:08 +0100958 private:
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000959 static constexpr uint8_t kRowIdTableShift = 32;
Isabelle Taylora0a22972018-08-03 12:06:12 +0100960
Primiano Tucci2da5d2e2018-08-10 14:23:31 +0100961 using StringHash = uint64_t;
Lalit Maganticaed37e2018-06-01 03:03:08 +0100962
Lalit Maganti5c454312019-04-08 12:11:17 +0100963 TraceStorage(const TraceStorage&) = delete;
964 TraceStorage& operator=(const TraceStorage&) = delete;
965
966 TraceStorage(TraceStorage&&) = default;
967 TraceStorage& operator=(TraceStorage&&) = default;
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000968
Lalit Maganti05e8c132018-11-09 18:16:12 +0000969 // Stats about parsing the trace.
Primiano Tucci0e38a142019-01-07 20:51:09 +0000970 StatsMap stats_{};
Lalit Maganti35622b72018-06-06 12:03:11 +0100971
Mikhail Khokhlove466c002019-05-23 13:33:33 +0100972 // Trace metadata from chrome and benchmarking infrastructure.
973 MetadataMap metadata_{};
974
Lalit Maganticaed37e2018-06-01 03:03:08 +0100975 // One entry for each CPU in the trace.
Lalit Magantiff69c112018-09-24 12:07:47 +0100976 Slices slices_;
Lalit Maganticaed37e2018-06-01 03:03:08 +0100977
Lalit Maganti6e9c55e2018-11-29 12:00:39 +0000978 // Args for all other tables.
979 Args args_;
980
Lalit Maganticaed37e2018-06-01 03:03:08 +0100981 // One entry for each unique string in the trace.
Lalit Maganti5c454312019-04-08 12:11:17 +0100982 StringPool string_pool_;
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100983
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100984 // One entry for each UniquePid, with UniquePid as the index.
Ioannis Ilkos9c03cbd2019-03-12 18:30:43 +0000985 // Never hold on to pointers to Process, as vector resize will
986 // invalidate them.
987 std::vector<Process> unique_processes_;
Isabelle Taylor68e42192018-06-19 16:19:31 +0100988
Isabelle Taylor68e42192018-06-19 16:19:31 +0100989 // One entry for each UniqueTid, with UniqueTid as the index.
Isabelle Taylor3dd366c2018-06-22 16:21:41 +0100990 std::deque<Thread> unique_threads_;
Primiano Tucci0d72a312018-08-07 14:42:45 +0100991
992 // Slices coming from userspace events (e.g. Chromium TRACE_EVENT macros).
993 NestableSlices nestable_slices_;
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100994
Hector Dearman2442d612019-06-04 18:05:23 +0100995 // The type of counters in the trace. Can be thought of as the "metadata".
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000996 CounterDefinitions counter_definitions_;
997
998 // The values from the Counter events from the trace. This includes CPU
999 // frequency events as well systrace trace_marker counter events.
1000 CounterValues counter_values_;
Primiano Tucci5cb84f82018-10-31 21:46:36 -07001001
1002 SqlStats sql_stats_;
Lalit Maganti6e9c55e2018-11-29 12:00:39 +00001003
Isabelle Taylorc8c11202018-11-05 11:36:22 +00001004 // These are instantaneous events in the trace. They have no duration
1005 // and do not have a value that make sense to track over time.
1006 // e.g. signal events
1007 Instants instants_;
Lalit Maganti1d915a62019-01-07 12:10:42 +00001008
1009 // Raw events are every ftrace event in the trace. The raw event includes
1010 // the timestamp and the pid. The args for the raw event will be in the
1011 // args table. This table can be used to generate a text version of the
1012 // trace.
1013 RawEvents raw_events_;
Primiano Tucci2c761ef2019-01-07 20:20:46 +00001014 AndroidLogs android_log_;
Florian Mayer438b5ab2019-05-02 11:18:06 +01001015
1016 HeapProfileMappings heap_profile_mappings_;
1017 HeapProfileFrames heap_profile_frames_;
1018 HeapProfileCallsites heap_profile_callsites_;
1019 HeapProfileAllocations heap_profile_allocations_;
Lalit Maganticaed37e2018-06-01 03:03:08 +01001020};
1021
1022} // namespace trace_processor
1023} // namespace perfetto
1024
Florian Mayerbee52132019-05-02 13:59:56 +01001025namespace std {
1026
1027template <>
1028struct hash<::perfetto::trace_processor::TraceStorage::HeapProfileFrames::Row> {
1029 using argument_type =
1030 ::perfetto::trace_processor::TraceStorage::HeapProfileFrames::Row;
1031 using result_type = size_t;
1032
1033 result_type operator()(const argument_type& r) const {
1034 return std::hash<::perfetto::trace_processor::StringId>{}(r.name_id) ^
1035 std::hash<int64_t>{}(r.mapping_row) ^ std::hash<int64_t>{}(r.rel_pc);
1036 }
1037};
1038
1039template <>
1040struct hash<
1041 ::perfetto::trace_processor::TraceStorage::HeapProfileCallsites::Row> {
1042 using argument_type =
1043 ::perfetto::trace_processor::TraceStorage::HeapProfileCallsites::Row;
1044 using result_type = size_t;
1045
1046 result_type operator()(const argument_type& r) const {
1047 return std::hash<int64_t>{}(r.depth) ^ std::hash<int64_t>{}(r.parent_id) ^
1048 std::hash<int64_t>{}(r.frame_row);
1049 }
1050};
1051
1052template <>
1053struct hash<
1054 ::perfetto::trace_processor::TraceStorage::HeapProfileMappings::Row> {
1055 using argument_type =
1056 ::perfetto::trace_processor::TraceStorage::HeapProfileMappings::Row;
1057 using result_type = size_t;
1058
1059 result_type operator()(const argument_type& r) const {
1060 return std::hash<::perfetto::trace_processor::StringId>{}(r.build_id) ^
1061 std::hash<int64_t>{}(r.offset) ^ std::hash<int64_t>{}(r.start) ^
1062 std::hash<int64_t>{}(r.end) ^ std::hash<int64_t>{}(r.load_bias) ^
1063 std::hash<::perfetto::trace_processor::StringId>{}(r.name_id);
1064 }
1065};
1066
1067} // namespace std
1068
Lalit Maganticaed37e2018-06-01 03:03:08 +01001069#endif // SRC_TRACE_PROCESSOR_TRACE_STORAGE_H_