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 "cpu_usage_info_table.h"
17
18 namespace SysTuning {
19 namespace TraceStreamer {
20 namespace {
21 enum Index { TS = 0, DUR, TOTAL_LOAD, USER_LOAD, SYSTEM_LOAD, THREADS };
22 }
CpuUsageInfoTable(const TraceDataCache * dataCache)23 CpuUsageInfoTable::CpuUsageInfoTable(const TraceDataCache* dataCache) : TableBase(dataCache)
24 {
25 tableColumn_.push_back(TableBase::ColumnInfo("ts", "INTEGER"));
26 tableColumn_.push_back(TableBase::ColumnInfo("dur", "INTEGER"));
27 tableColumn_.push_back(TableBase::ColumnInfo("total_load", "REAL"));
28 tableColumn_.push_back(TableBase::ColumnInfo("user_load", "REAL"));
29 tableColumn_.push_back(TableBase::ColumnInfo("system_load", "REAL"));
30 tableColumn_.push_back(TableBase::ColumnInfo("process_num", "INTEGER"));
31 tablePriKey_.push_back("ts");
32 }
33
~CpuUsageInfoTable()34 CpuUsageInfoTable::~CpuUsageInfoTable() {}
35
CreateCursor()36 std::unique_ptr<TableBase::Cursor> CpuUsageInfoTable::CreateCursor()
37 {
38 return std::make_unique<Cursor>(dataCache_, this);
39 }
40
Cursor(const TraceDataCache * dataCache,TableBase * table)41 CpuUsageInfoTable::Cursor::Cursor(const TraceDataCache* dataCache, TableBase* table)
42 : TableBase::Cursor(dataCache, table, static_cast<uint32_t>(dataCache->GetConstCpuUsageInfoData().Size())),
43 cpuUsageInfoObj_(dataCache->GetConstCpuUsageInfoData())
44 {
45 }
46
~Cursor()47 CpuUsageInfoTable::Cursor::~Cursor() {}
48
Column(int column) const49 int CpuUsageInfoTable::Cursor::Column(int column) const
50 {
51 switch (column) {
52 case TS: {
53 sqlite3_result_int64(context_, static_cast<int64_t>(cpuUsageInfoObj_.TimeStamData()[CurrentRow()]));
54 break;
55 }
56 case DUR: {
57 sqlite3_result_int64(context_, static_cast<int64_t>(cpuUsageInfoObj_.Durs()[CurrentRow()]));
58 break;
59 }
60 case TOTAL_LOAD: {
61 sqlite3_result_int64(context_, static_cast<int64_t>(cpuUsageInfoObj_.TotalLoad()[CurrentRow()]));
62 break;
63 }
64 case USER_LOAD: {
65 sqlite3_result_double(context_, cpuUsageInfoObj_.UserLoad()[CurrentRow()]);
66 break;
67 }
68 case SYSTEM_LOAD: {
69 sqlite3_result_double(context_, cpuUsageInfoObj_.SystemLoad()[CurrentRow()]);
70 break;
71 }
72 case THREADS: {
73 sqlite3_result_int(context_, static_cast<int>(cpuUsageInfoObj_.Threads()[CurrentRow()]));
74 break;
75 }
76 default:
77 TS_LOGF("Unregistered column : %d", column);
78 break;
79 }
80 return SQLITE_OK;
81 }
82 } // namespace TraceStreamer
83 } // namespace SysTuning
84