-
Notifications
You must be signed in to change notification settings - Fork 1
/
DiskWidget.cpp
102 lines (87 loc) · 2.6 KB
/
DiskWidget.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
91
92
93
94
95
96
97
98
99
100
101
#include "DiskWidget.h"
#include "NTreeReader.h"
#include <sys/statvfs.h>
#include <iostream>
using std::cout;
using std::endl;
DiskWidget::DiskWidget(QLabel* l) : max(100), sectorsize(512), lastread(0), lastwrite(0)
{
label = l;
}
void DiskWidget::Update()
{
DoUpdate(true);
}
void DiskWidget::DoUpdate(bool periodic)
{
struct statvfs stats;
// The following calculations easily overflow 32-bit ints
unsigned long long total, free, used, read, write;
total = free = used = read = write = 0;
int elapsed;
float localmax;
// These stats only work locally, but you can always mount a remote filesystem to monitor it
if (stat == Total || stat == Free || stat == Used)
{
statvfs(path.c_str(), &stats);
// Theoretically someday it could be possible to overflow even a long long, so reduce
// the numbers by a couple orders of magnitude and use KB instead of bytes
total = stats.f_blocks / KB() * stats.f_frsize;
free = stats.f_bavail / KB() * stats.f_frsize;
used = (stats.f_blocks - stats.f_bfree) / KB() * stats.f_frsize;
localmax = total;
}
else
{
string f = GetFile("/proc/diskstats", periodic);
if ((f != "/proc/diskstats" && periodic) || f == "/dev/null")
return;
NTreeReader reader(f, 2);
reader.Read(read, path, 2);
reader.Read(write, path, 6);
elapsed = timer.restart();
// Convert to kilobytes for consistency with other stats - the numbers we read above are
// number of sectors read/written from/to the disk, so need to account for sector size
read /= (1024.f / sectorsize);
write /= (1024.f / sectorsize);
localmax = max * unit / KB();
}
unsigned long long val;
float percent;
if (stat == Total)
val = total;
else if (stat == Free)
val = free;
else if (stat == Used)
val = used;
else if (stat == Read)
val = read - lastread;
else if (stat == Write)
val = write - lastwrite;
else if (stat == ReadWrite)
val = (read + write) - (lastread + lastwrite);
if (stat == Read || stat == Write || stat == ReadWrite)
val /= (float(elapsed) / 1000.f);
percent = float(val) / localmax;
val /= unit / KB();
lastread = read;
lastwrite = write;
if (type == Text)
{
QString text = format;
text = text.replace("%s", QString::number(val));
SetText(text);
}
else if (type == Image)
{
SetLabelMask(percent);
}
else if (type == Graph)
{
DrawGraph(percent);
}
}
void DiskWidget::ProcessFinished(QString)
{
DoUpdate(false);
}