uvm_meter.c revision 1.55 1 1.55 matt /* $NetBSD: uvm_meter.c,v 1.55 2010/12/20 00:25:48 matt Exp $ */
2 1.1 mrg
3 1.1 mrg /*
4 1.1 mrg * Copyright (c) 1997 Charles D. Cranor and Washington University.
5 1.1 mrg * Copyright (c) 1982, 1986, 1989, 1993
6 1.19 chs * The Regents of the University of California.
7 1.1 mrg *
8 1.1 mrg * All rights reserved.
9 1.1 mrg *
10 1.1 mrg * Redistribution and use in source and binary forms, with or without
11 1.1 mrg * modification, are permitted provided that the following conditions
12 1.1 mrg * are met:
13 1.1 mrg * 1. Redistributions of source code must retain the above copyright
14 1.1 mrg * notice, this list of conditions and the following disclaimer.
15 1.1 mrg * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 mrg * notice, this list of conditions and the following disclaimer in the
17 1.1 mrg * documentation and/or other materials provided with the distribution.
18 1.1 mrg * 3. All advertising materials mentioning features or use of this software
19 1.1 mrg * must display the following acknowledgement:
20 1.1 mrg * This product includes software developed by Charles D. Cranor,
21 1.19 chs * Washington University, and the University of California, Berkeley
22 1.1 mrg * and its contributors.
23 1.1 mrg * 4. Neither the name of the University nor the names of its contributors
24 1.1 mrg * may be used to endorse or promote products derived from this software
25 1.1 mrg * without specific prior written permission.
26 1.1 mrg *
27 1.1 mrg * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 1.1 mrg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 1.1 mrg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 1.1 mrg * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 1.1 mrg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 1.1 mrg * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 1.1 mrg * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 1.1 mrg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 1.1 mrg * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 1.1 mrg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 1.1 mrg * SUCH DAMAGE.
38 1.1 mrg *
39 1.1 mrg * @(#)vm_meter.c 8.4 (Berkeley) 1/4/94
40 1.3 mrg * from: Id: uvm_meter.c,v 1.1.2.1 1997/08/14 19:10:35 chuck Exp
41 1.1 mrg */
42 1.22 lukem
43 1.22 lukem #include <sys/cdefs.h>
44 1.55 matt __KERNEL_RCSID(0, "$NetBSD: uvm_meter.c,v 1.55 2010/12/20 00:25:48 matt Exp $");
45 1.1 mrg
46 1.1 mrg #include <sys/param.h>
47 1.55 matt #include <sys/systm.h>
48 1.55 matt #include <sys/cpu.h>
49 1.1 mrg #include <sys/proc.h>
50 1.1 mrg #include <sys/kernel.h>
51 1.41 yamt #include <sys/sysctl.h>
52 1.41 yamt
53 1.53 uebayasi #include <uvm/uvm.h>
54 1.41 yamt #include <uvm/uvm_pdpolicy.h>
55 1.1 mrg
56 1.1 mrg /*
57 1.1 mrg * maxslp: ???? XXXCDC
58 1.1 mrg */
59 1.1 mrg
60 1.1 mrg int maxslp = MAXSLP; /* patchable ... */
61 1.14 chs struct loadavg averunnable;
62 1.1 mrg
63 1.30 junyoung static void uvm_total(struct vmtotal *);
64 1.1 mrg
65 1.1 mrg /*
66 1.27 atatat * sysctl helper routine for the vm.vmmeter node.
67 1.1 mrg */
68 1.27 atatat static int
69 1.27 atatat sysctl_vm_meter(SYSCTLFN_ARGS)
70 1.1 mrg {
71 1.27 atatat struct sysctlnode node;
72 1.4 mrg struct vmtotal vmtotals;
73 1.1 mrg
74 1.27 atatat node = *rnode;
75 1.27 atatat node.sysctl_data = &vmtotals;
76 1.27 atatat uvm_total(&vmtotals);
77 1.23 chs
78 1.27 atatat return (sysctl_lookup(SYSCTLFN_CALL(&node)));
79 1.27 atatat }
80 1.23 chs
81 1.27 atatat /*
82 1.27 atatat * sysctl helper routine for the vm.uvmexp node.
83 1.27 atatat */
84 1.27 atatat static int
85 1.27 atatat sysctl_vm_uvmexp(SYSCTLFN_ARGS)
86 1.27 atatat {
87 1.27 atatat struct sysctlnode node;
88 1.23 chs
89 1.27 atatat node = *rnode;
90 1.27 atatat if (oldp)
91 1.27 atatat node.sysctl_size = min(*oldlenp, node.sysctl_size);
92 1.23 chs
93 1.27 atatat return (sysctl_lookup(SYSCTLFN_CALL(&node)));
94 1.15 simonb }
95 1.15 simonb
96 1.15 simonb static int
97 1.27 atatat sysctl_vm_uvmexp2(SYSCTLFN_ARGS)
98 1.15 simonb {
99 1.27 atatat struct sysctlnode node;
100 1.15 simonb struct uvmexp_sysctl u;
101 1.41 yamt int active, inactive;
102 1.55 matt CPU_INFO_ITERATOR cii;
103 1.55 matt struct cpu_info *ci;
104 1.41 yamt
105 1.41 yamt uvm_estimatepageable(&active, &inactive);
106 1.15 simonb
107 1.16 simonb memset(&u, 0, sizeof(u));
108 1.16 simonb
109 1.16 simonb /* Entries here are in order of uvmexp_sysctl, not uvmexp */
110 1.15 simonb u.pagesize = uvmexp.pagesize;
111 1.15 simonb u.pagemask = uvmexp.pagemask;
112 1.15 simonb u.pageshift = uvmexp.pageshift;
113 1.15 simonb u.npages = uvmexp.npages;
114 1.15 simonb u.free = uvmexp.free;
115 1.41 yamt u.active = active;
116 1.41 yamt u.inactive = inactive;
117 1.15 simonb u.paging = uvmexp.paging;
118 1.15 simonb u.wired = uvmexp.wired;
119 1.15 simonb u.zeropages = uvmexp.zeropages;
120 1.15 simonb u.reserve_pagedaemon = uvmexp.reserve_pagedaemon;
121 1.15 simonb u.reserve_kernel = uvmexp.reserve_kernel;
122 1.15 simonb u.freemin = uvmexp.freemin;
123 1.15 simonb u.freetarg = uvmexp.freetarg;
124 1.41 yamt u.inactarg = 0; /* unused */
125 1.15 simonb u.wiredmax = uvmexp.wiredmax;
126 1.15 simonb u.nswapdev = uvmexp.nswapdev;
127 1.15 simonb u.swpages = uvmexp.swpages;
128 1.15 simonb u.swpginuse = uvmexp.swpginuse;
129 1.15 simonb u.swpgonly = uvmexp.swpgonly;
130 1.15 simonb u.nswget = uvmexp.nswget;
131 1.55 matt for (CPU_INFO_FOREACH(cii, ci)) {
132 1.55 matt u.faults += ci->ci_data.cpu_nfault;
133 1.55 matt u.traps += ci->ci_data.cpu_ntrap;
134 1.55 matt u.intrs += ci->ci_data.cpu_nintr;
135 1.55 matt u.swtch += ci->ci_data.cpu_nswtch;
136 1.55 matt u.softs += ci->ci_data.cpu_nsoft;
137 1.55 matt u.syscalls += ci->ci_data.cpu_nsyscall;
138 1.55 matt }
139 1.15 simonb u.pageins = uvmexp.pageins;
140 1.15 simonb u.pgswapin = uvmexp.pgswapin;
141 1.15 simonb u.pgswapout = uvmexp.pgswapout;
142 1.15 simonb u.forks = uvmexp.forks;
143 1.15 simonb u.forks_ppwait = uvmexp.forks_ppwait;
144 1.15 simonb u.forks_sharevm = uvmexp.forks_sharevm;
145 1.15 simonb u.pga_zerohit = uvmexp.pga_zerohit;
146 1.15 simonb u.pga_zeromiss = uvmexp.pga_zeromiss;
147 1.15 simonb u.zeroaborts = uvmexp.zeroaborts;
148 1.15 simonb u.fltnoram = uvmexp.fltnoram;
149 1.15 simonb u.fltnoanon = uvmexp.fltnoanon;
150 1.15 simonb u.fltpgwait = uvmexp.fltpgwait;
151 1.15 simonb u.fltpgrele = uvmexp.fltpgrele;
152 1.15 simonb u.fltrelck = uvmexp.fltrelck;
153 1.15 simonb u.fltrelckok = uvmexp.fltrelckok;
154 1.15 simonb u.fltanget = uvmexp.fltanget;
155 1.15 simonb u.fltanretry = uvmexp.fltanretry;
156 1.15 simonb u.fltamcopy = uvmexp.fltamcopy;
157 1.15 simonb u.fltnamap = uvmexp.fltnamap;
158 1.15 simonb u.fltnomap = uvmexp.fltnomap;
159 1.15 simonb u.fltlget = uvmexp.fltlget;
160 1.15 simonb u.fltget = uvmexp.fltget;
161 1.15 simonb u.flt_anon = uvmexp.flt_anon;
162 1.15 simonb u.flt_acow = uvmexp.flt_acow;
163 1.15 simonb u.flt_obj = uvmexp.flt_obj;
164 1.15 simonb u.flt_prcopy = uvmexp.flt_prcopy;
165 1.15 simonb u.flt_przero = uvmexp.flt_przero;
166 1.15 simonb u.pdwoke = uvmexp.pdwoke;
167 1.15 simonb u.pdrevs = uvmexp.pdrevs;
168 1.15 simonb u.pdfreed = uvmexp.pdfreed;
169 1.15 simonb u.pdscans = uvmexp.pdscans;
170 1.15 simonb u.pdanscan = uvmexp.pdanscan;
171 1.15 simonb u.pdobscan = uvmexp.pdobscan;
172 1.15 simonb u.pdreact = uvmexp.pdreact;
173 1.15 simonb u.pdbusy = uvmexp.pdbusy;
174 1.15 simonb u.pdpageouts = uvmexp.pdpageouts;
175 1.15 simonb u.pdpending = uvmexp.pdpending;
176 1.15 simonb u.pddeact = uvmexp.pddeact;
177 1.16 simonb u.anonpages = uvmexp.anonpages;
178 1.23 chs u.filepages = uvmexp.filepages;
179 1.23 chs u.execpages = uvmexp.execpages;
180 1.18 thorpej u.colorhit = uvmexp.colorhit;
181 1.18 thorpej u.colormiss = uvmexp.colormiss;
182 1.49 ad u.cpuhit = uvmexp.cpuhit;
183 1.49 ad u.cpumiss = uvmexp.cpumiss;
184 1.15 simonb
185 1.27 atatat node = *rnode;
186 1.27 atatat node.sysctl_data = &u;
187 1.27 atatat node.sysctl_size = sizeof(u);
188 1.27 atatat return (sysctl_lookup(SYSCTLFN_CALL(&node)));
189 1.27 atatat }
190 1.27 atatat
191 1.27 atatat /*
192 1.38 yamt * sysctl helper routine for uvm_pctparam.
193 1.38 yamt */
194 1.38 yamt static int
195 1.41 yamt uvm_sysctlpctparam(SYSCTLFN_ARGS)
196 1.38 yamt {
197 1.38 yamt int t, error;
198 1.38 yamt struct sysctlnode node;
199 1.38 yamt struct uvm_pctparam *pct;
200 1.38 yamt
201 1.38 yamt pct = rnode->sysctl_data;
202 1.38 yamt t = pct->pct_pct;
203 1.38 yamt
204 1.38 yamt node = *rnode;
205 1.38 yamt node.sysctl_data = &t;
206 1.38 yamt error = sysctl_lookup(SYSCTLFN_CALL(&node));
207 1.38 yamt if (error || newp == NULL)
208 1.38 yamt return error;
209 1.38 yamt
210 1.38 yamt if (t < 0 || t > 100)
211 1.38 yamt return EINVAL;
212 1.38 yamt
213 1.41 yamt error = uvm_pctparam_check(pct, t);
214 1.41 yamt if (error) {
215 1.41 yamt return error;
216 1.41 yamt }
217 1.38 yamt uvm_pctparam_set(pct, t);
218 1.38 yamt
219 1.38 yamt return (0);
220 1.38 yamt }
221 1.38 yamt
222 1.38 yamt /*
223 1.27 atatat * uvm_sysctl: sysctl hook into UVM system.
224 1.27 atatat */
225 1.27 atatat SYSCTL_SETUP(sysctl_vm_setup, "sysctl vm subtree setup")
226 1.27 atatat {
227 1.27 atatat
228 1.31 atatat sysctl_createv(clog, 0, NULL, NULL,
229 1.31 atatat CTLFLAG_PERMANENT,
230 1.27 atatat CTLTYPE_NODE, "vm", NULL,
231 1.27 atatat NULL, 0, NULL, 0,
232 1.27 atatat CTL_VM, CTL_EOL);
233 1.31 atatat sysctl_createv(clog, 0, NULL, NULL,
234 1.31 atatat CTLFLAG_PERMANENT,
235 1.32 atatat CTLTYPE_STRUCT, "vmmeter",
236 1.32 atatat SYSCTL_DESCR("Simple system-wide virtual memory "
237 1.32 atatat "statistics"),
238 1.27 atatat sysctl_vm_meter, 0, NULL, sizeof(struct vmtotal),
239 1.27 atatat CTL_VM, VM_METER, CTL_EOL);
240 1.31 atatat sysctl_createv(clog, 0, NULL, NULL,
241 1.31 atatat CTLFLAG_PERMANENT,
242 1.32 atatat CTLTYPE_STRUCT, "loadavg",
243 1.32 atatat SYSCTL_DESCR("System load average history"),
244 1.27 atatat NULL, 0, &averunnable, sizeof(averunnable),
245 1.27 atatat CTL_VM, VM_LOADAVG, CTL_EOL);
246 1.31 atatat sysctl_createv(clog, 0, NULL, NULL,
247 1.31 atatat CTLFLAG_PERMANENT,
248 1.32 atatat CTLTYPE_STRUCT, "uvmexp",
249 1.32 atatat SYSCTL_DESCR("Detailed system-wide virtual memory "
250 1.32 atatat "statistics"),
251 1.27 atatat sysctl_vm_uvmexp, 0, &uvmexp, sizeof(uvmexp),
252 1.27 atatat CTL_VM, VM_UVMEXP, CTL_EOL);
253 1.31 atatat sysctl_createv(clog, 0, NULL, NULL,
254 1.31 atatat CTLFLAG_PERMANENT,
255 1.32 atatat CTLTYPE_INT, "nkmempages",
256 1.32 atatat SYSCTL_DESCR("Default number of pages in kmem_map"),
257 1.27 atatat NULL, 0, &nkmempages, 0,
258 1.27 atatat CTL_VM, VM_NKMEMPAGES, CTL_EOL);
259 1.31 atatat sysctl_createv(clog, 0, NULL, NULL,
260 1.31 atatat CTLFLAG_PERMANENT,
261 1.32 atatat CTLTYPE_STRUCT, "uvmexp2",
262 1.32 atatat SYSCTL_DESCR("Detailed system-wide virtual memory "
263 1.32 atatat "statistics (MI)"),
264 1.27 atatat sysctl_vm_uvmexp2, 0, NULL, 0,
265 1.27 atatat CTL_VM, VM_UVMEXP2, CTL_EOL);
266 1.31 atatat sysctl_createv(clog, 0, NULL, NULL,
267 1.32 atatat CTLFLAG_PERMANENT, CTLTYPE_INT, "maxslp",
268 1.32 atatat SYSCTL_DESCR("Maximum process sleep time before being "
269 1.32 atatat "swapped"),
270 1.27 atatat NULL, 0, &maxslp, 0,
271 1.27 atatat CTL_VM, VM_MAXSLP, CTL_EOL);
272 1.31 atatat sysctl_createv(clog, 0, NULL, NULL,
273 1.31 atatat CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
274 1.32 atatat CTLTYPE_INT, "uspace",
275 1.32 atatat SYSCTL_DESCR("Number of bytes allocated for a kernel "
276 1.32 atatat "stack"),
277 1.27 atatat NULL, USPACE, NULL, 0,
278 1.27 atatat CTL_VM, VM_USPACE, CTL_EOL);
279 1.31 atatat sysctl_createv(clog, 0, NULL, NULL,
280 1.31 atatat CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
281 1.51 mrg CTLTYPE_BOOL, "idlezero",
282 1.33 yamt SYSCTL_DESCR("Whether try to zero pages in idle loop"),
283 1.33 yamt NULL, 0, &vm_page_zero_enable, 0,
284 1.33 yamt CTL_VM, CTL_CREATE, CTL_EOL);
285 1.41 yamt
286 1.41 yamt uvmpdpol_sysctlsetup();
287 1.1 mrg }
288 1.1 mrg
289 1.1 mrg /*
290 1.1 mrg * uvm_total: calculate the current state of the system.
291 1.1 mrg */
292 1.4 mrg static void
293 1.35 thorpej uvm_total(struct vmtotal *totalp)
294 1.1 mrg {
295 1.24 thorpej struct lwp *l;
296 1.1 mrg #if 0
297 1.20 chs struct vm_map_entry * entry;
298 1.20 chs struct vm_map *map;
299 1.4 mrg int paging;
300 1.1 mrg #endif
301 1.41 yamt int active;
302 1.1 mrg
303 1.7 perry memset(totalp, 0, sizeof *totalp);
304 1.1 mrg
305 1.4 mrg /*
306 1.4 mrg * calculate process statistics
307 1.4 mrg */
308 1.48 ad mutex_enter(proc_lock);
309 1.44 ad LIST_FOREACH(l, &alllwp, l_list) {
310 1.46 pavel if (l->l_proc->p_flag & PK_SYSTEM)
311 1.4 mrg continue;
312 1.24 thorpej switch (l->l_stat) {
313 1.4 mrg case 0:
314 1.4 mrg continue;
315 1.6 mrg
316 1.24 thorpej case LSSLEEP:
317 1.24 thorpej case LSSTOP:
318 1.54 enami if ((l->l_flag & LW_SINTR) == 0) {
319 1.50 rmind totalp->t_dw++;
320 1.50 rmind } else if (l->l_slptime < maxslp) {
321 1.50 rmind totalp->t_sl++;
322 1.50 rmind }
323 1.24 thorpej if (l->l_slptime >= maxslp)
324 1.4 mrg continue;
325 1.4 mrg break;
326 1.6 mrg
327 1.24 thorpej case LSRUN:
328 1.24 thorpej case LSONPROC:
329 1.24 thorpej case LSIDL:
330 1.50 rmind totalp->t_rq++;
331 1.24 thorpej if (l->l_stat == LSIDL)
332 1.4 mrg continue;
333 1.4 mrg break;
334 1.4 mrg }
335 1.4 mrg /*
336 1.4 mrg * note active objects
337 1.4 mrg */
338 1.1 mrg #if 0
339 1.4 mrg /*
340 1.36 simonb * XXXCDC: BOGUS! rethink this. in the mean time
341 1.5 mrg * don't do it.
342 1.4 mrg */
343 1.4 mrg paging = 0;
344 1.5 mrg vm_map_lock(map);
345 1.4 mrg for (map = &p->p_vmspace->vm_map, entry = map->header.next;
346 1.4 mrg entry != &map->header; entry = entry->next) {
347 1.4 mrg if (entry->is_a_map || entry->is_sub_map ||
348 1.5 mrg entry->object.uvm_obj == NULL)
349 1.4 mrg continue;
350 1.5 mrg /* XXX how to do this with uvm */
351 1.4 mrg }
352 1.5 mrg vm_map_unlock(map);
353 1.4 mrg if (paging)
354 1.4 mrg totalp->t_pw++;
355 1.1 mrg #endif
356 1.4 mrg }
357 1.48 ad mutex_exit(proc_lock);
358 1.44 ad
359 1.4 mrg /*
360 1.4 mrg * Calculate object memory usage statistics.
361 1.4 mrg */
362 1.41 yamt uvm_estimatepageable(&active, NULL);
363 1.5 mrg totalp->t_free = uvmexp.free;
364 1.5 mrg totalp->t_vm = uvmexp.npages - uvmexp.free + uvmexp.swpginuse;
365 1.41 yamt totalp->t_avm = active + uvmexp.swpginuse; /* XXX */
366 1.5 mrg totalp->t_rm = uvmexp.npages - uvmexp.free;
367 1.41 yamt totalp->t_arm = active;
368 1.5 mrg totalp->t_vmshr = 0; /* XXX */
369 1.5 mrg totalp->t_avmshr = 0; /* XXX */
370 1.5 mrg totalp->t_rmshr = 0; /* XXX */
371 1.5 mrg totalp->t_armshr = 0; /* XXX */
372 1.1 mrg }
373 1.38 yamt
374 1.38 yamt void
375 1.38 yamt uvm_pctparam_set(struct uvm_pctparam *pct, int val)
376 1.38 yamt {
377 1.38 yamt
378 1.38 yamt pct->pct_pct = val;
379 1.38 yamt pct->pct_scaled = val * UVM_PCTPARAM_SCALE / 100;
380 1.38 yamt }
381 1.41 yamt
382 1.41 yamt int
383 1.41 yamt uvm_pctparam_get(struct uvm_pctparam *pct)
384 1.41 yamt {
385 1.41 yamt
386 1.41 yamt return pct->pct_pct;
387 1.41 yamt }
388 1.41 yamt
389 1.41 yamt int
390 1.41 yamt uvm_pctparam_check(struct uvm_pctparam *pct, int val)
391 1.41 yamt {
392 1.41 yamt
393 1.41 yamt if (pct->pct_check == NULL) {
394 1.41 yamt return 0;
395 1.41 yamt }
396 1.41 yamt return (*pct->pct_check)(pct, val);
397 1.41 yamt }
398 1.41 yamt
399 1.41 yamt void
400 1.41 yamt uvm_pctparam_init(struct uvm_pctparam *pct, int val,
401 1.41 yamt int (*fn)(struct uvm_pctparam *, int))
402 1.41 yamt {
403 1.41 yamt
404 1.41 yamt pct->pct_check = fn;
405 1.41 yamt uvm_pctparam_set(pct, val);
406 1.41 yamt }
407 1.41 yamt
408 1.41 yamt int
409 1.41 yamt uvm_pctparam_createsysctlnode(struct uvm_pctparam *pct, const char *name,
410 1.43 yamt const char *desc)
411 1.41 yamt {
412 1.41 yamt
413 1.41 yamt return sysctl_createv(NULL, 0, NULL, NULL,
414 1.41 yamt CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
415 1.41 yamt CTLTYPE_INT, name, SYSCTL_DESCR(desc),
416 1.41 yamt uvm_sysctlpctparam, 0, pct, 0, CTL_VM, CTL_CREATE, CTL_EOL);
417 1.41 yamt }
418