blob: 0ea97e63ed1d7c8ed97e9931c601d63bd51222b1 [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>
25#include <vector>
26
Lalit Maganti35622b72018-06-06 12:03:11 +010027#include "perfetto/base/logging.h"
Lalit Maganti770886a2018-11-16 17:40:21 +000028#include "perfetto/base/optional.h"
Primiano Tucci2da5d2e2018-08-10 14:23:31 +010029#include "perfetto/base/string_view.h"
Hector Dearmanc8339932018-08-10 11:22:46 +010030#include "perfetto/base/utils.h"
Lalit Maganti35622b72018-06-06 12:03:11 +010031
Lalit Maganticaed37e2018-06-01 03:03:08 +010032namespace perfetto {
33namespace trace_processor {
34
Isabelle Taylora0a22972018-08-03 12:06:12 +010035// UniquePid is an offset into |unique_processes_|. This is necessary because
36// Unix pids are reused and thus not guaranteed to be unique over a long
37// period of time.
38using UniquePid = uint32_t;
Primiano Tucci0d72a312018-08-07 14:42:45 +010039
Isabelle Taylora0a22972018-08-03 12:06:12 +010040// UniqueTid is an offset into |unique_threads_|. Necessary because tids can
41// be reused.
42using UniqueTid = uint32_t;
43
Primiano Tucci0d72a312018-08-07 14:42:45 +010044// StringId is an offset into |string_pool_|.
Lalit Maganti85ca4a82018-12-07 17:28:02 +000045using StringId = uint32_t;
Primiano Tucci0d72a312018-08-07 14:42:45 +010046
Lalit Maganti5ea9e932018-11-30 14:19:39 +000047// Identifiers for all the tables in the database.
48enum TableId : uint8_t {
49 // Intentionally don't have TableId == 0 so that RowId == 0 can refer to an
50 // invalid row id.
51 kCounters = 1,
Lalit Maganti1d915a62019-01-07 12:10:42 +000052 kRawEvents = 2,
Lalit Maganti5ea9e932018-11-30 14:19:39 +000053};
54
55// The top 8 bits are set to the TableId and the bottom 32 to the row of the
56// table.
Lalit Maganti85ca4a82018-12-07 17:28:02 +000057using RowId = int64_t;
Lalit Maganti5ea9e932018-11-30 14:19:39 +000058
59static const RowId kInvalidRowId = 0;
60
Isabelle Taylora97c5f52018-10-23 17:36:12 +010061enum RefType {
Primiano Tucci5403e4f2018-11-27 10:07:03 +000062 kRefNoRef = 0,
63 kRefUtid = 1,
64 kRefCpuId = 2,
65 kRefIrq = 3,
66 kRefSoftIrq = 4,
67 kRefUpid = 5,
68 kRefUtidLookupUpid = 6,
69 kRefMax
Isabelle Taylora97c5f52018-10-23 17:36:12 +010070};
Isabelle Taylor14674d42018-09-07 11:33:11 +010071
Lalit Maganticaed37e2018-06-01 03:03:08 +010072// Stores a data inside a trace file in a columnar form. This makes it efficient
73// to read or search across a single field of the trace (e.g. all the thread
74// names for a given CPU).
75class TraceStorage {
76 public:
Isabelle Taylor47328cf2018-06-12 14:33:59 +010077 TraceStorage();
Isabelle Taylora0a22972018-08-03 12:06:12 +010078 TraceStorage(const TraceStorage&) = delete;
79
80 virtual ~TraceStorage();
Isabelle Taylor47328cf2018-06-12 14:33:59 +010081
Isabelle Taylora0a22972018-08-03 12:06:12 +010082 struct Stats {
Lalit Maganti85ca4a82018-12-07 17:28:02 +000083 int64_t mismatched_sched_switch_tids = 0;
84 int64_t rss_stat_no_process = 0;
85 int64_t mem_counter_no_process = 0;
Isabelle Taylora0a22972018-08-03 12:06:12 +010086 };
Lalit Maganti35622b72018-06-06 12:03:11 +010087
Isabelle Taylora0a22972018-08-03 12:06:12 +010088 // Information about a unique process seen in a trace.
89 struct Process {
Primiano Tuccib75dcee2018-08-08 12:21:36 +010090 explicit Process(uint32_t p) : pid(p) {}
Lalit Maganti85ca4a82018-12-07 17:28:02 +000091 int64_t start_ns = 0;
92 int64_t end_ns = 0;
Isabelle Taylora0a22972018-08-03 12:06:12 +010093 StringId name_id = 0;
Primiano Tuccib75dcee2018-08-08 12:21:36 +010094 uint32_t pid = 0;
Isabelle Taylora0a22972018-08-03 12:06:12 +010095 };
96
97 // Information about a unique thread seen in a trace.
98 struct Thread {
Primiano Tuccib75dcee2018-08-08 12:21:36 +010099 explicit Thread(uint32_t t) : tid(t) {}
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000100 int64_t start_ns = 0;
101 int64_t end_ns = 0;
Isabelle Taylora0a22972018-08-03 12:06:12 +0100102 StringId name_id = 0;
Lalit Maganti770886a2018-11-16 17:40:21 +0000103 base::Optional<UniquePid> upid;
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100104 uint32_t tid = 0;
Isabelle Taylora0a22972018-08-03 12:06:12 +0100105 };
Isabelle Taylor3dd366c2018-06-22 16:21:41 +0100106
Lalit Maganti6e9c55e2018-11-29 12:00:39 +0000107 // Generic key value storage which can be referenced by other tables.
108 class Args {
109 public:
Lalit Maganti1d915a62019-01-07 12:10:42 +0000110 // Variadic type representing the possible values for the args table.
111 struct Variadic {
Lalit Maganti6e9c55e2018-11-29 12:00:39 +0000112 enum Type { kInt, kString, kReal };
113
Lalit Maganti1d915a62019-01-07 12:10:42 +0000114 static Variadic Integer(int64_t int_value) {
115 Variadic variadic;
116 variadic.type = Type::kInt;
117 variadic.int_value = int_value;
118 return variadic;
119 }
120
121 static Variadic String(StringId string_id) {
122 Variadic variadic;
123 variadic.type = Type::kString;
124 variadic.string_value = string_id;
125 return variadic;
126 }
127
128 static Variadic Real(double real_value) {
129 Variadic variadic;
130 variadic.type = Type::kReal;
131 variadic.real_value = real_value;
132 return variadic;
133 }
Lalit Maganti6e9c55e2018-11-29 12:00:39 +0000134
135 Type type;
136 union {
137 int64_t int_value;
138 StringId string_value;
139 double real_value;
140 };
141 };
142
Lalit Maganti5ea9e932018-11-30 14:19:39 +0000143 const std::deque<RowId>& ids() const { return ids_; }
144 const std::deque<StringId>& flat_keys() const { return flat_keys_; }
145 const std::deque<StringId>& keys() const { return keys_; }
Lalit Maganti1d915a62019-01-07 12:10:42 +0000146 const std::deque<Variadic>& arg_values() const { return arg_values_; }
Lalit Maganti79472be2018-12-04 13:41:27 +0000147 const std::multimap<RowId, uint32_t>& args_for_id() const {
148 return args_for_id_;
149 }
Lalit Maganti6e9c55e2018-11-29 12:00:39 +0000150 size_t args_count() const { return ids_.size(); }
151
Lalit Maganti1d915a62019-01-07 12:10:42 +0000152 void AddArg(RowId id, StringId flat_key, StringId key, Variadic value) {
Lalit Maganti5ea9e932018-11-30 14:19:39 +0000153 if (id == kInvalidRowId)
154 return;
155
156 ids_.emplace_back(id);
157 flat_keys_.emplace_back(flat_key);
158 keys_.emplace_back(key);
159 arg_values_.emplace_back(value);
Lalit Maganti79472be2018-12-04 13:41:27 +0000160 args_for_id_.emplace(id, static_cast<uint32_t>(args_count() - 1));
Lalit Maganti6e9c55e2018-11-29 12:00:39 +0000161 }
162
Lalit Maganti5ea9e932018-11-30 14:19:39 +0000163 private:
164 std::deque<RowId> ids_;
165 std::deque<StringId> flat_keys_;
166 std::deque<StringId> keys_;
Lalit Maganti1d915a62019-01-07 12:10:42 +0000167 std::deque<Variadic> arg_values_;
Lalit Maganti79472be2018-12-04 13:41:27 +0000168 std::multimap<RowId, uint32_t> args_for_id_;
Lalit Maganti6e9c55e2018-11-29 12:00:39 +0000169 };
170
Lalit Magantiff69c112018-09-24 12:07:47 +0100171 class Slices {
Lalit Maganti35622b72018-06-06 12:03:11 +0100172 public:
Lalit Magantifde29042018-10-04 13:28:52 +0100173 inline size_t AddSlice(uint32_t cpu,
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000174 int64_t start_ns,
175 int64_t duration_ns,
Lalit Magantifde29042018-10-04 13:28:52 +0100176 UniqueTid utid) {
Lalit Magantiff69c112018-09-24 12:07:47 +0100177 cpus_.emplace_back(cpu);
Lalit Maganti35622b72018-06-06 12:03:11 +0100178 start_ns_.emplace_back(start_ns);
179 durations_.emplace_back(duration_ns);
Isabelle Taylora0a22972018-08-03 12:06:12 +0100180 utids_.emplace_back(utid);
Lalit Magantifde29042018-10-04 13:28:52 +0100181 return slice_count() - 1;
182 }
183
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000184 void set_duration(size_t index, int64_t duration_ns) {
Lalit Magantifde29042018-10-04 13:28:52 +0100185 durations_[index] = duration_ns;
Lalit Maganti35622b72018-06-06 12:03:11 +0100186 }
187
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100188 size_t slice_count() const { return start_ns_.size(); }
Lalit Maganti35622b72018-06-06 12:03:11 +0100189
Lalit Magantiff69c112018-09-24 12:07:47 +0100190 const std::deque<uint32_t>& cpus() const { return cpus_; }
191
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000192 const std::deque<int64_t>& start_ns() const { return start_ns_; }
Lalit Maganti35622b72018-06-06 12:03:11 +0100193
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000194 const std::deque<int64_t>& durations() const { return durations_; }
Lalit Maganti35622b72018-06-06 12:03:11 +0100195
Isabelle Taylor68e42192018-06-19 16:19:31 +0100196 const std::deque<UniqueTid>& utids() const { return utids_; }
197
Lalit Maganti35622b72018-06-06 12:03:11 +0100198 private:
Hector Dearman947f12a2018-09-11 16:50:36 +0100199 // Each deque below has the same number of entries (the number of slices
Lalit Maganti35622b72018-06-06 12:03:11 +0100200 // in the trace for the CPU).
Lalit Magantiff69c112018-09-24 12:07:47 +0100201 std::deque<uint32_t> cpus_;
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000202 std::deque<int64_t> start_ns_;
203 std::deque<int64_t> durations_;
Isabelle Taylor68e42192018-06-19 16:19:31 +0100204 std::deque<UniqueTid> utids_;
Primiano Tucci0d72a312018-08-07 14:42:45 +0100205 };
Isabelle Taylor68e42192018-06-19 16:19:31 +0100206
Primiano Tucci0d72a312018-08-07 14:42:45 +0100207 class NestableSlices {
208 public:
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000209 inline size_t AddSlice(int64_t start_ns,
210 int64_t duration_ns,
Lalit Maganti7b37bbf2018-11-09 15:58:54 +0000211 UniqueTid utid,
212 StringId cat,
213 StringId name,
214 uint8_t depth,
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000215 int64_t stack_id,
216 int64_t parent_stack_id) {
Primiano Tucci0d72a312018-08-07 14:42:45 +0100217 start_ns_.emplace_back(start_ns);
218 durations_.emplace_back(duration_ns);
219 utids_.emplace_back(utid);
220 cats_.emplace_back(cat);
221 names_.emplace_back(name);
222 depths_.emplace_back(depth);
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100223 stack_ids_.emplace_back(stack_id);
224 parent_stack_ids_.emplace_back(parent_stack_id);
Lalit Maganti7b37bbf2018-11-09 15:58:54 +0000225 return slice_count() - 1;
226 }
227
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000228 void set_duration(size_t index, int64_t duration_ns) {
Lalit Maganti7b37bbf2018-11-09 15:58:54 +0000229 durations_[index] = duration_ns;
230 }
231
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000232 void set_stack_id(size_t index, int64_t stack_id) {
Lalit Maganti7b37bbf2018-11-09 15:58:54 +0000233 stack_ids_[index] = stack_id;
Primiano Tucci0d72a312018-08-07 14:42:45 +0100234 }
235
236 size_t slice_count() const { return start_ns_.size(); }
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000237 const std::deque<int64_t>& start_ns() const { return start_ns_; }
238 const std::deque<int64_t>& durations() const { return durations_; }
Primiano Tucci0d72a312018-08-07 14:42:45 +0100239 const std::deque<UniqueTid>& utids() const { return utids_; }
240 const std::deque<StringId>& cats() const { return cats_; }
241 const std::deque<StringId>& names() const { return names_; }
242 const std::deque<uint8_t>& depths() const { return depths_; }
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000243 const std::deque<int64_t>& stack_ids() const { return stack_ids_; }
244 const std::deque<int64_t>& parent_stack_ids() const {
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100245 return parent_stack_ids_;
246 }
Primiano Tucci0d72a312018-08-07 14:42:45 +0100247
248 private:
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000249 std::deque<int64_t> start_ns_;
250 std::deque<int64_t> durations_;
Primiano Tucci0d72a312018-08-07 14:42:45 +0100251 std::deque<UniqueTid> utids_;
252 std::deque<StringId> cats_;
253 std::deque<StringId> names_;
254 std::deque<uint8_t> depths_;
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000255 std::deque<int64_t> stack_ids_;
256 std::deque<int64_t> parent_stack_ids_;
Lalit Maganti35622b72018-06-06 12:03:11 +0100257 };
258
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100259 class Counters {
260 public:
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000261 inline size_t AddCounter(int64_t timestamp,
262 int64_t duration,
Lalit Magantifde29042018-10-04 13:28:52 +0100263 StringId name_id,
264 double value,
Lalit Magantifde29042018-10-04 13:28:52 +0100265 int64_t ref,
266 RefType type) {
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100267 timestamps_.emplace_back(timestamp);
268 durations_.emplace_back(duration);
269 name_ids_.emplace_back(name_id);
270 values_.emplace_back(value);
271 refs_.emplace_back(ref);
272 types_.emplace_back(type);
Lalit Magantifde29042018-10-04 13:28:52 +0100273 return counter_count() - 1;
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100274 }
Lalit Magantifde29042018-10-04 13:28:52 +0100275
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000276 void set_duration(size_t index, int64_t duration) {
Lalit Magantifde29042018-10-04 13:28:52 +0100277 durations_[index] = duration;
278 }
279
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100280 size_t counter_count() const { return timestamps_.size(); }
281
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000282 const std::deque<int64_t>& timestamps() const { return timestamps_; }
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100283
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000284 const std::deque<int64_t>& durations() const { return durations_; }
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100285
286 const std::deque<StringId>& name_ids() const { return name_ids_; }
287
288 const std::deque<double>& values() const { return values_; }
289
290 const std::deque<int64_t>& refs() const { return refs_; }
291
292 const std::deque<RefType>& types() const { return types_; }
293
294 private:
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000295 std::deque<int64_t> timestamps_;
296 std::deque<int64_t> durations_;
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100297 std::deque<StringId> name_ids_;
298 std::deque<double> values_;
299 std::deque<int64_t> refs_;
300 std::deque<RefType> types_;
301 };
302
Primiano Tucci5cb84f82018-10-31 21:46:36 -0700303 class SqlStats {
304 public:
305 static constexpr size_t kMaxLogEntries = 100;
306 void RecordQueryBegin(const std::string& query,
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000307 int64_t time_queued,
308 int64_t time_started);
309 void RecordQueryEnd(int64_t time_ended);
Primiano Tucci5cb84f82018-10-31 21:46:36 -0700310 size_t size() const { return queries_.size(); }
311 const std::deque<std::string>& queries() const { return queries_; }
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000312 const std::deque<int64_t>& times_queued() const { return times_queued_; }
313 const std::deque<int64_t>& times_started() const { return times_started_; }
314 const std::deque<int64_t>& times_ended() const { return times_ended_; }
Primiano Tucci5cb84f82018-10-31 21:46:36 -0700315
316 private:
317 std::deque<std::string> queries_;
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000318 std::deque<int64_t> times_queued_;
319 std::deque<int64_t> times_started_;
320 std::deque<int64_t> times_ended_;
Primiano Tucci5cb84f82018-10-31 21:46:36 -0700321 };
322
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000323 class Instants {
324 public:
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000325 inline size_t AddInstantEvent(int64_t timestamp,
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000326 StringId name_id,
327 double value,
328 int64_t ref,
329 RefType type) {
330 timestamps_.emplace_back(timestamp);
331 name_ids_.emplace_back(name_id);
332 values_.emplace_back(value);
333 refs_.emplace_back(ref);
334 types_.emplace_back(type);
335 return instant_count() - 1;
336 }
337
338 size_t instant_count() const { return timestamps_.size(); }
339
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000340 const std::deque<int64_t>& timestamps() const { return timestamps_; }
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000341
342 const std::deque<StringId>& name_ids() const { return name_ids_; }
343
344 const std::deque<double>& values() const { return values_; }
345
346 const std::deque<int64_t>& refs() const { return refs_; }
347
348 const std::deque<RefType>& types() const { return types_; }
349
350 private:
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000351 std::deque<int64_t> timestamps_;
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000352 std::deque<StringId> name_ids_;
353 std::deque<double> values_;
354 std::deque<int64_t> refs_;
355 std::deque<RefType> types_;
356 };
357
Lalit Maganti1d915a62019-01-07 12:10:42 +0000358 class RawEvents {
359 public:
360 inline RowId AddRawEvent(int64_t timestamp,
361 StringId name_id,
362 UniqueTid utid) {
363 timestamps_.emplace_back(timestamp);
364 name_ids_.emplace_back(name_id);
365 utids_.emplace_back(utid);
366 return CreateRowId(TableId::kRawEvents,
367 static_cast<uint32_t>(raw_event_count() - 1));
368 }
369
370 size_t raw_event_count() const { return timestamps_.size(); }
371
372 const std::deque<int64_t>& timestamps() const { return timestamps_; }
373
374 const std::deque<StringId>& name_ids() const { return name_ids_; }
375
376 const std::deque<UniqueTid>& utids() const { return utids_; }
377
378 private:
379 std::deque<int64_t> timestamps_;
380 std::deque<StringId> name_ids_;
381 std::deque<UniqueTid> utids_;
382 };
383
Isabelle Taylora0a22972018-08-03 12:06:12 +0100384 void ResetStorage();
Lalit Maganti35622b72018-06-06 12:03:11 +0100385
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100386 UniqueTid AddEmptyThread(uint32_t tid) {
387 unique_threads_.emplace_back(tid);
Isabelle Taylora0a22972018-08-03 12:06:12 +0100388 return static_cast<UniqueTid>(unique_threads_.size() - 1);
389 }
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100390
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100391 UniquePid AddEmptyProcess(uint32_t pid) {
392 unique_processes_.emplace_back(pid);
Isabelle Taylora0a22972018-08-03 12:06:12 +0100393 return static_cast<UniquePid>(unique_processes_.size() - 1);
394 }
Isabelle Taylor3dd366c2018-06-22 16:21:41 +0100395
Isabelle Taylora0a22972018-08-03 12:06:12 +0100396 // Return an unqiue identifier for the contents of each string.
397 // The string is copied internally and can be destroyed after this called.
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100398 // Virtual for testing.
399 virtual StringId InternString(base::StringView);
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100400
Isabelle Taylora0a22972018-08-03 12:06:12 +0100401 Process* GetMutableProcess(UniquePid upid) {
Lalit Maganti676658b2018-11-20 18:24:31 +0000402 PERFETTO_DCHECK(upid < unique_processes_.size());
Isabelle Taylora0a22972018-08-03 12:06:12 +0100403 return &unique_processes_[upid];
404 }
Isabelle Taylor3dd366c2018-06-22 16:21:41 +0100405
Isabelle Taylora0a22972018-08-03 12:06:12 +0100406 Thread* GetMutableThread(UniqueTid utid) {
Florian Mayera1aaec22018-09-19 17:02:26 +0100407 PERFETTO_DCHECK(utid < unique_threads_.size());
Isabelle Taylora0a22972018-08-03 12:06:12 +0100408 return &unique_threads_[utid];
409 }
Isabelle Taylor3dd366c2018-06-22 16:21:41 +0100410
Lalit Maganticaed37e2018-06-01 03:03:08 +0100411 // Reading methods.
Isabelle Taylora0a22972018-08-03 12:06:12 +0100412 const std::string& GetString(StringId id) const {
413 PERFETTO_DCHECK(id < string_pool_.size());
414 return string_pool_[id];
415 }
416
Lalit Magantie9d40532018-06-29 13:15:06 +0100417 const Process& GetProcess(UniquePid upid) const {
Lalit Maganti676658b2018-11-20 18:24:31 +0000418 PERFETTO_DCHECK(upid < unique_processes_.size());
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100419 return unique_processes_[upid];
420 }
421
Lalit Magantie9d40532018-06-29 13:15:06 +0100422 const Thread& GetThread(UniqueTid utid) const {
Lalit Maganti1f64cfa2018-09-18 13:20:02 +0100423 // Allow utid == 0 for idle thread retrieval.
Florian Mayera1aaec22018-09-19 17:02:26 +0100424 PERFETTO_DCHECK(utid < unique_threads_.size());
Isabelle Taylor68e42192018-06-19 16:19:31 +0100425 return unique_threads_[utid];
426 }
427
Lalit Maganti5ea9e932018-11-30 14:19:39 +0000428 static RowId CreateRowId(TableId table, uint32_t row) {
429 static constexpr uint8_t kRowIdTableShift = 32;
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000430 return (static_cast<RowId>(table) << kRowIdTableShift) | row;
Lalit Maganti5ea9e932018-11-30 14:19:39 +0000431 }
432
Lalit Magantiff69c112018-09-24 12:07:47 +0100433 const Slices& slices() const { return slices_; }
Lalit Magantifde29042018-10-04 13:28:52 +0100434 Slices* mutable_slices() { return &slices_; }
435
Primiano Tucci0d72a312018-08-07 14:42:45 +0100436 const NestableSlices& nestable_slices() const { return nestable_slices_; }
437 NestableSlices* mutable_nestable_slices() { return &nestable_slices_; }
438
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100439 const Counters& counters() const { return counters_; }
440 Counters* mutable_counters() { return &counters_; }
Isabelle Taylor14674d42018-09-07 11:33:11 +0100441
Primiano Tucci5cb84f82018-10-31 21:46:36 -0700442 const SqlStats& sql_stats() const { return sql_stats_; }
443 SqlStats* mutable_sql_stats() { return &sql_stats_; }
444
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000445 const Instants& instants() const { return instants_; }
446 Instants* mutable_instants() { return &instants_; }
447
Lalit Maganti05e8c132018-11-09 18:16:12 +0000448 const Stats& stats() const { return stats_; }
449 Stats* mutable_stats() { return &stats_; }
450
Lalit Maganti6e9c55e2018-11-29 12:00:39 +0000451 const Args& args() const { return args_; }
452 Args* mutable_args() { return &args_; }
453
Lalit Maganti1d915a62019-01-07 12:10:42 +0000454 const RawEvents& raw_events() const { return raw_events_; }
455 RawEvents* mutable_raw_events() { return &raw_events_; }
456
Lalit Magantiacda68b2018-10-29 15:23:25 +0000457 const std::deque<std::string>& string_pool() const { return string_pool_; }
458
Isabelle Taylore7003fb2018-07-17 11:39:01 +0100459 // |unique_processes_| always contains at least 1 element becuase the 0th ID
460 // is reserved to indicate an invalid process.
461 size_t process_count() const { return unique_processes_.size() - 1; }
Primiano Tucci0d72a312018-08-07 14:42:45 +0100462
Isabelle Taylore7003fb2018-07-17 11:39:01 +0100463 // |unique_threads_| always contains at least 1 element becuase the 0th ID
464 // is reserved to indicate an invalid thread.
465 size_t thread_count() const { return unique_threads_.size() - 1; }
466
Hector Dearman12323362018-08-09 16:09:28 +0100467 // Number of interned strings in the pool. Includes the empty string w/ ID=0.
468 size_t string_count() const { return string_pool_.size(); }
469
Lalit Maganticaed37e2018-06-01 03:03:08 +0100470 private:
Isabelle Taylora0a22972018-08-03 12:06:12 +0100471 TraceStorage& operator=(const TraceStorage&) = default;
472
Primiano Tucci2da5d2e2018-08-10 14:23:31 +0100473 using StringHash = uint64_t;
Lalit Maganticaed37e2018-06-01 03:03:08 +0100474
Lalit Maganti05e8c132018-11-09 18:16:12 +0000475 // Stats about parsing the trace.
Lalit Maganti35622b72018-06-06 12:03:11 +0100476 Stats stats_;
477
Lalit Maganticaed37e2018-06-01 03:03:08 +0100478 // One entry for each CPU in the trace.
Lalit Magantiff69c112018-09-24 12:07:47 +0100479 Slices slices_;
Lalit Maganticaed37e2018-06-01 03:03:08 +0100480
Lalit Maganti6e9c55e2018-11-29 12:00:39 +0000481 // Args for all other tables.
482 Args args_;
483
Lalit Maganticaed37e2018-06-01 03:03:08 +0100484 // One entry for each unique string in the trace.
Lalit Maganti35622b72018-06-06 12:03:11 +0100485 std::deque<std::string> string_pool_;
Lalit Maganticaed37e2018-06-01 03:03:08 +0100486
487 // One entry for each unique string in the trace.
Lalit Maganti35622b72018-06-06 12:03:11 +0100488 std::unordered_map<StringHash, StringId> string_index_;
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100489
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100490 // One entry for each UniquePid, with UniquePid as the index.
Isabelle Taylor3dd366c2018-06-22 16:21:41 +0100491 std::deque<Process> unique_processes_;
Isabelle Taylor68e42192018-06-19 16:19:31 +0100492
Isabelle Taylor68e42192018-06-19 16:19:31 +0100493 // One entry for each UniqueTid, with UniqueTid as the index.
Isabelle Taylor3dd366c2018-06-22 16:21:41 +0100494 std::deque<Thread> unique_threads_;
Primiano Tucci0d72a312018-08-07 14:42:45 +0100495
496 // Slices coming from userspace events (e.g. Chromium TRACE_EVENT macros).
497 NestableSlices nestable_slices_;
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100498
499 // Counter events from the trace. This includes CPU frequency events as well
500 // systrace trace_marker counter events.
501 Counters counters_;
Primiano Tucci5cb84f82018-10-31 21:46:36 -0700502
503 SqlStats sql_stats_;
Lalit Maganti6e9c55e2018-11-29 12:00:39 +0000504
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000505 // These are instantaneous events in the trace. They have no duration
506 // and do not have a value that make sense to track over time.
507 // e.g. signal events
508 Instants instants_;
Lalit Maganti1d915a62019-01-07 12:10:42 +0000509
510 // Raw events are every ftrace event in the trace. The raw event includes
511 // the timestamp and the pid. The args for the raw event will be in the
512 // args table. This table can be used to generate a text version of the
513 // trace.
514 RawEvents raw_events_;
Lalit Maganticaed37e2018-06-01 03:03:08 +0100515};
516
517} // namespace trace_processor
518} // namespace perfetto
519
520#endif // SRC_TRACE_PROCESSOR_TRACE_STORAGE_H_