Skip to content

Commit 8483b66

Browse files
textanalyticsmantextanalyticsman
textanalyticsman
authored and
textanalyticsman
committed
Adding CPU consumption monitoring
1 parent adadf98 commit 8483b66

File tree

4 files changed

+140
-7
lines changed

4 files changed

+140
-7
lines changed

include/processor.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
#ifndef PROCESSOR_H
22
#define PROCESSOR_H
33

4+
#include <vector>
5+
#include <string>
6+
47
class Processor {
58
public:
69
float Utilization(); // TODO: See src/processor.cpp
710

8-
// TODO: Declare any necessary private members
911
private:
12+
//This will save the CPU average numbers for two readings
13+
std::vector<std::vector<std::string>> cpu_data;
14+
// This is the delay between readings in seconds used to calculate CPU consumption
15+
unsigned int delay_reading{1000};
1016
};
1117

1218
#endif

src/linux_parser.cpp

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ std::string LinuxParser::RegularExpression(std::string patttern, std::string tex
8585
return result;
8686
}
8787

88-
// TODO: Read and return the system memory utilization
8988
// I need to get the first value from MemTotal: 33263352 kB
9089
// Then I need the second value from MemFree: 23736848 kB
9190
// Last I have to execute (MemTotal-MemFree)/100 to get the ratio
@@ -100,7 +99,7 @@ float LinuxParser::MemoryUtilization()
10099
// Pattern to look for memory information inside /proc/meminfo
101100
std::string pattern{ R"((Mem\w*:)(\s)*(\d+)(\s*)(kB)?)" };
102101

103-
std::ifstream filestream("/proc/meminfo");
102+
std::ifstream filestream(kProcDirectory + kMeminfoFilename);
104103
if (filestream.is_open())
105104
{
106105
while (std::getline(filestream, line))
@@ -139,8 +138,35 @@ long LinuxParser::ActiveJiffies() { return 0; }
139138
// TODO: Read and return the number of idle jiffies for the system
140139
long LinuxParser::IdleJiffies() { return 0; }
141140

142-
// TODO: Read and return CPU utilization
143-
vector<string> LinuxParser::CpuUtilization() { return {}; }
141+
// Read and return CPU utilization
142+
vector<string> LinuxParser::CpuUtilization()
143+
{
144+
std::string line;
145+
std::string pattern{ R"(cpu\s+((?:\d+\s*)+))" };
146+
147+
std::ifstream filestream(kProcDirectory + kStatFilename);
148+
std::string regular_expression_result;
149+
150+
if (filestream.is_open())
151+
{
152+
while (std::getline(filestream, line))
153+
{
154+
regular_expression_result = RegularExpression(pattern, line, 1);
155+
156+
if(regular_expression_result != "")
157+
break;
158+
}
159+
}
160+
161+
std::istringstream linestream{ regular_expression_result };
162+
std::vector<std::string> cpu_tokens;
163+
std::string tmp;
164+
while (linestream >> tmp)
165+
cpu_tokens.push_back(tmp);
166+
167+
return cpu_tokens;
168+
169+
}
144170

145171
// TODO: Read and return the total number of processes
146172
int LinuxParser::TotalProcesses() { return 0; }

src/main.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include<string>
66
#include<linux_parser.h>
77
#include<iostream>
8+
#include<vector>
89

910
int main() {
1011
System system;

src/processor.cpp

Lines changed: 102 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,104 @@
11
#include "processor.h"
2+
#include "linux_parser.h"
3+
#include <thread>
4+
#include <chrono>
5+
#include <iostream>
26

3-
// TODO: Return the aggregate CPU utilization
4-
float Processor::Utilization() { return 0.0; }
7+
// Return the aggregate CPU utilization
8+
float Processor::Utilization()
9+
{
10+
// I have used the algorihm proposed at https://stackoverflow.com/questions/23367857/accurate-calculation-of-cpu-usage-given-in-percentage-in-linux
11+
12+
// First reading to get CPU numbers
13+
Processor::cpu_data.push_back(LinuxParser::CpuUtilization());
14+
// Sleep during some time before executing second reading. Idea taken from https://stackoverflow.com/questions/52268378/c-usleep-returns-immediately-on-linux
15+
std::this_thread::sleep_for(std::chrono::milliseconds(Processor::delay_reading));
16+
// Second reading
17+
Processor::cpu_data.push_back(LinuxParser::CpuUtilization());
18+
19+
float first_idle{ 0.0 };
20+
float first_non_idle{ 0.0 };
21+
float first_total{ 0.0 };
22+
23+
float second_idle{ 0.0 };
24+
float second_non_idle{ 0.0 };
25+
float second_total{ 0.0 };
26+
// These are the indexes for idel and wait quantities
27+
unsigned const int index_idle{3};
28+
unsigned const int index_wait{4};
29+
30+
for(long unsigned int i=0; i<cpu_data[0].size(); ++i)
31+
{
32+
if(i==index_idle || i==index_wait)
33+
{
34+
first_idle += std::stof(cpu_data[0][i]);
35+
second_idle += std::stof(cpu_data[1][i]);
36+
}
37+
else
38+
{
39+
first_non_idle += std::stof(cpu_data[0][i]);
40+
second_non_idle += std::stof(cpu_data[1][i]);
41+
}
42+
}
43+
44+
cpu_data.clear();
45+
46+
first_total = first_idle + first_non_idle;
47+
second_total = second_idle + second_non_idle;
48+
49+
float total_difference {second_total - first_total};
50+
float idle_difference {second_idle - first_idle};
51+
52+
float consumption {(total_difference - idle_difference) / total_difference};
53+
54+
return consumption;
55+
}
56+
57+
/*float Processor::Utilization2()
58+
{
59+
// I have used the algorihm proposed at https://stackoverflow.com/questions/23367857/accurate-calculation-of-cpu-usage-given-in-percentage-in-linux
60+
61+
// First reading to get CPU numbers
62+
Processor::cpu_data.push_back(LinuxParser::CpuUtilization());
63+
64+
float first_idle{ 0 };
65+
float first_non_idle{ 0 };
66+
float first_total{ 0 };
67+
68+
float second_idle{ 0 };
69+
float second_non_idle{ 0 };
70+
float second_total{ 0 };
71+
// These are the indexes for idel and wait quantities
72+
unsigned const int index_idle{3};
73+
unsigned const int index_wait{4};
74+
75+
if(cpu_data.size()==2)
76+
{
77+
for(long unsigned int i=0; i<cpu_data.size(); ++i)
78+
{
79+
if(i==index_idle || i==index_wait)
80+
{
81+
first_idle += std::stof(cpu_data[0][i]);
82+
second_idle += std::stof(cpu_data[1][i]);
83+
}
84+
else
85+
{
86+
first_non_idle += std::stof(cpu_data[0][i]);
87+
second_non_idle += std::stof(cpu_data[1][i]);
88+
}
89+
}
90+
91+
cpu_data.clear();
92+
93+
first_total = first_idle + first_non_idle;
94+
second_total = second_idle + second_non_idle;
95+
96+
float total_difference {second_total - first_total};
97+
float idle_difference {second_idle - first_idle};
98+
99+
float consumption {(total_difference - idle_difference) / total_difference};
100+
}
101+
102+
103+
return consumption;
104+
}*/

0 commit comments

Comments
 (0)