• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 Index {
21     ID = 0,
22     CALLCHAIN_ID,
23     TYPE,
24     IPID,
25     ITID,
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 
EstimateFilterCost(FilterConstraints & fc,EstimatedIndexInfo & ei)55 void BioLatencySampleTable::EstimateFilterCost(FilterConstraints& fc, EstimatedIndexInfo& ei)
56 {
57     constexpr double filterBaseCost = 1000.0; // set-up and tear-down
58     constexpr double indexCost = 2.0;
59     ei.estimatedCost = filterBaseCost;
60 
61     auto rowCount = dataCache_->GetConstHidumpData().Size();
62     if (rowCount == 0 || rowCount == 1) {
63         ei.estimatedRows = rowCount;
64         ei.estimatedCost += indexCost * rowCount;
65         return;
66     }
67 
68     double filterCost = 0.0;
69     auto constraints = fc.GetConstraints();
70     if (constraints.empty()) { // scan all rows
71         filterCost = rowCount;
72     } else {
73         FilterByConstraint(fc, filterCost, rowCount);
74     }
75     ei.estimatedCost += filterCost;
76     ei.estimatedRows = rowCount;
77     ei.estimatedCost += rowCount * indexCost;
78 
79     ei.isOrdered = true;
80     auto orderbys = fc.GetOrderBys();
81     for (auto i = 0; i < orderbys.size(); i++) {
82         switch (orderbys[i].iColumn) {
83             case ID:
84                 break;
85             default: // other columns can be sorted by SQLite
86                 ei.isOrdered = false;
87                 break;
88         }
89     }
90 }
91 
FilterByConstraint(FilterConstraints & fc,double & filterCost,size_t rowCount)92 void BioLatencySampleTable::FilterByConstraint(FilterConstraints& fc, double& filterCost, size_t rowCount)
93 {
94     auto fcConstraints = fc.GetConstraints();
95     for (int32_t i = 0; i < static_cast<int32_t>(fcConstraints.size()); i++) {
96         if (rowCount <= 1) {
97             // only one row or nothing, needn't filter by constraint
98             filterCost += rowCount;
99             break;
100         }
101         const auto& c = fcConstraints[i];
102         switch (c.col) {
103             case ID: {
104                 if (CanFilterId(c.op, rowCount)) {
105                     fc.UpdateConstraint(i, true);
106                     filterCost += 1; // id can position by 1 step
107                 } else {
108                     filterCost += rowCount; // scan all rows
109                 }
110                 break;
111             }
112             default:                    // other column
113                 filterCost += rowCount; // scan all rows
114                 break;
115         }
116     }
117 }
118 
CreateCursor()119 std::unique_ptr<TableBase::Cursor> BioLatencySampleTable::CreateCursor()
120 {
121     return std::make_unique<Cursor>(dataCache_, this);
122 }
123 
Cursor(const TraceDataCache * dataCache,TableBase * table)124 BioLatencySampleTable::Cursor::Cursor(const TraceDataCache* dataCache, TableBase* table)
125     : TableBase::Cursor(dataCache, table, static_cast<uint32_t>(dataCache->GetConstBioLatencySampleData().Size())),
126       bioLatencySampleObj_(dataCache->GetConstBioLatencySampleData())
127 {
128 }
129 
~Cursor()130 BioLatencySampleTable::Cursor::~Cursor() {}
131 
Filter(const FilterConstraints & fc,sqlite3_value ** argv)132 int32_t BioLatencySampleTable::Cursor::Filter(const FilterConstraints& fc, sqlite3_value** argv)
133 {
134     // reset indexMap_
135     indexMap_ = std::make_unique<IndexMap>(0, rowCount_);
136 
137     if (rowCount_ <= 0) {
138         return SQLITE_OK;
139     }
140 
141     auto& cs = fc.GetConstraints();
142     for (size_t i = 0; i < cs.size(); i++) {
143         const auto& c = cs[i];
144         switch (c.col) {
145             case ID:
146                 FilterId(c.op, argv[i]);
147                 break;
148             default:
149                 break;
150         }
151     }
152 
153     auto orderbys = fc.GetOrderBys();
154     for (auto i = orderbys.size(); i > 0;) {
155         i--;
156         switch (orderbys[i].iColumn) {
157             case ID:
158                 indexMap_->SortBy(orderbys[i].desc);
159                 break;
160             default:
161                 break;
162         }
163     }
164 
165     return SQLITE_OK;
166 }
167 
Column(int32_t column) const168 int32_t BioLatencySampleTable::Cursor::Column(int32_t column) const
169 {
170     switch (column) {
171         case ID:
172             sqlite3_result_int64(context_, static_cast<int32_t>(bioLatencySampleObj_.IdsData()[CurrentRow()]));
173             break;
174         case CALLCHAIN_ID:
175             if (bioLatencySampleObj_.CallChainIds()[CurrentRow()] != INVALID_UINT32) {
176                 sqlite3_result_int64(context_, static_cast<int64_t>(bioLatencySampleObj_.CallChainIds()[CurrentRow()]));
177             } else {
178                 sqlite3_result_int64(context_, static_cast<int64_t>(INVALID_CALL_CHAIN_ID));
179             }
180             break;
181         case TYPE:
182             sqlite3_result_int64(context_, static_cast<int64_t>(bioLatencySampleObj_.Types()[CurrentRow()]));
183             break;
184         case IPID: {
185             if (bioLatencySampleObj_.Ipids()[CurrentRow()] != INVALID_UINT32) {
186                 sqlite3_result_int64(context_, static_cast<int64_t>(bioLatencySampleObj_.Ipids()[CurrentRow()]));
187             }
188             break;
189         }
190         case ITID: {
191             if (bioLatencySampleObj_.Itids()[CurrentRow()] != INVALID_UINT32) {
192                 sqlite3_result_int64(context_, static_cast<int64_t>(bioLatencySampleObj_.Itids()[CurrentRow()]));
193             }
194             break;
195         }
196         case START_TS: {
197             if (bioLatencySampleObj_.StartTs()[CurrentRow()] != INVALID_UINT64) {
198                 sqlite3_result_int64(context_, static_cast<int64_t>(bioLatencySampleObj_.StartTs()[CurrentRow()]));
199             }
200             break;
201         }
202         case END_TS: {
203             if (bioLatencySampleObj_.EndTs()[CurrentRow()] != INVALID_UINT64) {
204                 sqlite3_result_int64(context_, static_cast<int64_t>(bioLatencySampleObj_.EndTs()[CurrentRow()]));
205             }
206             break;
207         }
208         case LATENCY_DUR: {
209             if (bioLatencySampleObj_.LatencyDurs()[CurrentRow()] != INVALID_UINT64) {
210                 sqlite3_result_int64(context_, static_cast<int64_t>(bioLatencySampleObj_.LatencyDurs()[CurrentRow()]));
211             }
212             break;
213         }
214         case TIER: {
215             if (bioLatencySampleObj_.Tiers()[CurrentRow()] != INVALID_UINT32) {
216                 sqlite3_result_int64(context_, static_cast<int64_t>(bioLatencySampleObj_.Tiers()[CurrentRow()]));
217             }
218             break;
219         }
220         case SIZE: {
221             if (bioLatencySampleObj_.Sizes()[CurrentRow()] != INVALID_UINT64) {
222                 sqlite3_result_int64(context_, static_cast<int64_t>(bioLatencySampleObj_.Sizes()[CurrentRow()]));
223             }
224             break;
225         }
226         case BLOCK_NUMBER: {
227             if (bioLatencySampleObj_.BlockNumbers()[CurrentRow()] != INVALID_UINT64) {
228                 auto returnValueIndex0 = bioLatencySampleObj_.BlockNumbers()[CurrentRow()];
229                 sqlite3_result_text(context_, dataCache_->GetDataFromDict(returnValueIndex0).c_str(), STR_DEFAULT_LEN,
230                                     nullptr);
231             }
232             break;
233         }
234         case PATH: {
235             if (bioLatencySampleObj_.FilePathIds()[CurrentRow()] != INVALID_UINT64) {
236                 sqlite3_result_int64(context_, static_cast<int64_t>(bioLatencySampleObj_.FilePathIds()[CurrentRow()]));
237             }
238             break;
239         }
240         case DUR_PER_4K: {
241             if (bioLatencySampleObj_.DurPer4k()[CurrentRow()] != INVALID_UINT64) {
242                 sqlite3_result_int64(context_, static_cast<int64_t>(bioLatencySampleObj_.DurPer4k()[CurrentRow()]));
243             }
244             break;
245         }
246         default:
247             TS_LOGF("Unregistered column : %d", column);
248             break;
249     }
250     return SQLITE_OK;
251 }
252 } // namespace TraceStreamer
253 } // namespace SysTuning
254