• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved.
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 
CreateCursor()56 std::unique_ptr<TableBase::Cursor> SystemEventFilterTable::CreateCursor()
57 {
58     return std::make_unique<Cursor>(dataCache_, this);
59 }
60 
Cursor(const TraceDataCache * dataCache,TableBase * table)61 SystemEventFilterTable::Cursor::Cursor(const TraceDataCache *dataCache, TableBase *table)
62     : TableBase::Cursor(dataCache, table, static_cast<uint32_t>(dataCache->GetConstSysMeasureFilterData().Size())),
63       sysEventObj_(dataCache->GetConstSysMeasureFilterData())
64 {
65 }
66 
~Cursor()67 SystemEventFilterTable::Cursor::~Cursor() {}
68 
Filter(const FilterConstraints & fc,sqlite3_value ** argv)69 int32_t SystemEventFilterTable::Cursor::Filter(const FilterConstraints &fc, sqlite3_value **argv)
70 {
71     // reset indexMap_
72     indexMap_ = std::make_unique<IndexMap>(0, rowCount_);
73 
74     if (rowCount_ <= 0) {
75         return SQLITE_OK;
76     }
77 
78     auto &systemEventFilterCs = fc.GetConstraints();
79     for (size_t i = 0; i < systemEventFilterCs.size(); i++) {
80         const auto &c = systemEventFilterCs[i];
81         switch (static_cast<Index>(c.col)) {
82             case Index::ID:
83                 FilterSorted(c.col, c.op, argv[i]);
84                 break;
85             default:
86                 break;
87         }
88     }
89 
90     auto sysEventFilterOrderbys = fc.GetOrderBys();
91     for (auto i = sysEventFilterOrderbys.size(); i > 0;) {
92         i--;
93         switch (static_cast<Index>(sysEventFilterOrderbys[i].iColumn)) {
94             case Index::ID:
95                 indexMap_->SortBy(sysEventFilterOrderbys[i].desc);
96                 break;
97             default:
98                 break;
99         }
100     }
101 
102     return SQLITE_OK;
103 }
104 
Column(int32_t col) const105 int32_t SystemEventFilterTable::Cursor::Column(int32_t col) const
106 {
107     switch (static_cast<Index>(col)) {
108         case Index::ID:
109             sqlite3_result_int64(context_, sysEventObj_.IdsData()[CurrentRow()]);
110             break;
111         case Index::TYPE:
112             sqlite3_result_text(context_, dataCache_->GetDataFromDict(sysEventObj_.TypesData()[CurrentRow()]).c_str(),
113                                 STR_DEFAULT_LEN, nullptr);
114             break;
115         case Index::NAME:
116             sqlite3_result_text(context_, dataCache_->GetDataFromDict(sysEventObj_.NamesData()[CurrentRow()]).c_str(),
117                                 STR_DEFAULT_LEN, nullptr);
118             break;
119         default:
120             TS_LOGF("Unregistered column : %d", col);
121             break;
122     }
123     return SQLITE_OK;
124 }
125 
FilterSorted(int32_t col,unsigned char op,sqlite3_value * argv)126 void SystemEventFilterTable::Cursor::FilterSorted(int32_t col, unsigned char op, sqlite3_value *argv)
127 {
128     auto type = sqlite3_value_type(argv);
129     if (type != SQLITE_INTEGER) {
130         // other type consider it NULL, filter out nothing
131         indexMap_->Intersect(0, 0);
132         return;
133     }
134 
135     switch (static_cast<Index>(col)) {
136         case Index::ID: {
137             auto v = static_cast<uint64_t>(sqlite3_value_int64(argv));
138             auto getValue = [](const uint32_t &row) { return row; };
139             switch (op) {
140                 case SQLITE_INDEX_CONSTRAINT_EQ:
141                     indexMap_->IntersectabcEqual(sysEventObj_.IdsData(), v, getValue);
142                     break;
143                 case SQLITE_INDEX_CONSTRAINT_GT:
144                     v++;
145                 case SQLITE_INDEX_CONSTRAINT_GE: {
146                     indexMap_->IntersectGreaterEqual(sysEventObj_.IdsData(), v, getValue);
147                     break;
148                 }
149                 case SQLITE_INDEX_CONSTRAINT_LE:
150                     v++;
151                 case SQLITE_INDEX_CONSTRAINT_LT: {
152                     indexMap_->IntersectLessEqual(sysEventObj_.IdsData(), v, getValue);
153                     break;
154                 }
155                 default:
156                     break;
157             } // end of switch (op)
158         }     // end of case TS
159         default:
160             // can't filter, all rows
161             break;
162     }
163 }
164 
GetOrbyes(FilterConstraints & eventfc,EstimatedIndexInfo & eventei)165 void SystemEventFilterTable::GetOrbyes(FilterConstraints &eventfc, EstimatedIndexInfo &eventei)
166 {
167     auto eventorderbys = eventfc.GetOrderBys();
168     for (auto i = 0; i < eventorderbys.size(); i++) {
169         switch (static_cast<Index>(eventorderbys[i].iColumn)) {
170             case Index::ID:
171                 break;
172             default: // other columns can be sorted by SQLite
173                 eventei.isOrdered = false;
174                 break;
175         }
176     }
177 }
178 } // namespace TraceStreamer
179 } // namespace SysTuning
180