vm_meter.c revision 9759
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 141541Srgrimes * must display the following acknowledgement: 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 349759Sbde * $Id: vm_meter.c,v 1.7 1995/07/13 08:48:30 davidg Exp $ 351541Srgrimes */ 361541Srgrimes 371541Srgrimes#include <sys/param.h> 381541Srgrimes#include <sys/proc.h> 391541Srgrimes#include <sys/systm.h> 401541Srgrimes#include <sys/kernel.h> 411541Srgrimes#include <vm/vm.h> 421541Srgrimes#include <sys/sysctl.h> 431541Srgrimes 445455Sdgstruct loadavg averunnable; /* load average, of runnable procs */ 459759Sbdestruct vmmeter cnt; 461541Srgrimes 475455Sdgint maxslp = MAXSLP; 481541Srgrimes 491541Srgrimes/* 501541Srgrimes * Constants for averages over 1, 5, and 15 minutes 511541Srgrimes * when sampling at 5 second intervals. 521541Srgrimes */ 535455Sdgfixpt_t cexp[3] = { 541541Srgrimes 0.9200444146293232 * FSCALE, /* exp(-1/12) */ 551541Srgrimes 0.9834714538216174 * FSCALE, /* exp(-1/60) */ 561541Srgrimes 0.9944598480048967 * FSCALE, /* exp(-1/180) */ 571541Srgrimes}; 581541Srgrimes 591541Srgrimes/* 601541Srgrimes * Compute a tenex style load average of a quantity on 611541Srgrimes * 1, 5 and 15 minute intervals. 621541Srgrimes */ 639507Sdgstatic void 641541Srgrimesloadav(avg) 651541Srgrimes register struct loadavg *avg; 661541Srgrimes{ 671541Srgrimes register int i, nrun; 681541Srgrimes register struct proc *p; 691541Srgrimes 705455Sdg for (nrun = 0, p = (struct proc *) allproc; p != NULL; p = p->p_next) { 711541Srgrimes switch (p->p_stat) { 721541Srgrimes case SSLEEP: 731541Srgrimes if (p->p_priority > PZERO || p->p_slptime != 0) 741541Srgrimes continue; 751541Srgrimes /* fall through */ 761541Srgrimes case SRUN: 771541Srgrimes case SIDL: 781541Srgrimes nrun++; 791541Srgrimes } 801541Srgrimes } 811541Srgrimes for (i = 0; i < 3; i++) 821541Srgrimes avg->ldavg[i] = (cexp[i] * avg->ldavg[i] + 835455Sdg nrun * FSCALE * (FSCALE - cexp[i])) >> FSHIFT; 841541Srgrimes} 851541Srgrimes 869507Sdgvoid 879507Sdgvmmeter() 889507Sdg{ 899507Sdg 909507Sdg if (time.tv_sec % 5 == 0) 919507Sdg loadav(&averunnable); 929507Sdg if (proc0.p_slptime > maxslp / 2) 939507Sdg wakeup(&proc0); 949507Sdg} 959507Sdg 961541Srgrimes/* 971541Srgrimes * Attributes associated with virtual memory. 981541Srgrimes */ 991549Srgrimesint 1001541Srgrimesvm_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p) 1011541Srgrimes int *name; 1021541Srgrimes u_int namelen; 1031541Srgrimes void *oldp; 1041541Srgrimes size_t *oldlenp; 1051541Srgrimes void *newp; 1061541Srgrimes size_t newlen; 1071541Srgrimes struct proc *p; 1081541Srgrimes{ 1091541Srgrimes struct vmtotal vmtotals; 1101541Srgrimes 1111541Srgrimes /* all sysctl names at this level are terminal */ 1121541Srgrimes if (namelen != 1) 1135455Sdg return (ENOTDIR); /* overloaded */ 1141541Srgrimes 1151541Srgrimes switch (name[0]) { 1161541Srgrimes case VM_LOADAVG: 1171541Srgrimes averunnable.fscale = FSCALE; 1181541Srgrimes return (sysctl_rdstruct(oldp, oldlenp, newp, &averunnable, 1195455Sdg sizeof(averunnable))); 1201541Srgrimes case VM_METER: 1211541Srgrimes vmtotal(&vmtotals); 1221541Srgrimes return (sysctl_rdstruct(oldp, oldlenp, newp, &vmtotals, 1235455Sdg sizeof(vmtotals))); 1245455Sdg case VM_V_FREE_MIN: 1255455Sdg return (sysctl_int(oldp, oldlenp, newp, newlen, &cnt.v_free_min)); 1265455Sdg case VM_V_FREE_TARGET: 1275455Sdg return (sysctl_int(oldp, oldlenp, newp, newlen, &cnt.v_free_target)); 1285455Sdg case VM_V_FREE_RESERVED: 1295455Sdg return (sysctl_int(oldp, oldlenp, newp, newlen, &cnt.v_free_reserved)); 1305455Sdg case VM_V_INACTIVE_TARGET: 1315455Sdg return (sysctl_int(oldp, oldlenp, newp, newlen, &cnt.v_inactive_target)); 1325455Sdg case VM_V_CACHE_MIN: 1335455Sdg return (sysctl_int(oldp, oldlenp, newp, newlen, &cnt.v_cache_min)); 1345455Sdg case VM_V_CACHE_MAX: 1355455Sdg return (sysctl_int(oldp, oldlenp, newp, newlen, &cnt.v_cache_max)); 1365455Sdg case VM_V_PAGEOUT_FREE_MIN: 1375455Sdg return (sysctl_int(oldp, oldlenp, newp, newlen, &cnt.v_pageout_free_min)); 1385455Sdg 1391541Srgrimes default: 1401541Srgrimes return (EOPNOTSUPP); 1411541Srgrimes } 1421541Srgrimes /* NOTREACHED */ 1431541Srgrimes} 1441541Srgrimes 1451541Srgrimes/* 1461541Srgrimes * Calculate the current state of the system. 1471541Srgrimes * Done on demand from getkerninfo(). 1481541Srgrimes */ 1491541Srgrimesvoid 1501541Srgrimesvmtotal(totalp) 1511541Srgrimes register struct vmtotal *totalp; 1521541Srgrimes{ 1531541Srgrimes register struct proc *p; 1545455Sdg register vm_map_entry_t entry; 1551541Srgrimes register vm_object_t object; 1561541Srgrimes register vm_map_t map; 1571541Srgrimes int paging; 1581541Srgrimes 1591541Srgrimes bzero(totalp, sizeof *totalp); 1601541Srgrimes /* 1611541Srgrimes * Mark all objects as inactive. 1621541Srgrimes */ 1631541Srgrimes for (object = vm_object_list.tqh_first; 1645455Sdg object != NULL; 1655455Sdg object = object->object_list.tqe_next) 1661541Srgrimes object->flags &= ~OBJ_ACTIVE; 1671541Srgrimes /* 1681541Srgrimes * Calculate process statistics. 1691541Srgrimes */ 1705455Sdg for (p = (struct proc *) allproc; p != NULL; p = p->p_next) { 1711541Srgrimes if (p->p_flag & P_SYSTEM) 1721541Srgrimes continue; 1731541Srgrimes switch (p->p_stat) { 1741541Srgrimes case 0: 1751541Srgrimes continue; 1761541Srgrimes 1771541Srgrimes case SSLEEP: 1781541Srgrimes case SSTOP: 1791541Srgrimes if (p->p_flag & P_INMEM) { 1801541Srgrimes if (p->p_priority <= PZERO) 1811541Srgrimes totalp->t_dw++; 1821541Srgrimes else if (p->p_slptime < maxslp) 1831541Srgrimes totalp->t_sl++; 1841541Srgrimes } else if (p->p_slptime < maxslp) 1851541Srgrimes totalp->t_sw++; 1861541Srgrimes if (p->p_slptime >= maxslp) 1871541Srgrimes continue; 1881541Srgrimes break; 1891541Srgrimes 1901541Srgrimes case SRUN: 1911541Srgrimes case SIDL: 1921541Srgrimes if (p->p_flag & P_INMEM) 1931541Srgrimes totalp->t_rq++; 1941541Srgrimes else 1951541Srgrimes totalp->t_sw++; 1961541Srgrimes if (p->p_stat == SIDL) 1971541Srgrimes continue; 1981541Srgrimes break; 1991541Srgrimes } 2001541Srgrimes /* 2011541Srgrimes * Note active objects. 2021541Srgrimes */ 2031541Srgrimes paging = 0; 2041541Srgrimes for (map = &p->p_vmspace->vm_map, entry = map->header.next; 2055455Sdg entry != &map->header; entry = entry->next) { 2061541Srgrimes if (entry->is_a_map || entry->is_sub_map || 2071541Srgrimes entry->object.vm_object == NULL) 2081541Srgrimes continue; 2091541Srgrimes entry->object.vm_object->flags |= OBJ_ACTIVE; 2101541Srgrimes paging |= entry->object.vm_object->paging_in_progress; 2111541Srgrimes } 2121541Srgrimes if (paging) 2131541Srgrimes totalp->t_pw++; 2141541Srgrimes } 2151541Srgrimes /* 2161541Srgrimes * Calculate object memory usage statistics. 2171541Srgrimes */ 2181541Srgrimes for (object = vm_object_list.tqh_first; 2195455Sdg object != NULL; 2205455Sdg object = object->object_list.tqe_next) { 2211541Srgrimes totalp->t_vm += num_pages(object->size); 2221541Srgrimes totalp->t_rm += object->resident_page_count; 2231541Srgrimes if (object->flags & OBJ_ACTIVE) { 2241541Srgrimes totalp->t_avm += num_pages(object->size); 2251541Srgrimes totalp->t_arm += object->resident_page_count; 2261541Srgrimes } 2271541Srgrimes if (object->ref_count > 1) { 2281541Srgrimes /* shared object */ 2291541Srgrimes totalp->t_vmshr += num_pages(object->size); 2301541Srgrimes totalp->t_rmshr += object->resident_page_count; 2311541Srgrimes if (object->flags & OBJ_ACTIVE) { 2321541Srgrimes totalp->t_avmshr += num_pages(object->size); 2331541Srgrimes totalp->t_armshr += object->resident_page_count; 2341541Srgrimes } 2351541Srgrimes } 2361541Srgrimes } 2375455Sdg totalp->t_free = cnt.v_free_count + cnt.v_cache_count; 2381541Srgrimes} 239