Make a branch to make krunner Good Enough For Aaron™.
[kdebase/uwolfer.git] / workspace / ksysguard / ksysguardd / FreeBSD / Memory.c
blob9e3db646cc6593884e38a23ab9c5007eff324e5b
1 /*
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>
30 #include <fcntl.h>
31 #include <kvm.h>
32 #include <limits.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <unistd.h>
38 #include "Command.h"
39 #include "Memory.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;
51 static kvm_t *kd;
53 void
54 initMemory(struct SensorModul* sm)
56 char *nlistf = NULL;
57 char *memf = NULL;
58 char buf[_POSIX2_LINE_MAX];
60 if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == NULL) {
61 log_error("kvm_openfiles()");
62 return;
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);
74 void
75 exitMemory(void)
77 kvm_close(kd);
80 int
81 updateMemory(void)
83 size_t len;
84 FILE *file;
85 char buf[256];
86 struct kvm_swap kswap[16];
87 int i, swap_count, hlen, pagesize = getpagesize();
88 long blocksize;
90 len = sizeof (Total);
91 sysctlbyname("hw.physmem", &Total, &len, NULL, 0);
92 Total /= 1024;
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 */
109 Buffers /= 1024;
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;
124 return 0;
127 void
128 printMFree(const char* cmd)
130 fprintf(CurrentClient, "%d\n", MFree);
133 void
134 printMFreeInfo(const char* cmd)
136 fprintf(CurrentClient, "Free Memory\t0\t%d\tKB\n", Total);
139 void
140 printUsed(const char* cmd)
142 fprintf(CurrentClient, "%d\n", Used);
145 void
146 printUsedInfo(const char* cmd)
148 fprintf(CurrentClient, "Used Memory\t0\t%d\tKB\n", Total);
151 void
152 printBuffers(const char* cmd)
154 fprintf(CurrentClient, "%d\n", Buffers);
157 void
158 printBuffersInfo(const char* cmd)
160 fprintf(CurrentClient, "Buffer Memory\t0\t%d\tKB\n", Total);
163 void
164 printCached(const char* cmd)
166 fprintf(CurrentClient, "%d\n", Cached);
169 void
170 printCachedInfo(const char* cmd)
172 fprintf(CurrentClient, "Cached Memory\t0\t%d\tKB\n", Total);
175 void
176 printApplication(const char* cmd)
178 fprintf(CurrentClient, "%d\n", Application);
181 void
182 printApplicationInfo(const char* cmd)
184 fprintf(CurrentClient, "Application Memory\t0\t%ld\tKB\n", Total);
187 void
188 printSwapUsed(const char* cmd)
190 fprintf(CurrentClient, "%d\n", SUsed);
193 void
194 printSwapUsedInfo(const char* cmd)
196 fprintf(CurrentClient, "Used Swap Memory\t0\t%d\tKB\n", STotal);
199 void
200 printSwapFree(const char* cmd)
202 fprintf(CurrentClient, "%d\n", SFree);
205 void
206 printSwapFreeInfo(const char* cmd)
208 fprintf(CurrentClient, "Free Swap Memory\t0\t%d\tKB\n", STotal);