-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProcessParser.h
344 lines (315 loc) · 10.9 KB
/
ProcessParser.h
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#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"
using namespace std;
class ProcessParser{
private:
std::ifstream stream;
public:
static string getCmd(string pid);
static vector<string> getPidList();
static std::string getVmSize(string pid);
static std::string getCpuPercent(string pid);
static long int getSysUpTime();
static std::string getProcUpTime(string pid);
static string getProcUser(string pid);
static vector<string> getSysCpuPercent(string coreNumber);
static float getSysRamPercent();
static string getSysKernelVersion();
static int getNumberOfCores();
static int getTotalThreads();
static int getTotalNumberOfProcesses();
static int getNumberOfRunningProcesses();
static string getOSName();
static std::string PrintCpuStats(std::vector<std::string> values1, std::vector<std::string>values2);
static bool isPidExisting(string pid);
};
std::string ProcessParser::getVmSize(std::string pid){
std::string line;
std::string name = "vmData";
float result;
std::ifstream stream = Util::getStream(Path::basePath() + pid + Path::statusPath());
while(std::getline(stream, line)){
if(line.compare(0, name.size(), name) == 0){
std::istringstream buf(line);
std::istream_iterator<string> beg(buf), end;
vector<string> values(beg, end);
result = (stof(values[1])/1024);
break;
}
}
return to_string(result);
}
std::string ProcessParser::getCpuPercent(std::string pid){
std::string line;
float result;
std::ifstream stream = Util::getStream(Path::basePath() + pid + "/" + Path::statPath());
std::getline(stream, line);
std::istringstream buf(line);
std::istream_iterator<string> beg(buf), end;
std::vector<string> values(beg, end);
float utime = stof(ProcessParser::getProcUpTime(pid));
float stime = stof(values[14]);
float cutime = stof(values[15]);
float cstime = stof(values[16]);
float starttime = stof(values[21]);
float uptime = ProcessParser::getSysUpTime();
float freq = sysconf(_SC_CLK_TCK);
float total_time = utime + stime + cutime + cstime;
float seconds = uptime - (starttime/freq);
result = 100.0*((total_time/freq)/seconds);
return to_string(result);
}
std::string ProcessParser::getProcUpTime(std::string pid){
std::string line;
std::ifstream stream = Util::getStream(Path::basePath() + pid + "/" + Path::statPath());
std::getline(stream, line);
std::istringstream buf(line);
std::istream_iterator<string> beg(buf), end;
std::vector<string> values(beg, end);
return to_string(float(stof(values[13])/sysconf(_SC_CLK_TCK)));
}
long int ProcessParser::getSysUpTime(){
std::string line;
std::ifstream stream = Util::getStream(Path::basePath() + Path::upTimePath());
std::getline(stream, line);
std::istringstream buf(line);
std::istream_iterator<string> beg(buf), end;
std::vector<string> values(beg, end);
return stoi(values[0]);
}
string ProcessParser::getProcUser(std::string pid){
std::string line;
std::string name = "Uid";
std::string result = "";
std::ifstream stream = Util::getStream(Path::basePath() + pid + Path::statusPath());
while(std::getline(stream, line)){
if(line.compare(0, name.size(), name) == 0){
std::istringstream buf(line);
std::istream_iterator<std::string> beg(buf), end;
std::vector<string> values(beg, end);
result = values[1];
break;
}
}
stream = Util::getStream("/etc/passwd");
name = ("x:"+result);
while(std::getline(stream, line)){
if(line.find(name) != std::string::npos){
result = line.substr(0, line.find(":"));
return result;
}
}
return "";
}
std::vector<std::string> ProcessParser::getPidList(){
DIR* dir;
std::vector<std::string> container;
if(!(dir = opendir("/proc")))
throw std::runtime_error(std::strerror(errno));
while(dirent* dirp = readdir(dir)){
if(dirp->d_type != DT_DIR)
continue;
if (all_of(dirp->d_name, dirp->d_name + std::strlen(dirp->d_name), [](char c){ return std::isdigit(c); })) {
container.push_back(dirp->d_name);
}
}
if(closedir(dir))
throw std::runtime_error(std::strerror(errno));
return container;
}
std::string ProcessParser::getCmd(std::string pid){
std::string line;
std::ifstream stream = Util::getStream(Path::basePath() + pid + Path::cmdPath());
std::getline(stream, line);
return line;
}
int ProcessParser::getNumberOfCores(){
std::string line;
std::string name = "cpu cores";
std::ifstream stream = Util::getStream(Path::basePath() + "cpuinfo");
while(std::getline(stream, line)){
if(line.compare(0, name.size(), name) == 0){
std::istringstream buf(line);
std::istream_iterator<string> beg(buf), end;
std::vector<string> values(beg, end);
return stoi(values[3]);
break;
}
}
return 0;
}
std::vector<string> ProcessParser::getSysCpuPercent(std::string coreNumber){
std::string line;
std::string name = "cpu" + coreNumber;
std::ifstream stream = Util::getStream(Path::basePath() + Path::statPath());
while(std::getline(stream, line)){
if(line.compare((0), name.size(), name) == 0){
std::istringstream buf(line);
std::istream_iterator<string> beg(buf), end;
std::vector<string> values(beg, end);
return values;
}
}
return (vector<string>());
}
float getSysActiveCpuTime(vector<string> values){
return (stof(values[S_USER]) +
stof(values[S_NICE]) +
stof(values[S_SYSTEM]) +
stof(values[S_IRQ]) +
stof(values[S_SOFTIRQ]) +
stof(values[S_STEAL]) +
stof(values[S_GUEST]) +
stof(values[S_GUEST_NICE]));
}
float getSysIdleCpuTime(vector<string>values){
return (stof(values[S_IDLE]) + stof(values[S_IOWAIT]));
}
/*
Because CPU stats can be calculated only if you take measures in two different time,
this function has two parameters: two vectors of relevant values.
We use a formula to calculate overall activity of processor.
*/
string ProcessParser::PrintCpuStats(vector<string> values1, vector<string> values2){
float activeTime = getSysActiveCpuTime(values2) - getSysActiveCpuTime(values1);
float idleTime = getSysIdleCpuTime(values2) - getSysIdleCpuTime(values1);
float totalTime = activeTime + idleTime;
float result = 100.0*(activeTime / totalTime);
return to_string(result);
}
float ProcessParser::getSysRamPercent(){
std::string line;
std::string name1 = "MemAvailable:";
std::string name2 = "MemFree:";
std::string name3 = "Buffers:";
float total_mem = 0;
float free_mem = 0;
float buffers = 0;
std::ifstream stream = Util::getStream(Path::basePath() + Path::memInfoPath());
while(std::getline(stream, line)){
if(total_mem != 0 && free_mem != 0){
break;
}
if(line.compare(0, name1.size(), name1) == 0){
std::istringstream buf(line);
std::istream_iterator<string> beg(buf), end;
std::vector<string> values(beg, end);
total_mem = stof(values[1]);
}
else if(line.compare(0, name2.size(), name2) == 0){
std::istringstream buf(line);
std::istream_iterator<string> beg(buf), end;
std::vector<string> values(beg, end);
free_mem = stof(values[1]);
}
else if(line.compare(0, name3.size(), name3) == 0){
std::istringstream buf(line);
std::istream_iterator<string> beg(buf), end;
std::vector<string> values(beg, end);
buffers = stof(values[1]);
}
else{}
}
return float(100.0*(1-(free_mem/(total_mem-buffers))));
}
string ProcessParser::getSysKernelVersion(){
string line;
string name = "Linux version ";
ifstream stream = Util::getStream((Path::basePath() + Path::versionPath()));
while (std::getline(stream, line)) {
if (line.compare(0, name.size(),name) == 0) {
istringstream buf(line);
istream_iterator<string> beg(buf), end;
vector<string> values(beg, end);
return values[2];
}
}
return "";
}
string ProcessParser::getOSName(){
string line;
string name = "PRETTY_NAME=";
ifstream stream = Util::getStream(("/etc/os-release"));
while (std::getline(stream, line)) {
if (line.compare(0, name.size(), name) == 0) {
std::size_t found = line.find("=");
found++;
string result = line.substr(found);
result.erase(std::remove(result.begin(), result.end(), '"'), result.end());
return result;
}
}
return "";
}
int ProcessParser::getTotalThreads(){
string line;
int result = 0;
string name = "Threads:";
vector<string>_list = ProcessParser::getPidList();
for (int i=0 ; i<_list.size();i++) {
string pid = _list[i];
//getting every process and reading their number of their threads
ifstream stream = Util::getStream((Path::basePath() + pid + Path::statusPath()));
while (std::getline(stream, line)) {
if (line.compare(0, name.size(), name) == 0) {
istringstream buf(line);
istream_iterator<string> beg(buf), end;
vector<string> values(beg, end);
result += stoi(values[1]);
break;
}
}
}
return result;
}
int ProcessParser::getTotalNumberOfProcesses(){
string line;
int result = 0;
string name = "processes";
ifstream stream = Util::getStream(Path::basePath() + Path::statPath());
while (std::getline(stream, line)){
if (line.compare(0, name.size(), name) == 0){
istringstream buf(line);
istream_iterator<string> beg(buf), end;
vector<string> values(beg, end);
result += stoi(values[1]);
break;
}
}
return result;
}
int ProcessParser::getNumberOfRunningProcesses(){
string line;
int result = 0;
string name = "procs_running";
ifstream stream = Util::getStream((Path::basePath() + Path::statPath()));
while (std::getline(stream, line)) {
if (line.compare(0, name.size(), name) == 0) {
istringstream buf(line);
istream_iterator<string> beg(buf), end;
vector<string> values(beg, end);
result += stoi(values[1]);
break;
}
}
return result;
}