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 "native_hook_table.h"
17 
18 namespace SysTuning {
19 namespace TraceStreamer {
20 enum class Index : int32_t {
21     ID = 0,
22     CALLCHAIN_ID,
23     IPID,
24     ITID,
25     EVENT_TYPE,
26     SUB_TYPE_ID,
27     START_TS,
28     END_TS,
29     DURATION,
30     ADDR,
31     MEM_SIZE,
32     ALL_MEM_SIZE,
33     CURRENT_SIZE_DUR,
34     LAST_LIB_ID,
35     LAST_SYMBOL_ID
36 };
NativeHookTable(const TraceDataCache *dataCache)37 NativeHookTable::NativeHookTable(const TraceDataCache *dataCache) : TableBase(dataCache)
38 {
39     tableColumn_.push_back(TableBase::ColumnInfo("id", "INTEGER"));
40     tableColumn_.push_back(TableBase::ColumnInfo("callchain_id", "INTEGER"));
41     tableColumn_.push_back(TableBase::ColumnInfo("ipid", "INTEGER"));
42     tableColumn_.push_back(TableBase::ColumnInfo("itid", "INTEGER"));
43     tableColumn_.push_back(TableBase::ColumnInfo("event_type", "TEXT"));
44     tableColumn_.push_back(TableBase::ColumnInfo("sub_type_id", "INTEGER"));
45     tableColumn_.push_back(TableBase::ColumnInfo("start_ts", "INTEGER"));
46     tableColumn_.push_back(TableBase::ColumnInfo("end_ts", "INTEGER"));
47     tableColumn_.push_back(TableBase::ColumnInfo("dur", "INTEGER"));
48     tableColumn_.push_back(TableBase::ColumnInfo("addr", "INTEGER"));
49     tableColumn_.push_back(TableBase::ColumnInfo("heap_size", "INTEGER"));
50     tableColumn_.push_back(TableBase::ColumnInfo("all_heap_size", "INTEGER"));
51     tableColumn_.push_back(TableBase::ColumnInfo("current_size_dur", "INTEGER"));
52     tableColumn_.push_back(TableBase::ColumnInfo("last_lib_id", "INTEGER"));
53     tableColumn_.push_back(TableBase::ColumnInfo("last_symbol_id", "INTEGER"));
54     tablePriKey_.push_back("id");
55 }
56 
~NativeHookTable()57 NativeHookTable::~NativeHookTable() {}
58 
FilterByConstraint(FilterConstraints &hookfc, double &hookfilterCost, size_t hookrowCount, uint32_t hookcurrenti)59 void NativeHookTable::FilterByConstraint(FilterConstraints &hookfc,
60                                          double &hookfilterCost,
61                                          size_t hookrowCount,
62                                          uint32_t hookcurrenti)
63 {
64     // To use the EstimateFilterCost function in the TableBase parent class function to calculate the i-value of each
65     // for loop
66     const auto &hookc = hookfc.GetConstraints()[hookcurrenti];
67     switch (static_cast<Index>(hookc.col)) {
68         case Index::ID: {
69             if (CanFilterId(hookc.op, hookrowCount)) {
70                 hookfc.UpdateConstraint(hookcurrenti, true);
71                 hookfilterCost += 1; // id can position by 1 step
72             } else {
73                 hookfilterCost += hookrowCount; // scan all rows
74             }
75             break;
76         }
77         default:                            // other column
78             hookfilterCost += hookrowCount; // scan all rows
79             break;
80     }
81 }
82 
CreateCursor()83 std::unique_ptr<TableBase::Cursor> NativeHookTable::CreateCursor()
84 {
85     return std::make_unique<Cursor>(dataCache_, this);
86 }
87 
Cursor(const TraceDataCache *dataCache, TableBase *table)88 NativeHookTable::Cursor::Cursor(const TraceDataCache *dataCache, TableBase *table)
89     : TableBase::Cursor(dataCache, table, static_cast<uint32_t>(dataCache->GetConstNativeHookData().Size())),
90       nativeHookObj_(dataCache->GetConstNativeHookData())
91 {
92 }
93 
~Cursor()94 NativeHookTable::Cursor::~Cursor() {}
95 
Filter(const FilterConstraints &fc, sqlite3_value **argv)96 int32_t NativeHookTable::Cursor::Filter(const FilterConstraints &fc, sqlite3_value **argv)
97 {
98     // reset indexMap_
99     indexMap_ = std::make_unique<IndexMap>(0, rowCount_);
100 
101     if (rowCount_ <= 0) {
102         return SQLITE_OK;
103     }
104 
105     auto nativeHookCs = fc.GetConstraints();
106     std::set<uint32_t> sId = {static_cast<uint32_t>(Index::ID)};
107     SwapIndexFront(nativeHookCs, sId);
108     for (size_t i = 0; i < nativeHookCs.size(); i++) {
109         const auto &c = nativeHookCs[i];
110         switch (static_cast<Index>(c.col)) {
111             case Index::ID:
112                 FilterId(c.op, argv[c.idxInaConstraint]);
113                 break;
114             case Index::IPID:
115                 indexMap_->MixRange(c.op, static_cast<uint32_t>(sqlite3_value_int(argv[c.idxInaConstraint])),
116                                     nativeHookObj_.Ipids());
117                 break;
118             case Index::ITID:
119                 indexMap_->MixRange(c.op, static_cast<uint32_t>(sqlite3_value_int(argv[c.idxInaConstraint])),
120                                     nativeHookObj_.InternalTidsData());
121                 break;
122             case Index::CALLCHAIN_ID:
123                 indexMap_->MixRange(c.op, static_cast<uint32_t>(sqlite3_value_int64(argv[c.idxInaConstraint])),
124                                     nativeHookObj_.CallChainIds());
125                 break;
126             default:
127                 break;
128         }
129     }
130 
131     auto nativeHookOrderbys = fc.GetOrderBys();
132     for (auto i = nativeHookOrderbys.size(); i > 0;) {
133         i--;
134         switch (static_cast<Index>(nativeHookOrderbys[i].iColumn)) {
135             case Index::ID:
136                 indexMap_->SortBy(nativeHookOrderbys[i].desc);
137                 break;
138             default:
139                 break;
140         }
141     }
142 
143     return SQLITE_OK;
144 }
145 
Column(int32_t column) const146 int32_t NativeHookTable::Cursor::Column(int32_t column) const
147 {
148     switch (static_cast<Index>(column)) {
149         case Index::ID:
150             sqlite3_result_int64(context_, static_cast<int32_t>(nativeHookObj_.IdsData()[CurrentRow()]));
151             break;
152         case Index::CALLCHAIN_ID:
153             SetTypeColumn(nativeHookObj_.CallChainIds()[CurrentRow()], INVALID_UINT32, INVALID_CALL_CHAIN_ID);
154             break;
155         case Index::IPID:
156             sqlite3_result_int64(context_, static_cast<int64_t>(nativeHookObj_.Ipids()[CurrentRow()]));
157             break;
158         case Index::ITID:
159             sqlite3_result_int64(context_, static_cast<int64_t>(nativeHookObj_.InternalTidsData()[CurrentRow()]));
160             break;
161         case Index::EVENT_TYPE: {
162             SetTypeColumnTextNotEmpty(nativeHookObj_.EventTypes()[CurrentRow()].empty(),
163                                       nativeHookObj_.EventTypes()[CurrentRow()].c_str());
164             break;
165         }
166         case Index::SUB_TYPE_ID: {
167             SetTypeColumnInt64(nativeHookObj_.SubTypes()[CurrentRow()], INVALID_UINT64);
168             break;
169         }
170         case Index::START_TS:
171             sqlite3_result_int64(context_, static_cast<int64_t>(nativeHookObj_.TimeStampData()[CurrentRow()]));
172             break;
173         case Index::END_TS:
174             if (static_cast<int64_t>(nativeHookObj_.EndTimeStamps()[CurrentRow()]) != 0) {
175                 sqlite3_result_int64(context_, static_cast<int64_t>(nativeHookObj_.EndTimeStamps()[CurrentRow()]));
176             }
177             break;
178         default:
179             HandleTypeColumns(column);
180     }
181     return SQLITE_OK;
182 }
183 
HandleTypeColumns(int32_t column) const184 void NativeHookTable::Cursor::HandleTypeColumns(int32_t column) const
185 {
186     switch (static_cast<Index>(column)) {
187         case Index::DURATION:
188             if (static_cast<int64_t>(nativeHookObj_.Durations()[CurrentRow()]) != 0) {
189                 sqlite3_result_int64(context_, static_cast<int64_t>(nativeHookObj_.Durations()[CurrentRow()]));
190             }
191             break;
192         case Index::ADDR: {
193             sqlite3_result_int64(context_, static_cast<int64_t>(nativeHookObj_.Addrs()[CurrentRow()]));
194             break;
195         }
196         case Index::MEM_SIZE: {
197             sqlite3_result_int64(context_, static_cast<int64_t>(nativeHookObj_.MemSizes()[CurrentRow()]));
198             break;
199         }
200         case Index::ALL_MEM_SIZE: {
201             sqlite3_result_int64(context_, static_cast<int64_t>(nativeHookObj_.AllMemSizes()[CurrentRow()]));
202             break;
203         }
204         case Index::CURRENT_SIZE_DUR: {
205             sqlite3_result_int64(context_, static_cast<int64_t>(nativeHookObj_.CurrentSizeDurs()[CurrentRow()]));
206             break;
207         }
208         case Index::LAST_LIB_ID: {
209             SetTypeColumnInt64(nativeHookObj_.LastCallerPathIndexs()[CurrentRow()], INVALID_DATAINDEX);
210             break;
211         }
212         case Index::LAST_SYMBOL_ID: {
213             SetTypeColumnInt64(nativeHookObj_.LastSymbolIndexs()[CurrentRow()], INVALID_DATAINDEX);
214             break;
215         }
216         default:
217             TS_LOGF("Unregistered column : %d", column);
218             break;
219     }
220 }
GetOrbyes(FilterConstraints &hookfc, EstimatedIndexInfo &hookei)221 void NativeHookTable::GetOrbyes(FilterConstraints &hookfc, EstimatedIndexInfo &hookei)
222 {
223     auto hookorderbys = hookfc.GetOrderBys();
224     for (auto i = 0; i < hookorderbys.size(); i++) {
225         switch (static_cast<Index>(hookorderbys[i].iColumn)) {
226             case Index::ID:
227                 break;
228             default: // other columns can be sorted by SQLite
229                 hookei.isOrdered = false;
230                 break;
231         }
232     }
233 }
234 } // namespace TraceStreamer
235 } // namespace SysTuning
236