1df8bae1dSRodney W. Grimes /* 2df8bae1dSRodney W. Grimes * Copyright (c) 1982, 1986, 1989, 1993 3df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 4df8bae1dSRodney W. Grimes * 5df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 6df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 7df8bae1dSRodney W. Grimes * are met: 8df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 9df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 10df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 11df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 12df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 13df8bae1dSRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 14df8bae1dSRodney W. Grimes * must display the following acknowledgement: 15df8bae1dSRodney W. Grimes * This product includes software developed by the University of 16df8bae1dSRodney W. Grimes * California, Berkeley and its contributors. 17df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 18df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 19df8bae1dSRodney W. Grimes * without specific prior written permission. 20df8bae1dSRodney W. Grimes * 21df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31df8bae1dSRodney W. Grimes * SUCH DAMAGE. 32df8bae1dSRodney W. Grimes * 33df8bae1dSRodney W. Grimes * @(#)vm_meter.c 8.4 (Berkeley) 1/4/94 3408637435SBruce Evans * $Id: vm_meter.c,v 1.23 1997/11/24 15:15:33 bde Exp $ 35df8bae1dSRodney W. Grimes */ 36df8bae1dSRodney W. Grimes 37df8bae1dSRodney W. Grimes #include <sys/param.h> 38df8bae1dSRodney W. Grimes #include <sys/proc.h> 39df8bae1dSRodney W. Grimes #include <sys/systm.h> 40df8bae1dSRodney W. Grimes #include <sys/kernel.h> 4108637435SBruce Evans #include <sys/resource.h> 42efeaf95aSDavid Greenman #include <sys/vmmeter.h> 43efeaf95aSDavid Greenman 44df8bae1dSRodney W. Grimes #include <vm/vm.h> 459b4288a3SBruce Evans #include <vm/vm_extern.h> 46efeaf95aSDavid Greenman #include <vm/vm_param.h> 47996c772fSJohn Dyson #include <sys/lock.h> 48efeaf95aSDavid Greenman #include <vm/pmap.h> 49efeaf95aSDavid Greenman #include <vm/vm_map.h> 50efeaf95aSDavid Greenman #include <vm/vm_object.h> 51df8bae1dSRodney W. Grimes #include <sys/sysctl.h> 52df8bae1dSRodney W. Grimes 53946bb7a2SPoul-Henning Kamp struct loadavg averunnable = 54946bb7a2SPoul-Henning Kamp { {0, 0, 0}, FSCALE }; /* load average, of runnable procs */ 55946bb7a2SPoul-Henning Kamp 5628f8db14SBruce Evans struct vmmeter cnt; 57df8bae1dSRodney W. Grimes 58f708ef1bSPoul-Henning Kamp static int maxslp = MAXSLP; 59df8bae1dSRodney W. Grimes 60df8bae1dSRodney W. Grimes /* 61df8bae1dSRodney W. Grimes * Constants for averages over 1, 5, and 15 minutes 62df8bae1dSRodney W. Grimes * when sampling at 5 second intervals. 63df8bae1dSRodney W. Grimes */ 64f708ef1bSPoul-Henning Kamp static fixpt_t cexp[3] = { 65df8bae1dSRodney W. Grimes 0.9200444146293232 * FSCALE, /* exp(-1/12) */ 66df8bae1dSRodney W. Grimes 0.9834714538216174 * FSCALE, /* exp(-1/60) */ 67df8bae1dSRodney W. Grimes 0.9944598480048967 * FSCALE, /* exp(-1/180) */ 68df8bae1dSRodney W. Grimes }; 69df8bae1dSRodney W. Grimes 70df8bae1dSRodney W. Grimes /* 71df8bae1dSRodney W. Grimes * Compute a tenex style load average of a quantity on 72df8bae1dSRodney W. Grimes * 1, 5 and 15 minute intervals. 73df8bae1dSRodney W. Grimes */ 7424a1cce3SDavid Greenman static void 75a9ad941cSPoul-Henning Kamp loadav(struct loadavg *avg) 76df8bae1dSRodney W. Grimes { 77df8bae1dSRodney W. Grimes register int i, nrun; 78df8bae1dSRodney W. Grimes register struct proc *p; 79df8bae1dSRodney W. Grimes 801b67ec6dSJeffrey Hsu for (nrun = 0, p = allproc.lh_first; p != 0; p = p->p_list.le_next) { 81df8bae1dSRodney W. Grimes switch (p->p_stat) { 82df8bae1dSRodney W. Grimes case SSLEEP: 83df8bae1dSRodney W. Grimes if (p->p_priority > PZERO || p->p_slptime != 0) 84df8bae1dSRodney W. Grimes continue; 85df8bae1dSRodney W. Grimes /* fall through */ 86df8bae1dSRodney W. Grimes case SRUN: 87df8bae1dSRodney W. Grimes case SIDL: 88df8bae1dSRodney W. Grimes nrun++; 89df8bae1dSRodney W. Grimes } 90df8bae1dSRodney W. Grimes } 91df8bae1dSRodney W. Grimes for (i = 0; i < 3; i++) 92df8bae1dSRodney W. Grimes avg->ldavg[i] = (cexp[i] * avg->ldavg[i] + 93df8bae1dSRodney W. Grimes nrun * FSCALE * (FSCALE - cexp[i])) >> FSHIFT; 94df8bae1dSRodney W. Grimes } 95df8bae1dSRodney W. Grimes 9624a1cce3SDavid Greenman void 9724a1cce3SDavid Greenman vmmeter() 9824a1cce3SDavid Greenman { 9924a1cce3SDavid Greenman 10024a1cce3SDavid Greenman if (time.tv_sec % 5 == 0) 10124a1cce3SDavid Greenman loadav(&averunnable); 10224a1cce3SDavid Greenman if (proc0.p_slptime > maxslp / 2) 10324a1cce3SDavid Greenman wakeup(&proc0); 10424a1cce3SDavid Greenman } 10524a1cce3SDavid Greenman 106a9ad941cSPoul-Henning Kamp SYSCTL_INT(_vm, VM_V_FREE_MIN, v_free_min, 107a9ad941cSPoul-Henning Kamp CTLFLAG_RW, &cnt.v_free_min, 0, ""); 108a9ad941cSPoul-Henning Kamp SYSCTL_INT(_vm, VM_V_FREE_TARGET, v_free_target, 109a9ad941cSPoul-Henning Kamp CTLFLAG_RW, &cnt.v_free_target, 0, ""); 110a9ad941cSPoul-Henning Kamp SYSCTL_INT(_vm, VM_V_FREE_RESERVED, v_free_reserved, 111a9ad941cSPoul-Henning Kamp CTLFLAG_RW, &cnt.v_free_reserved, 0, ""); 112a9ad941cSPoul-Henning Kamp SYSCTL_INT(_vm, VM_V_INACTIVE_TARGET, v_inactive_target, 113a9ad941cSPoul-Henning Kamp CTLFLAG_RW, &cnt.v_inactive_target, 0, ""); 114a9ad941cSPoul-Henning Kamp SYSCTL_INT(_vm, VM_V_CACHE_MIN, v_cache_min, 115a9ad941cSPoul-Henning Kamp CTLFLAG_RW, &cnt.v_cache_min, 0, ""); 116a9ad941cSPoul-Henning Kamp SYSCTL_INT(_vm, VM_V_CACHE_MAX, v_cache_max, 117a9ad941cSPoul-Henning Kamp CTLFLAG_RW, &cnt.v_cache_max, 0, ""); 118a9ad941cSPoul-Henning Kamp SYSCTL_INT(_vm, VM_V_PAGEOUT_FREE_MIN, v_pageout_free_min, 119a9ad941cSPoul-Henning Kamp CTLFLAG_RW, &cnt.v_pageout_free_min, 0, ""); 120a9ad941cSPoul-Henning Kamp 121946bb7a2SPoul-Henning Kamp SYSCTL_STRUCT(_vm, VM_LOADAVG, loadavg, CTLFLAG_RD, &averunnable, loadavg, ""); 122a9ad941cSPoul-Henning Kamp 123a9ad941cSPoul-Henning Kamp static int 124a9ad941cSPoul-Henning Kamp vmtotal SYSCTL_HANDLER_ARGS 125df8bae1dSRodney W. Grimes { 126a9ad941cSPoul-Henning Kamp struct proc *p; 127a9ad941cSPoul-Henning Kamp struct vmtotal total, *totalp; 128a9ad941cSPoul-Henning Kamp vm_map_entry_t entry; 129a9ad941cSPoul-Henning Kamp vm_object_t object; 130a9ad941cSPoul-Henning Kamp vm_map_t map; 131df8bae1dSRodney W. Grimes int paging; 132df8bae1dSRodney W. Grimes 133a9ad941cSPoul-Henning Kamp totalp = &total; 134df8bae1dSRodney W. Grimes bzero(totalp, sizeof *totalp); 135df8bae1dSRodney W. Grimes /* 136df8bae1dSRodney W. Grimes * Mark all objects as inactive. 137df8bae1dSRodney W. Grimes */ 138b18bfc3dSJohn Dyson for (object = TAILQ_FIRST(&vm_object_list); 139df8bae1dSRodney W. Grimes object != NULL; 140b18bfc3dSJohn Dyson object = TAILQ_NEXT(object,object_list)) 141df8bae1dSRodney W. Grimes object->flags &= ~OBJ_ACTIVE; 142df8bae1dSRodney W. Grimes /* 143df8bae1dSRodney W. Grimes * Calculate process statistics. 144df8bae1dSRodney W. Grimes */ 1451b67ec6dSJeffrey Hsu for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) { 146df8bae1dSRodney W. Grimes if (p->p_flag & P_SYSTEM) 147df8bae1dSRodney W. Grimes continue; 148df8bae1dSRodney W. Grimes switch (p->p_stat) { 149df8bae1dSRodney W. Grimes case 0: 150df8bae1dSRodney W. Grimes continue; 151df8bae1dSRodney W. Grimes 152df8bae1dSRodney W. Grimes case SSLEEP: 153df8bae1dSRodney W. Grimes case SSTOP: 154df8bae1dSRodney W. Grimes if (p->p_flag & P_INMEM) { 155df8bae1dSRodney W. Grimes if (p->p_priority <= PZERO) 156df8bae1dSRodney W. Grimes totalp->t_dw++; 157df8bae1dSRodney W. Grimes else if (p->p_slptime < maxslp) 158df8bae1dSRodney W. Grimes totalp->t_sl++; 159df8bae1dSRodney W. Grimes } else if (p->p_slptime < maxslp) 160df8bae1dSRodney W. Grimes totalp->t_sw++; 161df8bae1dSRodney W. Grimes if (p->p_slptime >= maxslp) 162df8bae1dSRodney W. Grimes continue; 163df8bae1dSRodney W. Grimes break; 164df8bae1dSRodney W. Grimes 165df8bae1dSRodney W. Grimes case SRUN: 166df8bae1dSRodney W. Grimes case SIDL: 167df8bae1dSRodney W. Grimes if (p->p_flag & P_INMEM) 168df8bae1dSRodney W. Grimes totalp->t_rq++; 169df8bae1dSRodney W. Grimes else 170df8bae1dSRodney W. Grimes totalp->t_sw++; 171df8bae1dSRodney W. Grimes if (p->p_stat == SIDL) 172df8bae1dSRodney W. Grimes continue; 173df8bae1dSRodney W. Grimes break; 174df8bae1dSRodney W. Grimes } 175df8bae1dSRodney W. Grimes /* 176df8bae1dSRodney W. Grimes * Note active objects. 177df8bae1dSRodney W. Grimes */ 178df8bae1dSRodney W. Grimes paging = 0; 179df8bae1dSRodney W. Grimes for (map = &p->p_vmspace->vm_map, entry = map->header.next; 180df8bae1dSRodney W. Grimes entry != &map->header; entry = entry->next) { 181afa07f7eSJohn Dyson if ((entry->eflags & (MAP_ENTRY_IS_A_MAP|MAP_ENTRY_IS_SUB_MAP)) || 182df8bae1dSRodney W. Grimes entry->object.vm_object == NULL) 183df8bae1dSRodney W. Grimes continue; 184df8bae1dSRodney W. Grimes entry->object.vm_object->flags |= OBJ_ACTIVE; 185df8bae1dSRodney W. Grimes paging |= entry->object.vm_object->paging_in_progress; 186df8bae1dSRodney W. Grimes } 187df8bae1dSRodney W. Grimes if (paging) 188df8bae1dSRodney W. Grimes totalp->t_pw++; 189df8bae1dSRodney W. Grimes } 190df8bae1dSRodney W. Grimes /* 191df8bae1dSRodney W. Grimes * Calculate object memory usage statistics. 192df8bae1dSRodney W. Grimes */ 193b18bfc3dSJohn Dyson for (object = TAILQ_FIRST(&vm_object_list); 194df8bae1dSRodney W. Grimes object != NULL; 195b18bfc3dSJohn Dyson object = TAILQ_NEXT(object, object_list)) { 1965070c7f8SJohn Dyson totalp->t_vm += object->size; 197df8bae1dSRodney W. Grimes totalp->t_rm += object->resident_page_count; 198df8bae1dSRodney W. Grimes if (object->flags & OBJ_ACTIVE) { 1995070c7f8SJohn Dyson totalp->t_avm += object->size; 200df8bae1dSRodney W. Grimes totalp->t_arm += object->resident_page_count; 201df8bae1dSRodney W. Grimes } 2025070c7f8SJohn Dyson if (object->shadow_count > 1) { 203df8bae1dSRodney W. Grimes /* shared object */ 2045070c7f8SJohn Dyson totalp->t_vmshr += object->size; 205df8bae1dSRodney W. Grimes totalp->t_rmshr += object->resident_page_count; 206df8bae1dSRodney W. Grimes if (object->flags & OBJ_ACTIVE) { 2075070c7f8SJohn Dyson totalp->t_avmshr += object->size; 208df8bae1dSRodney W. Grimes totalp->t_armshr += object->resident_page_count; 209df8bae1dSRodney W. Grimes } 210df8bae1dSRodney W. Grimes } 211df8bae1dSRodney W. Grimes } 2120d94caffSDavid Greenman totalp->t_free = cnt.v_free_count + cnt.v_cache_count; 213a9ad941cSPoul-Henning Kamp return (sysctl_handle_opaque(oidp, totalp, sizeof total, req)); 214df8bae1dSRodney W. Grimes } 215a9ad941cSPoul-Henning Kamp 216a9ad941cSPoul-Henning Kamp SYSCTL_PROC(_vm, VM_METER, vmmeter, CTLTYPE_OPAQUE|CTLFLAG_RD, 217946bb7a2SPoul-Henning Kamp 0, sizeof(struct vmtotal), vmtotal, "S,vmtotal", ""); 218