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 "raw_table.h"
17 namespace SysTuning {
18 namespace TraceStreamer {
19 enum Index { ID = 0, TYPE, TS, NAME, CPU, INTERNAL_TID };
20 enum RawType { RAW_CPU_IDLE = 1, RAW_SCHED_WAKEUP = 2, RAW_SCHED_WAKING = 3 };
GetNameIndex(const std::string & name)21 uint32_t GetNameIndex(const std::string& name)
22 {
23 if (name == "cpu_idle") {
24 return RAW_CPU_IDLE;
25 } else if (name == "sched_wakeup") {
26 return RAW_SCHED_WAKEUP;
27 } else if (name == "sched_waking") {
28 return RAW_SCHED_WAKING;
29 } else {
30 return INVALID_UINT32;
31 }
32 }
RawTable(const TraceDataCache * dataCache)33 RawTable::RawTable(const TraceDataCache* dataCache) : TableBase(dataCache)
34 {
35 tableColumn_.push_back(TableBase::ColumnInfo("id", "INTEGER"));
36 tableColumn_.push_back(TableBase::ColumnInfo("type", "TEXT"));
37 tableColumn_.push_back(TableBase::ColumnInfo("ts", "INTEGER"));
38 tableColumn_.push_back(TableBase::ColumnInfo("name", "TEXT"));
39 tableColumn_.push_back(TableBase::ColumnInfo("cpu", "INTEGER"));
40 tableColumn_.push_back(TableBase::ColumnInfo("itid", "INTEGER"));
41 tablePriKey_.push_back("id");
42 }
43
~RawTable()44 RawTable::~RawTable() {}
45
EstimateFilterCost(FilterConstraints & fc,EstimatedIndexInfo & ei)46 void RawTable::EstimateFilterCost(FilterConstraints& fc, EstimatedIndexInfo& ei)
47 {
48 constexpr double filterBaseCost = 1000.0; // set-up and tear-down
49 constexpr double indexCost = 2.0;
50 ei.estimatedCost = filterBaseCost;
51
52 auto rowCount = dataCache_->GetConstRawTableData().Size();
53 if (rowCount == 0 || rowCount == 1) {
54 ei.estimatedRows = rowCount;
55 ei.estimatedCost += indexCost * rowCount;
56 return;
57 }
58
59 double filterCost = 0.0;
60 auto constraints = fc.GetConstraints();
61 if (constraints.empty()) { // scan all rows
62 filterCost = rowCount;
63 } else {
64 FilterByConstraint(fc, filterCost, rowCount);
65 }
66 ei.estimatedCost += filterCost;
67 ei.estimatedRows = rowCount;
68 ei.estimatedCost += rowCount * indexCost;
69
70 ei.isOrdered = true;
71 auto orderbys = fc.GetOrderBys();
72 for (auto i = 0; i < orderbys.size(); i++) {
73 switch (orderbys[i].iColumn) {
74 case ID:
75 break;
76 default: // other columns can be sorted by SQLite
77 ei.isOrdered = false;
78 break;
79 }
80 }
81 }
82
FilterByConstraint(FilterConstraints & fc,double & filterCost,size_t rowCount)83 void RawTable::FilterByConstraint(FilterConstraints& fc, double& filterCost, size_t rowCount)
84 {
85 auto fcConstraints = fc.GetConstraints();
86 for (int32_t i = 0; i < static_cast<int32_t>(fcConstraints.size()); i++) {
87 if (rowCount <= 1) {
88 // only one row or nothing, needn't filter by constraint
89 filterCost += rowCount;
90 break;
91 }
92 const auto& c = fcConstraints[i];
93 switch (c.col) {
94 case ID: {
95 if (CanFilterId(c.op, rowCount)) {
96 fc.UpdateConstraint(i, true);
97 filterCost += 1; // id can position by 1 step
98 } else {
99 filterCost += rowCount; // scan all rows
100 }
101 break;
102 }
103 default: // other column
104 filterCost += rowCount; // scan all rows
105 break;
106 }
107 }
108 }
109
CreateCursor()110 std::unique_ptr<TableBase::Cursor> RawTable::CreateCursor()
111 {
112 return std::make_unique<Cursor>(dataCache_, this);
113 }
114
Cursor(const TraceDataCache * dataCache,TableBase * table)115 RawTable::Cursor::Cursor(const TraceDataCache* dataCache, TableBase* table)
116 : TableBase::Cursor(dataCache, table, static_cast<uint32_t>(dataCache->GetConstRawTableData().Size())),
117 rawObj_(dataCache->GetConstRawTableData())
118 {
119 }
120
~Cursor()121 RawTable::Cursor::~Cursor() {}
Filter(const FilterConstraints & fc,sqlite3_value ** argv)122 int32_t RawTable::Cursor::Filter(const FilterConstraints& fc, sqlite3_value** argv)
123 {
124 // reset indexMap_
125 indexMap_ = std::make_unique<IndexMap>(0, rowCount_);
126
127 if (rowCount_ <= 0) {
128 return SQLITE_OK;
129 }
130
131 auto& cs = fc.GetConstraints();
132 for (size_t i = 0; i < cs.size(); i++) {
133 const auto& c = cs[i];
134 switch (c.col) {
135 case ID:
136 FilterId(c.op, argv[i]);
137 break;
138 case NAME:
139 indexMap_->MixRange(
140 c.op, GetNameIndex(std::string(reinterpret_cast<const char*>(sqlite3_value_text(argv[i])))),
141 rawObj_.NameData());
142 break;
143 case TS:
144 FilterTS(c.op, argv[i], rawObj_.TimeStampData());
145 break;
146 case INTERNAL_TID:
147 indexMap_->MixRange(c.op, static_cast<uint32_t>(sqlite3_value_int(argv[i])),
148 rawObj_.InternalTidsData());
149 break;
150 default:
151 break;
152 }
153 }
154
155 auto orderbys = fc.GetOrderBys();
156 for (auto i = orderbys.size(); i > 0;) {
157 i--;
158 switch (orderbys[i].iColumn) {
159 case ID:
160 indexMap_->SortBy(orderbys[i].desc);
161 break;
162 default:
163 break;
164 }
165 }
166
167 return SQLITE_OK;
168 }
169
Column(int32_t column) const170 int32_t RawTable::Cursor::Column(int32_t column) const
171 {
172 switch (column) {
173 case ID:
174 sqlite3_result_int64(context_, static_cast<int32_t>(CurrentRow()));
175 break;
176 case TYPE:
177 sqlite3_result_text(context_, "raw", STR_DEFAULT_LEN, nullptr);
178 break;
179 case TS:
180 sqlite3_result_int64(context_, static_cast<int64_t>(rawObj_.TimeStampData()[CurrentRow()]));
181 break;
182 case NAME: {
183 if (rawObj_.NameData()[CurrentRow()] == RAW_CPU_IDLE) {
184 sqlite3_result_text(context_, "cpu_idle", STR_DEFAULT_LEN, nullptr);
185 } else if (rawObj_.NameData()[CurrentRow()] == RAW_SCHED_WAKEUP) {
186 sqlite3_result_text(context_, "sched_wakeup", STR_DEFAULT_LEN, nullptr);
187 } else if (rawObj_.NameData()[CurrentRow()] == RAW_SCHED_WAKING) {
188 sqlite3_result_text(context_, "sched_waking", STR_DEFAULT_LEN, nullptr);
189 }
190 break;
191 }
192 case CPU:
193 sqlite3_result_int64(context_, static_cast<int32_t>(rawObj_.CpuData()[CurrentRow()]));
194 break;
195 case INTERNAL_TID:
196 sqlite3_result_int64(context_, static_cast<int32_t>(rawObj_.InternalTidData()[CurrentRow()]));
197 break;
198 default:
199 TS_LOGF("Unregistered column : %d", column);
200 break;
201 }
202 return SQLITE_OK;
203 }
204 } // namespace TraceStreamer
205 } // namespace SysTuning
206