xref: /aosp_15_r20/external/perfetto/src/trace_processor/trace_processor_impl.h (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
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   base::Status 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 RegisterSqlPackage(SqlPackage) 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 
RegisterSqlModule(SqlModule module)100   base::Status RegisterSqlModule(SqlModule module) override {
101     SqlPackage package;
102     package.name = std::move(module.name);
103     package.modules = std::move(module.files);
104     package.allow_override = module.allow_module_override;
105 
106     return RegisterSqlPackage(package);
107   }
108 
109  private:
110   // Needed for iterators to be able to access the context.
111   friend class IteratorImpl;
112 
113   template <typename Table>
RegisterStaticTable(Table * table)114   void RegisterStaticTable(Table* table) {
115     engine_->RegisterStaticTable(table, Table::Name(),
116                                  Table::ComputeStaticSchema());
117   }
118 
119   bool IsRootMetricField(const std::string& metric_name);
120 
121   void InitPerfettoSqlEngine();
122   void IncludeBeforeEofPrelude();
123   void IncludeAfterEofPrelude();
124 
125   const Config config_;
126   std::unique_ptr<PerfettoSqlEngine> engine_;
127 
128   DescriptorPool pool_;
129 
130   std::vector<metrics::SqlMetricFile> sql_metrics_;
131 
132   // Manually registeres SQL packages are stored here, to be able to restore
133   // them when running |RestoreInitialTables()|.
134   std::vector<SqlPackage> manually_registered_sql_packages_;
135 
136   std::unordered_map<std::string, std::string> proto_field_to_sql_metric_path_;
137   std::unordered_map<std::string, std::string> proto_fn_name_to_path_;
138 
139   // This is atomic because it is set by the CTRL-C signal handler and we need
140   // to prevent single-flow compiler optimizations in ExecuteQuery().
141   std::atomic<bool> query_interrupted_{false};
142 
143   // Track the number of objects registered with SQLite post prelude.
144   uint64_t sqlite_objects_post_prelude_ = 0;
145 
146   std::string current_trace_name_;
147   uint64_t bytes_parsed_ = 0;
148 
149   // NotifyEndOfFile should only be called once. Set to true whenever it is
150   // called.
151   bool notify_eof_called_ = false;
152 };
153 
154 }  // namespace perfetto::trace_processor
155 
156 #endif  // SRC_TRACE_PROCESSOR_TRACE_PROCESSOR_IMPL_H_
157