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_SQLITE_SQL_STATS_TABLE_H_
18 #define SRC_TRACE_PROCESSOR_SQLITE_SQL_STATS_TABLE_H_
19 
20 #include <limits>
21 #include <memory>
22 
23 #include "src/trace_processor/sqlite/sqlite_table.h"
24 
25 namespace perfetto {
26 namespace trace_processor {
27 
28 class QueryConstraints;
29 class TraceStorage;
30 
31 // A virtual table that allows to introspect performances of the SQL engine
32 // for the kMaxLogEntries queries.
33 class SqlStatsTable : public SqliteTable {
34  public:
35   enum Column {
36     kQuery = 0,
37     kTimeQueued = 1,
38     kTimeStarted = 2,
39     kTimeFirstNext = 3,
40     kTimeEnded = 4,
41   };
42 
43   // Implementation of the SQLite cursor interface.
44   class Cursor : public SqliteTable::Cursor {
45    public:
46     Cursor(SqlStatsTable* storage);
47     ~Cursor() override;
48 
49     // Implementation of SqliteTable::Cursor.
50     int Filter(const QueryConstraints&,
51                sqlite3_value**,
52                FilterHistory) override;
53     int Next() override;
54     int Eof() override;
55     int Column(sqlite3_context*, int N) override;
56 
57    private:
58     Cursor(Cursor&) = delete;
59     Cursor& operator=(const Cursor&) = delete;
60 
61     Cursor(Cursor&&) noexcept = default;
62     Cursor& operator=(Cursor&&) = default;
63 
64     size_t row_ = 0;
65     size_t num_rows_ = 0;
66     const TraceStorage* storage_ = nullptr;
67     SqlStatsTable* table_ = nullptr;
68   };
69 
70   SqlStatsTable(sqlite3*, const TraceStorage* storage);
71 
72   static void RegisterTable(sqlite3* db, const TraceStorage* storage);
73 
74   // Table implementation.
75   util::Status Init(int, const char* const*, Schema*) override;
76   std::unique_ptr<SqliteTable::Cursor> CreateCursor() override;
77   int BestIndex(const QueryConstraints&, BestIndexInfo*) override;
78 
79  private:
80   const TraceStorage* const storage_;
81 };
82 
83 }  // namespace trace_processor
84 }  // namespace perfetto
85 
86 #endif  // SRC_TRACE_PROCESSOR_SQLITE_SQL_STATS_TABLE_H_
87