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 #ifndef SRC_TRACE_PROCESSOR_TRACE_PROCESSOR_IMPL_H_ 18 #define SRC_TRACE_PROCESSOR_TRACE_PROCESSOR_IMPL_H_ 19 20 #include <sqlite3.h> 21 22 #include <atomic> 23 #include <cstddef> 24 #include <cstdint> 25 #include <memory> 26 #include <string> 27 #include <unordered_map> 28 #include <vector> 29 30 #include "perfetto/base/status.h" 31 #include "perfetto/trace_processor/basic_types.h" 32 #include "perfetto/trace_processor/trace_blob_view.h" 33 #include "perfetto/trace_processor/trace_processor.h" 34 #include "src/trace_processor/iterator_impl.h" 35 #include "src/trace_processor/metrics/metrics.h" 36 #include "src/trace_processor/perfetto_sql/engine/perfetto_sql_engine.h" 37 #include "src/trace_processor/perfetto_sql/intrinsics/functions/create_function.h" 38 #include "src/trace_processor/perfetto_sql/intrinsics/functions/create_view_function.h" 39 #include "src/trace_processor/trace_processor_storage_impl.h" 40 #include "src/trace_processor/util/descriptors.h" 41 42 namespace perfetto::trace_processor { 43 44 // Coordinates the loading of traces from an arbitrary source and allows 45 // execution of SQL queries on the events in these traces. 46 class TraceProcessorImpl : public TraceProcessor, 47 public TraceProcessorStorageImpl { 48 public: 49 explicit TraceProcessorImpl(const Config&); 50 51 TraceProcessorImpl(const TraceProcessorImpl&) = delete; 52 TraceProcessorImpl& operator=(const TraceProcessorImpl&) = delete; 53 54 TraceProcessorImpl(TraceProcessorImpl&&) = delete; 55 TraceProcessorImpl& operator=(TraceProcessorImpl&&) = delete; 56 57 ~TraceProcessorImpl() override; 58 59 // ================================================================= 60 // | TraceProcessorStorage implementation starts here | 61 // ================================================================= 62 63 base::Status Parse(TraceBlobView) override; 64 void Flush() override; 65 base::Status NotifyEndOfFile() override; 66 67 // ================================================================= 68 // | PerfettoSQL related functionality starts here | 69 // ================================================================= 70 71 Iterator ExecuteQuery(const std::string& sql) override; 72 73 base::Status RegisterSqlPackage(SqlPackage) override; 74 75 base::Status RegisterSqlModule(SqlModule module) override; 76 77 // ================================================================= 78 // | Trace-based metrics (v2) related functionality starts here | 79 // ================================================================= 80 81 base::Status Summarize(const TraceSummaryComputationSpec& computation, 82 const std::vector<TraceSummarySpecBytes>& specs, 83 std::vector<uint8_t>* output, 84 const TraceSummaryOutputSpec& output_spec) override; 85 86 // ================================================================= 87 // | Metatracing related functionality starts here | 88 // ================================================================= 89 90 void EnableMetatrace(MetatraceConfig config) override; 91 92 base::Status DisableAndReadMetatrace( 93 std::vector<uint8_t>* trace_proto) override; 94 95 // ================================================================= 96 // | Advanced functionality starts here | 97 // ================================================================= 98 99 std::string GetCurrentTraceName() override; 100 void SetCurrentTraceName(const std::string&) override; 101 102 base::Status RegisterFileContent(const std::string& path, 103 TraceBlobView content) override; 104 105 void InterruptQuery() override; 106 107 size_t RestoreInitialTables() override; 108 109 // ================================================================= 110 // | Trace-based metrics (v1) related functionality starts here | 111 // ================================================================= 112 113 base::Status RegisterMetric(const std::string& path, 114 const std::string& sql) override; 115 116 base::Status ExtendMetricsProto(const uint8_t* data, size_t size) override; 117 base::Status ExtendMetricsProto( 118 const uint8_t* data, 119 size_t size, 120 const std::vector<std::string>& skip_prefixes) override; 121 122 base::Status ComputeMetric(const std::vector<std::string>& metric_names, 123 std::vector<uint8_t>* metrics) override; 124 base::Status ComputeMetricText(const std::vector<std::string>& metric_names, 125 TraceProcessor::MetricResultFormat format, 126 std::string* metrics_string) override; 127 128 std::vector<uint8_t> GetMetricDescriptors() override; 129 130 // =================== 131 // | Experimental | 132 // =================== 133 134 base::Status AnalyzeStructuredQueries( 135 const std::vector<StructuredQueryBytes>&, 136 std::vector<AnalyzedStructuredQuery>*) override; 137 138 private: 139 // Needed for iterators to be able to access the context. 140 friend class IteratorImpl; 141 142 template <typename Table> RegisterStaticTable(Table * table)143 void RegisterStaticTable(Table* table) { 144 engine_->RegisterStaticTable(table, Table::Name(), 145 Table::ComputeStaticSchema()); 146 } 147 148 bool IsRootMetricField(const std::string& metric_name); 149 150 void InitPerfettoSqlEngine(); 151 void IncludeBeforeEofPrelude(); 152 void IncludeAfterEofPrelude(); 153 154 const Config config_; 155 std::unique_ptr<PerfettoSqlEngine> engine_; 156 157 DescriptorPool metrics_descriptor_pool_; 158 159 std::vector<metrics::SqlMetricFile> sql_metrics_; 160 161 // Manually registeres SQL packages are stored here, to be able to restore 162 // them when running |RestoreInitialTables()|. 163 std::vector<SqlPackage> manually_registered_sql_packages_; 164 165 std::unordered_map<std::string, std::string> proto_field_to_sql_metric_path_; 166 std::unordered_map<std::string, std::string> proto_fn_name_to_path_; 167 168 // This is atomic because it is set by the CTRL-C signal handler and we need 169 // to prevent single-flow compiler optimizations in ExecuteQuery(). 170 std::atomic<bool> query_interrupted_{false}; 171 172 // Track the number of objects registered with SQLite post prelude. 173 uint64_t sqlite_objects_post_prelude_ = 0; 174 175 std::string current_trace_name_; 176 uint64_t bytes_parsed_ = 0; 177 178 // NotifyEndOfFile should only be called once. Set to true whenever it is 179 // called. 180 bool notify_eof_called_ = false; 181 }; 182 183 } // namespace perfetto::trace_processor 184 185 #endif // SRC_TRACE_PROCESSOR_TRACE_PROCESSOR_IMPL_H_ 186