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