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> |
Mikhail Khokhlov | a8d310d | 2019-05-07 17:34:21 +0100 | [diff] [blame] | 21 | #include <fstream> |
Ioannis Ilkos | eff38f5 | 2018-10-29 10:37:55 +0000 | [diff] [blame] | 22 | #include <functional> |
| 23 | |
Lalit Maganti | 7709501 | 2019-02-19 13:13:59 +0000 | [diff] [blame] | 24 | #include "perfetto/base/logging.h" |
Eric Seckler | 83dcc8c | 2019-08-21 12:18:43 +0100 | [diff] [blame] | 25 | #include "perfetto/base/time.h" |
Primiano Tucci | 2c5488f | 2019-06-01 03:27:28 +0100 | [diff] [blame] | 26 | #include "perfetto/ext/base/string_splitter.h" |
| 27 | #include "perfetto/ext/base/string_utils.h" |
Lalit Maganti | 26f69bd | 2019-04-29 18:23:47 +0100 | [diff] [blame] | 28 | #include "perfetto/protozero/scattered_heap_buffer.h" |
Primiano Tucci | 2c761ef | 2019-01-07 20:20:46 +0000 | [diff] [blame] | 29 | #include "src/trace_processor/android_logs_table.h" |
Lalit Maganti | 6e9c55e | 2018-11-29 12:00:39 +0000 | [diff] [blame] | 30 | #include "src/trace_processor/args_table.h" |
Lalit Maganti | 4fa7c6c | 2019-02-06 15:06:36 +0000 | [diff] [blame] | 31 | #include "src/trace_processor/args_tracker.h" |
Primiano Tucci | a270f01 | 2019-01-07 20:01:00 +0000 | [diff] [blame] | 32 | #include "src/trace_processor/clock_tracker.h" |
Lalit Maganti | 8320e6d | 2019-03-14 18:49:33 +0000 | [diff] [blame] | 33 | #include "src/trace_processor/counter_definitions_table.h" |
| 34 | #include "src/trace_processor/counter_values_table.h" |
Oystein Eftevaag | 7f64c10 | 2019-08-29 10:27:31 -0700 | [diff] [blame] | 35 | #include "src/trace_processor/cpu_profile_stack_sample_table.h" |
Ioannis Ilkos | eff38f5 | 2018-10-29 10:37:55 +0000 | [diff] [blame] | 36 | #include "src/trace_processor/event_tracker.h" |
Hector Dearman | d8c3d5c | 2019-08-06 13:28:23 +0100 | [diff] [blame] | 37 | #include "src/trace_processor/forwarding_trace_parser.h" |
Florian Mayer | 25e013e | 2019-10-01 14:06:15 +0100 | [diff] [blame] | 38 | #include "src/trace_processor/heap_graph_tracker.h" |
Lalit Maganti | 9b2d52b | 2019-05-07 14:32:15 +0100 | [diff] [blame] | 39 | #include "src/trace_processor/heap_profile_allocation_table.h" |
Florian Mayer | 438b5ab | 2019-05-02 11:18:06 +0100 | [diff] [blame] | 40 | #include "src/trace_processor/heap_profile_tracker.h" |
Eric Seckler | acfe35b | 2019-10-18 15:50:17 +0100 | [diff] [blame] | 41 | #include "src/trace_processor/importers/ftrace/ftrace_module.h" |
Eric Seckler | d379513 | 2019-10-21 16:43:10 +0100 | [diff] [blame] | 42 | #include "src/trace_processor/importers/ftrace/sched_event_tracker.h" |
Eric Seckler | ab0604c | 2019-10-25 10:12:07 +0100 | [diff] [blame] | 43 | #include "src/trace_processor/importers/proto/android_probes_module.h" |
Eric Seckler | 63afd79 | 2019-10-23 15:05:05 +0100 | [diff] [blame] | 44 | #include "src/trace_processor/importers/proto/graphics_event_module.h" |
Eric Seckler | 87ede88 | 2019-10-18 10:48:36 +0100 | [diff] [blame] | 45 | #include "src/trace_processor/importers/proto/proto_importer_module.h" |
Eric Seckler | 137a467 | 2019-10-24 08:51:14 +0100 | [diff] [blame] | 46 | #include "src/trace_processor/importers/proto/proto_trace_tokenizer.h" |
Eric Seckler | ab0604c | 2019-10-25 10:12:07 +0100 | [diff] [blame] | 47 | #include "src/trace_processor/importers/proto/system_probes_module.h" |
Eric Seckler | 87ede88 | 2019-10-18 10:48:36 +0100 | [diff] [blame] | 48 | #include "src/trace_processor/importers/proto/track_event_module.h" |
Eric Seckler | d8b5208 | 2019-10-17 15:58:38 +0100 | [diff] [blame] | 49 | #include "src/trace_processor/importers/systrace/systrace_parser.h" |
| 50 | #include "src/trace_processor/importers/systrace/systrace_trace_parser.h" |
Isabelle Taylor | c8c1120 | 2018-11-05 11:36:22 +0000 | [diff] [blame] | 51 | #include "src/trace_processor/instants_table.h" |
Mikhail Khokhlov | e466c00 | 2019-05-23 13:33:33 +0100 | [diff] [blame] | 52 | #include "src/trace_processor/metadata_table.h" |
Ioannis Ilkos | eff38f5 | 2018-10-29 10:37:55 +0000 | [diff] [blame] | 53 | #include "src/trace_processor/process_table.h" |
| 54 | #include "src/trace_processor/process_tracker.h" |
Isabelle Taylor | d3f590e | 2019-01-08 17:21:23 +0000 | [diff] [blame] | 55 | #include "src/trace_processor/raw_table.h" |
Ioannis Ilkos | eff38f5 | 2018-10-29 10:37:55 +0000 | [diff] [blame] | 56 | #include "src/trace_processor/sched_slice_table.h" |
| 57 | #include "src/trace_processor/slice_table.h" |
| 58 | #include "src/trace_processor/slice_tracker.h" |
Lalit Maganti | 95693bb | 2018-11-05 16:20:24 +0000 | [diff] [blame] | 59 | #include "src/trace_processor/span_join_operator_table.h" |
Primiano Tucci | 5cb84f8 | 2018-10-31 21:46:36 -0700 | [diff] [blame] | 60 | #include "src/trace_processor/sql_stats_table.h" |
Lalit Maganti | 4069711 | 2019-09-04 22:06:42 +0100 | [diff] [blame] | 61 | #include "src/trace_processor/sqlite/db_sqlite_table.h" |
Lalit Maganti | 03d1062 | 2019-08-27 14:47:39 +0100 | [diff] [blame] | 62 | #include "src/trace_processor/sqlite/sqlite3_str_split.h" |
Lalit Maganti | 737b376 | 2019-08-26 13:46:37 -0700 | [diff] [blame] | 63 | #include "src/trace_processor/sqlite/sqlite_table.h" |
Oystein Eftevaag | 5419c58 | 2019-08-21 13:58:49 -0700 | [diff] [blame] | 64 | #include "src/trace_processor/stack_profile_callsite_table.h" |
| 65 | #include "src/trace_processor/stack_profile_frame_table.h" |
| 66 | #include "src/trace_processor/stack_profile_mapping_table.h" |
| 67 | #include "src/trace_processor/stack_profile_tracker.h" |
Lalit Maganti | 05e8c13 | 2018-11-09 18:16:12 +0000 | [diff] [blame] | 68 | #include "src/trace_processor/stats_table.h" |
Hector Dearman | f31a703 | 2019-03-29 10:05:09 +0000 | [diff] [blame] | 69 | #include "src/trace_processor/syscall_tracker.h" |
Ioannis Ilkos | eff38f5 | 2018-10-29 10:37:55 +0000 | [diff] [blame] | 70 | #include "src/trace_processor/thread_table.h" |
Deepanjan Roy | 01994ca | 2019-04-02 11:05:34 -0700 | [diff] [blame] | 71 | #include "src/trace_processor/trace_blob_view.h" |
Ioannis Ilkos | eff38f5 | 2018-10-29 10:37:55 +0000 | [diff] [blame] | 72 | #include "src/trace_processor/trace_sorter.h" |
Lalit Maganti | 4ea78d9 | 2019-09-20 20:20:41 +0100 | [diff] [blame] | 73 | #include "src/trace_processor/track_tracker.h" |
Mohammad Reza Zakerinasab | b06d185 | 2019-09-11 14:41:36 -0400 | [diff] [blame] | 74 | #include "src/trace_processor/vulkan_memory_tracker.h" |
Ioannis Ilkos | eff38f5 | 2018-10-29 10:37:55 +0000 | [diff] [blame] | 75 | #include "src/trace_processor/window_operator_table.h" |
| 76 | |
Eric Seckler | c622b8b | 2019-10-21 09:54:00 +0100 | [diff] [blame] | 77 | #if PERFETTO_BUILDFLAG(PERFETTO_TP_METRICS) |
| 78 | #include "src/trace_processor/metrics/descriptors.h" |
| 79 | #include "src/trace_processor/metrics/metrics.descriptor.h" |
| 80 | #include "src/trace_processor/metrics/metrics.h" |
| 81 | #include "src/trace_processor/metrics/sql_metrics.h" |
| 82 | |
Primiano Tucci | 355b8c8 | 2019-08-29 08:37:51 +0200 | [diff] [blame] | 83 | #include "protos/perfetto/metrics/android/mem_metric.pbzero.h" |
| 84 | #include "protos/perfetto/metrics/metrics.pbzero.h" |
Eric Seckler | c622b8b | 2019-10-21 09:54:00 +0100 | [diff] [blame] | 85 | #endif // PERFETTO_BUILDFLAG(PERFETTO_TP_METRICS) |
Lalit Maganti | 26f69bd | 2019-04-29 18:23:47 +0100 | [diff] [blame] | 86 | |
Primiano Tucci | 02c1176 | 2019-08-30 00:57:59 +0200 | [diff] [blame] | 87 | #if PERFETTO_BUILDFLAG(PERFETTO_TP_JSON) |
Mikhail Khokhlov | a8d310d | 2019-05-07 17:34:21 +0100 | [diff] [blame] | 88 | #include "src/trace_processor/export_json.h" |
Lalit Maganti | 17aa273 | 2019-02-08 15:47:26 +0000 | [diff] [blame] | 89 | #endif |
| 90 | |
Mikhail Khokhlov | 8643d1c | 2019-06-04 12:02:47 +0100 | [diff] [blame] | 91 | // 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] | 92 | // Just don't include it. |
Primiano Tucci | 02c1176 | 2019-08-30 00:57:59 +0200 | [diff] [blame] | 93 | #if PERFETTO_BUILDFLAG(PERFETTO_TP_PERCENTILE) |
Ioannis Ilkos | 178535e | 2018-11-05 17:32:45 +0000 | [diff] [blame] | 94 | // defined in sqlite_src/ext/misc/percentile.c |
| 95 | extern "C" int sqlite3_percentile_init(sqlite3* db, |
| 96 | char** error, |
| 97 | const sqlite3_api_routines* api); |
Lalit Maganti | 17aa273 | 2019-02-08 15:47:26 +0000 | [diff] [blame] | 98 | #endif |
Ioannis Ilkos | 178535e | 2018-11-05 17:32:45 +0000 | [diff] [blame] | 99 | |
Ioannis Ilkos | bf54ebb | 2019-03-08 13:26:14 +0000 | [diff] [blame] | 100 | namespace perfetto { |
| 101 | namespace trace_processor { |
Ioannis Ilkos | 178535e | 2018-11-05 17:32:45 +0000 | [diff] [blame] | 102 | namespace { |
Ioannis Ilkos | bf54ebb | 2019-03-08 13:26:14 +0000 | [diff] [blame] | 103 | |
Ioannis Ilkos | 7199d55 | 2019-03-13 18:35:31 +0000 | [diff] [blame] | 104 | void InitializeSqlite(sqlite3* db) { |
| 105 | char* error = nullptr; |
| 106 | sqlite3_exec(db, "PRAGMA temp_store=2", 0, 0, &error); |
| 107 | if (error) { |
| 108 | PERFETTO_FATAL("Error setting pragma temp_store: %s", error); |
| 109 | } |
Ioannis Ilkos | bf54ebb | 2019-03-08 13:26:14 +0000 | [diff] [blame] | 110 | sqlite3_str_split_init(db); |
Lalit Maganti | 17aa273 | 2019-02-08 15:47:26 +0000 | [diff] [blame] | 111 | // In Android tree builds, we don't have the percentile module. |
| 112 | // Just don't include it. |
Primiano Tucci | 02c1176 | 2019-08-30 00:57:59 +0200 | [diff] [blame] | 113 | #if PERFETTO_BUILDFLAG(PERFETTO_TP_PERCENTILE) |
Ioannis Ilkos | 178535e | 2018-11-05 17:32:45 +0000 | [diff] [blame] | 114 | sqlite3_percentile_init(db, &error, nullptr); |
Sami Kyostila | 3366894 | 2018-11-13 16:33:32 +0000 | [diff] [blame] | 115 | if (error) { |
Ioannis Ilkos | 178535e | 2018-11-05 17:32:45 +0000 | [diff] [blame] | 116 | PERFETTO_ELOG("Error initializing: %s", error); |
Sami Kyostila | 3366894 | 2018-11-13 16:33:32 +0000 | [diff] [blame] | 117 | sqlite3_free(error); |
| 118 | } |
Lalit Maganti | 17aa273 | 2019-02-08 15:47:26 +0000 | [diff] [blame] | 119 | #endif |
Sami Kyostila | 3366894 | 2018-11-13 16:33:32 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Lalit Maganti | 086295b | 2019-04-04 16:59:19 +0100 | [diff] [blame] | 122 | void BuildBoundsTable(sqlite3* db, std::pair<int64_t, int64_t> bounds) { |
| 123 | char* error = nullptr; |
| 124 | sqlite3_exec(db, "DELETE FROM trace_bounds", nullptr, nullptr, &error); |
| 125 | if (error) { |
| 126 | PERFETTO_ELOG("Error deleting from bounds table: %s", error); |
| 127 | sqlite3_free(error); |
| 128 | return; |
| 129 | } |
| 130 | |
| 131 | char* insert_sql = sqlite3_mprintf("INSERT INTO trace_bounds VALUES(%" PRId64 |
| 132 | ", %" PRId64 ")", |
| 133 | bounds.first, bounds.second); |
| 134 | |
| 135 | sqlite3_exec(db, insert_sql, 0, 0, &error); |
| 136 | sqlite3_free(insert_sql); |
| 137 | if (error) { |
| 138 | PERFETTO_ELOG("Error inserting bounds table: %s", error); |
| 139 | sqlite3_free(error); |
| 140 | } |
| 141 | } |
| 142 | |
Sami Kyostila | 3366894 | 2018-11-13 16:33:32 +0000 | [diff] [blame] | 143 | void CreateBuiltinTables(sqlite3* db) { |
| 144 | char* error = nullptr; |
| 145 | sqlite3_exec(db, "CREATE TABLE perfetto_tables(name STRING)", 0, 0, &error); |
| 146 | if (error) { |
| 147 | PERFETTO_ELOG("Error initializing: %s", error); |
| 148 | sqlite3_free(error); |
Ioannis Ilkos | 178535e | 2018-11-05 17:32:45 +0000 | [diff] [blame] | 149 | } |
Ioannis Ilkos | b8b1110 | 2019-01-29 17:56:55 +0000 | [diff] [blame] | 150 | sqlite3_exec(db, |
| 151 | "CREATE TABLE trace_bounds(start_ts BIG INT, end_ts BIG INT)", 0, |
| 152 | 0, &error); |
| 153 | if (error) { |
| 154 | PERFETTO_ELOG("Error initializing: %s", error); |
| 155 | sqlite3_free(error); |
| 156 | } |
Ioannis Ilkos | b8b1110 | 2019-01-29 17:56:55 +0000 | [diff] [blame] | 157 | |
Lalit Maganti | 086295b | 2019-04-04 16:59:19 +0100 | [diff] [blame] | 158 | // Initialize the bounds table with some data so even before parsing any data, |
| 159 | // we still have a valid table. |
| 160 | BuildBoundsTable(db, std::make_pair(0, 0)); |
Ioannis Ilkos | 178535e | 2018-11-05 17:32:45 +0000 | [diff] [blame] | 161 | } |
Hector Dearman | c0a2909 | 2018-11-08 17:34:23 +0000 | [diff] [blame] | 162 | |
Lalit Maganti | 8320e6d | 2019-03-14 18:49:33 +0000 | [diff] [blame] | 163 | void CreateBuiltinViews(sqlite3* db) { |
| 164 | char* error = nullptr; |
| 165 | sqlite3_exec(db, |
| 166 | "CREATE VIEW counters AS " |
| 167 | "SELECT * FROM counter_values " |
Lalit Maganti | 6fbb0ad | 2019-10-25 14:42:28 +0100 | [diff] [blame^] | 168 | "INNER JOIN counter_definitions USING(counter_id) " |
| 169 | "ORDER BY ts;", |
Lalit Maganti | 8320e6d | 2019-03-14 18:49:33 +0000 | [diff] [blame] | 170 | 0, 0, &error); |
| 171 | if (error) { |
| 172 | PERFETTO_ELOG("Error initializing: %s", error); |
| 173 | sqlite3_free(error); |
| 174 | } |
Eric Seckler | 972225e | 2019-04-18 11:07:12 +0100 | [diff] [blame] | 175 | |
| 176 | sqlite3_exec(db, |
| 177 | "CREATE VIEW slice AS " |
| 178 | "SELECT " |
| 179 | " *, " |
Lalit Maganti | 81a02cd | 2019-07-12 17:05:11 +0100 | [diff] [blame] | 180 | " category as cat, " |
Eric Seckler | 972225e | 2019-04-18 11:07:12 +0100 | [diff] [blame] | 181 | " CASE ref_type " |
| 182 | " WHEN 'utid' THEN ref " |
| 183 | " ELSE NULL " |
| 184 | " END AS utid " |
| 185 | "FROM internal_slice;", |
| 186 | 0, 0, &error); |
| 187 | if (error) { |
| 188 | PERFETTO_ELOG("Error initializing: %s", error); |
| 189 | sqlite3_free(error); |
| 190 | } |
| 191 | |
Lalit Maganti | 24b0833 | 2019-10-04 12:53:08 +0100 | [diff] [blame] | 192 | sqlite3_exec(db, |
| 193 | "CREATE VIEW gpu_slice AS " |
| 194 | "SELECT " |
| 195 | "* " |
| 196 | "FROM internal_gpu_slice join internal_slice using(slice_id);", |
| 197 | 0, 0, &error); |
| 198 | if (error) { |
| 199 | PERFETTO_ELOG("Error initializing: %s", error); |
| 200 | sqlite3_free(error); |
| 201 | } |
| 202 | |
Eric Seckler | 972225e | 2019-04-18 11:07:12 +0100 | [diff] [blame] | 203 | // Legacy view for "slice" table with a deprecated table name. |
| 204 | // TODO(eseckler): Remove this view when all users have switched to "slice". |
| 205 | sqlite3_exec(db, |
| 206 | "CREATE VIEW slices AS " |
| 207 | "SELECT * FROM slice;", |
| 208 | 0, 0, &error); |
| 209 | if (error) { |
| 210 | PERFETTO_ELOG("Error initializing: %s", error); |
| 211 | sqlite3_free(error); |
| 212 | } |
Lalit Maganti | 8320e6d | 2019-03-14 18:49:33 +0000 | [diff] [blame] | 213 | } |
| 214 | |
Primiano Tucci | 02c1176 | 2019-08-30 00:57:59 +0200 | [diff] [blame] | 215 | #if PERFETTO_BUILDFLAG(PERFETTO_TP_JSON) |
Mikhail Khokhlov | a8d310d | 2019-05-07 17:34:21 +0100 | [diff] [blame] | 216 | void ExportJson(sqlite3_context* ctx, int /*argc*/, sqlite3_value** argv) { |
| 217 | TraceStorage* storage = static_cast<TraceStorage*>(sqlite3_user_data(ctx)); |
Eric Seckler | 37ad6af | 2019-10-08 15:51:14 +0100 | [diff] [blame] | 218 | FILE* output; |
| 219 | if (sqlite3_value_type(argv[0]) == SQLITE_INTEGER) { |
| 220 | // Assume input is an FD. |
| 221 | output = fdopen(sqlite3_value_int(argv[0]), "w"); |
| 222 | if (!output) { |
| 223 | sqlite3_result_error(ctx, "Couldn't open output file from given FD", -1); |
| 224 | return; |
| 225 | } |
| 226 | } else { |
| 227 | const char* filename = |
| 228 | reinterpret_cast<const char*>(sqlite3_value_text(argv[0])); |
| 229 | output = fopen(filename, "w"); |
| 230 | if (!output) { |
| 231 | sqlite3_result_error(ctx, "Couldn't open output file", -1); |
| 232 | return; |
| 233 | } |
Mikhail Khokhlov | a8d310d | 2019-05-07 17:34:21 +0100 | [diff] [blame] | 234 | } |
| 235 | |
Eric Seckler | 7fb1d23 | 2019-10-23 14:42:43 +0100 | [diff] [blame] | 236 | util::Status result = json::ExportJson(storage, output); |
| 237 | if (!result.ok()) { |
| 238 | sqlite3_result_error(ctx, result.message().c_str(), -1); |
| 239 | return; |
Mikhail Khokhlov | a8d310d | 2019-05-07 17:34:21 +0100 | [diff] [blame] | 240 | } |
| 241 | } |
| 242 | |
| 243 | void CreateJsonExportFunction(TraceStorage* ts, sqlite3* db) { |
| 244 | auto ret = sqlite3_create_function_v2(db, "EXPORT_JSON", 1, SQLITE_UTF8, ts, |
| 245 | ExportJson, nullptr, nullptr, |
| 246 | sqlite_utils::kSqliteStatic); |
| 247 | if (ret) { |
| 248 | PERFETTO_ELOG("Error initializing EXPORT_JSON"); |
| 249 | } |
| 250 | } |
| 251 | #endif |
| 252 | |
Lalit Maganti | 062c8f8 | 2019-10-03 20:11:29 +0100 | [diff] [blame] | 253 | void Hash(sqlite3_context* ctx, int argc, sqlite3_value** argv) { |
| 254 | base::Hash hash; |
| 255 | for (int i = 0; i < argc; ++i) { |
| 256 | sqlite3_value* value = argv[i]; |
| 257 | switch (sqlite3_value_type(value)) { |
| 258 | case SQLITE_INTEGER: |
| 259 | hash.Update(sqlite3_value_int64(value)); |
| 260 | break; |
| 261 | case SQLITE_TEXT: { |
| 262 | const char* ptr = |
| 263 | reinterpret_cast<const char*>(sqlite3_value_text(value)); |
| 264 | hash.Update(ptr, strlen(ptr)); |
| 265 | break; |
| 266 | } |
| 267 | default: |
| 268 | sqlite3_result_error(ctx, "Unsupported type of arg passed to HASH", -1); |
| 269 | return; |
| 270 | } |
| 271 | } |
| 272 | sqlite3_result_int64(ctx, static_cast<int64_t>(hash.digest())); |
| 273 | } |
| 274 | |
| 275 | void CreateHashFunction(sqlite3* db) { |
| 276 | auto ret = sqlite3_create_function_v2( |
| 277 | db, "HASH", -1, SQLITE_UTF8 | SQLITE_DETERMINISTIC, nullptr, &Hash, |
| 278 | nullptr, nullptr, nullptr); |
| 279 | if (ret) { |
| 280 | PERFETTO_ELOG("Error initializing HASH"); |
| 281 | } |
| 282 | } |
| 283 | |
Eric Seckler | c622b8b | 2019-10-21 09:54:00 +0100 | [diff] [blame] | 284 | #if PERFETTO_BUILDFLAG(PERFETTO_TP_METRICS) |
Lalit Maganti | 2553b80 | 2019-05-24 13:25:58 +0100 | [diff] [blame] | 285 | void SetupMetrics(TraceProcessor* tp, |
| 286 | sqlite3* db, |
Lalit Maganti | 2553b80 | 2019-05-24 13:25:58 +0100 | [diff] [blame] | 287 | std::vector<metrics::SqlMetricFile>* sql_metrics) { |
Lalit Maganti | 5f8727d | 2019-05-24 13:38:50 +0100 | [diff] [blame] | 288 | tp->ExtendMetricsProto(kMetricsDescriptor.data(), kMetricsDescriptor.size()); |
| 289 | |
Lalit Maganti | 2553b80 | 2019-05-24 13:25:58 +0100 | [diff] [blame] | 290 | for (const auto& file_to_sql : metrics::sql_metrics::kFileToSql) { |
Lalit Maganti | 5f8727d | 2019-05-24 13:38:50 +0100 | [diff] [blame] | 291 | tp->RegisterMetric(file_to_sql.path, file_to_sql.sql); |
Lalit Maganti | 2553b80 | 2019-05-24 13:25:58 +0100 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | { |
| 295 | std::unique_ptr<metrics::RunMetricContext> ctx( |
| 296 | new metrics::RunMetricContext()); |
| 297 | ctx->tp = tp; |
| 298 | ctx->metrics = sql_metrics; |
| 299 | auto ret = sqlite3_create_function_v2( |
| 300 | db, "RUN_METRIC", -1, SQLITE_UTF8, ctx.release(), metrics::RunMetric, |
| 301 | nullptr, nullptr, |
| 302 | [](void* ptr) { delete static_cast<metrics::RunMetricContext*>(ptr); }); |
| 303 | if (ret) |
| 304 | PERFETTO_ELOG("Error initializing RUN_METRIC"); |
| 305 | } |
| 306 | |
| 307 | { |
| 308 | auto ret = sqlite3_create_function_v2( |
| 309 | db, "RepeatedField", 1, SQLITE_UTF8, nullptr, nullptr, |
| 310 | metrics::RepeatedFieldStep, metrics::RepeatedFieldFinal, nullptr); |
| 311 | if (ret) |
| 312 | PERFETTO_ELOG("Error initializing RepeatedField"); |
| 313 | } |
| 314 | } |
Eric Seckler | c622b8b | 2019-10-21 09:54:00 +0100 | [diff] [blame] | 315 | #endif // PERFETTO_BUILDFLAG(PERFETTO_TP_METRICS) |
Lalit Maganti | 2553b80 | 2019-05-24 13:25:58 +0100 | [diff] [blame] | 316 | |
Hector Dearman | c0a2909 | 2018-11-08 17:34:23 +0000 | [diff] [blame] | 317 | } // namespace |
| 318 | |
Lalit Maganti | 295a861 | 2019-05-21 13:57:42 +0100 | [diff] [blame] | 319 | TraceProcessorImpl::TraceProcessorImpl(const Config& cfg) { |
Ioannis Ilkos | eff38f5 | 2018-10-29 10:37:55 +0000 | [diff] [blame] | 320 | sqlite3* db = nullptr; |
Mikhail Khokhlov | 4a91128 | 2019-05-31 14:25:07 +0100 | [diff] [blame] | 321 | PERFETTO_CHECK(sqlite3_initialize() == SQLITE_OK); |
Ioannis Ilkos | eff38f5 | 2018-10-29 10:37:55 +0000 | [diff] [blame] | 322 | PERFETTO_CHECK(sqlite3_open(":memory:", &db) == SQLITE_OK); |
Ioannis Ilkos | 7199d55 | 2019-03-13 18:35:31 +0000 | [diff] [blame] | 323 | InitializeSqlite(db); |
Sami Kyostila | 3366894 | 2018-11-13 16:33:32 +0000 | [diff] [blame] | 324 | CreateBuiltinTables(db); |
Lalit Maganti | 8320e6d | 2019-03-14 18:49:33 +0000 | [diff] [blame] | 325 | CreateBuiltinViews(db); |
Ioannis Ilkos | eff38f5 | 2018-10-29 10:37:55 +0000 | [diff] [blame] | 326 | db_.reset(std::move(db)); |
| 327 | |
Lalit Maganti | 295a861 | 2019-05-21 13:57:42 +0100 | [diff] [blame] | 328 | context_.config = cfg; |
Ioannis Ilkos | eff38f5 | 2018-10-29 10:37:55 +0000 | [diff] [blame] | 329 | context_.storage.reset(new TraceStorage()); |
Lalit Maganti | 4ea78d9 | 2019-09-20 20:20:41 +0100 | [diff] [blame] | 330 | context_.track_tracker.reset(new TrackTracker(&context_)); |
Lalit Maganti | 4fa7c6c | 2019-02-06 15:06:36 +0000 | [diff] [blame] | 331 | context_.args_tracker.reset(new ArgsTracker(&context_)); |
Ioannis Ilkos | eff38f5 | 2018-10-29 10:37:55 +0000 | [diff] [blame] | 332 | context_.slice_tracker.reset(new SliceTracker(&context_)); |
| 333 | context_.event_tracker.reset(new EventTracker(&context_)); |
Ioannis Ilkos | eff38f5 | 2018-10-29 10:37:55 +0000 | [diff] [blame] | 334 | context_.process_tracker.reset(new ProcessTracker(&context_)); |
Hector Dearman | f31a703 | 2019-03-29 10:05:09 +0000 | [diff] [blame] | 335 | context_.syscall_tracker.reset(new SyscallTracker(&context_)); |
Primiano Tucci | a270f01 | 2019-01-07 20:01:00 +0000 | [diff] [blame] | 336 | context_.clock_tracker.reset(new ClockTracker(&context_)); |
Florian Mayer | 438b5ab | 2019-05-02 11:18:06 +0100 | [diff] [blame] | 337 | context_.heap_profile_tracker.reset(new HeapProfileTracker(&context_)); |
Florian Mayer | 25e013e | 2019-10-01 14:06:15 +0100 | [diff] [blame] | 338 | context_.heap_graph_tracker.reset(new HeapGraphTracker(&context_)); |
Eric Seckler | ac2ea61 | 2019-10-21 16:44:39 +0100 | [diff] [blame] | 339 | #if PERFETTO_BUILDFLAG(PERFETTO_TP_FTRACE) |
| 340 | context_.sched_tracker.reset(new SchedEventTracker(&context_)); |
Lalit Maganti | d33dc27 | 2019-06-06 14:51:45 +0100 | [diff] [blame] | 341 | context_.systrace_parser.reset(new SystraceParser(&context_)); |
Eric Seckler | ac2ea61 | 2019-10-21 16:44:39 +0100 | [diff] [blame] | 342 | #endif // PERFETTO_BUILDFLAG(PERFETTO_TP_FTRACE) |
Mohammad Reza Zakerinasab | b06d185 | 2019-09-11 14:41:36 -0400 | [diff] [blame] | 343 | context_.vulkan_memory_tracker.reset(new VulkanMemoryTracker(&context_)); |
Eric Seckler | 87ede88 | 2019-10-18 10:48:36 +0100 | [diff] [blame] | 344 | context_.ftrace_module.reset( |
| 345 | new ProtoImporterModule<FtraceModule>(&context_)); |
| 346 | context_.track_event_module.reset( |
| 347 | new ProtoImporterModule<TrackEventModule>(&context_)); |
Eric Seckler | ab0604c | 2019-10-25 10:12:07 +0100 | [diff] [blame] | 348 | context_.systrace_module.reset( |
| 349 | new ProtoImporterModule<SystraceProtoModule>(&context_)); |
| 350 | context_.android_probes_module.reset( |
| 351 | new ProtoImporterModule<AndroidProbesModule>(&context_)); |
Eric Seckler | 63afd79 | 2019-10-23 15:05:05 +0100 | [diff] [blame] | 352 | context_.graphics_event_module.reset( |
| 353 | new ProtoImporterModule<GraphicsEventModule>(&context_)); |
Ioannis Ilkos | eff38f5 | 2018-10-29 10:37:55 +0000 | [diff] [blame] | 354 | |
Primiano Tucci | 02c1176 | 2019-08-30 00:57:59 +0200 | [diff] [blame] | 355 | #if PERFETTO_BUILDFLAG(PERFETTO_TP_JSON) |
Mikhail Khokhlov | a8d310d | 2019-05-07 17:34:21 +0100 | [diff] [blame] | 356 | CreateJsonExportFunction(this->context_.storage.get(), db); |
| 357 | #endif |
Lalit Maganti | 062c8f8 | 2019-10-03 20:11:29 +0100 | [diff] [blame] | 358 | CreateHashFunction(db); |
Mikhail Khokhlov | a8d310d | 2019-05-07 17:34:21 +0100 | [diff] [blame] | 359 | |
Eric Seckler | c622b8b | 2019-10-21 09:54:00 +0100 | [diff] [blame] | 360 | #if PERFETTO_BUILDFLAG(PERFETTO_TP_METRICS) |
Lalit Maganti | 5f8727d | 2019-05-24 13:38:50 +0100 | [diff] [blame] | 361 | SetupMetrics(this, *db_, &sql_metrics_); |
Eric Seckler | c622b8b | 2019-10-21 09:54:00 +0100 | [diff] [blame] | 362 | #endif // PERFETTO_BUILDFLAG(PERFETTO_TP_METRICS) |
Lalit Maganti | 2553b80 | 2019-05-24 13:25:58 +0100 | [diff] [blame] | 363 | |
Lalit Maganti | 6e9c55e | 2018-11-29 12:00:39 +0000 | [diff] [blame] | 364 | ArgsTable::RegisterTable(*db_, context_.storage.get()); |
Ioannis Ilkos | eff38f5 | 2018-10-29 10:37:55 +0000 | [diff] [blame] | 365 | ProcessTable::RegisterTable(*db_, context_.storage.get()); |
Eric Seckler | ac2ea61 | 2019-10-21 16:44:39 +0100 | [diff] [blame] | 366 | #if PERFETTO_BUILDFLAG(PERFETTO_TP_FTRACE) |
Ioannis Ilkos | eff38f5 | 2018-10-29 10:37:55 +0000 | [diff] [blame] | 367 | SchedSliceTable::RegisterTable(*db_, context_.storage.get()); |
Eric Seckler | ac2ea61 | 2019-10-21 16:44:39 +0100 | [diff] [blame] | 368 | #endif // PERFETTO_BUILDFLAG(PERFETTO_TP_FTRACE) |
Ioannis Ilkos | eff38f5 | 2018-10-29 10:37:55 +0000 | [diff] [blame] | 369 | SliceTable::RegisterTable(*db_, context_.storage.get()); |
Primiano Tucci | 5cb84f8 | 2018-10-31 21:46:36 -0700 | [diff] [blame] | 370 | SqlStatsTable::RegisterTable(*db_, context_.storage.get()); |
Ioannis Ilkos | eff38f5 | 2018-10-29 10:37:55 +0000 | [diff] [blame] | 371 | ThreadTable::RegisterTable(*db_, context_.storage.get()); |
Lalit Maganti | 8320e6d | 2019-03-14 18:49:33 +0000 | [diff] [blame] | 372 | CounterDefinitionsTable::RegisterTable(*db_, context_.storage.get()); |
| 373 | CounterValuesTable::RegisterTable(*db_, context_.storage.get()); |
Lalit Maganti | 95693bb | 2018-11-05 16:20:24 +0000 | [diff] [blame] | 374 | SpanJoinOperatorTable::RegisterTable(*db_, context_.storage.get()); |
Ioannis Ilkos | eff38f5 | 2018-10-29 10:37:55 +0000 | [diff] [blame] | 375 | WindowOperatorTable::RegisterTable(*db_, context_.storage.get()); |
Isabelle Taylor | c8c1120 | 2018-11-05 11:36:22 +0000 | [diff] [blame] | 376 | InstantsTable::RegisterTable(*db_, context_.storage.get()); |
Lalit Maganti | 05e8c13 | 2018-11-09 18:16:12 +0000 | [diff] [blame] | 377 | StatsTable::RegisterTable(*db_, context_.storage.get()); |
Primiano Tucci | 2c761ef | 2019-01-07 20:20:46 +0000 | [diff] [blame] | 378 | AndroidLogsTable::RegisterTable(*db_, context_.storage.get()); |
Isabelle Taylor | d3f590e | 2019-01-08 17:21:23 +0000 | [diff] [blame] | 379 | RawTable::RegisterTable(*db_, context_.storage.get()); |
Lalit Maganti | 9b2d52b | 2019-05-07 14:32:15 +0100 | [diff] [blame] | 380 | HeapProfileAllocationTable::RegisterTable(*db_, context_.storage.get()); |
Oystein Eftevaag | 7f64c10 | 2019-08-29 10:27:31 -0700 | [diff] [blame] | 381 | CpuProfileStackSampleTable::RegisterTable(*db_, context_.storage.get()); |
Oystein Eftevaag | 5419c58 | 2019-08-21 13:58:49 -0700 | [diff] [blame] | 382 | StackProfileCallsiteTable::RegisterTable(*db_, context_.storage.get()); |
| 383 | StackProfileFrameTable::RegisterTable(*db_, context_.storage.get()); |
| 384 | StackProfileMappingTable::RegisterTable(*db_, context_.storage.get()); |
Mikhail Khokhlov | e466c00 | 2019-05-23 13:33:33 +0100 | [diff] [blame] | 385 | MetadataTable::RegisterTable(*db_, context_.storage.get()); |
Lalit Maganti | 4069711 | 2019-09-04 22:06:42 +0100 | [diff] [blame] | 386 | |
| 387 | // New style db-backed tables. |
Lalit Maganti | 786cc92 | 2019-09-06 16:04:32 +0100 | [diff] [blame] | 388 | const TraceStorage* storage = context_.storage.get(); |
Lalit Maganti | ac68e9c | 2019-09-09 18:12:15 +0100 | [diff] [blame] | 389 | DbSqliteTable::RegisterTable(*db_, &storage->track_table(), |
| 390 | storage->track_table().table_name()); |
Lalit Maganti | 21b2163 | 2019-09-28 16:50:23 +0100 | [diff] [blame] | 391 | DbSqliteTable::RegisterTable(*db_, &storage->thread_track_table(), |
| 392 | storage->thread_track_table().table_name()); |
Lalit Maganti | 53bdbb2 | 2019-09-25 11:11:18 +0100 | [diff] [blame] | 393 | DbSqliteTable::RegisterTable(*db_, &storage->process_track_table(), |
| 394 | storage->process_track_table().table_name()); |
Lalit Maganti | 786cc92 | 2019-09-06 16:04:32 +0100 | [diff] [blame] | 395 | DbSqliteTable::RegisterTable(*db_, &storage->gpu_slice_table(), |
Lalit Maganti | ac68e9c | 2019-09-09 18:12:15 +0100 | [diff] [blame] | 396 | storage->gpu_slice_table().table_name()); |
Lalit Maganti | 786cc92 | 2019-09-06 16:04:32 +0100 | [diff] [blame] | 397 | DbSqliteTable::RegisterTable(*db_, &storage->gpu_track_table(), |
Lalit Maganti | ac68e9c | 2019-09-09 18:12:15 +0100 | [diff] [blame] | 398 | storage->gpu_track_table().table_name()); |
Ioannis Ilkos | e0b47f5 | 2019-09-18 11:14:57 +0100 | [diff] [blame] | 399 | DbSqliteTable::RegisterTable(*db_, &storage->symbol_table(), |
| 400 | storage->symbol_table().table_name()); |
Florian Mayer | 25e013e | 2019-10-01 14:06:15 +0100 | [diff] [blame] | 401 | DbSqliteTable::RegisterTable(*db_, &storage->heap_graph_object_table(), |
| 402 | storage->heap_graph_object_table().table_name()); |
Mohammad Reza Zakerinasab | b06d185 | 2019-09-11 14:41:36 -0400 | [diff] [blame] | 403 | DbSqliteTable::RegisterTable( |
Florian Mayer | 9c01d16 | 2019-10-09 17:55:32 +0100 | [diff] [blame] | 404 | *db_, &storage->heap_graph_reference_table(), |
| 405 | storage->heap_graph_reference_table().table_name()); |
| 406 | DbSqliteTable::RegisterTable( |
Mohammad Reza Zakerinasab | b06d185 | 2019-09-11 14:41:36 -0400 | [diff] [blame] | 407 | *db_, &storage->vulkan_memory_allocations_table(), |
| 408 | storage->vulkan_memory_allocations_table().table_name()); |
Ioannis Ilkos | eff38f5 | 2018-10-29 10:37:55 +0000 | [diff] [blame] | 409 | } |
| 410 | |
Lalit Maganti | 7709501 | 2019-02-19 13:13:59 +0000 | [diff] [blame] | 411 | TraceProcessorImpl::~TraceProcessorImpl() { |
| 412 | for (auto* it : iterators_) |
| 413 | it->Reset(); |
| 414 | } |
Ioannis Ilkos | eff38f5 | 2018-10-29 10:37:55 +0000 | [diff] [blame] | 415 | |
Lalit Maganti | 1f06718 | 2019-05-09 14:50:05 +0100 | [diff] [blame] | 416 | util::Status TraceProcessorImpl::Parse(std::unique_ptr<uint8_t[]> data, |
| 417 | size_t size) { |
Ioannis Ilkos | eff38f5 | 2018-10-29 10:37:55 +0000 | [diff] [blame] | 418 | if (size == 0) |
Lalit Maganti | 1f06718 | 2019-05-09 14:50:05 +0100 | [diff] [blame] | 419 | return util::OkStatus(); |
Ioannis Ilkos | eff38f5 | 2018-10-29 10:37:55 +0000 | [diff] [blame] | 420 | if (unrecoverable_parse_error_) |
Lalit Maganti | 1f06718 | 2019-05-09 14:50:05 +0100 | [diff] [blame] | 421 | return util::ErrStatus( |
| 422 | "Failed unrecoverably while parsing in a previous Parse call"); |
Hector Dearman | d8c3d5c | 2019-08-06 13:28:23 +0100 | [diff] [blame] | 423 | if (!context_.chunk_reader) |
| 424 | context_.chunk_reader.reset(new ForwardingTraceParser(&context_)); |
Ioannis Ilkos | eff38f5 | 2018-10-29 10:37:55 +0000 | [diff] [blame] | 425 | |
Eric Seckler | 77b5278 | 2019-05-02 15:18:57 +0100 | [diff] [blame] | 426 | auto scoped_trace = context_.storage->TraceExecutionTimeIntoStats( |
| 427 | stats::parse_trace_duration_ns); |
Lalit Maganti | d71a945 | 2019-05-09 15:13:24 +0100 | [diff] [blame] | 428 | util::Status status = context_.chunk_reader->Parse(std::move(data), size); |
| 429 | unrecoverable_parse_error_ |= !status.ok(); |
| 430 | return status; |
Ioannis Ilkos | eff38f5 | 2018-10-29 10:37:55 +0000 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | void TraceProcessorImpl::NotifyEndOfFile() { |
Deepanjan Roy | 01994ca | 2019-04-02 11:05:34 -0700 | [diff] [blame] | 434 | if (unrecoverable_parse_error_ || !context_.chunk_reader) |
| 435 | return; |
| 436 | |
Lalit Maganti | d54d752 | 2019-05-30 14:36:08 +0100 | [diff] [blame] | 437 | if (context_.sorter) |
| 438 | context_.sorter->ExtractEventsForced(); |
Eric Seckler | ac2ea61 | 2019-10-21 16:44:39 +0100 | [diff] [blame] | 439 | #if PERFETTO_BUILDFLAG(PERFETTO_TP_FTRACE) |
Eric Seckler | d379513 | 2019-10-21 16:43:10 +0100 | [diff] [blame] | 440 | context_.sched_tracker->FlushPendingEvents(); |
Eric Seckler | ac2ea61 | 2019-10-21 16:44:39 +0100 | [diff] [blame] | 441 | #endif // PERFETTO_BUILDFLAG(PERFETTO_TP_FTRACE) |
Lalit Maganti | 521d97b | 2019-04-29 13:47:03 +0100 | [diff] [blame] | 442 | context_.event_tracker->FlushPendingEvents(); |
Eric Seckler | 87fd11a | 2019-06-07 15:52:28 +0100 | [diff] [blame] | 443 | context_.slice_tracker->FlushPendingSlices(); |
Ioannis Ilkos | b8b1110 | 2019-01-29 17:56:55 +0000 | [diff] [blame] | 444 | BuildBoundsTable(*db_, context_.storage->GetTraceTimestampBoundsNs()); |
Ioannis Ilkos | eff38f5 | 2018-10-29 10:37:55 +0000 | [diff] [blame] | 445 | } |
| 446 | |
Lalit Maganti | 7709501 | 2019-02-19 13:13:59 +0000 | [diff] [blame] | 447 | TraceProcessor::Iterator TraceProcessorImpl::ExecuteQuery( |
Lalit Maganti | aac2f65 | 2019-04-30 12:16:21 +0100 | [diff] [blame] | 448 | const std::string& sql, |
| 449 | int64_t time_queued) { |
Lalit Maganti | 7709501 | 2019-02-19 13:13:59 +0000 | [diff] [blame] | 450 | sqlite3_stmt* raw_stmt; |
Lalit Maganti | 73b1a0a | 2019-04-08 13:51:14 +0100 | [diff] [blame] | 451 | 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] | 452 | &raw_stmt, nullptr); |
Lalit Maganti | 1f06718 | 2019-05-09 14:50:05 +0100 | [diff] [blame] | 453 | util::Status status; |
Lalit Maganti | 06acc02 | 2019-04-03 16:02:15 +0100 | [diff] [blame] | 454 | uint32_t col_count = 0; |
| 455 | if (err != SQLITE_OK) { |
Lalit Maganti | 7ee6abb | 2019-05-09 17:57:32 +0100 | [diff] [blame] | 456 | status = util::ErrStatus("%s", sqlite3_errmsg(*db_)); |
Lalit Maganti | 7709501 | 2019-02-19 13:13:59 +0000 | [diff] [blame] | 457 | } else { |
| 458 | col_count = static_cast<uint32_t>(sqlite3_column_count(raw_stmt)); |
| 459 | } |
Lalit Maganti | aac2f65 | 2019-04-30 12:16:21 +0100 | [diff] [blame] | 460 | |
| 461 | base::TimeNanos t_start = base::GetWallTimeNs(); |
| 462 | uint32_t sql_stats_row = |
| 463 | context_.storage->mutable_sql_stats()->RecordQueryBegin(sql, time_queued, |
| 464 | t_start.count()); |
| 465 | |
| 466 | std::unique_ptr<IteratorImpl> impl(new IteratorImpl( |
Lalit Maganti | 1f06718 | 2019-05-09 14:50:05 +0100 | [diff] [blame] | 467 | this, *db_, ScopedStmt(raw_stmt), col_count, status, sql_stats_row)); |
Lalit Maganti | 7709501 | 2019-02-19 13:13:59 +0000 | [diff] [blame] | 468 | iterators_.emplace_back(impl.get()); |
| 469 | return TraceProcessor::Iterator(std::move(impl)); |
| 470 | } |
| 471 | |
Ioannis Ilkos | eff38f5 | 2018-10-29 10:37:55 +0000 | [diff] [blame] | 472 | void TraceProcessorImpl::InterruptQuery() { |
| 473 | if (!db_) |
| 474 | return; |
| 475 | query_interrupted_.store(true); |
| 476 | sqlite3_interrupt(db_.get()); |
| 477 | } |
| 478 | |
Eric Seckler | c622b8b | 2019-10-21 09:54:00 +0100 | [diff] [blame] | 479 | #if PERFETTO_BUILDFLAG(PERFETTO_TP_METRICS) |
Lalit Maganti | 5f8727d | 2019-05-24 13:38:50 +0100 | [diff] [blame] | 480 | util::Status TraceProcessorImpl::RegisterMetric(const std::string& path, |
| 481 | const std::string& sql) { |
Lalit Maganti | f9d8030 | 2019-06-19 12:48:25 +0100 | [diff] [blame] | 482 | std::string stripped_sql; |
| 483 | for (base::StringSplitter sp(sql, '\n'); sp.Next();) { |
| 484 | if (strncmp(sp.cur_token(), "--", 2) != 0) { |
| 485 | stripped_sql.append(sp.cur_token()); |
| 486 | stripped_sql.push_back('\n'); |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | // Check if the metric with the given path already exists and if it does, just |
| 491 | // update the SQL associated with it. |
| 492 | auto it = std::find_if( |
| 493 | sql_metrics_.begin(), sql_metrics_.end(), |
| 494 | [&path](const metrics::SqlMetricFile& m) { return m.path == path; }); |
| 495 | if (it != sql_metrics_.end()) { |
| 496 | it->sql = stripped_sql; |
| 497 | return util::OkStatus(); |
| 498 | } |
| 499 | |
Lalit Maganti | 5f8727d | 2019-05-24 13:38:50 +0100 | [diff] [blame] | 500 | auto sep_idx = path.rfind("/"); |
| 501 | std::string basename = |
| 502 | sep_idx == std::string::npos ? path : path.substr(sep_idx + 1); |
| 503 | |
| 504 | auto sql_idx = basename.rfind(".sql"); |
| 505 | if (sql_idx == std::string::npos) { |
| 506 | return util::ErrStatus("Unable to find .sql extension for metric"); |
| 507 | } |
| 508 | auto no_ext_name = basename.substr(0, sql_idx); |
| 509 | |
| 510 | metrics::SqlMetricFile metric; |
| 511 | metric.path = path; |
| 512 | metric.proto_field_name = no_ext_name; |
| 513 | metric.output_table_name = no_ext_name + "_output"; |
Lalit Maganti | ca4d514 | 2019-05-28 13:25:47 +0100 | [diff] [blame] | 514 | metric.sql = stripped_sql; |
Lalit Maganti | 5f8727d | 2019-05-24 13:38:50 +0100 | [diff] [blame] | 515 | sql_metrics_.emplace_back(metric); |
| 516 | return util::OkStatus(); |
| 517 | } |
| 518 | |
| 519 | util::Status TraceProcessorImpl::ExtendMetricsProto(const uint8_t* data, |
| 520 | size_t size) { |
Lalit Maganti | 996f544 | 2019-06-21 16:12:58 +0100 | [diff] [blame] | 521 | util::Status status = pool_.AddFromFileDescriptorSet(data, size); |
| 522 | if (!status.ok()) |
| 523 | return status; |
Lalit Maganti | 5f8727d | 2019-05-24 13:38:50 +0100 | [diff] [blame] | 524 | |
Lalit Maganti | 2553b80 | 2019-05-24 13:25:58 +0100 | [diff] [blame] | 525 | for (const auto& desc : pool_.descriptors()) { |
Lalit Maganti | 6221107 | 2019-05-10 14:09:58 +0100 | [diff] [blame] | 526 | // Convert the full name (e.g. .perfetto.protos.TraceMetrics.SubMetric) |
| 527 | // into a function name of the form (TraceMetrics_SubMetric). |
| 528 | auto fn_name = desc.full_name().substr(desc.package_name().size() + 1); |
| 529 | std::replace(fn_name.begin(), fn_name.end(), '.', '_'); |
| 530 | |
Lalit Maganti | b182d7e | 2019-05-10 14:13:06 +0100 | [diff] [blame] | 531 | std::unique_ptr<metrics::BuildProtoContext> ctx( |
| 532 | new metrics::BuildProtoContext()); |
| 533 | ctx->tp = this; |
Lalit Maganti | 2553b80 | 2019-05-24 13:25:58 +0100 | [diff] [blame] | 534 | ctx->pool = &pool_; |
Lalit Maganti | b182d7e | 2019-05-10 14:13:06 +0100 | [diff] [blame] | 535 | ctx->desc = &desc; |
| 536 | |
Lalit Maganti | 6221107 | 2019-05-10 14:09:58 +0100 | [diff] [blame] | 537 | auto ret = sqlite3_create_function_v2( |
Lalit Maganti | b182d7e | 2019-05-10 14:13:06 +0100 | [diff] [blame] | 538 | *db_, fn_name.c_str(), -1, SQLITE_UTF8, ctx.release(), |
| 539 | metrics::BuildProto, nullptr, nullptr, [](void* ptr) { |
| 540 | delete static_cast<metrics::BuildProtoContext*>(ptr); |
| 541 | }); |
Lalit Maganti | 6221107 | 2019-05-10 14:09:58 +0100 | [diff] [blame] | 542 | if (ret != SQLITE_OK) |
| 543 | return util::ErrStatus("%s", sqlite3_errmsg(*db_)); |
| 544 | } |
Lalit Maganti | 996f544 | 2019-06-21 16:12:58 +0100 | [diff] [blame] | 545 | return util::OkStatus(); |
| 546 | } |
Lalit Maganti | 031ad2b | 2019-05-14 17:37:14 +0100 | [diff] [blame] | 547 | |
Lalit Maganti | 996f544 | 2019-06-21 16:12:58 +0100 | [diff] [blame] | 548 | util::Status TraceProcessorImpl::ComputeMetric( |
| 549 | const std::vector<std::string>& metric_names, |
| 550 | std::vector<uint8_t>* metrics_proto) { |
Lalit Maganti | 2553b80 | 2019-05-24 13:25:58 +0100 | [diff] [blame] | 551 | auto opt_idx = pool_.FindDescriptorIdx(".perfetto.protos.TraceMetrics"); |
Lalit Maganti | 031ad2b | 2019-05-14 17:37:14 +0100 | [diff] [blame] | 552 | if (!opt_idx.has_value()) |
| 553 | return util::Status("Root metrics proto descriptor not found"); |
| 554 | |
Lalit Maganti | 2553b80 | 2019-05-24 13:25:58 +0100 | [diff] [blame] | 555 | const auto& root_descriptor = pool_.descriptors()[opt_idx.value()]; |
| 556 | return metrics::ComputeMetrics(this, metric_names, sql_metrics_, |
| 557 | root_descriptor, metrics_proto); |
Lalit Maganti | d9f86b6 | 2019-04-08 11:11:51 +0100 | [diff] [blame] | 558 | } |
Eric Seckler | c622b8b | 2019-10-21 09:54:00 +0100 | [diff] [blame] | 559 | #endif // PERFETTO_BUILDFLAG(PERFETTO_TP_METRICS) |
Lalit Maganti | d9f86b6 | 2019-04-08 11:11:51 +0100 | [diff] [blame] | 560 | |
Lalit Maganti | 7709501 | 2019-02-19 13:13:59 +0000 | [diff] [blame] | 561 | TraceProcessor::IteratorImpl::IteratorImpl(TraceProcessorImpl* trace_processor, |
| 562 | sqlite3* db, |
| 563 | ScopedStmt stmt, |
| 564 | uint32_t column_count, |
Lalit Maganti | 1f06718 | 2019-05-09 14:50:05 +0100 | [diff] [blame] | 565 | util::Status status, |
Lalit Maganti | aac2f65 | 2019-04-30 12:16:21 +0100 | [diff] [blame] | 566 | uint32_t sql_stats_row) |
Lalit Maganti | 7709501 | 2019-02-19 13:13:59 +0000 | [diff] [blame] | 567 | : trace_processor_(trace_processor), |
| 568 | db_(db), |
| 569 | stmt_(std::move(stmt)), |
| 570 | column_count_(column_count), |
Lalit Maganti | 1f06718 | 2019-05-09 14:50:05 +0100 | [diff] [blame] | 571 | status_(status), |
Lalit Maganti | aac2f65 | 2019-04-30 12:16:21 +0100 | [diff] [blame] | 572 | sql_stats_row_(sql_stats_row) {} |
Lalit Maganti | 7709501 | 2019-02-19 13:13:59 +0000 | [diff] [blame] | 573 | |
| 574 | TraceProcessor::IteratorImpl::~IteratorImpl() { |
| 575 | if (trace_processor_) { |
| 576 | auto* its = &trace_processor_->iterators_; |
| 577 | auto it = std::find(its->begin(), its->end(), this); |
| 578 | PERFETTO_CHECK(it != its->end()); |
| 579 | its->erase(it); |
Lalit Maganti | aac2f65 | 2019-04-30 12:16:21 +0100 | [diff] [blame] | 580 | |
| 581 | base::TimeNanos t_end = base::GetWallTimeNs(); |
| 582 | auto* sql_stats = trace_processor_->context_.storage->mutable_sql_stats(); |
| 583 | sql_stats->RecordQueryEnd(sql_stats_row_, t_end.count()); |
Lalit Maganti | 7709501 | 2019-02-19 13:13:59 +0000 | [diff] [blame] | 584 | } |
| 585 | } |
| 586 | |
| 587 | void TraceProcessor::IteratorImpl::Reset() { |
Lalit Maganti | 1f06718 | 2019-05-09 14:50:05 +0100 | [diff] [blame] | 588 | *this = IteratorImpl(nullptr, nullptr, ScopedStmt(), 0, |
| 589 | util::ErrStatus("Trace processor was deleted"), 0); |
Lalit Maganti | aac2f65 | 2019-04-30 12:16:21 +0100 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | void TraceProcessor::IteratorImpl::RecordFirstNextInSqlStats() { |
| 593 | base::TimeNanos t_first_next = base::GetWallTimeNs(); |
| 594 | auto* sql_stats = trace_processor_->context_.storage->mutable_sql_stats(); |
| 595 | sql_stats->RecordQueryFirstNext(sql_stats_row_, t_first_next.count()); |
Lalit Maganti | 7709501 | 2019-02-19 13:13:59 +0000 | [diff] [blame] | 596 | } |
| 597 | |
Ioannis Ilkos | eff38f5 | 2018-10-29 10:37:55 +0000 | [diff] [blame] | 598 | } // namespace trace_processor |
| 599 | } // namespace perfetto |