blob: 6d085225c4bd1b608d107b533cb73a7941af44f0 [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"
Ioannis Ilkose0b47f52019-09-18 11:14:57 +010038#include "src/trace_processor/tables/profiler_tables.h"
Lalit Maganti20539c22019-09-04 12:36:10 +010039#include "src/trace_processor/tables/slice_tables.h"
40#include "src/trace_processor/tables/track_tables.h"
Mikhail Khokhlov85a0dd02019-05-17 14:22:28 +010041#include "src/trace_processor/variadic.h"
Lalit Maganti35622b72018-06-06 12:03:11 +010042
Lalit Maganticaed37e2018-06-01 03:03:08 +010043namespace perfetto {
44namespace trace_processor {
45
Isabelle Taylora0a22972018-08-03 12:06:12 +010046// UniquePid is an offset into |unique_processes_|. This is necessary because
47// Unix pids are reused and thus not guaranteed to be unique over a long
48// period of time.
49using UniquePid = uint32_t;
Primiano Tucci0d72a312018-08-07 14:42:45 +010050
Isabelle Taylora0a22972018-08-03 12:06:12 +010051// UniqueTid is an offset into |unique_threads_|. Necessary because tids can
52// be reused.
53using UniqueTid = uint32_t;
54
Primiano Tucci0d72a312018-08-07 14:42:45 +010055// StringId is an offset into |string_pool_|.
Lalit Maganti5c454312019-04-08 12:11:17 +010056using StringId = StringPool::Id;
Lalit Maganti1a2936f2019-08-29 17:42:33 +010057static const StringId kNullStringId = StringId(0);
Primiano Tucci0d72a312018-08-07 14:42:45 +010058
Lalit Maganti5ea9e932018-11-30 14:19:39 +000059// Identifiers for all the tables in the database.
60enum TableId : uint8_t {
61 // Intentionally don't have TableId == 0 so that RowId == 0 can refer to an
62 // invalid row id.
Lalit Maganti8320e6d2019-03-14 18:49:33 +000063 kCounterValues = 1,
Lalit Maganti1d915a62019-01-07 12:10:42 +000064 kRawEvents = 2,
Lalit Maganti66ed7ad2019-01-11 16:47:26 +000065 kInstants = 3,
Isabelle Taylorb9222c32019-01-31 10:58:37 +000066 kSched = 4,
Eric Seckler70cc4422019-05-28 16:00:23 +010067 kNestableSlices = 5,
Ryan Savitski51413ad2019-07-09 14:25:21 +010068 kMetadataTable = 6,
Lalit Maganti5ea9e932018-11-30 14:19:39 +000069};
70
71// The top 8 bits are set to the TableId and the bottom 32 to the row of the
72// table.
Lalit Maganti85ca4a82018-12-07 17:28:02 +000073using RowId = int64_t;
Lalit Maganti5ea9e932018-11-30 14:19:39 +000074static const RowId kInvalidRowId = 0;
75
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +000076using ArgSetId = uint32_t;
77static const ArgSetId kInvalidArgSetId = 0;
78
Eric Seckler5703ede2019-07-10 10:13:02 +010079using TrackId = uint32_t;
80
81enum class VirtualTrackScope : uint8_t {
82 // VirtualTrack with global scope, will not have a |upid| set.
83 kGlobal = 0,
84 // VirtualTrack associated with a specific process via |upid|.
85 kProcess = 1
86};
87
Isabelle Taylora97c5f52018-10-23 17:36:12 +010088enum RefType {
Primiano Tucci5403e4f2018-11-27 10:07:03 +000089 kRefNoRef = 0,
90 kRefUtid = 1,
91 kRefCpuId = 2,
92 kRefIrq = 3,
93 kRefSoftIrq = 4,
94 kRefUpid = 5,
Sidath Senanayake1f5f93a2019-06-06 22:24:15 +010095 kRefGpuId = 6,
Eric Seckler5703ede2019-07-10 10:13:02 +010096 kRefTrack = 7,
Primiano Tucci5403e4f2018-11-27 10:07:03 +000097 kRefMax
Isabelle Taylora97c5f52018-10-23 17:36:12 +010098};
Isabelle Taylor14674d42018-09-07 11:33:11 +010099
Eric Seckler972225e2019-04-18 11:07:12 +0100100const std::vector<const char*>& GetRefTypeStringMap();
101
Lalit Maganticaed37e2018-06-01 03:03:08 +0100102// Stores a data inside a trace file in a columnar form. This makes it efficient
103// to read or search across a single field of the trace (e.g. all the thread
104// names for a given CPU).
105class TraceStorage {
106 public:
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100107 TraceStorage();
Isabelle Taylora0a22972018-08-03 12:06:12 +0100108
109 virtual ~TraceStorage();
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100110
Isabelle Taylora0a22972018-08-03 12:06:12 +0100111 // Information about a unique process seen in a trace.
112 struct Process {
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100113 explicit Process(uint32_t p) : pid(p) {}
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000114 int64_t start_ns = 0;
Lalit Maganti637589a2019-07-04 17:25:29 +0100115 int64_t end_ns = 0;
Isabelle Taylora0a22972018-08-03 12:06:12 +0100116 StringId name_id = 0;
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100117 uint32_t pid = 0;
Lalit Maganti369b0572019-07-11 15:35:09 +0100118 base::Optional<UniquePid> parent_upid;
Isabelle Taylora0a22972018-08-03 12:06:12 +0100119 };
120
121 // Information about a unique thread seen in a trace.
122 struct Thread {
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100123 explicit Thread(uint32_t t) : tid(t) {}
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000124 int64_t start_ns = 0;
Lalit Magantib5bd2332019-06-06 14:20:47 +0100125 int64_t end_ns = 0;
Isabelle Taylora0a22972018-08-03 12:06:12 +0100126 StringId name_id = 0;
Lalit Maganti770886a2018-11-16 17:40:21 +0000127 base::Optional<UniquePid> upid;
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100128 uint32_t tid = 0;
Isabelle Taylora0a22972018-08-03 12:06:12 +0100129 };
Isabelle Taylor3dd366c2018-06-22 16:21:41 +0100130
Lalit Maganti6e9c55e2018-11-29 12:00:39 +0000131 // Generic key value storage which can be referenced by other tables.
132 class Args {
133 public:
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000134 struct Arg {
135 StringId flat_key = 0;
136 StringId key = 0;
137 Variadic value = Variadic::Integer(0);
138
139 // This is only used by the arg tracker and so is not part of the hash.
140 RowId row_id = 0;
141 };
142
143 struct ArgHasher {
144 uint64_t operator()(const Arg& arg) const noexcept {
Lalit Maganti1f464742019-02-28 13:49:31 +0000145 base::Hash hash;
146 hash.Update(arg.key);
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000147 // We don't hash arg.flat_key because it's a subsequence of arg.key.
148 switch (arg.value.type) {
149 case Variadic::Type::kInt:
Lalit Maganti1f464742019-02-28 13:49:31 +0000150 hash.Update(arg.value.int_value);
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000151 break;
Eric Secklerc93823e2019-06-03 16:49:19 +0100152 case Variadic::Type::kUint:
153 hash.Update(arg.value.uint_value);
154 break;
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000155 case Variadic::Type::kString:
Lalit Maganti1f464742019-02-28 13:49:31 +0000156 hash.Update(arg.value.string_value);
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000157 break;
158 case Variadic::Type::kReal:
Lalit Maganti1f464742019-02-28 13:49:31 +0000159 hash.Update(arg.value.real_value);
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000160 break;
Eric Secklerc93823e2019-06-03 16:49:19 +0100161 case Variadic::Type::kPointer:
162 hash.Update(arg.value.pointer_value);
163 break;
164 case Variadic::Type::kBool:
165 hash.Update(arg.value.bool_value);
166 break;
167 case Variadic::Type::kJson:
168 hash.Update(arg.value.json_value);
169 break;
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000170 }
Lalit Maganti1f464742019-02-28 13:49:31 +0000171 return hash.digest();
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000172 }
173 };
174
175 const std::deque<ArgSetId>& set_ids() const { return set_ids_; }
Lalit Maganti5ea9e932018-11-30 14:19:39 +0000176 const std::deque<StringId>& flat_keys() const { return flat_keys_; }
177 const std::deque<StringId>& keys() const { return keys_; }
Lalit Maganti1d915a62019-01-07 12:10:42 +0000178 const std::deque<Variadic>& arg_values() const { return arg_values_; }
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000179 uint32_t args_count() const {
180 return static_cast<uint32_t>(set_ids_.size());
Lalit Maganti79472be2018-12-04 13:41:27 +0000181 }
Lalit Maganti6e9c55e2018-11-29 12:00:39 +0000182
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000183 ArgSetId AddArgSet(const std::vector<Arg>& args,
184 uint32_t begin,
185 uint32_t end) {
Lalit Maganti1f464742019-02-28 13:49:31 +0000186 base::Hash hash;
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000187 for (uint32_t i = begin; i < end; i++) {
Lalit Maganti1f464742019-02-28 13:49:31 +0000188 hash.Update(ArgHasher()(args[i]));
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000189 }
Lalit Maganti5ea9e932018-11-30 14:19:39 +0000190
Lalit Maganti1f464742019-02-28 13:49:31 +0000191 ArgSetHash digest = hash.digest();
192 auto it = arg_row_for_hash_.find(digest);
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000193 if (it != arg_row_for_hash_.end()) {
194 return set_ids_[it->second];
195 }
196
197 // The +1 ensures that nothing has an id == kInvalidArgSetId == 0.
198 ArgSetId id = static_cast<uint32_t>(arg_row_for_hash_.size()) + 1;
Lalit Maganti1f464742019-02-28 13:49:31 +0000199 arg_row_for_hash_.emplace(digest, args_count());
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000200 for (uint32_t i = begin; i < end; i++) {
201 const auto& arg = args[i];
202 set_ids_.emplace_back(id);
203 flat_keys_.emplace_back(arg.flat_key);
204 keys_.emplace_back(arg.key);
205 arg_values_.emplace_back(arg.value);
206 }
207 return id;
Lalit Maganti6e9c55e2018-11-29 12:00:39 +0000208 }
209
Lalit Maganti5ea9e932018-11-30 14:19:39 +0000210 private:
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000211 using ArgSetHash = uint64_t;
212
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000213 std::deque<ArgSetId> set_ids_;
Lalit Maganti5ea9e932018-11-30 14:19:39 +0000214 std::deque<StringId> flat_keys_;
215 std::deque<StringId> keys_;
Lalit Maganti1d915a62019-01-07 12:10:42 +0000216 std::deque<Variadic> arg_values_;
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000217
218 std::unordered_map<ArgSetHash, uint32_t> arg_row_for_hash_;
Lalit Maganti6e9c55e2018-11-29 12:00:39 +0000219 };
220
Lalit Magantid11d3e02019-07-26 12:32:09 +0530221 class Tracks {
222 public:
223 inline uint32_t AddTrack(StringId name) {
224 names_.emplace_back(name);
225 return track_count() - 1;
226 }
227
228 uint32_t track_count() const {
229 return static_cast<uint32_t>(names_.size());
230 }
231
232 const std::deque<StringId>& names() const { return names_; }
233
234 private:
235 std::deque<StringId> names_;
236 };
237
Eric Seckler5703ede2019-07-10 10:13:02 +0100238 class VirtualTracks {
239 public:
Lalit Magantid11d3e02019-07-26 12:32:09 +0530240 inline void AddVirtualTrack(TrackId track_id,
241 VirtualTrackScope scope,
242 UniquePid upid = 0u) {
Eric Seckler5703ede2019-07-10 10:13:02 +0100243 track_ids_.emplace_back(track_id);
Eric Seckler5703ede2019-07-10 10:13:02 +0100244 scopes_.emplace_back(scope);
245 upids_.emplace_back(upid);
Eric Seckler5703ede2019-07-10 10:13:02 +0100246 }
247
248 uint32_t virtual_track_count() const {
249 return static_cast<uint32_t>(track_ids_.size());
250 }
251
Lalit Magantid11d3e02019-07-26 12:32:09 +0530252 base::Optional<uint32_t> FindRowForTrackId(uint32_t track_id) const {
253 auto it =
254 std::lower_bound(track_ids().begin(), track_ids().end(), track_id);
255 if (it != track_ids().end() && *it == track_id) {
256 return static_cast<uint32_t>(std::distance(track_ids().begin(), it));
257 }
258 return base::nullopt;
259 }
260
Eric Seckler5703ede2019-07-10 10:13:02 +0100261 const std::deque<uint32_t>& track_ids() const { return track_ids_; }
Eric Seckler5703ede2019-07-10 10:13:02 +0100262 const std::deque<VirtualTrackScope>& scopes() const { return scopes_; }
263 const std::deque<UniquePid>& upids() const { return upids_; }
264
265 private:
266 std::deque<uint32_t> track_ids_;
Eric Seckler5703ede2019-07-10 10:13:02 +0100267 std::deque<VirtualTrackScope> scopes_;
268 std::deque<UniquePid> upids_;
269 };
270
Mikael Pessa803090c2019-08-23 16:03:34 -0700271 class GpuContexts {
272 public:
273 inline void AddGpuContext(uint64_t context_id,
274 UniquePid upid,
275 uint32_t priority) {
276 context_ids_.emplace_back(context_id);
277 upids_.emplace_back(upid);
278 priorities_.emplace_back(priority);
279 }
280
281 uint32_t gpu_context_count() const {
282 return static_cast<uint32_t>(context_ids_.size());
283 }
284
285 const std::deque<uint64_t>& context_ids() const { return context_ids_; }
286 const std::deque<UniquePid>& upids() const { return upids_; }
287 const std::deque<uint32_t>& priorities() const { return priorities_; }
288
289 private:
290 std::deque<uint64_t> context_ids_;
291 std::deque<UniquePid> upids_;
292 std::deque<uint32_t> priorities_;
293 };
294
Lalit Magantiff69c112018-09-24 12:07:47 +0100295 class Slices {
Lalit Maganti35622b72018-06-06 12:03:11 +0100296 public:
Lalit Magantifde29042018-10-04 13:28:52 +0100297 inline size_t AddSlice(uint32_t cpu,
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000298 int64_t start_ns,
299 int64_t duration_ns,
Lalit Magantib0b53ee2019-01-24 17:53:39 +0000300 UniqueTid utid,
301 ftrace_utils::TaskState end_state,
302 int32_t priority) {
Lalit Magantiff69c112018-09-24 12:07:47 +0100303 cpus_.emplace_back(cpu);
Lalit Maganti35622b72018-06-06 12:03:11 +0100304 start_ns_.emplace_back(start_ns);
305 durations_.emplace_back(duration_ns);
Isabelle Taylora0a22972018-08-03 12:06:12 +0100306 utids_.emplace_back(utid);
Lalit Magantib0b53ee2019-01-24 17:53:39 +0000307 end_states_.emplace_back(end_state);
308 priorities_.emplace_back(priority);
Lalit Maganti58638932019-01-31 10:48:26 +0000309
310 if (utid >= rows_for_utids_.size())
311 rows_for_utids_.resize(utid + 1);
312 rows_for_utids_[utid].emplace_back(slice_count() - 1);
Lalit Magantifde29042018-10-04 13:28:52 +0100313 return slice_count() - 1;
314 }
315
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000316 void set_duration(size_t index, int64_t duration_ns) {
Lalit Magantifde29042018-10-04 13:28:52 +0100317 durations_[index] = duration_ns;
Lalit Maganti35622b72018-06-06 12:03:11 +0100318 }
319
Lalit Magantib0b53ee2019-01-24 17:53:39 +0000320 void set_end_state(size_t index, ftrace_utils::TaskState end_state) {
321 end_states_[index] = end_state;
322 }
323
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100324 size_t slice_count() const { return start_ns_.size(); }
Lalit Maganti35622b72018-06-06 12:03:11 +0100325
Lalit Magantiff69c112018-09-24 12:07:47 +0100326 const std::deque<uint32_t>& cpus() const { return cpus_; }
327
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000328 const std::deque<int64_t>& start_ns() const { return start_ns_; }
Lalit Maganti35622b72018-06-06 12:03:11 +0100329
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000330 const std::deque<int64_t>& durations() const { return durations_; }
Lalit Maganti35622b72018-06-06 12:03:11 +0100331
Isabelle Taylor68e42192018-06-19 16:19:31 +0100332 const std::deque<UniqueTid>& utids() const { return utids_; }
333
Lalit Magantib0b53ee2019-01-24 17:53:39 +0000334 const std::deque<ftrace_utils::TaskState>& end_state() const {
335 return end_states_;
336 }
337
338 const std::deque<int32_t>& priorities() const { return priorities_; }
339
Lalit Maganti58638932019-01-31 10:48:26 +0000340 const std::deque<std::vector<uint32_t>>& rows_for_utids() const {
Lalit Magantif0f09c32019-01-18 17:29:31 +0000341 return rows_for_utids_;
342 }
343
Lalit Maganti35622b72018-06-06 12:03:11 +0100344 private:
Hector Dearman947f12a2018-09-11 16:50:36 +0100345 // Each deque below has the same number of entries (the number of slices
Lalit Maganti35622b72018-06-06 12:03:11 +0100346 // in the trace for the CPU).
Lalit Magantiff69c112018-09-24 12:07:47 +0100347 std::deque<uint32_t> cpus_;
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000348 std::deque<int64_t> start_ns_;
349 std::deque<int64_t> durations_;
Isabelle Taylor68e42192018-06-19 16:19:31 +0100350 std::deque<UniqueTid> utids_;
Lalit Magantib0b53ee2019-01-24 17:53:39 +0000351 std::deque<ftrace_utils::TaskState> end_states_;
352 std::deque<int32_t> priorities_;
Lalit Maganti58638932019-01-31 10:48:26 +0000353
354 // One row per utid.
355 std::deque<std::vector<uint32_t>> rows_for_utids_;
Primiano Tucci0d72a312018-08-07 14:42:45 +0100356 };
Isabelle Taylor68e42192018-06-19 16:19:31 +0100357
Primiano Tucci0d72a312018-08-07 14:42:45 +0100358 class NestableSlices {
359 public:
Eric Seckler70cc4422019-05-28 16:00:23 +0100360 inline uint32_t AddSlice(int64_t start_ns,
361 int64_t duration_ns,
362 int64_t ref,
363 RefType type,
Lalit Maganti81a02cd2019-07-12 17:05:11 +0100364 StringId category,
Eric Seckler70cc4422019-05-28 16:00:23 +0100365 StringId name,
366 uint8_t depth,
367 int64_t stack_id,
368 int64_t parent_stack_id) {
Primiano Tucci0d72a312018-08-07 14:42:45 +0100369 start_ns_.emplace_back(start_ns);
370 durations_.emplace_back(duration_ns);
Eric Seckler972225e2019-04-18 11:07:12 +0100371 refs_.emplace_back(ref);
372 types_.emplace_back(type);
Lalit Maganti81a02cd2019-07-12 17:05:11 +0100373 categories_.emplace_back(category);
Primiano Tucci0d72a312018-08-07 14:42:45 +0100374 names_.emplace_back(name);
375 depths_.emplace_back(depth);
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100376 stack_ids_.emplace_back(stack_id);
377 parent_stack_ids_.emplace_back(parent_stack_id);
Eric Seckler70cc4422019-05-28 16:00:23 +0100378 arg_set_ids_.emplace_back(kInvalidArgSetId);
Lalit Maganti7b37bbf2018-11-09 15:58:54 +0000379 return slice_count() - 1;
380 }
381
Eric Seckler70cc4422019-05-28 16:00:23 +0100382 void set_duration(uint32_t index, int64_t duration_ns) {
Lalit Maganti7b37bbf2018-11-09 15:58:54 +0000383 durations_[index] = duration_ns;
384 }
385
Eric Seckler70cc4422019-05-28 16:00:23 +0100386 void set_stack_id(uint32_t index, int64_t stack_id) {
Lalit Maganti7b37bbf2018-11-09 15:58:54 +0000387 stack_ids_[index] = stack_id;
Primiano Tucci0d72a312018-08-07 14:42:45 +0100388 }
389
Eric Seckler70cc4422019-05-28 16:00:23 +0100390 void set_arg_set_id(uint32_t index, ArgSetId id) {
391 arg_set_ids_[index] = id;
392 }
393
394 uint32_t slice_count() const {
395 return static_cast<uint32_t>(start_ns_.size());
396 }
397
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000398 const std::deque<int64_t>& start_ns() const { return start_ns_; }
399 const std::deque<int64_t>& durations() const { return durations_; }
Eric Seckler972225e2019-04-18 11:07:12 +0100400 const std::deque<int64_t>& refs() const { return refs_; }
401 const std::deque<RefType>& types() const { return types_; }
Lalit Maganti81a02cd2019-07-12 17:05:11 +0100402 const std::deque<StringId>& categories() const { return categories_; }
Primiano Tucci0d72a312018-08-07 14:42:45 +0100403 const std::deque<StringId>& names() const { return names_; }
404 const std::deque<uint8_t>& depths() const { return depths_; }
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000405 const std::deque<int64_t>& stack_ids() const { return stack_ids_; }
406 const std::deque<int64_t>& parent_stack_ids() const {
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100407 return parent_stack_ids_;
408 }
Eric Seckler70cc4422019-05-28 16:00:23 +0100409 const std::deque<ArgSetId>& arg_set_ids() const { return arg_set_ids_; }
Primiano Tucci0d72a312018-08-07 14:42:45 +0100410
411 private:
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000412 std::deque<int64_t> start_ns_;
413 std::deque<int64_t> durations_;
Eric Seckler972225e2019-04-18 11:07:12 +0100414 std::deque<int64_t> refs_;
415 std::deque<RefType> types_;
Lalit Maganti81a02cd2019-07-12 17:05:11 +0100416 std::deque<StringId> categories_;
Primiano Tucci0d72a312018-08-07 14:42:45 +0100417 std::deque<StringId> names_;
418 std::deque<uint8_t> depths_;
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000419 std::deque<int64_t> stack_ids_;
420 std::deque<int64_t> parent_stack_ids_;
Eric Seckler70cc4422019-05-28 16:00:23 +0100421 std::deque<ArgSetId> arg_set_ids_;
Lalit Maganti35622b72018-06-06 12:03:11 +0100422 };
423
Eric Secklerd3b89d52019-07-10 15:36:29 +0100424 class ThreadSlices {
425 public:
426 inline uint32_t AddThreadSlice(uint32_t slice_id,
427 int64_t thread_timestamp_ns,
428 int64_t thread_duration_ns,
429 int64_t thread_instruction_count,
430 int64_t thread_instruction_delta) {
431 slice_ids_.emplace_back(slice_id);
432 thread_timestamp_ns_.emplace_back(thread_timestamp_ns);
433 thread_duration_ns_.emplace_back(thread_duration_ns);
434 thread_instruction_counts_.emplace_back(thread_instruction_count);
435 thread_instruction_deltas_.emplace_back(thread_instruction_delta);
436 return slice_count() - 1;
437 }
438
Eric Secklerc3430c62019-07-22 10:49:58 +0100439 uint32_t slice_count() const {
440 return static_cast<uint32_t>(slice_ids_.size());
Eric Secklerd3b89d52019-07-10 15:36:29 +0100441 }
442
Eric Secklerc3430c62019-07-22 10:49:58 +0100443 const std::deque<uint32_t>& slice_ids() const { return slice_ids_; }
444 const std::deque<int64_t>& thread_timestamp_ns() const {
445 return thread_timestamp_ns_;
446 }
447 const std::deque<int64_t>& thread_duration_ns() const {
448 return thread_duration_ns_;
449 }
450 const std::deque<int64_t>& thread_instruction_counts() const {
451 return thread_instruction_counts_;
452 }
453 const std::deque<int64_t>& thread_instruction_deltas() const {
454 return thread_instruction_deltas_;
455 }
456
457 base::Optional<uint32_t> FindRowForSliceId(uint32_t slice_id) const {
458 auto it =
459 std::lower_bound(slice_ids().begin(), slice_ids().end(), slice_id);
460 if (it != slice_ids().end() && *it == slice_id) {
461 return static_cast<uint32_t>(std::distance(slice_ids().begin(), it));
462 }
463 return base::nullopt;
464 }
465
Eric Seckler54f30a32019-07-19 15:10:29 +0100466 void UpdateThreadDeltasForSliceId(uint32_t slice_id,
467 int64_t end_thread_timestamp_ns,
468 int64_t end_thread_instruction_count) {
Eric Secklerc3430c62019-07-22 10:49:58 +0100469 uint32_t row = *FindRowForSliceId(slice_id);
470 int64_t begin_ns = thread_timestamp_ns_[row];
471 thread_duration_ns_[row] = end_thread_timestamp_ns - begin_ns;
Eric Seckler54f30a32019-07-19 15:10:29 +0100472 int64_t begin_ticount = thread_instruction_counts_[row];
473 thread_instruction_deltas_[row] =
474 end_thread_instruction_count - begin_ticount;
Eric Secklerc3430c62019-07-22 10:49:58 +0100475 }
476
477 private:
478 std::deque<uint32_t> slice_ids_;
479 std::deque<int64_t> thread_timestamp_ns_;
480 std::deque<int64_t> thread_duration_ns_;
481 std::deque<int64_t> thread_instruction_counts_;
482 std::deque<int64_t> thread_instruction_deltas_;
483 };
484
485 class VirtualTrackSlices {
486 public:
487 inline uint32_t AddVirtualTrackSlice(uint32_t slice_id,
488 int64_t thread_timestamp_ns,
489 int64_t thread_duration_ns,
490 int64_t thread_instruction_count,
491 int64_t thread_instruction_delta) {
492 slice_ids_.emplace_back(slice_id);
493 thread_timestamp_ns_.emplace_back(thread_timestamp_ns);
494 thread_duration_ns_.emplace_back(thread_duration_ns);
495 thread_instruction_counts_.emplace_back(thread_instruction_count);
496 thread_instruction_deltas_.emplace_back(thread_instruction_delta);
497 return slice_count() - 1;
Eric Secklerd3b89d52019-07-10 15:36:29 +0100498 }
499
500 uint32_t slice_count() const {
501 return static_cast<uint32_t>(slice_ids_.size());
502 }
503
504 const std::deque<uint32_t>& slice_ids() const { return slice_ids_; }
505 const std::deque<int64_t>& thread_timestamp_ns() const {
506 return thread_timestamp_ns_;
507 }
508 const std::deque<int64_t>& thread_duration_ns() const {
509 return thread_duration_ns_;
510 }
511 const std::deque<int64_t>& thread_instruction_counts() const {
512 return thread_instruction_counts_;
513 }
514 const std::deque<int64_t>& thread_instruction_deltas() const {
515 return thread_instruction_deltas_;
516 }
517
Mikhail Khokhlov7db04912019-07-18 16:11:11 +0100518 base::Optional<uint32_t> FindRowForSliceId(uint32_t slice_id) const {
Eric Secklerd3b89d52019-07-10 15:36:29 +0100519 auto it =
520 std::lower_bound(slice_ids().begin(), slice_ids().end(), slice_id);
Mikhail Khokhlov7db04912019-07-18 16:11:11 +0100521 if (it != slice_ids().end() && *it == slice_id) {
522 return static_cast<uint32_t>(std::distance(slice_ids().begin(), it));
523 }
524 return base::nullopt;
Eric Secklerd3b89d52019-07-10 15:36:29 +0100525 }
526
Eric Seckler54f30a32019-07-19 15:10:29 +0100527 void UpdateThreadDeltasForSliceId(uint32_t slice_id,
528 int64_t end_thread_timestamp_ns,
529 int64_t end_thread_instruction_count) {
Mikhail Khokhlov7db04912019-07-18 16:11:11 +0100530 uint32_t row = *FindRowForSliceId(slice_id);
Eric Secklerd3b89d52019-07-10 15:36:29 +0100531 int64_t begin_ns = thread_timestamp_ns_[row];
532 thread_duration_ns_[row] = end_thread_timestamp_ns - begin_ns;
Eric Seckler54f30a32019-07-19 15:10:29 +0100533 int64_t begin_ticount = thread_instruction_counts_[row];
534 thread_instruction_deltas_[row] =
535 end_thread_instruction_count - begin_ticount;
Eric Secklerd3b89d52019-07-10 15:36:29 +0100536 }
537
538 private:
539 std::deque<uint32_t> slice_ids_;
540 std::deque<int64_t> thread_timestamp_ns_;
541 std::deque<int64_t> thread_duration_ns_;
542 std::deque<int64_t> thread_instruction_counts_;
543 std::deque<int64_t> thread_instruction_deltas_;
544 };
545
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000546 class CounterDefinitions {
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100547 public:
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000548 using Id = uint32_t;
Lalit Maganti521d97b2019-04-29 13:47:03 +0100549 static constexpr Id kInvalidId = std::numeric_limits<Id>::max();
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000550
551 inline Id AddCounterDefinition(StringId name_id,
552 int64_t ref,
Pascal Muetschard91d07892019-08-26 13:55:06 -0700553 RefType type,
Pascal Muetschardceb5c822019-08-26 14:39:15 -0700554 StringId desc_id = 0,
555 StringId unit_id = 0) {
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000556 base::Hash hash;
557 hash.Update(name_id);
558 hash.Update(ref);
559 hash.Update(type);
560
561 // TODO(lalitm): this is a perf bottleneck and likely we can do something
562 // quite a bit better here.
563 uint64_t digest = hash.digest();
564 auto it = hash_to_row_idx_.find(digest);
565 if (it != hash_to_row_idx_.end())
566 return it->second;
567
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100568 name_ids_.emplace_back(name_id);
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100569 refs_.emplace_back(ref);
570 types_.emplace_back(type);
Pascal Muetschard91d07892019-08-26 13:55:06 -0700571 desc_ids_.emplace_back(desc_id);
Pascal Muetschardceb5c822019-08-26 14:39:15 -0700572 unit_ids_.emplace_back(unit_id);
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000573 hash_to_row_idx_.emplace(digest, size() - 1);
574 return size() - 1;
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100575 }
Lalit Magantifde29042018-10-04 13:28:52 +0100576
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000577 uint32_t size() const { return static_cast<uint32_t>(name_ids_.size()); }
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100578
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100579 const std::deque<StringId>& name_ids() const { return name_ids_; }
580
Pascal Muetschard91d07892019-08-26 13:55:06 -0700581 const std::deque<StringId>& desc_ids() const { return desc_ids_; }
582
Pascal Muetschardceb5c822019-08-26 14:39:15 -0700583 const std::deque<StringId>& unit_ids() const { return unit_ids_; }
584
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100585 const std::deque<int64_t>& refs() const { return refs_; }
586
587 const std::deque<RefType>& types() const { return types_; }
588
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000589 private:
590 std::deque<StringId> name_ids_;
591 std::deque<int64_t> refs_;
592 std::deque<RefType> types_;
Pascal Muetschard91d07892019-08-26 13:55:06 -0700593 std::deque<StringId> desc_ids_;
Pascal Muetschardceb5c822019-08-26 14:39:15 -0700594 std::deque<StringId> unit_ids_;
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000595
596 std::unordered_map<uint64_t, uint32_t> hash_to_row_idx_;
597 };
598
599 class CounterValues {
600 public:
601 inline uint32_t AddCounterValue(CounterDefinitions::Id counter_id,
602 int64_t timestamp,
603 double value) {
604 counter_ids_.emplace_back(counter_id);
605 timestamps_.emplace_back(timestamp);
606 values_.emplace_back(value);
607 arg_set_ids_.emplace_back(kInvalidArgSetId);
Ioannis Ilkosf129c262019-04-25 14:53:51 +0100608
Lalit Maganti521d97b2019-04-29 13:47:03 +0100609 if (counter_id != CounterDefinitions::kInvalidId) {
610 if (counter_id >= rows_for_counter_id_.size()) {
611 rows_for_counter_id_.resize(counter_id + 1);
612 }
613 rows_for_counter_id_[counter_id].emplace_back(size() - 1);
614 }
615 return size() - 1;
616 }
617
618 void set_counter_id(uint32_t index, CounterDefinitions::Id counter_id) {
619 PERFETTO_DCHECK(counter_ids_[index] == CounterDefinitions::kInvalidId);
620
621 counter_ids_[index] = counter_id;
Ioannis Ilkosf129c262019-04-25 14:53:51 +0100622 if (counter_id >= rows_for_counter_id_.size()) {
623 rows_for_counter_id_.resize(counter_id + 1);
624 }
Lalit Maganti521d97b2019-04-29 13:47:03 +0100625
626 auto* new_rows = &rows_for_counter_id_[counter_id];
627 new_rows->insert(
628 std::upper_bound(new_rows->begin(), new_rows->end(), index), index);
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000629 }
630
631 void set_arg_set_id(uint32_t row, ArgSetId id) { arg_set_ids_[row] = id; }
632
633 uint32_t size() const { return static_cast<uint32_t>(counter_ids_.size()); }
634
635 const std::deque<CounterDefinitions::Id>& counter_ids() const {
636 return counter_ids_;
637 }
638
639 const std::deque<int64_t>& timestamps() const { return timestamps_; }
640
641 const std::deque<double>& values() const { return values_; }
642
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000643 const std::deque<ArgSetId>& arg_set_ids() const { return arg_set_ids_; }
644
Ioannis Ilkosf129c262019-04-25 14:53:51 +0100645 const std::deque<std::vector<uint32_t>>& rows_for_counter_id() const {
646 return rows_for_counter_id_;
647 }
648
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100649 private:
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000650 std::deque<CounterDefinitions::Id> counter_ids_;
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000651 std::deque<int64_t> timestamps_;
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100652 std::deque<double> values_;
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000653 std::deque<ArgSetId> arg_set_ids_;
Ioannis Ilkosf129c262019-04-25 14:53:51 +0100654
655 // Indexed by counter_id value and contains the row numbers corresponding to
656 // it.
657 std::deque<std::vector<uint32_t>> rows_for_counter_id_;
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100658 };
659
Primiano Tucci5cb84f82018-10-31 21:46:36 -0700660 class SqlStats {
661 public:
662 static constexpr size_t kMaxLogEntries = 100;
Lalit Magantiaac2f652019-04-30 12:16:21 +0100663 uint32_t RecordQueryBegin(const std::string& query,
664 int64_t time_queued,
665 int64_t time_started);
666 void RecordQueryFirstNext(uint32_t row, int64_t time_first_next);
667 void RecordQueryEnd(uint32_t row, int64_t time_end);
Primiano Tucci5cb84f82018-10-31 21:46:36 -0700668 size_t size() const { return queries_.size(); }
669 const std::deque<std::string>& queries() const { return queries_; }
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000670 const std::deque<int64_t>& times_queued() const { return times_queued_; }
671 const std::deque<int64_t>& times_started() const { return times_started_; }
Lalit Magantiaac2f652019-04-30 12:16:21 +0100672 const std::deque<int64_t>& times_first_next() const {
673 return times_first_next_;
674 }
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000675 const std::deque<int64_t>& times_ended() const { return times_ended_; }
Primiano Tucci5cb84f82018-10-31 21:46:36 -0700676
677 private:
Lalit Magantiaac2f652019-04-30 12:16:21 +0100678 uint32_t popped_queries_ = 0;
679
Primiano Tucci5cb84f82018-10-31 21:46:36 -0700680 std::deque<std::string> queries_;
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000681 std::deque<int64_t> times_queued_;
682 std::deque<int64_t> times_started_;
Lalit Magantiaac2f652019-04-30 12:16:21 +0100683 std::deque<int64_t> times_first_next_;
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000684 std::deque<int64_t> times_ended_;
Primiano Tucci5cb84f82018-10-31 21:46:36 -0700685 };
686
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000687 class Instants {
688 public:
Lalit Maganti66ed7ad2019-01-11 16:47:26 +0000689 inline uint32_t AddInstantEvent(int64_t timestamp,
690 StringId name_id,
691 double value,
692 int64_t ref,
693 RefType type) {
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000694 timestamps_.emplace_back(timestamp);
695 name_ids_.emplace_back(name_id);
696 values_.emplace_back(value);
697 refs_.emplace_back(ref);
698 types_.emplace_back(type);
Lalit Maganti1c21d172019-02-07 10:48:24 +0000699 arg_set_ids_.emplace_back(kInvalidArgSetId);
Lalit Maganti66ed7ad2019-01-11 16:47:26 +0000700 return static_cast<uint32_t>(instant_count() - 1);
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000701 }
702
Lalit Maganti521d97b2019-04-29 13:47:03 +0100703 void set_ref(uint32_t row, int64_t ref) { refs_[row] = ref; }
704
Lalit Maganti1c21d172019-02-07 10:48:24 +0000705 void set_arg_set_id(uint32_t row, ArgSetId id) { arg_set_ids_[row] = id; }
706
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000707 size_t instant_count() const { return timestamps_.size(); }
708
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000709 const std::deque<int64_t>& timestamps() const { return timestamps_; }
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000710
711 const std::deque<StringId>& name_ids() const { return name_ids_; }
712
713 const std::deque<double>& values() const { return values_; }
714
715 const std::deque<int64_t>& refs() const { return refs_; }
716
717 const std::deque<RefType>& types() const { return types_; }
718
Lalit Maganti1c21d172019-02-07 10:48:24 +0000719 const std::deque<ArgSetId>& arg_set_ids() const { return arg_set_ids_; }
720
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000721 private:
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000722 std::deque<int64_t> timestamps_;
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000723 std::deque<StringId> name_ids_;
724 std::deque<double> values_;
725 std::deque<int64_t> refs_;
726 std::deque<RefType> types_;
Lalit Maganti1c21d172019-02-07 10:48:24 +0000727 std::deque<ArgSetId> arg_set_ids_;
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000728 };
729
Lalit Maganti1d915a62019-01-07 12:10:42 +0000730 class RawEvents {
731 public:
732 inline RowId AddRawEvent(int64_t timestamp,
733 StringId name_id,
Lalit Magantie87cc812019-01-10 15:20:06 +0000734 uint32_t cpu,
Lalit Maganti1d915a62019-01-07 12:10:42 +0000735 UniqueTid utid) {
736 timestamps_.emplace_back(timestamp);
737 name_ids_.emplace_back(name_id);
Lalit Magantie87cc812019-01-10 15:20:06 +0000738 cpus_.emplace_back(cpu);
Lalit Maganti1d915a62019-01-07 12:10:42 +0000739 utids_.emplace_back(utid);
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000740 arg_set_ids_.emplace_back(kInvalidArgSetId);
Lalit Maganti1d915a62019-01-07 12:10:42 +0000741 return CreateRowId(TableId::kRawEvents,
742 static_cast<uint32_t>(raw_event_count() - 1));
743 }
744
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000745 void set_arg_set_id(uint32_t row, ArgSetId id) { arg_set_ids_[row] = id; }
746
Lalit Maganti1d915a62019-01-07 12:10:42 +0000747 size_t raw_event_count() const { return timestamps_.size(); }
748
749 const std::deque<int64_t>& timestamps() const { return timestamps_; }
750
751 const std::deque<StringId>& name_ids() const { return name_ids_; }
752
Lalit Magantie87cc812019-01-10 15:20:06 +0000753 const std::deque<uint32_t>& cpus() const { return cpus_; }
754
Lalit Maganti1d915a62019-01-07 12:10:42 +0000755 const std::deque<UniqueTid>& utids() const { return utids_; }
756
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000757 const std::deque<ArgSetId>& arg_set_ids() const { return arg_set_ids_; }
758
Lalit Maganti1d915a62019-01-07 12:10:42 +0000759 private:
760 std::deque<int64_t> timestamps_;
761 std::deque<StringId> name_ids_;
Lalit Magantie87cc812019-01-10 15:20:06 +0000762 std::deque<uint32_t> cpus_;
Lalit Maganti1d915a62019-01-07 12:10:42 +0000763 std::deque<UniqueTid> utids_;
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000764 std::deque<ArgSetId> arg_set_ids_;
Lalit Maganti1d915a62019-01-07 12:10:42 +0000765 };
766
Primiano Tucci2c761ef2019-01-07 20:20:46 +0000767 class AndroidLogs {
768 public:
769 inline size_t AddLogEvent(int64_t timestamp,
770 UniqueTid utid,
771 uint8_t prio,
772 StringId tag_id,
773 StringId msg_id) {
774 timestamps_.emplace_back(timestamp);
775 utids_.emplace_back(utid);
776 prios_.emplace_back(prio);
777 tag_ids_.emplace_back(tag_id);
778 msg_ids_.emplace_back(msg_id);
779 return size() - 1;
780 }
781
782 size_t size() const { return timestamps_.size(); }
783
784 const std::deque<int64_t>& timestamps() const { return timestamps_; }
785 const std::deque<UniqueTid>& utids() const { return utids_; }
786 const std::deque<uint8_t>& prios() const { return prios_; }
787 const std::deque<StringId>& tag_ids() const { return tag_ids_; }
788 const std::deque<StringId>& msg_ids() const { return msg_ids_; }
789
790 private:
791 std::deque<int64_t> timestamps_;
792 std::deque<UniqueTid> utids_;
793 std::deque<uint8_t> prios_;
794 std::deque<StringId> tag_ids_;
795 std::deque<StringId> msg_ids_;
796 };
797
Primiano Tucci0e38a142019-01-07 20:51:09 +0000798 struct Stats {
799 using IndexMap = std::map<int, int64_t>;
800 int64_t value = 0;
801 IndexMap indexed_values;
802 };
803 using StatsMap = std::array<Stats, stats::kNumKeys>;
804
Ryan Savitski51413ad2019-07-09 14:25:21 +0100805 class Metadata {
806 public:
807 const std::deque<metadata::KeyIDs>& keys() const { return keys_; }
808 const std::deque<Variadic>& values() const { return values_; }
809
810 RowId SetScalarMetadata(metadata::KeyIDs key, Variadic value) {
811 PERFETTO_DCHECK(key < metadata::kNumKeys);
812 PERFETTO_DCHECK(metadata::kKeyTypes[key] == metadata::kSingle);
813 PERFETTO_DCHECK(value.type == metadata::kValueTypes[key]);
814
815 // Already set - on release builds, overwrite the previous value.
816 auto it = scalar_indices.find(key);
817 if (it != scalar_indices.end()) {
818 PERFETTO_DFATAL("Setting a scalar metadata entry more than once.");
819 uint32_t index = static_cast<uint32_t>(it->second);
820 values_[index] = value;
821 return TraceStorage::CreateRowId(kMetadataTable, index);
822 }
823 // First time setting this key.
824 keys_.push_back(key);
825 values_.push_back(value);
826 uint32_t index = static_cast<uint32_t>(keys_.size() - 1);
827 scalar_indices[key] = index;
828 return TraceStorage::CreateRowId(kMetadataTable, index);
829 }
830
831 RowId AppendMetadata(metadata::KeyIDs key, Variadic value) {
832 PERFETTO_DCHECK(key < metadata::kNumKeys);
833 PERFETTO_DCHECK(metadata::kKeyTypes[key] == metadata::kMulti);
834 PERFETTO_DCHECK(value.type == metadata::kValueTypes[key]);
835
836 keys_.push_back(key);
837 values_.push_back(value);
838 uint32_t index = static_cast<uint32_t>(keys_.size() - 1);
839 return TraceStorage::CreateRowId(kMetadataTable, index);
840 }
841
Ryan Savitski0476ee92019-07-09 14:29:33 +0100842 void OverwriteMetadata(uint32_t index, Variadic value) {
843 PERFETTO_DCHECK(index < values_.size());
844 values_[index] = value;
845 }
846
Ryan Savitski51413ad2019-07-09 14:25:21 +0100847 private:
848 std::deque<metadata::KeyIDs> keys_;
849 std::deque<Variadic> values_;
850 // Extraneous state to track locations of entries that should have at most
851 // one row. Used only to maintain uniqueness during insertions.
852 std::map<metadata::KeyIDs, uint32_t> scalar_indices;
853 };
Mikhail Khokhlove466c002019-05-23 13:33:33 +0100854
Oystein Eftevaag5419c582019-08-21 13:58:49 -0700855 class StackProfileFrames {
Florian Mayer438b5ab2019-05-02 11:18:06 +0100856 public:
Florian Mayerbee52132019-05-02 13:59:56 +0100857 struct Row {
858 StringId name_id;
859 int64_t mapping_row;
860 int64_t rel_pc;
Florian Mayer438b5ab2019-05-02 11:18:06 +0100861
Florian Mayerbee52132019-05-02 13:59:56 +0100862 bool operator==(const Row& other) const {
863 return std::tie(name_id, mapping_row, rel_pc) ==
864 std::tie(other.name_id, other.mapping_row, other.rel_pc);
865 }
866 };
867
Lalit Maganti9b2d52b2019-05-07 14:32:15 +0100868 uint32_t size() const { return static_cast<uint32_t>(names_.size()); }
869
Florian Mayerbee52132019-05-02 13:59:56 +0100870 int64_t Insert(const Row& row) {
871 names_.emplace_back(row.name_id);
872 mappings_.emplace_back(row.mapping_row);
873 rel_pcs_.emplace_back(row.rel_pc);
Ioannis Ilkose0b47f52019-09-18 11:14:57 +0100874 symbol_set_ids_.emplace_back(0);
Florian Mayer438b5ab2019-05-02 11:18:06 +0100875 return static_cast<int64_t>(names_.size()) - 1;
876 }
877
Ioannis Ilkose0b47f52019-09-18 11:14:57 +0100878 void SetSymbolSetId(size_t row_idx, uint32_t symbol_set_id) {
879 PERFETTO_CHECK(row_idx < symbol_set_ids_.size());
880 symbol_set_ids_[row_idx] = symbol_set_id;
Ioannis Ilkose6727552019-08-14 15:10:59 +0100881 }
882
Florian Mayer438b5ab2019-05-02 11:18:06 +0100883 const std::deque<StringId>& names() const { return names_; }
884 const std::deque<int64_t>& mappings() const { return mappings_; }
885 const std::deque<int64_t>& rel_pcs() const { return rel_pcs_; }
Ioannis Ilkose0b47f52019-09-18 11:14:57 +0100886 const std::deque<uint32_t>& symbol_set_ids() const {
887 return symbol_set_ids_;
888 }
Florian Mayer438b5ab2019-05-02 11:18:06 +0100889
890 private:
891 std::deque<StringId> names_;
892 std::deque<int64_t> mappings_;
893 std::deque<int64_t> rel_pcs_;
Ioannis Ilkose0b47f52019-09-18 11:14:57 +0100894 std::deque<uint32_t> symbol_set_ids_;
Florian Mayer438b5ab2019-05-02 11:18:06 +0100895 };
896
Oystein Eftevaag5419c582019-08-21 13:58:49 -0700897 class StackProfileCallsites {
Florian Mayer438b5ab2019-05-02 11:18:06 +0100898 public:
Florian Mayerbee52132019-05-02 13:59:56 +0100899 struct Row {
900 int64_t depth;
901 int64_t parent_id;
902 int64_t frame_row;
903
904 bool operator==(const Row& other) const {
905 return std::tie(depth, parent_id, frame_row) ==
906 std::tie(other.depth, other.parent_id, other.frame_row);
Florian Mayer438b5ab2019-05-02 11:18:06 +0100907 }
Florian Mayerbee52132019-05-02 13:59:56 +0100908 };
909
Lalit Maganti9b2d52b2019-05-07 14:32:15 +0100910 uint32_t size() const { return static_cast<uint32_t>(frame_ids_.size()); }
911
Florian Mayerbee52132019-05-02 13:59:56 +0100912 int64_t Insert(const Row& row) {
913 frame_depths_.emplace_back(row.depth);
914 parent_callsite_ids_.emplace_back(row.parent_id);
915 frame_ids_.emplace_back(row.frame_row);
Florian Mayer438b5ab2019-05-02 11:18:06 +0100916 return static_cast<int64_t>(frame_depths_.size()) - 1;
917 }
918
919 const std::deque<int64_t>& frame_depths() const { return frame_depths_; }
920 const std::deque<int64_t>& parent_callsite_ids() const {
921 return parent_callsite_ids_;
922 }
923 const std::deque<int64_t>& frame_ids() const { return frame_ids_; }
924
925 private:
926 std::deque<int64_t> frame_depths_;
927 std::deque<int64_t> parent_callsite_ids_;
928 std::deque<int64_t> frame_ids_;
929 };
930
Oystein Eftevaag5419c582019-08-21 13:58:49 -0700931 class StackProfileMappings {
Florian Mayer438b5ab2019-05-02 11:18:06 +0100932 public:
Florian Mayerbee52132019-05-02 13:59:56 +0100933 struct Row {
934 StringId build_id;
Florian Mayer12655732019-07-02 15:08:26 +0100935 int64_t exact_offset;
936 int64_t start_offset;
Florian Mayerbee52132019-05-02 13:59:56 +0100937 int64_t start;
938 int64_t end;
939 int64_t load_bias;
940 StringId name_id;
Florian Mayer438b5ab2019-05-02 11:18:06 +0100941
Florian Mayerbee52132019-05-02 13:59:56 +0100942 bool operator==(const Row& other) const {
Florian Mayer12655732019-07-02 15:08:26 +0100943 return std::tie(build_id, exact_offset, start_offset, start, end,
944 load_bias, name_id) ==
945 std::tie(other.build_id, other.exact_offset, other.start_offset,
946 other.start, other.end, other.load_bias, other.name_id);
Florian Mayerbee52132019-05-02 13:59:56 +0100947 }
948 };
949
Lalit Maganti9b2d52b2019-05-07 14:32:15 +0100950 uint32_t size() const { return static_cast<uint32_t>(names_.size()); }
951
Florian Mayerbee52132019-05-02 13:59:56 +0100952 int64_t Insert(const Row& row) {
953 build_ids_.emplace_back(row.build_id);
Florian Mayer12655732019-07-02 15:08:26 +0100954 exact_offsets_.emplace_back(row.exact_offset);
955 start_offsets_.emplace_back(row.start_offset);
Florian Mayerbee52132019-05-02 13:59:56 +0100956 starts_.emplace_back(row.start);
957 ends_.emplace_back(row.end);
958 load_biases_.emplace_back(row.load_bias);
959 names_.emplace_back(row.name_id);
Florian Mayer438b5ab2019-05-02 11:18:06 +0100960 return static_cast<int64_t>(build_ids_.size()) - 1;
961 }
962
963 const std::deque<StringId>& build_ids() const { return build_ids_; }
Florian Mayer12655732019-07-02 15:08:26 +0100964 const std::deque<int64_t>& exact_offsets() const { return exact_offsets_; }
965 const std::deque<int64_t>& start_offsets() const { return start_offsets_; }
Florian Mayer438b5ab2019-05-02 11:18:06 +0100966 const std::deque<int64_t>& starts() const { return starts_; }
967 const std::deque<int64_t>& ends() const { return ends_; }
968 const std::deque<int64_t>& load_biases() const { return load_biases_; }
969 const std::deque<StringId>& names() const { return names_; }
970
971 private:
972 std::deque<StringId> build_ids_;
Florian Mayer12655732019-07-02 15:08:26 +0100973 std::deque<int64_t> exact_offsets_;
974 std::deque<int64_t> start_offsets_;
Florian Mayer438b5ab2019-05-02 11:18:06 +0100975 std::deque<int64_t> starts_;
976 std::deque<int64_t> ends_;
977 std::deque<int64_t> load_biases_;
978 std::deque<StringId> names_;
979 };
980
981 class HeapProfileAllocations {
982 public:
Florian Mayerbee52132019-05-02 13:59:56 +0100983 struct Row {
984 int64_t timestamp;
Florian Mayerdf1968c2019-05-13 16:39:35 +0100985 UniquePid upid;
Florian Mayerbee52132019-05-02 13:59:56 +0100986 int64_t callsite_id;
987 int64_t count;
988 int64_t size;
989 };
990
Lalit Maganti9b2d52b2019-05-07 14:32:15 +0100991 uint32_t size() const { return static_cast<uint32_t>(timestamps_.size()); }
992
Florian Mayerbee52132019-05-02 13:59:56 +0100993 void Insert(const Row& row) {
994 timestamps_.emplace_back(row.timestamp);
Florian Mayerdf1968c2019-05-13 16:39:35 +0100995 upids_.emplace_back(row.upid);
Florian Mayerbee52132019-05-02 13:59:56 +0100996 callsite_ids_.emplace_back(row.callsite_id);
997 counts_.emplace_back(row.count);
998 sizes_.emplace_back(row.size);
Florian Mayer438b5ab2019-05-02 11:18:06 +0100999 }
1000
1001 const std::deque<int64_t>& timestamps() const { return timestamps_; }
Florian Mayerdf1968c2019-05-13 16:39:35 +01001002 const std::deque<UniquePid>& upids() const { return upids_; }
Florian Mayer438b5ab2019-05-02 11:18:06 +01001003 const std::deque<int64_t>& callsite_ids() const { return callsite_ids_; }
1004 const std::deque<int64_t>& counts() const { return counts_; }
1005 const std::deque<int64_t>& sizes() const { return sizes_; }
1006
1007 private:
1008 std::deque<int64_t> timestamps_;
Florian Mayerdf1968c2019-05-13 16:39:35 +01001009 std::deque<UniquePid> upids_;
Florian Mayer438b5ab2019-05-02 11:18:06 +01001010 std::deque<int64_t> callsite_ids_;
1011 std::deque<int64_t> counts_;
1012 std::deque<int64_t> sizes_;
1013 };
1014
Oystein Eftevaag7f64c102019-08-29 10:27:31 -07001015 class CpuProfileStackSamples {
1016 public:
1017 struct Row {
1018 int64_t timestamp;
1019 int64_t callsite_id;
1020 UniqueTid utid;
1021 };
1022
1023 uint32_t size() const { return static_cast<uint32_t>(timestamps_.size()); }
1024
1025 void Insert(const Row& row) {
1026 timestamps_.emplace_back(row.timestamp);
1027 callsite_ids_.emplace_back(row.callsite_id);
1028 utids_.emplace_back(row.utid);
1029 }
1030
1031 const std::deque<int64_t>& timestamps() const { return timestamps_; }
1032 const std::deque<int64_t>& callsite_ids() const { return callsite_ids_; }
1033 const std::deque<UniqueTid>& utids() const { return utids_; }
1034
1035 private:
1036 std::deque<int64_t> timestamps_;
1037 std::deque<int64_t> callsite_ids_;
1038 std::deque<UniqueTid> utids_;
1039 };
1040
Primiano Tuccib75dcee2018-08-08 12:21:36 +01001041 UniqueTid AddEmptyThread(uint32_t tid) {
1042 unique_threads_.emplace_back(tid);
Isabelle Taylora0a22972018-08-03 12:06:12 +01001043 return static_cast<UniqueTid>(unique_threads_.size() - 1);
1044 }
Isabelle Taylor47328cf2018-06-12 14:33:59 +01001045
Primiano Tuccib75dcee2018-08-08 12:21:36 +01001046 UniquePid AddEmptyProcess(uint32_t pid) {
1047 unique_processes_.emplace_back(pid);
Isabelle Taylora0a22972018-08-03 12:06:12 +01001048 return static_cast<UniquePid>(unique_processes_.size() - 1);
1049 }
Isabelle Taylor3dd366c2018-06-22 16:21:41 +01001050
Isabelle Taylora0a22972018-08-03 12:06:12 +01001051 // Return an unqiue identifier for the contents of each string.
1052 // The string is copied internally and can be destroyed after this called.
Isabelle Taylor15314ea2018-09-19 11:35:19 +01001053 // Virtual for testing.
Lalit Maganti5c454312019-04-08 12:11:17 +01001054 virtual StringId InternString(base::StringView str) {
1055 return string_pool_.InternString(str);
1056 }
Isabelle Taylor47328cf2018-06-12 14:33:59 +01001057
Isabelle Taylora0a22972018-08-03 12:06:12 +01001058 Process* GetMutableProcess(UniquePid upid) {
Lalit Maganti676658b2018-11-20 18:24:31 +00001059 PERFETTO_DCHECK(upid < unique_processes_.size());
Isabelle Taylora0a22972018-08-03 12:06:12 +01001060 return &unique_processes_[upid];
1061 }
Isabelle Taylor3dd366c2018-06-22 16:21:41 +01001062
Isabelle Taylora0a22972018-08-03 12:06:12 +01001063 Thread* GetMutableThread(UniqueTid utid) {
Florian Mayera1aaec22018-09-19 17:02:26 +01001064 PERFETTO_DCHECK(utid < unique_threads_.size());
Isabelle Taylora0a22972018-08-03 12:06:12 +01001065 return &unique_threads_[utid];
1066 }
Isabelle Taylor3dd366c2018-06-22 16:21:41 +01001067
Primiano Tucci0e38a142019-01-07 20:51:09 +00001068 // Example usage: SetStats(stats::android_log_num_failed, 42);
1069 void SetStats(size_t key, int64_t value) {
1070 PERFETTO_DCHECK(key < stats::kNumKeys);
1071 PERFETTO_DCHECK(stats::kTypes[key] == stats::kSingle);
1072 stats_[key].value = value;
1073 }
1074
1075 // Example usage: IncrementStats(stats::android_log_num_failed, -1);
1076 void IncrementStats(size_t key, int64_t increment = 1) {
1077 PERFETTO_DCHECK(key < stats::kNumKeys);
1078 PERFETTO_DCHECK(stats::kTypes[key] == stats::kSingle);
1079 stats_[key].value += increment;
1080 }
1081
Florian Mayer438b5ab2019-05-02 11:18:06 +01001082 // Example usage: IncrementIndexedStats(stats::cpu_failure, 1);
1083 void IncrementIndexedStats(size_t key, int index, int64_t increment = 1) {
1084 PERFETTO_DCHECK(key < stats::kNumKeys);
1085 PERFETTO_DCHECK(stats::kTypes[key] == stats::kIndexed);
1086 stats_[key].indexed_values[index] += increment;
1087 }
1088
Primiano Tucci0e38a142019-01-07 20:51:09 +00001089 // Example usage: SetIndexedStats(stats::cpu_failure, 1, 42);
1090 void SetIndexedStats(size_t key, int index, int64_t value) {
1091 PERFETTO_DCHECK(key < stats::kNumKeys);
1092 PERFETTO_DCHECK(stats::kTypes[key] == stats::kIndexed);
1093 stats_[key].indexed_values[index] = value;
1094 }
1095
Mikhail Khokhlove466c002019-05-23 13:33:33 +01001096 // Example usage:
1097 // SetMetadata(metadata::benchmark_name,
1098 // Variadic::String(storage->InternString("foo"));
Ryan Savitski51413ad2019-07-09 14:25:21 +01001099 // Returns the RowId of the new entry.
Mikhail Khokhlovb1fe42a2019-05-23 13:58:05 +01001100 // Virtual for testing.
Ryan Savitski51413ad2019-07-09 14:25:21 +01001101 virtual RowId SetMetadata(metadata::KeyIDs key, Variadic value) {
1102 return metadata_.SetScalarMetadata(key, value);
Mikhail Khokhlove466c002019-05-23 13:33:33 +01001103 }
1104
1105 // Example usage:
1106 // AppendMetadata(metadata::benchmark_story_tags,
1107 // Variadic::String(storage->InternString("bar"));
Ryan Savitski51413ad2019-07-09 14:25:21 +01001108 // Returns the RowId of the new entry.
Mikhail Khokhlovb1fe42a2019-05-23 13:58:05 +01001109 // Virtual for testing.
Ryan Savitski51413ad2019-07-09 14:25:21 +01001110 virtual RowId AppendMetadata(metadata::KeyIDs key, Variadic value) {
1111 return metadata_.AppendMetadata(key, value);
Mikhail Khokhlove466c002019-05-23 13:33:33 +01001112 }
1113
Eric Seckler77b52782019-05-02 15:18:57 +01001114 class ScopedStatsTracer {
1115 public:
1116 ScopedStatsTracer(TraceStorage* storage, size_t key)
1117 : storage_(storage), key_(key), start_ns_(base::GetWallTimeNs()) {}
1118
1119 ~ScopedStatsTracer() {
1120 if (!storage_)
1121 return;
1122 auto delta_ns = base::GetWallTimeNs() - start_ns_;
1123 storage_->IncrementStats(key_, delta_ns.count());
1124 }
1125
1126 ScopedStatsTracer(ScopedStatsTracer&& other) noexcept { MoveImpl(&other); }
1127
1128 ScopedStatsTracer& operator=(ScopedStatsTracer&& other) {
1129 MoveImpl(&other);
1130 return *this;
1131 }
1132
1133 private:
1134 ScopedStatsTracer(const ScopedStatsTracer&) = delete;
1135 ScopedStatsTracer& operator=(const ScopedStatsTracer&) = delete;
1136
1137 void MoveImpl(ScopedStatsTracer* other) {
1138 storage_ = other->storage_;
1139 key_ = other->key_;
1140 start_ns_ = other->start_ns_;
1141 other->storage_ = nullptr;
1142 }
1143
1144 TraceStorage* storage_;
1145 size_t key_;
1146 base::TimeNanos start_ns_;
1147 };
1148
1149 ScopedStatsTracer TraceExecutionTimeIntoStats(size_t key) {
1150 return ScopedStatsTracer(this, key);
1151 }
1152
Lalit Maganticaed37e2018-06-01 03:03:08 +01001153 // Reading methods.
Eric Secklerc93823e2019-06-03 16:49:19 +01001154 // Virtual for testing.
1155 virtual NullTermStringView GetString(StringId id) const {
Lalit Maganti5c454312019-04-08 12:11:17 +01001156 return string_pool_.Get(id);
Isabelle Taylora0a22972018-08-03 12:06:12 +01001157 }
1158
Lalit Magantie9d40532018-06-29 13:15:06 +01001159 const Process& GetProcess(UniquePid upid) const {
Lalit Maganti676658b2018-11-20 18:24:31 +00001160 PERFETTO_DCHECK(upid < unique_processes_.size());
Isabelle Taylor47328cf2018-06-12 14:33:59 +01001161 return unique_processes_[upid];
1162 }
1163
Lalit Magantie9d40532018-06-29 13:15:06 +01001164 const Thread& GetThread(UniqueTid utid) const {
Lalit Maganti1f64cfa2018-09-18 13:20:02 +01001165 // Allow utid == 0 for idle thread retrieval.
Florian Mayera1aaec22018-09-19 17:02:26 +01001166 PERFETTO_DCHECK(utid < unique_threads_.size());
Isabelle Taylor68e42192018-06-19 16:19:31 +01001167 return unique_threads_[utid];
1168 }
1169
Lalit Maganti5ea9e932018-11-30 14:19:39 +00001170 static RowId CreateRowId(TableId table, uint32_t row) {
Lalit Maganti85ca4a82018-12-07 17:28:02 +00001171 return (static_cast<RowId>(table) << kRowIdTableShift) | row;
Lalit Maganti5ea9e932018-11-30 14:19:39 +00001172 }
1173
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +00001174 static std::pair<int8_t /*table*/, uint32_t /*row*/> ParseRowId(RowId rowid) {
1175 auto id = static_cast<uint64_t>(rowid);
1176 auto table_id = static_cast<uint8_t>(id >> kRowIdTableShift);
1177 auto row = static_cast<uint32_t>(id & ((1ull << kRowIdTableShift) - 1));
1178 return std::make_pair(table_id, row);
1179 }
1180
Lalit Magantiac68e9c2019-09-09 18:12:15 +01001181 const tables::TrackTable& track_table() const { return track_table_; }
1182 tables::TrackTable* mutable_track_table() { return &track_table_; }
Lalit Magantid11d3e02019-07-26 12:32:09 +05301183
Eric Seckler5703ede2019-07-10 10:13:02 +01001184 const VirtualTracks& virtual_tracks() const { return virtual_tracks_; }
1185 VirtualTracks* mutable_virtual_tracks() { return &virtual_tracks_; }
1186
Lalit Magantiff69c112018-09-24 12:07:47 +01001187 const Slices& slices() const { return slices_; }
Lalit Magantifde29042018-10-04 13:28:52 +01001188 Slices* mutable_slices() { return &slices_; }
1189
Primiano Tucci0d72a312018-08-07 14:42:45 +01001190 const NestableSlices& nestable_slices() const { return nestable_slices_; }
1191 NestableSlices* mutable_nestable_slices() { return &nestable_slices_; }
1192
Eric Secklerd3b89d52019-07-10 15:36:29 +01001193 const ThreadSlices& thread_slices() const { return thread_slices_; }
1194 ThreadSlices* mutable_thread_slices() { return &thread_slices_; }
1195
Eric Secklerc3430c62019-07-22 10:49:58 +01001196 const VirtualTrackSlices& virtual_track_slices() const {
1197 return virtual_track_slices_;
1198 }
1199 VirtualTrackSlices* mutable_virtual_track_slices() {
1200 return &virtual_track_slices_;
1201 }
1202
Lalit Maganti20539c22019-09-04 12:36:10 +01001203 const tables::GpuSliceTable& gpu_slice_table() const {
1204 return gpu_slice_table_;
1205 }
1206 tables::GpuSliceTable* mutable_gpu_slice_table() { return &gpu_slice_table_; }
Mikael Pessa7160ccc2019-07-25 11:19:26 -07001207
Lalit Maganti8320e6d2019-03-14 18:49:33 +00001208 const CounterDefinitions& counter_definitions() const {
1209 return counter_definitions_;
1210 }
1211 CounterDefinitions* mutable_counter_definitions() {
1212 return &counter_definitions_;
1213 }
1214
1215 const CounterValues& counter_values() const { return counter_values_; }
1216 CounterValues* mutable_counter_values() { return &counter_values_; }
Isabelle Taylor14674d42018-09-07 11:33:11 +01001217
Primiano Tucci5cb84f82018-10-31 21:46:36 -07001218 const SqlStats& sql_stats() const { return sql_stats_; }
1219 SqlStats* mutable_sql_stats() { return &sql_stats_; }
1220
Isabelle Taylorc8c11202018-11-05 11:36:22 +00001221 const Instants& instants() const { return instants_; }
1222 Instants* mutable_instants() { return &instants_; }
1223
Primiano Tucci2c761ef2019-01-07 20:20:46 +00001224 const AndroidLogs& android_logs() const { return android_log_; }
1225 AndroidLogs* mutable_android_log() { return &android_log_; }
1226
Primiano Tucci0e38a142019-01-07 20:51:09 +00001227 const StatsMap& stats() const { return stats_; }
Lalit Maganti05e8c132018-11-09 18:16:12 +00001228
Ryan Savitski51413ad2019-07-09 14:25:21 +01001229 const Metadata& metadata() const { return metadata_; }
Ryan Savitski0476ee92019-07-09 14:29:33 +01001230 Metadata* mutable_metadata() { return &metadata_; }
Mikhail Khokhlove466c002019-05-23 13:33:33 +01001231
Lalit Maganti6e9c55e2018-11-29 12:00:39 +00001232 const Args& args() const { return args_; }
1233 Args* mutable_args() { return &args_; }
1234
Lalit Maganti1d915a62019-01-07 12:10:42 +00001235 const RawEvents& raw_events() const { return raw_events_; }
1236 RawEvents* mutable_raw_events() { return &raw_events_; }
1237
Oystein Eftevaag5419c582019-08-21 13:58:49 -07001238 const StackProfileMappings& stack_profile_mappings() const {
1239 return stack_profile_mappings_;
Florian Mayer438b5ab2019-05-02 11:18:06 +01001240 }
Oystein Eftevaag5419c582019-08-21 13:58:49 -07001241 StackProfileMappings* mutable_stack_profile_mappings() {
1242 return &stack_profile_mappings_;
Florian Mayer438b5ab2019-05-02 11:18:06 +01001243 }
1244
Oystein Eftevaag5419c582019-08-21 13:58:49 -07001245 const StackProfileFrames& stack_profile_frames() const {
1246 return stack_profile_frames_;
Florian Mayer438b5ab2019-05-02 11:18:06 +01001247 }
Oystein Eftevaag5419c582019-08-21 13:58:49 -07001248 StackProfileFrames* mutable_stack_profile_frames() {
1249 return &stack_profile_frames_;
Florian Mayer438b5ab2019-05-02 11:18:06 +01001250 }
1251
Oystein Eftevaag5419c582019-08-21 13:58:49 -07001252 const StackProfileCallsites& stack_profile_callsites() const {
1253 return stack_profile_callsites_;
Florian Mayer438b5ab2019-05-02 11:18:06 +01001254 }
Oystein Eftevaag5419c582019-08-21 13:58:49 -07001255 StackProfileCallsites* mutable_stack_profile_callsites() {
1256 return &stack_profile_callsites_;
Florian Mayer438b5ab2019-05-02 11:18:06 +01001257 }
1258
1259 const HeapProfileAllocations& heap_profile_allocations() const {
1260 return heap_profile_allocations_;
1261 }
1262 HeapProfileAllocations* mutable_heap_profile_allocations() {
1263 return &heap_profile_allocations_;
1264 }
Oystein Eftevaag7f64c102019-08-29 10:27:31 -07001265 const CpuProfileStackSamples& cpu_profile_stack_samples() const {
1266 return cpu_profile_stack_samples_;
1267 }
1268 CpuProfileStackSamples* mutable_cpu_profile_stack_samples() {
1269 return &cpu_profile_stack_samples_;
1270 }
Florian Mayer438b5ab2019-05-02 11:18:06 +01001271
Ioannis Ilkose0b47f52019-09-18 11:14:57 +01001272 const tables::SymbolTable& symbol_table() const { return symbol_table_; }
1273
1274 tables::SymbolTable* mutable_symbol_table() { return &symbol_table_; }
1275
Lalit Maganti20539c22019-09-04 12:36:10 +01001276 const tables::GpuTrackTable& gpu_track_table() const {
1277 return gpu_track_table_;
1278 }
1279 tables::GpuTrackTable* mutable_gpu_track_table() { return &gpu_track_table_; }
Mikael Pessa7160ccc2019-07-25 11:19:26 -07001280
Lalit Maganti5c454312019-04-08 12:11:17 +01001281 const StringPool& string_pool() const { return string_pool_; }
Lalit Magantiacda68b2018-10-29 15:23:25 +00001282
Isabelle Taylore7003fb2018-07-17 11:39:01 +01001283 // |unique_processes_| always contains at least 1 element becuase the 0th ID
1284 // is reserved to indicate an invalid process.
Lalit Maganti9c6395b2019-01-17 00:25:09 +00001285 size_t process_count() const { return unique_processes_.size(); }
Primiano Tucci0d72a312018-08-07 14:42:45 +01001286
Isabelle Taylore7003fb2018-07-17 11:39:01 +01001287 // |unique_threads_| always contains at least 1 element becuase the 0th ID
1288 // is reserved to indicate an invalid thread.
Lalit Maganti9c6395b2019-01-17 00:25:09 +00001289 size_t thread_count() const { return unique_threads_.size(); }
Isabelle Taylore7003fb2018-07-17 11:39:01 +01001290
Hector Dearman12323362018-08-09 16:09:28 +01001291 // Number of interned strings in the pool. Includes the empty string w/ ID=0.
1292 size_t string_count() const { return string_pool_.size(); }
1293
Ioannis Ilkosb8b11102019-01-29 17:56:55 +00001294 // Start / end ts (in nanoseconds) across the parsed trace events.
1295 // Returns (0, 0) if the trace is empty.
1296 std::pair<int64_t, int64_t> GetTraceTimestampBoundsNs() const;
1297
Lalit Maganticaed37e2018-06-01 03:03:08 +01001298 private:
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +00001299 static constexpr uint8_t kRowIdTableShift = 32;
Isabelle Taylora0a22972018-08-03 12:06:12 +01001300
Primiano Tucci2da5d2e2018-08-10 14:23:31 +01001301 using StringHash = uint64_t;
Lalit Maganticaed37e2018-06-01 03:03:08 +01001302
Lalit Maganti5c454312019-04-08 12:11:17 +01001303 TraceStorage(const TraceStorage&) = delete;
1304 TraceStorage& operator=(const TraceStorage&) = delete;
1305
Lalit Maganti20539c22019-09-04 12:36:10 +01001306 TraceStorage(TraceStorage&&) = delete;
1307 TraceStorage& operator=(TraceStorage&&) = delete;
1308
1309 // One entry for each unique string in the trace.
1310 StringPool string_pool_;
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +00001311
Lalit Maganti05e8c132018-11-09 18:16:12 +00001312 // Stats about parsing the trace.
Primiano Tucci0e38a142019-01-07 20:51:09 +00001313 StatsMap stats_{};
Lalit Maganti35622b72018-06-06 12:03:11 +01001314
Ryan Savitski51413ad2019-07-09 14:25:21 +01001315 // Extra data extracted from the trace. Includes:
1316 // * metadata from chrome and benchmarking infrastructure
1317 // * descriptions of android packages
1318 Metadata metadata_{};
Mikhail Khokhlove466c002019-05-23 13:33:33 +01001319
Lalit Magantid11d3e02019-07-26 12:32:09 +05301320 // Metadata for tracks.
Lalit Magantiac68e9c2019-09-09 18:12:15 +01001321 tables::TrackTable track_table_{&string_pool_, nullptr};
Lalit Magantid11d3e02019-07-26 12:32:09 +05301322
Eric Seckler5703ede2019-07-10 10:13:02 +01001323 // Metadata for virtual slice tracks.
1324 VirtualTracks virtual_tracks_;
1325
Mikael Pessa803090c2019-08-23 16:03:34 -07001326 // Metadata for gpu tracks.
Lalit Maganti20539c22019-09-04 12:36:10 +01001327 tables::GpuTrackTable gpu_track_table_{&string_pool_, nullptr};
Mikael Pessa803090c2019-08-23 16:03:34 -07001328 GpuContexts gpu_contexts_;
1329
Lalit Maganticaed37e2018-06-01 03:03:08 +01001330 // One entry for each CPU in the trace.
Lalit Magantiff69c112018-09-24 12:07:47 +01001331 Slices slices_;
Lalit Maganticaed37e2018-06-01 03:03:08 +01001332
Lalit Maganti6e9c55e2018-11-29 12:00:39 +00001333 // Args for all other tables.
1334 Args args_;
1335
Isabelle Taylor47328cf2018-06-12 14:33:59 +01001336 // One entry for each UniquePid, with UniquePid as the index.
Ioannis Ilkos9c03cbd2019-03-12 18:30:43 +00001337 // Never hold on to pointers to Process, as vector resize will
1338 // invalidate them.
1339 std::vector<Process> unique_processes_;
Isabelle Taylor68e42192018-06-19 16:19:31 +01001340
Isabelle Taylor68e42192018-06-19 16:19:31 +01001341 // One entry for each UniqueTid, with UniqueTid as the index.
Isabelle Taylor3dd366c2018-06-22 16:21:41 +01001342 std::deque<Thread> unique_threads_;
Primiano Tucci0d72a312018-08-07 14:42:45 +01001343
1344 // Slices coming from userspace events (e.g. Chromium TRACE_EVENT macros).
1345 NestableSlices nestable_slices_;
Isabelle Taylor15314ea2018-09-19 11:35:19 +01001346
Eric Secklerd3b89d52019-07-10 15:36:29 +01001347 // Additional attributes for threads slices (sub-type of NestableSlices).
1348 ThreadSlices thread_slices_;
1349
Eric Secklerc3430c62019-07-22 10:49:58 +01001350 // Additional attributes for virtual track slices (sub-type of
1351 // NestableSlices).
1352 VirtualTrackSlices virtual_track_slices_;
1353
Mikael Pessa803090c2019-08-23 16:03:34 -07001354 // Additional attributes for gpu track slices (sub-type of
1355 // NestableSlices).
Lalit Maganti20539c22019-09-04 12:36:10 +01001356 tables::GpuSliceTable gpu_slice_table_{&string_pool_, nullptr};
Mikael Pessa803090c2019-08-23 16:03:34 -07001357
Hector Dearman2442d612019-06-04 18:05:23 +01001358 // The type of counters in the trace. Can be thought of as the "metadata".
Lalit Maganti8320e6d2019-03-14 18:49:33 +00001359 CounterDefinitions counter_definitions_;
1360
1361 // The values from the Counter events from the trace. This includes CPU
1362 // frequency events as well systrace trace_marker counter events.
1363 CounterValues counter_values_;
Primiano Tucci5cb84f82018-10-31 21:46:36 -07001364
1365 SqlStats sql_stats_;
Lalit Maganti6e9c55e2018-11-29 12:00:39 +00001366
Isabelle Taylorc8c11202018-11-05 11:36:22 +00001367 // These are instantaneous events in the trace. They have no duration
1368 // and do not have a value that make sense to track over time.
1369 // e.g. signal events
1370 Instants instants_;
Lalit Maganti1d915a62019-01-07 12:10:42 +00001371
1372 // Raw events are every ftrace event in the trace. The raw event includes
1373 // the timestamp and the pid. The args for the raw event will be in the
1374 // args table. This table can be used to generate a text version of the
1375 // trace.
1376 RawEvents raw_events_;
Primiano Tucci2c761ef2019-01-07 20:20:46 +00001377 AndroidLogs android_log_;
Florian Mayer438b5ab2019-05-02 11:18:06 +01001378
Oystein Eftevaag5419c582019-08-21 13:58:49 -07001379 StackProfileMappings stack_profile_mappings_;
1380 StackProfileFrames stack_profile_frames_;
1381 StackProfileCallsites stack_profile_callsites_;
Florian Mayer438b5ab2019-05-02 11:18:06 +01001382 HeapProfileAllocations heap_profile_allocations_;
Oystein Eftevaag7f64c102019-08-29 10:27:31 -07001383 CpuProfileStackSamples cpu_profile_stack_samples_;
Ioannis Ilkose0b47f52019-09-18 11:14:57 +01001384
1385 // Symbol tables (mappings from frames to symbol names)
1386 tables::SymbolTable symbol_table_{&string_pool_, nullptr};
Lalit Maganticaed37e2018-06-01 03:03:08 +01001387};
1388
1389} // namespace trace_processor
1390} // namespace perfetto
1391
Florian Mayerbee52132019-05-02 13:59:56 +01001392namespace std {
1393
1394template <>
Oystein Eftevaag5419c582019-08-21 13:58:49 -07001395struct hash<
1396 ::perfetto::trace_processor::TraceStorage::StackProfileFrames::Row> {
Florian Mayerbee52132019-05-02 13:59:56 +01001397 using argument_type =
Oystein Eftevaag5419c582019-08-21 13:58:49 -07001398 ::perfetto::trace_processor::TraceStorage::StackProfileFrames::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<::perfetto::trace_processor::StringId>{}(r.name_id) ^
1403 std::hash<int64_t>{}(r.mapping_row) ^ std::hash<int64_t>{}(r.rel_pc);
1404 }
1405};
1406
1407template <>
1408struct hash<
Oystein Eftevaag5419c582019-08-21 13:58:49 -07001409 ::perfetto::trace_processor::TraceStorage::StackProfileCallsites::Row> {
Florian Mayerbee52132019-05-02 13:59:56 +01001410 using argument_type =
Oystein Eftevaag5419c582019-08-21 13:58:49 -07001411 ::perfetto::trace_processor::TraceStorage::StackProfileCallsites::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<int64_t>{}(r.depth) ^ std::hash<int64_t>{}(r.parent_id) ^
1416 std::hash<int64_t>{}(r.frame_row);
1417 }
1418};
1419
1420template <>
1421struct hash<
Oystein Eftevaag5419c582019-08-21 13:58:49 -07001422 ::perfetto::trace_processor::TraceStorage::StackProfileMappings::Row> {
Florian Mayerbee52132019-05-02 13:59:56 +01001423 using argument_type =
Oystein Eftevaag5419c582019-08-21 13:58:49 -07001424 ::perfetto::trace_processor::TraceStorage::StackProfileMappings::Row;
Florian Mayerbee52132019-05-02 13:59:56 +01001425 using result_type = size_t;
1426
1427 result_type operator()(const argument_type& r) const {
1428 return std::hash<::perfetto::trace_processor::StringId>{}(r.build_id) ^
Florian Mayer12655732019-07-02 15:08:26 +01001429 std::hash<int64_t>{}(r.exact_offset) ^
1430 std::hash<int64_t>{}(r.start_offset) ^
1431 std::hash<int64_t>{}(r.start) ^ std::hash<int64_t>{}(r.end) ^
1432 std::hash<int64_t>{}(r.load_bias) ^
Florian Mayerbee52132019-05-02 13:59:56 +01001433 std::hash<::perfetto::trace_processor::StringId>{}(r.name_id);
1434 }
1435};
1436
1437} // namespace std
1438
Lalit Maganticaed37e2018-06-01 03:03:08 +01001439#endif // SRC_TRACE_PROCESSOR_TRACE_STORAGE_H_