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