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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
|
/***************************************************************************
* Copyright (C) 2003 by Hans Karlsson *
* karlsson.h@home.se *
* *
* This program 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 2 of the License, or *
* (at your option) any later version. *
***************************************************************************/
#include "memsensor.h"
#include <tqfile.h>
#include <tqglobal.h>
#include <tqtextstream.h>
#include <tqstring.h>
#include <tqregexp.h>
#ifdef Q_OS_FREEBSD
#include <sys/time.h>
#include <sys/param.h>
#include <sys/sysctl.h>
#include <sys/resource.h>
#include <unistd.h>
#include <kvm.h>
#include <sys/file.h>
#include <osreldate.h>
#endif
#if defined(Q_OS_NETBSD)
#include <sys/param.h>
#include <sys/sysctl.h>
#include <sys/sched.h>
#include <sys/swap.h>
#include <uvm/uvm_extern.h>
#endif
#include <kprocio.h>
#if defined Q_OS_FREEBSD || defined(Q_OS_NETBSD)
/* define pagetok in terms of pageshift */
#define pagetok(size) ((size) << pageshift)
#endif
MemSensor::MemSensor(int msec) : Sensor(msec)
{
#if defined Q_OS_FREEBSD || defined(Q_OS_NETBSD)
/* get the page size with "getpagesize" and calculate pageshift from it */
int pagesize = getpagesize();
pageshift = 0;
while (pagesize > 1)
{
pageshift++;
pagesize >>= 1;
}
/* we only need the amount of log(2)1024 for our conversion */
pageshift -= 10;
# if defined(Q_OS_FREEBSD) && defined(__FreeBSD_version) && __FreeBSD_version >= 500018
kd = kvm_open("/dev/null", "/dev/null", "/dev/null", O_RDONLY, "kvm_open");
# elif defined Q_OS_FREEBSD
connect(&ksp, TQ_SIGNAL(receivedStdout(TDEProcess *, char *, int )),
this,TQ_SLOT(receivedStdout(TDEProcess *, char *, int )));
connect(&ksp, TQ_SIGNAL(processExited(TDEProcess *)),
this,TQ_SLOT(processExited( TDEProcess * )));
swapTotal = swapUsed = 0;
MaxSet = false;
readValues();
# endif
#else
readValues();
#endif
}
MemSensor::~MemSensor()
{}
#ifdef Q_OS_FREEBSD
void MemSensor::receivedStdout(TDEProcess *, char *buffer, int len )
{
buffer[len] = 0;
sensorResult += TQString( TQCString(buffer) );
}
#else
void MemSensor::receivedStdout(TDEProcess *, char *, int)
{
}
#endif
void MemSensor::processExited(TDEProcess *)
{
#ifdef Q_OS_FREEBSD
TQStringList stringList = TQStringList::split('\n',sensorResult);
sensorResult = "";
TQStringList itemsList = TQStringList::split(' ', stringList[1]);
swapUsed = itemsList[2].toInt();
swapTotal = itemsList[1].toInt();
#endif
}
int MemSensor::getMemTotal()
{
#if defined Q_OS_FREEBSD || defined(Q_OS_NETBSD)
static int mem = 0;
size_t size = sizeof(mem);
sysctlbyname("hw.physmem", &mem, &size, NULL, 0);
return (mem / 1024);
#else
TQRegExp rx( "MemTotal:\\s*(\\d+)" );
rx.search( meminfo );
return ( rx.cap(1).toInt() );
#endif
}
int MemSensor::getMemFree()
{
#ifdef Q_OS_FREEBSD
static int mem = 0;
size_t size = sizeof(mem);
sysctlbyname("vm.stats.vm.v_free_count", &mem, &size, NULL, 0);
return (pagetok(mem));
#elif defined(Q_OS_NETBSD)
struct uvmexp_sysctl uvmexp;
int mib[2];
size_t ssize;
mib[0] = CTL_VM;
mib[1] = VM_UVMEXP2;
ssize = sizeof(uvmexp);
sysctl(mib,2,&uvmexp,&ssize,NULL,0);
return pagetok(uvmexp.free);
#else
TQRegExp rx( "MemFree:\\s*(\\d+)" );
rx.search( meminfo );
return ( rx.cap(1).toInt() );
#endif
}
int MemSensor::getBuffers()
{
#ifdef Q_OS_FREEBSD
static int mem = 0;
size_t size = sizeof(mem);
sysctlbyname("vfs.bufspace", &mem, &size, NULL, 0);
return (mem / 1024);
#elif defined(Q_OS_NETBSD)
static int buf_mem = 0;
size_t size = sizeof(buf_mem);
sysctlbyname("vm.bufmem", &buf_mem, &size, NULL, 0);
return (buf_mem / 1024);
#else
TQRegExp rx( "Buffers:\\s*(\\d+)" );
rx.search( meminfo );
return ( rx.cap(1).toInt() );
#endif
}
int MemSensor::getCached()
{
#ifdef Q_OS_FREEBSD
static int mem = 0;
size_t size = sizeof(mem);
sysctlbyname("vm.stats.vm.v_cache_count", &mem, &size, NULL, 0);
return (pagetok(mem));
#elif defined(Q_OS_NETBSD)
return 0;
#else
TQRegExp rx1( "Cached:\\s*(\\d+)" );
TQRegExp rx2( "SwapCached:\\s*(\\d+)" );
rx1.search( meminfo );
rx2.search( meminfo );
return ( rx1.cap(1).toInt() + rx2.cap(1).toInt() );
#endif
}
int MemSensor::getSwapTotal()
{
#ifdef Q_OS_FREEBSD
# if defined(__FreeBSD_version) && __FreeBSD_version >= 500018
int n = -1;
int pagesize = getpagesize();
int retavail = 0;
if (kd != NULL)
n = kvm_getswapinfo(kd, &swapinfo, 1, 0);
if (n < 0 || swapinfo.ksw_total == 0)
return(0);
retavail = swapinfo.ksw_total * pagesize / 1024;
return(retavail);
#else
return(swapTotal);
# endif
#elif defined(Q_OS_NETBSD)
struct uvmexp_sysctl uvmexp;
int STotal = 0;
int pagesize = 1;
int mib[2];
size_t ssize;
mib[0] = CTL_VM;
mib[1] = VM_UVMEXP;
ssize = sizeof(uvmexp);
if (sysctl(mib, 2, &uvmexp, &ssize, NULL, 0) != -1) {
pagesize = uvmexp.pagesize;
STotal = (pagesize*uvmexp.swpages) >> 10;
}
return STotal;
#else
TQRegExp rx( "SwapTotal:\\s*(\\d+)" );
rx.search( meminfo );
return ( rx.cap(1).toInt() );
#endif
}
int MemSensor::getSwapFree()
{
#ifdef Q_OS_FREEBSD
# if defined(__FreeBSD_version) && __FreeBSD_version >= 500018
int n = -1;
int pagesize = getpagesize();
int retfree = 0;
if (kd != NULL)
n = kvm_getswapinfo(kd, &swapinfo, 1, 0);
if (n < 0 || swapinfo.ksw_total == 0)
return(0);
retfree = (swapinfo.ksw_total - swapinfo.ksw_used) * pagesize / 1024;
return(retfree);
# else
return(swapTotal - swapUsed);
# endif
#elif defined(Q_OS_NETBSD)
struct uvmexp_sysctl uvmexp;
int STotal = 0;
int SFree = 0;
int SUsed = 0;
int pagesize = 1;
int mib[2];
size_t ssize;
mib[0] = CTL_VM;
mib[1] = VM_UVMEXP;
ssize = sizeof(uvmexp);
if (sysctl(mib,2,&uvmexp,&ssize,NULL,0) != -1) {
pagesize = uvmexp.pagesize;
STotal = (pagesize*uvmexp.swpages) >> 10;
SUsed = (pagesize*uvmexp.swpginuse) >> 10;
SFree = STotal - SUsed;
}
return SFree;
#else
TQRegExp rx( "SwapFree:\\s*(\\d+)" );
rx.search( meminfo );
return ( rx.cap(1).toInt() );
#endif
}
void MemSensor::readValues()
{
#if defined Q_OS_FREEBSD || defined(Q_OS_NETBSD)
# if defined(Q_OS_FREEBSD) && !(defined(__FreeBSD_version) && __FreeBSD_version >= 500018)
ksp.clearArguments();
ksp << "swapinfo";
ksp.start( TDEProcess::NotifyOnExit,KProcIO::Stdout);
# endif
#else
TQFile file("/proc/meminfo");
TQString line;
if ( file.open(IO_ReadOnly | IO_Translate) )
{
TQTextStream t( &file ); // use a text stream
meminfo = t.read();
file.close();
}
#endif
}
#define SUB_FORMAT_STR(fstring, value) \
format.replace(TQRegExp(#fstring, false), TQString::number((int)(value)));
void MemSensor::update()
{
readValues();
TQString format;
SensorParams *sp;
Meter *meter;
TQObjectListIt it( *objList );
#if defined(Q_OS_FREEBSD) && !(defined(__FreeBSD_version) && __FreeBSD_version >= 500018)
bool set = false;
#endif
int totalMem = getMemTotal();
int usedMem = totalMem - getMemFree();
int usedMemNoBuffers = usedMem - getBuffers() - getCached();
int totalSwap = getSwapTotal();
int usedSwap = totalSwap - getSwapFree();
while (it != 0)
{
sp = (SensorParams*)(*it);
#if defined(Q_OS_FREEBSD) && !(defined(__FreeBSD_version) && __FreeBSD_version >= 500018)
if ( (!MaxSet) && (totalSwap > 0) ) {
setMaxValue(sp);
bool set = true;
}
#endif
meter = sp->getMeter();
format = sp->getParam("FORMAT");
if (format.length() == 0 )
{
format = "%um";
}
SUB_FORMAT_STR(%fmbp, (totalMem - usedMemNoBuffers) * 100.0 / totalMem)
SUB_FORMAT_STR(%fmb, (totalMem - usedMemNoBuffers) / 1024.0 + 0.5)
SUB_FORMAT_STR(%fmp, (totalMem - usedMem) * 100.0 / totalMem)
SUB_FORMAT_STR(%fm, (totalMem - usedMem) / 1024.0 + 0.5)
SUB_FORMAT_STR(%umbp, usedMemNoBuffers * 100.0 / totalMem)
SUB_FORMAT_STR(%umb, usedMemNoBuffers / 1024.0 + 0.5)
SUB_FORMAT_STR(%ump, usedMem * 100.0 / totalMem)
SUB_FORMAT_STR(%um, usedMem / 1024.0 + 0.5)
SUB_FORMAT_STR(%tm, totalMem / 1024.0 + 0.5)
SUB_FORMAT_STR(%fsp, (totalSwap - usedSwap) * 100.0 / totalSwap)
SUB_FORMAT_STR(%fs, (totalSwap - usedSwap) / 1024.0 + 0.5)
SUB_FORMAT_STR(%usp, usedSwap * 100.0 / totalSwap)
SUB_FORMAT_STR(%us, usedSwap / 1024.0 + 0.5)
SUB_FORMAT_STR(%ts, totalSwap / 1024.0 + 0.5)
meter->setValue(format);
++it;
}
#if defined(Q_OS_FREEBSD) && !(defined(__FreeBSD_version) && __FreeBSD_version >= 500018)
if (set)
{
MaxSet = true;
}
#endif
}
#undef SUB_FORMAT_STR
void MemSensor::setMaxValue( SensorParams *sp )
{
Meter *meter;
meter = sp->getMeter();
TQString f;
f = sp->getParam("FORMAT");
if (f.length() == 0 )
{
f = "%um";
}
else if (f.endsWith("p"))
{
meter->setMax(100);
}
else if (f == "%fm" || f == "%um" || f == "%fmb" || f == "%umb")
{
meter->setMax(getMemTotal() / 1024);
}
else if (f == "%fs" || f == "%us")
{
meter->setMax(getSwapTotal() / 1024);
}
}
#include "memsensor.moc"
|