1 /* 2 * Copyright (c) 1982, 1986, 1989, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * @(#)vm_meter.c 8.4 (Berkeley) 1/4/94 34 * $FreeBSD$ 35 */ 36 37 #include <sys/param.h> 38 #include <sys/systm.h> 39 #include <sys/kernel.h> 40 #include <sys/lock.h> 41 #include <sys/mutex.h> 42 #include <sys/proc.h> 43 #include <sys/resource.h> 44 #include <sys/sx.h> 45 #include <sys/vmmeter.h> 46 47 #include <vm/vm.h> 48 #include <vm/vm_page.h> 49 #include <vm/vm_extern.h> 50 #include <vm/vm_param.h> 51 #include <vm/pmap.h> 52 #include <vm/vm_map.h> 53 #include <vm/vm_object.h> 54 #include <sys/sysctl.h> 55 56 struct loadavg averunnable = 57 { {0, 0, 0}, FSCALE }; /* load average, of runnable procs */ 58 59 struct vmmeter cnt; 60 61 int maxslp = MAXSLP; 62 63 /* 64 * Constants for averages over 1, 5, and 15 minutes 65 * when sampling at 5 second intervals. 66 */ 67 static 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 * Compute a tenex style load average of a quantity on 75 * 1, 5 and 15 minute intervals. 76 */ 77 static void 78 loadav(struct loadavg *avg) 79 { 80 int i, nrun; 81 struct proc *p; 82 83 sx_slock(&allproc_lock); 84 for (nrun = 0, p = LIST_FIRST(&allproc); p != 0; p = LIST_NEXT(p, p_list)) { 85 switch (p->p_stat) { 86 case SSLEEP: 87 if (p->p_pri.pri_level > PZERO || 88 p->p_slptime != 0) 89 continue; 90 /* FALLTHROUGH */ 91 case SRUN: 92 if ((p->p_flag & P_NOLOAD) != 0) 93 continue; 94 /* FALLTHROUGH */ 95 case SIDL: 96 nrun++; 97 } 98 } 99 sx_sunlock(&allproc_lock); 100 for (i = 0; i < 3; i++) 101 avg->ldavg[i] = (cexp[i] * avg->ldavg[i] + 102 nrun * FSCALE * (FSCALE - cexp[i])) >> FSHIFT; 103 } 104 105 void 106 vmmeter() 107 { 108 109 if (time_second % 5 == 0) 110 loadav(&averunnable); 111 } 112 113 SYSCTL_UINT(_vm, VM_V_FREE_MIN, v_free_min, 114 CTLFLAG_RW, &cnt.v_free_min, 0, ""); 115 SYSCTL_UINT(_vm, VM_V_FREE_TARGET, v_free_target, 116 CTLFLAG_RW, &cnt.v_free_target, 0, ""); 117 SYSCTL_UINT(_vm, VM_V_FREE_RESERVED, v_free_reserved, 118 CTLFLAG_RW, &cnt.v_free_reserved, 0, ""); 119 SYSCTL_UINT(_vm, VM_V_INACTIVE_TARGET, v_inactive_target, 120 CTLFLAG_RW, &cnt.v_inactive_target, 0, ""); 121 SYSCTL_UINT(_vm, VM_V_CACHE_MIN, v_cache_min, 122 CTLFLAG_RW, &cnt.v_cache_min, 0, ""); 123 SYSCTL_UINT(_vm, VM_V_CACHE_MAX, v_cache_max, 124 CTLFLAG_RW, &cnt.v_cache_max, 0, ""); 125 SYSCTL_UINT(_vm, VM_V_PAGEOUT_FREE_MIN, v_pageout_free_min, 126 CTLFLAG_RW, &cnt.v_pageout_free_min, 0, ""); 127 SYSCTL_UINT(_vm, OID_AUTO, v_free_severe, 128 CTLFLAG_RW, &cnt.v_free_severe, 0, ""); 129 130 SYSCTL_STRUCT(_vm, VM_LOADAVG, loadavg, CTLFLAG_RD, 131 &averunnable, loadavg, "Machine loadaverage history"); 132 133 static int 134 vmtotal(SYSCTL_HANDLER_ARGS) 135 { 136 struct proc *p; 137 struct vmtotal total, *totalp; 138 vm_map_entry_t entry; 139 vm_object_t object; 140 vm_map_t map; 141 int paging; 142 143 totalp = &total; 144 bzero(totalp, sizeof *totalp); 145 /* 146 * Mark all objects as inactive. 147 */ 148 GIANT_REQUIRED; 149 TAILQ_FOREACH(object, &vm_object_list, object_list) 150 vm_object_clear_flag(object, OBJ_ACTIVE); 151 /* 152 * Calculate process statistics. 153 */ 154 sx_slock(&allproc_lock); 155 LIST_FOREACH(p, &allproc, p_list) { 156 if (p->p_flag & P_SYSTEM) 157 continue; 158 mtx_lock_spin(&sched_lock); 159 switch (p->p_stat) { 160 case 0: 161 mtx_unlock_spin(&sched_lock); 162 continue; 163 164 case SMTX: 165 case SSLEEP: 166 case SSTOP: 167 if (p->p_sflag & PS_INMEM) { 168 if (p->p_pri.pri_level <= PZERO) 169 totalp->t_dw++; 170 else if (p->p_slptime < maxslp) 171 totalp->t_sl++; 172 } else if (p->p_slptime < maxslp) 173 totalp->t_sw++; 174 if (p->p_slptime >= maxslp) { 175 mtx_unlock_spin(&sched_lock); 176 continue; 177 } 178 break; 179 180 case SWAIT: 181 totalp->t_sl++; 182 continue; 183 184 case SRUN: 185 case SIDL: 186 if (p->p_sflag & PS_INMEM) 187 totalp->t_rq++; 188 else 189 totalp->t_sw++; 190 if (p->p_stat == SIDL) { 191 mtx_unlock_spin(&sched_lock); 192 continue; 193 } 194 break; 195 } 196 mtx_unlock_spin(&sched_lock); 197 /* 198 * Note active objects. 199 */ 200 paging = 0; 201 for (map = &p->p_vmspace->vm_map, entry = map->header.next; 202 entry != &map->header; entry = entry->next) { 203 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) || 204 entry->object.vm_object == NULL) 205 continue; 206 vm_object_set_flag(entry->object.vm_object, OBJ_ACTIVE); 207 paging |= entry->object.vm_object->paging_in_progress; 208 } 209 if (paging) 210 totalp->t_pw++; 211 } 212 sx_sunlock(&allproc_lock); 213 /* 214 * Calculate object memory usage statistics. 215 */ 216 TAILQ_FOREACH(object, &vm_object_list, object_list) { 217 /* 218 * devices, like /dev/mem, will badly skew our totals 219 */ 220 if (object->type == OBJT_DEVICE) 221 continue; 222 totalp->t_vm += object->size; 223 totalp->t_rm += object->resident_page_count; 224 if (object->flags & OBJ_ACTIVE) { 225 totalp->t_avm += object->size; 226 totalp->t_arm += object->resident_page_count; 227 } 228 if (object->shadow_count > 1) { 229 /* shared object */ 230 totalp->t_vmshr += object->size; 231 totalp->t_rmshr += object->resident_page_count; 232 if (object->flags & OBJ_ACTIVE) { 233 totalp->t_avmshr += object->size; 234 totalp->t_armshr += object->resident_page_count; 235 } 236 } 237 } 238 totalp->t_free = cnt.v_free_count + cnt.v_cache_count; 239 return (sysctl_handle_opaque(oidp, totalp, sizeof total, req)); 240 } 241 242 SYSCTL_PROC(_vm, VM_METER, vmmeter, CTLTYPE_OPAQUE|CTLFLAG_RD, 243 0, sizeof(struct vmtotal), vmtotal, "S,vmtotal", 244 "System virtual memory statistics"); 245 SYSCTL_NODE(_vm, OID_AUTO, stats, CTLFLAG_RW, 0, "VM meter stats"); 246 SYSCTL_NODE(_vm_stats, OID_AUTO, sys, CTLFLAG_RW, 0, "VM meter sys stats"); 247 SYSCTL_NODE(_vm_stats, OID_AUTO, vm, CTLFLAG_RW, 0, "VM meter vm stats"); 248 SYSCTL_NODE(_vm_stats, OID_AUTO, misc, CTLFLAG_RW, 0, "VM meter misc stats"); 249 SYSCTL_UINT(_vm_stats_sys, OID_AUTO, 250 v_swtch, CTLFLAG_RD, &cnt.v_swtch, 0, "Context switches"); 251 SYSCTL_UINT(_vm_stats_sys, OID_AUTO, 252 v_trap, CTLFLAG_RD, &cnt.v_trap, 0, "Traps"); 253 SYSCTL_UINT(_vm_stats_sys, OID_AUTO, 254 v_syscall, CTLFLAG_RD, &cnt.v_syscall, 0, "Syscalls"); 255 SYSCTL_UINT(_vm_stats_sys, OID_AUTO, v_intr, CTLFLAG_RD, 256 &cnt.v_intr, 0, "Hardware interrupts"); 257 SYSCTL_UINT(_vm_stats_sys, OID_AUTO, v_soft, CTLFLAG_RD, 258 &cnt.v_soft, 0, "Software interrupts"); 259 SYSCTL_UINT(_vm_stats_vm, OID_AUTO, 260 v_vm_faults, CTLFLAG_RD, &cnt.v_vm_faults, 0, "VM faults"); 261 SYSCTL_UINT(_vm_stats_vm, OID_AUTO, 262 v_cow_faults, CTLFLAG_RD, &cnt.v_cow_faults, 0, "COW faults"); 263 SYSCTL_UINT(_vm_stats_vm, OID_AUTO, 264 v_cow_optim, CTLFLAG_RD, &cnt.v_cow_optim, 0, "Optimized COW faults"); 265 SYSCTL_UINT(_vm_stats_vm, OID_AUTO, 266 v_zfod, CTLFLAG_RD, &cnt.v_zfod, 0, "Zero fill"); 267 SYSCTL_UINT(_vm_stats_vm, OID_AUTO, 268 v_ozfod, CTLFLAG_RD, &cnt.v_ozfod, 0, "Optimized zero fill"); 269 SYSCTL_UINT(_vm_stats_vm, OID_AUTO, 270 v_swapin, CTLFLAG_RD, &cnt.v_swapin, 0, "Swapin operations"); 271 SYSCTL_UINT(_vm_stats_vm, OID_AUTO, 272 v_swapout, CTLFLAG_RD, &cnt.v_swapout, 0, "Swapout operations"); 273 SYSCTL_UINT(_vm_stats_vm, OID_AUTO, 274 v_swappgsin, CTLFLAG_RD, &cnt.v_swappgsin, 0, "Swapin pages"); 275 SYSCTL_UINT(_vm_stats_vm, OID_AUTO, 276 v_swappgsout, CTLFLAG_RD, &cnt.v_swappgsout, 0, "Swapout pages"); 277 SYSCTL_UINT(_vm_stats_vm, OID_AUTO, 278 v_vnodein, CTLFLAG_RD, &cnt.v_vnodein, 0, "Vnodein operations"); 279 SYSCTL_UINT(_vm_stats_vm, OID_AUTO, 280 v_vnodeout, CTLFLAG_RD, &cnt.v_vnodeout, 0, "Vnodeout operations"); 281 SYSCTL_UINT(_vm_stats_vm, OID_AUTO, 282 v_vnodepgsin, CTLFLAG_RD, &cnt.v_vnodepgsin, 0, "Vnodein pages"); 283 SYSCTL_UINT(_vm_stats_vm, OID_AUTO, 284 v_vnodepgsout, CTLFLAG_RD, &cnt.v_vnodepgsout, 0, "Vnodeout pages"); 285 SYSCTL_UINT(_vm_stats_vm, OID_AUTO, 286 v_intrans, CTLFLAG_RD, &cnt.v_intrans, 0, "In transit page blocking"); 287 SYSCTL_UINT(_vm_stats_vm, OID_AUTO, 288 v_reactivated, CTLFLAG_RD, &cnt.v_reactivated, 0, "Reactivated pages"); 289 SYSCTL_UINT(_vm_stats_vm, OID_AUTO, 290 v_pdwakeups, CTLFLAG_RD, &cnt.v_pdwakeups, 0, "Pagedaemon wakeups"); 291 SYSCTL_UINT(_vm_stats_vm, OID_AUTO, 292 v_pdpages, CTLFLAG_RD, &cnt.v_pdpages, 0, "Pagedaemon page scans"); 293 SYSCTL_UINT(_vm_stats_vm, OID_AUTO, 294 v_dfree, CTLFLAG_RD, &cnt.v_dfree, 0, ""); 295 SYSCTL_UINT(_vm_stats_vm, OID_AUTO, 296 v_pfree, CTLFLAG_RD, &cnt.v_pfree, 0, ""); 297 SYSCTL_UINT(_vm_stats_vm, OID_AUTO, 298 v_tfree, CTLFLAG_RD, &cnt.v_tfree, 0, ""); 299 SYSCTL_UINT(_vm_stats_vm, OID_AUTO, 300 v_page_size, CTLFLAG_RD, &cnt.v_page_size, 0, ""); 301 SYSCTL_UINT(_vm_stats_vm, OID_AUTO, 302 v_page_count, CTLFLAG_RD, &cnt.v_page_count, 0, ""); 303 SYSCTL_UINT(_vm_stats_vm, OID_AUTO, 304 v_free_reserved, CTLFLAG_RD, &cnt.v_free_reserved, 0, ""); 305 SYSCTL_UINT(_vm_stats_vm, OID_AUTO, 306 v_free_target, CTLFLAG_RD, &cnt.v_free_target, 0, ""); 307 SYSCTL_UINT(_vm_stats_vm, OID_AUTO, 308 v_free_min, CTLFLAG_RD, &cnt.v_free_min, 0, ""); 309 SYSCTL_UINT(_vm_stats_vm, OID_AUTO, 310 v_free_count, CTLFLAG_RD, &cnt.v_free_count, 0, ""); 311 SYSCTL_UINT(_vm_stats_vm, OID_AUTO, 312 v_wire_count, CTLFLAG_RD, &cnt.v_wire_count, 0, ""); 313 SYSCTL_UINT(_vm_stats_vm, OID_AUTO, 314 v_active_count, CTLFLAG_RD, &cnt.v_active_count, 0, ""); 315 SYSCTL_UINT(_vm_stats_vm, OID_AUTO, 316 v_inactive_target, CTLFLAG_RD, &cnt.v_inactive_target, 0, ""); 317 SYSCTL_UINT(_vm_stats_vm, OID_AUTO, 318 v_inactive_count, CTLFLAG_RD, &cnt.v_inactive_count, 0, ""); 319 SYSCTL_UINT(_vm_stats_vm, OID_AUTO, 320 v_cache_count, CTLFLAG_RD, &cnt.v_cache_count, 0, ""); 321 SYSCTL_UINT(_vm_stats_vm, OID_AUTO, 322 v_cache_min, CTLFLAG_RD, &cnt.v_cache_min, 0, ""); 323 SYSCTL_UINT(_vm_stats_vm, OID_AUTO, 324 v_cache_max, CTLFLAG_RD, &cnt.v_cache_max, 0, ""); 325 SYSCTL_UINT(_vm_stats_vm, OID_AUTO, 326 v_pageout_free_min, CTLFLAG_RD, &cnt.v_pageout_free_min, 0, ""); 327 SYSCTL_UINT(_vm_stats_vm, OID_AUTO, 328 v_interrupt_free_min, CTLFLAG_RD, &cnt.v_interrupt_free_min, 0, ""); 329 SYSCTL_INT(_vm_stats_misc, OID_AUTO, 330 zero_page_count, CTLFLAG_RD, &vm_page_zero_count, 0, ""); 331 SYSCTL_UINT(_vm_stats_vm, OID_AUTO, 332 v_forks, CTLFLAG_RD, &cnt.v_forks, 0, "Number of fork() calls"); 333 SYSCTL_UINT(_vm_stats_vm, OID_AUTO, 334 v_vforks, CTLFLAG_RD, &cnt.v_vforks, 0, "Number of vfork() calls"); 335 SYSCTL_UINT(_vm_stats_vm, OID_AUTO, 336 v_rforks, CTLFLAG_RD, &cnt.v_rforks, 0, "Number of rfork() calls"); 337 SYSCTL_UINT(_vm_stats_vm, OID_AUTO, 338 v_kthreads, CTLFLAG_RD, &cnt.v_kthreads, 0, "Number of fork() calls by kernel"); 339 SYSCTL_UINT(_vm_stats_vm, OID_AUTO, 340 v_forkpages, CTLFLAG_RD, &cnt.v_forkpages, 0, "VM pages affected by fork()"); 341 SYSCTL_UINT(_vm_stats_vm, OID_AUTO, 342 v_vforkpages, CTLFLAG_RD, &cnt.v_vforkpages, 0, "VM pages affected by vfork()"); 343 SYSCTL_UINT(_vm_stats_vm, OID_AUTO, 344 v_rforkpages, CTLFLAG_RD, &cnt.v_rforkpages, 0, "VM pages affected by rfork()"); 345 SYSCTL_UINT(_vm_stats_vm, OID_AUTO, 346 v_kthreadpages, CTLFLAG_RD, &cnt.v_kthreadpages, 0, "VM pages affected by fork() by kernel"); 347 #if 0 348 SYSCTL_INT(_vm_stats_misc, OID_AUTO, 349 page_mask, CTLFLAG_RD, &page_mask, 0, ""); 350 SYSCTL_INT(_vm_stats_misc, OID_AUTO, 351 page_shift, CTLFLAG_RD, &page_shift, 0, ""); 352 SYSCTL_INT(_vm_stats_misc, OID_AUTO, 353 first_page, CTLFLAG_RD, &first_page, 0, ""); 354 SYSCTL_INT(_vm_stats_misc, OID_AUTO, 355 last_page, CTLFLAG_RD, &last_page, 0, ""); 356 SYSCTL_INT(_vm_stats_misc, OID_AUTO, 357 vm_page_bucket_count, CTLFLAG_RD, &vm_page_bucket_count, 0, ""); 358 SYSCTL_INT(_vm_stats_misc, OID_AUTO, 359 vm_page_hash_mask, CTLFLAG_RD, &vm_page_hash_mask, 0, ""); 360 #endif 361