• 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::kTimeStarted, "started",
43                               SqlValue::Type::kLong),
44           SqliteTable::Column(Column::kTimeFirstNext, "first_next",
45                               SqlValue::Type::kLong),
46           SqliteTable::Column(Column::kTimeEnded, "ended",
47                               SqlValue::Type::kLong),
48       },
49       {Column::kTimeStarted});
50   return util::OkStatus();
51 }
52 
CreateCursor()53 std::unique_ptr<SqliteTable::Cursor> SqlStatsTable::CreateCursor() {
54   return std::unique_ptr<SqliteTable::Cursor>(new Cursor(this));
55 }
56 
BestIndex(const QueryConstraints &,BestIndexInfo *)57 int SqlStatsTable::BestIndex(const QueryConstraints&, BestIndexInfo*) {
58   return SQLITE_OK;
59 }
60 
Cursor(SqlStatsTable * table)61 SqlStatsTable::Cursor::Cursor(SqlStatsTable* table)
62     : SqliteTable::Cursor(table), storage_(table->storage_), table_(table) {}
63 
64 SqlStatsTable::Cursor::~Cursor() = default;
65 
Filter(const QueryConstraints &,sqlite3_value **,FilterHistory)66 int SqlStatsTable::Cursor::Filter(const QueryConstraints&,
67                                   sqlite3_value**,
68                                   FilterHistory) {
69   *this = Cursor(table_);
70   num_rows_ = storage_->sql_stats().size();
71   return SQLITE_OK;
72 }
73 
Next()74 int SqlStatsTable::Cursor::Next() {
75   row_++;
76   return SQLITE_OK;
77 }
78 
Eof()79 int SqlStatsTable::Cursor::Eof() {
80   return row_ >= num_rows_;
81 }
82 
Column(sqlite3_context * context,int col)83 int SqlStatsTable::Cursor::Column(sqlite3_context* context, int col) {
84   const TraceStorage::SqlStats& stats = storage_->sql_stats();
85   switch (col) {
86     case Column::kQuery:
87       sqlite3_result_text(context, stats.queries()[row_].c_str(), -1,
88                           sqlite_utils::kSqliteStatic);
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