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