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 "frame_maps_table.h"
17
18 namespace SysTuning {
19 namespace TraceStreamer {
20 enum class Index : int32_t { ID = 0, SRC_ROW, DST_ROW };
FrameMapsTable(const TraceDataCache * dataCache)21 FrameMapsTable::FrameMapsTable(const TraceDataCache* dataCache) : TableBase(dataCache)
22 {
23 tableColumn_.push_back(TableBase::ColumnInfo("id", "INTEGER"));
24 tableColumn_.push_back(TableBase::ColumnInfo("src_row", "INTEGER"));
25 tableColumn_.push_back(TableBase::ColumnInfo("dst_row", "INTEGER"));
26 tablePriKey_.push_back("id");
27 }
28
~FrameMapsTable()29 FrameMapsTable::~FrameMapsTable() {}
30
FilterByConstraint(FilterConstraints & mapfc,double & mapfilterCost,size_t maprowCount,uint32_t mapcurrenti)31 void FrameMapsTable::FilterByConstraint(FilterConstraints& mapfc,
32 double& mapfilterCost,
33 size_t maprowCount,
34 uint32_t mapcurrenti)
35 {
36 // To use the EstimateFilterCost function in the TableBase parent class function to calculate the i-value of each
37 // for loop
38 const auto& mapc = mapfc.GetConstraints()[mapcurrenti];
39 switch (static_cast<Index>(mapc.col)) {
40 case Index::ID: {
41 if (CanFilterId(mapc.op, maprowCount)) {
42 mapfc.UpdateConstraint(mapcurrenti, true);
43 mapfilterCost += 1; // id can position by 1 step
44 } else {
45 mapfilterCost += maprowCount; // scan all rows
46 }
47 break;
48 }
49 default: // other column
50 mapfilterCost += maprowCount; // scan all rows
51 break;
52 }
53 }
54
CreateCursor()55 std::unique_ptr<TableBase::Cursor> FrameMapsTable::CreateCursor()
56 {
57 return std::make_unique<Cursor>(dataCache_, this);
58 }
59
Cursor(const TraceDataCache * dataCache,TableBase * table)60 FrameMapsTable::Cursor::Cursor(const TraceDataCache* dataCache, TableBase* table)
61 : TableBase::Cursor(dataCache, table, static_cast<uint32_t>(dataCache->GetConstFrameMapsData().Size())),
62 frameMapsObj_(dataCache->GetConstFrameMapsData())
63 {
64 }
65
~Cursor()66 FrameMapsTable::Cursor::~Cursor() {}
67
Filter(const FilterConstraints & fc,sqlite3_value ** argv)68 int32_t FrameMapsTable::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 frameMapsTabCs = fc.GetConstraints();
78 std::set<uint32_t> sId = {static_cast<uint32_t>(Index::ID)};
79 SwapIndexFront(frameMapsTabCs, sId);
80 for (size_t i = 0; i < frameMapsTabCs.size(); i++) {
81 const auto& c = frameMapsTabCs[i];
82 switch (static_cast<Index>(c.col)) {
83 case Index::ID:
84 FilterId(c.op, argv[i]);
85 break;
86 case Index::SRC_ROW:
87 indexMap_->MixRange(c.op, static_cast<uint64_t>(sqlite3_value_int64(argv[i])),
88 frameMapsObj_.SrcIndexs());
89 break;
90 case Index::DST_ROW:
91 indexMap_->MixRange(c.op, static_cast<uint64_t>(sqlite3_value_int(argv[i])), frameMapsObj_.DstIndexs());
92 break;
93 default:
94 break;
95 }
96 }
97
98 auto frameMapsTabOrderbys = fc.GetOrderBys();
99 for (auto i = frameMapsTabOrderbys.size(); i > 0;) {
100 i--;
101 switch (static_cast<Index>(frameMapsTabOrderbys[i].iColumn)) {
102 case Index::ID:
103 indexMap_->SortBy(frameMapsTabOrderbys[i].desc);
104 break;
105 default:
106 break;
107 }
108 }
109
110 return SQLITE_OK;
111 }
112
Column(int32_t column) const113 int32_t FrameMapsTable::Cursor::Column(int32_t column) const
114 {
115 switch (static_cast<Index>(column)) {
116 case Index::ID:
117 sqlite3_result_int64(context_, static_cast<int32_t>(CurrentRow()));
118 break;
119 case Index::SRC_ROW:
120 sqlite3_result_int64(context_, static_cast<int64_t>(frameMapsObj_.SrcIndexs()[CurrentRow()]));
121 break;
122 case Index::DST_ROW:
123 sqlite3_result_int64(context_, static_cast<int64_t>(frameMapsObj_.DstIndexs()[CurrentRow()]));
124 break;
125 default:
126 TS_LOGF("Unregistered column : %d", column);
127 break;
128 }
129 return SQLITE_OK;
130 }
GetOrbyes(FilterConstraints & mapfc,EstimatedIndexInfo & mapei)131 void FrameMapsTable::GetOrbyes(FilterConstraints& mapfc, EstimatedIndexInfo& mapei)
132 {
133 auto maporderbys = mapfc.GetOrderBys();
134 for (auto i = 0; i < maporderbys.size(); i++) {
135 switch (static_cast<Index>(maporderbys[i].iColumn)) {
136 case Index::ID:
137 break;
138 default: // other columns can be sorted by SQLite
139 mapei.isOrdered = false;
140 break;
141 }
142 }
143 }
144 } // namespace TraceStreamer
145 } // namespace SysTuning
146