blob: b403c569c8a88a655dede73f4f8fec013e9c07f4 [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,
Lalit Magantiacda68b2018-10-29 15:23:25 +000052 kMax = kSoftIrq + 1
Isabelle Taylora97c5f52018-10-23 17:36:12 +010053};
Isabelle Taylor14674d42018-09-07 11:33:11 +010054
Lalit Maganticaed37e2018-06-01 03:03:08 +010055// Stores a data inside a trace file in a columnar form. This makes it efficient
56// to read or search across a single field of the trace (e.g. all the thread
57// names for a given CPU).
58class TraceStorage {
59 public:
Isabelle Taylor47328cf2018-06-12 14:33:59 +010060 TraceStorage();
Isabelle Taylora0a22972018-08-03 12:06:12 +010061 TraceStorage(const TraceStorage&) = delete;
62
63 virtual ~TraceStorage();
Isabelle Taylor47328cf2018-06-12 14:33:59 +010064
Isabelle Taylora0a22972018-08-03 12:06:12 +010065 struct Stats {
66 uint64_t mismatched_sched_switch_tids_ = 0;
67 };
Lalit Maganti35622b72018-06-06 12:03:11 +010068
Isabelle Taylora0a22972018-08-03 12:06:12 +010069 // Information about a unique process seen in a trace.
70 struct Process {
Primiano Tuccib75dcee2018-08-08 12:21:36 +010071 explicit Process(uint32_t p) : pid(p) {}
Isabelle Taylora0a22972018-08-03 12:06:12 +010072 uint64_t start_ns = 0;
73 uint64_t end_ns = 0;
74 StringId name_id = 0;
Primiano Tuccib75dcee2018-08-08 12:21:36 +010075 uint32_t pid = 0;
Isabelle Taylora0a22972018-08-03 12:06:12 +010076 };
77
78 // Information about a unique thread seen in a trace.
79 struct Thread {
Primiano Tuccib75dcee2018-08-08 12:21:36 +010080 explicit Thread(uint32_t t) : tid(t) {}
Isabelle Taylora0a22972018-08-03 12:06:12 +010081 uint64_t start_ns = 0;
82 uint64_t end_ns = 0;
83 StringId name_id = 0;
84 UniquePid upid = 0;
Primiano Tuccib75dcee2018-08-08 12:21:36 +010085 uint32_t tid = 0;
Isabelle Taylora0a22972018-08-03 12:06:12 +010086 };
Isabelle Taylor3dd366c2018-06-22 16:21:41 +010087
Lalit Magantiff69c112018-09-24 12:07:47 +010088 class Slices {
Lalit Maganti35622b72018-06-06 12:03:11 +010089 public:
Lalit Magantifde29042018-10-04 13:28:52 +010090 inline size_t AddSlice(uint32_t cpu,
91 uint64_t start_ns,
92 uint64_t duration_ns,
93 UniqueTid utid) {
Lalit Magantiff69c112018-09-24 12:07:47 +010094 cpus_.emplace_back(cpu);
Lalit Maganti35622b72018-06-06 12:03:11 +010095 start_ns_.emplace_back(start_ns);
96 durations_.emplace_back(duration_ns);
Isabelle Taylora0a22972018-08-03 12:06:12 +010097 utids_.emplace_back(utid);
Lalit Magantifde29042018-10-04 13:28:52 +010098 return slice_count() - 1;
99 }
100
101 void set_duration(size_t index, uint64_t duration_ns) {
102 durations_[index] = duration_ns;
Lalit Maganti35622b72018-06-06 12:03:11 +0100103 }
104
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100105 size_t slice_count() const { return start_ns_.size(); }
Lalit Maganti35622b72018-06-06 12:03:11 +0100106
Lalit Magantiff69c112018-09-24 12:07:47 +0100107 const std::deque<uint32_t>& cpus() const { return cpus_; }
108
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100109 const std::deque<uint64_t>& start_ns() const { return start_ns_; }
Lalit Maganti35622b72018-06-06 12:03:11 +0100110
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100111 const std::deque<uint64_t>& durations() const { return durations_; }
Lalit Maganti35622b72018-06-06 12:03:11 +0100112
Isabelle Taylor68e42192018-06-19 16:19:31 +0100113 const std::deque<UniqueTid>& utids() const { return utids_; }
114
Lalit Maganti35622b72018-06-06 12:03:11 +0100115 private:
Hector Dearman947f12a2018-09-11 16:50:36 +0100116 // Each deque below has the same number of entries (the number of slices
Lalit Maganti35622b72018-06-06 12:03:11 +0100117 // in the trace for the CPU).
Lalit Magantiff69c112018-09-24 12:07:47 +0100118 std::deque<uint32_t> cpus_;
Lalit Maganti35622b72018-06-06 12:03:11 +0100119 std::deque<uint64_t> start_ns_;
120 std::deque<uint64_t> durations_;
Isabelle Taylor68e42192018-06-19 16:19:31 +0100121 std::deque<UniqueTid> utids_;
Primiano Tucci0d72a312018-08-07 14:42:45 +0100122 };
Isabelle Taylor68e42192018-06-19 16:19:31 +0100123
Primiano Tucci0d72a312018-08-07 14:42:45 +0100124 class NestableSlices {
125 public:
126 inline void AddSlice(uint64_t start_ns,
127 uint64_t duration_ns,
128 UniqueTid utid,
129 StringId cat,
130 StringId name,
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100131 uint8_t depth,
132 uint64_t stack_id,
133 uint64_t parent_stack_id) {
Primiano Tucci0d72a312018-08-07 14:42:45 +0100134 start_ns_.emplace_back(start_ns);
135 durations_.emplace_back(duration_ns);
136 utids_.emplace_back(utid);
137 cats_.emplace_back(cat);
138 names_.emplace_back(name);
139 depths_.emplace_back(depth);
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100140 stack_ids_.emplace_back(stack_id);
141 parent_stack_ids_.emplace_back(parent_stack_id);
Primiano Tucci0d72a312018-08-07 14:42:45 +0100142 }
143
144 size_t slice_count() const { return start_ns_.size(); }
145 const std::deque<uint64_t>& start_ns() const { return start_ns_; }
146 const std::deque<uint64_t>& durations() const { return durations_; }
147 const std::deque<UniqueTid>& utids() const { return utids_; }
148 const std::deque<StringId>& cats() const { return cats_; }
149 const std::deque<StringId>& names() const { return names_; }
150 const std::deque<uint8_t>& depths() const { return depths_; }
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100151 const std::deque<uint64_t>& stack_ids() const { return stack_ids_; }
152 const std::deque<uint64_t>& parent_stack_ids() const {
153 return parent_stack_ids_;
154 }
Primiano Tucci0d72a312018-08-07 14:42:45 +0100155
156 private:
157 std::deque<uint64_t> start_ns_;
158 std::deque<uint64_t> durations_;
159 std::deque<UniqueTid> utids_;
160 std::deque<StringId> cats_;
161 std::deque<StringId> names_;
162 std::deque<uint8_t> depths_;
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100163 std::deque<uint64_t> stack_ids_;
164 std::deque<uint64_t> parent_stack_ids_;
Lalit Maganti35622b72018-06-06 12:03:11 +0100165 };
166
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100167 class Counters {
168 public:
Lalit Magantifde29042018-10-04 13:28:52 +0100169 inline size_t AddCounter(uint64_t timestamp,
170 uint64_t duration,
171 StringId name_id,
172 double value,
Lalit Magantifde29042018-10-04 13:28:52 +0100173 int64_t ref,
174 RefType type) {
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100175 timestamps_.emplace_back(timestamp);
176 durations_.emplace_back(duration);
177 name_ids_.emplace_back(name_id);
178 values_.emplace_back(value);
179 refs_.emplace_back(ref);
180 types_.emplace_back(type);
Lalit Magantifde29042018-10-04 13:28:52 +0100181 return counter_count() - 1;
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100182 }
Lalit Magantifde29042018-10-04 13:28:52 +0100183
184 void set_duration(size_t index, uint64_t duration) {
185 durations_[index] = duration;
186 }
187
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100188 size_t counter_count() const { return timestamps_.size(); }
189
190 const std::deque<uint64_t>& timestamps() const { return timestamps_; }
191
192 const std::deque<uint64_t>& durations() const { return durations_; }
193
194 const std::deque<StringId>& name_ids() const { return name_ids_; }
195
196 const std::deque<double>& values() const { return values_; }
197
198 const std::deque<int64_t>& refs() const { return refs_; }
199
200 const std::deque<RefType>& types() const { return types_; }
201
202 private:
203 std::deque<uint64_t> timestamps_;
204 std::deque<uint64_t> durations_;
205 std::deque<StringId> name_ids_;
206 std::deque<double> values_;
207 std::deque<int64_t> refs_;
208 std::deque<RefType> types_;
209 };
210
Isabelle Taylora0a22972018-08-03 12:06:12 +0100211 void ResetStorage();
Lalit Maganti35622b72018-06-06 12:03:11 +0100212
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100213 UniqueTid AddEmptyThread(uint32_t tid) {
214 unique_threads_.emplace_back(tid);
Isabelle Taylora0a22972018-08-03 12:06:12 +0100215 return static_cast<UniqueTid>(unique_threads_.size() - 1);
216 }
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100217
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100218 UniquePid AddEmptyProcess(uint32_t pid) {
219 unique_processes_.emplace_back(pid);
Isabelle Taylora0a22972018-08-03 12:06:12 +0100220 return static_cast<UniquePid>(unique_processes_.size() - 1);
221 }
Isabelle Taylor3dd366c2018-06-22 16:21:41 +0100222
Isabelle Taylora0a22972018-08-03 12:06:12 +0100223 void AddMismatchedSchedSwitch() { ++stats_.mismatched_sched_switch_tids_; }
Lalit Maganticaed37e2018-06-01 03:03:08 +0100224
Isabelle Taylora0a22972018-08-03 12:06:12 +0100225 // Return an unqiue identifier for the contents of each string.
226 // The string is copied internally and can be destroyed after this called.
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100227 // Virtual for testing.
228 virtual StringId InternString(base::StringView);
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100229
Isabelle Taylora0a22972018-08-03 12:06:12 +0100230 Process* GetMutableProcess(UniquePid upid) {
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100231 PERFETTO_DCHECK(upid > 0 && upid < unique_processes_.size());
Isabelle Taylora0a22972018-08-03 12:06:12 +0100232 return &unique_processes_[upid];
233 }
Isabelle Taylor3dd366c2018-06-22 16:21:41 +0100234
Isabelle Taylora0a22972018-08-03 12:06:12 +0100235 Thread* GetMutableThread(UniqueTid utid) {
Florian Mayera1aaec22018-09-19 17:02:26 +0100236 PERFETTO_DCHECK(utid < unique_threads_.size());
Isabelle Taylora0a22972018-08-03 12:06:12 +0100237 return &unique_threads_[utid];
238 }
Isabelle Taylor3dd366c2018-06-22 16:21:41 +0100239
Lalit Maganticaed37e2018-06-01 03:03:08 +0100240 // Reading methods.
Isabelle Taylora0a22972018-08-03 12:06:12 +0100241 const std::string& GetString(StringId id) const {
242 PERFETTO_DCHECK(id < string_pool_.size());
243 return string_pool_[id];
244 }
245
Lalit Magantie9d40532018-06-29 13:15:06 +0100246 const Process& GetProcess(UniquePid upid) const {
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100247 PERFETTO_DCHECK(upid > 0 && upid < unique_processes_.size());
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100248 return unique_processes_[upid];
249 }
250
Lalit Magantie9d40532018-06-29 13:15:06 +0100251 const Thread& GetThread(UniqueTid utid) const {
Lalit Maganti1f64cfa2018-09-18 13:20:02 +0100252 // Allow utid == 0 for idle thread retrieval.
Florian Mayera1aaec22018-09-19 17:02:26 +0100253 PERFETTO_DCHECK(utid < unique_threads_.size());
Isabelle Taylor68e42192018-06-19 16:19:31 +0100254 return unique_threads_[utid];
255 }
256
Lalit Magantiff69c112018-09-24 12:07:47 +0100257 const Slices& slices() const { return slices_; }
Lalit Magantifde29042018-10-04 13:28:52 +0100258 Slices* mutable_slices() { return &slices_; }
259
Primiano Tucci0d72a312018-08-07 14:42:45 +0100260 const NestableSlices& nestable_slices() const { return nestable_slices_; }
261 NestableSlices* mutable_nestable_slices() { return &nestable_slices_; }
262
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100263 const Counters& counters() const { return counters_; }
264 Counters* mutable_counters() { return &counters_; }
Isabelle Taylor14674d42018-09-07 11:33:11 +0100265
Lalit Magantiacda68b2018-10-29 15:23:25 +0000266 const std::deque<std::string>& string_pool() const { return string_pool_; }
267
Isabelle Taylore7003fb2018-07-17 11:39:01 +0100268 // |unique_processes_| always contains at least 1 element becuase the 0th ID
269 // is reserved to indicate an invalid process.
270 size_t process_count() const { return unique_processes_.size() - 1; }
Primiano Tucci0d72a312018-08-07 14:42:45 +0100271
Isabelle Taylore7003fb2018-07-17 11:39:01 +0100272 // |unique_threads_| always contains at least 1 element becuase the 0th ID
273 // is reserved to indicate an invalid thread.
274 size_t thread_count() const { return unique_threads_.size() - 1; }
275
Hector Dearman12323362018-08-09 16:09:28 +0100276 // Number of interned strings in the pool. Includes the empty string w/ ID=0.
277 size_t string_count() const { return string_pool_.size(); }
278
Lalit Maganticaed37e2018-06-01 03:03:08 +0100279 private:
Isabelle Taylora0a22972018-08-03 12:06:12 +0100280 TraceStorage& operator=(const TraceStorage&) = default;
281
Primiano Tucci2da5d2e2018-08-10 14:23:31 +0100282 using StringHash = uint64_t;
Lalit Maganticaed37e2018-06-01 03:03:08 +0100283
Lalit Maganti35622b72018-06-06 12:03:11 +0100284 // Metadata counters for events being added.
285 Stats stats_;
286
Lalit Maganticaed37e2018-06-01 03:03:08 +0100287 // One entry for each CPU in the trace.
Lalit Magantiff69c112018-09-24 12:07:47 +0100288 Slices slices_;
Lalit Maganticaed37e2018-06-01 03:03:08 +0100289
290 // One entry for each unique string in the trace.
Lalit Maganti35622b72018-06-06 12:03:11 +0100291 std::deque<std::string> string_pool_;
Lalit Maganticaed37e2018-06-01 03:03:08 +0100292
293 // One entry for each unique string in the trace.
Lalit Maganti35622b72018-06-06 12:03:11 +0100294 std::unordered_map<StringHash, StringId> string_index_;
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100295
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100296 // One entry for each UniquePid, with UniquePid as the index.
Isabelle Taylor3dd366c2018-06-22 16:21:41 +0100297 std::deque<Process> unique_processes_;
Isabelle Taylor68e42192018-06-19 16:19:31 +0100298
Isabelle Taylor68e42192018-06-19 16:19:31 +0100299 // One entry for each UniqueTid, with UniqueTid as the index.
Isabelle Taylor3dd366c2018-06-22 16:21:41 +0100300 std::deque<Thread> unique_threads_;
Primiano Tucci0d72a312018-08-07 14:42:45 +0100301
302 // Slices coming from userspace events (e.g. Chromium TRACE_EVENT macros).
303 NestableSlices nestable_slices_;
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100304
305 // Counter events from the trace. This includes CPU frequency events as well
306 // systrace trace_marker counter events.
307 Counters counters_;
Lalit Maganticaed37e2018-06-01 03:03:08 +0100308};
309
310} // namespace trace_processor
311} // namespace perfetto
312
313#endif // SRC_TRACE_PROCESSOR_TRACE_STORAGE_H_