• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <functional>
24 #include <map>
25 #include <string>
26 #include <vector>
27 
28 #include "perfetto/ext/base/string_view.h"
29 #include "perfetto/trace_processor/basic_types.h"
30 #include "perfetto/trace_processor/status.h"
31 #include "perfetto/trace_processor/trace_processor.h"
32 #include "src/trace_processor/sqlite/create_function.h"
33 #include "src/trace_processor/sqlite/create_view_function.h"
34 #include "src/trace_processor/sqlite/db_sqlite_table.h"
35 #include "src/trace_processor/sqlite/query_cache.h"
36 #include "src/trace_processor/sqlite/scoped_db.h"
37 #include "src/trace_processor/trace_processor_storage_impl.h"
38 
39 #include "src/trace_processor/metrics/metrics.h"
40 #include "src/trace_processor/util/descriptors.h"
41 
42 namespace perfetto {
43 namespace trace_processor {
44 
45 // Coordinates the loading of traces from an arbitrary source and allows
46 // execution of SQL queries on the events in these traces.
47 class TraceProcessorImpl : public TraceProcessor,
48                            public TraceProcessorStorageImpl {
49  public:
50   explicit TraceProcessorImpl(const Config&);
51 
52   TraceProcessorImpl(const TraceProcessorImpl&) = delete;
53   TraceProcessorImpl& operator=(const TraceProcessorImpl&) = delete;
54 
55   TraceProcessorImpl(TraceProcessorImpl&&) = delete;
56   TraceProcessorImpl& operator=(TraceProcessorImpl&&) = delete;
57 
58   ~TraceProcessorImpl() override;
59 
60   // TraceProcessorStorage implementation:
61   base::Status Parse(TraceBlobView) 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 ExtendMetricsProto(const uint8_t* data, size_t size) override;
71 
72   base::Status ExtendMetricsProto(
73       const uint8_t* data,
74       size_t size,
75       const std::vector<std::string>& skip_prefixes) override;
76 
77   base::Status ComputeMetric(const std::vector<std::string>& metric_names,
78                              std::vector<uint8_t>* metrics) override;
79 
80   base::Status ComputeMetricText(const std::vector<std::string>& metric_names,
81                                  TraceProcessor::MetricResultFormat format,
82                                  std::string* metrics_string) override;
83 
84   std::vector<uint8_t> GetMetricDescriptors() override;
85 
86   void InterruptQuery() override;
87 
88   size_t RestoreInitialTables() override;
89 
90   std::string GetCurrentTraceName() override;
91   void SetCurrentTraceName(const std::string&) override;
92 
93   void EnableMetatrace() override;
94 
95   base::Status DisableAndReadMetatrace(
96       std::vector<uint8_t>* trace_proto) override;
97 
98  private:
99   // Needed for iterators to be able to access the context.
100   friend class IteratorImpl;
101 
102   template <typename Table>
RegisterDbTable(const Table & table)103   void RegisterDbTable(const Table& table) {
104     DbSqliteTable::RegisterTable(*db_, query_cache_.get(), Table::Schema(),
105                                  &table, table.table_name());
106   }
107 
RegisterDynamicTable(std::unique_ptr<DbSqliteTable::DynamicTableGenerator> generator)108   void RegisterDynamicTable(
109       std::unique_ptr<DbSqliteTable::DynamicTableGenerator> generator) {
110     DbSqliteTable::RegisterTable(*db_, query_cache_.get(),
111                                  std::move(generator));
112   }
113 
114   bool IsRootMetricField(const std::string& metric_name);
115 
116   // Keep this first: we need this to be destroyed after we clean up
117   // everything else.
118   ScopedDb db_;
119 
120   // State necessary for CREATE_FUNCTION invocations. We store this here as we
121   // need to finalize any prepared statements *before* we destroy the database.
122   CreateFunction::State create_function_state_;
123 
124   // State necessary for CREATE_VIEW_FUNCTION invocations. We store this here as
125   // we need to finalize any prepared statements *before* we destroy the
126   // database.
127   CreateViewFunction::State create_view_function_state_;
128 
129   std::unique_ptr<QueryCache> query_cache_;
130 
131   DescriptorPool pool_;
132   std::vector<metrics::SqlMetricFile> sql_metrics_;
133   std::unordered_map<std::string, std::string> proto_field_to_sql_metric_path_;
134 
135   // This is atomic because it is set by the CTRL-C signal handler and we need
136   // to prevent single-flow compiler optimizations in ExecuteQuery().
137   std::atomic<bool> query_interrupted_{false};
138 
139   // Keeps track of the tables created by the ingestion process. This is used
140   // by RestoreInitialTables() to delete all the tables/view that have been
141   // created after that point.
142   std::vector<std::string> initial_tables_;
143 
144   std::string current_trace_name_;
145   uint64_t bytes_parsed_ = 0;
146 };
147 
148 }  // namespace trace_processor
149 }  // namespace perfetto
150 
151 #endif  // SRC_TRACE_PROCESSOR_TRACE_PROCESSOR_IMPL_H_
152