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 // TraceProcessorStorage implementation: 60 base::Status Parse(TraceBlobView) override; 61 void Flush() override; 62 void NotifyEndOfFile() override; 63 64 // TraceProcessor implementation: 65 Iterator ExecuteQuery(const std::string& sql) override; 66 67 base::Status RegisterMetric(const std::string& path, 68 const std::string& sql) override; 69 70 base::Status RegisterSqlModule(SqlModule sql_module) override; 71 72 base::Status ExtendMetricsProto(const uint8_t* data, size_t size) override; 73 74 base::Status ExtendMetricsProto( 75 const uint8_t* data, 76 size_t size, 77 const std::vector<std::string>& skip_prefixes) override; 78 79 base::Status ComputeMetric(const std::vector<std::string>& metric_names, 80 std::vector<uint8_t>* metrics) override; 81 82 base::Status ComputeMetricText(const std::vector<std::string>& metric_names, 83 TraceProcessor::MetricResultFormat format, 84 std::string* metrics_string) override; 85 86 std::vector<uint8_t> GetMetricDescriptors() override; 87 88 void InterruptQuery() override; 89 90 size_t RestoreInitialTables() override; 91 92 std::string GetCurrentTraceName() override; 93 void SetCurrentTraceName(const std::string&) override; 94 95 void EnableMetatrace(MetatraceConfig config) override; 96 97 base::Status DisableAndReadMetatrace( 98 std::vector<uint8_t>* trace_proto) override; 99 100 private: 101 // Needed for iterators to be able to access the context. 102 friend class IteratorImpl; 103 104 template <typename Table> RegisterStaticTable(const Table & table)105 void RegisterStaticTable(const Table& table) { 106 engine_->RegisterStaticTable(table, Table::Name(), 107 Table::ComputeStaticSchema()); 108 } 109 110 bool IsRootMetricField(const std::string& metric_name); 111 112 void InitPerfettoSqlEngine(); 113 114 const Config config_; 115 std::unique_ptr<PerfettoSqlEngine> engine_; 116 117 DescriptorPool pool_; 118 119 std::vector<metrics::SqlMetricFile> sql_metrics_; 120 std::unordered_map<std::string, std::string> proto_field_to_sql_metric_path_; 121 122 // This is atomic because it is set by the CTRL-C signal handler and we need 123 // to prevent single-flow compiler optimizations in ExecuteQuery(). 124 std::atomic<bool> query_interrupted_{false}; 125 126 // Track the number of objects registered with SQLite after the constructor. 127 uint64_t sqlite_objects_post_constructor_initialization_ = 0; 128 129 std::string current_trace_name_; 130 uint64_t bytes_parsed_ = 0; 131 132 // NotifyEndOfFile should only be called once. Set to true whenever it is 133 // called. 134 bool notify_eof_called_ = false; 135 }; 136 137 } // namespace perfetto::trace_processor 138 139 #endif // SRC_TRACE_PROCESSOR_TRACE_PROCESSOR_IMPL_H_ 140