blob: 0405eb957f2b45a12a1e0c6c28d41e13e11da44a [file] [log] [blame]
Primiano Tucci3bf99f32018-07-24 10:28:28 +01001/*
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_TABLE_H_
18#define SRC_TRACE_PROCESSOR_TABLE_H_
19
20#include <sqlite3.h>
Primiano Tucci0e9bca42018-08-31 19:20:58 +020021
Primiano Tucci3bf99f32018-07-24 10:28:28 +010022#include <functional>
23#include <memory>
24#include <string>
25#include <vector>
26
Lalit Magantif45f8ad2018-12-06 12:58:15 +000027#include "perfetto/base/optional.h"
Lalit Maganti7ee6abb2019-05-09 17:57:32 +010028#include "perfetto/trace_processor/basic_types.h"
Primiano Tucci0e9bca42018-08-31 19:20:58 +020029#include "src/trace_processor/query_constraints.h"
30
Primiano Tucci3bf99f32018-07-24 10:28:28 +010031namespace perfetto {
32namespace trace_processor {
33
Primiano Tucci3bf99f32018-07-24 10:28:28 +010034class TraceStorage;
35
36// Abstract base class representing a SQLite virtual table. Implements the
37// common bookeeping required across all tables and allows subclasses to
38// implement a friendlier API than that required by SQLite.
39class Table : public sqlite3_vtab {
40 public:
Primiano Tuccib2ea4d42018-08-21 15:05:13 +020041 using Factory =
Lalit Maganti6003ae52018-09-18 14:36:02 +010042 std::function<std::unique_ptr<Table>(sqlite3*, const TraceStorage*)>;
Primiano Tuccib2ea4d42018-08-21 15:05:13 +020043
Lalit Magantia270b652018-09-26 11:54:33 +010044 // Allowed types for columns in a table.
45 enum ColumnType {
46 kString = 1,
Lalit Magantid632ab42018-12-12 21:07:57 +000047 kUint = 2,
48 kLong = 3,
49 kInt = 4,
50 kDouble = 5,
51 kUnknown = 6,
Lalit Magantia270b652018-09-26 11:54:33 +010052 };
53
54 // Describes a column of this table.
55 class Column {
56 public:
Lalit Magantiacda68b2018-10-29 15:23:25 +000057 Column(size_t idx, std::string name, ColumnType type, bool hidden = false);
Lalit Magantia270b652018-09-26 11:54:33 +010058
59 size_t index() const { return index_; }
60 const std::string& name() const { return name_; }
61 ColumnType type() const { return type_; }
62 bool hidden() const { return hidden_; }
63
64 private:
65 size_t index_ = 0;
66 std::string name_;
67 ColumnType type_ = ColumnType::kString;
68 bool hidden_ = false;
69 };
70
Primiano Tuccib2ea4d42018-08-21 15:05:13 +020071 // When set it logs all BestIndex and Filter actions on the console.
72 static bool debug;
Primiano Tucci3bf99f32018-07-24 10:28:28 +010073
74 // Public for unique_ptr destructor calls.
75 virtual ~Table();
76
77 // Abstract base class representing an SQLite Cursor. Presents a friendlier
78 // API for subclasses to implement.
79 class Cursor : public sqlite3_vtab_cursor {
80 public:
Lalit Magantif1a8d522019-04-26 11:05:15 +010081 Cursor(Table* table);
Primiano Tucci3bf99f32018-07-24 10:28:28 +010082 virtual ~Cursor();
83
84 // Methods to be implemented by derived table classes.
Lalit Magantif1a8d522019-04-26 11:05:15 +010085
86 // Called to intialise the cursor with the constraints of the query.
87 virtual int Filter(const QueryConstraints& qc, sqlite3_value**) = 0;
88
89 // Called to forward the cursor to the next row in the table.
Primiano Tucci3bf99f32018-07-24 10:28:28 +010090 virtual int Next() = 0;
Lalit Magantif1a8d522019-04-26 11:05:15 +010091
92 // Called to check if the cursor has reached eof. Column will be called iff
93 // this method returns true.
Primiano Tucci3bf99f32018-07-24 10:28:28 +010094 virtual int Eof() = 0;
Lalit Magantif1a8d522019-04-26 11:05:15 +010095
96 // Used to extract the value from the column at index |N|.
Primiano Tucci3bf99f32018-07-24 10:28:28 +010097 virtual int Column(sqlite3_context* context, int N) = 0;
98
Lalit Magantif4588fc2018-09-24 14:14:49 +010099 // Optional methods to implement.
100 virtual int RowId(sqlite3_int64*);
Lalit Magantif0b4e172018-09-27 13:43:26 +0100101
Lalit Magantif1a8d522019-04-26 11:05:15 +0100102 protected:
103 Cursor(Cursor&) = delete;
104 Cursor& operator=(const Cursor&) = delete;
Lalit Magantif0b4e172018-09-27 13:43:26 +0100105
Lalit Magantif1a8d522019-04-26 11:05:15 +0100106 Cursor(Cursor&&) noexcept = default;
107 Cursor& operator=(Cursor&&) = default;
Lalit Magantif4588fc2018-09-24 14:14:49 +0100108
Primiano Tucci3bf99f32018-07-24 10:28:28 +0100109 private:
110 friend class Table;
111
Lalit Magantif1a8d522019-04-26 11:05:15 +0100112 Table* table_ = nullptr;
Primiano Tucci3bf99f32018-07-24 10:28:28 +0100113 };
114
Lalit Magantia270b652018-09-26 11:54:33 +0100115 // The schema of the table. Created by subclasses to allow the table class to
116 // do filtering and inform SQLite about the CREATE table statement.
117 class Schema {
118 public:
119 Schema();
120 Schema(std::vector<Column>, std::vector<size_t> primary_keys);
121
122 // This class is explicitly copiable.
Lalit Maganti1ebebf12018-10-15 17:24:04 +0100123 Schema(const Schema&);
Lalit Magantia270b652018-09-26 11:54:33 +0100124 Schema& operator=(const Schema& t);
125
Lalit Magantif45f8ad2018-12-06 12:58:15 +0000126 std::string ToCreateTableStmt() const;
Lalit Magantia270b652018-09-26 11:54:33 +0100127
Lalit Magantiacda68b2018-10-29 15:23:25 +0000128 const std::vector<Column>& columns() const { return columns_; }
Lalit Magantia270b652018-09-26 11:54:33 +0100129 const std::vector<size_t> primary_keys() { return primary_keys_; }
130
131 private:
132 // The names and types of the columns of the table.
133 std::vector<Column> columns_;
134
135 // The primary keys of the table given by an offset into |columns|.
136 std::vector<size_t> primary_keys_;
137 };
138
Lalit Magantiacda68b2018-10-29 15:23:25 +0000139 protected:
140 // Populated by a BestIndex call to allow subclasses to tweak SQLite's
141 // handling of sets of constraints.
142 struct BestIndexInfo {
143 bool order_by_consumed = false;
144 uint32_t estimated_cost = 0;
145 std::vector<bool> omit;
146 };
147
Lalit Magantif1a8d522019-04-26 11:05:15 +0100148 struct TableDescriptor {
149 Table::Factory factory;
150 const TraceStorage* storage = nullptr;
151 std::string name;
152 sqlite3_module module = {};
153 };
154
Primiano Tucci3bf99f32018-07-24 10:28:28 +0100155 Table();
156
157 // Called by derived classes to register themselves with the SQLite db.
Sami Kyostila33668942018-11-13 16:33:32 +0000158 // |read_write| specifies whether the table can also be written to.
159 // |requires_args| should be true if the table requires arguments in order to
160 // be instantiated.
Lalit Magantif1a8d522019-04-26 11:05:15 +0100161 // Note: this function is inlined here because we use the TTable template to
162 // devirtualise the function calls.
163 template <typename TTable>
Primiano Tucci3bf99f32018-07-24 10:28:28 +0100164 static void Register(sqlite3* db,
165 const TraceStorage* storage,
Lalit Magantif1a8d522019-04-26 11:05:15 +0100166 const std::string& table_name,
Sami Kyostila33668942018-11-13 16:33:32 +0000167 bool read_write = false,
168 bool requires_args = false) {
Lalit Magantif1a8d522019-04-26 11:05:15 +0100169 using TCursor = typename TTable::Cursor;
170
171 std::unique_ptr<TableDescriptor> desc(new TableDescriptor());
172 desc->storage = storage;
173 desc->factory = GetFactory<TTable>();
174 desc->name = table_name;
175 sqlite3_module* module = &desc->module;
176 memset(module, 0, sizeof(*module));
177
178 auto create_fn = [](sqlite3* xdb, void* arg, int argc,
Lalit Maganti7ee6abb2019-05-09 17:57:32 +0100179 const char* const* argv, sqlite3_vtab** tab,
180 char** pzErr) {
Lalit Magantif1a8d522019-04-26 11:05:15 +0100181 const TableDescriptor* xdesc = static_cast<const TableDescriptor*>(arg);
182 auto table = xdesc->factory(xdb, xdesc->storage);
183 table->name_ = xdesc->name;
184
Lalit Maganti7ee6abb2019-05-09 17:57:32 +0100185 Schema schema;
186 util::Status status = table->Init(argc, argv, &schema);
187 if (!status.ok()) {
188 *pzErr = sqlite3_mprintf("%s", status.c_message());
Lalit Magantif1a8d522019-04-26 11:05:15 +0100189 return SQLITE_ERROR;
190 }
191
Lalit Magantif1a8d522019-04-26 11:05:15 +0100192 auto create_stmt = schema.ToCreateTableStmt();
193 PERFETTO_DLOG("Create table statement: %s", create_stmt.c_str());
194
195 int res = sqlite3_declare_vtab(xdb, create_stmt.c_str());
196 if (res != SQLITE_OK)
197 return res;
198
199 // Freed in xDisconnect().
200 table->schema_ = std::move(schema);
201 *tab = table.release();
202
203 return SQLITE_OK;
204 };
205 auto destroy_fn = [](sqlite3_vtab* t) {
206 delete static_cast<TTable*>(t);
207 return SQLITE_OK;
208 };
209
210 module->xCreate = create_fn;
211 module->xConnect = create_fn;
212 module->xDisconnect = destroy_fn;
213 module->xDestroy = destroy_fn;
214 module->xOpen = [](sqlite3_vtab* t, sqlite3_vtab_cursor** c) {
215 return static_cast<TTable*>(t)->OpenInternal(c);
216 };
217 module->xClose = [](sqlite3_vtab_cursor* c) {
218 delete static_cast<TCursor*>(c);
219 return SQLITE_OK;
220 };
221 module->xBestIndex = [](sqlite3_vtab* t, sqlite3_index_info* i) {
222 return static_cast<TTable*>(t)->BestIndexInternal(i);
223 };
224 module->xFilter = [](sqlite3_vtab_cursor* c, int i, const char* s, int a,
225 sqlite3_value** v) {
226 const auto& qc =
227 static_cast<Cursor*>(c)->table_->ParseConstraints(i, s, a);
228 return static_cast<TCursor*>(c)->Filter(qc, v);
229 };
230 module->xNext = [](sqlite3_vtab_cursor* c) {
231 return static_cast<TCursor*>(c)->Next();
232 };
233 module->xEof = [](sqlite3_vtab_cursor* c) {
234 return static_cast<TCursor*>(c)->Eof();
235 };
236 module->xColumn = [](sqlite3_vtab_cursor* c, sqlite3_context* a, int b) {
237 return static_cast<TCursor*>(c)->Column(a, b);
238 };
239 module->xRowid = [](sqlite3_vtab_cursor* c, sqlite3_int64* r) {
240 return static_cast<TCursor*>(c)->RowId(r);
241 };
242 module->xFindFunction =
243 [](sqlite3_vtab* t, int, const char* name,
244 void (**fn)(sqlite3_context*, int, sqlite3_value**), void** args) {
245 return static_cast<TTable*>(t)->FindFunction(name, fn, args);
246 };
247
248 if (read_write) {
249 module->xUpdate = [](sqlite3_vtab* t, int a, sqlite3_value** v,
250 sqlite3_int64* r) {
251 return static_cast<TTable*>(t)->Update(a, v, r);
252 };
253 }
254
255 int res = sqlite3_create_module_v2(
256 db, table_name.c_str(), module, desc.release(),
257 [](void* arg) { delete static_cast<TableDescriptor*>(arg); });
258 PERFETTO_CHECK(res == SQLITE_OK);
259
260 // Register virtual tables into an internal 'perfetto_tables' table. This is
261 // used for iterating through all the tables during a database export. Note
262 // that virtual tables requiring arguments aren't registered because they
263 // can't be automatically instantiated for exporting.
264 if (!requires_args) {
265 char* insert_sql = sqlite3_mprintf(
266 "INSERT INTO perfetto_tables(name) VALUES('%q')", table_name.c_str());
267 char* error = nullptr;
268 sqlite3_exec(db, insert_sql, 0, 0, &error);
269 sqlite3_free(insert_sql);
270 if (error) {
271 PERFETTO_ELOG("Error registering table: %s", error);
272 sqlite3_free(error);
273 }
274 }
Primiano Tucci3bf99f32018-07-24 10:28:28 +0100275 }
276
277 // Methods to be implemented by derived table classes.
Lalit Maganti7ee6abb2019-05-09 17:57:32 +0100278 virtual util::Status Init(int argc, const char* const* argv, Schema*) = 0;
Lalit Magantif1a8d522019-04-26 11:05:15 +0100279 virtual std::unique_ptr<Cursor> CreateCursor() = 0;
Primiano Tucci3bf99f32018-07-24 10:28:28 +0100280 virtual int BestIndex(const QueryConstraints& qc, BestIndexInfo* info) = 0;
281
282 // Optional metods to implement.
283 using FindFunctionFn = void (**)(sqlite3_context*, int, sqlite3_value**);
284 virtual int FindFunction(const char* name, FindFunctionFn fn, void** args);
285
Lalit Magantif4588fc2018-09-24 14:14:49 +0100286 // At registration time, the function should also pass true for |read_write|.
287 virtual int Update(int, sqlite3_value**, sqlite3_int64*);
288
Lalit Maganti43d8f142018-11-12 14:58:15 +0000289 void SetErrorMessage(char* error) {
290 sqlite3_free(zErrMsg);
291 zErrMsg = error;
292 }
293
Lalit Maganti127479a2019-03-13 18:52:11 +0000294 const Schema& schema() const { return schema_; }
295 const std::string& name() const { return name_; }
Lalit Magantiacda68b2018-10-29 15:23:25 +0000296
Primiano Tucci3bf99f32018-07-24 10:28:28 +0100297 private:
298 template <typename TableType>
299 static Factory GetFactory() {
Lalit Maganti6003ae52018-09-18 14:36:02 +0100300 return [](sqlite3* db, const TraceStorage* storage) {
301 return std::unique_ptr<Table>(new TableType(db, storage));
Primiano Tucci3bf99f32018-07-24 10:28:28 +0100302 };
303 }
304
305 static void RegisterInternal(sqlite3* db,
306 const TraceStorage*,
Lalit Maganti6003ae52018-09-18 14:36:02 +0100307 const std::string& name,
Lalit Magantif4588fc2018-09-24 14:14:49 +0100308 bool read_write,
Sami Kyostila33668942018-11-13 16:33:32 +0000309 bool requires_args,
Primiano Tucci3bf99f32018-07-24 10:28:28 +0100310 Factory);
311
Lalit Magantif1a8d522019-04-26 11:05:15 +0100312 const QueryConstraints& ParseConstraints(int idxNum,
313 const char* idxStr,
314 int argc);
315
Primiano Tucci3bf99f32018-07-24 10:28:28 +0100316 // Overriden functions from sqlite3_vtab.
317 int OpenInternal(sqlite3_vtab_cursor**);
318 int BestIndexInternal(sqlite3_index_info*);
319
320 Table(const Table&) = delete;
321 Table& operator=(const Table&) = delete;
Primiano Tuccib2ea4d42018-08-21 15:05:13 +0200322
323 std::string name_;
Lalit Magantia270b652018-09-26 11:54:33 +0100324 Schema schema_;
325
Primiano Tucci0e9bca42018-08-31 19:20:58 +0200326 QueryConstraints qc_cache_;
327 int qc_hash_ = 0;
328 int best_index_num_ = 0;
Primiano Tucci3bf99f32018-07-24 10:28:28 +0100329};
330
331} // namespace trace_processor
332} // namespace perfetto
333
334#endif // SRC_TRACE_PROCESSOR_TABLE_H_