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