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