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