• 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 "gpu_slice_table.h"
17 
18 namespace SysTuning {
19 namespace TraceStreamer {
20 enum Index { ID = 0, FRAME_ROW, DUR };
GPUSliceTable(const TraceDataCache * dataCache)21 GPUSliceTable::GPUSliceTable(const TraceDataCache* dataCache) : TableBase(dataCache)
22 {
23     tableColumn_.push_back(TableBase::ColumnInfo("id", "INTEGER"));
24     tableColumn_.push_back(TableBase::ColumnInfo("frame_row", "INTEGER"));
25     tableColumn_.push_back(TableBase::ColumnInfo("dur", "INTEGER"));
26     tablePriKey_.push_back("id");
27 }
28 
~GPUSliceTable()29 GPUSliceTable::~GPUSliceTable() {}
30 
EstimateFilterCost(FilterConstraints & fc,EstimatedIndexInfo & ei)31 void GPUSliceTable::EstimateFilterCost(FilterConstraints& fc, EstimatedIndexInfo& ei)
32 {
33     constexpr double filterBaseCost = 1000.0; // set-up and tear-down
34     constexpr double indexCost = 2.0;
35     ei.estimatedCost = filterBaseCost;
36 
37     auto rowCount = dataCache_->GetConstHidumpData().Size();
38     if (rowCount == 0 || rowCount == 1) {
39         ei.estimatedRows = rowCount;
40         ei.estimatedCost += indexCost * rowCount;
41         return;
42     }
43 
44     double filterCost = 0.0;
45     auto constraints = fc.GetConstraints();
46     if (constraints.empty()) { // scan all rows
47         filterCost = rowCount;
48     } else {
49         FilterByConstraint(fc, filterCost, rowCount);
50     }
51     ei.estimatedCost += filterCost;
52     ei.estimatedRows = rowCount;
53     ei.estimatedCost += rowCount * indexCost;
54 
55     ei.isOrdered = true;
56     auto orderbys = fc.GetOrderBys();
57     for (auto i = 0; i < orderbys.size(); i++) {
58         switch (orderbys[i].iColumn) {
59             case ID:
60                 break;
61             default: // other columns can be sorted by SQLite
62                 ei.isOrdered = false;
63                 break;
64         }
65     }
66 }
67 
FilterByConstraint(FilterConstraints & fc,double & filterCost,size_t rowCount)68 void GPUSliceTable::FilterByConstraint(FilterConstraints& fc, double& filterCost, size_t rowCount)
69 {
70     auto fcConstraints = fc.GetConstraints();
71     for (int32_t i = 0; i < static_cast<int32_t>(fcConstraints.size()); i++) {
72         if (rowCount <= 1) {
73             // only one row or nothing, needn't filter by constraint
74             filterCost += rowCount;
75             break;
76         }
77         const auto& c = fcConstraints[i];
78         switch (c.col) {
79             case ID: {
80                 if (CanFilterId(c.op, rowCount)) {
81                     fc.UpdateConstraint(i, true);
82                     filterCost += 1; // id can position by 1 step
83                 } else {
84                     filterCost += rowCount; // scan all rows
85                 }
86                 break;
87             }
88             default:                    // other column
89                 filterCost += rowCount; // scan all rows
90                 break;
91         }
92     }
93 }
94 
CreateCursor()95 std::unique_ptr<TableBase::Cursor> GPUSliceTable::CreateCursor()
96 {
97     return std::make_unique<Cursor>(dataCache_, this);
98 }
99 
Cursor(const TraceDataCache * dataCache,TableBase * table)100 GPUSliceTable::Cursor::Cursor(const TraceDataCache* dataCache, TableBase* table)
101     : TableBase::Cursor(dataCache, table, static_cast<uint32_t>(dataCache->GetConstGPUSliceData().Size())),
102       gpuSliceObj_(dataCache->GetConstGPUSliceData())
103 {
104 }
105 
~Cursor()106 GPUSliceTable::Cursor::~Cursor() {}
107 
Filter(const FilterConstraints & fc,sqlite3_value ** argv)108 int32_t GPUSliceTable::Cursor::Filter(const FilterConstraints& fc, sqlite3_value** argv)
109 {
110     // reset indexMap_
111     indexMap_ = std::make_unique<IndexMap>(0, rowCount_);
112 
113     if (rowCount_ <= 0) {
114         return SQLITE_OK;
115     }
116 
117     auto& cs = fc.GetConstraints();
118     for (size_t i = 0; i < cs.size(); i++) {
119         const auto& c = cs[i];
120         switch (c.col) {
121             case ID:
122                 FilterId(c.op, argv[i]);
123                 break;
124             case FRAME_ROW:
125                 indexMap_->MixRange(c.op, static_cast<uint32_t>(sqlite3_value_int(argv[i])), gpuSliceObj_.FrameRows());
126                 break;
127             case DUR:
128                 indexMap_->MixRange(c.op, static_cast<uint64_t>(sqlite3_value_int64(argv[i])), gpuSliceObj_.Durs());
129                 break;
130             default:
131                 break;
132         }
133     }
134 
135     auto orderbys = fc.GetOrderBys();
136     for (auto i = orderbys.size(); i > 0;) {
137         i--;
138         switch (orderbys[i].iColumn) {
139             case ID:
140                 indexMap_->SortBy(orderbys[i].desc);
141                 break;
142             default:
143                 break;
144         }
145     }
146 
147     return SQLITE_OK;
148 }
149 
Column(int32_t column) const150 int32_t GPUSliceTable::Cursor::Column(int32_t column) const
151 {
152     switch (column) {
153         case ID:
154             sqlite3_result_int64(context_, static_cast<int32_t>(CurrentRow()));
155             break;
156         case FRAME_ROW:
157             sqlite3_result_int64(context_, static_cast<int32_t>(gpuSliceObj_.FrameRows()[CurrentRow()]));
158             break;
159         case DUR:
160             if (gpuSliceObj_.Durs()[CurrentRow()] != INVALID_UINT64) {
161                 sqlite3_result_int64(context_, static_cast<int64_t>(gpuSliceObj_.Durs()[CurrentRow()]));
162             }
163             break;
164         default:
165             TS_LOGF("Unregistered column : %d", column);
166             break;
167     }
168     return SQLITE_OK;
169 }
170 } // namespace TraceStreamer
171 } // namespace SysTuning
172