summaryrefslogtreecommitdiffstats
path: root/src/datasource/cpuload.cpp
blob: 48831f3980116681a17d8c5a24940ac50f1027e7 (plain) (blame)
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
#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;
}