• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "src/trace_processor/sqlite/sqlite_utils.h"
26 #include "src/trace_processor/storage/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   SqliteTable::Register<SqlStatsTable>(db, storage, "sqlstats");
36 }
37 
Init(int,const char * const *,Schema * schema)38 util::Status SqlStatsTable::Init(int, const char* const*, Schema* schema) {
39   *schema = Schema(
40       {
41           SqliteTable::Column(Column::kQuery, "query", SqlValue::Type::kString),
42           SqliteTable::Column(Column::kTimeQueued, "queued",
43                               SqlValue::Type::kLong),
44           SqliteTable::Column(Column::kTimeStarted, "started",
45                               SqlValue::Type::kLong),
46           SqliteTable::Column(Column::kTimeFirstNext, "first_next",
47                               SqlValue::Type::kLong),
48           SqliteTable::Column(Column::kTimeEnded, "ended",
49                               SqlValue::Type::kLong),
50       },
51       {Column::kTimeQueued});
52   return util::OkStatus();
53 }
54 
CreateCursor()55 std::unique_ptr<SqliteTable::Cursor> SqlStatsTable::CreateCursor() {
56   return std::unique_ptr<SqliteTable::Cursor>(new Cursor(this));
57 }
58 
BestIndex(const QueryConstraints &,BestIndexInfo *)59 int SqlStatsTable::BestIndex(const QueryConstraints&, BestIndexInfo*) {
60   return SQLITE_OK;
61 }
62 
Cursor(SqlStatsTable * table)63 SqlStatsTable::Cursor::Cursor(SqlStatsTable* table)
64     : SqliteTable::Cursor(table), storage_(table->storage_), table_(table) {}
65 
66 SqlStatsTable::Cursor::~Cursor() = default;
67 
Filter(const QueryConstraints &,sqlite3_value **,FilterHistory)68 int SqlStatsTable::Cursor::Filter(const QueryConstraints&,
69                                   sqlite3_value**,
70                                   FilterHistory) {
71   *this = Cursor(table_);
72   num_rows_ = storage_->sql_stats().size();
73   return SQLITE_OK;
74 }
75 
Next()76 int SqlStatsTable::Cursor::Next() {
77   row_++;
78   return SQLITE_OK;
79 }
80 
Eof()81 int SqlStatsTable::Cursor::Eof() {
82   return row_ >= num_rows_;
83 }
84 
Column(sqlite3_context * context,int col)85 int SqlStatsTable::Cursor::Column(sqlite3_context* context, int col) {
86   const TraceStorage::SqlStats& stats = storage_->sql_stats();
87   switch (col) {
88     case Column::kQuery:
89       sqlite3_result_text(context, stats.queries()[row_].c_str(), -1,
90                           sqlite_utils::kSqliteStatic);
91       break;
92     case Column::kTimeQueued:
93       sqlite3_result_int64(context, stats.times_queued()[row_]);
94       break;
95     case Column::kTimeStarted:
96       sqlite3_result_int64(context, stats.times_started()[row_]);
97       break;
98     case Column::kTimeFirstNext:
99       sqlite3_result_int64(context, stats.times_first_next()[row_]);
100       break;
101     case Column::kTimeEnded:
102       sqlite3_result_int64(context, stats.times_ended()[row_]);
103       break;
104   }
105   return SQLITE_OK;
106 }
107 
108 }  // namespace trace_processor
109 }  // namespace perfetto
110