blob: 9241e2ebbd42b82b473cb315744d5de8342f45a8 [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 Maganti08884242019-02-19 12:28:32 +0000114 base::Optional<UniquePid> pupid;
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,
315 StringId cat,
316 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);
Primiano Tucci0d72a312018-08-07 14:42:45 +0100324 cats_.emplace_back(cat);
325 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_; }
Primiano Tucci0d72a312018-08-07 14:42:45 +0100353 const std::deque<StringId>& cats() const { return cats_; }
354 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_;
Eric Seckler651ca312019-06-05 16:41:51 +0100367 // TODO(eseckler): Remove this column and store the category in the args
368 // table instead, similar to what we do for instant events.
Primiano Tucci0d72a312018-08-07 14:42:45 +0100369 std::deque<StringId> cats_;
370 std::deque<StringId> names_;
371 std::deque<uint8_t> depths_;
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000372 std::deque<int64_t> stack_ids_;
373 std::deque<int64_t> parent_stack_ids_;
Eric Seckler70cc4422019-05-28 16:00:23 +0100374 std::deque<ArgSetId> arg_set_ids_;
Lalit Maganti35622b72018-06-06 12:03:11 +0100375 };
376
Eric Secklerd3b89d52019-07-10 15:36:29 +0100377 class ThreadSlices {
378 public:
379 inline uint32_t AddThreadSlice(uint32_t slice_id,
380 int64_t thread_timestamp_ns,
381 int64_t thread_duration_ns,
382 int64_t thread_instruction_count,
383 int64_t thread_instruction_delta) {
384 slice_ids_.emplace_back(slice_id);
385 thread_timestamp_ns_.emplace_back(thread_timestamp_ns);
386 thread_duration_ns_.emplace_back(thread_duration_ns);
387 thread_instruction_counts_.emplace_back(thread_instruction_count);
388 thread_instruction_deltas_.emplace_back(thread_instruction_delta);
389 return slice_count() - 1;
390 }
391
392 void set_thread_duration_ns(uint32_t index, int64_t thread_duration_ns) {
393 thread_duration_ns_[index] = thread_duration_ns;
394 }
395
396 void set_thread_instruction_delta(uint32_t index,
397 int64_t thread_instruction_delta) {
398 thread_instruction_deltas_[index] = thread_instruction_delta;
399 }
400
401 uint32_t slice_count() const {
402 return static_cast<uint32_t>(slice_ids_.size());
403 }
404
405 const std::deque<uint32_t>& slice_ids() const { return slice_ids_; }
406 const std::deque<int64_t>& thread_timestamp_ns() const {
407 return thread_timestamp_ns_;
408 }
409 const std::deque<int64_t>& thread_duration_ns() const {
410 return thread_duration_ns_;
411 }
412 const std::deque<int64_t>& thread_instruction_counts() const {
413 return thread_instruction_counts_;
414 }
415 const std::deque<int64_t>& thread_instruction_deltas() const {
416 return thread_instruction_deltas_;
417 }
418
419 uint32_t FindRowForSliceId(uint32_t slice_id) const {
420 auto it =
421 std::lower_bound(slice_ids().begin(), slice_ids().end(), slice_id);
422 PERFETTO_DCHECK(it != slice_ids().end() && *it == slice_id);
423 return static_cast<uint32_t>(std::distance(slice_ids().begin(), it));
424 }
425
426 void UpdateThreadDurationForSliceId(uint32_t slice_id,
427 int64_t end_thread_timestamp_ns) {
428 uint32_t row = FindRowForSliceId(slice_id);
429 int64_t begin_ns = thread_timestamp_ns_[row];
430 thread_duration_ns_[row] = end_thread_timestamp_ns - begin_ns;
431 }
432
433 private:
434 std::deque<uint32_t> slice_ids_;
435 std::deque<int64_t> thread_timestamp_ns_;
436 std::deque<int64_t> thread_duration_ns_;
437 std::deque<int64_t> thread_instruction_counts_;
438 std::deque<int64_t> thread_instruction_deltas_;
439 };
440
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000441 class CounterDefinitions {
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100442 public:
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000443 using Id = uint32_t;
Lalit Maganti521d97b2019-04-29 13:47:03 +0100444 static constexpr Id kInvalidId = std::numeric_limits<Id>::max();
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000445
446 inline Id AddCounterDefinition(StringId name_id,
447 int64_t ref,
448 RefType type) {
449 base::Hash hash;
450 hash.Update(name_id);
451 hash.Update(ref);
452 hash.Update(type);
453
454 // TODO(lalitm): this is a perf bottleneck and likely we can do something
455 // quite a bit better here.
456 uint64_t digest = hash.digest();
457 auto it = hash_to_row_idx_.find(digest);
458 if (it != hash_to_row_idx_.end())
459 return it->second;
460
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100461 name_ids_.emplace_back(name_id);
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100462 refs_.emplace_back(ref);
463 types_.emplace_back(type);
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000464 hash_to_row_idx_.emplace(digest, size() - 1);
465 return size() - 1;
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100466 }
Lalit Magantifde29042018-10-04 13:28:52 +0100467
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000468 uint32_t size() const { return static_cast<uint32_t>(name_ids_.size()); }
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100469
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100470 const std::deque<StringId>& name_ids() const { return name_ids_; }
471
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100472 const std::deque<int64_t>& refs() const { return refs_; }
473
474 const std::deque<RefType>& types() const { return types_; }
475
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000476 private:
477 std::deque<StringId> name_ids_;
478 std::deque<int64_t> refs_;
479 std::deque<RefType> types_;
480
481 std::unordered_map<uint64_t, uint32_t> hash_to_row_idx_;
482 };
483
484 class CounterValues {
485 public:
486 inline uint32_t AddCounterValue(CounterDefinitions::Id counter_id,
487 int64_t timestamp,
488 double value) {
489 counter_ids_.emplace_back(counter_id);
490 timestamps_.emplace_back(timestamp);
491 values_.emplace_back(value);
492 arg_set_ids_.emplace_back(kInvalidArgSetId);
Ioannis Ilkosf129c262019-04-25 14:53:51 +0100493
Lalit Maganti521d97b2019-04-29 13:47:03 +0100494 if (counter_id != CounterDefinitions::kInvalidId) {
495 if (counter_id >= rows_for_counter_id_.size()) {
496 rows_for_counter_id_.resize(counter_id + 1);
497 }
498 rows_for_counter_id_[counter_id].emplace_back(size() - 1);
499 }
500 return size() - 1;
501 }
502
503 void set_counter_id(uint32_t index, CounterDefinitions::Id counter_id) {
504 PERFETTO_DCHECK(counter_ids_[index] == CounterDefinitions::kInvalidId);
505
506 counter_ids_[index] = counter_id;
Ioannis Ilkosf129c262019-04-25 14:53:51 +0100507 if (counter_id >= rows_for_counter_id_.size()) {
508 rows_for_counter_id_.resize(counter_id + 1);
509 }
Lalit Maganti521d97b2019-04-29 13:47:03 +0100510
511 auto* new_rows = &rows_for_counter_id_[counter_id];
512 new_rows->insert(
513 std::upper_bound(new_rows->begin(), new_rows->end(), index), index);
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000514 }
515
516 void set_arg_set_id(uint32_t row, ArgSetId id) { arg_set_ids_[row] = id; }
517
518 uint32_t size() const { return static_cast<uint32_t>(counter_ids_.size()); }
519
520 const std::deque<CounterDefinitions::Id>& counter_ids() const {
521 return counter_ids_;
522 }
523
524 const std::deque<int64_t>& timestamps() const { return timestamps_; }
525
526 const std::deque<double>& values() const { return values_; }
527
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000528 const std::deque<ArgSetId>& arg_set_ids() const { return arg_set_ids_; }
529
Ioannis Ilkosf129c262019-04-25 14:53:51 +0100530 const std::deque<std::vector<uint32_t>>& rows_for_counter_id() const {
531 return rows_for_counter_id_;
532 }
533
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100534 private:
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000535 std::deque<CounterDefinitions::Id> counter_ids_;
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000536 std::deque<int64_t> timestamps_;
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100537 std::deque<double> values_;
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000538 std::deque<ArgSetId> arg_set_ids_;
Ioannis Ilkosf129c262019-04-25 14:53:51 +0100539
540 // Indexed by counter_id value and contains the row numbers corresponding to
541 // it.
542 std::deque<std::vector<uint32_t>> rows_for_counter_id_;
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100543 };
544
Primiano Tucci5cb84f82018-10-31 21:46:36 -0700545 class SqlStats {
546 public:
547 static constexpr size_t kMaxLogEntries = 100;
Lalit Magantiaac2f652019-04-30 12:16:21 +0100548 uint32_t RecordQueryBegin(const std::string& query,
549 int64_t time_queued,
550 int64_t time_started);
551 void RecordQueryFirstNext(uint32_t row, int64_t time_first_next);
552 void RecordQueryEnd(uint32_t row, int64_t time_end);
Primiano Tucci5cb84f82018-10-31 21:46:36 -0700553 size_t size() const { return queries_.size(); }
554 const std::deque<std::string>& queries() const { return queries_; }
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000555 const std::deque<int64_t>& times_queued() const { return times_queued_; }
556 const std::deque<int64_t>& times_started() const { return times_started_; }
Lalit Magantiaac2f652019-04-30 12:16:21 +0100557 const std::deque<int64_t>& times_first_next() const {
558 return times_first_next_;
559 }
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000560 const std::deque<int64_t>& times_ended() const { return times_ended_; }
Primiano Tucci5cb84f82018-10-31 21:46:36 -0700561
562 private:
Lalit Magantiaac2f652019-04-30 12:16:21 +0100563 uint32_t popped_queries_ = 0;
564
Primiano Tucci5cb84f82018-10-31 21:46:36 -0700565 std::deque<std::string> queries_;
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000566 std::deque<int64_t> times_queued_;
567 std::deque<int64_t> times_started_;
Lalit Magantiaac2f652019-04-30 12:16:21 +0100568 std::deque<int64_t> times_first_next_;
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000569 std::deque<int64_t> times_ended_;
Primiano Tucci5cb84f82018-10-31 21:46:36 -0700570 };
571
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000572 class Instants {
573 public:
Lalit Maganti66ed7ad2019-01-11 16:47:26 +0000574 inline uint32_t AddInstantEvent(int64_t timestamp,
575 StringId name_id,
576 double value,
577 int64_t ref,
578 RefType type) {
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000579 timestamps_.emplace_back(timestamp);
580 name_ids_.emplace_back(name_id);
581 values_.emplace_back(value);
582 refs_.emplace_back(ref);
583 types_.emplace_back(type);
Lalit Maganti1c21d172019-02-07 10:48:24 +0000584 arg_set_ids_.emplace_back(kInvalidArgSetId);
Lalit Maganti66ed7ad2019-01-11 16:47:26 +0000585 return static_cast<uint32_t>(instant_count() - 1);
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000586 }
587
Lalit Maganti521d97b2019-04-29 13:47:03 +0100588 void set_ref(uint32_t row, int64_t ref) { refs_[row] = ref; }
589
Lalit Maganti1c21d172019-02-07 10:48:24 +0000590 void set_arg_set_id(uint32_t row, ArgSetId id) { arg_set_ids_[row] = id; }
591
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000592 size_t instant_count() const { return timestamps_.size(); }
593
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000594 const std::deque<int64_t>& timestamps() const { return timestamps_; }
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000595
596 const std::deque<StringId>& name_ids() const { return name_ids_; }
597
598 const std::deque<double>& values() const { return values_; }
599
600 const std::deque<int64_t>& refs() const { return refs_; }
601
602 const std::deque<RefType>& types() const { return types_; }
603
Lalit Maganti1c21d172019-02-07 10:48:24 +0000604 const std::deque<ArgSetId>& arg_set_ids() const { return arg_set_ids_; }
605
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000606 private:
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000607 std::deque<int64_t> timestamps_;
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000608 std::deque<StringId> name_ids_;
609 std::deque<double> values_;
610 std::deque<int64_t> refs_;
611 std::deque<RefType> types_;
Lalit Maganti1c21d172019-02-07 10:48:24 +0000612 std::deque<ArgSetId> arg_set_ids_;
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000613 };
614
Lalit Maganti1d915a62019-01-07 12:10:42 +0000615 class RawEvents {
616 public:
617 inline RowId AddRawEvent(int64_t timestamp,
618 StringId name_id,
Lalit Magantie87cc812019-01-10 15:20:06 +0000619 uint32_t cpu,
Lalit Maganti1d915a62019-01-07 12:10:42 +0000620 UniqueTid utid) {
621 timestamps_.emplace_back(timestamp);
622 name_ids_.emplace_back(name_id);
Lalit Magantie87cc812019-01-10 15:20:06 +0000623 cpus_.emplace_back(cpu);
Lalit Maganti1d915a62019-01-07 12:10:42 +0000624 utids_.emplace_back(utid);
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000625 arg_set_ids_.emplace_back(kInvalidArgSetId);
Lalit Maganti1d915a62019-01-07 12:10:42 +0000626 return CreateRowId(TableId::kRawEvents,
627 static_cast<uint32_t>(raw_event_count() - 1));
628 }
629
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000630 void set_arg_set_id(uint32_t row, ArgSetId id) { arg_set_ids_[row] = id; }
631
Lalit Maganti1d915a62019-01-07 12:10:42 +0000632 size_t raw_event_count() const { return timestamps_.size(); }
633
634 const std::deque<int64_t>& timestamps() const { return timestamps_; }
635
636 const std::deque<StringId>& name_ids() const { return name_ids_; }
637
Lalit Magantie87cc812019-01-10 15:20:06 +0000638 const std::deque<uint32_t>& cpus() const { return cpus_; }
639
Lalit Maganti1d915a62019-01-07 12:10:42 +0000640 const std::deque<UniqueTid>& utids() const { return utids_; }
641
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000642 const std::deque<ArgSetId>& arg_set_ids() const { return arg_set_ids_; }
643
Lalit Maganti1d915a62019-01-07 12:10:42 +0000644 private:
645 std::deque<int64_t> timestamps_;
646 std::deque<StringId> name_ids_;
Lalit Magantie87cc812019-01-10 15:20:06 +0000647 std::deque<uint32_t> cpus_;
Lalit Maganti1d915a62019-01-07 12:10:42 +0000648 std::deque<UniqueTid> utids_;
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000649 std::deque<ArgSetId> arg_set_ids_;
Lalit Maganti1d915a62019-01-07 12:10:42 +0000650 };
651
Primiano Tucci2c761ef2019-01-07 20:20:46 +0000652 class AndroidLogs {
653 public:
654 inline size_t AddLogEvent(int64_t timestamp,
655 UniqueTid utid,
656 uint8_t prio,
657 StringId tag_id,
658 StringId msg_id) {
659 timestamps_.emplace_back(timestamp);
660 utids_.emplace_back(utid);
661 prios_.emplace_back(prio);
662 tag_ids_.emplace_back(tag_id);
663 msg_ids_.emplace_back(msg_id);
664 return size() - 1;
665 }
666
667 size_t size() const { return timestamps_.size(); }
668
669 const std::deque<int64_t>& timestamps() const { return timestamps_; }
670 const std::deque<UniqueTid>& utids() const { return utids_; }
671 const std::deque<uint8_t>& prios() const { return prios_; }
672 const std::deque<StringId>& tag_ids() const { return tag_ids_; }
673 const std::deque<StringId>& msg_ids() const { return msg_ids_; }
674
675 private:
676 std::deque<int64_t> timestamps_;
677 std::deque<UniqueTid> utids_;
678 std::deque<uint8_t> prios_;
679 std::deque<StringId> tag_ids_;
680 std::deque<StringId> msg_ids_;
681 };
682
Primiano Tucci0e38a142019-01-07 20:51:09 +0000683 struct Stats {
684 using IndexMap = std::map<int, int64_t>;
685 int64_t value = 0;
686 IndexMap indexed_values;
687 };
688 using StatsMap = std::array<Stats, stats::kNumKeys>;
689
Ryan Savitski51413ad2019-07-09 14:25:21 +0100690 class Metadata {
691 public:
692 const std::deque<metadata::KeyIDs>& keys() const { return keys_; }
693 const std::deque<Variadic>& values() const { return values_; }
694
695 RowId SetScalarMetadata(metadata::KeyIDs key, Variadic value) {
696 PERFETTO_DCHECK(key < metadata::kNumKeys);
697 PERFETTO_DCHECK(metadata::kKeyTypes[key] == metadata::kSingle);
698 PERFETTO_DCHECK(value.type == metadata::kValueTypes[key]);
699
700 // Already set - on release builds, overwrite the previous value.
701 auto it = scalar_indices.find(key);
702 if (it != scalar_indices.end()) {
703 PERFETTO_DFATAL("Setting a scalar metadata entry more than once.");
704 uint32_t index = static_cast<uint32_t>(it->second);
705 values_[index] = value;
706 return TraceStorage::CreateRowId(kMetadataTable, index);
707 }
708 // First time setting this key.
709 keys_.push_back(key);
710 values_.push_back(value);
711 uint32_t index = static_cast<uint32_t>(keys_.size() - 1);
712 scalar_indices[key] = index;
713 return TraceStorage::CreateRowId(kMetadataTable, index);
714 }
715
716 RowId AppendMetadata(metadata::KeyIDs key, Variadic value) {
717 PERFETTO_DCHECK(key < metadata::kNumKeys);
718 PERFETTO_DCHECK(metadata::kKeyTypes[key] == metadata::kMulti);
719 PERFETTO_DCHECK(value.type == metadata::kValueTypes[key]);
720
721 keys_.push_back(key);
722 values_.push_back(value);
723 uint32_t index = static_cast<uint32_t>(keys_.size() - 1);
724 return TraceStorage::CreateRowId(kMetadataTable, index);
725 }
726
Ryan Savitski0476ee92019-07-09 14:29:33 +0100727 void OverwriteMetadata(uint32_t index, Variadic value) {
728 PERFETTO_DCHECK(index < values_.size());
729 values_[index] = value;
730 }
731
Ryan Savitski51413ad2019-07-09 14:25:21 +0100732 private:
733 std::deque<metadata::KeyIDs> keys_;
734 std::deque<Variadic> values_;
735 // Extraneous state to track locations of entries that should have at most
736 // one row. Used only to maintain uniqueness during insertions.
737 std::map<metadata::KeyIDs, uint32_t> scalar_indices;
738 };
Mikhail Khokhlove466c002019-05-23 13:33:33 +0100739
Florian Mayer438b5ab2019-05-02 11:18:06 +0100740 class HeapProfileFrames {
741 public:
Florian Mayerbee52132019-05-02 13:59:56 +0100742 struct Row {
743 StringId name_id;
744 int64_t mapping_row;
745 int64_t rel_pc;
Florian Mayer438b5ab2019-05-02 11:18:06 +0100746
Florian Mayerbee52132019-05-02 13:59:56 +0100747 bool operator==(const Row& other) const {
748 return std::tie(name_id, mapping_row, rel_pc) ==
749 std::tie(other.name_id, other.mapping_row, other.rel_pc);
750 }
751 };
752
Lalit Maganti9b2d52b2019-05-07 14:32:15 +0100753 uint32_t size() const { return static_cast<uint32_t>(names_.size()); }
754
Florian Mayerbee52132019-05-02 13:59:56 +0100755 int64_t Insert(const Row& row) {
756 names_.emplace_back(row.name_id);
757 mappings_.emplace_back(row.mapping_row);
758 rel_pcs_.emplace_back(row.rel_pc);
Florian Mayer438b5ab2019-05-02 11:18:06 +0100759 return static_cast<int64_t>(names_.size()) - 1;
760 }
761
762 const std::deque<StringId>& names() const { return names_; }
763 const std::deque<int64_t>& mappings() const { return mappings_; }
764 const std::deque<int64_t>& rel_pcs() const { return rel_pcs_; }
765
766 private:
767 std::deque<StringId> names_;
768 std::deque<int64_t> mappings_;
769 std::deque<int64_t> rel_pcs_;
770 };
771
772 class HeapProfileCallsites {
773 public:
Florian Mayerbee52132019-05-02 13:59:56 +0100774 struct Row {
775 int64_t depth;
776 int64_t parent_id;
777 int64_t frame_row;
778
779 bool operator==(const Row& other) const {
780 return std::tie(depth, parent_id, frame_row) ==
781 std::tie(other.depth, other.parent_id, other.frame_row);
Florian Mayer438b5ab2019-05-02 11:18:06 +0100782 }
Florian Mayerbee52132019-05-02 13:59:56 +0100783 };
784
Lalit Maganti9b2d52b2019-05-07 14:32:15 +0100785 uint32_t size() const { return static_cast<uint32_t>(frame_ids_.size()); }
786
Florian Mayerbee52132019-05-02 13:59:56 +0100787 int64_t Insert(const Row& row) {
788 frame_depths_.emplace_back(row.depth);
789 parent_callsite_ids_.emplace_back(row.parent_id);
790 frame_ids_.emplace_back(row.frame_row);
Florian Mayer438b5ab2019-05-02 11:18:06 +0100791 return static_cast<int64_t>(frame_depths_.size()) - 1;
792 }
793
794 const std::deque<int64_t>& frame_depths() const { return frame_depths_; }
795 const std::deque<int64_t>& parent_callsite_ids() const {
796 return parent_callsite_ids_;
797 }
798 const std::deque<int64_t>& frame_ids() const { return frame_ids_; }
799
800 private:
801 std::deque<int64_t> frame_depths_;
802 std::deque<int64_t> parent_callsite_ids_;
803 std::deque<int64_t> frame_ids_;
804 };
805
806 class HeapProfileMappings {
807 public:
Florian Mayerbee52132019-05-02 13:59:56 +0100808 struct Row {
809 StringId build_id;
Florian Mayer12655732019-07-02 15:08:26 +0100810 int64_t exact_offset;
811 int64_t start_offset;
Florian Mayerbee52132019-05-02 13:59:56 +0100812 int64_t start;
813 int64_t end;
814 int64_t load_bias;
815 StringId name_id;
Florian Mayer438b5ab2019-05-02 11:18:06 +0100816
Florian Mayerbee52132019-05-02 13:59:56 +0100817 bool operator==(const Row& other) const {
Florian Mayer12655732019-07-02 15:08:26 +0100818 return std::tie(build_id, exact_offset, start_offset, start, end,
819 load_bias, name_id) ==
820 std::tie(other.build_id, other.exact_offset, other.start_offset,
821 other.start, other.end, other.load_bias, other.name_id);
Florian Mayerbee52132019-05-02 13:59:56 +0100822 }
823 };
824
Lalit Maganti9b2d52b2019-05-07 14:32:15 +0100825 uint32_t size() const { return static_cast<uint32_t>(names_.size()); }
826
Florian Mayerbee52132019-05-02 13:59:56 +0100827 int64_t Insert(const Row& row) {
828 build_ids_.emplace_back(row.build_id);
Florian Mayer12655732019-07-02 15:08:26 +0100829 exact_offsets_.emplace_back(row.exact_offset);
830 start_offsets_.emplace_back(row.start_offset);
Florian Mayerbee52132019-05-02 13:59:56 +0100831 starts_.emplace_back(row.start);
832 ends_.emplace_back(row.end);
833 load_biases_.emplace_back(row.load_bias);
834 names_.emplace_back(row.name_id);
Florian Mayer438b5ab2019-05-02 11:18:06 +0100835 return static_cast<int64_t>(build_ids_.size()) - 1;
836 }
837
838 const std::deque<StringId>& build_ids() const { return build_ids_; }
Florian Mayer12655732019-07-02 15:08:26 +0100839 const std::deque<int64_t>& exact_offsets() const { return exact_offsets_; }
840 const std::deque<int64_t>& start_offsets() const { return start_offsets_; }
Florian Mayer438b5ab2019-05-02 11:18:06 +0100841 const std::deque<int64_t>& starts() const { return starts_; }
842 const std::deque<int64_t>& ends() const { return ends_; }
843 const std::deque<int64_t>& load_biases() const { return load_biases_; }
844 const std::deque<StringId>& names() const { return names_; }
845
846 private:
847 std::deque<StringId> build_ids_;
Florian Mayer12655732019-07-02 15:08:26 +0100848 std::deque<int64_t> exact_offsets_;
849 std::deque<int64_t> start_offsets_;
Florian Mayer438b5ab2019-05-02 11:18:06 +0100850 std::deque<int64_t> starts_;
851 std::deque<int64_t> ends_;
852 std::deque<int64_t> load_biases_;
853 std::deque<StringId> names_;
854 };
855
856 class HeapProfileAllocations {
857 public:
Florian Mayerbee52132019-05-02 13:59:56 +0100858 struct Row {
859 int64_t timestamp;
Florian Mayerdf1968c2019-05-13 16:39:35 +0100860 UniquePid upid;
Florian Mayerbee52132019-05-02 13:59:56 +0100861 int64_t callsite_id;
862 int64_t count;
863 int64_t size;
864 };
865
Lalit Maganti9b2d52b2019-05-07 14:32:15 +0100866 uint32_t size() const { return static_cast<uint32_t>(timestamps_.size()); }
867
Florian Mayerbee52132019-05-02 13:59:56 +0100868 void Insert(const Row& row) {
869 timestamps_.emplace_back(row.timestamp);
Florian Mayerdf1968c2019-05-13 16:39:35 +0100870 upids_.emplace_back(row.upid);
Florian Mayerbee52132019-05-02 13:59:56 +0100871 callsite_ids_.emplace_back(row.callsite_id);
872 counts_.emplace_back(row.count);
873 sizes_.emplace_back(row.size);
Florian Mayer438b5ab2019-05-02 11:18:06 +0100874 }
875
876 const std::deque<int64_t>& timestamps() const { return timestamps_; }
Florian Mayerdf1968c2019-05-13 16:39:35 +0100877 const std::deque<UniquePid>& upids() const { return upids_; }
Florian Mayer438b5ab2019-05-02 11:18:06 +0100878 const std::deque<int64_t>& callsite_ids() const { return callsite_ids_; }
879 const std::deque<int64_t>& counts() const { return counts_; }
880 const std::deque<int64_t>& sizes() const { return sizes_; }
881
882 private:
883 std::deque<int64_t> timestamps_;
Florian Mayerdf1968c2019-05-13 16:39:35 +0100884 std::deque<UniquePid> upids_;
Florian Mayer438b5ab2019-05-02 11:18:06 +0100885 std::deque<int64_t> callsite_ids_;
886 std::deque<int64_t> counts_;
887 std::deque<int64_t> sizes_;
888 };
889
Isabelle Taylora0a22972018-08-03 12:06:12 +0100890 void ResetStorage();
Lalit Maganti35622b72018-06-06 12:03:11 +0100891
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100892 UniqueTid AddEmptyThread(uint32_t tid) {
893 unique_threads_.emplace_back(tid);
Isabelle Taylora0a22972018-08-03 12:06:12 +0100894 return static_cast<UniqueTid>(unique_threads_.size() - 1);
895 }
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100896
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100897 UniquePid AddEmptyProcess(uint32_t pid) {
898 unique_processes_.emplace_back(pid);
Isabelle Taylora0a22972018-08-03 12:06:12 +0100899 return static_cast<UniquePid>(unique_processes_.size() - 1);
900 }
Isabelle Taylor3dd366c2018-06-22 16:21:41 +0100901
Isabelle Taylora0a22972018-08-03 12:06:12 +0100902 // Return an unqiue identifier for the contents of each string.
903 // The string is copied internally and can be destroyed after this called.
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100904 // Virtual for testing.
Lalit Maganti5c454312019-04-08 12:11:17 +0100905 virtual StringId InternString(base::StringView str) {
906 return string_pool_.InternString(str);
907 }
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100908
Isabelle Taylora0a22972018-08-03 12:06:12 +0100909 Process* GetMutableProcess(UniquePid upid) {
Lalit Maganti676658b2018-11-20 18:24:31 +0000910 PERFETTO_DCHECK(upid < unique_processes_.size());
Isabelle Taylora0a22972018-08-03 12:06:12 +0100911 return &unique_processes_[upid];
912 }
Isabelle Taylor3dd366c2018-06-22 16:21:41 +0100913
Isabelle Taylora0a22972018-08-03 12:06:12 +0100914 Thread* GetMutableThread(UniqueTid utid) {
Florian Mayera1aaec22018-09-19 17:02:26 +0100915 PERFETTO_DCHECK(utid < unique_threads_.size());
Isabelle Taylora0a22972018-08-03 12:06:12 +0100916 return &unique_threads_[utid];
917 }
Isabelle Taylor3dd366c2018-06-22 16:21:41 +0100918
Primiano Tucci0e38a142019-01-07 20:51:09 +0000919 // Example usage: SetStats(stats::android_log_num_failed, 42);
920 void SetStats(size_t key, int64_t value) {
921 PERFETTO_DCHECK(key < stats::kNumKeys);
922 PERFETTO_DCHECK(stats::kTypes[key] == stats::kSingle);
923 stats_[key].value = value;
924 }
925
926 // Example usage: IncrementStats(stats::android_log_num_failed, -1);
927 void IncrementStats(size_t key, int64_t increment = 1) {
928 PERFETTO_DCHECK(key < stats::kNumKeys);
929 PERFETTO_DCHECK(stats::kTypes[key] == stats::kSingle);
930 stats_[key].value += increment;
931 }
932
Florian Mayer438b5ab2019-05-02 11:18:06 +0100933 // Example usage: IncrementIndexedStats(stats::cpu_failure, 1);
934 void IncrementIndexedStats(size_t key, int index, int64_t increment = 1) {
935 PERFETTO_DCHECK(key < stats::kNumKeys);
936 PERFETTO_DCHECK(stats::kTypes[key] == stats::kIndexed);
937 stats_[key].indexed_values[index] += increment;
938 }
939
Primiano Tucci0e38a142019-01-07 20:51:09 +0000940 // Example usage: SetIndexedStats(stats::cpu_failure, 1, 42);
941 void SetIndexedStats(size_t key, int index, int64_t value) {
942 PERFETTO_DCHECK(key < stats::kNumKeys);
943 PERFETTO_DCHECK(stats::kTypes[key] == stats::kIndexed);
944 stats_[key].indexed_values[index] = value;
945 }
946
Mikhail Khokhlove466c002019-05-23 13:33:33 +0100947 // Example usage:
948 // SetMetadata(metadata::benchmark_name,
949 // Variadic::String(storage->InternString("foo"));
Ryan Savitski51413ad2019-07-09 14:25:21 +0100950 // Returns the RowId of the new entry.
Mikhail Khokhlovb1fe42a2019-05-23 13:58:05 +0100951 // Virtual for testing.
Ryan Savitski51413ad2019-07-09 14:25:21 +0100952 virtual RowId SetMetadata(metadata::KeyIDs key, Variadic value) {
953 return metadata_.SetScalarMetadata(key, value);
Mikhail Khokhlove466c002019-05-23 13:33:33 +0100954 }
955
956 // Example usage:
957 // AppendMetadata(metadata::benchmark_story_tags,
958 // Variadic::String(storage->InternString("bar"));
Ryan Savitski51413ad2019-07-09 14:25:21 +0100959 // Returns the RowId of the new entry.
Mikhail Khokhlovb1fe42a2019-05-23 13:58:05 +0100960 // Virtual for testing.
Ryan Savitski51413ad2019-07-09 14:25:21 +0100961 virtual RowId AppendMetadata(metadata::KeyIDs key, Variadic value) {
962 return metadata_.AppendMetadata(key, value);
Mikhail Khokhlove466c002019-05-23 13:33:33 +0100963 }
964
Eric Seckler77b52782019-05-02 15:18:57 +0100965 class ScopedStatsTracer {
966 public:
967 ScopedStatsTracer(TraceStorage* storage, size_t key)
968 : storage_(storage), key_(key), start_ns_(base::GetWallTimeNs()) {}
969
970 ~ScopedStatsTracer() {
971 if (!storage_)
972 return;
973 auto delta_ns = base::GetWallTimeNs() - start_ns_;
974 storage_->IncrementStats(key_, delta_ns.count());
975 }
976
977 ScopedStatsTracer(ScopedStatsTracer&& other) noexcept { MoveImpl(&other); }
978
979 ScopedStatsTracer& operator=(ScopedStatsTracer&& other) {
980 MoveImpl(&other);
981 return *this;
982 }
983
984 private:
985 ScopedStatsTracer(const ScopedStatsTracer&) = delete;
986 ScopedStatsTracer& operator=(const ScopedStatsTracer&) = delete;
987
988 void MoveImpl(ScopedStatsTracer* other) {
989 storage_ = other->storage_;
990 key_ = other->key_;
991 start_ns_ = other->start_ns_;
992 other->storage_ = nullptr;
993 }
994
995 TraceStorage* storage_;
996 size_t key_;
997 base::TimeNanos start_ns_;
998 };
999
1000 ScopedStatsTracer TraceExecutionTimeIntoStats(size_t key) {
1001 return ScopedStatsTracer(this, key);
1002 }
1003
Lalit Maganticaed37e2018-06-01 03:03:08 +01001004 // Reading methods.
Eric Secklerc93823e2019-06-03 16:49:19 +01001005 // Virtual for testing.
1006 virtual NullTermStringView GetString(StringId id) const {
Lalit Maganti5c454312019-04-08 12:11:17 +01001007 return string_pool_.Get(id);
Isabelle Taylora0a22972018-08-03 12:06:12 +01001008 }
1009
Lalit Magantie9d40532018-06-29 13:15:06 +01001010 const Process& GetProcess(UniquePid upid) const {
Lalit Maganti676658b2018-11-20 18:24:31 +00001011 PERFETTO_DCHECK(upid < unique_processes_.size());
Isabelle Taylor47328cf2018-06-12 14:33:59 +01001012 return unique_processes_[upid];
1013 }
1014
Lalit Magantie9d40532018-06-29 13:15:06 +01001015 const Thread& GetThread(UniqueTid utid) const {
Lalit Maganti1f64cfa2018-09-18 13:20:02 +01001016 // Allow utid == 0 for idle thread retrieval.
Florian Mayera1aaec22018-09-19 17:02:26 +01001017 PERFETTO_DCHECK(utid < unique_threads_.size());
Isabelle Taylor68e42192018-06-19 16:19:31 +01001018 return unique_threads_[utid];
1019 }
1020
Lalit Maganti5ea9e932018-11-30 14:19:39 +00001021 static RowId CreateRowId(TableId table, uint32_t row) {
Lalit Maganti85ca4a82018-12-07 17:28:02 +00001022 return (static_cast<RowId>(table) << kRowIdTableShift) | row;
Lalit Maganti5ea9e932018-11-30 14:19:39 +00001023 }
1024
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +00001025 static std::pair<int8_t /*table*/, uint32_t /*row*/> ParseRowId(RowId rowid) {
1026 auto id = static_cast<uint64_t>(rowid);
1027 auto table_id = static_cast<uint8_t>(id >> kRowIdTableShift);
1028 auto row = static_cast<uint32_t>(id & ((1ull << kRowIdTableShift) - 1));
1029 return std::make_pair(table_id, row);
1030 }
1031
Eric Seckler5703ede2019-07-10 10:13:02 +01001032 const VirtualTracks& virtual_tracks() const { return virtual_tracks_; }
1033 VirtualTracks* mutable_virtual_tracks() { return &virtual_tracks_; }
1034
Lalit Magantiff69c112018-09-24 12:07:47 +01001035 const Slices& slices() const { return slices_; }
Lalit Magantifde29042018-10-04 13:28:52 +01001036 Slices* mutable_slices() { return &slices_; }
1037
Primiano Tucci0d72a312018-08-07 14:42:45 +01001038 const NestableSlices& nestable_slices() const { return nestable_slices_; }
1039 NestableSlices* mutable_nestable_slices() { return &nestable_slices_; }
1040
Eric Secklerd3b89d52019-07-10 15:36:29 +01001041 const ThreadSlices& thread_slices() const { return thread_slices_; }
1042 ThreadSlices* mutable_thread_slices() { return &thread_slices_; }
1043
Lalit Maganti8320e6d2019-03-14 18:49:33 +00001044 const CounterDefinitions& counter_definitions() const {
1045 return counter_definitions_;
1046 }
1047 CounterDefinitions* mutable_counter_definitions() {
1048 return &counter_definitions_;
1049 }
1050
1051 const CounterValues& counter_values() const { return counter_values_; }
1052 CounterValues* mutable_counter_values() { return &counter_values_; }
Isabelle Taylor14674d42018-09-07 11:33:11 +01001053
Primiano Tucci5cb84f82018-10-31 21:46:36 -07001054 const SqlStats& sql_stats() const { return sql_stats_; }
1055 SqlStats* mutable_sql_stats() { return &sql_stats_; }
1056
Isabelle Taylorc8c11202018-11-05 11:36:22 +00001057 const Instants& instants() const { return instants_; }
1058 Instants* mutable_instants() { return &instants_; }
1059
Primiano Tucci2c761ef2019-01-07 20:20:46 +00001060 const AndroidLogs& android_logs() const { return android_log_; }
1061 AndroidLogs* mutable_android_log() { return &android_log_; }
1062
Primiano Tucci0e38a142019-01-07 20:51:09 +00001063 const StatsMap& stats() const { return stats_; }
Lalit Maganti05e8c132018-11-09 18:16:12 +00001064
Ryan Savitski51413ad2019-07-09 14:25:21 +01001065 const Metadata& metadata() const { return metadata_; }
Ryan Savitski0476ee92019-07-09 14:29:33 +01001066 Metadata* mutable_metadata() { return &metadata_; }
Mikhail Khokhlove466c002019-05-23 13:33:33 +01001067
Lalit Maganti6e9c55e2018-11-29 12:00:39 +00001068 const Args& args() const { return args_; }
1069 Args* mutable_args() { return &args_; }
1070
Lalit Maganti1d915a62019-01-07 12:10:42 +00001071 const RawEvents& raw_events() const { return raw_events_; }
1072 RawEvents* mutable_raw_events() { return &raw_events_; }
1073
Florian Mayer438b5ab2019-05-02 11:18:06 +01001074 const HeapProfileMappings& heap_profile_mappings() const {
1075 return heap_profile_mappings_;
1076 }
1077 HeapProfileMappings* mutable_heap_profile_mappings() {
1078 return &heap_profile_mappings_;
1079 }
1080
1081 const HeapProfileFrames& heap_profile_frames() const {
1082 return heap_profile_frames_;
1083 }
1084 HeapProfileFrames* mutable_heap_profile_frames() {
1085 return &heap_profile_frames_;
1086 }
1087
1088 const HeapProfileCallsites& heap_profile_callsites() const {
1089 return heap_profile_callsites_;
1090 }
1091 HeapProfileCallsites* mutable_heap_profile_callsites() {
1092 return &heap_profile_callsites_;
1093 }
1094
1095 const HeapProfileAllocations& heap_profile_allocations() const {
1096 return heap_profile_allocations_;
1097 }
1098 HeapProfileAllocations* mutable_heap_profile_allocations() {
1099 return &heap_profile_allocations_;
1100 }
1101
Lalit Maganti5c454312019-04-08 12:11:17 +01001102 const StringPool& string_pool() const { return string_pool_; }
Lalit Magantiacda68b2018-10-29 15:23:25 +00001103
Isabelle Taylore7003fb2018-07-17 11:39:01 +01001104 // |unique_processes_| always contains at least 1 element becuase the 0th ID
1105 // is reserved to indicate an invalid process.
Lalit Maganti9c6395b2019-01-17 00:25:09 +00001106 size_t process_count() const { return unique_processes_.size(); }
Primiano Tucci0d72a312018-08-07 14:42:45 +01001107
Isabelle Taylore7003fb2018-07-17 11:39:01 +01001108 // |unique_threads_| always contains at least 1 element becuase the 0th ID
1109 // is reserved to indicate an invalid thread.
Lalit Maganti9c6395b2019-01-17 00:25:09 +00001110 size_t thread_count() const { return unique_threads_.size(); }
Isabelle Taylore7003fb2018-07-17 11:39:01 +01001111
Hector Dearman12323362018-08-09 16:09:28 +01001112 // Number of interned strings in the pool. Includes the empty string w/ ID=0.
1113 size_t string_count() const { return string_pool_.size(); }
1114
Ioannis Ilkosb8b11102019-01-29 17:56:55 +00001115 // Start / end ts (in nanoseconds) across the parsed trace events.
1116 // Returns (0, 0) if the trace is empty.
1117 std::pair<int64_t, int64_t> GetTraceTimestampBoundsNs() const;
1118
Lalit Maganticaed37e2018-06-01 03:03:08 +01001119 private:
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +00001120 static constexpr uint8_t kRowIdTableShift = 32;
Isabelle Taylora0a22972018-08-03 12:06:12 +01001121
Primiano Tucci2da5d2e2018-08-10 14:23:31 +01001122 using StringHash = uint64_t;
Lalit Maganticaed37e2018-06-01 03:03:08 +01001123
Lalit Maganti5c454312019-04-08 12:11:17 +01001124 TraceStorage(const TraceStorage&) = delete;
1125 TraceStorage& operator=(const TraceStorage&) = delete;
1126
1127 TraceStorage(TraceStorage&&) = default;
1128 TraceStorage& operator=(TraceStorage&&) = default;
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +00001129
Lalit Maganti05e8c132018-11-09 18:16:12 +00001130 // Stats about parsing the trace.
Primiano Tucci0e38a142019-01-07 20:51:09 +00001131 StatsMap stats_{};
Lalit Maganti35622b72018-06-06 12:03:11 +01001132
Ryan Savitski51413ad2019-07-09 14:25:21 +01001133 // Extra data extracted from the trace. Includes:
1134 // * metadata from chrome and benchmarking infrastructure
1135 // * descriptions of android packages
1136 Metadata metadata_{};
Mikhail Khokhlove466c002019-05-23 13:33:33 +01001137
Eric Seckler5703ede2019-07-10 10:13:02 +01001138 // Metadata for virtual slice tracks.
1139 VirtualTracks virtual_tracks_;
1140
Lalit Maganticaed37e2018-06-01 03:03:08 +01001141 // One entry for each CPU in the trace.
Lalit Magantiff69c112018-09-24 12:07:47 +01001142 Slices slices_;
Lalit Maganticaed37e2018-06-01 03:03:08 +01001143
Lalit Maganti6e9c55e2018-11-29 12:00:39 +00001144 // Args for all other tables.
1145 Args args_;
1146
Lalit Maganticaed37e2018-06-01 03:03:08 +01001147 // One entry for each unique string in the trace.
Lalit Maganti5c454312019-04-08 12:11:17 +01001148 StringPool string_pool_;
Isabelle Taylor47328cf2018-06-12 14:33:59 +01001149
Isabelle Taylor47328cf2018-06-12 14:33:59 +01001150 // One entry for each UniquePid, with UniquePid as the index.
Ioannis Ilkos9c03cbd2019-03-12 18:30:43 +00001151 // Never hold on to pointers to Process, as vector resize will
1152 // invalidate them.
1153 std::vector<Process> unique_processes_;
Isabelle Taylor68e42192018-06-19 16:19:31 +01001154
Isabelle Taylor68e42192018-06-19 16:19:31 +01001155 // One entry for each UniqueTid, with UniqueTid as the index.
Isabelle Taylor3dd366c2018-06-22 16:21:41 +01001156 std::deque<Thread> unique_threads_;
Primiano Tucci0d72a312018-08-07 14:42:45 +01001157
1158 // Slices coming from userspace events (e.g. Chromium TRACE_EVENT macros).
1159 NestableSlices nestable_slices_;
Isabelle Taylor15314ea2018-09-19 11:35:19 +01001160
Eric Secklerd3b89d52019-07-10 15:36:29 +01001161 // Additional attributes for threads slices (sub-type of NestableSlices).
1162 ThreadSlices thread_slices_;
1163
Hector Dearman2442d612019-06-04 18:05:23 +01001164 // The type of counters in the trace. Can be thought of as the "metadata".
Lalit Maganti8320e6d2019-03-14 18:49:33 +00001165 CounterDefinitions counter_definitions_;
1166
1167 // The values from the Counter events from the trace. This includes CPU
1168 // frequency events as well systrace trace_marker counter events.
1169 CounterValues counter_values_;
Primiano Tucci5cb84f82018-10-31 21:46:36 -07001170
1171 SqlStats sql_stats_;
Lalit Maganti6e9c55e2018-11-29 12:00:39 +00001172
Isabelle Taylorc8c11202018-11-05 11:36:22 +00001173 // These are instantaneous events in the trace. They have no duration
1174 // and do not have a value that make sense to track over time.
1175 // e.g. signal events
1176 Instants instants_;
Lalit Maganti1d915a62019-01-07 12:10:42 +00001177
1178 // Raw events are every ftrace event in the trace. The raw event includes
1179 // the timestamp and the pid. The args for the raw event will be in the
1180 // args table. This table can be used to generate a text version of the
1181 // trace.
1182 RawEvents raw_events_;
Primiano Tucci2c761ef2019-01-07 20:20:46 +00001183 AndroidLogs android_log_;
Florian Mayer438b5ab2019-05-02 11:18:06 +01001184
1185 HeapProfileMappings heap_profile_mappings_;
1186 HeapProfileFrames heap_profile_frames_;
1187 HeapProfileCallsites heap_profile_callsites_;
1188 HeapProfileAllocations heap_profile_allocations_;
Lalit Maganticaed37e2018-06-01 03:03:08 +01001189};
1190
1191} // namespace trace_processor
1192} // namespace perfetto
1193
Florian Mayerbee52132019-05-02 13:59:56 +01001194namespace std {
1195
1196template <>
1197struct hash<::perfetto::trace_processor::TraceStorage::HeapProfileFrames::Row> {
1198 using argument_type =
1199 ::perfetto::trace_processor::TraceStorage::HeapProfileFrames::Row;
1200 using result_type = size_t;
1201
1202 result_type operator()(const argument_type& r) const {
1203 return std::hash<::perfetto::trace_processor::StringId>{}(r.name_id) ^
1204 std::hash<int64_t>{}(r.mapping_row) ^ std::hash<int64_t>{}(r.rel_pc);
1205 }
1206};
1207
1208template <>
1209struct hash<
1210 ::perfetto::trace_processor::TraceStorage::HeapProfileCallsites::Row> {
1211 using argument_type =
1212 ::perfetto::trace_processor::TraceStorage::HeapProfileCallsites::Row;
1213 using result_type = size_t;
1214
1215 result_type operator()(const argument_type& r) const {
1216 return std::hash<int64_t>{}(r.depth) ^ std::hash<int64_t>{}(r.parent_id) ^
1217 std::hash<int64_t>{}(r.frame_row);
1218 }
1219};
1220
1221template <>
1222struct hash<
1223 ::perfetto::trace_processor::TraceStorage::HeapProfileMappings::Row> {
1224 using argument_type =
1225 ::perfetto::trace_processor::TraceStorage::HeapProfileMappings::Row;
1226 using result_type = size_t;
1227
1228 result_type operator()(const argument_type& r) const {
1229 return std::hash<::perfetto::trace_processor::StringId>{}(r.build_id) ^
Florian Mayer12655732019-07-02 15:08:26 +01001230 std::hash<int64_t>{}(r.exact_offset) ^
1231 std::hash<int64_t>{}(r.start_offset) ^
1232 std::hash<int64_t>{}(r.start) ^ std::hash<int64_t>{}(r.end) ^
1233 std::hash<int64_t>{}(r.load_bias) ^
Florian Mayerbee52132019-05-02 13:59:56 +01001234 std::hash<::perfetto::trace_processor::StringId>{}(r.name_id);
1235 }
1236};
1237
1238} // namespace std
1239
Lalit Maganticaed37e2018-06-01 03:03:08 +01001240#endif // SRC_TRACE_PROCESSOR_TRACE_STORAGE_H_