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 "live_process_table.h"
17
18 namespace SysTuning {
19 namespace TraceStreamer {
20 enum class Index : int32_t {
21 TS = 0,
22 DUR,
23 CPU_TIME,
24 PROCESS_ID,
25 PROCESS_NAME,
26 PARENT_PROCESS_ID,
27 UID,
28 USER_NAME,
29 CPU_USAGE,
30 PSS_INFO,
31 THREAD_SUM,
32 DISK_WRITES,
33 DISK_READS
34 };
LiveProcessTable(const TraceDataCache *dataCache)35 LiveProcessTable::LiveProcessTable(const TraceDataCache *dataCache) : TableBase(dataCache)
36 {
37 tableColumn_.push_back(TableBase::ColumnInfo("ts", "INTEGER"));
38 tableColumn_.push_back(TableBase::ColumnInfo("dur", "INTEGER"));
39 tableColumn_.push_back(TableBase::ColumnInfo("cpu_time", "INTEGER"));
40 tableColumn_.push_back(TableBase::ColumnInfo("process_id", "INTEGER"));
41 tableColumn_.push_back(TableBase::ColumnInfo("process_name", "TEXT"));
42 tableColumn_.push_back(TableBase::ColumnInfo("parent_process_id", "INTEGER"));
43 tableColumn_.push_back(TableBase::ColumnInfo("uid", "INTEGER"));
44 tableColumn_.push_back(TableBase::ColumnInfo("user_name", "TEXT"));
45 tableColumn_.push_back(TableBase::ColumnInfo("cpu_usage", "REAL"));
46 tableColumn_.push_back(TableBase::ColumnInfo("pss_info", "INTEGER"));
47 tableColumn_.push_back(TableBase::ColumnInfo("thread_num", "INTEGER"));
48 tableColumn_.push_back(TableBase::ColumnInfo("disk_writes", "INTEGER"));
49 tableColumn_.push_back(TableBase::ColumnInfo("disk_reads", "INTEGER"));
50 tablePriKey_.push_back("ts");
51 }
52
~LiveProcessTable()53 LiveProcessTable::~LiveProcessTable() {}
54
CreateCursor()55 std::unique_ptr<TableBase::Cursor> LiveProcessTable::CreateCursor()
56 {
57 return std::make_unique<Cursor>(dataCache_, this);
58 }
59
Cursor(const TraceDataCache *dataCache, TableBase *table)60 LiveProcessTable::Cursor::Cursor(const TraceDataCache *dataCache, TableBase *table)
61 : TableBase::Cursor(dataCache, table, static_cast<uint32_t>(dataCache->GetConstLiveProcessData().Size())),
62 liveProcessDetailDataObj_(dataCache->GetConstLiveProcessData())
63 {
64 }
65
~Cursor()66 LiveProcessTable::Cursor::~Cursor() {}
67
Column(int32_t column) const68 int32_t LiveProcessTable::Cursor::Column(int32_t column) const
69 {
70 switch (static_cast<Index>(column)) {
71 case Index::TS: {
72 sqlite3_result_int64(context_,
73 static_cast<int64_t>(liveProcessDetailDataObj_.TimeStampData()[CurrentRow()]));
74 break;
75 }
76 case Index::DUR: {
77 sqlite3_result_int64(context_, static_cast<int64_t>(liveProcessDetailDataObj_.Durs()[CurrentRow()]));
78 break;
79 }
80 case Index::CPU_TIME: {
81 sqlite3_result_int64(context_, static_cast<int64_t>(liveProcessDetailDataObj_.CpuTimes()[CurrentRow()]));
82 break;
83 }
84 case Index::PROCESS_ID: {
85 sqlite3_result_int(context_, static_cast<int32_t>(liveProcessDetailDataObj_.ProcessID()[CurrentRow()]));
86 break;
87 }
88 case Index::PROCESS_NAME: {
89 sqlite3_result_text(context_, liveProcessDetailDataObj_.ProcessName()[CurrentRow()].c_str(),
90 STR_DEFAULT_LEN, nullptr);
91 break;
92 }
93 case Index::PARENT_PROCESS_ID: {
94 sqlite3_result_int(context_,
95 static_cast<int32_t>(liveProcessDetailDataObj_.ParentProcessID()[CurrentRow()]));
96 break;
97 }
98 case Index::UID: {
99 sqlite3_result_int(context_, static_cast<int32_t>(liveProcessDetailDataObj_.Uid()[CurrentRow()]));
100 break;
101 default:
102 HandleTypeColumns(column);
103 }
104 }
105 return SQLITE_OK;
106 }
HandleTypeColumns(int32_t liveProcessColumn) const107 void LiveProcessTable::Cursor::HandleTypeColumns(int32_t liveProcessColumn) const
108 {
109 switch (static_cast<Index>(liveProcessColumn)) {
110 case Index::USER_NAME: {
111 sqlite3_result_text(context_, liveProcessDetailDataObj_.UserName()[CurrentRow()].c_str(), STR_DEFAULT_LEN,
112 nullptr);
113 break;
114 }
115 case Index::CPU_USAGE: {
116 sqlite3_result_double(context_, liveProcessDetailDataObj_.CpuUsage()[CurrentRow()]);
117 break;
118 }
119 case Index::PSS_INFO: {
120 sqlite3_result_int(context_, static_cast<int32_t>(liveProcessDetailDataObj_.PssInfo()[CurrentRow()]));
121 break;
122 }
123 case Index::THREAD_SUM: {
124 sqlite3_result_int(context_, static_cast<int32_t>(liveProcessDetailDataObj_.Threads()[CurrentRow()]));
125 break;
126 }
127 case Index::DISK_WRITES: {
128 sqlite3_result_int(context_, static_cast<int32_t>(liveProcessDetailDataObj_.DiskWrites()[CurrentRow()]));
129 break;
130 }
131 case Index::DISK_READS: {
132 sqlite3_result_int(context_, static_cast<int32_t>(liveProcessDetailDataObj_.DiskReads()[CurrentRow()]));
133 break;
134 }
135 default:
136 TS_LOGF("Unregistered liveProcessColumn : %d", liveProcessColumn);
137 break;
138 }
139 }
140 } // namespace TraceStreamer
141 } // namespace SysTuning
142