1 /* $OpenBSD: uvm_meter.c,v 1.28 2009/06/17 00:13:59 oga Exp $ */ 2 /* $NetBSD: uvm_meter.c,v 1.21 2001/07/14 06:36:03 matt Exp $ */ 3 4 /* 5 * Copyright (c) 1997 Charles D. Cranor and Washington University. 6 * Copyright (c) 1982, 1986, 1989, 1993 7 * The Regents of the University of California. 8 * 9 * All rights reserved. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by Charles D. Cranor, 22 * Washington University, and the University of California, Berkeley 23 * and its contributors. 24 * 4. Neither the name of the University nor the names of its contributors 25 * may be used to endorse or promote products derived from this software 26 * without specific prior written permission. 27 * 28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 38 * SUCH DAMAGE. 39 * 40 * @(#)vm_meter.c 8.4 (Berkeley) 1/4/94 41 * from: Id: uvm_meter.c,v 1.1.2.1 1997/08/14 19:10:35 chuck Exp 42 */ 43 44 #include <sys/param.h> 45 #include <sys/proc.h> 46 #include <sys/systm.h> 47 #include <sys/kernel.h> 48 #include <uvm/uvm_extern.h> 49 #include <sys/sysctl.h> 50 #include <sys/exec.h> 51 52 #ifdef UVM_SWAP_ENCRYPT 53 #include <uvm/uvm_swap.h> 54 #include <uvm/uvm_swap_encrypt.h> 55 #endif 56 57 /* 58 * maxslp: ???? XXXCDC 59 */ 60 61 int maxslp = MAXSLP; /* patchable ... */ 62 struct loadavg averunnable; 63 64 /* 65 * constants for averages over 1, 5, and 15 minutes when sampling at 66 * 5 second intervals. 67 */ 68 69 static fixpt_t cexp[3] = { 70 0.9200444146293232 * FSCALE, /* exp(-1/12) */ 71 0.9834714538216174 * FSCALE, /* exp(-1/60) */ 72 0.9944598480048967 * FSCALE, /* exp(-1/180) */ 73 }; 74 75 /* 76 * prototypes 77 */ 78 79 static void uvm_loadav(struct loadavg *); 80 81 /* 82 * uvm_meter: calculate load average and wake up the swapper (if needed) 83 */ 84 void 85 uvm_meter(void) 86 { 87 if ((time_second % 5) == 0) 88 uvm_loadav(&averunnable); 89 if (proc0.p_slptime > (maxslp / 2)) 90 wakeup(&proc0); 91 } 92 93 /* 94 * uvm_loadav: compute a tenex style load average of a quantity on 95 * 1, 5, and 15 minute intervals. 96 */ 97 static void 98 uvm_loadav(struct loadavg *avg) 99 { 100 CPU_INFO_ITERATOR cii; 101 struct cpu_info *ci; 102 int i, nrun; 103 struct proc *p; 104 int nrun_cpu[MAXCPUS]; 105 106 nrun = 0; 107 memset(nrun_cpu, 0, sizeof(nrun_cpu)); 108 109 LIST_FOREACH(p, &allproc, p_list) { 110 switch (p->p_stat) { 111 case SSLEEP: 112 if (p->p_priority > PZERO || p->p_slptime > 1) 113 continue; 114 /* FALLTHROUGH */ 115 case SRUN: 116 case SONPROC: 117 if (p == p->p_cpu->ci_schedstate.spc_idleproc) 118 continue; 119 case SIDL: 120 nrun++; 121 if (p->p_cpu) 122 nrun_cpu[CPU_INFO_UNIT(p->p_cpu)]++; 123 } 124 } 125 126 for (i = 0; i < 3; i++) { 127 avg->ldavg[i] = (cexp[i] * avg->ldavg[i] + 128 nrun * FSCALE * (FSCALE - cexp[i])) >> FSHIFT; 129 } 130 131 CPU_INFO_FOREACH(cii, ci) { 132 struct schedstate_percpu *spc = &ci->ci_schedstate; 133 134 if (nrun_cpu[CPU_INFO_UNIT(ci)] == 0) 135 continue; 136 spc->spc_ldavg = (cexp[0] * spc->spc_ldavg + 137 nrun_cpu[CPU_INFO_UNIT(ci)] * FSCALE * 138 (FSCALE - cexp[0])) >> FSHIFT; 139 } 140 } 141 142 /* 143 * uvm_sysctl: sysctl hook into UVM system. 144 */ 145 int 146 uvm_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp, 147 size_t newlen, struct proc *p) 148 { 149 struct vmtotal vmtotals; 150 int rv, t; 151 struct _ps_strings _ps = { PS_STRINGS }; 152 extern int uvm_km_pages_free; 153 154 switch (name[0]) { 155 case VM_SWAPENCRYPT: 156 #ifdef UVM_SWAP_ENCRYPT 157 return (swap_encrypt_ctl(name + 1, namelen - 1, oldp, oldlenp, 158 newp, newlen, p)); 159 #else 160 return (EOPNOTSUPP); 161 #endif 162 default: 163 /* all sysctl names at this level are terminal */ 164 if (namelen != 1) 165 return (ENOTDIR); /* overloaded */ 166 break; 167 } 168 169 switch (name[0]) { 170 case VM_LOADAVG: 171 return (sysctl_rdstruct(oldp, oldlenp, newp, &averunnable, 172 sizeof(averunnable))); 173 174 case VM_METER: 175 uvm_total(&vmtotals); 176 return (sysctl_rdstruct(oldp, oldlenp, newp, &vmtotals, 177 sizeof(vmtotals))); 178 179 case VM_UVMEXP: 180 return (sysctl_rdstruct(oldp, oldlenp, newp, &uvmexp, 181 sizeof(uvmexp))); 182 183 case VM_NKMEMPAGES: 184 return (sysctl_rdint(oldp, oldlenp, newp, nkmempages)); 185 186 case VM_PSSTRINGS: 187 return (sysctl_rdstruct(oldp, oldlenp, newp, &_ps, 188 sizeof(_ps))); 189 case VM_ANONMIN: 190 t = uvmexp.anonminpct; 191 rv = sysctl_int(oldp, oldlenp, newp, newlen, &t); 192 if (rv) { 193 return rv; 194 } 195 if (t + uvmexp.vtextminpct + uvmexp.vnodeminpct > 95 || t < 0) { 196 return EINVAL; 197 } 198 uvmexp.anonminpct = t; 199 uvmexp.anonmin = t * 256 / 100; 200 return rv; 201 202 case VM_VTEXTMIN: 203 t = uvmexp.vtextminpct; 204 rv = sysctl_int(oldp, oldlenp, newp, newlen, &t); 205 if (rv) { 206 return rv; 207 } 208 if (uvmexp.anonminpct + t + uvmexp.vnodeminpct > 95 || t < 0) { 209 return EINVAL; 210 } 211 uvmexp.vtextminpct = t; 212 uvmexp.vtextmin = t * 256 / 100; 213 return rv; 214 215 case VM_VNODEMIN: 216 t = uvmexp.vnodeminpct; 217 rv = sysctl_int(oldp, oldlenp, newp, newlen, &t); 218 if (rv) { 219 return rv; 220 } 221 if (uvmexp.anonminpct + uvmexp.vtextminpct + t > 95 || t < 0) { 222 return EINVAL; 223 } 224 uvmexp.vnodeminpct = t; 225 uvmexp.vnodemin = t * 256 / 100; 226 return rv; 227 228 case VM_MAXSLP: 229 return (sysctl_rdint(oldp, oldlenp, newp, maxslp)); 230 231 case VM_USPACE: 232 return (sysctl_rdint(oldp, oldlenp, newp, USPACE)); 233 234 case VM_KMPAGESFREE: 235 return (sysctl_rdint(oldp, oldlenp, newp, uvm_km_pages_free)); 236 237 default: 238 return (EOPNOTSUPP); 239 } 240 /* NOTREACHED */ 241 } 242 243 /* 244 * uvm_total: calculate the current state of the system. 245 */ 246 void 247 uvm_total(struct vmtotal *totalp) 248 { 249 struct proc *p; 250 #if 0 251 struct vm_map_entry * entry; 252 struct vm_map *map; 253 int paging; 254 #endif 255 256 memset(totalp, 0, sizeof *totalp); 257 258 /* 259 * calculate process statistics 260 */ 261 262 LIST_FOREACH(p, &allproc, p_list) { 263 if (p->p_flag & P_SYSTEM) 264 continue; 265 switch (p->p_stat) { 266 case 0: 267 continue; 268 269 case SSLEEP: 270 case SSTOP: 271 if (p->p_priority <= PZERO) 272 totalp->t_dw++; 273 else if (p->p_slptime < maxslp) 274 totalp->t_sl++; 275 if (p->p_slptime >= maxslp) 276 continue; 277 break; 278 279 case SRUN: 280 case SIDL: 281 case SONPROC: 282 totalp->t_rq++; 283 if (p->p_stat == SIDL) 284 continue; 285 break; 286 } 287 /* 288 * note active objects 289 */ 290 #if 0 291 /* 292 * XXXCDC: BOGUS! rethink this. in the mean time 293 * don't do it. 294 */ 295 paging = 0; 296 vm_map_lock(map); 297 for (map = &p->p_vmspace->vm_map, entry = map->header.next; 298 entry != &map->header; entry = entry->next) { 299 if (entry->is_a_map || entry->is_sub_map || 300 entry->object.uvm_obj == NULL) 301 continue; 302 /* XXX how to do this with uvm */ 303 } 304 vm_map_unlock(map); 305 if (paging) 306 totalp->t_pw++; 307 #endif 308 } 309 /* 310 * Calculate object memory usage statistics. 311 */ 312 totalp->t_free = uvmexp.free; 313 totalp->t_vm = uvmexp.npages - uvmexp.free + uvmexp.swpginuse; 314 totalp->t_avm = uvmexp.active + uvmexp.swpginuse; /* XXX */ 315 totalp->t_rm = uvmexp.npages - uvmexp.free; 316 totalp->t_arm = uvmexp.active; 317 totalp->t_vmshr = 0; /* XXX */ 318 totalp->t_avmshr = 0; /* XXX */ 319 totalp->t_rmshr = 0; /* XXX */ 320 totalp->t_armshr = 0; /* XXX */ 321 } 322