uvm_meter.c revision 1.44 1 /* $NetBSD: uvm_meter.c,v 1.44 2007/02/09 21:55:43 ad 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/cdefs.h>
44 __KERNEL_RCSID(0, "$NetBSD: uvm_meter.c,v 1.44 2007/02/09 21:55:43 ad Exp $");
45
46 #include <sys/param.h>
47 #include <sys/proc.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <sys/sysctl.h>
51
52 #include <uvm/uvm_extern.h>
53 #include <uvm/uvm_pdpolicy.h>
54
55 /*
56 * maxslp: ???? XXXCDC
57 */
58
59 int maxslp = MAXSLP; /* patchable ... */
60 struct loadavg averunnable;
61
62 /*
63 * constants for averages over 1, 5, and 15 minutes when sampling at
64 * 5 second intervals.
65 */
66
67 static const fixpt_t cexp[3] = {
68 0.9200444146293232 * FSCALE, /* exp(-1/12) */
69 0.9834714538216174 * FSCALE, /* exp(-1/60) */
70 0.9944598480048967 * FSCALE, /* exp(-1/180) */
71 };
72
73 /*
74 * prototypes
75 */
76
77 static void uvm_loadav(struct loadavg *);
78 static void uvm_total(struct vmtotal *);
79
80 /*
81 * uvm_meter: calculate load average and wake up the swapper (if needed)
82 */
83 void
84 uvm_meter(void)
85 {
86 if ((time_second % 5) == 0)
87 uvm_loadav(&averunnable);
88 if (lwp0.l_slptime > (maxslp / 2))
89 wakeup(&proc0);
90 }
91
92 /*
93 * uvm_loadav: compute a tenex style load average of a quantity on
94 * 1, 5, and 15 minute intervals. */
95 static void
96 uvm_loadav(struct loadavg *avg)
97 {
98 int i, nrun;
99 struct lwp *l;
100
101 mutex_enter(&proclist_mutex);
102 nrun = 0;
103 LIST_FOREACH(l, &alllwp, l_list) {
104 switch (l->l_stat) {
105 case LSSLEEP:
106 if (l->l_priority > PZERO || l->l_slptime > 1)
107 continue;
108 /* fall through */
109 case LSRUN:
110 case LSONPROC:
111 case LSIDL:
112 nrun++;
113 }
114 }
115 mutex_exit(&proclist_mutex);
116 for (i = 0; i < 3; i++)
117 avg->ldavg[i] = (cexp[i] * avg->ldavg[i] +
118 nrun * FSCALE * (FSCALE - cexp[i])) >> FSHIFT;
119 }
120
121 /*
122 * sysctl helper routine for the vm.vmmeter node.
123 */
124 static int
125 sysctl_vm_meter(SYSCTLFN_ARGS)
126 {
127 struct sysctlnode node;
128 struct vmtotal vmtotals;
129
130 node = *rnode;
131 node.sysctl_data = &vmtotals;
132 uvm_total(&vmtotals);
133
134 return (sysctl_lookup(SYSCTLFN_CALL(&node)));
135 }
136
137 /*
138 * sysctl helper routine for the vm.uvmexp node.
139 */
140 static int
141 sysctl_vm_uvmexp(SYSCTLFN_ARGS)
142 {
143 struct sysctlnode node;
144
145 node = *rnode;
146 if (oldp)
147 node.sysctl_size = min(*oldlenp, node.sysctl_size);
148
149 return (sysctl_lookup(SYSCTLFN_CALL(&node)));
150 }
151
152 static int
153 sysctl_vm_uvmexp2(SYSCTLFN_ARGS)
154 {
155 struct sysctlnode node;
156 struct uvmexp_sysctl u;
157 int active, inactive;
158
159 uvm_estimatepageable(&active, &inactive);
160
161 memset(&u, 0, sizeof(u));
162
163 /* Entries here are in order of uvmexp_sysctl, not uvmexp */
164 u.pagesize = uvmexp.pagesize;
165 u.pagemask = uvmexp.pagemask;
166 u.pageshift = uvmexp.pageshift;
167 u.npages = uvmexp.npages;
168 u.free = uvmexp.free;
169 u.active = active;
170 u.inactive = inactive;
171 u.paging = uvmexp.paging;
172 u.wired = uvmexp.wired;
173 u.zeropages = uvmexp.zeropages;
174 u.reserve_pagedaemon = uvmexp.reserve_pagedaemon;
175 u.reserve_kernel = uvmexp.reserve_kernel;
176 u.freemin = uvmexp.freemin;
177 u.freetarg = uvmexp.freetarg;
178 u.inactarg = 0; /* unused */
179 u.wiredmax = uvmexp.wiredmax;
180 u.nswapdev = uvmexp.nswapdev;
181 u.swpages = uvmexp.swpages;
182 u.swpginuse = uvmexp.swpginuse;
183 u.swpgonly = uvmexp.swpgonly;
184 u.nswget = uvmexp.nswget;
185 u.faults = uvmexp.faults;
186 u.traps = uvmexp.traps;
187 u.intrs = uvmexp.intrs;
188 u.swtch = uvmexp.swtch;
189 u.softs = uvmexp.softs;
190 u.syscalls = uvmexp.syscalls;
191 u.pageins = uvmexp.pageins;
192 u.swapins = uvmexp.swapins;
193 u.swapouts = uvmexp.swapouts;
194 u.pgswapin = uvmexp.pgswapin;
195 u.pgswapout = uvmexp.pgswapout;
196 u.forks = uvmexp.forks;
197 u.forks_ppwait = uvmexp.forks_ppwait;
198 u.forks_sharevm = uvmexp.forks_sharevm;
199 u.pga_zerohit = uvmexp.pga_zerohit;
200 u.pga_zeromiss = uvmexp.pga_zeromiss;
201 u.zeroaborts = uvmexp.zeroaborts;
202 u.fltnoram = uvmexp.fltnoram;
203 u.fltnoanon = uvmexp.fltnoanon;
204 u.fltpgwait = uvmexp.fltpgwait;
205 u.fltpgrele = uvmexp.fltpgrele;
206 u.fltrelck = uvmexp.fltrelck;
207 u.fltrelckok = uvmexp.fltrelckok;
208 u.fltanget = uvmexp.fltanget;
209 u.fltanretry = uvmexp.fltanretry;
210 u.fltamcopy = uvmexp.fltamcopy;
211 u.fltnamap = uvmexp.fltnamap;
212 u.fltnomap = uvmexp.fltnomap;
213 u.fltlget = uvmexp.fltlget;
214 u.fltget = uvmexp.fltget;
215 u.flt_anon = uvmexp.flt_anon;
216 u.flt_acow = uvmexp.flt_acow;
217 u.flt_obj = uvmexp.flt_obj;
218 u.flt_prcopy = uvmexp.flt_prcopy;
219 u.flt_przero = uvmexp.flt_przero;
220 u.pdwoke = uvmexp.pdwoke;
221 u.pdrevs = uvmexp.pdrevs;
222 u.pdswout = uvmexp.pdswout;
223 u.pdfreed = uvmexp.pdfreed;
224 u.pdscans = uvmexp.pdscans;
225 u.pdanscan = uvmexp.pdanscan;
226 u.pdobscan = uvmexp.pdobscan;
227 u.pdreact = uvmexp.pdreact;
228 u.pdbusy = uvmexp.pdbusy;
229 u.pdpageouts = uvmexp.pdpageouts;
230 u.pdpending = uvmexp.pdpending;
231 u.pddeact = uvmexp.pddeact;
232 u.anonpages = uvmexp.anonpages;
233 u.filepages = uvmexp.filepages;
234 u.execpages = uvmexp.execpages;
235 u.colorhit = uvmexp.colorhit;
236 u.colormiss = uvmexp.colormiss;
237
238 node = *rnode;
239 node.sysctl_data = &u;
240 node.sysctl_size = sizeof(u);
241 return (sysctl_lookup(SYSCTLFN_CALL(&node)));
242 }
243
244 /*
245 * sysctl helper routine for uvm_pctparam.
246 */
247 static int
248 uvm_sysctlpctparam(SYSCTLFN_ARGS)
249 {
250 int t, error;
251 struct sysctlnode node;
252 struct uvm_pctparam *pct;
253
254 pct = rnode->sysctl_data;
255 t = pct->pct_pct;
256
257 node = *rnode;
258 node.sysctl_data = &t;
259 error = sysctl_lookup(SYSCTLFN_CALL(&node));
260 if (error || newp == NULL)
261 return error;
262
263 if (t < 0 || t > 100)
264 return EINVAL;
265
266 error = uvm_pctparam_check(pct, t);
267 if (error) {
268 return error;
269 }
270 uvm_pctparam_set(pct, t);
271
272 return (0);
273 }
274
275 /*
276 * uvm_sysctl: sysctl hook into UVM system.
277 */
278 SYSCTL_SETUP(sysctl_vm_setup, "sysctl vm subtree setup")
279 {
280
281 sysctl_createv(clog, 0, NULL, NULL,
282 CTLFLAG_PERMANENT,
283 CTLTYPE_NODE, "vm", NULL,
284 NULL, 0, NULL, 0,
285 CTL_VM, CTL_EOL);
286 sysctl_createv(clog, 0, NULL, NULL,
287 CTLFLAG_PERMANENT,
288 CTLTYPE_STRUCT, "vmmeter",
289 SYSCTL_DESCR("Simple system-wide virtual memory "
290 "statistics"),
291 sysctl_vm_meter, 0, NULL, sizeof(struct vmtotal),
292 CTL_VM, VM_METER, CTL_EOL);
293 sysctl_createv(clog, 0, NULL, NULL,
294 CTLFLAG_PERMANENT,
295 CTLTYPE_STRUCT, "loadavg",
296 SYSCTL_DESCR("System load average history"),
297 NULL, 0, &averunnable, sizeof(averunnable),
298 CTL_VM, VM_LOADAVG, CTL_EOL);
299 sysctl_createv(clog, 0, NULL, NULL,
300 CTLFLAG_PERMANENT,
301 CTLTYPE_STRUCT, "uvmexp",
302 SYSCTL_DESCR("Detailed system-wide virtual memory "
303 "statistics"),
304 sysctl_vm_uvmexp, 0, &uvmexp, sizeof(uvmexp),
305 CTL_VM, VM_UVMEXP, CTL_EOL);
306 sysctl_createv(clog, 0, NULL, NULL,
307 CTLFLAG_PERMANENT,
308 CTLTYPE_INT, "nkmempages",
309 SYSCTL_DESCR("Default number of pages in kmem_map"),
310 NULL, 0, &nkmempages, 0,
311 CTL_VM, VM_NKMEMPAGES, CTL_EOL);
312 sysctl_createv(clog, 0, NULL, NULL,
313 CTLFLAG_PERMANENT,
314 CTLTYPE_STRUCT, "uvmexp2",
315 SYSCTL_DESCR("Detailed system-wide virtual memory "
316 "statistics (MI)"),
317 sysctl_vm_uvmexp2, 0, NULL, 0,
318 CTL_VM, VM_UVMEXP2, CTL_EOL);
319 sysctl_createv(clog, 0, NULL, NULL,
320 CTLFLAG_PERMANENT, CTLTYPE_INT, "maxslp",
321 SYSCTL_DESCR("Maximum process sleep time before being "
322 "swapped"),
323 NULL, 0, &maxslp, 0,
324 CTL_VM, VM_MAXSLP, CTL_EOL);
325 sysctl_createv(clog, 0, NULL, NULL,
326 CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
327 CTLTYPE_INT, "uspace",
328 SYSCTL_DESCR("Number of bytes allocated for a kernel "
329 "stack"),
330 NULL, USPACE, NULL, 0,
331 CTL_VM, VM_USPACE, CTL_EOL);
332 sysctl_createv(clog, 0, NULL, NULL,
333 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
334 CTLTYPE_INT, "idlezero",
335 SYSCTL_DESCR("Whether try to zero pages in idle loop"),
336 NULL, 0, &vm_page_zero_enable, 0,
337 CTL_VM, CTL_CREATE, CTL_EOL);
338
339 uvmpdpol_sysctlsetup();
340 }
341
342 /*
343 * uvm_total: calculate the current state of the system.
344 */
345 static void
346 uvm_total(struct vmtotal *totalp)
347 {
348 struct lwp *l;
349 #if 0
350 struct vm_map_entry * entry;
351 struct vm_map *map;
352 int paging;
353 #endif
354 int active;
355
356 memset(totalp, 0, sizeof *totalp);
357
358 /*
359 * calculate process statistics
360 */
361 mutex_enter(&proclist_mutex);
362 LIST_FOREACH(l, &alllwp, l_list) {
363 if (l->l_proc->p_flag & P_SYSTEM)
364 continue;
365 switch (l->l_stat) {
366 case 0:
367 continue;
368
369 case LSSLEEP:
370 case LSSTOP:
371 if (l->l_flag & L_INMEM) {
372 if (l->l_priority <= PZERO)
373 totalp->t_dw++;
374 else if (l->l_slptime < maxslp)
375 totalp->t_sl++;
376 } else if (l->l_slptime < maxslp)
377 totalp->t_sw++;
378 if (l->l_slptime >= maxslp)
379 continue;
380 break;
381
382 case LSRUN:
383 case LSONPROC:
384 case LSIDL:
385 if (l->l_flag & L_INMEM)
386 totalp->t_rq++;
387 else
388 totalp->t_sw++;
389 if (l->l_stat == LSIDL)
390 continue;
391 break;
392 }
393 /*
394 * note active objects
395 */
396 #if 0
397 /*
398 * XXXCDC: BOGUS! rethink this. in the mean time
399 * don't do it.
400 */
401 paging = 0;
402 vm_map_lock(map);
403 for (map = &p->p_vmspace->vm_map, entry = map->header.next;
404 entry != &map->header; entry = entry->next) {
405 if (entry->is_a_map || entry->is_sub_map ||
406 entry->object.uvm_obj == NULL)
407 continue;
408 /* XXX how to do this with uvm */
409 }
410 vm_map_unlock(map);
411 if (paging)
412 totalp->t_pw++;
413 #endif
414 }
415 mutex_exit(&proclist_mutex);
416
417 /*
418 * Calculate object memory usage statistics.
419 */
420 uvm_estimatepageable(&active, NULL);
421 totalp->t_free = uvmexp.free;
422 totalp->t_vm = uvmexp.npages - uvmexp.free + uvmexp.swpginuse;
423 totalp->t_avm = active + uvmexp.swpginuse; /* XXX */
424 totalp->t_rm = uvmexp.npages - uvmexp.free;
425 totalp->t_arm = active;
426 totalp->t_vmshr = 0; /* XXX */
427 totalp->t_avmshr = 0; /* XXX */
428 totalp->t_rmshr = 0; /* XXX */
429 totalp->t_armshr = 0; /* XXX */
430 }
431
432 void
433 uvm_pctparam_set(struct uvm_pctparam *pct, int val)
434 {
435
436 pct->pct_pct = val;
437 pct->pct_scaled = val * UVM_PCTPARAM_SCALE / 100;
438 }
439
440 int
441 uvm_pctparam_get(struct uvm_pctparam *pct)
442 {
443
444 return pct->pct_pct;
445 }
446
447 int
448 uvm_pctparam_check(struct uvm_pctparam *pct, int val)
449 {
450
451 if (pct->pct_check == NULL) {
452 return 0;
453 }
454 return (*pct->pct_check)(pct, val);
455 }
456
457 void
458 uvm_pctparam_init(struct uvm_pctparam *pct, int val,
459 int (*fn)(struct uvm_pctparam *, int))
460 {
461
462 pct->pct_check = fn;
463 uvm_pctparam_set(pct, val);
464 }
465
466 int
467 uvm_pctparam_createsysctlnode(struct uvm_pctparam *pct, const char *name,
468 const char *desc)
469 {
470
471 return sysctl_createv(NULL, 0, NULL, NULL,
472 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
473 CTLTYPE_INT, name, SYSCTL_DESCR(desc),
474 uvm_sysctlpctparam, 0, pct, 0, CTL_VM, CTL_CREATE, CTL_EOL);
475 }
476