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 "cpu_measure_filter_table.h"
17
18 #include <cmath>
19
20 #include "log.h"
21
22 namespace SysTuning {
23 namespace TraceStreamer {
24 enum class Index : int32_t { ID = 0, TYPE, NAME, CPU };
CpuMeasureFilterTable(const TraceDataCache * dataCache)25 CpuMeasureFilterTable::CpuMeasureFilterTable(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
~CpuMeasureFilterTable()34 CpuMeasureFilterTable::~CpuMeasureFilterTable() {}
35
FilterByConstraint(FilterConstraints & cpufc,double & cpufilterCost,size_t cpurowCount,uint32_t cpucurrenti)36 void CpuMeasureFilterTable::FilterByConstraint(FilterConstraints& cpufc,
37 double& cpufilterCost,
38 size_t cpurowCount,
39 uint32_t cpucurrenti)
40 {
41 // To use the EstimateFilterCost function in the TableBase parent class function to calculate the i-value of each
42 // for loop
43 const auto& cpuc = cpufc.GetConstraints()[cpucurrenti];
44 switch (static_cast<Index>(cpuc.col)) {
45 case Index::ID: {
46 auto cpuoldRowCount = cpurowCount;
47 if (CanFilterSorted(cpuc.op, cpurowCount)) {
48 cpufc.UpdateConstraint(cpucurrenti, true);
49 cpufilterCost += log2(cpuoldRowCount); // binary search
50 } else {
51 cpufilterCost += cpuoldRowCount;
52 }
53 break;
54 }
55 default: // other column
56 cpufilterCost += cpurowCount; // scan all rows
57 break;
58 }
59 }
60
CanFilterSorted(const char op,size_t & cpuRowCount) const61 bool CpuMeasureFilterTable::CanFilterSorted(const char op, size_t& cpuRowCount) const
62 {
63 switch (op) {
64 case SQLITE_INDEX_CONSTRAINT_EQ:
65 cpuRowCount = cpuRowCount / log2(cpuRowCount);
66 break;
67 case SQLITE_INDEX_CONSTRAINT_GT:
68 case SQLITE_INDEX_CONSTRAINT_GE:
69 case SQLITE_INDEX_CONSTRAINT_LE:
70 case SQLITE_INDEX_CONSTRAINT_LT:
71 cpuRowCount = (cpuRowCount >> 1);
72 break;
73 default:
74 return false;
75 }
76 return true;
77 }
78
CreateCursor()79 std::unique_ptr<TableBase::Cursor> CpuMeasureFilterTable::CreateCursor()
80 {
81 return std::make_unique<Cursor>(dataCache_, this);
82 }
83
Cursor(const TraceDataCache * dataCache,TableBase * table)84 CpuMeasureFilterTable::Cursor::Cursor(const TraceDataCache* dataCache, TableBase* table)
85 : TableBase::Cursor(dataCache, table, static_cast<uint32_t>(dataCache->GetConstCpuMeasureData().Size())),
86 cpuMeasureObj_(dataCache->GetConstCpuMeasureData())
87 {
88 }
89
~Cursor()90 CpuMeasureFilterTable::Cursor::~Cursor() {}
91
Filter(const FilterConstraints & fc,sqlite3_value ** argv)92 int32_t CpuMeasureFilterTable::Cursor::Filter(const FilterConstraints& fc, sqlite3_value** argv)
93 {
94 // reset indexMap_
95 indexMap_ = std::make_unique<IndexMap>(0, rowCount_);
96
97 if (rowCount_ <= 0) {
98 return SQLITE_OK;
99 }
100
101 auto& cpuMeasureFilterCs = fc.GetConstraints();
102 for (size_t i = 0; i < cpuMeasureFilterCs.size(); i++) {
103 const auto& c = cpuMeasureFilterCs[i];
104 switch (static_cast<Index>(c.col)) {
105 case Index::ID:
106 FilterSorted(c.col, c.op, argv[i]);
107 break;
108 case Index::CPU:
109 indexMap_->MixRange(c.op, static_cast<uint32_t>(sqlite3_value_int(argv[i])), cpuMeasureObj_.CpuData());
110 break;
111 default:
112 break;
113 }
114 }
115
116 auto cpuMeasureFilterTabOrderbys = fc.GetOrderBys();
117 for (auto i = cpuMeasureFilterTabOrderbys.size(); i > 0;) {
118 i--;
119 switch (static_cast<Index>(cpuMeasureFilterTabOrderbys[i].iColumn)) {
120 case Index::ID:
121 indexMap_->SortBy(cpuMeasureFilterTabOrderbys[i].desc);
122 break;
123 default:
124 break;
125 }
126 }
127
128 return SQLITE_OK;
129 }
130
Column(int32_t column) const131 int32_t CpuMeasureFilterTable::Cursor::Column(int32_t column) const
132 {
133 switch (static_cast<Index>(column)) {
134 case Index::ID:
135 sqlite3_result_int64(context_, static_cast<int64_t>(cpuMeasureObj_.IdsData()[CurrentRow()]));
136 break;
137 case Index::TYPE:
138 sqlite3_result_text(context_, "cpu_measure_filter", STR_DEFAULT_LEN, nullptr);
139 break;
140 case Index::NAME: {
141 const std::string& str =
142 dataCache_->GetDataFromDict(static_cast<size_t>(cpuMeasureObj_.NameData()[CurrentRow()]));
143 sqlite3_result_text(context_, str.c_str(), STR_DEFAULT_LEN, nullptr);
144 break;
145 }
146 case Index::CPU:
147 sqlite3_result_int64(context_, static_cast<int32_t>(cpuMeasureObj_.CpuData()[CurrentRow()]));
148 break;
149 default:
150 TS_LOGF("Unregistered column : %d", column);
151 break;
152 }
153 return SQLITE_OK;
154 }
155
FilterSorted(int32_t columns,unsigned char option,sqlite3_value * argv)156 void CpuMeasureFilterTable::Cursor::FilterSorted(int32_t columns, unsigned char option, sqlite3_value* argv)
157 {
158 auto valType = sqlite3_value_type(argv);
159 if (valType != SQLITE_INTEGER) {
160 // other valType consider it NULL, filter out nothing
161 indexMap_->Intersect(0, 0);
162 return;
163 }
164
165 switch (static_cast<Index>(columns)) {
166 case Index::ID: {
167 auto v = static_cast<uint64_t>(sqlite3_value_int64(argv));
168 auto getValue = [](const uint32_t& row) { return row; };
169 switch (option) {
170 case SQLITE_INDEX_CONSTRAINT_EQ:
171 indexMap_->IntersectabcEqual(cpuMeasureObj_.IdsData(), v, getValue);
172 break;
173 case SQLITE_INDEX_CONSTRAINT_GT:
174 v++;
175 case SQLITE_INDEX_CONSTRAINT_GE: {
176 indexMap_->IntersectGreaterEqual(cpuMeasureObj_.IdsData(), v, getValue);
177 break;
178 }
179 case SQLITE_INDEX_CONSTRAINT_LE:
180 v++;
181 case SQLITE_INDEX_CONSTRAINT_LT: {
182 indexMap_->IntersectLessEqual(cpuMeasureObj_.IdsData(), v, getValue);
183 break;
184 }
185 default:
186 break;
187 } // end of switch (option)
188 } // end of case TS
189 default:
190 // can't filter, all rows
191 break;
192 }
193 }
GetOrbyes(FilterConstraints & cpufc,EstimatedIndexInfo & cpuei)194 void CpuMeasureFilterTable::GetOrbyes(FilterConstraints& cpufc, EstimatedIndexInfo& cpuei)
195 {
196 auto cpuorderbys = cpufc.GetOrderBys();
197 for (auto i = 0; i < cpuorderbys.size(); i++) {
198 switch (static_cast<Index>(cpuorderbys[i].iColumn)) {
199 case Index::ID:
200 break;
201 default: // other columns can be sorted by SQLite
202 cpuei.isOrdered = false;
203 break;
204 }
205 }
206 }
207 } // namespace TraceStreamer
208 } // namespace SysTuning
209