#include "cpuload.h" #include 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& CpuLoad::getBars() const { static QList list; return list; } qint64 CpuLoad::getMaximum() { return 1000; }