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" |
Primiano Tucci | 2da5d2e | 2018-08-10 14:23:31 +0100 | [diff] [blame^] | 28 | #include "perfetto/base/string_view.h" |
Hector Dearman | c833993 | 2018-08-10 11:22:46 +0100 | [diff] [blame] | 29 | #include "perfetto/base/utils.h" |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 30 | |
Lalit Maganti | caed37e | 2018-06-01 03:03:08 +0100 | [diff] [blame] | 31 | namespace perfetto { |
| 32 | namespace trace_processor { |
| 33 | |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 34 | // 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. |
| 37 | using UniquePid = uint32_t; |
Primiano Tucci | 0d72a31 | 2018-08-07 14:42:45 +0100 | [diff] [blame] | 38 | |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 39 | // UniqueTid is an offset into |unique_threads_|. Necessary because tids can |
| 40 | // be reused. |
| 41 | using UniqueTid = uint32_t; |
| 42 | |
Primiano Tucci | 0d72a31 | 2018-08-07 14:42:45 +0100 | [diff] [blame] | 43 | // StringId is an offset into |string_pool_|. |
| 44 | using StringId = size_t; |
| 45 | |
Lalit Maganti | caed37e | 2018-06-01 03:03:08 +0100 | [diff] [blame] | 46 | // Stores a data inside a trace file in a columnar form. This makes it efficient |
| 47 | // to read or search across a single field of the trace (e.g. all the thread |
| 48 | // names for a given CPU). |
| 49 | class TraceStorage { |
| 50 | public: |
Isabelle Taylor | 47328cf | 2018-06-12 14:33:59 +0100 | [diff] [blame] | 51 | TraceStorage(); |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 52 | TraceStorage(const TraceStorage&) = delete; |
| 53 | |
| 54 | virtual ~TraceStorage(); |
Isabelle Taylor | 47328cf | 2018-06-12 14:33:59 +0100 | [diff] [blame] | 55 | |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 56 | struct Stats { |
| 57 | uint64_t mismatched_sched_switch_tids_ = 0; |
| 58 | }; |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 59 | |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 60 | // Information about a unique process seen in a trace. |
| 61 | struct Process { |
Primiano Tucci | b75dcee | 2018-08-08 12:21:36 +0100 | [diff] [blame] | 62 | explicit Process(uint32_t p) : pid(p) {} |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 63 | uint64_t start_ns = 0; |
| 64 | uint64_t end_ns = 0; |
| 65 | StringId name_id = 0; |
Primiano Tucci | b75dcee | 2018-08-08 12:21:36 +0100 | [diff] [blame] | 66 | uint32_t pid = 0; |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | // Information about a unique thread seen in a trace. |
| 70 | struct Thread { |
Primiano Tucci | b75dcee | 2018-08-08 12:21:36 +0100 | [diff] [blame] | 71 | explicit Thread(uint32_t t) : tid(t) {} |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 72 | uint64_t start_ns = 0; |
| 73 | uint64_t end_ns = 0; |
| 74 | StringId name_id = 0; |
| 75 | UniquePid upid = 0; |
Primiano Tucci | b75dcee | 2018-08-08 12:21:36 +0100 | [diff] [blame] | 76 | uint32_t tid = 0; |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 77 | }; |
Isabelle Taylor | 3dd366c | 2018-06-22 16:21:41 +0100 | [diff] [blame] | 78 | |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 79 | class SlicesPerCpu { |
| 80 | public: |
| 81 | inline void AddSlice(uint64_t start_ns, |
| 82 | uint64_t duration_ns, |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 83 | UniqueTid utid) { |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 84 | start_ns_.emplace_back(start_ns); |
| 85 | durations_.emplace_back(duration_ns); |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 86 | utids_.emplace_back(utid); |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 87 | } |
| 88 | |
Isabelle Taylor | 47328cf | 2018-06-12 14:33:59 +0100 | [diff] [blame] | 89 | size_t slice_count() const { return start_ns_.size(); } |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 90 | |
Isabelle Taylor | 47328cf | 2018-06-12 14:33:59 +0100 | [diff] [blame] | 91 | const std::deque<uint64_t>& start_ns() const { return start_ns_; } |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 92 | |
Isabelle Taylor | 47328cf | 2018-06-12 14:33:59 +0100 | [diff] [blame] | 93 | const std::deque<uint64_t>& durations() const { return durations_; } |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 94 | |
Isabelle Taylor | 68e4219 | 2018-06-19 16:19:31 +0100 | [diff] [blame] | 95 | const std::deque<UniqueTid>& utids() const { return utids_; } |
| 96 | |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 97 | 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 Taylor | 68e4219 | 2018-06-19 16:19:31 +0100 | [diff] [blame] | 102 | std::deque<UniqueTid> utids_; |
Primiano Tucci | 0d72a31 | 2018-08-07 14:42:45 +0100 | [diff] [blame] | 103 | }; |
Isabelle Taylor | 68e4219 | 2018-06-19 16:19:31 +0100 | [diff] [blame] | 104 | |
Primiano Tucci | 0d72a31 | 2018-08-07 14:42:45 +0100 | [diff] [blame] | 105 | 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 Tucci | b75dcee | 2018-08-08 12:21:36 +0100 | [diff] [blame] | 112 | uint8_t depth, |
| 113 | uint64_t stack_id, |
| 114 | uint64_t parent_stack_id) { |
Primiano Tucci | 0d72a31 | 2018-08-07 14:42:45 +0100 | [diff] [blame] | 115 | 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 Tucci | b75dcee | 2018-08-08 12:21:36 +0100 | [diff] [blame] | 121 | stack_ids_.emplace_back(stack_id); |
| 122 | parent_stack_ids_.emplace_back(parent_stack_id); |
Primiano Tucci | 0d72a31 | 2018-08-07 14:42:45 +0100 | [diff] [blame] | 123 | } |
| 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 Tucci | b75dcee | 2018-08-08 12:21:36 +0100 | [diff] [blame] | 132 | 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 Tucci | 0d72a31 | 2018-08-07 14:42:45 +0100 | [diff] [blame] | 136 | |
| 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 Tucci | b75dcee | 2018-08-08 12:21:36 +0100 | [diff] [blame] | 144 | std::deque<uint64_t> stack_ids_; |
| 145 | std::deque<uint64_t> parent_stack_ids_; |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 146 | }; |
| 147 | |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 148 | void ResetStorage(); |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 149 | |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 150 | void AddSliceToCpu(uint32_t cpu, |
| 151 | uint64_t start_ns, |
| 152 | uint64_t duration_ns, |
| 153 | UniqueTid utid); |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 154 | |
Primiano Tucci | b75dcee | 2018-08-08 12:21:36 +0100 | [diff] [blame] | 155 | UniqueTid AddEmptyThread(uint32_t tid) { |
| 156 | unique_threads_.emplace_back(tid); |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 157 | return static_cast<UniqueTid>(unique_threads_.size() - 1); |
| 158 | } |
Isabelle Taylor | 47328cf | 2018-06-12 14:33:59 +0100 | [diff] [blame] | 159 | |
Primiano Tucci | b75dcee | 2018-08-08 12:21:36 +0100 | [diff] [blame] | 160 | UniquePid AddEmptyProcess(uint32_t pid) { |
| 161 | unique_processes_.emplace_back(pid); |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 162 | return static_cast<UniquePid>(unique_processes_.size() - 1); |
| 163 | } |
Isabelle Taylor | 3dd366c | 2018-06-22 16:21:41 +0100 | [diff] [blame] | 164 | |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 165 | void AddMismatchedSchedSwitch() { ++stats_.mismatched_sched_switch_tids_; } |
Lalit Maganti | caed37e | 2018-06-01 03:03:08 +0100 | [diff] [blame] | 166 | |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 167 | // Return an unqiue identifier for the contents of each string. |
| 168 | // The string is copied internally and can be destroyed after this called. |
Primiano Tucci | 2da5d2e | 2018-08-10 14:23:31 +0100 | [diff] [blame^] | 169 | StringId InternString(base::StringView); |
Isabelle Taylor | 47328cf | 2018-06-12 14:33:59 +0100 | [diff] [blame] | 170 | |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 171 | Process* GetMutableProcess(UniquePid upid) { |
Primiano Tucci | b75dcee | 2018-08-08 12:21:36 +0100 | [diff] [blame] | 172 | PERFETTO_DCHECK(upid > 0 && upid < unique_processes_.size()); |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 173 | return &unique_processes_[upid]; |
| 174 | } |
Isabelle Taylor | 3dd366c | 2018-06-22 16:21:41 +0100 | [diff] [blame] | 175 | |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 176 | Thread* GetMutableThread(UniqueTid utid) { |
Primiano Tucci | b75dcee | 2018-08-08 12:21:36 +0100 | [diff] [blame] | 177 | PERFETTO_DCHECK(utid > 0 && utid < unique_threads_.size()); |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 178 | return &unique_threads_[utid]; |
| 179 | } |
Isabelle Taylor | 3dd366c | 2018-06-22 16:21:41 +0100 | [diff] [blame] | 180 | |
Lalit Maganti | caed37e | 2018-06-01 03:03:08 +0100 | [diff] [blame] | 181 | // Reading methods. |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 182 | const SlicesPerCpu& SlicesForCpu(uint32_t cpu) const { |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 183 | PERFETTO_DCHECK(cpu < cpu_events_.size()); |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 184 | return cpu_events_[cpu]; |
Lalit Maganti | caed37e | 2018-06-01 03:03:08 +0100 | [diff] [blame] | 185 | } |
| 186 | |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 187 | const std::string& GetString(StringId id) const { |
| 188 | PERFETTO_DCHECK(id < string_pool_.size()); |
| 189 | return string_pool_[id]; |
| 190 | } |
| 191 | |
Lalit Maganti | e9d4053 | 2018-06-29 13:15:06 +0100 | [diff] [blame] | 192 | const Process& GetProcess(UniquePid upid) const { |
Primiano Tucci | b75dcee | 2018-08-08 12:21:36 +0100 | [diff] [blame] | 193 | PERFETTO_DCHECK(upid > 0 && upid < unique_processes_.size()); |
Isabelle Taylor | 47328cf | 2018-06-12 14:33:59 +0100 | [diff] [blame] | 194 | return unique_processes_[upid]; |
| 195 | } |
| 196 | |
Lalit Maganti | e9d4053 | 2018-06-29 13:15:06 +0100 | [diff] [blame] | 197 | const Thread& GetThread(UniqueTid utid) const { |
Primiano Tucci | b75dcee | 2018-08-08 12:21:36 +0100 | [diff] [blame] | 198 | PERFETTO_DCHECK(utid > 0 && utid < unique_threads_.size()); |
Isabelle Taylor | 68e4219 | 2018-06-19 16:19:31 +0100 | [diff] [blame] | 199 | return unique_threads_[utid]; |
| 200 | } |
| 201 | |
Primiano Tucci | 0d72a31 | 2018-08-07 14:42:45 +0100 | [diff] [blame] | 202 | const NestableSlices& nestable_slices() const { return nestable_slices_; } |
| 203 | NestableSlices* mutable_nestable_slices() { return &nestable_slices_; } |
| 204 | |
Isabelle Taylor | e7003fb | 2018-07-17 11:39:01 +0100 | [diff] [blame] | 205 | // |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 Tucci | 0d72a31 | 2018-08-07 14:42:45 +0100 | [diff] [blame] | 208 | |
Isabelle Taylor | e7003fb | 2018-07-17 11:39:01 +0100 | [diff] [blame] | 209 | // |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 | |
Hector Dearman | 1232336 | 2018-08-09 16:09:28 +0100 | [diff] [blame] | 213 | // Number of interned strings in the pool. Includes the empty string w/ ID=0. |
| 214 | size_t string_count() const { return string_pool_.size(); } |
| 215 | |
Lalit Maganti | caed37e | 2018-06-01 03:03:08 +0100 | [diff] [blame] | 216 | private: |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 217 | TraceStorage& operator=(const TraceStorage&) = default; |
| 218 | |
Primiano Tucci | 2da5d2e | 2018-08-10 14:23:31 +0100 | [diff] [blame^] | 219 | using StringHash = uint64_t; |
Lalit Maganti | caed37e | 2018-06-01 03:03:08 +0100 | [diff] [blame] | 220 | |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 221 | // Metadata counters for events being added. |
| 222 | Stats stats_; |
| 223 | |
Lalit Maganti | caed37e | 2018-06-01 03:03:08 +0100 | [diff] [blame] | 224 | // One entry for each CPU in the trace. |
Hector Dearman | c833993 | 2018-08-10 11:22:46 +0100 | [diff] [blame] | 225 | std::array<SlicesPerCpu, base::kMaxCpus> cpu_events_; |
Lalit Maganti | caed37e | 2018-06-01 03:03:08 +0100 | [diff] [blame] | 226 | |
| 227 | // One entry for each unique string in the trace. |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 228 | std::deque<std::string> string_pool_; |
Lalit Maganti | caed37e | 2018-06-01 03:03:08 +0100 | [diff] [blame] | 229 | |
| 230 | // One entry for each unique string in the trace. |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 231 | std::unordered_map<StringHash, StringId> string_index_; |
Isabelle Taylor | 47328cf | 2018-06-12 14:33:59 +0100 | [diff] [blame] | 232 | |
Isabelle Taylor | 47328cf | 2018-06-12 14:33:59 +0100 | [diff] [blame] | 233 | // One entry for each UniquePid, with UniquePid as the index. |
Isabelle Taylor | 3dd366c | 2018-06-22 16:21:41 +0100 | [diff] [blame] | 234 | std::deque<Process> unique_processes_; |
Isabelle Taylor | 68e4219 | 2018-06-19 16:19:31 +0100 | [diff] [blame] | 235 | |
Isabelle Taylor | 68e4219 | 2018-06-19 16:19:31 +0100 | [diff] [blame] | 236 | // One entry for each UniqueTid, with UniqueTid as the index. |
Isabelle Taylor | 3dd366c | 2018-06-22 16:21:41 +0100 | [diff] [blame] | 237 | std::deque<Thread> unique_threads_; |
Primiano Tucci | 0d72a31 | 2018-08-07 14:42:45 +0100 | [diff] [blame] | 238 | |
| 239 | // Slices coming from userspace events (e.g. Chromium TRACE_EVENT macros). |
| 240 | NestableSlices nestable_slices_; |
Lalit Maganti | caed37e | 2018-06-01 03:03:08 +0100 | [diff] [blame] | 241 | }; |
| 242 | |
| 243 | } // namespace trace_processor |
| 244 | } // namespace perfetto |
| 245 | |
| 246 | #endif // SRC_TRACE_PROCESSOR_TRACE_STORAGE_H_ |