Skip to content

Commit f0eb5d1

Browse files
authored
Add files via upload
1 parent a2a3ebd commit f0eb5d1

File tree

1 file changed

+59
-35
lines changed

1 file changed

+59
-35
lines changed

src/linux_parser.cpp

+59-35
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,9 @@ float LinuxParser::MemoryUtilization() {
8282
if (key == "MemTotal:"){
8383
mem_total = std::stol(value);
8484
}
85-
if (key == "MemFree:"){
86-
mem_free = std::stol(value);
85+
if (key == "MemFree:"){
86+
if(value !="") mem_free = std::stol(value);
87+
8788
}
8889
}
8990
}
@@ -104,19 +105,54 @@ long LinuxParser::UpTime() {
104105
std::getline(stream, line);
105106
std::istringstream linestream(line);
106107
linestream >> uptime_s;
107-
uptime_l = std::stol(uptime_s);
108+
if(uptime_s!="") uptime_l = std::stol(uptime_s);
108109
}
109110

110111
return uptime_l;
111112
}
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+
}
112128

113129
// Read and return the number of jiffies for the system
114130
long LinuxParser::Jiffies() {
115131
return ActiveJiffies() + IdleJiffies();
116132
}
117133

118134
// 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+
}
120156

121157
// Read and return the number of active jiffies for the system
122158
long LinuxParser::ActiveJiffies() {
@@ -130,7 +166,7 @@ long LinuxParser::ActiveJiffies() {
130166
linestream >> cpu >> user >> nice >> system >> idle >> iowait >> irq \
131167
>> softirq >> steal >> guest >> guest_nice;
132168

133-
active_jiffies = user + nice + system + irq + softirq + steal;
169+
active_jiffies = user + nice + system + irq + softirq + steal + guest + guest_nice;
134170
}
135171
return active_jiffies;
136172
}
@@ -153,7 +189,7 @@ long LinuxParser::IdleJiffies() {
153189

154190
// Read and return CPU utilization
155191
vector<string> LinuxParser::CpuUtilization() {
156-
string line, cpu, cpu_time;
192+
string line, cpu;
157193
vector<string> cpu_utilizations;
158194
std::ifstream filestream(kProcDirectory + kStatFilename);
159195

@@ -162,28 +198,14 @@ vector<string> LinuxParser::CpuUtilization() {
162198
std::istringstream linestream(line);
163199
linestream >> cpu;
164200

165-
while (linestream >> cpu_time) {
166-
cpu_utilizations.emplace_back(cpu_time);
201+
while (linestream >> cpu) {
202+
cpu_utilizations.emplace_back(cpu);
167203
}
168204
}
169205
return cpu_utilizations;
170206
}
171207

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+
187209

188210
float LinuxParser::CpuUtilization(int pid){
189211
float cpu_utilization = 0.0f;
@@ -196,7 +218,7 @@ float LinuxParser::CpuUtilization(int pid){
196218
if (uptime_stream.is_open()) {
197219
std::getline(uptime_stream, line);
198220
splited_words = split(line, " ");
199-
uptime = std::stol(splited_words[0]);
221+
if(splited_words[0]!="") uptime = std::stol(splited_words[0]);
200222
}
201223

202224

@@ -205,17 +227,17 @@ float LinuxParser::CpuUtilization(int pid){
205227
std::getline(stream, line);
206228
splited_words = split(line, " ");
207229

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]);
213235

214236
long total_time = utime + stime + cutime + cstime ;
215237
long seconds = uptime - (starttime/hertz);
216238
cpu_utilization = 1.0f * (total_time/hertz) / seconds;
217239

218-
if (cpu_utilization >100.0) cpu_utilization = 0.0;
240+
//if (cpu_utilization >100.0) cpu_utilization = 0.0;
219241

220242
}
221243

@@ -232,7 +254,7 @@ int LinuxParser::TotalProcesses() {
232254
std::istringstream linestream(line);
233255
while (linestream >> key >> value) {
234256
if (key == "processes"){
235-
total_process = std::stoi(value);
257+
if(value!="") total_process = std::stoi(value);
236258
break;
237259
}
238260
}
@@ -252,7 +274,7 @@ int LinuxParser::RunningProcesses() {
252274
std::istringstream linestream(line);
253275
while (linestream >> key >> value) {
254276
if (key == "procs_running"){
255-
running_processes = std::stoi(value);
277+
if(value!="") running_processes = std::stoi(value);
256278
break;
257279
}
258280
}
@@ -289,7 +311,7 @@ string LinuxParser::Ram(int pid) {
289311
}
290312
}
291313
}
292-
ram = std::to_string(std::stol(ram)/1024);
314+
if(ram!="") ram = std::to_string(std::stol(ram)/1024);
293315

294316
return ram;
295317
}
@@ -338,13 +360,15 @@ long LinuxParser::UpTime(int pid) {
338360
if (stream.is_open()) {
339361
std::getline(stream, line);
340362
std::istringstream linestream(line);
363+
//linestream >> uptime;
341364
using StrIt = std::istream_iterator<std::string>;
342365
std::vector<std::string> container{StrIt{linestream}, StrIt{}};
343366
if (container.size() >= 22) {
344367
uptime_s= container.at(21);
345368
}
346369
}
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+
}
349373
return uptime;
350374
}

0 commit comments

Comments
 (0)