diff options
Diffstat (limited to 'src/datasource/cpuload.cpp')
-rw-r--r-- | src/datasource/cpuload.cpp | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/src/datasource/cpuload.cpp b/src/datasource/cpuload.cpp new file mode 100644 index 0000000..48831f3 --- /dev/null +++ b/src/datasource/cpuload.cpp @@ -0,0 +1,55 @@ +#include "cpuload.h" +#include <QStringList> + +CpuLoad::CpuLoad() : + _lastLoad(0), + _lastTotal(0), + _file("/proc/stat") +{ + // TODO Auto-generated constructor stub + +} + +CpuLoad::~CpuLoad() { + // TODO Auto-generated destructor stub +} + +qint64 CpuLoad::read() +{ + static QChar space(' '); + if (!_file.open(QIODevice::ReadOnly)) { + return -1; + } + char buffer[500]; + _file.readLine(buffer, sizeof(buffer)); + _file.close(); + QString line(buffer); + QStringList list = line.split(space, QString::SkipEmptyParts); + if (list.length() < 8) { + return -1; + } + // "cpu", user, nice, system, idle, iowait, irq, softirq + qint64 load = 0, total = 0; + for (int i = 1; i < 8; ++i) { + const qint64 value = list.at(i).toLongLong(); + if (i < 4 || i > 5) { + load += value; + } + total += value; + } + const qint64 ret = ((load - _lastLoad) * 1000) / (total - _lastTotal); + _lastTotal = total; + _lastLoad = load; + return ret; +} + +const QList<qint64>& CpuLoad::getBars() const +{ + static QList<qint64> list; + return list; +} + +qint64 CpuLoad::getMaximum() +{ + return 1000; +} |