-
Notifications
You must be signed in to change notification settings - Fork 1
/
CPUWidget.cpp
73 lines (61 loc) · 1.59 KB
/
CPUWidget.cpp
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include "CPUWidget.h"
#include "NTreeReader.h"
CPUWidget::CPUWidget(QLabel* l) : lastjiffies(0), lasttotal(0)
{
label = l;
}
void CPUWidget::Update()
{
if (GetFile("/proc/stat") == "/proc/stat")
DoUpdate();
}
void CPUWidget::DoUpdate()
{
NTreeReader read(GetFile("/proc/stat"), false);
size_t val;
size_t user, nice, system, idle, io, irq, sirq, total;
user = nice = system = idle = io = irq = sirq = total = 0;
read.Read(user, "cpu", 0);
read.Read(nice, "cpu", 1);
read.Read(system, "cpu", 2);
read.Read(idle, "cpu", 3);
read.Read(io, "cpu", 4);
read.Read(irq, "cpu", 5);
read.Read(sirq, "cpu", 6);
total = user + nice + system + idle + io + irq + sirq;
if (stat == User)
val = user;
else if (stat == Nice)
val = nice;
else if (stat == System)
val = system;
else if (stat == IO)
val = io;
else if (stat == Idle)
val = idle;
else if (stat == Busy)
val = user + nice + system + io + irq + sirq;
if (total == lasttotal) // Most likely means we failed to read the file
total = lasttotal + 1; // Avoid div by zero
size_t result = float(val - lastjiffies) / float(total - lasttotal) * 100.f;
lastjiffies = val;
lasttotal = total;
if (type == Text)
{
QString text = format;
text.replace("%s", QString::number(result));
SetText(text);
}
else if (type == Image)
{
SetLabelMask(float(result) / 100.f);
}
else if (type == Graph)
{
DrawGraph(float(result) / 100.f);
}
}
void CPUWidget::ProcessFinished(QString)
{
DoUpdate();
}