blob: 4f03c63383d0bf1d1c11236453d611c2397f4ca3 [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 Taylor14674d42018-09-07 11:33:11 +010046// A map containing timestamps and the cpu frequency set at that time.
47using CpuFreq =
48 std::deque<std::pair<uint64_t /*timestamp*/, uint32_t /*freq*/>>;
49
Lalit Maganticaed37e2018-06-01 03:03:08 +010050// Stores a data inside a trace file in a columnar form. This makes it efficient
51// to read or search across a single field of the trace (e.g. all the thread
52// names for a given CPU).
53class TraceStorage {
54 public:
Isabelle Taylor47328cf2018-06-12 14:33:59 +010055 TraceStorage();
Isabelle Taylora0a22972018-08-03 12:06:12 +010056 TraceStorage(const TraceStorage&) = delete;
57
58 virtual ~TraceStorage();
Isabelle Taylor47328cf2018-06-12 14:33:59 +010059
Isabelle Taylora0a22972018-08-03 12:06:12 +010060 struct Stats {
61 uint64_t mismatched_sched_switch_tids_ = 0;
62 };
Lalit Maganti35622b72018-06-06 12:03:11 +010063
Isabelle Taylora0a22972018-08-03 12:06:12 +010064 // Information about a unique process seen in a trace.
65 struct Process {
Primiano Tuccib75dcee2018-08-08 12:21:36 +010066 explicit Process(uint32_t p) : pid(p) {}
Isabelle Taylora0a22972018-08-03 12:06:12 +010067 uint64_t start_ns = 0;
68 uint64_t end_ns = 0;
69 StringId name_id = 0;
Primiano Tuccib75dcee2018-08-08 12:21:36 +010070 uint32_t pid = 0;
Isabelle Taylora0a22972018-08-03 12:06:12 +010071 };
72
73 // Information about a unique thread seen in a trace.
74 struct Thread {
Primiano Tuccib75dcee2018-08-08 12:21:36 +010075 explicit Thread(uint32_t t) : tid(t) {}
Isabelle Taylora0a22972018-08-03 12:06:12 +010076 uint64_t start_ns = 0;
77 uint64_t end_ns = 0;
78 StringId name_id = 0;
79 UniquePid upid = 0;
Primiano Tuccib75dcee2018-08-08 12:21:36 +010080 uint32_t tid = 0;
Isabelle Taylora0a22972018-08-03 12:06:12 +010081 };
Isabelle Taylor3dd366c2018-06-22 16:21:41 +010082
Lalit Maganti35622b72018-06-06 12:03:11 +010083 class SlicesPerCpu {
84 public:
85 inline void AddSlice(uint64_t start_ns,
86 uint64_t duration_ns,
Isabelle Taylor14674d42018-09-07 11:33:11 +010087 UniqueTid utid,
88 uint64_t cycles) {
Lalit Maganti35622b72018-06-06 12:03:11 +010089 start_ns_.emplace_back(start_ns);
90 durations_.emplace_back(duration_ns);
Isabelle Taylora0a22972018-08-03 12:06:12 +010091 utids_.emplace_back(utid);
Isabelle Taylor14674d42018-09-07 11:33:11 +010092 cycles_.emplace_back(cycles);
Lalit Maganti35622b72018-06-06 12:03:11 +010093 }
94
Isabelle Taylor47328cf2018-06-12 14:33:59 +010095 size_t slice_count() const { return start_ns_.size(); }
Lalit Maganti35622b72018-06-06 12:03:11 +010096
Isabelle Taylor47328cf2018-06-12 14:33:59 +010097 const std::deque<uint64_t>& start_ns() const { return start_ns_; }
Lalit Maganti35622b72018-06-06 12:03:11 +010098
Isabelle Taylor47328cf2018-06-12 14:33:59 +010099 const std::deque<uint64_t>& durations() const { return durations_; }
Lalit Maganti35622b72018-06-06 12:03:11 +0100100
Isabelle Taylor68e42192018-06-19 16:19:31 +0100101 const std::deque<UniqueTid>& utids() const { return utids_; }
102
Isabelle Taylor14674d42018-09-07 11:33:11 +0100103 const std::deque<uint64_t>& cycles() const { return cycles_; }
104
Lalit Maganti35622b72018-06-06 12:03:11 +0100105 private:
106 // Each vector below has the same number of entries (the number of slices
107 // in the trace for the CPU).
108 std::deque<uint64_t> start_ns_;
109 std::deque<uint64_t> durations_;
Isabelle Taylor68e42192018-06-19 16:19:31 +0100110 std::deque<UniqueTid> utids_;
Isabelle Taylor14674d42018-09-07 11:33:11 +0100111 std::deque<uint64_t> cycles_;
Primiano Tucci0d72a312018-08-07 14:42:45 +0100112 };
Isabelle Taylor68e42192018-06-19 16:19:31 +0100113
Primiano Tucci0d72a312018-08-07 14:42:45 +0100114 class NestableSlices {
115 public:
116 inline void AddSlice(uint64_t start_ns,
117 uint64_t duration_ns,
118 UniqueTid utid,
119 StringId cat,
120 StringId name,
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100121 uint8_t depth,
122 uint64_t stack_id,
123 uint64_t parent_stack_id) {
Primiano Tucci0d72a312018-08-07 14:42:45 +0100124 start_ns_.emplace_back(start_ns);
125 durations_.emplace_back(duration_ns);
126 utids_.emplace_back(utid);
127 cats_.emplace_back(cat);
128 names_.emplace_back(name);
129 depths_.emplace_back(depth);
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100130 stack_ids_.emplace_back(stack_id);
131 parent_stack_ids_.emplace_back(parent_stack_id);
Primiano Tucci0d72a312018-08-07 14:42:45 +0100132 }
133
134 size_t slice_count() const { return start_ns_.size(); }
135 const std::deque<uint64_t>& start_ns() const { return start_ns_; }
136 const std::deque<uint64_t>& durations() const { return durations_; }
137 const std::deque<UniqueTid>& utids() const { return utids_; }
138 const std::deque<StringId>& cats() const { return cats_; }
139 const std::deque<StringId>& names() const { return names_; }
140 const std::deque<uint8_t>& depths() const { return depths_; }
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100141 const std::deque<uint64_t>& stack_ids() const { return stack_ids_; }
142 const std::deque<uint64_t>& parent_stack_ids() const {
143 return parent_stack_ids_;
144 }
Primiano Tucci0d72a312018-08-07 14:42:45 +0100145
146 private:
147 std::deque<uint64_t> start_ns_;
148 std::deque<uint64_t> durations_;
149 std::deque<UniqueTid> utids_;
150 std::deque<StringId> cats_;
151 std::deque<StringId> names_;
152 std::deque<uint8_t> depths_;
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100153 std::deque<uint64_t> stack_ids_;
154 std::deque<uint64_t> parent_stack_ids_;
Lalit Maganti35622b72018-06-06 12:03:11 +0100155 };
156
Isabelle Taylora0a22972018-08-03 12:06:12 +0100157 void ResetStorage();
Lalit Maganti35622b72018-06-06 12:03:11 +0100158
Isabelle Taylora0a22972018-08-03 12:06:12 +0100159 void AddSliceToCpu(uint32_t cpu,
160 uint64_t start_ns,
161 uint64_t duration_ns,
Isabelle Taylor14674d42018-09-07 11:33:11 +0100162 UniqueTid utid,
163 uint64_t cycles);
Lalit Maganti35622b72018-06-06 12:03:11 +0100164
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100165 UniqueTid AddEmptyThread(uint32_t tid) {
166 unique_threads_.emplace_back(tid);
Isabelle Taylora0a22972018-08-03 12:06:12 +0100167 return static_cast<UniqueTid>(unique_threads_.size() - 1);
168 }
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100169
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100170 UniquePid AddEmptyProcess(uint32_t pid) {
171 unique_processes_.emplace_back(pid);
Isabelle Taylora0a22972018-08-03 12:06:12 +0100172 return static_cast<UniquePid>(unique_processes_.size() - 1);
173 }
Isabelle Taylor3dd366c2018-06-22 16:21:41 +0100174
Isabelle Taylora0a22972018-08-03 12:06:12 +0100175 void AddMismatchedSchedSwitch() { ++stats_.mismatched_sched_switch_tids_; }
Lalit Maganticaed37e2018-06-01 03:03:08 +0100176
Isabelle Taylora0a22972018-08-03 12:06:12 +0100177 // Return an unqiue identifier for the contents of each string.
178 // The string is copied internally and can be destroyed after this called.
Primiano Tucci2da5d2e2018-08-10 14:23:31 +0100179 StringId InternString(base::StringView);
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100180
Isabelle Taylora0a22972018-08-03 12:06:12 +0100181 Process* GetMutableProcess(UniquePid upid) {
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100182 PERFETTO_DCHECK(upid > 0 && upid < unique_processes_.size());
Isabelle Taylora0a22972018-08-03 12:06:12 +0100183 return &unique_processes_[upid];
184 }
Isabelle Taylor3dd366c2018-06-22 16:21:41 +0100185
Isabelle Taylora0a22972018-08-03 12:06:12 +0100186 Thread* GetMutableThread(UniqueTid utid) {
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100187 PERFETTO_DCHECK(utid > 0 && utid < unique_threads_.size());
Isabelle Taylora0a22972018-08-03 12:06:12 +0100188 return &unique_threads_[utid];
189 }
Isabelle Taylor3dd366c2018-06-22 16:21:41 +0100190
Lalit Maganticaed37e2018-06-01 03:03:08 +0100191 // Reading methods.
Lalit Maganti35622b72018-06-06 12:03:11 +0100192 const SlicesPerCpu& SlicesForCpu(uint32_t cpu) const {
Isabelle Taylora0a22972018-08-03 12:06:12 +0100193 PERFETTO_DCHECK(cpu < cpu_events_.size());
Lalit Maganti35622b72018-06-06 12:03:11 +0100194 return cpu_events_[cpu];
Lalit Maganticaed37e2018-06-01 03:03:08 +0100195 }
196
Isabelle Taylora0a22972018-08-03 12:06:12 +0100197 const std::string& GetString(StringId id) const {
198 PERFETTO_DCHECK(id < string_pool_.size());
199 return string_pool_[id];
200 }
201
Lalit Magantie9d40532018-06-29 13:15:06 +0100202 const Process& GetProcess(UniquePid upid) const {
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100203 PERFETTO_DCHECK(upid > 0 && upid < unique_processes_.size());
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100204 return unique_processes_[upid];
205 }
206
Lalit Magantie9d40532018-06-29 13:15:06 +0100207 const Thread& GetThread(UniqueTid utid) const {
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100208 PERFETTO_DCHECK(utid > 0 && utid < unique_threads_.size());
Isabelle Taylor68e42192018-06-19 16:19:31 +0100209 return unique_threads_[utid];
210 }
211
Primiano Tucci0d72a312018-08-07 14:42:45 +0100212 const NestableSlices& nestable_slices() const { return nestable_slices_; }
213 NestableSlices* mutable_nestable_slices() { return &nestable_slices_; }
214
Isabelle Taylor14674d42018-09-07 11:33:11 +0100215 // Virtual for testing.
216 virtual void PushCpuFreq(uint64_t timestamp,
217 uint32_t cpu,
218 uint32_t new_freq) {
219 auto& freqs = cpu_freq_[cpu];
220 PERFETTO_DCHECK(freqs.empty() || timestamp > freqs.back().first);
221 freqs.emplace_back(timestamp, new_freq);
222 }
223
224 const CpuFreq& GetFreqForCpu(uint32_t cpu) { return cpu_freq_[cpu]; }
225
Isabelle Taylore7003fb2018-07-17 11:39:01 +0100226 // |unique_processes_| always contains at least 1 element becuase the 0th ID
227 // is reserved to indicate an invalid process.
228 size_t process_count() const { return unique_processes_.size() - 1; }
Primiano Tucci0d72a312018-08-07 14:42:45 +0100229
Isabelle Taylore7003fb2018-07-17 11:39:01 +0100230 // |unique_threads_| always contains at least 1 element becuase the 0th ID
231 // is reserved to indicate an invalid thread.
232 size_t thread_count() const { return unique_threads_.size() - 1; }
233
Hector Dearman12323362018-08-09 16:09:28 +0100234 // Number of interned strings in the pool. Includes the empty string w/ ID=0.
235 size_t string_count() const { return string_pool_.size(); }
236
Lalit Maganticaed37e2018-06-01 03:03:08 +0100237 private:
Isabelle Taylora0a22972018-08-03 12:06:12 +0100238 TraceStorage& operator=(const TraceStorage&) = default;
239
Primiano Tucci2da5d2e2018-08-10 14:23:31 +0100240 using StringHash = uint64_t;
Lalit Maganticaed37e2018-06-01 03:03:08 +0100241
Lalit Maganti35622b72018-06-06 12:03:11 +0100242 // Metadata counters for events being added.
243 Stats stats_;
244
Lalit Maganticaed37e2018-06-01 03:03:08 +0100245 // One entry for each CPU in the trace.
Hector Dearmanc8339932018-08-10 11:22:46 +0100246 std::array<SlicesPerCpu, base::kMaxCpus> cpu_events_;
Lalit Maganticaed37e2018-06-01 03:03:08 +0100247
Isabelle Taylor14674d42018-09-07 11:33:11 +0100248 // One map containing frequencies for every CPU in the trace. The map contains
249 // timestamps and the cpu frequency value at that time.
250 std::array<CpuFreq, base::kMaxCpus> cpu_freq_;
251
Lalit Maganticaed37e2018-06-01 03:03:08 +0100252 // One entry for each unique string in the trace.
Lalit Maganti35622b72018-06-06 12:03:11 +0100253 std::deque<std::string> string_pool_;
Lalit Maganticaed37e2018-06-01 03:03:08 +0100254
255 // One entry for each unique string in the trace.
Lalit Maganti35622b72018-06-06 12:03:11 +0100256 std::unordered_map<StringHash, StringId> string_index_;
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100257
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100258 // One entry for each UniquePid, with UniquePid as the index.
Isabelle Taylor3dd366c2018-06-22 16:21:41 +0100259 std::deque<Process> unique_processes_;
Isabelle Taylor68e42192018-06-19 16:19:31 +0100260
Isabelle Taylor68e42192018-06-19 16:19:31 +0100261 // One entry for each UniqueTid, with UniqueTid as the index.
Isabelle Taylor3dd366c2018-06-22 16:21:41 +0100262 std::deque<Thread> unique_threads_;
Primiano Tucci0d72a312018-08-07 14:42:45 +0100263
264 // Slices coming from userspace events (e.g. Chromium TRACE_EVENT macros).
265 NestableSlices nestable_slices_;
Lalit Maganticaed37e2018-06-01 03:03:08 +0100266};
267
268} // namespace trace_processor
269} // namespace perfetto
270
271#endif // SRC_TRACE_PROCESSOR_TRACE_STORAGE_H_