Skip to content

Commit aad1000

Browse files
textanalyticsmantextanalyticsman
textanalyticsman
authored and
textanalyticsman
committed
Fixing bug for process that have died. Improving format
1 parent c1e1224 commit aad1000

File tree

3 files changed

+51
-33
lines changed

3 files changed

+51
-33
lines changed

src/format.cpp

+11-4
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,19 @@ using std::string;
66

77
// INPUT: Long int measuring seconds
88
// OUTPUT: HH:MM:SS
9+
// I have taken the idea from here: https://www.geeksforgeeks.org/converting-seconds-into-days-hours-minutes-and-seconds/
910
string Format::ElapsedTime(long seconds)
1011
{
11-
long int tdays{ seconds/60/60/24 };
12-
long int thours{ seconds/60/60%24 };
13-
long int tminutes{ seconds/60%60 };
14-
long int tseconds{ seconds%60 };
12+
long int tdays{ seconds/(24 * 3600) };
13+
seconds %= (24 * 3600);
14+
15+
long int thours{ seconds/3600 };
16+
seconds %= 3600;
17+
18+
long int tminutes{ seconds/60 };
19+
seconds %= 60;
20+
21+
long int tseconds{ seconds };
1522

1623
string result { std::to_string(tdays) + ":" + std::to_string(thours) + ":" + std::to_string(tminutes) + ":" + std::to_string(tseconds)};
1724

src/linux_parser.cpp

+39-27
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <sstream>
44
#include <string>
55
#include <vector>
6+
#include <iostream>
67

78
#include "linux_parser.h"
89

@@ -195,38 +196,49 @@ float LinuxParser::CpuUtilization(int pid)
195196

196197
if (filestream.is_open())
197198
std::getline(filestream, line);
198-
199-
std::istringstream linestream{ line };
200-
vector<float> cpu_tokens;
201-
std::string tmp;
202199

203-
int i{0};
204-
205-
while (linestream >> tmp)
200+
// Sometimes there are process that dies and then the pseudo file disappears, which causes errors.
201+
// Thus, if no data is detected, the analysis is by passed
202+
if(line != "")
206203
{
207-
++i;
208-
// Positions 14, 15, 16, 17 and 22 are used to calculate CPU consumption per process
209-
// https://stackoverflow.com/questions/16726779/how-do-i-get-the-total-cpu-usage-of-an-application-from-proc-pid-stat/16736599#16736599
210-
if(i == 14 || i == 15 || i == 16 || i == 17)
211-
cpu_tokens.push_back(stof(tmp));
212-
else if(i == 22)
213-
{
214-
cpu_tokens.push_back(stof(tmp));
215-
break;
216-
}
217-
}
204+
std::istringstream linestream{ line };
205+
vector<float> cpu_tokens;
206+
std::string tmp;
207+
208+
int index{0};
209+
210+
while (linestream >> tmp)
211+
{
212+
++index;
213+
// Positions 14, 15, 16, 17 and 22 are used to calculate CPU consumption per process
214+
// https://stackoverflow.com/questions/16726779/how-do-i-get-the-total-cpu-usage-of-an-application-from-proc-pid-stat/16736599#16736599
215+
if(index == 14 || index == 15 || index == 16 || index == 17)
216+
{
217+
cpu_tokens.push_back(stof(tmp));
218+
//std::cout << "PID: "<< pid << " Index: " << index << " Token: " << tmp << " ";
219+
}
220+
else if(index == 22)
221+
{
222+
cpu_tokens.push_back(stof(tmp));
223+
//std::cout << "PID: "<< pid << " Index: " << index << " Token: " << tmp << " ";
224+
break;
225+
}
226+
}
218227

219-
float total_time{0.0};
220-
long int hertz{sysconf(_SC_CLK_TCK)};
221-
float seconds{0.0};
222-
long uptime{UpTime()};
228+
//std::cout << "Vector's size: " << cpu_tokens.size() << std::endl;;
223229

224-
for(int i=0; i<4; ++i)
225-
total_time += cpu_tokens[i];
226-
227-
seconds = uptime - (cpu_tokens[4]/hertz);
230+
float total_time{0.0};
231+
long int hertz{sysconf(_SC_CLK_TCK)};
232+
float seconds{0.0};
233+
long uptime{UpTime()};
234+
235+
for(int i=0; i<4; ++i)
236+
total_time += cpu_tokens[i];
237+
238+
seconds = uptime - (cpu_tokens[4]/hertz);
228239

229-
cpu_consumption = total_time / hertz / seconds;
240+
cpu_consumption = total_time / hertz / seconds;
241+
}
230242

231243
return cpu_consumption;
232244
}

src/system.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ vector<Process>& System::Processes()
2424

2525
for(int pid : list_pids)
2626
{
27-
Process process(pid, LinuxParser::User(pid), LinuxParser::Command(pid),
28-
LinuxParser::CpuUtilization(pid));
27+
Process process(pid, LinuxParser::User(pid), LinuxParser::Command(pid), LinuxParser::CpuUtilization(pid));
2928
processes_.push_back(process);
3029
}
3130

0 commit comments

Comments
 (0)