• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "system_event_filter_table.h"
17 
18 #include <cmath>
19 
20 namespace SysTuning {
21 namespace TraceStreamer {
22 namespace {
23 enum Index { ID = 0, TYPE, NAME };
24 }
SystemEventFilterTable(const TraceDataCache * dataCache)25 SystemEventFilterTable::SystemEventFilterTable(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     tablePriKey_.push_back("id");
31 }
32 
~SystemEventFilterTable()33 SystemEventFilterTable::~SystemEventFilterTable() {}
34 
EstimateFilterCost(FilterConstraints & fc,EstimatedIndexInfo & ei)35 void SystemEventFilterTable::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_->GetConstSysMeasureFilterData().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 SystemEventFilterTable::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                 auto oldRowCount = rowCount;
85                 if (CanFilterSorted(c.op, rowCount)) {
86                     fc.UpdateConstraint(i, true);
87                     filterCost += log2(oldRowCount); // binary search
88                 } else {
89                     filterCost += oldRowCount;
90                 }
91                 break;
92             }
93             default:                    // other column
94                 filterCost += rowCount; // scan all rows
95                 break;
96         }
97     }
98 }
99 
CanFilterSorted(const char op,size_t & rowCount) const100 bool SystemEventFilterTable::CanFilterSorted(const char op, size_t& rowCount) const
101 {
102     switch (op) {
103         case SQLITE_INDEX_CONSTRAINT_EQ:
104             rowCount = rowCount / log2(rowCount);
105             break;
106         case SQLITE_INDEX_CONSTRAINT_GT:
107         case SQLITE_INDEX_CONSTRAINT_GE:
108         case SQLITE_INDEX_CONSTRAINT_LE:
109         case SQLITE_INDEX_CONSTRAINT_LT:
110             rowCount = (rowCount >> 1);
111             break;
112         default:
113             return false;
114     }
115     return true;
116 }
117 
CreateCursor()118 std::unique_ptr<TableBase::Cursor> SystemEventFilterTable::CreateCursor()
119 {
120     return std::make_unique<Cursor>(dataCache_, this);
121 }
122 
Cursor(const TraceDataCache * dataCache,TableBase * table)123 SystemEventFilterTable::Cursor::Cursor(const TraceDataCache* dataCache, TableBase* table)
124     : TableBase::Cursor(dataCache, table, static_cast<uint32_t>(dataCache->GetConstSysMeasureFilterData().Size())),
125       sysEventObj_(dataCache->GetConstSysMeasureFilterData())
126 {
127 }
128 
~Cursor()129 SystemEventFilterTable::Cursor::~Cursor() {}
130 
Filter(const FilterConstraints & fc,sqlite3_value ** argv)131 int SystemEventFilterTable::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                 FilterSorted(c.col, c.op, argv[i]);
146                 break;
147             default:
148                 break;
149         }
150     }
151 
152     auto orderbys = fc.GetOrderBys();
153     for (auto i = orderbys.size(); i > 0;) {
154         i--;
155         switch (orderbys[i].iColumn) {
156             case ID:
157                 indexMap_->SortBy(orderbys[i].desc);
158                 break;
159             default:
160                 break;
161         }
162     }
163 
164     return SQLITE_OK;
165 }
166 
Column(int col) const167 int SystemEventFilterTable::Cursor::Column(int col) const
168 {
169     switch (col) {
170         case ID:
171             sqlite3_result_int64(context_, sysEventObj_.IdsData()[CurrentRow()]);
172             break;
173         case TYPE:
174             sqlite3_result_text(context_, dataCache_->GetDataFromDict(sysEventObj_.TypesData()[CurrentRow()]).c_str(),
175                                 STR_DEFAULT_LEN, nullptr);
176             break;
177         case NAME:
178             sqlite3_result_text(context_, dataCache_->GetDataFromDict(sysEventObj_.NamesData()[CurrentRow()]).c_str(),
179                                 STR_DEFAULT_LEN, nullptr);
180             break;
181         default:
182             TS_LOGF("Unregistered column : %d", col);
183             break;
184     }
185     return SQLITE_OK;
186 }
187 
FilterSorted(int col,unsigned char op,sqlite3_value * argv)188 void SystemEventFilterTable::Cursor::FilterSorted(int col, unsigned char op, sqlite3_value* argv)
189 {
190     auto type = sqlite3_value_type(argv);
191     if (type != SQLITE_INTEGER) {
192         // other type consider it NULL, filter out nothing
193         indexMap_->Intersect(0, 0);
194         return;
195     }
196 
197     switch (col) {
198         case ID: {
199             auto v = static_cast<uint64_t>(sqlite3_value_int64(argv));
200             auto getValue = [](const uint32_t& row) { return row; };
201             switch (op) {
202                 case SQLITE_INDEX_CONSTRAINT_EQ:
203                     indexMap_->IntersectabcEqual(sysEventObj_.IdsData(), v, getValue);
204                     break;
205                 case SQLITE_INDEX_CONSTRAINT_GT:
206                     v++;
207                 case SQLITE_INDEX_CONSTRAINT_GE: {
208                     indexMap_->IntersectGreaterEqual(sysEventObj_.IdsData(), v, getValue);
209                     break;
210                 }
211                 case SQLITE_INDEX_CONSTRAINT_LE:
212                     v++;
213                 case SQLITE_INDEX_CONSTRAINT_LT: {
214                     indexMap_->IntersectLessEqual(sysEventObj_.IdsData(), v, getValue);
215                     break;
216                 }
217                 default:
218                     break;
219             } // end of switch (op)
220         } // end of case TS
221         default:
222             // can't filter, all rows
223             break;
224     }
225 }
226 } // namespace TraceStreamer
227 } // namespace SysTuning
228