blob: 2a80cab1f29e94c9f2507bd6ad464839872af6c7 [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 Maganti1f464742019-02-28 13:49:31 +000028#include "perfetto/base/hash.h"
Lalit Maganti35622b72018-06-06 12:03:11 +010029#include "perfetto/base/logging.h"
Lalit Maganti770886a2018-11-16 17:40:21 +000030#include "perfetto/base/optional.h"
Primiano Tucci2da5d2e2018-08-10 14:23:31 +010031#include "perfetto/base/string_view.h"
Hector Dearmanc8339932018-08-10 11:22:46 +010032#include "perfetto/base/utils.h"
Lalit Magantib0b53ee2019-01-24 17:53:39 +000033#include "src/trace_processor/ftrace_utils.h"
Primiano Tucci0e38a142019-01-07 20:51:09 +000034#include "src/trace_processor/stats.h"
Lalit Maganti5c454312019-04-08 12:11:17 +010035#include "src/trace_processor/string_pool.h"
Lalit Maganti35622b72018-06-06 12:03:11 +010036
Lalit Maganticaed37e2018-06-01 03:03:08 +010037namespace perfetto {
38namespace trace_processor {
39
Isabelle Taylora0a22972018-08-03 12:06:12 +010040// UniquePid is an offset into |unique_processes_|. This is necessary because
41// Unix pids are reused and thus not guaranteed to be unique over a long
42// period of time.
43using UniquePid = uint32_t;
Primiano Tucci0d72a312018-08-07 14:42:45 +010044
Isabelle Taylora0a22972018-08-03 12:06:12 +010045// UniqueTid is an offset into |unique_threads_|. Necessary because tids can
46// be reused.
47using UniqueTid = uint32_t;
48
Primiano Tucci0d72a312018-08-07 14:42:45 +010049// StringId is an offset into |string_pool_|.
Lalit Maganti5c454312019-04-08 12:11:17 +010050using StringId = StringPool::Id;
Primiano Tucci0d72a312018-08-07 14:42:45 +010051
Lalit Maganti5ea9e932018-11-30 14:19:39 +000052// Identifiers for all the tables in the database.
53enum TableId : uint8_t {
54 // Intentionally don't have TableId == 0 so that RowId == 0 can refer to an
55 // invalid row id.
Lalit Maganti8320e6d2019-03-14 18:49:33 +000056 kCounterValues = 1,
Lalit Maganti1d915a62019-01-07 12:10:42 +000057 kRawEvents = 2,
Lalit Maganti66ed7ad2019-01-11 16:47:26 +000058 kInstants = 3,
Isabelle Taylorb9222c32019-01-31 10:58:37 +000059 kSched = 4,
Lalit Maganti5ea9e932018-11-30 14:19:39 +000060};
61
62// The top 8 bits are set to the TableId and the bottom 32 to the row of the
63// table.
Lalit Maganti85ca4a82018-12-07 17:28:02 +000064using RowId = int64_t;
Lalit Maganti5ea9e932018-11-30 14:19:39 +000065static const RowId kInvalidRowId = 0;
66
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +000067using ArgSetId = uint32_t;
68static const ArgSetId kInvalidArgSetId = 0;
69
Isabelle Taylora97c5f52018-10-23 17:36:12 +010070enum RefType {
Primiano Tucci5403e4f2018-11-27 10:07:03 +000071 kRefNoRef = 0,
72 kRefUtid = 1,
73 kRefCpuId = 2,
74 kRefIrq = 3,
75 kRefSoftIrq = 4,
76 kRefUpid = 5,
77 kRefUtidLookupUpid = 6,
78 kRefMax
Isabelle Taylora97c5f52018-10-23 17:36:12 +010079};
Isabelle Taylor14674d42018-09-07 11:33:11 +010080
Eric Seckler972225e2019-04-18 11:07:12 +010081const std::vector<const char*>& GetRefTypeStringMap();
82
Lalit Maganticaed37e2018-06-01 03:03:08 +010083// Stores a data inside a trace file in a columnar form. This makes it efficient
84// to read or search across a single field of the trace (e.g. all the thread
85// names for a given CPU).
86class TraceStorage {
87 public:
Isabelle Taylor47328cf2018-06-12 14:33:59 +010088 TraceStorage();
Isabelle Taylora0a22972018-08-03 12:06:12 +010089
90 virtual ~TraceStorage();
Isabelle Taylor47328cf2018-06-12 14:33:59 +010091
Isabelle Taylora0a22972018-08-03 12:06:12 +010092 // Information about a unique process seen in a trace.
93 struct Process {
Primiano Tuccib75dcee2018-08-08 12:21:36 +010094 explicit Process(uint32_t p) : pid(p) {}
Lalit Maganti85ca4a82018-12-07 17:28:02 +000095 int64_t start_ns = 0;
Isabelle Taylora0a22972018-08-03 12:06:12 +010096 StringId name_id = 0;
Primiano Tuccib75dcee2018-08-08 12:21:36 +010097 uint32_t pid = 0;
Lalit Maganti08884242019-02-19 12:28:32 +000098 base::Optional<UniquePid> pupid;
Isabelle Taylora0a22972018-08-03 12:06:12 +010099 };
100
101 // Information about a unique thread seen in a trace.
102 struct Thread {
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100103 explicit Thread(uint32_t t) : tid(t) {}
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000104 int64_t start_ns = 0;
Isabelle Taylora0a22972018-08-03 12:06:12 +0100105 StringId name_id = 0;
Lalit Maganti770886a2018-11-16 17:40:21 +0000106 base::Optional<UniquePid> upid;
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100107 uint32_t tid = 0;
Isabelle Taylora0a22972018-08-03 12:06:12 +0100108 };
Isabelle Taylor3dd366c2018-06-22 16:21:41 +0100109
Lalit Maganti6e9c55e2018-11-29 12:00:39 +0000110 // Generic key value storage which can be referenced by other tables.
111 class Args {
112 public:
Lalit Maganti1d915a62019-01-07 12:10:42 +0000113 // Variadic type representing the possible values for the args table.
114 struct Variadic {
Lalit Maganti6e9c55e2018-11-29 12:00:39 +0000115 enum Type { kInt, kString, kReal };
116
Lalit Maganti1d915a62019-01-07 12:10:42 +0000117 static Variadic Integer(int64_t int_value) {
118 Variadic variadic;
119 variadic.type = Type::kInt;
120 variadic.int_value = int_value;
121 return variadic;
122 }
123
124 static Variadic String(StringId string_id) {
125 Variadic variadic;
126 variadic.type = Type::kString;
127 variadic.string_value = string_id;
128 return variadic;
129 }
130
131 static Variadic Real(double real_value) {
132 Variadic variadic;
133 variadic.type = Type::kReal;
134 variadic.real_value = real_value;
135 return variadic;
136 }
Lalit Maganti6e9c55e2018-11-29 12:00:39 +0000137
138 Type type;
139 union {
140 int64_t int_value;
141 StringId string_value;
142 double real_value;
143 };
144 };
145
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000146 struct Arg {
147 StringId flat_key = 0;
148 StringId key = 0;
149 Variadic value = Variadic::Integer(0);
150
151 // This is only used by the arg tracker and so is not part of the hash.
152 RowId row_id = 0;
153 };
154
155 struct ArgHasher {
156 uint64_t operator()(const Arg& arg) const noexcept {
Lalit Maganti1f464742019-02-28 13:49:31 +0000157 base::Hash hash;
158 hash.Update(arg.key);
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000159 // We don't hash arg.flat_key because it's a subsequence of arg.key.
160 switch (arg.value.type) {
161 case Variadic::Type::kInt:
Lalit Maganti1f464742019-02-28 13:49:31 +0000162 hash.Update(arg.value.int_value);
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000163 break;
164 case Variadic::Type::kString:
Lalit Maganti1f464742019-02-28 13:49:31 +0000165 hash.Update(arg.value.string_value);
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000166 break;
167 case Variadic::Type::kReal:
Lalit Maganti1f464742019-02-28 13:49:31 +0000168 hash.Update(arg.value.real_value);
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000169 break;
170 }
Lalit Maganti1f464742019-02-28 13:49:31 +0000171 return hash.digest();
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000172 }
173 };
174
175 const std::deque<ArgSetId>& set_ids() const { return set_ids_; }
Lalit Maganti5ea9e932018-11-30 14:19:39 +0000176 const std::deque<StringId>& flat_keys() const { return flat_keys_; }
177 const std::deque<StringId>& keys() const { return keys_; }
Lalit Maganti1d915a62019-01-07 12:10:42 +0000178 const std::deque<Variadic>& arg_values() const { return arg_values_; }
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000179 uint32_t args_count() const {
180 return static_cast<uint32_t>(set_ids_.size());
Lalit Maganti79472be2018-12-04 13:41:27 +0000181 }
Lalit Maganti6e9c55e2018-11-29 12:00:39 +0000182
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000183 ArgSetId AddArgSet(const std::vector<Arg>& args,
184 uint32_t begin,
185 uint32_t end) {
Lalit Maganti1f464742019-02-28 13:49:31 +0000186 base::Hash hash;
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000187 for (uint32_t i = begin; i < end; i++) {
Lalit Maganti1f464742019-02-28 13:49:31 +0000188 hash.Update(ArgHasher()(args[i]));
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000189 }
Lalit Maganti5ea9e932018-11-30 14:19:39 +0000190
Lalit Maganti1f464742019-02-28 13:49:31 +0000191 ArgSetHash digest = hash.digest();
192 auto it = arg_row_for_hash_.find(digest);
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000193 if (it != arg_row_for_hash_.end()) {
194 return set_ids_[it->second];
195 }
196
197 // The +1 ensures that nothing has an id == kInvalidArgSetId == 0.
198 ArgSetId id = static_cast<uint32_t>(arg_row_for_hash_.size()) + 1;
Lalit Maganti1f464742019-02-28 13:49:31 +0000199 arg_row_for_hash_.emplace(digest, args_count());
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000200 for (uint32_t i = begin; i < end; i++) {
201 const auto& arg = args[i];
202 set_ids_.emplace_back(id);
203 flat_keys_.emplace_back(arg.flat_key);
204 keys_.emplace_back(arg.key);
205 arg_values_.emplace_back(arg.value);
206 }
207 return id;
Lalit Maganti6e9c55e2018-11-29 12:00:39 +0000208 }
209
Lalit Maganti5ea9e932018-11-30 14:19:39 +0000210 private:
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000211 using ArgSetHash = uint64_t;
212
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000213 std::deque<ArgSetId> set_ids_;
Lalit Maganti5ea9e932018-11-30 14:19:39 +0000214 std::deque<StringId> flat_keys_;
215 std::deque<StringId> keys_;
Lalit Maganti1d915a62019-01-07 12:10:42 +0000216 std::deque<Variadic> arg_values_;
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000217
218 std::unordered_map<ArgSetHash, uint32_t> arg_row_for_hash_;
Lalit Maganti6e9c55e2018-11-29 12:00:39 +0000219 };
220
Lalit Magantiff69c112018-09-24 12:07:47 +0100221 class Slices {
Lalit Maganti35622b72018-06-06 12:03:11 +0100222 public:
Lalit Magantifde29042018-10-04 13:28:52 +0100223 inline size_t AddSlice(uint32_t cpu,
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000224 int64_t start_ns,
225 int64_t duration_ns,
Lalit Magantib0b53ee2019-01-24 17:53:39 +0000226 UniqueTid utid,
227 ftrace_utils::TaskState end_state,
228 int32_t priority) {
Lalit Magantiff69c112018-09-24 12:07:47 +0100229 cpus_.emplace_back(cpu);
Lalit Maganti35622b72018-06-06 12:03:11 +0100230 start_ns_.emplace_back(start_ns);
231 durations_.emplace_back(duration_ns);
Isabelle Taylora0a22972018-08-03 12:06:12 +0100232 utids_.emplace_back(utid);
Lalit Magantib0b53ee2019-01-24 17:53:39 +0000233 end_states_.emplace_back(end_state);
234 priorities_.emplace_back(priority);
Lalit Maganti58638932019-01-31 10:48:26 +0000235
236 if (utid >= rows_for_utids_.size())
237 rows_for_utids_.resize(utid + 1);
238 rows_for_utids_[utid].emplace_back(slice_count() - 1);
Lalit Magantifde29042018-10-04 13:28:52 +0100239 return slice_count() - 1;
240 }
241
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000242 void set_duration(size_t index, int64_t duration_ns) {
Lalit Magantifde29042018-10-04 13:28:52 +0100243 durations_[index] = duration_ns;
Lalit Maganti35622b72018-06-06 12:03:11 +0100244 }
245
Lalit Magantib0b53ee2019-01-24 17:53:39 +0000246 void set_end_state(size_t index, ftrace_utils::TaskState end_state) {
247 end_states_[index] = end_state;
248 }
249
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100250 size_t slice_count() const { return start_ns_.size(); }
Lalit Maganti35622b72018-06-06 12:03:11 +0100251
Lalit Magantiff69c112018-09-24 12:07:47 +0100252 const std::deque<uint32_t>& cpus() const { return cpus_; }
253
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000254 const std::deque<int64_t>& start_ns() const { return start_ns_; }
Lalit Maganti35622b72018-06-06 12:03:11 +0100255
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000256 const std::deque<int64_t>& durations() const { return durations_; }
Lalit Maganti35622b72018-06-06 12:03:11 +0100257
Isabelle Taylor68e42192018-06-19 16:19:31 +0100258 const std::deque<UniqueTid>& utids() const { return utids_; }
259
Lalit Magantib0b53ee2019-01-24 17:53:39 +0000260 const std::deque<ftrace_utils::TaskState>& end_state() const {
261 return end_states_;
262 }
263
264 const std::deque<int32_t>& priorities() const { return priorities_; }
265
Lalit Maganti58638932019-01-31 10:48:26 +0000266 const std::deque<std::vector<uint32_t>>& rows_for_utids() const {
Lalit Magantif0f09c32019-01-18 17:29:31 +0000267 return rows_for_utids_;
268 }
269
Lalit Maganti35622b72018-06-06 12:03:11 +0100270 private:
Hector Dearman947f12a2018-09-11 16:50:36 +0100271 // Each deque below has the same number of entries (the number of slices
Lalit Maganti35622b72018-06-06 12:03:11 +0100272 // in the trace for the CPU).
Lalit Magantiff69c112018-09-24 12:07:47 +0100273 std::deque<uint32_t> cpus_;
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000274 std::deque<int64_t> start_ns_;
275 std::deque<int64_t> durations_;
Isabelle Taylor68e42192018-06-19 16:19:31 +0100276 std::deque<UniqueTid> utids_;
Lalit Magantib0b53ee2019-01-24 17:53:39 +0000277 std::deque<ftrace_utils::TaskState> end_states_;
278 std::deque<int32_t> priorities_;
Lalit Maganti58638932019-01-31 10:48:26 +0000279
280 // One row per utid.
281 std::deque<std::vector<uint32_t>> rows_for_utids_;
Primiano Tucci0d72a312018-08-07 14:42:45 +0100282 };
Isabelle Taylor68e42192018-06-19 16:19:31 +0100283
Primiano Tucci0d72a312018-08-07 14:42:45 +0100284 class NestableSlices {
285 public:
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000286 inline size_t AddSlice(int64_t start_ns,
287 int64_t duration_ns,
Eric Seckler972225e2019-04-18 11:07:12 +0100288 int64_t ref,
289 RefType type,
Lalit Maganti7b37bbf2018-11-09 15:58:54 +0000290 StringId cat,
291 StringId name,
292 uint8_t depth,
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000293 int64_t stack_id,
294 int64_t parent_stack_id) {
Primiano Tucci0d72a312018-08-07 14:42:45 +0100295 start_ns_.emplace_back(start_ns);
296 durations_.emplace_back(duration_ns);
Eric Seckler972225e2019-04-18 11:07:12 +0100297 refs_.emplace_back(ref);
298 types_.emplace_back(type);
Primiano Tucci0d72a312018-08-07 14:42:45 +0100299 cats_.emplace_back(cat);
300 names_.emplace_back(name);
301 depths_.emplace_back(depth);
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100302 stack_ids_.emplace_back(stack_id);
303 parent_stack_ids_.emplace_back(parent_stack_id);
Lalit Maganti7b37bbf2018-11-09 15:58:54 +0000304 return slice_count() - 1;
305 }
306
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000307 void set_duration(size_t index, int64_t duration_ns) {
Lalit Maganti7b37bbf2018-11-09 15:58:54 +0000308 durations_[index] = duration_ns;
309 }
310
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000311 void set_stack_id(size_t index, int64_t stack_id) {
Lalit Maganti7b37bbf2018-11-09 15:58:54 +0000312 stack_ids_[index] = stack_id;
Primiano Tucci0d72a312018-08-07 14:42:45 +0100313 }
314
315 size_t slice_count() const { return start_ns_.size(); }
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000316 const std::deque<int64_t>& start_ns() const { return start_ns_; }
317 const std::deque<int64_t>& durations() const { return durations_; }
Eric Seckler972225e2019-04-18 11:07:12 +0100318 const std::deque<int64_t>& refs() const { return refs_; }
319 const std::deque<RefType>& types() const { return types_; }
Primiano Tucci0d72a312018-08-07 14:42:45 +0100320 const std::deque<StringId>& cats() const { return cats_; }
321 const std::deque<StringId>& names() const { return names_; }
322 const std::deque<uint8_t>& depths() const { return depths_; }
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000323 const std::deque<int64_t>& stack_ids() const { return stack_ids_; }
324 const std::deque<int64_t>& parent_stack_ids() const {
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100325 return parent_stack_ids_;
326 }
Primiano Tucci0d72a312018-08-07 14:42:45 +0100327
328 private:
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000329 std::deque<int64_t> start_ns_;
330 std::deque<int64_t> durations_;
Eric Seckler972225e2019-04-18 11:07:12 +0100331 std::deque<int64_t> refs_;
332 std::deque<RefType> types_;
Primiano Tucci0d72a312018-08-07 14:42:45 +0100333 std::deque<StringId> cats_;
334 std::deque<StringId> names_;
335 std::deque<uint8_t> depths_;
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000336 std::deque<int64_t> stack_ids_;
337 std::deque<int64_t> parent_stack_ids_;
Lalit Maganti35622b72018-06-06 12:03:11 +0100338 };
339
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000340 class CounterDefinitions {
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100341 public:
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000342 using Id = uint32_t;
343
344 inline Id AddCounterDefinition(StringId name_id,
345 int64_t ref,
346 RefType type) {
347 base::Hash hash;
348 hash.Update(name_id);
349 hash.Update(ref);
350 hash.Update(type);
351
352 // TODO(lalitm): this is a perf bottleneck and likely we can do something
353 // quite a bit better here.
354 uint64_t digest = hash.digest();
355 auto it = hash_to_row_idx_.find(digest);
356 if (it != hash_to_row_idx_.end())
357 return it->second;
358
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100359 name_ids_.emplace_back(name_id);
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100360 refs_.emplace_back(ref);
361 types_.emplace_back(type);
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000362 hash_to_row_idx_.emplace(digest, size() - 1);
363 return size() - 1;
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100364 }
Lalit Magantifde29042018-10-04 13:28:52 +0100365
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000366 uint32_t size() const { return static_cast<uint32_t>(name_ids_.size()); }
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100367
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100368 const std::deque<StringId>& name_ids() const { return name_ids_; }
369
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100370 const std::deque<int64_t>& refs() const { return refs_; }
371
372 const std::deque<RefType>& types() const { return types_; }
373
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000374 private:
375 std::deque<StringId> name_ids_;
376 std::deque<int64_t> refs_;
377 std::deque<RefType> types_;
378
379 std::unordered_map<uint64_t, uint32_t> hash_to_row_idx_;
380 };
381
382 class CounterValues {
383 public:
384 inline uint32_t AddCounterValue(CounterDefinitions::Id counter_id,
385 int64_t timestamp,
386 double value) {
387 counter_ids_.emplace_back(counter_id);
388 timestamps_.emplace_back(timestamp);
389 values_.emplace_back(value);
390 arg_set_ids_.emplace_back(kInvalidArgSetId);
Ioannis Ilkosf129c262019-04-25 14:53:51 +0100391
392 if (counter_id >= rows_for_counter_id_.size()) {
393 rows_for_counter_id_.resize(counter_id + 1);
394 }
395 uint32_t row = size() - 1;
396 rows_for_counter_id_[counter_id].emplace_back(row);
397 return row;
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000398 }
399
400 void set_arg_set_id(uint32_t row, ArgSetId id) { arg_set_ids_[row] = id; }
401
402 uint32_t size() const { return static_cast<uint32_t>(counter_ids_.size()); }
403
404 const std::deque<CounterDefinitions::Id>& counter_ids() const {
405 return counter_ids_;
406 }
407
408 const std::deque<int64_t>& timestamps() const { return timestamps_; }
409
410 const std::deque<double>& values() const { return values_; }
411
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000412 const std::deque<ArgSetId>& arg_set_ids() const { return arg_set_ids_; }
413
Ioannis Ilkosf129c262019-04-25 14:53:51 +0100414 const std::deque<std::vector<uint32_t>>& rows_for_counter_id() const {
415 return rows_for_counter_id_;
416 }
417
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100418 private:
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000419 std::deque<CounterDefinitions::Id> counter_ids_;
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000420 std::deque<int64_t> timestamps_;
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100421 std::deque<double> values_;
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000422 std::deque<ArgSetId> arg_set_ids_;
Ioannis Ilkosf129c262019-04-25 14:53:51 +0100423
424 // Indexed by counter_id value and contains the row numbers corresponding to
425 // it.
426 std::deque<std::vector<uint32_t>> rows_for_counter_id_;
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100427 };
428
Primiano Tucci5cb84f82018-10-31 21:46:36 -0700429 class SqlStats {
430 public:
431 static constexpr size_t kMaxLogEntries = 100;
432 void RecordQueryBegin(const std::string& query,
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000433 int64_t time_queued,
434 int64_t time_started);
435 void RecordQueryEnd(int64_t time_ended);
Primiano Tucci5cb84f82018-10-31 21:46:36 -0700436 size_t size() const { return queries_.size(); }
437 const std::deque<std::string>& queries() const { return queries_; }
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000438 const std::deque<int64_t>& times_queued() const { return times_queued_; }
439 const std::deque<int64_t>& times_started() const { return times_started_; }
440 const std::deque<int64_t>& times_ended() const { return times_ended_; }
Primiano Tucci5cb84f82018-10-31 21:46:36 -0700441
442 private:
443 std::deque<std::string> queries_;
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000444 std::deque<int64_t> times_queued_;
445 std::deque<int64_t> times_started_;
446 std::deque<int64_t> times_ended_;
Primiano Tucci5cb84f82018-10-31 21:46:36 -0700447 };
448
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000449 class Instants {
450 public:
Lalit Maganti66ed7ad2019-01-11 16:47:26 +0000451 inline uint32_t AddInstantEvent(int64_t timestamp,
452 StringId name_id,
453 double value,
454 int64_t ref,
455 RefType type) {
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000456 timestamps_.emplace_back(timestamp);
457 name_ids_.emplace_back(name_id);
458 values_.emplace_back(value);
459 refs_.emplace_back(ref);
460 types_.emplace_back(type);
Lalit Maganti1c21d172019-02-07 10:48:24 +0000461 arg_set_ids_.emplace_back(kInvalidArgSetId);
Lalit Maganti66ed7ad2019-01-11 16:47:26 +0000462 return static_cast<uint32_t>(instant_count() - 1);
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000463 }
464
Lalit Maganti1c21d172019-02-07 10:48:24 +0000465 void set_arg_set_id(uint32_t row, ArgSetId id) { arg_set_ids_[row] = id; }
466
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000467 size_t instant_count() const { return timestamps_.size(); }
468
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000469 const std::deque<int64_t>& timestamps() const { return timestamps_; }
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000470
471 const std::deque<StringId>& name_ids() const { return name_ids_; }
472
473 const std::deque<double>& values() const { return values_; }
474
475 const std::deque<int64_t>& refs() const { return refs_; }
476
477 const std::deque<RefType>& types() const { return types_; }
478
Lalit Maganti1c21d172019-02-07 10:48:24 +0000479 const std::deque<ArgSetId>& arg_set_ids() const { return arg_set_ids_; }
480
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000481 private:
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000482 std::deque<int64_t> timestamps_;
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000483 std::deque<StringId> name_ids_;
484 std::deque<double> values_;
485 std::deque<int64_t> refs_;
486 std::deque<RefType> types_;
Lalit Maganti1c21d172019-02-07 10:48:24 +0000487 std::deque<ArgSetId> arg_set_ids_;
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000488 };
489
Lalit Maganti1d915a62019-01-07 12:10:42 +0000490 class RawEvents {
491 public:
492 inline RowId AddRawEvent(int64_t timestamp,
493 StringId name_id,
Lalit Magantie87cc812019-01-10 15:20:06 +0000494 uint32_t cpu,
Lalit Maganti1d915a62019-01-07 12:10:42 +0000495 UniqueTid utid) {
496 timestamps_.emplace_back(timestamp);
497 name_ids_.emplace_back(name_id);
Lalit Magantie87cc812019-01-10 15:20:06 +0000498 cpus_.emplace_back(cpu);
Lalit Maganti1d915a62019-01-07 12:10:42 +0000499 utids_.emplace_back(utid);
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000500 arg_set_ids_.emplace_back(kInvalidArgSetId);
Lalit Maganti1d915a62019-01-07 12:10:42 +0000501 return CreateRowId(TableId::kRawEvents,
502 static_cast<uint32_t>(raw_event_count() - 1));
503 }
504
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000505 void set_arg_set_id(uint32_t row, ArgSetId id) { arg_set_ids_[row] = id; }
506
Lalit Maganti1d915a62019-01-07 12:10:42 +0000507 size_t raw_event_count() const { return timestamps_.size(); }
508
509 const std::deque<int64_t>& timestamps() const { return timestamps_; }
510
511 const std::deque<StringId>& name_ids() const { return name_ids_; }
512
Lalit Magantie87cc812019-01-10 15:20:06 +0000513 const std::deque<uint32_t>& cpus() const { return cpus_; }
514
Lalit Maganti1d915a62019-01-07 12:10:42 +0000515 const std::deque<UniqueTid>& utids() const { return utids_; }
516
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000517 const std::deque<ArgSetId>& arg_set_ids() const { return arg_set_ids_; }
518
Lalit Maganti1d915a62019-01-07 12:10:42 +0000519 private:
520 std::deque<int64_t> timestamps_;
521 std::deque<StringId> name_ids_;
Lalit Magantie87cc812019-01-10 15:20:06 +0000522 std::deque<uint32_t> cpus_;
Lalit Maganti1d915a62019-01-07 12:10:42 +0000523 std::deque<UniqueTid> utids_;
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000524 std::deque<ArgSetId> arg_set_ids_;
Lalit Maganti1d915a62019-01-07 12:10:42 +0000525 };
526
Primiano Tucci2c761ef2019-01-07 20:20:46 +0000527 class AndroidLogs {
528 public:
529 inline size_t AddLogEvent(int64_t timestamp,
530 UniqueTid utid,
531 uint8_t prio,
532 StringId tag_id,
533 StringId msg_id) {
534 timestamps_.emplace_back(timestamp);
535 utids_.emplace_back(utid);
536 prios_.emplace_back(prio);
537 tag_ids_.emplace_back(tag_id);
538 msg_ids_.emplace_back(msg_id);
539 return size() - 1;
540 }
541
542 size_t size() const { return timestamps_.size(); }
543
544 const std::deque<int64_t>& timestamps() const { return timestamps_; }
545 const std::deque<UniqueTid>& utids() const { return utids_; }
546 const std::deque<uint8_t>& prios() const { return prios_; }
547 const std::deque<StringId>& tag_ids() const { return tag_ids_; }
548 const std::deque<StringId>& msg_ids() const { return msg_ids_; }
549
550 private:
551 std::deque<int64_t> timestamps_;
552 std::deque<UniqueTid> utids_;
553 std::deque<uint8_t> prios_;
554 std::deque<StringId> tag_ids_;
555 std::deque<StringId> msg_ids_;
556 };
557
Primiano Tucci0e38a142019-01-07 20:51:09 +0000558 struct Stats {
559 using IndexMap = std::map<int, int64_t>;
560 int64_t value = 0;
561 IndexMap indexed_values;
562 };
563 using StatsMap = std::array<Stats, stats::kNumKeys>;
564
Isabelle Taylora0a22972018-08-03 12:06:12 +0100565 void ResetStorage();
Lalit Maganti35622b72018-06-06 12:03:11 +0100566
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100567 UniqueTid AddEmptyThread(uint32_t tid) {
568 unique_threads_.emplace_back(tid);
Isabelle Taylora0a22972018-08-03 12:06:12 +0100569 return static_cast<UniqueTid>(unique_threads_.size() - 1);
570 }
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100571
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100572 UniquePid AddEmptyProcess(uint32_t pid) {
573 unique_processes_.emplace_back(pid);
Isabelle Taylora0a22972018-08-03 12:06:12 +0100574 return static_cast<UniquePid>(unique_processes_.size() - 1);
575 }
Isabelle Taylor3dd366c2018-06-22 16:21:41 +0100576
Isabelle Taylora0a22972018-08-03 12:06:12 +0100577 // Return an unqiue identifier for the contents of each string.
578 // The string is copied internally and can be destroyed after this called.
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100579 // Virtual for testing.
Lalit Maganti5c454312019-04-08 12:11:17 +0100580 virtual StringId InternString(base::StringView str) {
581 return string_pool_.InternString(str);
582 }
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100583
Isabelle Taylora0a22972018-08-03 12:06:12 +0100584 Process* GetMutableProcess(UniquePid upid) {
Lalit Maganti676658b2018-11-20 18:24:31 +0000585 PERFETTO_DCHECK(upid < unique_processes_.size());
Isabelle Taylora0a22972018-08-03 12:06:12 +0100586 return &unique_processes_[upid];
587 }
Isabelle Taylor3dd366c2018-06-22 16:21:41 +0100588
Isabelle Taylora0a22972018-08-03 12:06:12 +0100589 Thread* GetMutableThread(UniqueTid utid) {
Florian Mayera1aaec22018-09-19 17:02:26 +0100590 PERFETTO_DCHECK(utid < unique_threads_.size());
Isabelle Taylora0a22972018-08-03 12:06:12 +0100591 return &unique_threads_[utid];
592 }
Isabelle Taylor3dd366c2018-06-22 16:21:41 +0100593
Primiano Tucci0e38a142019-01-07 20:51:09 +0000594 // Example usage: SetStats(stats::android_log_num_failed, 42);
595 void SetStats(size_t key, int64_t value) {
596 PERFETTO_DCHECK(key < stats::kNumKeys);
597 PERFETTO_DCHECK(stats::kTypes[key] == stats::kSingle);
598 stats_[key].value = value;
599 }
600
601 // Example usage: IncrementStats(stats::android_log_num_failed, -1);
602 void IncrementStats(size_t key, int64_t increment = 1) {
603 PERFETTO_DCHECK(key < stats::kNumKeys);
604 PERFETTO_DCHECK(stats::kTypes[key] == stats::kSingle);
605 stats_[key].value += increment;
606 }
607
608 // Example usage: SetIndexedStats(stats::cpu_failure, 1, 42);
609 void SetIndexedStats(size_t key, int index, int64_t value) {
610 PERFETTO_DCHECK(key < stats::kNumKeys);
611 PERFETTO_DCHECK(stats::kTypes[key] == stats::kIndexed);
612 stats_[key].indexed_values[index] = value;
613 }
614
Lalit Maganticaed37e2018-06-01 03:03:08 +0100615 // Reading methods.
Lalit Maganti5c454312019-04-08 12:11:17 +0100616 NullTermStringView GetString(StringId id) const {
617 return string_pool_.Get(id);
Isabelle Taylora0a22972018-08-03 12:06:12 +0100618 }
619
Lalit Magantie9d40532018-06-29 13:15:06 +0100620 const Process& GetProcess(UniquePid upid) const {
Lalit Maganti676658b2018-11-20 18:24:31 +0000621 PERFETTO_DCHECK(upid < unique_processes_.size());
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100622 return unique_processes_[upid];
623 }
624
Lalit Magantie9d40532018-06-29 13:15:06 +0100625 const Thread& GetThread(UniqueTid utid) const {
Lalit Maganti1f64cfa2018-09-18 13:20:02 +0100626 // Allow utid == 0 for idle thread retrieval.
Florian Mayera1aaec22018-09-19 17:02:26 +0100627 PERFETTO_DCHECK(utid < unique_threads_.size());
Isabelle Taylor68e42192018-06-19 16:19:31 +0100628 return unique_threads_[utid];
629 }
630
Lalit Maganti5ea9e932018-11-30 14:19:39 +0000631 static RowId CreateRowId(TableId table, uint32_t row) {
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000632 return (static_cast<RowId>(table) << kRowIdTableShift) | row;
Lalit Maganti5ea9e932018-11-30 14:19:39 +0000633 }
634
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000635 static std::pair<int8_t /*table*/, uint32_t /*row*/> ParseRowId(RowId rowid) {
636 auto id = static_cast<uint64_t>(rowid);
637 auto table_id = static_cast<uint8_t>(id >> kRowIdTableShift);
638 auto row = static_cast<uint32_t>(id & ((1ull << kRowIdTableShift) - 1));
639 return std::make_pair(table_id, row);
640 }
641
Lalit Magantiff69c112018-09-24 12:07:47 +0100642 const Slices& slices() const { return slices_; }
Lalit Magantifde29042018-10-04 13:28:52 +0100643 Slices* mutable_slices() { return &slices_; }
644
Primiano Tucci0d72a312018-08-07 14:42:45 +0100645 const NestableSlices& nestable_slices() const { return nestable_slices_; }
646 NestableSlices* mutable_nestable_slices() { return &nestable_slices_; }
647
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000648 const CounterDefinitions& counter_definitions() const {
649 return counter_definitions_;
650 }
651 CounterDefinitions* mutable_counter_definitions() {
652 return &counter_definitions_;
653 }
654
655 const CounterValues& counter_values() const { return counter_values_; }
656 CounterValues* mutable_counter_values() { return &counter_values_; }
Isabelle Taylor14674d42018-09-07 11:33:11 +0100657
Primiano Tucci5cb84f82018-10-31 21:46:36 -0700658 const SqlStats& sql_stats() const { return sql_stats_; }
659 SqlStats* mutable_sql_stats() { return &sql_stats_; }
660
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000661 const Instants& instants() const { return instants_; }
662 Instants* mutable_instants() { return &instants_; }
663
Primiano Tucci2c761ef2019-01-07 20:20:46 +0000664 const AndroidLogs& android_logs() const { return android_log_; }
665 AndroidLogs* mutable_android_log() { return &android_log_; }
666
Primiano Tucci0e38a142019-01-07 20:51:09 +0000667 const StatsMap& stats() const { return stats_; }
Lalit Maganti05e8c132018-11-09 18:16:12 +0000668
Lalit Maganti6e9c55e2018-11-29 12:00:39 +0000669 const Args& args() const { return args_; }
670 Args* mutable_args() { return &args_; }
671
Lalit Maganti1d915a62019-01-07 12:10:42 +0000672 const RawEvents& raw_events() const { return raw_events_; }
673 RawEvents* mutable_raw_events() { return &raw_events_; }
674
Lalit Maganti5c454312019-04-08 12:11:17 +0100675 const StringPool& string_pool() const { return string_pool_; }
Lalit Magantiacda68b2018-10-29 15:23:25 +0000676
Isabelle Taylore7003fb2018-07-17 11:39:01 +0100677 // |unique_processes_| always contains at least 1 element becuase the 0th ID
678 // is reserved to indicate an invalid process.
Lalit Maganti9c6395b2019-01-17 00:25:09 +0000679 size_t process_count() const { return unique_processes_.size(); }
Primiano Tucci0d72a312018-08-07 14:42:45 +0100680
Isabelle Taylore7003fb2018-07-17 11:39:01 +0100681 // |unique_threads_| always contains at least 1 element becuase the 0th ID
682 // is reserved to indicate an invalid thread.
Lalit Maganti9c6395b2019-01-17 00:25:09 +0000683 size_t thread_count() const { return unique_threads_.size(); }
Isabelle Taylore7003fb2018-07-17 11:39:01 +0100684
Hector Dearman12323362018-08-09 16:09:28 +0100685 // Number of interned strings in the pool. Includes the empty string w/ ID=0.
686 size_t string_count() const { return string_pool_.size(); }
687
Ioannis Ilkosb8b11102019-01-29 17:56:55 +0000688 // Start / end ts (in nanoseconds) across the parsed trace events.
689 // Returns (0, 0) if the trace is empty.
690 std::pair<int64_t, int64_t> GetTraceTimestampBoundsNs() const;
691
Lalit Maganticaed37e2018-06-01 03:03:08 +0100692 private:
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000693 static constexpr uint8_t kRowIdTableShift = 32;
Isabelle Taylora0a22972018-08-03 12:06:12 +0100694
Primiano Tucci2da5d2e2018-08-10 14:23:31 +0100695 using StringHash = uint64_t;
Lalit Maganticaed37e2018-06-01 03:03:08 +0100696
Lalit Maganti5c454312019-04-08 12:11:17 +0100697 TraceStorage(const TraceStorage&) = delete;
698 TraceStorage& operator=(const TraceStorage&) = delete;
699
700 TraceStorage(TraceStorage&&) = default;
701 TraceStorage& operator=(TraceStorage&&) = default;
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000702
Lalit Maganti05e8c132018-11-09 18:16:12 +0000703 // Stats about parsing the trace.
Primiano Tucci0e38a142019-01-07 20:51:09 +0000704 StatsMap stats_{};
Lalit Maganti35622b72018-06-06 12:03:11 +0100705
Lalit Maganticaed37e2018-06-01 03:03:08 +0100706 // One entry for each CPU in the trace.
Lalit Magantiff69c112018-09-24 12:07:47 +0100707 Slices slices_;
Lalit Maganticaed37e2018-06-01 03:03:08 +0100708
Lalit Maganti6e9c55e2018-11-29 12:00:39 +0000709 // Args for all other tables.
710 Args args_;
711
Lalit Maganticaed37e2018-06-01 03:03:08 +0100712 // One entry for each unique string in the trace.
Lalit Maganti5c454312019-04-08 12:11:17 +0100713 StringPool string_pool_;
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100714
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100715 // One entry for each UniquePid, with UniquePid as the index.
Ioannis Ilkos9c03cbd2019-03-12 18:30:43 +0000716 // Never hold on to pointers to Process, as vector resize will
717 // invalidate them.
718 std::vector<Process> unique_processes_;
Isabelle Taylor68e42192018-06-19 16:19:31 +0100719
Isabelle Taylor68e42192018-06-19 16:19:31 +0100720 // One entry for each UniqueTid, with UniqueTid as the index.
Isabelle Taylor3dd366c2018-06-22 16:21:41 +0100721 std::deque<Thread> unique_threads_;
Primiano Tucci0d72a312018-08-07 14:42:45 +0100722
723 // Slices coming from userspace events (e.g. Chromium TRACE_EVENT macros).
724 NestableSlices nestable_slices_;
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100725
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000726 // The type of counters in the trace. Can be thought of the the "metadata".
727 CounterDefinitions counter_definitions_;
728
729 // The values from the Counter events from the trace. This includes CPU
730 // frequency events as well systrace trace_marker counter events.
731 CounterValues counter_values_;
Primiano Tucci5cb84f82018-10-31 21:46:36 -0700732
733 SqlStats sql_stats_;
Lalit Maganti6e9c55e2018-11-29 12:00:39 +0000734
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000735 // These are instantaneous events in the trace. They have no duration
736 // and do not have a value that make sense to track over time.
737 // e.g. signal events
738 Instants instants_;
Lalit Maganti1d915a62019-01-07 12:10:42 +0000739
740 // Raw events are every ftrace event in the trace. The raw event includes
741 // the timestamp and the pid. The args for the raw event will be in the
742 // args table. This table can be used to generate a text version of the
743 // trace.
744 RawEvents raw_events_;
Primiano Tucci2c761ef2019-01-07 20:20:46 +0000745 AndroidLogs android_log_;
Lalit Maganticaed37e2018-06-01 03:03:08 +0100746};
747
748} // namespace trace_processor
749} // namespace perfetto
750
751#endif // SRC_TRACE_PROCESSOR_TRACE_STORAGE_H_