• 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/flat_hash_map.h"
29 #include "perfetto/ext/base/string_view.h"
30 #include "perfetto/trace_processor/basic_types.h"
31 #include "perfetto/trace_processor/status.h"
32 #include "perfetto/trace_processor/trace_processor.h"
33 #include "src/trace_processor/prelude/functions/create_function.h"
34 #include "src/trace_processor/prelude/functions/create_view_function.h"
35 #include "src/trace_processor/prelude/functions/import.h"
36 #include "src/trace_processor/sqlite/db_sqlite_table.h"
37 #include "src/trace_processor/sqlite/query_cache.h"
38 #include "src/trace_processor/sqlite/scoped_db.h"
39 #include "src/trace_processor/sqlite/sqlite_engine.h"
40 #include "src/trace_processor/trace_processor_storage_impl.h"
41 #include "src/trace_processor/util/sql_modules.h"
42 
43 #include "src/trace_processor/metrics/metrics.h"
44 #include "src/trace_processor/util/descriptors.h"
45 
46 namespace perfetto {
47 namespace trace_processor {
48 
49 // Coordinates the loading of traces from an arbitrary source and allows
50 // execution of SQL queries on the events in these traces.
51 class TraceProcessorImpl : public TraceProcessor,
52                            public TraceProcessorStorageImpl {
53  public:
54   explicit TraceProcessorImpl(const Config&);
55 
56   TraceProcessorImpl(const TraceProcessorImpl&) = delete;
57   TraceProcessorImpl& operator=(const TraceProcessorImpl&) = delete;
58 
59   TraceProcessorImpl(TraceProcessorImpl&&) = delete;
60   TraceProcessorImpl& operator=(TraceProcessorImpl&&) = delete;
61 
62   ~TraceProcessorImpl() override;
63 
64   // TraceProcessorStorage implementation:
65   base::Status Parse(TraceBlobView) override;
66   void Flush() override;
67   void NotifyEndOfFile() override;
68 
69   // TraceProcessor implementation:
70   Iterator ExecuteQuery(const std::string& sql) override;
71 
72   base::Status RegisterMetric(const std::string& path,
73                               const std::string& sql) override;
74 
75   base::Status RegisterSqlModule(SqlModule sql_module) override;
76 
77   base::Status ExtendMetricsProto(const uint8_t* data, size_t size) override;
78 
79   base::Status ExtendMetricsProto(
80       const uint8_t* data,
81       size_t size,
82       const std::vector<std::string>& skip_prefixes) override;
83 
84   base::Status ComputeMetric(const std::vector<std::string>& metric_names,
85                              std::vector<uint8_t>* metrics) override;
86 
87   base::Status ComputeMetricText(const std::vector<std::string>& metric_names,
88                                  TraceProcessor::MetricResultFormat format,
89                                  std::string* metrics_string) override;
90 
91   std::vector<uint8_t> GetMetricDescriptors() override;
92 
93   void InterruptQuery() override;
94 
95   size_t RestoreInitialTables() override;
96 
97   std::string GetCurrentTraceName() override;
98   void SetCurrentTraceName(const std::string&) override;
99 
100   void EnableMetatrace(MetatraceConfig config) override;
101 
102   base::Status DisableAndReadMetatrace(
103       std::vector<uint8_t>* trace_proto) override;
104 
105  private:
106   // Needed for iterators to be able to access the context.
107   friend class IteratorImpl;
108 
109   template <typename Table>
RegisterDbTable(const Table & table)110   void RegisterDbTable(const Table& table) {
111     engine_.RegisterTable(table, Table::Name());
112   }
113 
RegisterTableFunction(std::unique_ptr<TableFunction> fn)114   void RegisterTableFunction(std::unique_ptr<TableFunction> fn) {
115     engine_.RegisterTableFunction(std::move(fn));
116   }
117 
118   template <typename View>
119   void RegisterView(const View& view);
120 
121   bool IsRootMetricField(const std::string& metric_name);
122 
123   SqliteEngine engine_;
124 
125   DescriptorPool pool_;
126 
127   // Map from module name to module contents. Used for IMPORT function.
128   base::FlatHashMap<std::string, sql_modules::RegisteredModule> sql_modules_;
129   std::vector<metrics::SqlMetricFile> sql_metrics_;
130   std::unordered_map<std::string, std::string> proto_field_to_sql_metric_path_;
131 
132   // This is atomic because it is set by the CTRL-C signal handler and we need
133   // to prevent single-flow compiler optimizations in ExecuteQuery().
134   std::atomic<bool> query_interrupted_{false};
135 
136   // Keeps track of the tables created by the ingestion process. This is used
137   // by RestoreInitialTables() to delete all the tables/view that have been
138   // created after that point.
139   std::vector<std::string> initial_tables_;
140 
141   std::string current_trace_name_;
142   uint64_t bytes_parsed_ = 0;
143 
144   // NotifyEndOfFile should only be called once. Set to true whenever it is
145   // called.
146   bool notify_eof_called_ = false;
147 };
148 
149 }  // namespace trace_processor
150 }  // namespace perfetto
151 
152 #endif  // SRC_TRACE_PROCESSOR_TRACE_PROCESSOR_IMPL_H_
153