1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "perf_files_table.h"
17
18 namespace SysTuning {
19 namespace TraceStreamer {
20 namespace {
21 enum Index { ID = 0, FILE_ID, SERIAL_ID, SYMBOL, PATH };
22 }
PerfFilesTable(const TraceDataCache * dataCache)23 PerfFilesTable::PerfFilesTable(const TraceDataCache* dataCache) : TableBase(dataCache)
24 {
25 tableColumn_.push_back(TableBase::ColumnInfo("id", "INTEGER"));
26 tableColumn_.push_back(TableBase::ColumnInfo("file_id", "INTEGER"));
27 tableColumn_.push_back(TableBase::ColumnInfo("serial_id", "INTEGER"));
28 tableColumn_.push_back(TableBase::ColumnInfo("symbol", "TEXT"));
29 tableColumn_.push_back(TableBase::ColumnInfo("path", "TEXT"));
30 tablePriKey_.push_back("id");
31 }
32
~PerfFilesTable()33 PerfFilesTable::~PerfFilesTable() {}
34
EstimateFilterCost(FilterConstraints & fc,EstimatedIndexInfo & ei)35 void PerfFilesTable::EstimateFilterCost(FilterConstraints& fc, EstimatedIndexInfo& ei)
36 {
37 constexpr double filterBaseCost = 1000.0; // set-up and tear-down
38 constexpr double indexCost = 2.0;
39 ei.estimatedCost = filterBaseCost;
40
41 auto rowCount = dataCache_->GetConstPerfFilesData().Size();
42 if (rowCount == 0 || rowCount == 1) {
43 ei.estimatedRows = rowCount;
44 ei.estimatedCost += indexCost * rowCount;
45 return;
46 }
47
48 double filterCost = 0.0;
49 auto constraints = fc.GetConstraints();
50 if (constraints.empty()) { // scan all rows
51 filterCost = rowCount;
52 } else {
53 FilterByConstraint(fc, filterCost, rowCount);
54 }
55 ei.estimatedCost += filterCost;
56 ei.estimatedRows = rowCount;
57 ei.estimatedCost += rowCount * indexCost;
58
59 ei.isOrdered = true;
60 auto orderbys = fc.GetOrderBys();
61 for (auto i = 0; i < orderbys.size(); i++) {
62 switch (orderbys[i].iColumn) {
63 case ID:
64 break;
65 default: // other columns can be sorted by SQLite
66 ei.isOrdered = false;
67 break;
68 }
69 }
70 }
71
FilterByConstraint(FilterConstraints & fc,double & filterCost,size_t rowCount)72 void PerfFilesTable::FilterByConstraint(FilterConstraints& fc, double& filterCost, size_t rowCount)
73 {
74 auto fcConstraints = fc.GetConstraints();
75 for (int i = 0; i < static_cast<int>(fcConstraints.size()); i++) {
76 if (rowCount <= 1) {
77 // only one row or nothing, needn't filter by constraint
78 filterCost += rowCount;
79 break;
80 }
81 const auto& c = fcConstraints[i];
82 switch (c.col) {
83 case ID: {
84 if (CanFilterId(c.op, rowCount)) {
85 fc.UpdateConstraint(i, true);
86 filterCost += 1; // id can position by 1 step
87 } else {
88 filterCost += rowCount; // scan all rows
89 }
90 break;
91 }
92 default: // other column
93 filterCost += rowCount; // scan all rows
94 break;
95 }
96 }
97 }
98
CanFilterId(const char op,size_t & rowCount)99 bool PerfFilesTable::CanFilterId(const char op, size_t& rowCount)
100 {
101 switch (op) {
102 case SQLITE_INDEX_CONSTRAINT_EQ:
103 rowCount = 1;
104 break;
105 case SQLITE_INDEX_CONSTRAINT_GT:
106 case SQLITE_INDEX_CONSTRAINT_GE:
107 case SQLITE_INDEX_CONSTRAINT_LE:
108 case SQLITE_INDEX_CONSTRAINT_LT:
109 // assume filter out a half of rows
110 rowCount = (rowCount >> 1);
111 break;
112 default:
113 return false;
114 }
115 return true;
116 }
117
CreateCursor()118 std::unique_ptr<TableBase::Cursor> PerfFilesTable::CreateCursor()
119 {
120 return std::make_unique<Cursor>(dataCache_, this);
121 }
122
Cursor(const TraceDataCache * dataCache,TableBase * table)123 PerfFilesTable::Cursor::Cursor(const TraceDataCache* dataCache, TableBase* table)
124 : TableBase::Cursor(dataCache, table, static_cast<uint32_t>(dataCache->GetConstPerfFilesData().Size())),
125 perfFilesObj_(dataCache->GetConstPerfFilesData())
126 {
127 }
128
~Cursor()129 PerfFilesTable::Cursor::~Cursor() {}
130
Filter(const FilterConstraints & fc,sqlite3_value ** argv)131 int PerfFilesTable::Cursor::Filter(const FilterConstraints& fc, sqlite3_value** argv)
132 {
133 // reset indexMap_
134 indexMap_ = std::make_unique<IndexMap>(0, rowCount_);
135
136 if (rowCount_ <= 0) {
137 return SQLITE_OK;
138 }
139
140 auto& cs = fc.GetConstraints();
141 for (size_t i = 0; i < cs.size(); i++) {
142 const auto& c = cs[i];
143 switch (c.col) {
144 case ID:
145 FilterId(c.op, argv[i]);
146 break;
147 case FILE_ID:
148 indexMap_->MixRange(c.op, static_cast<uint64_t>(sqlite3_value_int64(argv[i])), perfFilesObj_.FileIds());
149 break;
150 default:
151 break;
152 }
153 }
154
155 auto orderbys = fc.GetOrderBys();
156 for (auto i = orderbys.size(); i > 0;) {
157 i--;
158 switch (orderbys[i].iColumn) {
159 case ID:
160 indexMap_->SortBy(orderbys[i].desc);
161 break;
162 default:
163 break;
164 }
165 }
166
167 return SQLITE_OK;
168 }
169
Column(int column) const170 int PerfFilesTable::Cursor::Column(int column) const
171 {
172 switch (column) {
173 case ID:
174 sqlite3_result_int64(context_, static_cast<int32_t>(perfFilesObj_.IdsData()[CurrentRow()]));
175 break;
176 case FILE_ID:
177 sqlite3_result_int64(context_, static_cast<int64_t>(perfFilesObj_.FileIds()[CurrentRow()]));
178 break;
179 case SERIAL_ID:
180 sqlite3_result_int(context_, static_cast<int32_t>(perfFilesObj_.Serials()[CurrentRow()]));
181 break;
182 case SYMBOL:
183 if (perfFilesObj_.Symbols()[CurrentRow()] != INVALID_UINT64) {
184 auto symbolIndex = static_cast<size_t>(perfFilesObj_.Symbols()[CurrentRow()]);
185 sqlite3_result_text(context_, dataCache_->GetDataFromDict(symbolIndex).c_str(), STR_DEFAULT_LEN,
186 nullptr);
187 }
188 break;
189 case PATH:
190 if (perfFilesObj_.FilePaths()[CurrentRow()] != INVALID_UINT64) {
191 auto pathIndex = static_cast<size_t>(perfFilesObj_.FilePaths()[CurrentRow()]);
192 sqlite3_result_text(context_, dataCache_->GetDataFromDict(pathIndex).c_str(), STR_DEFAULT_LEN, nullptr);
193 }
194 break;
195 default:
196 TS_LOGF("Unregistered column : %d", column);
197 break;
198 }
199 return SQLITE_OK;
200 }
201 } // namespace TraceStreamer
202 } // namespace SysTuning
203