• 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/stats_table.h"
18 
19 #include "src/trace_processor/sqlite_utils.h"
20 
21 namespace perfetto {
22 namespace trace_processor {
23 
StatsTable(sqlite3 *,const TraceStorage * storage)24 StatsTable::StatsTable(sqlite3*, const TraceStorage* storage)
25     : storage_(storage) {}
26 
RegisterTable(sqlite3 * db,const TraceStorage * storage)27 void StatsTable::RegisterTable(sqlite3* db, const TraceStorage* storage) {
28   Table::Register<StatsTable>(db, storage, "stats");
29 }
30 
Init(int,const char * const *)31 base::Optional<Table::Schema> StatsTable::Init(int, const char* const*) {
32   return Schema(
33       {
34           Table::Column(Column::kName, "name", ColumnType::kString),
35           // Calling a column "index" causes sqlite to silently fail, hence idx.
36           Table::Column(Column::kIndex, "idx", ColumnType::kUint),
37           Table::Column(Column::kSeverity, "severity", ColumnType::kString),
38           Table::Column(Column::kSource, "source", ColumnType::kString),
39           Table::Column(Column::kValue, "value", ColumnType::kLong),
40       },
41       {Column::kName});
42 }
43 
CreateCursor()44 std::unique_ptr<Table::Cursor> StatsTable::CreateCursor() {
45   return std::unique_ptr<Table::Cursor>(new Cursor(this));
46 }
47 
BestIndex(const QueryConstraints &,BestIndexInfo *)48 int StatsTable::BestIndex(const QueryConstraints&, BestIndexInfo*) {
49   return SQLITE_OK;
50 }
51 
Cursor(StatsTable * table)52 StatsTable::Cursor::Cursor(StatsTable* table)
53     : Table::Cursor(table), table_(table), storage_(table->storage_) {}
54 
Filter(const QueryConstraints &,sqlite3_value **)55 int StatsTable::Cursor::Filter(const QueryConstraints&, sqlite3_value**) {
56   *this = Cursor(table_);
57   return SQLITE_OK;
58 }
59 
Column(sqlite3_context * ctx,int N)60 int StatsTable::Cursor::Column(sqlite3_context* ctx, int N) {
61   const auto kSqliteStatic = sqlite_utils::kSqliteStatic;
62   switch (N) {
63     case Column::kName:
64       sqlite3_result_text(ctx, stats::kNames[key_], -1, kSqliteStatic);
65       break;
66     case Column::kIndex:
67       if (stats::kTypes[key_] == stats::kIndexed) {
68         sqlite3_result_int(ctx, index_->first);
69       } else {
70         sqlite3_result_null(ctx);
71       }
72       break;
73     case Column::kSeverity:
74       switch (stats::kSeverities[key_]) {
75         case stats::kInfo:
76           sqlite3_result_text(ctx, "info", -1, kSqliteStatic);
77           break;
78         case stats::kError:
79           sqlite3_result_text(ctx, "error", -1, kSqliteStatic);
80           break;
81       }
82       break;
83     case Column::kSource:
84       switch (stats::kSources[key_]) {
85         case stats::kTrace:
86           sqlite3_result_text(ctx, "trace", -1, kSqliteStatic);
87           break;
88         case stats::kAnalysis:
89           sqlite3_result_text(ctx, "analysis", -1, kSqliteStatic);
90           break;
91       }
92       break;
93     case Column::kValue:
94       if (stats::kTypes[key_] == stats::kIndexed) {
95         sqlite3_result_int64(ctx, index_->second);
96       } else {
97         sqlite3_result_int64(ctx, storage_->stats()[key_].value);
98       }
99       break;
100     default:
101       PERFETTO_FATAL("Unknown column %d", N);
102       break;
103   }
104   return SQLITE_OK;
105 }
106 
Next()107 int StatsTable::Cursor::Next() {
108   static_assert(stats::kTypes[0] == stats::kSingle,
109                 "the first stats entry cannot be indexed");
110   const auto* cur_entry = &storage_->stats()[key_];
111   if (stats::kTypes[key_] == stats::kIndexed) {
112     if (++index_ != cur_entry->indexed_values.end()) {
113       return SQLITE_OK;
114     }
115   }
116   while (++key_ < stats::kNumKeys) {
117     cur_entry = &storage_->stats()[key_];
118     index_ = cur_entry->indexed_values.begin();
119     if (stats::kTypes[key_] == stats::kSingle ||
120         !cur_entry->indexed_values.empty()) {
121       break;
122     }
123   }
124   return SQLITE_OK;
125 }
126 
Eof()127 int StatsTable::Cursor::Eof() {
128   return key_ >= stats::kNumKeys;
129 }
130 
131 }  // namespace trace_processor
132 }  // namespace perfetto
133