blob: ac8d63b0a80f7400c5a5166dd69fe6eb3c5eeddf [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
Lalit Maganticaed37e2018-06-01 03:03:08 +010081// Stores a data inside a trace file in a columnar form. This makes it efficient
82// to read or search across a single field of the trace (e.g. all the thread
83// names for a given CPU).
84class TraceStorage {
85 public:
Isabelle Taylor47328cf2018-06-12 14:33:59 +010086 TraceStorage();
Isabelle Taylora0a22972018-08-03 12:06:12 +010087
88 virtual ~TraceStorage();
Isabelle Taylor47328cf2018-06-12 14:33:59 +010089
Isabelle Taylora0a22972018-08-03 12:06:12 +010090 // Information about a unique process seen in a trace.
91 struct Process {
Primiano Tuccib75dcee2018-08-08 12:21:36 +010092 explicit Process(uint32_t p) : pid(p) {}
Lalit Maganti85ca4a82018-12-07 17:28:02 +000093 int64_t start_ns = 0;
Isabelle Taylora0a22972018-08-03 12:06:12 +010094 StringId name_id = 0;
Primiano Tuccib75dcee2018-08-08 12:21:36 +010095 uint32_t pid = 0;
Lalit Maganti08884242019-02-19 12:28:32 +000096 base::Optional<UniquePid> pupid;
Isabelle Taylora0a22972018-08-03 12:06:12 +010097 };
98
99 // Information about a unique thread seen in a trace.
100 struct Thread {
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100101 explicit Thread(uint32_t t) : tid(t) {}
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000102 int64_t start_ns = 0;
Isabelle Taylora0a22972018-08-03 12:06:12 +0100103 StringId name_id = 0;
Lalit Maganti770886a2018-11-16 17:40:21 +0000104 base::Optional<UniquePid> upid;
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100105 uint32_t tid = 0;
Isabelle Taylora0a22972018-08-03 12:06:12 +0100106 };
Isabelle Taylor3dd366c2018-06-22 16:21:41 +0100107
Lalit Maganti6e9c55e2018-11-29 12:00:39 +0000108 // Generic key value storage which can be referenced by other tables.
109 class Args {
110 public:
Lalit Maganti1d915a62019-01-07 12:10:42 +0000111 // Variadic type representing the possible values for the args table.
112 struct Variadic {
Lalit Maganti6e9c55e2018-11-29 12:00:39 +0000113 enum Type { kInt, kString, kReal };
114
Lalit Maganti1d915a62019-01-07 12:10:42 +0000115 static Variadic Integer(int64_t int_value) {
116 Variadic variadic;
117 variadic.type = Type::kInt;
118 variadic.int_value = int_value;
119 return variadic;
120 }
121
122 static Variadic String(StringId string_id) {
123 Variadic variadic;
124 variadic.type = Type::kString;
125 variadic.string_value = string_id;
126 return variadic;
127 }
128
129 static Variadic Real(double real_value) {
130 Variadic variadic;
131 variadic.type = Type::kReal;
132 variadic.real_value = real_value;
133 return variadic;
134 }
Lalit Maganti6e9c55e2018-11-29 12:00:39 +0000135
136 Type type;
137 union {
138 int64_t int_value;
139 StringId string_value;
140 double real_value;
141 };
142 };
143
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000144 struct Arg {
145 StringId flat_key = 0;
146 StringId key = 0;
147 Variadic value = Variadic::Integer(0);
148
149 // This is only used by the arg tracker and so is not part of the hash.
150 RowId row_id = 0;
151 };
152
153 struct ArgHasher {
154 uint64_t operator()(const Arg& arg) const noexcept {
Lalit Maganti1f464742019-02-28 13:49:31 +0000155 base::Hash hash;
156 hash.Update(arg.key);
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000157 // We don't hash arg.flat_key because it's a subsequence of arg.key.
158 switch (arg.value.type) {
159 case Variadic::Type::kInt:
Lalit Maganti1f464742019-02-28 13:49:31 +0000160 hash.Update(arg.value.int_value);
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000161 break;
162 case Variadic::Type::kString:
Lalit Maganti1f464742019-02-28 13:49:31 +0000163 hash.Update(arg.value.string_value);
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000164 break;
165 case Variadic::Type::kReal:
Lalit Maganti1f464742019-02-28 13:49:31 +0000166 hash.Update(arg.value.real_value);
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000167 break;
168 }
Lalit Maganti1f464742019-02-28 13:49:31 +0000169 return hash.digest();
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000170 }
171 };
172
173 const std::deque<ArgSetId>& set_ids() const { return set_ids_; }
Lalit Maganti5ea9e932018-11-30 14:19:39 +0000174 const std::deque<StringId>& flat_keys() const { return flat_keys_; }
175 const std::deque<StringId>& keys() const { return keys_; }
Lalit Maganti1d915a62019-01-07 12:10:42 +0000176 const std::deque<Variadic>& arg_values() const { return arg_values_; }
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000177 uint32_t args_count() const {
178 return static_cast<uint32_t>(set_ids_.size());
Lalit Maganti79472be2018-12-04 13:41:27 +0000179 }
Lalit Maganti6e9c55e2018-11-29 12:00:39 +0000180
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000181 ArgSetId AddArgSet(const std::vector<Arg>& args,
182 uint32_t begin,
183 uint32_t end) {
Lalit Maganti1f464742019-02-28 13:49:31 +0000184 base::Hash hash;
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000185 for (uint32_t i = begin; i < end; i++) {
Lalit Maganti1f464742019-02-28 13:49:31 +0000186 hash.Update(ArgHasher()(args[i]));
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000187 }
Lalit Maganti5ea9e932018-11-30 14:19:39 +0000188
Lalit Maganti1f464742019-02-28 13:49:31 +0000189 ArgSetHash digest = hash.digest();
190 auto it = arg_row_for_hash_.find(digest);
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000191 if (it != arg_row_for_hash_.end()) {
192 return set_ids_[it->second];
193 }
194
195 // The +1 ensures that nothing has an id == kInvalidArgSetId == 0.
196 ArgSetId id = static_cast<uint32_t>(arg_row_for_hash_.size()) + 1;
Lalit Maganti1f464742019-02-28 13:49:31 +0000197 arg_row_for_hash_.emplace(digest, args_count());
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000198 for (uint32_t i = begin; i < end; i++) {
199 const auto& arg = args[i];
200 set_ids_.emplace_back(id);
201 flat_keys_.emplace_back(arg.flat_key);
202 keys_.emplace_back(arg.key);
203 arg_values_.emplace_back(arg.value);
204 }
205 return id;
Lalit Maganti6e9c55e2018-11-29 12:00:39 +0000206 }
207
Lalit Maganti5ea9e932018-11-30 14:19:39 +0000208 private:
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000209 using ArgSetHash = uint64_t;
210
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000211 std::deque<ArgSetId> set_ids_;
Lalit Maganti5ea9e932018-11-30 14:19:39 +0000212 std::deque<StringId> flat_keys_;
213 std::deque<StringId> keys_;
Lalit Maganti1d915a62019-01-07 12:10:42 +0000214 std::deque<Variadic> arg_values_;
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000215
216 std::unordered_map<ArgSetHash, uint32_t> arg_row_for_hash_;
Lalit Maganti6e9c55e2018-11-29 12:00:39 +0000217 };
218
Lalit Magantiff69c112018-09-24 12:07:47 +0100219 class Slices {
Lalit Maganti35622b72018-06-06 12:03:11 +0100220 public:
Lalit Magantifde29042018-10-04 13:28:52 +0100221 inline size_t AddSlice(uint32_t cpu,
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000222 int64_t start_ns,
223 int64_t duration_ns,
Lalit Magantib0b53ee2019-01-24 17:53:39 +0000224 UniqueTid utid,
225 ftrace_utils::TaskState end_state,
226 int32_t priority) {
Lalit Magantiff69c112018-09-24 12:07:47 +0100227 cpus_.emplace_back(cpu);
Lalit Maganti35622b72018-06-06 12:03:11 +0100228 start_ns_.emplace_back(start_ns);
229 durations_.emplace_back(duration_ns);
Isabelle Taylora0a22972018-08-03 12:06:12 +0100230 utids_.emplace_back(utid);
Lalit Magantib0b53ee2019-01-24 17:53:39 +0000231 end_states_.emplace_back(end_state);
232 priorities_.emplace_back(priority);
Lalit Maganti58638932019-01-31 10:48:26 +0000233
234 if (utid >= rows_for_utids_.size())
235 rows_for_utids_.resize(utid + 1);
236 rows_for_utids_[utid].emplace_back(slice_count() - 1);
Lalit Magantifde29042018-10-04 13:28:52 +0100237 return slice_count() - 1;
238 }
239
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000240 void set_duration(size_t index, int64_t duration_ns) {
Lalit Magantifde29042018-10-04 13:28:52 +0100241 durations_[index] = duration_ns;
Lalit Maganti35622b72018-06-06 12:03:11 +0100242 }
243
Lalit Magantib0b53ee2019-01-24 17:53:39 +0000244 void set_end_state(size_t index, ftrace_utils::TaskState end_state) {
245 end_states_[index] = end_state;
246 }
247
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100248 size_t slice_count() const { return start_ns_.size(); }
Lalit Maganti35622b72018-06-06 12:03:11 +0100249
Lalit Magantiff69c112018-09-24 12:07:47 +0100250 const std::deque<uint32_t>& cpus() const { return cpus_; }
251
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000252 const std::deque<int64_t>& start_ns() const { return start_ns_; }
Lalit Maganti35622b72018-06-06 12:03:11 +0100253
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000254 const std::deque<int64_t>& durations() const { return durations_; }
Lalit Maganti35622b72018-06-06 12:03:11 +0100255
Isabelle Taylor68e42192018-06-19 16:19:31 +0100256 const std::deque<UniqueTid>& utids() const { return utids_; }
257
Lalit Magantib0b53ee2019-01-24 17:53:39 +0000258 const std::deque<ftrace_utils::TaskState>& end_state() const {
259 return end_states_;
260 }
261
262 const std::deque<int32_t>& priorities() const { return priorities_; }
263
Lalit Maganti58638932019-01-31 10:48:26 +0000264 const std::deque<std::vector<uint32_t>>& rows_for_utids() const {
Lalit Magantif0f09c32019-01-18 17:29:31 +0000265 return rows_for_utids_;
266 }
267
Lalit Maganti35622b72018-06-06 12:03:11 +0100268 private:
Hector Dearman947f12a2018-09-11 16:50:36 +0100269 // Each deque below has the same number of entries (the number of slices
Lalit Maganti35622b72018-06-06 12:03:11 +0100270 // in the trace for the CPU).
Lalit Magantiff69c112018-09-24 12:07:47 +0100271 std::deque<uint32_t> cpus_;
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000272 std::deque<int64_t> start_ns_;
273 std::deque<int64_t> durations_;
Isabelle Taylor68e42192018-06-19 16:19:31 +0100274 std::deque<UniqueTid> utids_;
Lalit Magantib0b53ee2019-01-24 17:53:39 +0000275 std::deque<ftrace_utils::TaskState> end_states_;
276 std::deque<int32_t> priorities_;
Lalit Maganti58638932019-01-31 10:48:26 +0000277
278 // One row per utid.
279 std::deque<std::vector<uint32_t>> rows_for_utids_;
Primiano Tucci0d72a312018-08-07 14:42:45 +0100280 };
Isabelle Taylor68e42192018-06-19 16:19:31 +0100281
Primiano Tucci0d72a312018-08-07 14:42:45 +0100282 class NestableSlices {
283 public:
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000284 inline size_t AddSlice(int64_t start_ns,
285 int64_t duration_ns,
Lalit Maganti7b37bbf2018-11-09 15:58:54 +0000286 UniqueTid utid,
287 StringId cat,
288 StringId name,
289 uint8_t depth,
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000290 int64_t stack_id,
291 int64_t parent_stack_id) {
Primiano Tucci0d72a312018-08-07 14:42:45 +0100292 start_ns_.emplace_back(start_ns);
293 durations_.emplace_back(duration_ns);
294 utids_.emplace_back(utid);
295 cats_.emplace_back(cat);
296 names_.emplace_back(name);
297 depths_.emplace_back(depth);
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100298 stack_ids_.emplace_back(stack_id);
299 parent_stack_ids_.emplace_back(parent_stack_id);
Lalit Maganti7b37bbf2018-11-09 15:58:54 +0000300 return slice_count() - 1;
301 }
302
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000303 void set_duration(size_t index, int64_t duration_ns) {
Lalit Maganti7b37bbf2018-11-09 15:58:54 +0000304 durations_[index] = duration_ns;
305 }
306
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000307 void set_stack_id(size_t index, int64_t stack_id) {
Lalit Maganti7b37bbf2018-11-09 15:58:54 +0000308 stack_ids_[index] = stack_id;
Primiano Tucci0d72a312018-08-07 14:42:45 +0100309 }
310
311 size_t slice_count() const { return start_ns_.size(); }
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000312 const std::deque<int64_t>& start_ns() const { return start_ns_; }
313 const std::deque<int64_t>& durations() const { return durations_; }
Primiano Tucci0d72a312018-08-07 14:42:45 +0100314 const std::deque<UniqueTid>& utids() const { return utids_; }
315 const std::deque<StringId>& cats() const { return cats_; }
316 const std::deque<StringId>& names() const { return names_; }
317 const std::deque<uint8_t>& depths() const { return depths_; }
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000318 const std::deque<int64_t>& stack_ids() const { return stack_ids_; }
319 const std::deque<int64_t>& parent_stack_ids() const {
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100320 return parent_stack_ids_;
321 }
Primiano Tucci0d72a312018-08-07 14:42:45 +0100322
323 private:
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000324 std::deque<int64_t> start_ns_;
325 std::deque<int64_t> durations_;
Primiano Tucci0d72a312018-08-07 14:42:45 +0100326 std::deque<UniqueTid> utids_;
327 std::deque<StringId> cats_;
328 std::deque<StringId> names_;
329 std::deque<uint8_t> depths_;
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000330 std::deque<int64_t> stack_ids_;
331 std::deque<int64_t> parent_stack_ids_;
Lalit Maganti35622b72018-06-06 12:03:11 +0100332 };
333
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000334 class CounterDefinitions {
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100335 public:
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000336 using Id = uint32_t;
337
338 inline Id AddCounterDefinition(StringId name_id,
339 int64_t ref,
340 RefType type) {
341 base::Hash hash;
342 hash.Update(name_id);
343 hash.Update(ref);
344 hash.Update(type);
345
346 // TODO(lalitm): this is a perf bottleneck and likely we can do something
347 // quite a bit better here.
348 uint64_t digest = hash.digest();
349 auto it = hash_to_row_idx_.find(digest);
350 if (it != hash_to_row_idx_.end())
351 return it->second;
352
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100353 name_ids_.emplace_back(name_id);
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100354 refs_.emplace_back(ref);
355 types_.emplace_back(type);
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000356 hash_to_row_idx_.emplace(digest, size() - 1);
357 return size() - 1;
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100358 }
Lalit Magantifde29042018-10-04 13:28:52 +0100359
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000360 uint32_t size() const { return static_cast<uint32_t>(name_ids_.size()); }
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100361
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100362 const std::deque<StringId>& name_ids() const { return name_ids_; }
363
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100364 const std::deque<int64_t>& refs() const { return refs_; }
365
366 const std::deque<RefType>& types() const { return types_; }
367
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000368 private:
369 std::deque<StringId> name_ids_;
370 std::deque<int64_t> refs_;
371 std::deque<RefType> types_;
372
373 std::unordered_map<uint64_t, uint32_t> hash_to_row_idx_;
374 };
375
376 class CounterValues {
377 public:
378 inline uint32_t AddCounterValue(CounterDefinitions::Id counter_id,
379 int64_t timestamp,
380 double value) {
381 counter_ids_.emplace_back(counter_id);
382 timestamps_.emplace_back(timestamp);
383 values_.emplace_back(value);
384 arg_set_ids_.emplace_back(kInvalidArgSetId);
385 return size() - 1;
386 }
387
388 void set_arg_set_id(uint32_t row, ArgSetId id) { arg_set_ids_[row] = id; }
389
390 uint32_t size() const { return static_cast<uint32_t>(counter_ids_.size()); }
391
392 const std::deque<CounterDefinitions::Id>& counter_ids() const {
393 return counter_ids_;
394 }
395
396 const std::deque<int64_t>& timestamps() const { return timestamps_; }
397
398 const std::deque<double>& values() const { return values_; }
399
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000400 const std::deque<ArgSetId>& arg_set_ids() const { return arg_set_ids_; }
401
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100402 private:
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000403 std::deque<CounterDefinitions::Id> counter_ids_;
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000404 std::deque<int64_t> timestamps_;
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100405 std::deque<double> values_;
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000406 std::deque<ArgSetId> arg_set_ids_;
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100407 };
408
Primiano Tucci5cb84f82018-10-31 21:46:36 -0700409 class SqlStats {
410 public:
411 static constexpr size_t kMaxLogEntries = 100;
412 void RecordQueryBegin(const std::string& query,
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000413 int64_t time_queued,
414 int64_t time_started);
415 void RecordQueryEnd(int64_t time_ended);
Primiano Tucci5cb84f82018-10-31 21:46:36 -0700416 size_t size() const { return queries_.size(); }
417 const std::deque<std::string>& queries() const { return queries_; }
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000418 const std::deque<int64_t>& times_queued() const { return times_queued_; }
419 const std::deque<int64_t>& times_started() const { return times_started_; }
420 const std::deque<int64_t>& times_ended() const { return times_ended_; }
Primiano Tucci5cb84f82018-10-31 21:46:36 -0700421
422 private:
423 std::deque<std::string> queries_;
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000424 std::deque<int64_t> times_queued_;
425 std::deque<int64_t> times_started_;
426 std::deque<int64_t> times_ended_;
Primiano Tucci5cb84f82018-10-31 21:46:36 -0700427 };
428
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000429 class Instants {
430 public:
Lalit Maganti66ed7ad2019-01-11 16:47:26 +0000431 inline uint32_t AddInstantEvent(int64_t timestamp,
432 StringId name_id,
433 double value,
434 int64_t ref,
435 RefType type) {
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000436 timestamps_.emplace_back(timestamp);
437 name_ids_.emplace_back(name_id);
438 values_.emplace_back(value);
439 refs_.emplace_back(ref);
440 types_.emplace_back(type);
Lalit Maganti1c21d172019-02-07 10:48:24 +0000441 arg_set_ids_.emplace_back(kInvalidArgSetId);
Lalit Maganti66ed7ad2019-01-11 16:47:26 +0000442 return static_cast<uint32_t>(instant_count() - 1);
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000443 }
444
Lalit Maganti1c21d172019-02-07 10:48:24 +0000445 void set_arg_set_id(uint32_t row, ArgSetId id) { arg_set_ids_[row] = id; }
446
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000447 size_t instant_count() const { return timestamps_.size(); }
448
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000449 const std::deque<int64_t>& timestamps() const { return timestamps_; }
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000450
451 const std::deque<StringId>& name_ids() const { return name_ids_; }
452
453 const std::deque<double>& values() const { return values_; }
454
455 const std::deque<int64_t>& refs() const { return refs_; }
456
457 const std::deque<RefType>& types() const { return types_; }
458
Lalit Maganti1c21d172019-02-07 10:48:24 +0000459 const std::deque<ArgSetId>& arg_set_ids() const { return arg_set_ids_; }
460
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000461 private:
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000462 std::deque<int64_t> timestamps_;
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000463 std::deque<StringId> name_ids_;
464 std::deque<double> values_;
465 std::deque<int64_t> refs_;
466 std::deque<RefType> types_;
Lalit Maganti1c21d172019-02-07 10:48:24 +0000467 std::deque<ArgSetId> arg_set_ids_;
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000468 };
469
Lalit Maganti1d915a62019-01-07 12:10:42 +0000470 class RawEvents {
471 public:
472 inline RowId AddRawEvent(int64_t timestamp,
473 StringId name_id,
Lalit Magantie87cc812019-01-10 15:20:06 +0000474 uint32_t cpu,
Lalit Maganti1d915a62019-01-07 12:10:42 +0000475 UniqueTid utid) {
476 timestamps_.emplace_back(timestamp);
477 name_ids_.emplace_back(name_id);
Lalit Magantie87cc812019-01-10 15:20:06 +0000478 cpus_.emplace_back(cpu);
Lalit Maganti1d915a62019-01-07 12:10:42 +0000479 utids_.emplace_back(utid);
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000480 arg_set_ids_.emplace_back(kInvalidArgSetId);
Lalit Maganti1d915a62019-01-07 12:10:42 +0000481 return CreateRowId(TableId::kRawEvents,
482 static_cast<uint32_t>(raw_event_count() - 1));
483 }
484
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000485 void set_arg_set_id(uint32_t row, ArgSetId id) { arg_set_ids_[row] = id; }
486
Lalit Maganti1d915a62019-01-07 12:10:42 +0000487 size_t raw_event_count() const { return timestamps_.size(); }
488
489 const std::deque<int64_t>& timestamps() const { return timestamps_; }
490
491 const std::deque<StringId>& name_ids() const { return name_ids_; }
492
Lalit Magantie87cc812019-01-10 15:20:06 +0000493 const std::deque<uint32_t>& cpus() const { return cpus_; }
494
Lalit Maganti1d915a62019-01-07 12:10:42 +0000495 const std::deque<UniqueTid>& utids() const { return utids_; }
496
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000497 const std::deque<ArgSetId>& arg_set_ids() const { return arg_set_ids_; }
498
Lalit Maganti1d915a62019-01-07 12:10:42 +0000499 private:
500 std::deque<int64_t> timestamps_;
501 std::deque<StringId> name_ids_;
Lalit Magantie87cc812019-01-10 15:20:06 +0000502 std::deque<uint32_t> cpus_;
Lalit Maganti1d915a62019-01-07 12:10:42 +0000503 std::deque<UniqueTid> utids_;
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000504 std::deque<ArgSetId> arg_set_ids_;
Lalit Maganti1d915a62019-01-07 12:10:42 +0000505 };
506
Primiano Tucci2c761ef2019-01-07 20:20:46 +0000507 class AndroidLogs {
508 public:
509 inline size_t AddLogEvent(int64_t timestamp,
510 UniqueTid utid,
511 uint8_t prio,
512 StringId tag_id,
513 StringId msg_id) {
514 timestamps_.emplace_back(timestamp);
515 utids_.emplace_back(utid);
516 prios_.emplace_back(prio);
517 tag_ids_.emplace_back(tag_id);
518 msg_ids_.emplace_back(msg_id);
519 return size() - 1;
520 }
521
522 size_t size() const { return timestamps_.size(); }
523
524 const std::deque<int64_t>& timestamps() const { return timestamps_; }
525 const std::deque<UniqueTid>& utids() const { return utids_; }
526 const std::deque<uint8_t>& prios() const { return prios_; }
527 const std::deque<StringId>& tag_ids() const { return tag_ids_; }
528 const std::deque<StringId>& msg_ids() const { return msg_ids_; }
529
530 private:
531 std::deque<int64_t> timestamps_;
532 std::deque<UniqueTid> utids_;
533 std::deque<uint8_t> prios_;
534 std::deque<StringId> tag_ids_;
535 std::deque<StringId> msg_ids_;
536 };
537
Primiano Tucci0e38a142019-01-07 20:51:09 +0000538 struct Stats {
539 using IndexMap = std::map<int, int64_t>;
540 int64_t value = 0;
541 IndexMap indexed_values;
542 };
543 using StatsMap = std::array<Stats, stats::kNumKeys>;
544
Isabelle Taylora0a22972018-08-03 12:06:12 +0100545 void ResetStorage();
Lalit Maganti35622b72018-06-06 12:03:11 +0100546
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100547 UniqueTid AddEmptyThread(uint32_t tid) {
548 unique_threads_.emplace_back(tid);
Isabelle Taylora0a22972018-08-03 12:06:12 +0100549 return static_cast<UniqueTid>(unique_threads_.size() - 1);
550 }
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100551
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100552 UniquePid AddEmptyProcess(uint32_t pid) {
553 unique_processes_.emplace_back(pid);
Isabelle Taylora0a22972018-08-03 12:06:12 +0100554 return static_cast<UniquePid>(unique_processes_.size() - 1);
555 }
Isabelle Taylor3dd366c2018-06-22 16:21:41 +0100556
Isabelle Taylora0a22972018-08-03 12:06:12 +0100557 // Return an unqiue identifier for the contents of each string.
558 // The string is copied internally and can be destroyed after this called.
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100559 // Virtual for testing.
Lalit Maganti5c454312019-04-08 12:11:17 +0100560 virtual StringId InternString(base::StringView str) {
561 return string_pool_.InternString(str);
562 }
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100563
Isabelle Taylora0a22972018-08-03 12:06:12 +0100564 Process* GetMutableProcess(UniquePid upid) {
Lalit Maganti676658b2018-11-20 18:24:31 +0000565 PERFETTO_DCHECK(upid < unique_processes_.size());
Isabelle Taylora0a22972018-08-03 12:06:12 +0100566 return &unique_processes_[upid];
567 }
Isabelle Taylor3dd366c2018-06-22 16:21:41 +0100568
Isabelle Taylora0a22972018-08-03 12:06:12 +0100569 Thread* GetMutableThread(UniqueTid utid) {
Florian Mayera1aaec22018-09-19 17:02:26 +0100570 PERFETTO_DCHECK(utid < unique_threads_.size());
Isabelle Taylora0a22972018-08-03 12:06:12 +0100571 return &unique_threads_[utid];
572 }
Isabelle Taylor3dd366c2018-06-22 16:21:41 +0100573
Primiano Tucci0e38a142019-01-07 20:51:09 +0000574 // Example usage: SetStats(stats::android_log_num_failed, 42);
575 void SetStats(size_t key, int64_t value) {
576 PERFETTO_DCHECK(key < stats::kNumKeys);
577 PERFETTO_DCHECK(stats::kTypes[key] == stats::kSingle);
578 stats_[key].value = value;
579 }
580
581 // Example usage: IncrementStats(stats::android_log_num_failed, -1);
582 void IncrementStats(size_t key, int64_t increment = 1) {
583 PERFETTO_DCHECK(key < stats::kNumKeys);
584 PERFETTO_DCHECK(stats::kTypes[key] == stats::kSingle);
585 stats_[key].value += increment;
586 }
587
588 // Example usage: SetIndexedStats(stats::cpu_failure, 1, 42);
589 void SetIndexedStats(size_t key, int index, int64_t value) {
590 PERFETTO_DCHECK(key < stats::kNumKeys);
591 PERFETTO_DCHECK(stats::kTypes[key] == stats::kIndexed);
592 stats_[key].indexed_values[index] = value;
593 }
594
Lalit Maganticaed37e2018-06-01 03:03:08 +0100595 // Reading methods.
Lalit Maganti5c454312019-04-08 12:11:17 +0100596 NullTermStringView GetString(StringId id) const {
597 return string_pool_.Get(id);
Isabelle Taylora0a22972018-08-03 12:06:12 +0100598 }
599
Lalit Magantie9d40532018-06-29 13:15:06 +0100600 const Process& GetProcess(UniquePid upid) const {
Lalit Maganti676658b2018-11-20 18:24:31 +0000601 PERFETTO_DCHECK(upid < unique_processes_.size());
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100602 return unique_processes_[upid];
603 }
604
Lalit Magantie9d40532018-06-29 13:15:06 +0100605 const Thread& GetThread(UniqueTid utid) const {
Lalit Maganti1f64cfa2018-09-18 13:20:02 +0100606 // Allow utid == 0 for idle thread retrieval.
Florian Mayera1aaec22018-09-19 17:02:26 +0100607 PERFETTO_DCHECK(utid < unique_threads_.size());
Isabelle Taylor68e42192018-06-19 16:19:31 +0100608 return unique_threads_[utid];
609 }
610
Lalit Maganti5ea9e932018-11-30 14:19:39 +0000611 static RowId CreateRowId(TableId table, uint32_t row) {
Lalit Maganti85ca4a82018-12-07 17:28:02 +0000612 return (static_cast<RowId>(table) << kRowIdTableShift) | row;
Lalit Maganti5ea9e932018-11-30 14:19:39 +0000613 }
614
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000615 static std::pair<int8_t /*table*/, uint32_t /*row*/> ParseRowId(RowId rowid) {
616 auto id = static_cast<uint64_t>(rowid);
617 auto table_id = static_cast<uint8_t>(id >> kRowIdTableShift);
618 auto row = static_cast<uint32_t>(id & ((1ull << kRowIdTableShift) - 1));
619 return std::make_pair(table_id, row);
620 }
621
Lalit Magantiff69c112018-09-24 12:07:47 +0100622 const Slices& slices() const { return slices_; }
Lalit Magantifde29042018-10-04 13:28:52 +0100623 Slices* mutable_slices() { return &slices_; }
624
Primiano Tucci0d72a312018-08-07 14:42:45 +0100625 const NestableSlices& nestable_slices() const { return nestable_slices_; }
626 NestableSlices* mutable_nestable_slices() { return &nestable_slices_; }
627
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000628 const CounterDefinitions& counter_definitions() const {
629 return counter_definitions_;
630 }
631 CounterDefinitions* mutable_counter_definitions() {
632 return &counter_definitions_;
633 }
634
635 const CounterValues& counter_values() const { return counter_values_; }
636 CounterValues* mutable_counter_values() { return &counter_values_; }
Isabelle Taylor14674d42018-09-07 11:33:11 +0100637
Primiano Tucci5cb84f82018-10-31 21:46:36 -0700638 const SqlStats& sql_stats() const { return sql_stats_; }
639 SqlStats* mutable_sql_stats() { return &sql_stats_; }
640
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000641 const Instants& instants() const { return instants_; }
642 Instants* mutable_instants() { return &instants_; }
643
Primiano Tucci2c761ef2019-01-07 20:20:46 +0000644 const AndroidLogs& android_logs() const { return android_log_; }
645 AndroidLogs* mutable_android_log() { return &android_log_; }
646
Primiano Tucci0e38a142019-01-07 20:51:09 +0000647 const StatsMap& stats() const { return stats_; }
Lalit Maganti05e8c132018-11-09 18:16:12 +0000648
Lalit Maganti6e9c55e2018-11-29 12:00:39 +0000649 const Args& args() const { return args_; }
650 Args* mutable_args() { return &args_; }
651
Lalit Maganti1d915a62019-01-07 12:10:42 +0000652 const RawEvents& raw_events() const { return raw_events_; }
653 RawEvents* mutable_raw_events() { return &raw_events_; }
654
Lalit Maganti5c454312019-04-08 12:11:17 +0100655 const StringPool& string_pool() const { return string_pool_; }
Lalit Magantiacda68b2018-10-29 15:23:25 +0000656
Isabelle Taylore7003fb2018-07-17 11:39:01 +0100657 // |unique_processes_| always contains at least 1 element becuase the 0th ID
658 // is reserved to indicate an invalid process.
Lalit Maganti9c6395b2019-01-17 00:25:09 +0000659 size_t process_count() const { return unique_processes_.size(); }
Primiano Tucci0d72a312018-08-07 14:42:45 +0100660
Isabelle Taylore7003fb2018-07-17 11:39:01 +0100661 // |unique_threads_| always contains at least 1 element becuase the 0th ID
662 // is reserved to indicate an invalid thread.
Lalit Maganti9c6395b2019-01-17 00:25:09 +0000663 size_t thread_count() const { return unique_threads_.size(); }
Isabelle Taylore7003fb2018-07-17 11:39:01 +0100664
Hector Dearman12323362018-08-09 16:09:28 +0100665 // Number of interned strings in the pool. Includes the empty string w/ ID=0.
666 size_t string_count() const { return string_pool_.size(); }
667
Ioannis Ilkosb8b11102019-01-29 17:56:55 +0000668 // Start / end ts (in nanoseconds) across the parsed trace events.
669 // Returns (0, 0) if the trace is empty.
670 std::pair<int64_t, int64_t> GetTraceTimestampBoundsNs() const;
671
Lalit Maganticaed37e2018-06-01 03:03:08 +0100672 private:
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000673 static constexpr uint8_t kRowIdTableShift = 32;
Isabelle Taylora0a22972018-08-03 12:06:12 +0100674
Primiano Tucci2da5d2e2018-08-10 14:23:31 +0100675 using StringHash = uint64_t;
Lalit Maganticaed37e2018-06-01 03:03:08 +0100676
Lalit Maganti5c454312019-04-08 12:11:17 +0100677 TraceStorage(const TraceStorage&) = delete;
678 TraceStorage& operator=(const TraceStorage&) = delete;
679
680 TraceStorage(TraceStorage&&) = default;
681 TraceStorage& operator=(TraceStorage&&) = default;
Lalit Maganti4fa7c6c2019-02-06 15:06:36 +0000682
Lalit Maganti05e8c132018-11-09 18:16:12 +0000683 // Stats about parsing the trace.
Primiano Tucci0e38a142019-01-07 20:51:09 +0000684 StatsMap stats_{};
Lalit Maganti35622b72018-06-06 12:03:11 +0100685
Lalit Maganticaed37e2018-06-01 03:03:08 +0100686 // One entry for each CPU in the trace.
Lalit Magantiff69c112018-09-24 12:07:47 +0100687 Slices slices_;
Lalit Maganticaed37e2018-06-01 03:03:08 +0100688
Lalit Maganti6e9c55e2018-11-29 12:00:39 +0000689 // Args for all other tables.
690 Args args_;
691
Lalit Maganticaed37e2018-06-01 03:03:08 +0100692 // One entry for each unique string in the trace.
Lalit Maganti5c454312019-04-08 12:11:17 +0100693 StringPool string_pool_;
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100694
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100695 // One entry for each UniquePid, with UniquePid as the index.
Ioannis Ilkos9c03cbd2019-03-12 18:30:43 +0000696 // Never hold on to pointers to Process, as vector resize will
697 // invalidate them.
698 std::vector<Process> unique_processes_;
Isabelle Taylor68e42192018-06-19 16:19:31 +0100699
Isabelle Taylor68e42192018-06-19 16:19:31 +0100700 // One entry for each UniqueTid, with UniqueTid as the index.
Isabelle Taylor3dd366c2018-06-22 16:21:41 +0100701 std::deque<Thread> unique_threads_;
Primiano Tucci0d72a312018-08-07 14:42:45 +0100702
703 // Slices coming from userspace events (e.g. Chromium TRACE_EVENT macros).
704 NestableSlices nestable_slices_;
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100705
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000706 // The type of counters in the trace. Can be thought of the the "metadata".
707 CounterDefinitions counter_definitions_;
708
709 // The values from the Counter events from the trace. This includes CPU
710 // frequency events as well systrace trace_marker counter events.
711 CounterValues counter_values_;
Primiano Tucci5cb84f82018-10-31 21:46:36 -0700712
713 SqlStats sql_stats_;
Lalit Maganti6e9c55e2018-11-29 12:00:39 +0000714
Isabelle Taylorc8c11202018-11-05 11:36:22 +0000715 // These are instantaneous events in the trace. They have no duration
716 // and do not have a value that make sense to track over time.
717 // e.g. signal events
718 Instants instants_;
Lalit Maganti1d915a62019-01-07 12:10:42 +0000719
720 // Raw events are every ftrace event in the trace. The raw event includes
721 // the timestamp and the pid. The args for the raw event will be in the
722 // args table. This table can be used to generate a text version of the
723 // trace.
724 RawEvents raw_events_;
Primiano Tucci2c761ef2019-01-07 20:20:46 +0000725 AndroidLogs android_log_;
Lalit Maganticaed37e2018-06-01 03:03:08 +0100726};
727
728} // namespace trace_processor
729} // namespace perfetto
730
731#endif // SRC_TRACE_PROCESSOR_TRACE_STORAGE_H_