|
3 | 3 | #include <sstream>
|
4 | 4 | #include <string>
|
5 | 5 | #include <vector>
|
| 6 | +#include <iostream> |
6 | 7 |
|
7 | 8 | #include "linux_parser.h"
|
8 | 9 |
|
@@ -195,38 +196,49 @@ float LinuxParser::CpuUtilization(int pid)
|
195 | 196 |
|
196 | 197 | if (filestream.is_open())
|
197 | 198 | std::getline(filestream, line);
|
198 |
| - |
199 |
| - std::istringstream linestream{ line }; |
200 |
| - vector<float> cpu_tokens; |
201 |
| - std::string tmp; |
202 | 199 |
|
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 != "") |
206 | 203 | {
|
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 | + } |
218 | 227 |
|
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;; |
223 | 229 |
|
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); |
228 | 239 |
|
229 |
| - cpu_consumption = total_time / hertz / seconds; |
| 240 | + cpu_consumption = total_time / hertz / seconds; |
| 241 | + } |
230 | 242 |
|
231 | 243 | return cpu_consumption;
|
232 | 244 | }
|
|
0 commit comments