Lalit Maganti | caed37e | 2018-06-01 03:03:08 +0100 | [diff] [blame] | 1 | /* |
| 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 Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 20 | #include <array> |
Lalit Maganti | caed37e | 2018-06-01 03:03:08 +0100 | [diff] [blame] | 21 | #include <deque> |
Isabelle Taylor | 47328cf | 2018-06-12 14:33:59 +0100 | [diff] [blame] | 22 | #include <map> |
Lalit Maganti | caed37e | 2018-06-01 03:03:08 +0100 | [diff] [blame] | 23 | #include <string> |
| 24 | #include <unordered_map> |
| 25 | #include <vector> |
| 26 | |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 27 | #include "perfetto/base/logging.h" |
| 28 | |
Lalit Maganti | caed37e | 2018-06-01 03:03:08 +0100 | [diff] [blame] | 29 | namespace perfetto { |
| 30 | namespace trace_processor { |
| 31 | |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 32 | // 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. |
| 35 | using UniquePid = uint32_t; |
Primiano Tucci | 0d72a31 | 2018-08-07 14:42:45 +0100 | [diff] [blame^] | 36 | |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 37 | // UniqueTid is an offset into |unique_threads_|. Necessary because tids can |
| 38 | // be reused. |
| 39 | using UniqueTid = uint32_t; |
| 40 | |
Primiano Tucci | 0d72a31 | 2018-08-07 14:42:45 +0100 | [diff] [blame^] | 41 | // StringId is an offset into |string_pool_|. |
| 42 | using StringId = size_t; |
| 43 | |
Lalit Maganti | caed37e | 2018-06-01 03:03:08 +0100 | [diff] [blame] | 44 | // 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). |
| 47 | class TraceStorage { |
| 48 | public: |
Isabelle Taylor | 47328cf | 2018-06-12 14:33:59 +0100 | [diff] [blame] | 49 | TraceStorage(); |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 50 | TraceStorage(const TraceStorage&) = delete; |
| 51 | |
| 52 | virtual ~TraceStorage(); |
Isabelle Taylor | 47328cf | 2018-06-12 14:33:59 +0100 | [diff] [blame] | 53 | |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 54 | constexpr static size_t kMaxCpus = 128; |
Isabelle Taylor | 68e4219 | 2018-06-19 16:19:31 +0100 | [diff] [blame] | 55 | |
Isabelle Taylor | 68e4219 | 2018-06-19 16:19:31 +0100 | [diff] [blame] | 56 | |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 57 | struct Stats { |
| 58 | uint64_t mismatched_sched_switch_tids_ = 0; |
| 59 | }; |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 60 | |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 61 | // 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 Taylor | 3dd366c | 2018-06-22 16:21:41 +0100 | [diff] [blame] | 75 | |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 76 | class SlicesPerCpu { |
| 77 | public: |
| 78 | inline void AddSlice(uint64_t start_ns, |
| 79 | uint64_t duration_ns, |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 80 | UniqueTid utid) { |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 81 | start_ns_.emplace_back(start_ns); |
| 82 | durations_.emplace_back(duration_ns); |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 83 | utids_.emplace_back(utid); |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 84 | } |
| 85 | |
Isabelle Taylor | 47328cf | 2018-06-12 14:33:59 +0100 | [diff] [blame] | 86 | size_t slice_count() const { return start_ns_.size(); } |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 87 | |
Isabelle Taylor | 47328cf | 2018-06-12 14:33:59 +0100 | [diff] [blame] | 88 | const std::deque<uint64_t>& start_ns() const { return start_ns_; } |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 89 | |
Isabelle Taylor | 47328cf | 2018-06-12 14:33:59 +0100 | [diff] [blame] | 90 | const std::deque<uint64_t>& durations() const { return durations_; } |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 91 | |
Isabelle Taylor | 68e4219 | 2018-06-19 16:19:31 +0100 | [diff] [blame] | 92 | const std::deque<UniqueTid>& utids() const { return utids_; } |
| 93 | |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 94 | 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 Taylor | 68e4219 | 2018-06-19 16:19:31 +0100 | [diff] [blame] | 99 | std::deque<UniqueTid> utids_; |
Primiano Tucci | 0d72a31 | 2018-08-07 14:42:45 +0100 | [diff] [blame^] | 100 | }; |
Isabelle Taylor | 68e4219 | 2018-06-19 16:19:31 +0100 | [diff] [blame] | 101 | |
Primiano Tucci | 0d72a31 | 2018-08-07 14:42:45 +0100 | [diff] [blame^] | 102 | 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 Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 133 | }; |
| 134 | |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 135 | void ResetStorage(); |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 136 | |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 137 | void AddSliceToCpu(uint32_t cpu, |
| 138 | uint64_t start_ns, |
| 139 | uint64_t duration_ns, |
| 140 | UniqueTid utid); |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 141 | |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 142 | UniqueTid AddEmptyThread() { |
| 143 | unique_threads_.emplace_back(); |
| 144 | return static_cast<UniqueTid>(unique_threads_.size() - 1); |
| 145 | } |
Isabelle Taylor | 47328cf | 2018-06-12 14:33:59 +0100 | [diff] [blame] | 146 | |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 147 | UniquePid AddEmptyProcess() { |
| 148 | unique_processes_.emplace_back(); |
| 149 | return static_cast<UniquePid>(unique_processes_.size() - 1); |
| 150 | } |
Isabelle Taylor | 3dd366c | 2018-06-22 16:21:41 +0100 | [diff] [blame] | 151 | |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 152 | void AddMismatchedSchedSwitch() { ++stats_.mismatched_sched_switch_tids_; } |
Lalit Maganti | caed37e | 2018-06-01 03:03:08 +0100 | [diff] [blame] | 153 | |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 154 | // 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 Taylor | 47328cf | 2018-06-12 14:33:59 +0100 | [diff] [blame] | 157 | |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 158 | Process* GetMutableProcess(UniquePid upid) { |
| 159 | PERFETTO_DCHECK(upid < unique_processes_.size()); |
| 160 | return &unique_processes_[upid]; |
| 161 | } |
Isabelle Taylor | 3dd366c | 2018-06-22 16:21:41 +0100 | [diff] [blame] | 162 | |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 163 | Thread* GetMutableThread(UniqueTid utid) { |
| 164 | PERFETTO_DCHECK(utid < unique_threads_.size()); |
| 165 | return &unique_threads_[utid]; |
| 166 | } |
Isabelle Taylor | 3dd366c | 2018-06-22 16:21:41 +0100 | [diff] [blame] | 167 | |
Lalit Maganti | caed37e | 2018-06-01 03:03:08 +0100 | [diff] [blame] | 168 | // Reading methods. |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 169 | const SlicesPerCpu& SlicesForCpu(uint32_t cpu) const { |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 170 | PERFETTO_DCHECK(cpu < cpu_events_.size()); |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 171 | return cpu_events_[cpu]; |
Lalit Maganti | caed37e | 2018-06-01 03:03:08 +0100 | [diff] [blame] | 172 | } |
| 173 | |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 174 | const std::string& GetString(StringId id) const { |
| 175 | PERFETTO_DCHECK(id < string_pool_.size()); |
| 176 | return string_pool_[id]; |
| 177 | } |
| 178 | |
Lalit Maganti | e9d4053 | 2018-06-29 13:15:06 +0100 | [diff] [blame] | 179 | const Process& GetProcess(UniquePid upid) const { |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 180 | PERFETTO_DCHECK(upid < unique_processes_.size()); |
Isabelle Taylor | 47328cf | 2018-06-12 14:33:59 +0100 | [diff] [blame] | 181 | return unique_processes_[upid]; |
| 182 | } |
| 183 | |
Lalit Maganti | e9d4053 | 2018-06-29 13:15:06 +0100 | [diff] [blame] | 184 | const Thread& GetThread(UniqueTid utid) const { |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 185 | PERFETTO_DCHECK(utid < unique_threads_.size()); |
Isabelle Taylor | 68e4219 | 2018-06-19 16:19:31 +0100 | [diff] [blame] | 186 | return unique_threads_[utid]; |
| 187 | } |
| 188 | |
Primiano Tucci | 0d72a31 | 2018-08-07 14:42:45 +0100 | [diff] [blame^] | 189 | const NestableSlices& nestable_slices() const { return nestable_slices_; } |
| 190 | NestableSlices* mutable_nestable_slices() { return &nestable_slices_; } |
| 191 | |
Isabelle Taylor | e7003fb | 2018-07-17 11:39:01 +0100 | [diff] [blame] | 192 | // |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 Tucci | 0d72a31 | 2018-08-07 14:42:45 +0100 | [diff] [blame^] | 195 | |
Isabelle Taylor | e7003fb | 2018-07-17 11:39:01 +0100 | [diff] [blame] | 196 | // |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 Maganti | caed37e | 2018-06-01 03:03:08 +0100 | [diff] [blame] | 200 | private: |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 201 | TraceStorage& operator=(const TraceStorage&) = default; |
| 202 | |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 203 | using StringHash = uint32_t; |
Lalit Maganti | caed37e | 2018-06-01 03:03:08 +0100 | [diff] [blame] | 204 | |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 205 | // Metadata counters for events being added. |
| 206 | Stats stats_; |
| 207 | |
Lalit Maganti | caed37e | 2018-06-01 03:03:08 +0100 | [diff] [blame] | 208 | // One entry for each CPU in the trace. |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 209 | std::array<SlicesPerCpu, kMaxCpus> cpu_events_; |
Lalit Maganti | caed37e | 2018-06-01 03:03:08 +0100 | [diff] [blame] | 210 | |
| 211 | // One entry for each unique string in the trace. |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 212 | std::deque<std::string> string_pool_; |
Lalit Maganti | caed37e | 2018-06-01 03:03:08 +0100 | [diff] [blame] | 213 | |
| 214 | // One entry for each unique string in the trace. |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 215 | std::unordered_map<StringHash, StringId> string_index_; |
Isabelle Taylor | 47328cf | 2018-06-12 14:33:59 +0100 | [diff] [blame] | 216 | |
Isabelle Taylor | 47328cf | 2018-06-12 14:33:59 +0100 | [diff] [blame] | 217 | // One entry for each UniquePid, with UniquePid as the index. |
Isabelle Taylor | 3dd366c | 2018-06-22 16:21:41 +0100 | [diff] [blame] | 218 | std::deque<Process> unique_processes_; |
Isabelle Taylor | 68e4219 | 2018-06-19 16:19:31 +0100 | [diff] [blame] | 219 | |
Isabelle Taylor | 68e4219 | 2018-06-19 16:19:31 +0100 | [diff] [blame] | 220 | // One entry for each UniqueTid, with UniqueTid as the index. |
Isabelle Taylor | 3dd366c | 2018-06-22 16:21:41 +0100 | [diff] [blame] | 221 | std::deque<Thread> unique_threads_; |
Primiano Tucci | 0d72a31 | 2018-08-07 14:42:45 +0100 | [diff] [blame^] | 222 | |
| 223 | // Slices coming from userspace events (e.g. Chromium TRACE_EVENT macros). |
| 224 | NestableSlices nestable_slices_; |
Lalit Maganti | caed37e | 2018-06-01 03:03:08 +0100 | [diff] [blame] | 225 | }; |
| 226 | |
| 227 | } // namespace trace_processor |
| 228 | } // namespace perfetto |
| 229 | |
| 230 | #endif // SRC_TRACE_PROCESSOR_TRACE_STORAGE_H_ |