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 | |
| 32 | // Stores a data inside a trace file in a columnar form. This makes it efficient |
| 33 | // to read or search across a single field of the trace (e.g. all the thread |
| 34 | // names for a given CPU). |
| 35 | class TraceStorage { |
| 36 | public: |
Isabelle Taylor | 47328cf | 2018-06-12 14:33:59 +0100 | [diff] [blame] | 37 | TraceStorage(); |
| 38 | |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 39 | constexpr static size_t kMaxCpus = 128; |
Isabelle Taylor | 68e4219 | 2018-06-19 16:19:31 +0100 | [diff] [blame] | 40 | |
Isabelle Taylor | 47328cf | 2018-06-12 14:33:59 +0100 | [diff] [blame] | 41 | // StringId is an offset into |string_pool_|. |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 42 | using StringId = size_t; |
Isabelle Taylor | 68e4219 | 2018-06-19 16:19:31 +0100 | [diff] [blame] | 43 | |
Isabelle Taylor | 47328cf | 2018-06-12 14:33:59 +0100 | [diff] [blame] | 44 | // UniquePid is an offset into |unique_processes_|. This is necessary because |
| 45 | // Unix pids are reused and thus not guaranteed to be unique over a long |
| 46 | // period of time. |
| 47 | using UniquePid = uint32_t; |
| 48 | using UniqueProcessIterator = |
| 49 | std::multimap<uint32_t, UniquePid>::const_iterator; |
| 50 | using UniqueProcessRange = |
| 51 | std::pair<UniqueProcessIterator, UniqueProcessIterator>; |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 52 | |
Isabelle Taylor | 3dd366c | 2018-06-22 16:21:41 +0100 | [diff] [blame] | 53 | // UniqueTid is an offset into |unique_threads_|. Necessary because tids can |
| 54 | // be reused. |
| 55 | using UniqueTid = uint32_t; |
| 56 | using UniqueThreadIterator = |
| 57 | std::multimap<uint32_t, UniqueTid>::const_iterator; |
| 58 | using UniqueThreadRange = |
| 59 | std::pair<UniqueThreadIterator, UniqueThreadIterator>; |
| 60 | |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 61 | class SlicesPerCpu { |
| 62 | public: |
| 63 | inline void AddSlice(uint64_t start_ns, |
| 64 | uint64_t duration_ns, |
Isabelle Taylor | 68e4219 | 2018-06-19 16:19:31 +0100 | [diff] [blame] | 65 | uint32_t tid, |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 66 | StringId thread_name_id) { |
| 67 | start_ns_.emplace_back(start_ns); |
| 68 | durations_.emplace_back(duration_ns); |
Isabelle Taylor | 68e4219 | 2018-06-19 16:19:31 +0100 | [diff] [blame] | 69 | |
| 70 | auto pair_it = storage_->tids_.equal_range(tid); |
Isabelle Taylor | 68e4219 | 2018-06-19 16:19:31 +0100 | [diff] [blame] | 71 | // If there is a previous utid for that tid, use that. |
| 72 | if (pair_it.first != pair_it.second) { |
| 73 | UniqueTid prev_utid = std::prev(pair_it.second)->second; |
| 74 | utids_.emplace_back(prev_utid); |
| 75 | } else { |
| 76 | // If none exist, assign a new utid and store it. |
Isabelle Taylor | 3dd366c | 2018-06-22 16:21:41 +0100 | [diff] [blame] | 77 | Thread new_thread; |
Isabelle Taylor | 68e4219 | 2018-06-19 16:19:31 +0100 | [diff] [blame] | 78 | new_thread.name_id = thread_name_id; |
| 79 | new_thread.start_ns = start_ns; |
Isabelle Taylor | 3dd366c | 2018-06-22 16:21:41 +0100 | [diff] [blame] | 80 | new_thread.upid = 0; |
Isabelle Taylor | 68e4219 | 2018-06-19 16:19:31 +0100 | [diff] [blame] | 81 | storage_->tids_.emplace(tid, storage_->unique_threads_.size()); |
| 82 | utids_.emplace_back(storage_->unique_threads_.size()); |
| 83 | storage_->unique_threads_.emplace_back(std::move(new_thread)); |
| 84 | } |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 85 | } |
| 86 | |
Isabelle Taylor | 47328cf | 2018-06-12 14:33:59 +0100 | [diff] [blame] | 87 | size_t slice_count() const { return start_ns_.size(); } |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 88 | |
Isabelle Taylor | 47328cf | 2018-06-12 14:33:59 +0100 | [diff] [blame] | 89 | const std::deque<uint64_t>& start_ns() const { return start_ns_; } |
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>& durations() const { return durations_; } |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 92 | |
Isabelle Taylor | 68e4219 | 2018-06-19 16:19:31 +0100 | [diff] [blame] | 93 | const std::deque<UniqueTid>& utids() const { return utids_; } |
| 94 | |
| 95 | void InitalizeSlices(TraceStorage* storage) { storage_ = storage; } |
| 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_; |
| 103 | |
| 104 | TraceStorage* storage_; |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | struct Stats { |
| 108 | uint64_t mismatched_sched_switch_tids_ = 0; |
| 109 | }; |
| 110 | |
| 111 | virtual ~TraceStorage(); |
| 112 | |
Isabelle Taylor | 3dd366c | 2018-06-22 16:21:41 +0100 | [diff] [blame] | 113 | // Information about a unique process seen in a trace. |
| 114 | struct Process { |
Isabelle Taylor | 68e4219 | 2018-06-19 16:19:31 +0100 | [diff] [blame] | 115 | uint64_t start_ns = 0; |
| 116 | uint64_t end_ns = 0; |
| 117 | StringId name_id; |
Isabelle Taylor | 47328cf | 2018-06-12 14:33:59 +0100 | [diff] [blame] | 118 | }; |
| 119 | |
Isabelle Taylor | 3dd366c | 2018-06-22 16:21:41 +0100 | [diff] [blame] | 120 | // Information about a unique thread seen in a trace. |
| 121 | struct Thread { |
| 122 | uint64_t start_ns = 0; |
| 123 | uint64_t end_ns = 0; |
| 124 | StringId name_id; |
| 125 | UniquePid upid; |
| 126 | }; |
| 127 | |
Lalit Maganti | caed37e | 2018-06-01 03:03:08 +0100 | [diff] [blame] | 128 | // Adds a sched slice for a given cpu. |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 129 | // Virtual for testing. |
| 130 | virtual void PushSchedSwitch(uint32_t cpu, |
| 131 | uint64_t timestamp, |
| 132 | uint32_t prev_pid, |
| 133 | uint32_t prev_state, |
| 134 | const char* prev_comm, |
| 135 | size_t prev_comm_len, |
| 136 | uint32_t next_pid); |
Lalit Maganti | caed37e | 2018-06-01 03:03:08 +0100 | [diff] [blame] | 137 | |
Isabelle Taylor | 47328cf | 2018-06-12 14:33:59 +0100 | [diff] [blame] | 138 | // Adds a process entry for a given pid. |
| 139 | virtual void PushProcess(uint32_t pid, |
| 140 | const char* process_name, |
| 141 | size_t process_name_len); |
| 142 | |
Isabelle Taylor | 3dd366c | 2018-06-22 16:21:41 +0100 | [diff] [blame] | 143 | // Adds a thread entry for the tid. |
| 144 | virtual void MatchThreadToProcess(uint32_t tid, uint32_t tgid); |
| 145 | |
Isabelle Taylor | 47328cf | 2018-06-12 14:33:59 +0100 | [diff] [blame] | 146 | // Returns the bounds of a range that includes all UniquePids that have the |
| 147 | // requested pid. |
| 148 | UniqueProcessRange UpidsForPid(uint32_t pid); |
| 149 | |
Isabelle Taylor | 3dd366c | 2018-06-22 16:21:41 +0100 | [diff] [blame] | 150 | // Returns the bounds of a range that includes all UniqueTids that have the |
| 151 | // requested tid. |
| 152 | UniqueThreadRange UtidsForTid(uint32_t tid); |
| 153 | |
Lalit Maganti | caed37e | 2018-06-01 03:03:08 +0100 | [diff] [blame] | 154 | // Reading methods. |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 155 | const SlicesPerCpu& SlicesForCpu(uint32_t cpu) const { |
Isabelle Taylor | 47328cf | 2018-06-12 14:33:59 +0100 | [diff] [blame] | 156 | PERFETTO_CHECK(cpu < cpu_events_.size()); |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 157 | return cpu_events_[cpu]; |
Lalit Maganti | caed37e | 2018-06-01 03:03:08 +0100 | [diff] [blame] | 158 | } |
| 159 | |
Lalit Maganti | e9d4053 | 2018-06-29 13:15:06 +0100 | [diff] [blame] | 160 | const Process& GetProcess(UniquePid upid) const { |
Isabelle Taylor | 47328cf | 2018-06-12 14:33:59 +0100 | [diff] [blame] | 161 | PERFETTO_CHECK(upid < unique_processes_.size()); |
| 162 | return unique_processes_[upid]; |
| 163 | } |
| 164 | |
Lalit Maganti | e9d4053 | 2018-06-29 13:15:06 +0100 | [diff] [blame] | 165 | const Thread& GetThread(UniqueTid utid) const { |
Isabelle Taylor | 68e4219 | 2018-06-19 16:19:31 +0100 | [diff] [blame] | 166 | PERFETTO_CHECK(utid < unique_threads_.size()); |
| 167 | return unique_threads_[utid]; |
| 168 | } |
| 169 | |
Lalit Maganti | e9d4053 | 2018-06-29 13:15:06 +0100 | [diff] [blame] | 170 | const std::string& GetString(StringId id) const { |
Isabelle Taylor | 47328cf | 2018-06-12 14:33:59 +0100 | [diff] [blame] | 171 | PERFETTO_CHECK(id < string_pool_.size()); |
| 172 | return string_pool_[id]; |
| 173 | } |
| 174 | |
Isabelle Taylor | e7003fb | 2018-07-17 11:39:01 +0100 | [diff] [blame^] | 175 | // |unique_processes_| always contains at least 1 element becuase the 0th ID |
| 176 | // is reserved to indicate an invalid process. |
| 177 | size_t process_count() const { return unique_processes_.size() - 1; } |
| 178 | // |unique_threads_| always contains at least 1 element becuase the 0th ID |
| 179 | // is reserved to indicate an invalid thread. |
| 180 | size_t thread_count() const { return unique_threads_.size() - 1; } |
| 181 | |
Lalit Maganti | caed37e | 2018-06-01 03:03:08 +0100 | [diff] [blame] | 182 | private: |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 183 | using StringHash = uint32_t; |
Lalit Maganti | caed37e | 2018-06-01 03:03:08 +0100 | [diff] [blame] | 184 | |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 185 | struct SchedSwitchEvent { |
| 186 | uint64_t cpu = 0; |
| 187 | uint64_t timestamp = 0; |
| 188 | uint32_t prev_pid = 0; |
| 189 | uint32_t prev_state = 0; |
Isabelle Taylor | 68e4219 | 2018-06-19 16:19:31 +0100 | [diff] [blame] | 190 | StringId prev_thread_name_id = 0; |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 191 | uint32_t next_pid = 0; |
Lalit Maganti | caed37e | 2018-06-01 03:03:08 +0100 | [diff] [blame] | 192 | |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 193 | bool valid() const { return timestamp != 0; } |
Lalit Maganti | caed37e | 2018-06-01 03:03:08 +0100 | [diff] [blame] | 194 | }; |
| 195 | |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 196 | // Return an unqiue identifier for the contents of each string. |
| 197 | // The string is copied internally and can be destroyed after this called. |
| 198 | StringId InternString(const char* data, size_t length); |
| 199 | |
| 200 | // Metadata counters for events being added. |
| 201 | Stats stats_; |
| 202 | |
Lalit Maganti | caed37e | 2018-06-01 03:03:08 +0100 | [diff] [blame] | 203 | // One entry for each CPU in the trace. |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 204 | std::array<SchedSwitchEvent, kMaxCpus> last_sched_per_cpu_; |
| 205 | |
| 206 | // One entry for each CPU in the trace. |
| 207 | std::array<SlicesPerCpu, kMaxCpus> cpu_events_; |
Lalit Maganti | caed37e | 2018-06-01 03:03:08 +0100 | [diff] [blame] | 208 | |
| 209 | // One entry for each unique string in the trace. |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 210 | std::deque<std::string> string_pool_; |
Lalit Maganti | caed37e | 2018-06-01 03:03:08 +0100 | [diff] [blame] | 211 | |
| 212 | // One entry for each unique string in the trace. |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 213 | std::unordered_map<StringHash, StringId> string_index_; |
Isabelle Taylor | 47328cf | 2018-06-12 14:33:59 +0100 | [diff] [blame] | 214 | |
| 215 | // Each pid can have multiple UniquePid entries, a new UniquePid is assigned |
| 216 | // each time a process is seen in the trace. |
| 217 | std::multimap<uint32_t, UniquePid> pids_; |
| 218 | |
| 219 | // One entry for each UniquePid, with UniquePid as the index. |
Isabelle Taylor | 3dd366c | 2018-06-22 16:21:41 +0100 | [diff] [blame] | 220 | std::deque<Process> unique_processes_; |
Isabelle Taylor | 68e4219 | 2018-06-19 16:19:31 +0100 | [diff] [blame] | 221 | |
| 222 | // Each tid can have multiple UniqueTid entries, a new UniqueTid is assigned |
| 223 | // each time a thread is seen in the trace. |
| 224 | std::multimap<uint32_t, UniqueTid> tids_; |
| 225 | |
| 226 | // One entry for each UniqueTid, with UniqueTid as the index. |
Isabelle Taylor | 3dd366c | 2018-06-22 16:21:41 +0100 | [diff] [blame] | 227 | std::deque<Thread> unique_threads_; |
Lalit Maganti | caed37e | 2018-06-01 03:03:08 +0100 | [diff] [blame] | 228 | }; |
| 229 | |
| 230 | } // namespace trace_processor |
| 231 | } // namespace perfetto |
| 232 | |
| 233 | #endif // SRC_TRACE_PROCESSOR_TRACE_STORAGE_H_ |