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