-
Notifications
You must be signed in to change notification settings - Fork 1
/
MemoryWidget.cpp
91 lines (81 loc) · 1.86 KB
/
MemoryWidget.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include "MemoryWidget.h"
#include "NTreeReader.h"
#include <iostream>
#include <sys/sysinfo.h>
using std::cout;
using std::endl;
MemoryWidget::MemoryWidget(QLabel* l) : stat(Free)
{
label = l;
}
void MemoryWidget::Update()
{
if (GetFile("/proc/meminfo") == "/proc/meminfo")
DoUpdate();
}
void MemoryWidget::DoUpdate()
{
// Cool, /proc/meminfo is parsable by NTreeReader
NTreeReader read(GetFile("/proc/meminfo", false));
size_t val;
size_t total, free, buffer, cache, totalswap, freeswap;
total = totalswap = 1;
free = buffer = cache = freeswap = 0;
float percent;
read.Read(total, "MemTotal:");
read.Read(free, "MemFree:");
read.Read(buffer, "Buffers:");
read.Read(cache, "Cached:");
read.Read(totalswap, "SwapTotal:");
read.Read(freeswap, "SwapFree:");
if (stat == Total)
{
val = total;
percent = 100.f;
}
else if (stat == Free)
{
val = free + buffer + cache;
percent = float(val) / float(total);
}
else if (stat == Used)
{
val = total - (free + buffer + cache);
percent = float(val) / float(total);
}
else if (stat == TotalSwap)
{
val = totalswap;
percent = 100.f;
}
else if (stat == FreeSwap)
{
val = freeswap;
percent = float(val) / float(totalswap);
}
else if (stat == UsedSwap)
{
val = totalswap - freeswap;
percent = float(val) / float(totalswap);
}
// /proc/meminfo gives values in KB, for the sake of converting it's easier to have it in bytes
val = val * KB() / unit;
if (type == Text)
{
QString text = format;
text.replace("%s", QString::number(val));
SetText(text);
}
else if (type == Image)
{
SetLabelMask(percent);
}
else if (type == Graph)
{
DrawGraph(percent);
}
}
void MemoryWidget::ProcessFinished(QString)
{
DoUpdate();
}