blob: cc59228263eedd3aea8557c1de0cb9abcc1614a5 [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"
Primiano Tucci2da5d2e2018-08-10 14:23:31 +010028#include "perfetto/base/string_view.h"
Hector Dearmanc8339932018-08-10 11:22:46 +010029#include "perfetto/base/utils.h"
Lalit Maganti35622b72018-06-06 12:03:11 +010030
Lalit Maganticaed37e2018-06-01 03:03:08 +010031namespace perfetto {
32namespace trace_processor {
33
Isabelle Taylora0a22972018-08-03 12:06:12 +010034// UniquePid is an offset into |unique_processes_|. This is necessary because
35// Unix pids are reused and thus not guaranteed to be unique over a long
36// period of time.
37using UniquePid = uint32_t;
Primiano Tucci0d72a312018-08-07 14:42:45 +010038
Isabelle Taylora0a22972018-08-03 12:06:12 +010039// UniqueTid is an offset into |unique_threads_|. Necessary because tids can
40// be reused.
41using UniqueTid = uint32_t;
42
Primiano Tucci0d72a312018-08-07 14:42:45 +010043// StringId is an offset into |string_pool_|.
44using StringId = size_t;
45
Isabelle Taylora97c5f52018-10-23 17:36:12 +010046enum RefType {
47 kNoRef = 0,
48 kUtid = 1,
49 kCpuId = 2,
50 kIrq = 3,
51 kSoftIrq = 4,
Primiano Tuccic9d4a8b2018-10-30 20:19:01 -070052 kUpid = 5,
53 kMax = kUpid + 1
Isabelle Taylora97c5f52018-10-23 17:36:12 +010054};
Isabelle Taylor14674d42018-09-07 11:33:11 +010055
Lalit Maganticaed37e2018-06-01 03:03:08 +010056// Stores a data inside a trace file in a columnar form. This makes it efficient
57// to read or search across a single field of the trace (e.g. all the thread
58// names for a given CPU).
59class TraceStorage {
60 public:
Isabelle Taylor47328cf2018-06-12 14:33:59 +010061 TraceStorage();
Isabelle Taylora0a22972018-08-03 12:06:12 +010062 TraceStorage(const TraceStorage&) = delete;
63
64 virtual ~TraceStorage();
Isabelle Taylor47328cf2018-06-12 14:33:59 +010065
Isabelle Taylora0a22972018-08-03 12:06:12 +010066 struct Stats {
67 uint64_t mismatched_sched_switch_tids_ = 0;
68 };
Lalit Maganti35622b72018-06-06 12:03:11 +010069
Isabelle Taylora0a22972018-08-03 12:06:12 +010070 // Information about a unique process seen in a trace.
71 struct Process {
Primiano Tuccib75dcee2018-08-08 12:21:36 +010072 explicit Process(uint32_t p) : pid(p) {}
Isabelle Taylora0a22972018-08-03 12:06:12 +010073 uint64_t start_ns = 0;
74 uint64_t end_ns = 0;
75 StringId name_id = 0;
Primiano Tuccib75dcee2018-08-08 12:21:36 +010076 uint32_t pid = 0;
Isabelle Taylora0a22972018-08-03 12:06:12 +010077 };
78
79 // Information about a unique thread seen in a trace.
80 struct Thread {
Primiano Tuccib75dcee2018-08-08 12:21:36 +010081 explicit Thread(uint32_t t) : tid(t) {}
Isabelle Taylora0a22972018-08-03 12:06:12 +010082 uint64_t start_ns = 0;
83 uint64_t end_ns = 0;
84 StringId name_id = 0;
85 UniquePid upid = 0;
Primiano Tuccib75dcee2018-08-08 12:21:36 +010086 uint32_t tid = 0;
Isabelle Taylora0a22972018-08-03 12:06:12 +010087 };
Isabelle Taylor3dd366c2018-06-22 16:21:41 +010088
Lalit Magantiff69c112018-09-24 12:07:47 +010089 class Slices {
Lalit Maganti35622b72018-06-06 12:03:11 +010090 public:
Lalit Magantifde29042018-10-04 13:28:52 +010091 inline size_t AddSlice(uint32_t cpu,
92 uint64_t start_ns,
93 uint64_t duration_ns,
94 UniqueTid utid) {
Lalit Magantiff69c112018-09-24 12:07:47 +010095 cpus_.emplace_back(cpu);
Lalit Maganti35622b72018-06-06 12:03:11 +010096 start_ns_.emplace_back(start_ns);
97 durations_.emplace_back(duration_ns);
Isabelle Taylora0a22972018-08-03 12:06:12 +010098 utids_.emplace_back(utid);
Lalit Magantifde29042018-10-04 13:28:52 +010099 return slice_count() - 1;
100 }
101
102 void set_duration(size_t index, uint64_t duration_ns) {
103 durations_[index] = duration_ns;
Lalit Maganti35622b72018-06-06 12:03:11 +0100104 }
105
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100106 size_t slice_count() const { return start_ns_.size(); }
Lalit Maganti35622b72018-06-06 12:03:11 +0100107
Lalit Magantiff69c112018-09-24 12:07:47 +0100108 const std::deque<uint32_t>& cpus() const { return cpus_; }
109
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100110 const std::deque<uint64_t>& start_ns() const { return start_ns_; }
Lalit Maganti35622b72018-06-06 12:03:11 +0100111
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100112 const std::deque<uint64_t>& durations() const { return durations_; }
Lalit Maganti35622b72018-06-06 12:03:11 +0100113
Isabelle Taylor68e42192018-06-19 16:19:31 +0100114 const std::deque<UniqueTid>& utids() const { return utids_; }
115
Lalit Maganti35622b72018-06-06 12:03:11 +0100116 private:
Hector Dearman947f12a2018-09-11 16:50:36 +0100117 // Each deque below has the same number of entries (the number of slices
Lalit Maganti35622b72018-06-06 12:03:11 +0100118 // in the trace for the CPU).
Lalit Magantiff69c112018-09-24 12:07:47 +0100119 std::deque<uint32_t> cpus_;
Lalit Maganti35622b72018-06-06 12:03:11 +0100120 std::deque<uint64_t> start_ns_;
121 std::deque<uint64_t> durations_;
Isabelle Taylor68e42192018-06-19 16:19:31 +0100122 std::deque<UniqueTid> utids_;
Primiano Tucci0d72a312018-08-07 14:42:45 +0100123 };
Isabelle Taylor68e42192018-06-19 16:19:31 +0100124
Primiano Tucci0d72a312018-08-07 14:42:45 +0100125 class NestableSlices {
126 public:
127 inline void AddSlice(uint64_t start_ns,
128 uint64_t duration_ns,
129 UniqueTid utid,
130 StringId cat,
131 StringId name,
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100132 uint8_t depth,
133 uint64_t stack_id,
134 uint64_t parent_stack_id) {
Primiano Tucci0d72a312018-08-07 14:42:45 +0100135 start_ns_.emplace_back(start_ns);
136 durations_.emplace_back(duration_ns);
137 utids_.emplace_back(utid);
138 cats_.emplace_back(cat);
139 names_.emplace_back(name);
140 depths_.emplace_back(depth);
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100141 stack_ids_.emplace_back(stack_id);
142 parent_stack_ids_.emplace_back(parent_stack_id);
Primiano Tucci0d72a312018-08-07 14:42:45 +0100143 }
144
145 size_t slice_count() const { return start_ns_.size(); }
146 const std::deque<uint64_t>& start_ns() const { return start_ns_; }
147 const std::deque<uint64_t>& durations() const { return durations_; }
148 const std::deque<UniqueTid>& utids() const { return utids_; }
149 const std::deque<StringId>& cats() const { return cats_; }
150 const std::deque<StringId>& names() const { return names_; }
151 const std::deque<uint8_t>& depths() const { return depths_; }
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100152 const std::deque<uint64_t>& stack_ids() const { return stack_ids_; }
153 const std::deque<uint64_t>& parent_stack_ids() const {
154 return parent_stack_ids_;
155 }
Primiano Tucci0d72a312018-08-07 14:42:45 +0100156
157 private:
158 std::deque<uint64_t> start_ns_;
159 std::deque<uint64_t> durations_;
160 std::deque<UniqueTid> utids_;
161 std::deque<StringId> cats_;
162 std::deque<StringId> names_;
163 std::deque<uint8_t> depths_;
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100164 std::deque<uint64_t> stack_ids_;
165 std::deque<uint64_t> parent_stack_ids_;
Lalit Maganti35622b72018-06-06 12:03:11 +0100166 };
167
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100168 class Counters {
169 public:
Lalit Magantifde29042018-10-04 13:28:52 +0100170 inline size_t AddCounter(uint64_t timestamp,
171 uint64_t duration,
172 StringId name_id,
173 double value,
Lalit Magantifde29042018-10-04 13:28:52 +0100174 int64_t ref,
175 RefType type) {
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100176 timestamps_.emplace_back(timestamp);
177 durations_.emplace_back(duration);
178 name_ids_.emplace_back(name_id);
179 values_.emplace_back(value);
180 refs_.emplace_back(ref);
181 types_.emplace_back(type);
Lalit Magantifde29042018-10-04 13:28:52 +0100182 return counter_count() - 1;
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100183 }
Lalit Magantifde29042018-10-04 13:28:52 +0100184
185 void set_duration(size_t index, uint64_t duration) {
186 durations_[index] = duration;
187 }
188
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100189 size_t counter_count() const { return timestamps_.size(); }
190
191 const std::deque<uint64_t>& timestamps() const { return timestamps_; }
192
193 const std::deque<uint64_t>& durations() const { return durations_; }
194
195 const std::deque<StringId>& name_ids() const { return name_ids_; }
196
197 const std::deque<double>& values() const { return values_; }
198
199 const std::deque<int64_t>& refs() const { return refs_; }
200
201 const std::deque<RefType>& types() const { return types_; }
202
203 private:
204 std::deque<uint64_t> timestamps_;
205 std::deque<uint64_t> durations_;
206 std::deque<StringId> name_ids_;
207 std::deque<double> values_;
208 std::deque<int64_t> refs_;
209 std::deque<RefType> types_;
210 };
211
Primiano Tucci5cb84f82018-10-31 21:46:36 -0700212 class SqlStats {
213 public:
214 static constexpr size_t kMaxLogEntries = 100;
215 void RecordQueryBegin(const std::string& query,
216 uint64_t time_queued,
217 uint64_t time_started);
218 void RecordQueryEnd(uint64_t time_ended);
219 size_t size() const { return queries_.size(); }
220 const std::deque<std::string>& queries() const { return queries_; }
221 const std::deque<uint64_t>& times_queued() const { return times_queued_; }
222 const std::deque<uint64_t>& times_started() const { return times_started_; }
223 const std::deque<uint64_t>& times_ended() const { return times_ended_; }
224
225 private:
226 std::deque<std::string> queries_;
227 std::deque<uint64_t> times_queued_;
228 std::deque<uint64_t> times_started_;
229 std::deque<uint64_t> times_ended_;
230 };
231
Isabelle Taylora0a22972018-08-03 12:06:12 +0100232 void ResetStorage();
Lalit Maganti35622b72018-06-06 12:03:11 +0100233
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100234 UniqueTid AddEmptyThread(uint32_t tid) {
235 unique_threads_.emplace_back(tid);
Isabelle Taylora0a22972018-08-03 12:06:12 +0100236 return static_cast<UniqueTid>(unique_threads_.size() - 1);
237 }
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100238
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100239 UniquePid AddEmptyProcess(uint32_t pid) {
240 unique_processes_.emplace_back(pid);
Isabelle Taylora0a22972018-08-03 12:06:12 +0100241 return static_cast<UniquePid>(unique_processes_.size() - 1);
242 }
Isabelle Taylor3dd366c2018-06-22 16:21:41 +0100243
Isabelle Taylora0a22972018-08-03 12:06:12 +0100244 void AddMismatchedSchedSwitch() { ++stats_.mismatched_sched_switch_tids_; }
Lalit Maganticaed37e2018-06-01 03:03:08 +0100245
Isabelle Taylora0a22972018-08-03 12:06:12 +0100246 // Return an unqiue identifier for the contents of each string.
247 // The string is copied internally and can be destroyed after this called.
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100248 // Virtual for testing.
249 virtual StringId InternString(base::StringView);
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100250
Isabelle Taylora0a22972018-08-03 12:06:12 +0100251 Process* GetMutableProcess(UniquePid upid) {
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100252 PERFETTO_DCHECK(upid > 0 && upid < unique_processes_.size());
Isabelle Taylora0a22972018-08-03 12:06:12 +0100253 return &unique_processes_[upid];
254 }
Isabelle Taylor3dd366c2018-06-22 16:21:41 +0100255
Isabelle Taylora0a22972018-08-03 12:06:12 +0100256 Thread* GetMutableThread(UniqueTid utid) {
Florian Mayera1aaec22018-09-19 17:02:26 +0100257 PERFETTO_DCHECK(utid < unique_threads_.size());
Isabelle Taylora0a22972018-08-03 12:06:12 +0100258 return &unique_threads_[utid];
259 }
Isabelle Taylor3dd366c2018-06-22 16:21:41 +0100260
Lalit Maganticaed37e2018-06-01 03:03:08 +0100261 // Reading methods.
Isabelle Taylora0a22972018-08-03 12:06:12 +0100262 const std::string& GetString(StringId id) const {
263 PERFETTO_DCHECK(id < string_pool_.size());
264 return string_pool_[id];
265 }
266
Lalit Magantie9d40532018-06-29 13:15:06 +0100267 const Process& GetProcess(UniquePid upid) const {
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100268 PERFETTO_DCHECK(upid > 0 && upid < unique_processes_.size());
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100269 return unique_processes_[upid];
270 }
271
Lalit Magantie9d40532018-06-29 13:15:06 +0100272 const Thread& GetThread(UniqueTid utid) const {
Lalit Maganti1f64cfa2018-09-18 13:20:02 +0100273 // Allow utid == 0 for idle thread retrieval.
Florian Mayera1aaec22018-09-19 17:02:26 +0100274 PERFETTO_DCHECK(utid < unique_threads_.size());
Isabelle Taylor68e42192018-06-19 16:19:31 +0100275 return unique_threads_[utid];
276 }
277
Lalit Magantiff69c112018-09-24 12:07:47 +0100278 const Slices& slices() const { return slices_; }
Lalit Magantifde29042018-10-04 13:28:52 +0100279 Slices* mutable_slices() { return &slices_; }
280
Primiano Tucci0d72a312018-08-07 14:42:45 +0100281 const NestableSlices& nestable_slices() const { return nestable_slices_; }
282 NestableSlices* mutable_nestable_slices() { return &nestable_slices_; }
283
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100284 const Counters& counters() const { return counters_; }
285 Counters* mutable_counters() { return &counters_; }
Isabelle Taylor14674d42018-09-07 11:33:11 +0100286
Primiano Tucci5cb84f82018-10-31 21:46:36 -0700287 const SqlStats& sql_stats() const { return sql_stats_; }
288 SqlStats* mutable_sql_stats() { return &sql_stats_; }
289
Lalit Magantiacda68b2018-10-29 15:23:25 +0000290 const std::deque<std::string>& string_pool() const { return string_pool_; }
291
Isabelle Taylore7003fb2018-07-17 11:39:01 +0100292 // |unique_processes_| always contains at least 1 element becuase the 0th ID
293 // is reserved to indicate an invalid process.
294 size_t process_count() const { return unique_processes_.size() - 1; }
Primiano Tucci0d72a312018-08-07 14:42:45 +0100295
Isabelle Taylore7003fb2018-07-17 11:39:01 +0100296 // |unique_threads_| always contains at least 1 element becuase the 0th ID
297 // is reserved to indicate an invalid thread.
298 size_t thread_count() const { return unique_threads_.size() - 1; }
299
Hector Dearman12323362018-08-09 16:09:28 +0100300 // Number of interned strings in the pool. Includes the empty string w/ ID=0.
301 size_t string_count() const { return string_pool_.size(); }
302
Lalit Maganticaed37e2018-06-01 03:03:08 +0100303 private:
Isabelle Taylora0a22972018-08-03 12:06:12 +0100304 TraceStorage& operator=(const TraceStorage&) = default;
305
Primiano Tucci2da5d2e2018-08-10 14:23:31 +0100306 using StringHash = uint64_t;
Lalit Maganticaed37e2018-06-01 03:03:08 +0100307
Lalit Maganti35622b72018-06-06 12:03:11 +0100308 // Metadata counters for events being added.
309 Stats stats_;
310
Lalit Maganticaed37e2018-06-01 03:03:08 +0100311 // One entry for each CPU in the trace.
Lalit Magantiff69c112018-09-24 12:07:47 +0100312 Slices slices_;
Lalit Maganticaed37e2018-06-01 03:03:08 +0100313
314 // One entry for each unique string in the trace.
Lalit Maganti35622b72018-06-06 12:03:11 +0100315 std::deque<std::string> string_pool_;
Lalit Maganticaed37e2018-06-01 03:03:08 +0100316
317 // One entry for each unique string in the trace.
Lalit Maganti35622b72018-06-06 12:03:11 +0100318 std::unordered_map<StringHash, StringId> string_index_;
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100319
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100320 // One entry for each UniquePid, with UniquePid as the index.
Isabelle Taylor3dd366c2018-06-22 16:21:41 +0100321 std::deque<Process> unique_processes_;
Isabelle Taylor68e42192018-06-19 16:19:31 +0100322
Isabelle Taylor68e42192018-06-19 16:19:31 +0100323 // One entry for each UniqueTid, with UniqueTid as the index.
Isabelle Taylor3dd366c2018-06-22 16:21:41 +0100324 std::deque<Thread> unique_threads_;
Primiano Tucci0d72a312018-08-07 14:42:45 +0100325
326 // Slices coming from userspace events (e.g. Chromium TRACE_EVENT macros).
327 NestableSlices nestable_slices_;
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100328
329 // Counter events from the trace. This includes CPU frequency events as well
330 // systrace trace_marker counter events.
331 Counters counters_;
Primiano Tucci5cb84f82018-10-31 21:46:36 -0700332
333 SqlStats sql_stats_;
Lalit Maganticaed37e2018-06-01 03:03:08 +0100334};
335
336} // namespace trace_processor
337} // namespace perfetto
338
339#endif // SRC_TRACE_PROCESSOR_TRACE_STORAGE_H_