• 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 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[i]);
84                 break;
85             case Index::THREAD_ID:
86                 indexMap_->MixRange(c.op, static_cast<uint32_t>(sqlite3_value_int64(argv[i])), perfThreadObj_.Tids());
87                 break;
88             case Index::PROCESS_ID:
89                 indexMap_->MixRange(c.op, static_cast<uint32_t>(sqlite3_value_int64(argv[i])), perfThreadObj_.Pids());
90                 break;
91             default:
92                 break;
93         }
94     }
95 
96     auto perfThreadOrderbys = fc.GetOrderBys();
97     for (auto i = perfThreadOrderbys.size(); i > 0;) {
98         i--;
99         switch (static_cast<Index>(perfThreadOrderbys[i].iColumn)) {
100             case Index::ID:
101                 indexMap_->SortBy(perfThreadOrderbys[i].desc);
102                 break;
103             default:
104                 break;
105         }
106     }
107 
108     return SQLITE_OK;
109 }
110 
Column(int32_t column) const111 int32_t PerfThreadTable::Cursor::Column(int32_t column) const
112 {
113     switch (static_cast<Index>(column)) {
114         case Index::ID:
115             sqlite3_result_int64(context_, static_cast<int32_t>(perfThreadObj_.IdsData()[CurrentRow()]));
116             break;
117         case Index::THREAD_ID:
118             sqlite3_result_int64(context_, static_cast<int64_t>(perfThreadObj_.Tids()[CurrentRow()]));
119             break;
120         case Index::PROCESS_ID:
121             sqlite3_result_int64(context_, static_cast<int64_t>(perfThreadObj_.Pids()[CurrentRow()]));
122             break;
123         case Index::THREAD_NAME:
124             if (perfThreadObj_.ThreadNames()[CurrentRow()] != INVALID_UINT64) {
125                 auto threadNameIndex = static_cast<size_t>(perfThreadObj_.ThreadNames()[CurrentRow()]);
126                 if (dataCache_->GetDataFromDict(threadNameIndex).empty()) {
127                     break;
128                 }
129                 sqlite3_result_text(context_, dataCache_->GetDataFromDict(threadNameIndex).c_str(), STR_DEFAULT_LEN,
130                                     nullptr);
131             }
132             break;
133         default:
134             TS_LOGF("Unregistered column : %d", column);
135             break;
136     }
137     return SQLITE_OK;
138 }
139 
GetOrbyes(FilterConstraints & threadfc,EstimatedIndexInfo & threadei)140 void PerfThreadTable::GetOrbyes(FilterConstraints& threadfc, EstimatedIndexInfo& threadei)
141 {
142     auto threadorderbys = threadfc.GetOrderBys();
143     for (auto i = 0; i < threadorderbys.size(); i++) {
144         switch (static_cast<Index>(threadorderbys[i].iColumn)) {
145             case Index::ID:
146                 break;
147             default: // other columns can be sorted by SQLite
148                 threadei.isOrdered = false;
149                 break;
150         }
151     }
152 }
153 } // namespace TraceStreamer
154 } // namespace SysTuning
155