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_|. |
Lalit Maganti | 85ca4a8 | 2018-12-07 17:28:02 +0000 | [diff] [blame] | 45 | using StringId = uint32_t; |
Primiano Tucci | 0d72a31 | 2018-08-07 14:42:45 +0100 | [diff] [blame] | 46 | |
Lalit Maganti | 5ea9e93 | 2018-11-30 14:19:39 +0000 | [diff] [blame] | 47 | // Identifiers for all the tables in the database. |
| 48 | enum TableId : uint8_t { |
| 49 | // Intentionally don't have TableId == 0 so that RowId == 0 can refer to an |
| 50 | // invalid row id. |
| 51 | kCounters = 1, |
Lalit Maganti | 1d915a6 | 2019-01-07 12:10:42 +0000 | [diff] [blame^] | 52 | kRawEvents = 2, |
Lalit Maganti | 5ea9e93 | 2018-11-30 14:19:39 +0000 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | // The top 8 bits are set to the TableId and the bottom 32 to the row of the |
| 56 | // table. |
Lalit Maganti | 85ca4a8 | 2018-12-07 17:28:02 +0000 | [diff] [blame] | 57 | using RowId = int64_t; |
Lalit Maganti | 5ea9e93 | 2018-11-30 14:19:39 +0000 | [diff] [blame] | 58 | |
| 59 | static const RowId kInvalidRowId = 0; |
| 60 | |
Isabelle Taylor | a97c5f5 | 2018-10-23 17:36:12 +0100 | [diff] [blame] | 61 | enum RefType { |
Primiano Tucci | 5403e4f | 2018-11-27 10:07:03 +0000 | [diff] [blame] | 62 | kRefNoRef = 0, |
| 63 | kRefUtid = 1, |
| 64 | kRefCpuId = 2, |
| 65 | kRefIrq = 3, |
| 66 | kRefSoftIrq = 4, |
| 67 | kRefUpid = 5, |
| 68 | kRefUtidLookupUpid = 6, |
| 69 | kRefMax |
Isabelle Taylor | a97c5f5 | 2018-10-23 17:36:12 +0100 | [diff] [blame] | 70 | }; |
Isabelle Taylor | 14674d4 | 2018-09-07 11:33:11 +0100 | [diff] [blame] | 71 | |
Lalit Maganti | caed37e | 2018-06-01 03:03:08 +0100 | [diff] [blame] | 72 | // Stores a data inside a trace file in a columnar form. This makes it efficient |
| 73 | // to read or search across a single field of the trace (e.g. all the thread |
| 74 | // names for a given CPU). |
| 75 | class TraceStorage { |
| 76 | public: |
Isabelle Taylor | 47328cf | 2018-06-12 14:33:59 +0100 | [diff] [blame] | 77 | TraceStorage(); |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 78 | TraceStorage(const TraceStorage&) = delete; |
| 79 | |
| 80 | virtual ~TraceStorage(); |
Isabelle Taylor | 47328cf | 2018-06-12 14:33:59 +0100 | [diff] [blame] | 81 | |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 82 | struct Stats { |
Lalit Maganti | 85ca4a8 | 2018-12-07 17:28:02 +0000 | [diff] [blame] | 83 | int64_t mismatched_sched_switch_tids = 0; |
| 84 | int64_t rss_stat_no_process = 0; |
| 85 | int64_t mem_counter_no_process = 0; |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 86 | }; |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 87 | |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 88 | // Information about a unique process seen in a trace. |
| 89 | struct Process { |
Primiano Tucci | b75dcee | 2018-08-08 12:21:36 +0100 | [diff] [blame] | 90 | explicit Process(uint32_t p) : pid(p) {} |
Lalit Maganti | 85ca4a8 | 2018-12-07 17:28:02 +0000 | [diff] [blame] | 91 | int64_t start_ns = 0; |
| 92 | int64_t end_ns = 0; |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 93 | StringId name_id = 0; |
Primiano Tucci | b75dcee | 2018-08-08 12:21:36 +0100 | [diff] [blame] | 94 | uint32_t pid = 0; |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | // Information about a unique thread seen in a trace. |
| 98 | struct Thread { |
Primiano Tucci | b75dcee | 2018-08-08 12:21:36 +0100 | [diff] [blame] | 99 | explicit Thread(uint32_t t) : tid(t) {} |
Lalit Maganti | 85ca4a8 | 2018-12-07 17:28:02 +0000 | [diff] [blame] | 100 | int64_t start_ns = 0; |
| 101 | int64_t end_ns = 0; |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 102 | StringId name_id = 0; |
Lalit Maganti | 770886a | 2018-11-16 17:40:21 +0000 | [diff] [blame] | 103 | base::Optional<UniquePid> upid; |
Primiano Tucci | b75dcee | 2018-08-08 12:21:36 +0100 | [diff] [blame] | 104 | uint32_t tid = 0; |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 105 | }; |
Isabelle Taylor | 3dd366c | 2018-06-22 16:21:41 +0100 | [diff] [blame] | 106 | |
Lalit Maganti | 6e9c55e | 2018-11-29 12:00:39 +0000 | [diff] [blame] | 107 | // Generic key value storage which can be referenced by other tables. |
| 108 | class Args { |
| 109 | public: |
Lalit Maganti | 1d915a6 | 2019-01-07 12:10:42 +0000 | [diff] [blame^] | 110 | // Variadic type representing the possible values for the args table. |
| 111 | struct Variadic { |
Lalit Maganti | 6e9c55e | 2018-11-29 12:00:39 +0000 | [diff] [blame] | 112 | enum Type { kInt, kString, kReal }; |
| 113 | |
Lalit Maganti | 1d915a6 | 2019-01-07 12:10:42 +0000 | [diff] [blame^] | 114 | static Variadic Integer(int64_t int_value) { |
| 115 | Variadic variadic; |
| 116 | variadic.type = Type::kInt; |
| 117 | variadic.int_value = int_value; |
| 118 | return variadic; |
| 119 | } |
| 120 | |
| 121 | static Variadic String(StringId string_id) { |
| 122 | Variadic variadic; |
| 123 | variadic.type = Type::kString; |
| 124 | variadic.string_value = string_id; |
| 125 | return variadic; |
| 126 | } |
| 127 | |
| 128 | static Variadic Real(double real_value) { |
| 129 | Variadic variadic; |
| 130 | variadic.type = Type::kReal; |
| 131 | variadic.real_value = real_value; |
| 132 | return variadic; |
| 133 | } |
Lalit Maganti | 6e9c55e | 2018-11-29 12:00:39 +0000 | [diff] [blame] | 134 | |
| 135 | Type type; |
| 136 | union { |
| 137 | int64_t int_value; |
| 138 | StringId string_value; |
| 139 | double real_value; |
| 140 | }; |
| 141 | }; |
| 142 | |
Lalit Maganti | 5ea9e93 | 2018-11-30 14:19:39 +0000 | [diff] [blame] | 143 | const std::deque<RowId>& ids() const { return ids_; } |
| 144 | const std::deque<StringId>& flat_keys() const { return flat_keys_; } |
| 145 | const std::deque<StringId>& keys() const { return keys_; } |
Lalit Maganti | 1d915a6 | 2019-01-07 12:10:42 +0000 | [diff] [blame^] | 146 | const std::deque<Variadic>& arg_values() const { return arg_values_; } |
Lalit Maganti | 79472be | 2018-12-04 13:41:27 +0000 | [diff] [blame] | 147 | const std::multimap<RowId, uint32_t>& args_for_id() const { |
| 148 | return args_for_id_; |
| 149 | } |
Lalit Maganti | 6e9c55e | 2018-11-29 12:00:39 +0000 | [diff] [blame] | 150 | size_t args_count() const { return ids_.size(); } |
| 151 | |
Lalit Maganti | 1d915a6 | 2019-01-07 12:10:42 +0000 | [diff] [blame^] | 152 | void AddArg(RowId id, StringId flat_key, StringId key, Variadic value) { |
Lalit Maganti | 5ea9e93 | 2018-11-30 14:19:39 +0000 | [diff] [blame] | 153 | if (id == kInvalidRowId) |
| 154 | return; |
| 155 | |
| 156 | ids_.emplace_back(id); |
| 157 | flat_keys_.emplace_back(flat_key); |
| 158 | keys_.emplace_back(key); |
| 159 | arg_values_.emplace_back(value); |
Lalit Maganti | 79472be | 2018-12-04 13:41:27 +0000 | [diff] [blame] | 160 | args_for_id_.emplace(id, static_cast<uint32_t>(args_count() - 1)); |
Lalit Maganti | 6e9c55e | 2018-11-29 12:00:39 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Lalit Maganti | 5ea9e93 | 2018-11-30 14:19:39 +0000 | [diff] [blame] | 163 | private: |
| 164 | std::deque<RowId> ids_; |
| 165 | std::deque<StringId> flat_keys_; |
| 166 | std::deque<StringId> keys_; |
Lalit Maganti | 1d915a6 | 2019-01-07 12:10:42 +0000 | [diff] [blame^] | 167 | std::deque<Variadic> arg_values_; |
Lalit Maganti | 79472be | 2018-12-04 13:41:27 +0000 | [diff] [blame] | 168 | std::multimap<RowId, uint32_t> args_for_id_; |
Lalit Maganti | 6e9c55e | 2018-11-29 12:00:39 +0000 | [diff] [blame] | 169 | }; |
| 170 | |
Lalit Maganti | ff69c11 | 2018-09-24 12:07:47 +0100 | [diff] [blame] | 171 | class Slices { |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 172 | public: |
Lalit Maganti | fde2904 | 2018-10-04 13:28:52 +0100 | [diff] [blame] | 173 | inline size_t AddSlice(uint32_t cpu, |
Lalit Maganti | 85ca4a8 | 2018-12-07 17:28:02 +0000 | [diff] [blame] | 174 | int64_t start_ns, |
| 175 | int64_t duration_ns, |
Lalit Maganti | fde2904 | 2018-10-04 13:28:52 +0100 | [diff] [blame] | 176 | UniqueTid utid) { |
Lalit Maganti | ff69c11 | 2018-09-24 12:07:47 +0100 | [diff] [blame] | 177 | cpus_.emplace_back(cpu); |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 178 | start_ns_.emplace_back(start_ns); |
| 179 | durations_.emplace_back(duration_ns); |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 180 | utids_.emplace_back(utid); |
Lalit Maganti | fde2904 | 2018-10-04 13:28:52 +0100 | [diff] [blame] | 181 | return slice_count() - 1; |
| 182 | } |
| 183 | |
Lalit Maganti | 85ca4a8 | 2018-12-07 17:28:02 +0000 | [diff] [blame] | 184 | void set_duration(size_t index, int64_t duration_ns) { |
Lalit Maganti | fde2904 | 2018-10-04 13:28:52 +0100 | [diff] [blame] | 185 | durations_[index] = duration_ns; |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 186 | } |
| 187 | |
Isabelle Taylor | 47328cf | 2018-06-12 14:33:59 +0100 | [diff] [blame] | 188 | size_t slice_count() const { return start_ns_.size(); } |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 189 | |
Lalit Maganti | ff69c11 | 2018-09-24 12:07:47 +0100 | [diff] [blame] | 190 | const std::deque<uint32_t>& cpus() const { return cpus_; } |
| 191 | |
Lalit Maganti | 85ca4a8 | 2018-12-07 17:28:02 +0000 | [diff] [blame] | 192 | const std::deque<int64_t>& start_ns() const { return start_ns_; } |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 193 | |
Lalit Maganti | 85ca4a8 | 2018-12-07 17:28:02 +0000 | [diff] [blame] | 194 | const std::deque<int64_t>& durations() const { return durations_; } |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 195 | |
Isabelle Taylor | 68e4219 | 2018-06-19 16:19:31 +0100 | [diff] [blame] | 196 | const std::deque<UniqueTid>& utids() const { return utids_; } |
| 197 | |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 198 | private: |
Hector Dearman | 947f12a | 2018-09-11 16:50:36 +0100 | [diff] [blame] | 199 | // 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] | 200 | // in the trace for the CPU). |
Lalit Maganti | ff69c11 | 2018-09-24 12:07:47 +0100 | [diff] [blame] | 201 | std::deque<uint32_t> cpus_; |
Lalit Maganti | 85ca4a8 | 2018-12-07 17:28:02 +0000 | [diff] [blame] | 202 | std::deque<int64_t> start_ns_; |
| 203 | std::deque<int64_t> durations_; |
Isabelle Taylor | 68e4219 | 2018-06-19 16:19:31 +0100 | [diff] [blame] | 204 | std::deque<UniqueTid> utids_; |
Primiano Tucci | 0d72a31 | 2018-08-07 14:42:45 +0100 | [diff] [blame] | 205 | }; |
Isabelle Taylor | 68e4219 | 2018-06-19 16:19:31 +0100 | [diff] [blame] | 206 | |
Primiano Tucci | 0d72a31 | 2018-08-07 14:42:45 +0100 | [diff] [blame] | 207 | class NestableSlices { |
| 208 | public: |
Lalit Maganti | 85ca4a8 | 2018-12-07 17:28:02 +0000 | [diff] [blame] | 209 | inline size_t AddSlice(int64_t start_ns, |
| 210 | int64_t duration_ns, |
Lalit Maganti | 7b37bbf | 2018-11-09 15:58:54 +0000 | [diff] [blame] | 211 | UniqueTid utid, |
| 212 | StringId cat, |
| 213 | StringId name, |
| 214 | uint8_t depth, |
Lalit Maganti | 85ca4a8 | 2018-12-07 17:28:02 +0000 | [diff] [blame] | 215 | int64_t stack_id, |
| 216 | int64_t parent_stack_id) { |
Primiano Tucci | 0d72a31 | 2018-08-07 14:42:45 +0100 | [diff] [blame] | 217 | start_ns_.emplace_back(start_ns); |
| 218 | durations_.emplace_back(duration_ns); |
| 219 | utids_.emplace_back(utid); |
| 220 | cats_.emplace_back(cat); |
| 221 | names_.emplace_back(name); |
| 222 | depths_.emplace_back(depth); |
Primiano Tucci | b75dcee | 2018-08-08 12:21:36 +0100 | [diff] [blame] | 223 | stack_ids_.emplace_back(stack_id); |
| 224 | parent_stack_ids_.emplace_back(parent_stack_id); |
Lalit Maganti | 7b37bbf | 2018-11-09 15:58:54 +0000 | [diff] [blame] | 225 | return slice_count() - 1; |
| 226 | } |
| 227 | |
Lalit Maganti | 85ca4a8 | 2018-12-07 17:28:02 +0000 | [diff] [blame] | 228 | void set_duration(size_t index, int64_t duration_ns) { |
Lalit Maganti | 7b37bbf | 2018-11-09 15:58:54 +0000 | [diff] [blame] | 229 | durations_[index] = duration_ns; |
| 230 | } |
| 231 | |
Lalit Maganti | 85ca4a8 | 2018-12-07 17:28:02 +0000 | [diff] [blame] | 232 | void set_stack_id(size_t index, int64_t stack_id) { |
Lalit Maganti | 7b37bbf | 2018-11-09 15:58:54 +0000 | [diff] [blame] | 233 | stack_ids_[index] = stack_id; |
Primiano Tucci | 0d72a31 | 2018-08-07 14:42:45 +0100 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | size_t slice_count() const { return start_ns_.size(); } |
Lalit Maganti | 85ca4a8 | 2018-12-07 17:28:02 +0000 | [diff] [blame] | 237 | const std::deque<int64_t>& start_ns() const { return start_ns_; } |
| 238 | const std::deque<int64_t>& durations() const { return durations_; } |
Primiano Tucci | 0d72a31 | 2018-08-07 14:42:45 +0100 | [diff] [blame] | 239 | const std::deque<UniqueTid>& utids() const { return utids_; } |
| 240 | const std::deque<StringId>& cats() const { return cats_; } |
| 241 | const std::deque<StringId>& names() const { return names_; } |
| 242 | const std::deque<uint8_t>& depths() const { return depths_; } |
Lalit Maganti | 85ca4a8 | 2018-12-07 17:28:02 +0000 | [diff] [blame] | 243 | const std::deque<int64_t>& stack_ids() const { return stack_ids_; } |
| 244 | const std::deque<int64_t>& parent_stack_ids() const { |
Primiano Tucci | b75dcee | 2018-08-08 12:21:36 +0100 | [diff] [blame] | 245 | return parent_stack_ids_; |
| 246 | } |
Primiano Tucci | 0d72a31 | 2018-08-07 14:42:45 +0100 | [diff] [blame] | 247 | |
| 248 | private: |
Lalit Maganti | 85ca4a8 | 2018-12-07 17:28:02 +0000 | [diff] [blame] | 249 | std::deque<int64_t> start_ns_; |
| 250 | std::deque<int64_t> durations_; |
Primiano Tucci | 0d72a31 | 2018-08-07 14:42:45 +0100 | [diff] [blame] | 251 | std::deque<UniqueTid> utids_; |
| 252 | std::deque<StringId> cats_; |
| 253 | std::deque<StringId> names_; |
| 254 | std::deque<uint8_t> depths_; |
Lalit Maganti | 85ca4a8 | 2018-12-07 17:28:02 +0000 | [diff] [blame] | 255 | std::deque<int64_t> stack_ids_; |
| 256 | std::deque<int64_t> parent_stack_ids_; |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 257 | }; |
| 258 | |
Isabelle Taylor | 15314ea | 2018-09-19 11:35:19 +0100 | [diff] [blame] | 259 | class Counters { |
| 260 | public: |
Lalit Maganti | 85ca4a8 | 2018-12-07 17:28:02 +0000 | [diff] [blame] | 261 | inline size_t AddCounter(int64_t timestamp, |
| 262 | int64_t duration, |
Lalit Maganti | fde2904 | 2018-10-04 13:28:52 +0100 | [diff] [blame] | 263 | StringId name_id, |
| 264 | double value, |
Lalit Maganti | fde2904 | 2018-10-04 13:28:52 +0100 | [diff] [blame] | 265 | int64_t ref, |
| 266 | RefType type) { |
Isabelle Taylor | 15314ea | 2018-09-19 11:35:19 +0100 | [diff] [blame] | 267 | timestamps_.emplace_back(timestamp); |
| 268 | durations_.emplace_back(duration); |
| 269 | name_ids_.emplace_back(name_id); |
| 270 | values_.emplace_back(value); |
| 271 | refs_.emplace_back(ref); |
| 272 | types_.emplace_back(type); |
Lalit Maganti | fde2904 | 2018-10-04 13:28:52 +0100 | [diff] [blame] | 273 | return counter_count() - 1; |
Isabelle Taylor | 15314ea | 2018-09-19 11:35:19 +0100 | [diff] [blame] | 274 | } |
Lalit Maganti | fde2904 | 2018-10-04 13:28:52 +0100 | [diff] [blame] | 275 | |
Lalit Maganti | 85ca4a8 | 2018-12-07 17:28:02 +0000 | [diff] [blame] | 276 | void set_duration(size_t index, int64_t duration) { |
Lalit Maganti | fde2904 | 2018-10-04 13:28:52 +0100 | [diff] [blame] | 277 | durations_[index] = duration; |
| 278 | } |
| 279 | |
Isabelle Taylor | 15314ea | 2018-09-19 11:35:19 +0100 | [diff] [blame] | 280 | size_t counter_count() const { return timestamps_.size(); } |
| 281 | |
Lalit Maganti | 85ca4a8 | 2018-12-07 17:28:02 +0000 | [diff] [blame] | 282 | const std::deque<int64_t>& timestamps() const { return timestamps_; } |
Isabelle Taylor | 15314ea | 2018-09-19 11:35:19 +0100 | [diff] [blame] | 283 | |
Lalit Maganti | 85ca4a8 | 2018-12-07 17:28:02 +0000 | [diff] [blame] | 284 | const std::deque<int64_t>& durations() const { return durations_; } |
Isabelle Taylor | 15314ea | 2018-09-19 11:35:19 +0100 | [diff] [blame] | 285 | |
| 286 | const std::deque<StringId>& name_ids() const { return name_ids_; } |
| 287 | |
| 288 | const std::deque<double>& values() const { return values_; } |
| 289 | |
| 290 | const std::deque<int64_t>& refs() const { return refs_; } |
| 291 | |
| 292 | const std::deque<RefType>& types() const { return types_; } |
| 293 | |
| 294 | private: |
Lalit Maganti | 85ca4a8 | 2018-12-07 17:28:02 +0000 | [diff] [blame] | 295 | std::deque<int64_t> timestamps_; |
| 296 | std::deque<int64_t> durations_; |
Isabelle Taylor | 15314ea | 2018-09-19 11:35:19 +0100 | [diff] [blame] | 297 | std::deque<StringId> name_ids_; |
| 298 | std::deque<double> values_; |
| 299 | std::deque<int64_t> refs_; |
| 300 | std::deque<RefType> types_; |
| 301 | }; |
| 302 | |
Primiano Tucci | 5cb84f8 | 2018-10-31 21:46:36 -0700 | [diff] [blame] | 303 | class SqlStats { |
| 304 | public: |
| 305 | static constexpr size_t kMaxLogEntries = 100; |
| 306 | void RecordQueryBegin(const std::string& query, |
Lalit Maganti | 85ca4a8 | 2018-12-07 17:28:02 +0000 | [diff] [blame] | 307 | int64_t time_queued, |
| 308 | int64_t time_started); |
| 309 | void RecordQueryEnd(int64_t time_ended); |
Primiano Tucci | 5cb84f8 | 2018-10-31 21:46:36 -0700 | [diff] [blame] | 310 | size_t size() const { return queries_.size(); } |
| 311 | const std::deque<std::string>& queries() const { return queries_; } |
Lalit Maganti | 85ca4a8 | 2018-12-07 17:28:02 +0000 | [diff] [blame] | 312 | const std::deque<int64_t>& times_queued() const { return times_queued_; } |
| 313 | const std::deque<int64_t>& times_started() const { return times_started_; } |
| 314 | const std::deque<int64_t>& times_ended() const { return times_ended_; } |
Primiano Tucci | 5cb84f8 | 2018-10-31 21:46:36 -0700 | [diff] [blame] | 315 | |
| 316 | private: |
| 317 | std::deque<std::string> queries_; |
Lalit Maganti | 85ca4a8 | 2018-12-07 17:28:02 +0000 | [diff] [blame] | 318 | std::deque<int64_t> times_queued_; |
| 319 | std::deque<int64_t> times_started_; |
| 320 | std::deque<int64_t> times_ended_; |
Primiano Tucci | 5cb84f8 | 2018-10-31 21:46:36 -0700 | [diff] [blame] | 321 | }; |
| 322 | |
Isabelle Taylor | c8c1120 | 2018-11-05 11:36:22 +0000 | [diff] [blame] | 323 | class Instants { |
| 324 | public: |
Lalit Maganti | 85ca4a8 | 2018-12-07 17:28:02 +0000 | [diff] [blame] | 325 | inline size_t AddInstantEvent(int64_t timestamp, |
Isabelle Taylor | c8c1120 | 2018-11-05 11:36:22 +0000 | [diff] [blame] | 326 | StringId name_id, |
| 327 | double value, |
| 328 | int64_t ref, |
| 329 | RefType type) { |
| 330 | timestamps_.emplace_back(timestamp); |
| 331 | name_ids_.emplace_back(name_id); |
| 332 | values_.emplace_back(value); |
| 333 | refs_.emplace_back(ref); |
| 334 | types_.emplace_back(type); |
| 335 | return instant_count() - 1; |
| 336 | } |
| 337 | |
| 338 | size_t instant_count() const { return timestamps_.size(); } |
| 339 | |
Lalit Maganti | 85ca4a8 | 2018-12-07 17:28:02 +0000 | [diff] [blame] | 340 | const std::deque<int64_t>& timestamps() const { return timestamps_; } |
Isabelle Taylor | c8c1120 | 2018-11-05 11:36:22 +0000 | [diff] [blame] | 341 | |
| 342 | const std::deque<StringId>& name_ids() const { return name_ids_; } |
| 343 | |
| 344 | const std::deque<double>& values() const { return values_; } |
| 345 | |
| 346 | const std::deque<int64_t>& refs() const { return refs_; } |
| 347 | |
| 348 | const std::deque<RefType>& types() const { return types_; } |
| 349 | |
| 350 | private: |
Lalit Maganti | 85ca4a8 | 2018-12-07 17:28:02 +0000 | [diff] [blame] | 351 | std::deque<int64_t> timestamps_; |
Isabelle Taylor | c8c1120 | 2018-11-05 11:36:22 +0000 | [diff] [blame] | 352 | std::deque<StringId> name_ids_; |
| 353 | std::deque<double> values_; |
| 354 | std::deque<int64_t> refs_; |
| 355 | std::deque<RefType> types_; |
| 356 | }; |
| 357 | |
Lalit Maganti | 1d915a6 | 2019-01-07 12:10:42 +0000 | [diff] [blame^] | 358 | class RawEvents { |
| 359 | public: |
| 360 | inline RowId AddRawEvent(int64_t timestamp, |
| 361 | StringId name_id, |
| 362 | UniqueTid utid) { |
| 363 | timestamps_.emplace_back(timestamp); |
| 364 | name_ids_.emplace_back(name_id); |
| 365 | utids_.emplace_back(utid); |
| 366 | return CreateRowId(TableId::kRawEvents, |
| 367 | static_cast<uint32_t>(raw_event_count() - 1)); |
| 368 | } |
| 369 | |
| 370 | size_t raw_event_count() const { return timestamps_.size(); } |
| 371 | |
| 372 | const std::deque<int64_t>& timestamps() const { return timestamps_; } |
| 373 | |
| 374 | const std::deque<StringId>& name_ids() const { return name_ids_; } |
| 375 | |
| 376 | const std::deque<UniqueTid>& utids() const { return utids_; } |
| 377 | |
| 378 | private: |
| 379 | std::deque<int64_t> timestamps_; |
| 380 | std::deque<StringId> name_ids_; |
| 381 | std::deque<UniqueTid> utids_; |
| 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 | 5ea9e93 | 2018-11-30 14:19:39 +0000 | [diff] [blame] | 428 | static RowId CreateRowId(TableId table, uint32_t row) { |
| 429 | static constexpr uint8_t kRowIdTableShift = 32; |
Lalit Maganti | 85ca4a8 | 2018-12-07 17:28:02 +0000 | [diff] [blame] | 430 | return (static_cast<RowId>(table) << kRowIdTableShift) | row; |
Lalit Maganti | 5ea9e93 | 2018-11-30 14:19:39 +0000 | [diff] [blame] | 431 | } |
| 432 | |
Lalit Maganti | ff69c11 | 2018-09-24 12:07:47 +0100 | [diff] [blame] | 433 | const Slices& slices() const { return slices_; } |
Lalit Maganti | fde2904 | 2018-10-04 13:28:52 +0100 | [diff] [blame] | 434 | Slices* mutable_slices() { return &slices_; } |
| 435 | |
Primiano Tucci | 0d72a31 | 2018-08-07 14:42:45 +0100 | [diff] [blame] | 436 | const NestableSlices& nestable_slices() const { return nestable_slices_; } |
| 437 | NestableSlices* mutable_nestable_slices() { return &nestable_slices_; } |
| 438 | |
Isabelle Taylor | 15314ea | 2018-09-19 11:35:19 +0100 | [diff] [blame] | 439 | const Counters& counters() const { return counters_; } |
| 440 | Counters* mutable_counters() { return &counters_; } |
Isabelle Taylor | 14674d4 | 2018-09-07 11:33:11 +0100 | [diff] [blame] | 441 | |
Primiano Tucci | 5cb84f8 | 2018-10-31 21:46:36 -0700 | [diff] [blame] | 442 | const SqlStats& sql_stats() const { return sql_stats_; } |
| 443 | SqlStats* mutable_sql_stats() { return &sql_stats_; } |
| 444 | |
Isabelle Taylor | c8c1120 | 2018-11-05 11:36:22 +0000 | [diff] [blame] | 445 | const Instants& instants() const { return instants_; } |
| 446 | Instants* mutable_instants() { return &instants_; } |
| 447 | |
Lalit Maganti | 05e8c13 | 2018-11-09 18:16:12 +0000 | [diff] [blame] | 448 | const Stats& stats() const { return stats_; } |
| 449 | Stats* mutable_stats() { return &stats_; } |
| 450 | |
Lalit Maganti | 6e9c55e | 2018-11-29 12:00:39 +0000 | [diff] [blame] | 451 | const Args& args() const { return args_; } |
| 452 | Args* mutable_args() { return &args_; } |
| 453 | |
Lalit Maganti | 1d915a6 | 2019-01-07 12:10:42 +0000 | [diff] [blame^] | 454 | const RawEvents& raw_events() const { return raw_events_; } |
| 455 | RawEvents* mutable_raw_events() { return &raw_events_; } |
| 456 | |
Lalit Maganti | acda68b | 2018-10-29 15:23:25 +0000 | [diff] [blame] | 457 | const std::deque<std::string>& string_pool() const { return string_pool_; } |
| 458 | |
Isabelle Taylor | e7003fb | 2018-07-17 11:39:01 +0100 | [diff] [blame] | 459 | // |unique_processes_| always contains at least 1 element becuase the 0th ID |
| 460 | // is reserved to indicate an invalid process. |
| 461 | size_t process_count() const { return unique_processes_.size() - 1; } |
Primiano Tucci | 0d72a31 | 2018-08-07 14:42:45 +0100 | [diff] [blame] | 462 | |
Isabelle Taylor | e7003fb | 2018-07-17 11:39:01 +0100 | [diff] [blame] | 463 | // |unique_threads_| always contains at least 1 element becuase the 0th ID |
| 464 | // is reserved to indicate an invalid thread. |
| 465 | size_t thread_count() const { return unique_threads_.size() - 1; } |
| 466 | |
Hector Dearman | 1232336 | 2018-08-09 16:09:28 +0100 | [diff] [blame] | 467 | // Number of interned strings in the pool. Includes the empty string w/ ID=0. |
| 468 | size_t string_count() const { return string_pool_.size(); } |
| 469 | |
Lalit Maganti | caed37e | 2018-06-01 03:03:08 +0100 | [diff] [blame] | 470 | private: |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 471 | TraceStorage& operator=(const TraceStorage&) = default; |
| 472 | |
Primiano Tucci | 2da5d2e | 2018-08-10 14:23:31 +0100 | [diff] [blame] | 473 | using StringHash = uint64_t; |
Lalit Maganti | caed37e | 2018-06-01 03:03:08 +0100 | [diff] [blame] | 474 | |
Lalit Maganti | 05e8c13 | 2018-11-09 18:16:12 +0000 | [diff] [blame] | 475 | // Stats about parsing the trace. |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 476 | Stats stats_; |
| 477 | |
Lalit Maganti | caed37e | 2018-06-01 03:03:08 +0100 | [diff] [blame] | 478 | // One entry for each CPU in the trace. |
Lalit Maganti | ff69c11 | 2018-09-24 12:07:47 +0100 | [diff] [blame] | 479 | Slices slices_; |
Lalit Maganti | caed37e | 2018-06-01 03:03:08 +0100 | [diff] [blame] | 480 | |
Lalit Maganti | 6e9c55e | 2018-11-29 12:00:39 +0000 | [diff] [blame] | 481 | // Args for all other tables. |
| 482 | Args args_; |
| 483 | |
Lalit Maganti | caed37e | 2018-06-01 03:03:08 +0100 | [diff] [blame] | 484 | // One entry for each unique string in the trace. |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 485 | std::deque<std::string> string_pool_; |
Lalit Maganti | caed37e | 2018-06-01 03:03:08 +0100 | [diff] [blame] | 486 | |
| 487 | // One entry for each unique string in the trace. |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 488 | std::unordered_map<StringHash, StringId> string_index_; |
Isabelle Taylor | 47328cf | 2018-06-12 14:33:59 +0100 | [diff] [blame] | 489 | |
Isabelle Taylor | 47328cf | 2018-06-12 14:33:59 +0100 | [diff] [blame] | 490 | // One entry for each UniquePid, with UniquePid as the index. |
Isabelle Taylor | 3dd366c | 2018-06-22 16:21:41 +0100 | [diff] [blame] | 491 | std::deque<Process> unique_processes_; |
Isabelle Taylor | 68e4219 | 2018-06-19 16:19:31 +0100 | [diff] [blame] | 492 | |
Isabelle Taylor | 68e4219 | 2018-06-19 16:19:31 +0100 | [diff] [blame] | 493 | // One entry for each UniqueTid, with UniqueTid as the index. |
Isabelle Taylor | 3dd366c | 2018-06-22 16:21:41 +0100 | [diff] [blame] | 494 | std::deque<Thread> unique_threads_; |
Primiano Tucci | 0d72a31 | 2018-08-07 14:42:45 +0100 | [diff] [blame] | 495 | |
| 496 | // Slices coming from userspace events (e.g. Chromium TRACE_EVENT macros). |
| 497 | NestableSlices nestable_slices_; |
Isabelle Taylor | 15314ea | 2018-09-19 11:35:19 +0100 | [diff] [blame] | 498 | |
| 499 | // Counter events from the trace. This includes CPU frequency events as well |
| 500 | // systrace trace_marker counter events. |
| 501 | Counters counters_; |
Primiano Tucci | 5cb84f8 | 2018-10-31 21:46:36 -0700 | [diff] [blame] | 502 | |
| 503 | SqlStats sql_stats_; |
Lalit Maganti | 6e9c55e | 2018-11-29 12:00:39 +0000 | [diff] [blame] | 504 | |
Isabelle Taylor | c8c1120 | 2018-11-05 11:36:22 +0000 | [diff] [blame] | 505 | // These are instantaneous events in the trace. They have no duration |
| 506 | // and do not have a value that make sense to track over time. |
| 507 | // e.g. signal events |
| 508 | Instants instants_; |
Lalit Maganti | 1d915a6 | 2019-01-07 12:10:42 +0000 | [diff] [blame^] | 509 | |
| 510 | // Raw events are every ftrace event in the trace. The raw event includes |
| 511 | // the timestamp and the pid. The args for the raw event will be in the |
| 512 | // args table. This table can be used to generate a text version of the |
| 513 | // trace. |
| 514 | RawEvents raw_events_; |
Lalit Maganti | caed37e | 2018-06-01 03:03:08 +0100 | [diff] [blame] | 515 | }; |
| 516 | |
| 517 | } // namespace trace_processor |
| 518 | } // namespace perfetto |
| 519 | |
| 520 | #endif // SRC_TRACE_PROCESSOR_TRACE_STORAGE_H_ |