@@ -123,7 +123,7 @@ float LinuxParser::MemoryUtilization()
123
123
return value;
124
124
}
125
125
126
- // TODO: Read and return the system uptime
126
+ // Read and return the system uptime
127
127
long LinuxParser::UpTime ()
128
128
{
129
129
long int uptime{0 };
@@ -213,20 +213,14 @@ float LinuxParser::CpuUtilization(int pid)
213
213
// Positions 14, 15, 16, 17 and 22 are used to calculate CPU consumption per process
214
214
// https://stackoverflow.com/questions/16726779/how-do-i-get-the-total-cpu-usage-of-an-application-from-proc-pid-stat/16736599#16736599
215
215
if (index == 14 || index == 15 || index == 16 || index == 17 )
216
- {
217
216
cpu_tokens.push_back (stof (tmp));
218
- // std::cout << "PID: "<< pid << " Index: " << index << " Token: " << tmp << " ";
219
- }
220
217
else if (index == 22 )
221
218
{
222
219
cpu_tokens.push_back (stof (tmp));
223
- // std::cout << "PID: "<< pid << " Index: " << index << " Token: " << tmp << " ";
224
220
break ;
225
221
}
226
222
}
227
223
228
- // std::cout << "Vector's size: " << cpu_tokens.size() << std::endl;;
229
-
230
224
float total_time{0.0 };
231
225
long int hertz{sysconf (_SC_CLK_TCK)};
232
226
float seconds{0.0 };
@@ -243,11 +237,47 @@ float LinuxParser::CpuUtilization(int pid)
243
237
return cpu_consumption;
244
238
}
245
239
246
- // TODO: Read and return the total number of processes
247
- int LinuxParser::TotalProcesses () { return 0 ; }
240
+ // Read and return the total number of processes
241
+ int LinuxParser::TotalProcesses ()
242
+ {
243
+ std::string line;
244
+ // Pattern to look for total number of processes
245
+ std::string pattern{ R"( processes\s{1}(\d+))" };
246
+
247
+ std::ifstream filestream (kProcDirectory + kStatFilename );
248
+ std::string regular_expression_result;
249
+ if (filestream.is_open ())
250
+ {
251
+ while (std::getline (filestream, line))
252
+ {
253
+ regular_expression_result = RegularExpression (pattern, line, 1 );
254
+ if (regular_expression_result != " " )
255
+ break ;
256
+ }
257
+ }
258
+ return std::stoi (regular_expression_result);
259
+ }
260
+
261
+ // Read and return the number of running processes
262
+ int LinuxParser::RunningProcesses ()
263
+ {
264
+ std::string line;
265
+ // Pattern to look for total number of processes
266
+ std::string pattern{ R"( procs_running\s{1}(\d+))" };
248
267
249
- // TODO: Read and return the number of running processes
250
- int LinuxParser::RunningProcesses () { return 0 ; }
268
+ std::ifstream filestream (kProcDirectory + kStatFilename );
269
+ std::string regular_expression_result;
270
+ if (filestream.is_open ())
271
+ {
272
+ while (std::getline (filestream, line))
273
+ {
274
+ regular_expression_result = RegularExpression (pattern, line, 1 );
275
+ if (regular_expression_result != " " )
276
+ break ;
277
+ }
278
+ }
279
+ return std::stoi (regular_expression_result);
280
+ }
251
281
252
282
// Read and return the command associated with a process
253
283
string LinuxParser::Command (int pid)
@@ -263,9 +293,35 @@ string LinuxParser::Command(int pid)
263
293
return command_line;
264
294
}
265
295
266
- // TODO: Read and return the memory used by a process
267
- // REMOVE: [[maybe_unused]] once you define the function
268
- string LinuxParser::Ram (int pid[[maybe_unused]]) { return string (); }
296
+ // Read and return the memory used by a process
297
+ string LinuxParser::Ram (int pid)
298
+ {
299
+ std::string line;
300
+ std::string pattern{ R"( VmSize:\s{1}(\d+)\s{1}kB)" };
301
+
302
+ std::ifstream filestream (kProcDirectory + std::to_string (pid) + kStatusFilename );
303
+ std::string ram;
304
+
305
+ if (filestream.is_open ())
306
+ {
307
+ while (std::getline (filestream, line))
308
+ {
309
+ ram = RegularExpression (pattern, line, 1 );
310
+
311
+ if (ram != " " )
312
+ break ;
313
+ }
314
+ }
315
+
316
+ long int result{0 };
317
+ if (ram!=" " ) // to control cases when the process died in the middle of the process
318
+ {
319
+ result = std::stol (ram);
320
+ result = result / 1024 ;
321
+ }
322
+
323
+ return std::to_string (result);
324
+ }
269
325
270
326
// Read and return the user ID associated with a process
271
327
string LinuxParser::Uid (int pid)
@@ -315,6 +371,36 @@ string LinuxParser::User(int pid)
315
371
return user_name;
316
372
}
317
373
318
- // TODO: Read and return the uptime of a process
319
- // REMOVE: [[maybe_unused]] once you define the function
320
- long LinuxParser::UpTime (int pid[[maybe_unused]]) { return 0 ; }
374
+ // Read and return the uptime of a process
375
+ long LinuxParser::UpTime (int pid)
376
+ {
377
+ long int uptime{ 0 };
378
+ std::string line;
379
+
380
+ std::ifstream filestream (kProcDirectory + std::to_string (pid) + kStatFilename );
381
+
382
+ if (filestream.is_open ())
383
+ std::getline (filestream, line);
384
+
385
+ // Sometimes there are process that dies and then the pseudo file disappears, which causes errors.
386
+ // Thus, if no data is detected, the analysis is by passed
387
+ if (line != " " )
388
+ {
389
+ std::istringstream linestream{ line };
390
+ std::string tmp;
391
+ int index {0 };
392
+
393
+ while (linestream >> tmp)
394
+ {
395
+ ++index ;
396
+ if (index == 22 )
397
+ break ;
398
+ }
399
+
400
+ long int hertz{ sysconf (_SC_CLK_TCK) };
401
+
402
+ uptime = std::stol (tmp) / hertz;
403
+ }
404
+
405
+ return uptime;
406
+ }
0 commit comments