uvm_meter.c revision 1.20 1 /* $NetBSD: uvm_meter.c,v 1.20 2001/06/02 18:09:27 chs Exp $ */
2
3 /*
4 * Copyright (c) 1997 Charles D. Cranor and Washington University.
5 * Copyright (c) 1982, 1986, 1989, 1993
6 * The Regents of the University of California.
7 *
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by Charles D. Cranor,
21 * Washington University, and the University of California, Berkeley
22 * and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
38 *
39 * @(#)vm_meter.c 8.4 (Berkeley) 1/4/94
40 * from: Id: uvm_meter.c,v 1.1.2.1 1997/08/14 19:10:35 chuck Exp
41 */
42
43 #include <sys/param.h>
44 #include <sys/proc.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <uvm/uvm_extern.h>
48 #include <sys/sysctl.h>
49
50 /*
51 * maxslp: ???? XXXCDC
52 */
53
54 int maxslp = MAXSLP; /* patchable ... */
55 struct loadavg averunnable;
56
57 /*
58 * constants for averages over 1, 5, and 15 minutes when sampling at
59 * 5 second intervals.
60 */
61
62 static fixpt_t cexp[3] = {
63 0.9200444146293232 * FSCALE, /* exp(-1/12) */
64 0.9834714538216174 * FSCALE, /* exp(-1/60) */
65 0.9944598480048967 * FSCALE, /* exp(-1/180) */
66 };
67
68 /*
69 * prototypes
70 */
71
72 static void uvm_loadav __P((struct loadavg *));
73 static void uvm_total __P((struct vmtotal *));
74 static int sysctl_uvmexp __P((void *, size_t *));
75
76 /*
77 * uvm_meter: calculate load average and wake up the swapper (if needed)
78 */
79 void
80 uvm_meter()
81 {
82 if ((time.tv_sec % 5) == 0)
83 uvm_loadav(&averunnable);
84 if (proc0.p_slptime > (maxslp / 2))
85 wakeup(&proc0);
86 }
87
88 /*
89 * uvm_loadav: compute a tenex style load average of a quantity on
90 * 1, 5, and 15 minute internvals.
91 */
92 static void
93 uvm_loadav(avg)
94 struct loadavg *avg;
95 {
96 int i, nrun;
97 struct proc *p;
98
99 proclist_lock_read();
100 nrun = 0;
101 LIST_FOREACH(p, &allproc, p_list) {
102 switch (p->p_stat) {
103 case SSLEEP:
104 if (p->p_priority > PZERO || p->p_slptime > 1)
105 continue;
106 /* fall through */
107 case SRUN:
108 case SONPROC:
109 case SIDL:
110 nrun++;
111 }
112 }
113 proclist_unlock_read();
114 for (i = 0; i < 3; i++)
115 avg->ldavg[i] = (cexp[i] * avg->ldavg[i] +
116 nrun * FSCALE * (FSCALE - cexp[i])) >> FSHIFT;
117 }
118
119 /*
120 * uvm_sysctl: sysctl hook into UVM system.
121 */
122 int
123 uvm_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
124 int *name;
125 u_int namelen;
126 void *oldp;
127 size_t *oldlenp;
128 void *newp;
129 size_t newlen;
130 struct proc *p;
131 {
132 struct vmtotal vmtotals;
133 int rv, t;
134
135 /* all sysctl names at this level are terminal */
136 if (namelen != 1)
137 return (ENOTDIR); /* overloaded */
138
139 switch (name[0]) {
140 case VM_LOADAVG:
141 return (sysctl_rdstruct(oldp, oldlenp, newp, &averunnable,
142 sizeof(averunnable)));
143
144 case VM_METER:
145 uvm_total(&vmtotals);
146 return (sysctl_rdstruct(oldp, oldlenp, newp, &vmtotals,
147 sizeof(vmtotals)));
148
149 case VM_UVMEXP:
150 return (sysctl_rdminstruct(oldp, oldlenp, newp, &uvmexp,
151 sizeof(uvmexp)));
152 case VM_UVMEXP2:
153 if (newp)
154 return (EPERM);
155 return (sysctl_uvmexp(oldp, oldlenp));
156
157 case VM_NKMEMPAGES:
158 return (sysctl_rdint(oldp, oldlenp, newp, nkmempages));
159
160 case VM_ANONMIN:
161 t = uvmexp.anonminpct;
162 rv = sysctl_int(oldp, oldlenp, newp, newlen, &t);
163 if (rv) {
164 return rv;
165 }
166 if (t + uvmexp.vtextminpct + uvmexp.vnodeminpct > 95 || t < 0) {
167 return EINVAL;
168 }
169 uvmexp.anonminpct = t;
170 uvmexp.anonmin = t * 256 / 100;
171 return rv;
172
173 case VM_VTEXTMIN:
174 t = uvmexp.vtextminpct;
175 rv = sysctl_int(oldp, oldlenp, newp, newlen, &t);
176 if (rv) {
177 return rv;
178 }
179 if (uvmexp.anonminpct + t + uvmexp.vnodeminpct > 95 || t < 0) {
180 return EINVAL;
181 }
182 uvmexp.vtextminpct = t;
183 uvmexp.vtextmin = t * 256 / 100;
184 return rv;
185
186 case VM_VNODEMIN:
187 t = uvmexp.vnodeminpct;
188 rv = sysctl_int(oldp, oldlenp, newp, newlen, &t);
189 if (rv) {
190 return rv;
191 }
192 if (uvmexp.anonminpct + uvmexp.vtextminpct + t > 95 || t < 0) {
193 return EINVAL;
194 }
195 uvmexp.vnodeminpct = t;
196 uvmexp.vnodemin = t * 256 / 100;
197 return rv;
198
199 default:
200 return (EOPNOTSUPP);
201 }
202 /* NOTREACHED */
203 }
204
205 static int
206 sysctl_uvmexp(oldp, oldlenp)
207 void *oldp;
208 size_t *oldlenp;
209 {
210 struct uvmexp_sysctl u;
211
212 memset(&u, 0, sizeof(u));
213
214 /* Entries here are in order of uvmexp_sysctl, not uvmexp */
215 u.pagesize = uvmexp.pagesize;
216 u.pagemask = uvmexp.pagemask;
217 u.pageshift = uvmexp.pageshift;
218 u.npages = uvmexp.npages;
219 u.free = uvmexp.free;
220 u.active = uvmexp.active;
221 u.inactive = uvmexp.inactive;
222 u.paging = uvmexp.paging;
223 u.wired = uvmexp.wired;
224 u.zeropages = uvmexp.zeropages;
225 u.reserve_pagedaemon = uvmexp.reserve_pagedaemon;
226 u.reserve_kernel = uvmexp.reserve_kernel;
227 u.freemin = uvmexp.freemin;
228 u.freetarg = uvmexp.freetarg;
229 u.inactarg = uvmexp.inactarg;
230 u.wiredmax = uvmexp.wiredmax;
231 u.nswapdev = uvmexp.nswapdev;
232 u.swpages = uvmexp.swpages;
233 u.swpginuse = uvmexp.swpginuse;
234 u.swpgonly = uvmexp.swpgonly;
235 u.nswget = uvmexp.nswget;
236 u.nanon = uvmexp.nanon;
237 u.nanonneeded = uvmexp.nanonneeded;
238 u.nfreeanon = uvmexp.nfreeanon;
239 u.faults = uvmexp.faults;
240 u.traps = uvmexp.traps;
241 u.intrs = uvmexp.intrs;
242 u.swtch = uvmexp.swtch;
243 u.softs = uvmexp.softs;
244 u.syscalls = uvmexp.syscalls;
245 u.pageins = uvmexp.pageins;
246 u.swapins = uvmexp.swapins;
247 u.swapouts = uvmexp.swapouts;
248 u.pgswapin = uvmexp.pgswapin;
249 u.pgswapout = uvmexp.pgswapout;
250 u.forks = uvmexp.forks;
251 u.forks_ppwait = uvmexp.forks_ppwait;
252 u.forks_sharevm = uvmexp.forks_sharevm;
253 u.pga_zerohit = uvmexp.pga_zerohit;
254 u.pga_zeromiss = uvmexp.pga_zeromiss;
255 u.zeroaborts = uvmexp.zeroaborts;
256 u.fltnoram = uvmexp.fltnoram;
257 u.fltnoanon = uvmexp.fltnoanon;
258 u.fltpgwait = uvmexp.fltpgwait;
259 u.fltpgrele = uvmexp.fltpgrele;
260 u.fltrelck = uvmexp.fltrelck;
261 u.fltrelckok = uvmexp.fltrelckok;
262 u.fltanget = uvmexp.fltanget;
263 u.fltanretry = uvmexp.fltanretry;
264 u.fltamcopy = uvmexp.fltamcopy;
265 u.fltnamap = uvmexp.fltnamap;
266 u.fltnomap = uvmexp.fltnomap;
267 u.fltlget = uvmexp.fltlget;
268 u.fltget = uvmexp.fltget;
269 u.flt_anon = uvmexp.flt_anon;
270 u.flt_acow = uvmexp.flt_acow;
271 u.flt_obj = uvmexp.flt_obj;
272 u.flt_prcopy = uvmexp.flt_prcopy;
273 u.flt_przero = uvmexp.flt_przero;
274 u.pdwoke = uvmexp.pdwoke;
275 u.pdrevs = uvmexp.pdrevs;
276 u.pdswout = uvmexp.pdswout;
277 u.pdfreed = uvmexp.pdfreed;
278 u.pdscans = uvmexp.pdscans;
279 u.pdanscan = uvmexp.pdanscan;
280 u.pdobscan = uvmexp.pdobscan;
281 u.pdreact = uvmexp.pdreact;
282 u.pdbusy = uvmexp.pdbusy;
283 u.pdpageouts = uvmexp.pdpageouts;
284 u.pdpending = uvmexp.pdpending;
285 u.pddeact = uvmexp.pddeact;
286 u.anonpages = uvmexp.anonpages;
287 u.vnodepages = uvmexp.vnodepages;
288 u.vtextpages = uvmexp.vtextpages;
289 u.colorhit = uvmexp.colorhit;
290 u.colormiss = uvmexp.colormiss;
291
292 return (sysctl_rdminstruct(oldp, oldlenp, NULL, &u, sizeof(u)));
293 }
294
295 /*
296 * uvm_total: calculate the current state of the system.
297 */
298 static void
299 uvm_total(totalp)
300 struct vmtotal *totalp;
301 {
302 struct proc *p;
303 #if 0
304 struct vm_map_entry * entry;
305 struct vm_map *map;
306 int paging;
307 #endif
308
309 memset(totalp, 0, sizeof *totalp);
310
311 /*
312 * calculate process statistics
313 */
314
315 proclist_lock_read();
316 LIST_FOREACH(p, &allproc, p_list) {
317 if (p->p_flag & P_SYSTEM)
318 continue;
319 switch (p->p_stat) {
320 case 0:
321 continue;
322
323 case SSLEEP:
324 case SSTOP:
325 if (p->p_flag & P_INMEM) {
326 if (p->p_priority <= PZERO)
327 totalp->t_dw++;
328 else if (p->p_slptime < maxslp)
329 totalp->t_sl++;
330 } else if (p->p_slptime < maxslp)
331 totalp->t_sw++;
332 if (p->p_slptime >= maxslp)
333 continue;
334 break;
335
336 case SRUN:
337 case SONPROC:
338 case SIDL:
339 if (p->p_flag & P_INMEM)
340 totalp->t_rq++;
341 else
342 totalp->t_sw++;
343 if (p->p_stat == SIDL)
344 continue;
345 break;
346 }
347 /*
348 * note active objects
349 */
350 #if 0
351 /*
352 * XXXCDC: BOGUS! rethink this. in the mean time
353 * don't do it.
354 */
355 paging = 0;
356 vm_map_lock(map);
357 for (map = &p->p_vmspace->vm_map, entry = map->header.next;
358 entry != &map->header; entry = entry->next) {
359 if (entry->is_a_map || entry->is_sub_map ||
360 entry->object.uvm_obj == NULL)
361 continue;
362 /* XXX how to do this with uvm */
363 }
364 vm_map_unlock(map);
365 if (paging)
366 totalp->t_pw++;
367 #endif
368 }
369 proclist_unlock_read();
370 /*
371 * Calculate object memory usage statistics.
372 */
373 totalp->t_free = uvmexp.free;
374 totalp->t_vm = uvmexp.npages - uvmexp.free + uvmexp.swpginuse;
375 totalp->t_avm = uvmexp.active + uvmexp.swpginuse; /* XXX */
376 totalp->t_rm = uvmexp.npages - uvmexp.free;
377 totalp->t_arm = uvmexp.active;
378 totalp->t_vmshr = 0; /* XXX */
379 totalp->t_avmshr = 0; /* XXX */
380 totalp->t_rmshr = 0; /* XXX */
381 totalp->t_armshr = 0; /* XXX */
382 }
383