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 class Index : int32_t {
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
FilterByConstraint(FilterConstraints & callfc,double & callfilterCost,size_t callRowCnt,uint32_t callcurrenti)41 void EbpfCallStackTable::FilterByConstraint(FilterConstraints& callfc,
42 double& callfilterCost,
43 size_t callRowCnt,
44 uint32_t callcurrenti)
45 {
46 // To use the EstimateFilterCost function in the TableBase parent class function to calculate the i-value of each
47 // for loop
48 const auto& callc = callfc.GetConstraints()[callcurrenti];
49 switch (static_cast<Index>(callc.col)) {
50 case Index::ID: {
51 if (CanFilterId(callc.op, callRowCnt)) {
52 callfc.UpdateConstraint(callcurrenti, true);
53 callfilterCost += 1; // id can position by 1 step
54 } else {
55 callfilterCost += callRowCnt; // scan all rows
56 }
57 break;
58 }
59 default: // other column
60 callfilterCost += callRowCnt; // scan all rows
61 break;
62 }
63 }
64
CreateCursor()65 std::unique_ptr<TableBase::Cursor> EbpfCallStackTable::CreateCursor()
66 {
67 return std::make_unique<Cursor>(dataCache_, this);
68 }
69
Cursor(const TraceDataCache * dataCache,TableBase * table)70 EbpfCallStackTable::Cursor::Cursor(const TraceDataCache* dataCache, TableBase* table)
71 : TableBase::Cursor(dataCache, table, static_cast<uint32_t>(dataCache->GetConstEbpfCallStackData().Size())),
72 ebpfCallStackObj_(dataCache->GetConstEbpfCallStackData())
73 {
74 }
75
~Cursor()76 EbpfCallStackTable::Cursor::~Cursor() {}
77
Filter(const FilterConstraints & fc,sqlite3_value ** argv)78 int32_t EbpfCallStackTable::Cursor::Filter(const FilterConstraints& fc, sqlite3_value** argv)
79 {
80 // reset indexMap_
81 indexMap_ = std::make_unique<IndexMap>(0, rowCount_);
82
83 if (rowCount_ <= 0) {
84 return SQLITE_OK;
85 }
86
87 auto& ebpfCallStackCs = fc.GetConstraints();
88 for (size_t i = 0; i < ebpfCallStackCs.size(); i++) {
89 const auto& c = ebpfCallStackCs[i];
90 switch (static_cast<Index>(c.col)) {
91 case Index::ID:
92 FilterId(c.op, argv[i]);
93 break;
94 default:
95 break;
96 }
97 }
98
99 auto ebpfCallStackTabOrderbys = fc.GetOrderBys();
100 for (auto i = ebpfCallStackTabOrderbys.size(); i > 0;) {
101 i--;
102 switch (static_cast<Index>(ebpfCallStackTabOrderbys[i].iColumn)) {
103 case Index::ID:
104 indexMap_->SortBy(ebpfCallStackTabOrderbys[i].desc);
105 break;
106 default:
107 break;
108 }
109 }
110
111 return SQLITE_OK;
112 }
113
Column(int32_t column) const114 int32_t EbpfCallStackTable::Cursor::Column(int32_t column) const
115 {
116 switch (static_cast<Index>(column)) {
117 case Index::ID:
118 sqlite3_result_int64(context_, static_cast<int32_t>(ebpfCallStackObj_.IdsData()[CurrentRow()]));
119 break;
120 case Index::CALLCHAIN_ID:
121 sqlite3_result_int64(context_, static_cast<int64_t>(ebpfCallStackObj_.CallChainIds()[CurrentRow()]));
122 break;
123 case Index::DEPTH:
124 sqlite3_result_int64(context_, static_cast<int64_t>(ebpfCallStackObj_.Depths()[CurrentRow()]));
125 break;
126 case Index::IP: {
127 if (ebpfCallStackObj_.Ips()[CurrentRow()] != INVALID_UINT64) {
128 auto returnValueIndex = ebpfCallStackObj_.Ips()[CurrentRow()];
129 sqlite3_result_text(context_, dataCache_->GetDataFromDict(returnValueIndex).c_str(), STR_DEFAULT_LEN,
130 nullptr);
131 }
132 break;
133 }
134 case Index::SYMBOLS_ID: {
135 if (ebpfCallStackObj_.SymbolIds()[CurrentRow()] != INVALID_UINT64) {
136 sqlite3_result_int64(context_, static_cast<int64_t>(ebpfCallStackObj_.SymbolIds()[CurrentRow()]));
137 }
138 break;
139 }
140 case Index::FILE_PATH_ID: {
141 if (ebpfCallStackObj_.FilePathIds()[CurrentRow()] != INVALID_UINT64) {
142 sqlite3_result_int64(context_, static_cast<int64_t>(ebpfCallStackObj_.FilePathIds()[CurrentRow()]));
143 }
144 break;
145 }
146 default:
147 TS_LOGF("Unregistered column : %d", column);
148 break;
149 }
150 return SQLITE_OK;
151 }
GetOrbyes(FilterConstraints & ebpfCallfc,EstimatedIndexInfo & ebpfCalleInfo)152 void EbpfCallStackTable::GetOrbyes(FilterConstraints& ebpfCallfc, EstimatedIndexInfo& ebpfCalleInfo)
153 {
154 auto ebpfCallOrderbys = ebpfCallfc.GetOrderBys();
155 for (auto i = 0; i < ebpfCallOrderbys.size(); i++) {
156 switch (static_cast<Index>(ebpfCallOrderbys[i].iColumn)) {
157 case Index::ID:
158 break;
159 default: // other columns can be sorted by SQLitep
160 ebpfCalleInfo.isOrdered = false;
161 break;
162 }
163 }
164 }
165 } // namespace TraceStreamer
166 } // namespace SysTuning
167