1 /*
2 * Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved.
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 "symbols_table.h"
17 #include "trace_data_cache.h"
18
19 namespace SysTuning {
20 namespace TraceStreamer {
21 enum class Index : int32_t { ID = 0, STR, ADDR };
SymbolsTable(const TraceDataCache * dataCache)22 SymbolsTable::SymbolsTable(const TraceDataCache *dataCache) : TableBase(dataCache)
23 {
24 tableColumn_.push_back(TableBase::ColumnInfo("id", "INTEGER"));
25 tableColumn_.push_back(TableBase::ColumnInfo("funcname", "TEXT"));
26 tableColumn_.push_back(TableBase::ColumnInfo("addr", "INTEGER"));
27 tablePriKey_.push_back("id");
28 }
29
~SymbolsTable()30 SymbolsTable::~SymbolsTable() {}
31
FilterByConstraint(FilterConstraints & symfc,double & symfilterCost,size_t symrowCount,uint32_t symcurrenti)32 void SymbolsTable::FilterByConstraint(FilterConstraints &symfc,
33 double &symfilterCost,
34 size_t symrowCount,
35 uint32_t symcurrenti)
36 {
37 // To use the EstimateFilterCost function in the TableBase parent class function to calculate the i-value of each
38 // for loop
39 const auto &symc = symfc.GetConstraints()[symcurrenti];
40 switch (static_cast<Index>(symc.col)) {
41 case Index::ID: {
42 if (CanFilterId(symc.op, symrowCount)) {
43 symfc.UpdateConstraint(symcurrenti, true);
44 symfilterCost += 1; // id can position by 1 step
45 } else {
46 symfilterCost += symrowCount; // scan all rows
47 }
48 break;
49 }
50 default: // other column
51 symfilterCost += symrowCount; // scan all rows
52 break;
53 }
54 }
55
CreateCursor()56 std::unique_ptr<TableBase::Cursor> SymbolsTable::CreateCursor()
57 {
58 return std::make_unique<Cursor>(dataCache_, this);
59 }
60
Cursor(const TraceDataCache * dataCache,TableBase * table)61 SymbolsTable::Cursor::Cursor(const TraceDataCache *dataCache, TableBase *table)
62 : TableBase::Cursor(dataCache, table, static_cast<uint32_t>(dataCache->GetConstSymbolsData().Size()))
63 {
64 }
65
~Cursor()66 SymbolsTable::Cursor::~Cursor() {}
67
Filter(const FilterConstraints & fc,sqlite3_value ** argv)68 int32_t SymbolsTable::Cursor::Filter(const FilterConstraints &fc, sqlite3_value **argv)
69 {
70 // reset indexMap_
71 indexMap_ = std::make_unique<IndexMap>(0, rowCount_);
72
73 if (rowCount_ <= 0) {
74 return SQLITE_OK;
75 }
76
77 auto &symTabCs = fc.GetConstraints();
78 for (size_t i = 0; i < symTabCs.size(); i++) {
79 const auto &c = symTabCs[i];
80 switch (static_cast<Index>(c.col)) {
81 case Index::ID:
82 FilterId(c.op, argv[i]);
83 break;
84 default:
85 break;
86 }
87 }
88
89 auto symTabOrderbys = fc.GetOrderBys();
90 for (auto i = symTabOrderbys.size(); i > 0;) {
91 i--;
92 switch (static_cast<Index>(symTabOrderbys[i].iColumn)) {
93 case Index::ID:
94 indexMap_->SortBy(symTabOrderbys[i].desc);
95 break;
96 default:
97 break;
98 }
99 }
100
101 return SQLITE_OK;
102 }
103
Column(int32_t col) const104 int32_t SymbolsTable::Cursor::Column(int32_t col) const
105 {
106 DataIndex index = static_cast<DataIndex>(CurrentRow());
107 switch (static_cast<Index>(col)) {
108 case Index::ID:
109 sqlite3_result_int64(context_, static_cast<sqlite3_int64>(CurrentRow()));
110 break;
111 case Index::STR:
112 sqlite3_result_text(
113 context_,
114 dataCache_->GetDataFromDict(dataCache_->GetConstSymbolsData().GetConstFuncNames()[index]).c_str(),
115 STR_DEFAULT_LEN, nullptr);
116 break;
117 case Index::ADDR:
118 sqlite3_result_int64(context_,
119 static_cast<sqlite3_int64>(dataCache_->GetConstSymbolsData().GetConstAddrs()[index]));
120 break;
121 default:
122 TS_LOGF("Unknown column %d", col);
123 break;
124 }
125 return SQLITE_OK;
126 }
127
GetOrbyes(FilterConstraints & symfc,EstimatedIndexInfo & symei)128 void SymbolsTable::GetOrbyes(FilterConstraints &symfc, EstimatedIndexInfo &symei)
129 {
130 auto symorderbys = symfc.GetOrderBys();
131 for (auto i = 0; i < symorderbys.size(); i++) {
132 switch (static_cast<Index>(symorderbys[i].iColumn)) {
133 case Index::ID:
134 break;
135 default: // other columns can be sorted by SQLite
136 symei.isOrdered = false;
137 break;
138 }
139 }
140 }
141 } // namespace TraceStreamer
142 } // namespace SysTuning
143