Files
Stacer/stacer-core/Info/cpu_info.cpp
Oğuzhan İnan 82247ac648 stacer native
2017-08-23 14:49:45 +03:00

97 lines
2.5 KiB
C++

#include "cpu_info.h"
CpuInfo::CpuInfo()
{ }
quint8 CpuInfo::getCpuCoreCount()
{
static quint8 count = 0;
if (! count)
{
QStringList cpuinfo = FileUtil::readListFromFile(PROC_CPUINFO);
if (! cpuinfo.isEmpty())
count = cpuinfo.filter(QRegExp("^processor")).count();
}
return count;
}
QList<int> CpuInfo::getCpuPercents()
{
QList<double> cpuTimes;
QList<int> cpuPercents;
QStringList times = FileUtil::readListFromFile(PROC_STAT);
if (! times.isEmpty())
{
/* user nice system idle iowait irq softirq steal guest guest_nice
cpu 4705 356 584 3699 23 23 0 0 0 0
.
cpuN 4705 356 584 3699 23 23 0 0 0 0
The meanings of the columns are as follows, from left to right:
- user: normal processes executing in user mode
- nice: niced processes executing in user mode
- system: processes executing in kernel mode
- idle: twiddling thumbs
- iowait: waiting for I/O to complete
- irq: servicing interrupts
- softirq: servicing softirqs
- steal: involuntary wait
- guest: running a normal guest
- guest_nice: running a niced guest
*/
for (int i = 0; i < CpuInfo::getCpuCoreCount()+1; ++i)
{
QStringList n_times = times.at(i).split(QRegExp("\\s+"));
n_times.removeFirst();
foreach (QString t, n_times)
cpuTimes << t.toDouble();
cpuPercents << getCpuPercent(cpuTimes, i);
cpuTimes.clear();
}
}
return cpuPercents;
}
int CpuInfo::getCpuPercent(QList<double>cpuTimes, int processor)
{
const int N = getCpuCoreCount()+1;
static QVector<double> l_idles(N);
static QVector<double> l_totals(N);
double idle, total,
idle_delta, total_delta;
int utilisation = 0;
if (cpuTimes.count() > 0) {
idle = cpuTimes.at(3) + cpuTimes.at(4); // get (idle + iowait)
foreach (double t, cpuTimes) total += t; // get total time
idle_delta = idle - l_idles[processor];
total_delta = total - l_totals[processor];
if (total_delta)
utilisation = 100 * ((total_delta - idle_delta) / total_delta);
l_idles[processor] = idle;
l_totals[processor] = total;
}
if (utilisation > 100) utilisation = 100;
else if (utilisation < 0) utilisation = 0;
return utilisation;
}