blob: fd58794d402570c02cfba9c3142180021ccad9d2 [file] [log] [blame]
Lalit Maganticaed37e2018-06-01 03:03:08 +01001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef SRC_TRACE_PROCESSOR_TRACE_STORAGE_H_
18#define SRC_TRACE_PROCESSOR_TRACE_STORAGE_H_
19
Lalit Maganti35622b72018-06-06 12:03:11 +010020#include <array>
Lalit Maganticaed37e2018-06-01 03:03:08 +010021#include <deque>
Isabelle Taylor47328cf2018-06-12 14:33:59 +010022#include <map>
Lalit Maganticaed37e2018-06-01 03:03:08 +010023#include <string>
24#include <unordered_map>
Ioannis Ilkosb8b11102019-01-29 17:56:55 +000025#include <utility>
Lalit Maganticaed37e2018-06-01 03:03:08 +010026#include <vector>
27
Lalit Maganti35622b72018-06-06 12:03:11 +010028#include "perfetto/base/logging.h"
Primiano Tucci2c5488f2019-06-01 03:27:28 +010029#include "perfetto/ext/base/hash.h"
30#include "perfetto/ext/base/optional.h"
31#include "perfetto/ext/base/string_view.h"
32#include "perfetto/ext/base/time.h"
33#include "perfetto/ext/base/utils.h"
Lalit Magantib0b53ee2019-01-24 17:53:39 +000034#include "src/trace_processor/ftrace_utils.h"
Mikhail Khokhlove466c002019-05-23 13:33:33 +010035#include "src/trace_processor/metadata.h"
Primiano Tucci0e38a142019-01-07 20:51:09 +000036#include "src/trace_processor/stats.h"
Lalit Maganti5c454312019-04-08 12:11:17 +010037#include "src/trace_processor/string_pool.h"
Mikhail Khokhlov85a0dd02019-05-17 14:22:28 +010038#include "src/trace_processor/variadic.h"
Lalit Maganti35622b72018-06-06 12:03:11 +010039
Lalit Maganticaed37e2018-06-01 03:03:08 +010040namespace perfetto {
41namespace trace_processor {
42
Isabelle Taylora0a22972018-08-03 12:06:12 +010043// UniquePid is an offset into |unique_processes_|. This is necessary because
44// Unix pids are reused and thus not guaranteed to be unique over a long
45// period of time.
46using UniquePid = uint32_t;
Primiano Tucci0d72a312018-08-07 14:42:45 +010047
Isabelle Taylora0a22972018-08-03 12:06:12 +010048// UniqueTid is an offset into |unique_threads_|. Necessary because tids can
49// be reused.
50using UniqueTid = uint32_t;
51
Primiano Tucci0d72a312018-08-07 14:42:45 +010052// StringId is an offset into |string_pool_|.
Lalit Maganti5c454312019-04-08 12:11:17 +010053using StringId = StringPool::Id;
Primiano Tucci0d72a312018-08-07 14:42:45 +010054
Lalit Maganti5ea9e932018-11-30 14:19:39 +000055// Identifiers for all the tables in the database.
56enum TableId : uint8_t {
57 // Intentionally don't have TableId == 0 so that RowId == 0 can refer to an
58 // invalid row id.
Lalit Maganti8320e6d2019-03-14 18:49:33 +000059 kCounterValues = 1,
Lalit Maganti1d915a62019-01-07 12:10:42 +000060 kRawEvents = 2,
Lalit Maganti66ed7ad2019-01-11 16:47:26 +000061 kInstants = 3,
Isabelle Taylorb9222c32019-01-31 10:58:37 +000062 kSched = 4,
Eric Seckler70cc4422019-05-28 16:00:23 +010063 kNestableSlices = 5,
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
Lalit Magantiff69c112018-09-24 12:07:47 +0100267 class Slices {
Lalit Maganti35622b72018-06-06 12:03:11 +0100268 public:
Lalit Magantifde29042018-10-04 13:28:52 +0100269 inline size_t AddSlice(uint32_t cpu,
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000270 int64_t start_ns,
271 int64_t duration_ns,
Lalit Magantib0b53ee2019-01-24 17:53:39 +0000272 UniqueTid utid,
273 ftrace_utils::TaskState end_state,
274 int32_t priority) {
Lalit Magantiff69c112018-09-24 12:07:47 +0100275 cpus_.emplace_back(cpu);
Lalit Maganti35622b72018-06-06 12:03:11 +0100276 start_ns_.emplace_back(start_ns);
277 durations_.emplace_back(duration_ns);
Isabelle Taylora0a22972018-08-03 12:06:12 +0100278 utids_.emplace_back(utid);
Lalit Magantib0b53ee2019-01-24 17:53:39 +0000279 end_states_.emplace_back(end_state);
280 priorities_.emplace_back(priority);
Lalit Maganti58638932019-01-31 10:48:26 +0000281
282 if (utid >= rows_for_utids_.size())
283 rows_for_utids_.resize(utid + 1);
284 rows_for_utids_[utid].emplace_back(slice_count() - 1);
Lalit Magantifde29042018-10-04 13:28:52 +0100285 return slice_count() - 1;
286 }
287
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000288 void set_duration(size_t index, int64_t duration_ns) {
Lalit Magantifde29042018-10-04 13:28:52 +0100289 durations_[index] = duration_ns;
Lalit Maganti35622b72018-06-06 12:03:11 +0100290 }
291
Lalit Magantib0b53ee2019-01-24 17:53:39 +0000292 void set_end_state(size_t index, ftrace_utils::TaskState end_state) {
293 end_states_[index] = end_state;
294 }
295
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100296 size_t slice_count() const { return start_ns_.size(); }
Lalit Maganti35622b72018-06-06 12:03:11 +0100297
Lalit Magantiff69c112018-09-24 12:07:47 +0100298 const std::deque<uint32_t>& cpus() const { return cpus_; }
299
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000300 const std::deque<int64_t>& start_ns() const { return start_ns_; }
Lalit Maganti35622b72018-06-06 12:03:11 +0100301
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000302 const std::deque<int64_t>& durations() const { return durations_; }
Lalit Maganti35622b72018-06-06 12:03:11 +0100303
Isabelle Taylor68e42192018-06-19 16:19:31 +0100304 const std::deque<UniqueTid>& utids() const { return utids_; }
305
Lalit Magantib0b53ee2019-01-24 17:53:39 +0000306 const std::deque<ftrace_utils::TaskState>& end_state() const {
307 return end_states_;
308 }
309
310 const std::deque<int32_t>& priorities() const { return priorities_; }
311
Lalit Maganti58638932019-01-31 10:48:26 +0000312 const std::deque<std::vector<uint32_t>>& rows_for_utids() const {
Lalit Magantif0f09c32019-01-18 17:29:31 +0000313 return rows_for_utids_;
314 }
315
Lalit Maganti35622b72018-06-06 12:03:11 +0100316 private:
Hector Dearman947f12a2018-09-11 16:50:36 +0100317 // Each deque below has the same number of entries (the number of slices
Lalit Maganti35622b72018-06-06 12:03:11 +0100318 // in the trace for the CPU).
Lalit Magantiff69c112018-09-24 12:07:47 +0100319 std::deque<uint32_t> cpus_;
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000320 std::deque<int64_t> start_ns_;
321 std::deque<int64_t> durations_;
Isabelle Taylor68e42192018-06-19 16:19:31 +0100322 std::deque<UniqueTid> utids_;
Lalit Magantib0b53ee2019-01-24 17:53:39 +0000323 std::deque<ftrace_utils::TaskState> end_states_;
324 std::deque<int32_t> priorities_;
Lalit Maganti58638932019-01-31 10:48:26 +0000325
326 // One row per utid.
327 std::deque<std::vector<uint32_t>> rows_for_utids_;
Primiano Tucci0d72a312018-08-07 14:42:45 +0100328 };
Isabelle Taylor68e42192018-06-19 16:19:31 +0100329
Primiano Tucci0d72a312018-08-07 14:42:45 +0100330 class NestableSlices {
331 public:
Eric Seckler70cc4422019-05-28 16:00:23 +0100332 inline uint32_t AddSlice(int64_t start_ns,
333 int64_t duration_ns,
334 int64_t ref,
335 RefType type,
Lalit Maganti81a02cd2019-07-12 17:05:11 +0100336 StringId category,
Eric Seckler70cc4422019-05-28 16:00:23 +0100337 StringId name,
338 uint8_t depth,
339 int64_t stack_id,
340 int64_t parent_stack_id) {
Primiano Tucci0d72a312018-08-07 14:42:45 +0100341 start_ns_.emplace_back(start_ns);
342 durations_.emplace_back(duration_ns);
Eric Seckler972225e2019-04-18 11:07:12 +0100343 refs_.emplace_back(ref);
344 types_.emplace_back(type);
Lalit Maganti81a02cd2019-07-12 17:05:11 +0100345 categories_.emplace_back(category);
Primiano Tucci0d72a312018-08-07 14:42:45 +0100346 names_.emplace_back(name);
347 depths_.emplace_back(depth);
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100348 stack_ids_.emplace_back(stack_id);
349 parent_stack_ids_.emplace_back(parent_stack_id);
Eric Seckler70cc4422019-05-28 16:00:23 +0100350 arg_set_ids_.emplace_back(kInvalidArgSetId);
Lalit Maganti7b37bbf2018-11-09 15:58:54 +0000351 return slice_count() - 1;
352 }
353
Eric Seckler70cc4422019-05-28 16:00:23 +0100354 void set_duration(uint32_t index, int64_t duration_ns) {
Lalit Maganti7b37bbf2018-11-09 15:58:54 +0000355 durations_[index] = duration_ns;
356 }
357
Eric Seckler70cc4422019-05-28 16:00:23 +0100358 void set_stack_id(uint32_t index, int64_t stack_id) {
Lalit Maganti7b37bbf2018-11-09 15:58:54 +0000359 stack_ids_[index] = stack_id;
Primiano Tucci0d72a312018-08-07 14:42:45 +0100360 }
361
Eric Seckler70cc4422019-05-28 16:00:23 +0100362 void set_arg_set_id(uint32_t index, ArgSetId id) {
363 arg_set_ids_[index] = id;
364 }
365
366 uint32_t slice_count() const {
367 return static_cast<uint32_t>(start_ns_.size());
368 }
369
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000370 const std::deque<int64_t>& start_ns() const { return start_ns_; }
371 const std::deque<int64_t>& durations() const { return durations_; }
Eric Seckler972225e2019-04-18 11:07:12 +0100372 const std::deque<int64_t>& refs() const { return refs_; }
373 const std::deque<RefType>& types() const { return types_; }
Lalit Maganti81a02cd2019-07-12 17:05:11 +0100374 const std::deque<StringId>& categories() const { return categories_; }
Primiano Tucci0d72a312018-08-07 14:42:45 +0100375 const std::deque<StringId>& names() const { return names_; }
376 const std::deque<uint8_t>& depths() const { return depths_; }
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000377 const std::deque<int64_t>& stack_ids() const { return stack_ids_; }
378 const std::deque<int64_t>& parent_stack_ids() const {
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100379 return parent_stack_ids_;
380 }
Eric Seckler70cc4422019-05-28 16:00:23 +0100381 const std::deque<ArgSetId>& arg_set_ids() const { return arg_set_ids_; }
Primiano Tucci0d72a312018-08-07 14:42:45 +0100382
383 private:
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000384 std::deque<int64_t> start_ns_;
385 std::deque<int64_t> durations_;
Eric Seckler972225e2019-04-18 11:07:12 +0100386 std::deque<int64_t> refs_;
387 std::deque<RefType> types_;
Lalit Maganti81a02cd2019-07-12 17:05:11 +0100388 std::deque<StringId> categories_;
Primiano Tucci0d72a312018-08-07 14:42:45 +0100389 std::deque<StringId> names_;
390 std::deque<uint8_t> depths_;
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000391 std::deque<int64_t> stack_ids_;
392 std::deque<int64_t> parent_stack_ids_;
Eric Seckler70cc4422019-05-28 16:00:23 +0100393 std::deque<ArgSetId> arg_set_ids_;
Lalit Maganti35622b72018-06-06 12:03:11 +0100394 };
395
Eric Secklerd3b89d52019-07-10 15:36:29 +0100396 class ThreadSlices {
397 public:
398 inline uint32_t AddThreadSlice(uint32_t slice_id,
399 int64_t thread_timestamp_ns,
400 int64_t thread_duration_ns,
401 int64_t thread_instruction_count,
402 int64_t thread_instruction_delta) {
403 slice_ids_.emplace_back(slice_id);
404 thread_timestamp_ns_.emplace_back(thread_timestamp_ns);
405 thread_duration_ns_.emplace_back(thread_duration_ns);
406 thread_instruction_counts_.emplace_back(thread_instruction_count);
407 thread_instruction_deltas_.emplace_back(thread_instruction_delta);
408 return slice_count() - 1;
409 }
410
Eric Secklerc3430c62019-07-22 10:49:58 +0100411 uint32_t slice_count() const {
412 return static_cast<uint32_t>(slice_ids_.size());
Eric Secklerd3b89d52019-07-10 15:36:29 +0100413 }
414
Eric Secklerc3430c62019-07-22 10:49:58 +0100415 const std::deque<uint32_t>& slice_ids() const { return slice_ids_; }
416 const std::deque<int64_t>& thread_timestamp_ns() const {
417 return thread_timestamp_ns_;
418 }
419 const std::deque<int64_t>& thread_duration_ns() const {
420 return thread_duration_ns_;
421 }
422 const std::deque<int64_t>& thread_instruction_counts() const {
423 return thread_instruction_counts_;
424 }
425 const std::deque<int64_t>& thread_instruction_deltas() const {
426 return thread_instruction_deltas_;
427 }
428
429 base::Optional<uint32_t> FindRowForSliceId(uint32_t slice_id) const {
430 auto it =
431 std::lower_bound(slice_ids().begin(), slice_ids().end(), slice_id);
432 if (it != slice_ids().end() && *it == slice_id) {
433 return static_cast<uint32_t>(std::distance(slice_ids().begin(), it));
434 }
435 return base::nullopt;
436 }
437
Eric Seckler54f30a32019-07-19 15:10:29 +0100438 void UpdateThreadDeltasForSliceId(uint32_t slice_id,
439 int64_t end_thread_timestamp_ns,
440 int64_t end_thread_instruction_count) {
Eric Secklerc3430c62019-07-22 10:49:58 +0100441 uint32_t row = *FindRowForSliceId(slice_id);
442 int64_t begin_ns = thread_timestamp_ns_[row];
443 thread_duration_ns_[row] = end_thread_timestamp_ns - begin_ns;
Eric Seckler54f30a32019-07-19 15:10:29 +0100444 int64_t begin_ticount = thread_instruction_counts_[row];
445 thread_instruction_deltas_[row] =
446 end_thread_instruction_count - begin_ticount;
Eric Secklerc3430c62019-07-22 10:49:58 +0100447 }
448
449 private:
450 std::deque<uint32_t> slice_ids_;
451 std::deque<int64_t> thread_timestamp_ns_;
452 std::deque<int64_t> thread_duration_ns_;
453 std::deque<int64_t> thread_instruction_counts_;
454 std::deque<int64_t> thread_instruction_deltas_;
455 };
456
457 class VirtualTrackSlices {
458 public:
459 inline uint32_t AddVirtualTrackSlice(uint32_t slice_id,
460 int64_t thread_timestamp_ns,
461 int64_t thread_duration_ns,
462 int64_t thread_instruction_count,
463 int64_t thread_instruction_delta) {
464 slice_ids_.emplace_back(slice_id);
465 thread_timestamp_ns_.emplace_back(thread_timestamp_ns);
466 thread_duration_ns_.emplace_back(thread_duration_ns);
467 thread_instruction_counts_.emplace_back(thread_instruction_count);
468 thread_instruction_deltas_.emplace_back(thread_instruction_delta);
469 return slice_count() - 1;
Eric Secklerd3b89d52019-07-10 15:36:29 +0100470 }
471
472 uint32_t slice_count() const {
473 return static_cast<uint32_t>(slice_ids_.size());
474 }
475
476 const std::deque<uint32_t>& slice_ids() const { return slice_ids_; }
477 const std::deque<int64_t>& thread_timestamp_ns() const {
478 return thread_timestamp_ns_;
479 }
480 const std::deque<int64_t>& thread_duration_ns() const {
481 return thread_duration_ns_;
482 }
483 const std::deque<int64_t>& thread_instruction_counts() const {
484 return thread_instruction_counts_;
485 }
486 const std::deque<int64_t>& thread_instruction_deltas() const {
487 return thread_instruction_deltas_;
488 }
489
Mikhail Khokhlov7db04912019-07-18 16:11:11 +0100490 base::Optional<uint32_t> FindRowForSliceId(uint32_t slice_id) const {
Eric Secklerd3b89d52019-07-10 15:36:29 +0100491 auto it =
492 std::lower_bound(slice_ids().begin(), slice_ids().end(), slice_id);
Mikhail Khokhlov7db04912019-07-18 16:11:11 +0100493 if (it != slice_ids().end() && *it == slice_id) {
494 return static_cast<uint32_t>(std::distance(slice_ids().begin(), it));
495 }
496 return base::nullopt;
Eric Secklerd3b89d52019-07-10 15:36:29 +0100497 }
498
Eric Seckler54f30a32019-07-19 15:10:29 +0100499 void UpdateThreadDeltasForSliceId(uint32_t slice_id,
500 int64_t end_thread_timestamp_ns,
501 int64_t end_thread_instruction_count) {
Mikhail Khokhlov7db04912019-07-18 16:11:11 +0100502 uint32_t row = *FindRowForSliceId(slice_id);
Eric Secklerd3b89d52019-07-10 15:36:29 +0100503 int64_t begin_ns = thread_timestamp_ns_[row];
504 thread_duration_ns_[row] = end_thread_timestamp_ns - begin_ns;
Eric Seckler54f30a32019-07-19 15:10:29 +0100505 int64_t begin_ticount = thread_instruction_counts_[row];
506 thread_instruction_deltas_[row] =
507 end_thread_instruction_count - begin_ticount;
Eric Secklerd3b89d52019-07-10 15:36:29 +0100508 }
509
510 private:
511 std::deque<uint32_t> slice_ids_;
512 std::deque<int64_t> thread_timestamp_ns_;
513 std::deque<int64_t> thread_duration_ns_;
514 std::deque<int64_t> thread_instruction_counts_;
515 std::deque<int64_t> thread_instruction_deltas_;
516 };
517
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000518 class CounterDefinitions {
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100519 public:
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000520 using Id = uint32_t;
Lalit Maganti521d97b2019-04-29 13:47:03 +0100521 static constexpr Id kInvalidId = std::numeric_limits<Id>::max();
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000522
523 inline Id AddCounterDefinition(StringId name_id,
524 int64_t ref,
525 RefType type) {
526 base::Hash hash;
527 hash.Update(name_id);
528 hash.Update(ref);
529 hash.Update(type);
530
531 // TODO(lalitm): this is a perf bottleneck and likely we can do something
532 // quite a bit better here.
533 uint64_t digest = hash.digest();
534 auto it = hash_to_row_idx_.find(digest);
535 if (it != hash_to_row_idx_.end())
536 return it->second;
537
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100538 name_ids_.emplace_back(name_id);
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100539 refs_.emplace_back(ref);
540 types_.emplace_back(type);
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000541 hash_to_row_idx_.emplace(digest, size() - 1);
542 return size() - 1;
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100543 }
Lalit Magantifde29042018-10-04 13:28:52 +0100544
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000545 uint32_t size() const { return static_cast<uint32_t>(name_ids_.size()); }
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100546
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100547 const std::deque<StringId>& name_ids() const { return name_ids_; }
548
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100549 const std::deque<int64_t>& refs() const { return refs_; }
550
551 const std::deque<RefType>& types() const { return types_; }
552
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000553 private:
554 std::deque<StringId> name_ids_;
555 std::deque<int64_t> refs_;
556 std::deque<RefType> types_;
557
558 std::unordered_map<uint64_t, uint32_t> hash_to_row_idx_;
559 };
560
561 class CounterValues {
562 public:
563 inline uint32_t AddCounterValue(CounterDefinitions::Id counter_id,
564 int64_t timestamp,
565 double value) {
566 counter_ids_.emplace_back(counter_id);
567 timestamps_.emplace_back(timestamp);
568 values_.emplace_back(value);
569 arg_set_ids_.emplace_back(kInvalidArgSetId);
Ioannis Ilkosf129c262019-04-25 14:53:51 +0100570
Lalit Maganti521d97b2019-04-29 13:47:03 +0100571 if (counter_id != CounterDefinitions::kInvalidId) {
572 if (counter_id >= rows_for_counter_id_.size()) {
573 rows_for_counter_id_.resize(counter_id + 1);
574 }
575 rows_for_counter_id_[counter_id].emplace_back(size() - 1);
576 }
577 return size() - 1;
578 }
579
580 void set_counter_id(uint32_t index, CounterDefinitions::Id counter_id) {
581 PERFETTO_DCHECK(counter_ids_[index] == CounterDefinitions::kInvalidId);
582
583 counter_ids_[index] = counter_id;
Ioannis Ilkosf129c262019-04-25 14:53:51 +0100584 if (counter_id >= rows_for_counter_id_.size()) {
585 rows_for_counter_id_.resize(counter_id + 1);
586 }
Lalit Maganti521d97b2019-04-29 13:47:03 +0100587
588 auto* new_rows = &rows_for_counter_id_[counter_id];
589 new_rows->insert(
590 std::upper_bound(new_rows->begin(), new_rows->end(), index), index);
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000591 }
592
593 void set_arg_set_id(uint32_t row, ArgSetId id) { arg_set_ids_[row] = id; }
594
595 uint32_t size() const { return static_cast<uint32_t>(counter_ids_.size()); }
596
597 const std::deque<CounterDefinitions::Id>& counter_ids() const {
598 return counter_ids_;
599 }
600
601 const std::deque<int64_t>& timestamps() const { return timestamps_; }
602
603 const std::deque<double>& values() const { return values_; }
604
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000605 const std::deque<ArgSetId>& arg_set_ids() const { return arg_set_ids_; }
606
Ioannis Ilkosf129c262019-04-25 14:53:51 +0100607 const std::deque<std::vector<uint32_t>>& rows_for_counter_id() const {
608 return rows_for_counter_id_;
609 }
610
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100611 private:
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000612 std::deque<CounterDefinitions::Id> counter_ids_;
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000613 std::deque<int64_t> timestamps_;
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100614 std::deque<double> values_;
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000615 std::deque<ArgSetId> arg_set_ids_;
Ioannis Ilkosf129c262019-04-25 14:53:51 +0100616
617 // Indexed by counter_id value and contains the row numbers corresponding to
618 // it.
619 std::deque<std::vector<uint32_t>> rows_for_counter_id_;
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100620 };
621
Primiano Tucci5cb84f82018-10-31 21:46:36 -0700622 class SqlStats {
623 public:
624 static constexpr size_t kMaxLogEntries = 100;
Lalit Magantiaac2f652019-04-30 12:16:21 +0100625 uint32_t RecordQueryBegin(const std::string& query,
626 int64_t time_queued,
627 int64_t time_started);
628 void RecordQueryFirstNext(uint32_t row, int64_t time_first_next);
629 void RecordQueryEnd(uint32_t row, int64_t time_end);
Primiano Tucci5cb84f82018-10-31 21:46:36 -0700630 size_t size() const { return queries_.size(); }
631 const std::deque<std::string>& queries() const { return queries_; }
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000632 const std::deque<int64_t>& times_queued() const { return times_queued_; }
633 const std::deque<int64_t>& times_started() const { return times_started_; }
Lalit Magantiaac2f652019-04-30 12:16:21 +0100634 const std::deque<int64_t>& times_first_next() const {
635 return times_first_next_;
636 }
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000637 const std::deque<int64_t>& times_ended() const { return times_ended_; }
Primiano Tucci5cb84f82018-10-31 21:46:36 -0700638
639 private:
Lalit Magantiaac2f652019-04-30 12:16:21 +0100640 uint32_t popped_queries_ = 0;
641
Primiano Tucci5cb84f82018-10-31 21:46:36 -0700642 std::deque<std::string> queries_;
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000643 std::deque<int64_t> times_queued_;
644 std::deque<int64_t> times_started_;
Lalit Magantiaac2f652019-04-30 12:16:21 +0100645 std::deque<int64_t> times_first_next_;
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000646 std::deque<int64_t> times_ended_;
Primiano Tucci5cb84f82018-10-31 21:46:36 -0700647 };
648
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000649 class Instants {
650 public:
Lalit Maganti66ed7ad2019-01-11 16:47:26 +0000651 inline uint32_t AddInstantEvent(int64_t timestamp,
652 StringId name_id,
653 double value,
654 int64_t ref,
655 RefType type) {
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000656 timestamps_.emplace_back(timestamp);
657 name_ids_.emplace_back(name_id);
658 values_.emplace_back(value);
659 refs_.emplace_back(ref);
660 types_.emplace_back(type);
Lalit Maganti1c21d172019-02-07 10:48:24 +0000661 arg_set_ids_.emplace_back(kInvalidArgSetId);
Lalit Maganti66ed7ad2019-01-11 16:47:26 +0000662 return static_cast<uint32_t>(instant_count() - 1);
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000663 }
664
Lalit Maganti521d97b2019-04-29 13:47:03 +0100665 void set_ref(uint32_t row, int64_t ref) { refs_[row] = ref; }
666
Lalit Maganti1c21d172019-02-07 10:48:24 +0000667 void set_arg_set_id(uint32_t row, ArgSetId id) { arg_set_ids_[row] = id; }
668
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000669 size_t instant_count() const { return timestamps_.size(); }
670
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000671 const std::deque<int64_t>& timestamps() const { return timestamps_; }
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000672
673 const std::deque<StringId>& name_ids() const { return name_ids_; }
674
675 const std::deque<double>& values() const { return values_; }
676
677 const std::deque<int64_t>& refs() const { return refs_; }
678
679 const std::deque<RefType>& types() const { return types_; }
680
Lalit Maganti1c21d172019-02-07 10:48:24 +0000681 const std::deque<ArgSetId>& arg_set_ids() const { return arg_set_ids_; }
682
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000683 private:
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000684 std::deque<int64_t> timestamps_;
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000685 std::deque<StringId> name_ids_;
686 std::deque<double> values_;
687 std::deque<int64_t> refs_;
688 std::deque<RefType> types_;
Lalit Maganti1c21d172019-02-07 10:48:24 +0000689 std::deque<ArgSetId> arg_set_ids_;
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000690 };
691
Lalit Maganti1d915a62019-01-07 12:10:42 +0000692 class RawEvents {
693 public:
694 inline RowId AddRawEvent(int64_t timestamp,
695 StringId name_id,
Lalit Magantie87cc812019-01-10 15:20:06 +0000696 uint32_t cpu,
Lalit Maganti1d915a62019-01-07 12:10:42 +0000697 UniqueTid utid) {
698 timestamps_.emplace_back(timestamp);
699 name_ids_.emplace_back(name_id);
Lalit Magantie87cc812019-01-10 15:20:06 +0000700 cpus_.emplace_back(cpu);
Lalit Maganti1d915a62019-01-07 12:10:42 +0000701 utids_.emplace_back(utid);
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000702 arg_set_ids_.emplace_back(kInvalidArgSetId);
Lalit Maganti1d915a62019-01-07 12:10:42 +0000703 return CreateRowId(TableId::kRawEvents,
704 static_cast<uint32_t>(raw_event_count() - 1));
705 }
706
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000707 void set_arg_set_id(uint32_t row, ArgSetId id) { arg_set_ids_[row] = id; }
708
Lalit Maganti1d915a62019-01-07 12:10:42 +0000709 size_t raw_event_count() const { return timestamps_.size(); }
710
711 const std::deque<int64_t>& timestamps() const { return timestamps_; }
712
713 const std::deque<StringId>& name_ids() const { return name_ids_; }
714
Lalit Magantie87cc812019-01-10 15:20:06 +0000715 const std::deque<uint32_t>& cpus() const { return cpus_; }
716
Lalit Maganti1d915a62019-01-07 12:10:42 +0000717 const std::deque<UniqueTid>& utids() const { return utids_; }
718
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000719 const std::deque<ArgSetId>& arg_set_ids() const { return arg_set_ids_; }
720
Lalit Maganti1d915a62019-01-07 12:10:42 +0000721 private:
722 std::deque<int64_t> timestamps_;
723 std::deque<StringId> name_ids_;
Lalit Magantie87cc812019-01-10 15:20:06 +0000724 std::deque<uint32_t> cpus_;
Lalit Maganti1d915a62019-01-07 12:10:42 +0000725 std::deque<UniqueTid> utids_;
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000726 std::deque<ArgSetId> arg_set_ids_;
Lalit Maganti1d915a62019-01-07 12:10:42 +0000727 };
728
Primiano Tucci2c761ef2019-01-07 20:20:46 +0000729 class AndroidLogs {
730 public:
731 inline size_t AddLogEvent(int64_t timestamp,
732 UniqueTid utid,
733 uint8_t prio,
734 StringId tag_id,
735 StringId msg_id) {
736 timestamps_.emplace_back(timestamp);
737 utids_.emplace_back(utid);
738 prios_.emplace_back(prio);
739 tag_ids_.emplace_back(tag_id);
740 msg_ids_.emplace_back(msg_id);
741 return size() - 1;
742 }
743
744 size_t size() const { return timestamps_.size(); }
745
746 const std::deque<int64_t>& timestamps() const { return timestamps_; }
747 const std::deque<UniqueTid>& utids() const { return utids_; }
748 const std::deque<uint8_t>& prios() const { return prios_; }
749 const std::deque<StringId>& tag_ids() const { return tag_ids_; }
750 const std::deque<StringId>& msg_ids() const { return msg_ids_; }
751
752 private:
753 std::deque<int64_t> timestamps_;
754 std::deque<UniqueTid> utids_;
755 std::deque<uint8_t> prios_;
756 std::deque<StringId> tag_ids_;
757 std::deque<StringId> msg_ids_;
758 };
759
Primiano Tucci0e38a142019-01-07 20:51:09 +0000760 struct Stats {
761 using IndexMap = std::map<int, int64_t>;
762 int64_t value = 0;
763 IndexMap indexed_values;
764 };
765 using StatsMap = std::array<Stats, stats::kNumKeys>;
766
Ryan Savitski51413ad2019-07-09 14:25:21 +0100767 class Metadata {
768 public:
769 const std::deque<metadata::KeyIDs>& keys() const { return keys_; }
770 const std::deque<Variadic>& values() const { return values_; }
771
772 RowId SetScalarMetadata(metadata::KeyIDs key, Variadic value) {
773 PERFETTO_DCHECK(key < metadata::kNumKeys);
774 PERFETTO_DCHECK(metadata::kKeyTypes[key] == metadata::kSingle);
775 PERFETTO_DCHECK(value.type == metadata::kValueTypes[key]);
776
777 // Already set - on release builds, overwrite the previous value.
778 auto it = scalar_indices.find(key);
779 if (it != scalar_indices.end()) {
780 PERFETTO_DFATAL("Setting a scalar metadata entry more than once.");
781 uint32_t index = static_cast<uint32_t>(it->second);
782 values_[index] = value;
783 return TraceStorage::CreateRowId(kMetadataTable, index);
784 }
785 // First time setting this key.
786 keys_.push_back(key);
787 values_.push_back(value);
788 uint32_t index = static_cast<uint32_t>(keys_.size() - 1);
789 scalar_indices[key] = index;
790 return TraceStorage::CreateRowId(kMetadataTable, index);
791 }
792
793 RowId AppendMetadata(metadata::KeyIDs key, Variadic value) {
794 PERFETTO_DCHECK(key < metadata::kNumKeys);
795 PERFETTO_DCHECK(metadata::kKeyTypes[key] == metadata::kMulti);
796 PERFETTO_DCHECK(value.type == metadata::kValueTypes[key]);
797
798 keys_.push_back(key);
799 values_.push_back(value);
800 uint32_t index = static_cast<uint32_t>(keys_.size() - 1);
801 return TraceStorage::CreateRowId(kMetadataTable, index);
802 }
803
Ryan Savitski0476ee92019-07-09 14:29:33 +0100804 void OverwriteMetadata(uint32_t index, Variadic value) {
805 PERFETTO_DCHECK(index < values_.size());
806 values_[index] = value;
807 }
808
Ryan Savitski51413ad2019-07-09 14:25:21 +0100809 private:
810 std::deque<metadata::KeyIDs> keys_;
811 std::deque<Variadic> values_;
812 // Extraneous state to track locations of entries that should have at most
813 // one row. Used only to maintain uniqueness during insertions.
814 std::map<metadata::KeyIDs, uint32_t> scalar_indices;
815 };
Mikhail Khokhlove466c002019-05-23 13:33:33 +0100816
Florian Mayer438b5ab2019-05-02 11:18:06 +0100817 class HeapProfileFrames {
818 public:
Florian Mayerbee52132019-05-02 13:59:56 +0100819 struct Row {
820 StringId name_id;
821 int64_t mapping_row;
822 int64_t rel_pc;
Florian Mayer438b5ab2019-05-02 11:18:06 +0100823
Florian Mayerbee52132019-05-02 13:59:56 +0100824 bool operator==(const Row& other) const {
825 return std::tie(name_id, mapping_row, rel_pc) ==
826 std::tie(other.name_id, other.mapping_row, other.rel_pc);
827 }
828 };
829
Lalit Maganti9b2d52b2019-05-07 14:32:15 +0100830 uint32_t size() const { return static_cast<uint32_t>(names_.size()); }
831
Florian Mayerbee52132019-05-02 13:59:56 +0100832 int64_t Insert(const Row& row) {
833 names_.emplace_back(row.name_id);
834 mappings_.emplace_back(row.mapping_row);
835 rel_pcs_.emplace_back(row.rel_pc);
Florian Mayer438b5ab2019-05-02 11:18:06 +0100836 return static_cast<int64_t>(names_.size()) - 1;
837 }
838
839 const std::deque<StringId>& names() const { return names_; }
840 const std::deque<int64_t>& mappings() const { return mappings_; }
841 const std::deque<int64_t>& rel_pcs() const { return rel_pcs_; }
842
843 private:
844 std::deque<StringId> names_;
845 std::deque<int64_t> mappings_;
846 std::deque<int64_t> rel_pcs_;
847 };
848
849 class HeapProfileCallsites {
850 public:
Florian Mayerbee52132019-05-02 13:59:56 +0100851 struct Row {
852 int64_t depth;
853 int64_t parent_id;
854 int64_t frame_row;
855
856 bool operator==(const Row& other) const {
857 return std::tie(depth, parent_id, frame_row) ==
858 std::tie(other.depth, other.parent_id, other.frame_row);
Florian Mayer438b5ab2019-05-02 11:18:06 +0100859 }
Florian Mayerbee52132019-05-02 13:59:56 +0100860 };
861
Lalit Maganti9b2d52b2019-05-07 14:32:15 +0100862 uint32_t size() const { return static_cast<uint32_t>(frame_ids_.size()); }
863
Florian Mayerbee52132019-05-02 13:59:56 +0100864 int64_t Insert(const Row& row) {
865 frame_depths_.emplace_back(row.depth);
866 parent_callsite_ids_.emplace_back(row.parent_id);
867 frame_ids_.emplace_back(row.frame_row);
Florian Mayer438b5ab2019-05-02 11:18:06 +0100868 return static_cast<int64_t>(frame_depths_.size()) - 1;
869 }
870
871 const std::deque<int64_t>& frame_depths() const { return frame_depths_; }
872 const std::deque<int64_t>& parent_callsite_ids() const {
873 return parent_callsite_ids_;
874 }
875 const std::deque<int64_t>& frame_ids() const { return frame_ids_; }
876
877 private:
878 std::deque<int64_t> frame_depths_;
879 std::deque<int64_t> parent_callsite_ids_;
880 std::deque<int64_t> frame_ids_;
881 };
882
883 class HeapProfileMappings {
884 public:
Florian Mayerbee52132019-05-02 13:59:56 +0100885 struct Row {
886 StringId build_id;
Florian Mayer12655732019-07-02 15:08:26 +0100887 int64_t exact_offset;
888 int64_t start_offset;
Florian Mayerbee52132019-05-02 13:59:56 +0100889 int64_t start;
890 int64_t end;
891 int64_t load_bias;
892 StringId name_id;
Florian Mayer438b5ab2019-05-02 11:18:06 +0100893
Florian Mayerbee52132019-05-02 13:59:56 +0100894 bool operator==(const Row& other) const {
Florian Mayer12655732019-07-02 15:08:26 +0100895 return std::tie(build_id, exact_offset, start_offset, start, end,
896 load_bias, name_id) ==
897 std::tie(other.build_id, other.exact_offset, other.start_offset,
898 other.start, other.end, other.load_bias, other.name_id);
Florian Mayerbee52132019-05-02 13:59:56 +0100899 }
900 };
901
Lalit Maganti9b2d52b2019-05-07 14:32:15 +0100902 uint32_t size() const { return static_cast<uint32_t>(names_.size()); }
903
Florian Mayerbee52132019-05-02 13:59:56 +0100904 int64_t Insert(const Row& row) {
905 build_ids_.emplace_back(row.build_id);
Florian Mayer12655732019-07-02 15:08:26 +0100906 exact_offsets_.emplace_back(row.exact_offset);
907 start_offsets_.emplace_back(row.start_offset);
Florian Mayerbee52132019-05-02 13:59:56 +0100908 starts_.emplace_back(row.start);
909 ends_.emplace_back(row.end);
910 load_biases_.emplace_back(row.load_bias);
911 names_.emplace_back(row.name_id);
Florian Mayer438b5ab2019-05-02 11:18:06 +0100912 return static_cast<int64_t>(build_ids_.size()) - 1;
913 }
914
915 const std::deque<StringId>& build_ids() const { return build_ids_; }
Florian Mayer12655732019-07-02 15:08:26 +0100916 const std::deque<int64_t>& exact_offsets() const { return exact_offsets_; }
917 const std::deque<int64_t>& start_offsets() const { return start_offsets_; }
Florian Mayer438b5ab2019-05-02 11:18:06 +0100918 const std::deque<int64_t>& starts() const { return starts_; }
919 const std::deque<int64_t>& ends() const { return ends_; }
920 const std::deque<int64_t>& load_biases() const { return load_biases_; }
921 const std::deque<StringId>& names() const { return names_; }
922
923 private:
924 std::deque<StringId> build_ids_;
Florian Mayer12655732019-07-02 15:08:26 +0100925 std::deque<int64_t> exact_offsets_;
926 std::deque<int64_t> start_offsets_;
Florian Mayer438b5ab2019-05-02 11:18:06 +0100927 std::deque<int64_t> starts_;
928 std::deque<int64_t> ends_;
929 std::deque<int64_t> load_biases_;
930 std::deque<StringId> names_;
931 };
932
933 class HeapProfileAllocations {
934 public:
Florian Mayerbee52132019-05-02 13:59:56 +0100935 struct Row {
936 int64_t timestamp;
Florian Mayerdf1968c2019-05-13 16:39:35 +0100937 UniquePid upid;
Florian Mayerbee52132019-05-02 13:59:56 +0100938 int64_t callsite_id;
939 int64_t count;
940 int64_t size;
941 };
942
Lalit Maganti9b2d52b2019-05-07 14:32:15 +0100943 uint32_t size() const { return static_cast<uint32_t>(timestamps_.size()); }
944
Florian Mayerbee52132019-05-02 13:59:56 +0100945 void Insert(const Row& row) {
946 timestamps_.emplace_back(row.timestamp);
Florian Mayerdf1968c2019-05-13 16:39:35 +0100947 upids_.emplace_back(row.upid);
Florian Mayerbee52132019-05-02 13:59:56 +0100948 callsite_ids_.emplace_back(row.callsite_id);
949 counts_.emplace_back(row.count);
950 sizes_.emplace_back(row.size);
Florian Mayer438b5ab2019-05-02 11:18:06 +0100951 }
952
953 const std::deque<int64_t>& timestamps() const { return timestamps_; }
Florian Mayerdf1968c2019-05-13 16:39:35 +0100954 const std::deque<UniquePid>& upids() const { return upids_; }
Florian Mayer438b5ab2019-05-02 11:18:06 +0100955 const std::deque<int64_t>& callsite_ids() const { return callsite_ids_; }
956 const std::deque<int64_t>& counts() const { return counts_; }
957 const std::deque<int64_t>& sizes() const { return sizes_; }
958
959 private:
960 std::deque<int64_t> timestamps_;
Florian Mayerdf1968c2019-05-13 16:39:35 +0100961 std::deque<UniquePid> upids_;
Florian Mayer438b5ab2019-05-02 11:18:06 +0100962 std::deque<int64_t> callsite_ids_;
963 std::deque<int64_t> counts_;
964 std::deque<int64_t> sizes_;
965 };
966
Isabelle Taylora0a22972018-08-03 12:06:12 +0100967 void ResetStorage();
Lalit Maganti35622b72018-06-06 12:03:11 +0100968
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100969 UniqueTid AddEmptyThread(uint32_t tid) {
970 unique_threads_.emplace_back(tid);
Isabelle Taylora0a22972018-08-03 12:06:12 +0100971 return static_cast<UniqueTid>(unique_threads_.size() - 1);
972 }
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100973
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100974 UniquePid AddEmptyProcess(uint32_t pid) {
975 unique_processes_.emplace_back(pid);
Isabelle Taylora0a22972018-08-03 12:06:12 +0100976 return static_cast<UniquePid>(unique_processes_.size() - 1);
977 }
Isabelle Taylor3dd366c2018-06-22 16:21:41 +0100978
Isabelle Taylora0a22972018-08-03 12:06:12 +0100979 // Return an unqiue identifier for the contents of each string.
980 // The string is copied internally and can be destroyed after this called.
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100981 // Virtual for testing.
Lalit Maganti5c454312019-04-08 12:11:17 +0100982 virtual StringId InternString(base::StringView str) {
983 return string_pool_.InternString(str);
984 }
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100985
Isabelle Taylora0a22972018-08-03 12:06:12 +0100986 Process* GetMutableProcess(UniquePid upid) {
Lalit Maganti676658b2018-11-20 18:24:31 +0000987 PERFETTO_DCHECK(upid < unique_processes_.size());
Isabelle Taylora0a22972018-08-03 12:06:12 +0100988 return &unique_processes_[upid];
989 }
Isabelle Taylor3dd366c2018-06-22 16:21:41 +0100990
Isabelle Taylora0a22972018-08-03 12:06:12 +0100991 Thread* GetMutableThread(UniqueTid utid) {
Florian Mayera1aaec22018-09-19 17:02:26 +0100992 PERFETTO_DCHECK(utid < unique_threads_.size());
Isabelle Taylora0a22972018-08-03 12:06:12 +0100993 return &unique_threads_[utid];
994 }
Isabelle Taylor3dd366c2018-06-22 16:21:41 +0100995
Primiano Tucci0e38a142019-01-07 20:51:09 +0000996 // Example usage: SetStats(stats::android_log_num_failed, 42);
997 void SetStats(size_t key, int64_t value) {
998 PERFETTO_DCHECK(key < stats::kNumKeys);
999 PERFETTO_DCHECK(stats::kTypes[key] == stats::kSingle);
1000 stats_[key].value = value;
1001 }
1002
1003 // Example usage: IncrementStats(stats::android_log_num_failed, -1);
1004 void IncrementStats(size_t key, int64_t increment = 1) {
1005 PERFETTO_DCHECK(key < stats::kNumKeys);
1006 PERFETTO_DCHECK(stats::kTypes[key] == stats::kSingle);
1007 stats_[key].value += increment;
1008 }
1009
Florian Mayer438b5ab2019-05-02 11:18:06 +01001010 // Example usage: IncrementIndexedStats(stats::cpu_failure, 1);
1011 void IncrementIndexedStats(size_t key, int index, int64_t increment = 1) {
1012 PERFETTO_DCHECK(key < stats::kNumKeys);
1013 PERFETTO_DCHECK(stats::kTypes[key] == stats::kIndexed);
1014 stats_[key].indexed_values[index] += increment;
1015 }
1016
Primiano Tucci0e38a142019-01-07 20:51:09 +00001017 // Example usage: SetIndexedStats(stats::cpu_failure, 1, 42);
1018 void SetIndexedStats(size_t key, int index, int64_t value) {
1019 PERFETTO_DCHECK(key < stats::kNumKeys);
1020 PERFETTO_DCHECK(stats::kTypes[key] == stats::kIndexed);
1021 stats_[key].indexed_values[index] = value;
1022 }
1023
Mikhail Khokhlove466c002019-05-23 13:33:33 +01001024 // Example usage:
1025 // SetMetadata(metadata::benchmark_name,
1026 // Variadic::String(storage->InternString("foo"));
Ryan Savitski51413ad2019-07-09 14:25:21 +01001027 // Returns the RowId of the new entry.
Mikhail Khokhlovb1fe42a2019-05-23 13:58:05 +01001028 // Virtual for testing.
Ryan Savitski51413ad2019-07-09 14:25:21 +01001029 virtual RowId SetMetadata(metadata::KeyIDs key, Variadic value) {
1030 return metadata_.SetScalarMetadata(key, value);
Mikhail Khokhlove466c002019-05-23 13:33:33 +01001031 }
1032
1033 // Example usage:
1034 // AppendMetadata(metadata::benchmark_story_tags,
1035 // Variadic::String(storage->InternString("bar"));
Ryan Savitski51413ad2019-07-09 14:25:21 +01001036 // Returns the RowId of the new entry.
Mikhail Khokhlovb1fe42a2019-05-23 13:58:05 +01001037 // Virtual for testing.
Ryan Savitski51413ad2019-07-09 14:25:21 +01001038 virtual RowId AppendMetadata(metadata::KeyIDs key, Variadic value) {
1039 return metadata_.AppendMetadata(key, value);
Mikhail Khokhlove466c002019-05-23 13:33:33 +01001040 }
1041
Eric Seckler77b52782019-05-02 15:18:57 +01001042 class ScopedStatsTracer {
1043 public:
1044 ScopedStatsTracer(TraceStorage* storage, size_t key)
1045 : storage_(storage), key_(key), start_ns_(base::GetWallTimeNs()) {}
1046
1047 ~ScopedStatsTracer() {
1048 if (!storage_)
1049 return;
1050 auto delta_ns = base::GetWallTimeNs() - start_ns_;
1051 storage_->IncrementStats(key_, delta_ns.count());
1052 }
1053
1054 ScopedStatsTracer(ScopedStatsTracer&& other) noexcept { MoveImpl(&other); }
1055
1056 ScopedStatsTracer& operator=(ScopedStatsTracer&& other) {
1057 MoveImpl(&other);
1058 return *this;
1059 }
1060
1061 private:
1062 ScopedStatsTracer(const ScopedStatsTracer&) = delete;
1063 ScopedStatsTracer& operator=(const ScopedStatsTracer&) = delete;
1064
1065 void MoveImpl(ScopedStatsTracer* other) {
1066 storage_ = other->storage_;
1067 key_ = other->key_;
1068 start_ns_ = other->start_ns_;
1069 other->storage_ = nullptr;
1070 }
1071
1072 TraceStorage* storage_;
1073 size_t key_;
1074 base::TimeNanos start_ns_;
1075 };
1076
1077 ScopedStatsTracer TraceExecutionTimeIntoStats(size_t key) {
1078 return ScopedStatsTracer(this, key);
1079 }
1080
Lalit Maganticaed37e2018-06-01 03:03:08 +01001081 // Reading methods.
Eric Secklerc93823e2019-06-03 16:49:19 +01001082 // Virtual for testing.
1083 virtual NullTermStringView GetString(StringId id) const {
Lalit Maganti5c454312019-04-08 12:11:17 +01001084 return string_pool_.Get(id);
Isabelle Taylora0a22972018-08-03 12:06:12 +01001085 }
1086
Lalit Magantie9d40532018-06-29 13:15:06 +01001087 const Process& GetProcess(UniquePid upid) const {
Lalit Maganti676658b2018-11-20 18:24:31 +00001088 PERFETTO_DCHECK(upid < unique_processes_.size());
Isabelle Taylor47328cf2018-06-12 14:33:59 +01001089 return unique_processes_[upid];
1090 }
1091
Lalit Magantie9d40532018-06-29 13:15:06 +01001092 const Thread& GetThread(UniqueTid utid) const {
Lalit Maganti1f64cfa2018-09-18 13:20:02 +01001093 // Allow utid == 0 for idle thread retrieval.
Florian Mayera1aaec22018-09-19 17:02:26 +01001094 PERFETTO_DCHECK(utid < unique_threads_.size());
Isabelle Taylor68e42192018-06-19 16:19:31 +01001095 return unique_threads_[utid];
1096 }
1097
Lalit Maganti5ea9e932018-11-30 14:19:39 +00001098 static RowId CreateRowId(TableId table, uint32_t row) {
Lalit Maganti85ca4a82018-12-07 17:28:02 +00001099 return (static_cast<RowId>(table) << kRowIdTableShift) | row;
Lalit Maganti5ea9e932018-11-30 14:19:39 +00001100 }
1101
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +00001102 static std::pair<int8_t /*table*/, uint32_t /*row*/> ParseRowId(RowId rowid) {
1103 auto id = static_cast<uint64_t>(rowid);
1104 auto table_id = static_cast<uint8_t>(id >> kRowIdTableShift);
1105 auto row = static_cast<uint32_t>(id & ((1ull << kRowIdTableShift) - 1));
1106 return std::make_pair(table_id, row);
1107 }
1108
Lalit Magantid11d3e02019-07-26 12:32:09 +05301109 const Tracks& tracks() const { return tracks_; }
1110 Tracks* mutable_tracks() { return &tracks_; }
1111
Eric Seckler5703ede2019-07-10 10:13:02 +01001112 const VirtualTracks& virtual_tracks() const { return virtual_tracks_; }
1113 VirtualTracks* mutable_virtual_tracks() { return &virtual_tracks_; }
1114
Lalit Magantiff69c112018-09-24 12:07:47 +01001115 const Slices& slices() const { return slices_; }
Lalit Magantifde29042018-10-04 13:28:52 +01001116 Slices* mutable_slices() { return &slices_; }
1117
Primiano Tucci0d72a312018-08-07 14:42:45 +01001118 const NestableSlices& nestable_slices() const { return nestable_slices_; }
1119 NestableSlices* mutable_nestable_slices() { return &nestable_slices_; }
1120
Eric Secklerd3b89d52019-07-10 15:36:29 +01001121 const ThreadSlices& thread_slices() const { return thread_slices_; }
1122 ThreadSlices* mutable_thread_slices() { return &thread_slices_; }
1123
Eric Secklerc3430c62019-07-22 10:49:58 +01001124 const VirtualTrackSlices& virtual_track_slices() const {
1125 return virtual_track_slices_;
1126 }
1127 VirtualTrackSlices* mutable_virtual_track_slices() {
1128 return &virtual_track_slices_;
1129 }
1130
Lalit Maganti8320e6d2019-03-14 18:49:33 +00001131 const CounterDefinitions& counter_definitions() const {
1132 return counter_definitions_;
1133 }
1134 CounterDefinitions* mutable_counter_definitions() {
1135 return &counter_definitions_;
1136 }
1137
1138 const CounterValues& counter_values() const { return counter_values_; }
1139 CounterValues* mutable_counter_values() { return &counter_values_; }
Isabelle Taylor14674d42018-09-07 11:33:11 +01001140
Primiano Tucci5cb84f82018-10-31 21:46:36 -07001141 const SqlStats& sql_stats() const { return sql_stats_; }
1142 SqlStats* mutable_sql_stats() { return &sql_stats_; }
1143
Isabelle Taylorc8c11202018-11-05 11:36:22 +00001144 const Instants& instants() const { return instants_; }
1145 Instants* mutable_instants() { return &instants_; }
1146
Primiano Tucci2c761ef2019-01-07 20:20:46 +00001147 const AndroidLogs& android_logs() const { return android_log_; }
1148 AndroidLogs* mutable_android_log() { return &android_log_; }
1149
Primiano Tucci0e38a142019-01-07 20:51:09 +00001150 const StatsMap& stats() const { return stats_; }
Lalit Maganti05e8c132018-11-09 18:16:12 +00001151
Ryan Savitski51413ad2019-07-09 14:25:21 +01001152 const Metadata& metadata() const { return metadata_; }
Ryan Savitski0476ee92019-07-09 14:29:33 +01001153 Metadata* mutable_metadata() { return &metadata_; }
Mikhail Khokhlove466c002019-05-23 13:33:33 +01001154
Lalit Maganti6e9c55e2018-11-29 12:00:39 +00001155 const Args& args() const { return args_; }
1156 Args* mutable_args() { return &args_; }
1157
Lalit Maganti1d915a62019-01-07 12:10:42 +00001158 const RawEvents& raw_events() const { return raw_events_; }
1159 RawEvents* mutable_raw_events() { return &raw_events_; }
1160
Florian Mayer438b5ab2019-05-02 11:18:06 +01001161 const HeapProfileMappings& heap_profile_mappings() const {
1162 return heap_profile_mappings_;
1163 }
1164 HeapProfileMappings* mutable_heap_profile_mappings() {
1165 return &heap_profile_mappings_;
1166 }
1167
1168 const HeapProfileFrames& heap_profile_frames() const {
1169 return heap_profile_frames_;
1170 }
1171 HeapProfileFrames* mutable_heap_profile_frames() {
1172 return &heap_profile_frames_;
1173 }
1174
1175 const HeapProfileCallsites& heap_profile_callsites() const {
1176 return heap_profile_callsites_;
1177 }
1178 HeapProfileCallsites* mutable_heap_profile_callsites() {
1179 return &heap_profile_callsites_;
1180 }
1181
1182 const HeapProfileAllocations& heap_profile_allocations() const {
1183 return heap_profile_allocations_;
1184 }
1185 HeapProfileAllocations* mutable_heap_profile_allocations() {
1186 return &heap_profile_allocations_;
1187 }
1188
Lalit Maganti5c454312019-04-08 12:11:17 +01001189 const StringPool& string_pool() const { return string_pool_; }
Lalit Magantiacda68b2018-10-29 15:23:25 +00001190
Isabelle Taylore7003fb2018-07-17 11:39:01 +01001191 // |unique_processes_| always contains at least 1 element becuase the 0th ID
1192 // is reserved to indicate an invalid process.
Lalit Maganti9c6395b2019-01-17 00:25:09 +00001193 size_t process_count() const { return unique_processes_.size(); }
Primiano Tucci0d72a312018-08-07 14:42:45 +01001194
Isabelle Taylore7003fb2018-07-17 11:39:01 +01001195 // |unique_threads_| always contains at least 1 element becuase the 0th ID
1196 // is reserved to indicate an invalid thread.
Lalit Maganti9c6395b2019-01-17 00:25:09 +00001197 size_t thread_count() const { return unique_threads_.size(); }
Isabelle Taylore7003fb2018-07-17 11:39:01 +01001198
Hector Dearman12323362018-08-09 16:09:28 +01001199 // Number of interned strings in the pool. Includes the empty string w/ ID=0.
1200 size_t string_count() const { return string_pool_.size(); }
1201
Ioannis Ilkosb8b11102019-01-29 17:56:55 +00001202 // Start / end ts (in nanoseconds) across the parsed trace events.
1203 // Returns (0, 0) if the trace is empty.
1204 std::pair<int64_t, int64_t> GetTraceTimestampBoundsNs() const;
1205
Lalit Maganticaed37e2018-06-01 03:03:08 +01001206 private:
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +00001207 static constexpr uint8_t kRowIdTableShift = 32;
Isabelle Taylora0a22972018-08-03 12:06:12 +01001208
Primiano Tucci2da5d2e2018-08-10 14:23:31 +01001209 using StringHash = uint64_t;
Lalit Maganticaed37e2018-06-01 03:03:08 +01001210
Lalit Maganti5c454312019-04-08 12:11:17 +01001211 TraceStorage(const TraceStorage&) = delete;
1212 TraceStorage& operator=(const TraceStorage&) = delete;
1213
1214 TraceStorage(TraceStorage&&) = default;
1215 TraceStorage& operator=(TraceStorage&&) = default;
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +00001216
Lalit Maganti05e8c132018-11-09 18:16:12 +00001217 // Stats about parsing the trace.
Primiano Tucci0e38a142019-01-07 20:51:09 +00001218 StatsMap stats_{};
Lalit Maganti35622b72018-06-06 12:03:11 +01001219
Ryan Savitski51413ad2019-07-09 14:25:21 +01001220 // Extra data extracted from the trace. Includes:
1221 // * metadata from chrome and benchmarking infrastructure
1222 // * descriptions of android packages
1223 Metadata metadata_{};
Mikhail Khokhlove466c002019-05-23 13:33:33 +01001224
Lalit Magantid11d3e02019-07-26 12:32:09 +05301225 // Metadata for tracks.
1226 Tracks tracks_;
1227
Eric Seckler5703ede2019-07-10 10:13:02 +01001228 // Metadata for virtual slice tracks.
1229 VirtualTracks virtual_tracks_;
1230
Lalit Maganticaed37e2018-06-01 03:03:08 +01001231 // One entry for each CPU in the trace.
Lalit Magantiff69c112018-09-24 12:07:47 +01001232 Slices slices_;
Lalit Maganticaed37e2018-06-01 03:03:08 +01001233
Lalit Maganti6e9c55e2018-11-29 12:00:39 +00001234 // Args for all other tables.
1235 Args args_;
1236
Lalit Maganticaed37e2018-06-01 03:03:08 +01001237 // One entry for each unique string in the trace.
Lalit Maganti5c454312019-04-08 12:11:17 +01001238 StringPool string_pool_;
Isabelle Taylor47328cf2018-06-12 14:33:59 +01001239
Isabelle Taylor47328cf2018-06-12 14:33:59 +01001240 // One entry for each UniquePid, with UniquePid as the index.
Ioannis Ilkos9c03cbd2019-03-12 18:30:43 +00001241 // Never hold on to pointers to Process, as vector resize will
1242 // invalidate them.
1243 std::vector<Process> unique_processes_;
Isabelle Taylor68e42192018-06-19 16:19:31 +01001244
Isabelle Taylor68e42192018-06-19 16:19:31 +01001245 // One entry for each UniqueTid, with UniqueTid as the index.
Isabelle Taylor3dd366c2018-06-22 16:21:41 +01001246 std::deque<Thread> unique_threads_;
Primiano Tucci0d72a312018-08-07 14:42:45 +01001247
1248 // Slices coming from userspace events (e.g. Chromium TRACE_EVENT macros).
1249 NestableSlices nestable_slices_;
Isabelle Taylor15314ea2018-09-19 11:35:19 +01001250
Eric Secklerd3b89d52019-07-10 15:36:29 +01001251 // Additional attributes for threads slices (sub-type of NestableSlices).
1252 ThreadSlices thread_slices_;
1253
Eric Secklerc3430c62019-07-22 10:49:58 +01001254 // Additional attributes for virtual track slices (sub-type of
1255 // NestableSlices).
1256 VirtualTrackSlices virtual_track_slices_;
1257
Hector Dearman2442d612019-06-04 18:05:23 +01001258 // The type of counters in the trace. Can be thought of as the "metadata".
Lalit Maganti8320e6d2019-03-14 18:49:33 +00001259 CounterDefinitions counter_definitions_;
1260
1261 // The values from the Counter events from the trace. This includes CPU
1262 // frequency events as well systrace trace_marker counter events.
1263 CounterValues counter_values_;
Primiano Tucci5cb84f82018-10-31 21:46:36 -07001264
1265 SqlStats sql_stats_;
Lalit Maganti6e9c55e2018-11-29 12:00:39 +00001266
Isabelle Taylorc8c11202018-11-05 11:36:22 +00001267 // These are instantaneous events in the trace. They have no duration
1268 // and do not have a value that make sense to track over time.
1269 // e.g. signal events
1270 Instants instants_;
Lalit Maganti1d915a62019-01-07 12:10:42 +00001271
1272 // Raw events are every ftrace event in the trace. The raw event includes
1273 // the timestamp and the pid. The args for the raw event will be in the
1274 // args table. This table can be used to generate a text version of the
1275 // trace.
1276 RawEvents raw_events_;
Primiano Tucci2c761ef2019-01-07 20:20:46 +00001277 AndroidLogs android_log_;
Florian Mayer438b5ab2019-05-02 11:18:06 +01001278
1279 HeapProfileMappings heap_profile_mappings_;
1280 HeapProfileFrames heap_profile_frames_;
1281 HeapProfileCallsites heap_profile_callsites_;
1282 HeapProfileAllocations heap_profile_allocations_;
Lalit Maganticaed37e2018-06-01 03:03:08 +01001283};
1284
1285} // namespace trace_processor
1286} // namespace perfetto
1287
Florian Mayerbee52132019-05-02 13:59:56 +01001288namespace std {
1289
1290template <>
1291struct hash<::perfetto::trace_processor::TraceStorage::HeapProfileFrames::Row> {
1292 using argument_type =
1293 ::perfetto::trace_processor::TraceStorage::HeapProfileFrames::Row;
1294 using result_type = size_t;
1295
1296 result_type operator()(const argument_type& r) const {
1297 return std::hash<::perfetto::trace_processor::StringId>{}(r.name_id) ^
1298 std::hash<int64_t>{}(r.mapping_row) ^ std::hash<int64_t>{}(r.rel_pc);
1299 }
1300};
1301
1302template <>
1303struct hash<
1304 ::perfetto::trace_processor::TraceStorage::HeapProfileCallsites::Row> {
1305 using argument_type =
1306 ::perfetto::trace_processor::TraceStorage::HeapProfileCallsites::Row;
1307 using result_type = size_t;
1308
1309 result_type operator()(const argument_type& r) const {
1310 return std::hash<int64_t>{}(r.depth) ^ std::hash<int64_t>{}(r.parent_id) ^
1311 std::hash<int64_t>{}(r.frame_row);
1312 }
1313};
1314
1315template <>
1316struct hash<
1317 ::perfetto::trace_processor::TraceStorage::HeapProfileMappings::Row> {
1318 using argument_type =
1319 ::perfetto::trace_processor::TraceStorage::HeapProfileMappings::Row;
1320 using result_type = size_t;
1321
1322 result_type operator()(const argument_type& r) const {
1323 return std::hash<::perfetto::trace_processor::StringId>{}(r.build_id) ^
Florian Mayer12655732019-07-02 15:08:26 +01001324 std::hash<int64_t>{}(r.exact_offset) ^
1325 std::hash<int64_t>{}(r.start_offset) ^
1326 std::hash<int64_t>{}(r.start) ^ std::hash<int64_t>{}(r.end) ^
1327 std::hash<int64_t>{}(r.load_bias) ^
Florian Mayerbee52132019-05-02 13:59:56 +01001328 std::hash<::perfetto::trace_processor::StringId>{}(r.name_id);
1329 }
1330};
1331
1332} // namespace std
1333
Lalit Maganticaed37e2018-06-01 03:03:08 +01001334#endif // SRC_TRACE_PROCESSOR_TRACE_STORAGE_H_