• 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 <string>
25 #include <vector>
26 
27 #include "perfetto/ext/base/string_view.h"
28 #include "perfetto/trace_processor/basic_types.h"
29 #include "perfetto/trace_processor/status.h"
30 #include "perfetto/trace_processor/trace_processor.h"
31 #include "src/trace_processor/sqlite/db_sqlite_table.h"
32 #include "src/trace_processor/sqlite/query_cache.h"
33 #include "src/trace_processor/sqlite/scoped_db.h"
34 #include "src/trace_processor/trace_processor_storage_impl.h"
35 
36 #include "src/trace_processor/metrics/metrics.h"
37 #include "src/trace_processor/util/descriptors.h"
38 
39 namespace perfetto {
40 namespace trace_processor {
41 
42 // Coordinates the loading of traces from an arbitrary source and allows
43 // execution of SQL queries on the events in these traces.
44 class TraceProcessorImpl : public TraceProcessor,
45                            public TraceProcessorStorageImpl {
46  public:
47   explicit TraceProcessorImpl(const Config&);
48 
49   ~TraceProcessorImpl() override;
50 
51   // TraceProcessorStorage implementation:
52   util::Status Parse(std::unique_ptr<uint8_t[]>, size_t) override;
53   void NotifyEndOfFile() override;
54 
55   // TraceProcessor implementation:
56   Iterator ExecuteQuery(const std::string& sql,
57                         int64_t time_queued = 0) override;
58 
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 
64   util::Status ComputeMetric(const std::vector<std::string>& metric_names,
65                              std::vector<uint8_t>* metrics) override;
66 
67   util::Status ComputeMetricText(const std::vector<std::string>& metric_names,
68                                  TraceProcessor::MetricResultFormat format,
69                                  std::string* metrics_string) override;
70 
71   std::vector<uint8_t> GetMetricDescriptors() override;
72 
73   void InterruptQuery() override;
74 
75   size_t RestoreInitialTables() override;
76 
77   std::string GetCurrentTraceName() override;
78   void SetCurrentTraceName(const std::string&) override;
79 
80   void EnableMetatrace() override;
81 
82   util::Status DisableAndReadMetatrace(
83       std::vector<uint8_t>* trace_proto) override;
84 
85  private:
86   // Needed for iterators to be able to access the context.
87   friend class IteratorImpl;
88 
89   template <typename Table>
RegisterDbTable(const Table & table)90   void RegisterDbTable(const Table& table) {
91     DbSqliteTable::RegisterTable(*db_, query_cache_.get(), Table::Schema(),
92                                  &table, table.table_name());
93   }
94 
RegisterDynamicTable(std::unique_ptr<DbSqliteTable::DynamicTableGenerator> generator)95   void RegisterDynamicTable(
96       std::unique_ptr<DbSqliteTable::DynamicTableGenerator> generator) {
97     DbSqliteTable::RegisterTable(*db_, query_cache_.get(),
98                                  std::move(generator));
99   }
100 
101   bool IsRootMetricField(const std::string& metric_name);
102   ScopedDb db_;
103   std::unique_ptr<QueryCache> query_cache_;
104 
105   DescriptorPool pool_;
106   std::vector<metrics::SqlMetricFile> sql_metrics_;
107 
108   // This is atomic because it is set by the CTRL-C signal handler and we need
109   // to prevent single-flow compiler optimizations in ExecuteQuery().
110   std::atomic<bool> query_interrupted_{false};
111 
112   // Keeps track of the tables created by the ingestion process. This is used
113   // by RestoreInitialTables() to delete all the tables/view that have been
114   // created after that point.
115   std::vector<std::string> initial_tables_;
116 
117   std::string current_trace_name_;
118   uint64_t bytes_parsed_ = 0;
119 };
120 
121 
122 }  // namespace trace_processor
123 }  // namespace perfetto
124 
125 #endif  // SRC_TRACE_PROCESSOR_TRACE_PROCESSOR_IMPL_H_
126