forked from KDE/liquidshell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSysLoad.cxx
299 lines (252 loc) · 9.7 KB
/
SysLoad.cxx
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
Copyright 2017 Martin Koller, [email protected]
This file is part of liquidshell.
liquidshell is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
liquidshell is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with liquidshell. If not, see <http://www.gnu.org/licenses/>.
*/
#include <SysLoad.hxx>
#include <QFile>
#include <QPainter>
#include <QToolTip>
#include <QMouseEvent>
#include <QDebug>
#include <KRun>
#include <KLocalizedString>
#include <NetworkManagerQt/Manager>
//--------------------------------------------------------------------------------
const int INTERVAL_MS = 800;
//--------------------------------------------------------------------------------
SysLoad::SysLoad(QWidget *parent)
: QFrame(parent)
{
setFrameShape(QFrame::StyledPanel);
timeoutTimer.setInterval(INTERVAL_MS);
timeoutTimer.start();
connect(&timeoutTimer, &QTimer::timeout, this, &SysLoad::fetch);
connect(NetworkManager::notifier(), &NetworkManager::Notifier::primaryConnectionChanged,
[this]() { maxBytes = 100; }); // reset since we're changing network (which might be slower)
setFixedWidth(60);
}
//--------------------------------------------------------------------------------
void SysLoad::fetch()
{
QFile f("/proc/stat");
if ( !f.open(QIODevice::ReadOnly) )
return;
int num = 0;
bool first = cpus.isEmpty();
while ( true )
{
QByteArray line = f.readLine();
if ( line.isEmpty() )
break;
if ( line.startsWith("cpu") && !line.startsWith("cpu ") )
{
// time is in 1/100s units https://www.kernel.org/doc/Documentation/filesystems/proc.txt
CpuData data;
sscanf(line.constData(), "%*s %d %d %d", &data.userCPU, &data.niceCPU, &data.systemCPU);
if ( first )
cpus.append(data);
else
{
cpus[num].userPercent = (data.userCPU - cpus[num].userCPU) * 1000.0 / INTERVAL_MS;
cpus[num].nicePercent = (data.niceCPU - cpus[num].niceCPU) * 1000.0 / INTERVAL_MS;
cpus[num].systemPercent = (data.systemCPU - cpus[num].systemCPU) * 1000.0 / INTERVAL_MS;
cpus[num].userCPU = data.userCPU;
cpus[num].niceCPU = data.niceCPU;
cpus[num].systemCPU = data.systemCPU;
num++;
}
}
}
f.close();
// get memory information
size_t memTotal = 0, memFree = 0, swapTotal = 0, swapFree = 0, cached = 0;
f.setFileName("/proc/meminfo");
if ( f.open(QIODevice::ReadOnly) )
{
while ( true )
{
QByteArray line = f.readLine();
if ( line.isEmpty() )
break;
if ( line.startsWith("MemTotal:") ) sscanf(line, "%*s %zd kB", &memTotal);
else if ( line.startsWith("MemFree:") ) sscanf(line, "%*s %zd kB", &memFree);
else if ( line.startsWith("SwapTotal:") ) sscanf(line, "%*s %zd kB", &swapTotal);
else if ( line.startsWith("SwapFree:") ) sscanf(line, "%*s %zd kB", &swapFree);
else if ( line.startsWith("Cached:") ) sscanf(line, "%*s %zd kB", &cached);
}
memData.memPercent = memTotal ? (double(memTotal - memFree) / double(memTotal)) * 100.0 : 0.0;
memData.memCachedPercent = memTotal ? (double(cached) / double(memTotal)) * 100.0 : 0.0;
memData.swapPercent = swapTotal ? (double(swapTotal - swapFree) / double(swapTotal)) * 100.0 : 0.0;
f.close();
}
f.setFileName("/proc/cpuinfo"); // get speed of cores
if ( f.open(QIODevice::ReadOnly) )
{
int num = 0;
while ( true )
{
QByteArray line = f.readLine();
if ( line.isEmpty() )
break;
if ( line.startsWith("cpu MHz") )
{
if ( num < cpus.count() )
sscanf(line, "cpu MHz %*s %lf", &cpus[num++].MHz);
}
}
f.close();
}
f.setFileName("/proc/net/dev");
sumSent = sumReceived = 0;
if ( f.open(QIODevice::ReadOnly) )
{
while ( true )
{
QByteArray line = f.readLine();
if ( line.isEmpty() )
break;
int colon = line.indexOf(':');
if ( colon > 1 )
{
QByteArray device = line.left(colon).trimmed();
size_t received = 0, sent = 0;
sscanf(line.data() + colon + 1, "%zd %*d %*d %*d %*d %*d %*d %*d %zd", &received, &sent);
if ( !netDevs.contains(device) )
{
NetworkData data;
data.prevReceived = received;
data.prevSent = sent;
data.valid = false; // first scan not valid since we count differences
netDevs.insert(device, data);
}
else
{
NetworkData &data = netDevs[device];
data.received = received - data.prevReceived;
data.sent = sent - data.prevSent;
data.prevReceived = received;
data.prevSent = sent;
data.valid = true;
if ( !device.startsWith("tun") ) // TODO: correct ?
{
sumReceived += data.received;
sumSent += data.sent;
}
}
}
}
f.close();
}
update();
QString tip;
for (int i = 0; i < cpus.count(); i++)
{
if ( i ) tip += "<br>";
tip += i18n("CPU %1: %2% (%3 MHz)", i,
locale().toString(std::min(100.0, cpus[i].userPercent + cpus[i].nicePercent + cpus[i].systemPercent), 'f', 1),
static_cast<int>(cpus[i].MHz));
}
tip += "<hr>";
memFree += cached; // show also the cached memory as free (for user applications)
size_t memUsed = memTotal - memFree;
size_t swapUsed = swapTotal - swapFree;
int memUsedPercent = memTotal ? memUsed * 100 / memTotal : 0;
int swapUsedPercent = swapTotal ? swapUsed * 100 / swapTotal : 0;
tip += i18n("Memory Total: %1 MB (%2 GB)" , memTotal / 1024, locale().toString(memTotal / 1024.0 / 1024.0, 'f', 2));
tip += "<br>";
tip += i18n("Memory Used: %1 MB (%2 GB) %3%", memUsed / 1024, locale().toString(memUsed / 1024.0 / 1024.0, 'f', 2), memUsedPercent);
tip += "<br>";
tip += i18n("Memory Free: %1 MB (%2 GB)" , memFree / 1024, locale().toString(memFree / 1024.0 / 1024.0, 'f', 2));
tip += "<hr>";
tip += i18n("Swap Total: %1 MB (%2 GB)" , swapTotal / 1024, locale().toString(swapTotal / 1024.0 / 1024.0, 'f', 2));
tip += "<br>";
tip += i18n("Swap Used: %1 MB (%2 GB) %3%" , swapUsed / 1024, locale().toString(swapUsed / 1024.0 / 1024.0, 'f', 2), swapUsedPercent);
tip += "<br>";
tip += i18n("Swap Free: %1 MB (%2 GB)" , swapFree / 1024, locale().toString(swapFree / 1024.0 / 1024.0, 'f', 2));
tip += "<hr>";
tip += i18n("Net send/receive: %1/%2 KB/sec",
locale().toString((sumSent / 1024.0) / (INTERVAL_MS / 1000.0), 'f', 2),
locale().toString((sumReceived / 1024.0) / (INTERVAL_MS / 1000.0), 'f', 2));
tip += "<br>";
tip += i18n("Net max used: %1 KB/sec", locale().toString((maxBytes / 1024) / (INTERVAL_MS / 1000.0), 'f', 2));
if ( underMouse() )
QToolTip::showText(QCursor::pos(), QLatin1String("<html>") + tip + QLatin1String("</html>"), this, rect());
}
//--------------------------------------------------------------------------------
void SysLoad::paintEvent(QPaintEvent *event)
{
QFrame::paintEvent(event);
const int cpuBars = cpuSummaryBar ? 1 : cpus.count();
int const barWidth = contentsRect().width() / (cpuBars + 2 + 1); // mem usage 2 bars + netSum
int x = contentsRect().x(), y = contentsRect().y() + contentsRect().height();
QPainter painter(this);
// cpu
QVector<CpuData> drawCpus;
if ( !cpuSummaryBar )
drawCpus = cpus;
else
{
CpuData sum;
for (const CpuData &data : cpus)
{
sum.userPercent += data.userPercent;
sum.systemPercent += data.systemPercent;
sum.nicePercent += data.nicePercent;
}
sum.userPercent /= cpus.count();
sum.systemPercent /= cpus.count();
sum.nicePercent /= cpus.count();
drawCpus.append(sum);
}
for (const CpuData &data : drawCpus)
{
int h = contentsRect().height() * (data.userPercent / 100.0);
painter.fillRect(x, y - h, barWidth, h, cpuUserColor);
y -= h;
h = contentsRect().height() * (data.systemPercent / 100.0);
painter.fillRect(x, y - h, barWidth, h, cpuSystemColor);
y -= h;
h = contentsRect().height() * (data.nicePercent / 100.0);
painter.fillRect(x, y - h, barWidth, h, cpuNiceColor);
x += barWidth;
y = contentsRect().y() + contentsRect().height();
}
// memory
int h = contentsRect().height() * (memData.memPercent / 100.0);
painter.fillRect(x, y - h, barWidth, h, memUsedColor);
y -= h;
h = contentsRect().height() * (memData.memCachedPercent / 100.0);
painter.fillRect(x, y, barWidth, h, memCachedColor);
x += barWidth;
y = contentsRect().y() + contentsRect().height();
h = contentsRect().height() * (memData.swapPercent / 100.0);
painter.fillRect(x, y - h, barWidth, h, memSwapColor);
// net
maxBytes = std::max(maxBytes, (sumReceived + sumSent));
x += barWidth;
y = contentsRect().y() + contentsRect().height();
h = contentsRect().height() * (double(sumReceived) / maxBytes);
painter.fillRect(x, y - h, barWidth, h, netReceivedColor);
y -= h;
h = contentsRect().height() * (double(sumSent) / maxBytes);
painter.fillRect(x, y - h, barWidth, h, netSentColor);
}
//--------------------------------------------------------------------------------
void SysLoad::mousePressEvent(QMouseEvent *event)
{
if ( event->button() == Qt::LeftButton )
{
KRun::runCommand("ksysguard", this);
}
}
//--------------------------------------------------------------------------------