blob: c462d197f1c40aea848d5159ba0ef6e3b99bc4d1 [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 Taylor15314ea2018-09-19 11:35:19 +010046enum RefType { kUPID = 0, kCPU_ID = 1 };
Isabelle Taylor14674d42018-09-07 11:33:11 +010047
Lalit Maganticaed37e2018-06-01 03:03:08 +010048// Stores a data inside a trace file in a columnar form. This makes it efficient
49// to read or search across a single field of the trace (e.g. all the thread
50// names for a given CPU).
51class TraceStorage {
52 public:
Isabelle Taylor47328cf2018-06-12 14:33:59 +010053 TraceStorage();
Isabelle Taylora0a22972018-08-03 12:06:12 +010054 TraceStorage(const TraceStorage&) = delete;
55
56 virtual ~TraceStorage();
Isabelle Taylor47328cf2018-06-12 14:33:59 +010057
Isabelle Taylora0a22972018-08-03 12:06:12 +010058 struct Stats {
59 uint64_t mismatched_sched_switch_tids_ = 0;
60 };
Lalit Maganti35622b72018-06-06 12:03:11 +010061
Isabelle Taylora0a22972018-08-03 12:06:12 +010062 // Information about a unique process seen in a trace.
63 struct Process {
Primiano Tuccib75dcee2018-08-08 12:21:36 +010064 explicit Process(uint32_t p) : pid(p) {}
Isabelle Taylora0a22972018-08-03 12:06:12 +010065 uint64_t start_ns = 0;
66 uint64_t end_ns = 0;
67 StringId name_id = 0;
Primiano Tuccib75dcee2018-08-08 12:21:36 +010068 uint32_t pid = 0;
Isabelle Taylora0a22972018-08-03 12:06:12 +010069 };
70
71 // Information about a unique thread seen in a trace.
72 struct Thread {
Primiano Tuccib75dcee2018-08-08 12:21:36 +010073 explicit Thread(uint32_t t) : tid(t) {}
Isabelle Taylora0a22972018-08-03 12:06:12 +010074 uint64_t start_ns = 0;
75 uint64_t end_ns = 0;
76 StringId name_id = 0;
77 UniquePid upid = 0;
Primiano Tuccib75dcee2018-08-08 12:21:36 +010078 uint32_t tid = 0;
Isabelle Taylora0a22972018-08-03 12:06:12 +010079 };
Isabelle Taylor3dd366c2018-06-22 16:21:41 +010080
Lalit Magantic42c66b2018-09-18 16:08:41 +010081 class SlicesPerCpu {
Lalit Maganti35622b72018-06-06 12:03:11 +010082 public:
Lalit Magantic42c66b2018-09-18 16:08:41 +010083 inline void AddSlice(uint64_t start_ns,
Lalit Maganti35622b72018-06-06 12:03:11 +010084 uint64_t duration_ns,
Isabelle Taylor15314ea2018-09-19 11:35:19 +010085 UniqueTid utid) {
Lalit Maganti35622b72018-06-06 12:03:11 +010086 start_ns_.emplace_back(start_ns);
87 durations_.emplace_back(duration_ns);
Isabelle Taylora0a22972018-08-03 12:06:12 +010088 utids_.emplace_back(utid);
Lalit Maganti35622b72018-06-06 12:03:11 +010089 }
90
Isabelle Taylor47328cf2018-06-12 14:33:59 +010091 size_t slice_count() const { return start_ns_.size(); }
Lalit Maganti35622b72018-06-06 12:03:11 +010092
Isabelle Taylor47328cf2018-06-12 14:33:59 +010093 const std::deque<uint64_t>& start_ns() const { return start_ns_; }
Lalit Maganti35622b72018-06-06 12:03:11 +010094
Isabelle Taylor47328cf2018-06-12 14:33:59 +010095 const std::deque<uint64_t>& durations() const { return durations_; }
Lalit Maganti35622b72018-06-06 12:03:11 +010096
Isabelle Taylor68e42192018-06-19 16:19:31 +010097 const std::deque<UniqueTid>& utids() const { return utids_; }
98
Lalit Maganti35622b72018-06-06 12:03:11 +010099 private:
Hector Dearman947f12a2018-09-11 16:50:36 +0100100 // Each deque below has the same number of entries (the number of slices
Lalit Maganti35622b72018-06-06 12:03:11 +0100101 // in the trace for the CPU).
102 std::deque<uint64_t> start_ns_;
103 std::deque<uint64_t> durations_;
Isabelle Taylor68e42192018-06-19 16:19:31 +0100104 std::deque<UniqueTid> utids_;
Primiano Tucci0d72a312018-08-07 14:42:45 +0100105 };
Isabelle Taylor68e42192018-06-19 16:19:31 +0100106
Primiano Tucci0d72a312018-08-07 14:42:45 +0100107 class NestableSlices {
108 public:
109 inline void AddSlice(uint64_t start_ns,
110 uint64_t duration_ns,
111 UniqueTid utid,
112 StringId cat,
113 StringId name,
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100114 uint8_t depth,
115 uint64_t stack_id,
116 uint64_t parent_stack_id) {
Primiano Tucci0d72a312018-08-07 14:42:45 +0100117 start_ns_.emplace_back(start_ns);
118 durations_.emplace_back(duration_ns);
119 utids_.emplace_back(utid);
120 cats_.emplace_back(cat);
121 names_.emplace_back(name);
122 depths_.emplace_back(depth);
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100123 stack_ids_.emplace_back(stack_id);
124 parent_stack_ids_.emplace_back(parent_stack_id);
Primiano Tucci0d72a312018-08-07 14:42:45 +0100125 }
126
127 size_t slice_count() const { return start_ns_.size(); }
128 const std::deque<uint64_t>& start_ns() const { return start_ns_; }
129 const std::deque<uint64_t>& durations() const { return durations_; }
130 const std::deque<UniqueTid>& utids() const { return utids_; }
131 const std::deque<StringId>& cats() const { return cats_; }
132 const std::deque<StringId>& names() const { return names_; }
133 const std::deque<uint8_t>& depths() const { return depths_; }
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100134 const std::deque<uint64_t>& stack_ids() const { return stack_ids_; }
135 const std::deque<uint64_t>& parent_stack_ids() const {
136 return parent_stack_ids_;
137 }
Primiano Tucci0d72a312018-08-07 14:42:45 +0100138
139 private:
140 std::deque<uint64_t> start_ns_;
141 std::deque<uint64_t> durations_;
142 std::deque<UniqueTid> utids_;
143 std::deque<StringId> cats_;
144 std::deque<StringId> names_;
145 std::deque<uint8_t> depths_;
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100146 std::deque<uint64_t> stack_ids_;
147 std::deque<uint64_t> parent_stack_ids_;
Lalit Maganti35622b72018-06-06 12:03:11 +0100148 };
149
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100150 class Counters {
151 public:
152 inline void AddCounter(uint64_t timestamp,
153 uint64_t duration,
154 StringId name_id,
155 double value,
Isabelle Taylor31e04402018-09-19 12:13:25 +0100156 double value_delta,
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100157 int64_t ref,
158 RefType type) {
159 timestamps_.emplace_back(timestamp);
160 durations_.emplace_back(duration);
161 name_ids_.emplace_back(name_id);
162 values_.emplace_back(value);
Isabelle Taylor31e04402018-09-19 12:13:25 +0100163 value_deltas_.emplace_back(value_delta);
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100164 refs_.emplace_back(ref);
165 types_.emplace_back(type);
166 }
167 size_t counter_count() const { return timestamps_.size(); }
168
169 const std::deque<uint64_t>& timestamps() const { return timestamps_; }
170
171 const std::deque<uint64_t>& durations() const { return durations_; }
172
173 const std::deque<StringId>& name_ids() const { return name_ids_; }
174
175 const std::deque<double>& values() const { return values_; }
176
Isabelle Taylor31e04402018-09-19 12:13:25 +0100177 const std::deque<double>& value_deltas() const { return value_deltas_; }
178
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100179 const std::deque<int64_t>& refs() const { return refs_; }
180
181 const std::deque<RefType>& types() const { return types_; }
182
183 private:
184 std::deque<uint64_t> timestamps_;
185 std::deque<uint64_t> durations_;
186 std::deque<StringId> name_ids_;
187 std::deque<double> values_;
Isabelle Taylor31e04402018-09-19 12:13:25 +0100188 std::deque<double> value_deltas_;
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100189 std::deque<int64_t> refs_;
190 std::deque<RefType> types_;
191 };
192
Isabelle Taylora0a22972018-08-03 12:06:12 +0100193 void ResetStorage();
Lalit Maganti35622b72018-06-06 12:03:11 +0100194
Isabelle Taylora0a22972018-08-03 12:06:12 +0100195 void AddSliceToCpu(uint32_t cpu,
196 uint64_t start_ns,
197 uint64_t duration_ns,
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100198 UniqueTid utid);
Lalit Maganti35622b72018-06-06 12:03:11 +0100199
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100200 UniqueTid AddEmptyThread(uint32_t tid) {
201 unique_threads_.emplace_back(tid);
Isabelle Taylora0a22972018-08-03 12:06:12 +0100202 return static_cast<UniqueTid>(unique_threads_.size() - 1);
203 }
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100204
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100205 UniquePid AddEmptyProcess(uint32_t pid) {
206 unique_processes_.emplace_back(pid);
Isabelle Taylora0a22972018-08-03 12:06:12 +0100207 return static_cast<UniquePid>(unique_processes_.size() - 1);
208 }
Isabelle Taylor3dd366c2018-06-22 16:21:41 +0100209
Isabelle Taylora0a22972018-08-03 12:06:12 +0100210 void AddMismatchedSchedSwitch() { ++stats_.mismatched_sched_switch_tids_; }
Lalit Maganticaed37e2018-06-01 03:03:08 +0100211
Isabelle Taylora0a22972018-08-03 12:06:12 +0100212 // Return an unqiue identifier for the contents of each string.
213 // The string is copied internally and can be destroyed after this called.
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100214 // Virtual for testing.
215 virtual StringId InternString(base::StringView);
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100216
Isabelle Taylora0a22972018-08-03 12:06:12 +0100217 Process* GetMutableProcess(UniquePid upid) {
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100218 PERFETTO_DCHECK(upid > 0 && upid < unique_processes_.size());
Isabelle Taylora0a22972018-08-03 12:06:12 +0100219 return &unique_processes_[upid];
220 }
Isabelle Taylor3dd366c2018-06-22 16:21:41 +0100221
Isabelle Taylora0a22972018-08-03 12:06:12 +0100222 Thread* GetMutableThread(UniqueTid utid) {
Florian Mayera1aaec22018-09-19 17:02:26 +0100223 PERFETTO_DCHECK(utid < unique_threads_.size());
Isabelle Taylora0a22972018-08-03 12:06:12 +0100224 return &unique_threads_[utid];
225 }
Isabelle Taylor3dd366c2018-06-22 16:21:41 +0100226
Lalit Maganticaed37e2018-06-01 03:03:08 +0100227 // Reading methods.
Lalit Magantic42c66b2018-09-18 16:08:41 +0100228 const SlicesPerCpu& SlicesForCpu(uint32_t cpu) const {
229 PERFETTO_DCHECK(cpu < cpu_events_.size());
230 return cpu_events_[cpu];
231 }
232
Isabelle Taylora0a22972018-08-03 12:06:12 +0100233 const std::string& GetString(StringId id) const {
234 PERFETTO_DCHECK(id < string_pool_.size());
235 return string_pool_[id];
236 }
237
Lalit Magantie9d40532018-06-29 13:15:06 +0100238 const Process& GetProcess(UniquePid upid) const {
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100239 PERFETTO_DCHECK(upid > 0 && upid < unique_processes_.size());
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100240 return unique_processes_[upid];
241 }
242
Lalit Magantie9d40532018-06-29 13:15:06 +0100243 const Thread& GetThread(UniqueTid utid) const {
Lalit Maganti1f64cfa2018-09-18 13:20:02 +0100244 // Allow utid == 0 for idle thread retrieval.
Florian Mayera1aaec22018-09-19 17:02:26 +0100245 PERFETTO_DCHECK(utid < unique_threads_.size());
Isabelle Taylor68e42192018-06-19 16:19:31 +0100246 return unique_threads_[utid];
247 }
248
Primiano Tucci0d72a312018-08-07 14:42:45 +0100249 const NestableSlices& nestable_slices() const { return nestable_slices_; }
250 NestableSlices* mutable_nestable_slices() { return &nestable_slices_; }
251
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100252 const Counters& counters() const { return counters_; }
253 Counters* mutable_counters() { return &counters_; }
Isabelle Taylor14674d42018-09-07 11:33:11 +0100254
Isabelle Taylore7003fb2018-07-17 11:39:01 +0100255 // |unique_processes_| always contains at least 1 element becuase the 0th ID
256 // is reserved to indicate an invalid process.
257 size_t process_count() const { return unique_processes_.size() - 1; }
Primiano Tucci0d72a312018-08-07 14:42:45 +0100258
Isabelle Taylore7003fb2018-07-17 11:39:01 +0100259 // |unique_threads_| always contains at least 1 element becuase the 0th ID
260 // is reserved to indicate an invalid thread.
261 size_t thread_count() const { return unique_threads_.size() - 1; }
262
Hector Dearman12323362018-08-09 16:09:28 +0100263 // Number of interned strings in the pool. Includes the empty string w/ ID=0.
264 size_t string_count() const { return string_pool_.size(); }
265
Lalit Maganticaed37e2018-06-01 03:03:08 +0100266 private:
Isabelle Taylora0a22972018-08-03 12:06:12 +0100267 TraceStorage& operator=(const TraceStorage&) = default;
268
Primiano Tucci2da5d2e2018-08-10 14:23:31 +0100269 using StringHash = uint64_t;
Lalit Maganticaed37e2018-06-01 03:03:08 +0100270
Lalit Maganti35622b72018-06-06 12:03:11 +0100271 // Metadata counters for events being added.
272 Stats stats_;
273
Lalit Maganticaed37e2018-06-01 03:03:08 +0100274 // One entry for each CPU in the trace.
Lalit Magantic42c66b2018-09-18 16:08:41 +0100275 std::array<SlicesPerCpu, base::kMaxCpus> cpu_events_;
Lalit Maganticaed37e2018-06-01 03:03:08 +0100276
277 // One entry for each unique string in the trace.
Lalit Maganti35622b72018-06-06 12:03:11 +0100278 std::deque<std::string> string_pool_;
Lalit Maganticaed37e2018-06-01 03:03:08 +0100279
280 // One entry for each unique string in the trace.
Lalit Maganti35622b72018-06-06 12:03:11 +0100281 std::unordered_map<StringHash, StringId> string_index_;
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100282
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100283 // One entry for each UniquePid, with UniquePid as the index.
Isabelle Taylor3dd366c2018-06-22 16:21:41 +0100284 std::deque<Process> unique_processes_;
Isabelle Taylor68e42192018-06-19 16:19:31 +0100285
Isabelle Taylor68e42192018-06-19 16:19:31 +0100286 // One entry for each UniqueTid, with UniqueTid as the index.
Isabelle Taylor3dd366c2018-06-22 16:21:41 +0100287 std::deque<Thread> unique_threads_;
Primiano Tucci0d72a312018-08-07 14:42:45 +0100288
289 // Slices coming from userspace events (e.g. Chromium TRACE_EVENT macros).
290 NestableSlices nestable_slices_;
Isabelle Taylor15314ea2018-09-19 11:35:19 +0100291
292 // Counter events from the trace. This includes CPU frequency events as well
293 // systrace trace_marker counter events.
294 Counters counters_;
Lalit Maganticaed37e2018-06-01 03:03:08 +0100295};
296
297} // namespace trace_processor
298} // namespace perfetto
299
300#endif // SRC_TRACE_PROCESSOR_TRACE_STORAGE_H_