• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "native_hook_frame_table.h"
17 
18 namespace SysTuning {
19 namespace TraceStreamer {
20 namespace {
21 enum Index { ID = 0, CALLCHAIN_ID, DEPTH, SYMBOL_ID, FILE_ID, OFFSET, SYMBOL_OFFSET };
22 }
NativeHookFrameTable(const TraceDataCache * dataCache)23 NativeHookFrameTable::NativeHookFrameTable(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("symbol_id", "INTEGER"));
29     tableColumn_.push_back(TableBase::ColumnInfo("file_id", "INTEGER"));
30     tableColumn_.push_back(TableBase::ColumnInfo("offset", "INTEGER"));
31     tableColumn_.push_back(TableBase::ColumnInfo("symbol_offset", "INTEGER"));
32     tablePriKey_.push_back("id");
33 }
34 
~NativeHookFrameTable()35 NativeHookFrameTable::~NativeHookFrameTable() {}
36 
EstimateFilterCost(FilterConstraints & fc,EstimatedIndexInfo & ei)37 void NativeHookFrameTable::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_->GetConstNativeHookFrameData().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 NativeHookFrameTable::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 NativeHookFrameTable::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> NativeHookFrameTable::CreateCursor()
121 {
122     return std::make_unique<Cursor>(dataCache_, this);
123 }
124 
Cursor(const TraceDataCache * dataCache,TableBase * table)125 NativeHookFrameTable::Cursor::Cursor(const TraceDataCache* dataCache, TableBase* table)
126     : TableBase::Cursor(dataCache, table, static_cast<uint32_t>(dataCache->GetConstNativeHookFrameData().Size())),
127       nativeHookFrameInfoObj_(dataCache->GetConstNativeHookFrameData())
128 {
129 }
130 
~Cursor()131 NativeHookFrameTable::Cursor::~Cursor() {}
132 
Filter(const FilterConstraints & fc,sqlite3_value ** argv)133 int NativeHookFrameTable::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                                     nativeHookFrameInfoObj_.CallChainIds());
152                 break;
153             case SYMBOL_ID:
154                 indexMap_->MixRange(c.op, static_cast<uint64_t>(sqlite3_value_int64(argv[i])),
155                                     nativeHookFrameInfoObj_.SymbolNames());
156                 break;
157             case FILE_ID:
158                 indexMap_->MixRange(c.op, static_cast<uint64_t>(sqlite3_value_int64(argv[i])),
159                                     nativeHookFrameInfoObj_.FilePaths());
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 NativeHookFrameTable::Cursor::Column(int column) const
182 {
183     switch (column) {
184         case ID:
185             sqlite3_result_int64(context_, static_cast<int32_t>(CurrentRow()));
186             break;
187         case CALLCHAIN_ID:
188             sqlite3_result_int64(context_, static_cast<int64_t>(nativeHookFrameInfoObj_.CallChainIds()[CurrentRow()]));
189             break;
190         case DEPTH:
191             sqlite3_result_int64(context_, static_cast<int64_t>(nativeHookFrameInfoObj_.Depths()[CurrentRow()]));
192             break;
193         case SYMBOL_ID:
194             if (nativeHookFrameInfoObj_.SymbolNames()[CurrentRow()] != INVALID_UINT64) {
195                 sqlite3_result_int64(context_,
196                                      static_cast<int64_t>(nativeHookFrameInfoObj_.SymbolNames()[CurrentRow()]));
197             }
198             break;
199         case FILE_ID: {
200             if (nativeHookFrameInfoObj_.FilePaths()[CurrentRow()] != INVALID_UINT64) {
201                 sqlite3_result_int64(context_, static_cast<int64_t>(nativeHookFrameInfoObj_.FilePaths()[CurrentRow()]));
202             }
203             break;
204         }
205         case OFFSET: {
206             sqlite3_result_int64(context_, static_cast<int64_t>(nativeHookFrameInfoObj_.Offsets()[CurrentRow()]));
207             break;
208         }
209         case SYMBOL_OFFSET: {
210             sqlite3_result_int64(context_, static_cast<int64_t>(nativeHookFrameInfoObj_.SymbolOffsets()[CurrentRow()]));
211             break;
212         }
213         default:
214             TS_LOGF("Unregistered column : %d", column);
215             break;
216     }
217     return SQLITE_OK;
218 }
219 } // namespace TraceStreamer
220 } // namespace SysTuning
221