vm_meter.c revision 58634
11541Srgrimes/* 21541Srgrimes * Copyright (c) 1982, 1986, 1989, 1993 31541Srgrimes * The Regents of the University of California. All rights reserved. 41541Srgrimes * 51541Srgrimes * Redistribution and use in source and binary forms, with or without 61541Srgrimes * modification, are permitted provided that the following conditions 71541Srgrimes * are met: 81541Srgrimes * 1. Redistributions of source code must retain the above copyright 91541Srgrimes * notice, this list of conditions and the following disclaimer. 101541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111541Srgrimes * notice, this list of conditions and the following disclaimer in the 121541Srgrimes * documentation and/or other materials provided with the distribution. 131541Srgrimes * 3. All advertising materials mentioning features or use of this software 1458634Scharnier * must display the following acknowledgment: 151541Srgrimes * This product includes software developed by the University of 161541Srgrimes * California, Berkeley and its contributors. 171541Srgrimes * 4. Neither the name of the University nor the names of its contributors 181541Srgrimes * may be used to endorse or promote products derived from this software 191541Srgrimes * without specific prior written permission. 201541Srgrimes * 211541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 221541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 231541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 241541Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 251541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 261541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 271541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 281541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 291541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 301541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 311541Srgrimes * SUCH DAMAGE. 321541Srgrimes * 331541Srgrimes * @(#)vm_meter.c 8.4 (Berkeley) 1/4/94 3450477Speter * $FreeBSD: head/sys/vm/vm_meter.c 58634 2000-03-26 15:20:23Z charnier $ 351541Srgrimes */ 361541Srgrimes 371541Srgrimes#include <sys/param.h> 381541Srgrimes#include <sys/proc.h> 391541Srgrimes#include <sys/systm.h> 401541Srgrimes#include <sys/kernel.h> 4134924Sbde#include <sys/resource.h> 4212662Sdg#include <sys/vmmeter.h> 4312662Sdg 441541Srgrimes#include <vm/vm.h> 4540794Speter#include <vm/vm_page.h> 4612726Sbde#include <vm/vm_extern.h> 4712662Sdg#include <vm/vm_param.h> 4822521Sdyson#include <sys/lock.h> 4912662Sdg#include <vm/pmap.h> 5012662Sdg#include <vm/vm_map.h> 5112662Sdg#include <vm/vm_object.h> 521541Srgrimes#include <sys/sysctl.h> 531541Srgrimes 5412623Sphkstruct loadavg averunnable = 5512623Sphk { {0, 0, 0}, FSCALE }; /* load average, of runnable procs */ 5612623Sphk 579759Sbdestruct vmmeter cnt; 581541Srgrimes 5912820Sphkstatic int maxslp = MAXSLP; 601541Srgrimes 611541Srgrimes/* 621541Srgrimes * Constants for averages over 1, 5, and 15 minutes 631541Srgrimes * when sampling at 5 second intervals. 641541Srgrimes */ 6512820Sphkstatic fixpt_t cexp[3] = { 661541Srgrimes 0.9200444146293232 * FSCALE, /* exp(-1/12) */ 671541Srgrimes 0.9834714538216174 * FSCALE, /* exp(-1/60) */ 681541Srgrimes 0.9944598480048967 * FSCALE, /* exp(-1/180) */ 691541Srgrimes}; 701541Srgrimes 711541Srgrimes/* 721541Srgrimes * Compute a tenex style load average of a quantity on 731541Srgrimes * 1, 5 and 15 minute intervals. 741541Srgrimes */ 759507Sdgstatic void 7612286Sphkloadav(struct loadavg *avg) 771541Srgrimes{ 781541Srgrimes register int i, nrun; 791541Srgrimes register struct proc *p; 801541Srgrimes 8114531Shsu for (nrun = 0, p = allproc.lh_first; p != 0; p = p->p_list.le_next) { 821541Srgrimes switch (p->p_stat) { 831541Srgrimes case SSLEEP: 841541Srgrimes if (p->p_priority > PZERO || p->p_slptime != 0) 851541Srgrimes continue; 8658634Scharnier /* FALLTHROUGH */ 871541Srgrimes case SRUN: 881541Srgrimes case SIDL: 891541Srgrimes nrun++; 901541Srgrimes } 911541Srgrimes } 921541Srgrimes for (i = 0; i < 3; i++) 931541Srgrimes avg->ldavg[i] = (cexp[i] * avg->ldavg[i] + 945455Sdg nrun * FSCALE * (FSCALE - cexp[i])) >> FSHIFT; 951541Srgrimes} 961541Srgrimes 979507Sdgvoid 989507Sdgvmmeter() 999507Sdg{ 1009507Sdg 10134961Sphk if (time_second % 5 == 0) 1029507Sdg loadav(&averunnable); 1039507Sdg if (proc0.p_slptime > maxslp / 2) 1049507Sdg wakeup(&proc0); 1059507Sdg} 1069507Sdg 10712286SphkSYSCTL_INT(_vm, VM_V_FREE_MIN, v_free_min, 10812286Sphk CTLFLAG_RW, &cnt.v_free_min, 0, ""); 10912286SphkSYSCTL_INT(_vm, VM_V_FREE_TARGET, v_free_target, 11012286Sphk CTLFLAG_RW, &cnt.v_free_target, 0, ""); 11112286SphkSYSCTL_INT(_vm, VM_V_FREE_RESERVED, v_free_reserved, 11212286Sphk CTLFLAG_RW, &cnt.v_free_reserved, 0, ""); 11312286SphkSYSCTL_INT(_vm, VM_V_INACTIVE_TARGET, v_inactive_target, 11412286Sphk CTLFLAG_RW, &cnt.v_inactive_target, 0, ""); 11512286SphkSYSCTL_INT(_vm, VM_V_CACHE_MIN, v_cache_min, 11612286Sphk CTLFLAG_RW, &cnt.v_cache_min, 0, ""); 11712286SphkSYSCTL_INT(_vm, VM_V_CACHE_MAX, v_cache_max, 11812286Sphk CTLFLAG_RW, &cnt.v_cache_max, 0, ""); 11912286SphkSYSCTL_INT(_vm, VM_V_PAGEOUT_FREE_MIN, v_pageout_free_min, 12012286Sphk CTLFLAG_RW, &cnt.v_pageout_free_min, 0, ""); 12151337SdillonSYSCTL_INT(_vm, OID_AUTO, v_free_severe, 12251337Sdillon CTLFLAG_RW, &cnt.v_free_severe, 0, ""); 12312286Sphk 12446381SbillfSYSCTL_STRUCT(_vm, VM_LOADAVG, loadavg, CTLFLAG_RD, 12546381Sbillf &averunnable, loadavg, "Machine loadaverage history"); 1261541Srgrimes 12712286Sphkstatic int 12812286Sphkvmtotal SYSCTL_HANDLER_ARGS 1291541Srgrimes{ 13012286Sphk struct proc *p; 13112286Sphk struct vmtotal total, *totalp; 13212286Sphk vm_map_entry_t entry; 13312286Sphk vm_object_t object; 13412286Sphk vm_map_t map; 1351541Srgrimes int paging; 1361541Srgrimes 13712286Sphk totalp = &total; 1381541Srgrimes bzero(totalp, sizeof *totalp); 1391541Srgrimes /* 1401541Srgrimes * Mark all objects as inactive. 1411541Srgrimes */ 14215809Sdyson for (object = TAILQ_FIRST(&vm_object_list); 1435455Sdg object != NULL; 14415809Sdyson object = TAILQ_NEXT(object,object_list)) 14538517Sdfr vm_object_clear_flag(object, OBJ_ACTIVE); 1461541Srgrimes /* 1471541Srgrimes * Calculate process statistics. 1481541Srgrimes */ 14914531Shsu for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) { 1501541Srgrimes if (p->p_flag & P_SYSTEM) 1511541Srgrimes continue; 1521541Srgrimes switch (p->p_stat) { 1531541Srgrimes case 0: 1541541Srgrimes continue; 1551541Srgrimes 1561541Srgrimes case SSLEEP: 1571541Srgrimes case SSTOP: 1581541Srgrimes if (p->p_flag & P_INMEM) { 1591541Srgrimes if (p->p_priority <= PZERO) 1601541Srgrimes totalp->t_dw++; 1611541Srgrimes else if (p->p_slptime < maxslp) 1621541Srgrimes totalp->t_sl++; 1631541Srgrimes } else if (p->p_slptime < maxslp) 1641541Srgrimes totalp->t_sw++; 1651541Srgrimes if (p->p_slptime >= maxslp) 1661541Srgrimes continue; 1671541Srgrimes break; 1681541Srgrimes 1691541Srgrimes case SRUN: 1701541Srgrimes case SIDL: 1711541Srgrimes if (p->p_flag & P_INMEM) 1721541Srgrimes totalp->t_rq++; 1731541Srgrimes else 1741541Srgrimes totalp->t_sw++; 1751541Srgrimes if (p->p_stat == SIDL) 1761541Srgrimes continue; 1771541Srgrimes break; 1781541Srgrimes } 1791541Srgrimes /* 1801541Srgrimes * Note active objects. 1811541Srgrimes */ 1821541Srgrimes paging = 0; 1831541Srgrimes for (map = &p->p_vmspace->vm_map, entry = map->header.next; 1845455Sdg entry != &map->header; entry = entry->next) { 18543748Sdillon if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) || 1861541Srgrimes entry->object.vm_object == NULL) 1871541Srgrimes continue; 18838517Sdfr vm_object_set_flag(entry->object.vm_object, OBJ_ACTIVE); 1891541Srgrimes paging |= entry->object.vm_object->paging_in_progress; 1901541Srgrimes } 1911541Srgrimes if (paging) 1921541Srgrimes totalp->t_pw++; 1931541Srgrimes } 1941541Srgrimes /* 1951541Srgrimes * Calculate object memory usage statistics. 1961541Srgrimes */ 19715809Sdyson for (object = TAILQ_FIRST(&vm_object_list); 1985455Sdg object != NULL; 19915809Sdyson object = TAILQ_NEXT(object, object_list)) { 20042957Sdillon /* 20142957Sdillon * devices, like /dev/mem, will badly skew our totals 20242957Sdillon */ 20342957Sdillon if (object->type == OBJT_DEVICE) 20442957Sdillon continue; 20518169Sdyson totalp->t_vm += object->size; 2061541Srgrimes totalp->t_rm += object->resident_page_count; 2071541Srgrimes if (object->flags & OBJ_ACTIVE) { 20818169Sdyson totalp->t_avm += object->size; 2091541Srgrimes totalp->t_arm += object->resident_page_count; 2101541Srgrimes } 21118169Sdyson if (object->shadow_count > 1) { 2121541Srgrimes /* shared object */ 21318169Sdyson totalp->t_vmshr += object->size; 2141541Srgrimes totalp->t_rmshr += object->resident_page_count; 2151541Srgrimes if (object->flags & OBJ_ACTIVE) { 21618169Sdyson totalp->t_avmshr += object->size; 2171541Srgrimes totalp->t_armshr += object->resident_page_count; 2181541Srgrimes } 2191541Srgrimes } 2201541Srgrimes } 2215455Sdg totalp->t_free = cnt.v_free_count + cnt.v_cache_count; 22212286Sphk return (sysctl_handle_opaque(oidp, totalp, sizeof total, req)); 2231541Srgrimes} 22412286Sphk 22512286SphkSYSCTL_PROC(_vm, VM_METER, vmmeter, CTLTYPE_OPAQUE|CTLFLAG_RD, 22646381Sbillf 0, sizeof(struct vmtotal), vmtotal, "S,vmtotal", 22746381Sbillf "System virtual memory statistics"); 22840794SpeterSYSCTL_NODE(_vm, OID_AUTO, stats, CTLFLAG_RW, 0, "VM meter stats"); 22940794SpeterSYSCTL_NODE(_vm_stats, OID_AUTO, sys, CTLFLAG_RW, 0, "VM meter sys stats"); 23040794SpeterSYSCTL_NODE(_vm_stats, OID_AUTO, vm, CTLFLAG_RW, 0, "VM meter vm stats"); 23140794SpeterSYSCTL_NODE(_vm_stats, OID_AUTO, misc, CTLFLAG_RW, 0, "VM meter misc stats"); 23240794SpeterSYSCTL_INT(_vm_stats_sys, OID_AUTO, 23340794Speter v_swtch, CTLFLAG_RD, &cnt.v_swtch, 0, "Context switches"); 23440794SpeterSYSCTL_INT(_vm_stats_sys, OID_AUTO, 23540794Speter v_trap, CTLFLAG_RD, &cnt.v_trap, 0, "Traps"); 23640794SpeterSYSCTL_INT(_vm_stats_sys, OID_AUTO, 23740794Speter v_syscall, CTLFLAG_RD, &cnt.v_syscall, 0, "Syscalls"); 23846381SbillfSYSCTL_INT(_vm_stats_sys, OID_AUTO, v_intr, CTLFLAG_RD, 23946381Sbillf &cnt.v_intr, 0, "Hardware interrupts"); 24046381SbillfSYSCTL_INT(_vm_stats_sys, OID_AUTO, v_soft, CTLFLAG_RD, 24146381Sbillf &cnt.v_soft, 0, "Software interrupts"); 24240794SpeterSYSCTL_INT(_vm_stats_vm, OID_AUTO, 24340794Speter v_vm_faults, CTLFLAG_RD, &cnt.v_vm_faults, 0, "VM faults"); 24440794SpeterSYSCTL_INT(_vm_stats_vm, OID_AUTO, 24540794Speter v_cow_faults, CTLFLAG_RD, &cnt.v_cow_faults, 0, "COW faults"); 24640794SpeterSYSCTL_INT(_vm_stats_vm, OID_AUTO, 24740794Speter v_cow_optim, CTLFLAG_RD, &cnt.v_cow_optim, 0, "Optimized COW faults"); 24840794SpeterSYSCTL_INT(_vm_stats_vm, OID_AUTO, 24940794Speter v_zfod, CTLFLAG_RD, &cnt.v_zfod, 0, "Zero fill"); 25040794SpeterSYSCTL_INT(_vm_stats_vm, OID_AUTO, 25140794Speter v_ozfod, CTLFLAG_RD, &cnt.v_ozfod, 0, "Optimized zero fill"); 25240794SpeterSYSCTL_INT(_vm_stats_vm, OID_AUTO, 25340794Speter v_swapin, CTLFLAG_RD, &cnt.v_swapin, 0, "Swapin operations"); 25440794SpeterSYSCTL_INT(_vm_stats_vm, OID_AUTO, 25540794Speter v_swapout, CTLFLAG_RD, &cnt.v_swapout, 0, "Swapout operations"); 25640794SpeterSYSCTL_INT(_vm_stats_vm, OID_AUTO, 25740794Speter v_swappgsin, CTLFLAG_RD, &cnt.v_swappgsin, 0, "Swapin pages"); 25840794SpeterSYSCTL_INT(_vm_stats_vm, OID_AUTO, 25940794Speter v_swappgsout, CTLFLAG_RD, &cnt.v_swappgsout, 0, "Swapout pages"); 26040794SpeterSYSCTL_INT(_vm_stats_vm, OID_AUTO, 26140794Speter v_vnodein, CTLFLAG_RD, &cnt.v_vnodein, 0, "Vnodein operations"); 26240794SpeterSYSCTL_INT(_vm_stats_vm, OID_AUTO, 26340794Speter v_vnodeout, CTLFLAG_RD, &cnt.v_vnodeout, 0, "Vnodeout operations"); 26440794SpeterSYSCTL_INT(_vm_stats_vm, OID_AUTO, 26540794Speter v_vnodepgsin, CTLFLAG_RD, &cnt.v_vnodepgsin, 0, "Vnodein pages"); 26640794SpeterSYSCTL_INT(_vm_stats_vm, OID_AUTO, 26740794Speter v_vnodepgsout, CTLFLAG_RD, &cnt.v_vnodepgsout, 0, "Vnodeout pages"); 26840794SpeterSYSCTL_INT(_vm_stats_vm, OID_AUTO, 26940794Speter v_intrans, CTLFLAG_RD, &cnt.v_intrans, 0, "In transit page blocking"); 27040794SpeterSYSCTL_INT(_vm_stats_vm, OID_AUTO, 27140794Speter v_reactivated, CTLFLAG_RD, &cnt.v_reactivated, 0, "Reactivated pages"); 27240794SpeterSYSCTL_INT(_vm_stats_vm, OID_AUTO, 27340794Speter v_pdwakeups, CTLFLAG_RD, &cnt.v_pdwakeups, 0, "Pagedaemon wakeups"); 27440794SpeterSYSCTL_INT(_vm_stats_vm, OID_AUTO, 27540794Speter v_pdpages, CTLFLAG_RD, &cnt.v_pdpages, 0, "Pagedaemon page scans"); 27640794SpeterSYSCTL_INT(_vm_stats_vm, OID_AUTO, 27740794Speter v_dfree, CTLFLAG_RD, &cnt.v_dfree, 0, ""); 27840794SpeterSYSCTL_INT(_vm_stats_vm, OID_AUTO, 27940794Speter v_pfree, CTLFLAG_RD, &cnt.v_pfree, 0, ""); 28040794SpeterSYSCTL_INT(_vm_stats_vm, OID_AUTO, 28140794Speter v_tfree, CTLFLAG_RD, &cnt.v_tfree, 0, ""); 28240794SpeterSYSCTL_INT(_vm_stats_vm, OID_AUTO, 28340794Speter v_page_size, CTLFLAG_RD, &cnt.v_page_size, 0, ""); 28440794SpeterSYSCTL_INT(_vm_stats_vm, OID_AUTO, 28540794Speter v_page_count, CTLFLAG_RD, &cnt.v_page_count, 0, ""); 28640794SpeterSYSCTL_INT(_vm_stats_vm, OID_AUTO, 28740794Speter v_free_reserved, CTLFLAG_RD, &cnt.v_free_reserved, 0, ""); 28840794SpeterSYSCTL_INT(_vm_stats_vm, OID_AUTO, 28940794Speter v_free_target, CTLFLAG_RD, &cnt.v_free_target, 0, ""); 29040794SpeterSYSCTL_INT(_vm_stats_vm, OID_AUTO, 29140794Speter v_free_min, CTLFLAG_RD, &cnt.v_free_min, 0, ""); 29240794SpeterSYSCTL_INT(_vm_stats_vm, OID_AUTO, 29340794Speter v_free_count, CTLFLAG_RD, &cnt.v_free_count, 0, ""); 29440794SpeterSYSCTL_INT(_vm_stats_vm, OID_AUTO, 29540794Speter v_wire_count, CTLFLAG_RD, &cnt.v_wire_count, 0, ""); 29640794SpeterSYSCTL_INT(_vm_stats_vm, OID_AUTO, 29740794Speter v_active_count, CTLFLAG_RD, &cnt.v_active_count, 0, ""); 29840794SpeterSYSCTL_INT(_vm_stats_vm, OID_AUTO, 29940794Speter v_inactive_target, CTLFLAG_RD, &cnt.v_inactive_target, 0, ""); 30040794SpeterSYSCTL_INT(_vm_stats_vm, OID_AUTO, 30140794Speter v_inactive_count, CTLFLAG_RD, &cnt.v_inactive_count, 0, ""); 30240794SpeterSYSCTL_INT(_vm_stats_vm, OID_AUTO, 30340794Speter v_cache_count, CTLFLAG_RD, &cnt.v_cache_count, 0, ""); 30440794SpeterSYSCTL_INT(_vm_stats_vm, OID_AUTO, 30540794Speter v_cache_min, CTLFLAG_RD, &cnt.v_cache_min, 0, ""); 30640794SpeterSYSCTL_INT(_vm_stats_vm, OID_AUTO, 30740794Speter v_cache_max, CTLFLAG_RD, &cnt.v_cache_max, 0, ""); 30840794SpeterSYSCTL_INT(_vm_stats_vm, OID_AUTO, 30940794Speter v_pageout_free_min, CTLFLAG_RD, &cnt.v_pageout_free_min, 0, ""); 31040794SpeterSYSCTL_INT(_vm_stats_vm, OID_AUTO, 31140794Speter v_interrupt_free_min, CTLFLAG_RD, &cnt.v_interrupt_free_min, 0, ""); 31240794SpeterSYSCTL_INT(_vm_stats_misc, OID_AUTO, 31340794Speter zero_page_count, CTLFLAG_RD, &vm_page_zero_count, 0, ""); 31440794Speter#if 0 31540794SpeterSYSCTL_INT(_vm_stats_misc, OID_AUTO, 31640794Speter page_mask, CTLFLAG_RD, &page_mask, 0, ""); 31740794SpeterSYSCTL_INT(_vm_stats_misc, OID_AUTO, 31840794Speter page_shift, CTLFLAG_RD, &page_shift, 0, ""); 31940794SpeterSYSCTL_INT(_vm_stats_misc, OID_AUTO, 32040794Speter first_page, CTLFLAG_RD, &first_page, 0, ""); 32140794SpeterSYSCTL_INT(_vm_stats_misc, OID_AUTO, 32240794Speter last_page, CTLFLAG_RD, &last_page, 0, ""); 32340794SpeterSYSCTL_INT(_vm_stats_misc, OID_AUTO, 32440794Speter vm_page_bucket_count, CTLFLAG_RD, &vm_page_bucket_count, 0, ""); 32540794SpeterSYSCTL_INT(_vm_stats_misc, OID_AUTO, 32640794Speter vm_page_hash_mask, CTLFLAG_RD, &vm_page_hash_mask, 0, ""); 32740794Speter#endif 328