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 "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[i]);
87 break;
88 case Index::FILE_ID:
89 indexMap_->MixRange(c.op, static_cast<uint64_t>(sqlite3_value_int64(argv[i])), perfFilesObj_.FileIds());
90 break;
91 default:
92 break;
93 }
94 }
95
96 auto perfFilesTabOrderbys = fc.GetOrderBys();
97 for (auto i = perfFilesTabOrderbys.size(); i > 0;) {
98 i--;
99 switch (static_cast<Index>(perfFilesTabOrderbys[i].iColumn)) {
100 case Index::ID:
101 indexMap_->SortBy(perfFilesTabOrderbys[i].desc);
102 break;
103 default:
104 break;
105 }
106 }
107
108 return SQLITE_OK;
109 }
110
Column(int32_t column) const111 int32_t PerfFilesTable::Cursor::Column(int32_t column) const
112 {
113 switch (static_cast<Index>(column)) {
114 case Index::ID:
115 sqlite3_result_int64(context_, static_cast<int32_t>(perfFilesObj_.IdsData()[CurrentRow()]));
116 break;
117 case Index::FILE_ID:
118 sqlite3_result_int64(context_, static_cast<int64_t>(perfFilesObj_.FileIds()[CurrentRow()]));
119 break;
120 case Index::SERIAL_ID:
121 sqlite3_result_int(context_, static_cast<int32_t>(perfFilesObj_.Serials()[CurrentRow()]));
122 break;
123 case Index::SYMBOL:
124 if (perfFilesObj_.Symbols()[CurrentRow()] != INVALID_UINT64) {
125 auto symbolIndex = static_cast<size_t>(perfFilesObj_.Symbols()[CurrentRow()]);
126 sqlite3_result_text(context_, dataCache_->GetDataFromDict(symbolIndex).c_str(), STR_DEFAULT_LEN,
127 nullptr);
128 }
129 break;
130 case Index::PATH:
131 if (perfFilesObj_.FilePaths()[CurrentRow()] != INVALID_UINT64) {
132 auto pathIndex = static_cast<size_t>(perfFilesObj_.FilePaths()[CurrentRow()]);
133 sqlite3_result_text(context_, dataCache_->GetDataFromDict(pathIndex).c_str(), STR_DEFAULT_LEN, nullptr);
134 }
135 break;
136 default:
137 TS_LOGF("Unregistered column : %d", column);
138 break;
139 }
140 return SQLITE_OK;
141 }
142
GetOrbyes(FilterConstraints & filesfc,EstimatedIndexInfo & filesei)143 void PerfFilesTable::GetOrbyes(FilterConstraints& filesfc, EstimatedIndexInfo& filesei)
144 {
145 auto filesorderbys = filesfc.GetOrderBys();
146 for (auto i = 0; i < filesorderbys.size(); i++) {
147 switch (static_cast<Index>(filesorderbys[i].iColumn)) {
148 case Index::ID:
149 break;
150 default: // other columns can be sorted by SQLite
151 filesei.isOrdered = false;
152 break;
153 }
154 }
155 }
156 } // namespace TraceStreamer
157 } // namespace SysTuning
158