1 /*
2     KSysGuard, the KDE System Guard
3 
4 	Copyright (c) 1999-2000 Hans Petter Bieker <bieker@kde.org>
5 	Copyright (c) 1999 Chris Schlaeger <cs@kde.org>
6 
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.
11 
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.
16 
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.
20 
21 */
22 
23 #include <fcntl.h>
24 #include <limits.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <sys/param.h>
29 #include <sys/sysctl.h>
30 #include <sys/types.h>
31 #include <sys/vmmeter.h>
32 #include <unistd.h>
33 /* Everything post 1.5.x uses uvm/uvm_* includes */
34 #if __NetBSD_Version__ >= 105010000
35 #include <uvm/uvm_param.h>
36 #else
37 #include <vm/vm_param.h>
38 #endif
39 
40 #include "Command.h"
41 #include "Memory.h"
42 #include "ksysguardd.h"
43 
44 static size_t Total = 0;
45 static size_t MFree = 0;
46 static size_t Used = 0;
47 static size_t Active = 0;
48 static size_t Inactive = 0;
49 static size_t Wired = 0;
50 static size_t Execpages = 0;
51 static size_t Filepages = 0;
52 static size_t STotal = 0;
53 static size_t SFree = 0;
54 static size_t SUsed = 0;
55 
56 void
initMemory(struct SensorModul * sm)57 initMemory(struct SensorModul* sm)
58 {
59         registerMonitor("mem/physical/free", "integer", printMFree, printMFreeInfo, sm);
60 	registerMonitor("mem/physical/used", "integer", printUsed, printUsedInfo, sm);
61 	registerMonitor("mem/physical/active", "integer", printActive, printActiveInfo, sm);
62 	registerMonitor("mem/physical/inactive", "integer", printInactive, printInactiveInfo, sm);
63 	registerMonitor("mem/physical/wired", "integer", printWired, printWiredInfo, sm);
64 	registerMonitor("mem/physical/execpages", "integer", printExecpages, printExecpagesInfo, sm);
65 	registerMonitor("mem/physical/filepages", "integer", printFilepages, printFilepagesInfo, sm);
66 	registerMonitor("mem/swap/free", "integer", printSwapFree, printSwapFreeInfo, sm);
67 	registerMonitor("mem/swap/used", "integer", printSwapUsed, printSwapUsedInfo, sm);
68 }
69 
70 void
exitMemory(void)71 exitMemory(void)
72 {
73 }
74 
75 int
updateMemory(void)76 updateMemory(void)
77 {
78 
79 #define ARRLEN(X) (sizeof(X)/sizeof(X[0]))
80   size_t len;
81 
82   {
83     static int mib[]={ CTL_HW, HW_PHYSMEM };
84 
85     len = sizeof(Total);
86     sysctl(mib, ARRLEN(mib), &Total, &len, NULL, 0);
87     Total >>= 10;
88   }
89 
90   {
91     struct uvmexp_sysctl x;
92     static int mib[] = { CTL_VM, VM_UVMEXP2 };
93 
94     len = sizeof(x);
95     STotal = SUsed = SFree = -1;
96     Active = Inactive = Wired = Execpages = Filepages = MFree = Used = -1;
97     if (-1 < sysctl(mib, ARRLEN(mib), &x, &len, NULL, 0)) {
98       STotal = (x.pagesize*x.swpages) >> 10;
99       SUsed = (x.pagesize*x.swpginuse) >> 10;
100       SFree = STotal - SUsed;
101       MFree = (x.free * x.pagesize) >> 10;
102       Active = (x.active * x.pagesize) >> 10;
103       Inactive = (x.inactive * x.pagesize) >> 10;
104       Wired = (x.wired * x.pagesize) >> 10;
105       Execpages = (x.execpages * x.pagesize) >> 10;
106       Filepages = (x.filepages * x.pagesize) >> 10;
107       Used = Total - MFree;
108     }
109   }
110   return 0;
111 }
112 
113 void
printMFree(const char * cmd)114 printMFree(const char* cmd)
115 {
116 	fprintf(CurrentClient, "%d\n", MFree);
117 }
118 
119 void
printMFreeInfo(const char * cmd)120 printMFreeInfo(const char* cmd)
121 {
122 	fprintf(CurrentClient, "Free Memory\t0\t%d\tKB\n", Total);
123 }
124 
125 void
printUsed(const char * cmd)126 printUsed(const char* cmd)
127 {
128 	fprintf(CurrentClient, "%d\n", Used);
129 }
130 
131 void
printUsedInfo(const char * cmd)132 printUsedInfo(const char* cmd)
133 {
134 	fprintf(CurrentClient, "Used Memory\t0\t%d\tKB\n", Total);
135 }
136 
137 void
printActive(const char * cmd)138 printActive(const char* cmd)
139 {
140 	fprintf(CurrentClient, "%d\n", Active);
141 }
142 
143 void
printActiveInfo(const char * cmd)144 printActiveInfo(const char* cmd)
145 {
146 	fprintf(CurrentClient, "Active Memory\t0\t%d\tKB\n", Total);
147 }
148 
149 void
printInactive(const char * cmd)150 printInactive(const char* cmd)
151 {
152 	fprintf(CurrentClient, "%d\n", Inactive);
153 }
154 
155 void
printInactiveInfo(const char * cmd)156 printInactiveInfo(const char* cmd)
157 {
158 	fprintf(CurrentClient, "Inactive Memory\t0\t%d\tKB\n", Total);
159 }
160 
161 void
printWired(const char * cmd)162 printWired(const char* cmd)
163 {
164 	fprintf(CurrentClient, "%d\n", Wired);
165 }
166 
167 void
printWiredInfo(const char * cmd)168 printWiredInfo(const char* cmd)
169 {
170 	fprintf(CurrentClient, "Wired Memory\t0\t%d\tKB\n", Total);
171 }
172 
173 void
printExecpages(const char * cmd)174 printExecpages(const char* cmd)
175 {
176 	fprintf(CurrentClient, "%d\n", Execpages);
177 }
178 
179 void
printExecpagesInfo(const char * cmd)180 printExecpagesInfo(const char* cmd)
181 {
182 	fprintf(CurrentClient, "Exec Pages\t0\t%d\tKB\n", Total);
183 }
184 
185 void
printFilepages(const char * cmd)186 printFilepages(const char* cmd)
187 {
188 	fprintf(CurrentClient, "%d\n", Filepages);
189 }
190 
191 void
printFilepagesInfo(const char * cmd)192 printFilepagesInfo(const char* cmd)
193 {
194 	fprintf(CurrentClient, "File Pages\t0\t%d\tKB\n", Total);
195 }
196 
197 void
printSwapUsed(const char * cmd)198 printSwapUsed(const char* cmd)
199 {
200 	fprintf(CurrentClient, "%d\n", SUsed);
201 }
202 
203 void
printSwapUsedInfo(const char * cmd)204 printSwapUsedInfo(const char* cmd)
205 {
206 	fprintf(CurrentClient, "Used Swap Memory\t0\t%d\tKB\n", STotal);
207 }
208 
209 void
printSwapFree(const char * cmd)210 printSwapFree(const char* cmd)
211 {
212 	fprintf(CurrentClient, "%d\n", SFree);
213 }
214 
215 void
printSwapFreeInfo(const char * cmd)216 printSwapFreeInfo(const char* cmd)
217 {
218 	fprintf(CurrentClient, "Free Swap Memory\t0\t%d\tKB\n", STotal);
219 }
220