blob: f3d8f7bbeb8711102582e536886e8d1584c91854 [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
Eric Seckler5703ede2019-07-10 10:13:02 +0100217 class VirtualTracks {
218 public:
219 inline uint32_t AddVirtualTrack(TrackId track_id,
220 StringId name,
221 VirtualTrackScope scope,
222 UniquePid upid = 0u) {
223 track_ids_.emplace_back(track_id);
224 names_.emplace_back(name);
225 scopes_.emplace_back(scope);
226 upids_.emplace_back(upid);
227 return virtual_track_count() - 1;
228 }
229
230 uint32_t virtual_track_count() const {
231 return static_cast<uint32_t>(track_ids_.size());
232 }
233
234 const std::deque<uint32_t>& track_ids() const { return track_ids_; }
235 const std::deque<StringId>& names() const { return names_; }
236 const std::deque<VirtualTrackScope>& scopes() const { return scopes_; }
237 const std::deque<UniquePid>& upids() const { return upids_; }
238
239 private:
240 std::deque<uint32_t> track_ids_;
241 std::deque<StringId> names_;
242 std::deque<VirtualTrackScope> scopes_;
243 std::deque<UniquePid> upids_;
244 };
245
Lalit Magantiff69c112018-09-24 12:07:47 +0100246 class Slices {
Lalit Maganti35622b72018-06-06 12:03:11 +0100247 public:
Lalit Magantifde29042018-10-04 13:28:52 +0100248 inline size_t AddSlice(uint32_t cpu,
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000249 int64_t start_ns,
250 int64_t duration_ns,
Lalit Magantib0b53ee2019-01-24 17:53:39 +0000251 UniqueTid utid,
252 ftrace_utils::TaskState end_state,
253 int32_t priority) {
Lalit Magantiff69c112018-09-24 12:07:47 +0100254 cpus_.emplace_back(cpu);
Lalit Maganti35622b72018-06-06 12:03:11 +0100255 start_ns_.emplace_back(start_ns);
256 durations_.emplace_back(duration_ns);
Isabelle Taylora0a22972018-08-03 12:06:12 +0100257 utids_.emplace_back(utid);
Lalit Magantib0b53ee2019-01-24 17:53:39 +0000258 end_states_.emplace_back(end_state);
259 priorities_.emplace_back(priority);
Lalit Maganti58638932019-01-31 10:48:26 +0000260
261 if (utid >= rows_for_utids_.size())
262 rows_for_utids_.resize(utid + 1);
263 rows_for_utids_[utid].emplace_back(slice_count() - 1);
Lalit Magantifde29042018-10-04 13:28:52 +0100264 return slice_count() - 1;
265 }
266
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000267 void set_duration(size_t index, int64_t duration_ns) {
Lalit Magantifde29042018-10-04 13:28:52 +0100268 durations_[index] = duration_ns;
Lalit Maganti35622b72018-06-06 12:03:11 +0100269 }
270
Lalit Magantib0b53ee2019-01-24 17:53:39 +0000271 void set_end_state(size_t index, ftrace_utils::TaskState end_state) {
272 end_states_[index] = end_state;
273 }
274
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100275 size_t slice_count() const { return start_ns_.size(); }
Lalit Maganti35622b72018-06-06 12:03:11 +0100276
Lalit Magantiff69c112018-09-24 12:07:47 +0100277 const std::deque<uint32_t>& cpus() const { return cpus_; }
278
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000279 const std::deque<int64_t>& start_ns() const { return start_ns_; }
Lalit Maganti35622b72018-06-06 12:03:11 +0100280
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000281 const std::deque<int64_t>& durations() const { return durations_; }
Lalit Maganti35622b72018-06-06 12:03:11 +0100282
Isabelle Taylor68e42192018-06-19 16:19:31 +0100283 const std::deque<UniqueTid>& utids() const { return utids_; }
284
Lalit Magantib0b53ee2019-01-24 17:53:39 +0000285 const std::deque<ftrace_utils::TaskState>& end_state() const {
286 return end_states_;
287 }
288
289 const std::deque<int32_t>& priorities() const { return priorities_; }
290
Lalit Maganti58638932019-01-31 10:48:26 +0000291 const std::deque<std::vector<uint32_t>>& rows_for_utids() const {
Lalit Magantif0f09c32019-01-18 17:29:31 +0000292 return rows_for_utids_;
293 }
294
Lalit Maganti35622b72018-06-06 12:03:11 +0100295 private:
Hector Dearman947f12a2018-09-11 16:50:36 +0100296 // Each deque below has the same number of entries (the number of slices
Lalit Maganti35622b72018-06-06 12:03:11 +0100297 // in the trace for the CPU).
Lalit Magantiff69c112018-09-24 12:07:47 +0100298 std::deque<uint32_t> cpus_;
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000299 std::deque<int64_t> start_ns_;
300 std::deque<int64_t> durations_;
Isabelle Taylor68e42192018-06-19 16:19:31 +0100301 std::deque<UniqueTid> utids_;
Lalit Magantib0b53ee2019-01-24 17:53:39 +0000302 std::deque<ftrace_utils::TaskState> end_states_;
303 std::deque<int32_t> priorities_;
Lalit Maganti58638932019-01-31 10:48:26 +0000304
305 // One row per utid.
306 std::deque<std::vector<uint32_t>> rows_for_utids_;
Primiano Tucci0d72a312018-08-07 14:42:45 +0100307 };
Isabelle Taylor68e42192018-06-19 16:19:31 +0100308
Primiano Tucci0d72a312018-08-07 14:42:45 +0100309 class NestableSlices {
310 public:
Eric Seckler70cc4422019-05-28 16:00:23 +0100311 inline uint32_t AddSlice(int64_t start_ns,
312 int64_t duration_ns,
313 int64_t ref,
314 RefType type,
Lalit Maganti81a02cd2019-07-12 17:05:11 +0100315 StringId category,
Eric Seckler70cc4422019-05-28 16:00:23 +0100316 StringId name,
317 uint8_t depth,
318 int64_t stack_id,
319 int64_t parent_stack_id) {
Primiano Tucci0d72a312018-08-07 14:42:45 +0100320 start_ns_.emplace_back(start_ns);
321 durations_.emplace_back(duration_ns);
Eric Seckler972225e2019-04-18 11:07:12 +0100322 refs_.emplace_back(ref);
323 types_.emplace_back(type);
Lalit Maganti81a02cd2019-07-12 17:05:11 +0100324 categories_.emplace_back(category);
Primiano Tucci0d72a312018-08-07 14:42:45 +0100325 names_.emplace_back(name);
326 depths_.emplace_back(depth);
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100327 stack_ids_.emplace_back(stack_id);
328 parent_stack_ids_.emplace_back(parent_stack_id);
Eric Seckler70cc4422019-05-28 16:00:23 +0100329 arg_set_ids_.emplace_back(kInvalidArgSetId);
Lalit Maganti7b37bbf2018-11-09 15:58:54 +0000330 return slice_count() - 1;
331 }
332
Eric Seckler70cc4422019-05-28 16:00:23 +0100333 void set_duration(uint32_t index, int64_t duration_ns) {
Lalit Maganti7b37bbf2018-11-09 15:58:54 +0000334 durations_[index] = duration_ns;
335 }
336
Eric Seckler70cc4422019-05-28 16:00:23 +0100337 void set_stack_id(uint32_t index, int64_t stack_id) {
Lalit Maganti7b37bbf2018-11-09 15:58:54 +0000338 stack_ids_[index] = stack_id;
Primiano Tucci0d72a312018-08-07 14:42:45 +0100339 }
340
Eric Seckler70cc4422019-05-28 16:00:23 +0100341 void set_arg_set_id(uint32_t index, ArgSetId id) {
342 arg_set_ids_[index] = id;
343 }
344
345 uint32_t slice_count() const {
346 return static_cast<uint32_t>(start_ns_.size());
347 }
348
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000349 const std::deque<int64_t>& start_ns() const { return start_ns_; }
350 const std::deque<int64_t>& durations() const { return durations_; }
Eric Seckler972225e2019-04-18 11:07:12 +0100351 const std::deque<int64_t>& refs() const { return refs_; }
352 const std::deque<RefType>& types() const { return types_; }
Lalit Maganti81a02cd2019-07-12 17:05:11 +0100353 const std::deque<StringId>& categories() const { return categories_; }
Primiano Tucci0d72a312018-08-07 14:42:45 +0100354 const std::deque<StringId>& names() const { return names_; }
355 const std::deque<uint8_t>& depths() const { return depths_; }
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000356 const std::deque<int64_t>& stack_ids() const { return stack_ids_; }
357 const std::deque<int64_t>& parent_stack_ids() const {
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100358 return parent_stack_ids_;
359 }
Eric Seckler70cc4422019-05-28 16:00:23 +0100360 const std::deque<ArgSetId>& arg_set_ids() const { return arg_set_ids_; }
Primiano Tucci0d72a312018-08-07 14:42:45 +0100361
362 private:
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000363 std::deque<int64_t> start_ns_;
364 std::deque<int64_t> durations_;
Eric Seckler972225e2019-04-18 11:07:12 +0100365 std::deque<int64_t> refs_;
366 std::deque<RefType> types_;
Lalit Maganti81a02cd2019-07-12 17:05:11 +0100367 std::deque<StringId> categories_;
Primiano Tucci0d72a312018-08-07 14:42:45 +0100368 std::deque<StringId> names_;
369 std::deque<uint8_t> depths_;
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000370 std::deque<int64_t> stack_ids_;
371 std::deque<int64_t> parent_stack_ids_;
Eric Seckler70cc4422019-05-28 16:00:23 +0100372 std::deque<ArgSetId> arg_set_ids_;
Lalit Maganti35622b72018-06-06 12:03:11 +0100373 };
374
Eric Secklerd3b89d52019-07-10 15:36:29 +0100375 class ThreadSlices {
376 public:
377 inline uint32_t AddThreadSlice(uint32_t slice_id,
378 int64_t thread_timestamp_ns,
379 int64_t thread_duration_ns,
380 int64_t thread_instruction_count,
381 int64_t thread_instruction_delta) {
382 slice_ids_.emplace_back(slice_id);
383 thread_timestamp_ns_.emplace_back(thread_timestamp_ns);
384 thread_duration_ns_.emplace_back(thread_duration_ns);
385 thread_instruction_counts_.emplace_back(thread_instruction_count);
386 thread_instruction_deltas_.emplace_back(thread_instruction_delta);
387 return slice_count() - 1;
388 }
389
Eric Secklerc3430c62019-07-22 10:49:58 +0100390 uint32_t slice_count() const {
391 return static_cast<uint32_t>(slice_ids_.size());
Eric Secklerd3b89d52019-07-10 15:36:29 +0100392 }
393
Eric Secklerc3430c62019-07-22 10:49:58 +0100394 const std::deque<uint32_t>& slice_ids() const { return slice_ids_; }
395 const std::deque<int64_t>& thread_timestamp_ns() const {
396 return thread_timestamp_ns_;
397 }
398 const std::deque<int64_t>& thread_duration_ns() const {
399 return thread_duration_ns_;
400 }
401 const std::deque<int64_t>& thread_instruction_counts() const {
402 return thread_instruction_counts_;
403 }
404 const std::deque<int64_t>& thread_instruction_deltas() const {
405 return thread_instruction_deltas_;
406 }
407
408 base::Optional<uint32_t> FindRowForSliceId(uint32_t slice_id) const {
409 auto it =
410 std::lower_bound(slice_ids().begin(), slice_ids().end(), slice_id);
411 if (it != slice_ids().end() && *it == slice_id) {
412 return static_cast<uint32_t>(std::distance(slice_ids().begin(), it));
413 }
414 return base::nullopt;
415 }
416
Eric Seckler54f30a32019-07-19 15:10:29 +0100417 void UpdateThreadDeltasForSliceId(uint32_t slice_id,
418 int64_t end_thread_timestamp_ns,
419 int64_t end_thread_instruction_count) {
Eric Secklerc3430c62019-07-22 10:49:58 +0100420 uint32_t row = *FindRowForSliceId(slice_id);
421 int64_t begin_ns = thread_timestamp_ns_[row];
422 thread_duration_ns_[row] = end_thread_timestamp_ns - begin_ns;
Eric Seckler54f30a32019-07-19 15:10:29 +0100423 int64_t begin_ticount = thread_instruction_counts_[row];
424 thread_instruction_deltas_[row] =
425 end_thread_instruction_count - begin_ticount;
Eric Secklerc3430c62019-07-22 10:49:58 +0100426 }
427
428 private:
429 std::deque<uint32_t> slice_ids_;
430 std::deque<int64_t> thread_timestamp_ns_;
431 std::deque<int64_t> thread_duration_ns_;
432 std::deque<int64_t> thread_instruction_counts_;
433 std::deque<int64_t> thread_instruction_deltas_;
434 };
435
436 class VirtualTrackSlices {
437 public:
438 inline uint32_t AddVirtualTrackSlice(uint32_t slice_id,
439 int64_t thread_timestamp_ns,
440 int64_t thread_duration_ns,
441 int64_t thread_instruction_count,
442 int64_t thread_instruction_delta) {
443 slice_ids_.emplace_back(slice_id);
444 thread_timestamp_ns_.emplace_back(thread_timestamp_ns);
445 thread_duration_ns_.emplace_back(thread_duration_ns);
446 thread_instruction_counts_.emplace_back(thread_instruction_count);
447 thread_instruction_deltas_.emplace_back(thread_instruction_delta);
448 return slice_count() - 1;
Eric Secklerd3b89d52019-07-10 15:36:29 +0100449 }
450
451 uint32_t slice_count() const {
452 return static_cast<uint32_t>(slice_ids_.size());
453 }
454
455 const std::deque<uint32_t>& slice_ids() const { return slice_ids_; }
456 const std::deque<int64_t>& thread_timestamp_ns() const {
457 return thread_timestamp_ns_;
458 }
459 const std::deque<int64_t>& thread_duration_ns() const {
460 return thread_duration_ns_;
461 }
462 const std::deque<int64_t>& thread_instruction_counts() const {
463 return thread_instruction_counts_;
464 }
465 const std::deque<int64_t>& thread_instruction_deltas() const {
466 return thread_instruction_deltas_;
467 }
468
Mikhail Khokhlov7db04912019-07-18 16:11:11 +0100469 base::Optional<uint32_t> FindRowForSliceId(uint32_t slice_id) const {
Eric Secklerd3b89d52019-07-10 15:36:29 +0100470 auto it =
471 std::lower_bound(slice_ids().begin(), slice_ids().end(), slice_id);
Mikhail Khokhlov7db04912019-07-18 16:11:11 +0100472 if (it != slice_ids().end() && *it == slice_id) {
473 return static_cast<uint32_t>(std::distance(slice_ids().begin(), it));
474 }
475 return base::nullopt;
Eric Secklerd3b89d52019-07-10 15:36:29 +0100476 }
477
Eric Seckler54f30a32019-07-19 15:10:29 +0100478 void UpdateThreadDeltasForSliceId(uint32_t slice_id,
479 int64_t end_thread_timestamp_ns,
480 int64_t end_thread_instruction_count) {
Mikhail Khokhlov7db04912019-07-18 16:11:11 +0100481 uint32_t row = *FindRowForSliceId(slice_id);
Eric Secklerd3b89d52019-07-10 15:36:29 +0100482 int64_t begin_ns = thread_timestamp_ns_[row];
483 thread_duration_ns_[row] = end_thread_timestamp_ns - begin_ns;
Eric Seckler54f30a32019-07-19 15:10:29 +0100484 int64_t begin_ticount = thread_instruction_counts_[row];
485 thread_instruction_deltas_[row] =
486 end_thread_instruction_count - begin_ticount;
Eric Secklerd3b89d52019-07-10 15:36:29 +0100487 }
488
489 private:
490 std::deque<uint32_t> slice_ids_;
491 std::deque<int64_t> thread_timestamp_ns_;
492 std::deque<int64_t> thread_duration_ns_;
493 std::deque<int64_t> thread_instruction_counts_;
494 std::deque<int64_t> thread_instruction_deltas_;
495 };
496
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000497 class CounterDefinitions {
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100498 public:
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000499 using Id = uint32_t;
Lalit Maganti521d97b2019-04-29 13:47:03 +0100500 static constexpr Id kInvalidId = std::numeric_limits<Id>::max();
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000501
502 inline Id AddCounterDefinition(StringId name_id,
503 int64_t ref,
504 RefType type) {
505 base::Hash hash;
506 hash.Update(name_id);
507 hash.Update(ref);
508 hash.Update(type);
509
510 // TODO(lalitm): this is a perf bottleneck and likely we can do something
511 // quite a bit better here.
512 uint64_t digest = hash.digest();
513 auto it = hash_to_row_idx_.find(digest);
514 if (it != hash_to_row_idx_.end())
515 return it->second;
516
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100517 name_ids_.emplace_back(name_id);
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100518 refs_.emplace_back(ref);
519 types_.emplace_back(type);
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000520 hash_to_row_idx_.emplace(digest, size() - 1);
521 return size() - 1;
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100522 }
Lalit Magantifde29042018-10-04 13:28:52 +0100523
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000524 uint32_t size() const { return static_cast<uint32_t>(name_ids_.size()); }
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100525
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100526 const std::deque<StringId>& name_ids() const { return name_ids_; }
527
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100528 const std::deque<int64_t>& refs() const { return refs_; }
529
530 const std::deque<RefType>& types() const { return types_; }
531
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000532 private:
533 std::deque<StringId> name_ids_;
534 std::deque<int64_t> refs_;
535 std::deque<RefType> types_;
536
537 std::unordered_map<uint64_t, uint32_t> hash_to_row_idx_;
538 };
539
540 class CounterValues {
541 public:
542 inline uint32_t AddCounterValue(CounterDefinitions::Id counter_id,
543 int64_t timestamp,
544 double value) {
545 counter_ids_.emplace_back(counter_id);
546 timestamps_.emplace_back(timestamp);
547 values_.emplace_back(value);
548 arg_set_ids_.emplace_back(kInvalidArgSetId);
Ioannis Ilkosf129c262019-04-25 14:53:51 +0100549
Lalit Maganti521d97b2019-04-29 13:47:03 +0100550 if (counter_id != CounterDefinitions::kInvalidId) {
551 if (counter_id >= rows_for_counter_id_.size()) {
552 rows_for_counter_id_.resize(counter_id + 1);
553 }
554 rows_for_counter_id_[counter_id].emplace_back(size() - 1);
555 }
556 return size() - 1;
557 }
558
559 void set_counter_id(uint32_t index, CounterDefinitions::Id counter_id) {
560 PERFETTO_DCHECK(counter_ids_[index] == CounterDefinitions::kInvalidId);
561
562 counter_ids_[index] = counter_id;
Ioannis Ilkosf129c262019-04-25 14:53:51 +0100563 if (counter_id >= rows_for_counter_id_.size()) {
564 rows_for_counter_id_.resize(counter_id + 1);
565 }
Lalit Maganti521d97b2019-04-29 13:47:03 +0100566
567 auto* new_rows = &rows_for_counter_id_[counter_id];
568 new_rows->insert(
569 std::upper_bound(new_rows->begin(), new_rows->end(), index), index);
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000570 }
571
572 void set_arg_set_id(uint32_t row, ArgSetId id) { arg_set_ids_[row] = id; }
573
574 uint32_t size() const { return static_cast<uint32_t>(counter_ids_.size()); }
575
576 const std::deque<CounterDefinitions::Id>& counter_ids() const {
577 return counter_ids_;
578 }
579
580 const std::deque<int64_t>& timestamps() const { return timestamps_; }
581
582 const std::deque<double>& values() const { return values_; }
583
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000584 const std::deque<ArgSetId>& arg_set_ids() const { return arg_set_ids_; }
585
Ioannis Ilkosf129c262019-04-25 14:53:51 +0100586 const std::deque<std::vector<uint32_t>>& rows_for_counter_id() const {
587 return rows_for_counter_id_;
588 }
589
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100590 private:
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000591 std::deque<CounterDefinitions::Id> counter_ids_;
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000592 std::deque<int64_t> timestamps_;
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100593 std::deque<double> values_;
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000594 std::deque<ArgSetId> arg_set_ids_;
Ioannis Ilkosf129c262019-04-25 14:53:51 +0100595
596 // Indexed by counter_id value and contains the row numbers corresponding to
597 // it.
598 std::deque<std::vector<uint32_t>> rows_for_counter_id_;
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100599 };
600
Primiano Tucci5cb84f82018-10-31 21:46:36 -0700601 class SqlStats {
602 public:
603 static constexpr size_t kMaxLogEntries = 100;
Lalit Magantiaac2f652019-04-30 12:16:21 +0100604 uint32_t RecordQueryBegin(const std::string& query,
605 int64_t time_queued,
606 int64_t time_started);
607 void RecordQueryFirstNext(uint32_t row, int64_t time_first_next);
608 void RecordQueryEnd(uint32_t row, int64_t time_end);
Primiano Tucci5cb84f82018-10-31 21:46:36 -0700609 size_t size() const { return queries_.size(); }
610 const std::deque<std::string>& queries() const { return queries_; }
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000611 const std::deque<int64_t>& times_queued() const { return times_queued_; }
612 const std::deque<int64_t>& times_started() const { return times_started_; }
Lalit Magantiaac2f652019-04-30 12:16:21 +0100613 const std::deque<int64_t>& times_first_next() const {
614 return times_first_next_;
615 }
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000616 const std::deque<int64_t>& times_ended() const { return times_ended_; }
Primiano Tucci5cb84f82018-10-31 21:46:36 -0700617
618 private:
Lalit Magantiaac2f652019-04-30 12:16:21 +0100619 uint32_t popped_queries_ = 0;
620
Primiano Tucci5cb84f82018-10-31 21:46:36 -0700621 std::deque<std::string> queries_;
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000622 std::deque<int64_t> times_queued_;
623 std::deque<int64_t> times_started_;
Lalit Magantiaac2f652019-04-30 12:16:21 +0100624 std::deque<int64_t> times_first_next_;
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000625 std::deque<int64_t> times_ended_;
Primiano Tucci5cb84f82018-10-31 21:46:36 -0700626 };
627
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000628 class Instants {
629 public:
Lalit Maganti66ed7ad2019-01-11 16:47:26 +0000630 inline uint32_t AddInstantEvent(int64_t timestamp,
631 StringId name_id,
632 double value,
633 int64_t ref,
634 RefType type) {
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000635 timestamps_.emplace_back(timestamp);
636 name_ids_.emplace_back(name_id);
637 values_.emplace_back(value);
638 refs_.emplace_back(ref);
639 types_.emplace_back(type);
Lalit Maganti1c21d172019-02-07 10:48:24 +0000640 arg_set_ids_.emplace_back(kInvalidArgSetId);
Lalit Maganti66ed7ad2019-01-11 16:47:26 +0000641 return static_cast<uint32_t>(instant_count() - 1);
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000642 }
643
Lalit Maganti521d97b2019-04-29 13:47:03 +0100644 void set_ref(uint32_t row, int64_t ref) { refs_[row] = ref; }
645
Lalit Maganti1c21d172019-02-07 10:48:24 +0000646 void set_arg_set_id(uint32_t row, ArgSetId id) { arg_set_ids_[row] = id; }
647
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000648 size_t instant_count() const { return timestamps_.size(); }
649
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000650 const std::deque<int64_t>& timestamps() const { return timestamps_; }
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000651
652 const std::deque<StringId>& name_ids() const { return name_ids_; }
653
654 const std::deque<double>& values() const { return values_; }
655
656 const std::deque<int64_t>& refs() const { return refs_; }
657
658 const std::deque<RefType>& types() const { return types_; }
659
Lalit Maganti1c21d172019-02-07 10:48:24 +0000660 const std::deque<ArgSetId>& arg_set_ids() const { return arg_set_ids_; }
661
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000662 private:
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000663 std::deque<int64_t> timestamps_;
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000664 std::deque<StringId> name_ids_;
665 std::deque<double> values_;
666 std::deque<int64_t> refs_;
667 std::deque<RefType> types_;
Lalit Maganti1c21d172019-02-07 10:48:24 +0000668 std::deque<ArgSetId> arg_set_ids_;
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000669 };
670
Lalit Maganti1d915a62019-01-07 12:10:42 +0000671 class RawEvents {
672 public:
673 inline RowId AddRawEvent(int64_t timestamp,
674 StringId name_id,
Lalit Magantie87cc812019-01-10 15:20:06 +0000675 uint32_t cpu,
Lalit Maganti1d915a62019-01-07 12:10:42 +0000676 UniqueTid utid) {
677 timestamps_.emplace_back(timestamp);
678 name_ids_.emplace_back(name_id);
Lalit Magantie87cc812019-01-10 15:20:06 +0000679 cpus_.emplace_back(cpu);
Lalit Maganti1d915a62019-01-07 12:10:42 +0000680 utids_.emplace_back(utid);
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000681 arg_set_ids_.emplace_back(kInvalidArgSetId);
Lalit Maganti1d915a62019-01-07 12:10:42 +0000682 return CreateRowId(TableId::kRawEvents,
683 static_cast<uint32_t>(raw_event_count() - 1));
684 }
685
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000686 void set_arg_set_id(uint32_t row, ArgSetId id) { arg_set_ids_[row] = id; }
687
Lalit Maganti1d915a62019-01-07 12:10:42 +0000688 size_t raw_event_count() const { return timestamps_.size(); }
689
690 const std::deque<int64_t>& timestamps() const { return timestamps_; }
691
692 const std::deque<StringId>& name_ids() const { return name_ids_; }
693
Lalit Magantie87cc812019-01-10 15:20:06 +0000694 const std::deque<uint32_t>& cpus() const { return cpus_; }
695
Lalit Maganti1d915a62019-01-07 12:10:42 +0000696 const std::deque<UniqueTid>& utids() const { return utids_; }
697
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000698 const std::deque<ArgSetId>& arg_set_ids() const { return arg_set_ids_; }
699
Lalit Maganti1d915a62019-01-07 12:10:42 +0000700 private:
701 std::deque<int64_t> timestamps_;
702 std::deque<StringId> name_ids_;
Lalit Magantie87cc812019-01-10 15:20:06 +0000703 std::deque<uint32_t> cpus_;
Lalit Maganti1d915a62019-01-07 12:10:42 +0000704 std::deque<UniqueTid> utids_;
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000705 std::deque<ArgSetId> arg_set_ids_;
Lalit Maganti1d915a62019-01-07 12:10:42 +0000706 };
707
Primiano Tucci2c761ef2019-01-07 20:20:46 +0000708 class AndroidLogs {
709 public:
710 inline size_t AddLogEvent(int64_t timestamp,
711 UniqueTid utid,
712 uint8_t prio,
713 StringId tag_id,
714 StringId msg_id) {
715 timestamps_.emplace_back(timestamp);
716 utids_.emplace_back(utid);
717 prios_.emplace_back(prio);
718 tag_ids_.emplace_back(tag_id);
719 msg_ids_.emplace_back(msg_id);
720 return size() - 1;
721 }
722
723 size_t size() const { return timestamps_.size(); }
724
725 const std::deque<int64_t>& timestamps() const { return timestamps_; }
726 const std::deque<UniqueTid>& utids() const { return utids_; }
727 const std::deque<uint8_t>& prios() const { return prios_; }
728 const std::deque<StringId>& tag_ids() const { return tag_ids_; }
729 const std::deque<StringId>& msg_ids() const { return msg_ids_; }
730
731 private:
732 std::deque<int64_t> timestamps_;
733 std::deque<UniqueTid> utids_;
734 std::deque<uint8_t> prios_;
735 std::deque<StringId> tag_ids_;
736 std::deque<StringId> msg_ids_;
737 };
738
Primiano Tucci0e38a142019-01-07 20:51:09 +0000739 struct Stats {
740 using IndexMap = std::map<int, int64_t>;
741 int64_t value = 0;
742 IndexMap indexed_values;
743 };
744 using StatsMap = std::array<Stats, stats::kNumKeys>;
745
Ryan Savitski51413ad2019-07-09 14:25:21 +0100746 class Metadata {
747 public:
748 const std::deque<metadata::KeyIDs>& keys() const { return keys_; }
749 const std::deque<Variadic>& values() const { return values_; }
750
751 RowId SetScalarMetadata(metadata::KeyIDs key, Variadic value) {
752 PERFETTO_DCHECK(key < metadata::kNumKeys);
753 PERFETTO_DCHECK(metadata::kKeyTypes[key] == metadata::kSingle);
754 PERFETTO_DCHECK(value.type == metadata::kValueTypes[key]);
755
756 // Already set - on release builds, overwrite the previous value.
757 auto it = scalar_indices.find(key);
758 if (it != scalar_indices.end()) {
759 PERFETTO_DFATAL("Setting a scalar metadata entry more than once.");
760 uint32_t index = static_cast<uint32_t>(it->second);
761 values_[index] = value;
762 return TraceStorage::CreateRowId(kMetadataTable, index);
763 }
764 // First time setting this key.
765 keys_.push_back(key);
766 values_.push_back(value);
767 uint32_t index = static_cast<uint32_t>(keys_.size() - 1);
768 scalar_indices[key] = index;
769 return TraceStorage::CreateRowId(kMetadataTable, index);
770 }
771
772 RowId AppendMetadata(metadata::KeyIDs key, Variadic value) {
773 PERFETTO_DCHECK(key < metadata::kNumKeys);
774 PERFETTO_DCHECK(metadata::kKeyTypes[key] == metadata::kMulti);
775 PERFETTO_DCHECK(value.type == metadata::kValueTypes[key]);
776
777 keys_.push_back(key);
778 values_.push_back(value);
779 uint32_t index = static_cast<uint32_t>(keys_.size() - 1);
780 return TraceStorage::CreateRowId(kMetadataTable, index);
781 }
782
Ryan Savitski0476ee92019-07-09 14:29:33 +0100783 void OverwriteMetadata(uint32_t index, Variadic value) {
784 PERFETTO_DCHECK(index < values_.size());
785 values_[index] = value;
786 }
787
Ryan Savitski51413ad2019-07-09 14:25:21 +0100788 private:
789 std::deque<metadata::KeyIDs> keys_;
790 std::deque<Variadic> values_;
791 // Extraneous state to track locations of entries that should have at most
792 // one row. Used only to maintain uniqueness during insertions.
793 std::map<metadata::KeyIDs, uint32_t> scalar_indices;
794 };
Mikhail Khokhlove466c002019-05-23 13:33:33 +0100795
Florian Mayer438b5ab2019-05-02 11:18:06 +0100796 class HeapProfileFrames {
797 public:
Florian Mayerbee52132019-05-02 13:59:56 +0100798 struct Row {
799 StringId name_id;
800 int64_t mapping_row;
801 int64_t rel_pc;
Florian Mayer438b5ab2019-05-02 11:18:06 +0100802
Florian Mayerbee52132019-05-02 13:59:56 +0100803 bool operator==(const Row& other) const {
804 return std::tie(name_id, mapping_row, rel_pc) ==
805 std::tie(other.name_id, other.mapping_row, other.rel_pc);
806 }
807 };
808
Lalit Maganti9b2d52b2019-05-07 14:32:15 +0100809 uint32_t size() const { return static_cast<uint32_t>(names_.size()); }
810
Florian Mayerbee52132019-05-02 13:59:56 +0100811 int64_t Insert(const Row& row) {
812 names_.emplace_back(row.name_id);
813 mappings_.emplace_back(row.mapping_row);
814 rel_pcs_.emplace_back(row.rel_pc);
Florian Mayer438b5ab2019-05-02 11:18:06 +0100815 return static_cast<int64_t>(names_.size()) - 1;
816 }
817
818 const std::deque<StringId>& names() const { return names_; }
819 const std::deque<int64_t>& mappings() const { return mappings_; }
820 const std::deque<int64_t>& rel_pcs() const { return rel_pcs_; }
821
822 private:
823 std::deque<StringId> names_;
824 std::deque<int64_t> mappings_;
825 std::deque<int64_t> rel_pcs_;
826 };
827
828 class HeapProfileCallsites {
829 public:
Florian Mayerbee52132019-05-02 13:59:56 +0100830 struct Row {
831 int64_t depth;
832 int64_t parent_id;
833 int64_t frame_row;
834
835 bool operator==(const Row& other) const {
836 return std::tie(depth, parent_id, frame_row) ==
837 std::tie(other.depth, other.parent_id, other.frame_row);
Florian Mayer438b5ab2019-05-02 11:18:06 +0100838 }
Florian Mayerbee52132019-05-02 13:59:56 +0100839 };
840
Lalit Maganti9b2d52b2019-05-07 14:32:15 +0100841 uint32_t size() const { return static_cast<uint32_t>(frame_ids_.size()); }
842
Florian Mayerbee52132019-05-02 13:59:56 +0100843 int64_t Insert(const Row& row) {
844 frame_depths_.emplace_back(row.depth);
845 parent_callsite_ids_.emplace_back(row.parent_id);
846 frame_ids_.emplace_back(row.frame_row);
Florian Mayer438b5ab2019-05-02 11:18:06 +0100847 return static_cast<int64_t>(frame_depths_.size()) - 1;
848 }
849
850 const std::deque<int64_t>& frame_depths() const { return frame_depths_; }
851 const std::deque<int64_t>& parent_callsite_ids() const {
852 return parent_callsite_ids_;
853 }
854 const std::deque<int64_t>& frame_ids() const { return frame_ids_; }
855
856 private:
857 std::deque<int64_t> frame_depths_;
858 std::deque<int64_t> parent_callsite_ids_;
859 std::deque<int64_t> frame_ids_;
860 };
861
862 class HeapProfileMappings {
863 public:
Florian Mayerbee52132019-05-02 13:59:56 +0100864 struct Row {
865 StringId build_id;
Florian Mayer12655732019-07-02 15:08:26 +0100866 int64_t exact_offset;
867 int64_t start_offset;
Florian Mayerbee52132019-05-02 13:59:56 +0100868 int64_t start;
869 int64_t end;
870 int64_t load_bias;
871 StringId name_id;
Florian Mayer438b5ab2019-05-02 11:18:06 +0100872
Florian Mayerbee52132019-05-02 13:59:56 +0100873 bool operator==(const Row& other) const {
Florian Mayer12655732019-07-02 15:08:26 +0100874 return std::tie(build_id, exact_offset, start_offset, start, end,
875 load_bias, name_id) ==
876 std::tie(other.build_id, other.exact_offset, other.start_offset,
877 other.start, other.end, other.load_bias, other.name_id);
Florian Mayerbee52132019-05-02 13:59:56 +0100878 }
879 };
880
Lalit Maganti9b2d52b2019-05-07 14:32:15 +0100881 uint32_t size() const { return static_cast<uint32_t>(names_.size()); }
882
Florian Mayerbee52132019-05-02 13:59:56 +0100883 int64_t Insert(const Row& row) {
884 build_ids_.emplace_back(row.build_id);
Florian Mayer12655732019-07-02 15:08:26 +0100885 exact_offsets_.emplace_back(row.exact_offset);
886 start_offsets_.emplace_back(row.start_offset);
Florian Mayerbee52132019-05-02 13:59:56 +0100887 starts_.emplace_back(row.start);
888 ends_.emplace_back(row.end);
889 load_biases_.emplace_back(row.load_bias);
890 names_.emplace_back(row.name_id);
Florian Mayer438b5ab2019-05-02 11:18:06 +0100891 return static_cast<int64_t>(build_ids_.size()) - 1;
892 }
893
894 const std::deque<StringId>& build_ids() const { return build_ids_; }
Florian Mayer12655732019-07-02 15:08:26 +0100895 const std::deque<int64_t>& exact_offsets() const { return exact_offsets_; }
896 const std::deque<int64_t>& start_offsets() const { return start_offsets_; }
Florian Mayer438b5ab2019-05-02 11:18:06 +0100897 const std::deque<int64_t>& starts() const { return starts_; }
898 const std::deque<int64_t>& ends() const { return ends_; }
899 const std::deque<int64_t>& load_biases() const { return load_biases_; }
900 const std::deque<StringId>& names() const { return names_; }
901
902 private:
903 std::deque<StringId> build_ids_;
Florian Mayer12655732019-07-02 15:08:26 +0100904 std::deque<int64_t> exact_offsets_;
905 std::deque<int64_t> start_offsets_;
Florian Mayer438b5ab2019-05-02 11:18:06 +0100906 std::deque<int64_t> starts_;
907 std::deque<int64_t> ends_;
908 std::deque<int64_t> load_biases_;
909 std::deque<StringId> names_;
910 };
911
912 class HeapProfileAllocations {
913 public:
Florian Mayerbee52132019-05-02 13:59:56 +0100914 struct Row {
915 int64_t timestamp;
Florian Mayerdf1968c2019-05-13 16:39:35 +0100916 UniquePid upid;
Florian Mayerbee52132019-05-02 13:59:56 +0100917 int64_t callsite_id;
918 int64_t count;
919 int64_t size;
920 };
921
Lalit Maganti9b2d52b2019-05-07 14:32:15 +0100922 uint32_t size() const { return static_cast<uint32_t>(timestamps_.size()); }
923
Florian Mayerbee52132019-05-02 13:59:56 +0100924 void Insert(const Row& row) {
925 timestamps_.emplace_back(row.timestamp);
Florian Mayerdf1968c2019-05-13 16:39:35 +0100926 upids_.emplace_back(row.upid);
Florian Mayerbee52132019-05-02 13:59:56 +0100927 callsite_ids_.emplace_back(row.callsite_id);
928 counts_.emplace_back(row.count);
929 sizes_.emplace_back(row.size);
Florian Mayer438b5ab2019-05-02 11:18:06 +0100930 }
931
932 const std::deque<int64_t>& timestamps() const { return timestamps_; }
Florian Mayerdf1968c2019-05-13 16:39:35 +0100933 const std::deque<UniquePid>& upids() const { return upids_; }
Florian Mayer438b5ab2019-05-02 11:18:06 +0100934 const std::deque<int64_t>& callsite_ids() const { return callsite_ids_; }
935 const std::deque<int64_t>& counts() const { return counts_; }
936 const std::deque<int64_t>& sizes() const { return sizes_; }
937
938 private:
939 std::deque<int64_t> timestamps_;
Florian Mayerdf1968c2019-05-13 16:39:35 +0100940 std::deque<UniquePid> upids_;
Florian Mayer438b5ab2019-05-02 11:18:06 +0100941 std::deque<int64_t> callsite_ids_;
942 std::deque<int64_t> counts_;
943 std::deque<int64_t> sizes_;
944 };
945
Isabelle Taylora0a22972018-08-03 12:06:12 +0100946 void ResetStorage();
Lalit Maganti35622b72018-06-06 12:03:11 +0100947
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100948 UniqueTid AddEmptyThread(uint32_t tid) {
949 unique_threads_.emplace_back(tid);
Isabelle Taylora0a22972018-08-03 12:06:12 +0100950 return static_cast<UniqueTid>(unique_threads_.size() - 1);
951 }
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100952
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100953 UniquePid AddEmptyProcess(uint32_t pid) {
954 unique_processes_.emplace_back(pid);
Isabelle Taylora0a22972018-08-03 12:06:12 +0100955 return static_cast<UniquePid>(unique_processes_.size() - 1);
956 }
Isabelle Taylor3dd366c2018-06-22 16:21:41 +0100957
Isabelle Taylora0a22972018-08-03 12:06:12 +0100958 // Return an unqiue identifier for the contents of each string.
959 // The string is copied internally and can be destroyed after this called.
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100960 // Virtual for testing.
Lalit Maganti5c454312019-04-08 12:11:17 +0100961 virtual StringId InternString(base::StringView str) {
962 return string_pool_.InternString(str);
963 }
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100964
Isabelle Taylora0a22972018-08-03 12:06:12 +0100965 Process* GetMutableProcess(UniquePid upid) {
Lalit Maganti676658b2018-11-20 18:24:31 +0000966 PERFETTO_DCHECK(upid < unique_processes_.size());
Isabelle Taylora0a22972018-08-03 12:06:12 +0100967 return &unique_processes_[upid];
968 }
Isabelle Taylor3dd366c2018-06-22 16:21:41 +0100969
Isabelle Taylora0a22972018-08-03 12:06:12 +0100970 Thread* GetMutableThread(UniqueTid utid) {
Florian Mayera1aaec22018-09-19 17:02:26 +0100971 PERFETTO_DCHECK(utid < unique_threads_.size());
Isabelle Taylora0a22972018-08-03 12:06:12 +0100972 return &unique_threads_[utid];
973 }
Isabelle Taylor3dd366c2018-06-22 16:21:41 +0100974
Primiano Tucci0e38a142019-01-07 20:51:09 +0000975 // Example usage: SetStats(stats::android_log_num_failed, 42);
976 void SetStats(size_t key, int64_t value) {
977 PERFETTO_DCHECK(key < stats::kNumKeys);
978 PERFETTO_DCHECK(stats::kTypes[key] == stats::kSingle);
979 stats_[key].value = value;
980 }
981
982 // Example usage: IncrementStats(stats::android_log_num_failed, -1);
983 void IncrementStats(size_t key, int64_t increment = 1) {
984 PERFETTO_DCHECK(key < stats::kNumKeys);
985 PERFETTO_DCHECK(stats::kTypes[key] == stats::kSingle);
986 stats_[key].value += increment;
987 }
988
Florian Mayer438b5ab2019-05-02 11:18:06 +0100989 // Example usage: IncrementIndexedStats(stats::cpu_failure, 1);
990 void IncrementIndexedStats(size_t key, int index, int64_t increment = 1) {
991 PERFETTO_DCHECK(key < stats::kNumKeys);
992 PERFETTO_DCHECK(stats::kTypes[key] == stats::kIndexed);
993 stats_[key].indexed_values[index] += increment;
994 }
995
Primiano Tucci0e38a142019-01-07 20:51:09 +0000996 // Example usage: SetIndexedStats(stats::cpu_failure, 1, 42);
997 void SetIndexedStats(size_t key, int index, int64_t value) {
998 PERFETTO_DCHECK(key < stats::kNumKeys);
999 PERFETTO_DCHECK(stats::kTypes[key] == stats::kIndexed);
1000 stats_[key].indexed_values[index] = value;
1001 }
1002
Mikhail Khokhlove466c002019-05-23 13:33:33 +01001003 // Example usage:
1004 // SetMetadata(metadata::benchmark_name,
1005 // Variadic::String(storage->InternString("foo"));
Ryan Savitski51413ad2019-07-09 14:25:21 +01001006 // Returns the RowId of the new entry.
Mikhail Khokhlovb1fe42a2019-05-23 13:58:05 +01001007 // Virtual for testing.
Ryan Savitski51413ad2019-07-09 14:25:21 +01001008 virtual RowId SetMetadata(metadata::KeyIDs key, Variadic value) {
1009 return metadata_.SetScalarMetadata(key, value);
Mikhail Khokhlove466c002019-05-23 13:33:33 +01001010 }
1011
1012 // Example usage:
1013 // AppendMetadata(metadata::benchmark_story_tags,
1014 // Variadic::String(storage->InternString("bar"));
Ryan Savitski51413ad2019-07-09 14:25:21 +01001015 // Returns the RowId of the new entry.
Mikhail Khokhlovb1fe42a2019-05-23 13:58:05 +01001016 // Virtual for testing.
Ryan Savitski51413ad2019-07-09 14:25:21 +01001017 virtual RowId AppendMetadata(metadata::KeyIDs key, Variadic value) {
1018 return metadata_.AppendMetadata(key, value);
Mikhail Khokhlove466c002019-05-23 13:33:33 +01001019 }
1020
Eric Seckler77b52782019-05-02 15:18:57 +01001021 class ScopedStatsTracer {
1022 public:
1023 ScopedStatsTracer(TraceStorage* storage, size_t key)
1024 : storage_(storage), key_(key), start_ns_(base::GetWallTimeNs()) {}
1025
1026 ~ScopedStatsTracer() {
1027 if (!storage_)
1028 return;
1029 auto delta_ns = base::GetWallTimeNs() - start_ns_;
1030 storage_->IncrementStats(key_, delta_ns.count());
1031 }
1032
1033 ScopedStatsTracer(ScopedStatsTracer&& other) noexcept { MoveImpl(&other); }
1034
1035 ScopedStatsTracer& operator=(ScopedStatsTracer&& other) {
1036 MoveImpl(&other);
1037 return *this;
1038 }
1039
1040 private:
1041 ScopedStatsTracer(const ScopedStatsTracer&) = delete;
1042 ScopedStatsTracer& operator=(const ScopedStatsTracer&) = delete;
1043
1044 void MoveImpl(ScopedStatsTracer* other) {
1045 storage_ = other->storage_;
1046 key_ = other->key_;
1047 start_ns_ = other->start_ns_;
1048 other->storage_ = nullptr;
1049 }
1050
1051 TraceStorage* storage_;
1052 size_t key_;
1053 base::TimeNanos start_ns_;
1054 };
1055
1056 ScopedStatsTracer TraceExecutionTimeIntoStats(size_t key) {
1057 return ScopedStatsTracer(this, key);
1058 }
1059
Lalit Maganticaed37e2018-06-01 03:03:08 +01001060 // Reading methods.
Eric Secklerc93823e2019-06-03 16:49:19 +01001061 // Virtual for testing.
1062 virtual NullTermStringView GetString(StringId id) const {
Lalit Maganti5c454312019-04-08 12:11:17 +01001063 return string_pool_.Get(id);
Isabelle Taylora0a22972018-08-03 12:06:12 +01001064 }
1065
Lalit Magantie9d40532018-06-29 13:15:06 +01001066 const Process& GetProcess(UniquePid upid) const {
Lalit Maganti676658b2018-11-20 18:24:31 +00001067 PERFETTO_DCHECK(upid < unique_processes_.size());
Isabelle Taylor47328cf2018-06-12 14:33:59 +01001068 return unique_processes_[upid];
1069 }
1070
Lalit Magantie9d40532018-06-29 13:15:06 +01001071 const Thread& GetThread(UniqueTid utid) const {
Lalit Maganti1f64cfa2018-09-18 13:20:02 +01001072 // Allow utid == 0 for idle thread retrieval.
Florian Mayera1aaec22018-09-19 17:02:26 +01001073 PERFETTO_DCHECK(utid < unique_threads_.size());
Isabelle Taylor68e42192018-06-19 16:19:31 +01001074 return unique_threads_[utid];
1075 }
1076
Lalit Maganti5ea9e932018-11-30 14:19:39 +00001077 static RowId CreateRowId(TableId table, uint32_t row) {
Lalit Maganti85ca4a82018-12-07 17:28:02 +00001078 return (static_cast<RowId>(table) << kRowIdTableShift) | row;
Lalit Maganti5ea9e932018-11-30 14:19:39 +00001079 }
1080
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +00001081 static std::pair<int8_t /*table*/, uint32_t /*row*/> ParseRowId(RowId rowid) {
1082 auto id = static_cast<uint64_t>(rowid);
1083 auto table_id = static_cast<uint8_t>(id >> kRowIdTableShift);
1084 auto row = static_cast<uint32_t>(id & ((1ull << kRowIdTableShift) - 1));
1085 return std::make_pair(table_id, row);
1086 }
1087
Eric Seckler5703ede2019-07-10 10:13:02 +01001088 const VirtualTracks& virtual_tracks() const { return virtual_tracks_; }
1089 VirtualTracks* mutable_virtual_tracks() { return &virtual_tracks_; }
1090
Lalit Magantiff69c112018-09-24 12:07:47 +01001091 const Slices& slices() const { return slices_; }
Lalit Magantifde29042018-10-04 13:28:52 +01001092 Slices* mutable_slices() { return &slices_; }
1093
Primiano Tucci0d72a312018-08-07 14:42:45 +01001094 const NestableSlices& nestable_slices() const { return nestable_slices_; }
1095 NestableSlices* mutable_nestable_slices() { return &nestable_slices_; }
1096
Eric Secklerd3b89d52019-07-10 15:36:29 +01001097 const ThreadSlices& thread_slices() const { return thread_slices_; }
1098 ThreadSlices* mutable_thread_slices() { return &thread_slices_; }
1099
Eric Secklerc3430c62019-07-22 10:49:58 +01001100 const VirtualTrackSlices& virtual_track_slices() const {
1101 return virtual_track_slices_;
1102 }
1103 VirtualTrackSlices* mutable_virtual_track_slices() {
1104 return &virtual_track_slices_;
1105 }
1106
Lalit Maganti8320e6d2019-03-14 18:49:33 +00001107 const CounterDefinitions& counter_definitions() const {
1108 return counter_definitions_;
1109 }
1110 CounterDefinitions* mutable_counter_definitions() {
1111 return &counter_definitions_;
1112 }
1113
1114 const CounterValues& counter_values() const { return counter_values_; }
1115 CounterValues* mutable_counter_values() { return &counter_values_; }
Isabelle Taylor14674d42018-09-07 11:33:11 +01001116
Primiano Tucci5cb84f82018-10-31 21:46:36 -07001117 const SqlStats& sql_stats() const { return sql_stats_; }
1118 SqlStats* mutable_sql_stats() { return &sql_stats_; }
1119
Isabelle Taylorc8c11202018-11-05 11:36:22 +00001120 const Instants& instants() const { return instants_; }
1121 Instants* mutable_instants() { return &instants_; }
1122
Primiano Tucci2c761ef2019-01-07 20:20:46 +00001123 const AndroidLogs& android_logs() const { return android_log_; }
1124 AndroidLogs* mutable_android_log() { return &android_log_; }
1125
Primiano Tucci0e38a142019-01-07 20:51:09 +00001126 const StatsMap& stats() const { return stats_; }
Lalit Maganti05e8c132018-11-09 18:16:12 +00001127
Ryan Savitski51413ad2019-07-09 14:25:21 +01001128 const Metadata& metadata() const { return metadata_; }
Ryan Savitski0476ee92019-07-09 14:29:33 +01001129 Metadata* mutable_metadata() { return &metadata_; }
Mikhail Khokhlove466c002019-05-23 13:33:33 +01001130
Lalit Maganti6e9c55e2018-11-29 12:00:39 +00001131 const Args& args() const { return args_; }
1132 Args* mutable_args() { return &args_; }
1133
Lalit Maganti1d915a62019-01-07 12:10:42 +00001134 const RawEvents& raw_events() const { return raw_events_; }
1135 RawEvents* mutable_raw_events() { return &raw_events_; }
1136
Florian Mayer438b5ab2019-05-02 11:18:06 +01001137 const HeapProfileMappings& heap_profile_mappings() const {
1138 return heap_profile_mappings_;
1139 }
1140 HeapProfileMappings* mutable_heap_profile_mappings() {
1141 return &heap_profile_mappings_;
1142 }
1143
1144 const HeapProfileFrames& heap_profile_frames() const {
1145 return heap_profile_frames_;
1146 }
1147 HeapProfileFrames* mutable_heap_profile_frames() {
1148 return &heap_profile_frames_;
1149 }
1150
1151 const HeapProfileCallsites& heap_profile_callsites() const {
1152 return heap_profile_callsites_;
1153 }
1154 HeapProfileCallsites* mutable_heap_profile_callsites() {
1155 return &heap_profile_callsites_;
1156 }
1157
1158 const HeapProfileAllocations& heap_profile_allocations() const {
1159 return heap_profile_allocations_;
1160 }
1161 HeapProfileAllocations* mutable_heap_profile_allocations() {
1162 return &heap_profile_allocations_;
1163 }
1164
Lalit Maganti5c454312019-04-08 12:11:17 +01001165 const StringPool& string_pool() const { return string_pool_; }
Lalit Magantiacda68b2018-10-29 15:23:25 +00001166
Isabelle Taylore7003fb2018-07-17 11:39:01 +01001167 // |unique_processes_| always contains at least 1 element becuase the 0th ID
1168 // is reserved to indicate an invalid process.
Lalit Maganti9c6395b2019-01-17 00:25:09 +00001169 size_t process_count() const { return unique_processes_.size(); }
Primiano Tucci0d72a312018-08-07 14:42:45 +01001170
Isabelle Taylore7003fb2018-07-17 11:39:01 +01001171 // |unique_threads_| always contains at least 1 element becuase the 0th ID
1172 // is reserved to indicate an invalid thread.
Lalit Maganti9c6395b2019-01-17 00:25:09 +00001173 size_t thread_count() const { return unique_threads_.size(); }
Isabelle Taylore7003fb2018-07-17 11:39:01 +01001174
Hector Dearman12323362018-08-09 16:09:28 +01001175 // Number of interned strings in the pool. Includes the empty string w/ ID=0.
1176 size_t string_count() const { return string_pool_.size(); }
1177
Ioannis Ilkosb8b11102019-01-29 17:56:55 +00001178 // Start / end ts (in nanoseconds) across the parsed trace events.
1179 // Returns (0, 0) if the trace is empty.
1180 std::pair<int64_t, int64_t> GetTraceTimestampBoundsNs() const;
1181
Lalit Maganticaed37e2018-06-01 03:03:08 +01001182 private:
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +00001183 static constexpr uint8_t kRowIdTableShift = 32;
Isabelle Taylora0a22972018-08-03 12:06:12 +01001184
Primiano Tucci2da5d2e2018-08-10 14:23:31 +01001185 using StringHash = uint64_t;
Lalit Maganticaed37e2018-06-01 03:03:08 +01001186
Lalit Maganti5c454312019-04-08 12:11:17 +01001187 TraceStorage(const TraceStorage&) = delete;
1188 TraceStorage& operator=(const TraceStorage&) = delete;
1189
1190 TraceStorage(TraceStorage&&) = default;
1191 TraceStorage& operator=(TraceStorage&&) = default;
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +00001192
Lalit Maganti05e8c132018-11-09 18:16:12 +00001193 // Stats about parsing the trace.
Primiano Tucci0e38a142019-01-07 20:51:09 +00001194 StatsMap stats_{};
Lalit Maganti35622b72018-06-06 12:03:11 +01001195
Ryan Savitski51413ad2019-07-09 14:25:21 +01001196 // Extra data extracted from the trace. Includes:
1197 // * metadata from chrome and benchmarking infrastructure
1198 // * descriptions of android packages
1199 Metadata metadata_{};
Mikhail Khokhlove466c002019-05-23 13:33:33 +01001200
Eric Seckler5703ede2019-07-10 10:13:02 +01001201 // Metadata for virtual slice tracks.
1202 VirtualTracks virtual_tracks_;
1203
Lalit Maganticaed37e2018-06-01 03:03:08 +01001204 // One entry for each CPU in the trace.
Lalit Magantiff69c112018-09-24 12:07:47 +01001205 Slices slices_;
Lalit Maganticaed37e2018-06-01 03:03:08 +01001206
Lalit Maganti6e9c55e2018-11-29 12:00:39 +00001207 // Args for all other tables.
1208 Args args_;
1209
Lalit Maganticaed37e2018-06-01 03:03:08 +01001210 // One entry for each unique string in the trace.
Lalit Maganti5c454312019-04-08 12:11:17 +01001211 StringPool string_pool_;
Isabelle Taylor47328cf2018-06-12 14:33:59 +01001212
Isabelle Taylor47328cf2018-06-12 14:33:59 +01001213 // One entry for each UniquePid, with UniquePid as the index.
Ioannis Ilkos9c03cbd2019-03-12 18:30:43 +00001214 // Never hold on to pointers to Process, as vector resize will
1215 // invalidate them.
1216 std::vector<Process> unique_processes_;
Isabelle Taylor68e42192018-06-19 16:19:31 +01001217
Isabelle Taylor68e42192018-06-19 16:19:31 +01001218 // One entry for each UniqueTid, with UniqueTid as the index.
Isabelle Taylor3dd366c2018-06-22 16:21:41 +01001219 std::deque<Thread> unique_threads_;
Primiano Tucci0d72a312018-08-07 14:42:45 +01001220
1221 // Slices coming from userspace events (e.g. Chromium TRACE_EVENT macros).
1222 NestableSlices nestable_slices_;
Isabelle Taylor15314ea2018-09-19 11:35:19 +01001223
Eric Secklerd3b89d52019-07-10 15:36:29 +01001224 // Additional attributes for threads slices (sub-type of NestableSlices).
1225 ThreadSlices thread_slices_;
1226
Eric Secklerc3430c62019-07-22 10:49:58 +01001227 // Additional attributes for virtual track slices (sub-type of
1228 // NestableSlices).
1229 VirtualTrackSlices virtual_track_slices_;
1230
Hector Dearman2442d612019-06-04 18:05:23 +01001231 // The type of counters in the trace. Can be thought of as the "metadata".
Lalit Maganti8320e6d2019-03-14 18:49:33 +00001232 CounterDefinitions counter_definitions_;
1233
1234 // The values from the Counter events from the trace. This includes CPU
1235 // frequency events as well systrace trace_marker counter events.
1236 CounterValues counter_values_;
Primiano Tucci5cb84f82018-10-31 21:46:36 -07001237
1238 SqlStats sql_stats_;
Lalit Maganti6e9c55e2018-11-29 12:00:39 +00001239
Isabelle Taylorc8c11202018-11-05 11:36:22 +00001240 // These are instantaneous events in the trace. They have no duration
1241 // and do not have a value that make sense to track over time.
1242 // e.g. signal events
1243 Instants instants_;
Lalit Maganti1d915a62019-01-07 12:10:42 +00001244
1245 // Raw events are every ftrace event in the trace. The raw event includes
1246 // the timestamp and the pid. The args for the raw event will be in the
1247 // args table. This table can be used to generate a text version of the
1248 // trace.
1249 RawEvents raw_events_;
Primiano Tucci2c761ef2019-01-07 20:20:46 +00001250 AndroidLogs android_log_;
Florian Mayer438b5ab2019-05-02 11:18:06 +01001251
1252 HeapProfileMappings heap_profile_mappings_;
1253 HeapProfileFrames heap_profile_frames_;
1254 HeapProfileCallsites heap_profile_callsites_;
1255 HeapProfileAllocations heap_profile_allocations_;
Lalit Maganticaed37e2018-06-01 03:03:08 +01001256};
1257
1258} // namespace trace_processor
1259} // namespace perfetto
1260
Florian Mayerbee52132019-05-02 13:59:56 +01001261namespace std {
1262
1263template <>
1264struct hash<::perfetto::trace_processor::TraceStorage::HeapProfileFrames::Row> {
1265 using argument_type =
1266 ::perfetto::trace_processor::TraceStorage::HeapProfileFrames::Row;
1267 using result_type = size_t;
1268
1269 result_type operator()(const argument_type& r) const {
1270 return std::hash<::perfetto::trace_processor::StringId>{}(r.name_id) ^
1271 std::hash<int64_t>{}(r.mapping_row) ^ std::hash<int64_t>{}(r.rel_pc);
1272 }
1273};
1274
1275template <>
1276struct hash<
1277 ::perfetto::trace_processor::TraceStorage::HeapProfileCallsites::Row> {
1278 using argument_type =
1279 ::perfetto::trace_processor::TraceStorage::HeapProfileCallsites::Row;
1280 using result_type = size_t;
1281
1282 result_type operator()(const argument_type& r) const {
1283 return std::hash<int64_t>{}(r.depth) ^ std::hash<int64_t>{}(r.parent_id) ^
1284 std::hash<int64_t>{}(r.frame_row);
1285 }
1286};
1287
1288template <>
1289struct hash<
1290 ::perfetto::trace_processor::TraceStorage::HeapProfileMappings::Row> {
1291 using argument_type =
1292 ::perfetto::trace_processor::TraceStorage::HeapProfileMappings::Row;
1293 using result_type = size_t;
1294
1295 result_type operator()(const argument_type& r) const {
1296 return std::hash<::perfetto::trace_processor::StringId>{}(r.build_id) ^
Florian Mayer12655732019-07-02 15:08:26 +01001297 std::hash<int64_t>{}(r.exact_offset) ^
1298 std::hash<int64_t>{}(r.start_offset) ^
1299 std::hash<int64_t>{}(r.start) ^ std::hash<int64_t>{}(r.end) ^
1300 std::hash<int64_t>{}(r.load_bias) ^
Florian Mayerbee52132019-05-02 13:59:56 +01001301 std::hash<::perfetto::trace_processor::StringId>{}(r.name_id);
1302 }
1303};
1304
1305} // namespace std
1306
Lalit Maganticaed37e2018-06-01 03:03:08 +01001307#endif // SRC_TRACE_PROCESSOR_TRACE_STORAGE_H_