• 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 "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[i]);
113                 break;
114             case Index::IPID:
115                 indexMap_->MixRange(c.op, static_cast<uint32_t>(sqlite3_value_int(argv[i])), nativeHookObj_.Ipids());
116                 break;
117             case Index::ITID:
118                 indexMap_->MixRange(c.op, static_cast<uint32_t>(sqlite3_value_int(argv[i])),
119                                     nativeHookObj_.InternalTidsData());
120                 break;
121             case Index::CALLCHAIN_ID:
122                 indexMap_->MixRange(c.op, static_cast<uint32_t>(sqlite3_value_int64(argv[i])),
123                                     nativeHookObj_.CallChainIds());
124                 break;
125             default:
126                 break;
127         }
128     }
129 
130     auto nativeHookOrderbys = fc.GetOrderBys();
131     for (auto i = nativeHookOrderbys.size(); i > 0;) {
132         i--;
133         switch (static_cast<Index>(nativeHookOrderbys[i].iColumn)) {
134             case Index::ID:
135                 indexMap_->SortBy(nativeHookOrderbys[i].desc);
136                 break;
137             default:
138                 break;
139         }
140     }
141 
142     return SQLITE_OK;
143 }
144 
Column(int32_t column) const145 int32_t NativeHookTable::Cursor::Column(int32_t column) const
146 {
147     switch (static_cast<Index>(column)) {
148         case Index::ID:
149             sqlite3_result_int64(context_, static_cast<int32_t>(nativeHookObj_.IdsData()[CurrentRow()]));
150             break;
151         case Index::CALLCHAIN_ID:
152             SetTypeColumn(nativeHookObj_.CallChainIds()[CurrentRow()], INVALID_UINT32, INVALID_CALL_CHAIN_ID);
153             break;
154         case Index::IPID:
155             sqlite3_result_int64(context_, static_cast<int64_t>(nativeHookObj_.Ipids()[CurrentRow()]));
156             break;
157         case Index::ITID:
158             sqlite3_result_int64(context_, static_cast<int64_t>(nativeHookObj_.InternalTidsData()[CurrentRow()]));
159             break;
160         case Index::EVENT_TYPE: {
161             SetTypeColumnTextNotEmpty(nativeHookObj_.EventTypes()[CurrentRow()].empty(),
162                                       nativeHookObj_.EventTypes()[CurrentRow()].c_str());
163             break;
164         }
165         case Index::SUB_TYPE_ID: {
166             SetTypeColumnInt64(nativeHookObj_.SubTypes()[CurrentRow()], INVALID_UINT64);
167             break;
168         }
169         case Index::START_TS:
170             sqlite3_result_int64(context_, static_cast<int64_t>(nativeHookObj_.TimeStampData()[CurrentRow()]));
171             break;
172         case Index::END_TS:
173             if (static_cast<int64_t>(nativeHookObj_.EndTimeStamps()[CurrentRow()]) != 0) {
174                 sqlite3_result_int64(context_, static_cast<int64_t>(nativeHookObj_.EndTimeStamps()[CurrentRow()]));
175             }
176             break;
177         default:
178             HandleTypeColumns(column);
179     }
180     return SQLITE_OK;
181 }
182 
HandleTypeColumns(int32_t column) const183 void NativeHookTable::Cursor::HandleTypeColumns(int32_t column) const
184 {
185     switch (static_cast<Index>(column)) {
186         case Index::DURATION:
187             if (static_cast<int64_t>(nativeHookObj_.Durations()[CurrentRow()]) != 0) {
188                 sqlite3_result_int64(context_, static_cast<int64_t>(nativeHookObj_.Durations()[CurrentRow()]));
189             }
190             break;
191         case Index::ADDR: {
192             sqlite3_result_int64(context_, static_cast<int64_t>(nativeHookObj_.Addrs()[CurrentRow()]));
193             break;
194         }
195         case Index::MEM_SIZE: {
196             sqlite3_result_int64(context_, static_cast<int64_t>(nativeHookObj_.MemSizes()[CurrentRow()]));
197             break;
198         }
199         case Index::ALL_MEM_SIZE: {
200             sqlite3_result_int64(context_, static_cast<int64_t>(nativeHookObj_.AllMemSizes()[CurrentRow()]));
201             break;
202         }
203         case Index::CURRENT_SIZE_DUR: {
204             sqlite3_result_int64(context_, static_cast<int64_t>(nativeHookObj_.CurrentSizeDurs()[CurrentRow()]));
205             break;
206         }
207         case Index::LAST_LIB_ID: {
208             SetTypeColumnInt64(nativeHookObj_.LastCallerPathIndexs()[CurrentRow()], INVALID_DATAINDEX);
209             break;
210         }
211         case Index::LAST_SYMBOL_ID: {
212             SetTypeColumnInt64(nativeHookObj_.LastSymbolIndexs()[CurrentRow()], INVALID_DATAINDEX);
213             break;
214         }
215         default:
216             TS_LOGF("Unregistered column : %d", column);
217             break;
218     }
219 }
GetOrbyes(FilterConstraints & hookfc,EstimatedIndexInfo & hookei)220 void NativeHookTable::GetOrbyes(FilterConstraints& hookfc, EstimatedIndexInfo& hookei)
221 {
222     auto hookorderbys = hookfc.GetOrderBys();
223     for (auto i = 0; i < hookorderbys.size(); i++) {
224         switch (static_cast<Index>(hookorderbys[i].iColumn)) {
225             case Index::ID:
226                 break;
227             default: // other columns can be sorted by SQLite
228                 hookei.isOrdered = false;
229                 break;
230         }
231     }
232 }
233 } // namespace TraceStreamer
234 } // namespace SysTuning
235