2
KSysGuard, the KDE System Guard
4
Copyright (c) 1999 Chris Schlaeger <cs@kde.org>
5
Copyright (c) 1999-2000 Hans Petter Bieker <bieker@kde.org>
7
This program is free software; you can redistribute it and/or modify
8
it under the terms of the GNU General Public License as published by
9
the Free Software Foundation; either version 2 of the License, or
10
(at your option) any later version.
12
This program is distributed in the hope that it will be useful,
13
but WITHOUT ANY WARRANTY; without even the implied warranty of
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
GNU General Public License for more details.
17
You should have received a copy of the GNU General Public License
18
along with this program; if not, write to the Free Software
19
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23
#include <sys/param.h>
24
#include <sys/sysctl.h>
25
#include <sys/dkstat.h>
36
#include "ksysguardd.h"
38
static size_t Total = 0;
39
static size_t MFree = 0;
40
static size_t Used = 0;
41
static size_t Application = 0;
42
static size_t Active = 0;
43
static size_t InActive = 0;
44
static size_t STotal = 0;
45
static size_t SFree = 0;
46
static size_t SUsed = 0;
47
static int pageshift = 0;
49
/* define pagetok in terms of pageshift */
50
#define pagetok(size) ((size) << pageshift)
52
void swapmode(int *used, int *total);
55
initMemory(struct SensorModul* sm)
58
static int physmem_mib[] = { CTL_HW, HW_PHYSMEM };
60
/* get the page size with "getpagesize" and calculate pageshift from
62
pagesize = getpagesize();
64
while (pagesize > 1) {
69
sysctl(physmem_mib, 2, &Total, &size, NULL, 0);
71
swapmode(&SUsed, &STotal);
73
registerMonitor("mem/physical/free", "integer", printMFree, printMFreeInfo, sm);
74
registerMonitor("mem/physical/active", "integer", printActive, printActiveInfo, sm);
75
registerMonitor("mem/physical/inactive", "integer", printInActive, printInActiveInfo, sm);
76
registerMonitor("mem/physical/used", "integer", printUsed, printUsedInfo, sm);
77
registerMonitor("mem/physical/application", "integer", printApplication, printApplicationInfo, sm);
78
registerMonitor("mem/swap/free", "integer", printSwapFree, printSwapFreeInfo, sm);
79
registerMonitor("mem/swap/used", "integer", printSwapUsed, printSwapUsedInfo, sm);
90
static int vmtotal_mib[] = {CTL_VM, VM_METER};
92
struct vmtotal vmtotal;
93
size = sizeof(vmtotal);
95
if (sysctl(vmtotal_mib, 2, &vmtotal, &size, NULL, 0) < 0)
98
MFree = pagetok(vmtotal.t_free);
100
Active = pagetok(vmtotal.t_arm);
102
InActive = pagetok(vmtotal.t_rm);
106
Used = Total - MFree;
109
swapmode(&SUsed, &STotal);
110
SFree = STotal - SUsed;
115
printMFree(const char* cmd)
117
fprintf(CurrentClient, "%d\n", MFree);
121
printMFreeInfo(const char* cmd)
123
fprintf(CurrentClient, "Free Memory\t0\t%d\tKB\n", Total);
127
printUsed(const char* cmd)
129
fprintf(CurrentClient, "%d\n", Used);
133
printUsedInfo(const char* cmd)
135
fprintf(CurrentClient, "Used Memory\t0\t%d\tKB\n", Total);
139
printApplication(const char* cmd)
141
fprintf(CurrentClient, "%d\n", Application);
145
printApplicationInfo(const char* cmd)
147
fprintf(CurrentClient, "Application Memory\t0\t%ld\tKB\n", Total);
151
printActive(const char* cmd)
153
fprintf(CurrentClient, "%d\n", Active);
157
printActiveInfo(const char* cmd)
159
fprintf(CurrentClient, "Active Memory\t0\t%d\tKB\n", Total);
163
printInActive(const char* cmd)
165
fprintf(CurrentClient, "%d\n", InActive);
169
printInActiveInfo(const char* cmd)
171
fprintf(CurrentClient, "InActive Memory\t0\t%d\tKB\n", Total);
175
printSwapUsed(const char* cmd)
177
fprintf(CurrentClient, "%d\n", SUsed);
181
printSwapUsedInfo(const char* cmd)
183
fprintf(CurrentClient, "Used Swap Memory\t0\t%d\tKB\n", STotal);
187
printSwapFree(const char* cmd)
189
fprintf(CurrentClient, "%d\n", SFree);
193
printSwapFreeInfo(const char* cmd)
195
fprintf(CurrentClient, "Free Swap Memory\t0\t%d\tKB\n", STotal);
199
This function swapmode was originally written by Tobias
200
Weingartner <weingart@openbsd.org>
202
Taken from OpenBSD top command
205
swapmode (int *used, int *total)
207
int nswap, rnswap, i;
208
struct swapent *swdev;
212
/* Number of swap devices */
213
nswap = swapctl(SWAP_NSWAP, 0, 0);
217
swdev = (struct swapent *) malloc(nswap * sizeof(*swdev));
221
rnswap = swapctl(SWAP_STATS, swdev, nswap);
227
/* if rnswap != nswap, then what? */
229
/* Total things up */
230
for (i = 0; i < nswap; i++) {
231
if (swdev[i].se_flags & SWF_ENABLE) {
232
*used += (swdev[i].se_inuse / (1024 / DEV_BSIZE));
233
*total += (swdev[i].se_nblks / (1024 / DEV_BSIZE));