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