Home | History | Annotate | Line # | Download | only in uvm
uvm_meter.c revision 1.53
      1 /*	$NetBSD: uvm_meter.c,v 1.53 2010/11/06 12:18:17 uebayasi 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/cdefs.h>
     44 __KERNEL_RCSID(0, "$NetBSD: uvm_meter.c,v 1.53 2010/11/06 12:18:17 uebayasi Exp $");
     45 
     46 #include <sys/param.h>
     47 #include <sys/proc.h>
     48 #include <sys/systm.h>
     49 #include <sys/kernel.h>
     50 #include <sys/sysctl.h>
     51 
     52 #include <uvm/uvm.h>
     53 #include <uvm/uvm_pdpolicy.h>
     54 
     55 /*
     56  * maxslp: ???? XXXCDC
     57  */
     58 
     59 int maxslp = MAXSLP;	/* patchable ... */
     60 struct loadavg averunnable;
     61 
     62 static void uvm_total(struct vmtotal *);
     63 
     64 /*
     65  * sysctl helper routine for the vm.vmmeter node.
     66  */
     67 static int
     68 sysctl_vm_meter(SYSCTLFN_ARGS)
     69 {
     70 	struct sysctlnode node;
     71 	struct vmtotal vmtotals;
     72 
     73 	node = *rnode;
     74 	node.sysctl_data = &vmtotals;
     75 	uvm_total(&vmtotals);
     76 
     77 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
     78 }
     79 
     80 /*
     81  * sysctl helper routine for the vm.uvmexp node.
     82  */
     83 static int
     84 sysctl_vm_uvmexp(SYSCTLFN_ARGS)
     85 {
     86 	struct sysctlnode node;
     87 
     88 	node = *rnode;
     89 	if (oldp)
     90 		node.sysctl_size = min(*oldlenp, node.sysctl_size);
     91 
     92 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
     93 }
     94 
     95 static int
     96 sysctl_vm_uvmexp2(SYSCTLFN_ARGS)
     97 {
     98 	struct sysctlnode node;
     99 	struct uvmexp_sysctl u;
    100 	int active, inactive;
    101 
    102 	uvm_estimatepageable(&active, &inactive);
    103 
    104 	memset(&u, 0, sizeof(u));
    105 
    106 	/* Entries here are in order of uvmexp_sysctl, not uvmexp */
    107 	u.pagesize = uvmexp.pagesize;
    108 	u.pagemask = uvmexp.pagemask;
    109 	u.pageshift = uvmexp.pageshift;
    110 	u.npages = uvmexp.npages;
    111 	u.free = uvmexp.free;
    112 	u.active = active;
    113 	u.inactive = inactive;
    114 	u.paging = uvmexp.paging;
    115 	u.wired = uvmexp.wired;
    116 	u.zeropages = uvmexp.zeropages;
    117 	u.reserve_pagedaemon = uvmexp.reserve_pagedaemon;
    118 	u.reserve_kernel = uvmexp.reserve_kernel;
    119 	u.freemin = uvmexp.freemin;
    120 	u.freetarg = uvmexp.freetarg;
    121 	u.inactarg = 0; /* unused */
    122 	u.wiredmax = uvmexp.wiredmax;
    123 	u.nswapdev = uvmexp.nswapdev;
    124 	u.swpages = uvmexp.swpages;
    125 	u.swpginuse = uvmexp.swpginuse;
    126 	u.swpgonly = uvmexp.swpgonly;
    127 	u.nswget = uvmexp.nswget;
    128 	u.faults = uvmexp.faults;
    129 	u.traps = uvmexp.traps;
    130 	u.intrs = uvmexp.intrs;
    131 	u.swtch = uvmexp.swtch;
    132 	u.softs = uvmexp.softs;
    133 	u.syscalls = uvmexp.syscalls;
    134 	u.pageins = uvmexp.pageins;
    135 	u.pgswapin = uvmexp.pgswapin;
    136 	u.pgswapout = uvmexp.pgswapout;
    137 	u.forks = uvmexp.forks;
    138 	u.forks_ppwait = uvmexp.forks_ppwait;
    139 	u.forks_sharevm = uvmexp.forks_sharevm;
    140 	u.pga_zerohit = uvmexp.pga_zerohit;
    141 	u.pga_zeromiss = uvmexp.pga_zeromiss;
    142 	u.zeroaborts = uvmexp.zeroaborts;
    143 	u.fltnoram = uvmexp.fltnoram;
    144 	u.fltnoanon = uvmexp.fltnoanon;
    145 	u.fltpgwait = uvmexp.fltpgwait;
    146 	u.fltpgrele = uvmexp.fltpgrele;
    147 	u.fltrelck = uvmexp.fltrelck;
    148 	u.fltrelckok = uvmexp.fltrelckok;
    149 	u.fltanget = uvmexp.fltanget;
    150 	u.fltanretry = uvmexp.fltanretry;
    151 	u.fltamcopy = uvmexp.fltamcopy;
    152 	u.fltnamap = uvmexp.fltnamap;
    153 	u.fltnomap = uvmexp.fltnomap;
    154 	u.fltlget = uvmexp.fltlget;
    155 	u.fltget = uvmexp.fltget;
    156 	u.flt_anon = uvmexp.flt_anon;
    157 	u.flt_acow = uvmexp.flt_acow;
    158 	u.flt_obj = uvmexp.flt_obj;
    159 	u.flt_prcopy = uvmexp.flt_prcopy;
    160 	u.flt_przero = uvmexp.flt_przero;
    161 	u.pdwoke = uvmexp.pdwoke;
    162 	u.pdrevs = uvmexp.pdrevs;
    163 	u.pdfreed = uvmexp.pdfreed;
    164 	u.pdscans = uvmexp.pdscans;
    165 	u.pdanscan = uvmexp.pdanscan;
    166 	u.pdobscan = uvmexp.pdobscan;
    167 	u.pdreact = uvmexp.pdreact;
    168 	u.pdbusy = uvmexp.pdbusy;
    169 	u.pdpageouts = uvmexp.pdpageouts;
    170 	u.pdpending = uvmexp.pdpending;
    171 	u.pddeact = uvmexp.pddeact;
    172 	u.anonpages = uvmexp.anonpages;
    173 	u.filepages = uvmexp.filepages;
    174 	u.execpages = uvmexp.execpages;
    175 	u.colorhit = uvmexp.colorhit;
    176 	u.colormiss = uvmexp.colormiss;
    177 	u.cpuhit = uvmexp.cpuhit;
    178 	u.cpumiss = uvmexp.cpumiss;
    179 
    180 	node = *rnode;
    181 	node.sysctl_data = &u;
    182 	node.sysctl_size = sizeof(u);
    183 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
    184 }
    185 
    186 /*
    187  * sysctl helper routine for uvm_pctparam.
    188  */
    189 static int
    190 uvm_sysctlpctparam(SYSCTLFN_ARGS)
    191 {
    192 	int t, error;
    193 	struct sysctlnode node;
    194 	struct uvm_pctparam *pct;
    195 
    196 	pct = rnode->sysctl_data;
    197 	t = pct->pct_pct;
    198 
    199 	node = *rnode;
    200 	node.sysctl_data = &t;
    201 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    202 	if (error || newp == NULL)
    203 		return error;
    204 
    205 	if (t < 0 || t > 100)
    206 		return EINVAL;
    207 
    208 	error = uvm_pctparam_check(pct, t);
    209 	if (error) {
    210 		return error;
    211 	}
    212 	uvm_pctparam_set(pct, t);
    213 
    214 	return (0);
    215 }
    216 
    217 /*
    218  * uvm_sysctl: sysctl hook into UVM system.
    219  */
    220 SYSCTL_SETUP(sysctl_vm_setup, "sysctl vm subtree setup")
    221 {
    222 
    223 	sysctl_createv(clog, 0, NULL, NULL,
    224 		       CTLFLAG_PERMANENT,
    225 		       CTLTYPE_NODE, "vm", NULL,
    226 		       NULL, 0, NULL, 0,
    227 		       CTL_VM, CTL_EOL);
    228 	sysctl_createv(clog, 0, NULL, NULL,
    229 		       CTLFLAG_PERMANENT,
    230 		       CTLTYPE_STRUCT, "vmmeter",
    231 		       SYSCTL_DESCR("Simple system-wide virtual memory "
    232 				    "statistics"),
    233 		       sysctl_vm_meter, 0, NULL, sizeof(struct vmtotal),
    234 		       CTL_VM, VM_METER, CTL_EOL);
    235 	sysctl_createv(clog, 0, NULL, NULL,
    236 		       CTLFLAG_PERMANENT,
    237 		       CTLTYPE_STRUCT, "loadavg",
    238 		       SYSCTL_DESCR("System load average history"),
    239 		       NULL, 0, &averunnable, sizeof(averunnable),
    240 		       CTL_VM, VM_LOADAVG, CTL_EOL);
    241 	sysctl_createv(clog, 0, NULL, NULL,
    242 		       CTLFLAG_PERMANENT,
    243 		       CTLTYPE_STRUCT, "uvmexp",
    244 		       SYSCTL_DESCR("Detailed system-wide virtual memory "
    245 				    "statistics"),
    246 		       sysctl_vm_uvmexp, 0, &uvmexp, sizeof(uvmexp),
    247 		       CTL_VM, VM_UVMEXP, CTL_EOL);
    248 	sysctl_createv(clog, 0, NULL, NULL,
    249 		       CTLFLAG_PERMANENT,
    250 		       CTLTYPE_INT, "nkmempages",
    251 		       SYSCTL_DESCR("Default number of pages in kmem_map"),
    252 		       NULL, 0, &nkmempages, 0,
    253 		       CTL_VM, VM_NKMEMPAGES, CTL_EOL);
    254 	sysctl_createv(clog, 0, NULL, NULL,
    255 		       CTLFLAG_PERMANENT,
    256 		       CTLTYPE_STRUCT, "uvmexp2",
    257 		       SYSCTL_DESCR("Detailed system-wide virtual memory "
    258 				    "statistics (MI)"),
    259 		       sysctl_vm_uvmexp2, 0, NULL, 0,
    260 		       CTL_VM, VM_UVMEXP2, CTL_EOL);
    261 	sysctl_createv(clog, 0, NULL, NULL,
    262 		       CTLFLAG_PERMANENT, CTLTYPE_INT, "maxslp",
    263 		       SYSCTL_DESCR("Maximum process sleep time before being "
    264 				    "swapped"),
    265 		       NULL, 0, &maxslp, 0,
    266 		       CTL_VM, VM_MAXSLP, CTL_EOL);
    267 	sysctl_createv(clog, 0, NULL, NULL,
    268 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    269 		       CTLTYPE_INT, "uspace",
    270 		       SYSCTL_DESCR("Number of bytes allocated for a kernel "
    271 				    "stack"),
    272 		       NULL, USPACE, NULL, 0,
    273 		       CTL_VM, VM_USPACE, CTL_EOL);
    274 	sysctl_createv(clog, 0, NULL, NULL,
    275 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    276 		       CTLTYPE_BOOL, "idlezero",
    277 		       SYSCTL_DESCR("Whether try to zero pages in idle loop"),
    278 		       NULL, 0, &vm_page_zero_enable, 0,
    279 		       CTL_VM, CTL_CREATE, CTL_EOL);
    280 
    281 	uvmpdpol_sysctlsetup();
    282 }
    283 
    284 /*
    285  * uvm_total: calculate the current state of the system.
    286  */
    287 static void
    288 uvm_total(struct vmtotal *totalp)
    289 {
    290 	struct lwp *l;
    291 #if 0
    292 	struct vm_map_entry *	entry;
    293 	struct vm_map *map;
    294 	int paging;
    295 #endif
    296 	int active;
    297 
    298 	memset(totalp, 0, sizeof *totalp);
    299 
    300 	/*
    301 	 * calculate process statistics
    302 	 */
    303 	mutex_enter(proc_lock);
    304 	LIST_FOREACH(l, &alllwp, l_list) {
    305 		if (l->l_proc->p_flag & PK_SYSTEM)
    306 			continue;
    307 		switch (l->l_stat) {
    308 		case 0:
    309 			continue;
    310 
    311 		case LSSLEEP:
    312 		case LSSTOP:
    313 			if (lwp_eprio(l) <= PZERO) {
    314 				totalp->t_dw++;
    315 			} else if (l->l_slptime < maxslp) {
    316 				totalp->t_sl++;
    317 			}
    318 			if (l->l_slptime >= maxslp)
    319 				continue;
    320 			break;
    321 
    322 		case LSRUN:
    323 		case LSONPROC:
    324 		case LSIDL:
    325 			totalp->t_rq++;
    326 			if (l->l_stat == LSIDL)
    327 				continue;
    328 			break;
    329 		}
    330 		/*
    331 		 * note active objects
    332 		 */
    333 #if 0
    334 		/*
    335 		 * XXXCDC: BOGUS!  rethink this.  in the mean time
    336 		 * don't do it.
    337 		 */
    338 		paging = 0;
    339 		vm_map_lock(map);
    340 		for (map = &p->p_vmspace->vm_map, entry = map->header.next;
    341 		    entry != &map->header; entry = entry->next) {
    342 			if (entry->is_a_map || entry->is_sub_map ||
    343 			    entry->object.uvm_obj == NULL)
    344 				continue;
    345 			/* XXX how to do this with uvm */
    346 		}
    347 		vm_map_unlock(map);
    348 		if (paging)
    349 			totalp->t_pw++;
    350 #endif
    351 	}
    352 	mutex_exit(proc_lock);
    353 
    354 	/*
    355 	 * Calculate object memory usage statistics.
    356 	 */
    357 	uvm_estimatepageable(&active, NULL);
    358 	totalp->t_free = uvmexp.free;
    359 	totalp->t_vm = uvmexp.npages - uvmexp.free + uvmexp.swpginuse;
    360 	totalp->t_avm = active + uvmexp.swpginuse;	/* XXX */
    361 	totalp->t_rm = uvmexp.npages - uvmexp.free;
    362 	totalp->t_arm = active;
    363 	totalp->t_vmshr = 0;		/* XXX */
    364 	totalp->t_avmshr = 0;		/* XXX */
    365 	totalp->t_rmshr = 0;		/* XXX */
    366 	totalp->t_armshr = 0;		/* XXX */
    367 }
    368 
    369 void
    370 uvm_pctparam_set(struct uvm_pctparam *pct, int val)
    371 {
    372 
    373 	pct->pct_pct = val;
    374 	pct->pct_scaled = val * UVM_PCTPARAM_SCALE / 100;
    375 }
    376 
    377 int
    378 uvm_pctparam_get(struct uvm_pctparam *pct)
    379 {
    380 
    381 	return pct->pct_pct;
    382 }
    383 
    384 int
    385 uvm_pctparam_check(struct uvm_pctparam *pct, int val)
    386 {
    387 
    388 	if (pct->pct_check == NULL) {
    389 		return 0;
    390 	}
    391 	return (*pct->pct_check)(pct, val);
    392 }
    393 
    394 void
    395 uvm_pctparam_init(struct uvm_pctparam *pct, int val,
    396     int (*fn)(struct uvm_pctparam *, int))
    397 {
    398 
    399 	pct->pct_check = fn;
    400 	uvm_pctparam_set(pct, val);
    401 }
    402 
    403 int
    404 uvm_pctparam_createsysctlnode(struct uvm_pctparam *pct, const char *name,
    405     const char *desc)
    406 {
    407 
    408 	return sysctl_createv(NULL, 0, NULL, NULL,
    409 	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    410 	    CTLTYPE_INT, name, SYSCTL_DESCR(desc),
    411 	    uvm_sysctlpctparam, 0, pct, 0, CTL_VM, CTL_CREATE, CTL_EOL);
    412 }
    413