Ioannis Ilkos | eff38f5 | 2018-10-29 10:37:55 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | #include "src/trace_processor/trace_processor_impl.h" |
| 18 | |
Ioannis Ilkos | b8b1110 | 2019-01-29 17:56:55 +0000 | [diff] [blame] | 19 | #include <inttypes.h> |
Hector Dearman | c0a2909 | 2018-11-08 17:34:23 +0000 | [diff] [blame] | 20 | #include <algorithm> |
Ioannis Ilkos | eff38f5 | 2018-10-29 10:37:55 +0000 | [diff] [blame] | 21 | |
Lalit Maganti | 7709501 | 2019-02-19 13:13:59 +0000 | [diff] [blame] | 22 | #include "perfetto/base/logging.h" |
Eric Seckler | 83dcc8c | 2019-08-21 12:18:43 +0100 | [diff] [blame] | 23 | #include "perfetto/base/time.h" |
Primiano Tucci | 2c5488f | 2019-06-01 03:27:28 +0100 | [diff] [blame] | 24 | #include "perfetto/ext/base/string_splitter.h" |
| 25 | #include "perfetto/ext/base/string_utils.h" |
Eric Seckler | f2de4db | 2020-02-14 14:17:20 +0000 | [diff] [blame] | 26 | #include "src/trace_processor/additional_modules.h" |
Lalit Maganti | 920f4f9 | 2020-02-19 13:45:05 +0000 | [diff] [blame] | 27 | #include "src/trace_processor/experimental_counter_dur_generator.h" |
| 28 | #include "src/trace_processor/experimental_flamegraph_generator.h" |
Mikhail Khokhlov | 81491b9 | 2019-12-19 13:58:22 +0000 | [diff] [blame] | 29 | #include "src/trace_processor/importers/ftrace/sched_event_tracker.h" |
Lalit Maganti | 6d1f7b5 | 2020-02-27 13:16:44 +0000 | [diff] [blame] | 30 | #include "src/trace_processor/importers/fuchsia/fuchsia_trace_parser.h" |
| 31 | #include "src/trace_processor/importers/fuchsia/fuchsia_trace_tokenizer.h" |
Lalit Maganti | 9d538bd | 2020-03-12 23:48:16 +0000 | [diff] [blame^] | 32 | #include "src/trace_processor/importers/gzip/gzip_trace_parser.h" |
Lalit Maganti | 6d1f7b5 | 2020-02-27 13:16:44 +0000 | [diff] [blame] | 33 | #include "src/trace_processor/importers/json/json_trace_parser.h" |
| 34 | #include "src/trace_processor/importers/json/json_trace_tokenizer.h" |
| 35 | #include "src/trace_processor/importers/systrace/systrace_trace_parser.h" |
Hector Dearman | 93b0b09 | 2020-01-09 14:35:04 +0000 | [diff] [blame] | 36 | #include "src/trace_processor/metadata_tracker.h" |
Primiano Tucci | 5cb84f8 | 2018-10-31 21:46:36 -0700 | [diff] [blame] | 37 | #include "src/trace_processor/sql_stats_table.h" |
Lalit Maganti | 6073245 | 2020-02-04 16:02:53 +0000 | [diff] [blame] | 38 | #include "src/trace_processor/sqlite/span_join_operator_table.h" |
Lalit Maganti | 03d1062 | 2019-08-27 14:47:39 +0100 | [diff] [blame] | 39 | #include "src/trace_processor/sqlite/sqlite3_str_split.h" |
Lalit Maganti | 737b376 | 2019-08-26 13:46:37 -0700 | [diff] [blame] | 40 | #include "src/trace_processor/sqlite/sqlite_table.h" |
Lalit Maganti | 1b620a6 | 2020-01-30 18:24:53 +0000 | [diff] [blame] | 41 | #include "src/trace_processor/sqlite/sqlite_utils.h" |
Lalit Maganti | 6073245 | 2020-02-04 16:02:53 +0000 | [diff] [blame] | 42 | #include "src/trace_processor/sqlite/window_operator_table.h" |
Lalit Maganti | 679b29d | 2020-01-13 13:26:28 +0000 | [diff] [blame] | 43 | #include "src/trace_processor/sqlite_raw_table.h" |
Lalit Maganti | 05e8c13 | 2018-11-09 18:16:12 +0000 | [diff] [blame] | 44 | #include "src/trace_processor/stats_table.h" |
Lalit Maganti | 83b53fa | 2020-01-13 12:55:36 +0000 | [diff] [blame] | 45 | #include "src/trace_processor/types/variadic.h" |
Ioannis Ilkos | eff38f5 | 2018-10-29 10:37:55 +0000 | [diff] [blame] | 46 | |
Eric Seckler | c622b8b | 2019-10-21 09:54:00 +0100 | [diff] [blame] | 47 | #include "src/trace_processor/metrics/metrics.descriptor.h" |
| 48 | #include "src/trace_processor/metrics/metrics.h" |
| 49 | #include "src/trace_processor/metrics/sql_metrics.h" |
Lalit Maganti | 26f69bd | 2019-04-29 18:23:47 +0100 | [diff] [blame] | 50 | |
Primiano Tucci | 02c1176 | 2019-08-30 00:57:59 +0200 | [diff] [blame] | 51 | #if PERFETTO_BUILDFLAG(PERFETTO_TP_JSON) |
Mikhail Khokhlov | a8d310d | 2019-05-07 17:34:21 +0100 | [diff] [blame] | 52 | #include "src/trace_processor/export_json.h" |
Lalit Maganti | 17aa273 | 2019-02-08 15:47:26 +0000 | [diff] [blame] | 53 | #endif |
| 54 | |
Primiano Tucci | a1a26de | 2019-11-12 15:45:19 -0800 | [diff] [blame] | 55 | #if !PERFETTO_BUILDFLAG(PERFETTO_OS_WIN) |
| 56 | #include <cxxabi.h> |
| 57 | #endif |
| 58 | |
Mikhail Khokhlov | 8643d1c | 2019-06-04 12:02:47 +0100 | [diff] [blame] | 59 | // In Android and Chromium tree builds, we don't have the percentile module. |
Lalit Maganti | 17aa273 | 2019-02-08 15:47:26 +0000 | [diff] [blame] | 60 | // Just don't include it. |
Primiano Tucci | 02c1176 | 2019-08-30 00:57:59 +0200 | [diff] [blame] | 61 | #if PERFETTO_BUILDFLAG(PERFETTO_TP_PERCENTILE) |
Ioannis Ilkos | 178535e | 2018-11-05 17:32:45 +0000 | [diff] [blame] | 62 | // defined in sqlite_src/ext/misc/percentile.c |
| 63 | extern "C" int sqlite3_percentile_init(sqlite3* db, |
| 64 | char** error, |
| 65 | const sqlite3_api_routines* api); |
Eric Seckler | a7870e6 | 2019-11-01 10:11:58 +0000 | [diff] [blame] | 66 | #endif // PERFETTO_BUILDFLAG(PERFETTO_TP_PERCENTILE) |
Ioannis Ilkos | 178535e | 2018-11-05 17:32:45 +0000 | [diff] [blame] | 67 | |
Ioannis Ilkos | bf54ebb | 2019-03-08 13:26:14 +0000 | [diff] [blame] | 68 | namespace perfetto { |
| 69 | namespace trace_processor { |
Ioannis Ilkos | 178535e | 2018-11-05 17:32:45 +0000 | [diff] [blame] | 70 | namespace { |
Ioannis Ilkos | bf54ebb | 2019-03-08 13:26:14 +0000 | [diff] [blame] | 71 | |
Primiano Tucci | 11a79e7 | 2019-10-29 22:13:10 +0100 | [diff] [blame] | 72 | const char kAllTablesQuery[] = |
| 73 | "SELECT tbl_name, type FROM (SELECT * FROM sqlite_master UNION ALL SELECT " |
| 74 | "* FROM sqlite_temp_master)"; |
| 75 | |
Ioannis Ilkos | 7199d55 | 2019-03-13 18:35:31 +0000 | [diff] [blame] | 76 | void InitializeSqlite(sqlite3* db) { |
| 77 | char* error = nullptr; |
| 78 | sqlite3_exec(db, "PRAGMA temp_store=2", 0, 0, &error); |
| 79 | if (error) { |
| 80 | PERFETTO_FATAL("Error setting pragma temp_store: %s", error); |
| 81 | } |
Ioannis Ilkos | bf54ebb | 2019-03-08 13:26:14 +0000 | [diff] [blame] | 82 | sqlite3_str_split_init(db); |
Lalit Maganti | 17aa273 | 2019-02-08 15:47:26 +0000 | [diff] [blame] | 83 | // In Android tree builds, we don't have the percentile module. |
| 84 | // Just don't include it. |
Primiano Tucci | 02c1176 | 2019-08-30 00:57:59 +0200 | [diff] [blame] | 85 | #if PERFETTO_BUILDFLAG(PERFETTO_TP_PERCENTILE) |
Ioannis Ilkos | 178535e | 2018-11-05 17:32:45 +0000 | [diff] [blame] | 86 | sqlite3_percentile_init(db, &error, nullptr); |
Sami Kyostila | 3366894 | 2018-11-13 16:33:32 +0000 | [diff] [blame] | 87 | if (error) { |
Ioannis Ilkos | 178535e | 2018-11-05 17:32:45 +0000 | [diff] [blame] | 88 | PERFETTO_ELOG("Error initializing: %s", error); |
Sami Kyostila | 3366894 | 2018-11-13 16:33:32 +0000 | [diff] [blame] | 89 | sqlite3_free(error); |
| 90 | } |
Lalit Maganti | 17aa273 | 2019-02-08 15:47:26 +0000 | [diff] [blame] | 91 | #endif |
Sami Kyostila | 3366894 | 2018-11-13 16:33:32 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Lalit Maganti | 086295b | 2019-04-04 16:59:19 +0100 | [diff] [blame] | 94 | void BuildBoundsTable(sqlite3* db, std::pair<int64_t, int64_t> bounds) { |
| 95 | char* error = nullptr; |
| 96 | sqlite3_exec(db, "DELETE FROM trace_bounds", nullptr, nullptr, &error); |
| 97 | if (error) { |
| 98 | PERFETTO_ELOG("Error deleting from bounds table: %s", error); |
| 99 | sqlite3_free(error); |
| 100 | return; |
| 101 | } |
| 102 | |
| 103 | char* insert_sql = sqlite3_mprintf("INSERT INTO trace_bounds VALUES(%" PRId64 |
| 104 | ", %" PRId64 ")", |
| 105 | bounds.first, bounds.second); |
| 106 | |
| 107 | sqlite3_exec(db, insert_sql, 0, 0, &error); |
| 108 | sqlite3_free(insert_sql); |
| 109 | if (error) { |
| 110 | PERFETTO_ELOG("Error inserting bounds table: %s", error); |
| 111 | sqlite3_free(error); |
| 112 | } |
| 113 | } |
| 114 | |
Sami Kyostila | 3366894 | 2018-11-13 16:33:32 +0000 | [diff] [blame] | 115 | void CreateBuiltinTables(sqlite3* db) { |
| 116 | char* error = nullptr; |
| 117 | sqlite3_exec(db, "CREATE TABLE perfetto_tables(name STRING)", 0, 0, &error); |
| 118 | if (error) { |
| 119 | PERFETTO_ELOG("Error initializing: %s", error); |
| 120 | sqlite3_free(error); |
Ioannis Ilkos | 178535e | 2018-11-05 17:32:45 +0000 | [diff] [blame] | 121 | } |
Ioannis Ilkos | b8b1110 | 2019-01-29 17:56:55 +0000 | [diff] [blame] | 122 | sqlite3_exec(db, |
| 123 | "CREATE TABLE trace_bounds(start_ts BIG INT, end_ts BIG INT)", 0, |
| 124 | 0, &error); |
| 125 | if (error) { |
| 126 | PERFETTO_ELOG("Error initializing: %s", error); |
| 127 | sqlite3_free(error); |
| 128 | } |
Ioannis Ilkos | b8b1110 | 2019-01-29 17:56:55 +0000 | [diff] [blame] | 129 | |
Lalit Maganti | 086295b | 2019-04-04 16:59:19 +0100 | [diff] [blame] | 130 | // Initialize the bounds table with some data so even before parsing any data, |
| 131 | // we still have a valid table. |
| 132 | BuildBoundsTable(db, std::make_pair(0, 0)); |
Ioannis Ilkos | 178535e | 2018-11-05 17:32:45 +0000 | [diff] [blame] | 133 | } |
Hector Dearman | c0a2909 | 2018-11-08 17:34:23 +0000 | [diff] [blame] | 134 | |
Lalit Maganti | 8320e6d | 2019-03-14 18:49:33 +0000 | [diff] [blame] | 135 | void CreateBuiltinViews(sqlite3* db) { |
| 136 | char* error = nullptr; |
| 137 | sqlite3_exec(db, |
Lalit Maganti | 809b2f9 | 2019-11-07 13:27:26 +0000 | [diff] [blame] | 138 | "CREATE VIEW counter_definitions AS " |
| 139 | "SELECT " |
| 140 | " *, " |
Lalit Maganti | bbc9700 | 2019-11-07 17:57:23 +0000 | [diff] [blame] | 141 | " id AS counter_id " |
Lalit Maganti | 809b2f9 | 2019-11-07 13:27:26 +0000 | [diff] [blame] | 142 | "FROM counter_track", |
| 143 | 0, 0, &error); |
| 144 | if (error) { |
| 145 | PERFETTO_ELOG("Error initializing: %s", error); |
| 146 | sqlite3_free(error); |
| 147 | } |
| 148 | |
| 149 | sqlite3_exec(db, |
Lalit Maganti | 69c9444 | 2019-11-07 18:59:02 +0000 | [diff] [blame] | 150 | "CREATE VIEW counter_values AS " |
| 151 | "SELECT " |
| 152 | " *, " |
| 153 | " track_id as counter_id " |
| 154 | "FROM counter", |
Lalit Maganti | bbc9700 | 2019-11-07 17:57:23 +0000 | [diff] [blame] | 155 | 0, 0, &error); |
| 156 | if (error) { |
| 157 | PERFETTO_ELOG("Error initializing: %s", error); |
| 158 | sqlite3_free(error); |
| 159 | } |
| 160 | |
| 161 | sqlite3_exec(db, |
Lalit Maganti | 8320e6d | 2019-03-14 18:49:33 +0000 | [diff] [blame] | 162 | "CREATE VIEW counters AS " |
Lalit Maganti | 69c9444 | 2019-11-07 18:59:02 +0000 | [diff] [blame] | 163 | "SELECT * " |
Lalit Maganti | bbc9700 | 2019-11-07 17:57:23 +0000 | [diff] [blame] | 164 | "FROM counter_values v " |
| 165 | "INNER JOIN counter_track t " |
| 166 | "ON v.track_id = t.id " |
Lalit Maganti | 6fbb0ad | 2019-10-25 14:42:28 +0100 | [diff] [blame] | 167 | "ORDER BY ts;", |
Lalit Maganti | 8320e6d | 2019-03-14 18:49:33 +0000 | [diff] [blame] | 168 | 0, 0, &error); |
| 169 | if (error) { |
| 170 | PERFETTO_ELOG("Error initializing: %s", error); |
| 171 | sqlite3_free(error); |
| 172 | } |
Eric Seckler | 972225e | 2019-04-18 11:07:12 +0100 | [diff] [blame] | 173 | |
| 174 | sqlite3_exec(db, |
| 175 | "CREATE VIEW slice AS " |
| 176 | "SELECT " |
| 177 | " *, " |
Lalit Maganti | bbc9700 | 2019-11-07 17:57:23 +0000 | [diff] [blame] | 178 | " category AS cat, " |
Lalit Maganti | c7ea02e | 2019-12-12 14:06:49 +0000 | [diff] [blame] | 179 | " id AS slice_id " |
Eric Seckler | 972225e | 2019-04-18 11:07:12 +0100 | [diff] [blame] | 180 | "FROM internal_slice;", |
| 181 | 0, 0, &error); |
| 182 | if (error) { |
| 183 | PERFETTO_ELOG("Error initializing: %s", error); |
| 184 | sqlite3_free(error); |
| 185 | } |
| 186 | |
Lalit Maganti | 24b0833 | 2019-10-04 12:53:08 +0100 | [diff] [blame] | 187 | sqlite3_exec(db, |
Lalit Maganti | 80f566d | 2020-01-06 13:35:01 +0000 | [diff] [blame] | 188 | "CREATE VIEW instants AS " |
| 189 | "SELECT " |
| 190 | "*, " |
| 191 | "0.0 as value " |
| 192 | "FROM instant;", |
| 193 | 0, 0, &error); |
Lalit Maganti | 1b620a6 | 2020-01-30 18:24:53 +0000 | [diff] [blame] | 194 | |
| 195 | if (error) { |
| 196 | PERFETTO_ELOG("Error initializing: %s", error); |
| 197 | sqlite3_free(error); |
| 198 | } |
| 199 | |
| 200 | sqlite3_exec(db, |
| 201 | "CREATE VIEW sched AS " |
| 202 | "SELECT " |
| 203 | "*, " |
| 204 | "ts + dur as ts_end " |
| 205 | "FROM sched_slice;", |
| 206 | 0, 0, &error); |
| 207 | |
Lalit Maganti | 80f566d | 2020-01-06 13:35:01 +0000 | [diff] [blame] | 208 | if (error) { |
| 209 | PERFETTO_ELOG("Error initializing: %s", error); |
| 210 | sqlite3_free(error); |
| 211 | } |
| 212 | |
Eric Seckler | 972225e | 2019-04-18 11:07:12 +0100 | [diff] [blame] | 213 | // Legacy view for "slice" table with a deprecated table name. |
| 214 | // TODO(eseckler): Remove this view when all users have switched to "slice". |
| 215 | sqlite3_exec(db, |
| 216 | "CREATE VIEW slices AS " |
| 217 | "SELECT * FROM slice;", |
| 218 | 0, 0, &error); |
| 219 | if (error) { |
| 220 | PERFETTO_ELOG("Error initializing: %s", error); |
| 221 | sqlite3_free(error); |
| 222 | } |
Lalit Maganti | eac7fff | 2020-01-10 16:42:54 +0000 | [diff] [blame] | 223 | |
| 224 | sqlite3_exec(db, |
| 225 | "CREATE VIEW thread AS " |
| 226 | "SELECT " |
| 227 | "id as utid, " |
| 228 | "* " |
| 229 | "FROM internal_thread;", |
| 230 | 0, 0, &error); |
| 231 | if (error) { |
| 232 | PERFETTO_ELOG("Error initializing: %s", error); |
| 233 | sqlite3_free(error); |
| 234 | } |
| 235 | |
| 236 | sqlite3_exec(db, |
| 237 | "CREATE VIEW process AS " |
| 238 | "SELECT " |
| 239 | "id as upid, " |
| 240 | "* " |
| 241 | "FROM internal_process;", |
| 242 | 0, 0, &error); |
| 243 | if (error) { |
| 244 | PERFETTO_ELOG("Error initializing: %s", error); |
| 245 | sqlite3_free(error); |
| 246 | } |
Lalit Maganti | 8320e6d | 2019-03-14 18:49:33 +0000 | [diff] [blame] | 247 | } |
| 248 | |
Primiano Tucci | 02c1176 | 2019-08-30 00:57:59 +0200 | [diff] [blame] | 249 | #if PERFETTO_BUILDFLAG(PERFETTO_TP_JSON) |
Mikhail Khokhlov | a8d310d | 2019-05-07 17:34:21 +0100 | [diff] [blame] | 250 | void ExportJson(sqlite3_context* ctx, int /*argc*/, sqlite3_value** argv) { |
| 251 | TraceStorage* storage = static_cast<TraceStorage*>(sqlite3_user_data(ctx)); |
Eric Seckler | 37ad6af | 2019-10-08 15:51:14 +0100 | [diff] [blame] | 252 | FILE* output; |
| 253 | if (sqlite3_value_type(argv[0]) == SQLITE_INTEGER) { |
| 254 | // Assume input is an FD. |
| 255 | output = fdopen(sqlite3_value_int(argv[0]), "w"); |
| 256 | if (!output) { |
| 257 | sqlite3_result_error(ctx, "Couldn't open output file from given FD", -1); |
| 258 | return; |
| 259 | } |
| 260 | } else { |
| 261 | const char* filename = |
| 262 | reinterpret_cast<const char*>(sqlite3_value_text(argv[0])); |
| 263 | output = fopen(filename, "w"); |
| 264 | if (!output) { |
| 265 | sqlite3_result_error(ctx, "Couldn't open output file", -1); |
| 266 | return; |
| 267 | } |
Mikhail Khokhlov | a8d310d | 2019-05-07 17:34:21 +0100 | [diff] [blame] | 268 | } |
| 269 | |
Eric Seckler | 7fb1d23 | 2019-10-23 14:42:43 +0100 | [diff] [blame] | 270 | util::Status result = json::ExportJson(storage, output); |
| 271 | if (!result.ok()) { |
| 272 | sqlite3_result_error(ctx, result.message().c_str(), -1); |
| 273 | return; |
Mikhail Khokhlov | a8d310d | 2019-05-07 17:34:21 +0100 | [diff] [blame] | 274 | } |
| 275 | } |
| 276 | |
| 277 | void CreateJsonExportFunction(TraceStorage* ts, sqlite3* db) { |
| 278 | auto ret = sqlite3_create_function_v2(db, "EXPORT_JSON", 1, SQLITE_UTF8, ts, |
| 279 | ExportJson, nullptr, nullptr, |
| 280 | sqlite_utils::kSqliteStatic); |
| 281 | if (ret) { |
| 282 | PERFETTO_ELOG("Error initializing EXPORT_JSON"); |
| 283 | } |
| 284 | } |
| 285 | #endif |
| 286 | |
Lalit Maganti | 062c8f8 | 2019-10-03 20:11:29 +0100 | [diff] [blame] | 287 | void Hash(sqlite3_context* ctx, int argc, sqlite3_value** argv) { |
| 288 | base::Hash hash; |
| 289 | for (int i = 0; i < argc; ++i) { |
| 290 | sqlite3_value* value = argv[i]; |
| 291 | switch (sqlite3_value_type(value)) { |
| 292 | case SQLITE_INTEGER: |
| 293 | hash.Update(sqlite3_value_int64(value)); |
| 294 | break; |
| 295 | case SQLITE_TEXT: { |
| 296 | const char* ptr = |
| 297 | reinterpret_cast<const char*>(sqlite3_value_text(value)); |
| 298 | hash.Update(ptr, strlen(ptr)); |
| 299 | break; |
| 300 | } |
| 301 | default: |
| 302 | sqlite3_result_error(ctx, "Unsupported type of arg passed to HASH", -1); |
| 303 | return; |
| 304 | } |
| 305 | } |
| 306 | sqlite3_result_int64(ctx, static_cast<int64_t>(hash.digest())); |
| 307 | } |
| 308 | |
Neda Topoljanac | c2aecf7 | 2019-11-12 14:11:19 +0000 | [diff] [blame] | 309 | void Demangle(sqlite3_context* ctx, int argc, sqlite3_value** argv) { |
| 310 | if (argc != 1) { |
| 311 | sqlite3_result_error(ctx, "Unsupported number of arg passed to DEMANGLE", |
| 312 | -1); |
| 313 | return; |
| 314 | } |
| 315 | sqlite3_value* value = argv[0]; |
| 316 | if (sqlite3_value_type(value) != SQLITE_TEXT) { |
| 317 | sqlite3_result_error(ctx, "Unsupported type of arg passed to DEMANGLE", -1); |
| 318 | return; |
| 319 | } |
| 320 | const char* ptr = reinterpret_cast<const char*>(sqlite3_value_text(value)); |
Primiano Tucci | a1a26de | 2019-11-12 15:45:19 -0800 | [diff] [blame] | 321 | #if !PERFETTO_BUILDFLAG(PERFETTO_OS_WIN) |
Neda Topoljanac | c2aecf7 | 2019-11-12 14:11:19 +0000 | [diff] [blame] | 322 | int ignored = 0; |
| 323 | // This memory was allocated by malloc and will be passed to SQLite to free. |
| 324 | char* demangled_name = abi::__cxa_demangle(ptr, nullptr, nullptr, &ignored); |
| 325 | if (!demangled_name) { |
| 326 | sqlite3_result_null(ctx); |
| 327 | return; |
| 328 | } |
| 329 | sqlite3_result_text(ctx, demangled_name, -1, free); |
Primiano Tucci | a1a26de | 2019-11-12 15:45:19 -0800 | [diff] [blame] | 330 | #else |
| 331 | sqlite3_result_text(ctx, ptr, -1, sqlite_utils::kSqliteTransient); |
| 332 | #endif |
Neda Topoljanac | c2aecf7 | 2019-11-12 14:11:19 +0000 | [diff] [blame] | 333 | } |
| 334 | |
Pascal Muetschard | 7e47609 | 2020-01-15 16:41:30 -0800 | [diff] [blame] | 335 | void LastNonNullStep(sqlite3_context* ctx, int argc, sqlite3_value** argv) { |
| 336 | if (argc != 1) { |
| 337 | sqlite3_result_error(ctx, |
| 338 | "Unsupported number of args passed to LAST_NON_NULL", |
| 339 | -1); |
| 340 | return; |
| 341 | } |
| 342 | sqlite3_value* value = argv[0]; |
| 343 | if (sqlite3_value_type(value) == SQLITE_NULL) { |
| 344 | return; |
| 345 | } |
| 346 | sqlite3_value** ptr = reinterpret_cast<sqlite3_value**>( |
| 347 | sqlite3_aggregate_context(ctx, sizeof(sqlite3_value*))); |
| 348 | if (ptr) { |
| 349 | if (*ptr != nullptr) { |
| 350 | sqlite3_value_free(*ptr); |
| 351 | } |
| 352 | *ptr = sqlite3_value_dup(value); |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | void LastNonNullInverse(sqlite3_context* ctx, int argc, sqlite3_value** argv) { |
| 357 | // Do nothing. |
| 358 | base::ignore_result(ctx); |
| 359 | base::ignore_result(argc); |
| 360 | base::ignore_result(argv); |
| 361 | } |
| 362 | |
| 363 | void LastNonNullValue(sqlite3_context* ctx) { |
| 364 | sqlite3_value** ptr = |
| 365 | reinterpret_cast<sqlite3_value**>(sqlite3_aggregate_context(ctx, 0)); |
| 366 | if (!ptr || !*ptr) { |
| 367 | sqlite3_result_null(ctx); |
| 368 | } else { |
| 369 | sqlite3_result_value(ctx, *ptr); |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | void LastNonNullFinal(sqlite3_context* ctx) { |
| 374 | sqlite3_value** ptr = |
| 375 | reinterpret_cast<sqlite3_value**>(sqlite3_aggregate_context(ctx, 0)); |
| 376 | if (!ptr || !*ptr) { |
| 377 | sqlite3_result_null(ctx); |
| 378 | } else { |
| 379 | sqlite3_result_value(ctx, *ptr); |
| 380 | sqlite3_value_free(*ptr); |
| 381 | } |
| 382 | } |
| 383 | |
Lalit Maganti | 062c8f8 | 2019-10-03 20:11:29 +0100 | [diff] [blame] | 384 | void CreateHashFunction(sqlite3* db) { |
| 385 | auto ret = sqlite3_create_function_v2( |
| 386 | db, "HASH", -1, SQLITE_UTF8 | SQLITE_DETERMINISTIC, nullptr, &Hash, |
| 387 | nullptr, nullptr, nullptr); |
| 388 | if (ret) { |
| 389 | PERFETTO_ELOG("Error initializing HASH"); |
| 390 | } |
| 391 | } |
| 392 | |
Neda Topoljanac | c2aecf7 | 2019-11-12 14:11:19 +0000 | [diff] [blame] | 393 | void CreateDemangledNameFunction(sqlite3* db) { |
| 394 | auto ret = sqlite3_create_function_v2( |
| 395 | db, "DEMANGLE", 1, SQLITE_UTF8 | SQLITE_DETERMINISTIC, nullptr, &Demangle, |
| 396 | nullptr, nullptr, nullptr); |
| 397 | if (ret != SQLITE_OK) { |
| 398 | PERFETTO_ELOG("Error initializing DEMANGLE: %s", sqlite3_errmsg(db)); |
| 399 | } |
| 400 | } |
| 401 | |
Pascal Muetschard | 7e47609 | 2020-01-15 16:41:30 -0800 | [diff] [blame] | 402 | void CreateLastNonNullFunction(sqlite3* db) { |
| 403 | auto ret = sqlite3_create_window_function( |
| 404 | db, "LAST_NON_NULL", 1, SQLITE_UTF8 | SQLITE_DETERMINISTIC, nullptr, |
| 405 | &LastNonNullStep, &LastNonNullFinal, &LastNonNullValue, |
| 406 | &LastNonNullInverse, nullptr); |
| 407 | if (ret) { |
| 408 | PERFETTO_ELOG("Error initializing LAST_NON_NULL"); |
| 409 | } |
| 410 | } |
| 411 | |
Lalit Maganti | 2553b80 | 2019-05-24 13:25:58 +0100 | [diff] [blame] | 412 | void SetupMetrics(TraceProcessor* tp, |
| 413 | sqlite3* db, |
Lalit Maganti | 2553b80 | 2019-05-24 13:25:58 +0100 | [diff] [blame] | 414 | std::vector<metrics::SqlMetricFile>* sql_metrics) { |
Lalit Maganti | 5f8727d | 2019-05-24 13:38:50 +0100 | [diff] [blame] | 415 | tp->ExtendMetricsProto(kMetricsDescriptor.data(), kMetricsDescriptor.size()); |
| 416 | |
Lalit Maganti | 2553b80 | 2019-05-24 13:25:58 +0100 | [diff] [blame] | 417 | for (const auto& file_to_sql : metrics::sql_metrics::kFileToSql) { |
Lalit Maganti | 5f8727d | 2019-05-24 13:38:50 +0100 | [diff] [blame] | 418 | tp->RegisterMetric(file_to_sql.path, file_to_sql.sql); |
Lalit Maganti | 2553b80 | 2019-05-24 13:25:58 +0100 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | { |
| 422 | std::unique_ptr<metrics::RunMetricContext> ctx( |
| 423 | new metrics::RunMetricContext()); |
| 424 | ctx->tp = tp; |
| 425 | ctx->metrics = sql_metrics; |
| 426 | auto ret = sqlite3_create_function_v2( |
| 427 | db, "RUN_METRIC", -1, SQLITE_UTF8, ctx.release(), metrics::RunMetric, |
| 428 | nullptr, nullptr, |
| 429 | [](void* ptr) { delete static_cast<metrics::RunMetricContext*>(ptr); }); |
| 430 | if (ret) |
| 431 | PERFETTO_ELOG("Error initializing RUN_METRIC"); |
| 432 | } |
| 433 | |
| 434 | { |
| 435 | auto ret = sqlite3_create_function_v2( |
| 436 | db, "RepeatedField", 1, SQLITE_UTF8, nullptr, nullptr, |
| 437 | metrics::RepeatedFieldStep, metrics::RepeatedFieldFinal, nullptr); |
| 438 | if (ret) |
| 439 | PERFETTO_ELOG("Error initializing RepeatedField"); |
| 440 | } |
| 441 | } |
Oystein Eftevaag | 477bbff | 2020-01-27 11:07:49 -0800 | [diff] [blame] | 442 | |
| 443 | void EnsureSqliteInitialized() { |
| 444 | // sqlite3_initialize isn't actually thread-safe despite being documented |
| 445 | // as such; we need to make sure multiple TraceProcessorImpl instances don't |
| 446 | // call it concurrently and only gets called once per process, instead. |
Lalit Maganti | 1e5630e | 2020-01-29 12:34:14 +0000 | [diff] [blame] | 447 | static bool init_once = [] { return sqlite3_initialize() == SQLITE_OK; }(); |
Oystein Eftevaag | 477bbff | 2020-01-27 11:07:49 -0800 | [diff] [blame] | 448 | PERFETTO_CHECK(init_once); |
| 449 | } |
| 450 | |
Hector Dearman | c0a2909 | 2018-11-08 17:34:23 +0000 | [diff] [blame] | 451 | } // namespace |
| 452 | |
Eric Seckler | a7870e6 | 2019-11-01 10:11:58 +0000 | [diff] [blame] | 453 | TraceProcessorImpl::TraceProcessorImpl(const Config& cfg) |
| 454 | : TraceProcessorStorageImpl(cfg) { |
Lalit Maganti | 6d1f7b5 | 2020-02-27 13:16:44 +0000 | [diff] [blame] | 455 | context_.fuchsia_trace_tokenizer.reset(new FuchsiaTraceTokenizer(&context_)); |
| 456 | context_.fuchsia_trace_parser.reset(new FuchsiaTraceParser(&context_)); |
| 457 | |
| 458 | context_.systrace_trace_parser.reset(new SystraceTraceParser(&context_)); |
| 459 | |
Lalit Maganti | 9d538bd | 2020-03-12 23:48:16 +0000 | [diff] [blame^] | 460 | if (gzip_utils::IsGzipSupported()) |
| 461 | context_.gzip_trace_parser.reset(new GzipTraceParser(&context_)); |
Lalit Maganti | 6d1f7b5 | 2020-02-27 13:16:44 +0000 | [diff] [blame] | 462 | |
| 463 | #if PERFETTO_BUILDFLAG(PERFETTO_TP_JSON) |
| 464 | context_.json_trace_tokenizer.reset(new JsonTraceTokenizer(&context_)); |
| 465 | context_.json_trace_parser.reset(new JsonTraceParser(&context_)); |
| 466 | #endif |
| 467 | |
Mikhail Khokhlov | 692087e | 2019-12-11 10:53:45 +0000 | [diff] [blame] | 468 | RegisterAdditionalModules(&context_); |
Lalit Maganti | 6d1f7b5 | 2020-02-27 13:16:44 +0000 | [diff] [blame] | 469 | |
Ioannis Ilkos | eff38f5 | 2018-10-29 10:37:55 +0000 | [diff] [blame] | 470 | sqlite3* db = nullptr; |
Oystein Eftevaag | 477bbff | 2020-01-27 11:07:49 -0800 | [diff] [blame] | 471 | EnsureSqliteInitialized(); |
Ioannis Ilkos | eff38f5 | 2018-10-29 10:37:55 +0000 | [diff] [blame] | 472 | PERFETTO_CHECK(sqlite3_open(":memory:", &db) == SQLITE_OK); |
Ioannis Ilkos | 7199d55 | 2019-03-13 18:35:31 +0000 | [diff] [blame] | 473 | InitializeSqlite(db); |
Sami Kyostila | 3366894 | 2018-11-13 16:33:32 +0000 | [diff] [blame] | 474 | CreateBuiltinTables(db); |
Lalit Maganti | 8320e6d | 2019-03-14 18:49:33 +0000 | [diff] [blame] | 475 | CreateBuiltinViews(db); |
Ioannis Ilkos | eff38f5 | 2018-10-29 10:37:55 +0000 | [diff] [blame] | 476 | db_.reset(std::move(db)); |
| 477 | |
Primiano Tucci | 02c1176 | 2019-08-30 00:57:59 +0200 | [diff] [blame] | 478 | #if PERFETTO_BUILDFLAG(PERFETTO_TP_JSON) |
Mikhail Khokhlov | a8d310d | 2019-05-07 17:34:21 +0100 | [diff] [blame] | 479 | CreateJsonExportFunction(this->context_.storage.get(), db); |
| 480 | #endif |
Lalit Maganti | 062c8f8 | 2019-10-03 20:11:29 +0100 | [diff] [blame] | 481 | CreateHashFunction(db); |
Neda Topoljanac | c2aecf7 | 2019-11-12 14:11:19 +0000 | [diff] [blame] | 482 | CreateDemangledNameFunction(db); |
Pascal Muetschard | 7e47609 | 2020-01-15 16:41:30 -0800 | [diff] [blame] | 483 | CreateLastNonNullFunction(db); |
Mikhail Khokhlov | a8d310d | 2019-05-07 17:34:21 +0100 | [diff] [blame] | 484 | |
Lalit Maganti | 5f8727d | 2019-05-24 13:38:50 +0100 | [diff] [blame] | 485 | SetupMetrics(this, *db_, &sql_metrics_); |
Lalit Maganti | 2553b80 | 2019-05-24 13:25:58 +0100 | [diff] [blame] | 486 | |
Lalit Maganti | 65800eb | 2020-01-30 15:18:59 +0000 | [diff] [blame] | 487 | // Setup the query cache. |
| 488 | query_cache_.reset(new QueryCache()); |
| 489 | |
Lalit Maganti | 786cc92 | 2019-09-06 16:04:32 +0100 | [diff] [blame] | 490 | const TraceStorage* storage = context_.storage.get(); |
Lalit Maganti | 809b2f9 | 2019-11-07 13:27:26 +0000 | [diff] [blame] | 491 | |
Lalit Maganti | 679b29d | 2020-01-13 13:26:28 +0000 | [diff] [blame] | 492 | SqlStatsTable::RegisterTable(*db_, storage); |
Lalit Maganti | 679b29d | 2020-01-13 13:26:28 +0000 | [diff] [blame] | 493 | StatsTable::RegisterTable(*db_, storage); |
| 494 | |
Lalit Maganti | 8a39fae | 2020-01-20 17:22:10 +0000 | [diff] [blame] | 495 | // Operator tables. |
| 496 | SpanJoinOperatorTable::RegisterTable(*db_, storage); |
| 497 | WindowOperatorTable::RegisterTable(*db_, storage); |
| 498 | |
Lalit Maganti | 679b29d | 2020-01-13 13:26:28 +0000 | [diff] [blame] | 499 | // New style tables but with some custom logic. |
Lalit Maganti | 65800eb | 2020-01-30 15:18:59 +0000 | [diff] [blame] | 500 | SqliteRawTable::RegisterTable(*db_, query_cache_.get(), |
| 501 | context_.storage.get()); |
Lalit Maganti | 920f4f9 | 2020-02-19 13:45:05 +0000 | [diff] [blame] | 502 | |
| 503 | // Tables dynamically generated at query time. |
| 504 | RegisterDynamicTable(std::unique_ptr<ExperimentalFlamegraphGenerator>( |
| 505 | new ExperimentalFlamegraphGenerator(&context_))); |
| 506 | RegisterDynamicTable(std::unique_ptr<ExperimentalCounterDurGenerator>( |
| 507 | new ExperimentalCounterDurGenerator(storage->counter_table()))); |
Lalit Maganti | 679b29d | 2020-01-13 13:26:28 +0000 | [diff] [blame] | 508 | |
| 509 | // New style db-backed tables. |
Lalit Maganti | 65800eb | 2020-01-30 15:18:59 +0000 | [diff] [blame] | 510 | RegisterDbTable(storage->arg_table()); |
| 511 | RegisterDbTable(storage->thread_table()); |
| 512 | RegisterDbTable(storage->process_table()); |
Lalit Maganti | 1908e26 | 2020-01-09 14:33:19 +0000 | [diff] [blame] | 513 | |
Lalit Maganti | 65800eb | 2020-01-30 15:18:59 +0000 | [diff] [blame] | 514 | RegisterDbTable(storage->slice_table()); |
Lalit Maganti | 1b620a6 | 2020-01-30 18:24:53 +0000 | [diff] [blame] | 515 | RegisterDbTable(storage->sched_slice_table()); |
Lalit Maganti | 65800eb | 2020-01-30 15:18:59 +0000 | [diff] [blame] | 516 | RegisterDbTable(storage->instant_table()); |
| 517 | RegisterDbTable(storage->gpu_slice_table()); |
Lalit Maganti | 1308e3f | 2019-12-09 20:24:20 +0000 | [diff] [blame] | 518 | |
Lalit Maganti | 65800eb | 2020-01-30 15:18:59 +0000 | [diff] [blame] | 519 | RegisterDbTable(storage->track_table()); |
| 520 | RegisterDbTable(storage->thread_track_table()); |
| 521 | RegisterDbTable(storage->process_track_table()); |
| 522 | RegisterDbTable(storage->gpu_track_table()); |
Lalit Maganti | 809b2f9 | 2019-11-07 13:27:26 +0000 | [diff] [blame] | 523 | |
Lalit Maganti | 65800eb | 2020-01-30 15:18:59 +0000 | [diff] [blame] | 524 | RegisterDbTable(storage->counter_table()); |
Lalit Maganti | 4b3b3ad | 2019-12-04 19:27:35 +0000 | [diff] [blame] | 525 | |
Lalit Maganti | 65800eb | 2020-01-30 15:18:59 +0000 | [diff] [blame] | 526 | RegisterDbTable(storage->counter_track_table()); |
| 527 | RegisterDbTable(storage->process_counter_track_table()); |
| 528 | RegisterDbTable(storage->thread_counter_track_table()); |
| 529 | RegisterDbTable(storage->cpu_counter_track_table()); |
| 530 | RegisterDbTable(storage->irq_counter_track_table()); |
| 531 | RegisterDbTable(storage->softirq_counter_track_table()); |
| 532 | RegisterDbTable(storage->gpu_counter_track_table()); |
Lalit Maganti | 809b2f9 | 2019-11-07 13:27:26 +0000 | [diff] [blame] | 533 | |
Lalit Maganti | 65800eb | 2020-01-30 15:18:59 +0000 | [diff] [blame] | 534 | RegisterDbTable(storage->heap_graph_object_table()); |
| 535 | RegisterDbTable(storage->heap_graph_reference_table()); |
Lalit Maganti | 809b2f9 | 2019-11-07 13:27:26 +0000 | [diff] [blame] | 536 | |
Lalit Maganti | 65800eb | 2020-01-30 15:18:59 +0000 | [diff] [blame] | 537 | RegisterDbTable(storage->symbol_table()); |
| 538 | RegisterDbTable(storage->heap_profile_allocation_table()); |
| 539 | RegisterDbTable(storage->cpu_profile_stack_sample_table()); |
| 540 | RegisterDbTable(storage->stack_profile_callsite_table()); |
| 541 | RegisterDbTable(storage->stack_profile_mapping_table()); |
| 542 | RegisterDbTable(storage->stack_profile_frame_table()); |
Lalit Maganti | 809b2f9 | 2019-11-07 13:27:26 +0000 | [diff] [blame] | 543 | |
Lalit Maganti | 65800eb | 2020-01-30 15:18:59 +0000 | [diff] [blame] | 544 | RegisterDbTable(storage->android_log_table()); |
Lalit Maganti | f1ab24f | 2020-01-07 17:42:26 +0000 | [diff] [blame] | 545 | |
Lalit Maganti | 65800eb | 2020-01-30 15:18:59 +0000 | [diff] [blame] | 546 | RegisterDbTable(storage->vulkan_memory_allocations_table()); |
Lalit Maganti | ededb0e | 2020-01-08 12:50:34 +0000 | [diff] [blame] | 547 | |
Lalit Maganti | 65800eb | 2020-01-30 15:18:59 +0000 | [diff] [blame] | 548 | RegisterDbTable(storage->metadata_table()); |
Ioannis Ilkos | eff38f5 | 2018-10-29 10:37:55 +0000 | [diff] [blame] | 549 | } |
| 550 | |
Lalit Maganti | 7709501 | 2019-02-19 13:13:59 +0000 | [diff] [blame] | 551 | TraceProcessorImpl::~TraceProcessorImpl() { |
| 552 | for (auto* it : iterators_) |
| 553 | it->Reset(); |
| 554 | } |
Ioannis Ilkos | eff38f5 | 2018-10-29 10:37:55 +0000 | [diff] [blame] | 555 | |
Lalit Maganti | 1f06718 | 2019-05-09 14:50:05 +0100 | [diff] [blame] | 556 | util::Status TraceProcessorImpl::Parse(std::unique_ptr<uint8_t[]> data, |
| 557 | size_t size) { |
Primiano Tucci | ee2ce1d | 2019-11-01 19:14:17 +0100 | [diff] [blame] | 558 | bytes_parsed_ += size; |
Eric Seckler | a7870e6 | 2019-11-01 10:11:58 +0000 | [diff] [blame] | 559 | return TraceProcessorStorageImpl::Parse(std::move(data), size); |
Ioannis Ilkos | eff38f5 | 2018-10-29 10:37:55 +0000 | [diff] [blame] | 560 | } |
| 561 | |
Primiano Tucci | ee2ce1d | 2019-11-01 19:14:17 +0100 | [diff] [blame] | 562 | std::string TraceProcessorImpl::GetCurrentTraceName() { |
| 563 | if (current_trace_name_.empty()) |
| 564 | return ""; |
| 565 | auto size = " (" + std::to_string(bytes_parsed_ / 1024 / 1024) + " MB)"; |
| 566 | return current_trace_name_ + size; |
| 567 | } |
| 568 | |
| 569 | void TraceProcessorImpl::SetCurrentTraceName(const std::string& name) { |
| 570 | current_trace_name_ = name; |
| 571 | } |
| 572 | |
Ioannis Ilkos | eff38f5 | 2018-10-29 10:37:55 +0000 | [diff] [blame] | 573 | void TraceProcessorImpl::NotifyEndOfFile() { |
Primiano Tucci | ee2ce1d | 2019-11-01 19:14:17 +0100 | [diff] [blame] | 574 | if (current_trace_name_.empty()) |
| 575 | current_trace_name_ = "Unnamed trace"; |
| 576 | |
Eric Seckler | a7870e6 | 2019-11-01 10:11:58 +0000 | [diff] [blame] | 577 | TraceProcessorStorageImpl::NotifyEndOfFile(); |
Deepanjan Roy | 01994ca | 2019-04-02 11:05:34 -0700 | [diff] [blame] | 578 | |
Mikhail Khokhlov | 81491b9 | 2019-12-19 13:58:22 +0000 | [diff] [blame] | 579 | SchedEventTracker::GetOrCreate(&context_)->FlushPendingEvents(); |
Hector Dearman | 93b0b09 | 2020-01-09 14:35:04 +0000 | [diff] [blame] | 580 | context_.metadata_tracker->SetMetadata( |
| 581 | metadata::trace_size_bytes, |
| 582 | Variadic::Integer(static_cast<int64_t>(bytes_parsed_))); |
Ioannis Ilkos | b8b1110 | 2019-01-29 17:56:55 +0000 | [diff] [blame] | 583 | BuildBoundsTable(*db_, context_.storage->GetTraceTimestampBoundsNs()); |
Primiano Tucci | 11a79e7 | 2019-10-29 22:13:10 +0100 | [diff] [blame] | 584 | |
| 585 | // Create a snapshot of all tables and views created so far. This is so later |
| 586 | // we can drop all extra tables created by the UI and reset to the original |
| 587 | // state (see RestoreInitialTables). |
| 588 | initial_tables_.clear(); |
| 589 | auto it = ExecuteQuery(kAllTablesQuery); |
| 590 | while (it.Next()) { |
| 591 | auto value = it.Get(0); |
| 592 | PERFETTO_CHECK(value.type == SqlValue::Type::kString); |
| 593 | initial_tables_.push_back(value.string_value); |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | size_t TraceProcessorImpl::RestoreInitialTables() { |
| 598 | std::vector<std::pair<std::string, std::string>> deletion_list; |
| 599 | std::string msg = "Resetting DB to initial state, deleting table/views:"; |
| 600 | for (auto it = ExecuteQuery(kAllTablesQuery); it.Next();) { |
| 601 | std::string name(it.Get(0).string_value); |
| 602 | std::string type(it.Get(1).string_value); |
| 603 | if (std::find(initial_tables_.begin(), initial_tables_.end(), name) == |
| 604 | initial_tables_.end()) { |
| 605 | msg += " " + name; |
| 606 | deletion_list.push_back(std::make_pair(type, name)); |
| 607 | } |
| 608 | } |
| 609 | |
| 610 | PERFETTO_LOG("%s", msg.c_str()); |
| 611 | for (const auto& tn : deletion_list) { |
| 612 | std::string query = "DROP " + tn.first + " " + tn.second; |
| 613 | auto it = ExecuteQuery(query); |
| 614 | while (it.Next()) { |
| 615 | } |
Primiano Tucci | 1b4c8fc | 2020-01-15 22:29:42 +0000 | [diff] [blame] | 616 | // Index deletion can legitimately fail. If one creates an index "i" on a |
| 617 | // table "t" but issues the deletion in the order (t, i), the DROP index i |
| 618 | // will fail with "no such index" because deleting the table "t" |
| 619 | // automatically deletes all associated indexes. |
| 620 | if (!it.Status().ok() && tn.first != "index") |
| 621 | PERFETTO_FATAL("%s -> %s", query.c_str(), it.Status().c_message()); |
Primiano Tucci | 11a79e7 | 2019-10-29 22:13:10 +0100 | [diff] [blame] | 622 | } |
| 623 | return deletion_list.size(); |
Ioannis Ilkos | eff38f5 | 2018-10-29 10:37:55 +0000 | [diff] [blame] | 624 | } |
| 625 | |
Lalit Maganti | 7709501 | 2019-02-19 13:13:59 +0000 | [diff] [blame] | 626 | TraceProcessor::Iterator TraceProcessorImpl::ExecuteQuery( |
Lalit Maganti | aac2f65 | 2019-04-30 12:16:21 +0100 | [diff] [blame] | 627 | const std::string& sql, |
| 628 | int64_t time_queued) { |
Lalit Maganti | 7709501 | 2019-02-19 13:13:59 +0000 | [diff] [blame] | 629 | sqlite3_stmt* raw_stmt; |
Lalit Maganti | 73b1a0a | 2019-04-08 13:51:14 +0100 | [diff] [blame] | 630 | int err = sqlite3_prepare_v2(*db_, sql.c_str(), static_cast<int>(sql.size()), |
Lalit Maganti | 7709501 | 2019-02-19 13:13:59 +0000 | [diff] [blame] | 631 | &raw_stmt, nullptr); |
Lalit Maganti | 1f06718 | 2019-05-09 14:50:05 +0100 | [diff] [blame] | 632 | util::Status status; |
Lalit Maganti | 06acc02 | 2019-04-03 16:02:15 +0100 | [diff] [blame] | 633 | uint32_t col_count = 0; |
| 634 | if (err != SQLITE_OK) { |
Lalit Maganti | 7ee6abb | 2019-05-09 17:57:32 +0100 | [diff] [blame] | 635 | status = util::ErrStatus("%s", sqlite3_errmsg(*db_)); |
Lalit Maganti | 7709501 | 2019-02-19 13:13:59 +0000 | [diff] [blame] | 636 | } else { |
| 637 | col_count = static_cast<uint32_t>(sqlite3_column_count(raw_stmt)); |
| 638 | } |
Lalit Maganti | aac2f65 | 2019-04-30 12:16:21 +0100 | [diff] [blame] | 639 | |
| 640 | base::TimeNanos t_start = base::GetWallTimeNs(); |
| 641 | uint32_t sql_stats_row = |
| 642 | context_.storage->mutable_sql_stats()->RecordQueryBegin(sql, time_queued, |
| 643 | t_start.count()); |
| 644 | |
| 645 | std::unique_ptr<IteratorImpl> impl(new IteratorImpl( |
Lalit Maganti | 1f06718 | 2019-05-09 14:50:05 +0100 | [diff] [blame] | 646 | this, *db_, ScopedStmt(raw_stmt), col_count, status, sql_stats_row)); |
Lalit Maganti | 7709501 | 2019-02-19 13:13:59 +0000 | [diff] [blame] | 647 | iterators_.emplace_back(impl.get()); |
| 648 | return TraceProcessor::Iterator(std::move(impl)); |
| 649 | } |
| 650 | |
Ioannis Ilkos | eff38f5 | 2018-10-29 10:37:55 +0000 | [diff] [blame] | 651 | void TraceProcessorImpl::InterruptQuery() { |
| 652 | if (!db_) |
| 653 | return; |
| 654 | query_interrupted_.store(true); |
| 655 | sqlite3_interrupt(db_.get()); |
| 656 | } |
| 657 | |
Lalit Maganti | 5f8727d | 2019-05-24 13:38:50 +0100 | [diff] [blame] | 658 | util::Status TraceProcessorImpl::RegisterMetric(const std::string& path, |
| 659 | const std::string& sql) { |
Lalit Maganti | f9d8030 | 2019-06-19 12:48:25 +0100 | [diff] [blame] | 660 | std::string stripped_sql; |
| 661 | for (base::StringSplitter sp(sql, '\n'); sp.Next();) { |
| 662 | if (strncmp(sp.cur_token(), "--", 2) != 0) { |
| 663 | stripped_sql.append(sp.cur_token()); |
| 664 | stripped_sql.push_back('\n'); |
| 665 | } |
| 666 | } |
| 667 | |
| 668 | // Check if the metric with the given path already exists and if it does, just |
| 669 | // update the SQL associated with it. |
| 670 | auto it = std::find_if( |
| 671 | sql_metrics_.begin(), sql_metrics_.end(), |
| 672 | [&path](const metrics::SqlMetricFile& m) { return m.path == path; }); |
| 673 | if (it != sql_metrics_.end()) { |
| 674 | it->sql = stripped_sql; |
| 675 | return util::OkStatus(); |
| 676 | } |
| 677 | |
Lalit Maganti | 5f8727d | 2019-05-24 13:38:50 +0100 | [diff] [blame] | 678 | auto sep_idx = path.rfind("/"); |
| 679 | std::string basename = |
| 680 | sep_idx == std::string::npos ? path : path.substr(sep_idx + 1); |
| 681 | |
| 682 | auto sql_idx = basename.rfind(".sql"); |
| 683 | if (sql_idx == std::string::npos) { |
| 684 | return util::ErrStatus("Unable to find .sql extension for metric"); |
| 685 | } |
| 686 | auto no_ext_name = basename.substr(0, sql_idx); |
| 687 | |
| 688 | metrics::SqlMetricFile metric; |
| 689 | metric.path = path; |
| 690 | metric.proto_field_name = no_ext_name; |
| 691 | metric.output_table_name = no_ext_name + "_output"; |
Lalit Maganti | ca4d514 | 2019-05-28 13:25:47 +0100 | [diff] [blame] | 692 | metric.sql = stripped_sql; |
Lalit Maganti | 5f8727d | 2019-05-24 13:38:50 +0100 | [diff] [blame] | 693 | sql_metrics_.emplace_back(metric); |
| 694 | return util::OkStatus(); |
| 695 | } |
| 696 | |
| 697 | util::Status TraceProcessorImpl::ExtendMetricsProto(const uint8_t* data, |
| 698 | size_t size) { |
Lalit Maganti | 996f544 | 2019-06-21 16:12:58 +0100 | [diff] [blame] | 699 | util::Status status = pool_.AddFromFileDescriptorSet(data, size); |
| 700 | if (!status.ok()) |
| 701 | return status; |
Lalit Maganti | 5f8727d | 2019-05-24 13:38:50 +0100 | [diff] [blame] | 702 | |
Lalit Maganti | 2553b80 | 2019-05-24 13:25:58 +0100 | [diff] [blame] | 703 | for (const auto& desc : pool_.descriptors()) { |
Lalit Maganti | 6221107 | 2019-05-10 14:09:58 +0100 | [diff] [blame] | 704 | // Convert the full name (e.g. .perfetto.protos.TraceMetrics.SubMetric) |
| 705 | // into a function name of the form (TraceMetrics_SubMetric). |
| 706 | auto fn_name = desc.full_name().substr(desc.package_name().size() + 1); |
| 707 | std::replace(fn_name.begin(), fn_name.end(), '.', '_'); |
| 708 | |
Lalit Maganti | b182d7e | 2019-05-10 14:13:06 +0100 | [diff] [blame] | 709 | std::unique_ptr<metrics::BuildProtoContext> ctx( |
| 710 | new metrics::BuildProtoContext()); |
| 711 | ctx->tp = this; |
Lalit Maganti | 2553b80 | 2019-05-24 13:25:58 +0100 | [diff] [blame] | 712 | ctx->pool = &pool_; |
Lalit Maganti | b182d7e | 2019-05-10 14:13:06 +0100 | [diff] [blame] | 713 | ctx->desc = &desc; |
| 714 | |
Lalit Maganti | 6221107 | 2019-05-10 14:09:58 +0100 | [diff] [blame] | 715 | auto ret = sqlite3_create_function_v2( |
Lalit Maganti | b182d7e | 2019-05-10 14:13:06 +0100 | [diff] [blame] | 716 | *db_, fn_name.c_str(), -1, SQLITE_UTF8, ctx.release(), |
| 717 | metrics::BuildProto, nullptr, nullptr, [](void* ptr) { |
| 718 | delete static_cast<metrics::BuildProtoContext*>(ptr); |
| 719 | }); |
Lalit Maganti | 6221107 | 2019-05-10 14:09:58 +0100 | [diff] [blame] | 720 | if (ret != SQLITE_OK) |
| 721 | return util::ErrStatus("%s", sqlite3_errmsg(*db_)); |
| 722 | } |
Lalit Maganti | 996f544 | 2019-06-21 16:12:58 +0100 | [diff] [blame] | 723 | return util::OkStatus(); |
| 724 | } |
Lalit Maganti | 031ad2b | 2019-05-14 17:37:14 +0100 | [diff] [blame] | 725 | |
Lalit Maganti | 996f544 | 2019-06-21 16:12:58 +0100 | [diff] [blame] | 726 | util::Status TraceProcessorImpl::ComputeMetric( |
| 727 | const std::vector<std::string>& metric_names, |
| 728 | std::vector<uint8_t>* metrics_proto) { |
Lalit Maganti | 2553b80 | 2019-05-24 13:25:58 +0100 | [diff] [blame] | 729 | auto opt_idx = pool_.FindDescriptorIdx(".perfetto.protos.TraceMetrics"); |
Lalit Maganti | 031ad2b | 2019-05-14 17:37:14 +0100 | [diff] [blame] | 730 | if (!opt_idx.has_value()) |
| 731 | return util::Status("Root metrics proto descriptor not found"); |
| 732 | |
Lalit Maganti | 2553b80 | 2019-05-24 13:25:58 +0100 | [diff] [blame] | 733 | const auto& root_descriptor = pool_.descriptors()[opt_idx.value()]; |
| 734 | return metrics::ComputeMetrics(this, metric_names, sql_metrics_, |
| 735 | root_descriptor, metrics_proto); |
Lalit Maganti | d9f86b6 | 2019-04-08 11:11:51 +0100 | [diff] [blame] | 736 | } |
| 737 | |
Lalit Maganti | 7709501 | 2019-02-19 13:13:59 +0000 | [diff] [blame] | 738 | TraceProcessor::IteratorImpl::IteratorImpl(TraceProcessorImpl* trace_processor, |
| 739 | sqlite3* db, |
| 740 | ScopedStmt stmt, |
| 741 | uint32_t column_count, |
Lalit Maganti | 1f06718 | 2019-05-09 14:50:05 +0100 | [diff] [blame] | 742 | util::Status status, |
Lalit Maganti | aac2f65 | 2019-04-30 12:16:21 +0100 | [diff] [blame] | 743 | uint32_t sql_stats_row) |
Lalit Maganti | 7709501 | 2019-02-19 13:13:59 +0000 | [diff] [blame] | 744 | : trace_processor_(trace_processor), |
| 745 | db_(db), |
| 746 | stmt_(std::move(stmt)), |
| 747 | column_count_(column_count), |
Lalit Maganti | 1f06718 | 2019-05-09 14:50:05 +0100 | [diff] [blame] | 748 | status_(status), |
Lalit Maganti | aac2f65 | 2019-04-30 12:16:21 +0100 | [diff] [blame] | 749 | sql_stats_row_(sql_stats_row) {} |
Lalit Maganti | 7709501 | 2019-02-19 13:13:59 +0000 | [diff] [blame] | 750 | |
| 751 | TraceProcessor::IteratorImpl::~IteratorImpl() { |
| 752 | if (trace_processor_) { |
| 753 | auto* its = &trace_processor_->iterators_; |
| 754 | auto it = std::find(its->begin(), its->end(), this); |
| 755 | PERFETTO_CHECK(it != its->end()); |
| 756 | its->erase(it); |
Lalit Maganti | aac2f65 | 2019-04-30 12:16:21 +0100 | [diff] [blame] | 757 | |
| 758 | base::TimeNanos t_end = base::GetWallTimeNs(); |
| 759 | auto* sql_stats = trace_processor_->context_.storage->mutable_sql_stats(); |
| 760 | sql_stats->RecordQueryEnd(sql_stats_row_, t_end.count()); |
Lalit Maganti | 7709501 | 2019-02-19 13:13:59 +0000 | [diff] [blame] | 761 | } |
| 762 | } |
| 763 | |
| 764 | void TraceProcessor::IteratorImpl::Reset() { |
Lalit Maganti | 1f06718 | 2019-05-09 14:50:05 +0100 | [diff] [blame] | 765 | *this = IteratorImpl(nullptr, nullptr, ScopedStmt(), 0, |
| 766 | util::ErrStatus("Trace processor was deleted"), 0); |
Lalit Maganti | aac2f65 | 2019-04-30 12:16:21 +0100 | [diff] [blame] | 767 | } |
| 768 | |
| 769 | void TraceProcessor::IteratorImpl::RecordFirstNextInSqlStats() { |
| 770 | base::TimeNanos t_first_next = base::GetWallTimeNs(); |
| 771 | auto* sql_stats = trace_processor_->context_.storage->mutable_sql_stats(); |
| 772 | sql_stats->RecordQueryFirstNext(sql_stats_row_, t_first_next.count()); |
Lalit Maganti | 7709501 | 2019-02-19 13:13:59 +0000 | [diff] [blame] | 773 | } |
| 774 | |
Ioannis Ilkos | eff38f5 | 2018-10-29 10:37:55 +0000 | [diff] [blame] | 775 | } // namespace trace_processor |
| 776 | } // namespace perfetto |