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" |
Lalit Maganti | 770886a | 2018-11-16 17:40:21 +0000 | [diff] [blame] | 28 | #include "perfetto/base/optional.h" |
Primiano Tucci | 2da5d2e | 2018-08-10 14:23:31 +0100 | [diff] [blame] | 29 | #include "perfetto/base/string_view.h" |
Hector Dearman | c833993 | 2018-08-10 11:22:46 +0100 | [diff] [blame] | 30 | #include "perfetto/base/utils.h" |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 31 | |
Lalit Maganti | caed37e | 2018-06-01 03:03:08 +0100 | [diff] [blame] | 32 | namespace perfetto { |
| 33 | namespace trace_processor { |
| 34 | |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 35 | // UniquePid is an offset into |unique_processes_|. This is necessary because |
| 36 | // Unix pids are reused and thus not guaranteed to be unique over a long |
| 37 | // period of time. |
| 38 | using UniquePid = uint32_t; |
Primiano Tucci | 0d72a31 | 2018-08-07 14:42:45 +0100 | [diff] [blame] | 39 | |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 40 | // UniqueTid is an offset into |unique_threads_|. Necessary because tids can |
| 41 | // be reused. |
| 42 | using UniqueTid = uint32_t; |
| 43 | |
Primiano Tucci | 0d72a31 | 2018-08-07 14:42:45 +0100 | [diff] [blame] | 44 | // StringId is an offset into |string_pool_|. |
| 45 | using StringId = size_t; |
| 46 | |
Isabelle Taylor | a97c5f5 | 2018-10-23 17:36:12 +0100 | [diff] [blame] | 47 | enum RefType { |
Primiano Tucci | 5403e4f | 2018-11-27 10:07:03 +0000 | [diff] [blame] | 48 | kRefNoRef = 0, |
| 49 | kRefUtid = 1, |
| 50 | kRefCpuId = 2, |
| 51 | kRefIrq = 3, |
| 52 | kRefSoftIrq = 4, |
| 53 | kRefUpid = 5, |
| 54 | kRefUtidLookupUpid = 6, |
| 55 | kRefMax |
Isabelle Taylor | a97c5f5 | 2018-10-23 17:36:12 +0100 | [diff] [blame] | 56 | }; |
Isabelle Taylor | 14674d4 | 2018-09-07 11:33:11 +0100 | [diff] [blame] | 57 | |
Lalit Maganti | caed37e | 2018-06-01 03:03:08 +0100 | [diff] [blame] | 58 | // Stores a data inside a trace file in a columnar form. This makes it efficient |
| 59 | // to read or search across a single field of the trace (e.g. all the thread |
| 60 | // names for a given CPU). |
| 61 | class TraceStorage { |
| 62 | public: |
Isabelle Taylor | 47328cf | 2018-06-12 14:33:59 +0100 | [diff] [blame] | 63 | TraceStorage(); |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 64 | TraceStorage(const TraceStorage&) = delete; |
| 65 | |
| 66 | virtual ~TraceStorage(); |
Isabelle Taylor | 47328cf | 2018-06-12 14:33:59 +0100 | [diff] [blame] | 67 | |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 68 | struct Stats { |
Lalit Maganti | 6fdb984 | 2018-11-12 11:00:00 +0000 | [diff] [blame] | 69 | uint64_t mismatched_sched_switch_tids = 0; |
| 70 | uint64_t rss_stat_no_process = 0; |
| 71 | uint64_t mem_counter_no_process = 0; |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 72 | }; |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 73 | |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 74 | // Information about a unique process seen in a trace. |
| 75 | struct Process { |
Primiano Tucci | b75dcee | 2018-08-08 12:21:36 +0100 | [diff] [blame] | 76 | explicit Process(uint32_t p) : pid(p) {} |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 77 | uint64_t start_ns = 0; |
| 78 | uint64_t end_ns = 0; |
| 79 | StringId name_id = 0; |
Primiano Tucci | b75dcee | 2018-08-08 12:21:36 +0100 | [diff] [blame] | 80 | uint32_t pid = 0; |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 81 | }; |
| 82 | |
| 83 | // Information about a unique thread seen in a trace. |
| 84 | struct Thread { |
Primiano Tucci | b75dcee | 2018-08-08 12:21:36 +0100 | [diff] [blame] | 85 | explicit Thread(uint32_t t) : tid(t) {} |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 86 | uint64_t start_ns = 0; |
| 87 | uint64_t end_ns = 0; |
| 88 | StringId name_id = 0; |
Lalit Maganti | 770886a | 2018-11-16 17:40:21 +0000 | [diff] [blame] | 89 | base::Optional<UniquePid> upid; |
Primiano Tucci | b75dcee | 2018-08-08 12:21:36 +0100 | [diff] [blame] | 90 | uint32_t tid = 0; |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 91 | }; |
Isabelle Taylor | 3dd366c | 2018-06-22 16:21:41 +0100 | [diff] [blame] | 92 | |
Lalit Maganti | 6e9c55e | 2018-11-29 12:00:39 +0000 | [diff] [blame^] | 93 | // Generic key value storage which can be referenced by other tables. |
| 94 | class Args { |
| 95 | public: |
| 96 | using Id = uint64_t; |
| 97 | |
| 98 | // Varardic type representing the possible values for the args table. |
| 99 | struct Varardic { |
| 100 | enum Type { kInt, kString, kReal }; |
| 101 | |
| 102 | Varardic(int64_t int_val) : type(kInt), int_value(int_val) {} |
| 103 | Varardic(StringId string_val) : type(kString), string_value(string_val) {} |
| 104 | Varardic(double real_val) : type(kReal), real_value(real_val) {} |
| 105 | |
| 106 | Type type; |
| 107 | union { |
| 108 | int64_t int_value; |
| 109 | StringId string_value; |
| 110 | double real_value; |
| 111 | }; |
| 112 | }; |
| 113 | |
| 114 | // Identifiers for all the tables in the database. |
| 115 | enum TableId : uint8_t { |
| 116 | // Intentionally don't have Table == 0 so that Id == 0 can refer to an |
| 117 | // invalid id. |
| 118 | kCounters = 1, |
| 119 | }; |
| 120 | |
| 121 | // Allows insertion of data into the args table. |
| 122 | class Inserter { |
| 123 | public: |
| 124 | Inserter() : storage_(nullptr) {} |
| 125 | |
| 126 | Inserter(TraceStorage* storage, TableId table_id, uint32_t row) |
| 127 | : storage_(storage), table_and_row_(std::make_pair(table_id, row)) {} |
| 128 | |
| 129 | // Allow std::move(). |
| 130 | Inserter(Inserter&&) noexcept = default; |
| 131 | Inserter& operator=(Inserter&&) = default; |
| 132 | |
| 133 | // Disable implicit copy. |
| 134 | Inserter(const Inserter&) = delete; |
| 135 | Inserter& operator=(const Inserter&) = delete; |
| 136 | |
| 137 | void AddInt64Arg(StringId key_without_index, |
| 138 | StringId key_with_index, |
| 139 | int64_t value) { |
| 140 | if (!table_and_row_.has_value()) |
| 141 | return; |
| 142 | |
| 143 | const auto& table_and_row = table_and_row_.value(); |
| 144 | TableId table_id = table_and_row.first; |
| 145 | uint32_t row = table_and_row.second; |
| 146 | |
| 147 | Id id = CreateId(table_id, row); |
| 148 | auto* args = storage_->mutable_args(); |
| 149 | args->ids_.emplace_back(id); |
| 150 | args->keys_without_index_.emplace_back(key_without_index); |
| 151 | args->keys_with_index_.emplace_back(key_with_index); |
| 152 | args->arg_values_.emplace_back(value); |
| 153 | args->args_for_id_.emplace(id, args->args_count() - 1); |
| 154 | |
| 155 | switch (table_id) { |
| 156 | case TableId::kCounters: |
| 157 | storage_->mutable_counters()->set_arg_id(row, id); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | private: |
| 162 | TraceStorage* storage_; |
| 163 | base::Optional<std::pair<TableId, uint32_t>> table_and_row_; |
| 164 | }; |
| 165 | |
| 166 | static const Id kInvalidId; |
| 167 | |
| 168 | const std::deque<Id>& ids() const { return ids_; } |
| 169 | const std::deque<StringId>& keys_without_index() const { |
| 170 | return keys_without_index_; |
| 171 | } |
| 172 | const std::deque<StringId>& keys_with_index() const { |
| 173 | return keys_with_index_; |
| 174 | } |
| 175 | const std::deque<Varardic>& arg_values() const { return arg_values_; } |
| 176 | size_t args_count() const { return ids_.size(); } |
| 177 | |
| 178 | private: |
| 179 | static Id CreateId(TableId table, uint32_t row) { |
| 180 | static constexpr uint64_t kRowIdTableShift = 32ul; |
| 181 | return (static_cast<uint64_t>(table) << kRowIdTableShift) | row; |
| 182 | } |
| 183 | |
| 184 | std::deque<Id> ids_; |
| 185 | std::deque<StringId> keys_without_index_; |
| 186 | std::deque<StringId> keys_with_index_; |
| 187 | std::deque<Varardic> arg_values_; |
| 188 | std::multimap<Id, size_t> args_for_id_; |
| 189 | }; |
| 190 | |
Lalit Maganti | ff69c11 | 2018-09-24 12:07:47 +0100 | [diff] [blame] | 191 | class Slices { |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 192 | public: |
Lalit Maganti | fde2904 | 2018-10-04 13:28:52 +0100 | [diff] [blame] | 193 | inline size_t AddSlice(uint32_t cpu, |
| 194 | uint64_t start_ns, |
| 195 | uint64_t duration_ns, |
| 196 | UniqueTid utid) { |
Lalit Maganti | ff69c11 | 2018-09-24 12:07:47 +0100 | [diff] [blame] | 197 | cpus_.emplace_back(cpu); |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 198 | start_ns_.emplace_back(start_ns); |
| 199 | durations_.emplace_back(duration_ns); |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 200 | utids_.emplace_back(utid); |
Lalit Maganti | fde2904 | 2018-10-04 13:28:52 +0100 | [diff] [blame] | 201 | return slice_count() - 1; |
| 202 | } |
| 203 | |
| 204 | void set_duration(size_t index, uint64_t duration_ns) { |
| 205 | durations_[index] = duration_ns; |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 206 | } |
| 207 | |
Isabelle Taylor | 47328cf | 2018-06-12 14:33:59 +0100 | [diff] [blame] | 208 | size_t slice_count() const { return start_ns_.size(); } |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 209 | |
Lalit Maganti | ff69c11 | 2018-09-24 12:07:47 +0100 | [diff] [blame] | 210 | const std::deque<uint32_t>& cpus() const { return cpus_; } |
| 211 | |
Isabelle Taylor | 47328cf | 2018-06-12 14:33:59 +0100 | [diff] [blame] | 212 | const std::deque<uint64_t>& start_ns() const { return start_ns_; } |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 213 | |
Isabelle Taylor | 47328cf | 2018-06-12 14:33:59 +0100 | [diff] [blame] | 214 | const std::deque<uint64_t>& durations() const { return durations_; } |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 215 | |
Isabelle Taylor | 68e4219 | 2018-06-19 16:19:31 +0100 | [diff] [blame] | 216 | const std::deque<UniqueTid>& utids() const { return utids_; } |
| 217 | |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 218 | private: |
Hector Dearman | 947f12a | 2018-09-11 16:50:36 +0100 | [diff] [blame] | 219 | // Each deque below has the same number of entries (the number of slices |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 220 | // in the trace for the CPU). |
Lalit Maganti | ff69c11 | 2018-09-24 12:07:47 +0100 | [diff] [blame] | 221 | std::deque<uint32_t> cpus_; |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 222 | std::deque<uint64_t> start_ns_; |
| 223 | std::deque<uint64_t> durations_; |
Isabelle Taylor | 68e4219 | 2018-06-19 16:19:31 +0100 | [diff] [blame] | 224 | std::deque<UniqueTid> utids_; |
Primiano Tucci | 0d72a31 | 2018-08-07 14:42:45 +0100 | [diff] [blame] | 225 | }; |
Isabelle Taylor | 68e4219 | 2018-06-19 16:19:31 +0100 | [diff] [blame] | 226 | |
Primiano Tucci | 0d72a31 | 2018-08-07 14:42:45 +0100 | [diff] [blame] | 227 | class NestableSlices { |
| 228 | public: |
Lalit Maganti | 7b37bbf | 2018-11-09 15:58:54 +0000 | [diff] [blame] | 229 | inline size_t AddSlice(uint64_t start_ns, |
| 230 | uint64_t duration_ns, |
| 231 | UniqueTid utid, |
| 232 | StringId cat, |
| 233 | StringId name, |
| 234 | uint8_t depth, |
| 235 | uint64_t stack_id, |
| 236 | uint64_t parent_stack_id) { |
Primiano Tucci | 0d72a31 | 2018-08-07 14:42:45 +0100 | [diff] [blame] | 237 | start_ns_.emplace_back(start_ns); |
| 238 | durations_.emplace_back(duration_ns); |
| 239 | utids_.emplace_back(utid); |
| 240 | cats_.emplace_back(cat); |
| 241 | names_.emplace_back(name); |
| 242 | depths_.emplace_back(depth); |
Primiano Tucci | b75dcee | 2018-08-08 12:21:36 +0100 | [diff] [blame] | 243 | stack_ids_.emplace_back(stack_id); |
| 244 | parent_stack_ids_.emplace_back(parent_stack_id); |
Lalit Maganti | 7b37bbf | 2018-11-09 15:58:54 +0000 | [diff] [blame] | 245 | return slice_count() - 1; |
| 246 | } |
| 247 | |
| 248 | void set_duration(size_t index, uint64_t duration_ns) { |
| 249 | durations_[index] = duration_ns; |
| 250 | } |
| 251 | |
| 252 | void set_stack_id(size_t index, uint64_t stack_id) { |
| 253 | stack_ids_[index] = stack_id; |
Primiano Tucci | 0d72a31 | 2018-08-07 14:42:45 +0100 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | size_t slice_count() const { return start_ns_.size(); } |
| 257 | const std::deque<uint64_t>& start_ns() const { return start_ns_; } |
| 258 | const std::deque<uint64_t>& durations() const { return durations_; } |
| 259 | const std::deque<UniqueTid>& utids() const { return utids_; } |
| 260 | const std::deque<StringId>& cats() const { return cats_; } |
| 261 | const std::deque<StringId>& names() const { return names_; } |
| 262 | const std::deque<uint8_t>& depths() const { return depths_; } |
Primiano Tucci | b75dcee | 2018-08-08 12:21:36 +0100 | [diff] [blame] | 263 | const std::deque<uint64_t>& stack_ids() const { return stack_ids_; } |
| 264 | const std::deque<uint64_t>& parent_stack_ids() const { |
| 265 | return parent_stack_ids_; |
| 266 | } |
Primiano Tucci | 0d72a31 | 2018-08-07 14:42:45 +0100 | [diff] [blame] | 267 | |
| 268 | private: |
| 269 | std::deque<uint64_t> start_ns_; |
| 270 | std::deque<uint64_t> durations_; |
| 271 | std::deque<UniqueTid> utids_; |
| 272 | std::deque<StringId> cats_; |
| 273 | std::deque<StringId> names_; |
| 274 | std::deque<uint8_t> depths_; |
Primiano Tucci | b75dcee | 2018-08-08 12:21:36 +0100 | [diff] [blame] | 275 | std::deque<uint64_t> stack_ids_; |
| 276 | std::deque<uint64_t> parent_stack_ids_; |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 277 | }; |
| 278 | |
Isabelle Taylor | 15314ea | 2018-09-19 11:35:19 +0100 | [diff] [blame] | 279 | class Counters { |
| 280 | public: |
Lalit Maganti | fde2904 | 2018-10-04 13:28:52 +0100 | [diff] [blame] | 281 | inline size_t AddCounter(uint64_t timestamp, |
| 282 | uint64_t duration, |
| 283 | StringId name_id, |
| 284 | double value, |
Lalit Maganti | fde2904 | 2018-10-04 13:28:52 +0100 | [diff] [blame] | 285 | int64_t ref, |
| 286 | RefType type) { |
Isabelle Taylor | 15314ea | 2018-09-19 11:35:19 +0100 | [diff] [blame] | 287 | timestamps_.emplace_back(timestamp); |
| 288 | durations_.emplace_back(duration); |
| 289 | name_ids_.emplace_back(name_id); |
| 290 | values_.emplace_back(value); |
| 291 | refs_.emplace_back(ref); |
| 292 | types_.emplace_back(type); |
Lalit Maganti | 6e9c55e | 2018-11-29 12:00:39 +0000 | [diff] [blame^] | 293 | arg_ids_.emplace_back(Args::kInvalidId); |
Lalit Maganti | fde2904 | 2018-10-04 13:28:52 +0100 | [diff] [blame] | 294 | return counter_count() - 1; |
Isabelle Taylor | 15314ea | 2018-09-19 11:35:19 +0100 | [diff] [blame] | 295 | } |
Lalit Maganti | fde2904 | 2018-10-04 13:28:52 +0100 | [diff] [blame] | 296 | |
| 297 | void set_duration(size_t index, uint64_t duration) { |
| 298 | durations_[index] = duration; |
| 299 | } |
| 300 | |
Lalit Maganti | 6e9c55e | 2018-11-29 12:00:39 +0000 | [diff] [blame^] | 301 | void set_arg_id(size_t index, Args::Id arg_id) { arg_ids_[index] = arg_id; } |
| 302 | |
Isabelle Taylor | 15314ea | 2018-09-19 11:35:19 +0100 | [diff] [blame] | 303 | size_t counter_count() const { return timestamps_.size(); } |
| 304 | |
| 305 | const std::deque<uint64_t>& timestamps() const { return timestamps_; } |
| 306 | |
| 307 | const std::deque<uint64_t>& durations() const { return durations_; } |
| 308 | |
| 309 | const std::deque<StringId>& name_ids() const { return name_ids_; } |
| 310 | |
| 311 | const std::deque<double>& values() const { return values_; } |
| 312 | |
| 313 | const std::deque<int64_t>& refs() const { return refs_; } |
| 314 | |
| 315 | const std::deque<RefType>& types() const { return types_; } |
| 316 | |
Lalit Maganti | 6e9c55e | 2018-11-29 12:00:39 +0000 | [diff] [blame^] | 317 | const std::deque<Args::Id>& arg_ids() const { return arg_ids_; } |
| 318 | |
Isabelle Taylor | 15314ea | 2018-09-19 11:35:19 +0100 | [diff] [blame] | 319 | private: |
| 320 | std::deque<uint64_t> timestamps_; |
| 321 | std::deque<uint64_t> durations_; |
| 322 | std::deque<StringId> name_ids_; |
| 323 | std::deque<double> values_; |
| 324 | std::deque<int64_t> refs_; |
| 325 | std::deque<RefType> types_; |
Lalit Maganti | 6e9c55e | 2018-11-29 12:00:39 +0000 | [diff] [blame^] | 326 | std::deque<Args::Id> arg_ids_; |
Isabelle Taylor | 15314ea | 2018-09-19 11:35:19 +0100 | [diff] [blame] | 327 | }; |
| 328 | |
Primiano Tucci | 5cb84f8 | 2018-10-31 21:46:36 -0700 | [diff] [blame] | 329 | class SqlStats { |
| 330 | public: |
| 331 | static constexpr size_t kMaxLogEntries = 100; |
| 332 | void RecordQueryBegin(const std::string& query, |
| 333 | uint64_t time_queued, |
| 334 | uint64_t time_started); |
| 335 | void RecordQueryEnd(uint64_t time_ended); |
| 336 | size_t size() const { return queries_.size(); } |
| 337 | const std::deque<std::string>& queries() const { return queries_; } |
| 338 | const std::deque<uint64_t>& times_queued() const { return times_queued_; } |
| 339 | const std::deque<uint64_t>& times_started() const { return times_started_; } |
| 340 | const std::deque<uint64_t>& times_ended() const { return times_ended_; } |
| 341 | |
| 342 | private: |
| 343 | std::deque<std::string> queries_; |
| 344 | std::deque<uint64_t> times_queued_; |
| 345 | std::deque<uint64_t> times_started_; |
| 346 | std::deque<uint64_t> times_ended_; |
| 347 | }; |
| 348 | |
Isabelle Taylor | c8c1120 | 2018-11-05 11:36:22 +0000 | [diff] [blame] | 349 | class Instants { |
| 350 | public: |
| 351 | inline size_t AddInstantEvent(uint64_t timestamp, |
| 352 | StringId name_id, |
| 353 | double value, |
| 354 | int64_t ref, |
| 355 | RefType type) { |
| 356 | timestamps_.emplace_back(timestamp); |
| 357 | name_ids_.emplace_back(name_id); |
| 358 | values_.emplace_back(value); |
| 359 | refs_.emplace_back(ref); |
| 360 | types_.emplace_back(type); |
| 361 | return instant_count() - 1; |
| 362 | } |
| 363 | |
| 364 | size_t instant_count() const { return timestamps_.size(); } |
| 365 | |
| 366 | const std::deque<uint64_t>& timestamps() const { return timestamps_; } |
| 367 | |
| 368 | const std::deque<StringId>& name_ids() const { return name_ids_; } |
| 369 | |
| 370 | const std::deque<double>& values() const { return values_; } |
| 371 | |
| 372 | const std::deque<int64_t>& refs() const { return refs_; } |
| 373 | |
| 374 | const std::deque<RefType>& types() const { return types_; } |
| 375 | |
| 376 | private: |
| 377 | std::deque<uint64_t> timestamps_; |
| 378 | std::deque<StringId> name_ids_; |
| 379 | std::deque<double> values_; |
| 380 | std::deque<int64_t> refs_; |
| 381 | std::deque<RefType> types_; |
| 382 | }; |
| 383 | |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 384 | void ResetStorage(); |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 385 | |
Primiano Tucci | b75dcee | 2018-08-08 12:21:36 +0100 | [diff] [blame] | 386 | UniqueTid AddEmptyThread(uint32_t tid) { |
| 387 | unique_threads_.emplace_back(tid); |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 388 | return static_cast<UniqueTid>(unique_threads_.size() - 1); |
| 389 | } |
Isabelle Taylor | 47328cf | 2018-06-12 14:33:59 +0100 | [diff] [blame] | 390 | |
Primiano Tucci | b75dcee | 2018-08-08 12:21:36 +0100 | [diff] [blame] | 391 | UniquePid AddEmptyProcess(uint32_t pid) { |
| 392 | unique_processes_.emplace_back(pid); |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 393 | return static_cast<UniquePid>(unique_processes_.size() - 1); |
| 394 | } |
Isabelle Taylor | 3dd366c | 2018-06-22 16:21:41 +0100 | [diff] [blame] | 395 | |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 396 | // Return an unqiue identifier for the contents of each string. |
| 397 | // The string is copied internally and can be destroyed after this called. |
Isabelle Taylor | 15314ea | 2018-09-19 11:35:19 +0100 | [diff] [blame] | 398 | // Virtual for testing. |
| 399 | virtual StringId InternString(base::StringView); |
Isabelle Taylor | 47328cf | 2018-06-12 14:33:59 +0100 | [diff] [blame] | 400 | |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 401 | Process* GetMutableProcess(UniquePid upid) { |
Lalit Maganti | 676658b | 2018-11-20 18:24:31 +0000 | [diff] [blame] | 402 | PERFETTO_DCHECK(upid < unique_processes_.size()); |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 403 | return &unique_processes_[upid]; |
| 404 | } |
Isabelle Taylor | 3dd366c | 2018-06-22 16:21:41 +0100 | [diff] [blame] | 405 | |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 406 | Thread* GetMutableThread(UniqueTid utid) { |
Florian Mayer | a1aaec2 | 2018-09-19 17:02:26 +0100 | [diff] [blame] | 407 | PERFETTO_DCHECK(utid < unique_threads_.size()); |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 408 | return &unique_threads_[utid]; |
| 409 | } |
Isabelle Taylor | 3dd366c | 2018-06-22 16:21:41 +0100 | [diff] [blame] | 410 | |
Lalit Maganti | caed37e | 2018-06-01 03:03:08 +0100 | [diff] [blame] | 411 | // Reading methods. |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 412 | const std::string& GetString(StringId id) const { |
| 413 | PERFETTO_DCHECK(id < string_pool_.size()); |
| 414 | return string_pool_[id]; |
| 415 | } |
| 416 | |
Lalit Maganti | e9d4053 | 2018-06-29 13:15:06 +0100 | [diff] [blame] | 417 | const Process& GetProcess(UniquePid upid) const { |
Lalit Maganti | 676658b | 2018-11-20 18:24:31 +0000 | [diff] [blame] | 418 | PERFETTO_DCHECK(upid < unique_processes_.size()); |
Isabelle Taylor | 47328cf | 2018-06-12 14:33:59 +0100 | [diff] [blame] | 419 | return unique_processes_[upid]; |
| 420 | } |
| 421 | |
Lalit Maganti | e9d4053 | 2018-06-29 13:15:06 +0100 | [diff] [blame] | 422 | const Thread& GetThread(UniqueTid utid) const { |
Lalit Maganti | 1f64cfa | 2018-09-18 13:20:02 +0100 | [diff] [blame] | 423 | // Allow utid == 0 for idle thread retrieval. |
Florian Mayer | a1aaec2 | 2018-09-19 17:02:26 +0100 | [diff] [blame] | 424 | PERFETTO_DCHECK(utid < unique_threads_.size()); |
Isabelle Taylor | 68e4219 | 2018-06-19 16:19:31 +0100 | [diff] [blame] | 425 | return unique_threads_[utid]; |
| 426 | } |
| 427 | |
Lalit Maganti | ff69c11 | 2018-09-24 12:07:47 +0100 | [diff] [blame] | 428 | const Slices& slices() const { return slices_; } |
Lalit Maganti | fde2904 | 2018-10-04 13:28:52 +0100 | [diff] [blame] | 429 | Slices* mutable_slices() { return &slices_; } |
| 430 | |
Primiano Tucci | 0d72a31 | 2018-08-07 14:42:45 +0100 | [diff] [blame] | 431 | const NestableSlices& nestable_slices() const { return nestable_slices_; } |
| 432 | NestableSlices* mutable_nestable_slices() { return &nestable_slices_; } |
| 433 | |
Isabelle Taylor | 15314ea | 2018-09-19 11:35:19 +0100 | [diff] [blame] | 434 | const Counters& counters() const { return counters_; } |
| 435 | Counters* mutable_counters() { return &counters_; } |
Isabelle Taylor | 14674d4 | 2018-09-07 11:33:11 +0100 | [diff] [blame] | 436 | |
Primiano Tucci | 5cb84f8 | 2018-10-31 21:46:36 -0700 | [diff] [blame] | 437 | const SqlStats& sql_stats() const { return sql_stats_; } |
| 438 | SqlStats* mutable_sql_stats() { return &sql_stats_; } |
| 439 | |
Isabelle Taylor | c8c1120 | 2018-11-05 11:36:22 +0000 | [diff] [blame] | 440 | const Instants& instants() const { return instants_; } |
| 441 | Instants* mutable_instants() { return &instants_; } |
| 442 | |
Lalit Maganti | 05e8c13 | 2018-11-09 18:16:12 +0000 | [diff] [blame] | 443 | const Stats& stats() const { return stats_; } |
| 444 | Stats* mutable_stats() { return &stats_; } |
| 445 | |
Lalit Maganti | 6e9c55e | 2018-11-29 12:00:39 +0000 | [diff] [blame^] | 446 | const Args& args() const { return args_; } |
| 447 | Args* mutable_args() { return &args_; } |
| 448 | |
Lalit Maganti | acda68b | 2018-10-29 15:23:25 +0000 | [diff] [blame] | 449 | const std::deque<std::string>& string_pool() const { return string_pool_; } |
| 450 | |
Isabelle Taylor | e7003fb | 2018-07-17 11:39:01 +0100 | [diff] [blame] | 451 | // |unique_processes_| always contains at least 1 element becuase the 0th ID |
| 452 | // is reserved to indicate an invalid process. |
| 453 | size_t process_count() const { return unique_processes_.size() - 1; } |
Primiano Tucci | 0d72a31 | 2018-08-07 14:42:45 +0100 | [diff] [blame] | 454 | |
Isabelle Taylor | e7003fb | 2018-07-17 11:39:01 +0100 | [diff] [blame] | 455 | // |unique_threads_| always contains at least 1 element becuase the 0th ID |
| 456 | // is reserved to indicate an invalid thread. |
| 457 | size_t thread_count() const { return unique_threads_.size() - 1; } |
| 458 | |
Hector Dearman | 1232336 | 2018-08-09 16:09:28 +0100 | [diff] [blame] | 459 | // Number of interned strings in the pool. Includes the empty string w/ ID=0. |
| 460 | size_t string_count() const { return string_pool_.size(); } |
| 461 | |
Lalit Maganti | caed37e | 2018-06-01 03:03:08 +0100 | [diff] [blame] | 462 | private: |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 463 | TraceStorage& operator=(const TraceStorage&) = default; |
| 464 | |
Primiano Tucci | 2da5d2e | 2018-08-10 14:23:31 +0100 | [diff] [blame] | 465 | using StringHash = uint64_t; |
Lalit Maganti | caed37e | 2018-06-01 03:03:08 +0100 | [diff] [blame] | 466 | |
Lalit Maganti | 05e8c13 | 2018-11-09 18:16:12 +0000 | [diff] [blame] | 467 | // Stats about parsing the trace. |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 468 | Stats stats_; |
| 469 | |
Lalit Maganti | caed37e | 2018-06-01 03:03:08 +0100 | [diff] [blame] | 470 | // One entry for each CPU in the trace. |
Lalit Maganti | ff69c11 | 2018-09-24 12:07:47 +0100 | [diff] [blame] | 471 | Slices slices_; |
Lalit Maganti | caed37e | 2018-06-01 03:03:08 +0100 | [diff] [blame] | 472 | |
Lalit Maganti | 6e9c55e | 2018-11-29 12:00:39 +0000 | [diff] [blame^] | 473 | // Args for all other tables. |
| 474 | Args args_; |
| 475 | |
Lalit Maganti | caed37e | 2018-06-01 03:03:08 +0100 | [diff] [blame] | 476 | // One entry for each unique string in the trace. |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 477 | std::deque<std::string> string_pool_; |
Lalit Maganti | caed37e | 2018-06-01 03:03:08 +0100 | [diff] [blame] | 478 | |
| 479 | // One entry for each unique string in the trace. |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 480 | std::unordered_map<StringHash, StringId> string_index_; |
Isabelle Taylor | 47328cf | 2018-06-12 14:33:59 +0100 | [diff] [blame] | 481 | |
Isabelle Taylor | 47328cf | 2018-06-12 14:33:59 +0100 | [diff] [blame] | 482 | // One entry for each UniquePid, with UniquePid as the index. |
Isabelle Taylor | 3dd366c | 2018-06-22 16:21:41 +0100 | [diff] [blame] | 483 | std::deque<Process> unique_processes_; |
Isabelle Taylor | 68e4219 | 2018-06-19 16:19:31 +0100 | [diff] [blame] | 484 | |
Isabelle Taylor | 68e4219 | 2018-06-19 16:19:31 +0100 | [diff] [blame] | 485 | // One entry for each UniqueTid, with UniqueTid as the index. |
Isabelle Taylor | 3dd366c | 2018-06-22 16:21:41 +0100 | [diff] [blame] | 486 | std::deque<Thread> unique_threads_; |
Primiano Tucci | 0d72a31 | 2018-08-07 14:42:45 +0100 | [diff] [blame] | 487 | |
| 488 | // Slices coming from userspace events (e.g. Chromium TRACE_EVENT macros). |
| 489 | NestableSlices nestable_slices_; |
Isabelle Taylor | 15314ea | 2018-09-19 11:35:19 +0100 | [diff] [blame] | 490 | |
| 491 | // Counter events from the trace. This includes CPU frequency events as well |
| 492 | // systrace trace_marker counter events. |
| 493 | Counters counters_; |
Primiano Tucci | 5cb84f8 | 2018-10-31 21:46:36 -0700 | [diff] [blame] | 494 | |
| 495 | SqlStats sql_stats_; |
Lalit Maganti | 6e9c55e | 2018-11-29 12:00:39 +0000 | [diff] [blame^] | 496 | |
Isabelle Taylor | c8c1120 | 2018-11-05 11:36:22 +0000 | [diff] [blame] | 497 | // These are instantaneous events in the trace. They have no duration |
| 498 | // and do not have a value that make sense to track over time. |
| 499 | // e.g. signal events |
| 500 | Instants instants_; |
Lalit Maganti | caed37e | 2018-06-01 03:03:08 +0100 | [diff] [blame] | 501 | }; |
| 502 | |
| 503 | } // namespace trace_processor |
| 504 | } // namespace perfetto |
| 505 | |
| 506 | #endif // SRC_TRACE_PROCESSOR_TRACE_STORAGE_H_ |