@@ -82,8 +82,9 @@ float LinuxParser::MemoryUtilization() {
82
82
if (key == " MemTotal:" ){
83
83
mem_total = std::stol (value);
84
84
}
85
- if (key == " MemFree:" ){
86
- mem_free = std::stol (value);
85
+ if (key == " MemFree:" ){
86
+ if (value !=" " ) mem_free = std::stol (value);
87
+
87
88
}
88
89
}
89
90
}
@@ -104,19 +105,54 @@ long LinuxParser::UpTime() {
104
105
std::getline (stream, line);
105
106
std::istringstream linestream (line);
106
107
linestream >> uptime_s;
107
- uptime_l = std::stol (uptime_s);
108
+ if (uptime_s!= " " ) uptime_l = std::stol (uptime_s);
108
109
}
109
110
110
111
return uptime_l;
111
112
}
113
+ vector<string> split (const string& str, const string& delim)
114
+ {
115
+ vector<string> tokens;
116
+ size_t prev = 0 , pos = 0 ;
117
+ do
118
+ {
119
+ pos = str.find (delim, prev);
120
+ if (pos == string::npos) pos = str.length ();
121
+ string token = str.substr (prev, pos-prev);
122
+ if (!token.empty ()) tokens.push_back (token);
123
+ prev = pos + delim.length ();
124
+ }
125
+ while (pos < str.length () && prev < str.length ());
126
+ return tokens;
127
+ }
112
128
113
129
// Read and return the number of jiffies for the system
114
130
long LinuxParser::Jiffies () {
115
131
return ActiveJiffies () + IdleJiffies ();
116
132
}
117
133
118
134
// Read and return the number of active jiffies for a PID
119
- long LinuxParser::ActiveJiffies (int pid[[maybe_unused]]) { return 0 ; }
135
+ long LinuxParser::ActiveJiffies (int pid) {
136
+
137
+ vector<string> splited_words;
138
+ string line;
139
+ long utime = 0 , stime = 0 , cutime = 0 , cstime = 0 , active_jiffies=0 ;
140
+
141
+
142
+ std::ifstream stream (kProcDirectory + " /" + std::to_string (pid) + kStatFilename );
143
+ if (stream.is_open ()) {
144
+ std::getline (stream, line);
145
+ splited_words = split (line, " " );
146
+
147
+ if (splited_words[13 ]!=" " ) utime = std::stol (splited_words[13 ]);
148
+ if (splited_words[14 ]!=" " ) stime = std::stol (splited_words[14 ]);
149
+ if (splited_words[15 ]!=" " ) cutime = std::stol (splited_words[15 ]);
150
+ if (splited_words[16 ]!=" " ) cstime = std::stol (splited_words[16 ]);
151
+
152
+ active_jiffies = utime + stime + cutime + cstime ;
153
+ }
154
+ return active_jiffies;
155
+ }
120
156
121
157
// Read and return the number of active jiffies for the system
122
158
long LinuxParser::ActiveJiffies () {
@@ -130,7 +166,7 @@ long LinuxParser::ActiveJiffies() {
130
166
linestream >> cpu >> user >> nice >> system >> idle >> iowait >> irq \
131
167
>> softirq >> steal >> guest >> guest_nice;
132
168
133
- active_jiffies = user + nice + system + irq + softirq + steal;
169
+ active_jiffies = user + nice + system + irq + softirq + steal + guest + guest_nice ;
134
170
}
135
171
return active_jiffies;
136
172
}
@@ -153,7 +189,7 @@ long LinuxParser::IdleJiffies() {
153
189
154
190
// Read and return CPU utilization
155
191
vector<string> LinuxParser::CpuUtilization () {
156
- string line, cpu, cpu_time ;
192
+ string line, cpu;
157
193
vector<string> cpu_utilizations;
158
194
std::ifstream filestream (kProcDirectory + kStatFilename );
159
195
@@ -162,28 +198,14 @@ vector<string> LinuxParser::CpuUtilization() {
162
198
std::istringstream linestream (line);
163
199
linestream >> cpu;
164
200
165
- while (linestream >> cpu_time ) {
166
- cpu_utilizations.emplace_back (cpu_time );
201
+ while (linestream >> cpu ) {
202
+ cpu_utilizations.emplace_back (cpu );
167
203
}
168
204
}
169
205
return cpu_utilizations;
170
206
}
171
207
172
- vector<string> split (const string& str, const string& delim)
173
- {
174
- vector<string> tokens;
175
- size_t prev = 0 , pos = 0 ;
176
- do
177
- {
178
- pos = str.find (delim, prev);
179
- if (pos == string::npos) pos = str.length ();
180
- string token = str.substr (prev, pos-prev);
181
- if (!token.empty ()) tokens.push_back (token);
182
- prev = pos + delim.length ();
183
- }
184
- while (pos < str.length () && prev < str.length ());
185
- return tokens;
186
- }
208
+
187
209
188
210
float LinuxParser::CpuUtilization (int pid){
189
211
float cpu_utilization = 0 .0f ;
@@ -196,7 +218,7 @@ float LinuxParser::CpuUtilization(int pid){
196
218
if (uptime_stream.is_open ()) {
197
219
std::getline (uptime_stream, line);
198
220
splited_words = split (line, " " );
199
- uptime = std::stol (splited_words[0 ]);
221
+ if (splited_words[ 0 ]!= " " ) uptime = std::stol (splited_words[0 ]);
200
222
}
201
223
202
224
@@ -205,17 +227,17 @@ float LinuxParser::CpuUtilization(int pid){
205
227
std::getline (stream, line);
206
228
splited_words = split (line, " " );
207
229
208
- utime = std::stol (splited_words[13 ]);
209
- stime = std::stol (splited_words[14 ]);
210
- cutime = std::stol (splited_words[15 ]);
211
- cstime = std::stol (splited_words[16 ]);
212
- starttime = std::stol (splited_words[21 ]);
230
+ if (splited_words[ 13 ]!= " " ) utime = std::stol (splited_words[13 ]);
231
+ if (splited_words[ 14 ]!= " " ) stime = std::stol (splited_words[14 ]);
232
+ if (splited_words[ 15 ]!= " " ) cutime = std::stol (splited_words[15 ]);
233
+ if (splited_words[ 16 ]!= " " ) cstime = std::stol (splited_words[16 ]);
234
+ if (splited_words[ 21 ]!= " " ) starttime = std::stol (splited_words[21 ]);
213
235
214
236
long total_time = utime + stime + cutime + cstime ;
215
237
long seconds = uptime - (starttime/hertz);
216
238
cpu_utilization = 1 .0f * (total_time/hertz) / seconds;
217
239
218
- if (cpu_utilization >100.0 ) cpu_utilization = 0.0 ;
240
+ // if (cpu_utilization >100.0) cpu_utilization = 0.0;
219
241
220
242
}
221
243
@@ -232,7 +254,7 @@ int LinuxParser::TotalProcesses() {
232
254
std::istringstream linestream (line);
233
255
while (linestream >> key >> value) {
234
256
if (key == " processes" ){
235
- total_process = std::stoi (value);
257
+ if (value!= " " ) total_process = std::stoi (value);
236
258
break ;
237
259
}
238
260
}
@@ -252,7 +274,7 @@ int LinuxParser::RunningProcesses() {
252
274
std::istringstream linestream (line);
253
275
while (linestream >> key >> value) {
254
276
if (key == " procs_running" ){
255
- running_processes = std::stoi (value);
277
+ if (value!= " " ) running_processes = std::stoi (value);
256
278
break ;
257
279
}
258
280
}
@@ -289,7 +311,7 @@ string LinuxParser::Ram(int pid) {
289
311
}
290
312
}
291
313
}
292
- ram = std::to_string (std::stol (ram)/1024 );
314
+ if (ram!= " " ) ram = std::to_string (std::stol (ram)/1024 );
293
315
294
316
return ram;
295
317
}
@@ -338,13 +360,15 @@ long LinuxParser::UpTime(int pid) {
338
360
if (stream.is_open ()) {
339
361
std::getline (stream, line);
340
362
std::istringstream linestream (line);
363
+ // linestream >> uptime;
341
364
using StrIt = std::istream_iterator<std::string>;
342
365
std::vector<std::string> container{StrIt{linestream}, StrIt{}};
343
366
if (container.size () >= 22 ) {
344
367
uptime_s= container.at (21 );
345
368
}
346
369
}
347
-
348
- uptime = std::stoi (uptime_s) / sysconf (_SC_CLK_TCK);
370
+ if (uptime_s !=" " ) {
371
+ uptime = UpTime ()- std::stol (uptime_s) / sysconf (_SC_CLK_TCK);
372
+ }
349
373
return uptime;
350
374
}
0 commit comments