1 /* $NetBSD: uvm_meter.c,v 1.22 2001/11/10 07:37:00 lukem 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.22 2001/11/10 07:37:00 lukem Exp $"); 45 46 #include <sys/param.h> 47 #include <sys/proc.h> 48 #include <sys/systm.h> 49 #include <sys/kernel.h> 50 #include <uvm/uvm_extern.h> 51 #include <sys/sysctl.h> 52 53 /* 54 * maxslp: ???? XXXCDC 55 */ 56 57 int maxslp = MAXSLP; /* patchable ... */ 58 struct loadavg averunnable; 59 60 /* 61 * constants for averages over 1, 5, and 15 minutes when sampling at 62 * 5 second intervals. 63 */ 64 65 static fixpt_t cexp[3] = { 66 0.9200444146293232 * FSCALE, /* exp(-1/12) */ 67 0.9834714538216174 * FSCALE, /* exp(-1/60) */ 68 0.9944598480048967 * FSCALE, /* exp(-1/180) */ 69 }; 70 71 /* 72 * prototypes 73 */ 74 75 static void uvm_loadav __P((struct loadavg *)); 76 static void uvm_total __P((struct vmtotal *)); 77 static int sysctl_uvmexp __P((void *, size_t *)); 78 79 /* 80 * uvm_meter: calculate load average and wake up the swapper (if needed) 81 */ 82 void 83 uvm_meter() 84 { 85 if ((time.tv_sec % 5) == 0) 86 uvm_loadav(&averunnable); 87 if (proc0.p_slptime > (maxslp / 2)) 88 wakeup(&proc0); 89 } 90 91 /* 92 * uvm_loadav: compute a tenex style load average of a quantity on 93 * 1, 5, and 15 minute internvals. 94 */ 95 static void 96 uvm_loadav(avg) 97 struct loadavg *avg; 98 { 99 int i, nrun; 100 struct proc *p; 101 102 proclist_lock_read(); 103 nrun = 0; 104 LIST_FOREACH(p, &allproc, p_list) { 105 switch (p->p_stat) { 106 case SSLEEP: 107 if (p->p_priority > PZERO || p->p_slptime > 1) 108 continue; 109 /* fall through */ 110 case SRUN: 111 case SONPROC: 112 case SIDL: 113 nrun++; 114 } 115 } 116 proclist_unlock_read(); 117 for (i = 0; i < 3; i++) 118 avg->ldavg[i] = (cexp[i] * avg->ldavg[i] + 119 nrun * FSCALE * (FSCALE - cexp[i])) >> FSHIFT; 120 } 121 122 /* 123 * uvm_sysctl: sysctl hook into UVM system. 124 */ 125 int 126 uvm_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p) 127 int *name; 128 u_int namelen; 129 void *oldp; 130 size_t *oldlenp; 131 void *newp; 132 size_t newlen; 133 struct proc *p; 134 { 135 struct vmtotal vmtotals; 136 int rv, t; 137 138 /* all sysctl names at this level are terminal */ 139 if (namelen != 1) 140 return (ENOTDIR); /* overloaded */ 141 142 switch (name[0]) { 143 case VM_LOADAVG: 144 return (sysctl_rdstruct(oldp, oldlenp, newp, &averunnable, 145 sizeof(averunnable))); 146 147 case VM_METER: 148 uvm_total(&vmtotals); 149 return (sysctl_rdstruct(oldp, oldlenp, newp, &vmtotals, 150 sizeof(vmtotals))); 151 152 case VM_UVMEXP: 153 return (sysctl_rdminstruct(oldp, oldlenp, newp, &uvmexp, 154 sizeof(uvmexp))); 155 case VM_UVMEXP2: 156 if (newp) 157 return (EPERM); 158 return (sysctl_uvmexp(oldp, oldlenp)); 159 160 case VM_NKMEMPAGES: 161 return (sysctl_rdint(oldp, oldlenp, newp, nkmempages)); 162 163 case VM_ANONMIN: 164 t = uvmexp.anonminpct; 165 rv = sysctl_int(oldp, oldlenp, newp, newlen, &t); 166 if (rv) { 167 return rv; 168 } 169 if (t + uvmexp.vtextminpct + uvmexp.vnodeminpct > 95 || t < 0) { 170 return EINVAL; 171 } 172 uvmexp.anonminpct = t; 173 uvmexp.anonmin = t * 256 / 100; 174 return rv; 175 176 case VM_VTEXTMIN: 177 t = uvmexp.vtextminpct; 178 rv = sysctl_int(oldp, oldlenp, newp, newlen, &t); 179 if (rv) { 180 return rv; 181 } 182 if (uvmexp.anonminpct + t + uvmexp.vnodeminpct > 95 || t < 0) { 183 return EINVAL; 184 } 185 uvmexp.vtextminpct = t; 186 uvmexp.vtextmin = t * 256 / 100; 187 return rv; 188 189 case VM_VNODEMIN: 190 t = uvmexp.vnodeminpct; 191 rv = sysctl_int(oldp, oldlenp, newp, newlen, &t); 192 if (rv) { 193 return rv; 194 } 195 if (uvmexp.anonminpct + uvmexp.vtextminpct + t > 95 || t < 0) { 196 return EINVAL; 197 } 198 uvmexp.vnodeminpct = t; 199 uvmexp.vnodemin = t * 256 / 100; 200 return rv; 201 202 case VM_MAXSLP: 203 return (sysctl_rdint(oldp, oldlenp, newp, maxslp)); 204 205 case VM_USPACE: 206 return (sysctl_rdint(oldp, oldlenp, newp, USPACE)); 207 208 default: 209 return (EOPNOTSUPP); 210 } 211 /* NOTREACHED */ 212 } 213 214 static int 215 sysctl_uvmexp(oldp, oldlenp) 216 void *oldp; 217 size_t *oldlenp; 218 { 219 struct uvmexp_sysctl u; 220 221 memset(&u, 0, sizeof(u)); 222 223 /* Entries here are in order of uvmexp_sysctl, not uvmexp */ 224 u.pagesize = uvmexp.pagesize; 225 u.pagemask = uvmexp.pagemask; 226 u.pageshift = uvmexp.pageshift; 227 u.npages = uvmexp.npages; 228 u.free = uvmexp.free; 229 u.active = uvmexp.active; 230 u.inactive = uvmexp.inactive; 231 u.paging = uvmexp.paging; 232 u.wired = uvmexp.wired; 233 u.zeropages = uvmexp.zeropages; 234 u.reserve_pagedaemon = uvmexp.reserve_pagedaemon; 235 u.reserve_kernel = uvmexp.reserve_kernel; 236 u.freemin = uvmexp.freemin; 237 u.freetarg = uvmexp.freetarg; 238 u.inactarg = uvmexp.inactarg; 239 u.wiredmax = uvmexp.wiredmax; 240 u.nswapdev = uvmexp.nswapdev; 241 u.swpages = uvmexp.swpages; 242 u.swpginuse = uvmexp.swpginuse; 243 u.swpgonly = uvmexp.swpgonly; 244 u.nswget = uvmexp.nswget; 245 u.nanon = uvmexp.nanon; 246 u.nanonneeded = uvmexp.nanonneeded; 247 u.nfreeanon = uvmexp.nfreeanon; 248 u.faults = uvmexp.faults; 249 u.traps = uvmexp.traps; 250 u.intrs = uvmexp.intrs; 251 u.swtch = uvmexp.swtch; 252 u.softs = uvmexp.softs; 253 u.syscalls = uvmexp.syscalls; 254 u.pageins = uvmexp.pageins; 255 u.swapins = uvmexp.swapins; 256 u.swapouts = uvmexp.swapouts; 257 u.pgswapin = uvmexp.pgswapin; 258 u.pgswapout = uvmexp.pgswapout; 259 u.forks = uvmexp.forks; 260 u.forks_ppwait = uvmexp.forks_ppwait; 261 u.forks_sharevm = uvmexp.forks_sharevm; 262 u.pga_zerohit = uvmexp.pga_zerohit; 263 u.pga_zeromiss = uvmexp.pga_zeromiss; 264 u.zeroaborts = uvmexp.zeroaborts; 265 u.fltnoram = uvmexp.fltnoram; 266 u.fltnoanon = uvmexp.fltnoanon; 267 u.fltpgwait = uvmexp.fltpgwait; 268 u.fltpgrele = uvmexp.fltpgrele; 269 u.fltrelck = uvmexp.fltrelck; 270 u.fltrelckok = uvmexp.fltrelckok; 271 u.fltanget = uvmexp.fltanget; 272 u.fltanretry = uvmexp.fltanretry; 273 u.fltamcopy = uvmexp.fltamcopy; 274 u.fltnamap = uvmexp.fltnamap; 275 u.fltnomap = uvmexp.fltnomap; 276 u.fltlget = uvmexp.fltlget; 277 u.fltget = uvmexp.fltget; 278 u.flt_anon = uvmexp.flt_anon; 279 u.flt_acow = uvmexp.flt_acow; 280 u.flt_obj = uvmexp.flt_obj; 281 u.flt_prcopy = uvmexp.flt_prcopy; 282 u.flt_przero = uvmexp.flt_przero; 283 u.pdwoke = uvmexp.pdwoke; 284 u.pdrevs = uvmexp.pdrevs; 285 u.pdswout = uvmexp.pdswout; 286 u.pdfreed = uvmexp.pdfreed; 287 u.pdscans = uvmexp.pdscans; 288 u.pdanscan = uvmexp.pdanscan; 289 u.pdobscan = uvmexp.pdobscan; 290 u.pdreact = uvmexp.pdreact; 291 u.pdbusy = uvmexp.pdbusy; 292 u.pdpageouts = uvmexp.pdpageouts; 293 u.pdpending = uvmexp.pdpending; 294 u.pddeact = uvmexp.pddeact; 295 u.anonpages = uvmexp.anonpages; 296 u.vnodepages = uvmexp.vnodepages; 297 u.vtextpages = uvmexp.vtextpages; 298 u.colorhit = uvmexp.colorhit; 299 u.colormiss = uvmexp.colormiss; 300 301 return (sysctl_rdminstruct(oldp, oldlenp, NULL, &u, sizeof(u))); 302 } 303 304 /* 305 * uvm_total: calculate the current state of the system. 306 */ 307 static void 308 uvm_total(totalp) 309 struct vmtotal *totalp; 310 { 311 struct proc *p; 312 #if 0 313 struct vm_map_entry * entry; 314 struct vm_map *map; 315 int paging; 316 #endif 317 318 memset(totalp, 0, sizeof *totalp); 319 320 /* 321 * calculate process statistics 322 */ 323 324 proclist_lock_read(); 325 LIST_FOREACH(p, &allproc, p_list) { 326 if (p->p_flag & P_SYSTEM) 327 continue; 328 switch (p->p_stat) { 329 case 0: 330 continue; 331 332 case SSLEEP: 333 case SSTOP: 334 if (p->p_flag & P_INMEM) { 335 if (p->p_priority <= PZERO) 336 totalp->t_dw++; 337 else if (p->p_slptime < maxslp) 338 totalp->t_sl++; 339 } else if (p->p_slptime < maxslp) 340 totalp->t_sw++; 341 if (p->p_slptime >= maxslp) 342 continue; 343 break; 344 345 case SRUN: 346 case SONPROC: 347 case SIDL: 348 if (p->p_flag & P_INMEM) 349 totalp->t_rq++; 350 else 351 totalp->t_sw++; 352 if (p->p_stat == SIDL) 353 continue; 354 break; 355 } 356 /* 357 * note active objects 358 */ 359 #if 0 360 /* 361 * XXXCDC: BOGUS! rethink this. in the mean time 362 * don't do it. 363 */ 364 paging = 0; 365 vm_map_lock(map); 366 for (map = &p->p_vmspace->vm_map, entry = map->header.next; 367 entry != &map->header; entry = entry->next) { 368 if (entry->is_a_map || entry->is_sub_map || 369 entry->object.uvm_obj == NULL) 370 continue; 371 /* XXX how to do this with uvm */ 372 } 373 vm_map_unlock(map); 374 if (paging) 375 totalp->t_pw++; 376 #endif 377 } 378 proclist_unlock_read(); 379 /* 380 * Calculate object memory usage statistics. 381 */ 382 totalp->t_free = uvmexp.free; 383 totalp->t_vm = uvmexp.npages - uvmexp.free + uvmexp.swpginuse; 384 totalp->t_avm = uvmexp.active + uvmexp.swpginuse; /* XXX */ 385 totalp->t_rm = uvmexp.npages - uvmexp.free; 386 totalp->t_arm = uvmexp.active; 387 totalp->t_vmshr = 0; /* XXX */ 388 totalp->t_avmshr = 0; /* XXX */ 389 totalp->t_rmshr = 0; /* XXX */ 390 totalp->t_armshr = 0; /* XXX */ 391 } 392