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 "hidump_table.h"
17
18 namespace SysTuning {
19 namespace TraceStreamer {
20 namespace {
21 enum Index { ID = 0, TS, FPS };
22 }
HidumpTable(const TraceDataCache * dataCache)23 HidumpTable::HidumpTable(const TraceDataCache* dataCache) : TableBase(dataCache)
24 {
25 tableColumn_.push_back(TableBase::ColumnInfo("id", "INTEGER"));
26 tableColumn_.push_back(TableBase::ColumnInfo("ts", "INTEGER"));
27 tableColumn_.push_back(TableBase::ColumnInfo("fps", "INTEGER"));
28 tablePriKey_.push_back("ts");
29 }
30
~HidumpTable()31 HidumpTable::~HidumpTable() {}
32
EstimateFilterCost(FilterConstraints & fc,EstimatedIndexInfo & ei)33 void HidumpTable::EstimateFilterCost(FilterConstraints& fc, EstimatedIndexInfo& ei)
34 {
35 constexpr double filterBaseCost = 1000.0; // set-up and tear-down
36 constexpr double indexCost = 2.0;
37 ei.estimatedCost = filterBaseCost;
38
39 auto rowCount = dataCache_->GetConstHidumpData().Size();
40 if (rowCount == 0 || rowCount == 1) {
41 ei.estimatedRows = rowCount;
42 ei.estimatedCost += indexCost * rowCount;
43 return;
44 }
45
46 double filterCost = 0.0;
47 auto constraints = fc.GetConstraints();
48 if (constraints.empty()) { // scan all rows
49 filterCost = rowCount;
50 } else {
51 FilterByConstraint(fc, filterCost, rowCount);
52 }
53 ei.estimatedCost += filterCost;
54 ei.estimatedRows = rowCount;
55 ei.estimatedCost += rowCount * indexCost;
56
57 ei.isOrdered = true;
58 auto orderbys = fc.GetOrderBys();
59 for (auto i = 0; i < orderbys.size(); i++) {
60 switch (orderbys[i].iColumn) {
61 case ID:
62 break;
63 default: // other columns can be sorted by SQLite
64 ei.isOrdered = false;
65 break;
66 }
67 }
68 }
69
FilterByConstraint(FilterConstraints & fc,double & filterCost,size_t rowCount)70 void HidumpTable::FilterByConstraint(FilterConstraints& fc, double& filterCost, size_t rowCount)
71 {
72 auto fcConstraints = fc.GetConstraints();
73 for (int i = 0; i < static_cast<int>(fcConstraints.size()); i++) {
74 if (rowCount <= 1) {
75 // only one row or nothing, needn't filter by constraint
76 filterCost += rowCount;
77 break;
78 }
79 const auto& c = fcConstraints[i];
80 switch (c.col) {
81 case ID: {
82 if (CanFilterId(c.op, rowCount)) {
83 fc.UpdateConstraint(i, true);
84 filterCost += 1; // id can position by 1 step
85 } else {
86 filterCost += rowCount; // scan all rows
87 }
88 break;
89 }
90 default: // other column
91 filterCost += rowCount; // scan all rows
92 break;
93 }
94 }
95 }
96
CanFilterId(const char op,size_t & rowCount)97 bool HidumpTable::CanFilterId(const char op, size_t& rowCount)
98 {
99 switch (op) {
100 case SQLITE_INDEX_CONSTRAINT_EQ:
101 rowCount = 1;
102 break;
103 case SQLITE_INDEX_CONSTRAINT_GT:
104 case SQLITE_INDEX_CONSTRAINT_GE:
105 case SQLITE_INDEX_CONSTRAINT_LE:
106 case SQLITE_INDEX_CONSTRAINT_LT:
107 // assume filter out a half of rows
108 rowCount = (rowCount >> 1);
109 break;
110 default:
111 return false;
112 }
113 return true;
114 }
115
CreateCursor()116 std::unique_ptr<TableBase::Cursor> HidumpTable::CreateCursor()
117 {
118 return std::make_unique<Cursor>(dataCache_, this);
119 }
120
Cursor(const TraceDataCache * dataCache,TableBase * table)121 HidumpTable::Cursor::Cursor(const TraceDataCache* dataCache, TableBase* table)
122 : TableBase::Cursor(dataCache, table, static_cast<uint32_t>(dataCache->GetConstHidumpData().Size())),
123 hidumpObj_(dataCache->GetConstHidumpData())
124 {
125 }
126
~Cursor()127 HidumpTable::Cursor::~Cursor() {}
128
Filter(const FilterConstraints & fc,sqlite3_value ** argv)129 int HidumpTable::Cursor::Filter(const FilterConstraints& fc, sqlite3_value** argv)
130 {
131 // reset indexMap_
132 indexMap_ = std::make_unique<IndexMap>(0, rowCount_);
133
134 if (rowCount_ <= 0) {
135 return SQLITE_OK;
136 }
137
138 auto& cs = fc.GetConstraints();
139 for (size_t i = 0; i < cs.size(); i++) {
140 const auto& c = cs[i];
141 switch (c.col) {
142 case ID:
143 FilterId(c.op, argv[i]);
144 break;
145 case TS:
146 indexMap_->MixRange(c.op, static_cast<uint64_t>(sqlite3_value_int64(argv[i])),
147 hidumpObj_.TimeStamData());
148 break;
149 case FPS:
150 indexMap_->MixRange(c.op, static_cast<uint32_t>(sqlite3_value_int(argv[i])), hidumpObj_.Fpss());
151 break;
152 default:
153 break;
154 }
155 }
156
157 auto orderbys = fc.GetOrderBys();
158 for (auto i = orderbys.size(); i > 0;) {
159 i--;
160 switch (orderbys[i].iColumn) {
161 case ID:
162 indexMap_->SortBy(orderbys[i].desc);
163 break;
164 default:
165 break;
166 }
167 }
168
169 return SQLITE_OK;
170 }
171
Column(int column) const172 int HidumpTable::Cursor::Column(int column) const
173 {
174 switch (column) {
175 case ID:
176 sqlite3_result_int64(context_, static_cast<int32_t>(CurrentRow()));
177 break;
178 case TS:
179 sqlite3_result_int64(context_, static_cast<int64_t>(hidumpObj_.TimeStamData()[CurrentRow()]));
180 break;
181 case FPS: {
182 sqlite3_result_int64(context_, static_cast<int64_t>(hidumpObj_.Fpss()[CurrentRow()]));
183 break;
184 }
185 default:
186 TS_LOGF("Unregistered column : %d", column);
187 break;
188 }
189 return SQLITE_OK;
190 }
191 } // namespace TraceStreamer
192 } // namespace SysTuning
193