vm_meter.c revision 9507
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 349507Sdg * $Id: vm_meter.c,v 1.6 1995/01/10 07:32:47 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 */ 451541Srgrimes 465455Sdgint maxslp = MAXSLP; 471541Srgrimes 481541Srgrimes/* 491541Srgrimes * Constants for averages over 1, 5, and 15 minutes 501541Srgrimes * when sampling at 5 second intervals. 511541Srgrimes */ 525455Sdgfixpt_t cexp[3] = { 531541Srgrimes 0.9200444146293232 * FSCALE, /* exp(-1/12) */ 541541Srgrimes 0.9834714538216174 * FSCALE, /* exp(-1/60) */ 551541Srgrimes 0.9944598480048967 * FSCALE, /* exp(-1/180) */ 561541Srgrimes}; 571541Srgrimes 581541Srgrimes/* 591541Srgrimes * Compute a tenex style load average of a quantity on 601541Srgrimes * 1, 5 and 15 minute intervals. 611541Srgrimes */ 629507Sdgstatic void 631541Srgrimesloadav(avg) 641541Srgrimes register struct loadavg *avg; 651541Srgrimes{ 661541Srgrimes register int i, nrun; 671541Srgrimes register struct proc *p; 681541Srgrimes 695455Sdg for (nrun = 0, p = (struct proc *) allproc; p != NULL; p = p->p_next) { 701541Srgrimes switch (p->p_stat) { 711541Srgrimes case SSLEEP: 721541Srgrimes if (p->p_priority > PZERO || p->p_slptime != 0) 731541Srgrimes continue; 741541Srgrimes /* fall through */ 751541Srgrimes case SRUN: 761541Srgrimes case SIDL: 771541Srgrimes nrun++; 781541Srgrimes } 791541Srgrimes } 801541Srgrimes for (i = 0; i < 3; i++) 811541Srgrimes avg->ldavg[i] = (cexp[i] * avg->ldavg[i] + 825455Sdg nrun * FSCALE * (FSCALE - cexp[i])) >> FSHIFT; 831541Srgrimes} 841541Srgrimes 859507Sdgvoid 869507Sdgvmmeter() 879507Sdg{ 889507Sdg 899507Sdg if (time.tv_sec % 5 == 0) 909507Sdg loadav(&averunnable); 919507Sdg if (proc0.p_slptime > maxslp / 2) 929507Sdg wakeup(&proc0); 939507Sdg} 949507Sdg 951541Srgrimes/* 961541Srgrimes * Attributes associated with virtual memory. 971541Srgrimes */ 981549Srgrimesint 991541Srgrimesvm_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p) 1001541Srgrimes int *name; 1011541Srgrimes u_int namelen; 1021541Srgrimes void *oldp; 1031541Srgrimes size_t *oldlenp; 1041541Srgrimes void *newp; 1051541Srgrimes size_t newlen; 1061541Srgrimes struct proc *p; 1071541Srgrimes{ 1081541Srgrimes struct vmtotal vmtotals; 1091541Srgrimes 1101541Srgrimes /* all sysctl names at this level are terminal */ 1111541Srgrimes if (namelen != 1) 1125455Sdg return (ENOTDIR); /* overloaded */ 1131541Srgrimes 1141541Srgrimes switch (name[0]) { 1151541Srgrimes case VM_LOADAVG: 1161541Srgrimes averunnable.fscale = FSCALE; 1171541Srgrimes return (sysctl_rdstruct(oldp, oldlenp, newp, &averunnable, 1185455Sdg sizeof(averunnable))); 1191541Srgrimes case VM_METER: 1201541Srgrimes vmtotal(&vmtotals); 1211541Srgrimes return (sysctl_rdstruct(oldp, oldlenp, newp, &vmtotals, 1225455Sdg sizeof(vmtotals))); 1235455Sdg case VM_V_FREE_MIN: 1245455Sdg return (sysctl_int(oldp, oldlenp, newp, newlen, &cnt.v_free_min)); 1255455Sdg case VM_V_FREE_TARGET: 1265455Sdg return (sysctl_int(oldp, oldlenp, newp, newlen, &cnt.v_free_target)); 1275455Sdg case VM_V_FREE_RESERVED: 1285455Sdg return (sysctl_int(oldp, oldlenp, newp, newlen, &cnt.v_free_reserved)); 1295455Sdg case VM_V_INACTIVE_TARGET: 1305455Sdg return (sysctl_int(oldp, oldlenp, newp, newlen, &cnt.v_inactive_target)); 1315455Sdg case VM_V_CACHE_MIN: 1325455Sdg return (sysctl_int(oldp, oldlenp, newp, newlen, &cnt.v_cache_min)); 1335455Sdg case VM_V_CACHE_MAX: 1345455Sdg return (sysctl_int(oldp, oldlenp, newp, newlen, &cnt.v_cache_max)); 1355455Sdg case VM_V_PAGEOUT_FREE_MIN: 1365455Sdg return (sysctl_int(oldp, oldlenp, newp, newlen, &cnt.v_pageout_free_min)); 1375455Sdg 1381541Srgrimes default: 1391541Srgrimes return (EOPNOTSUPP); 1401541Srgrimes } 1411541Srgrimes /* NOTREACHED */ 1421541Srgrimes} 1431541Srgrimes 1441541Srgrimes/* 1451541Srgrimes * Calculate the current state of the system. 1461541Srgrimes * Done on demand from getkerninfo(). 1471541Srgrimes */ 1481541Srgrimesvoid 1491541Srgrimesvmtotal(totalp) 1501541Srgrimes register struct vmtotal *totalp; 1511541Srgrimes{ 1521541Srgrimes register struct proc *p; 1535455Sdg register vm_map_entry_t entry; 1541541Srgrimes register vm_object_t object; 1551541Srgrimes register vm_map_t map; 1561541Srgrimes int paging; 1571541Srgrimes 1581541Srgrimes bzero(totalp, sizeof *totalp); 1591541Srgrimes /* 1601541Srgrimes * Mark all objects as inactive. 1611541Srgrimes */ 1621541Srgrimes for (object = vm_object_list.tqh_first; 1635455Sdg object != NULL; 1645455Sdg object = object->object_list.tqe_next) 1651541Srgrimes object->flags &= ~OBJ_ACTIVE; 1661541Srgrimes /* 1671541Srgrimes * Calculate process statistics. 1681541Srgrimes */ 1695455Sdg for (p = (struct proc *) allproc; p != NULL; p = p->p_next) { 1701541Srgrimes if (p->p_flag & P_SYSTEM) 1711541Srgrimes continue; 1721541Srgrimes switch (p->p_stat) { 1731541Srgrimes case 0: 1741541Srgrimes continue; 1751541Srgrimes 1761541Srgrimes case SSLEEP: 1771541Srgrimes case SSTOP: 1781541Srgrimes if (p->p_flag & P_INMEM) { 1791541Srgrimes if (p->p_priority <= PZERO) 1801541Srgrimes totalp->t_dw++; 1811541Srgrimes else if (p->p_slptime < maxslp) 1821541Srgrimes totalp->t_sl++; 1831541Srgrimes } else if (p->p_slptime < maxslp) 1841541Srgrimes totalp->t_sw++; 1851541Srgrimes if (p->p_slptime >= maxslp) 1861541Srgrimes continue; 1871541Srgrimes break; 1881541Srgrimes 1891541Srgrimes case SRUN: 1901541Srgrimes case SIDL: 1911541Srgrimes if (p->p_flag & P_INMEM) 1921541Srgrimes totalp->t_rq++; 1931541Srgrimes else 1941541Srgrimes totalp->t_sw++; 1951541Srgrimes if (p->p_stat == SIDL) 1961541Srgrimes continue; 1971541Srgrimes break; 1981541Srgrimes } 1991541Srgrimes /* 2001541Srgrimes * Note active objects. 2011541Srgrimes */ 2021541Srgrimes paging = 0; 2031541Srgrimes for (map = &p->p_vmspace->vm_map, entry = map->header.next; 2045455Sdg entry != &map->header; entry = entry->next) { 2051541Srgrimes if (entry->is_a_map || entry->is_sub_map || 2061541Srgrimes entry->object.vm_object == NULL) 2071541Srgrimes continue; 2081541Srgrimes entry->object.vm_object->flags |= OBJ_ACTIVE; 2091541Srgrimes paging |= entry->object.vm_object->paging_in_progress; 2101541Srgrimes } 2111541Srgrimes if (paging) 2121541Srgrimes totalp->t_pw++; 2131541Srgrimes } 2141541Srgrimes /* 2151541Srgrimes * Calculate object memory usage statistics. 2161541Srgrimes */ 2171541Srgrimes for (object = vm_object_list.tqh_first; 2185455Sdg object != NULL; 2195455Sdg object = object->object_list.tqe_next) { 2201541Srgrimes totalp->t_vm += num_pages(object->size); 2211541Srgrimes totalp->t_rm += object->resident_page_count; 2221541Srgrimes if (object->flags & OBJ_ACTIVE) { 2231541Srgrimes totalp->t_avm += num_pages(object->size); 2241541Srgrimes totalp->t_arm += object->resident_page_count; 2251541Srgrimes } 2261541Srgrimes if (object->ref_count > 1) { 2271541Srgrimes /* shared object */ 2281541Srgrimes totalp->t_vmshr += num_pages(object->size); 2291541Srgrimes totalp->t_rmshr += object->resident_page_count; 2301541Srgrimes if (object->flags & OBJ_ACTIVE) { 2311541Srgrimes totalp->t_avmshr += num_pages(object->size); 2321541Srgrimes totalp->t_armshr += object->resident_page_count; 2331541Srgrimes } 2341541Srgrimes } 2351541Srgrimes } 2365455Sdg totalp->t_free = cnt.v_free_count + cnt.v_cache_count; 2371541Srgrimes} 238