• 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 "live_process_table.h"
17 
18 namespace SysTuning {
19 namespace TraceStreamer {
20 enum Index {
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 (column) {
71         case TS: {
72             sqlite3_result_int64(context_,
73                                  static_cast<int64_t>(liveProcessDetailDataObj_.TimeStampData()[CurrentRow()]));
74             break;
75         }
76         case DUR: {
77             sqlite3_result_int64(context_, static_cast<int64_t>(liveProcessDetailDataObj_.Durs()[CurrentRow()]));
78             break;
79         }
80         case CPU_TIME: {
81             sqlite3_result_int64(context_, static_cast<int64_t>(liveProcessDetailDataObj_.CpuTimes()[CurrentRow()]));
82             break;
83         }
84         case PROCESS_ID: {
85             sqlite3_result_int(context_, static_cast<int32_t>(liveProcessDetailDataObj_.ProcessID()[CurrentRow()]));
86             break;
87         }
88         case PROCESS_NAME: {
89             sqlite3_result_text(context_, liveProcessDetailDataObj_.ProcessName()[CurrentRow()].c_str(),
90                                 STR_DEFAULT_LEN, nullptr);
91             break;
92         }
93         case PARENT_PROCESS_ID: {
94             sqlite3_result_int(context_,
95                                static_cast<int32_t>(liveProcessDetailDataObj_.ParentProcessID()[CurrentRow()]));
96             break;
97         }
98         case UID: {
99             sqlite3_result_int(context_, static_cast<int32_t>(liveProcessDetailDataObj_.Uid()[CurrentRow()]));
100             break;
101         }
102         case USER_NAME: {
103             sqlite3_result_text(context_, liveProcessDetailDataObj_.UserName()[CurrentRow()].c_str(), STR_DEFAULT_LEN,
104                                 nullptr);
105             break;
106         }
107         case CPU_USAGE: {
108             sqlite3_result_double(context_, liveProcessDetailDataObj_.CpuUsage()[CurrentRow()]);
109             break;
110         }
111         case PSS_INFO: {
112             sqlite3_result_int(context_, static_cast<int32_t>(liveProcessDetailDataObj_.PssInfo()[CurrentRow()]));
113             break;
114         }
115         case THREAD_SUM: {
116             sqlite3_result_int(context_, static_cast<int32_t>(liveProcessDetailDataObj_.Threads()[CurrentRow()]));
117             break;
118         }
119         case DISK_WRITES: {
120             sqlite3_result_int(context_, static_cast<int32_t>(liveProcessDetailDataObj_.DiskWrites()[CurrentRow()]));
121             break;
122         }
123         case DISK_READS: {
124             sqlite3_result_int(context_, static_cast<int32_t>(liveProcessDetailDataObj_.DiskReads()[CurrentRow()]));
125             break;
126         }
127         default:
128             TS_LOGF("Unregistered column : %d", column);
129             break;
130     }
131     return SQLITE_OK;
132 }
133 } // namespace TraceStreamer
134 } // namespace SysTuning
135