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 "perf_sample_table.h"
17
18 namespace SysTuning {
19 namespace TraceStreamer {
20 enum Index {
21 ID = 0,
22 CALLCHAIN_ID,
23 TIMESTAMP,
24 THREAD_ID,
25 EVENT_COUNT,
26 EVENT_TYPE_ID,
27 TIMESTAMP_TRACE,
28 CPU_ID,
29 THREAD_STATE
30 };
PerfSampleTable(const TraceDataCache * dataCache)31 PerfSampleTable::PerfSampleTable(const TraceDataCache* dataCache) : TableBase(dataCache)
32 {
33 tableColumn_.push_back(TableBase::ColumnInfo("id", "INTEGER"));
34 tableColumn_.push_back(TableBase::ColumnInfo("callchain_id", "INTEGER"));
35 tableColumn_.push_back(TableBase::ColumnInfo("timeStamp", "INTEGER"));
36 tableColumn_.push_back(TableBase::ColumnInfo("thread_id", "INTEGER"));
37 tableColumn_.push_back(TableBase::ColumnInfo("event_count", "INTEGER"));
38 tableColumn_.push_back(TableBase::ColumnInfo("event_type_id", "INTEGER"));
39 tableColumn_.push_back(TableBase::ColumnInfo("timestamp_trace", "INTEGER"));
40 tableColumn_.push_back(TableBase::ColumnInfo("cpu_id", "INTEGER"));
41 tableColumn_.push_back(TableBase::ColumnInfo("thread_state", "TEXT"));
42 tablePriKey_.push_back("id");
43 }
44
~PerfSampleTable()45 PerfSampleTable::~PerfSampleTable() {}
46
EstimateFilterCost(FilterConstraints & fc,EstimatedIndexInfo & ei)47 void PerfSampleTable::EstimateFilterCost(FilterConstraints& fc, EstimatedIndexInfo& ei)
48 {
49 constexpr double filterBaseCost = 1000.0; // set-up and tear-down
50 constexpr double indexCost = 2.0;
51 ei.estimatedCost = filterBaseCost;
52
53 auto rowCount = dataCache_->GetConstPerfSampleData().Size();
54 if (rowCount == 0 || rowCount == 1) {
55 ei.estimatedRows = rowCount;
56 ei.estimatedCost += indexCost * rowCount;
57 return;
58 }
59
60 double filterCost = 0.0;
61 auto constraints = fc.GetConstraints();
62 if (constraints.empty()) { // scan all rows
63 filterCost = rowCount;
64 } else {
65 FilterByConstraint(fc, filterCost, rowCount);
66 }
67 ei.estimatedCost += filterCost;
68 ei.estimatedRows = rowCount;
69 ei.estimatedCost += rowCount * indexCost;
70
71 ei.isOrdered = true;
72 auto orderbys = fc.GetOrderBys();
73 for (auto i = 0; i < orderbys.size(); i++) {
74 switch (orderbys[i].iColumn) {
75 case ID:
76 break;
77 default: // other columns can be sorted by SQLite
78 ei.isOrdered = false;
79 break;
80 }
81 }
82 }
83
FilterByConstraint(FilterConstraints & fc,double & filterCost,size_t rowCount)84 void PerfSampleTable::FilterByConstraint(FilterConstraints& fc, double& filterCost, size_t rowCount)
85 {
86 auto fcConstraints = fc.GetConstraints();
87 for (int32_t i = 0; i < static_cast<int32_t>(fcConstraints.size()); i++) {
88 if (rowCount <= 1) {
89 // only one row or nothing, needn't filter by constraint
90 filterCost += rowCount;
91 break;
92 }
93 const auto& c = fcConstraints[i];
94 switch (c.col) {
95 case ID: {
96 if (CanFilterId(c.op, rowCount)) {
97 fc.UpdateConstraint(i, true);
98 filterCost += 1; // id can position by 1 step
99 } else {
100 filterCost += rowCount; // scan all rows
101 }
102 break;
103 }
104 default: // other column
105 filterCost += rowCount; // scan all rows
106 break;
107 }
108 }
109 }
110
CreateCursor()111 std::unique_ptr<TableBase::Cursor> PerfSampleTable::CreateCursor()
112 {
113 return std::make_unique<Cursor>(dataCache_, this);
114 }
115
Cursor(const TraceDataCache * dataCache,TableBase * table)116 PerfSampleTable::Cursor::Cursor(const TraceDataCache* dataCache, TableBase* table)
117 : TableBase::Cursor(dataCache, table, static_cast<uint32_t>(dataCache->GetConstPerfSampleData().Size())),
118 perfSampleObj_(dataCache->GetConstPerfSampleData())
119 {
120 }
121
~Cursor()122 PerfSampleTable::Cursor::~Cursor() {}
123
Filter(const FilterConstraints & fc,sqlite3_value ** argv)124 int32_t PerfSampleTable::Cursor::Filter(const FilterConstraints& fc, sqlite3_value** argv)
125 {
126 // reset indexMap_
127 indexMap_ = std::make_unique<IndexMap>(0, rowCount_);
128
129 if (rowCount_ <= 0) {
130 return SQLITE_OK;
131 }
132
133 auto& cs = fc.GetConstraints();
134 for (size_t i = 0; i < cs.size(); i++) {
135 const auto& c = cs[i];
136 switch (c.col) {
137 case ID:
138 FilterId(c.op, argv[i]);
139 break;
140 case CALLCHAIN_ID:
141 indexMap_->MixRange(c.op, static_cast<uint32_t>(sqlite3_value_int64(argv[i])),
142 perfSampleObj_.SampleIds());
143 break;
144 case THREAD_ID:
145 indexMap_->MixRange(c.op, static_cast<uint32_t>(sqlite3_value_int64(argv[i])), perfSampleObj_.Tids());
146 break;
147 case EVENT_TYPE_ID:
148 indexMap_->MixRange(c.op, static_cast<uint64_t>(sqlite3_value_int64(argv[i])),
149 perfSampleObj_.EventTypeIds());
150 break;
151 case CPU_ID:
152 indexMap_->MixRange(c.op, static_cast<uint64_t>(sqlite3_value_int64(argv[i])), perfSampleObj_.CpuIds());
153 break;
154 default:
155 break;
156 }
157 }
158 auto orderbys = fc.GetOrderBys();
159 for (auto i = orderbys.size(); i > 0;) {
160 i--;
161 switch (orderbys[i].iColumn) {
162 case ID:
163 indexMap_->SortBy(orderbys[i].desc);
164 break;
165 default:
166 break;
167 }
168 }
169
170 return SQLITE_OK;
171 }
172
Column(int32_t column) const173 int32_t PerfSampleTable::Cursor::Column(int32_t column) const
174 {
175 switch (column) {
176 case ID:
177 sqlite3_result_int64(context_, static_cast<int32_t>(perfSampleObj_.IdsData()[CurrentRow()]));
178 break;
179 case CALLCHAIN_ID:
180 if (perfSampleObj_.SampleIds()[CurrentRow()] != INVALID_UINT32) {
181 sqlite3_result_int64(context_, static_cast<int64_t>(perfSampleObj_.SampleIds()[CurrentRow()]));
182 } else {
183 sqlite3_result_int64(context_, static_cast<int64_t>(INVALID_CALL_CHAIN_ID));
184 }
185 break;
186 case TIMESTAMP:
187 sqlite3_result_int64(context_, static_cast<int64_t>(perfSampleObj_.TimeStampData()[CurrentRow()]));
188 break;
189 case THREAD_ID:
190 sqlite3_result_int64(context_, static_cast<int64_t>(perfSampleObj_.Tids()[CurrentRow()]));
191 break;
192 case EVENT_COUNT:
193 sqlite3_result_int64(context_, static_cast<int64_t>(perfSampleObj_.EventCounts()[CurrentRow()]));
194 break;
195 case EVENT_TYPE_ID:
196 sqlite3_result_int64(context_, static_cast<int64_t>(perfSampleObj_.EventTypeIds()[CurrentRow()]));
197 break;
198 case TIMESTAMP_TRACE:
199 sqlite3_result_int64(context_, static_cast<int64_t>(perfSampleObj_.TimestampTraces()[CurrentRow()]));
200 break;
201 case CPU_ID:
202 sqlite3_result_int64(context_, static_cast<int64_t>(perfSampleObj_.CpuIds()[CurrentRow()]));
203 break;
204 case THREAD_STATE:
205 if (perfSampleObj_.ThreadStates()[CurrentRow()] != INVALID_UINT64) {
206 auto threadStateIndex = static_cast<size_t>(perfSampleObj_.ThreadStates()[CurrentRow()]);
207 sqlite3_result_text(context_, dataCache_->GetDataFromDict(threadStateIndex).c_str(), STR_DEFAULT_LEN,
208 nullptr);
209 }
210 break;
211 default:
212 TS_LOGF("Unregistered column : %d", column);
213 break;
214 }
215 return SQLITE_OK;
216 }
217 } // namespace TraceStreamer
218 } // namespace SysTuning
219