blob: 3a5455b78491e4f7c378ac2f48f25af9f6f6d640 [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"
Eric Seckler83dcc8c2019-08-21 12:18:43 +010029#include "perfetto/base/time.h"
Primiano Tucci2c5488f2019-06-01 03:27:28 +010030#include "perfetto/ext/base/hash.h"
31#include "perfetto/ext/base/optional.h"
32#include "perfetto/ext/base/string_view.h"
Primiano Tucci2c5488f2019-06-01 03:27:28 +010033#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"
Lalit Maganti20539c22019-09-04 12:36:10 +010038#include "src/trace_processor/tables/slice_tables.h"
39#include "src/trace_processor/tables/track_tables.h"
Mikhail Khokhlov85a0dd02019-05-17 14:22:28 +010040#include "src/trace_processor/variadic.h"
Lalit Maganti35622b72018-06-06 12:03:11 +010041
Lalit Maganticaed37e2018-06-01 03:03:08 +010042namespace perfetto {
43namespace trace_processor {
44
Isabelle Taylora0a22972018-08-03 12:06:12 +010045// UniquePid is an offset into |unique_processes_|. This is necessary because
46// Unix pids are reused and thus not guaranteed to be unique over a long
47// period of time.
48using UniquePid = uint32_t;
Primiano Tucci0d72a312018-08-07 14:42:45 +010049
Isabelle Taylora0a22972018-08-03 12:06:12 +010050// UniqueTid is an offset into |unique_threads_|. Necessary because tids can
51// be reused.
52using UniqueTid = uint32_t;
53
Primiano Tucci0d72a312018-08-07 14:42:45 +010054// StringId is an offset into |string_pool_|.
Lalit Maganti5c454312019-04-08 12:11:17 +010055using StringId = StringPool::Id;
Lalit Maganti1a2936f2019-08-29 17:42:33 +010056static const StringId kNullStringId = StringId(0);
Primiano Tucci0d72a312018-08-07 14:42:45 +010057
Lalit Maganti5ea9e932018-11-30 14:19:39 +000058// Identifiers for all the tables in the database.
59enum TableId : uint8_t {
60 // Intentionally don't have TableId == 0 so that RowId == 0 can refer to an
61 // invalid row id.
Lalit Maganti8320e6d2019-03-14 18:49:33 +000062 kCounterValues = 1,
Lalit Maganti1d915a62019-01-07 12:10:42 +000063 kRawEvents = 2,
Lalit Maganti66ed7ad2019-01-11 16:47:26 +000064 kInstants = 3,
Isabelle Taylorb9222c32019-01-31 10:58:37 +000065 kSched = 4,
Eric Seckler70cc4422019-05-28 16:00:23 +010066 kNestableSlices = 5,
Ryan Savitski51413ad2019-07-09 14:25:21 +010067 kMetadataTable = 6,
Lalit Maganti5ea9e932018-11-30 14:19:39 +000068};
69
70// The top 8 bits are set to the TableId and the bottom 32 to the row of the
71// table.
Lalit Maganti85ca4a82018-12-07 17:28:02 +000072using RowId = int64_t;
Lalit Maganti5ea9e932018-11-30 14:19:39 +000073static const RowId kInvalidRowId = 0;
74
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +000075using ArgSetId = uint32_t;
76static const ArgSetId kInvalidArgSetId = 0;
77
Eric Seckler5703ede2019-07-10 10:13:02 +010078using TrackId = uint32_t;
79
80enum class VirtualTrackScope : uint8_t {
81 // VirtualTrack with global scope, will not have a |upid| set.
82 kGlobal = 0,
83 // VirtualTrack associated with a specific process via |upid|.
84 kProcess = 1
85};
86
Isabelle Taylora97c5f52018-10-23 17:36:12 +010087enum RefType {
Primiano Tucci5403e4f2018-11-27 10:07:03 +000088 kRefNoRef = 0,
89 kRefUtid = 1,
90 kRefCpuId = 2,
91 kRefIrq = 3,
92 kRefSoftIrq = 4,
93 kRefUpid = 5,
Sidath Senanayake1f5f93a2019-06-06 22:24:15 +010094 kRefGpuId = 6,
Eric Seckler5703ede2019-07-10 10:13:02 +010095 kRefTrack = 7,
Primiano Tucci5403e4f2018-11-27 10:07:03 +000096 kRefMax
Isabelle Taylora97c5f52018-10-23 17:36:12 +010097};
Isabelle Taylor14674d42018-09-07 11:33:11 +010098
Eric Seckler972225e2019-04-18 11:07:12 +010099const std::vector<const char*>& GetRefTypeStringMap();
100
Lalit Maganticaed37e2018-06-01 03:03:08 +0100101// Stores a data inside a trace file in a columnar form. This makes it efficient
102// to read or search across a single field of the trace (e.g. all the thread
103// names for a given CPU).
104class TraceStorage {
105 public:
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100106 TraceStorage();
Isabelle Taylora0a22972018-08-03 12:06:12 +0100107
108 virtual ~TraceStorage();
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100109
Isabelle Taylora0a22972018-08-03 12:06:12 +0100110 // Information about a unique process seen in a trace.
111 struct Process {
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100112 explicit Process(uint32_t p) : pid(p) {}
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000113 int64_t start_ns = 0;
Lalit Maganti637589a2019-07-04 17:25:29 +0100114 int64_t end_ns = 0;
Isabelle Taylora0a22972018-08-03 12:06:12 +0100115 StringId name_id = 0;
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100116 uint32_t pid = 0;
Lalit Maganti369b0572019-07-11 15:35:09 +0100117 base::Optional<UniquePid> parent_upid;
Isabelle Taylora0a22972018-08-03 12:06:12 +0100118 };
119
120 // Information about a unique thread seen in a trace.
121 struct Thread {
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100122 explicit Thread(uint32_t t) : tid(t) {}
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000123 int64_t start_ns = 0;
Lalit Magantib5bd2332019-06-06 14:20:47 +0100124 int64_t end_ns = 0;
Isabelle Taylora0a22972018-08-03 12:06:12 +0100125 StringId name_id = 0;
Lalit Maganti770886a2018-11-16 17:40:21 +0000126 base::Optional<UniquePid> upid;
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100127 uint32_t tid = 0;
Isabelle Taylora0a22972018-08-03 12:06:12 +0100128 };
Isabelle Taylor3dd366c2018-06-22 16:21:41 +0100129
Lalit Maganti6e9c55e2018-11-29 12:00:39 +0000130 // Generic key value storage which can be referenced by other tables.
131 class Args {
132 public:
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000133 struct Arg {
134 StringId flat_key = 0;
135 StringId key = 0;
136 Variadic value = Variadic::Integer(0);
137
138 // This is only used by the arg tracker and so is not part of the hash.
139 RowId row_id = 0;
140 };
141
142 struct ArgHasher {
143 uint64_t operator()(const Arg& arg) const noexcept {
Lalit Maganti1f464742019-02-28 13:49:31 +0000144 base::Hash hash;
145 hash.Update(arg.key);
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000146 // We don't hash arg.flat_key because it's a subsequence of arg.key.
147 switch (arg.value.type) {
148 case Variadic::Type::kInt:
Lalit Maganti1f464742019-02-28 13:49:31 +0000149 hash.Update(arg.value.int_value);
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000150 break;
Eric Secklerc93823e2019-06-03 16:49:19 +0100151 case Variadic::Type::kUint:
152 hash.Update(arg.value.uint_value);
153 break;
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000154 case Variadic::Type::kString:
Lalit Maganti1f464742019-02-28 13:49:31 +0000155 hash.Update(arg.value.string_value);
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000156 break;
157 case Variadic::Type::kReal:
Lalit Maganti1f464742019-02-28 13:49:31 +0000158 hash.Update(arg.value.real_value);
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000159 break;
Eric Secklerc93823e2019-06-03 16:49:19 +0100160 case Variadic::Type::kPointer:
161 hash.Update(arg.value.pointer_value);
162 break;
163 case Variadic::Type::kBool:
164 hash.Update(arg.value.bool_value);
165 break;
166 case Variadic::Type::kJson:
167 hash.Update(arg.value.json_value);
168 break;
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000169 }
Lalit Maganti1f464742019-02-28 13:49:31 +0000170 return hash.digest();
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000171 }
172 };
173
174 const std::deque<ArgSetId>& set_ids() const { return set_ids_; }
Lalit Maganti5ea9e932018-11-30 14:19:39 +0000175 const std::deque<StringId>& flat_keys() const { return flat_keys_; }
176 const std::deque<StringId>& keys() const { return keys_; }
Lalit Maganti1d915a62019-01-07 12:10:42 +0000177 const std::deque<Variadic>& arg_values() const { return arg_values_; }
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000178 uint32_t args_count() const {
179 return static_cast<uint32_t>(set_ids_.size());
Lalit Maganti79472be2018-12-04 13:41:27 +0000180 }
Lalit Maganti6e9c55e2018-11-29 12:00:39 +0000181
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000182 ArgSetId AddArgSet(const std::vector<Arg>& args,
183 uint32_t begin,
184 uint32_t end) {
Lalit Maganti1f464742019-02-28 13:49:31 +0000185 base::Hash hash;
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000186 for (uint32_t i = begin; i < end; i++) {
Lalit Maganti1f464742019-02-28 13:49:31 +0000187 hash.Update(ArgHasher()(args[i]));
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000188 }
Lalit Maganti5ea9e932018-11-30 14:19:39 +0000189
Lalit Maganti1f464742019-02-28 13:49:31 +0000190 ArgSetHash digest = hash.digest();
191 auto it = arg_row_for_hash_.find(digest);
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000192 if (it != arg_row_for_hash_.end()) {
193 return set_ids_[it->second];
194 }
195
196 // The +1 ensures that nothing has an id == kInvalidArgSetId == 0.
197 ArgSetId id = static_cast<uint32_t>(arg_row_for_hash_.size()) + 1;
Lalit Maganti1f464742019-02-28 13:49:31 +0000198 arg_row_for_hash_.emplace(digest, args_count());
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000199 for (uint32_t i = begin; i < end; i++) {
200 const auto& arg = args[i];
201 set_ids_.emplace_back(id);
202 flat_keys_.emplace_back(arg.flat_key);
203 keys_.emplace_back(arg.key);
204 arg_values_.emplace_back(arg.value);
205 }
206 return id;
Lalit Maganti6e9c55e2018-11-29 12:00:39 +0000207 }
208
Lalit Maganti5ea9e932018-11-30 14:19:39 +0000209 private:
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000210 using ArgSetHash = uint64_t;
211
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000212 std::deque<ArgSetId> set_ids_;
Lalit Maganti5ea9e932018-11-30 14:19:39 +0000213 std::deque<StringId> flat_keys_;
214 std::deque<StringId> keys_;
Lalit Maganti1d915a62019-01-07 12:10:42 +0000215 std::deque<Variadic> arg_values_;
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000216
217 std::unordered_map<ArgSetHash, uint32_t> arg_row_for_hash_;
Lalit Maganti6e9c55e2018-11-29 12:00:39 +0000218 };
219
Lalit Magantid11d3e02019-07-26 12:32:09 +0530220 class Tracks {
221 public:
222 inline uint32_t AddTrack(StringId name) {
223 names_.emplace_back(name);
224 return track_count() - 1;
225 }
226
227 uint32_t track_count() const {
228 return static_cast<uint32_t>(names_.size());
229 }
230
231 const std::deque<StringId>& names() const { return names_; }
232
233 private:
234 std::deque<StringId> names_;
235 };
236
Eric Seckler5703ede2019-07-10 10:13:02 +0100237 class VirtualTracks {
238 public:
Lalit Magantid11d3e02019-07-26 12:32:09 +0530239 inline void AddVirtualTrack(TrackId track_id,
240 VirtualTrackScope scope,
241 UniquePid upid = 0u) {
Eric Seckler5703ede2019-07-10 10:13:02 +0100242 track_ids_.emplace_back(track_id);
Eric Seckler5703ede2019-07-10 10:13:02 +0100243 scopes_.emplace_back(scope);
244 upids_.emplace_back(upid);
Eric Seckler5703ede2019-07-10 10:13:02 +0100245 }
246
247 uint32_t virtual_track_count() const {
248 return static_cast<uint32_t>(track_ids_.size());
249 }
250
Lalit Magantid11d3e02019-07-26 12:32:09 +0530251 base::Optional<uint32_t> FindRowForTrackId(uint32_t track_id) const {
252 auto it =
253 std::lower_bound(track_ids().begin(), track_ids().end(), track_id);
254 if (it != track_ids().end() && *it == track_id) {
255 return static_cast<uint32_t>(std::distance(track_ids().begin(), it));
256 }
257 return base::nullopt;
258 }
259
Eric Seckler5703ede2019-07-10 10:13:02 +0100260 const std::deque<uint32_t>& track_ids() const { return track_ids_; }
Eric Seckler5703ede2019-07-10 10:13:02 +0100261 const std::deque<VirtualTrackScope>& scopes() const { return scopes_; }
262 const std::deque<UniquePid>& upids() const { return upids_; }
263
264 private:
265 std::deque<uint32_t> track_ids_;
Eric Seckler5703ede2019-07-10 10:13:02 +0100266 std::deque<VirtualTrackScope> scopes_;
267 std::deque<UniquePid> upids_;
268 };
269
Mikael Pessa803090c2019-08-23 16:03:34 -0700270 class GpuContexts {
271 public:
272 inline void AddGpuContext(uint64_t context_id,
273 UniquePid upid,
274 uint32_t priority) {
275 context_ids_.emplace_back(context_id);
276 upids_.emplace_back(upid);
277 priorities_.emplace_back(priority);
278 }
279
280 uint32_t gpu_context_count() const {
281 return static_cast<uint32_t>(context_ids_.size());
282 }
283
284 const std::deque<uint64_t>& context_ids() const { return context_ids_; }
285 const std::deque<UniquePid>& upids() const { return upids_; }
286 const std::deque<uint32_t>& priorities() const { return priorities_; }
287
288 private:
289 std::deque<uint64_t> context_ids_;
290 std::deque<UniquePid> upids_;
291 std::deque<uint32_t> priorities_;
292 };
293
Lalit Magantiff69c112018-09-24 12:07:47 +0100294 class Slices {
Lalit Maganti35622b72018-06-06 12:03:11 +0100295 public:
Lalit Magantifde29042018-10-04 13:28:52 +0100296 inline size_t AddSlice(uint32_t cpu,
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000297 int64_t start_ns,
298 int64_t duration_ns,
Lalit Magantib0b53ee2019-01-24 17:53:39 +0000299 UniqueTid utid,
300 ftrace_utils::TaskState end_state,
301 int32_t priority) {
Lalit Magantiff69c112018-09-24 12:07:47 +0100302 cpus_.emplace_back(cpu);
Lalit Maganti35622b72018-06-06 12:03:11 +0100303 start_ns_.emplace_back(start_ns);
304 durations_.emplace_back(duration_ns);
Isabelle Taylora0a22972018-08-03 12:06:12 +0100305 utids_.emplace_back(utid);
Lalit Magantib0b53ee2019-01-24 17:53:39 +0000306 end_states_.emplace_back(end_state);
307 priorities_.emplace_back(priority);
Lalit Maganti58638932019-01-31 10:48:26 +0000308
309 if (utid >= rows_for_utids_.size())
310 rows_for_utids_.resize(utid + 1);
311 rows_for_utids_[utid].emplace_back(slice_count() - 1);
Lalit Magantifde29042018-10-04 13:28:52 +0100312 return slice_count() - 1;
313 }
314
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000315 void set_duration(size_t index, int64_t duration_ns) {
Lalit Magantifde29042018-10-04 13:28:52 +0100316 durations_[index] = duration_ns;
Lalit Maganti35622b72018-06-06 12:03:11 +0100317 }
318
Lalit Magantib0b53ee2019-01-24 17:53:39 +0000319 void set_end_state(size_t index, ftrace_utils::TaskState end_state) {
320 end_states_[index] = end_state;
321 }
322
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100323 size_t slice_count() const { return start_ns_.size(); }
Lalit Maganti35622b72018-06-06 12:03:11 +0100324
Lalit Magantiff69c112018-09-24 12:07:47 +0100325 const std::deque<uint32_t>& cpus() const { return cpus_; }
326
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000327 const std::deque<int64_t>& start_ns() const { return start_ns_; }
Lalit Maganti35622b72018-06-06 12:03:11 +0100328
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000329 const std::deque<int64_t>& durations() const { return durations_; }
Lalit Maganti35622b72018-06-06 12:03:11 +0100330
Isabelle Taylor68e42192018-06-19 16:19:31 +0100331 const std::deque<UniqueTid>& utids() const { return utids_; }
332
Lalit Magantib0b53ee2019-01-24 17:53:39 +0000333 const std::deque<ftrace_utils::TaskState>& end_state() const {
334 return end_states_;
335 }
336
337 const std::deque<int32_t>& priorities() const { return priorities_; }
338
Lalit Maganti58638932019-01-31 10:48:26 +0000339 const std::deque<std::vector<uint32_t>>& rows_for_utids() const {
Lalit Magantif0f09c32019-01-18 17:29:31 +0000340 return rows_for_utids_;
341 }
342
Lalit Maganti35622b72018-06-06 12:03:11 +0100343 private:
Hector Dearman947f12a2018-09-11 16:50:36 +0100344 // Each deque below has the same number of entries (the number of slices
Lalit Maganti35622b72018-06-06 12:03:11 +0100345 // in the trace for the CPU).
Lalit Magantiff69c112018-09-24 12:07:47 +0100346 std::deque<uint32_t> cpus_;
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000347 std::deque<int64_t> start_ns_;
348 std::deque<int64_t> durations_;
Isabelle Taylor68e42192018-06-19 16:19:31 +0100349 std::deque<UniqueTid> utids_;
Lalit Magantib0b53ee2019-01-24 17:53:39 +0000350 std::deque<ftrace_utils::TaskState> end_states_;
351 std::deque<int32_t> priorities_;
Lalit Maganti58638932019-01-31 10:48:26 +0000352
353 // One row per utid.
354 std::deque<std::vector<uint32_t>> rows_for_utids_;
Primiano Tucci0d72a312018-08-07 14:42:45 +0100355 };
Isabelle Taylor68e42192018-06-19 16:19:31 +0100356
Primiano Tucci0d72a312018-08-07 14:42:45 +0100357 class NestableSlices {
358 public:
Eric Seckler70cc4422019-05-28 16:00:23 +0100359 inline uint32_t AddSlice(int64_t start_ns,
360 int64_t duration_ns,
361 int64_t ref,
362 RefType type,
Lalit Maganti81a02cd2019-07-12 17:05:11 +0100363 StringId category,
Eric Seckler70cc4422019-05-28 16:00:23 +0100364 StringId name,
365 uint8_t depth,
366 int64_t stack_id,
367 int64_t parent_stack_id) {
Primiano Tucci0d72a312018-08-07 14:42:45 +0100368 start_ns_.emplace_back(start_ns);
369 durations_.emplace_back(duration_ns);
Eric Seckler972225e2019-04-18 11:07:12 +0100370 refs_.emplace_back(ref);
371 types_.emplace_back(type);
Lalit Maganti81a02cd2019-07-12 17:05:11 +0100372 categories_.emplace_back(category);
Primiano Tucci0d72a312018-08-07 14:42:45 +0100373 names_.emplace_back(name);
374 depths_.emplace_back(depth);
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100375 stack_ids_.emplace_back(stack_id);
376 parent_stack_ids_.emplace_back(parent_stack_id);
Eric Seckler70cc4422019-05-28 16:00:23 +0100377 arg_set_ids_.emplace_back(kInvalidArgSetId);
Lalit Maganti7b37bbf2018-11-09 15:58:54 +0000378 return slice_count() - 1;
379 }
380
Eric Seckler70cc4422019-05-28 16:00:23 +0100381 void set_duration(uint32_t index, int64_t duration_ns) {
Lalit Maganti7b37bbf2018-11-09 15:58:54 +0000382 durations_[index] = duration_ns;
383 }
384
Eric Seckler70cc4422019-05-28 16:00:23 +0100385 void set_stack_id(uint32_t index, int64_t stack_id) {
Lalit Maganti7b37bbf2018-11-09 15:58:54 +0000386 stack_ids_[index] = stack_id;
Primiano Tucci0d72a312018-08-07 14:42:45 +0100387 }
388
Eric Seckler70cc4422019-05-28 16:00:23 +0100389 void set_arg_set_id(uint32_t index, ArgSetId id) {
390 arg_set_ids_[index] = id;
391 }
392
393 uint32_t slice_count() const {
394 return static_cast<uint32_t>(start_ns_.size());
395 }
396
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000397 const std::deque<int64_t>& start_ns() const { return start_ns_; }
398 const std::deque<int64_t>& durations() const { return durations_; }
Eric Seckler972225e2019-04-18 11:07:12 +0100399 const std::deque<int64_t>& refs() const { return refs_; }
400 const std::deque<RefType>& types() const { return types_; }
Lalit Maganti81a02cd2019-07-12 17:05:11 +0100401 const std::deque<StringId>& categories() const { return categories_; }
Primiano Tucci0d72a312018-08-07 14:42:45 +0100402 const std::deque<StringId>& names() const { return names_; }
403 const std::deque<uint8_t>& depths() const { return depths_; }
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000404 const std::deque<int64_t>& stack_ids() const { return stack_ids_; }
405 const std::deque<int64_t>& parent_stack_ids() const {
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100406 return parent_stack_ids_;
407 }
Eric Seckler70cc4422019-05-28 16:00:23 +0100408 const std::deque<ArgSetId>& arg_set_ids() const { return arg_set_ids_; }
Primiano Tucci0d72a312018-08-07 14:42:45 +0100409
410 private:
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000411 std::deque<int64_t> start_ns_;
412 std::deque<int64_t> durations_;
Eric Seckler972225e2019-04-18 11:07:12 +0100413 std::deque<int64_t> refs_;
414 std::deque<RefType> types_;
Lalit Maganti81a02cd2019-07-12 17:05:11 +0100415 std::deque<StringId> categories_;
Primiano Tucci0d72a312018-08-07 14:42:45 +0100416 std::deque<StringId> names_;
417 std::deque<uint8_t> depths_;
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000418 std::deque<int64_t> stack_ids_;
419 std::deque<int64_t> parent_stack_ids_;
Eric Seckler70cc4422019-05-28 16:00:23 +0100420 std::deque<ArgSetId> arg_set_ids_;
Lalit Maganti35622b72018-06-06 12:03:11 +0100421 };
422
Eric Secklerd3b89d52019-07-10 15:36:29 +0100423 class ThreadSlices {
424 public:
425 inline uint32_t AddThreadSlice(uint32_t slice_id,
426 int64_t thread_timestamp_ns,
427 int64_t thread_duration_ns,
428 int64_t thread_instruction_count,
429 int64_t thread_instruction_delta) {
430 slice_ids_.emplace_back(slice_id);
431 thread_timestamp_ns_.emplace_back(thread_timestamp_ns);
432 thread_duration_ns_.emplace_back(thread_duration_ns);
433 thread_instruction_counts_.emplace_back(thread_instruction_count);
434 thread_instruction_deltas_.emplace_back(thread_instruction_delta);
435 return slice_count() - 1;
436 }
437
Eric Secklerc3430c62019-07-22 10:49:58 +0100438 uint32_t slice_count() const {
439 return static_cast<uint32_t>(slice_ids_.size());
Eric Secklerd3b89d52019-07-10 15:36:29 +0100440 }
441
Eric Secklerc3430c62019-07-22 10:49:58 +0100442 const std::deque<uint32_t>& slice_ids() const { return slice_ids_; }
443 const std::deque<int64_t>& thread_timestamp_ns() const {
444 return thread_timestamp_ns_;
445 }
446 const std::deque<int64_t>& thread_duration_ns() const {
447 return thread_duration_ns_;
448 }
449 const std::deque<int64_t>& thread_instruction_counts() const {
450 return thread_instruction_counts_;
451 }
452 const std::deque<int64_t>& thread_instruction_deltas() const {
453 return thread_instruction_deltas_;
454 }
455
456 base::Optional<uint32_t> FindRowForSliceId(uint32_t slice_id) const {
457 auto it =
458 std::lower_bound(slice_ids().begin(), slice_ids().end(), slice_id);
459 if (it != slice_ids().end() && *it == slice_id) {
460 return static_cast<uint32_t>(std::distance(slice_ids().begin(), it));
461 }
462 return base::nullopt;
463 }
464
Eric Seckler54f30a32019-07-19 15:10:29 +0100465 void UpdateThreadDeltasForSliceId(uint32_t slice_id,
466 int64_t end_thread_timestamp_ns,
467 int64_t end_thread_instruction_count) {
Eric Secklerc3430c62019-07-22 10:49:58 +0100468 uint32_t row = *FindRowForSliceId(slice_id);
469 int64_t begin_ns = thread_timestamp_ns_[row];
470 thread_duration_ns_[row] = end_thread_timestamp_ns - begin_ns;
Eric Seckler54f30a32019-07-19 15:10:29 +0100471 int64_t begin_ticount = thread_instruction_counts_[row];
472 thread_instruction_deltas_[row] =
473 end_thread_instruction_count - begin_ticount;
Eric Secklerc3430c62019-07-22 10:49:58 +0100474 }
475
476 private:
477 std::deque<uint32_t> slice_ids_;
478 std::deque<int64_t> thread_timestamp_ns_;
479 std::deque<int64_t> thread_duration_ns_;
480 std::deque<int64_t> thread_instruction_counts_;
481 std::deque<int64_t> thread_instruction_deltas_;
482 };
483
484 class VirtualTrackSlices {
485 public:
486 inline uint32_t AddVirtualTrackSlice(uint32_t slice_id,
487 int64_t thread_timestamp_ns,
488 int64_t thread_duration_ns,
489 int64_t thread_instruction_count,
490 int64_t thread_instruction_delta) {
491 slice_ids_.emplace_back(slice_id);
492 thread_timestamp_ns_.emplace_back(thread_timestamp_ns);
493 thread_duration_ns_.emplace_back(thread_duration_ns);
494 thread_instruction_counts_.emplace_back(thread_instruction_count);
495 thread_instruction_deltas_.emplace_back(thread_instruction_delta);
496 return slice_count() - 1;
Eric Secklerd3b89d52019-07-10 15:36:29 +0100497 }
498
499 uint32_t slice_count() const {
500 return static_cast<uint32_t>(slice_ids_.size());
501 }
502
503 const std::deque<uint32_t>& slice_ids() const { return slice_ids_; }
504 const std::deque<int64_t>& thread_timestamp_ns() const {
505 return thread_timestamp_ns_;
506 }
507 const std::deque<int64_t>& thread_duration_ns() const {
508 return thread_duration_ns_;
509 }
510 const std::deque<int64_t>& thread_instruction_counts() const {
511 return thread_instruction_counts_;
512 }
513 const std::deque<int64_t>& thread_instruction_deltas() const {
514 return thread_instruction_deltas_;
515 }
516
Mikhail Khokhlov7db04912019-07-18 16:11:11 +0100517 base::Optional<uint32_t> FindRowForSliceId(uint32_t slice_id) const {
Eric Secklerd3b89d52019-07-10 15:36:29 +0100518 auto it =
519 std::lower_bound(slice_ids().begin(), slice_ids().end(), slice_id);
Mikhail Khokhlov7db04912019-07-18 16:11:11 +0100520 if (it != slice_ids().end() && *it == slice_id) {
521 return static_cast<uint32_t>(std::distance(slice_ids().begin(), it));
522 }
523 return base::nullopt;
Eric Secklerd3b89d52019-07-10 15:36:29 +0100524 }
525
Eric Seckler54f30a32019-07-19 15:10:29 +0100526 void UpdateThreadDeltasForSliceId(uint32_t slice_id,
527 int64_t end_thread_timestamp_ns,
528 int64_t end_thread_instruction_count) {
Mikhail Khokhlov7db04912019-07-18 16:11:11 +0100529 uint32_t row = *FindRowForSliceId(slice_id);
Eric Secklerd3b89d52019-07-10 15:36:29 +0100530 int64_t begin_ns = thread_timestamp_ns_[row];
531 thread_duration_ns_[row] = end_thread_timestamp_ns - begin_ns;
Eric Seckler54f30a32019-07-19 15:10:29 +0100532 int64_t begin_ticount = thread_instruction_counts_[row];
533 thread_instruction_deltas_[row] =
534 end_thread_instruction_count - begin_ticount;
Eric Secklerd3b89d52019-07-10 15:36:29 +0100535 }
536
537 private:
538 std::deque<uint32_t> slice_ids_;
539 std::deque<int64_t> thread_timestamp_ns_;
540 std::deque<int64_t> thread_duration_ns_;
541 std::deque<int64_t> thread_instruction_counts_;
542 std::deque<int64_t> thread_instruction_deltas_;
543 };
544
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000545 class CounterDefinitions {
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100546 public:
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000547 using Id = uint32_t;
Lalit Maganti521d97b2019-04-29 13:47:03 +0100548 static constexpr Id kInvalidId = std::numeric_limits<Id>::max();
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000549
550 inline Id AddCounterDefinition(StringId name_id,
551 int64_t ref,
Pascal Muetschard91d07892019-08-26 13:55:06 -0700552 RefType type,
Pascal Muetschardceb5c822019-08-26 14:39:15 -0700553 StringId desc_id = 0,
554 StringId unit_id = 0) {
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000555 base::Hash hash;
556 hash.Update(name_id);
557 hash.Update(ref);
558 hash.Update(type);
559
560 // TODO(lalitm): this is a perf bottleneck and likely we can do something
561 // quite a bit better here.
562 uint64_t digest = hash.digest();
563 auto it = hash_to_row_idx_.find(digest);
564 if (it != hash_to_row_idx_.end())
565 return it->second;
566
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100567 name_ids_.emplace_back(name_id);
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100568 refs_.emplace_back(ref);
569 types_.emplace_back(type);
Pascal Muetschard91d07892019-08-26 13:55:06 -0700570 desc_ids_.emplace_back(desc_id);
Pascal Muetschardceb5c822019-08-26 14:39:15 -0700571 unit_ids_.emplace_back(unit_id);
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000572 hash_to_row_idx_.emplace(digest, size() - 1);
573 return size() - 1;
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100574 }
Lalit Magantifde29042018-10-04 13:28:52 +0100575
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000576 uint32_t size() const { return static_cast<uint32_t>(name_ids_.size()); }
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100577
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100578 const std::deque<StringId>& name_ids() const { return name_ids_; }
579
Pascal Muetschard91d07892019-08-26 13:55:06 -0700580 const std::deque<StringId>& desc_ids() const { return desc_ids_; }
581
Pascal Muetschardceb5c822019-08-26 14:39:15 -0700582 const std::deque<StringId>& unit_ids() const { return unit_ids_; }
583
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100584 const std::deque<int64_t>& refs() const { return refs_; }
585
586 const std::deque<RefType>& types() const { return types_; }
587
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000588 private:
589 std::deque<StringId> name_ids_;
590 std::deque<int64_t> refs_;
591 std::deque<RefType> types_;
Pascal Muetschard91d07892019-08-26 13:55:06 -0700592 std::deque<StringId> desc_ids_;
Pascal Muetschardceb5c822019-08-26 14:39:15 -0700593 std::deque<StringId> unit_ids_;
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000594
595 std::unordered_map<uint64_t, uint32_t> hash_to_row_idx_;
596 };
597
598 class CounterValues {
599 public:
600 inline uint32_t AddCounterValue(CounterDefinitions::Id counter_id,
601 int64_t timestamp,
602 double value) {
603 counter_ids_.emplace_back(counter_id);
604 timestamps_.emplace_back(timestamp);
605 values_.emplace_back(value);
606 arg_set_ids_.emplace_back(kInvalidArgSetId);
Ioannis Ilkosf129c262019-04-25 14:53:51 +0100607
Lalit Maganti521d97b2019-04-29 13:47:03 +0100608 if (counter_id != CounterDefinitions::kInvalidId) {
609 if (counter_id >= rows_for_counter_id_.size()) {
610 rows_for_counter_id_.resize(counter_id + 1);
611 }
612 rows_for_counter_id_[counter_id].emplace_back(size() - 1);
613 }
614 return size() - 1;
615 }
616
617 void set_counter_id(uint32_t index, CounterDefinitions::Id counter_id) {
618 PERFETTO_DCHECK(counter_ids_[index] == CounterDefinitions::kInvalidId);
619
620 counter_ids_[index] = counter_id;
Ioannis Ilkosf129c262019-04-25 14:53:51 +0100621 if (counter_id >= rows_for_counter_id_.size()) {
622 rows_for_counter_id_.resize(counter_id + 1);
623 }
Lalit Maganti521d97b2019-04-29 13:47:03 +0100624
625 auto* new_rows = &rows_for_counter_id_[counter_id];
626 new_rows->insert(
627 std::upper_bound(new_rows->begin(), new_rows->end(), index), index);
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000628 }
629
630 void set_arg_set_id(uint32_t row, ArgSetId id) { arg_set_ids_[row] = id; }
631
632 uint32_t size() const { return static_cast<uint32_t>(counter_ids_.size()); }
633
634 const std::deque<CounterDefinitions::Id>& counter_ids() const {
635 return counter_ids_;
636 }
637
638 const std::deque<int64_t>& timestamps() const { return timestamps_; }
639
640 const std::deque<double>& values() const { return values_; }
641
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000642 const std::deque<ArgSetId>& arg_set_ids() const { return arg_set_ids_; }
643
Ioannis Ilkosf129c262019-04-25 14:53:51 +0100644 const std::deque<std::vector<uint32_t>>& rows_for_counter_id() const {
645 return rows_for_counter_id_;
646 }
647
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100648 private:
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000649 std::deque<CounterDefinitions::Id> counter_ids_;
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000650 std::deque<int64_t> timestamps_;
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100651 std::deque<double> values_;
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000652 std::deque<ArgSetId> arg_set_ids_;
Ioannis Ilkosf129c262019-04-25 14:53:51 +0100653
654 // Indexed by counter_id value and contains the row numbers corresponding to
655 // it.
656 std::deque<std::vector<uint32_t>> rows_for_counter_id_;
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100657 };
658
Primiano Tucci5cb84f82018-10-31 21:46:36 -0700659 class SqlStats {
660 public:
661 static constexpr size_t kMaxLogEntries = 100;
Lalit Magantiaac2f652019-04-30 12:16:21 +0100662 uint32_t RecordQueryBegin(const std::string& query,
663 int64_t time_queued,
664 int64_t time_started);
665 void RecordQueryFirstNext(uint32_t row, int64_t time_first_next);
666 void RecordQueryEnd(uint32_t row, int64_t time_end);
Primiano Tucci5cb84f82018-10-31 21:46:36 -0700667 size_t size() const { return queries_.size(); }
668 const std::deque<std::string>& queries() const { return queries_; }
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000669 const std::deque<int64_t>& times_queued() const { return times_queued_; }
670 const std::deque<int64_t>& times_started() const { return times_started_; }
Lalit Magantiaac2f652019-04-30 12:16:21 +0100671 const std::deque<int64_t>& times_first_next() const {
672 return times_first_next_;
673 }
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000674 const std::deque<int64_t>& times_ended() const { return times_ended_; }
Primiano Tucci5cb84f82018-10-31 21:46:36 -0700675
676 private:
Lalit Magantiaac2f652019-04-30 12:16:21 +0100677 uint32_t popped_queries_ = 0;
678
Primiano Tucci5cb84f82018-10-31 21:46:36 -0700679 std::deque<std::string> queries_;
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000680 std::deque<int64_t> times_queued_;
681 std::deque<int64_t> times_started_;
Lalit Magantiaac2f652019-04-30 12:16:21 +0100682 std::deque<int64_t> times_first_next_;
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000683 std::deque<int64_t> times_ended_;
Primiano Tucci5cb84f82018-10-31 21:46:36 -0700684 };
685
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000686 class Instants {
687 public:
Lalit Maganti66ed7ad2019-01-11 16:47:26 +0000688 inline uint32_t AddInstantEvent(int64_t timestamp,
689 StringId name_id,
690 double value,
691 int64_t ref,
692 RefType type) {
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000693 timestamps_.emplace_back(timestamp);
694 name_ids_.emplace_back(name_id);
695 values_.emplace_back(value);
696 refs_.emplace_back(ref);
697 types_.emplace_back(type);
Lalit Maganti1c21d172019-02-07 10:48:24 +0000698 arg_set_ids_.emplace_back(kInvalidArgSetId);
Lalit Maganti66ed7ad2019-01-11 16:47:26 +0000699 return static_cast<uint32_t>(instant_count() - 1);
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000700 }
701
Lalit Maganti521d97b2019-04-29 13:47:03 +0100702 void set_ref(uint32_t row, int64_t ref) { refs_[row] = ref; }
703
Lalit Maganti1c21d172019-02-07 10:48:24 +0000704 void set_arg_set_id(uint32_t row, ArgSetId id) { arg_set_ids_[row] = id; }
705
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000706 size_t instant_count() const { return timestamps_.size(); }
707
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000708 const std::deque<int64_t>& timestamps() const { return timestamps_; }
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000709
710 const std::deque<StringId>& name_ids() const { return name_ids_; }
711
712 const std::deque<double>& values() const { return values_; }
713
714 const std::deque<int64_t>& refs() const { return refs_; }
715
716 const std::deque<RefType>& types() const { return types_; }
717
Lalit Maganti1c21d172019-02-07 10:48:24 +0000718 const std::deque<ArgSetId>& arg_set_ids() const { return arg_set_ids_; }
719
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000720 private:
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000721 std::deque<int64_t> timestamps_;
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000722 std::deque<StringId> name_ids_;
723 std::deque<double> values_;
724 std::deque<int64_t> refs_;
725 std::deque<RefType> types_;
Lalit Maganti1c21d172019-02-07 10:48:24 +0000726 std::deque<ArgSetId> arg_set_ids_;
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000727 };
728
Lalit Maganti1d915a62019-01-07 12:10:42 +0000729 class RawEvents {
730 public:
731 inline RowId AddRawEvent(int64_t timestamp,
732 StringId name_id,
Lalit Magantie87cc812019-01-10 15:20:06 +0000733 uint32_t cpu,
Lalit Maganti1d915a62019-01-07 12:10:42 +0000734 UniqueTid utid) {
735 timestamps_.emplace_back(timestamp);
736 name_ids_.emplace_back(name_id);
Lalit Magantie87cc812019-01-10 15:20:06 +0000737 cpus_.emplace_back(cpu);
Lalit Maganti1d915a62019-01-07 12:10:42 +0000738 utids_.emplace_back(utid);
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000739 arg_set_ids_.emplace_back(kInvalidArgSetId);
Lalit Maganti1d915a62019-01-07 12:10:42 +0000740 return CreateRowId(TableId::kRawEvents,
741 static_cast<uint32_t>(raw_event_count() - 1));
742 }
743
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000744 void set_arg_set_id(uint32_t row, ArgSetId id) { arg_set_ids_[row] = id; }
745
Lalit Maganti1d915a62019-01-07 12:10:42 +0000746 size_t raw_event_count() const { return timestamps_.size(); }
747
748 const std::deque<int64_t>& timestamps() const { return timestamps_; }
749
750 const std::deque<StringId>& name_ids() const { return name_ids_; }
751
Lalit Magantie87cc812019-01-10 15:20:06 +0000752 const std::deque<uint32_t>& cpus() const { return cpus_; }
753
Lalit Maganti1d915a62019-01-07 12:10:42 +0000754 const std::deque<UniqueTid>& utids() const { return utids_; }
755
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000756 const std::deque<ArgSetId>& arg_set_ids() const { return arg_set_ids_; }
757
Lalit Maganti1d915a62019-01-07 12:10:42 +0000758 private:
759 std::deque<int64_t> timestamps_;
760 std::deque<StringId> name_ids_;
Lalit Magantie87cc812019-01-10 15:20:06 +0000761 std::deque<uint32_t> cpus_;
Lalit Maganti1d915a62019-01-07 12:10:42 +0000762 std::deque<UniqueTid> utids_;
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000763 std::deque<ArgSetId> arg_set_ids_;
Lalit Maganti1d915a62019-01-07 12:10:42 +0000764 };
765
Primiano Tucci2c761ef2019-01-07 20:20:46 +0000766 class AndroidLogs {
767 public:
768 inline size_t AddLogEvent(int64_t timestamp,
769 UniqueTid utid,
770 uint8_t prio,
771 StringId tag_id,
772 StringId msg_id) {
773 timestamps_.emplace_back(timestamp);
774 utids_.emplace_back(utid);
775 prios_.emplace_back(prio);
776 tag_ids_.emplace_back(tag_id);
777 msg_ids_.emplace_back(msg_id);
778 return size() - 1;
779 }
780
781 size_t size() const { return timestamps_.size(); }
782
783 const std::deque<int64_t>& timestamps() const { return timestamps_; }
784 const std::deque<UniqueTid>& utids() const { return utids_; }
785 const std::deque<uint8_t>& prios() const { return prios_; }
786 const std::deque<StringId>& tag_ids() const { return tag_ids_; }
787 const std::deque<StringId>& msg_ids() const { return msg_ids_; }
788
789 private:
790 std::deque<int64_t> timestamps_;
791 std::deque<UniqueTid> utids_;
792 std::deque<uint8_t> prios_;
793 std::deque<StringId> tag_ids_;
794 std::deque<StringId> msg_ids_;
795 };
796
Primiano Tucci0e38a142019-01-07 20:51:09 +0000797 struct Stats {
798 using IndexMap = std::map<int, int64_t>;
799 int64_t value = 0;
800 IndexMap indexed_values;
801 };
802 using StatsMap = std::array<Stats, stats::kNumKeys>;
803
Ryan Savitski51413ad2019-07-09 14:25:21 +0100804 class Metadata {
805 public:
806 const std::deque<metadata::KeyIDs>& keys() const { return keys_; }
807 const std::deque<Variadic>& values() const { return values_; }
808
809 RowId SetScalarMetadata(metadata::KeyIDs key, Variadic value) {
810 PERFETTO_DCHECK(key < metadata::kNumKeys);
811 PERFETTO_DCHECK(metadata::kKeyTypes[key] == metadata::kSingle);
812 PERFETTO_DCHECK(value.type == metadata::kValueTypes[key]);
813
814 // Already set - on release builds, overwrite the previous value.
815 auto it = scalar_indices.find(key);
816 if (it != scalar_indices.end()) {
817 PERFETTO_DFATAL("Setting a scalar metadata entry more than once.");
818 uint32_t index = static_cast<uint32_t>(it->second);
819 values_[index] = value;
820 return TraceStorage::CreateRowId(kMetadataTable, index);
821 }
822 // First time setting this key.
823 keys_.push_back(key);
824 values_.push_back(value);
825 uint32_t index = static_cast<uint32_t>(keys_.size() - 1);
826 scalar_indices[key] = index;
827 return TraceStorage::CreateRowId(kMetadataTable, index);
828 }
829
830 RowId AppendMetadata(metadata::KeyIDs key, Variadic value) {
831 PERFETTO_DCHECK(key < metadata::kNumKeys);
832 PERFETTO_DCHECK(metadata::kKeyTypes[key] == metadata::kMulti);
833 PERFETTO_DCHECK(value.type == metadata::kValueTypes[key]);
834
835 keys_.push_back(key);
836 values_.push_back(value);
837 uint32_t index = static_cast<uint32_t>(keys_.size() - 1);
838 return TraceStorage::CreateRowId(kMetadataTable, index);
839 }
840
Ryan Savitski0476ee92019-07-09 14:29:33 +0100841 void OverwriteMetadata(uint32_t index, Variadic value) {
842 PERFETTO_DCHECK(index < values_.size());
843 values_[index] = value;
844 }
845
Ryan Savitski51413ad2019-07-09 14:25:21 +0100846 private:
847 std::deque<metadata::KeyIDs> keys_;
848 std::deque<Variadic> values_;
849 // Extraneous state to track locations of entries that should have at most
850 // one row. Used only to maintain uniqueness during insertions.
851 std::map<metadata::KeyIDs, uint32_t> scalar_indices;
852 };
Mikhail Khokhlove466c002019-05-23 13:33:33 +0100853
Oystein Eftevaag5419c582019-08-21 13:58:49 -0700854 class StackProfileFrames {
Florian Mayer438b5ab2019-05-02 11:18:06 +0100855 public:
Florian Mayerbee52132019-05-02 13:59:56 +0100856 struct Row {
857 StringId name_id;
858 int64_t mapping_row;
859 int64_t rel_pc;
Florian Mayer438b5ab2019-05-02 11:18:06 +0100860
Florian Mayerbee52132019-05-02 13:59:56 +0100861 bool operator==(const Row& other) const {
862 return std::tie(name_id, mapping_row, rel_pc) ==
863 std::tie(other.name_id, other.mapping_row, other.rel_pc);
864 }
865 };
866
Lalit Maganti9b2d52b2019-05-07 14:32:15 +0100867 uint32_t size() const { return static_cast<uint32_t>(names_.size()); }
868
Florian Mayerbee52132019-05-02 13:59:56 +0100869 int64_t Insert(const Row& row) {
870 names_.emplace_back(row.name_id);
871 mappings_.emplace_back(row.mapping_row);
872 rel_pcs_.emplace_back(row.rel_pc);
Florian Mayer438b5ab2019-05-02 11:18:06 +0100873 return static_cast<int64_t>(names_.size()) - 1;
874 }
875
Ioannis Ilkose6727552019-08-14 15:10:59 +0100876 void SetFrameName(size_t row_idx, StringId name_id) {
877 PERFETTO_CHECK(row_idx < names_.size());
878 names_[row_idx] = name_id;
879 }
880
Florian Mayer438b5ab2019-05-02 11:18:06 +0100881 const std::deque<StringId>& names() const { return names_; }
882 const std::deque<int64_t>& mappings() const { return mappings_; }
883 const std::deque<int64_t>& rel_pcs() const { return rel_pcs_; }
884
885 private:
886 std::deque<StringId> names_;
887 std::deque<int64_t> mappings_;
888 std::deque<int64_t> rel_pcs_;
889 };
890
Oystein Eftevaag5419c582019-08-21 13:58:49 -0700891 class StackProfileCallsites {
Florian Mayer438b5ab2019-05-02 11:18:06 +0100892 public:
Florian Mayerbee52132019-05-02 13:59:56 +0100893 struct Row {
894 int64_t depth;
895 int64_t parent_id;
896 int64_t frame_row;
897
898 bool operator==(const Row& other) const {
899 return std::tie(depth, parent_id, frame_row) ==
900 std::tie(other.depth, other.parent_id, other.frame_row);
Florian Mayer438b5ab2019-05-02 11:18:06 +0100901 }
Florian Mayerbee52132019-05-02 13:59:56 +0100902 };
903
Lalit Maganti9b2d52b2019-05-07 14:32:15 +0100904 uint32_t size() const { return static_cast<uint32_t>(frame_ids_.size()); }
905
Florian Mayerbee52132019-05-02 13:59:56 +0100906 int64_t Insert(const Row& row) {
907 frame_depths_.emplace_back(row.depth);
908 parent_callsite_ids_.emplace_back(row.parent_id);
909 frame_ids_.emplace_back(row.frame_row);
Florian Mayer438b5ab2019-05-02 11:18:06 +0100910 return static_cast<int64_t>(frame_depths_.size()) - 1;
911 }
912
913 const std::deque<int64_t>& frame_depths() const { return frame_depths_; }
914 const std::deque<int64_t>& parent_callsite_ids() const {
915 return parent_callsite_ids_;
916 }
917 const std::deque<int64_t>& frame_ids() const { return frame_ids_; }
918
919 private:
920 std::deque<int64_t> frame_depths_;
921 std::deque<int64_t> parent_callsite_ids_;
922 std::deque<int64_t> frame_ids_;
923 };
924
Oystein Eftevaag5419c582019-08-21 13:58:49 -0700925 class StackProfileMappings {
Florian Mayer438b5ab2019-05-02 11:18:06 +0100926 public:
Florian Mayerbee52132019-05-02 13:59:56 +0100927 struct Row {
928 StringId build_id;
Florian Mayer12655732019-07-02 15:08:26 +0100929 int64_t exact_offset;
930 int64_t start_offset;
Florian Mayerbee52132019-05-02 13:59:56 +0100931 int64_t start;
932 int64_t end;
933 int64_t load_bias;
934 StringId name_id;
Florian Mayer438b5ab2019-05-02 11:18:06 +0100935
Florian Mayerbee52132019-05-02 13:59:56 +0100936 bool operator==(const Row& other) const {
Florian Mayer12655732019-07-02 15:08:26 +0100937 return std::tie(build_id, exact_offset, start_offset, start, end,
938 load_bias, name_id) ==
939 std::tie(other.build_id, other.exact_offset, other.start_offset,
940 other.start, other.end, other.load_bias, other.name_id);
Florian Mayerbee52132019-05-02 13:59:56 +0100941 }
942 };
943
Lalit Maganti9b2d52b2019-05-07 14:32:15 +0100944 uint32_t size() const { return static_cast<uint32_t>(names_.size()); }
945
Florian Mayerbee52132019-05-02 13:59:56 +0100946 int64_t Insert(const Row& row) {
947 build_ids_.emplace_back(row.build_id);
Florian Mayer12655732019-07-02 15:08:26 +0100948 exact_offsets_.emplace_back(row.exact_offset);
949 start_offsets_.emplace_back(row.start_offset);
Florian Mayerbee52132019-05-02 13:59:56 +0100950 starts_.emplace_back(row.start);
951 ends_.emplace_back(row.end);
952 load_biases_.emplace_back(row.load_bias);
953 names_.emplace_back(row.name_id);
Florian Mayer438b5ab2019-05-02 11:18:06 +0100954 return static_cast<int64_t>(build_ids_.size()) - 1;
955 }
956
957 const std::deque<StringId>& build_ids() const { return build_ids_; }
Florian Mayer12655732019-07-02 15:08:26 +0100958 const std::deque<int64_t>& exact_offsets() const { return exact_offsets_; }
959 const std::deque<int64_t>& start_offsets() const { return start_offsets_; }
Florian Mayer438b5ab2019-05-02 11:18:06 +0100960 const std::deque<int64_t>& starts() const { return starts_; }
961 const std::deque<int64_t>& ends() const { return ends_; }
962 const std::deque<int64_t>& load_biases() const { return load_biases_; }
963 const std::deque<StringId>& names() const { return names_; }
964
965 private:
966 std::deque<StringId> build_ids_;
Florian Mayer12655732019-07-02 15:08:26 +0100967 std::deque<int64_t> exact_offsets_;
968 std::deque<int64_t> start_offsets_;
Florian Mayer438b5ab2019-05-02 11:18:06 +0100969 std::deque<int64_t> starts_;
970 std::deque<int64_t> ends_;
971 std::deque<int64_t> load_biases_;
972 std::deque<StringId> names_;
973 };
974
975 class HeapProfileAllocations {
976 public:
Florian Mayerbee52132019-05-02 13:59:56 +0100977 struct Row {
978 int64_t timestamp;
Florian Mayerdf1968c2019-05-13 16:39:35 +0100979 UniquePid upid;
Florian Mayerbee52132019-05-02 13:59:56 +0100980 int64_t callsite_id;
981 int64_t count;
982 int64_t size;
983 };
984
Lalit Maganti9b2d52b2019-05-07 14:32:15 +0100985 uint32_t size() const { return static_cast<uint32_t>(timestamps_.size()); }
986
Florian Mayerbee52132019-05-02 13:59:56 +0100987 void Insert(const Row& row) {
988 timestamps_.emplace_back(row.timestamp);
Florian Mayerdf1968c2019-05-13 16:39:35 +0100989 upids_.emplace_back(row.upid);
Florian Mayerbee52132019-05-02 13:59:56 +0100990 callsite_ids_.emplace_back(row.callsite_id);
991 counts_.emplace_back(row.count);
992 sizes_.emplace_back(row.size);
Florian Mayer438b5ab2019-05-02 11:18:06 +0100993 }
994
995 const std::deque<int64_t>& timestamps() const { return timestamps_; }
Florian Mayerdf1968c2019-05-13 16:39:35 +0100996 const std::deque<UniquePid>& upids() const { return upids_; }
Florian Mayer438b5ab2019-05-02 11:18:06 +0100997 const std::deque<int64_t>& callsite_ids() const { return callsite_ids_; }
998 const std::deque<int64_t>& counts() const { return counts_; }
999 const std::deque<int64_t>& sizes() const { return sizes_; }
1000
1001 private:
1002 std::deque<int64_t> timestamps_;
Florian Mayerdf1968c2019-05-13 16:39:35 +01001003 std::deque<UniquePid> upids_;
Florian Mayer438b5ab2019-05-02 11:18:06 +01001004 std::deque<int64_t> callsite_ids_;
1005 std::deque<int64_t> counts_;
1006 std::deque<int64_t> sizes_;
1007 };
1008
Oystein Eftevaag7f64c102019-08-29 10:27:31 -07001009 class CpuProfileStackSamples {
1010 public:
1011 struct Row {
1012 int64_t timestamp;
1013 int64_t callsite_id;
1014 UniqueTid utid;
1015 };
1016
1017 uint32_t size() const { return static_cast<uint32_t>(timestamps_.size()); }
1018
1019 void Insert(const Row& row) {
1020 timestamps_.emplace_back(row.timestamp);
1021 callsite_ids_.emplace_back(row.callsite_id);
1022 utids_.emplace_back(row.utid);
1023 }
1024
1025 const std::deque<int64_t>& timestamps() const { return timestamps_; }
1026 const std::deque<int64_t>& callsite_ids() const { return callsite_ids_; }
1027 const std::deque<UniqueTid>& utids() const { return utids_; }
1028
1029 private:
1030 std::deque<int64_t> timestamps_;
1031 std::deque<int64_t> callsite_ids_;
1032 std::deque<UniqueTid> utids_;
1033 };
1034
Primiano Tuccib75dcee2018-08-08 12:21:36 +01001035 UniqueTid AddEmptyThread(uint32_t tid) {
1036 unique_threads_.emplace_back(tid);
Isabelle Taylora0a22972018-08-03 12:06:12 +01001037 return static_cast<UniqueTid>(unique_threads_.size() - 1);
1038 }
Isabelle Taylor47328cf2018-06-12 14:33:59 +01001039
Primiano Tuccib75dcee2018-08-08 12:21:36 +01001040 UniquePid AddEmptyProcess(uint32_t pid) {
1041 unique_processes_.emplace_back(pid);
Isabelle Taylora0a22972018-08-03 12:06:12 +01001042 return static_cast<UniquePid>(unique_processes_.size() - 1);
1043 }
Isabelle Taylor3dd366c2018-06-22 16:21:41 +01001044
Isabelle Taylora0a22972018-08-03 12:06:12 +01001045 // Return an unqiue identifier for the contents of each string.
1046 // The string is copied internally and can be destroyed after this called.
Isabelle Taylor15314ea2018-09-19 11:35:19 +01001047 // Virtual for testing.
Lalit Maganti5c454312019-04-08 12:11:17 +01001048 virtual StringId InternString(base::StringView str) {
1049 return string_pool_.InternString(str);
1050 }
Isabelle Taylor47328cf2018-06-12 14:33:59 +01001051
Isabelle Taylora0a22972018-08-03 12:06:12 +01001052 Process* GetMutableProcess(UniquePid upid) {
Lalit Maganti676658b2018-11-20 18:24:31 +00001053 PERFETTO_DCHECK(upid < unique_processes_.size());
Isabelle Taylora0a22972018-08-03 12:06:12 +01001054 return &unique_processes_[upid];
1055 }
Isabelle Taylor3dd366c2018-06-22 16:21:41 +01001056
Isabelle Taylora0a22972018-08-03 12:06:12 +01001057 Thread* GetMutableThread(UniqueTid utid) {
Florian Mayera1aaec22018-09-19 17:02:26 +01001058 PERFETTO_DCHECK(utid < unique_threads_.size());
Isabelle Taylora0a22972018-08-03 12:06:12 +01001059 return &unique_threads_[utid];
1060 }
Isabelle Taylor3dd366c2018-06-22 16:21:41 +01001061
Primiano Tucci0e38a142019-01-07 20:51:09 +00001062 // Example usage: SetStats(stats::android_log_num_failed, 42);
1063 void SetStats(size_t key, int64_t value) {
1064 PERFETTO_DCHECK(key < stats::kNumKeys);
1065 PERFETTO_DCHECK(stats::kTypes[key] == stats::kSingle);
1066 stats_[key].value = value;
1067 }
1068
1069 // Example usage: IncrementStats(stats::android_log_num_failed, -1);
1070 void IncrementStats(size_t key, int64_t increment = 1) {
1071 PERFETTO_DCHECK(key < stats::kNumKeys);
1072 PERFETTO_DCHECK(stats::kTypes[key] == stats::kSingle);
1073 stats_[key].value += increment;
1074 }
1075
Florian Mayer438b5ab2019-05-02 11:18:06 +01001076 // Example usage: IncrementIndexedStats(stats::cpu_failure, 1);
1077 void IncrementIndexedStats(size_t key, int index, int64_t increment = 1) {
1078 PERFETTO_DCHECK(key < stats::kNumKeys);
1079 PERFETTO_DCHECK(stats::kTypes[key] == stats::kIndexed);
1080 stats_[key].indexed_values[index] += increment;
1081 }
1082
Primiano Tucci0e38a142019-01-07 20:51:09 +00001083 // Example usage: SetIndexedStats(stats::cpu_failure, 1, 42);
1084 void SetIndexedStats(size_t key, int index, int64_t value) {
1085 PERFETTO_DCHECK(key < stats::kNumKeys);
1086 PERFETTO_DCHECK(stats::kTypes[key] == stats::kIndexed);
1087 stats_[key].indexed_values[index] = value;
1088 }
1089
Mikhail Khokhlove466c002019-05-23 13:33:33 +01001090 // Example usage:
1091 // SetMetadata(metadata::benchmark_name,
1092 // Variadic::String(storage->InternString("foo"));
Ryan Savitski51413ad2019-07-09 14:25:21 +01001093 // Returns the RowId of the new entry.
Mikhail Khokhlovb1fe42a2019-05-23 13:58:05 +01001094 // Virtual for testing.
Ryan Savitski51413ad2019-07-09 14:25:21 +01001095 virtual RowId SetMetadata(metadata::KeyIDs key, Variadic value) {
1096 return metadata_.SetScalarMetadata(key, value);
Mikhail Khokhlove466c002019-05-23 13:33:33 +01001097 }
1098
1099 // Example usage:
1100 // AppendMetadata(metadata::benchmark_story_tags,
1101 // Variadic::String(storage->InternString("bar"));
Ryan Savitski51413ad2019-07-09 14:25:21 +01001102 // Returns the RowId of the new entry.
Mikhail Khokhlovb1fe42a2019-05-23 13:58:05 +01001103 // Virtual for testing.
Ryan Savitski51413ad2019-07-09 14:25:21 +01001104 virtual RowId AppendMetadata(metadata::KeyIDs key, Variadic value) {
1105 return metadata_.AppendMetadata(key, value);
Mikhail Khokhlove466c002019-05-23 13:33:33 +01001106 }
1107
Eric Seckler77b52782019-05-02 15:18:57 +01001108 class ScopedStatsTracer {
1109 public:
1110 ScopedStatsTracer(TraceStorage* storage, size_t key)
1111 : storage_(storage), key_(key), start_ns_(base::GetWallTimeNs()) {}
1112
1113 ~ScopedStatsTracer() {
1114 if (!storage_)
1115 return;
1116 auto delta_ns = base::GetWallTimeNs() - start_ns_;
1117 storage_->IncrementStats(key_, delta_ns.count());
1118 }
1119
1120 ScopedStatsTracer(ScopedStatsTracer&& other) noexcept { MoveImpl(&other); }
1121
1122 ScopedStatsTracer& operator=(ScopedStatsTracer&& other) {
1123 MoveImpl(&other);
1124 return *this;
1125 }
1126
1127 private:
1128 ScopedStatsTracer(const ScopedStatsTracer&) = delete;
1129 ScopedStatsTracer& operator=(const ScopedStatsTracer&) = delete;
1130
1131 void MoveImpl(ScopedStatsTracer* other) {
1132 storage_ = other->storage_;
1133 key_ = other->key_;
1134 start_ns_ = other->start_ns_;
1135 other->storage_ = nullptr;
1136 }
1137
1138 TraceStorage* storage_;
1139 size_t key_;
1140 base::TimeNanos start_ns_;
1141 };
1142
1143 ScopedStatsTracer TraceExecutionTimeIntoStats(size_t key) {
1144 return ScopedStatsTracer(this, key);
1145 }
1146
Lalit Maganticaed37e2018-06-01 03:03:08 +01001147 // Reading methods.
Eric Secklerc93823e2019-06-03 16:49:19 +01001148 // Virtual for testing.
1149 virtual NullTermStringView GetString(StringId id) const {
Lalit Maganti5c454312019-04-08 12:11:17 +01001150 return string_pool_.Get(id);
Isabelle Taylora0a22972018-08-03 12:06:12 +01001151 }
1152
Lalit Magantie9d40532018-06-29 13:15:06 +01001153 const Process& GetProcess(UniquePid upid) const {
Lalit Maganti676658b2018-11-20 18:24:31 +00001154 PERFETTO_DCHECK(upid < unique_processes_.size());
Isabelle Taylor47328cf2018-06-12 14:33:59 +01001155 return unique_processes_[upid];
1156 }
1157
Lalit Magantie9d40532018-06-29 13:15:06 +01001158 const Thread& GetThread(UniqueTid utid) const {
Lalit Maganti1f64cfa2018-09-18 13:20:02 +01001159 // Allow utid == 0 for idle thread retrieval.
Florian Mayera1aaec22018-09-19 17:02:26 +01001160 PERFETTO_DCHECK(utid < unique_threads_.size());
Isabelle Taylor68e42192018-06-19 16:19:31 +01001161 return unique_threads_[utid];
1162 }
1163
Lalit Maganti5ea9e932018-11-30 14:19:39 +00001164 static RowId CreateRowId(TableId table, uint32_t row) {
Lalit Maganti85ca4a82018-12-07 17:28:02 +00001165 return (static_cast<RowId>(table) << kRowIdTableShift) | row;
Lalit Maganti5ea9e932018-11-30 14:19:39 +00001166 }
1167
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +00001168 static std::pair<int8_t /*table*/, uint32_t /*row*/> ParseRowId(RowId rowid) {
1169 auto id = static_cast<uint64_t>(rowid);
1170 auto table_id = static_cast<uint8_t>(id >> kRowIdTableShift);
1171 auto row = static_cast<uint32_t>(id & ((1ull << kRowIdTableShift) - 1));
1172 return std::make_pair(table_id, row);
1173 }
1174
Lalit Magantiac68e9c2019-09-09 18:12:15 +01001175 const tables::TrackTable& track_table() const { return track_table_; }
1176 tables::TrackTable* mutable_track_table() { return &track_table_; }
Lalit Magantid11d3e02019-07-26 12:32:09 +05301177
Eric Seckler5703ede2019-07-10 10:13:02 +01001178 const VirtualTracks& virtual_tracks() const { return virtual_tracks_; }
1179 VirtualTracks* mutable_virtual_tracks() { return &virtual_tracks_; }
1180
Lalit Magantiff69c112018-09-24 12:07:47 +01001181 const Slices& slices() const { return slices_; }
Lalit Magantifde29042018-10-04 13:28:52 +01001182 Slices* mutable_slices() { return &slices_; }
1183
Primiano Tucci0d72a312018-08-07 14:42:45 +01001184 const NestableSlices& nestable_slices() const { return nestable_slices_; }
1185 NestableSlices* mutable_nestable_slices() { return &nestable_slices_; }
1186
Eric Secklerd3b89d52019-07-10 15:36:29 +01001187 const ThreadSlices& thread_slices() const { return thread_slices_; }
1188 ThreadSlices* mutable_thread_slices() { return &thread_slices_; }
1189
Eric Secklerc3430c62019-07-22 10:49:58 +01001190 const VirtualTrackSlices& virtual_track_slices() const {
1191 return virtual_track_slices_;
1192 }
1193 VirtualTrackSlices* mutable_virtual_track_slices() {
1194 return &virtual_track_slices_;
1195 }
1196
Lalit Maganti20539c22019-09-04 12:36:10 +01001197 const tables::GpuSliceTable& gpu_slice_table() const {
1198 return gpu_slice_table_;
1199 }
1200 tables::GpuSliceTable* mutable_gpu_slice_table() { return &gpu_slice_table_; }
Mikael Pessa7160ccc2019-07-25 11:19:26 -07001201
Lalit Maganti8320e6d2019-03-14 18:49:33 +00001202 const CounterDefinitions& counter_definitions() const {
1203 return counter_definitions_;
1204 }
1205 CounterDefinitions* mutable_counter_definitions() {
1206 return &counter_definitions_;
1207 }
1208
1209 const CounterValues& counter_values() const { return counter_values_; }
1210 CounterValues* mutable_counter_values() { return &counter_values_; }
Isabelle Taylor14674d42018-09-07 11:33:11 +01001211
Primiano Tucci5cb84f82018-10-31 21:46:36 -07001212 const SqlStats& sql_stats() const { return sql_stats_; }
1213 SqlStats* mutable_sql_stats() { return &sql_stats_; }
1214
Isabelle Taylorc8c11202018-11-05 11:36:22 +00001215 const Instants& instants() const { return instants_; }
1216 Instants* mutable_instants() { return &instants_; }
1217
Primiano Tucci2c761ef2019-01-07 20:20:46 +00001218 const AndroidLogs& android_logs() const { return android_log_; }
1219 AndroidLogs* mutable_android_log() { return &android_log_; }
1220
Primiano Tucci0e38a142019-01-07 20:51:09 +00001221 const StatsMap& stats() const { return stats_; }
Lalit Maganti05e8c132018-11-09 18:16:12 +00001222
Ryan Savitski51413ad2019-07-09 14:25:21 +01001223 const Metadata& metadata() const { return metadata_; }
Ryan Savitski0476ee92019-07-09 14:29:33 +01001224 Metadata* mutable_metadata() { return &metadata_; }
Mikhail Khokhlove466c002019-05-23 13:33:33 +01001225
Lalit Maganti6e9c55e2018-11-29 12:00:39 +00001226 const Args& args() const { return args_; }
1227 Args* mutable_args() { return &args_; }
1228
Lalit Maganti1d915a62019-01-07 12:10:42 +00001229 const RawEvents& raw_events() const { return raw_events_; }
1230 RawEvents* mutable_raw_events() { return &raw_events_; }
1231
Oystein Eftevaag5419c582019-08-21 13:58:49 -07001232 const StackProfileMappings& stack_profile_mappings() const {
1233 return stack_profile_mappings_;
Florian Mayer438b5ab2019-05-02 11:18:06 +01001234 }
Oystein Eftevaag5419c582019-08-21 13:58:49 -07001235 StackProfileMappings* mutable_stack_profile_mappings() {
1236 return &stack_profile_mappings_;
Florian Mayer438b5ab2019-05-02 11:18:06 +01001237 }
1238
Oystein Eftevaag5419c582019-08-21 13:58:49 -07001239 const StackProfileFrames& stack_profile_frames() const {
1240 return stack_profile_frames_;
Florian Mayer438b5ab2019-05-02 11:18:06 +01001241 }
Oystein Eftevaag5419c582019-08-21 13:58:49 -07001242 StackProfileFrames* mutable_stack_profile_frames() {
1243 return &stack_profile_frames_;
Florian Mayer438b5ab2019-05-02 11:18:06 +01001244 }
1245
Oystein Eftevaag5419c582019-08-21 13:58:49 -07001246 const StackProfileCallsites& stack_profile_callsites() const {
1247 return stack_profile_callsites_;
Florian Mayer438b5ab2019-05-02 11:18:06 +01001248 }
Oystein Eftevaag5419c582019-08-21 13:58:49 -07001249 StackProfileCallsites* mutable_stack_profile_callsites() {
1250 return &stack_profile_callsites_;
Florian Mayer438b5ab2019-05-02 11:18:06 +01001251 }
1252
1253 const HeapProfileAllocations& heap_profile_allocations() const {
1254 return heap_profile_allocations_;
1255 }
1256 HeapProfileAllocations* mutable_heap_profile_allocations() {
1257 return &heap_profile_allocations_;
1258 }
Oystein Eftevaag7f64c102019-08-29 10:27:31 -07001259 const CpuProfileStackSamples& cpu_profile_stack_samples() const {
1260 return cpu_profile_stack_samples_;
1261 }
1262 CpuProfileStackSamples* mutable_cpu_profile_stack_samples() {
1263 return &cpu_profile_stack_samples_;
1264 }
Florian Mayer438b5ab2019-05-02 11:18:06 +01001265
Lalit Maganti20539c22019-09-04 12:36:10 +01001266 const tables::GpuTrackTable& gpu_track_table() const {
1267 return gpu_track_table_;
1268 }
1269 tables::GpuTrackTable* mutable_gpu_track_table() { return &gpu_track_table_; }
Mikael Pessa7160ccc2019-07-25 11:19:26 -07001270
Lalit Maganti5c454312019-04-08 12:11:17 +01001271 const StringPool& string_pool() const { return string_pool_; }
Lalit Magantiacda68b2018-10-29 15:23:25 +00001272
Isabelle Taylore7003fb2018-07-17 11:39:01 +01001273 // |unique_processes_| always contains at least 1 element becuase the 0th ID
1274 // is reserved to indicate an invalid process.
Lalit Maganti9c6395b2019-01-17 00:25:09 +00001275 size_t process_count() const { return unique_processes_.size(); }
Primiano Tucci0d72a312018-08-07 14:42:45 +01001276
Isabelle Taylore7003fb2018-07-17 11:39:01 +01001277 // |unique_threads_| always contains at least 1 element becuase the 0th ID
1278 // is reserved to indicate an invalid thread.
Lalit Maganti9c6395b2019-01-17 00:25:09 +00001279 size_t thread_count() const { return unique_threads_.size(); }
Isabelle Taylore7003fb2018-07-17 11:39:01 +01001280
Hector Dearman12323362018-08-09 16:09:28 +01001281 // Number of interned strings in the pool. Includes the empty string w/ ID=0.
1282 size_t string_count() const { return string_pool_.size(); }
1283
Ioannis Ilkosb8b11102019-01-29 17:56:55 +00001284 // Start / end ts (in nanoseconds) across the parsed trace events.
1285 // Returns (0, 0) if the trace is empty.
1286 std::pair<int64_t, int64_t> GetTraceTimestampBoundsNs() const;
1287
Lalit Maganticaed37e2018-06-01 03:03:08 +01001288 private:
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +00001289 static constexpr uint8_t kRowIdTableShift = 32;
Isabelle Taylora0a22972018-08-03 12:06:12 +01001290
Primiano Tucci2da5d2e2018-08-10 14:23:31 +01001291 using StringHash = uint64_t;
Lalit Maganticaed37e2018-06-01 03:03:08 +01001292
Lalit Maganti5c454312019-04-08 12:11:17 +01001293 TraceStorage(const TraceStorage&) = delete;
1294 TraceStorage& operator=(const TraceStorage&) = delete;
1295
Lalit Maganti20539c22019-09-04 12:36:10 +01001296 TraceStorage(TraceStorage&&) = delete;
1297 TraceStorage& operator=(TraceStorage&&) = delete;
1298
1299 // One entry for each unique string in the trace.
1300 StringPool string_pool_;
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +00001301
Lalit Maganti05e8c132018-11-09 18:16:12 +00001302 // Stats about parsing the trace.
Primiano Tucci0e38a142019-01-07 20:51:09 +00001303 StatsMap stats_{};
Lalit Maganti35622b72018-06-06 12:03:11 +01001304
Ryan Savitski51413ad2019-07-09 14:25:21 +01001305 // Extra data extracted from the trace. Includes:
1306 // * metadata from chrome and benchmarking infrastructure
1307 // * descriptions of android packages
1308 Metadata metadata_{};
Mikhail Khokhlove466c002019-05-23 13:33:33 +01001309
Lalit Magantid11d3e02019-07-26 12:32:09 +05301310 // Metadata for tracks.
Lalit Magantiac68e9c2019-09-09 18:12:15 +01001311 tables::TrackTable track_table_{&string_pool_, nullptr};
Lalit Magantid11d3e02019-07-26 12:32:09 +05301312
Eric Seckler5703ede2019-07-10 10:13:02 +01001313 // Metadata for virtual slice tracks.
1314 VirtualTracks virtual_tracks_;
1315
Mikael Pessa803090c2019-08-23 16:03:34 -07001316 // Metadata for gpu tracks.
Lalit Maganti20539c22019-09-04 12:36:10 +01001317 tables::GpuTrackTable gpu_track_table_{&string_pool_, nullptr};
Mikael Pessa803090c2019-08-23 16:03:34 -07001318 GpuContexts gpu_contexts_;
1319
Lalit Maganticaed37e2018-06-01 03:03:08 +01001320 // One entry for each CPU in the trace.
Lalit Magantiff69c112018-09-24 12:07:47 +01001321 Slices slices_;
Lalit Maganticaed37e2018-06-01 03:03:08 +01001322
Lalit Maganti6e9c55e2018-11-29 12:00:39 +00001323 // Args for all other tables.
1324 Args args_;
1325
Isabelle Taylor47328cf2018-06-12 14:33:59 +01001326 // One entry for each UniquePid, with UniquePid as the index.
Ioannis Ilkos9c03cbd2019-03-12 18:30:43 +00001327 // Never hold on to pointers to Process, as vector resize will
1328 // invalidate them.
1329 std::vector<Process> unique_processes_;
Isabelle Taylor68e42192018-06-19 16:19:31 +01001330
Isabelle Taylor68e42192018-06-19 16:19:31 +01001331 // One entry for each UniqueTid, with UniqueTid as the index.
Isabelle Taylor3dd366c2018-06-22 16:21:41 +01001332 std::deque<Thread> unique_threads_;
Primiano Tucci0d72a312018-08-07 14:42:45 +01001333
1334 // Slices coming from userspace events (e.g. Chromium TRACE_EVENT macros).
1335 NestableSlices nestable_slices_;
Isabelle Taylor15314ea2018-09-19 11:35:19 +01001336
Eric Secklerd3b89d52019-07-10 15:36:29 +01001337 // Additional attributes for threads slices (sub-type of NestableSlices).
1338 ThreadSlices thread_slices_;
1339
Eric Secklerc3430c62019-07-22 10:49:58 +01001340 // Additional attributes for virtual track slices (sub-type of
1341 // NestableSlices).
1342 VirtualTrackSlices virtual_track_slices_;
1343
Mikael Pessa803090c2019-08-23 16:03:34 -07001344 // Additional attributes for gpu track slices (sub-type of
1345 // NestableSlices).
Lalit Maganti20539c22019-09-04 12:36:10 +01001346 tables::GpuSliceTable gpu_slice_table_{&string_pool_, nullptr};
Mikael Pessa803090c2019-08-23 16:03:34 -07001347
Hector Dearman2442d612019-06-04 18:05:23 +01001348 // The type of counters in the trace. Can be thought of as the "metadata".
Lalit Maganti8320e6d2019-03-14 18:49:33 +00001349 CounterDefinitions counter_definitions_;
1350
1351 // The values from the Counter events from the trace. This includes CPU
1352 // frequency events as well systrace trace_marker counter events.
1353 CounterValues counter_values_;
Primiano Tucci5cb84f82018-10-31 21:46:36 -07001354
1355 SqlStats sql_stats_;
Lalit Maganti6e9c55e2018-11-29 12:00:39 +00001356
Isabelle Taylorc8c11202018-11-05 11:36:22 +00001357 // These are instantaneous events in the trace. They have no duration
1358 // and do not have a value that make sense to track over time.
1359 // e.g. signal events
1360 Instants instants_;
Lalit Maganti1d915a62019-01-07 12:10:42 +00001361
1362 // Raw events are every ftrace event in the trace. The raw event includes
1363 // the timestamp and the pid. The args for the raw event will be in the
1364 // args table. This table can be used to generate a text version of the
1365 // trace.
1366 RawEvents raw_events_;
Primiano Tucci2c761ef2019-01-07 20:20:46 +00001367 AndroidLogs android_log_;
Florian Mayer438b5ab2019-05-02 11:18:06 +01001368
Oystein Eftevaag5419c582019-08-21 13:58:49 -07001369 StackProfileMappings stack_profile_mappings_;
1370 StackProfileFrames stack_profile_frames_;
1371 StackProfileCallsites stack_profile_callsites_;
Florian Mayer438b5ab2019-05-02 11:18:06 +01001372 HeapProfileAllocations heap_profile_allocations_;
Oystein Eftevaag7f64c102019-08-29 10:27:31 -07001373 CpuProfileStackSamples cpu_profile_stack_samples_;
Lalit Maganticaed37e2018-06-01 03:03:08 +01001374};
1375
1376} // namespace trace_processor
1377} // namespace perfetto
1378
Florian Mayerbee52132019-05-02 13:59:56 +01001379namespace std {
1380
1381template <>
Oystein Eftevaag5419c582019-08-21 13:58:49 -07001382struct hash<
1383 ::perfetto::trace_processor::TraceStorage::StackProfileFrames::Row> {
Florian Mayerbee52132019-05-02 13:59:56 +01001384 using argument_type =
Oystein Eftevaag5419c582019-08-21 13:58:49 -07001385 ::perfetto::trace_processor::TraceStorage::StackProfileFrames::Row;
Florian Mayerbee52132019-05-02 13:59:56 +01001386 using result_type = size_t;
1387
1388 result_type operator()(const argument_type& r) const {
1389 return std::hash<::perfetto::trace_processor::StringId>{}(r.name_id) ^
1390 std::hash<int64_t>{}(r.mapping_row) ^ std::hash<int64_t>{}(r.rel_pc);
1391 }
1392};
1393
1394template <>
1395struct hash<
Oystein Eftevaag5419c582019-08-21 13:58:49 -07001396 ::perfetto::trace_processor::TraceStorage::StackProfileCallsites::Row> {
Florian Mayerbee52132019-05-02 13:59:56 +01001397 using argument_type =
Oystein Eftevaag5419c582019-08-21 13:58:49 -07001398 ::perfetto::trace_processor::TraceStorage::StackProfileCallsites::Row;
Florian Mayerbee52132019-05-02 13:59:56 +01001399 using result_type = size_t;
1400
1401 result_type operator()(const argument_type& r) const {
1402 return std::hash<int64_t>{}(r.depth) ^ std::hash<int64_t>{}(r.parent_id) ^
1403 std::hash<int64_t>{}(r.frame_row);
1404 }
1405};
1406
1407template <>
1408struct hash<
Oystein Eftevaag5419c582019-08-21 13:58:49 -07001409 ::perfetto::trace_processor::TraceStorage::StackProfileMappings::Row> {
Florian Mayerbee52132019-05-02 13:59:56 +01001410 using argument_type =
Oystein Eftevaag5419c582019-08-21 13:58:49 -07001411 ::perfetto::trace_processor::TraceStorage::StackProfileMappings::Row;
Florian Mayerbee52132019-05-02 13:59:56 +01001412 using result_type = size_t;
1413
1414 result_type operator()(const argument_type& r) const {
1415 return std::hash<::perfetto::trace_processor::StringId>{}(r.build_id) ^
Florian Mayer12655732019-07-02 15:08:26 +01001416 std::hash<int64_t>{}(r.exact_offset) ^
1417 std::hash<int64_t>{}(r.start_offset) ^
1418 std::hash<int64_t>{}(r.start) ^ std::hash<int64_t>{}(r.end) ^
1419 std::hash<int64_t>{}(r.load_bias) ^
Florian Mayerbee52132019-05-02 13:59:56 +01001420 std::hash<::perfetto::trace_processor::StringId>{}(r.name_id);
1421 }
1422};
1423
1424} // namespace std
1425
Lalit Maganticaed37e2018-06-01 03:03:08 +01001426#endif // SRC_TRACE_PROCESSOR_TRACE_STORAGE_H_