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