Home | History | Annotate | Line # | Download | only in systat
bufcache.c revision 1.10
      1  1.10  simonb /*	$NetBSD: bufcache.c,v 1.10 2000/12/01 02:19:43 simonb Exp $	*/
      2   1.1  simonb 
      3   1.1  simonb /*-
      4   1.1  simonb  * Copyright (c) 1999 The NetBSD Foundation, Inc.
      5   1.1  simonb  * All rights reserved.
      6   1.1  simonb  *
      7   1.1  simonb  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1  simonb  * by Simon Burge.
      9   1.1  simonb  *
     10   1.1  simonb  * Redistribution and use in source and binary forms, with or without
     11   1.1  simonb  * modification, are permitted provided that the following conditions
     12   1.1  simonb  * are met:
     13   1.1  simonb  * 1. Redistributions of source code must retain the above copyright
     14   1.1  simonb  *    notice, this list of conditions and the following disclaimer.
     15   1.1  simonb  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1  simonb  *    notice, this list of conditions and the following disclaimer in the
     17   1.1  simonb  *    documentation and/or other materials provided with the distribution.
     18   1.1  simonb  * 3. All advertising materials mentioning features or use of this software
     19   1.1  simonb  *    must display the following acknowledgement:
     20   1.1  simonb  *        This product includes software developed by the NetBSD
     21   1.1  simonb  *        Foundation, Inc. and its contributors.
     22   1.1  simonb  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23   1.1  simonb  *    contributors may be used to endorse or promote products derived
     24   1.1  simonb  *    from this software without specific prior written permission.
     25   1.1  simonb  *
     26   1.1  simonb  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27   1.1  simonb  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28   1.1  simonb  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29   1.1  simonb  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30   1.1  simonb  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31   1.1  simonb  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32   1.1  simonb  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33   1.1  simonb  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34   1.1  simonb  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35   1.1  simonb  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36   1.1  simonb  * POSSIBILITY OF SUCH DAMAGE.
     37   1.1  simonb  */
     38   1.1  simonb 
     39   1.1  simonb #include <sys/cdefs.h>
     40   1.1  simonb #ifndef lint
     41  1.10  simonb __RCSID("$NetBSD: bufcache.c,v 1.10 2000/12/01 02:19:43 simonb Exp $");
     42   1.1  simonb #endif /* not lint */
     43   1.1  simonb 
     44   1.1  simonb #include <sys/param.h>
     45   1.1  simonb #include <sys/buf.h>
     46   1.1  simonb #include <sys/mount.h>
     47   1.9  simonb #include <sys/sysctl.h>
     48   1.1  simonb #include <sys/vnode.h>
     49   1.1  simonb 
     50   1.9  simonb #include <uvm/uvm_extern.h>
     51   1.9  simonb 
     52   1.1  simonb #include <err.h>
     53   1.9  simonb #include <errno.h>
     54   1.1  simonb #include <kvm.h>
     55   1.1  simonb #include <nlist.h>
     56   1.1  simonb #include <stdlib.h>
     57   1.9  simonb #include <string.h>
     58   1.2     mrg #include <unistd.h>
     59   1.1  simonb 
     60   1.1  simonb #include "systat.h"
     61   1.1  simonb #include "extern.h"
     62   1.1  simonb 
     63   1.1  simonb /*
     64   1.1  simonb  * Definitions for the buffer free lists (from sys/kern/vfs_bio.c).
     65   1.1  simonb  */
     66   1.1  simonb #define	BQUEUES		4		/* number of free buffer queues */
     67   1.1  simonb 
     68   1.1  simonb #define	BQ_LOCKED	0		/* super-blocks &c */
     69   1.1  simonb #define	BQ_LRU		1		/* lru, useful buffers */
     70   1.1  simonb #define	BQ_AGE		2		/* rubbish */
     71   1.1  simonb #define	BQ_EMPTY	3		/* buffer headers with no memory */
     72   1.1  simonb 
     73   1.1  simonb #define VCACHE_SIZE	50
     74   1.1  simonb 
     75   1.1  simonb struct vcache {
     76   1.1  simonb 	int vc_age;
     77   1.1  simonb 	struct vnode *vc_addr;
     78   1.1  simonb 	struct vnode vc_node;
     79   1.1  simonb };
     80   1.1  simonb 
     81   1.1  simonb struct ml_entry {
     82   1.1  simonb 	int ml_count;
     83   1.1  simonb 	long ml_size;
     84   1.1  simonb 	long ml_valid;
     85   1.1  simonb 	struct mount *ml_addr;
     86   1.1  simonb 	struct mount ml_mount;
     87   1.1  simonb 	LIST_ENTRY(ml_entry) ml_entries;
     88   1.1  simonb };
     89   1.1  simonb 
     90   1.1  simonb static struct nlist namelist[] = {
     91   1.1  simonb #define	X_NBUF		0
     92   1.1  simonb 	{ "_nbuf" },
     93   1.1  simonb #define	X_BUF		1
     94   1.1  simonb 	{ "_buf" },
     95   1.1  simonb #define	X_BUFQUEUES	2
     96   1.1  simonb 	{ "_bufqueues" },
     97   1.1  simonb #define	X_BUFPAGES	3
     98   1.1  simonb 	{ "_bufpages" },
     99   1.1  simonb 	{ "" },
    100   1.1  simonb };
    101   1.1  simonb 
    102   1.1  simonb static struct vcache vcache[VCACHE_SIZE];
    103   1.1  simonb static LIST_HEAD(mount_list, ml_entry) mount_list;
    104   1.1  simonb 
    105   1.6   soren static int nbuf, bufpages, bufkb;
    106   1.9  simonb static struct uvmexp_sysctl uvmexp;
    107   1.1  simonb static void *bufaddr;
    108   1.1  simonb static struct buf *buf = NULL;
    109   1.1  simonb static TAILQ_HEAD(bqueues, buf) bufqueues[BQUEUES];
    110   1.1  simonb 
    111   1.8      ad static void	vc_init(void);
    112   1.8      ad static void	ml_init(void);
    113   1.8      ad static struct 	vnode *vc_lookup(struct vnode *);
    114   1.8      ad static struct 	mount *ml_lookup(struct mount *, int, int);
    115   1.1  simonb 
    116   1.1  simonb 
    117   1.1  simonb WINDOW *
    118   1.8      ad openbufcache(void)
    119   1.1  simonb {
    120   1.1  simonb 
    121   1.1  simonb 	return (subwin(stdscr, LINES-5-1, 0, 5, 0));
    122   1.1  simonb }
    123   1.1  simonb 
    124   1.1  simonb void
    125   1.8      ad closebufcache(WINDOW *w)
    126   1.1  simonb {
    127   1.1  simonb 
    128   1.1  simonb 	if (w == NULL)
    129   1.1  simonb 		return;
    130   1.1  simonb 	wclear(w);
    131   1.1  simonb 	wrefresh(w);
    132   1.1  simonb 	delwin(w);
    133   1.1  simonb 	ml_init();		/* Clear out mount list */
    134   1.1  simonb }
    135   1.1  simonb 
    136   1.1  simonb void
    137   1.8      ad labelbufcache(void)
    138   1.1  simonb {
    139   1.9  simonb 	mvwprintw(wnd, 0, 0,
    140   1.9  simonb 	    "There are %d metadata buffers using %d kBytes of memory.",
    141   1.6   soren 	    nbuf, bufkb);
    142   1.1  simonb 	wclrtoeol(wnd);
    143   1.1  simonb 	wmove(wnd, 1, 0);
    144   1.9  simonb 	wmove(wnd, 2, 0);
    145   1.1  simonb 	wclrtoeol(wnd);
    146   1.9  simonb 	mvwaddstr(wnd, 3, 0,
    147   1.4     mrg "File System          Bufs used   %   kB in use   %  Bufsize kB   %  Util %");
    148   1.1  simonb 	wclrtoeol(wnd);
    149   1.1  simonb }
    150   1.1  simonb 
    151   1.1  simonb void
    152   1.8      ad showbufcache(void)
    153   1.1  simonb {
    154   1.1  simonb 	int tbuf, i, lastrow;
    155   1.1  simonb 	long tvalid, tsize;
    156   1.1  simonb 	struct ml_entry *ml;
    157   1.1  simonb 
    158   1.9  simonb 	mvwprintw(wnd, 1, 0,
    159   1.9  simonb 	    "There are %llu pages for vnode page cache using %llu kBytes of memory.",
    160   1.9  simonb 	    (long long)uvmexp.vnodepages,
    161   1.9  simonb 	    (long long) uvmexp.vnodepages * getpagesize() / 1024);
    162   1.9  simonb 	wclrtoeol(wnd);
    163   1.9  simonb 
    164   1.1  simonb 	tbuf = tvalid = tsize = 0;
    165   1.9  simonb 	lastrow = 4;	/* Leave room for header. */
    166   1.1  simonb 	for (i = lastrow, ml = LIST_FIRST(&mount_list); ml != NULL;
    167   1.1  simonb 	    i++, ml = LIST_NEXT(ml, ml_entries)) {
    168   1.1  simonb 
    169   1.1  simonb 		/* Display in window if enough room. */
    170   1.1  simonb 		if (i < getmaxy(wnd) - 2) {
    171   1.1  simonb 			mvwprintw(wnd, i, 0, "%-20.20s", ml->ml_addr == NULL ?
    172   1.1  simonb 			    "NULL" : ml->ml_mount.mnt_stat.f_mntonname);
    173   1.4     mrg 			wprintw(wnd,
    174   1.7     jdc 			    "    %6d %3d    %8ld %3ld    %8ld %3ld     %3ld",
    175   1.6   soren 			    ml->ml_count, (100 * ml->ml_count) / nbuf,
    176   1.6   soren 			    ml->ml_valid, (100 * ml->ml_valid) / bufkb,
    177   1.6   soren 			    ml->ml_size, (100 * ml->ml_size) / bufkb,
    178   1.4     mrg 			    (100 * ml->ml_valid) / ml->ml_size);
    179   1.1  simonb 			wclrtoeol(wnd);
    180   1.1  simonb 			lastrow = i;
    181   1.1  simonb 		}
    182   1.1  simonb 
    183   1.1  simonb 		/* Update statistics. */
    184   1.1  simonb 		tbuf += ml->ml_count;
    185   1.6   soren 		tvalid += ml->ml_valid;
    186   1.6   soren 		tsize += ml->ml_size;
    187   1.1  simonb 	}
    188   1.1  simonb 
    189   1.1  simonb 	wclrtobot(wnd);
    190   1.4     mrg 	mvwprintw(wnd, lastrow + 2, 0,
    191   1.7     jdc 	    "%-20s    %6d %3d    %8ld %3ld    %8ld %3ld     %3ld",
    192   1.4     mrg 	    "Total:", tbuf, (100 * tbuf) / nbuf,
    193   1.6   soren 	    tvalid, (100 * tvalid) / bufkb,
    194   1.6   soren 	    tsize, (100 * tsize) / bufkb, (100 * tvalid) / tsize);
    195   1.1  simonb }
    196   1.1  simonb 
    197   1.1  simonb int
    198   1.8      ad initbufcache(void)
    199   1.1  simonb {
    200   1.1  simonb 	if (namelist[X_NBUF].n_type == 0) {
    201   1.1  simonb 		if (kvm_nlist(kd, namelist)) {
    202   1.1  simonb 			nlisterr(namelist);
    203   1.1  simonb 			return(0);
    204   1.1  simonb 		}
    205   1.1  simonb 		if (namelist[X_NBUF].n_type == 0) {
    206  1.10  simonb 			error("No namelist");
    207   1.1  simonb 			return(0);
    208   1.1  simonb 		}
    209   1.1  simonb 	}
    210   1.1  simonb 
    211   1.1  simonb 	NREAD(X_NBUF, &nbuf, sizeof(nbuf));
    212   1.1  simonb 	NREAD(X_BUFPAGES, &bufpages, sizeof(bufpages));
    213   1.6   soren 	bufkb = bufpages * sysconf(_SC_PAGESIZE) / 1024;
    214   1.1  simonb 
    215   1.5   jwise 	if ((buf = malloc(nbuf * sizeof(struct buf))) == NULL) {
    216   1.5   jwise 		error("malloc failed");
    217   1.5   jwise 		die(0);
    218   1.5   jwise 	}
    219   1.1  simonb 	NREAD(X_BUF, &bufaddr, sizeof(bufaddr));
    220   1.1  simonb 
    221   1.1  simonb 	return(1);
    222   1.1  simonb }
    223   1.1  simonb 
    224   1.1  simonb void
    225   1.8      ad fetchbufcache(void)
    226   1.1  simonb {
    227   1.9  simonb 	int i, count, mib[2];
    228   1.9  simonb 	size_t size;
    229   1.1  simonb 	struct buf *bp;
    230   1.1  simonb 	struct vnode *vn;
    231   1.1  simonb 	struct mount *mt;
    232   1.1  simonb 	struct ml_entry *ml;
    233   1.9  simonb 
    234   1.9  simonb 	/* Re-read pages used for vnodes */
    235   1.9  simonb 	size = sizeof(uvmexp);
    236   1.9  simonb 	mib[0] = CTL_VM;
    237   1.9  simonb 	mib[1] = VM_UVMEXP2;
    238   1.9  simonb 	if (sysctl(mib, 2, &uvmexp, &size, NULL, 0) < 0) {
    239   1.9  simonb 		error("can't get uvmexp: %s\n", strerror(errno));
    240   1.9  simonb 		memset(&uvmexp, 0, sizeof(uvmexp));
    241   1.9  simonb 	}
    242   1.1  simonb 
    243   1.1  simonb 	/* Re-read bufqueues lists and buffer cache headers */
    244   1.1  simonb 	NREAD(X_BUFQUEUES, bufqueues, sizeof(bufqueues));
    245   1.1  simonb 	KREAD(bufaddr, buf, sizeof(struct buf) * nbuf);
    246   1.1  simonb 
    247   1.1  simonb 	/* Initialise vnode cache and mount list. */
    248   1.1  simonb 	vc_init();
    249   1.1  simonb 	ml_init();
    250   1.1  simonb 	for (i = 0; i < BQUEUES; i++) {
    251   1.1  simonb 		for (bp = bufqueues[i].tqh_first; bp != NULL;
    252   1.1  simonb 		    bp = bp->b_freelist.tqe_next) {
    253   1.1  simonb 			if (bp != NULL) {
    254   1.1  simonb 				bp = (struct buf *)((u_long)bp + (u_long)buf -
    255   1.1  simonb 				    (u_long)bufaddr);
    256   1.1  simonb 
    257   1.1  simonb 				if (bp->b_vp != NULL) {
    258   1.1  simonb 					vn = vc_lookup(bp->b_vp);
    259   1.1  simonb 					if (vn == NULL)
    260   1.1  simonb 						errx(1,
    261   1.1  simonb 						    "vc_lookup returns NULL!\n");
    262   1.1  simonb 					if (vn->v_mount != NULL)
    263   1.1  simonb 						mt = ml_lookup(vn->v_mount,
    264   1.1  simonb 						    bp->b_bufsize,
    265   1.1  simonb 						    bp->b_bcount);
    266   1.1  simonb 				}
    267   1.1  simonb 			}
    268   1.1  simonb 		}
    269   1.1  simonb 	}
    270   1.1  simonb 
    271   1.1  simonb 	/* simple sort - there's not that many entries */
    272   1.1  simonb 	do {
    273   1.1  simonb 		if ((ml = LIST_FIRST(&mount_list)) == NULL ||
    274   1.1  simonb 		    LIST_NEXT(ml, ml_entries) == NULL)
    275   1.1  simonb 			break;
    276   1.1  simonb 
    277   1.1  simonb 		count = 0;
    278   1.1  simonb 		for (ml = LIST_FIRST(&mount_list); ml != NULL;
    279   1.1  simonb 		    ml = LIST_NEXT(ml, ml_entries)) {
    280   1.1  simonb 			if (LIST_NEXT(ml, ml_entries) == NULL)
    281   1.1  simonb 				break;
    282   1.1  simonb 			if (ml->ml_count < LIST_NEXT(ml, ml_entries)->ml_count) {
    283   1.1  simonb 				ml = LIST_NEXT(ml, ml_entries);
    284   1.1  simonb 				LIST_REMOVE(ml, ml_entries);
    285   1.1  simonb 				LIST_INSERT_HEAD(&mount_list, ml, ml_entries);
    286   1.1  simonb 				count++;
    287   1.1  simonb 			}
    288   1.1  simonb 		}
    289   1.1  simonb 	} while (count != 0);
    290   1.1  simonb }
    291   1.1  simonb 
    292   1.1  simonb static void
    293   1.8      ad vc_init(void)
    294   1.1  simonb {
    295   1.1  simonb 	int i;
    296   1.1  simonb 
    297   1.1  simonb 	/* vc_addr == NULL for unused cache entry. */
    298   1.1  simonb 	for (i = 0; i < VCACHE_SIZE; i++)
    299   1.1  simonb 		vcache[i].vc_addr = NULL;
    300   1.1  simonb }
    301   1.1  simonb 
    302   1.1  simonb static void
    303   1.8      ad ml_init(void)
    304   1.1  simonb {
    305   1.1  simonb 	struct ml_entry *ml;
    306   1.1  simonb 
    307   1.1  simonb 	/* Throw out the current mount list and start again. */
    308   1.1  simonb 	while ((ml = LIST_FIRST(&mount_list)) != NULL) {
    309   1.1  simonb 		LIST_REMOVE(ml, ml_entries);
    310   1.1  simonb 		free(ml);
    311   1.1  simonb 	}
    312   1.1  simonb }
    313   1.1  simonb 
    314   1.1  simonb 
    315   1.1  simonb static struct vnode *
    316   1.8      ad vc_lookup(struct vnode *vaddr)
    317   1.1  simonb {
    318   1.1  simonb 	struct vnode *ret;
    319   1.1  simonb 	int i, oldest, match;
    320   1.1  simonb 
    321   1.1  simonb 	ret = NULL;
    322   1.1  simonb 	oldest = match = 0;
    323   1.1  simonb 	for (i = 0; i < VCACHE_SIZE || vcache[i].vc_addr == NULL; i++) {
    324   1.1  simonb 		vcache[i].vc_age++;
    325   1.1  simonb 		if (vcache[i].vc_addr == NULL)
    326   1.1  simonb 			break;
    327   1.1  simonb 		if (vcache[i].vc_age < vcache[oldest].vc_age)
    328   1.1  simonb 			oldest = i;
    329   1.1  simonb 		if (vcache[i].vc_addr == vaddr) {
    330   1.1  simonb 			vcache[i].vc_age = 0;
    331   1.1  simonb 			match = i;
    332   1.1  simonb 			ret = &vcache[i].vc_node;
    333   1.1  simonb 		}
    334   1.1  simonb 	}
    335   1.1  simonb 
    336   1.1  simonb 	/* Find an entry in the cache? */
    337   1.1  simonb 	if (ret != NULL)
    338   1.1  simonb 		return(ret);
    339   1.1  simonb 
    340   1.1  simonb 	/* Go past the end of the cache? */
    341   1.1  simonb 	if  (i >= VCACHE_SIZE)
    342   1.1  simonb 		i = oldest;
    343   1.1  simonb 
    344   1.1  simonb 	/* Read in new vnode and reset age counter. */
    345   1.1  simonb 	KREAD(vaddr, &vcache[i].vc_node, sizeof(struct vnode));
    346   1.1  simonb 	vcache[i].vc_addr = vaddr;
    347   1.1  simonb 	vcache[i].vc_age = 0;
    348   1.1  simonb 
    349   1.1  simonb 	return(&vcache[i].vc_node);
    350   1.1  simonb }
    351   1.1  simonb 
    352   1.1  simonb static struct mount *
    353   1.8      ad ml_lookup(struct mount *maddr, int size, int valid)
    354   1.1  simonb {
    355   1.1  simonb 	struct ml_entry *ml;
    356   1.1  simonb 
    357   1.1  simonb 	for (ml = LIST_FIRST(&mount_list); ml != NULL;
    358   1.1  simonb 	    ml = LIST_NEXT(ml, ml_entries))
    359   1.1  simonb 		if (ml->ml_addr == maddr) {
    360   1.1  simonb 			ml->ml_count++;
    361   1.6   soren 			ml->ml_size += size / 1024;
    362   1.6   soren 			ml->ml_valid += valid / 1024;
    363   1.1  simonb 			if (ml->ml_addr == NULL)
    364   1.1  simonb 				return(NULL);
    365   1.1  simonb 			else
    366   1.1  simonb 				return(&ml->ml_mount);
    367   1.1  simonb 		}
    368   1.1  simonb 
    369   1.5   jwise 	if ((ml = malloc(sizeof(struct ml_entry))) == NULL) {
    370   1.5   jwise 		error("out of memory");
    371   1.5   jwise 		die(0);
    372   1.5   jwise 	}
    373   1.1  simonb 	LIST_INSERT_HEAD(&mount_list, ml, ml_entries);
    374   1.1  simonb 	ml->ml_count = 1;
    375   1.6   soren 	ml->ml_size = size / 1024;
    376   1.6   soren 	ml->ml_valid = valid / 1024;
    377   1.1  simonb 	ml->ml_addr = maddr;
    378   1.1  simonb 	if (maddr == NULL)
    379   1.1  simonb 		return(NULL);
    380   1.1  simonb 
    381   1.1  simonb 	KREAD(maddr, &ml->ml_mount, sizeof(struct mount));
    382   1.1  simonb 	return(&ml->ml_mount);
    383   1.1  simonb }
    384