2 KSysGuard, the KDE System Guard
4 Copyright (c) 1999-2000 Hans Petter Bieker <bieker@kde.org>
5 Copyright (c) 1999 Chris Schlaeger <cs@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/types.h>
24 #include <sys/param.h>
25 #include <sys/sysctl.h>
26 #include <sys/vmmeter.h>
28 #include <vm/vm_param.h>
40 #include "ksysguardd.h"
42 static size_t Total
= 0;
43 static size_t MFree
= 0;
44 static size_t Used
= 0;
45 static size_t Buffers
= 0;
46 static size_t Cached
= 0;
47 static size_t Application
= 0;
48 static size_t STotal
= 0;
49 static size_t SFree
= 0;
50 static size_t SUsed
= 0;
54 initMemory(struct SensorModul
* sm
)
58 char buf
[_POSIX2_LINE_MAX
];
60 if ((kd
= kvm_openfiles(nlistf
, memf
, NULL
, O_RDONLY
, buf
)) == NULL
) {
61 log_error("kvm_openfiles()");
65 registerMonitor("mem/physical/free", "integer", printMFree
, printMFreeInfo
, sm
);
66 registerMonitor("mem/physical/used", "integer", printUsed
, printUsedInfo
, sm
);
67 registerMonitor("mem/physical/buf", "integer", printBuffers
, printBuffersInfo
, sm
);
68 registerMonitor("mem/physical/cached", "integer", printCached
, printCachedInfo
, sm
);
69 registerMonitor("mem/physical/application", "integer", printApplication
, printApplicationInfo
, sm
);
70 registerMonitor("mem/swap/free", "integer", printSwapFree
, printSwapFreeInfo
, sm
);
71 registerMonitor("mem/swap/used", "integer", printSwapUsed
, printSwapUsedInfo
, sm
);
86 struct kvm_swap kswap
[16];
87 int i
, swap_count
, hlen
, pagesize
= getpagesize();
91 sysctlbyname("hw.physmem", &Total
, &len
, NULL
, 0);
94 /* Borrowed from pstat */
95 swap_count
= kvm_getswapinfo(kd
, kswap
, 16, SWIF_DEV_PREFIX
);
96 getbsize(&hlen
, &blocksize
);
98 #define CONVERT(v) ((int)((quad_t)(v) * pagesize / blocksize))
100 if (swap_count
> 0) {
101 STotal
= CONVERT(kswap
[0].ksw_total
);
102 SUsed
= CONVERT(kswap
[0].ksw_used
);
103 SFree
= CONVERT(kswap
[0].ksw_total
- kswap
[0].ksw_used
);
106 len
= sizeof (Buffers
);
107 if ((sysctlbyname("vfs.bufspace", &Buffers
, &len
, NULL
, 0) == -1) || !len
)
108 Buffers
= 0; /* Doesn't work under FreeBSD v2.2.x */
111 len
= sizeof (Cached
);
112 if ((sysctlbyname("vm.stats.vm.v_cache_count", &Cached
, &len
, NULL
, 0) == -1) || !len
)
113 Cached
= 0; /* Doesn't work under FreeBSD v2.2.x */
114 Cached
*= pagesize
/ 1024;
116 len
= sizeof (MFree
);
117 if ((sysctlbyname("vm.stats.vm.v_free_count", &MFree
, &len
, NULL
, 0) == -1) || !len
)
118 MFree
= 0; /* Doesn't work under FreeBSD v2.2.x */
119 MFree
*= pagesize
/ 1024;
121 Used
= Total
- MFree
;
122 Application
= Used
- Buffers
- Cached
;
128 printMFree(const char* cmd
)
130 fprintf(CurrentClient
, "%d\n", MFree
);
134 printMFreeInfo(const char* cmd
)
136 fprintf(CurrentClient
, "Free Memory\t0\t%d\tKB\n", Total
);
140 printUsed(const char* cmd
)
142 fprintf(CurrentClient
, "%d\n", Used
);
146 printUsedInfo(const char* cmd
)
148 fprintf(CurrentClient
, "Used Memory\t0\t%d\tKB\n", Total
);
152 printBuffers(const char* cmd
)
154 fprintf(CurrentClient
, "%d\n", Buffers
);
158 printBuffersInfo(const char* cmd
)
160 fprintf(CurrentClient
, "Buffer Memory\t0\t%d\tKB\n", Total
);
164 printCached(const char* cmd
)
166 fprintf(CurrentClient
, "%d\n", Cached
);
170 printCachedInfo(const char* cmd
)
172 fprintf(CurrentClient
, "Cached Memory\t0\t%d\tKB\n", Total
);
176 printApplication(const char* cmd
)
178 fprintf(CurrentClient
, "%d\n", Application
);
182 printApplicationInfo(const char* cmd
)
184 fprintf(CurrentClient
, "Application Memory\t0\t%ld\tKB\n", Total
);
188 printSwapUsed(const char* cmd
)
190 fprintf(CurrentClient
, "%d\n", SUsed
);
194 printSwapUsedInfo(const char* cmd
)
196 fprintf(CurrentClient
, "Used Swap Memory\t0\t%d\tKB\n", STotal
);
200 printSwapFree(const char* cmd
)
202 fprintf(CurrentClient
, "%d\n", SFree
);
206 printSwapFreeInfo(const char* cmd
)
208 fprintf(CurrentClient
, "Free Swap Memory\t0\t%d\tKB\n", STotal
);