summaryrefslogtreecommitdiffstats
path: root/src/datasource
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
downloadspeedcheck-d4f10f9fe446da2aff65088e5236d6ffcdd034b5.tar.gz
speedcheck-d4f10f9fe446da2aff65088e5236d6ffcdd034b5.tar.xz
speedcheck-d4f10f9fe446da2aff65088e5236d6ffcdd034b5.zip
initial commit
Diffstat (limited to 'src/datasource')
-rw-r--r--src/datasource/cpuload.cpp55
-rw-r--r--src/datasource/cpuload.h22
-rw-r--r--src/datasource/idatasource.h16
-rw-r--r--src/datasource/networkspeed.cpp50
-rw-r--r--src/datasource/networkspeed.h24
5 files changed, 167 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;
+}
diff --git a/src/datasource/cpuload.h b/src/datasource/cpuload.h
new file mode 100644
index 0000000..2a5a2cb
--- /dev/null
+++ b/src/datasource/cpuload.h
@@ -0,0 +1,22 @@
+#ifndef CPULOAD_H_
+#define CPULOAD_H_
+
+#include "idatasource.h"
+#include <QFile>
+
+class CpuLoad : public IDataSource {
+public:
+ CpuLoad();
+ virtual ~CpuLoad();
+
+ virtual qint64 read();
+ virtual const QList<qint64>& getBars() const;
+ virtual qint64 getMaximum();
+
+private:
+ qint64 _lastLoad;
+ qint64 _lastTotal;
+ QFile _file;
+};
+
+#endif /* CPULOAD_H_ */
diff --git a/src/datasource/idatasource.h b/src/datasource/idatasource.h
new file mode 100644
index 0000000..4639d57
--- /dev/null
+++ b/src/datasource/idatasource.h
@@ -0,0 +1,16 @@
+#ifndef IDATASOURCE_H_
+#define IDATASOURCE_H_
+
+#include <QList>
+
+class IDataSource {
+public:
+ IDataSource() {};
+ virtual ~IDataSource() {};
+
+ virtual qint64 read() = 0;
+ virtual const QList<qint64>& getBars() const = 0;
+ virtual qint64 getMaximum() = 0;
+};
+
+#endif /* IDATASOURCE_H_ */
diff --git a/src/datasource/networkspeed.cpp b/src/datasource/networkspeed.cpp
new file mode 100644
index 0000000..8888fd4
--- /dev/null
+++ b/src/datasource/networkspeed.cpp
@@ -0,0 +1,50 @@
+#include "networkspeed.h"
+
+NetworkSpeed::NetworkSpeed() :
+ _lastBytes(0),
+ _lastMs(0),
+ _file("/sys/class/net/eth0/statistics/rx_bytes")
+{
+ _timer.start();
+}
+
+NetworkSpeed::~NetworkSpeed()
+{
+ // TODO Auto-generated destructor stub
+}
+
+qint64 NetworkSpeed::read()
+{
+ char buffer[500];
+ qint64 ret, now;
+ if (!_file.open(QIODevice::ReadOnly)) {
+ return -1;
+ }
+ now = _timer.elapsed();
+ _file.readLine(buffer, sizeof(buffer));
+ _file.close();
+ QString line(buffer);
+ const qint64 counter = (qint64)line.toLongLong();
+ if (_lastBytes == 0) {
+ ret = 0;
+ } else {
+ ret = counter - _lastBytes;
+ }
+ if (_lastMs < now) {
+ ret = (ret * 1000) / (now - _lastMs);
+ }
+ _lastMs = now;
+ _lastBytes = counter;
+ return ret;
+}
+
+const QList<qint64>& NetworkSpeed::getBars() const
+{
+ static QList<qint64> list = QList<qint64>() << ((qint64)1024 * 1024 * 10);
+ return list;
+}
+
+qint64 NetworkSpeed::getMaximum()
+{
+ return ((qint64)1024 * 1024 * 100);
+}
diff --git a/src/datasource/networkspeed.h b/src/datasource/networkspeed.h
new file mode 100644
index 0000000..482f4cd
--- /dev/null
+++ b/src/datasource/networkspeed.h
@@ -0,0 +1,24 @@
+#ifndef NETWORKSPEED_H_
+#define NETWORKSPEED_H_
+
+#include "idatasource.h"
+#include <QElapsedTimer>
+#include <QFile>
+
+class NetworkSpeed : public IDataSource {
+public:
+ NetworkSpeed();
+ virtual ~NetworkSpeed();
+
+ virtual qint64 read();
+ virtual const QList<qint64>& getBars() const;
+ virtual qint64 getMaximum();
+
+private:
+ qint64 _lastBytes;
+ qint64 _lastMs;
+ QElapsedTimer _timer;
+ QFile _file;
+};
+
+#endif /* NETWORKSPEED_H_ */