blob: 0799eae3e33b2f332ef82da03112c200b6131c2f [file] [log] [blame]
Lalit Maganticaed37e2018-06-01 03:03:08 +01001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef SRC_TRACE_PROCESSOR_TRACE_STORAGE_H_
18#define SRC_TRACE_PROCESSOR_TRACE_STORAGE_H_
19
Lalit Maganti35622b72018-06-06 12:03:11 +010020#include <array>
Lalit Maganticaed37e2018-06-01 03:03:08 +010021#include <deque>
Isabelle Taylor47328cf2018-06-12 14:33:59 +010022#include <map>
Lalit Maganticaed37e2018-06-01 03:03:08 +010023#include <string>
24#include <unordered_map>
Ioannis Ilkosb8b11102019-01-29 17:56:55 +000025#include <utility>
Lalit Maganticaed37e2018-06-01 03:03:08 +010026#include <vector>
27
Lalit Maganti35622b72018-06-06 12:03:11 +010028#include "perfetto/base/logging.h"
Eric Seckler83dcc8c2019-08-21 12:18:43 +010029#include "perfetto/base/time.h"
Primiano Tucci2c5488f2019-06-01 03:27:28 +010030#include "perfetto/ext/base/hash.h"
31#include "perfetto/ext/base/optional.h"
32#include "perfetto/ext/base/string_view.h"
Primiano Tucci2c5488f2019-06-01 03:27:28 +010033#include "perfetto/ext/base/utils.h"
Lalit Magantib0b53ee2019-01-24 17:53:39 +000034#include "src/trace_processor/ftrace_utils.h"
Mikhail Khokhlove466c002019-05-23 13:33:33 +010035#include "src/trace_processor/metadata.h"
Primiano Tucci0e38a142019-01-07 20:51:09 +000036#include "src/trace_processor/stats.h"
Lalit Maganti5c454312019-04-08 12:11:17 +010037#include "src/trace_processor/string_pool.h"
Ioannis Ilkose0b47f52019-09-18 11:14:57 +010038#include "src/trace_processor/tables/profiler_tables.h"
Lalit Maganti20539c22019-09-04 12:36:10 +010039#include "src/trace_processor/tables/slice_tables.h"
40#include "src/trace_processor/tables/track_tables.h"
Mikhail Khokhlov85a0dd02019-05-17 14:22:28 +010041#include "src/trace_processor/variadic.h"
Lalit Maganti35622b72018-06-06 12:03:11 +010042
Lalit Maganticaed37e2018-06-01 03:03:08 +010043namespace perfetto {
44namespace trace_processor {
45
Isabelle Taylora0a22972018-08-03 12:06:12 +010046// UniquePid is an offset into |unique_processes_|. This is necessary because
47// Unix pids are reused and thus not guaranteed to be unique over a long
48// period of time.
49using UniquePid = uint32_t;
Primiano Tucci0d72a312018-08-07 14:42:45 +010050
Isabelle Taylora0a22972018-08-03 12:06:12 +010051// UniqueTid is an offset into |unique_threads_|. Necessary because tids can
52// be reused.
53using UniqueTid = uint32_t;
54
Primiano Tucci0d72a312018-08-07 14:42:45 +010055// StringId is an offset into |string_pool_|.
Lalit Maganti5c454312019-04-08 12:11:17 +010056using StringId = StringPool::Id;
Lalit Maganti1a2936f2019-08-29 17:42:33 +010057static const StringId kNullStringId = StringId(0);
Primiano Tucci0d72a312018-08-07 14:42:45 +010058
Lalit Maganti5ea9e932018-11-30 14:19:39 +000059// Identifiers for all the tables in the database.
60enum TableId : uint8_t {
61 // Intentionally don't have TableId == 0 so that RowId == 0 can refer to an
62 // invalid row id.
Lalit Maganti8320e6d2019-03-14 18:49:33 +000063 kCounterValues = 1,
Lalit Maganti1d915a62019-01-07 12:10:42 +000064 kRawEvents = 2,
Lalit Maganti66ed7ad2019-01-11 16:47:26 +000065 kInstants = 3,
Isabelle Taylorb9222c32019-01-31 10:58:37 +000066 kSched = 4,
Eric Seckler70cc4422019-05-28 16:00:23 +010067 kNestableSlices = 5,
Ryan Savitski51413ad2019-07-09 14:25:21 +010068 kMetadataTable = 6,
Lalit Maganti53bdbb22019-09-25 11:11:18 +010069 kTrack = 7,
Lalit Maganti5ea9e932018-11-30 14:19:39 +000070};
71
72// The top 8 bits are set to the TableId and the bottom 32 to the row of the
73// table.
Lalit Maganti85ca4a82018-12-07 17:28:02 +000074using RowId = int64_t;
Lalit Maganti5ea9e932018-11-30 14:19:39 +000075static const RowId kInvalidRowId = 0;
76
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +000077using ArgSetId = uint32_t;
78static const ArgSetId kInvalidArgSetId = 0;
79
Eric Seckler5703ede2019-07-10 10:13:02 +010080using TrackId = uint32_t;
81
Isabelle Taylora97c5f52018-10-23 17:36:12 +010082enum RefType {
Primiano Tucci5403e4f2018-11-27 10:07:03 +000083 kRefNoRef = 0,
84 kRefUtid = 1,
85 kRefCpuId = 2,
86 kRefIrq = 3,
87 kRefSoftIrq = 4,
88 kRefUpid = 5,
Sidath Senanayake1f5f93a2019-06-06 22:24:15 +010089 kRefGpuId = 6,
Eric Seckler5703ede2019-07-10 10:13:02 +010090 kRefTrack = 7,
Primiano Tucci5403e4f2018-11-27 10:07:03 +000091 kRefMax
Isabelle Taylora97c5f52018-10-23 17:36:12 +010092};
Isabelle Taylor14674d42018-09-07 11:33:11 +010093
Eric Seckler972225e2019-04-18 11:07:12 +010094const std::vector<const char*>& GetRefTypeStringMap();
95
Lalit Maganticaed37e2018-06-01 03:03:08 +010096// Stores a data inside a trace file in a columnar form. This makes it efficient
97// to read or search across a single field of the trace (e.g. all the thread
98// names for a given CPU).
99class TraceStorage {
100 public:
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100101 TraceStorage();
Isabelle Taylora0a22972018-08-03 12:06:12 +0100102
103 virtual ~TraceStorage();
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100104
Isabelle Taylora0a22972018-08-03 12:06:12 +0100105 // Information about a unique process seen in a trace.
106 struct Process {
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100107 explicit Process(uint32_t p) : pid(p) {}
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000108 int64_t start_ns = 0;
Lalit Maganti637589a2019-07-04 17:25:29 +0100109 int64_t end_ns = 0;
Isabelle Taylora0a22972018-08-03 12:06:12 +0100110 StringId name_id = 0;
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100111 uint32_t pid = 0;
Lalit Maganti369b0572019-07-11 15:35:09 +0100112 base::Optional<UniquePid> parent_upid;
Isabelle Taylora0a22972018-08-03 12:06:12 +0100113 };
114
115 // Information about a unique thread seen in a trace.
116 struct Thread {
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100117 explicit Thread(uint32_t t) : tid(t) {}
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000118 int64_t start_ns = 0;
Lalit Magantib5bd2332019-06-06 14:20:47 +0100119 int64_t end_ns = 0;
Isabelle Taylora0a22972018-08-03 12:06:12 +0100120 StringId name_id = 0;
Lalit Maganti770886a2018-11-16 17:40:21 +0000121 base::Optional<UniquePid> upid;
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100122 uint32_t tid = 0;
Isabelle Taylora0a22972018-08-03 12:06:12 +0100123 };
Isabelle Taylor3dd366c2018-06-22 16:21:41 +0100124
Lalit Maganti6e9c55e2018-11-29 12:00:39 +0000125 // Generic key value storage which can be referenced by other tables.
126 class Args {
127 public:
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000128 struct Arg {
129 StringId flat_key = 0;
130 StringId key = 0;
131 Variadic value = Variadic::Integer(0);
132
133 // This is only used by the arg tracker and so is not part of the hash.
134 RowId row_id = 0;
135 };
136
137 struct ArgHasher {
138 uint64_t operator()(const Arg& arg) const noexcept {
Lalit Maganti1f464742019-02-28 13:49:31 +0000139 base::Hash hash;
140 hash.Update(arg.key);
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000141 // We don't hash arg.flat_key because it's a subsequence of arg.key.
142 switch (arg.value.type) {
143 case Variadic::Type::kInt:
Lalit Maganti1f464742019-02-28 13:49:31 +0000144 hash.Update(arg.value.int_value);
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000145 break;
Eric Secklerc93823e2019-06-03 16:49:19 +0100146 case Variadic::Type::kUint:
147 hash.Update(arg.value.uint_value);
148 break;
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000149 case Variadic::Type::kString:
Lalit Maganti1f464742019-02-28 13:49:31 +0000150 hash.Update(arg.value.string_value);
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000151 break;
152 case Variadic::Type::kReal:
Lalit Maganti1f464742019-02-28 13:49:31 +0000153 hash.Update(arg.value.real_value);
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000154 break;
Eric Secklerc93823e2019-06-03 16:49:19 +0100155 case Variadic::Type::kPointer:
156 hash.Update(arg.value.pointer_value);
157 break;
158 case Variadic::Type::kBool:
159 hash.Update(arg.value.bool_value);
160 break;
161 case Variadic::Type::kJson:
162 hash.Update(arg.value.json_value);
163 break;
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000164 }
Lalit Maganti1f464742019-02-28 13:49:31 +0000165 return hash.digest();
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000166 }
167 };
168
169 const std::deque<ArgSetId>& set_ids() const { return set_ids_; }
Lalit Maganti5ea9e932018-11-30 14:19:39 +0000170 const std::deque<StringId>& flat_keys() const { return flat_keys_; }
171 const std::deque<StringId>& keys() const { return keys_; }
Lalit Maganti1d915a62019-01-07 12:10:42 +0000172 const std::deque<Variadic>& arg_values() const { return arg_values_; }
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000173 uint32_t args_count() const {
174 return static_cast<uint32_t>(set_ids_.size());
Lalit Maganti79472be2018-12-04 13:41:27 +0000175 }
Lalit Maganti6e9c55e2018-11-29 12:00:39 +0000176
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000177 ArgSetId AddArgSet(const std::vector<Arg>& args,
178 uint32_t begin,
179 uint32_t end) {
Lalit Maganti1f464742019-02-28 13:49:31 +0000180 base::Hash hash;
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000181 for (uint32_t i = begin; i < end; i++) {
Lalit Maganti1f464742019-02-28 13:49:31 +0000182 hash.Update(ArgHasher()(args[i]));
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000183 }
Lalit Maganti5ea9e932018-11-30 14:19:39 +0000184
Lalit Maganti1f464742019-02-28 13:49:31 +0000185 ArgSetHash digest = hash.digest();
186 auto it = arg_row_for_hash_.find(digest);
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000187 if (it != arg_row_for_hash_.end()) {
188 return set_ids_[it->second];
189 }
190
191 // The +1 ensures that nothing has an id == kInvalidArgSetId == 0.
192 ArgSetId id = static_cast<uint32_t>(arg_row_for_hash_.size()) + 1;
Lalit Maganti1f464742019-02-28 13:49:31 +0000193 arg_row_for_hash_.emplace(digest, args_count());
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000194 for (uint32_t i = begin; i < end; i++) {
195 const auto& arg = args[i];
196 set_ids_.emplace_back(id);
197 flat_keys_.emplace_back(arg.flat_key);
198 keys_.emplace_back(arg.key);
199 arg_values_.emplace_back(arg.value);
200 }
201 return id;
Lalit Maganti6e9c55e2018-11-29 12:00:39 +0000202 }
203
Lalit Maganti5ea9e932018-11-30 14:19:39 +0000204 private:
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000205 using ArgSetHash = uint64_t;
206
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000207 std::deque<ArgSetId> set_ids_;
Lalit Maganti5ea9e932018-11-30 14:19:39 +0000208 std::deque<StringId> flat_keys_;
209 std::deque<StringId> keys_;
Lalit Maganti1d915a62019-01-07 12:10:42 +0000210 std::deque<Variadic> arg_values_;
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000211
212 std::unordered_map<ArgSetHash, uint32_t> arg_row_for_hash_;
Lalit Maganti6e9c55e2018-11-29 12:00:39 +0000213 };
214
Lalit Magantid11d3e02019-07-26 12:32:09 +0530215 class Tracks {
216 public:
217 inline uint32_t AddTrack(StringId name) {
218 names_.emplace_back(name);
219 return track_count() - 1;
220 }
221
222 uint32_t track_count() const {
223 return static_cast<uint32_t>(names_.size());
224 }
225
226 const std::deque<StringId>& names() const { return names_; }
227
228 private:
229 std::deque<StringId> names_;
230 };
231
Mikael Pessa803090c2019-08-23 16:03:34 -0700232 class GpuContexts {
233 public:
234 inline void AddGpuContext(uint64_t context_id,
235 UniquePid upid,
236 uint32_t priority) {
237 context_ids_.emplace_back(context_id);
238 upids_.emplace_back(upid);
239 priorities_.emplace_back(priority);
240 }
241
242 uint32_t gpu_context_count() const {
243 return static_cast<uint32_t>(context_ids_.size());
244 }
245
246 const std::deque<uint64_t>& context_ids() const { return context_ids_; }
247 const std::deque<UniquePid>& upids() const { return upids_; }
248 const std::deque<uint32_t>& priorities() const { return priorities_; }
249
250 private:
251 std::deque<uint64_t> context_ids_;
252 std::deque<UniquePid> upids_;
253 std::deque<uint32_t> priorities_;
254 };
255
Lalit Magantiff69c112018-09-24 12:07:47 +0100256 class Slices {
Lalit Maganti35622b72018-06-06 12:03:11 +0100257 public:
Lalit Magantifde29042018-10-04 13:28:52 +0100258 inline size_t AddSlice(uint32_t cpu,
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000259 int64_t start_ns,
260 int64_t duration_ns,
Lalit Magantib0b53ee2019-01-24 17:53:39 +0000261 UniqueTid utid,
262 ftrace_utils::TaskState end_state,
263 int32_t priority) {
Lalit Magantiff69c112018-09-24 12:07:47 +0100264 cpus_.emplace_back(cpu);
Lalit Maganti35622b72018-06-06 12:03:11 +0100265 start_ns_.emplace_back(start_ns);
266 durations_.emplace_back(duration_ns);
Isabelle Taylora0a22972018-08-03 12:06:12 +0100267 utids_.emplace_back(utid);
Lalit Magantib0b53ee2019-01-24 17:53:39 +0000268 end_states_.emplace_back(end_state);
269 priorities_.emplace_back(priority);
Lalit Maganti58638932019-01-31 10:48:26 +0000270
271 if (utid >= rows_for_utids_.size())
272 rows_for_utids_.resize(utid + 1);
273 rows_for_utids_[utid].emplace_back(slice_count() - 1);
Lalit Magantifde29042018-10-04 13:28:52 +0100274 return slice_count() - 1;
275 }
276
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000277 void set_duration(size_t index, int64_t duration_ns) {
Lalit Magantifde29042018-10-04 13:28:52 +0100278 durations_[index] = duration_ns;
Lalit Maganti35622b72018-06-06 12:03:11 +0100279 }
280
Lalit Magantib0b53ee2019-01-24 17:53:39 +0000281 void set_end_state(size_t index, ftrace_utils::TaskState end_state) {
282 end_states_[index] = end_state;
283 }
284
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100285 size_t slice_count() const { return start_ns_.size(); }
Lalit Maganti35622b72018-06-06 12:03:11 +0100286
Lalit Magantiff69c112018-09-24 12:07:47 +0100287 const std::deque<uint32_t>& cpus() const { return cpus_; }
288
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000289 const std::deque<int64_t>& start_ns() const { return start_ns_; }
Lalit Maganti35622b72018-06-06 12:03:11 +0100290
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000291 const std::deque<int64_t>& durations() const { return durations_; }
Lalit Maganti35622b72018-06-06 12:03:11 +0100292
Isabelle Taylor68e42192018-06-19 16:19:31 +0100293 const std::deque<UniqueTid>& utids() const { return utids_; }
294
Lalit Magantib0b53ee2019-01-24 17:53:39 +0000295 const std::deque<ftrace_utils::TaskState>& end_state() const {
296 return end_states_;
297 }
298
299 const std::deque<int32_t>& priorities() const { return priorities_; }
300
Lalit Maganti58638932019-01-31 10:48:26 +0000301 const std::deque<std::vector<uint32_t>>& rows_for_utids() const {
Lalit Magantif0f09c32019-01-18 17:29:31 +0000302 return rows_for_utids_;
303 }
304
Lalit Maganti35622b72018-06-06 12:03:11 +0100305 private:
Hector Dearman947f12a2018-09-11 16:50:36 +0100306 // Each deque below has the same number of entries (the number of slices
Lalit Maganti35622b72018-06-06 12:03:11 +0100307 // in the trace for the CPU).
Lalit Magantiff69c112018-09-24 12:07:47 +0100308 std::deque<uint32_t> cpus_;
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000309 std::deque<int64_t> start_ns_;
310 std::deque<int64_t> durations_;
Isabelle Taylor68e42192018-06-19 16:19:31 +0100311 std::deque<UniqueTid> utids_;
Lalit Magantib0b53ee2019-01-24 17:53:39 +0000312 std::deque<ftrace_utils::TaskState> end_states_;
313 std::deque<int32_t> priorities_;
Lalit Maganti58638932019-01-31 10:48:26 +0000314
315 // One row per utid.
316 std::deque<std::vector<uint32_t>> rows_for_utids_;
Primiano Tucci0d72a312018-08-07 14:42:45 +0100317 };
Isabelle Taylor68e42192018-06-19 16:19:31 +0100318
Primiano Tucci0d72a312018-08-07 14:42:45 +0100319 class NestableSlices {
320 public:
Eric Seckler70cc4422019-05-28 16:00:23 +0100321 inline uint32_t AddSlice(int64_t start_ns,
322 int64_t duration_ns,
323 int64_t ref,
324 RefType type,
Lalit Maganti81a02cd2019-07-12 17:05:11 +0100325 StringId category,
Eric Seckler70cc4422019-05-28 16:00:23 +0100326 StringId name,
327 uint8_t depth,
328 int64_t stack_id,
329 int64_t parent_stack_id) {
Primiano Tucci0d72a312018-08-07 14:42:45 +0100330 start_ns_.emplace_back(start_ns);
331 durations_.emplace_back(duration_ns);
Eric Seckler972225e2019-04-18 11:07:12 +0100332 refs_.emplace_back(ref);
333 types_.emplace_back(type);
Lalit Maganti81a02cd2019-07-12 17:05:11 +0100334 categories_.emplace_back(category);
Primiano Tucci0d72a312018-08-07 14:42:45 +0100335 names_.emplace_back(name);
336 depths_.emplace_back(depth);
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100337 stack_ids_.emplace_back(stack_id);
338 parent_stack_ids_.emplace_back(parent_stack_id);
Eric Seckler70cc4422019-05-28 16:00:23 +0100339 arg_set_ids_.emplace_back(kInvalidArgSetId);
Lalit Maganti7b37bbf2018-11-09 15:58:54 +0000340 return slice_count() - 1;
341 }
342
Eric Seckler70cc4422019-05-28 16:00:23 +0100343 void set_duration(uint32_t index, int64_t duration_ns) {
Lalit Maganti7b37bbf2018-11-09 15:58:54 +0000344 durations_[index] = duration_ns;
345 }
346
Eric Seckler70cc4422019-05-28 16:00:23 +0100347 void set_stack_id(uint32_t index, int64_t stack_id) {
Lalit Maganti7b37bbf2018-11-09 15:58:54 +0000348 stack_ids_[index] = stack_id;
Primiano Tucci0d72a312018-08-07 14:42:45 +0100349 }
350
Eric Seckler70cc4422019-05-28 16:00:23 +0100351 void set_arg_set_id(uint32_t index, ArgSetId id) {
352 arg_set_ids_[index] = id;
353 }
354
355 uint32_t slice_count() const {
356 return static_cast<uint32_t>(start_ns_.size());
357 }
358
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000359 const std::deque<int64_t>& start_ns() const { return start_ns_; }
360 const std::deque<int64_t>& durations() const { return durations_; }
Eric Seckler972225e2019-04-18 11:07:12 +0100361 const std::deque<int64_t>& refs() const { return refs_; }
362 const std::deque<RefType>& types() const { return types_; }
Lalit Maganti81a02cd2019-07-12 17:05:11 +0100363 const std::deque<StringId>& categories() const { return categories_; }
Primiano Tucci0d72a312018-08-07 14:42:45 +0100364 const std::deque<StringId>& names() const { return names_; }
365 const std::deque<uint8_t>& depths() const { return depths_; }
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000366 const std::deque<int64_t>& stack_ids() const { return stack_ids_; }
367 const std::deque<int64_t>& parent_stack_ids() const {
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100368 return parent_stack_ids_;
369 }
Eric Seckler70cc4422019-05-28 16:00:23 +0100370 const std::deque<ArgSetId>& arg_set_ids() const { return arg_set_ids_; }
Primiano Tucci0d72a312018-08-07 14:42:45 +0100371
372 private:
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000373 std::deque<int64_t> start_ns_;
374 std::deque<int64_t> durations_;
Eric Seckler972225e2019-04-18 11:07:12 +0100375 std::deque<int64_t> refs_;
376 std::deque<RefType> types_;
Lalit Maganti81a02cd2019-07-12 17:05:11 +0100377 std::deque<StringId> categories_;
Primiano Tucci0d72a312018-08-07 14:42:45 +0100378 std::deque<StringId> names_;
379 std::deque<uint8_t> depths_;
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000380 std::deque<int64_t> stack_ids_;
381 std::deque<int64_t> parent_stack_ids_;
Eric Seckler70cc4422019-05-28 16:00:23 +0100382 std::deque<ArgSetId> arg_set_ids_;
Lalit Maganti35622b72018-06-06 12:03:11 +0100383 };
384
Eric Secklerd3b89d52019-07-10 15:36:29 +0100385 class ThreadSlices {
386 public:
387 inline uint32_t AddThreadSlice(uint32_t slice_id,
388 int64_t thread_timestamp_ns,
389 int64_t thread_duration_ns,
390 int64_t thread_instruction_count,
391 int64_t thread_instruction_delta) {
392 slice_ids_.emplace_back(slice_id);
393 thread_timestamp_ns_.emplace_back(thread_timestamp_ns);
394 thread_duration_ns_.emplace_back(thread_duration_ns);
395 thread_instruction_counts_.emplace_back(thread_instruction_count);
396 thread_instruction_deltas_.emplace_back(thread_instruction_delta);
397 return slice_count() - 1;
398 }
399
Eric Secklerc3430c62019-07-22 10:49:58 +0100400 uint32_t slice_count() const {
401 return static_cast<uint32_t>(slice_ids_.size());
Eric Secklerd3b89d52019-07-10 15:36:29 +0100402 }
403
Eric Secklerc3430c62019-07-22 10:49:58 +0100404 const std::deque<uint32_t>& slice_ids() const { return slice_ids_; }
405 const std::deque<int64_t>& thread_timestamp_ns() const {
406 return thread_timestamp_ns_;
407 }
408 const std::deque<int64_t>& thread_duration_ns() const {
409 return thread_duration_ns_;
410 }
411 const std::deque<int64_t>& thread_instruction_counts() const {
412 return thread_instruction_counts_;
413 }
414 const std::deque<int64_t>& thread_instruction_deltas() const {
415 return thread_instruction_deltas_;
416 }
417
418 base::Optional<uint32_t> FindRowForSliceId(uint32_t slice_id) const {
419 auto it =
420 std::lower_bound(slice_ids().begin(), slice_ids().end(), slice_id);
421 if (it != slice_ids().end() && *it == slice_id) {
422 return static_cast<uint32_t>(std::distance(slice_ids().begin(), it));
423 }
424 return base::nullopt;
425 }
426
Eric Seckler54f30a32019-07-19 15:10:29 +0100427 void UpdateThreadDeltasForSliceId(uint32_t slice_id,
428 int64_t end_thread_timestamp_ns,
429 int64_t end_thread_instruction_count) {
Eric Secklerc3430c62019-07-22 10:49:58 +0100430 uint32_t row = *FindRowForSliceId(slice_id);
431 int64_t begin_ns = thread_timestamp_ns_[row];
432 thread_duration_ns_[row] = end_thread_timestamp_ns - begin_ns;
Eric Seckler54f30a32019-07-19 15:10:29 +0100433 int64_t begin_ticount = thread_instruction_counts_[row];
434 thread_instruction_deltas_[row] =
435 end_thread_instruction_count - begin_ticount;
Eric Secklerc3430c62019-07-22 10:49:58 +0100436 }
437
438 private:
439 std::deque<uint32_t> slice_ids_;
440 std::deque<int64_t> thread_timestamp_ns_;
441 std::deque<int64_t> thread_duration_ns_;
442 std::deque<int64_t> thread_instruction_counts_;
443 std::deque<int64_t> thread_instruction_deltas_;
444 };
445
446 class VirtualTrackSlices {
447 public:
448 inline uint32_t AddVirtualTrackSlice(uint32_t slice_id,
449 int64_t thread_timestamp_ns,
450 int64_t thread_duration_ns,
451 int64_t thread_instruction_count,
452 int64_t thread_instruction_delta) {
453 slice_ids_.emplace_back(slice_id);
454 thread_timestamp_ns_.emplace_back(thread_timestamp_ns);
455 thread_duration_ns_.emplace_back(thread_duration_ns);
456 thread_instruction_counts_.emplace_back(thread_instruction_count);
457 thread_instruction_deltas_.emplace_back(thread_instruction_delta);
458 return slice_count() - 1;
Eric Secklerd3b89d52019-07-10 15:36:29 +0100459 }
460
461 uint32_t slice_count() const {
462 return static_cast<uint32_t>(slice_ids_.size());
463 }
464
465 const std::deque<uint32_t>& slice_ids() const { return slice_ids_; }
466 const std::deque<int64_t>& thread_timestamp_ns() const {
467 return thread_timestamp_ns_;
468 }
469 const std::deque<int64_t>& thread_duration_ns() const {
470 return thread_duration_ns_;
471 }
472 const std::deque<int64_t>& thread_instruction_counts() const {
473 return thread_instruction_counts_;
474 }
475 const std::deque<int64_t>& thread_instruction_deltas() const {
476 return thread_instruction_deltas_;
477 }
478
Mikhail Khokhlov7db04912019-07-18 16:11:11 +0100479 base::Optional<uint32_t> FindRowForSliceId(uint32_t slice_id) const {
Eric Secklerd3b89d52019-07-10 15:36:29 +0100480 auto it =
481 std::lower_bound(slice_ids().begin(), slice_ids().end(), slice_id);
Mikhail Khokhlov7db04912019-07-18 16:11:11 +0100482 if (it != slice_ids().end() && *it == slice_id) {
483 return static_cast<uint32_t>(std::distance(slice_ids().begin(), it));
484 }
485 return base::nullopt;
Eric Secklerd3b89d52019-07-10 15:36:29 +0100486 }
487
Eric Seckler54f30a32019-07-19 15:10:29 +0100488 void UpdateThreadDeltasForSliceId(uint32_t slice_id,
489 int64_t end_thread_timestamp_ns,
490 int64_t end_thread_instruction_count) {
Mikhail Khokhlov7db04912019-07-18 16:11:11 +0100491 uint32_t row = *FindRowForSliceId(slice_id);
Eric Secklerd3b89d52019-07-10 15:36:29 +0100492 int64_t begin_ns = thread_timestamp_ns_[row];
493 thread_duration_ns_[row] = end_thread_timestamp_ns - begin_ns;
Eric Seckler54f30a32019-07-19 15:10:29 +0100494 int64_t begin_ticount = thread_instruction_counts_[row];
495 thread_instruction_deltas_[row] =
496 end_thread_instruction_count - begin_ticount;
Eric Secklerd3b89d52019-07-10 15:36:29 +0100497 }
498
499 private:
500 std::deque<uint32_t> slice_ids_;
501 std::deque<int64_t> thread_timestamp_ns_;
502 std::deque<int64_t> thread_duration_ns_;
503 std::deque<int64_t> thread_instruction_counts_;
504 std::deque<int64_t> thread_instruction_deltas_;
505 };
506
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000507 class CounterDefinitions {
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100508 public:
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000509 using Id = uint32_t;
Lalit Maganti521d97b2019-04-29 13:47:03 +0100510 static constexpr Id kInvalidId = std::numeric_limits<Id>::max();
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000511
512 inline Id AddCounterDefinition(StringId name_id,
513 int64_t ref,
Pascal Muetschard91d07892019-08-26 13:55:06 -0700514 RefType type,
Pascal Muetschardceb5c822019-08-26 14:39:15 -0700515 StringId desc_id = 0,
516 StringId unit_id = 0) {
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000517 base::Hash hash;
518 hash.Update(name_id);
519 hash.Update(ref);
520 hash.Update(type);
521
522 // TODO(lalitm): this is a perf bottleneck and likely we can do something
523 // quite a bit better here.
524 uint64_t digest = hash.digest();
525 auto it = hash_to_row_idx_.find(digest);
526 if (it != hash_to_row_idx_.end())
527 return it->second;
528
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100529 name_ids_.emplace_back(name_id);
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100530 refs_.emplace_back(ref);
531 types_.emplace_back(type);
Pascal Muetschard91d07892019-08-26 13:55:06 -0700532 desc_ids_.emplace_back(desc_id);
Pascal Muetschardceb5c822019-08-26 14:39:15 -0700533 unit_ids_.emplace_back(unit_id);
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000534 hash_to_row_idx_.emplace(digest, size() - 1);
535 return size() - 1;
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100536 }
Lalit Magantifde29042018-10-04 13:28:52 +0100537
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000538 uint32_t size() const { return static_cast<uint32_t>(name_ids_.size()); }
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100539
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100540 const std::deque<StringId>& name_ids() const { return name_ids_; }
541
Pascal Muetschard91d07892019-08-26 13:55:06 -0700542 const std::deque<StringId>& desc_ids() const { return desc_ids_; }
543
Pascal Muetschardceb5c822019-08-26 14:39:15 -0700544 const std::deque<StringId>& unit_ids() const { return unit_ids_; }
545
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100546 const std::deque<int64_t>& refs() const { return refs_; }
547
548 const std::deque<RefType>& types() const { return types_; }
549
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000550 private:
551 std::deque<StringId> name_ids_;
552 std::deque<int64_t> refs_;
553 std::deque<RefType> types_;
Pascal Muetschard91d07892019-08-26 13:55:06 -0700554 std::deque<StringId> desc_ids_;
Pascal Muetschardceb5c822019-08-26 14:39:15 -0700555 std::deque<StringId> unit_ids_;
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000556
557 std::unordered_map<uint64_t, uint32_t> hash_to_row_idx_;
558 };
559
560 class CounterValues {
561 public:
562 inline uint32_t AddCounterValue(CounterDefinitions::Id counter_id,
563 int64_t timestamp,
564 double value) {
565 counter_ids_.emplace_back(counter_id);
566 timestamps_.emplace_back(timestamp);
567 values_.emplace_back(value);
568 arg_set_ids_.emplace_back(kInvalidArgSetId);
Ioannis Ilkosf129c262019-04-25 14:53:51 +0100569
Lalit Maganti521d97b2019-04-29 13:47:03 +0100570 if (counter_id != CounterDefinitions::kInvalidId) {
571 if (counter_id >= rows_for_counter_id_.size()) {
572 rows_for_counter_id_.resize(counter_id + 1);
573 }
574 rows_for_counter_id_[counter_id].emplace_back(size() - 1);
575 }
576 return size() - 1;
577 }
578
579 void set_counter_id(uint32_t index, CounterDefinitions::Id counter_id) {
580 PERFETTO_DCHECK(counter_ids_[index] == CounterDefinitions::kInvalidId);
581
582 counter_ids_[index] = counter_id;
Ioannis Ilkosf129c262019-04-25 14:53:51 +0100583 if (counter_id >= rows_for_counter_id_.size()) {
584 rows_for_counter_id_.resize(counter_id + 1);
585 }
Lalit Maganti521d97b2019-04-29 13:47:03 +0100586
587 auto* new_rows = &rows_for_counter_id_[counter_id];
588 new_rows->insert(
589 std::upper_bound(new_rows->begin(), new_rows->end(), index), index);
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000590 }
591
592 void set_arg_set_id(uint32_t row, ArgSetId id) { arg_set_ids_[row] = id; }
593
594 uint32_t size() const { return static_cast<uint32_t>(counter_ids_.size()); }
595
596 const std::deque<CounterDefinitions::Id>& counter_ids() const {
597 return counter_ids_;
598 }
599
600 const std::deque<int64_t>& timestamps() const { return timestamps_; }
601
602 const std::deque<double>& values() const { return values_; }
603
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000604 const std::deque<ArgSetId>& arg_set_ids() const { return arg_set_ids_; }
605
Ioannis Ilkosf129c262019-04-25 14:53:51 +0100606 const std::deque<std::vector<uint32_t>>& rows_for_counter_id() const {
607 return rows_for_counter_id_;
608 }
609
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100610 private:
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000611 std::deque<CounterDefinitions::Id> counter_ids_;
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000612 std::deque<int64_t> timestamps_;
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100613 std::deque<double> values_;
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000614 std::deque<ArgSetId> arg_set_ids_;
Ioannis Ilkosf129c262019-04-25 14:53:51 +0100615
616 // Indexed by counter_id value and contains the row numbers corresponding to
617 // it.
618 std::deque<std::vector<uint32_t>> rows_for_counter_id_;
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100619 };
620
Primiano Tucci5cb84f82018-10-31 21:46:36 -0700621 class SqlStats {
622 public:
623 static constexpr size_t kMaxLogEntries = 100;
Lalit Magantiaac2f652019-04-30 12:16:21 +0100624 uint32_t RecordQueryBegin(const std::string& query,
625 int64_t time_queued,
626 int64_t time_started);
627 void RecordQueryFirstNext(uint32_t row, int64_t time_first_next);
628 void RecordQueryEnd(uint32_t row, int64_t time_end);
Primiano Tucci5cb84f82018-10-31 21:46:36 -0700629 size_t size() const { return queries_.size(); }
630 const std::deque<std::string>& queries() const { return queries_; }
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000631 const std::deque<int64_t>& times_queued() const { return times_queued_; }
632 const std::deque<int64_t>& times_started() const { return times_started_; }
Lalit Magantiaac2f652019-04-30 12:16:21 +0100633 const std::deque<int64_t>& times_first_next() const {
634 return times_first_next_;
635 }
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000636 const std::deque<int64_t>& times_ended() const { return times_ended_; }
Primiano Tucci5cb84f82018-10-31 21:46:36 -0700637
638 private:
Lalit Magantiaac2f652019-04-30 12:16:21 +0100639 uint32_t popped_queries_ = 0;
640
Primiano Tucci5cb84f82018-10-31 21:46:36 -0700641 std::deque<std::string> queries_;
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000642 std::deque<int64_t> times_queued_;
643 std::deque<int64_t> times_started_;
Lalit Magantiaac2f652019-04-30 12:16:21 +0100644 std::deque<int64_t> times_first_next_;
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000645 std::deque<int64_t> times_ended_;
Primiano Tucci5cb84f82018-10-31 21:46:36 -0700646 };
647
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000648 class Instants {
649 public:
Lalit Maganti66ed7ad2019-01-11 16:47:26 +0000650 inline uint32_t AddInstantEvent(int64_t timestamp,
651 StringId name_id,
652 double value,
653 int64_t ref,
654 RefType type) {
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000655 timestamps_.emplace_back(timestamp);
656 name_ids_.emplace_back(name_id);
657 values_.emplace_back(value);
658 refs_.emplace_back(ref);
659 types_.emplace_back(type);
Lalit Maganti1c21d172019-02-07 10:48:24 +0000660 arg_set_ids_.emplace_back(kInvalidArgSetId);
Lalit Maganti66ed7ad2019-01-11 16:47:26 +0000661 return static_cast<uint32_t>(instant_count() - 1);
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000662 }
663
Lalit Maganti521d97b2019-04-29 13:47:03 +0100664 void set_ref(uint32_t row, int64_t ref) { refs_[row] = ref; }
665
Lalit Maganti1c21d172019-02-07 10:48:24 +0000666 void set_arg_set_id(uint32_t row, ArgSetId id) { arg_set_ids_[row] = id; }
667
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000668 size_t instant_count() const { return timestamps_.size(); }
669
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000670 const std::deque<int64_t>& timestamps() const { return timestamps_; }
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000671
672 const std::deque<StringId>& name_ids() const { return name_ids_; }
673
674 const std::deque<double>& values() const { return values_; }
675
676 const std::deque<int64_t>& refs() const { return refs_; }
677
678 const std::deque<RefType>& types() const { return types_; }
679
Lalit Maganti1c21d172019-02-07 10:48:24 +0000680 const std::deque<ArgSetId>& arg_set_ids() const { return arg_set_ids_; }
681
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000682 private:
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000683 std::deque<int64_t> timestamps_;
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000684 std::deque<StringId> name_ids_;
685 std::deque<double> values_;
686 std::deque<int64_t> refs_;
687 std::deque<RefType> types_;
Lalit Maganti1c21d172019-02-07 10:48:24 +0000688 std::deque<ArgSetId> arg_set_ids_;
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000689 };
690
Lalit Maganti1d915a62019-01-07 12:10:42 +0000691 class RawEvents {
692 public:
693 inline RowId AddRawEvent(int64_t timestamp,
694 StringId name_id,
Lalit Magantie87cc812019-01-10 15:20:06 +0000695 uint32_t cpu,
Lalit Maganti1d915a62019-01-07 12:10:42 +0000696 UniqueTid utid) {
697 timestamps_.emplace_back(timestamp);
698 name_ids_.emplace_back(name_id);
Lalit Magantie87cc812019-01-10 15:20:06 +0000699 cpus_.emplace_back(cpu);
Lalit Maganti1d915a62019-01-07 12:10:42 +0000700 utids_.emplace_back(utid);
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000701 arg_set_ids_.emplace_back(kInvalidArgSetId);
Lalit Maganti1d915a62019-01-07 12:10:42 +0000702 return CreateRowId(TableId::kRawEvents,
703 static_cast<uint32_t>(raw_event_count() - 1));
704 }
705
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000706 void set_arg_set_id(uint32_t row, ArgSetId id) { arg_set_ids_[row] = id; }
707
Lalit Maganti1d915a62019-01-07 12:10:42 +0000708 size_t raw_event_count() const { return timestamps_.size(); }
709
710 const std::deque<int64_t>& timestamps() const { return timestamps_; }
711
712 const std::deque<StringId>& name_ids() const { return name_ids_; }
713
Lalit Magantie87cc812019-01-10 15:20:06 +0000714 const std::deque<uint32_t>& cpus() const { return cpus_; }
715
Lalit Maganti1d915a62019-01-07 12:10:42 +0000716 const std::deque<UniqueTid>& utids() const { return utids_; }
717
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000718 const std::deque<ArgSetId>& arg_set_ids() const { return arg_set_ids_; }
719
Lalit Maganti1d915a62019-01-07 12:10:42 +0000720 private:
721 std::deque<int64_t> timestamps_;
722 std::deque<StringId> name_ids_;
Lalit Magantie87cc812019-01-10 15:20:06 +0000723 std::deque<uint32_t> cpus_;
Lalit Maganti1d915a62019-01-07 12:10:42 +0000724 std::deque<UniqueTid> utids_;
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000725 std::deque<ArgSetId> arg_set_ids_;
Lalit Maganti1d915a62019-01-07 12:10:42 +0000726 };
727
Primiano Tucci2c761ef2019-01-07 20:20:46 +0000728 class AndroidLogs {
729 public:
730 inline size_t AddLogEvent(int64_t timestamp,
731 UniqueTid utid,
732 uint8_t prio,
733 StringId tag_id,
734 StringId msg_id) {
735 timestamps_.emplace_back(timestamp);
736 utids_.emplace_back(utid);
737 prios_.emplace_back(prio);
738 tag_ids_.emplace_back(tag_id);
739 msg_ids_.emplace_back(msg_id);
740 return size() - 1;
741 }
742
743 size_t size() const { return timestamps_.size(); }
744
745 const std::deque<int64_t>& timestamps() const { return timestamps_; }
746 const std::deque<UniqueTid>& utids() const { return utids_; }
747 const std::deque<uint8_t>& prios() const { return prios_; }
748 const std::deque<StringId>& tag_ids() const { return tag_ids_; }
749 const std::deque<StringId>& msg_ids() const { return msg_ids_; }
750
751 private:
752 std::deque<int64_t> timestamps_;
753 std::deque<UniqueTid> utids_;
754 std::deque<uint8_t> prios_;
755 std::deque<StringId> tag_ids_;
756 std::deque<StringId> msg_ids_;
757 };
758
Primiano Tucci0e38a142019-01-07 20:51:09 +0000759 struct Stats {
760 using IndexMap = std::map<int, int64_t>;
761 int64_t value = 0;
762 IndexMap indexed_values;
763 };
764 using StatsMap = std::array<Stats, stats::kNumKeys>;
765
Ryan Savitski51413ad2019-07-09 14:25:21 +0100766 class Metadata {
767 public:
768 const std::deque<metadata::KeyIDs>& keys() const { return keys_; }
769 const std::deque<Variadic>& values() const { return values_; }
770
771 RowId SetScalarMetadata(metadata::KeyIDs key, Variadic value) {
772 PERFETTO_DCHECK(key < metadata::kNumKeys);
773 PERFETTO_DCHECK(metadata::kKeyTypes[key] == metadata::kSingle);
774 PERFETTO_DCHECK(value.type == metadata::kValueTypes[key]);
775
776 // Already set - on release builds, overwrite the previous value.
777 auto it = scalar_indices.find(key);
778 if (it != scalar_indices.end()) {
779 PERFETTO_DFATAL("Setting a scalar metadata entry more than once.");
780 uint32_t index = static_cast<uint32_t>(it->second);
781 values_[index] = value;
782 return TraceStorage::CreateRowId(kMetadataTable, index);
783 }
784 // First time setting this key.
785 keys_.push_back(key);
786 values_.push_back(value);
787 uint32_t index = static_cast<uint32_t>(keys_.size() - 1);
788 scalar_indices[key] = index;
789 return TraceStorage::CreateRowId(kMetadataTable, index);
790 }
791
792 RowId AppendMetadata(metadata::KeyIDs key, Variadic value) {
793 PERFETTO_DCHECK(key < metadata::kNumKeys);
794 PERFETTO_DCHECK(metadata::kKeyTypes[key] == metadata::kMulti);
795 PERFETTO_DCHECK(value.type == metadata::kValueTypes[key]);
796
797 keys_.push_back(key);
798 values_.push_back(value);
799 uint32_t index = static_cast<uint32_t>(keys_.size() - 1);
800 return TraceStorage::CreateRowId(kMetadataTable, index);
801 }
802
Ryan Savitski0476ee92019-07-09 14:29:33 +0100803 void OverwriteMetadata(uint32_t index, Variadic value) {
804 PERFETTO_DCHECK(index < values_.size());
805 values_[index] = value;
806 }
807
Ryan Savitski51413ad2019-07-09 14:25:21 +0100808 private:
809 std::deque<metadata::KeyIDs> keys_;
810 std::deque<Variadic> values_;
811 // Extraneous state to track locations of entries that should have at most
812 // one row. Used only to maintain uniqueness during insertions.
813 std::map<metadata::KeyIDs, uint32_t> scalar_indices;
814 };
Mikhail Khokhlove466c002019-05-23 13:33:33 +0100815
Oystein Eftevaag5419c582019-08-21 13:58:49 -0700816 class StackProfileFrames {
Florian Mayer438b5ab2019-05-02 11:18:06 +0100817 public:
Florian Mayerbee52132019-05-02 13:59:56 +0100818 struct Row {
819 StringId name_id;
820 int64_t mapping_row;
821 int64_t rel_pc;
Florian Mayer438b5ab2019-05-02 11:18:06 +0100822
Florian Mayerbee52132019-05-02 13:59:56 +0100823 bool operator==(const Row& other) const {
824 return std::tie(name_id, mapping_row, rel_pc) ==
825 std::tie(other.name_id, other.mapping_row, other.rel_pc);
826 }
827 };
828
Lalit Maganti9b2d52b2019-05-07 14:32:15 +0100829 uint32_t size() const { return static_cast<uint32_t>(names_.size()); }
830
Florian Mayerbee52132019-05-02 13:59:56 +0100831 int64_t Insert(const Row& row) {
832 names_.emplace_back(row.name_id);
833 mappings_.emplace_back(row.mapping_row);
834 rel_pcs_.emplace_back(row.rel_pc);
Ioannis Ilkose0b47f52019-09-18 11:14:57 +0100835 symbol_set_ids_.emplace_back(0);
Florian Mayera480cd62019-09-24 10:36:56 +0100836 size_t row_number = names_.size() - 1;
837 index_.emplace(std::make_pair(row.mapping_row, row.rel_pc), row_number);
838 return static_cast<int64_t>(row_number);
839 }
840
841 ssize_t FindFrameRow(size_t mapping_row, uint64_t rel_pc) const {
842 auto it = index_.find(std::make_pair(mapping_row, rel_pc));
843 if (it == index_.end())
844 return -1;
845 return static_cast<ssize_t>(it->second);
Florian Mayer438b5ab2019-05-02 11:18:06 +0100846 }
847
Ioannis Ilkose0b47f52019-09-18 11:14:57 +0100848 void SetSymbolSetId(size_t row_idx, uint32_t symbol_set_id) {
849 PERFETTO_CHECK(row_idx < symbol_set_ids_.size());
850 symbol_set_ids_[row_idx] = symbol_set_id;
Ioannis Ilkose6727552019-08-14 15:10:59 +0100851 }
852
Florian Mayer438b5ab2019-05-02 11:18:06 +0100853 const std::deque<StringId>& names() const { return names_; }
854 const std::deque<int64_t>& mappings() const { return mappings_; }
855 const std::deque<int64_t>& rel_pcs() const { return rel_pcs_; }
Ioannis Ilkose0b47f52019-09-18 11:14:57 +0100856 const std::deque<uint32_t>& symbol_set_ids() const {
857 return symbol_set_ids_;
858 }
Florian Mayer438b5ab2019-05-02 11:18:06 +0100859
860 private:
861 std::deque<StringId> names_;
862 std::deque<int64_t> mappings_;
863 std::deque<int64_t> rel_pcs_;
Ioannis Ilkose0b47f52019-09-18 11:14:57 +0100864 std::deque<uint32_t> symbol_set_ids_;
Florian Mayera480cd62019-09-24 10:36:56 +0100865
866 std::map<std::pair<size_t /* mapping row */, uint64_t /* rel_pc */>, size_t>
867 index_;
Florian Mayer438b5ab2019-05-02 11:18:06 +0100868 };
869
Oystein Eftevaag5419c582019-08-21 13:58:49 -0700870 class StackProfileCallsites {
Florian Mayer438b5ab2019-05-02 11:18:06 +0100871 public:
Florian Mayerbee52132019-05-02 13:59:56 +0100872 struct Row {
873 int64_t depth;
874 int64_t parent_id;
875 int64_t frame_row;
876
877 bool operator==(const Row& other) const {
878 return std::tie(depth, parent_id, frame_row) ==
879 std::tie(other.depth, other.parent_id, other.frame_row);
Florian Mayer438b5ab2019-05-02 11:18:06 +0100880 }
Florian Mayerbee52132019-05-02 13:59:56 +0100881 };
882
Lalit Maganti9b2d52b2019-05-07 14:32:15 +0100883 uint32_t size() const { return static_cast<uint32_t>(frame_ids_.size()); }
884
Florian Mayerbee52132019-05-02 13:59:56 +0100885 int64_t Insert(const Row& row) {
886 frame_depths_.emplace_back(row.depth);
887 parent_callsite_ids_.emplace_back(row.parent_id);
888 frame_ids_.emplace_back(row.frame_row);
Florian Mayer438b5ab2019-05-02 11:18:06 +0100889 return static_cast<int64_t>(frame_depths_.size()) - 1;
890 }
891
892 const std::deque<int64_t>& frame_depths() const { return frame_depths_; }
893 const std::deque<int64_t>& parent_callsite_ids() const {
894 return parent_callsite_ids_;
895 }
896 const std::deque<int64_t>& frame_ids() const { return frame_ids_; }
897
898 private:
899 std::deque<int64_t> frame_depths_;
900 std::deque<int64_t> parent_callsite_ids_;
901 std::deque<int64_t> frame_ids_;
902 };
903
Oystein Eftevaag5419c582019-08-21 13:58:49 -0700904 class StackProfileMappings {
Florian Mayer438b5ab2019-05-02 11:18:06 +0100905 public:
Florian Mayerbee52132019-05-02 13:59:56 +0100906 struct Row {
907 StringId build_id;
Florian Mayer12655732019-07-02 15:08:26 +0100908 int64_t exact_offset;
909 int64_t start_offset;
Florian Mayerbee52132019-05-02 13:59:56 +0100910 int64_t start;
911 int64_t end;
912 int64_t load_bias;
913 StringId name_id;
Florian Mayer438b5ab2019-05-02 11:18:06 +0100914
Florian Mayerbee52132019-05-02 13:59:56 +0100915 bool operator==(const Row& other) const {
Florian Mayer12655732019-07-02 15:08:26 +0100916 return std::tie(build_id, exact_offset, start_offset, start, end,
917 load_bias, name_id) ==
918 std::tie(other.build_id, other.exact_offset, other.start_offset,
919 other.start, other.end, other.load_bias, other.name_id);
Florian Mayerbee52132019-05-02 13:59:56 +0100920 }
921 };
922
Lalit Maganti9b2d52b2019-05-07 14:32:15 +0100923 uint32_t size() const { return static_cast<uint32_t>(names_.size()); }
924
Florian Mayerbee52132019-05-02 13:59:56 +0100925 int64_t Insert(const Row& row) {
926 build_ids_.emplace_back(row.build_id);
Florian Mayer12655732019-07-02 15:08:26 +0100927 exact_offsets_.emplace_back(row.exact_offset);
928 start_offsets_.emplace_back(row.start_offset);
Florian Mayerbee52132019-05-02 13:59:56 +0100929 starts_.emplace_back(row.start);
930 ends_.emplace_back(row.end);
931 load_biases_.emplace_back(row.load_bias);
932 names_.emplace_back(row.name_id);
Florian Mayera480cd62019-09-24 10:36:56 +0100933
934 size_t row_number = build_ids_.size() - 1;
935 index_.emplace(std::make_pair(row.name_id, row.build_id), row_number);
936 return static_cast<int64_t>(row_number);
937 }
938
939 ssize_t FindMappingRow(StringId name, StringId build_id) const {
940 auto it = index_.find(std::make_pair(name, build_id));
941 if (it == index_.end())
942 return -1;
943 return static_cast<ssize_t>(it->second);
Florian Mayer438b5ab2019-05-02 11:18:06 +0100944 }
945
946 const std::deque<StringId>& build_ids() const { return build_ids_; }
Florian Mayer12655732019-07-02 15:08:26 +0100947 const std::deque<int64_t>& exact_offsets() const { return exact_offsets_; }
948 const std::deque<int64_t>& start_offsets() const { return start_offsets_; }
Florian Mayer438b5ab2019-05-02 11:18:06 +0100949 const std::deque<int64_t>& starts() const { return starts_; }
950 const std::deque<int64_t>& ends() const { return ends_; }
951 const std::deque<int64_t>& load_biases() const { return load_biases_; }
952 const std::deque<StringId>& names() const { return names_; }
953
954 private:
955 std::deque<StringId> build_ids_;
Florian Mayer12655732019-07-02 15:08:26 +0100956 std::deque<int64_t> exact_offsets_;
957 std::deque<int64_t> start_offsets_;
Florian Mayer438b5ab2019-05-02 11:18:06 +0100958 std::deque<int64_t> starts_;
959 std::deque<int64_t> ends_;
960 std::deque<int64_t> load_biases_;
961 std::deque<StringId> names_;
Florian Mayera480cd62019-09-24 10:36:56 +0100962
963 std::map<std::pair<StringId /* name */, StringId /* build id */>, size_t>
964 index_;
Florian Mayer438b5ab2019-05-02 11:18:06 +0100965 };
966
967 class HeapProfileAllocations {
968 public:
Florian Mayerbee52132019-05-02 13:59:56 +0100969 struct Row {
970 int64_t timestamp;
Florian Mayerdf1968c2019-05-13 16:39:35 +0100971 UniquePid upid;
Florian Mayerbee52132019-05-02 13:59:56 +0100972 int64_t callsite_id;
973 int64_t count;
974 int64_t size;
975 };
976
Lalit Maganti9b2d52b2019-05-07 14:32:15 +0100977 uint32_t size() const { return static_cast<uint32_t>(timestamps_.size()); }
978
Florian Mayerbee52132019-05-02 13:59:56 +0100979 void Insert(const Row& row) {
980 timestamps_.emplace_back(row.timestamp);
Florian Mayerdf1968c2019-05-13 16:39:35 +0100981 upids_.emplace_back(row.upid);
Florian Mayerbee52132019-05-02 13:59:56 +0100982 callsite_ids_.emplace_back(row.callsite_id);
983 counts_.emplace_back(row.count);
984 sizes_.emplace_back(row.size);
Florian Mayer438b5ab2019-05-02 11:18:06 +0100985 }
986
987 const std::deque<int64_t>& timestamps() const { return timestamps_; }
Florian Mayerdf1968c2019-05-13 16:39:35 +0100988 const std::deque<UniquePid>& upids() const { return upids_; }
Florian Mayer438b5ab2019-05-02 11:18:06 +0100989 const std::deque<int64_t>& callsite_ids() const { return callsite_ids_; }
990 const std::deque<int64_t>& counts() const { return counts_; }
991 const std::deque<int64_t>& sizes() const { return sizes_; }
992
993 private:
994 std::deque<int64_t> timestamps_;
Florian Mayerdf1968c2019-05-13 16:39:35 +0100995 std::deque<UniquePid> upids_;
Florian Mayer438b5ab2019-05-02 11:18:06 +0100996 std::deque<int64_t> callsite_ids_;
997 std::deque<int64_t> counts_;
998 std::deque<int64_t> sizes_;
999 };
1000
Oystein Eftevaag7f64c102019-08-29 10:27:31 -07001001 class CpuProfileStackSamples {
1002 public:
1003 struct Row {
1004 int64_t timestamp;
1005 int64_t callsite_id;
1006 UniqueTid utid;
1007 };
1008
1009 uint32_t size() const { return static_cast<uint32_t>(timestamps_.size()); }
1010
1011 void Insert(const Row& row) {
1012 timestamps_.emplace_back(row.timestamp);
1013 callsite_ids_.emplace_back(row.callsite_id);
1014 utids_.emplace_back(row.utid);
1015 }
1016
1017 const std::deque<int64_t>& timestamps() const { return timestamps_; }
1018 const std::deque<int64_t>& callsite_ids() const { return callsite_ids_; }
1019 const std::deque<UniqueTid>& utids() const { return utids_; }
1020
1021 private:
1022 std::deque<int64_t> timestamps_;
1023 std::deque<int64_t> callsite_ids_;
1024 std::deque<UniqueTid> utids_;
1025 };
1026
Primiano Tuccib75dcee2018-08-08 12:21:36 +01001027 UniqueTid AddEmptyThread(uint32_t tid) {
1028 unique_threads_.emplace_back(tid);
Isabelle Taylora0a22972018-08-03 12:06:12 +01001029 return static_cast<UniqueTid>(unique_threads_.size() - 1);
1030 }
Isabelle Taylor47328cf2018-06-12 14:33:59 +01001031
Primiano Tuccib75dcee2018-08-08 12:21:36 +01001032 UniquePid AddEmptyProcess(uint32_t pid) {
1033 unique_processes_.emplace_back(pid);
Isabelle Taylora0a22972018-08-03 12:06:12 +01001034 return static_cast<UniquePid>(unique_processes_.size() - 1);
1035 }
Isabelle Taylor3dd366c2018-06-22 16:21:41 +01001036
Isabelle Taylora0a22972018-08-03 12:06:12 +01001037 // Return an unqiue identifier for the contents of each string.
1038 // The string is copied internally and can be destroyed after this called.
Isabelle Taylor15314ea2018-09-19 11:35:19 +01001039 // Virtual for testing.
Lalit Maganti5c454312019-04-08 12:11:17 +01001040 virtual StringId InternString(base::StringView str) {
1041 return string_pool_.InternString(str);
1042 }
Isabelle Taylor47328cf2018-06-12 14:33:59 +01001043
Isabelle Taylora0a22972018-08-03 12:06:12 +01001044 Process* GetMutableProcess(UniquePid upid) {
Lalit Maganti676658b2018-11-20 18:24:31 +00001045 PERFETTO_DCHECK(upid < unique_processes_.size());
Isabelle Taylora0a22972018-08-03 12:06:12 +01001046 return &unique_processes_[upid];
1047 }
Isabelle Taylor3dd366c2018-06-22 16:21:41 +01001048
Isabelle Taylora0a22972018-08-03 12:06:12 +01001049 Thread* GetMutableThread(UniqueTid utid) {
Florian Mayera1aaec22018-09-19 17:02:26 +01001050 PERFETTO_DCHECK(utid < unique_threads_.size());
Isabelle Taylora0a22972018-08-03 12:06:12 +01001051 return &unique_threads_[utid];
1052 }
Isabelle Taylor3dd366c2018-06-22 16:21:41 +01001053
Primiano Tucci0e38a142019-01-07 20:51:09 +00001054 // Example usage: SetStats(stats::android_log_num_failed, 42);
1055 void SetStats(size_t key, int64_t value) {
1056 PERFETTO_DCHECK(key < stats::kNumKeys);
1057 PERFETTO_DCHECK(stats::kTypes[key] == stats::kSingle);
1058 stats_[key].value = value;
1059 }
1060
1061 // Example usage: IncrementStats(stats::android_log_num_failed, -1);
1062 void IncrementStats(size_t key, int64_t increment = 1) {
1063 PERFETTO_DCHECK(key < stats::kNumKeys);
1064 PERFETTO_DCHECK(stats::kTypes[key] == stats::kSingle);
1065 stats_[key].value += increment;
1066 }
1067
Florian Mayer438b5ab2019-05-02 11:18:06 +01001068 // Example usage: IncrementIndexedStats(stats::cpu_failure, 1);
1069 void IncrementIndexedStats(size_t key, int index, int64_t increment = 1) {
1070 PERFETTO_DCHECK(key < stats::kNumKeys);
1071 PERFETTO_DCHECK(stats::kTypes[key] == stats::kIndexed);
1072 stats_[key].indexed_values[index] += increment;
1073 }
1074
Primiano Tucci0e38a142019-01-07 20:51:09 +00001075 // Example usage: SetIndexedStats(stats::cpu_failure, 1, 42);
1076 void SetIndexedStats(size_t key, int index, int64_t value) {
1077 PERFETTO_DCHECK(key < stats::kNumKeys);
1078 PERFETTO_DCHECK(stats::kTypes[key] == stats::kIndexed);
1079 stats_[key].indexed_values[index] = value;
1080 }
1081
Mikhail Khokhlove466c002019-05-23 13:33:33 +01001082 // Example usage:
1083 // SetMetadata(metadata::benchmark_name,
1084 // Variadic::String(storage->InternString("foo"));
Ryan Savitski51413ad2019-07-09 14:25:21 +01001085 // Returns the RowId of the new entry.
Mikhail Khokhlovb1fe42a2019-05-23 13:58:05 +01001086 // Virtual for testing.
Ryan Savitski51413ad2019-07-09 14:25:21 +01001087 virtual RowId SetMetadata(metadata::KeyIDs key, Variadic value) {
1088 return metadata_.SetScalarMetadata(key, value);
Mikhail Khokhlove466c002019-05-23 13:33:33 +01001089 }
1090
1091 // Example usage:
1092 // AppendMetadata(metadata::benchmark_story_tags,
1093 // Variadic::String(storage->InternString("bar"));
Ryan Savitski51413ad2019-07-09 14:25:21 +01001094 // Returns the RowId of the new entry.
Mikhail Khokhlovb1fe42a2019-05-23 13:58:05 +01001095 // Virtual for testing.
Ryan Savitski51413ad2019-07-09 14:25:21 +01001096 virtual RowId AppendMetadata(metadata::KeyIDs key, Variadic value) {
1097 return metadata_.AppendMetadata(key, value);
Mikhail Khokhlove466c002019-05-23 13:33:33 +01001098 }
1099
Eric Seckler77b52782019-05-02 15:18:57 +01001100 class ScopedStatsTracer {
1101 public:
1102 ScopedStatsTracer(TraceStorage* storage, size_t key)
1103 : storage_(storage), key_(key), start_ns_(base::GetWallTimeNs()) {}
1104
1105 ~ScopedStatsTracer() {
1106 if (!storage_)
1107 return;
1108 auto delta_ns = base::GetWallTimeNs() - start_ns_;
1109 storage_->IncrementStats(key_, delta_ns.count());
1110 }
1111
1112 ScopedStatsTracer(ScopedStatsTracer&& other) noexcept { MoveImpl(&other); }
1113
1114 ScopedStatsTracer& operator=(ScopedStatsTracer&& other) {
1115 MoveImpl(&other);
1116 return *this;
1117 }
1118
1119 private:
1120 ScopedStatsTracer(const ScopedStatsTracer&) = delete;
1121 ScopedStatsTracer& operator=(const ScopedStatsTracer&) = delete;
1122
1123 void MoveImpl(ScopedStatsTracer* other) {
1124 storage_ = other->storage_;
1125 key_ = other->key_;
1126 start_ns_ = other->start_ns_;
1127 other->storage_ = nullptr;
1128 }
1129
1130 TraceStorage* storage_;
1131 size_t key_;
1132 base::TimeNanos start_ns_;
1133 };
1134
1135 ScopedStatsTracer TraceExecutionTimeIntoStats(size_t key) {
1136 return ScopedStatsTracer(this, key);
1137 }
1138
Lalit Maganticaed37e2018-06-01 03:03:08 +01001139 // Reading methods.
Eric Secklerc93823e2019-06-03 16:49:19 +01001140 // Virtual for testing.
1141 virtual NullTermStringView GetString(StringId id) const {
Lalit Maganti5c454312019-04-08 12:11:17 +01001142 return string_pool_.Get(id);
Isabelle Taylora0a22972018-08-03 12:06:12 +01001143 }
1144
Lalit Magantie9d40532018-06-29 13:15:06 +01001145 const Process& GetProcess(UniquePid upid) const {
Lalit Maganti676658b2018-11-20 18:24:31 +00001146 PERFETTO_DCHECK(upid < unique_processes_.size());
Isabelle Taylor47328cf2018-06-12 14:33:59 +01001147 return unique_processes_[upid];
1148 }
1149
Lalit Magantie9d40532018-06-29 13:15:06 +01001150 const Thread& GetThread(UniqueTid utid) const {
Lalit Maganti1f64cfa2018-09-18 13:20:02 +01001151 // Allow utid == 0 for idle thread retrieval.
Florian Mayera1aaec22018-09-19 17:02:26 +01001152 PERFETTO_DCHECK(utid < unique_threads_.size());
Isabelle Taylor68e42192018-06-19 16:19:31 +01001153 return unique_threads_[utid];
1154 }
1155
Lalit Maganti5ea9e932018-11-30 14:19:39 +00001156 static RowId CreateRowId(TableId table, uint32_t row) {
Lalit Maganti85ca4a82018-12-07 17:28:02 +00001157 return (static_cast<RowId>(table) << kRowIdTableShift) | row;
Lalit Maganti5ea9e932018-11-30 14:19:39 +00001158 }
1159
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +00001160 static std::pair<int8_t /*table*/, uint32_t /*row*/> ParseRowId(RowId rowid) {
1161 auto id = static_cast<uint64_t>(rowid);
1162 auto table_id = static_cast<uint8_t>(id >> kRowIdTableShift);
1163 auto row = static_cast<uint32_t>(id & ((1ull << kRowIdTableShift) - 1));
1164 return std::make_pair(table_id, row);
1165 }
1166
Lalit Magantiac68e9c2019-09-09 18:12:15 +01001167 const tables::TrackTable& track_table() const { return track_table_; }
1168 tables::TrackTable* mutable_track_table() { return &track_table_; }
Lalit Magantid11d3e02019-07-26 12:32:09 +05301169
Lalit Maganti53bdbb22019-09-25 11:11:18 +01001170 const tables::ProcessTrackTable& process_track_table() const {
1171 return process_track_table_;
Lalit Maganti4ea78d92019-09-20 20:20:41 +01001172 }
Lalit Maganti53bdbb22019-09-25 11:11:18 +01001173 tables::ProcessTrackTable* mutable_process_track_table() {
1174 return &process_track_table_;
Lalit Maganti4ea78d92019-09-20 20:20:41 +01001175 }
Eric Seckler5703ede2019-07-10 10:13:02 +01001176
Lalit Magantifedc15b2019-09-26 18:37:52 +01001177 const tables::ThreadTrackTable& thread_track_table() const {
1178 return thread_track_table_;
1179 }
1180 tables::ThreadTrackTable* mutable_thread_track_table() {
1181 return &thread_track_table_;
1182 }
1183
Lalit Magantiff69c112018-09-24 12:07:47 +01001184 const Slices& slices() const { return slices_; }
Lalit Magantifde29042018-10-04 13:28:52 +01001185 Slices* mutable_slices() { return &slices_; }
1186
Primiano Tucci0d72a312018-08-07 14:42:45 +01001187 const NestableSlices& nestable_slices() const { return nestable_slices_; }
1188 NestableSlices* mutable_nestable_slices() { return &nestable_slices_; }
1189
Eric Secklerd3b89d52019-07-10 15:36:29 +01001190 const ThreadSlices& thread_slices() const { return thread_slices_; }
1191 ThreadSlices* mutable_thread_slices() { return &thread_slices_; }
1192
Eric Secklerc3430c62019-07-22 10:49:58 +01001193 const VirtualTrackSlices& virtual_track_slices() const {
1194 return virtual_track_slices_;
1195 }
1196 VirtualTrackSlices* mutable_virtual_track_slices() {
1197 return &virtual_track_slices_;
1198 }
1199
Lalit Maganti20539c22019-09-04 12:36:10 +01001200 const tables::GpuSliceTable& gpu_slice_table() const {
1201 return gpu_slice_table_;
1202 }
1203 tables::GpuSliceTable* mutable_gpu_slice_table() { return &gpu_slice_table_; }
Mikael Pessa7160ccc2019-07-25 11:19:26 -07001204
Lalit Maganti8320e6d2019-03-14 18:49:33 +00001205 const CounterDefinitions& counter_definitions() const {
1206 return counter_definitions_;
1207 }
1208 CounterDefinitions* mutable_counter_definitions() {
1209 return &counter_definitions_;
1210 }
1211
1212 const CounterValues& counter_values() const { return counter_values_; }
1213 CounterValues* mutable_counter_values() { return &counter_values_; }
Isabelle Taylor14674d42018-09-07 11:33:11 +01001214
Primiano Tucci5cb84f82018-10-31 21:46:36 -07001215 const SqlStats& sql_stats() const { return sql_stats_; }
1216 SqlStats* mutable_sql_stats() { return &sql_stats_; }
1217
Isabelle Taylorc8c11202018-11-05 11:36:22 +00001218 const Instants& instants() const { return instants_; }
1219 Instants* mutable_instants() { return &instants_; }
1220
Primiano Tucci2c761ef2019-01-07 20:20:46 +00001221 const AndroidLogs& android_logs() const { return android_log_; }
1222 AndroidLogs* mutable_android_log() { return &android_log_; }
1223
Primiano Tucci0e38a142019-01-07 20:51:09 +00001224 const StatsMap& stats() const { return stats_; }
Lalit Maganti05e8c132018-11-09 18:16:12 +00001225
Ryan Savitski51413ad2019-07-09 14:25:21 +01001226 const Metadata& metadata() const { return metadata_; }
Ryan Savitski0476ee92019-07-09 14:29:33 +01001227 Metadata* mutable_metadata() { return &metadata_; }
Mikhail Khokhlove466c002019-05-23 13:33:33 +01001228
Lalit Maganti6e9c55e2018-11-29 12:00:39 +00001229 const Args& args() const { return args_; }
1230 Args* mutable_args() { return &args_; }
1231
Lalit Maganti1d915a62019-01-07 12:10:42 +00001232 const RawEvents& raw_events() const { return raw_events_; }
1233 RawEvents* mutable_raw_events() { return &raw_events_; }
1234
Oystein Eftevaag5419c582019-08-21 13:58:49 -07001235 const StackProfileMappings& stack_profile_mappings() const {
1236 return stack_profile_mappings_;
Florian Mayer438b5ab2019-05-02 11:18:06 +01001237 }
Oystein Eftevaag5419c582019-08-21 13:58:49 -07001238 StackProfileMappings* mutable_stack_profile_mappings() {
1239 return &stack_profile_mappings_;
Florian Mayer438b5ab2019-05-02 11:18:06 +01001240 }
1241
Oystein Eftevaag5419c582019-08-21 13:58:49 -07001242 const StackProfileFrames& stack_profile_frames() const {
1243 return stack_profile_frames_;
Florian Mayer438b5ab2019-05-02 11:18:06 +01001244 }
Oystein Eftevaag5419c582019-08-21 13:58:49 -07001245 StackProfileFrames* mutable_stack_profile_frames() {
1246 return &stack_profile_frames_;
Florian Mayer438b5ab2019-05-02 11:18:06 +01001247 }
1248
Oystein Eftevaag5419c582019-08-21 13:58:49 -07001249 const StackProfileCallsites& stack_profile_callsites() const {
1250 return stack_profile_callsites_;
Florian Mayer438b5ab2019-05-02 11:18:06 +01001251 }
Oystein Eftevaag5419c582019-08-21 13:58:49 -07001252 StackProfileCallsites* mutable_stack_profile_callsites() {
1253 return &stack_profile_callsites_;
Florian Mayer438b5ab2019-05-02 11:18:06 +01001254 }
1255
1256 const HeapProfileAllocations& heap_profile_allocations() const {
1257 return heap_profile_allocations_;
1258 }
1259 HeapProfileAllocations* mutable_heap_profile_allocations() {
1260 return &heap_profile_allocations_;
1261 }
Oystein Eftevaag7f64c102019-08-29 10:27:31 -07001262 const CpuProfileStackSamples& cpu_profile_stack_samples() const {
1263 return cpu_profile_stack_samples_;
1264 }
1265 CpuProfileStackSamples* mutable_cpu_profile_stack_samples() {
1266 return &cpu_profile_stack_samples_;
1267 }
Florian Mayer438b5ab2019-05-02 11:18:06 +01001268
Ioannis Ilkose0b47f52019-09-18 11:14:57 +01001269 const tables::SymbolTable& symbol_table() const { return symbol_table_; }
1270
1271 tables::SymbolTable* mutable_symbol_table() { return &symbol_table_; }
1272
Lalit Maganti20539c22019-09-04 12:36:10 +01001273 const tables::GpuTrackTable& gpu_track_table() const {
1274 return gpu_track_table_;
1275 }
1276 tables::GpuTrackTable* mutable_gpu_track_table() { return &gpu_track_table_; }
Mikael Pessa7160ccc2019-07-25 11:19:26 -07001277
Lalit Maganti5c454312019-04-08 12:11:17 +01001278 const StringPool& string_pool() const { return string_pool_; }
Lalit Magantiacda68b2018-10-29 15:23:25 +00001279
Hector Dearman5145e502019-09-18 16:52:24 +01001280 // |unique_processes_| always contains at least 1 element because the 0th ID
Isabelle Taylore7003fb2018-07-17 11:39:01 +01001281 // is reserved to indicate an invalid process.
Lalit Maganti9c6395b2019-01-17 00:25:09 +00001282 size_t process_count() const { return unique_processes_.size(); }
Primiano Tucci0d72a312018-08-07 14:42:45 +01001283
Hector Dearman5145e502019-09-18 16:52:24 +01001284 // |unique_threads_| always contains at least 1 element because the 0th ID
Isabelle Taylore7003fb2018-07-17 11:39:01 +01001285 // is reserved to indicate an invalid thread.
Lalit Maganti9c6395b2019-01-17 00:25:09 +00001286 size_t thread_count() const { return unique_threads_.size(); }
Isabelle Taylore7003fb2018-07-17 11:39:01 +01001287
Hector Dearman12323362018-08-09 16:09:28 +01001288 // Number of interned strings in the pool. Includes the empty string w/ ID=0.
1289 size_t string_count() const { return string_pool_.size(); }
1290
Ioannis Ilkosb8b11102019-01-29 17:56:55 +00001291 // Start / end ts (in nanoseconds) across the parsed trace events.
1292 // Returns (0, 0) if the trace is empty.
1293 std::pair<int64_t, int64_t> GetTraceTimestampBoundsNs() const;
1294
Lalit Maganticaed37e2018-06-01 03:03:08 +01001295 private:
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +00001296 static constexpr uint8_t kRowIdTableShift = 32;
Isabelle Taylora0a22972018-08-03 12:06:12 +01001297
Primiano Tucci2da5d2e2018-08-10 14:23:31 +01001298 using StringHash = uint64_t;
Lalit Maganticaed37e2018-06-01 03:03:08 +01001299
Lalit Maganti5c454312019-04-08 12:11:17 +01001300 TraceStorage(const TraceStorage&) = delete;
1301 TraceStorage& operator=(const TraceStorage&) = delete;
1302
Lalit Maganti20539c22019-09-04 12:36:10 +01001303 TraceStorage(TraceStorage&&) = delete;
1304 TraceStorage& operator=(TraceStorage&&) = delete;
1305
1306 // One entry for each unique string in the trace.
1307 StringPool string_pool_;
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +00001308
Lalit Maganti05e8c132018-11-09 18:16:12 +00001309 // Stats about parsing the trace.
Primiano Tucci0e38a142019-01-07 20:51:09 +00001310 StatsMap stats_{};
Lalit Maganti35622b72018-06-06 12:03:11 +01001311
Ryan Savitski51413ad2019-07-09 14:25:21 +01001312 // Extra data extracted from the trace. Includes:
1313 // * metadata from chrome and benchmarking infrastructure
1314 // * descriptions of android packages
1315 Metadata metadata_{};
Mikhail Khokhlove466c002019-05-23 13:33:33 +01001316
Lalit Magantid11d3e02019-07-26 12:32:09 +05301317 // Metadata for tracks.
Lalit Magantiac68e9c2019-09-09 18:12:15 +01001318 tables::TrackTable track_table_{&string_pool_, nullptr};
Lalit Maganti4ea78d92019-09-20 20:20:41 +01001319 tables::GpuTrackTable gpu_track_table_{&string_pool_, &track_table_};
Lalit Maganti53bdbb22019-09-25 11:11:18 +01001320 tables::ProcessTrackTable process_track_table_{&string_pool_, &track_table_};
Lalit Magantifedc15b2019-09-26 18:37:52 +01001321 tables::ThreadTrackTable thread_track_table_{&string_pool_, &track_table_};
Eric Seckler5703ede2019-07-10 10:13:02 +01001322
Mikael Pessa803090c2019-08-23 16:03:34 -07001323 // Metadata for gpu tracks.
Mikael Pessa803090c2019-08-23 16:03:34 -07001324 GpuContexts gpu_contexts_;
1325
Lalit Maganticaed37e2018-06-01 03:03:08 +01001326 // One entry for each CPU in the trace.
Lalit Magantiff69c112018-09-24 12:07:47 +01001327 Slices slices_;
Lalit Maganticaed37e2018-06-01 03:03:08 +01001328
Lalit Maganti6e9c55e2018-11-29 12:00:39 +00001329 // Args for all other tables.
1330 Args args_;
1331
Isabelle Taylor47328cf2018-06-12 14:33:59 +01001332 // One entry for each UniquePid, with UniquePid as the index.
Ioannis Ilkos9c03cbd2019-03-12 18:30:43 +00001333 // Never hold on to pointers to Process, as vector resize will
1334 // invalidate them.
1335 std::vector<Process> unique_processes_;
Isabelle Taylor68e42192018-06-19 16:19:31 +01001336
Isabelle Taylor68e42192018-06-19 16:19:31 +01001337 // One entry for each UniqueTid, with UniqueTid as the index.
Isabelle Taylor3dd366c2018-06-22 16:21:41 +01001338 std::deque<Thread> unique_threads_;
Primiano Tucci0d72a312018-08-07 14:42:45 +01001339
1340 // Slices coming from userspace events (e.g. Chromium TRACE_EVENT macros).
1341 NestableSlices nestable_slices_;
Isabelle Taylor15314ea2018-09-19 11:35:19 +01001342
Eric Secklerd3b89d52019-07-10 15:36:29 +01001343 // Additional attributes for threads slices (sub-type of NestableSlices).
1344 ThreadSlices thread_slices_;
1345
Eric Secklerc3430c62019-07-22 10:49:58 +01001346 // Additional attributes for virtual track slices (sub-type of
1347 // NestableSlices).
1348 VirtualTrackSlices virtual_track_slices_;
1349
Mikael Pessa803090c2019-08-23 16:03:34 -07001350 // Additional attributes for gpu track slices (sub-type of
1351 // NestableSlices).
Lalit Maganti20539c22019-09-04 12:36:10 +01001352 tables::GpuSliceTable gpu_slice_table_{&string_pool_, nullptr};
Mikael Pessa803090c2019-08-23 16:03:34 -07001353
Hector Dearman2442d612019-06-04 18:05:23 +01001354 // The type of counters in the trace. Can be thought of as the "metadata".
Lalit Maganti8320e6d2019-03-14 18:49:33 +00001355 CounterDefinitions counter_definitions_;
1356
1357 // The values from the Counter events from the trace. This includes CPU
1358 // frequency events as well systrace trace_marker counter events.
1359 CounterValues counter_values_;
Primiano Tucci5cb84f82018-10-31 21:46:36 -07001360
1361 SqlStats sql_stats_;
Lalit Maganti6e9c55e2018-11-29 12:00:39 +00001362
Isabelle Taylorc8c11202018-11-05 11:36:22 +00001363 // These are instantaneous events in the trace. They have no duration
1364 // and do not have a value that make sense to track over time.
1365 // e.g. signal events
1366 Instants instants_;
Lalit Maganti1d915a62019-01-07 12:10:42 +00001367
1368 // Raw events are every ftrace event in the trace. The raw event includes
1369 // the timestamp and the pid. The args for the raw event will be in the
1370 // args table. This table can be used to generate a text version of the
1371 // trace.
1372 RawEvents raw_events_;
Primiano Tucci2c761ef2019-01-07 20:20:46 +00001373 AndroidLogs android_log_;
Florian Mayer438b5ab2019-05-02 11:18:06 +01001374
Oystein Eftevaag5419c582019-08-21 13:58:49 -07001375 StackProfileMappings stack_profile_mappings_;
1376 StackProfileFrames stack_profile_frames_;
1377 StackProfileCallsites stack_profile_callsites_;
Florian Mayer438b5ab2019-05-02 11:18:06 +01001378 HeapProfileAllocations heap_profile_allocations_;
Oystein Eftevaag7f64c102019-08-29 10:27:31 -07001379 CpuProfileStackSamples cpu_profile_stack_samples_;
Ioannis Ilkose0b47f52019-09-18 11:14:57 +01001380
1381 // Symbol tables (mappings from frames to symbol names)
1382 tables::SymbolTable symbol_table_{&string_pool_, nullptr};
Lalit Maganticaed37e2018-06-01 03:03:08 +01001383};
1384
1385} // namespace trace_processor
1386} // namespace perfetto
1387
Florian Mayerbee52132019-05-02 13:59:56 +01001388namespace std {
1389
1390template <>
Oystein Eftevaag5419c582019-08-21 13:58:49 -07001391struct hash<
1392 ::perfetto::trace_processor::TraceStorage::StackProfileFrames::Row> {
Florian Mayerbee52132019-05-02 13:59:56 +01001393 using argument_type =
Oystein Eftevaag5419c582019-08-21 13:58:49 -07001394 ::perfetto::trace_processor::TraceStorage::StackProfileFrames::Row;
Florian Mayerbee52132019-05-02 13:59:56 +01001395 using result_type = size_t;
1396
1397 result_type operator()(const argument_type& r) const {
1398 return std::hash<::perfetto::trace_processor::StringId>{}(r.name_id) ^
1399 std::hash<int64_t>{}(r.mapping_row) ^ std::hash<int64_t>{}(r.rel_pc);
1400 }
1401};
1402
1403template <>
1404struct hash<
Oystein Eftevaag5419c582019-08-21 13:58:49 -07001405 ::perfetto::trace_processor::TraceStorage::StackProfileCallsites::Row> {
Florian Mayerbee52132019-05-02 13:59:56 +01001406 using argument_type =
Oystein Eftevaag5419c582019-08-21 13:58:49 -07001407 ::perfetto::trace_processor::TraceStorage::StackProfileCallsites::Row;
Florian Mayerbee52132019-05-02 13:59:56 +01001408 using result_type = size_t;
1409
1410 result_type operator()(const argument_type& r) const {
1411 return std::hash<int64_t>{}(r.depth) ^ std::hash<int64_t>{}(r.parent_id) ^
1412 std::hash<int64_t>{}(r.frame_row);
1413 }
1414};
1415
1416template <>
1417struct hash<
Oystein Eftevaag5419c582019-08-21 13:58:49 -07001418 ::perfetto::trace_processor::TraceStorage::StackProfileMappings::Row> {
Florian Mayerbee52132019-05-02 13:59:56 +01001419 using argument_type =
Oystein Eftevaag5419c582019-08-21 13:58:49 -07001420 ::perfetto::trace_processor::TraceStorage::StackProfileMappings::Row;
Florian Mayerbee52132019-05-02 13:59:56 +01001421 using result_type = size_t;
1422
1423 result_type operator()(const argument_type& r) const {
1424 return std::hash<::perfetto::trace_processor::StringId>{}(r.build_id) ^
Florian Mayer12655732019-07-02 15:08:26 +01001425 std::hash<int64_t>{}(r.exact_offset) ^
1426 std::hash<int64_t>{}(r.start_offset) ^
1427 std::hash<int64_t>{}(r.start) ^ std::hash<int64_t>{}(r.end) ^
1428 std::hash<int64_t>{}(r.load_bias) ^
Florian Mayerbee52132019-05-02 13:59:56 +01001429 std::hash<::perfetto::trace_processor::StringId>{}(r.name_id);
1430 }
1431};
1432
1433} // namespace std
1434
Lalit Maganticaed37e2018-06-01 03:03:08 +01001435#endif // SRC_TRACE_PROCESSOR_TRACE_STORAGE_H_