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