• 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 "instants_table.h"
17 #include <cmath>
18 
19 namespace SysTuning {
20 namespace TraceStreamer {
21 enum Index { TS = 0, NAME, REF, WAKEUP_FROM, REF_TYPE, VALUE };
InstantsTable(const TraceDataCache * dataCache)22 InstantsTable::InstantsTable(const TraceDataCache* dataCache) : TableBase(dataCache)
23 {
24     tableColumn_.push_back(TableBase::ColumnInfo("ts", "INTEGER"));
25     tableColumn_.push_back(TableBase::ColumnInfo("name", "TEXT"));
26     tableColumn_.push_back(TableBase::ColumnInfo("ref", "INTEGER"));
27     tableColumn_.push_back(TableBase::ColumnInfo("wakeup_from", "INTEGER"));
28     tableColumn_.push_back(TableBase::ColumnInfo("ref_type", "TEXT"));
29     tableColumn_.push_back(TableBase::ColumnInfo("value", "REAL"));
30     tablePriKey_.push_back("ts");
31     tablePriKey_.push_back("ref");
32 }
33 
~InstantsTable()34 InstantsTable::~InstantsTable() {}
35 
CreateCursor()36 std::unique_ptr<TableBase::Cursor> InstantsTable::CreateCursor()
37 {
38     return std::make_unique<Cursor>(dataCache_, this);
39 }
40 
Cursor(const TraceDataCache * dataCache,TableBase * table)41 InstantsTable::Cursor::Cursor(const TraceDataCache* dataCache, TableBase* table)
42     : TableBase::Cursor(dataCache, table, static_cast<uint32_t>(dataCache->GetConstInstantsData().Size())),
43       InstantsObj_(dataCache->GetConstInstantsData())
44 {
45 }
46 
~Cursor()47 InstantsTable::Cursor::~Cursor() {}
48 
EstimateFilterCost(FilterConstraints & fc,EstimatedIndexInfo & ei)49 void InstantsTable::EstimateFilterCost(FilterConstraints& fc, EstimatedIndexInfo& ei)
50 {
51     constexpr double filterBaseCost = 1000.0; // set-up and tear-down
52     constexpr double indexCost = 2.0;
53     ei.estimatedCost = filterBaseCost;
54 
55     auto rowCount = dataCache_->GetConstMeasureData().Size();
56     if (rowCount == 0 || rowCount == 1) {
57         ei.estimatedRows = rowCount;
58         ei.estimatedCost += indexCost * rowCount;
59         return;
60     }
61 
62     double filterCost = 0.0;
63     auto constraints = fc.GetConstraints();
64     if (constraints.empty()) { // scan all rows
65         filterCost = rowCount;
66     } else {
67         FilterByConstraint(fc, filterCost, rowCount);
68     }
69     ei.estimatedCost += filterCost;
70     ei.estimatedRows = rowCount;
71     ei.estimatedCost += rowCount * indexCost;
72 
73     ei.isOrdered = true;
74     auto orderbys = fc.GetOrderBys();
75     for (auto i = 0; i < orderbys.size(); i++) {
76         switch (orderbys[i].iColumn) {
77             case TS:
78                 break;
79             case NAME:
80                 break;
81             case REF:
82                 break;
83             case WAKEUP_FROM:
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 InstantsTable::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 TS: {
104                 auto oldRowCount = rowCount;
105                 if (CanFilterSorted(c.op, rowCount)) {
106                     fc.UpdateConstraint(i, true);
107                     filterCost += log2(oldRowCount); // binary search
108                 } else {
109                     filterCost += oldRowCount;
110                 }
111                 break;
112             }
113             default:                    // other column
114                 filterCost += rowCount; // scan all rows
115                 break;
116         }
117     }
118 }
119 
CanFilterSorted(const char op,size_t & rowCount) const120 bool InstantsTable::CanFilterSorted(const char op, size_t& rowCount) const
121 {
122     switch (op) {
123         case SQLITE_INDEX_CONSTRAINT_EQ:
124             rowCount = rowCount / log2(rowCount);
125             break;
126         case SQLITE_INDEX_CONSTRAINT_GT:
127         case SQLITE_INDEX_CONSTRAINT_GE:
128         case SQLITE_INDEX_CONSTRAINT_LE:
129         case SQLITE_INDEX_CONSTRAINT_LT:
130             rowCount = (rowCount >> 1);
131             break;
132         default:
133             return false;
134     }
135     return true;
136 }
137 
Filter(const FilterConstraints & fc,sqlite3_value ** argv)138 int32_t InstantsTable::Cursor::Filter(const FilterConstraints& fc, sqlite3_value** argv)
139 {
140     // reset
141     indexMap_ = std::make_unique<IndexMap>(0, rowCount_);
142     if (rowCount_ <= 0) {
143         return SQLITE_OK;
144     }
145     auto& cs = fc.GetConstraints();
146     for (size_t i = 0; i < cs.size(); i++) {
147         const auto& c = cs[i];
148         switch (c.col) {
149             case TS:
150                 FilterTS(c.op, argv[i], InstantsObj_.TimeStampData());
151                 break;
152             case NAME:
153                 indexMap_->MixRange(c.op,
154                                     dataCache_->GetConstDataIndex(
155                                         std::string(reinterpret_cast<const char*>(sqlite3_value_text(argv[i])))),
156                                     InstantsObj_.NameIndexsData());
157                 break;
158             case REF:
159                 indexMap_->MixRange(c.op, static_cast<uint32_t>(sqlite3_value_int(argv[i])),
160                                     InstantsObj_.InternalTidsData());
161                 break;
162             case WAKEUP_FROM:
163                 indexMap_->MixRange(c.op, static_cast<int64_t>(sqlite3_value_int64(argv[i])),
164                                     InstantsObj_.WakeupFromPidsData());
165                 break;
166             default:
167                 break;
168         }
169     }
170 
171     auto orderbys = fc.GetOrderBys();
172     for (auto i = orderbys.size(); i > 0;) {
173         i--;
174         switch (orderbys[i].iColumn) {
175             case TS:
176                 indexMap_->SortBy(orderbys[i].desc);
177                 break;
178             case NAME:
179                 indexMap_->SortBy(orderbys[i].desc);
180                 break;
181             case REF:
182                 indexMap_->SortBy(orderbys[i].desc);
183                 break;
184             case WAKEUP_FROM:
185                 indexMap_->SortBy(orderbys[i].desc);
186                 break;
187             default:
188                 break;
189         }
190     }
191 
192     return SQLITE_OK;
193 }
194 
Column(int32_t column) const195 int32_t InstantsTable::Cursor::Column(int32_t column) const
196 {
197     size_t stringIdentity = static_cast<size_t>(InstantsObj_.NameIndexsData()[CurrentRow()]);
198     switch (column) {
199         case TS:
200             sqlite3_result_int64(context_, static_cast<int64_t>(InstantsObj_.TimeStampData()[CurrentRow()]));
201             break;
202         case NAME: {
203             sqlite3_result_text(context_, dataCache_->GetDataFromDict(stringIdentity).c_str(), STR_DEFAULT_LEN,
204                                 nullptr);
205             break;
206         }
207         case REF:
208             sqlite3_result_int64(context_, static_cast<int32_t>(InstantsObj_.InternalTidsData()[CurrentRow()]));
209             break;
210         case WAKEUP_FROM:
211             sqlite3_result_int64(context_, static_cast<int32_t>(InstantsObj_.WakeupFromPidsData()[CurrentRow()]));
212             break;
213         case REF_TYPE: {
214             sqlite3_result_text(context_, "itid", STR_DEFAULT_LEN, nullptr);
215             break;
216         }
217         case VALUE: {
218             sqlite3_result_double(context_, 0.0);
219             break;
220         }
221         default:
222             TS_LOGF("Unregistered column : %d", column);
223             break;
224     }
225     return SQLITE_OK;
226 }
227 } // namespace TraceStreamer
228 } // namespace SysTuning
229