-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpustatreader.cpp
140 lines (89 loc) · 4.07 KB
/
cpustatreader.cpp
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
139
140
#include "cpustatreader.h"
#include "StatReader.h"
#include <QThread>
#include <QFile>
#include <QTextStream>
CPUStatReader::CPUStatReader(): StatReader("CPUStatReader") {
CPUStats::get_cpus_activity();//dummy read to populate
CPUStats::get_cpus_activity();//dummy read to populate
auto currentStat= CPUStats::get_cpus_activity();
for(auto& cStat : currentStat){
StatTypes::CPUData record;
record.CPUName = cStat.first; //intialise names
record.currentActivityData.resize(m_dataPointsPerMinute,0);
record.currentClockSpeed.resize(m_dataPointsPerMinute,0);
m_DataVec.append(std::move(record));
}
QFile f("/proc/cpuinfo");
f.open(QIODevice::ReadOnly | QIODevice::Text);
if(f.isOpen()){
QTextStream in(&f);
QString line = in.readLine();
while(!line.isNull()){
if(line.startsWith("model")){
auto stringlist = line.split("\t: ");
model = stringlist.at(1); //* 9.53674E-7;//9.3132257461548E-7;
//auto stringlistBase = model.split(" @ ");
}
if(line.startsWith("cache")){
auto stringlist = line.split("\t: ");
cacheSize = stringlist.at(1); //* 9.53674E-7;//9.3132257461548E-7;
}
if(line.startsWith("siblings")){
auto stringlist = line.split("\t: ");
CPUCoresLogical = stringlist.at(1); //* 9.53674E-7;//9.3132257461548E-7;
}
if(line.startsWith("cpu cores")){
auto stringlist = line.split("\t: ");
CPUCoresPhys = stringlist.at(1); //* 9.53674E-7;//9.3132257461548E-7;
break;
}
line = in.readLine();
}
}
}
void CPUStatReader::measure_main_loop()
{
while(!m_quit){
auto currentCPU = CPUStats::get_cpus_activity_vector();
{//enterign data race critical section
std::lock_guard<std::mutex> lock(m_DataVecMutex);
for(size_t i=0; i<m_DataVec.size(); i++){
m_DataVec[i].currentActivityData.pop_front();
m_DataVec[i].currentActivityData.push_back(currentCPU[i]);
//filtering first order
//for(int k = +1; k < cpuYPlotData[i].size()-1; k++)
//int k = cpuYPlotData[i].size()-2;
// cpuYPlotData[i][k] = (cpuYPlotData[i][k-1]+ cpuYPlotData[i][k] +cpuYPlotData[i][k+1] )/ 3.0;
//filtering 2nd order to power n
constexpr int filterIterations = 8;
size_t k = m_DataVec[i].currentActivityData.size()-3;
for(int n=filterIterations; n>=0; n--){
//for(int k = +2; k < cpuYPlotData[i].size()-2; k++)
m_DataVec[i].currentActivityData[k] = (m_DataVec[i].currentActivityData[k-2] + m_DataVec[i].currentActivityData[k-1] + m_DataVec[i].currentActivityData[k] +m_DataVec[i].currentActivityData[k+1] +m_DataVec[i].currentActivityData[k+2] )/ 5.0;
k--;
}
if(i==0){//only measure average over all cores
QFile f("/proc/cpuinfo");
f.open(QIODevice::ReadOnly | QIODevice::Text);
if(f.isOpen()){
QTextStream in(&f);
QString line = in.readLine();
while(!line.isNull()){
if(line.startsWith("cpu MHz")){
auto stringlist = line.split("\t: ");
m_DataVec[0].currentClockSpeed.pop_front();
m_DataVec[0].currentClockSpeed.push_back(stringlist.at(1).toDouble());
filter_data_deque( m_DataVec[0].currentClockSpeed);
break;
}
line = in.readLine();
}
}
}
}
}
emit data_ready();
QThread::msleep(m_cycleTimeMs);
}
}