blob: 64918fff7eafbd31fb22eb33de94184468705517 [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"
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,
Ryan Savitski51413ad2019-07-09 14:25:21 +010064 kMetadataTable = 6,
Lalit Maganti5ea9e932018-11-30 14:19:39 +000065};
66
67// The top 8 bits are set to the TableId and the bottom 32 to the row of the
68// table.
Lalit Maganti85ca4a82018-12-07 17:28:02 +000069using RowId = int64_t;
Lalit Maganti5ea9e932018-11-30 14:19:39 +000070static const RowId kInvalidRowId = 0;
71
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +000072using ArgSetId = uint32_t;
73static const ArgSetId kInvalidArgSetId = 0;
74
Eric Seckler5703ede2019-07-10 10:13:02 +010075using TrackId = uint32_t;
76
77enum class VirtualTrackScope : uint8_t {
78 // VirtualTrack with global scope, will not have a |upid| set.
79 kGlobal = 0,
80 // VirtualTrack associated with a specific process via |upid|.
81 kProcess = 1
82};
83
Isabelle Taylora97c5f52018-10-23 17:36:12 +010084enum RefType {
Primiano Tucci5403e4f2018-11-27 10:07:03 +000085 kRefNoRef = 0,
86 kRefUtid = 1,
87 kRefCpuId = 2,
88 kRefIrq = 3,
89 kRefSoftIrq = 4,
90 kRefUpid = 5,
Sidath Senanayake1f5f93a2019-06-06 22:24:15 +010091 kRefGpuId = 6,
Eric Seckler5703ede2019-07-10 10:13:02 +010092 kRefTrack = 7,
Primiano Tucci5403e4f2018-11-27 10:07:03 +000093 kRefMax
Isabelle Taylora97c5f52018-10-23 17:36:12 +010094};
Isabelle Taylor14674d42018-09-07 11:33:11 +010095
Eric Seckler972225e2019-04-18 11:07:12 +010096const std::vector<const char*>& GetRefTypeStringMap();
97
Lalit Maganticaed37e2018-06-01 03:03:08 +010098// Stores a data inside a trace file in a columnar form. This makes it efficient
99// to read or search across a single field of the trace (e.g. all the thread
100// names for a given CPU).
101class TraceStorage {
102 public:
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100103 TraceStorage();
Isabelle Taylora0a22972018-08-03 12:06:12 +0100104
105 virtual ~TraceStorage();
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100106
Isabelle Taylora0a22972018-08-03 12:06:12 +0100107 // Information about a unique process seen in a trace.
108 struct Process {
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100109 explicit Process(uint32_t p) : pid(p) {}
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000110 int64_t start_ns = 0;
Lalit Maganti637589a2019-07-04 17:25:29 +0100111 int64_t end_ns = 0;
Isabelle Taylora0a22972018-08-03 12:06:12 +0100112 StringId name_id = 0;
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100113 uint32_t pid = 0;
Lalit Maganti369b0572019-07-11 15:35:09 +0100114 base::Optional<UniquePid> parent_upid;
Isabelle Taylora0a22972018-08-03 12:06:12 +0100115 };
116
117 // Information about a unique thread seen in a trace.
118 struct Thread {
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100119 explicit Thread(uint32_t t) : tid(t) {}
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000120 int64_t start_ns = 0;
Lalit Magantib5bd2332019-06-06 14:20:47 +0100121 int64_t end_ns = 0;
Isabelle Taylora0a22972018-08-03 12:06:12 +0100122 StringId name_id = 0;
Lalit Maganti770886a2018-11-16 17:40:21 +0000123 base::Optional<UniquePid> upid;
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100124 uint32_t tid = 0;
Isabelle Taylora0a22972018-08-03 12:06:12 +0100125 };
Isabelle Taylor3dd366c2018-06-22 16:21:41 +0100126
Lalit Maganti6e9c55e2018-11-29 12:00:39 +0000127 // Generic key value storage which can be referenced by other tables.
128 class Args {
129 public:
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000130 struct Arg {
131 StringId flat_key = 0;
132 StringId key = 0;
133 Variadic value = Variadic::Integer(0);
134
135 // This is only used by the arg tracker and so is not part of the hash.
136 RowId row_id = 0;
137 };
138
139 struct ArgHasher {
140 uint64_t operator()(const Arg& arg) const noexcept {
Lalit Maganti1f464742019-02-28 13:49:31 +0000141 base::Hash hash;
142 hash.Update(arg.key);
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000143 // We don't hash arg.flat_key because it's a subsequence of arg.key.
144 switch (arg.value.type) {
145 case Variadic::Type::kInt:
Lalit Maganti1f464742019-02-28 13:49:31 +0000146 hash.Update(arg.value.int_value);
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000147 break;
Eric Secklerc93823e2019-06-03 16:49:19 +0100148 case Variadic::Type::kUint:
149 hash.Update(arg.value.uint_value);
150 break;
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000151 case Variadic::Type::kString:
Lalit Maganti1f464742019-02-28 13:49:31 +0000152 hash.Update(arg.value.string_value);
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000153 break;
154 case Variadic::Type::kReal:
Lalit Maganti1f464742019-02-28 13:49:31 +0000155 hash.Update(arg.value.real_value);
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000156 break;
Eric Secklerc93823e2019-06-03 16:49:19 +0100157 case Variadic::Type::kPointer:
158 hash.Update(arg.value.pointer_value);
159 break;
160 case Variadic::Type::kBool:
161 hash.Update(arg.value.bool_value);
162 break;
163 case Variadic::Type::kJson:
164 hash.Update(arg.value.json_value);
165 break;
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000166 }
Lalit Maganti1f464742019-02-28 13:49:31 +0000167 return hash.digest();
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000168 }
169 };
170
171 const std::deque<ArgSetId>& set_ids() const { return set_ids_; }
Lalit Maganti5ea9e932018-11-30 14:19:39 +0000172 const std::deque<StringId>& flat_keys() const { return flat_keys_; }
173 const std::deque<StringId>& keys() const { return keys_; }
Lalit Maganti1d915a62019-01-07 12:10:42 +0000174 const std::deque<Variadic>& arg_values() const { return arg_values_; }
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000175 uint32_t args_count() const {
176 return static_cast<uint32_t>(set_ids_.size());
Lalit Maganti79472be2018-12-04 13:41:27 +0000177 }
Lalit Maganti6e9c55e2018-11-29 12:00:39 +0000178
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000179 ArgSetId AddArgSet(const std::vector<Arg>& args,
180 uint32_t begin,
181 uint32_t end) {
Lalit Maganti1f464742019-02-28 13:49:31 +0000182 base::Hash hash;
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000183 for (uint32_t i = begin; i < end; i++) {
Lalit Maganti1f464742019-02-28 13:49:31 +0000184 hash.Update(ArgHasher()(args[i]));
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000185 }
Lalit Maganti5ea9e932018-11-30 14:19:39 +0000186
Lalit Maganti1f464742019-02-28 13:49:31 +0000187 ArgSetHash digest = hash.digest();
188 auto it = arg_row_for_hash_.find(digest);
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000189 if (it != arg_row_for_hash_.end()) {
190 return set_ids_[it->second];
191 }
192
193 // The +1 ensures that nothing has an id == kInvalidArgSetId == 0.
194 ArgSetId id = static_cast<uint32_t>(arg_row_for_hash_.size()) + 1;
Lalit Maganti1f464742019-02-28 13:49:31 +0000195 arg_row_for_hash_.emplace(digest, args_count());
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000196 for (uint32_t i = begin; i < end; i++) {
197 const auto& arg = args[i];
198 set_ids_.emplace_back(id);
199 flat_keys_.emplace_back(arg.flat_key);
200 keys_.emplace_back(arg.key);
201 arg_values_.emplace_back(arg.value);
202 }
203 return id;
Lalit Maganti6e9c55e2018-11-29 12:00:39 +0000204 }
205
Lalit Maganti5ea9e932018-11-30 14:19:39 +0000206 private:
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000207 using ArgSetHash = uint64_t;
208
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000209 std::deque<ArgSetId> set_ids_;
Lalit Maganti5ea9e932018-11-30 14:19:39 +0000210 std::deque<StringId> flat_keys_;
211 std::deque<StringId> keys_;
Lalit Maganti1d915a62019-01-07 12:10:42 +0000212 std::deque<Variadic> arg_values_;
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000213
214 std::unordered_map<ArgSetHash, uint32_t> arg_row_for_hash_;
Lalit Maganti6e9c55e2018-11-29 12:00:39 +0000215 };
216
Lalit Magantid11d3e02019-07-26 12:32:09 +0530217 class Tracks {
218 public:
219 inline uint32_t AddTrack(StringId name) {
220 names_.emplace_back(name);
221 return track_count() - 1;
222 }
223
224 uint32_t track_count() const {
225 return static_cast<uint32_t>(names_.size());
226 }
227
228 const std::deque<StringId>& names() const { return names_; }
229
230 private:
231 std::deque<StringId> names_;
232 };
233
Eric Seckler5703ede2019-07-10 10:13:02 +0100234 class VirtualTracks {
235 public:
Lalit Magantid11d3e02019-07-26 12:32:09 +0530236 inline void AddVirtualTrack(TrackId track_id,
237 VirtualTrackScope scope,
238 UniquePid upid = 0u) {
Eric Seckler5703ede2019-07-10 10:13:02 +0100239 track_ids_.emplace_back(track_id);
Eric Seckler5703ede2019-07-10 10:13:02 +0100240 scopes_.emplace_back(scope);
241 upids_.emplace_back(upid);
Eric Seckler5703ede2019-07-10 10:13:02 +0100242 }
243
244 uint32_t virtual_track_count() const {
245 return static_cast<uint32_t>(track_ids_.size());
246 }
247
Lalit Magantid11d3e02019-07-26 12:32:09 +0530248 base::Optional<uint32_t> FindRowForTrackId(uint32_t track_id) const {
249 auto it =
250 std::lower_bound(track_ids().begin(), track_ids().end(), track_id);
251 if (it != track_ids().end() && *it == track_id) {
252 return static_cast<uint32_t>(std::distance(track_ids().begin(), it));
253 }
254 return base::nullopt;
255 }
256
Eric Seckler5703ede2019-07-10 10:13:02 +0100257 const std::deque<uint32_t>& track_ids() const { return track_ids_; }
Eric Seckler5703ede2019-07-10 10:13:02 +0100258 const std::deque<VirtualTrackScope>& scopes() const { return scopes_; }
259 const std::deque<UniquePid>& upids() const { return upids_; }
260
261 private:
262 std::deque<uint32_t> track_ids_;
Eric Seckler5703ede2019-07-10 10:13:02 +0100263 std::deque<VirtualTrackScope> scopes_;
264 std::deque<UniquePid> upids_;
265 };
266
Mikael Pessa803090c2019-08-23 16:03:34 -0700267 class GpuTracks {
268 public:
269 inline void AddGpuTrack(TrackId track_id,
270 StringId scope,
271 // For all below: null == numeric_limits<int_type>::max()
272 uint64_t context_id) {
273 track_ids_.emplace_back(track_id);
274 scopes_.emplace_back(scope);
275 context_ids_.emplace_back(context_id);
276 }
277
278 uint32_t gpu_track_count() const {
279 return static_cast<uint32_t>(track_ids_.size());
280 }
281
282 base::Optional<uint32_t> FindRowForTrackId(uint32_t track_id) const {
283 auto it =
284 std::lower_bound(track_ids().begin(), track_ids().end(), track_id);
285 if (it != track_ids().end() && *it == track_id) {
286 return static_cast<uint32_t>(std::distance(track_ids().begin(), it));
287 }
288 return base::nullopt;
289 }
290
291 const std::deque<TrackId>& track_ids() const { return track_ids_; }
292 const std::deque<StringId>& scopes() const { return scopes_; }
293 const std::deque<uint64_t>& context_ids() const {
294 return context_ids_;
295 }
296
297 private:
298 std::deque<TrackId> track_ids_;
299 std::deque<StringId> scopes_;
300
301 // For all below: null == numeric_limits<int_type>::max()
302 std::deque<uint64_t> context_ids_;
303 };
304
305 class GpuContexts {
306 public:
307 inline void AddGpuContext(uint64_t context_id,
308 UniquePid upid,
309 uint32_t priority) {
310 context_ids_.emplace_back(context_id);
311 upids_.emplace_back(upid);
312 priorities_.emplace_back(priority);
313 }
314
315 uint32_t gpu_context_count() const {
316 return static_cast<uint32_t>(context_ids_.size());
317 }
318
319 const std::deque<uint64_t>& context_ids() const { return context_ids_; }
320 const std::deque<UniquePid>& upids() const { return upids_; }
321 const std::deque<uint32_t>& priorities() const { return priorities_; }
322
323 private:
324 std::deque<uint64_t> context_ids_;
325 std::deque<UniquePid> upids_;
326 std::deque<uint32_t> priorities_;
327 };
328
Lalit Magantiff69c112018-09-24 12:07:47 +0100329 class Slices {
Lalit Maganti35622b72018-06-06 12:03:11 +0100330 public:
Lalit Magantifde29042018-10-04 13:28:52 +0100331 inline size_t AddSlice(uint32_t cpu,
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000332 int64_t start_ns,
333 int64_t duration_ns,
Lalit Magantib0b53ee2019-01-24 17:53:39 +0000334 UniqueTid utid,
335 ftrace_utils::TaskState end_state,
336 int32_t priority) {
Lalit Magantiff69c112018-09-24 12:07:47 +0100337 cpus_.emplace_back(cpu);
Lalit Maganti35622b72018-06-06 12:03:11 +0100338 start_ns_.emplace_back(start_ns);
339 durations_.emplace_back(duration_ns);
Isabelle Taylora0a22972018-08-03 12:06:12 +0100340 utids_.emplace_back(utid);
Lalit Magantib0b53ee2019-01-24 17:53:39 +0000341 end_states_.emplace_back(end_state);
342 priorities_.emplace_back(priority);
Lalit Maganti58638932019-01-31 10:48:26 +0000343
344 if (utid >= rows_for_utids_.size())
345 rows_for_utids_.resize(utid + 1);
346 rows_for_utids_[utid].emplace_back(slice_count() - 1);
Lalit Magantifde29042018-10-04 13:28:52 +0100347 return slice_count() - 1;
348 }
349
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000350 void set_duration(size_t index, int64_t duration_ns) {
Lalit Magantifde29042018-10-04 13:28:52 +0100351 durations_[index] = duration_ns;
Lalit Maganti35622b72018-06-06 12:03:11 +0100352 }
353
Lalit Magantib0b53ee2019-01-24 17:53:39 +0000354 void set_end_state(size_t index, ftrace_utils::TaskState end_state) {
355 end_states_[index] = end_state;
356 }
357
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100358 size_t slice_count() const { return start_ns_.size(); }
Lalit Maganti35622b72018-06-06 12:03:11 +0100359
Lalit Magantiff69c112018-09-24 12:07:47 +0100360 const std::deque<uint32_t>& cpus() const { return cpus_; }
361
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000362 const std::deque<int64_t>& start_ns() const { return start_ns_; }
Lalit Maganti35622b72018-06-06 12:03:11 +0100363
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000364 const std::deque<int64_t>& durations() const { return durations_; }
Lalit Maganti35622b72018-06-06 12:03:11 +0100365
Isabelle Taylor68e42192018-06-19 16:19:31 +0100366 const std::deque<UniqueTid>& utids() const { return utids_; }
367
Lalit Magantib0b53ee2019-01-24 17:53:39 +0000368 const std::deque<ftrace_utils::TaskState>& end_state() const {
369 return end_states_;
370 }
371
372 const std::deque<int32_t>& priorities() const { return priorities_; }
373
Lalit Maganti58638932019-01-31 10:48:26 +0000374 const std::deque<std::vector<uint32_t>>& rows_for_utids() const {
Lalit Magantif0f09c32019-01-18 17:29:31 +0000375 return rows_for_utids_;
376 }
377
Lalit Maganti35622b72018-06-06 12:03:11 +0100378 private:
Hector Dearman947f12a2018-09-11 16:50:36 +0100379 // Each deque below has the same number of entries (the number of slices
Lalit Maganti35622b72018-06-06 12:03:11 +0100380 // in the trace for the CPU).
Lalit Magantiff69c112018-09-24 12:07:47 +0100381 std::deque<uint32_t> cpus_;
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000382 std::deque<int64_t> start_ns_;
383 std::deque<int64_t> durations_;
Isabelle Taylor68e42192018-06-19 16:19:31 +0100384 std::deque<UniqueTid> utids_;
Lalit Magantib0b53ee2019-01-24 17:53:39 +0000385 std::deque<ftrace_utils::TaskState> end_states_;
386 std::deque<int32_t> priorities_;
Lalit Maganti58638932019-01-31 10:48:26 +0000387
388 // One row per utid.
389 std::deque<std::vector<uint32_t>> rows_for_utids_;
Primiano Tucci0d72a312018-08-07 14:42:45 +0100390 };
Isabelle Taylor68e42192018-06-19 16:19:31 +0100391
Primiano Tucci0d72a312018-08-07 14:42:45 +0100392 class NestableSlices {
393 public:
Eric Seckler70cc4422019-05-28 16:00:23 +0100394 inline uint32_t AddSlice(int64_t start_ns,
395 int64_t duration_ns,
396 int64_t ref,
397 RefType type,
Lalit Maganti81a02cd2019-07-12 17:05:11 +0100398 StringId category,
Eric Seckler70cc4422019-05-28 16:00:23 +0100399 StringId name,
400 uint8_t depth,
401 int64_t stack_id,
402 int64_t parent_stack_id) {
Primiano Tucci0d72a312018-08-07 14:42:45 +0100403 start_ns_.emplace_back(start_ns);
404 durations_.emplace_back(duration_ns);
Eric Seckler972225e2019-04-18 11:07:12 +0100405 refs_.emplace_back(ref);
406 types_.emplace_back(type);
Lalit Maganti81a02cd2019-07-12 17:05:11 +0100407 categories_.emplace_back(category);
Primiano Tucci0d72a312018-08-07 14:42:45 +0100408 names_.emplace_back(name);
409 depths_.emplace_back(depth);
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100410 stack_ids_.emplace_back(stack_id);
411 parent_stack_ids_.emplace_back(parent_stack_id);
Eric Seckler70cc4422019-05-28 16:00:23 +0100412 arg_set_ids_.emplace_back(kInvalidArgSetId);
Lalit Maganti7b37bbf2018-11-09 15:58:54 +0000413 return slice_count() - 1;
414 }
415
Eric Seckler70cc4422019-05-28 16:00:23 +0100416 void set_duration(uint32_t index, int64_t duration_ns) {
Lalit Maganti7b37bbf2018-11-09 15:58:54 +0000417 durations_[index] = duration_ns;
418 }
419
Eric Seckler70cc4422019-05-28 16:00:23 +0100420 void set_stack_id(uint32_t index, int64_t stack_id) {
Lalit Maganti7b37bbf2018-11-09 15:58:54 +0000421 stack_ids_[index] = stack_id;
Primiano Tucci0d72a312018-08-07 14:42:45 +0100422 }
423
Eric Seckler70cc4422019-05-28 16:00:23 +0100424 void set_arg_set_id(uint32_t index, ArgSetId id) {
425 arg_set_ids_[index] = id;
426 }
427
428 uint32_t slice_count() const {
429 return static_cast<uint32_t>(start_ns_.size());
430 }
431
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000432 const std::deque<int64_t>& start_ns() const { return start_ns_; }
433 const std::deque<int64_t>& durations() const { return durations_; }
Eric Seckler972225e2019-04-18 11:07:12 +0100434 const std::deque<int64_t>& refs() const { return refs_; }
435 const std::deque<RefType>& types() const { return types_; }
Lalit Maganti81a02cd2019-07-12 17:05:11 +0100436 const std::deque<StringId>& categories() const { return categories_; }
Primiano Tucci0d72a312018-08-07 14:42:45 +0100437 const std::deque<StringId>& names() const { return names_; }
438 const std::deque<uint8_t>& depths() const { return depths_; }
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000439 const std::deque<int64_t>& stack_ids() const { return stack_ids_; }
440 const std::deque<int64_t>& parent_stack_ids() const {
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100441 return parent_stack_ids_;
442 }
Eric Seckler70cc4422019-05-28 16:00:23 +0100443 const std::deque<ArgSetId>& arg_set_ids() const { return arg_set_ids_; }
Primiano Tucci0d72a312018-08-07 14:42:45 +0100444
445 private:
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000446 std::deque<int64_t> start_ns_;
447 std::deque<int64_t> durations_;
Eric Seckler972225e2019-04-18 11:07:12 +0100448 std::deque<int64_t> refs_;
449 std::deque<RefType> types_;
Lalit Maganti81a02cd2019-07-12 17:05:11 +0100450 std::deque<StringId> categories_;
Primiano Tucci0d72a312018-08-07 14:42:45 +0100451 std::deque<StringId> names_;
452 std::deque<uint8_t> depths_;
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000453 std::deque<int64_t> stack_ids_;
454 std::deque<int64_t> parent_stack_ids_;
Eric Seckler70cc4422019-05-28 16:00:23 +0100455 std::deque<ArgSetId> arg_set_ids_;
Lalit Maganti35622b72018-06-06 12:03:11 +0100456 };
457
Eric Secklerd3b89d52019-07-10 15:36:29 +0100458 class ThreadSlices {
459 public:
460 inline uint32_t AddThreadSlice(uint32_t slice_id,
461 int64_t thread_timestamp_ns,
462 int64_t thread_duration_ns,
463 int64_t thread_instruction_count,
464 int64_t thread_instruction_delta) {
465 slice_ids_.emplace_back(slice_id);
466 thread_timestamp_ns_.emplace_back(thread_timestamp_ns);
467 thread_duration_ns_.emplace_back(thread_duration_ns);
468 thread_instruction_counts_.emplace_back(thread_instruction_count);
469 thread_instruction_deltas_.emplace_back(thread_instruction_delta);
470 return slice_count() - 1;
471 }
472
Eric Secklerc3430c62019-07-22 10:49:58 +0100473 uint32_t slice_count() const {
474 return static_cast<uint32_t>(slice_ids_.size());
Eric Secklerd3b89d52019-07-10 15:36:29 +0100475 }
476
Eric Secklerc3430c62019-07-22 10:49:58 +0100477 const std::deque<uint32_t>& slice_ids() const { return slice_ids_; }
478 const std::deque<int64_t>& thread_timestamp_ns() const {
479 return thread_timestamp_ns_;
480 }
481 const std::deque<int64_t>& thread_duration_ns() const {
482 return thread_duration_ns_;
483 }
484 const std::deque<int64_t>& thread_instruction_counts() const {
485 return thread_instruction_counts_;
486 }
487 const std::deque<int64_t>& thread_instruction_deltas() const {
488 return thread_instruction_deltas_;
489 }
490
491 base::Optional<uint32_t> FindRowForSliceId(uint32_t slice_id) const {
492 auto it =
493 std::lower_bound(slice_ids().begin(), slice_ids().end(), slice_id);
494 if (it != slice_ids().end() && *it == slice_id) {
495 return static_cast<uint32_t>(std::distance(slice_ids().begin(), it));
496 }
497 return base::nullopt;
498 }
499
Eric Seckler54f30a32019-07-19 15:10:29 +0100500 void UpdateThreadDeltasForSliceId(uint32_t slice_id,
501 int64_t end_thread_timestamp_ns,
502 int64_t end_thread_instruction_count) {
Eric Secklerc3430c62019-07-22 10:49:58 +0100503 uint32_t row = *FindRowForSliceId(slice_id);
504 int64_t begin_ns = thread_timestamp_ns_[row];
505 thread_duration_ns_[row] = end_thread_timestamp_ns - begin_ns;
Eric Seckler54f30a32019-07-19 15:10:29 +0100506 int64_t begin_ticount = thread_instruction_counts_[row];
507 thread_instruction_deltas_[row] =
508 end_thread_instruction_count - begin_ticount;
Eric Secklerc3430c62019-07-22 10:49:58 +0100509 }
510
511 private:
512 std::deque<uint32_t> slice_ids_;
513 std::deque<int64_t> thread_timestamp_ns_;
514 std::deque<int64_t> thread_duration_ns_;
515 std::deque<int64_t> thread_instruction_counts_;
516 std::deque<int64_t> thread_instruction_deltas_;
517 };
518
519 class VirtualTrackSlices {
520 public:
521 inline uint32_t AddVirtualTrackSlice(uint32_t slice_id,
522 int64_t thread_timestamp_ns,
523 int64_t thread_duration_ns,
524 int64_t thread_instruction_count,
525 int64_t thread_instruction_delta) {
526 slice_ids_.emplace_back(slice_id);
527 thread_timestamp_ns_.emplace_back(thread_timestamp_ns);
528 thread_duration_ns_.emplace_back(thread_duration_ns);
529 thread_instruction_counts_.emplace_back(thread_instruction_count);
530 thread_instruction_deltas_.emplace_back(thread_instruction_delta);
531 return slice_count() - 1;
Eric Secklerd3b89d52019-07-10 15:36:29 +0100532 }
533
534 uint32_t slice_count() const {
535 return static_cast<uint32_t>(slice_ids_.size());
536 }
537
538 const std::deque<uint32_t>& slice_ids() const { return slice_ids_; }
539 const std::deque<int64_t>& thread_timestamp_ns() const {
540 return thread_timestamp_ns_;
541 }
542 const std::deque<int64_t>& thread_duration_ns() const {
543 return thread_duration_ns_;
544 }
545 const std::deque<int64_t>& thread_instruction_counts() const {
546 return thread_instruction_counts_;
547 }
548 const std::deque<int64_t>& thread_instruction_deltas() const {
549 return thread_instruction_deltas_;
550 }
551
Mikhail Khokhlov7db04912019-07-18 16:11:11 +0100552 base::Optional<uint32_t> FindRowForSliceId(uint32_t slice_id) const {
Eric Secklerd3b89d52019-07-10 15:36:29 +0100553 auto it =
554 std::lower_bound(slice_ids().begin(), slice_ids().end(), slice_id);
Mikhail Khokhlov7db04912019-07-18 16:11:11 +0100555 if (it != slice_ids().end() && *it == slice_id) {
556 return static_cast<uint32_t>(std::distance(slice_ids().begin(), it));
557 }
558 return base::nullopt;
Eric Secklerd3b89d52019-07-10 15:36:29 +0100559 }
560
Eric Seckler54f30a32019-07-19 15:10:29 +0100561 void UpdateThreadDeltasForSliceId(uint32_t slice_id,
562 int64_t end_thread_timestamp_ns,
563 int64_t end_thread_instruction_count) {
Mikhail Khokhlov7db04912019-07-18 16:11:11 +0100564 uint32_t row = *FindRowForSliceId(slice_id);
Eric Secklerd3b89d52019-07-10 15:36:29 +0100565 int64_t begin_ns = thread_timestamp_ns_[row];
566 thread_duration_ns_[row] = end_thread_timestamp_ns - begin_ns;
Eric Seckler54f30a32019-07-19 15:10:29 +0100567 int64_t begin_ticount = thread_instruction_counts_[row];
568 thread_instruction_deltas_[row] =
569 end_thread_instruction_count - begin_ticount;
Eric Secklerd3b89d52019-07-10 15:36:29 +0100570 }
571
572 private:
573 std::deque<uint32_t> slice_ids_;
574 std::deque<int64_t> thread_timestamp_ns_;
575 std::deque<int64_t> thread_duration_ns_;
576 std::deque<int64_t> thread_instruction_counts_;
577 std::deque<int64_t> thread_instruction_deltas_;
578 };
579
Mikael Pessa803090c2019-08-23 16:03:34 -0700580 class GpuSlices {
581 public:
582 inline uint32_t AddGpuSlice(uint32_t slice_id,
583 // For all below: null == numeric_limits<int_type>::max()
584 uint64_t context_id,
585 uint64_t render_target,
586 uint32_t frame_id,
587 uint32_t job_id,
588 uint32_t hw_queue_id) {
589 slice_ids_.emplace_back(slice_id);
590 context_ids_.emplace_back(context_id);
591 render_targets_.emplace_back(render_target);
592 frame_ids_.emplace_back(frame_id);
593 job_ids_.emplace_back(job_id);
594 hw_queue_ids_.emplace_back(hw_queue_id);
595 return slice_count() - 1;
596 }
597
598 uint32_t slice_count() const {
599 return static_cast<uint32_t>(slice_ids_.size());
600 }
601
602 const std::deque<uint32_t>& slice_ids() const { return slice_ids_; }
603 const std::deque<uint64_t>& context_ids() const {
604 return context_ids_;
605 }
606 const std::deque<uint64_t>& render_targets() const {
607 return render_targets_;
608 }
609 const std::deque<uint32_t>& frame_ids() const {
610 return frame_ids_;
611 }
612 const std::deque<uint32_t>& job_ids() const {
613 return job_ids_;
614 }
615 const std::deque<uint32_t>& hw_queue_ids() const {
616 return hw_queue_ids_;
617 }
618
619 base::Optional<uint32_t> FindRowForSliceId(uint32_t slice_id) const {
620 auto it =
621 std::lower_bound(slice_ids().begin(), slice_ids().end(), slice_id);
622 if (it != slice_ids().end() && *it == slice_id) {
623 return static_cast<uint32_t>(std::distance(slice_ids().begin(), it));
624 }
625 return base::nullopt;
626 }
627
628 private:
629 std::deque<uint32_t> slice_ids_;
630
631 // For all below: null == numeric_limits<int_type>::max()
632 std::deque<uint64_t> context_ids_;
633 std::deque<uint64_t> render_targets_;
634 std::deque<uint32_t> frame_ids_;
635 std::deque<uint32_t> job_ids_;
636 std::deque<uint32_t> hw_queue_ids_;
637 };
638
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000639 class CounterDefinitions {
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100640 public:
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000641 using Id = uint32_t;
Lalit Maganti521d97b2019-04-29 13:47:03 +0100642 static constexpr Id kInvalidId = std::numeric_limits<Id>::max();
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000643
644 inline Id AddCounterDefinition(StringId name_id,
645 int64_t ref,
646 RefType type) {
647 base::Hash hash;
648 hash.Update(name_id);
649 hash.Update(ref);
650 hash.Update(type);
651
652 // TODO(lalitm): this is a perf bottleneck and likely we can do something
653 // quite a bit better here.
654 uint64_t digest = hash.digest();
655 auto it = hash_to_row_idx_.find(digest);
656 if (it != hash_to_row_idx_.end())
657 return it->second;
658
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100659 name_ids_.emplace_back(name_id);
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100660 refs_.emplace_back(ref);
661 types_.emplace_back(type);
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000662 hash_to_row_idx_.emplace(digest, size() - 1);
663 return size() - 1;
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100664 }
Lalit Magantifde29042018-10-04 13:28:52 +0100665
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000666 uint32_t size() const { return static_cast<uint32_t>(name_ids_.size()); }
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100667
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100668 const std::deque<StringId>& name_ids() const { return name_ids_; }
669
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100670 const std::deque<int64_t>& refs() const { return refs_; }
671
672 const std::deque<RefType>& types() const { return types_; }
673
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000674 private:
675 std::deque<StringId> name_ids_;
676 std::deque<int64_t> refs_;
677 std::deque<RefType> types_;
678
679 std::unordered_map<uint64_t, uint32_t> hash_to_row_idx_;
680 };
681
682 class CounterValues {
683 public:
684 inline uint32_t AddCounterValue(CounterDefinitions::Id counter_id,
685 int64_t timestamp,
686 double value) {
687 counter_ids_.emplace_back(counter_id);
688 timestamps_.emplace_back(timestamp);
689 values_.emplace_back(value);
690 arg_set_ids_.emplace_back(kInvalidArgSetId);
Ioannis Ilkosf129c262019-04-25 14:53:51 +0100691
Lalit Maganti521d97b2019-04-29 13:47:03 +0100692 if (counter_id != CounterDefinitions::kInvalidId) {
693 if (counter_id >= rows_for_counter_id_.size()) {
694 rows_for_counter_id_.resize(counter_id + 1);
695 }
696 rows_for_counter_id_[counter_id].emplace_back(size() - 1);
697 }
698 return size() - 1;
699 }
700
701 void set_counter_id(uint32_t index, CounterDefinitions::Id counter_id) {
702 PERFETTO_DCHECK(counter_ids_[index] == CounterDefinitions::kInvalidId);
703
704 counter_ids_[index] = counter_id;
Ioannis Ilkosf129c262019-04-25 14:53:51 +0100705 if (counter_id >= rows_for_counter_id_.size()) {
706 rows_for_counter_id_.resize(counter_id + 1);
707 }
Lalit Maganti521d97b2019-04-29 13:47:03 +0100708
709 auto* new_rows = &rows_for_counter_id_[counter_id];
710 new_rows->insert(
711 std::upper_bound(new_rows->begin(), new_rows->end(), index), index);
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000712 }
713
714 void set_arg_set_id(uint32_t row, ArgSetId id) { arg_set_ids_[row] = id; }
715
716 uint32_t size() const { return static_cast<uint32_t>(counter_ids_.size()); }
717
718 const std::deque<CounterDefinitions::Id>& counter_ids() const {
719 return counter_ids_;
720 }
721
722 const std::deque<int64_t>& timestamps() const { return timestamps_; }
723
724 const std::deque<double>& values() const { return values_; }
725
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000726 const std::deque<ArgSetId>& arg_set_ids() const { return arg_set_ids_; }
727
Ioannis Ilkosf129c262019-04-25 14:53:51 +0100728 const std::deque<std::vector<uint32_t>>& rows_for_counter_id() const {
729 return rows_for_counter_id_;
730 }
731
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100732 private:
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000733 std::deque<CounterDefinitions::Id> counter_ids_;
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000734 std::deque<int64_t> timestamps_;
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100735 std::deque<double> values_;
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000736 std::deque<ArgSetId> arg_set_ids_;
Ioannis Ilkosf129c262019-04-25 14:53:51 +0100737
738 // Indexed by counter_id value and contains the row numbers corresponding to
739 // it.
740 std::deque<std::vector<uint32_t>> rows_for_counter_id_;
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100741 };
742
Primiano Tucci5cb84f82018-10-31 21:46:36 -0700743 class SqlStats {
744 public:
745 static constexpr size_t kMaxLogEntries = 100;
Lalit Magantiaac2f652019-04-30 12:16:21 +0100746 uint32_t RecordQueryBegin(const std::string& query,
747 int64_t time_queued,
748 int64_t time_started);
749 void RecordQueryFirstNext(uint32_t row, int64_t time_first_next);
750 void RecordQueryEnd(uint32_t row, int64_t time_end);
Primiano Tucci5cb84f82018-10-31 21:46:36 -0700751 size_t size() const { return queries_.size(); }
752 const std::deque<std::string>& queries() const { return queries_; }
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000753 const std::deque<int64_t>& times_queued() const { return times_queued_; }
754 const std::deque<int64_t>& times_started() const { return times_started_; }
Lalit Magantiaac2f652019-04-30 12:16:21 +0100755 const std::deque<int64_t>& times_first_next() const {
756 return times_first_next_;
757 }
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000758 const std::deque<int64_t>& times_ended() const { return times_ended_; }
Primiano Tucci5cb84f82018-10-31 21:46:36 -0700759
760 private:
Lalit Magantiaac2f652019-04-30 12:16:21 +0100761 uint32_t popped_queries_ = 0;
762
Primiano Tucci5cb84f82018-10-31 21:46:36 -0700763 std::deque<std::string> queries_;
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000764 std::deque<int64_t> times_queued_;
765 std::deque<int64_t> times_started_;
Lalit Magantiaac2f652019-04-30 12:16:21 +0100766 std::deque<int64_t> times_first_next_;
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000767 std::deque<int64_t> times_ended_;
Primiano Tucci5cb84f82018-10-31 21:46:36 -0700768 };
769
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000770 class Instants {
771 public:
Lalit Maganti66ed7ad2019-01-11 16:47:26 +0000772 inline uint32_t AddInstantEvent(int64_t timestamp,
773 StringId name_id,
774 double value,
775 int64_t ref,
776 RefType type) {
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000777 timestamps_.emplace_back(timestamp);
778 name_ids_.emplace_back(name_id);
779 values_.emplace_back(value);
780 refs_.emplace_back(ref);
781 types_.emplace_back(type);
Lalit Maganti1c21d172019-02-07 10:48:24 +0000782 arg_set_ids_.emplace_back(kInvalidArgSetId);
Lalit Maganti66ed7ad2019-01-11 16:47:26 +0000783 return static_cast<uint32_t>(instant_count() - 1);
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000784 }
785
Lalit Maganti521d97b2019-04-29 13:47:03 +0100786 void set_ref(uint32_t row, int64_t ref) { refs_[row] = ref; }
787
Lalit Maganti1c21d172019-02-07 10:48:24 +0000788 void set_arg_set_id(uint32_t row, ArgSetId id) { arg_set_ids_[row] = id; }
789
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000790 size_t instant_count() const { return timestamps_.size(); }
791
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000792 const std::deque<int64_t>& timestamps() const { return timestamps_; }
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000793
794 const std::deque<StringId>& name_ids() const { return name_ids_; }
795
796 const std::deque<double>& values() const { return values_; }
797
798 const std::deque<int64_t>& refs() const { return refs_; }
799
800 const std::deque<RefType>& types() const { return types_; }
801
Lalit Maganti1c21d172019-02-07 10:48:24 +0000802 const std::deque<ArgSetId>& arg_set_ids() const { return arg_set_ids_; }
803
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000804 private:
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000805 std::deque<int64_t> timestamps_;
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000806 std::deque<StringId> name_ids_;
807 std::deque<double> values_;
808 std::deque<int64_t> refs_;
809 std::deque<RefType> types_;
Lalit Maganti1c21d172019-02-07 10:48:24 +0000810 std::deque<ArgSetId> arg_set_ids_;
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000811 };
812
Lalit Maganti1d915a62019-01-07 12:10:42 +0000813 class RawEvents {
814 public:
815 inline RowId AddRawEvent(int64_t timestamp,
816 StringId name_id,
Lalit Magantie87cc812019-01-10 15:20:06 +0000817 uint32_t cpu,
Lalit Maganti1d915a62019-01-07 12:10:42 +0000818 UniqueTid utid) {
819 timestamps_.emplace_back(timestamp);
820 name_ids_.emplace_back(name_id);
Lalit Magantie87cc812019-01-10 15:20:06 +0000821 cpus_.emplace_back(cpu);
Lalit Maganti1d915a62019-01-07 12:10:42 +0000822 utids_.emplace_back(utid);
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000823 arg_set_ids_.emplace_back(kInvalidArgSetId);
Lalit Maganti1d915a62019-01-07 12:10:42 +0000824 return CreateRowId(TableId::kRawEvents,
825 static_cast<uint32_t>(raw_event_count() - 1));
826 }
827
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000828 void set_arg_set_id(uint32_t row, ArgSetId id) { arg_set_ids_[row] = id; }
829
Lalit Maganti1d915a62019-01-07 12:10:42 +0000830 size_t raw_event_count() const { return timestamps_.size(); }
831
832 const std::deque<int64_t>& timestamps() const { return timestamps_; }
833
834 const std::deque<StringId>& name_ids() const { return name_ids_; }
835
Lalit Magantie87cc812019-01-10 15:20:06 +0000836 const std::deque<uint32_t>& cpus() const { return cpus_; }
837
Lalit Maganti1d915a62019-01-07 12:10:42 +0000838 const std::deque<UniqueTid>& utids() const { return utids_; }
839
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000840 const std::deque<ArgSetId>& arg_set_ids() const { return arg_set_ids_; }
841
Lalit Maganti1d915a62019-01-07 12:10:42 +0000842 private:
843 std::deque<int64_t> timestamps_;
844 std::deque<StringId> name_ids_;
Lalit Magantie87cc812019-01-10 15:20:06 +0000845 std::deque<uint32_t> cpus_;
Lalit Maganti1d915a62019-01-07 12:10:42 +0000846 std::deque<UniqueTid> utids_;
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000847 std::deque<ArgSetId> arg_set_ids_;
Lalit Maganti1d915a62019-01-07 12:10:42 +0000848 };
849
Primiano Tucci2c761ef2019-01-07 20:20:46 +0000850 class AndroidLogs {
851 public:
852 inline size_t AddLogEvent(int64_t timestamp,
853 UniqueTid utid,
854 uint8_t prio,
855 StringId tag_id,
856 StringId msg_id) {
857 timestamps_.emplace_back(timestamp);
858 utids_.emplace_back(utid);
859 prios_.emplace_back(prio);
860 tag_ids_.emplace_back(tag_id);
861 msg_ids_.emplace_back(msg_id);
862 return size() - 1;
863 }
864
865 size_t size() const { return timestamps_.size(); }
866
867 const std::deque<int64_t>& timestamps() const { return timestamps_; }
868 const std::deque<UniqueTid>& utids() const { return utids_; }
869 const std::deque<uint8_t>& prios() const { return prios_; }
870 const std::deque<StringId>& tag_ids() const { return tag_ids_; }
871 const std::deque<StringId>& msg_ids() const { return msg_ids_; }
872
873 private:
874 std::deque<int64_t> timestamps_;
875 std::deque<UniqueTid> utids_;
876 std::deque<uint8_t> prios_;
877 std::deque<StringId> tag_ids_;
878 std::deque<StringId> msg_ids_;
879 };
880
Primiano Tucci0e38a142019-01-07 20:51:09 +0000881 struct Stats {
882 using IndexMap = std::map<int, int64_t>;
883 int64_t value = 0;
884 IndexMap indexed_values;
885 };
886 using StatsMap = std::array<Stats, stats::kNumKeys>;
887
Ryan Savitski51413ad2019-07-09 14:25:21 +0100888 class Metadata {
889 public:
890 const std::deque<metadata::KeyIDs>& keys() const { return keys_; }
891 const std::deque<Variadic>& values() const { return values_; }
892
893 RowId SetScalarMetadata(metadata::KeyIDs key, Variadic value) {
894 PERFETTO_DCHECK(key < metadata::kNumKeys);
895 PERFETTO_DCHECK(metadata::kKeyTypes[key] == metadata::kSingle);
896 PERFETTO_DCHECK(value.type == metadata::kValueTypes[key]);
897
898 // Already set - on release builds, overwrite the previous value.
899 auto it = scalar_indices.find(key);
900 if (it != scalar_indices.end()) {
901 PERFETTO_DFATAL("Setting a scalar metadata entry more than once.");
902 uint32_t index = static_cast<uint32_t>(it->second);
903 values_[index] = value;
904 return TraceStorage::CreateRowId(kMetadataTable, index);
905 }
906 // First time setting this key.
907 keys_.push_back(key);
908 values_.push_back(value);
909 uint32_t index = static_cast<uint32_t>(keys_.size() - 1);
910 scalar_indices[key] = index;
911 return TraceStorage::CreateRowId(kMetadataTable, index);
912 }
913
914 RowId AppendMetadata(metadata::KeyIDs key, Variadic value) {
915 PERFETTO_DCHECK(key < metadata::kNumKeys);
916 PERFETTO_DCHECK(metadata::kKeyTypes[key] == metadata::kMulti);
917 PERFETTO_DCHECK(value.type == metadata::kValueTypes[key]);
918
919 keys_.push_back(key);
920 values_.push_back(value);
921 uint32_t index = static_cast<uint32_t>(keys_.size() - 1);
922 return TraceStorage::CreateRowId(kMetadataTable, index);
923 }
924
Ryan Savitski0476ee92019-07-09 14:29:33 +0100925 void OverwriteMetadata(uint32_t index, Variadic value) {
926 PERFETTO_DCHECK(index < values_.size());
927 values_[index] = value;
928 }
929
Ryan Savitski51413ad2019-07-09 14:25:21 +0100930 private:
931 std::deque<metadata::KeyIDs> keys_;
932 std::deque<Variadic> values_;
933 // Extraneous state to track locations of entries that should have at most
934 // one row. Used only to maintain uniqueness during insertions.
935 std::map<metadata::KeyIDs, uint32_t> scalar_indices;
936 };
Mikhail Khokhlove466c002019-05-23 13:33:33 +0100937
Oystein Eftevaag5419c582019-08-21 13:58:49 -0700938 class StackProfileFrames {
Florian Mayer438b5ab2019-05-02 11:18:06 +0100939 public:
Florian Mayerbee52132019-05-02 13:59:56 +0100940 struct Row {
941 StringId name_id;
942 int64_t mapping_row;
943 int64_t rel_pc;
Florian Mayer438b5ab2019-05-02 11:18:06 +0100944
Florian Mayerbee52132019-05-02 13:59:56 +0100945 bool operator==(const Row& other) const {
946 return std::tie(name_id, mapping_row, rel_pc) ==
947 std::tie(other.name_id, other.mapping_row, other.rel_pc);
948 }
949 };
950
Lalit Maganti9b2d52b2019-05-07 14:32:15 +0100951 uint32_t size() const { return static_cast<uint32_t>(names_.size()); }
952
Florian Mayerbee52132019-05-02 13:59:56 +0100953 int64_t Insert(const Row& row) {
954 names_.emplace_back(row.name_id);
955 mappings_.emplace_back(row.mapping_row);
956 rel_pcs_.emplace_back(row.rel_pc);
Florian Mayer438b5ab2019-05-02 11:18:06 +0100957 return static_cast<int64_t>(names_.size()) - 1;
958 }
959
Ioannis Ilkose6727552019-08-14 15:10:59 +0100960 void SetFrameName(size_t row_idx, StringId name_id) {
961 PERFETTO_CHECK(row_idx < names_.size());
962 names_[row_idx] = name_id;
963 }
964
Florian Mayer438b5ab2019-05-02 11:18:06 +0100965 const std::deque<StringId>& names() const { return names_; }
966 const std::deque<int64_t>& mappings() const { return mappings_; }
967 const std::deque<int64_t>& rel_pcs() const { return rel_pcs_; }
968
969 private:
970 std::deque<StringId> names_;
971 std::deque<int64_t> mappings_;
972 std::deque<int64_t> rel_pcs_;
973 };
974
Oystein Eftevaag5419c582019-08-21 13:58:49 -0700975 class StackProfileCallsites {
Florian Mayer438b5ab2019-05-02 11:18:06 +0100976 public:
Florian Mayerbee52132019-05-02 13:59:56 +0100977 struct Row {
978 int64_t depth;
979 int64_t parent_id;
980 int64_t frame_row;
981
982 bool operator==(const Row& other) const {
983 return std::tie(depth, parent_id, frame_row) ==
984 std::tie(other.depth, other.parent_id, other.frame_row);
Florian Mayer438b5ab2019-05-02 11:18:06 +0100985 }
Florian Mayerbee52132019-05-02 13:59:56 +0100986 };
987
Lalit Maganti9b2d52b2019-05-07 14:32:15 +0100988 uint32_t size() const { return static_cast<uint32_t>(frame_ids_.size()); }
989
Florian Mayerbee52132019-05-02 13:59:56 +0100990 int64_t Insert(const Row& row) {
991 frame_depths_.emplace_back(row.depth);
992 parent_callsite_ids_.emplace_back(row.parent_id);
993 frame_ids_.emplace_back(row.frame_row);
Florian Mayer438b5ab2019-05-02 11:18:06 +0100994 return static_cast<int64_t>(frame_depths_.size()) - 1;
995 }
996
997 const std::deque<int64_t>& frame_depths() const { return frame_depths_; }
998 const std::deque<int64_t>& parent_callsite_ids() const {
999 return parent_callsite_ids_;
1000 }
1001 const std::deque<int64_t>& frame_ids() const { return frame_ids_; }
1002
1003 private:
1004 std::deque<int64_t> frame_depths_;
1005 std::deque<int64_t> parent_callsite_ids_;
1006 std::deque<int64_t> frame_ids_;
1007 };
1008
Oystein Eftevaag5419c582019-08-21 13:58:49 -07001009 class StackProfileMappings {
Florian Mayer438b5ab2019-05-02 11:18:06 +01001010 public:
Florian Mayerbee52132019-05-02 13:59:56 +01001011 struct Row {
1012 StringId build_id;
Florian Mayer12655732019-07-02 15:08:26 +01001013 int64_t exact_offset;
1014 int64_t start_offset;
Florian Mayerbee52132019-05-02 13:59:56 +01001015 int64_t start;
1016 int64_t end;
1017 int64_t load_bias;
1018 StringId name_id;
Florian Mayer438b5ab2019-05-02 11:18:06 +01001019
Florian Mayerbee52132019-05-02 13:59:56 +01001020 bool operator==(const Row& other) const {
Florian Mayer12655732019-07-02 15:08:26 +01001021 return std::tie(build_id, exact_offset, start_offset, start, end,
1022 load_bias, name_id) ==
1023 std::tie(other.build_id, other.exact_offset, other.start_offset,
1024 other.start, other.end, other.load_bias, other.name_id);
Florian Mayerbee52132019-05-02 13:59:56 +01001025 }
1026 };
1027
Lalit Maganti9b2d52b2019-05-07 14:32:15 +01001028 uint32_t size() const { return static_cast<uint32_t>(names_.size()); }
1029
Florian Mayerbee52132019-05-02 13:59:56 +01001030 int64_t Insert(const Row& row) {
1031 build_ids_.emplace_back(row.build_id);
Florian Mayer12655732019-07-02 15:08:26 +01001032 exact_offsets_.emplace_back(row.exact_offset);
1033 start_offsets_.emplace_back(row.start_offset);
Florian Mayerbee52132019-05-02 13:59:56 +01001034 starts_.emplace_back(row.start);
1035 ends_.emplace_back(row.end);
1036 load_biases_.emplace_back(row.load_bias);
1037 names_.emplace_back(row.name_id);
Florian Mayer438b5ab2019-05-02 11:18:06 +01001038 return static_cast<int64_t>(build_ids_.size()) - 1;
1039 }
1040
1041 const std::deque<StringId>& build_ids() const { return build_ids_; }
Florian Mayer12655732019-07-02 15:08:26 +01001042 const std::deque<int64_t>& exact_offsets() const { return exact_offsets_; }
1043 const std::deque<int64_t>& start_offsets() const { return start_offsets_; }
Florian Mayer438b5ab2019-05-02 11:18:06 +01001044 const std::deque<int64_t>& starts() const { return starts_; }
1045 const std::deque<int64_t>& ends() const { return ends_; }
1046 const std::deque<int64_t>& load_biases() const { return load_biases_; }
1047 const std::deque<StringId>& names() const { return names_; }
1048
1049 private:
1050 std::deque<StringId> build_ids_;
Florian Mayer12655732019-07-02 15:08:26 +01001051 std::deque<int64_t> exact_offsets_;
1052 std::deque<int64_t> start_offsets_;
Florian Mayer438b5ab2019-05-02 11:18:06 +01001053 std::deque<int64_t> starts_;
1054 std::deque<int64_t> ends_;
1055 std::deque<int64_t> load_biases_;
1056 std::deque<StringId> names_;
1057 };
1058
1059 class HeapProfileAllocations {
1060 public:
Florian Mayerbee52132019-05-02 13:59:56 +01001061 struct Row {
1062 int64_t timestamp;
Florian Mayerdf1968c2019-05-13 16:39:35 +01001063 UniquePid upid;
Florian Mayerbee52132019-05-02 13:59:56 +01001064 int64_t callsite_id;
1065 int64_t count;
1066 int64_t size;
1067 };
1068
Lalit Maganti9b2d52b2019-05-07 14:32:15 +01001069 uint32_t size() const { return static_cast<uint32_t>(timestamps_.size()); }
1070
Florian Mayerbee52132019-05-02 13:59:56 +01001071 void Insert(const Row& row) {
1072 timestamps_.emplace_back(row.timestamp);
Florian Mayerdf1968c2019-05-13 16:39:35 +01001073 upids_.emplace_back(row.upid);
Florian Mayerbee52132019-05-02 13:59:56 +01001074 callsite_ids_.emplace_back(row.callsite_id);
1075 counts_.emplace_back(row.count);
1076 sizes_.emplace_back(row.size);
Florian Mayer438b5ab2019-05-02 11:18:06 +01001077 }
1078
1079 const std::deque<int64_t>& timestamps() const { return timestamps_; }
Florian Mayerdf1968c2019-05-13 16:39:35 +01001080 const std::deque<UniquePid>& upids() const { return upids_; }
Florian Mayer438b5ab2019-05-02 11:18:06 +01001081 const std::deque<int64_t>& callsite_ids() const { return callsite_ids_; }
1082 const std::deque<int64_t>& counts() const { return counts_; }
1083 const std::deque<int64_t>& sizes() const { return sizes_; }
1084
1085 private:
1086 std::deque<int64_t> timestamps_;
Florian Mayerdf1968c2019-05-13 16:39:35 +01001087 std::deque<UniquePid> upids_;
Florian Mayer438b5ab2019-05-02 11:18:06 +01001088 std::deque<int64_t> callsite_ids_;
1089 std::deque<int64_t> counts_;
1090 std::deque<int64_t> sizes_;
1091 };
1092
Isabelle Taylora0a22972018-08-03 12:06:12 +01001093 void ResetStorage();
Lalit Maganti35622b72018-06-06 12:03:11 +01001094
Primiano Tuccib75dcee2018-08-08 12:21:36 +01001095 UniqueTid AddEmptyThread(uint32_t tid) {
1096 unique_threads_.emplace_back(tid);
Isabelle Taylora0a22972018-08-03 12:06:12 +01001097 return static_cast<UniqueTid>(unique_threads_.size() - 1);
1098 }
Isabelle Taylor47328cf2018-06-12 14:33:59 +01001099
Primiano Tuccib75dcee2018-08-08 12:21:36 +01001100 UniquePid AddEmptyProcess(uint32_t pid) {
1101 unique_processes_.emplace_back(pid);
Isabelle Taylora0a22972018-08-03 12:06:12 +01001102 return static_cast<UniquePid>(unique_processes_.size() - 1);
1103 }
Isabelle Taylor3dd366c2018-06-22 16:21:41 +01001104
Isabelle Taylora0a22972018-08-03 12:06:12 +01001105 // Return an unqiue identifier for the contents of each string.
1106 // The string is copied internally and can be destroyed after this called.
Isabelle Taylor15314ea2018-09-19 11:35:19 +01001107 // Virtual for testing.
Lalit Maganti5c454312019-04-08 12:11:17 +01001108 virtual StringId InternString(base::StringView str) {
1109 return string_pool_.InternString(str);
1110 }
Isabelle Taylor47328cf2018-06-12 14:33:59 +01001111
Isabelle Taylora0a22972018-08-03 12:06:12 +01001112 Process* GetMutableProcess(UniquePid upid) {
Lalit Maganti676658b2018-11-20 18:24:31 +00001113 PERFETTO_DCHECK(upid < unique_processes_.size());
Isabelle Taylora0a22972018-08-03 12:06:12 +01001114 return &unique_processes_[upid];
1115 }
Isabelle Taylor3dd366c2018-06-22 16:21:41 +01001116
Isabelle Taylora0a22972018-08-03 12:06:12 +01001117 Thread* GetMutableThread(UniqueTid utid) {
Florian Mayera1aaec22018-09-19 17:02:26 +01001118 PERFETTO_DCHECK(utid < unique_threads_.size());
Isabelle Taylora0a22972018-08-03 12:06:12 +01001119 return &unique_threads_[utid];
1120 }
Isabelle Taylor3dd366c2018-06-22 16:21:41 +01001121
Primiano Tucci0e38a142019-01-07 20:51:09 +00001122 // Example usage: SetStats(stats::android_log_num_failed, 42);
1123 void SetStats(size_t key, int64_t value) {
1124 PERFETTO_DCHECK(key < stats::kNumKeys);
1125 PERFETTO_DCHECK(stats::kTypes[key] == stats::kSingle);
1126 stats_[key].value = value;
1127 }
1128
1129 // Example usage: IncrementStats(stats::android_log_num_failed, -1);
1130 void IncrementStats(size_t key, int64_t increment = 1) {
1131 PERFETTO_DCHECK(key < stats::kNumKeys);
1132 PERFETTO_DCHECK(stats::kTypes[key] == stats::kSingle);
1133 stats_[key].value += increment;
1134 }
1135
Florian Mayer438b5ab2019-05-02 11:18:06 +01001136 // Example usage: IncrementIndexedStats(stats::cpu_failure, 1);
1137 void IncrementIndexedStats(size_t key, int index, int64_t increment = 1) {
1138 PERFETTO_DCHECK(key < stats::kNumKeys);
1139 PERFETTO_DCHECK(stats::kTypes[key] == stats::kIndexed);
1140 stats_[key].indexed_values[index] += increment;
1141 }
1142
Primiano Tucci0e38a142019-01-07 20:51:09 +00001143 // Example usage: SetIndexedStats(stats::cpu_failure, 1, 42);
1144 void SetIndexedStats(size_t key, int index, int64_t value) {
1145 PERFETTO_DCHECK(key < stats::kNumKeys);
1146 PERFETTO_DCHECK(stats::kTypes[key] == stats::kIndexed);
1147 stats_[key].indexed_values[index] = value;
1148 }
1149
Mikhail Khokhlove466c002019-05-23 13:33:33 +01001150 // Example usage:
1151 // SetMetadata(metadata::benchmark_name,
1152 // Variadic::String(storage->InternString("foo"));
Ryan Savitski51413ad2019-07-09 14:25:21 +01001153 // Returns the RowId of the new entry.
Mikhail Khokhlovb1fe42a2019-05-23 13:58:05 +01001154 // Virtual for testing.
Ryan Savitski51413ad2019-07-09 14:25:21 +01001155 virtual RowId SetMetadata(metadata::KeyIDs key, Variadic value) {
1156 return metadata_.SetScalarMetadata(key, value);
Mikhail Khokhlove466c002019-05-23 13:33:33 +01001157 }
1158
1159 // Example usage:
1160 // AppendMetadata(metadata::benchmark_story_tags,
1161 // Variadic::String(storage->InternString("bar"));
Ryan Savitski51413ad2019-07-09 14:25:21 +01001162 // Returns the RowId of the new entry.
Mikhail Khokhlovb1fe42a2019-05-23 13:58:05 +01001163 // Virtual for testing.
Ryan Savitski51413ad2019-07-09 14:25:21 +01001164 virtual RowId AppendMetadata(metadata::KeyIDs key, Variadic value) {
1165 return metadata_.AppendMetadata(key, value);
Mikhail Khokhlove466c002019-05-23 13:33:33 +01001166 }
1167
Eric Seckler77b52782019-05-02 15:18:57 +01001168 class ScopedStatsTracer {
1169 public:
1170 ScopedStatsTracer(TraceStorage* storage, size_t key)
1171 : storage_(storage), key_(key), start_ns_(base::GetWallTimeNs()) {}
1172
1173 ~ScopedStatsTracer() {
1174 if (!storage_)
1175 return;
1176 auto delta_ns = base::GetWallTimeNs() - start_ns_;
1177 storage_->IncrementStats(key_, delta_ns.count());
1178 }
1179
1180 ScopedStatsTracer(ScopedStatsTracer&& other) noexcept { MoveImpl(&other); }
1181
1182 ScopedStatsTracer& operator=(ScopedStatsTracer&& other) {
1183 MoveImpl(&other);
1184 return *this;
1185 }
1186
1187 private:
1188 ScopedStatsTracer(const ScopedStatsTracer&) = delete;
1189 ScopedStatsTracer& operator=(const ScopedStatsTracer&) = delete;
1190
1191 void MoveImpl(ScopedStatsTracer* other) {
1192 storage_ = other->storage_;
1193 key_ = other->key_;
1194 start_ns_ = other->start_ns_;
1195 other->storage_ = nullptr;
1196 }
1197
1198 TraceStorage* storage_;
1199 size_t key_;
1200 base::TimeNanos start_ns_;
1201 };
1202
1203 ScopedStatsTracer TraceExecutionTimeIntoStats(size_t key) {
1204 return ScopedStatsTracer(this, key);
1205 }
1206
Lalit Maganticaed37e2018-06-01 03:03:08 +01001207 // Reading methods.
Eric Secklerc93823e2019-06-03 16:49:19 +01001208 // Virtual for testing.
1209 virtual NullTermStringView GetString(StringId id) const {
Lalit Maganti5c454312019-04-08 12:11:17 +01001210 return string_pool_.Get(id);
Isabelle Taylora0a22972018-08-03 12:06:12 +01001211 }
1212
Lalit Magantie9d40532018-06-29 13:15:06 +01001213 const Process& GetProcess(UniquePid upid) const {
Lalit Maganti676658b2018-11-20 18:24:31 +00001214 PERFETTO_DCHECK(upid < unique_processes_.size());
Isabelle Taylor47328cf2018-06-12 14:33:59 +01001215 return unique_processes_[upid];
1216 }
1217
Lalit Magantie9d40532018-06-29 13:15:06 +01001218 const Thread& GetThread(UniqueTid utid) const {
Lalit Maganti1f64cfa2018-09-18 13:20:02 +01001219 // Allow utid == 0 for idle thread retrieval.
Florian Mayera1aaec22018-09-19 17:02:26 +01001220 PERFETTO_DCHECK(utid < unique_threads_.size());
Isabelle Taylor68e42192018-06-19 16:19:31 +01001221 return unique_threads_[utid];
1222 }
1223
Lalit Maganti5ea9e932018-11-30 14:19:39 +00001224 static RowId CreateRowId(TableId table, uint32_t row) {
Lalit Maganti85ca4a82018-12-07 17:28:02 +00001225 return (static_cast<RowId>(table) << kRowIdTableShift) | row;
Lalit Maganti5ea9e932018-11-30 14:19:39 +00001226 }
1227
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +00001228 static std::pair<int8_t /*table*/, uint32_t /*row*/> ParseRowId(RowId rowid) {
1229 auto id = static_cast<uint64_t>(rowid);
1230 auto table_id = static_cast<uint8_t>(id >> kRowIdTableShift);
1231 auto row = static_cast<uint32_t>(id & ((1ull << kRowIdTableShift) - 1));
1232 return std::make_pair(table_id, row);
1233 }
1234
Lalit Magantid11d3e02019-07-26 12:32:09 +05301235 const Tracks& tracks() const { return tracks_; }
1236 Tracks* mutable_tracks() { return &tracks_; }
1237
Eric Seckler5703ede2019-07-10 10:13:02 +01001238 const VirtualTracks& virtual_tracks() const { return virtual_tracks_; }
1239 VirtualTracks* mutable_virtual_tracks() { return &virtual_tracks_; }
1240
Lalit Magantiff69c112018-09-24 12:07:47 +01001241 const Slices& slices() const { return slices_; }
Lalit Magantifde29042018-10-04 13:28:52 +01001242 Slices* mutable_slices() { return &slices_; }
1243
Primiano Tucci0d72a312018-08-07 14:42:45 +01001244 const NestableSlices& nestable_slices() const { return nestable_slices_; }
1245 NestableSlices* mutable_nestable_slices() { return &nestable_slices_; }
1246
Eric Secklerd3b89d52019-07-10 15:36:29 +01001247 const ThreadSlices& thread_slices() const { return thread_slices_; }
1248 ThreadSlices* mutable_thread_slices() { return &thread_slices_; }
1249
Eric Secklerc3430c62019-07-22 10:49:58 +01001250 const VirtualTrackSlices& virtual_track_slices() const {
1251 return virtual_track_slices_;
1252 }
1253 VirtualTrackSlices* mutable_virtual_track_slices() {
1254 return &virtual_track_slices_;
1255 }
1256
Mikael Pessa7160ccc2019-07-25 11:19:26 -07001257 const GpuSlices& gpu_track_slices() const { return gpu_track_slices_; }
1258 GpuSlices* mutable_gpu_track_slices() { return &gpu_track_slices_; }
1259
Lalit Maganti8320e6d2019-03-14 18:49:33 +00001260 const CounterDefinitions& counter_definitions() const {
1261 return counter_definitions_;
1262 }
1263 CounterDefinitions* mutable_counter_definitions() {
1264 return &counter_definitions_;
1265 }
1266
1267 const CounterValues& counter_values() const { return counter_values_; }
1268 CounterValues* mutable_counter_values() { return &counter_values_; }
Isabelle Taylor14674d42018-09-07 11:33:11 +01001269
Primiano Tucci5cb84f82018-10-31 21:46:36 -07001270 const SqlStats& sql_stats() const { return sql_stats_; }
1271 SqlStats* mutable_sql_stats() { return &sql_stats_; }
1272
Isabelle Taylorc8c11202018-11-05 11:36:22 +00001273 const Instants& instants() const { return instants_; }
1274 Instants* mutable_instants() { return &instants_; }
1275
Primiano Tucci2c761ef2019-01-07 20:20:46 +00001276 const AndroidLogs& android_logs() const { return android_log_; }
1277 AndroidLogs* mutable_android_log() { return &android_log_; }
1278
Primiano Tucci0e38a142019-01-07 20:51:09 +00001279 const StatsMap& stats() const { return stats_; }
Lalit Maganti05e8c132018-11-09 18:16:12 +00001280
Ryan Savitski51413ad2019-07-09 14:25:21 +01001281 const Metadata& metadata() const { return metadata_; }
Ryan Savitski0476ee92019-07-09 14:29:33 +01001282 Metadata* mutable_metadata() { return &metadata_; }
Mikhail Khokhlove466c002019-05-23 13:33:33 +01001283
Lalit Maganti6e9c55e2018-11-29 12:00:39 +00001284 const Args& args() const { return args_; }
1285 Args* mutable_args() { return &args_; }
1286
Lalit Maganti1d915a62019-01-07 12:10:42 +00001287 const RawEvents& raw_events() const { return raw_events_; }
1288 RawEvents* mutable_raw_events() { return &raw_events_; }
1289
Oystein Eftevaag5419c582019-08-21 13:58:49 -07001290 const StackProfileMappings& stack_profile_mappings() const {
1291 return stack_profile_mappings_;
Florian Mayer438b5ab2019-05-02 11:18:06 +01001292 }
Oystein Eftevaag5419c582019-08-21 13:58:49 -07001293 StackProfileMappings* mutable_stack_profile_mappings() {
1294 return &stack_profile_mappings_;
Florian Mayer438b5ab2019-05-02 11:18:06 +01001295 }
1296
Oystein Eftevaag5419c582019-08-21 13:58:49 -07001297 const StackProfileFrames& stack_profile_frames() const {
1298 return stack_profile_frames_;
Florian Mayer438b5ab2019-05-02 11:18:06 +01001299 }
Oystein Eftevaag5419c582019-08-21 13:58:49 -07001300 StackProfileFrames* mutable_stack_profile_frames() {
1301 return &stack_profile_frames_;
Florian Mayer438b5ab2019-05-02 11:18:06 +01001302 }
1303
Oystein Eftevaag5419c582019-08-21 13:58:49 -07001304 const StackProfileCallsites& stack_profile_callsites() const {
1305 return stack_profile_callsites_;
Florian Mayer438b5ab2019-05-02 11:18:06 +01001306 }
Oystein Eftevaag5419c582019-08-21 13:58:49 -07001307 StackProfileCallsites* mutable_stack_profile_callsites() {
1308 return &stack_profile_callsites_;
Florian Mayer438b5ab2019-05-02 11:18:06 +01001309 }
1310
1311 const HeapProfileAllocations& heap_profile_allocations() const {
1312 return heap_profile_allocations_;
1313 }
1314 HeapProfileAllocations* mutable_heap_profile_allocations() {
1315 return &heap_profile_allocations_;
1316 }
1317
Mikael Pessa7160ccc2019-07-25 11:19:26 -07001318 const GpuTracks& gpu_tracks() const { return gpu_tracks_; }
1319 GpuTracks* mutable_gpu_tracks() { return &gpu_tracks_; }
1320
Lalit Maganti5c454312019-04-08 12:11:17 +01001321 const StringPool& string_pool() const { return string_pool_; }
Lalit Magantiacda68b2018-10-29 15:23:25 +00001322
Isabelle Taylore7003fb2018-07-17 11:39:01 +01001323 // |unique_processes_| always contains at least 1 element becuase the 0th ID
1324 // is reserved to indicate an invalid process.
Lalit Maganti9c6395b2019-01-17 00:25:09 +00001325 size_t process_count() const { return unique_processes_.size(); }
Primiano Tucci0d72a312018-08-07 14:42:45 +01001326
Isabelle Taylore7003fb2018-07-17 11:39:01 +01001327 // |unique_threads_| always contains at least 1 element becuase the 0th ID
1328 // is reserved to indicate an invalid thread.
Lalit Maganti9c6395b2019-01-17 00:25:09 +00001329 size_t thread_count() const { return unique_threads_.size(); }
Isabelle Taylore7003fb2018-07-17 11:39:01 +01001330
Hector Dearman12323362018-08-09 16:09:28 +01001331 // Number of interned strings in the pool. Includes the empty string w/ ID=0.
1332 size_t string_count() const { return string_pool_.size(); }
1333
Ioannis Ilkosb8b11102019-01-29 17:56:55 +00001334 // Start / end ts (in nanoseconds) across the parsed trace events.
1335 // Returns (0, 0) if the trace is empty.
1336 std::pair<int64_t, int64_t> GetTraceTimestampBoundsNs() const;
1337
Lalit Maganticaed37e2018-06-01 03:03:08 +01001338 private:
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +00001339 static constexpr uint8_t kRowIdTableShift = 32;
Isabelle Taylora0a22972018-08-03 12:06:12 +01001340
Primiano Tucci2da5d2e2018-08-10 14:23:31 +01001341 using StringHash = uint64_t;
Lalit Maganticaed37e2018-06-01 03:03:08 +01001342
Lalit Maganti5c454312019-04-08 12:11:17 +01001343 TraceStorage(const TraceStorage&) = delete;
1344 TraceStorage& operator=(const TraceStorage&) = delete;
1345
1346 TraceStorage(TraceStorage&&) = default;
1347 TraceStorage& operator=(TraceStorage&&) = default;
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +00001348
Lalit Maganti05e8c132018-11-09 18:16:12 +00001349 // Stats about parsing the trace.
Primiano Tucci0e38a142019-01-07 20:51:09 +00001350 StatsMap stats_{};
Lalit Maganti35622b72018-06-06 12:03:11 +01001351
Ryan Savitski51413ad2019-07-09 14:25:21 +01001352 // Extra data extracted from the trace. Includes:
1353 // * metadata from chrome and benchmarking infrastructure
1354 // * descriptions of android packages
1355 Metadata metadata_{};
Mikhail Khokhlove466c002019-05-23 13:33:33 +01001356
Lalit Magantid11d3e02019-07-26 12:32:09 +05301357 // Metadata for tracks.
1358 Tracks tracks_;
1359
Eric Seckler5703ede2019-07-10 10:13:02 +01001360 // Metadata for virtual slice tracks.
1361 VirtualTracks virtual_tracks_;
1362
Mikael Pessa803090c2019-08-23 16:03:34 -07001363 // Metadata for gpu tracks.
1364 GpuTracks gpu_tracks_;
1365 GpuContexts gpu_contexts_;
1366
Lalit Maganticaed37e2018-06-01 03:03:08 +01001367 // One entry for each CPU in the trace.
Lalit Magantiff69c112018-09-24 12:07:47 +01001368 Slices slices_;
Lalit Maganticaed37e2018-06-01 03:03:08 +01001369
Lalit Maganti6e9c55e2018-11-29 12:00:39 +00001370 // Args for all other tables.
1371 Args args_;
1372
Lalit Maganticaed37e2018-06-01 03:03:08 +01001373 // One entry for each unique string in the trace.
Lalit Maganti5c454312019-04-08 12:11:17 +01001374 StringPool string_pool_;
Isabelle Taylor47328cf2018-06-12 14:33:59 +01001375
Isabelle Taylor47328cf2018-06-12 14:33:59 +01001376 // One entry for each UniquePid, with UniquePid as the index.
Ioannis Ilkos9c03cbd2019-03-12 18:30:43 +00001377 // Never hold on to pointers to Process, as vector resize will
1378 // invalidate them.
1379 std::vector<Process> unique_processes_;
Isabelle Taylor68e42192018-06-19 16:19:31 +01001380
Isabelle Taylor68e42192018-06-19 16:19:31 +01001381 // One entry for each UniqueTid, with UniqueTid as the index.
Isabelle Taylor3dd366c2018-06-22 16:21:41 +01001382 std::deque<Thread> unique_threads_;
Primiano Tucci0d72a312018-08-07 14:42:45 +01001383
1384 // Slices coming from userspace events (e.g. Chromium TRACE_EVENT macros).
1385 NestableSlices nestable_slices_;
Isabelle Taylor15314ea2018-09-19 11:35:19 +01001386
Eric Secklerd3b89d52019-07-10 15:36:29 +01001387 // Additional attributes for threads slices (sub-type of NestableSlices).
1388 ThreadSlices thread_slices_;
1389
Eric Secklerc3430c62019-07-22 10:49:58 +01001390 // Additional attributes for virtual track slices (sub-type of
1391 // NestableSlices).
1392 VirtualTrackSlices virtual_track_slices_;
1393
Mikael Pessa803090c2019-08-23 16:03:34 -07001394 // Additional attributes for gpu track slices (sub-type of
1395 // NestableSlices).
1396 GpuSlices gpu_track_slices_;
1397
Hector Dearman2442d612019-06-04 18:05:23 +01001398 // The type of counters in the trace. Can be thought of as the "metadata".
Lalit Maganti8320e6d2019-03-14 18:49:33 +00001399 CounterDefinitions counter_definitions_;
1400
1401 // The values from the Counter events from the trace. This includes CPU
1402 // frequency events as well systrace trace_marker counter events.
1403 CounterValues counter_values_;
Primiano Tucci5cb84f82018-10-31 21:46:36 -07001404
1405 SqlStats sql_stats_;
Lalit Maganti6e9c55e2018-11-29 12:00:39 +00001406
Isabelle Taylorc8c11202018-11-05 11:36:22 +00001407 // These are instantaneous events in the trace. They have no duration
1408 // and do not have a value that make sense to track over time.
1409 // e.g. signal events
1410 Instants instants_;
Lalit Maganti1d915a62019-01-07 12:10:42 +00001411
1412 // Raw events are every ftrace event in the trace. The raw event includes
1413 // the timestamp and the pid. The args for the raw event will be in the
1414 // args table. This table can be used to generate a text version of the
1415 // trace.
1416 RawEvents raw_events_;
Primiano Tucci2c761ef2019-01-07 20:20:46 +00001417 AndroidLogs android_log_;
Florian Mayer438b5ab2019-05-02 11:18:06 +01001418
Oystein Eftevaag5419c582019-08-21 13:58:49 -07001419 StackProfileMappings stack_profile_mappings_;
1420 StackProfileFrames stack_profile_frames_;
1421 StackProfileCallsites stack_profile_callsites_;
Florian Mayer438b5ab2019-05-02 11:18:06 +01001422 HeapProfileAllocations heap_profile_allocations_;
Lalit Maganticaed37e2018-06-01 03:03:08 +01001423};
1424
1425} // namespace trace_processor
1426} // namespace perfetto
1427
Florian Mayerbee52132019-05-02 13:59:56 +01001428namespace std {
1429
1430template <>
Oystein Eftevaag5419c582019-08-21 13:58:49 -07001431struct hash<
1432 ::perfetto::trace_processor::TraceStorage::StackProfileFrames::Row> {
Florian Mayerbee52132019-05-02 13:59:56 +01001433 using argument_type =
Oystein Eftevaag5419c582019-08-21 13:58:49 -07001434 ::perfetto::trace_processor::TraceStorage::StackProfileFrames::Row;
Florian Mayerbee52132019-05-02 13:59:56 +01001435 using result_type = size_t;
1436
1437 result_type operator()(const argument_type& r) const {
1438 return std::hash<::perfetto::trace_processor::StringId>{}(r.name_id) ^
1439 std::hash<int64_t>{}(r.mapping_row) ^ std::hash<int64_t>{}(r.rel_pc);
1440 }
1441};
1442
1443template <>
1444struct hash<
Oystein Eftevaag5419c582019-08-21 13:58:49 -07001445 ::perfetto::trace_processor::TraceStorage::StackProfileCallsites::Row> {
Florian Mayerbee52132019-05-02 13:59:56 +01001446 using argument_type =
Oystein Eftevaag5419c582019-08-21 13:58:49 -07001447 ::perfetto::trace_processor::TraceStorage::StackProfileCallsites::Row;
Florian Mayerbee52132019-05-02 13:59:56 +01001448 using result_type = size_t;
1449
1450 result_type operator()(const argument_type& r) const {
1451 return std::hash<int64_t>{}(r.depth) ^ std::hash<int64_t>{}(r.parent_id) ^
1452 std::hash<int64_t>{}(r.frame_row);
1453 }
1454};
1455
1456template <>
1457struct hash<
Oystein Eftevaag5419c582019-08-21 13:58:49 -07001458 ::perfetto::trace_processor::TraceStorage::StackProfileMappings::Row> {
Florian Mayerbee52132019-05-02 13:59:56 +01001459 using argument_type =
Oystein Eftevaag5419c582019-08-21 13:58:49 -07001460 ::perfetto::trace_processor::TraceStorage::StackProfileMappings::Row;
Florian Mayerbee52132019-05-02 13:59:56 +01001461 using result_type = size_t;
1462
1463 result_type operator()(const argument_type& r) const {
1464 return std::hash<::perfetto::trace_processor::StringId>{}(r.build_id) ^
Florian Mayer12655732019-07-02 15:08:26 +01001465 std::hash<int64_t>{}(r.exact_offset) ^
1466 std::hash<int64_t>{}(r.start_offset) ^
1467 std::hash<int64_t>{}(r.start) ^ std::hash<int64_t>{}(r.end) ^
1468 std::hash<int64_t>{}(r.load_bias) ^
Florian Mayerbee52132019-05-02 13:59:56 +01001469 std::hash<::perfetto::trace_processor::StringId>{}(r.name_id);
1470 }
1471};
1472
1473} // namespace std
1474
Lalit Maganticaed37e2018-06-01 03:03:08 +01001475#endif // SRC_TRACE_PROCESSOR_TRACE_STORAGE_H_