1 /*
2  * Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved.
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 enum class Index : int32_t { ID = 0, THREAD_ID, PROCESS_ID, THREAD_NAME };
PerfThreadTable(const TraceDataCache *dataCache)21 PerfThreadTable::PerfThreadTable(const TraceDataCache *dataCache) : TableBase(dataCache)
22 {
23     tableColumn_.push_back(TableBase::ColumnInfo("id", "INTEGER"));
24     tableColumn_.push_back(TableBase::ColumnInfo("thread_id", "INTEGER"));
25     tableColumn_.push_back(TableBase::ColumnInfo("process_id", "INTEGER"));
26     tableColumn_.push_back(TableBase::ColumnInfo("thread_name", "TEXT"));
27     tablePriKey_.push_back("id");
28 }
29 
~PerfThreadTable()30 PerfThreadTable::~PerfThreadTable() {}
31 
FilterByConstraint(FilterConstraints &threadfc, double &threadfilterCost, size_t threadRowCnt, uint32_t threadcurrenti)32 void PerfThreadTable::FilterByConstraint(FilterConstraints &threadfc,
33                                          double &threadfilterCost,
34                                          size_t threadRowCnt,
35                                          uint32_t threadcurrenti)
36 {
37     const auto &perfThreadc = threadfc.GetConstraints()[threadcurrenti];
38     switch (static_cast<Index>(perfThreadc.col)) {
39         case Index::ID: {
40             if (CanFilterId(perfThreadc.op, threadRowCnt)) {
41                 threadfc.UpdateConstraint(threadcurrenti, true);
42                 threadfilterCost += 1; // id can position by 1 step
43             } else {
44                 threadfilterCost += threadRowCnt; // scan all rows
45             }
46             break;
47         }
48         default:                              // other column
49             threadfilterCost += threadRowCnt; // scan all rows
50             break;
51     }
52 }
53 
CreateCursor()54 std::unique_ptr<TableBase::Cursor> PerfThreadTable::CreateCursor()
55 {
56     return std::make_unique<Cursor>(dataCache_, this);
57 }
58 
Cursor(const TraceDataCache *dataCache, TableBase *table)59 PerfThreadTable::Cursor::Cursor(const TraceDataCache *dataCache, TableBase *table)
60     : TableBase::Cursor(dataCache, table, static_cast<uint32_t>(dataCache->GetConstPerfThreadData().Size())),
61       perfThreadObj_(dataCache->GetConstPerfThreadData())
62 {
63 }
64 
~Cursor()65 PerfThreadTable::Cursor::~Cursor() {}
66 
Filter(const FilterConstraints &fc, sqlite3_value **argv)67 int32_t PerfThreadTable::Cursor::Filter(const FilterConstraints &fc, sqlite3_value **argv)
68 {
69     // reset indexMap_
70     indexMap_ = std::make_unique<IndexMap>(0, rowCount_);
71 
72     if (rowCount_ <= 0) {
73         return SQLITE_OK;
74     }
75 
76     auto perfThreadCs = fc.GetConstraints();
77     std::set<uint32_t> sId = {static_cast<uint32_t>(Index::ID)};
78     SwapIndexFront(perfThreadCs, sId);
79     for (size_t i = 0; i < perfThreadCs.size(); i++) {
80         const auto &c = perfThreadCs[i];
81         switch (static_cast<Index>(c.col)) {
82             case Index::ID:
83                 FilterId(c.op, argv[c.idxInaConstraint]);
84                 break;
85             case Index::THREAD_ID:
86                 indexMap_->MixRange(c.op, static_cast<uint32_t>(sqlite3_value_int64(argv[c.idxInaConstraint])),
87                                     perfThreadObj_.Tids());
88                 break;
89             case Index::PROCESS_ID:
90                 indexMap_->MixRange(c.op, static_cast<uint32_t>(sqlite3_value_int64(argv[c.idxInaConstraint])),
91                                     perfThreadObj_.Pids());
92                 break;
93             default:
94                 break;
95         }
96     }
97 
98     auto perfThreadOrderbys = fc.GetOrderBys();
99     for (auto i = perfThreadOrderbys.size(); i > 0;) {
100         i--;
101         switch (static_cast<Index>(perfThreadOrderbys[i].iColumn)) {
102             case Index::ID:
103                 indexMap_->SortBy(perfThreadOrderbys[i].desc);
104                 break;
105             default:
106                 break;
107         }
108     }
109 
110     return SQLITE_OK;
111 }
112 
Column(int32_t column) const113 int32_t PerfThreadTable::Cursor::Column(int32_t column) const
114 {
115     switch (static_cast<Index>(column)) {
116         case Index::ID:
117             sqlite3_result_int64(context_, static_cast<int32_t>(perfThreadObj_.IdsData()[CurrentRow()]));
118             break;
119         case Index::THREAD_ID:
120             sqlite3_result_int64(context_, static_cast<int64_t>(perfThreadObj_.Tids()[CurrentRow()]));
121             break;
122         case Index::PROCESS_ID:
123             sqlite3_result_int64(context_, static_cast<int64_t>(perfThreadObj_.Pids()[CurrentRow()]));
124             break;
125         case Index::THREAD_NAME:
126             if (perfThreadObj_.ThreadNames()[CurrentRow()] != INVALID_UINT64) {
127                 auto threadNameIndex = static_cast<size_t>(perfThreadObj_.ThreadNames()[CurrentRow()]);
128                 if (dataCache_->GetDataFromDict(threadNameIndex).empty()) {
129                     break;
130                 }
131                 sqlite3_result_text(context_, dataCache_->GetDataFromDict(threadNameIndex).c_str(), STR_DEFAULT_LEN,
132                                     nullptr);
133             }
134             break;
135         default:
136             TS_LOGF("Unregistered column : %d", column);
137             break;
138     }
139     return SQLITE_OK;
140 }
141 
GetOrbyes(FilterConstraints &threadfc, EstimatedIndexInfo &threadei)142 void PerfThreadTable::GetOrbyes(FilterConstraints &threadfc, EstimatedIndexInfo &threadei)
143 {
144     auto threadorderbys = threadfc.GetOrderBys();
145     for (auto i = 0; i < threadorderbys.size(); i++) {
146         switch (static_cast<Index>(threadorderbys[i].iColumn)) {
147             case Index::ID:
148                 break;
149             default: // other columns can be sorted by SQLite
150                 threadei.isOrdered = false;
151                 break;
152         }
153     }
154 }
155 } // namespace TraceStreamer
156 } // namespace SysTuning
157