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 "frame_slice_table.h"
17
18 namespace SysTuning {
19 namespace TraceStreamer {
20 enum Index { ID = 0, TS, VSYNC, IPID, ITID, CALLSTACK_ID, DUR, SRC, DST, TYPE, TYPE_DESC, FLAG, DEPTH, FRAME_NO };
FrameSliceTable(const TraceDataCache * dataCache)21 FrameSliceTable::FrameSliceTable(const TraceDataCache* dataCache) : TableBase(dataCache)
22 {
23 tableColumn_.push_back(TableBase::ColumnInfo("id", "INTEGER"));
24 tableColumn_.push_back(TableBase::ColumnInfo("ts", "INTEGER"));
25 tableColumn_.push_back(TableBase::ColumnInfo("vsync", "INTEGER"));
26 tableColumn_.push_back(TableBase::ColumnInfo("ipid", "INTEGER"));
27 tableColumn_.push_back(TableBase::ColumnInfo("itid", "INTEGER"));
28 tableColumn_.push_back(TableBase::ColumnInfo("callstack_id", "INTEGER"));
29 tableColumn_.push_back(TableBase::ColumnInfo("dur", "INTEGER"));
30 tableColumn_.push_back(TableBase::ColumnInfo("src", "TEXT"));
31 tableColumn_.push_back(TableBase::ColumnInfo("dst", "INTEGER"));
32 tableColumn_.push_back(TableBase::ColumnInfo("type", "INTEGER"));
33 tableColumn_.push_back(TableBase::ColumnInfo("type_desc", "TEXT"));
34 tableColumn_.push_back(TableBase::ColumnInfo("flag", "INTEGER"));
35 tableColumn_.push_back(TableBase::ColumnInfo("depth", "INTEGER"));
36 tableColumn_.push_back(TableBase::ColumnInfo("frame_no", "INTEGER"));
37 tablePriKey_.push_back("id");
38 }
39
~FrameSliceTable()40 FrameSliceTable::~FrameSliceTable() {}
41
EstimateFilterCost(FilterConstraints & fc,EstimatedIndexInfo & ei)42 void FrameSliceTable::EstimateFilterCost(FilterConstraints& fc, EstimatedIndexInfo& ei)
43 {
44 constexpr double filterBaseCost = 1000.0; // set-up and tear-down
45 constexpr double indexCost = 2.0;
46 ei.estimatedCost = filterBaseCost;
47
48 auto rowCount = dataCache_->GetConstHidumpData().Size();
49 if (rowCount == 0 || rowCount == 1) {
50 ei.estimatedRows = rowCount;
51 ei.estimatedCost += indexCost * rowCount;
52 return;
53 }
54
55 double filterCost = 0.0;
56 auto constraints = fc.GetConstraints();
57 if (constraints.empty()) { // scan all rows
58 filterCost = rowCount;
59 } else {
60 FilterByConstraint(fc, filterCost, rowCount);
61 }
62 ei.estimatedCost += filterCost;
63 ei.estimatedRows = rowCount;
64 ei.estimatedCost += rowCount * indexCost;
65
66 ei.isOrdered = true;
67 auto orderbys = fc.GetOrderBys();
68 for (auto i = 0; i < orderbys.size(); i++) {
69 switch (orderbys[i].iColumn) {
70 case ID:
71 break;
72 default: // other columns can be sorted by SQLite
73 ei.isOrdered = false;
74 break;
75 }
76 }
77 }
78
FilterByConstraint(FilterConstraints & fc,double & filterCost,size_t rowCount)79 void FrameSliceTable::FilterByConstraint(FilterConstraints& fc, double& filterCost, size_t rowCount)
80 {
81 auto fcConstraints = fc.GetConstraints();
82 for (int32_t i = 0; i < static_cast<int32_t>(fcConstraints.size()); i++) {
83 if (rowCount <= 1) {
84 // only one row or nothing, needn't filter by constraint
85 filterCost += rowCount;
86 break;
87 }
88 const auto& c = fcConstraints[i];
89 switch (c.col) {
90 case ID: {
91 if (CanFilterId(c.op, rowCount)) {
92 fc.UpdateConstraint(i, true);
93 filterCost += 1; // id can position by 1 step
94 } else {
95 filterCost += rowCount; // scan all rows
96 }
97 break;
98 }
99 default: // other column
100 filterCost += rowCount; // scan all rows
101 break;
102 }
103 }
104 }
105
CreateCursor()106 std::unique_ptr<TableBase::Cursor> FrameSliceTable::CreateCursor()
107 {
108 return std::make_unique<Cursor>(dataCache_, this);
109 }
110
Cursor(const TraceDataCache * dataCache,TableBase * table)111 FrameSliceTable::Cursor::Cursor(const TraceDataCache* dataCache, TableBase* table)
112 : TableBase::Cursor(dataCache, table, static_cast<uint32_t>(dataCache->GetConstFameSliceData().Size())),
113 frameSliceObj_(dataCache->GetConstFameSliceData())
114 {
115 }
116
~Cursor()117 FrameSliceTable::Cursor::~Cursor() {}
118
Filter(const FilterConstraints & fc,sqlite3_value ** argv)119 int32_t FrameSliceTable::Cursor::Filter(const FilterConstraints& fc, sqlite3_value** argv)
120 {
121 // reset indexMap_
122 indexMap_ = std::make_unique<IndexMap>(0, rowCount_);
123
124 if (rowCount_ <= 0) {
125 return SQLITE_OK;
126 }
127
128 auto& cs = fc.GetConstraints();
129 for (size_t i = 0; i < cs.size(); i++) {
130 const auto& c = cs[i];
131 switch (c.col) {
132 case ID:
133 FilterId(c.op, argv[i]);
134 break;
135 case ITID:
136 indexMap_->MixRange(c.op, static_cast<uint32_t>(sqlite3_value_int(argv[i])),
137 frameSliceObj_.InternalTidsData());
138 break;
139 case IPID:
140 indexMap_->MixRange(c.op, static_cast<uint32_t>(sqlite3_value_int(argv[i])), frameSliceObj_.Ipids());
141 break;
142 case VSYNC:
143 indexMap_->MixRange(c.op, static_cast<uint32_t>(sqlite3_value_int(argv[i])), frameSliceObj_.VsyncIds());
144 break;
145 case CALLSTACK_ID:
146 indexMap_->MixRange(c.op, static_cast<uint64_t>(sqlite3_value_int(argv[i])),
147 frameSliceObj_.CallStackIds());
148 break;
149 case DUR:
150 indexMap_->MixRange(c.op, static_cast<uint64_t>(sqlite3_value_int(argv[i])), frameSliceObj_.Durs());
151 break;
152 case TYPE:
153 indexMap_->MixRange(c.op, static_cast<uint8_t>(sqlite3_value_int(argv[i])), frameSliceObj_.Types());
154 break;
155 case FLAG:
156 indexMap_->MixRange(c.op, static_cast<uint8_t>(sqlite3_value_int(argv[i])), frameSliceObj_.Flags());
157 break;
158 case DEPTH:
159 indexMap_->MixRange(c.op, static_cast<uint8_t>(sqlite3_value_int(argv[i])), frameSliceObj_.Depths());
160 break;
161 case FRAME_NO:
162 indexMap_->MixRange(c.op, static_cast<uint32_t>(sqlite3_value_int(argv[i])), frameSliceObj_.FrameNos());
163 break;
164 default:
165 break;
166 }
167 }
168
169 auto orderbys = fc.GetOrderBys();
170 for (auto i = orderbys.size(); i > 0;) {
171 i--;
172 switch (orderbys[i].iColumn) {
173 case ID:
174 indexMap_->SortBy(orderbys[i].desc);
175 break;
176 default:
177 break;
178 }
179 }
180
181 return SQLITE_OK;
182 }
183
Column(int32_t column) const184 int32_t FrameSliceTable::Cursor::Column(int32_t column) const
185 {
186 switch (column) {
187 case ID:
188 sqlite3_result_int64(context_, static_cast<int32_t>(CurrentRow()));
189 break;
190 case TS:
191 sqlite3_result_int64(context_, static_cast<int64_t>(frameSliceObj_.TimeStampData()[CurrentRow()]));
192 break;
193 case VSYNC:
194 sqlite3_result_int64(context_, static_cast<int32_t>(frameSliceObj_.VsyncIds()[CurrentRow()]));
195 break;
196 case IPID:
197 sqlite3_result_int64(context_, static_cast<int32_t>(frameSliceObj_.Ipids()[CurrentRow()]));
198 break;
199 case ITID:
200 sqlite3_result_int64(context_, static_cast<int32_t>(frameSliceObj_.InternalTidsData()[CurrentRow()]));
201 break;
202 case CALLSTACK_ID:
203 sqlite3_result_int64(context_, static_cast<int64_t>(frameSliceObj_.CallStackIds()[CurrentRow()]));
204 break;
205 case DUR:
206 if (frameSliceObj_.Durs()[CurrentRow()] != INVALID_UINT64) {
207 sqlite3_result_int64(context_, static_cast<int64_t>(frameSliceObj_.Durs()[CurrentRow()]));
208 }
209 break;
210 case SRC:
211 sqlite3_result_text(context_, frameSliceObj_.Srcs()[CurrentRow()].c_str(),
212 frameSliceObj_.Srcs()[CurrentRow()].length(), nullptr);
213 break;
214 case DST:
215 if (frameSliceObj_.Dsts()[CurrentRow()] != INVALID_UINT64) {
216 sqlite3_result_int64(context_, static_cast<int64_t>(frameSliceObj_.Dsts()[CurrentRow()]));
217 }
218 break;
219 case TYPE:
220 sqlite3_result_int64(context_, static_cast<int64_t>(frameSliceObj_.Types()[CurrentRow()]));
221 break;
222 case TYPE_DESC:
223 sqlite3_result_text(context_, frameSliceObj_.Types()[CurrentRow()] == 0 ? "actural" : "expect",
224 STR_DEFAULT_LEN, nullptr);
225 break;
226 case FLAG:
227 if (frameSliceObj_.Flags()[CurrentRow()] != INVALID_UINT8) {
228 sqlite3_result_int(context_, static_cast<int32_t>(frameSliceObj_.Flags()[CurrentRow()]));
229 }
230 break;
231 case DEPTH:
232 if (frameSliceObj_.Depths()[CurrentRow()] != INVALID_UINT8) {
233 sqlite3_result_int(context_, static_cast<int32_t>(frameSliceObj_.Depths()[CurrentRow()]));
234 }
235 break;
236 case FRAME_NO:
237 if (frameSliceObj_.FrameNos()[CurrentRow()] != INVALID_UINT32) {
238 sqlite3_result_int(context_, static_cast<int32_t>(frameSliceObj_.FrameNos()[CurrentRow()]));
239 }
240 break;
241 default:
242 TS_LOGF("Unregistered column : %d", column);
243 break;
244 }
245 return SQLITE_OK;
246 }
247 } // namespace TraceStreamer
248 } // namespace SysTuning
249