-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcessParser.h
More file actions
307 lines (280 loc) · 9.61 KB
/
ProcessParser.h
File metadata and controls
307 lines (280 loc) · 9.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#pragma once
#include <algorithm>
#include <iostream>
#include <math.h>
#include <thread>
#include <chrono>
#include <iterator>
#include <string>
#include <stdlib.h>
#include <stdio.h>
#include <vector>
#include <fstream>
#include <sstream>
#include <stdexcept>
#include <cerrno>
#include <cstring>
#include <dirent.h>
#include <time.h>
#include <unistd.h>
#include "constants.h"
#include "util.h"
class ProcessParser {
public:
static std::string getCmd(const std::string& pid);
static std::vector<std::string> getPidList();
static std::string getVmSize(const std::string& pid);
static std::string getCpuPercent(const std::string& pid);
static double getSysUpTime();
static std::string getProcUpTime(const std::string& pid);
static std::string getProcUser(const std::string& pid);
static std::vector<std::string> getSysCpuPercent(std::string coreNumber = "");
static float getSysRamPercent();
static std::string getSysKernelVersion();
static int getTotalThreads();
static int getNumberOfCores();
static int getTotalNumberOfProcesses();
static int getNumberOfRunningProcesses();
static std::string getOsName();
static float getSysActiveCpuTime(std::vector<std::string> values);
static float getSysIdleCpuTime(std::vector<std::string> values);
static std::string printCpuStats(std::vector<std::string> values1, std::vector<std::string> values2);
};
inline std::string ProcessParser::getCmd(const std::string& pid){
std::ifstream input = Util::getStream((Path::basePath() + pid + Path::cmdPath()));
std::string cmdLine;
std::getline(input, cmdLine);
return cmdLine;
}
inline std::vector<std::string> ProcessParser::getPidList(){
std::vector<std::string> pidList;
DIR* dir = opendir(Path::basePath().c_str());
struct dirent* dp;
while((dp = readdir(dir)) != nullptr){
std::string line = dp->d_name;
if(std::all_of(line.begin(),line.end(), ::isdigit))
pidList.push_back(line);
}
closedir(dir);
return pidList;
}
inline std::string ProcessParser::getVmSize(const std::string& pid){
std::ifstream input = Util::getStream((Path::basePath() + pid + Path::statusPath()));
std::string vmSize;
while (std::getline(input, vmSize))
{
if(vmSize.rfind("VmSize:",0) == 0) {
std::istringstream iss(vmSize);
std::string key, value, unit;
iss >> key >> value >> unit;
return std::to_string(stof(value)/float(1024))+" "+"MB";
}
}
return "0 MB";
}
inline std::string ProcessParser::getCpuPercent(const std::string& pid){
std::ifstream stat = Util::getStream(Path::basePath() + pid + "/" + Path::statPath());
std::string line;
std::string utime;
std::string stime;
std::string starttime;
std::getline(stat, line);
std::istringstream iss(line);
std::string token;
int index =1;
while(iss >> token){
switch (index)
{
case 14:
utime = token;
break;
case 15:
stime = token;
break;
case 22:
starttime = token;
break;
}
++index;
}
double uptime = ProcessParser::getSysUpTime();
double hertz = sysconf(_SC_CLK_TCK);
if((uptime - (stof(starttime)/hertz)) <= 0.0000001)return "0.0";
return std::to_string(100 * ((stof(utime)+stof(stime)) / hertz) / (uptime - (stof(starttime)/hertz)));
}
inline double ProcessParser::getSysUpTime(){
std::ifstream uptimeFile = Util::getStream((Path::basePath()+Path::upTimePath()));
std::string line;
std::getline(uptimeFile, line);
std::istringstream iss(line);
iss >> line;
return stod(line);
}
inline std::string ProcessParser::getProcUpTime(const std::string& pid){
std::ifstream stat = Util::getStream((Path::basePath() + pid + "/" + Path::statPath()));
std::string line;
std::getline(stat,line);
std::istringstream iss(line);
std::string token;
int index =1;
std::string starttime;
while(iss >>token){
if(index == 22)starttime = token;
index++;
}
double uptime = ProcessParser::getSysUpTime();
double hertz = sysconf(_SC_CLK_TCK);
return std::to_string(uptime - (stod(starttime) / hertz));
}
inline std::string ProcessParser::getProcUser(const std::string& pid){
std::ifstream stream = Util::getStream((Path::basePath() + pid + Path::statusPath()));
std::string line;
std::string user;
while(std::getline(stream, line)){
if(line.rfind("Uid:",0) == 0){
std::istringstream iss(line);
std::string key;
iss >> key >>user;
break;
}
}
stream = Util::getStream("/etc/passwd");
while(std::getline(stream, line)){
if(line.find(("x:"+user)) != std::string::npos){
user = line.substr(0,line.find(":"));
return user;
}
}
return std::string();
}
inline std::vector<std::string> ProcessParser::getSysCpuPercent(std::string coreNumber){
std::ifstream stat = Util::getStream((Path::basePath()+Path::statPath()));
std::string line,name = "cpu"+coreNumber ;
while(std::getline(stat,line)){
if(line.rfind(name,0) == 0){
std::istringstream iss(line);
std::istream_iterator<std::string> beg(iss),end;
std::vector<std::string> values(beg,end);
return values;
}
}
return (std::vector<std::string>());
}
inline float ProcessParser::getSysRamPercent(){
std::ifstream meminfo = Util::getStream((Path::basePath()+Path::mamInfoPath()));
std::string line,key;
float memFree,memAvailable,buffers;
while(std::getline(meminfo,line)){
std::istringstream iss(line);
if(line.rfind("MemFree:",0) == 0){
iss >> key >> memFree;
}
else if(line.rfind("MemAvailable:",0) == 0){
iss >> key >> memAvailable;
}
else if(line.rfind("Buffers:",0) == 0){
iss >> key >> buffers;
}
}
return float(100.0*(1-(memFree/(memAvailable-buffers))));
}
inline std::string ProcessParser::getSysKernelVersion(){
std::ifstream input = Util::getStream((Path::basePath()+Path::versionPath()));
std::string line,token1,token2,token3;
std::getline(input, line);
std::istringstream iss(line);
if(iss >> token1 >> token2 >> token3)
return token3;
else
return std::string();
}
inline int ProcessParser::getTotalThreads(){
int totalThreads =0;
std::vector<std::string> pidList = ProcessParser::getPidList();
for(const std::string& pid : pidList){
try {
std::ifstream stream = Util::getStream((Path::basePath()+pid+Path::statusPath()));
std::string line,key,value;
while(getline(stream,line)){
if(line.rfind("Threads:",0)==0){
std::istringstream iss(line);
iss >> key >> value;
totalThreads += std::stoi(value);
break;
}
}
} catch (...) {
continue;
}
}
return totalThreads;
}
inline int ProcessParser::getNumberOfCores() {
return sysconf(_SC_NPROCESSORS_ONLN);
}
inline int ProcessParser::getTotalNumberOfProcesses()
{
std::ifstream stream = Util::getStream((Path::basePath() + Path::statPath()));
std::string line,key ,value;
while (std::getline(stream, line)) {
if (line.rfind("processes",0) == 0) {
std::istringstream iss(line);
iss >> key >> value;
break;
}
}
return std::stoi(value);
}
inline int ProcessParser::getNumberOfRunningProcesses()
{
std::ifstream stream = Util::getStream((Path::basePath() + Path::statPath()));
std::string line,key ,value;
while (std::getline(stream, line)) {
if (line.rfind("procs_running",0) == 0) {
std::istringstream iss(line);
iss >> key >> value;
break;
}
}
return std::stoi(value);
}
inline std::string ProcessParser::getOsName(){
std::ifstream stream = Util::getStream(("/etc/os-release"));
std::string line;
while (std::getline(stream, line)) {
if (line.rfind("PRETTY_NAME=",0) == 0) {
std::string result = line.substr(line.find("=")+1);
if (result.front() == '"' && result.back() == '"') {
result = result.substr(1, result.length()-2);
}
return result;
}
}
return std::string();
}
inline float ProcessParser::getSysActiveCpuTime(std::vector<std::string> values){
return (stof(values[CPUStates::S_USER]) +
stof(values[CPUStates::S_NICE]) +
stof(values[CPUStates::S_SYSTEM]) +
stof(values[CPUStates::S_IRQ]) +
stof(values[CPUStates::S_SOFTIRQ]) +
stof(values[CPUStates::S_STEAL]) +
stof(values[CPUStates::S_GUEST]) +
stof(values[CPUStates::S_GUEST_NICE]));
}
inline float ProcessParser::getSysIdleCpuTime(std::vector<std::string> values){
return (stof(values[CPUStates::S_IDLE]) +
stof(values[CPUStates::S_IOWAIT]));
}
inline std::string ProcessParser::printCpuStats(std::vector<std::string> values1, std::vector<std::string> values2){
float activeTime = getSysActiveCpuTime(values2) - getSysActiveCpuTime(values1);
float idleTime = getSysIdleCpuTime(values2) - getSysIdleCpuTime(values1);
float totalTime = activeTime + idleTime;
if(totalTime == 0) {
return "0.0";
}
float result = 100.0 * (activeTime / totalTime);
char buffer[32];
snprintf(buffer, sizeof(buffer), "%.2f", result);
return std::string(buffer);
}