1fb726d48Sopenharmony_ci/*
2fb726d48Sopenharmony_ci * Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved.
3fb726d48Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4fb726d48Sopenharmony_ci * you may not use this file except in compliance with the License.
5fb726d48Sopenharmony_ci * You may obtain a copy of the License at
6fb726d48Sopenharmony_ci *
7fb726d48Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
8fb726d48Sopenharmony_ci *
9fb726d48Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10fb726d48Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11fb726d48Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12fb726d48Sopenharmony_ci * See the License for the specific language governing permissions and
13fb726d48Sopenharmony_ci * limitations under the License.
14fb726d48Sopenharmony_ci */
15fb726d48Sopenharmony_ci
16fb726d48Sopenharmony_ci#include "measure_table.h"
17fb726d48Sopenharmony_ci#include <cmath>
18fb726d48Sopenharmony_ci
19fb726d48Sopenharmony_cinamespace SysTuning {
20fb726d48Sopenharmony_cinamespace TraceStreamer {
21fb726d48Sopenharmony_cienum class Index : int32_t { TYPE = 0, TS, DUR, VALUE, FILTER_ID };
22fb726d48Sopenharmony_ciMeasureTable::MeasureTable(const TraceDataCache *dataCache) : TableBase(dataCache)
23fb726d48Sopenharmony_ci{
24fb726d48Sopenharmony_ci    tableColumn_.push_back(TableBase::ColumnInfo("type", "TEXT"));
25fb726d48Sopenharmony_ci    tableColumn_.push_back(TableBase::ColumnInfo("ts", "INTEGER"));
26fb726d48Sopenharmony_ci    tableColumn_.push_back(TableBase::ColumnInfo("dur", "INTEGER"));
27fb726d48Sopenharmony_ci    tableColumn_.push_back(TableBase::ColumnInfo("value", "INTEGER"));
28fb726d48Sopenharmony_ci    tableColumn_.push_back(TableBase::ColumnInfo("filter_id", "INTEGER"));
29fb726d48Sopenharmony_ci    tablePriKey_.push_back("ts");
30fb726d48Sopenharmony_ci    tablePriKey_.push_back("filter_id");
31fb726d48Sopenharmony_ci}
32fb726d48Sopenharmony_ci
33fb726d48Sopenharmony_ciMeasureTable::~MeasureTable() {}
34fb726d48Sopenharmony_ci
35fb726d48Sopenharmony_cistd::unique_ptr<TableBase::Cursor> MeasureTable::CreateCursor()
36fb726d48Sopenharmony_ci{
37fb726d48Sopenharmony_ci    return std::make_unique<Cursor>(dataCache_, this);
38fb726d48Sopenharmony_ci}
39fb726d48Sopenharmony_ci
40fb726d48Sopenharmony_ciMeasureTable::Cursor::Cursor(const TraceDataCache *dataCache, TableBase *table)
41fb726d48Sopenharmony_ci    : TableBase::Cursor(dataCache,
42fb726d48Sopenharmony_ci                        table,
43fb726d48Sopenharmony_ci                        static_cast<uint32_t>(table->name_ == "measure"
44fb726d48Sopenharmony_ci                                                  ? dataCache->GetConstMeasureData().Size()
45fb726d48Sopenharmony_ci                                                  : (table->name_ == "process_measure"
46fb726d48Sopenharmony_ci                                                         ? dataCache->GetConstProcessMeasureData().Size()
47fb726d48Sopenharmony_ci                                                         : (table->name_ == "sys_mem_measure"
48fb726d48Sopenharmony_ci                                                                ? dataCache->GetConstSysMemMeasureData().Size()
49fb726d48Sopenharmony_ci                                                                : dataCache->GetConstXpowerMeasureData().Size())))),
50fb726d48Sopenharmony_ci      measureObj(table->name_ == "measure"
51fb726d48Sopenharmony_ci                     ? dataCache->GetConstMeasureData()
52fb726d48Sopenharmony_ci                     : (table->name_ == "process_measure"
53fb726d48Sopenharmony_ci                            ? dataCache->GetConstProcessMeasureData()
54fb726d48Sopenharmony_ci                            : (table->name_ == "sys_mem_measure" ? dataCache->GetConstSysMemMeasureData()
55fb726d48Sopenharmony_ci                                                                 : dataCache->GetConstXpowerMeasureData())))
56fb726d48Sopenharmony_ci{
57fb726d48Sopenharmony_ci}
58fb726d48Sopenharmony_ci
59fb726d48Sopenharmony_ciMeasureTable::Cursor::~Cursor() {}
60fb726d48Sopenharmony_ci
61fb726d48Sopenharmony_civoid MeasureTable::FilterByConstraint(FilterConstraints &measurefc,
62fb726d48Sopenharmony_ci                                      double &measurefilterCost,
63fb726d48Sopenharmony_ci                                      size_t measurerowCount,
64fb726d48Sopenharmony_ci                                      uint32_t measurecurrenti)
65fb726d48Sopenharmony_ci{
66fb726d48Sopenharmony_ci    // To use the EstimateFilterCost function in the TableBase parent class function to calculate the i-value of each
67fb726d48Sopenharmony_ci    // for loop
68fb726d48Sopenharmony_ci    const auto &measurec = measurefc.GetConstraints()[measurecurrenti];
69fb726d48Sopenharmony_ci    switch (static_cast<Index>(measurec.col)) {
70fb726d48Sopenharmony_ci        case Index::TS: {
71fb726d48Sopenharmony_ci            auto measureoldRowCount = measurerowCount;
72fb726d48Sopenharmony_ci            if (CanFilterSorted(measurec.op, measurerowCount)) {
73fb726d48Sopenharmony_ci                measurefc.UpdateConstraint(measurecurrenti, true);
74fb726d48Sopenharmony_ci                measurefilterCost += log2(measureoldRowCount); // binary search
75fb726d48Sopenharmony_ci            } else {
76fb726d48Sopenharmony_ci                measurefilterCost += measureoldRowCount;
77fb726d48Sopenharmony_ci            }
78fb726d48Sopenharmony_ci            break;
79fb726d48Sopenharmony_ci        }
80fb726d48Sopenharmony_ci        default:                                  // other column
81fb726d48Sopenharmony_ci            measurefilterCost += measurerowCount; // scan all rows
82fb726d48Sopenharmony_ci            break;
83fb726d48Sopenharmony_ci    }
84fb726d48Sopenharmony_ci}
85fb726d48Sopenharmony_ci
86fb726d48Sopenharmony_ciint32_t MeasureTable::Cursor::Filter(const FilterConstraints &fc, sqlite3_value **argv)
87fb726d48Sopenharmony_ci{
88fb726d48Sopenharmony_ci    // reset
89fb726d48Sopenharmony_ci    indexMap_ = std::make_unique<IndexMap>(0, rowCount_);
90fb726d48Sopenharmony_ci
91fb726d48Sopenharmony_ci    if (rowCount_ <= 0) {
92fb726d48Sopenharmony_ci        return SQLITE_OK;
93fb726d48Sopenharmony_ci    }
94fb726d48Sopenharmony_ci    auto measureTabCs = fc.GetConstraints();
95fb726d48Sopenharmony_ci    std::set<uint32_t> sId = {static_cast<uint32_t>(Index::TS)};
96fb726d48Sopenharmony_ci    SwapIndexFront(measureTabCs, sId);
97fb726d48Sopenharmony_ci    for (size_t i = 0; i < measureTabCs.size(); i++) {
98fb726d48Sopenharmony_ci        const auto &c = measureTabCs[i];
99fb726d48Sopenharmony_ci        switch (static_cast<Index>(c.col)) {
100fb726d48Sopenharmony_ci            case Index::TS:
101fb726d48Sopenharmony_ci                FilterTS(c.op, argv[c.idxInaConstraint], measureObj.TimeStampData());
102fb726d48Sopenharmony_ci                break;
103fb726d48Sopenharmony_ci            case Index::FILTER_ID:
104fb726d48Sopenharmony_ci                indexMap_->MixRange(c.op, static_cast<uint32_t>(sqlite3_value_int(argv[c.idxInaConstraint])),
105fb726d48Sopenharmony_ci                                    measureObj.FilterIdData());
106fb726d48Sopenharmony_ci                break;
107fb726d48Sopenharmony_ci            default:
108fb726d48Sopenharmony_ci                break;
109fb726d48Sopenharmony_ci        }
110fb726d48Sopenharmony_ci    }
111fb726d48Sopenharmony_ci
112fb726d48Sopenharmony_ci    auto orderbys = fc.GetOrderBys();
113fb726d48Sopenharmony_ci    for (auto i = orderbys.size(); i > 0;) {
114fb726d48Sopenharmony_ci        i--;
115fb726d48Sopenharmony_ci        switch (static_cast<Index>(orderbys[i].iColumn)) {
116fb726d48Sopenharmony_ci            case Index::TS:
117fb726d48Sopenharmony_ci                indexMap_->SortBy(orderbys[i].desc);
118fb726d48Sopenharmony_ci                break;
119fb726d48Sopenharmony_ci            case Index::FILTER_ID:
120fb726d48Sopenharmony_ci                indexMap_->SortBy(orderbys[i].desc);
121fb726d48Sopenharmony_ci                break;
122fb726d48Sopenharmony_ci            default:
123fb726d48Sopenharmony_ci                break;
124fb726d48Sopenharmony_ci        }
125fb726d48Sopenharmony_ci    }
126fb726d48Sopenharmony_ci
127fb726d48Sopenharmony_ci    return SQLITE_OK;
128fb726d48Sopenharmony_ci}
129fb726d48Sopenharmony_ci
130fb726d48Sopenharmony_ciint32_t MeasureTable::Cursor::Column(int32_t column) const
131fb726d48Sopenharmony_ci{
132fb726d48Sopenharmony_ci    switch (static_cast<Index>(column)) {
133fb726d48Sopenharmony_ci        case Index::TYPE:
134fb726d48Sopenharmony_ci            sqlite3_result_text(context_, "measure", STR_DEFAULT_LEN, nullptr);
135fb726d48Sopenharmony_ci            break;
136fb726d48Sopenharmony_ci        case Index::TS:
137fb726d48Sopenharmony_ci            sqlite3_result_int64(context_, static_cast<int64_t>(measureObj.TimeStampData()[CurrentRow()]));
138fb726d48Sopenharmony_ci            break;
139fb726d48Sopenharmony_ci        case Index::DUR:
140fb726d48Sopenharmony_ci            if (measureObj.DursData()[CurrentRow()] != INVALID_UINT64) {
141fb726d48Sopenharmony_ci                sqlite3_result_int64(context_, static_cast<int64_t>(measureObj.DursData()[CurrentRow()]));
142fb726d48Sopenharmony_ci            }
143fb726d48Sopenharmony_ci            break;
144fb726d48Sopenharmony_ci        case Index::VALUE:
145fb726d48Sopenharmony_ci            sqlite3_result_int64(context_, static_cast<int64_t>(measureObj.ValuesData()[CurrentRow()]));
146fb726d48Sopenharmony_ci            break;
147fb726d48Sopenharmony_ci        case Index::FILTER_ID:
148fb726d48Sopenharmony_ci            sqlite3_result_int64(context_, static_cast<int32_t>(measureObj.FilterIdData()[CurrentRow()]));
149fb726d48Sopenharmony_ci            break;
150fb726d48Sopenharmony_ci        default:
151fb726d48Sopenharmony_ci            TS_LOGF("Unregistered column : %d", column);
152fb726d48Sopenharmony_ci            break;
153fb726d48Sopenharmony_ci    }
154fb726d48Sopenharmony_ci    return SQLITE_OK;
155fb726d48Sopenharmony_ci}
156fb726d48Sopenharmony_ci
157fb726d48Sopenharmony_civoid MeasureTable::GetOrbyes(FilterConstraints &measurefc, EstimatedIndexInfo &measureei)
158fb726d48Sopenharmony_ci{
159fb726d48Sopenharmony_ci    auto measureorderbys = measurefc.GetOrderBys();
160fb726d48Sopenharmony_ci    for (auto i = 0; i < measureorderbys.size(); i++) {
161fb726d48Sopenharmony_ci        switch (static_cast<Index>(measureorderbys[i].iColumn)) {
162fb726d48Sopenharmony_ci            case Index::TS:
163fb726d48Sopenharmony_ci                break;
164fb726d48Sopenharmony_ci            default: // other columns can be sorted by SQLite
165fb726d48Sopenharmony_ci                measureei.isOrdered = false;
166fb726d48Sopenharmony_ci                break;
167fb726d48Sopenharmony_ci        }
168fb726d48Sopenharmony_ci    }
169fb726d48Sopenharmony_ci}
170fb726d48Sopenharmony_ci} // namespace TraceStreamer
171fb726d48Sopenharmony_ci} // namespace SysTuning
172