blob: 952e2c0caab8f227a71814807be7cc20ab00274a [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 Taylor68e42192018-06-19 16:19:31 +010056
Isabelle Taylora0a22972018-08-03 12:06:12 +010057 struct Stats {
58 uint64_t mismatched_sched_switch_tids_ = 0;
59 };
Lalit Maganti35622b72018-06-06 12:03:11 +010060
Isabelle Taylora0a22972018-08-03 12:06:12 +010061 // Information about a unique process seen in a trace.
62 struct Process {
63 uint64_t start_ns = 0;
64 uint64_t end_ns = 0;
65 StringId name_id = 0;
66 };
67
68 // Information about a unique thread seen in a trace.
69 struct Thread {
70 uint64_t start_ns = 0;
71 uint64_t end_ns = 0;
72 StringId name_id = 0;
73 UniquePid upid = 0;
74 };
Isabelle Taylor3dd366c2018-06-22 16:21:41 +010075
Lalit Maganti35622b72018-06-06 12:03:11 +010076 class SlicesPerCpu {
77 public:
78 inline void AddSlice(uint64_t start_ns,
79 uint64_t duration_ns,
Isabelle Taylora0a22972018-08-03 12:06:12 +010080 UniqueTid utid) {
Lalit Maganti35622b72018-06-06 12:03:11 +010081 start_ns_.emplace_back(start_ns);
82 durations_.emplace_back(duration_ns);
Isabelle Taylora0a22972018-08-03 12:06:12 +010083 utids_.emplace_back(utid);
Lalit Maganti35622b72018-06-06 12:03:11 +010084 }
85
Isabelle Taylor47328cf2018-06-12 14:33:59 +010086 size_t slice_count() const { return start_ns_.size(); }
Lalit Maganti35622b72018-06-06 12:03:11 +010087
Isabelle Taylor47328cf2018-06-12 14:33:59 +010088 const std::deque<uint64_t>& start_ns() const { return start_ns_; }
Lalit Maganti35622b72018-06-06 12:03:11 +010089
Isabelle Taylor47328cf2018-06-12 14:33:59 +010090 const std::deque<uint64_t>& durations() const { return durations_; }
Lalit Maganti35622b72018-06-06 12:03:11 +010091
Isabelle Taylor68e42192018-06-19 16:19:31 +010092 const std::deque<UniqueTid>& utids() const { return utids_; }
93
Lalit Maganti35622b72018-06-06 12:03:11 +010094 private:
95 // Each vector below has the same number of entries (the number of slices
96 // in the trace for the CPU).
97 std::deque<uint64_t> start_ns_;
98 std::deque<uint64_t> durations_;
Isabelle Taylor68e42192018-06-19 16:19:31 +010099 std::deque<UniqueTid> utids_;
Primiano Tucci0d72a312018-08-07 14:42:45 +0100100 };
Isabelle Taylor68e42192018-06-19 16:19:31 +0100101
Primiano Tucci0d72a312018-08-07 14:42:45 +0100102 class NestableSlices {
103 public:
104 inline void AddSlice(uint64_t start_ns,
105 uint64_t duration_ns,
106 UniqueTid utid,
107 StringId cat,
108 StringId name,
109 uint8_t depth) {
110 start_ns_.emplace_back(start_ns);
111 durations_.emplace_back(duration_ns);
112 utids_.emplace_back(utid);
113 cats_.emplace_back(cat);
114 names_.emplace_back(name);
115 depths_.emplace_back(depth);
116 }
117
118 size_t slice_count() const { return start_ns_.size(); }
119 const std::deque<uint64_t>& start_ns() const { return start_ns_; }
120 const std::deque<uint64_t>& durations() const { return durations_; }
121 const std::deque<UniqueTid>& utids() const { return utids_; }
122 const std::deque<StringId>& cats() const { return cats_; }
123 const std::deque<StringId>& names() const { return names_; }
124 const std::deque<uint8_t>& depths() const { return depths_; }
125
126 private:
127 std::deque<uint64_t> start_ns_;
128 std::deque<uint64_t> durations_;
129 std::deque<UniqueTid> utids_;
130 std::deque<StringId> cats_;
131 std::deque<StringId> names_;
132 std::deque<uint8_t> depths_;
Lalit Maganti35622b72018-06-06 12:03:11 +0100133 };
134
Isabelle Taylora0a22972018-08-03 12:06:12 +0100135 void ResetStorage();
Lalit Maganti35622b72018-06-06 12:03:11 +0100136
Isabelle Taylora0a22972018-08-03 12:06:12 +0100137 void AddSliceToCpu(uint32_t cpu,
138 uint64_t start_ns,
139 uint64_t duration_ns,
140 UniqueTid utid);
Lalit Maganti35622b72018-06-06 12:03:11 +0100141
Isabelle Taylora0a22972018-08-03 12:06:12 +0100142 UniqueTid AddEmptyThread() {
143 unique_threads_.emplace_back();
144 return static_cast<UniqueTid>(unique_threads_.size() - 1);
145 }
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100146
Isabelle Taylora0a22972018-08-03 12:06:12 +0100147 UniquePid AddEmptyProcess() {
148 unique_processes_.emplace_back();
149 return static_cast<UniquePid>(unique_processes_.size() - 1);
150 }
Isabelle Taylor3dd366c2018-06-22 16:21:41 +0100151
Isabelle Taylora0a22972018-08-03 12:06:12 +0100152 void AddMismatchedSchedSwitch() { ++stats_.mismatched_sched_switch_tids_; }
Lalit Maganticaed37e2018-06-01 03:03:08 +0100153
Isabelle Taylora0a22972018-08-03 12:06:12 +0100154 // Return an unqiue identifier for the contents of each string.
155 // The string is copied internally and can be destroyed after this called.
156 StringId InternString(const char* data, size_t length);
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100157
Isabelle Taylora0a22972018-08-03 12:06:12 +0100158 Process* GetMutableProcess(UniquePid upid) {
159 PERFETTO_DCHECK(upid < unique_processes_.size());
160 return &unique_processes_[upid];
161 }
Isabelle Taylor3dd366c2018-06-22 16:21:41 +0100162
Isabelle Taylora0a22972018-08-03 12:06:12 +0100163 Thread* GetMutableThread(UniqueTid utid) {
164 PERFETTO_DCHECK(utid < unique_threads_.size());
165 return &unique_threads_[utid];
166 }
Isabelle Taylor3dd366c2018-06-22 16:21:41 +0100167
Lalit Maganticaed37e2018-06-01 03:03:08 +0100168 // Reading methods.
Lalit Maganti35622b72018-06-06 12:03:11 +0100169 const SlicesPerCpu& SlicesForCpu(uint32_t cpu) const {
Isabelle Taylora0a22972018-08-03 12:06:12 +0100170 PERFETTO_DCHECK(cpu < cpu_events_.size());
Lalit Maganti35622b72018-06-06 12:03:11 +0100171 return cpu_events_[cpu];
Lalit Maganticaed37e2018-06-01 03:03:08 +0100172 }
173
Isabelle Taylora0a22972018-08-03 12:06:12 +0100174 const std::string& GetString(StringId id) const {
175 PERFETTO_DCHECK(id < string_pool_.size());
176 return string_pool_[id];
177 }
178
Lalit Magantie9d40532018-06-29 13:15:06 +0100179 const Process& GetProcess(UniquePid upid) const {
Isabelle Taylora0a22972018-08-03 12:06:12 +0100180 PERFETTO_DCHECK(upid < unique_processes_.size());
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100181 return unique_processes_[upid];
182 }
183
Lalit Magantie9d40532018-06-29 13:15:06 +0100184 const Thread& GetThread(UniqueTid utid) const {
Isabelle Taylora0a22972018-08-03 12:06:12 +0100185 PERFETTO_DCHECK(utid < unique_threads_.size());
Isabelle Taylor68e42192018-06-19 16:19:31 +0100186 return unique_threads_[utid];
187 }
188
Primiano Tucci0d72a312018-08-07 14:42:45 +0100189 const NestableSlices& nestable_slices() const { return nestable_slices_; }
190 NestableSlices* mutable_nestable_slices() { return &nestable_slices_; }
191
Isabelle Taylore7003fb2018-07-17 11:39:01 +0100192 // |unique_processes_| always contains at least 1 element becuase the 0th ID
193 // is reserved to indicate an invalid process.
194 size_t process_count() const { return unique_processes_.size() - 1; }
Primiano Tucci0d72a312018-08-07 14:42:45 +0100195
Isabelle Taylore7003fb2018-07-17 11:39:01 +0100196 // |unique_threads_| always contains at least 1 element becuase the 0th ID
197 // is reserved to indicate an invalid thread.
198 size_t thread_count() const { return unique_threads_.size() - 1; }
199
Lalit Maganticaed37e2018-06-01 03:03:08 +0100200 private:
Isabelle Taylora0a22972018-08-03 12:06:12 +0100201 TraceStorage& operator=(const TraceStorage&) = default;
202
Lalit Maganti35622b72018-06-06 12:03:11 +0100203 using StringHash = uint32_t;
Lalit Maganticaed37e2018-06-01 03:03:08 +0100204
Lalit Maganti35622b72018-06-06 12:03:11 +0100205 // Metadata counters for events being added.
206 Stats stats_;
207
Lalit Maganticaed37e2018-06-01 03:03:08 +0100208 // One entry for each CPU in the trace.
Lalit Maganti35622b72018-06-06 12:03:11 +0100209 std::array<SlicesPerCpu, kMaxCpus> cpu_events_;
Lalit Maganticaed37e2018-06-01 03:03:08 +0100210
211 // One entry for each unique string in the trace.
Lalit Maganti35622b72018-06-06 12:03:11 +0100212 std::deque<std::string> string_pool_;
Lalit Maganticaed37e2018-06-01 03:03:08 +0100213
214 // One entry for each unique string in the trace.
Lalit Maganti35622b72018-06-06 12:03:11 +0100215 std::unordered_map<StringHash, StringId> string_index_;
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100216
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100217 // One entry for each UniquePid, with UniquePid as the index.
Isabelle Taylor3dd366c2018-06-22 16:21:41 +0100218 std::deque<Process> unique_processes_;
Isabelle Taylor68e42192018-06-19 16:19:31 +0100219
Isabelle Taylor68e42192018-06-19 16:19:31 +0100220 // One entry for each UniqueTid, with UniqueTid as the index.
Isabelle Taylor3dd366c2018-06-22 16:21:41 +0100221 std::deque<Thread> unique_threads_;
Primiano Tucci0d72a312018-08-07 14:42:45 +0100222
223 // Slices coming from userspace events (e.g. Chromium TRACE_EVENT macros).
224 NestableSlices nestable_slices_;
Lalit Maganticaed37e2018-06-01 03:03:08 +0100225};
226
227} // namespace trace_processor
228} // namespace perfetto
229
230#endif // SRC_TRACE_PROCESSOR_TRACE_STORAGE_H_