forked from udacity/CppND-System-Monitor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcessParser.h
More file actions
438 lines (357 loc) · 12.3 KB
/
Copy pathProcessParser.h
File metadata and controls
438 lines (357 loc) · 12.3 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
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
#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"
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 getSysActiveCpuTime(vector<string> values);
static float getSysIdleCpuTime(vector<string> values);
static float getSysRamPercent();
static string getSysKernelVersion();
static int getTotalThreads();
static int getNumberOfCores();
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);
};
string ProcessParser::getCmd(string pid) {
string line;
ifstream stream;
Util::getStream((Path::basePath() + pid + Path::cmdPath()), stream);
getline(stream, line);
return line;
}
vector<string> ProcessParser::getPidList() {
DIR* dir;
// scan /proc for all directories with numbers as their names and if found store it in a container
vector<string> container;
if(!(dir = opendir("/proc")))
throw std::runtime_error(std::strerror(errno));
while(dirent* dirp = readdir(dir)) {
// check if not directory
if(dirp->d_type != DT_DIR)
continue;
// check every character of the name to be digit
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);
}
}
// check if directory is closed
if(closedir(dir))
throw std::runtime_error(std::strerror(errno));
return container;
}
string ProcessParser::getVmSize(string pid) {
string line;
// declare name to search in file
string name = "VmData"; // in case of doubt, verify it from "cat /proc/pid#/status"
float result;
// open stream for specific file
std::ifstream stream;
Util::getStream((Path::basePath() + pid + Path::statusPath()), stream);
while(getline(stream, line)) {
// searching line by line
if(line.compare(0, name.size(), name) == 0) {
//slicing string line on ws for values using sstream
istringstream buf(line);
istream_iterator<string> beg(buf), end;
vector<string> values(beg, end);
// conversion of kB -> MB
result = stof(values[1])/float(1024);
break;
}
}
return to_string(result);
}
std::string ProcessParser::getCpuPercent(string pid) {
string line;
float result;
// open stream for specific file
std::ifstream stream;
Util::getStream((Path::basePath() + pid + "/" + Path::statPath()), stream);
getline(stream, line);
string str = line;
//slicing string line on ws for values using sstream
istringstream buf(str);
istream_iterator<string> beg(buf), end;
vector<string> values(beg, end);
// acquring revelant time for calculation of CPU usage for selected process
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);
}
long int ProcessParser::getSysUpTime() {
string line;
ifstream stream;
Util::getStream((Path::basePath() + Path::upTimePath()), stream);
getline(stream, line);
string str = line;
//slicing string line on ws for values using sstream
istringstream buf(str);
istream_iterator<string> beg(buf), end;
vector<string> values(beg, end);
return stoi(values[0]);
}
std::string ProcessParser::getProcUpTime(string pid) {
string line;
float result;
ifstream stream;
Util::getStream((Path::basePath() + pid + "/" + Path::statPath()), stream);
getline(stream, line);
string str = line;
//slicing string line on ws for values using sstream
istringstream buf(str);
istream_iterator<string> beg(buf), end;
vector<string> values(beg, end);
// sysconf returns clock ticks of the host machine
return to_string(float(stof(values[13])/sysconf(_SC_CLK_TCK)));
}
std::string ProcessParser::getProcUser(string pid) {
string line;
string result = "";
string name = "Uid:"; // Uid = User ID
ifstream stream;
Util::getStream((Path::basePath() + pid + Path::statusPath()), stream);
while(getline(stream, line)) {
if (line.compare(0, name.size(), name) == 0) {
//slicing string line for using sstream
istringstream buf(line);
istream_iterator<string> beg(buf), end;
vector<string> values(beg, end);
result = values[1];
break;
}
}
// for getting user name
Util::getStream("/etc/passwd", stream);
name = ("x:" + result);
// searching for the username
while(getline(stream, line)) {
if (line.find(name) != string::npos) {
result = line.substr(0, line.find(":"));
return result;
}
}
return "";
}
vector<string> ProcessParser::getSysCpuPercent(string coreNumber){
// function gives CPU percent for entire CPU when no argument is passed, else specific CPU core
string line;
string name = "cpu" + coreNumber;
int result;
ifstream stream;
Util::getStream((Path::basePath() + Path::statPath()), stream);
while(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;
}
}
return (vector<string> ());
}
float ProcessParser::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 ProcessParser::getSysIdleCpuTime(vector<string> values) {
return (stof(values[S_IDLE]) + stod(values[S_IOWAIT]));
}
float ProcessParser::getSysRamPercent() {
string line;
string name1 = "MemAvailble:";
string name2 = "MemFree:";
string name3 = "Buffers:";
int result;
ifstream stream;
Util::getStream((Path::basePath() + Path::memInfoPath()), stream);
float total_mem = 0;
float free_mem = 0;
float buffers = 0;
while(getline(stream, line)) {
if(total_mem != 0 && free_mem != 0)
break;
if(line.compare(0, name1.size(), name1) == 0) {
istringstream buf(line);
istream_iterator<string> beg(buf), end;
vector<string> values(beg, end);
total_mem = stof(values[1]);
}
if (line.compare(0, name2.size(), name2) == 0) {
istringstream buf(line);
istream_iterator<string> beg(buf), end;
vector<string> values(beg, end);
free_mem = stof(values[1]);
}
if (line.compare(0, name3.size(), name3) == 0) {
istringstream buf(line);
istream_iterator<string> beg(buf), end;
vector<string> values(beg, end);
buffers = stof(values[1]);
}
}
// calculate usage
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()), stream);
while (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 "";
}
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];
// Get every process and read their number of threads
ifstream stream;
Util::getStream((Path::basePath() + pid + Path::statusPath()), stream);
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::getNumberOfCores() {
// Get number of host cpu cores
string line;
string name = "cpu cores";
ifstream stream;
Util::getStream((Path::basePath() + "cpuinfo"), stream);
while(getline(stream, line)) {
// searching line by line
if(line.compare(0, name.size(), name) == 0) {
istringstream buf(line);
istream_iterator<string> beg(buf), end;
vector<string> values(beg, end);
return stoi(values[3]);
}
}
return 0;
}
int ProcessParser::getTotalNumberOfProcesses() {
string line;
int result = 0;
string name = "processes";
ifstream stream;
Util::getStream((Path::basePath() + Path::statPath()), stream);
while (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()), stream);
while (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;
}
string ProcessParser::getOSName() {
string line;
string name = "PRETTY_NAME=";
ifstream stream;
Util::getStream(("/etc/os-release"), stream);
while (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 "";
}
string ProcessParser::PrintCpuStats(vector<string> values1, vector<string>values2) {
float active_time = getSysActiveCpuTime(values2) - getSysActiveCpuTime(values1);
float idle_time = getSysIdleCpuTime(values2) - getSysIdleCpuTime(values1);
float total_time = active_time + idle_time;
float result = 100.0*(active_time / total_time);
return to_string(result);
}
bool ProcessParser::isPidExisting(string pid) {
vector<string> pidList = ProcessParser::getPidList();
for(int i=0; i < pidList.size(); i++) {
if(pidList[i] == pid)
return true;
}
return false;
}