Lalit Maganti | 11d851c | 2018-06-12 13:22:17 +0100 | [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 | |
Ioannis Ilkos | eff38f5 | 2018-10-29 10:37:55 +0000 | [diff] [blame] | 17 | #ifndef SRC_TRACE_PROCESSOR_TRACE_PROCESSOR_IMPL_H_ |
| 18 | #define SRC_TRACE_PROCESSOR_TRACE_PROCESSOR_IMPL_H_ |
Lalit Maganti | 11d851c | 2018-06-12 13:22:17 +0100 | [diff] [blame] | 19 | |
Primiano Tucci | 02c1176 | 2019-08-30 00:57:59 +0200 | [diff] [blame] | 20 | #include <sqlite3.h> |
| 21 | |
Primiano Tucci | 2464714 | 2018-08-13 21:18:24 +0200 | [diff] [blame] | 22 | #include <atomic> |
Primiano Tucci | 5968caf | 2018-08-06 10:31:46 +0100 | [diff] [blame] | 23 | #include <functional> |
Primiano Tucci | 11a79e7 | 2019-10-29 22:13:10 +0100 | [diff] [blame] | 24 | #include <string> |
Lalit Maganti | 7709501 | 2019-02-19 13:13:59 +0000 | [diff] [blame] | 25 | #include <vector> |
Lalit Maganti | 11d851c | 2018-06-12 13:22:17 +0100 | [diff] [blame] | 26 | |
Primiano Tucci | 2c5488f | 2019-06-01 03:27:28 +0100 | [diff] [blame] | 27 | #include "perfetto/ext/base/string_view.h" |
Ioannis Ilkos | eff38f5 | 2018-10-29 10:37:55 +0000 | [diff] [blame] | 28 | #include "perfetto/trace_processor/basic_types.h" |
Primiano Tucci | 2c5488f | 2019-06-01 03:27:28 +0100 | [diff] [blame] | 29 | #include "perfetto/trace_processor/status.h" |
Ioannis Ilkos | f837129 | 2018-11-05 16:47:35 +0000 | [diff] [blame] | 30 | #include "perfetto/trace_processor/trace_processor.h" |
Lalit Maganti | 65800eb | 2020-01-30 15:18:59 +0000 | [diff] [blame] | 31 | #include "src/trace_processor/sqlite/db_sqlite_table.h" |
| 32 | #include "src/trace_processor/sqlite/query_cache.h" |
Lalit Maganti | 03d1062 | 2019-08-27 14:47:39 +0100 | [diff] [blame] | 33 | #include "src/trace_processor/sqlite/scoped_db.h" |
Eric Seckler | a7870e6 | 2019-11-01 10:11:58 +0000 | [diff] [blame] | 34 | #include "src/trace_processor/trace_processor_storage_impl.h" |
Lalit Maganti | 11d851c | 2018-06-12 13:22:17 +0100 | [diff] [blame] | 35 | |
Eric Seckler | c622b8b | 2019-10-21 09:54:00 +0100 | [diff] [blame] | 36 | #include "src/trace_processor/metrics/metrics.h" |
Lalit Maganti | 3d9bbff | 2020-04-15 13:40:58 +0100 | [diff] [blame] | 37 | #include "src/trace_processor/util/descriptors.h" |
Eric Seckler | c622b8b | 2019-10-21 09:54:00 +0100 | [diff] [blame] | 38 | |
Lalit Maganti | 11d851c | 2018-06-12 13:22:17 +0100 | [diff] [blame] | 39 | namespace perfetto { |
| 40 | namespace trace_processor { |
| 41 | |
Primiano Tucci | 7e33029 | 2018-08-24 19:10:52 +0200 | [diff] [blame] | 42 | // Coordinates the loading of traces from an arbitrary source and allows |
Lalit Maganti | 11d851c | 2018-06-12 13:22:17 +0100 | [diff] [blame] | 43 | // execution of SQL queries on the events in these traces. |
Eric Seckler | a7870e6 | 2019-11-01 10:11:58 +0000 | [diff] [blame] | 44 | class TraceProcessorImpl : public TraceProcessor, |
| 45 | public TraceProcessorStorageImpl { |
Lalit Maganti | 11d851c | 2018-06-12 13:22:17 +0100 | [diff] [blame] | 46 | public: |
Ioannis Ilkos | eff38f5 | 2018-10-29 10:37:55 +0000 | [diff] [blame] | 47 | explicit TraceProcessorImpl(const Config&); |
Lalit Maganti | 11d851c | 2018-06-12 13:22:17 +0100 | [diff] [blame] | 48 | |
Ioannis Ilkos | f837129 | 2018-11-05 16:47:35 +0000 | [diff] [blame] | 49 | ~TraceProcessorImpl() override; |
Lalit Maganti | 11d851c | 2018-06-12 13:22:17 +0100 | [diff] [blame] | 50 | |
Eric Seckler | a7870e6 | 2019-11-01 10:11:58 +0000 | [diff] [blame] | 51 | // TraceProcessorStorage implementation: |
Lalit Maganti | 1f06718 | 2019-05-09 14:50:05 +0100 | [diff] [blame] | 52 | util::Status Parse(std::unique_ptr<uint8_t[]>, size_t) override; |
Ioannis Ilkos | f837129 | 2018-11-05 16:47:35 +0000 | [diff] [blame] | 53 | void NotifyEndOfFile() override; |
Lalit Maganti | 11d851c | 2018-06-12 13:22:17 +0100 | [diff] [blame] | 54 | |
Eric Seckler | a7870e6 | 2019-11-01 10:11:58 +0000 | [diff] [blame] | 55 | // TraceProcessor implementation: |
Lalit Maganti | aac2f65 | 2019-04-30 12:16:21 +0100 | [diff] [blame] | 56 | Iterator ExecuteQuery(const std::string& sql, |
| 57 | int64_t time_queued = 0) override; |
Lalit Maganti | 7709501 | 2019-02-19 13:13:59 +0000 | [diff] [blame] | 58 | |
Lalit Maganti | 5f8727d | 2019-05-24 13:38:50 +0100 | [diff] [blame] | 59 | util::Status RegisterMetric(const std::string& path, |
| 60 | const std::string& sql) override; |
| 61 | |
| 62 | util::Status ExtendMetricsProto(const uint8_t* data, size_t size) override; |
| 63 | |
Lalit Maganti | d71a945 | 2019-05-09 15:13:24 +0100 | [diff] [blame] | 64 | util::Status ComputeMetric(const std::vector<std::string>& metric_names, |
| 65 | std::vector<uint8_t>* metrics) override; |
Lalit Maganti | d9f86b6 | 2019-04-08 11:11:51 +0100 | [diff] [blame] | 66 | |
Ioannis Ilkos | f837129 | 2018-11-05 16:47:35 +0000 | [diff] [blame] | 67 | void InterruptQuery() override; |
Primiano Tucci | 2464714 | 2018-08-13 21:18:24 +0200 | [diff] [blame] | 68 | |
Primiano Tucci | 11a79e7 | 2019-10-29 22:13:10 +0100 | [diff] [blame] | 69 | size_t RestoreInitialTables() override; |
| 70 | |
Primiano Tucci | ee2ce1d | 2019-11-01 19:14:17 +0100 | [diff] [blame] | 71 | std::string GetCurrentTraceName() override; |
| 72 | void SetCurrentTraceName(const std::string&) override; |
| 73 | |
Lalit Maganti | 427b833 | 2020-05-27 16:09:45 +0100 | [diff] [blame] | 74 | void EnableMetatrace() override; |
| 75 | |
| 76 | util::Status DisableAndReadMetatrace( |
| 77 | std::vector<uint8_t>* trace_proto) override; |
| 78 | |
Lalit Maganti | 11d851c | 2018-06-12 13:22:17 +0100 | [diff] [blame] | 79 | private: |
Lalit Maganti | 7709501 | 2019-02-19 13:13:59 +0000 | [diff] [blame] | 80 | // Needed for iterators to be able to delete themselves from the vector. |
| 81 | friend class IteratorImpl; |
| 82 | |
Lalit Maganti | 65800eb | 2020-01-30 15:18:59 +0000 | [diff] [blame] | 83 | template <typename Table> |
| 84 | void RegisterDbTable(const Table& table) { |
Lalit Maganti | bdbf2d1 | 2020-02-12 14:54:16 +0000 | [diff] [blame] | 85 | DbSqliteTable::RegisterTable(*db_, query_cache_.get(), Table::Schema(), |
| 86 | &table, table.table_name()); |
Lalit Maganti | 65800eb | 2020-01-30 15:18:59 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Lalit Maganti | 920f4f9 | 2020-02-19 13:45:05 +0000 | [diff] [blame] | 89 | void RegisterDynamicTable( |
| 90 | std::unique_ptr<DbSqliteTable::DynamicTableGenerator> generator) { |
| 91 | DbSqliteTable::RegisterTable(*db_, query_cache_.get(), |
| 92 | std::move(generator)); |
| 93 | } |
| 94 | |
Eric Seckler | a7870e6 | 2019-11-01 10:11:58 +0000 | [diff] [blame] | 95 | ScopedDb db_; |
Lalit Maganti | 65800eb | 2020-01-30 15:18:59 +0000 | [diff] [blame] | 96 | std::unique_ptr<QueryCache> query_cache_; |
Primiano Tucci | 2464714 | 2018-08-13 21:18:24 +0200 | [diff] [blame] | 97 | |
Stephen Nusko | bc76a6c | 2019-12-03 11:55:27 +0000 | [diff] [blame] | 98 | DescriptorPool pool_; |
Lalit Maganti | 2553b80 | 2019-05-24 13:25:58 +0100 | [diff] [blame] | 99 | std::vector<metrics::SqlMetricFile> sql_metrics_; |
| 100 | |
Lalit Maganti | 7709501 | 2019-02-19 13:13:59 +0000 | [diff] [blame] | 101 | std::vector<IteratorImpl*> iterators_; |
| 102 | |
Primiano Tucci | 2464714 | 2018-08-13 21:18:24 +0200 | [diff] [blame] | 103 | // This is atomic because it is set by the CTRL-C signal handler and we need |
| 104 | // to prevent single-flow compiler optimizations in ExecuteQuery(). |
| 105 | std::atomic<bool> query_interrupted_{false}; |
Primiano Tucci | 11a79e7 | 2019-10-29 22:13:10 +0100 | [diff] [blame] | 106 | |
| 107 | // Keeps track of the tables created by the ingestion process. This is used |
| 108 | // by RestoreInitialTables() to delete all the tables/view that have been |
| 109 | // created after that point. |
| 110 | std::vector<std::string> initial_tables_; |
Primiano Tucci | ee2ce1d | 2019-11-01 19:14:17 +0100 | [diff] [blame] | 111 | |
| 112 | std::string current_trace_name_; |
| 113 | uint64_t bytes_parsed_ = 0; |
Lalit Maganti | 11d851c | 2018-06-12 13:22:17 +0100 | [diff] [blame] | 114 | }; |
Lalit Maganti | 7709501 | 2019-02-19 13:13:59 +0000 | [diff] [blame] | 115 | |
Lalit Maganti | 7709501 | 2019-02-19 13:13:59 +0000 | [diff] [blame] | 116 | |
Lalit Maganti | 11d851c | 2018-06-12 13:22:17 +0100 | [diff] [blame] | 117 | } // namespace trace_processor |
| 118 | } // namespace perfetto |
| 119 | |
Ioannis Ilkos | eff38f5 | 2018-10-29 10:37:55 +0000 | [diff] [blame] | 120 | #endif // SRC_TRACE_PROCESSOR_TRACE_PROCESSOR_IMPL_H_ |