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 enum class Index : int32_t { ID = 0, TYPE, NAME };
SystemEventFilterTable(const TraceDataCache * dataCache)23 SystemEventFilterTable::SystemEventFilterTable(const TraceDataCache* dataCache) : TableBase(dataCache)
24 {
25 tableColumn_.push_back(TableBase::ColumnInfo("id", "INTEGER"));
26 tableColumn_.push_back(TableBase::ColumnInfo("type", "TEXT"));
27 tableColumn_.push_back(TableBase::ColumnInfo("name", "TEXT"));
28 tablePriKey_.push_back("id");
29 }
30
~SystemEventFilterTable()31 SystemEventFilterTable::~SystemEventFilterTable() {}
32
FilterByConstraint(FilterConstraints & eventfc,double & eventfilterCost,size_t eventrowCount,uint32_t eventcurrenti)33 void SystemEventFilterTable::FilterByConstraint(FilterConstraints& eventfc,
34 double& eventfilterCost,
35 size_t eventrowCount,
36 uint32_t eventcurrenti)
37 {
38 const auto& eventc = eventfc.GetConstraints()[eventcurrenti];
39 switch (static_cast<Index>(eventc.col)) {
40 case Index::ID: {
41 auto eventoldRowCount = eventrowCount;
42 if (CanFilterSorted(eventc.op, eventrowCount)) {
43 eventfc.UpdateConstraint(eventcurrenti, true);
44 eventfilterCost += log2(eventoldRowCount); // binary search
45 } else {
46 eventfilterCost += eventoldRowCount;
47 }
48 break;
49 }
50 default: // other column
51 eventfilterCost += eventrowCount; // scan all rows
52 break;
53 }
54 }
55
CanFilterSorted(const char op,size_t & sysRowCnt) const56 bool SystemEventFilterTable::CanFilterSorted(const char op, size_t& sysRowCnt) const
57 {
58 switch (op) {
59 case SQLITE_INDEX_CONSTRAINT_EQ:
60 sysRowCnt = sysRowCnt / log2(sysRowCnt);
61 break;
62 case SQLITE_INDEX_CONSTRAINT_GT:
63 case SQLITE_INDEX_CONSTRAINT_GE:
64 case SQLITE_INDEX_CONSTRAINT_LE:
65 case SQLITE_INDEX_CONSTRAINT_LT:
66 sysRowCnt = (sysRowCnt >> 1);
67 break;
68 default:
69 return false;
70 }
71 return true;
72 }
73
CreateCursor()74 std::unique_ptr<TableBase::Cursor> SystemEventFilterTable::CreateCursor()
75 {
76 return std::make_unique<Cursor>(dataCache_, this);
77 }
78
Cursor(const TraceDataCache * dataCache,TableBase * table)79 SystemEventFilterTable::Cursor::Cursor(const TraceDataCache* dataCache, TableBase* table)
80 : TableBase::Cursor(dataCache, table, static_cast<uint32_t>(dataCache->GetConstSysMeasureFilterData().Size())),
81 sysEventObj_(dataCache->GetConstSysMeasureFilterData())
82 {
83 }
84
~Cursor()85 SystemEventFilterTable::Cursor::~Cursor() {}
86
Filter(const FilterConstraints & fc,sqlite3_value ** argv)87 int32_t SystemEventFilterTable::Cursor::Filter(const FilterConstraints& fc, sqlite3_value** argv)
88 {
89 // reset indexMap_
90 indexMap_ = std::make_unique<IndexMap>(0, rowCount_);
91
92 if (rowCount_ <= 0) {
93 return SQLITE_OK;
94 }
95
96 auto& systemEventFilterCs = fc.GetConstraints();
97 for (size_t i = 0; i < systemEventFilterCs.size(); i++) {
98 const auto& c = systemEventFilterCs[i];
99 switch (static_cast<Index>(c.col)) {
100 case Index::ID:
101 FilterSorted(c.col, c.op, argv[i]);
102 break;
103 default:
104 break;
105 }
106 }
107
108 auto sysEventFilterOrderbys = fc.GetOrderBys();
109 for (auto i = sysEventFilterOrderbys.size(); i > 0;) {
110 i--;
111 switch (static_cast<Index>(sysEventFilterOrderbys[i].iColumn)) {
112 case Index::ID:
113 indexMap_->SortBy(sysEventFilterOrderbys[i].desc);
114 break;
115 default:
116 break;
117 }
118 }
119
120 return SQLITE_OK;
121 }
122
Column(int32_t col) const123 int32_t SystemEventFilterTable::Cursor::Column(int32_t col) const
124 {
125 switch (static_cast<Index>(col)) {
126 case Index::ID:
127 sqlite3_result_int64(context_, sysEventObj_.IdsData()[CurrentRow()]);
128 break;
129 case Index::TYPE:
130 sqlite3_result_text(context_, dataCache_->GetDataFromDict(sysEventObj_.TypesData()[CurrentRow()]).c_str(),
131 STR_DEFAULT_LEN, nullptr);
132 break;
133 case Index::NAME:
134 sqlite3_result_text(context_, dataCache_->GetDataFromDict(sysEventObj_.NamesData()[CurrentRow()]).c_str(),
135 STR_DEFAULT_LEN, nullptr);
136 break;
137 default:
138 TS_LOGF("Unregistered column : %d", col);
139 break;
140 }
141 return SQLITE_OK;
142 }
143
FilterSorted(int32_t col,unsigned char op,sqlite3_value * argv)144 void SystemEventFilterTable::Cursor::FilterSorted(int32_t col, unsigned char op, sqlite3_value* argv)
145 {
146 auto type = sqlite3_value_type(argv);
147 if (type != SQLITE_INTEGER) {
148 // other type consider it NULL, filter out nothing
149 indexMap_->Intersect(0, 0);
150 return;
151 }
152
153 switch (static_cast<Index>(col)) {
154 case Index::ID: {
155 auto v = static_cast<uint64_t>(sqlite3_value_int64(argv));
156 auto getValue = [](const uint32_t& row) { return row; };
157 switch (op) {
158 case SQLITE_INDEX_CONSTRAINT_EQ:
159 indexMap_->IntersectabcEqual(sysEventObj_.IdsData(), v, getValue);
160 break;
161 case SQLITE_INDEX_CONSTRAINT_GT:
162 v++;
163 case SQLITE_INDEX_CONSTRAINT_GE: {
164 indexMap_->IntersectGreaterEqual(sysEventObj_.IdsData(), v, getValue);
165 break;
166 }
167 case SQLITE_INDEX_CONSTRAINT_LE:
168 v++;
169 case SQLITE_INDEX_CONSTRAINT_LT: {
170 indexMap_->IntersectLessEqual(sysEventObj_.IdsData(), v, getValue);
171 break;
172 }
173 default:
174 break;
175 } // end of switch (op)
176 } // end of case TS
177 default:
178 // can't filter, all rows
179 break;
180 }
181 }
182
GetOrbyes(FilterConstraints & eventfc,EstimatedIndexInfo & eventei)183 void SystemEventFilterTable::GetOrbyes(FilterConstraints& eventfc, EstimatedIndexInfo& eventei)
184 {
185 auto eventorderbys = eventfc.GetOrderBys();
186 for (auto i = 0; i < eventorderbys.size(); i++) {
187 switch (static_cast<Index>(eventorderbys[i].iColumn)) {
188 case Index::ID:
189 break;
190 default: // other columns can be sorted by SQLite
191 eventei.isOrdered = false;
192 break;
193 }
194 }
195 }
196 } // namespace TraceStreamer
197 } // namespace SysTuning
198