-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcpustats.cpp
More file actions
138 lines (91 loc) · 3.01 KB
/
cpustats.cpp
File metadata and controls
138 lines (91 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include "cpustats.h"
std::vector<CPUStats::CPUData> CPUStats::m_CurrentEntries;
std::vector<CPUStats::CPUData> CPUStats::m_PreviousEntries;
std::vector<std::pair<std::string, float> > CPUStats::get_cpus_activity()
{
std::vector<std::pair<std::string, float> > retvec;
std::vector<float> current = get_cpus_activity_vector();
if(!m_PreviousEntries.empty()){
for(size_t i = 0; i < m_CurrentEntries.size(); ++i)
{
retvec.emplace_back(std::pair<std::string, float>(m_CurrentEntries[i].cpu,current[i]));
}
}
return retvec;
}
std::vector<float> CPUStats::get_cpus_activity_vector()
{
static bool isFirstCall=true;
if(isFirstCall){
ReadStatsCPU(m_CurrentEntries);
ReadStatsCPU(m_PreviousEntries);
isFirstCall=false;
}
std::vector<float> retvec;
ReadStatsCPU(m_CurrentEntries);
if(!m_PreviousEntries.empty()){
for(size_t i = 0; i < m_CurrentEntries.size(); ++i)
{
const CPUData & e1 = m_PreviousEntries[i];
const CPUData & e2 = m_CurrentEntries[i];
auto ACTIVE_TIME = static_cast<float>(GetActiveTime(e2) - GetActiveTime(e1));
auto IDLE_TIME = static_cast<float>(GetIdleTime(e2) - GetIdleTime(e1));
auto TOTAL_TIME = ACTIVE_TIME + IDLE_TIME;
retvec.emplace_back((100.f * ACTIVE_TIME / TOTAL_TIME));
}
}
m_PreviousEntries=m_CurrentEntries;
return retvec;
}
void CPUStats::ReadStatsCPU(std::vector<CPUData> & entries)
{
entries.clear();
std::ifstream fileStat("/proc/stat");
std::string line;
const std::string STR_CPU("cpu");
const std::size_t LEN_STR_CPU = STR_CPU.size();
const std::string STR_TOT("tot");
while(std::getline(fileStat, line))
{
// cpu stats line found
if(!line.compare(0, LEN_STR_CPU, STR_CPU))
{
std::istringstream ss(line);
// store entry
entries.emplace_back(CPUData());
CPUData & entry = entries.back();
// read cpu label
ss >> entry.cpu;
// remove "cpu" from the label when it's a processor number
if(entry.cpu.size() > LEN_STR_CPU)
entry.cpu.erase(0, LEN_STR_CPU);
// replace "cpu" with "tot" when it's total values
else
entry.cpu = STR_TOT;
// read times
for(int i = 0; i < NUM_CPU_STATES; ++i)
ss >> entry.times[i];
}
}
}
size_t CPUStats::GetIdleTime(const CPUData & e)
{
return e.times[S_IDLE] +
e.times[S_IOWAIT];
}
size_t CPUStats::GetActiveTime(const CPUData & e)
{
return e.times[S_USER] +
e.times[S_NICE] +
e.times[S_SYSTEM] +
e.times[S_IRQ] +
e.times[S_SOFTIRQ] +
e.times[S_STEAL] +
e.times[S_GUEST] +
e.times[S_GUEST_NICE];
}
size_t CPUStats::get_cpu_count()
{
ReadStatsCPU(m_CurrentEntries);
return m_CurrentEntries.size()-1;
}