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 "clk_event_filter_table.h"
17
18 #include <cmath>
19
20 namespace SysTuning {
21 namespace TraceStreamer {
22 namespace {
23 enum Index { ID = 0, TYPE, NAME, CPU };
24 }
ClkEventFilterTable(const TraceDataCache * dataCache)25 ClkEventFilterTable::ClkEventFilterTable(const TraceDataCache* dataCache) : TableBase(dataCache)
26 {
27 tableColumn_.push_back(TableBase::ColumnInfo("id", "INTEGER"));
28 tableColumn_.push_back(TableBase::ColumnInfo("type", "TEXT"));
29 tableColumn_.push_back(TableBase::ColumnInfo("name", "TEXT"));
30 tableColumn_.push_back(TableBase::ColumnInfo("cpu", "INTEGER"));
31 tablePriKey_.push_back("id");
32 }
33
~ClkEventFilterTable()34 ClkEventFilterTable::~ClkEventFilterTable() {}
35
36
EstimateFilterCost(FilterConstraints & fc,EstimatedIndexInfo & ei)37 void ClkEventFilterTable::EstimateFilterCost(FilterConstraints& fc, EstimatedIndexInfo& ei)
38 {
39 constexpr double filterBaseCost = 1000.0; // set-up and tear-down
40 constexpr double indexCost = 2.0;
41 ei.estimatedCost = filterBaseCost;
42
43 auto rowCount = dataCache_->GetConstClkEventFilterData().Size();
44 if (rowCount == 0 || rowCount == 1) {
45 ei.estimatedRows = rowCount;
46 ei.estimatedCost += indexCost * rowCount;
47 return;
48 }
49
50 double filterCost = 0.0;
51 auto constraints = fc.GetConstraints();
52 if (constraints.empty()) { // scan all rows
53 filterCost = rowCount;
54 } else {
55 FilterByConstraint(fc, filterCost, rowCount);
56 }
57 ei.estimatedCost += filterCost;
58 ei.estimatedRows = rowCount;
59 ei.estimatedCost += rowCount * indexCost;
60
61 ei.isOrdered = true;
62 auto orderbys = fc.GetOrderBys();
63 for (auto i = 0; i < orderbys.size(); i++) {
64 switch (orderbys[i].iColumn) {
65 case ID:
66 break;
67 default: // other columns can be sorted by SQLite
68 ei.isOrdered = false;
69 break;
70 }
71 }
72 }
73
FilterByConstraint(FilterConstraints & fc,double & filterCost,size_t rowCount)74 void ClkEventFilterTable::FilterByConstraint(FilterConstraints& fc, double& filterCost, size_t rowCount)
75 {
76 auto fcConstraints = fc.GetConstraints();
77 for (int i = 0; i < static_cast<int>(fcConstraints.size()); i++) {
78 if (rowCount <= 1) {
79 // only one row or nothing, needn't filter by constraint
80 filterCost += rowCount;
81 break;
82 }
83 const auto& c = fcConstraints[i];
84 switch (c.col) {
85 case ID: {
86 auto oldRowCount = rowCount;
87 if (CanFilterSorted(c.op, rowCount)) {
88 fc.UpdateConstraint(i, true);
89 filterCost += log2(oldRowCount); // binary search
90 } else {
91 filterCost += oldRowCount;
92 }
93 break;
94 }
95 default: // other column
96 filterCost += rowCount; // scan all rows
97 break;
98 }
99 }
100 }
101
CanFilterSorted(const char op,size_t & rowCount) const102 bool ClkEventFilterTable::CanFilterSorted(const char op, size_t& rowCount) const
103 {
104 switch (op) {
105 case SQLITE_INDEX_CONSTRAINT_EQ:
106 rowCount = rowCount / log2(rowCount);
107 break;
108 case SQLITE_INDEX_CONSTRAINT_GT:
109 case SQLITE_INDEX_CONSTRAINT_GE:
110 case SQLITE_INDEX_CONSTRAINT_LE:
111 case SQLITE_INDEX_CONSTRAINT_LT:
112 rowCount = (rowCount >> 1);
113 break;
114 default:
115 return false;
116 }
117 return true;
118 }
119
CreateCursor()120 std::unique_ptr<TableBase::Cursor> ClkEventFilterTable::CreateCursor()
121 {
122 return std::make_unique<Cursor>(dataCache_, this);
123 }
124
Cursor(const TraceDataCache * dataCache,TableBase * table)125 ClkEventFilterTable::Cursor::Cursor(const TraceDataCache* dataCache, TableBase* table)
126 : TableBase::Cursor(dataCache, table, static_cast<uint32_t>(dataCache->GetConstClkEventFilterData().Size()))
127 {
128 }
129
~Cursor()130 ClkEventFilterTable::Cursor::~Cursor() {}
131
Filter(const FilterConstraints & fc,sqlite3_value ** argv)132 int ClkEventFilterTable::Cursor::Filter(const FilterConstraints& fc, sqlite3_value** argv)
133 {
134 // reset indexMap_
135 indexMap_ = std::make_unique<IndexMap>(0, rowCount_);
136
137 if (rowCount_ <= 0) {
138 return SQLITE_OK;
139 }
140
141 auto& cs = fc.GetConstraints();
142 for (size_t i = 0; i < cs.size(); i++) {
143 const auto& c = cs[i];
144 switch (c.col) {
145 case ID:
146 FilterSorted(c.col, c.op, argv[i]);
147 break;
148 default:
149 break;
150 }
151 }
152
153 auto orderbys = fc.GetOrderBys();
154 for (auto i = orderbys.size(); i > 0;) {
155 i--;
156 switch (orderbys[i].iColumn) {
157 case ID:
158 indexMap_->SortBy(orderbys[i].desc);
159 break;
160 default:
161 break;
162 }
163 }
164
165 return SQLITE_OK;
166 }
167
Column(int col) const168 int ClkEventFilterTable::Cursor::Column(int col) const
169 {
170 switch (col) {
171 case ID:
172 sqlite3_result_int64(context_,
173 static_cast<sqlite3_int64>(dataCache_->GetConstClkEventFilterData().IdsData()[CurrentRow()]));
174 break;
175 case TYPE: {
176 size_t typeId = static_cast<size_t>(dataCache_->GetConstClkEventFilterData().RatesData()[CurrentRow()]);
177 sqlite3_result_text(context_, dataCache_->GetDataFromDict(typeId).c_str(), STR_DEFAULT_LEN, nullptr);
178 break;
179 }
180 case NAME: {
181 size_t strId =
182 static_cast<size_t>(dataCache_->GetConstClkEventFilterData().NamesData()[CurrentRow()]);
183 sqlite3_result_text(context_, dataCache_->GetDataFromDict(strId).c_str(), STR_DEFAULT_LEN, nullptr);
184 break;
185 }
186 case CPU:
187 sqlite3_result_int64(context_,
188 static_cast<sqlite3_int64>(dataCache_->GetConstClkEventFilterData().CpusData()[CurrentRow()]));
189 break;
190 default:
191 TS_LOGF("Unregistered column : %d", col);
192 break;
193 }
194 return SQLITE_OK;
195 }
196
FilterSorted(int col,unsigned char op,sqlite3_value * argv)197 void ClkEventFilterTable::Cursor::FilterSorted(int col, unsigned char op, sqlite3_value* argv)
198 {
199 auto type = sqlite3_value_type(argv);
200 if (type != SQLITE_INTEGER) {
201 // other type consider it NULL, filter out nothing
202 indexMap_->Intersect(0, 0);
203 return;
204 }
205
206 switch (col) {
207 case ID: {
208 auto v = static_cast<uint64_t>(sqlite3_value_int64(argv));
209 auto getValue = [](const uint32_t& row) {
210 return row;
211 };
212 switch (op) {
213 case SQLITE_INDEX_CONSTRAINT_EQ:
214 indexMap_->IntersectabcEqual(
215 dataCache_->GetConstClkEventFilterData().IdsData(), v, getValue);
216 break;
217 case SQLITE_INDEX_CONSTRAINT_GT:
218 v++;
219 case SQLITE_INDEX_CONSTRAINT_GE: {
220 indexMap_->IntersectGreaterEqual(
221 dataCache_->GetConstClkEventFilterData().IdsData(), v, getValue);
222 break;
223 }
224 case SQLITE_INDEX_CONSTRAINT_LE:
225 v++;
226 case SQLITE_INDEX_CONSTRAINT_LT: {
227 indexMap_->IntersectLessEqual(
228 dataCache_->GetConstClkEventFilterData().IdsData(), v, getValue);
229 break;
230 }
231 default:
232 break;
233 } // end of switch (op)
234 } // end of case TS
235 default:
236 // can't filter, all rows
237 break;
238 }
239 }
240 } // namespace TraceStreamer
241 } // namespace SysTuning
242