Home | History | Annotate | Line # | Download | only in uvm
uvm_meter.c revision 1.14
      1 /*	$NetBSD: uvm_meter.c,v 1.14 2000/11/24 18:54:31 chs Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1997 Charles D. Cranor and Washington University.
      5  * Copyright (c) 1982, 1986, 1989, 1993
      6  *      The Regents of the University of California.
      7  *
      8  * All rights reserved.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *      This product includes software developed by Charles D. Cranor,
     21  *      Washington University, and the University of California, Berkeley
     22  *      and its contributors.
     23  * 4. Neither the name of the University nor the names of its contributors
     24  *    may be used to endorse or promote products derived from this software
     25  *    without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     37  * SUCH DAMAGE.
     38  *
     39  *      @(#)vm_meter.c  8.4 (Berkeley) 1/4/94
     40  * from: Id: uvm_meter.c,v 1.1.2.1 1997/08/14 19:10:35 chuck Exp
     41  */
     42 
     43 #include <sys/param.h>
     44 #include <sys/proc.h>
     45 #include <sys/systm.h>
     46 #include <sys/kernel.h>
     47 #include <uvm/uvm_extern.h>
     48 #include <sys/sysctl.h>
     49 
     50 /*
     51  * maxslp: ???? XXXCDC
     52  */
     53 
     54 int maxslp = MAXSLP;	/* patchable ... */
     55 struct loadavg averunnable;
     56 
     57 /*
     58  * constants for averages over 1, 5, and 15 minutes when sampling at
     59  * 5 second intervals.
     60  */
     61 
     62 static fixpt_t cexp[3] = {
     63 	0.9200444146293232 * FSCALE,	/* exp(-1/12) */
     64 	0.9834714538216174 * FSCALE,	/* exp(-1/60) */
     65 	0.9944598480048967 * FSCALE,	/* exp(-1/180) */
     66 };
     67 
     68 /*
     69  * prototypes
     70  */
     71 
     72 static void uvm_loadav __P((struct loadavg *));
     73 static void uvm_total __P((struct vmtotal *));
     74 
     75 /*
     76  * uvm_meter: calculate load average and wake up the swapper (if needed)
     77  */
     78 void
     79 uvm_meter()
     80 {
     81 	if ((time.tv_sec % 5) == 0)
     82 		uvm_loadav(&averunnable);
     83 	if (proc0.p_slptime > (maxslp / 2))
     84 		wakeup(&proc0);
     85 }
     86 
     87 /*
     88  * uvm_loadav: compute a tenex style load average of a quantity on
     89  * 1, 5, and 15 minute internvals.
     90  */
     91 static void
     92 uvm_loadav(avg)
     93 	struct loadavg *avg;
     94 {
     95 	int i, nrun;
     96 	struct proc *p;
     97 
     98 	proclist_lock_read();
     99 	nrun = 0;
    100 	LIST_FOREACH(p, &allproc, p_list) {
    101 		switch (p->p_stat) {
    102 		case SSLEEP:
    103 			if (p->p_priority > PZERO || p->p_slptime > 1)
    104 				continue;
    105 		/* fall through */
    106 		case SRUN:
    107 		case SONPROC:
    108 		case SIDL:
    109 			nrun++;
    110 		}
    111 	}
    112 	proclist_unlock_read();
    113 	for (i = 0; i < 3; i++)
    114 		avg->ldavg[i] = (cexp[i] * avg->ldavg[i] +
    115 		    nrun * FSCALE * (FSCALE - cexp[i])) >> FSHIFT;
    116 }
    117 
    118 /*
    119  * uvm_sysctl: sysctl hook into UVM system.
    120  */
    121 int
    122 uvm_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
    123 	int *name;
    124 	u_int namelen;
    125 	void *oldp;
    126 	size_t *oldlenp;
    127 	void *newp;
    128 	size_t newlen;
    129 	struct proc *p;
    130 {
    131 	struct vmtotal vmtotals;
    132 
    133 	/* all sysctl names at this level are terminal */
    134 	if (namelen != 1)
    135 		return (ENOTDIR);		/* overloaded */
    136 
    137 	switch (name[0]) {
    138 	case VM_LOADAVG:
    139 		return (sysctl_rdstruct(oldp, oldlenp, newp, &averunnable,
    140 		    sizeof(averunnable)));
    141 
    142 	case VM_METER:
    143 		uvm_total(&vmtotals);
    144 		return (sysctl_rdstruct(oldp, oldlenp, newp, &vmtotals,
    145 		    sizeof(vmtotals)));
    146 
    147 	case VM_UVMEXP:
    148 		return (sysctl_rdstruct(oldp, oldlenp, newp, &uvmexp,
    149 		    sizeof(uvmexp)));
    150 
    151 	case VM_NKMEMPAGES:
    152 		return (sysctl_rdint(oldp, oldlenp, newp, nkmempages));
    153 
    154 	default:
    155 		return (EOPNOTSUPP);
    156 	}
    157 	/* NOTREACHED */
    158 }
    159 
    160 /*
    161  * uvm_total: calculate the current state of the system.
    162  */
    163 static void
    164 uvm_total(totalp)
    165 	struct vmtotal *totalp;
    166 {
    167 	struct proc *p;
    168 #if 0
    169 	vm_map_entry_t	entry;
    170 	vm_map_t map;
    171 	int paging;
    172 #endif
    173 
    174 	memset(totalp, 0, sizeof *totalp);
    175 
    176 	/*
    177 	 * calculate process statistics
    178 	 */
    179 
    180 	proclist_lock_read();
    181 	LIST_FOREACH(p, &allproc, p_list) {
    182 		if (p->p_flag & P_SYSTEM)
    183 			continue;
    184 		switch (p->p_stat) {
    185 		case 0:
    186 			continue;
    187 
    188 		case SSLEEP:
    189 		case SSTOP:
    190 			if (p->p_flag & P_INMEM) {
    191 				if (p->p_priority <= PZERO)
    192 					totalp->t_dw++;
    193 				else if (p->p_slptime < maxslp)
    194 					totalp->t_sl++;
    195 			} else if (p->p_slptime < maxslp)
    196 				totalp->t_sw++;
    197 			if (p->p_slptime >= maxslp)
    198 				continue;
    199 			break;
    200 
    201 		case SRUN:
    202 		case SONPROC:
    203 		case SIDL:
    204 			if (p->p_flag & P_INMEM)
    205 				totalp->t_rq++;
    206 			else
    207 				totalp->t_sw++;
    208 			if (p->p_stat == SIDL)
    209 				continue;
    210 			break;
    211 		}
    212 		/*
    213 		 * note active objects
    214 		 */
    215 #if 0
    216 		/*
    217 		 * XXXCDC: BOGUS!  rethink this.   in the mean time
    218 		 * don't do it.
    219 		 */
    220 		paging = 0;
    221 		vm_map_lock(map);
    222 		for (map = &p->p_vmspace->vm_map, entry = map->header.next;
    223 		    entry != &map->header; entry = entry->next) {
    224 			if (entry->is_a_map || entry->is_sub_map ||
    225 			    entry->object.uvm_obj == NULL)
    226 				continue;
    227 			/* XXX how to do this with uvm */
    228 		}
    229 		vm_map_unlock(map);
    230 		if (paging)
    231 			totalp->t_pw++;
    232 #endif
    233 	}
    234 	proclist_unlock_read();
    235 	/*
    236 	 * Calculate object memory usage statistics.
    237 	 */
    238 	totalp->t_free = uvmexp.free;
    239 	totalp->t_vm = uvmexp.npages - uvmexp.free + uvmexp.swpginuse;
    240 	totalp->t_avm = uvmexp.active + uvmexp.swpginuse;	/* XXX */
    241 	totalp->t_rm = uvmexp.npages - uvmexp.free;
    242 	totalp->t_arm = uvmexp.active;
    243 	totalp->t_vmshr = 0;		/* XXX */
    244 	totalp->t_avmshr = 0;		/* XXX */
    245 	totalp->t_rmshr = 0;		/* XXX */
    246 	totalp->t_armshr = 0;		/* XXX */
    247 }
    248