summaryrefslogtreecommitdiffstats
path: root/src/datasource/cpuload.cpp
diff options
context:
space:
mode:
authorSimon Rettberg2017-02-24 12:33:55 +0100
committerSimon Rettberg2017-02-24 12:33:55 +0100
commitd4f10f9fe446da2aff65088e5236d6ffcdd034b5 (patch)
tree5d7467a6f67272064619c000a14800e706368ac6 /src/datasource/cpuload.cpp
downloadspeedcheck-d4f10f9fe446da2aff65088e5236d6ffcdd034b5.tar.gz
speedcheck-d4f10f9fe446da2aff65088e5236d6ffcdd034b5.tar.xz
speedcheck-d4f10f9fe446da2aff65088e5236d6ffcdd034b5.zip
initial commit
Diffstat (limited to 'src/datasource/cpuload.cpp')
-rw-r--r--src/datasource/cpuload.cpp55
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;
+}