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 #include "src/trace_processor/sql_stats_table.h"
18
19 #include <sqlite3.h>
20
21 #include <algorithm>
22 #include <bitset>
23 #include <numeric>
24
25 #include "src/trace_processor/sqlite_utils.h"
26 #include "src/trace_processor/trace_storage.h"
27
28 namespace perfetto {
29 namespace trace_processor {
30
SqlStatsTable(sqlite3 *,const TraceStorage * storage)31 SqlStatsTable::SqlStatsTable(sqlite3*, const TraceStorage* storage)
32 : storage_(storage) {}
33
RegisterTable(sqlite3 * db,const TraceStorage * storage)34 void SqlStatsTable::RegisterTable(sqlite3* db, const TraceStorage* storage) {
35 Table::Register<SqlStatsTable>(db, storage, "sqlstats");
36 }
37
Init(int,const char * const *)38 base::Optional<Table::Schema> SqlStatsTable::Init(int, const char* const*) {
39 return Schema(
40 {
41 Table::Column(Column::kQuery, "query", ColumnType::kString),
42 Table::Column(Column::kTimeQueued, "queued", ColumnType::kLong),
43 Table::Column(Column::kTimeStarted, "started", ColumnType::kLong),
44 Table::Column(Column::kTimeFirstNext, "first_next",
45 ColumnType::kLong),
46 Table::Column(Column::kTimeEnded, "ended", ColumnType::kLong),
47 },
48 {Column::kTimeQueued});
49 }
50
CreateCursor()51 std::unique_ptr<Table::Cursor> SqlStatsTable::CreateCursor() {
52 return std::unique_ptr<Table::Cursor>(new Cursor(this));
53 }
54
BestIndex(const QueryConstraints &,BestIndexInfo * info)55 int SqlStatsTable::BestIndex(const QueryConstraints&, BestIndexInfo* info) {
56 info->order_by_consumed = false; // Delegate sorting to SQLite.
57 return SQLITE_OK;
58 }
59
Cursor(SqlStatsTable * table)60 SqlStatsTable::Cursor::Cursor(SqlStatsTable* table)
61 : Table::Cursor(table), storage_(table->storage_), table_(table) {}
62
63 SqlStatsTable::Cursor::~Cursor() = default;
64
Filter(const QueryConstraints &,sqlite3_value **)65 int SqlStatsTable::Cursor::Filter(const QueryConstraints&, sqlite3_value**) {
66 *this = Cursor(table_);
67 num_rows_ = storage_->sql_stats().size();
68 return SQLITE_OK;
69 }
70
Next()71 int SqlStatsTable::Cursor::Next() {
72 row_++;
73 return SQLITE_OK;
74 }
75
Eof()76 int SqlStatsTable::Cursor::Eof() {
77 return row_ >= num_rows_;
78 }
79
Column(sqlite3_context * context,int col)80 int SqlStatsTable::Cursor::Column(sqlite3_context* context, int col) {
81 const TraceStorage::SqlStats& stats = storage_->sql_stats();
82 switch (col) {
83 case Column::kQuery:
84 sqlite3_result_text(context, stats.queries()[row_].c_str(), -1,
85 sqlite_utils::kSqliteStatic);
86 break;
87 case Column::kTimeQueued:
88 sqlite3_result_int64(context, stats.times_queued()[row_]);
89 break;
90 case Column::kTimeStarted:
91 sqlite3_result_int64(context, stats.times_started()[row_]);
92 break;
93 case Column::kTimeFirstNext:
94 sqlite3_result_int64(context, stats.times_first_next()[row_]);
95 break;
96 case Column::kTimeEnded:
97 sqlite3_result_int64(context, stats.times_ended()[row_]);
98 break;
99 }
100 return SQLITE_OK;
101 }
102
103 } // namespace trace_processor
104 } // namespace perfetto
105