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 "thread_state_table.h"
17
18 #include <cmath>
19
20 namespace SysTuning {
21 namespace TraceStreamer {
22 namespace {
23 enum Index { ID = 0, TYPE, TS, DUR, CPU, INTERNAL_TID, TID, PID, STATE };
24 }
ThreadStateTable(const TraceDataCache * dataCache)25 ThreadStateTable::ThreadStateTable(const TraceDataCache* dataCache) : TableBase(dataCache)
26 {
27 tableColumn_.push_back(TableBase::ColumnInfo("id", "INTEGER"));
28 tableColumn_.push_back(TableBase::ColumnInfo("type", "TEXT"));
29 tableColumn_.push_back(TableBase::ColumnInfo("ts", "INTEGER"));
30 tableColumn_.push_back(TableBase::ColumnInfo("dur", "INTEGER"));
31 tableColumn_.push_back(TableBase::ColumnInfo("cpu", "INTEGER"));
32 tableColumn_.push_back(TableBase::ColumnInfo("itid", "INTEGER"));
33 tableColumn_.push_back(TableBase::ColumnInfo("tid", "INTEGER"));
34 tableColumn_.push_back(TableBase::ColumnInfo("pid", "INTEGER"));
35 tableColumn_.push_back(TableBase::ColumnInfo("state", "TEXT"));
36 tablePriKey_.push_back("id");
37 }
38
~ThreadStateTable()39 ThreadStateTable::~ThreadStateTable() {}
40
EstimateFilterCost(FilterConstraints & fc,EstimatedIndexInfo & ei)41 void ThreadStateTable::EstimateFilterCost(FilterConstraints& fc, EstimatedIndexInfo& ei)
42 {
43 constexpr double filterBaseCost = 1000.0; // set-up and tear-down
44 constexpr double indexCost = 2.0;
45 ei.estimatedCost = filterBaseCost;
46
47 auto rowCount = dataCache_->GetConstThreadStateData().Size();
48 if (rowCount == 0 || rowCount == 1) {
49 ei.estimatedRows = rowCount;
50 ei.estimatedCost += indexCost * rowCount;
51 return;
52 }
53
54 double filterCost = 0.0;
55 auto constraints = fc.GetConstraints();
56 if (constraints.empty()) { // scan all rows
57 filterCost = rowCount;
58 } else {
59 FilterByConstraint(fc, filterCost, rowCount);
60 }
61 ei.estimatedCost += filterCost;
62 ei.estimatedRows = rowCount;
63 ei.estimatedCost += rowCount * indexCost;
64
65 ei.isOrdered = true;
66 auto orderbys = fc.GetOrderBys();
67 for (auto i = 0; i < orderbys.size(); i++) {
68 switch (orderbys[i].iColumn) {
69 case ID:
70 case TS:
71 break;
72 default: // other columns can be sorted by SQLite
73 ei.isOrdered = false;
74 break;
75 }
76 }
77 }
78
FilterByConstraint(FilterConstraints & fc,double & filterCost,size_t rowCount)79 void ThreadStateTable::FilterByConstraint(FilterConstraints& fc, double& filterCost, size_t rowCount)
80 {
81 auto fcConstraints = fc.GetConstraints();
82 for (int i = 0; i < static_cast<int>(fcConstraints.size()); i++) {
83 if (rowCount <= 1) {
84 // only one row or nothing, needn't filter by constraint
85 filterCost += rowCount;
86 break;
87 }
88 const auto& c = fcConstraints[i];
89 switch (c.col) {
90 case ID: {
91 if (CanFilterId(c.op, rowCount)) {
92 fc.UpdateConstraint(i, true);
93 filterCost += 1; // id can position by 1 step
94 } else {
95 filterCost += rowCount; // scan all rows
96 }
97 break;
98 }
99 case TS: {
100 auto oldRowCount = rowCount;
101 if (CanFilterSorted(c.op, rowCount)) {
102 fc.UpdateConstraint(i, true);
103 filterCost += log2(oldRowCount); // binary search
104 } else {
105 filterCost += oldRowCount;
106 }
107 break;
108 }
109 default: // other column
110 filterCost += rowCount; // scan all rows
111 break;
112 }
113 }
114 }
115
CanFilterId(const char op,size_t & rowCount)116 bool ThreadStateTable::CanFilterId(const char op, size_t& rowCount)
117 {
118 switch (op) {
119 case SQLITE_INDEX_CONSTRAINT_EQ:
120 rowCount = 1;
121 break;
122 case SQLITE_INDEX_CONSTRAINT_GT:
123 case SQLITE_INDEX_CONSTRAINT_GE:
124 case SQLITE_INDEX_CONSTRAINT_LE:
125 case SQLITE_INDEX_CONSTRAINT_LT:
126 // assume filter out a half of rows
127 rowCount = (rowCount >> 1);
128 break;
129 default:
130 return false;
131 }
132 return true;
133 }
134
CanFilterSorted(const char op,size_t & rowCount) const135 bool ThreadStateTable::CanFilterSorted(const char op, size_t& rowCount) const
136 {
137 switch (op) {
138 case SQLITE_INDEX_CONSTRAINT_EQ:
139 rowCount = rowCount / log2(rowCount);
140 break;
141 case SQLITE_INDEX_CONSTRAINT_GT:
142 case SQLITE_INDEX_CONSTRAINT_GE:
143 case SQLITE_INDEX_CONSTRAINT_LE:
144 case SQLITE_INDEX_CONSTRAINT_LT:
145 rowCount = (rowCount >> 1);
146 break;
147 default:
148 return false;
149 }
150 return true;
151 }
152
CreateCursor()153 std::unique_ptr<TableBase::Cursor> ThreadStateTable::CreateCursor()
154 {
155 return std::make_unique<Cursor>(dataCache_, this);
156 }
157
Cursor(const TraceDataCache * dataCache,TableBase * table)158 ThreadStateTable::Cursor::Cursor(const TraceDataCache* dataCache, TableBase* table)
159 : TableBase::Cursor(dataCache, table, dataCache->GetConstThreadStateData().Size()),
160 threadStateObj_(dataCache->GetConstThreadStateData())
161 {
162 }
163
~Cursor()164 ThreadStateTable::Cursor::~Cursor() {}
165
Filter(const FilterConstraints & fc,sqlite3_value ** argv)166 int ThreadStateTable::Cursor::Filter(const FilterConstraints& fc, sqlite3_value** argv)
167 {
168 // reset
169 if (rowCount_ <= 0) {
170 return SQLITE_OK;
171 }
172 IndexMap* indexMapBack = indexMap_.get();
173 if (indexMap_->HasData()) {
174 indexMapBack = std::make_unique<IndexMap>(0, rowCount_).get();
175 }
176 auto& cs = fc.GetConstraints();
177 for (size_t i = 0; i < cs.size(); i++) {
178 const auto& c = cs[i];
179 switch (c.col) {
180 case ID:
181 indexMapBack->FilterId(c.op, argv[i]);
182 break;
183 case TS:
184 indexMapBack->FilterTS(c.op, argv[i], threadStateObj_.TimeStamsData());
185 break;
186 case INTERNAL_TID:
187 indexMapBack->MixRange(c.op, static_cast<uint32_t>(sqlite3_value_int(argv[i])),
188 threadStateObj_.ItidsData());
189 break;
190 case TID:
191 indexMapBack->MixRange(c.op, static_cast<uint32_t>(sqlite3_value_int(argv[i])),
192 threadStateObj_.TidsData());
193 break;
194 case PID:
195 indexMapBack->MixRange(c.op, static_cast<uint32_t>(sqlite3_value_int(argv[i])),
196 threadStateObj_.PidsData());
197 break;
198 case DUR:
199 indexMapBack->MixRange(c.op, static_cast<uint64_t>(sqlite3_value_int64(argv[i])),
200 threadStateObj_.DursData());
201 break;
202 case CPU:
203 indexMapBack->MixRange(c.op, static_cast<uint32_t>(sqlite3_value_int(argv[i])),
204 threadStateObj_.CpusData());
205 break;
206 case STATE:
207 indexMapBack->MixRange(c.op,
208 dataCache_->GetConstDataIndex(
209 std::string(reinterpret_cast<const char*>(sqlite3_value_text(argv[i])))),
210 threadStateObj_.StatesData());
211 break;
212 default:
213 break;
214 }
215 }
216 if (indexMap_->HasData()) {
217 indexMap_->Merge(indexMapBack);
218 }
219
220 auto orderbys = fc.GetOrderBys();
221 for (auto i = orderbys.size(); i > 0;) {
222 i--;
223 switch (orderbys[i].iColumn) {
224 case ID:
225 case TS:
226 indexMap_->SortBy(orderbys[i].desc);
227 break;
228 default:
229 break;
230 }
231 }
232
233 return SQLITE_OK;
234 }
235
Column(int col) const236 int ThreadStateTable::Cursor::Column(int col) const
237 {
238 switch (col) {
239 case ID:
240 sqlite3_result_int64(context_, static_cast<sqlite3_int64>(CurrentRow()));
241 break;
242 case TYPE:
243 sqlite3_result_text(context_, "thread_state", STR_DEFAULT_LEN, nullptr);
244 break;
245 case TS:
246 sqlite3_result_int64(context_, static_cast<sqlite3_int64>(threadStateObj_.TimeStamsData()[CurrentRow()]));
247 break;
248 case DUR:
249 if (static_cast<sqlite3_int64>(threadStateObj_.DursData()[CurrentRow()]) != INVALID_TIME) {
250 sqlite3_result_int64(context_, static_cast<sqlite3_int64>(threadStateObj_.DursData()[CurrentRow()]));
251 }
252 break;
253 case CPU:
254 if (threadStateObj_.CpusData()[CurrentRow()] != INVALID_CPU) {
255 sqlite3_result_int64(context_, static_cast<sqlite3_int64>(threadStateObj_.CpusData()[CurrentRow()]));
256 }
257 break;
258 case INTERNAL_TID:
259 sqlite3_result_int64(context_, static_cast<sqlite3_int64>(threadStateObj_.ItidsData()[CurrentRow()]));
260 break;
261 case TID:
262 sqlite3_result_int64(context_, static_cast<sqlite3_int64>(threadStateObj_.TidsData()[CurrentRow()]));
263 break;
264 case PID:
265 sqlite3_result_int64(context_, static_cast<sqlite3_int64>(threadStateObj_.PidsData()[CurrentRow()]));
266 break;
267 case STATE: {
268 const std::string& str = dataCache_->GetConstSchedStateData(threadStateObj_.StatesData()[CurrentRow()]);
269 sqlite3_result_text(context_, str.c_str(), STR_DEFAULT_LEN, nullptr);
270 break;
271 }
272 default:
273 TS_LOGF("Unregistered column : %d", col);
274 break;
275 }
276 return SQLITE_OK;
277 }
278 } // namespace TraceStreamer
279 } // namespace SysTuning
280