1
1
#include " processor.h"
2
+ #include " linux_parser.h"
3
+ #include < thread>
4
+ #include < chrono>
5
+ #include < iostream>
2
6
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