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 "perf_files_table.h"
17
18 namespace SysTuning {
19 namespace TraceStreamer {
20 enum class Index : int32_t { ID = 0, FILE_ID, SERIAL_ID, SYMBOL, PATH };
PerfFilesTable(const TraceDataCache *dataCache)21 PerfFilesTable::PerfFilesTable(const TraceDataCache *dataCache) : TableBase(dataCache)
22 {
23 tableColumn_.push_back(TableBase::ColumnInfo("id", "INTEGER"));
24 tableColumn_.push_back(TableBase::ColumnInfo("file_id", "INTEGER"));
25 tableColumn_.push_back(TableBase::ColumnInfo("serial_id", "INTEGER"));
26 tableColumn_.push_back(TableBase::ColumnInfo("symbol", "TEXT"));
27 tableColumn_.push_back(TableBase::ColumnInfo("path", "TEXT"));
28 tablePriKey_.push_back("id");
29 }
30
~PerfFilesTable()31 PerfFilesTable::~PerfFilesTable() {}
32
FilterByConstraint(FilterConstraints &filesfc, double &filesfilterCost, size_t filesrowCount, uint32_t filescurrenti)33 void PerfFilesTable::FilterByConstraint(FilterConstraints &filesfc,
34 double &filesfilterCost,
35 size_t filesrowCount,
36 uint32_t filescurrenti)
37 {
38 // To use the EstimateFilterCost function in the TableBase parent class function to calculate the i-value of each
39 // for loop
40 const auto &filesc = filesfc.GetConstraints()[filescurrenti];
41 switch (static_cast<Index>(filesc.col)) {
42 case Index::ID: {
43 if (CanFilterId(filesc.op, filesrowCount)) {
44 filesfc.UpdateConstraint(filescurrenti, true);
45 filesfilterCost += 1; // id can position by 1 step
46 } else {
47 filesfilterCost += filesrowCount; // scan all rows
48 }
49 break;
50 }
51 default: // other column
52 filesfilterCost += filesrowCount; // scan all rows
53 break;
54 }
55 }
56
CreateCursor()57 std::unique_ptr<TableBase::Cursor> PerfFilesTable::CreateCursor()
58 {
59 return std::make_unique<Cursor>(dataCache_, this);
60 }
61
Cursor(const TraceDataCache *dataCache, TableBase *table)62 PerfFilesTable::Cursor::Cursor(const TraceDataCache *dataCache, TableBase *table)
63 : TableBase::Cursor(dataCache, table, static_cast<uint32_t>(dataCache->GetConstPerfFilesData().Size())),
64 perfFilesObj_(dataCache->GetConstPerfFilesData())
65 {
66 }
67
~Cursor()68 PerfFilesTable::Cursor::~Cursor() {}
69
Filter(const FilterConstraints &fc, sqlite3_value **argv)70 int32_t PerfFilesTable::Cursor::Filter(const FilterConstraints &fc, sqlite3_value **argv)
71 {
72 // reset indexMap_
73 indexMap_ = std::make_unique<IndexMap>(0, rowCount_);
74
75 if (rowCount_ <= 0) {
76 return SQLITE_OK;
77 }
78
79 auto perfFilesTabCs = fc.GetConstraints();
80 std::set<uint32_t> sId = {static_cast<uint32_t>(Index::ID)};
81 SwapIndexFront(perfFilesTabCs, sId);
82 for (size_t i = 0; i < perfFilesTabCs.size(); i++) {
83 const auto &c = perfFilesTabCs[i];
84 switch (static_cast<Index>(c.col)) {
85 case Index::ID:
86 FilterId(c.op, argv[c.idxInaConstraint]);
87 break;
88 case Index::FILE_ID:
89 indexMap_->MixRange(c.op, static_cast<uint64_t>(sqlite3_value_int64(argv[c.idxInaConstraint])),
90 perfFilesObj_.FileIds());
91 break;
92 default:
93 break;
94 }
95 }
96
97 auto perfFilesTabOrderbys = fc.GetOrderBys();
98 for (auto i = perfFilesTabOrderbys.size(); i > 0;) {
99 i--;
100 switch (static_cast<Index>(perfFilesTabOrderbys[i].iColumn)) {
101 case Index::ID:
102 indexMap_->SortBy(perfFilesTabOrderbys[i].desc);
103 break;
104 default:
105 break;
106 }
107 }
108
109 return SQLITE_OK;
110 }
111
Column(int32_t column) const112 int32_t PerfFilesTable::Cursor::Column(int32_t column) const
113 {
114 switch (static_cast<Index>(column)) {
115 case Index::ID:
116 sqlite3_result_int64(context_, static_cast<int32_t>(perfFilesObj_.IdsData()[CurrentRow()]));
117 break;
118 case Index::FILE_ID:
119 sqlite3_result_int64(context_, static_cast<int64_t>(perfFilesObj_.FileIds()[CurrentRow()]));
120 break;
121 case Index::SERIAL_ID:
122 sqlite3_result_int(context_, static_cast<int32_t>(perfFilesObj_.Serials()[CurrentRow()]));
123 break;
124 case Index::SYMBOL:
125 if (perfFilesObj_.Symbols()[CurrentRow()] != INVALID_UINT64) {
126 auto symbolIndex = static_cast<size_t>(perfFilesObj_.Symbols()[CurrentRow()]);
127 sqlite3_result_text(context_, dataCache_->GetDataFromDict(symbolIndex).c_str(), STR_DEFAULT_LEN,
128 nullptr);
129 }
130 break;
131 case Index::PATH:
132 if (perfFilesObj_.FilePaths()[CurrentRow()] != INVALID_UINT64) {
133 auto pathIndex = static_cast<size_t>(perfFilesObj_.FilePaths()[CurrentRow()]);
134 sqlite3_result_text(context_, dataCache_->GetDataFromDict(pathIndex).c_str(), STR_DEFAULT_LEN, nullptr);
135 }
136 break;
137 default:
138 TS_LOGF("Unregistered column : %d", column);
139 break;
140 }
141 return SQLITE_OK;
142 }
143
GetOrbyes(FilterConstraints &filesfc, EstimatedIndexInfo &filesei)144 void PerfFilesTable::GetOrbyes(FilterConstraints &filesfc, EstimatedIndexInfo &filesei)
145 {
146 auto filesorderbys = filesfc.GetOrderBys();
147 for (auto i = 0; i < filesorderbys.size(); i++) {
148 switch (static_cast<Index>(filesorderbys[i].iColumn)) {
149 case Index::ID:
150 break;
151 default: // other columns can be sorted by SQLite
152 filesei.isOrdered = false;
153 break;
154 }
155 }
156 }
157 } // namespace TraceStreamer
158 } // namespace SysTuning
159