Home | History | Annotate | Line # | Download | only in systat
bufcache.c revision 1.21
      1 /*	$NetBSD: bufcache.c,v 1.21 2008/01/24 17:32:58 ad 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.21 2008/01/24 17:32:58 ad Exp $");
     42 #endif /* not lint */
     43 
     44 #include <sys/param.h>
     45 #include <sys/buf.h>
     46 #include <sys/mount.h>
     47 #include <sys/sysctl.h>
     48 #include <sys/vnode.h>
     49 
     50 #include <uvm/uvm_extern.h>
     51 
     52 #include <err.h>
     53 #include <errno.h>
     54 #include <inttypes.h>
     55 #include <math.h>
     56 #include <stdlib.h>
     57 #include <string.h>
     58 #include <unistd.h>
     59 #include <stdbool.h>
     60 
     61 #include <miscfs/specfs/specdev.h>
     62 
     63 #include "systat.h"
     64 #include "extern.h"
     65 
     66 #define VCACHE_SIZE	50
     67 #define	PAGEINFO_ROWS	 5
     68 
     69 struct vcache {
     70 	int vc_age;
     71 	struct vnode *vc_addr;
     72 	struct vnode vc_node;
     73 };
     74 
     75 struct ml_entry {
     76 	u_int ml_count;
     77 	u_long ml_size;
     78 	u_long ml_valid;
     79 	struct mount *ml_addr;
     80 	LIST_ENTRY(ml_entry) ml_entries;
     81 	struct mount ml_mount;
     82 };
     83 
     84 static struct nlist namelist[] = {
     85 #define	X_BUFMEM	0
     86 	{ .n_name = "_bufmem" },
     87 	{ .n_name = NULL },
     88 };
     89 
     90 static struct vcache vcache[VCACHE_SIZE];
     91 static LIST_HEAD(mount_list, ml_entry) mount_list;
     92 
     93 static u_long bufmem;
     94 static u_int nbuf, pgwidth, kbwidth;
     95 static struct uvmexp_sysctl uvmexp;
     96 
     97 static void	vc_init(void);
     98 static void	ml_init(void);
     99 static struct 	vnode *vc_lookup(struct vnode *);
    100 static struct 	mount *ml_lookup(struct mount *, int, int);
    101 static void	fetchuvmexp(void);
    102 
    103 
    104 WINDOW *
    105 openbufcache(void)
    106 {
    107 
    108 	return (subwin(stdscr, -1, 0, 5, 0));
    109 }
    110 
    111 void
    112 closebufcache(WINDOW *w)
    113 {
    114 
    115 	if (w == NULL)
    116 		return;
    117 	wclear(w);
    118 	wrefresh(w);
    119 	delwin(w);
    120 	ml_init();		/* Clear out mount list */
    121 }
    122 
    123 void
    124 labelbufcache(void)
    125 {
    126 	int i;
    127 
    128 	for (i = 0; i <= PAGEINFO_ROWS; i++) {
    129 		wmove(wnd, i, 0);
    130 		wclrtoeol(wnd);
    131 	}
    132 	mvwaddstr(wnd, PAGEINFO_ROWS + 1, 0, "File System          Bufs used"
    133 	    "   %   kB in use   %  Bufsize kB   %  Util %");
    134 	wclrtoeol(wnd);
    135 }
    136 
    137 void
    138 showbufcache(void)
    139 {
    140 	int tbuf, i, lastrow;
    141 	double tvalid, tsize;
    142 	struct ml_entry *ml;
    143 
    144 	NREAD(X_BUFMEM, &bufmem, sizeof(bufmem));
    145 
    146 	mvwprintw(wnd, 0, 0,
    147 	    "   %*d metadata buffers using             %*ld kBytes of "
    148 	    "memory (%2.0f%%).",
    149 	    pgwidth, nbuf, kbwidth, bufmem / 1024,
    150 	    ((bufmem * 100.0) + 0.5) / getpagesize() / uvmexp.npages);
    151 	wclrtoeol(wnd);
    152 	mvwprintw(wnd, 1, 0,
    153 	    "   %*" PRIu64 " pages for cached file data using   %*"
    154 	    PRIu64 " kBytes of memory (%2.0f%%).",
    155 	    pgwidth, uvmexp.filepages,
    156 	    kbwidth, uvmexp.filepages * getpagesize() / 1024,
    157 	    (uvmexp.filepages * 100 + 0.5) / uvmexp.npages);
    158 	wclrtoeol(wnd);
    159 	mvwprintw(wnd, 2, 0,
    160 	    "   %*" PRIu64 " pages for executables using        %*"
    161 	    PRIu64 " kBytes of memory (%2.0f%%).",
    162 	    pgwidth, uvmexp.execpages,
    163 	    kbwidth, uvmexp.execpages * getpagesize() / 1024,
    164 	    (uvmexp.execpages * 100 + 0.5) / uvmexp.npages);
    165 	wclrtoeol(wnd);
    166 	mvwprintw(wnd, 3, 0,
    167 	    "   %*" PRIu64 " pages for anon (non-file) data     %*"
    168 	    PRIu64 " kBytes of memory (%2.0f%%).",
    169 	    pgwidth, uvmexp.anonpages,
    170 	    kbwidth, uvmexp.anonpages * getpagesize() / 1024,
    171 	    (uvmexp.anonpages * 100 + 0.5) / uvmexp.npages);
    172 	wclrtoeol(wnd);
    173 	mvwprintw(wnd, 4, 0,
    174 	    "   %*" PRIu64 " free pages                         %*"
    175 	    PRIu64 " kBytes of memory (%2.0f%%).",
    176 	    pgwidth, uvmexp.free,
    177 	    kbwidth, uvmexp.free * getpagesize() / 1024,
    178 	    (uvmexp.free * 100 + 0.5) / uvmexp.npages);
    179 	wclrtoeol(wnd);
    180 
    181 	if (nbuf == 0 || bufmem == 0) {
    182 		wclrtobot(wnd);
    183 		return;
    184 	}
    185 
    186 	tbuf = 0;
    187 	tvalid = tsize = 0;
    188 	lastrow = PAGEINFO_ROWS + 2;	/* Leave room for header. */
    189 	for (i = lastrow, ml = LIST_FIRST(&mount_list); ml != NULL;
    190 	    i++, ml = LIST_NEXT(ml, ml_entries)) {
    191 
    192 		int cnt = ml->ml_count;
    193 		double v = ml->ml_valid;
    194 		double s = ml->ml_size;
    195 
    196 		/* Display in window if enough room. */
    197 		if (i < getmaxy(wnd) - 2) {
    198 			mvwprintw(wnd, i, 0, "%-20.20s", ml->ml_addr == NULL ?
    199 			    "NULL" : ml->ml_mount.mnt_stat.f_mntonname);
    200 			wprintw(wnd,
    201 			    "    %6d %3d    %8ld %3.0f    %8ld %3.0f     %3.0f",
    202 			    cnt, (100 * cnt) / nbuf,
    203 			    (long)(v/1024), 100 * v / bufmem,
    204 			    (long)(s/1024), 100 * s / bufmem,
    205 			    100 * v / s);
    206 			wclrtoeol(wnd);
    207 			lastrow = i;
    208 		}
    209 
    210 		/* Update statistics. */
    211 		tbuf += cnt;
    212 		tvalid += v;
    213 		tsize += s;
    214 	}
    215 
    216 	wclrtobot(wnd);
    217 	mvwprintw(wnd, lastrow + 2, 0,
    218 	    "%-20s    %6d %3d    %8ld %3.0f    %8ld %3.0f     %3.0f",
    219 	    "Total:", tbuf, (100 * tbuf) / nbuf,
    220 	    (long)(tvalid/1024), 100 * tvalid / bufmem,
    221 	    (long)(tsize/1024), 100 * tsize / bufmem,
    222 	    tsize != 0 ? ((100 * tvalid) / tsize) : 0);
    223 }
    224 
    225 int
    226 initbufcache(void)
    227 {
    228 	if (namelist[0].n_type == 0) {
    229 		if (kvm_nlist(kd, namelist)) {
    230 			nlisterr(namelist);
    231 			return(0);
    232 		}
    233 	}
    234 
    235 	fetchuvmexp();
    236 	pgwidth = (int)(floor(log10((double)uvmexp.npages)) + 1);
    237 	kbwidth = (int)(floor(log10(uvmexp.npages * getpagesize() / 1024.0)) +
    238 	    1);
    239 
    240 	return(1);
    241 }
    242 
    243 static void
    244 fetchuvmexp(void)
    245 {
    246 	int mib[2];
    247 	size_t size;
    248 
    249 	/* Re-read pages used for vnodes & executables */
    250 	size = sizeof(uvmexp);
    251 	mib[0] = CTL_VM;
    252 	mib[1] = VM_UVMEXP2;
    253 	if (sysctl(mib, 2, &uvmexp, &size, NULL, 0) < 0) {
    254 		error("can't get uvmexp: %s\n", strerror(errno));
    255 		memset(&uvmexp, 0, sizeof(uvmexp));
    256 	}
    257 }
    258 
    259 void
    260 fetchbufcache(void)
    261 {
    262 	int count;
    263 	struct buf_sysctl *bp, *buffers;
    264 	struct vnode *vn;
    265 	struct mount *mt;
    266 	struct ml_entry *ml;
    267 	int mib[6];
    268 	size_t size;
    269 	int extraslop = 0;
    270 
    271 	/* Re-read pages used for vnodes & executables */
    272 	fetchuvmexp();
    273 
    274 	/* Initialise vnode cache and mount list. */
    275 	vc_init();
    276 	ml_init();
    277 
    278 	/* Get metadata buffers */
    279 	size = 0;
    280 	buffers = NULL;
    281 	mib[0] = CTL_KERN;
    282 	mib[1] = KERN_BUF;
    283 	mib[2] = KERN_BUF_ALL;
    284 	mib[3] = KERN_BUF_ALL;
    285 	mib[4] = (int)sizeof(struct buf_sysctl);
    286 	mib[5] = INT_MAX; /* we want them all */
    287 again:
    288 	if (sysctl(mib, 6, NULL, &size, NULL, 0) < 0) {
    289 		error("can't get buffers size: %s\n", strerror(errno));
    290 		return;
    291 	}
    292 	if (size == 0)
    293 		return;
    294 
    295 	size += extraslop * sizeof(struct buf_sysctl);
    296 	buffers = malloc(size);
    297 	if (buffers == NULL) {
    298 		error("can't allocate buffers: %s\n", strerror(errno));
    299 		return;
    300 	}
    301 	if (sysctl(mib, 6, buffers, &size, NULL, 0) < 0) {
    302 		free(buffers);
    303 		if (extraslop == 0) {
    304 			extraslop = 100;
    305 			goto again;
    306 		}
    307 		error("can't get buffers: %s\n", strerror(errno));
    308 		return;
    309 	}
    310 
    311 	nbuf = size / sizeof(struct buf_sysctl);
    312 	for (bp = buffers; bp < buffers + nbuf; bp++) {
    313 		if (UINT64TOPTR(bp->b_vp) != NULL) {
    314 			struct mount *mp;
    315 			vn = vc_lookup(UINT64TOPTR(bp->b_vp));
    316 			if (vn == NULL)
    317 				break;
    318 
    319 			mp = vn->v_mount;
    320 			/*
    321 			 * References to mounted-on vnodes should be
    322 			 * counted towards the mounted filesystem.
    323 			 */
    324 			if (vn->v_type == VBLK && vn->v_specnode != NULL) {
    325 				specnode_t sn;
    326 				specdev_t sd;
    327 				if (!KREAD(vn->v_specnode, &sn, sizeof(sn)))
    328 					continue;
    329 				if (!KREAD(sn.sn_dev, &sd, sizeof(sd)))
    330 					continue;
    331 				if (sd.sd_mountpoint)
    332 					mp = sd.sd_mountpoint;
    333 			}
    334 			if (mp != NULL)
    335 				mt = ml_lookup(mp,
    336 				    bp->b_bufsize,
    337 				    bp->b_bcount);
    338 		}
    339 	}
    340 
    341 	/* simple sort - there's not that many entries */
    342 	do {
    343 		if ((ml = LIST_FIRST(&mount_list)) == NULL ||
    344 		    LIST_NEXT(ml, ml_entries) == NULL)
    345 			break;
    346 
    347 		count = 0;
    348 		for (ml = LIST_FIRST(&mount_list); ml != NULL;
    349 		    ml = LIST_NEXT(ml, ml_entries)) {
    350 			if (LIST_NEXT(ml, ml_entries) == NULL)
    351 				break;
    352 			if (ml->ml_count < LIST_NEXT(ml, ml_entries)->ml_count) {
    353 				ml = LIST_NEXT(ml, ml_entries);
    354 				LIST_REMOVE(ml, ml_entries);
    355 				LIST_INSERT_HEAD(&mount_list, ml, ml_entries);
    356 				count++;
    357 			}
    358 		}
    359 	} while (count != 0);
    360 
    361 	free(buffers);
    362 }
    363 
    364 static void
    365 vc_init(void)
    366 {
    367 	int i;
    368 
    369 	/* vc_addr == NULL for unused cache entry. */
    370 	for (i = 0; i < VCACHE_SIZE; i++)
    371 		vcache[i].vc_addr = NULL;
    372 }
    373 
    374 static void
    375 ml_init(void)
    376 {
    377 	struct ml_entry *ml;
    378 
    379 	/* Throw out the current mount list and start again. */
    380 	while ((ml = LIST_FIRST(&mount_list)) != NULL) {
    381 		LIST_REMOVE(ml, ml_entries);
    382 		free(ml);
    383 	}
    384 }
    385 
    386 
    387 static struct vnode *
    388 vc_lookup(struct vnode *vaddr)
    389 {
    390 	struct vnode *ret;
    391 	size_t i, oldest;
    392 
    393 	ret = NULL;
    394 	oldest = 0;
    395 	for (i = 0; i < VCACHE_SIZE; i++) {
    396 		if (vcache[i].vc_addr == NULL)
    397 			break;
    398 		vcache[i].vc_age++;
    399 		if (vcache[i].vc_age < vcache[oldest].vc_age)
    400 			oldest = i;
    401 		if (vcache[i].vc_addr == vaddr) {
    402 			vcache[i].vc_age = 0;
    403 			ret = &vcache[i].vc_node;
    404 		}
    405 	}
    406 
    407 	/* Find an entry in the cache? */
    408 	if (ret != NULL)
    409 		return(ret);
    410 
    411 	/* Go past the end of the cache? */
    412 	if  (i >= VCACHE_SIZE)
    413 		i = oldest;
    414 
    415 	/* Read in new vnode and reset age counter. */
    416 	if (KREAD(vaddr, &vcache[i].vc_node, sizeof(struct vnode)) == 0)
    417 		return NULL;
    418 	vcache[i].vc_addr = vaddr;
    419 	vcache[i].vc_age = 0;
    420 
    421 	return(&vcache[i].vc_node);
    422 }
    423 
    424 static struct mount *
    425 ml_lookup(struct mount *maddr, int size, int valid)
    426 {
    427 	struct ml_entry *ml;
    428 
    429 	for (ml = LIST_FIRST(&mount_list); ml != NULL;
    430 	    ml = LIST_NEXT(ml, ml_entries))
    431 		if (ml->ml_addr == maddr) {
    432 			ml->ml_count++;
    433 			ml->ml_size += size;
    434 			ml->ml_valid += valid;
    435 			if (ml->ml_addr == NULL)
    436 				return(NULL);
    437 			else
    438 				return(&ml->ml_mount);
    439 		}
    440 
    441 	if ((ml = malloc(sizeof(struct ml_entry))) == NULL) {
    442 		error("out of memory");
    443 		die(0);
    444 	}
    445 	LIST_INSERT_HEAD(&mount_list, ml, ml_entries);
    446 	ml->ml_count = 1;
    447 	ml->ml_size = size;
    448 	ml->ml_valid = valid;
    449 	ml->ml_addr = maddr;
    450 	if (maddr == NULL)
    451 		return(NULL);
    452 
    453 	KREAD(maddr, &ml->ml_mount, sizeof(struct mount));
    454 	return(&ml->ml_mount);
    455 }
    456