forked from udacity/CppND-System-Monitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProcessParser.h
293 lines (262 loc) · 8.74 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
#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 "consts.h"
#include <pwd.h>
using namespace std;
class ProcessParser{
public:
std::ifstream stream;
static long get_sys_active_cpu_time(vector<int> values); //given
static long get_sys_idle_cpu_time(vector<int>values); //given
public:
static string getCmd(string pid); // done
static vector<int> getPidList(); // done
static std::string getVmSize(string pid); // done
static float getCpuPercent(string pid); // done
static float getSysUpTime(); // done
static float getProcUpTime(string pid); // done
static string getProcUser(string pid); // done
static vector<int> getSysCpuPercent(string coreNumber = ""); // done
static float getSysRamPercent(); // done
static string getSysKernelVersion(); // done
static int getTotalThreads(); // done
static int getTotalNumberOfProcesses(); // done
static int getNumberOfRunningProcesses(); // done
static string getOSName(); // done
static std::string PrintCpuStats(std::vector<int> values1, std::vector<int>values2); // given
static bool isPidExisting(string pid); // done
static int get_number_of_cores(); // done
};
//////////////// System Info /////////////////
int ProcessParser::get_number_of_cores(){
// Get the number of host cpu cores
string line;
string name = "cpu cores";
ifstream stream;
Util::getStream((Path::BASE + "cpuinfo"), 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);
return stoi(values[3]);
}
}
return 0;
}
std::string ProcessParser::getOSName(){
ifstream s;
Util::getStream("/etc/os-release", s);
std::string name = Util::getStringFromStream(s, "PRETTY_NAME", '=');
s.close();
return name.substr(1, name.size()-2);
}
float ProcessParser::getSysRamPercent(){
ifstream s;
Util::getStream(Path::BASE + Path::MEM_F, s);
float mtotal = Util::getItemFromStream<float>(s, "MemTotal");
float mavail = Util::getItemFromStream<float>(s, "MemAvailable");
//float buffs = Util::getItemFromStream<float>(s, "Buffers");
s.close();
return 100.0 * (1-mavail/mtotal);
}
std::string ProcessParser::getSysKernelVersion(){
ifstream s;
Util::getStream(Path::BASE + Path::VERSION_F, s);
std::string line;
while(getline(s, line)){
istringstream ss(line);
vector<std::string> vec = Util::getSpacedList<std::string>(ss);
s.close();
return vec[2];
}
}
int ProcessParser::getTotalThreads(){
vector<int> pidlist = getPidList();
int total {0};
for (int pid : pidlist){
ifstream s;
Util::getStream(Path::BASE + to_string(pid) + Path::STATUS_F, s);
int threads = Util::getItemFromStream<int>(s, "Threads");
s.close();
total += threads;
}
return total;
}
int ProcessParser::getTotalNumberOfProcesses(){
ifstream s;
Util::getStream(Path::BASE + Path::STAT_F, s);
int procs = Util::getItemFromStream<int>(s, "processes", ' ');
s.close();
return procs;
}
int ProcessParser::getNumberOfRunningProcesses(){
ifstream s;
Util::getStream(Path::BASE + Path::STAT_F, s);
int procs = Util::getItemFromStream<int>(s, "procs_running", ' ');
s.close();
return procs;
}
string ProcessParser::PrintCpuStats(vector<int> values1, vector<int> values2)
{
/*
Because CPU stats can be calculated only if you take measures in two different time,
this function has two paramaters: two vectors of relevant values.
We use a formula to calculate overall activity of processor.
*/
double a1 = get_sys_active_cpu_time(values1);
double a2 = get_sys_active_cpu_time(values2);
double at = a2 -a1;
double i1 = get_sys_idle_cpu_time(values1);
double i2 = get_sys_idle_cpu_time(values2);
double it = i2 -i1;
double result = 100.0*(at / (at+it));
return to_string(result);
}
long ProcessParser::get_sys_active_cpu_time(vector<int> values){
return ((values[S_USER]) +
(values[S_NICE]) +
(values[S_SYSTEM]) +
(values[S_IRQ]) +
(values[S_SOFTIRQ]) +
(values[S_STEAL]) +
(values[S_GUEST]) +
(values[S_GUEST_NICE]));
}
long ProcessParser::get_sys_idle_cpu_time(vector<int>values){
return ((values[S_IDLE]) + (values[S_IOWAIT]));
}
vector<int> ProcessParser::getSysCpuPercent(string coreNumber){
std::ifstream s;
Util::getStream(Path::BASE + Path::STAT_F, s);
std::string line, key, value, outs("test");
std::string name = "cpu" + coreNumber;
while(getline(s, line)){
std::istringstream ss(line);
getline(ss, key, ' ');
if (key.substr(0,name.size())==name){
vector<int> v = Util::getSpacedList<int>(ss);
s.close();
return v;
}
}
s.close();
vector<int> vec;
vec.push_back(-1);
return vec;
}
float ProcessParser::getSysUpTime(){
std::ifstream s;
Util::getStream(Path::BASE + Path::UPTIME_F, s);
std::string line, key, value;
float uptime;
s >> uptime;
s.close();
return uptime;
}
//////////////// Process Info /////////////////
std::vector<int> ProcessParser::getPidList(){
DIR* dir;
vector<int> container;
if(!(dir = opendir("/proc")))
throw std::runtime_error(std::strerror(errno));
while (dirent* dirp = readdir(dir)) {
if (dirp->d_type != DT_DIR)
continue;
try {
if (to_string(stoi(dirp->d_name)) == dirp->d_name)
container.push_back(stoi(dirp->d_name));
}
catch (std::invalid_argument){
continue;
}
}
if (closedir(dir))
throw std::runtime_error(std::strerror(errno));
return container;
}
bool ProcessParser::isPidExisting(string pid){
vector<int> vec = ProcessParser::getPidList();
return (std::find(vec.begin(), vec.end(), stoi(pid)) != vec.end());
}
std::string ProcessParser::getVmSize(string pid){
ifstream s;
Util::getStream(Path::BASE + pid + Path::STATUS_F, s);
int size = Util::getItemFromStream<int>(s, "VmSize");
s.close();
return std::to_string(size/1024);
}
std::string ProcessParser::getProcUser(string pid){
ifstream s;
Util::getStream(Path::BASE + pid + Path::STATUS_F, s);
int uid = Util::getItemFromStream<int>(s,"Uid");
struct passwd *pws;
pws = getpwuid(uid);
s.close();
return pws->pw_name;
}
string ProcessParser::getCmd(string pid){
std::ifstream s;
Util::getStream(Path::BASE + pid + Path::CMD_F, s);
std::string line;
while(getline(s, line)){
s.close();
return line;
}
s.close();
return std::string("");
}
float ProcessParser::getProcUpTime(string pid){
std::ifstream s;
Util::getStream(Path::BASE + pid + Path::STAT_F, s);
std::string line, key, value;
while(getline(s, line)){
std::istringstream buf(line);
std::istream_iterator<string> beg(buf), end;
std::vector<string> vals(beg, end);
s.close();
return stof(vals[13])/sysconf(_SC_CLK_TCK);
}
s.close();
return -1;
}
float ProcessParser::getCpuPercent(string pid){
std::ifstream s;
Util::getStream(Path::BASE + pid + Path::STAT_F, s);
std::string line, key, value;
while(getline(s, line)){
std::istringstream buf(line);
std::istream_iterator<string> beg(buf), end;
std::vector<string> vals(beg, end);
float utime = stof(vals[13]);
float stime = stof(vals[14]);
float cutime = stof(vals[15]);
float cstime = stof(vals[16]);
float start = stof(vals[21]);
float sysup = ProcessParser::getSysUpTime();
float ticks = sysconf(_SC_CLK_TCK);
float total = (utime + stime + cutime + cstime) / ticks;
float result = total/(sysup - (start/ticks));
s.close();
return result * 100.0;
}
s.close();
return -1;
}