blob: 9613ca64303af43591764d196ce18ee587fd2589 [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"
28
Lalit Maganticaed37e2018-06-01 03:03:08 +010029namespace perfetto {
30namespace trace_processor {
31
Isabelle Taylora0a22972018-08-03 12:06:12 +010032// UniquePid is an offset into |unique_processes_|. This is necessary because
33// Unix pids are reused and thus not guaranteed to be unique over a long
34// period of time.
35using UniquePid = uint32_t;
Primiano Tucci0d72a312018-08-07 14:42:45 +010036
Isabelle Taylora0a22972018-08-03 12:06:12 +010037// UniqueTid is an offset into |unique_threads_|. Necessary because tids can
38// be reused.
39using UniqueTid = uint32_t;
40
Primiano Tucci0d72a312018-08-07 14:42:45 +010041// StringId is an offset into |string_pool_|.
42using StringId = size_t;
43
Lalit Maganticaed37e2018-06-01 03:03:08 +010044// Stores a data inside a trace file in a columnar form. This makes it efficient
45// to read or search across a single field of the trace (e.g. all the thread
46// names for a given CPU).
47class TraceStorage {
48 public:
Isabelle Taylor47328cf2018-06-12 14:33:59 +010049 TraceStorage();
Isabelle Taylora0a22972018-08-03 12:06:12 +010050 TraceStorage(const TraceStorage&) = delete;
51
52 virtual ~TraceStorage();
Isabelle Taylor47328cf2018-06-12 14:33:59 +010053
Lalit Maganti35622b72018-06-06 12:03:11 +010054 constexpr static size_t kMaxCpus = 128;
Isabelle Taylor68e42192018-06-19 16:19:31 +010055
Isabelle Taylora0a22972018-08-03 12:06:12 +010056 struct Stats {
57 uint64_t mismatched_sched_switch_tids_ = 0;
58 };
Lalit Maganti35622b72018-06-06 12:03:11 +010059
Isabelle Taylora0a22972018-08-03 12:06:12 +010060 // Information about a unique process seen in a trace.
61 struct Process {
Primiano Tuccib75dcee2018-08-08 12:21:36 +010062 explicit Process(uint32_t p) : pid(p) {}
Isabelle Taylora0a22972018-08-03 12:06:12 +010063 uint64_t start_ns = 0;
64 uint64_t end_ns = 0;
65 StringId name_id = 0;
Primiano Tuccib75dcee2018-08-08 12:21:36 +010066 uint32_t pid = 0;
Isabelle Taylora0a22972018-08-03 12:06:12 +010067 };
68
69 // Information about a unique thread seen in a trace.
70 struct Thread {
Primiano Tuccib75dcee2018-08-08 12:21:36 +010071 explicit Thread(uint32_t t) : tid(t) {}
Isabelle Taylora0a22972018-08-03 12:06:12 +010072 uint64_t start_ns = 0;
73 uint64_t end_ns = 0;
74 StringId name_id = 0;
75 UniquePid upid = 0;
Primiano Tuccib75dcee2018-08-08 12:21:36 +010076 uint32_t tid = 0;
Isabelle Taylora0a22972018-08-03 12:06:12 +010077 };
Isabelle Taylor3dd366c2018-06-22 16:21:41 +010078
Lalit Maganti35622b72018-06-06 12:03:11 +010079 class SlicesPerCpu {
80 public:
81 inline void AddSlice(uint64_t start_ns,
82 uint64_t duration_ns,
Isabelle Taylora0a22972018-08-03 12:06:12 +010083 UniqueTid utid) {
Lalit Maganti35622b72018-06-06 12:03:11 +010084 start_ns_.emplace_back(start_ns);
85 durations_.emplace_back(duration_ns);
Isabelle Taylora0a22972018-08-03 12:06:12 +010086 utids_.emplace_back(utid);
Lalit Maganti35622b72018-06-06 12:03:11 +010087 }
88
Isabelle Taylor47328cf2018-06-12 14:33:59 +010089 size_t slice_count() const { return start_ns_.size(); }
Lalit Maganti35622b72018-06-06 12:03:11 +010090
Isabelle Taylor47328cf2018-06-12 14:33:59 +010091 const std::deque<uint64_t>& start_ns() const { return start_ns_; }
Lalit Maganti35622b72018-06-06 12:03:11 +010092
Isabelle Taylor47328cf2018-06-12 14:33:59 +010093 const std::deque<uint64_t>& durations() const { return durations_; }
Lalit Maganti35622b72018-06-06 12:03:11 +010094
Isabelle Taylor68e42192018-06-19 16:19:31 +010095 const std::deque<UniqueTid>& utids() const { return utids_; }
96
Lalit Maganti35622b72018-06-06 12:03:11 +010097 private:
98 // Each vector below has the same number of entries (the number of slices
99 // in the trace for the CPU).
100 std::deque<uint64_t> start_ns_;
101 std::deque<uint64_t> durations_;
Isabelle Taylor68e42192018-06-19 16:19:31 +0100102 std::deque<UniqueTid> utids_;
Primiano Tucci0d72a312018-08-07 14:42:45 +0100103 };
Isabelle Taylor68e42192018-06-19 16:19:31 +0100104
Primiano Tucci0d72a312018-08-07 14:42:45 +0100105 class NestableSlices {
106 public:
107 inline void AddSlice(uint64_t start_ns,
108 uint64_t duration_ns,
109 UniqueTid utid,
110 StringId cat,
111 StringId name,
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100112 uint8_t depth,
113 uint64_t stack_id,
114 uint64_t parent_stack_id) {
Primiano Tucci0d72a312018-08-07 14:42:45 +0100115 start_ns_.emplace_back(start_ns);
116 durations_.emplace_back(duration_ns);
117 utids_.emplace_back(utid);
118 cats_.emplace_back(cat);
119 names_.emplace_back(name);
120 depths_.emplace_back(depth);
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100121 stack_ids_.emplace_back(stack_id);
122 parent_stack_ids_.emplace_back(parent_stack_id);
Primiano Tucci0d72a312018-08-07 14:42:45 +0100123 }
124
125 size_t slice_count() const { return start_ns_.size(); }
126 const std::deque<uint64_t>& start_ns() const { return start_ns_; }
127 const std::deque<uint64_t>& durations() const { return durations_; }
128 const std::deque<UniqueTid>& utids() const { return utids_; }
129 const std::deque<StringId>& cats() const { return cats_; }
130 const std::deque<StringId>& names() const { return names_; }
131 const std::deque<uint8_t>& depths() const { return depths_; }
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100132 const std::deque<uint64_t>& stack_ids() const { return stack_ids_; }
133 const std::deque<uint64_t>& parent_stack_ids() const {
134 return parent_stack_ids_;
135 }
Primiano Tucci0d72a312018-08-07 14:42:45 +0100136
137 private:
138 std::deque<uint64_t> start_ns_;
139 std::deque<uint64_t> durations_;
140 std::deque<UniqueTid> utids_;
141 std::deque<StringId> cats_;
142 std::deque<StringId> names_;
143 std::deque<uint8_t> depths_;
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100144 std::deque<uint64_t> stack_ids_;
145 std::deque<uint64_t> parent_stack_ids_;
Lalit Maganti35622b72018-06-06 12:03:11 +0100146 };
147
Isabelle Taylora0a22972018-08-03 12:06:12 +0100148 void ResetStorage();
Lalit Maganti35622b72018-06-06 12:03:11 +0100149
Isabelle Taylora0a22972018-08-03 12:06:12 +0100150 void AddSliceToCpu(uint32_t cpu,
151 uint64_t start_ns,
152 uint64_t duration_ns,
153 UniqueTid utid);
Lalit Maganti35622b72018-06-06 12:03:11 +0100154
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100155 UniqueTid AddEmptyThread(uint32_t tid) {
156 unique_threads_.emplace_back(tid);
Isabelle Taylora0a22972018-08-03 12:06:12 +0100157 return static_cast<UniqueTid>(unique_threads_.size() - 1);
158 }
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100159
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100160 UniquePid AddEmptyProcess(uint32_t pid) {
161 unique_processes_.emplace_back(pid);
Isabelle Taylora0a22972018-08-03 12:06:12 +0100162 return static_cast<UniquePid>(unique_processes_.size() - 1);
163 }
Isabelle Taylor3dd366c2018-06-22 16:21:41 +0100164
Isabelle Taylora0a22972018-08-03 12:06:12 +0100165 void AddMismatchedSchedSwitch() { ++stats_.mismatched_sched_switch_tids_; }
Lalit Maganticaed37e2018-06-01 03:03:08 +0100166
Isabelle Taylora0a22972018-08-03 12:06:12 +0100167 // Return an unqiue identifier for the contents of each string.
168 // The string is copied internally and can be destroyed after this called.
169 StringId InternString(const char* data, size_t length);
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100170
Isabelle Taylora0a22972018-08-03 12:06:12 +0100171 Process* GetMutableProcess(UniquePid upid) {
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100172 PERFETTO_DCHECK(upid > 0 && upid < unique_processes_.size());
Isabelle Taylora0a22972018-08-03 12:06:12 +0100173 return &unique_processes_[upid];
174 }
Isabelle Taylor3dd366c2018-06-22 16:21:41 +0100175
Isabelle Taylora0a22972018-08-03 12:06:12 +0100176 Thread* GetMutableThread(UniqueTid utid) {
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100177 PERFETTO_DCHECK(utid > 0 && utid < unique_threads_.size());
Isabelle Taylora0a22972018-08-03 12:06:12 +0100178 return &unique_threads_[utid];
179 }
Isabelle Taylor3dd366c2018-06-22 16:21:41 +0100180
Lalit Maganticaed37e2018-06-01 03:03:08 +0100181 // Reading methods.
Lalit Maganti35622b72018-06-06 12:03:11 +0100182 const SlicesPerCpu& SlicesForCpu(uint32_t cpu) const {
Isabelle Taylora0a22972018-08-03 12:06:12 +0100183 PERFETTO_DCHECK(cpu < cpu_events_.size());
Lalit Maganti35622b72018-06-06 12:03:11 +0100184 return cpu_events_[cpu];
Lalit Maganticaed37e2018-06-01 03:03:08 +0100185 }
186
Isabelle Taylora0a22972018-08-03 12:06:12 +0100187 const std::string& GetString(StringId id) const {
188 PERFETTO_DCHECK(id < string_pool_.size());
189 return string_pool_[id];
190 }
191
Lalit Magantie9d40532018-06-29 13:15:06 +0100192 const Process& GetProcess(UniquePid upid) const {
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100193 PERFETTO_DCHECK(upid > 0 && upid < unique_processes_.size());
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100194 return unique_processes_[upid];
195 }
196
Lalit Magantie9d40532018-06-29 13:15:06 +0100197 const Thread& GetThread(UniqueTid utid) const {
Primiano Tuccib75dcee2018-08-08 12:21:36 +0100198 PERFETTO_DCHECK(utid > 0 && utid < unique_threads_.size());
Isabelle Taylor68e42192018-06-19 16:19:31 +0100199 return unique_threads_[utid];
200 }
201
Primiano Tucci0d72a312018-08-07 14:42:45 +0100202 const NestableSlices& nestable_slices() const { return nestable_slices_; }
203 NestableSlices* mutable_nestable_slices() { return &nestable_slices_; }
204
Isabelle Taylore7003fb2018-07-17 11:39:01 +0100205 // |unique_processes_| always contains at least 1 element becuase the 0th ID
206 // is reserved to indicate an invalid process.
207 size_t process_count() const { return unique_processes_.size() - 1; }
Primiano Tucci0d72a312018-08-07 14:42:45 +0100208
Isabelle Taylore7003fb2018-07-17 11:39:01 +0100209 // |unique_threads_| always contains at least 1 element becuase the 0th ID
210 // is reserved to indicate an invalid thread.
211 size_t thread_count() const { return unique_threads_.size() - 1; }
212
Lalit Maganticaed37e2018-06-01 03:03:08 +0100213 private:
Isabelle Taylora0a22972018-08-03 12:06:12 +0100214 TraceStorage& operator=(const TraceStorage&) = default;
215
Lalit Maganti35622b72018-06-06 12:03:11 +0100216 using StringHash = uint32_t;
Lalit Maganticaed37e2018-06-01 03:03:08 +0100217
Lalit Maganti35622b72018-06-06 12:03:11 +0100218 // Metadata counters for events being added.
219 Stats stats_;
220
Lalit Maganticaed37e2018-06-01 03:03:08 +0100221 // One entry for each CPU in the trace.
Lalit Maganti35622b72018-06-06 12:03:11 +0100222 std::array<SlicesPerCpu, kMaxCpus> cpu_events_;
Lalit Maganticaed37e2018-06-01 03:03:08 +0100223
224 // One entry for each unique string in the trace.
Lalit Maganti35622b72018-06-06 12:03:11 +0100225 std::deque<std::string> string_pool_;
Lalit Maganticaed37e2018-06-01 03:03:08 +0100226
227 // One entry for each unique string in the trace.
Lalit Maganti35622b72018-06-06 12:03:11 +0100228 std::unordered_map<StringHash, StringId> string_index_;
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100229
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100230 // One entry for each UniquePid, with UniquePid as the index.
Isabelle Taylor3dd366c2018-06-22 16:21:41 +0100231 std::deque<Process> unique_processes_;
Isabelle Taylor68e42192018-06-19 16:19:31 +0100232
Isabelle Taylor68e42192018-06-19 16:19:31 +0100233 // One entry for each UniqueTid, with UniqueTid as the index.
Isabelle Taylor3dd366c2018-06-22 16:21:41 +0100234 std::deque<Thread> unique_threads_;
Primiano Tucci0d72a312018-08-07 14:42:45 +0100235
236 // Slices coming from userspace events (e.g. Chromium TRACE_EVENT macros).
237 NestableSlices nestable_slices_;
Lalit Maganticaed37e2018-06-01 03:03:08 +0100238};
239
240} // namespace trace_processor
241} // namespace perfetto
242
243#endif // SRC_TRACE_PROCESSOR_TRACE_STORAGE_H_