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 "bio_latency_sample_table.h"
17
18 namespace SysTuning {
19 namespace TraceStreamer {
20 enum class Index : int32_t {
21 ID = 0,
22 CALLCHAIN_ID,
23 TYPE,
24 IPIDS,
25 ITIDS,
26 START_TS,
27 END_TS,
28 LATENCY_DUR,
29 TIER,
30 SIZE,
31 BLOCK_NUMBER,
32 PATH,
33 DUR_PER_4K,
34 };
BioLatencySampleTable(const TraceDataCache * dataCache)35 BioLatencySampleTable::BioLatencySampleTable(const TraceDataCache* dataCache) : TableBase(dataCache)
36 {
37 tableColumn_.push_back(TableBase::ColumnInfo("id", "INTEGER"));
38 tableColumn_.push_back(TableBase::ColumnInfo("callchain_id", "INTEGER"));
39 tableColumn_.push_back(TableBase::ColumnInfo("type", "INTEGER"));
40 tableColumn_.push_back(TableBase::ColumnInfo("ipid", "INTEGER"));
41 tableColumn_.push_back(TableBase::ColumnInfo("itid", "INTEGER"));
42 tableColumn_.push_back(TableBase::ColumnInfo("start_ts", "INTEGER"));
43 tableColumn_.push_back(TableBase::ColumnInfo("end_ts", "INTEGER"));
44 tableColumn_.push_back(TableBase::ColumnInfo("latency_dur", "INTEGER"));
45 tableColumn_.push_back(TableBase::ColumnInfo("tier", "INTEGER"));
46 tableColumn_.push_back(TableBase::ColumnInfo("size", "INTEGER"));
47 tableColumn_.push_back(TableBase::ColumnInfo("block_number", "TEXT"));
48 tableColumn_.push_back(TableBase::ColumnInfo("path_id", "TEXT"));
49 tableColumn_.push_back(TableBase::ColumnInfo("dur_per_4k", "INTEGER"));
50 tablePriKey_.push_back("id");
51 }
52
~BioLatencySampleTable()53 BioLatencySampleTable::~BioLatencySampleTable() {}
54
FilterByConstraint(FilterConstraints & biofc,double & biofilterCost,size_t biorowCount,uint32_t biocurrenti)55 void BioLatencySampleTable::FilterByConstraint(FilterConstraints& biofc,
56 double& biofilterCost,
57 size_t biorowCount,
58 uint32_t biocurrenti)
59 {
60 // To use the EstimateFilterCost function in the TableBase parent class function to calculate the i-value of each
61 // for loop
62 const auto& bioc = biofc.GetConstraints()[biocurrenti];
63 switch (static_cast<Index>(bioc.col)) {
64 case Index::ID: {
65 if (CanFilterId(bioc.op, biorowCount)) {
66 biofc.UpdateConstraint(biocurrenti, true);
67 biofilterCost += 1; // id can position by 1 step
68 } else {
69 biofilterCost += biorowCount; // scan all rows
70 }
71 break;
72 }
73 default: // other column
74 biofilterCost += biorowCount; // scan all rows
75 break;
76 }
77 }
78
CreateCursor()79 std::unique_ptr<TableBase::Cursor> BioLatencySampleTable::CreateCursor()
80 {
81 return std::make_unique<Cursor>(dataCache_, this);
82 }
83
Cursor(const TraceDataCache * dataCache,TableBase * table)84 BioLatencySampleTable::Cursor::Cursor(const TraceDataCache* dataCache, TableBase* table)
85 : TableBase::Cursor(dataCache, table, static_cast<uint32_t>(dataCache->GetConstBioLatencySampleData().Size())),
86 bioLatencySampleObj_(dataCache->GetConstBioLatencySampleData())
87 {
88 }
89
~Cursor()90 BioLatencySampleTable::Cursor::~Cursor() {}
91
Filter(const FilterConstraints & fc,sqlite3_value ** argv)92 int32_t BioLatencySampleTable::Cursor::Filter(const FilterConstraints& fc, sqlite3_value** argv)
93 {
94 // reset indexMap_
95 indexMap_ = std::make_unique<IndexMap>(0, rowCount_);
96
97 if (rowCount_ <= 0) {
98 return SQLITE_OK;
99 }
100
101 auto& bioLateSamTabCs = fc.GetConstraints();
102 for (size_t i = 0; i < bioLateSamTabCs.size(); i++) {
103 const auto& c = bioLateSamTabCs[i];
104 switch (static_cast<Index>(c.col)) {
105 case Index::ID:
106 FilterId(c.op, argv[i]);
107 break;
108 default:
109 break;
110 }
111 }
112
113 auto bioLatSampleTabOrderbys = fc.GetOrderBys();
114 for (auto i = bioLatSampleTabOrderbys.size(); i > 0;) {
115 i--;
116 switch (static_cast<Index>(bioLatSampleTabOrderbys[i].iColumn)) {
117 case Index::ID:
118 indexMap_->SortBy(bioLatSampleTabOrderbys[i].desc);
119 break;
120 default:
121 break;
122 }
123 }
124
125 return SQLITE_OK;
126 }
127
Column(int32_t column) const128 int32_t BioLatencySampleTable::Cursor::Column(int32_t column) const
129 {
130 switch (static_cast<Index>(column)) {
131 case Index::ID:
132 sqlite3_result_int64(context_, static_cast<int32_t>(bioLatencySampleObj_.IdsData()[CurrentRow()]));
133 break;
134 case Index::CALLCHAIN_ID:
135 SetTypeColumn(bioLatencySampleObj_.CallChainIds()[CurrentRow()], INVALID_UINT32, INVALID_CALL_CHAIN_ID);
136 break;
137 case Index::TYPE:
138 sqlite3_result_int64(context_, static_cast<int64_t>(bioLatencySampleObj_.Types()[CurrentRow()]));
139 break;
140 case Index::IPIDS:
141 SetTypeColumnInt64(bioLatencySampleObj_.Ipids()[CurrentRow()], INVALID_UINT64);
142 break;
143 case Index::ITIDS:
144 SetTypeColumnInt64(bioLatencySampleObj_.Itids()[CurrentRow()], INVALID_UINT64);
145 break;
146 case Index::START_TS:
147 SetTypeColumnInt64(bioLatencySampleObj_.StartTs()[CurrentRow()], INVALID_UINT64);
148 break;
149 case Index::END_TS:
150 SetTypeColumnInt64(bioLatencySampleObj_.EndTs()[CurrentRow()], INVALID_UINT64);
151
152 break;
153 case Index::LATENCY_DUR:
154 SetTypeColumnInt64(bioLatencySampleObj_.LatencyDurs()[CurrentRow()], INVALID_UINT64);
155 break;
156 case Index::TIER:
157 SetTypeColumnInt64(bioLatencySampleObj_.Tiers()[CurrentRow()], INVALID_UINT64);
158 break;
159 case Index::SIZE:
160 SetTypeColumnInt64(bioLatencySampleObj_.Sizes()[CurrentRow()], INVALID_UINT64);
161 break;
162 case Index::BLOCK_NUMBER:
163 SetTypeColumnText(bioLatencySampleObj_.BlockNumbers()[CurrentRow()], INVALID_UINT64);
164 break;
165 case Index::PATH:
166 SetTypeColumnInt64(bioLatencySampleObj_.FilePathIds()[CurrentRow()], INVALID_UINT64);
167 break;
168 case Index::DUR_PER_4K:
169 SetTypeColumnInt64(bioLatencySampleObj_.DurPer4k()[CurrentRow()], INVALID_UINT64);
170 break;
171 default:
172 TS_LOGF("Unregistered column : %d", column);
173 break;
174 }
175 return SQLITE_OK;
176 }
GetOrbyes(FilterConstraints & biofc,EstimatedIndexInfo & bioei)177 void BioLatencySampleTable::GetOrbyes(FilterConstraints& biofc, EstimatedIndexInfo& bioei)
178 {
179 auto bioorderbys = biofc.GetOrderBys();
180 for (auto i = 0; i < bioorderbys.size(); i++) {
181 switch (static_cast<Index>(bioorderbys[i].iColumn)) {
182 case Index::ID:
183 break;
184 default: // other columns can be sorted by SQLite
185 bioei.isOrdered = false;
186 break;
187 }
188 }
189 }
190 } // namespace TraceStreamer
191 } // namespace SysTuning
192