Home | History | Annotate | Line # | Download | only in mpool
mpool.c revision 1.10.2.1
      1  1.10.2.1        he /*	$NetBSD: mpool.c,v 1.10.2.1 2000/01/15 16:23:15 he Exp $	*/
      2       1.5       cgd 
      3       1.1       cgd /*-
      4       1.6       cgd  * Copyright (c) 1990, 1993, 1994
      5       1.1       cgd  *	The Regents of the University of California.  All rights reserved.
      6       1.1       cgd  *
      7       1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8       1.1       cgd  * modification, are permitted provided that the following conditions
      9       1.1       cgd  * are met:
     10       1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11       1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12       1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13       1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14       1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15       1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     16       1.1       cgd  *    must display the following acknowledgement:
     17       1.1       cgd  *	This product includes software developed by the University of
     18       1.1       cgd  *	California, Berkeley and its contributors.
     19       1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     20       1.1       cgd  *    may be used to endorse or promote products derived from this software
     21       1.1       cgd  *    without specific prior written permission.
     22       1.1       cgd  *
     23       1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24       1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25       1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26       1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27       1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28       1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29       1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30       1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31       1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32       1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33       1.1       cgd  * SUCH DAMAGE.
     34       1.1       cgd  */
     35       1.1       cgd 
     36       1.7  christos #include <sys/cdefs.h>
     37       1.1       cgd #if defined(LIBC_SCCS) && !defined(lint)
     38       1.5       cgd #if 0
     39       1.6       cgd static char sccsid[] = "@(#)mpool.c	8.5 (Berkeley) 7/26/94";
     40       1.5       cgd #else
     41  1.10.2.1        he __RCSID("$NetBSD: mpool.c,v 1.10.2.1 2000/01/15 16:23:15 he Exp $");
     42       1.5       cgd #endif
     43       1.1       cgd #endif /* LIBC_SCCS and not lint */
     44       1.1       cgd 
     45       1.8       jtc #include "namespace.h"
     46       1.1       cgd #include <sys/param.h>
     47       1.6       cgd #include <sys/queue.h>
     48       1.1       cgd #include <sys/stat.h>
     49       1.1       cgd 
     50       1.1       cgd #include <errno.h>
     51       1.1       cgd #include <stdio.h>
     52       1.1       cgd #include <stdlib.h>
     53       1.1       cgd #include <string.h>
     54       1.1       cgd #include <unistd.h>
     55       1.1       cgd 
     56       1.1       cgd #include <db.h>
     57       1.6       cgd 
     58       1.1       cgd #define	__MPOOLINTERFACE_PRIVATE
     59       1.6       cgd #include <mpool.h>
     60       1.8       jtc 
     61       1.8       jtc #ifdef __weak_alias
     62       1.8       jtc __weak_alias(mpool_close,_mpool_close);
     63       1.8       jtc __weak_alias(mpool_filter,_mpool_filter);
     64       1.8       jtc __weak_alias(mpool_get,_mpool_get);
     65       1.8       jtc __weak_alias(mpool_new,_mpool_new);
     66       1.8       jtc __weak_alias(mpool_open,_mpool_open);
     67       1.8       jtc __weak_alias(mpool_put,_mpool_put);
     68       1.8       jtc __weak_alias(mpool_sync,_mpool_sync);
     69       1.8       jtc #endif
     70       1.1       cgd 
     71       1.1       cgd static BKT *mpool_bkt __P((MPOOL *));
     72       1.1       cgd static BKT *mpool_look __P((MPOOL *, pgno_t));
     73       1.1       cgd static int  mpool_write __P((MPOOL *, BKT *));
     74       1.1       cgd 
     75       1.1       cgd /*
     76       1.6       cgd  * mpool_open --
     77       1.6       cgd  *	Initialize a memory pool.
     78       1.1       cgd  */
     79      1.10  christos /*ARGSUSED*/
     80       1.1       cgd MPOOL *
     81       1.1       cgd mpool_open(key, fd, pagesize, maxcache)
     82       1.6       cgd 	void *key;
     83       1.1       cgd 	int fd;
     84       1.1       cgd 	pgno_t pagesize, maxcache;
     85       1.1       cgd {
     86       1.1       cgd 	struct stat sb;
     87       1.1       cgd 	MPOOL *mp;
     88       1.1       cgd 	int entry;
     89       1.1       cgd 
     90       1.6       cgd 	/*
     91       1.6       cgd 	 * Get information about the file.
     92       1.6       cgd 	 *
     93       1.6       cgd 	 * XXX
     94       1.6       cgd 	 * We don't currently handle pipes, although we should.
     95       1.6       cgd 	 */
     96       1.1       cgd 	if (fstat(fd, &sb))
     97       1.1       cgd 		return (NULL);
     98       1.1       cgd 	if (!S_ISREG(sb.st_mode)) {
     99       1.1       cgd 		errno = ESPIPE;
    100       1.1       cgd 		return (NULL);
    101       1.1       cgd 	}
    102       1.1       cgd 
    103       1.6       cgd 	/* Allocate and initialize the MPOOL cookie. */
    104       1.6       cgd 	if ((mp = (MPOOL *)calloc(1, sizeof(MPOOL))) == NULL)
    105       1.1       cgd 		return (NULL);
    106       1.6       cgd 	CIRCLEQ_INIT(&mp->lqh);
    107       1.1       cgd 	for (entry = 0; entry < HASHSIZE; ++entry)
    108       1.6       cgd 		CIRCLEQ_INIT(&mp->hqh[entry]);
    109       1.1       cgd 	mp->maxcache = maxcache;
    110      1.10  christos 	mp->npages = (pgno_t)(sb.st_size / pagesize);
    111       1.1       cgd 	mp->pagesize = pagesize;
    112       1.1       cgd 	mp->fd = fd;
    113       1.1       cgd 	return (mp);
    114       1.1       cgd }
    115       1.1       cgd 
    116       1.1       cgd /*
    117       1.6       cgd  * mpool_filter --
    118       1.6       cgd  *	Initialize input/output filters.
    119       1.1       cgd  */
    120       1.1       cgd void
    121       1.1       cgd mpool_filter(mp, pgin, pgout, pgcookie)
    122       1.1       cgd 	MPOOL *mp;
    123       1.1       cgd 	void (*pgin) __P((void *, pgno_t, void *));
    124       1.1       cgd 	void (*pgout) __P((void *, pgno_t, void *));
    125       1.1       cgd 	void *pgcookie;
    126       1.1       cgd {
    127       1.1       cgd 	mp->pgin = pgin;
    128       1.1       cgd 	mp->pgout = pgout;
    129       1.1       cgd 	mp->pgcookie = pgcookie;
    130       1.1       cgd }
    131       1.1       cgd 
    132       1.1       cgd /*
    133       1.6       cgd  * mpool_new --
    134       1.6       cgd  *	Get a new page of memory.
    135       1.1       cgd  */
    136       1.1       cgd void *
    137       1.1       cgd mpool_new(mp, pgnoaddr)
    138       1.1       cgd 	MPOOL *mp;
    139       1.1       cgd 	pgno_t *pgnoaddr;
    140       1.1       cgd {
    141       1.6       cgd 	struct _hqh *head;
    142       1.6       cgd 	BKT *bp;
    143       1.1       cgd 
    144       1.6       cgd 	if (mp->npages == MAX_PAGE_NUMBER) {
    145       1.6       cgd 		(void)fprintf(stderr, "mpool_new: page allocation overflow.\n");
    146       1.6       cgd 		abort();
    147       1.6       cgd 	}
    148       1.1       cgd #ifdef STATISTICS
    149       1.1       cgd 	++mp->pagenew;
    150       1.1       cgd #endif
    151       1.1       cgd 	/*
    152       1.6       cgd 	 * Get a BKT from the cache.  Assign a new page number, attach
    153       1.6       cgd 	 * it to the head of the hash chain, the tail of the lru chain,
    154       1.6       cgd 	 * and return.
    155       1.1       cgd 	 */
    156       1.6       cgd 	if ((bp = mpool_bkt(mp)) == NULL)
    157       1.1       cgd 		return (NULL);
    158       1.6       cgd 	*pgnoaddr = bp->pgno = mp->npages++;
    159       1.6       cgd 	bp->flags = MPOOL_PINNED;
    160       1.6       cgd 
    161       1.6       cgd 	head = &mp->hqh[HASHKEY(bp->pgno)];
    162       1.6       cgd 	CIRCLEQ_INSERT_HEAD(head, bp, hq);
    163       1.6       cgd 	CIRCLEQ_INSERT_TAIL(&mp->lqh, bp, q);
    164       1.6       cgd 	return (bp->page);
    165       1.1       cgd }
    166       1.1       cgd 
    167       1.1       cgd /*
    168       1.6       cgd  * mpool_get
    169       1.6       cgd  *	Get a page.
    170       1.1       cgd  */
    171      1.10  christos /*ARGSUSED*/
    172       1.1       cgd void *
    173       1.1       cgd mpool_get(mp, pgno, flags)
    174       1.1       cgd 	MPOOL *mp;
    175       1.1       cgd 	pgno_t pgno;
    176       1.6       cgd 	u_int flags;				/* XXX not used? */
    177       1.1       cgd {
    178       1.6       cgd 	struct _hqh *head;
    179       1.6       cgd 	BKT *bp;
    180       1.1       cgd 	off_t off;
    181       1.1       cgd 	int nr;
    182       1.1       cgd 
    183       1.6       cgd 	/* Check for attempt to retrieve a non-existent page. */
    184       1.6       cgd 	if (pgno >= mp->npages) {
    185       1.6       cgd 		errno = EINVAL;
    186       1.6       cgd 		return (NULL);
    187       1.6       cgd 	}
    188       1.6       cgd 
    189       1.1       cgd #ifdef STATISTICS
    190       1.6       cgd 	++mp->pageget;
    191       1.1       cgd #endif
    192       1.6       cgd 
    193       1.6       cgd 	/* Check for a page that is cached. */
    194       1.6       cgd 	if ((bp = mpool_look(mp, pgno)) != NULL) {
    195       1.1       cgd #ifdef DEBUG
    196       1.6       cgd 		if (bp->flags & MPOOL_PINNED) {
    197       1.6       cgd 			(void)fprintf(stderr,
    198       1.6       cgd 			    "mpool_get: page %d already pinned\n", bp->pgno);
    199       1.6       cgd 			abort();
    200       1.6       cgd 		}
    201       1.6       cgd #endif
    202       1.6       cgd 		/*
    203       1.6       cgd 		 * Move the page to the head of the hash chain and the tail
    204       1.6       cgd 		 * of the lru chain.
    205       1.6       cgd 		 */
    206       1.6       cgd 		head = &mp->hqh[HASHKEY(bp->pgno)];
    207       1.6       cgd 		CIRCLEQ_REMOVE(head, bp, hq);
    208       1.6       cgd 		CIRCLEQ_INSERT_HEAD(head, bp, hq);
    209       1.6       cgd 		CIRCLEQ_REMOVE(&mp->lqh, bp, q);
    210       1.6       cgd 		CIRCLEQ_INSERT_TAIL(&mp->lqh, bp, q);
    211       1.6       cgd 
    212       1.6       cgd 		/* Return a pinned page. */
    213       1.6       cgd 		bp->flags |= MPOOL_PINNED;
    214       1.6       cgd 		return (bp->page);
    215       1.1       cgd 	}
    216       1.1       cgd 
    217       1.1       cgd 	/* Get a page from the cache. */
    218       1.6       cgd 	if ((bp = mpool_bkt(mp)) == NULL)
    219       1.1       cgd 		return (NULL);
    220       1.1       cgd 
    221       1.6       cgd 	/* Read in the contents. */
    222       1.1       cgd #ifdef STATISTICS
    223       1.1       cgd 	++mp->pageread;
    224       1.1       cgd #endif
    225       1.1       cgd 	off = mp->pagesize * pgno;
    226      1.10  christos 	if ((nr = pread(mp->fd, bp->page, (size_t)mp->pagesize, off)) != (int)mp->pagesize) {
    227       1.1       cgd 		if (nr >= 0)
    228       1.1       cgd 			errno = EFTYPE;
    229       1.1       cgd 		return (NULL);
    230       1.1       cgd 	}
    231       1.1       cgd 
    232       1.6       cgd 	/* Set the page number, pin the page. */
    233       1.6       cgd 	bp->pgno = pgno;
    234       1.6       cgd 	bp->flags = MPOOL_PINNED;
    235       1.6       cgd 
    236       1.6       cgd 	/*
    237       1.6       cgd 	 * Add the page to the head of the hash chain and the tail
    238       1.6       cgd 	 * of the lru chain.
    239       1.6       cgd 	 */
    240       1.6       cgd 	head = &mp->hqh[HASHKEY(bp->pgno)];
    241       1.6       cgd 	CIRCLEQ_INSERT_HEAD(head, bp, hq);
    242       1.6       cgd 	CIRCLEQ_INSERT_TAIL(&mp->lqh, bp, q);
    243       1.6       cgd 
    244       1.6       cgd 	/* Run through the user's filter. */
    245       1.6       cgd 	if (mp->pgin != NULL)
    246       1.6       cgd 		(mp->pgin)(mp->pgcookie, bp->pgno, bp->page);
    247       1.6       cgd 
    248       1.6       cgd 	return (bp->page);
    249       1.1       cgd }
    250       1.1       cgd 
    251       1.1       cgd /*
    252       1.6       cgd  * mpool_put
    253       1.6       cgd  *	Return a page.
    254       1.1       cgd  */
    255      1.10  christos /*ARGSUSED*/
    256       1.1       cgd int
    257       1.1       cgd mpool_put(mp, page, flags)
    258       1.1       cgd 	MPOOL *mp;
    259       1.1       cgd 	void *page;
    260       1.1       cgd 	u_int flags;
    261       1.1       cgd {
    262       1.6       cgd 	BKT *bp;
    263       1.1       cgd 
    264       1.1       cgd #ifdef STATISTICS
    265       1.1       cgd 	++mp->pageput;
    266       1.1       cgd #endif
    267      1.10  christos 	bp = (BKT *)(void *)((char *)page - sizeof(BKT));
    268       1.1       cgd #ifdef DEBUG
    269       1.6       cgd 	if (!(bp->flags & MPOOL_PINNED)) {
    270       1.6       cgd 		(void)fprintf(stderr,
    271       1.6       cgd 		    "mpool_put: page %d not pinned\n", bp->pgno);
    272       1.6       cgd 		abort();
    273       1.1       cgd 	}
    274       1.1       cgd #endif
    275       1.6       cgd 	bp->flags &= ~MPOOL_PINNED;
    276       1.6       cgd 	bp->flags |= flags & MPOOL_DIRTY;
    277       1.1       cgd 	return (RET_SUCCESS);
    278       1.1       cgd }
    279       1.1       cgd 
    280       1.1       cgd /*
    281       1.6       cgd  * mpool_close
    282       1.6       cgd  *	Close the buffer pool.
    283       1.1       cgd  */
    284       1.1       cgd int
    285       1.1       cgd mpool_close(mp)
    286       1.1       cgd 	MPOOL *mp;
    287       1.1       cgd {
    288       1.6       cgd 	BKT *bp;
    289       1.1       cgd 
    290       1.1       cgd 	/* Free up any space allocated to the lru pages. */
    291       1.6       cgd 	while ((bp = mp->lqh.cqh_first) != (void *)&mp->lqh) {
    292       1.6       cgd 		CIRCLEQ_REMOVE(&mp->lqh, mp->lqh.cqh_first, q);
    293       1.6       cgd 		free(bp);
    294       1.1       cgd 	}
    295       1.6       cgd 
    296       1.6       cgd 	/* Free the MPOOL cookie. */
    297       1.1       cgd 	free(mp);
    298       1.1       cgd 	return (RET_SUCCESS);
    299       1.1       cgd }
    300       1.1       cgd 
    301       1.1       cgd /*
    302       1.6       cgd  * mpool_sync
    303       1.6       cgd  *	Sync the pool to disk.
    304       1.1       cgd  */
    305       1.1       cgd int
    306       1.1       cgd mpool_sync(mp)
    307       1.1       cgd 	MPOOL *mp;
    308       1.1       cgd {
    309       1.6       cgd 	BKT *bp;
    310       1.1       cgd 
    311       1.6       cgd 	/* Walk the lru chain, flushing any dirty pages to disk. */
    312       1.6       cgd 	for (bp = mp->lqh.cqh_first;
    313       1.6       cgd 	    bp != (void *)&mp->lqh; bp = bp->q.cqe_next)
    314       1.6       cgd 		if (bp->flags & MPOOL_DIRTY &&
    315       1.6       cgd 		    mpool_write(mp, bp) == RET_ERROR)
    316       1.1       cgd 			return (RET_ERROR);
    317       1.6       cgd 
    318       1.6       cgd 	/* Sync the file descriptor. */
    319       1.1       cgd 	return (fsync(mp->fd) ? RET_ERROR : RET_SUCCESS);
    320       1.1       cgd }
    321       1.1       cgd 
    322       1.1       cgd /*
    323       1.6       cgd  * mpool_bkt
    324       1.6       cgd  *	Get a page from the cache (or create one).
    325       1.1       cgd  */
    326       1.1       cgd static BKT *
    327       1.1       cgd mpool_bkt(mp)
    328       1.1       cgd 	MPOOL *mp;
    329       1.1       cgd {
    330       1.6       cgd 	struct _hqh *head;
    331       1.6       cgd 	BKT *bp;
    332       1.1       cgd 
    333       1.6       cgd 	/* If under the max cached, always create a new page. */
    334       1.1       cgd 	if (mp->curcache < mp->maxcache)
    335       1.1       cgd 		goto new;
    336       1.1       cgd 
    337       1.1       cgd 	/*
    338       1.6       cgd 	 * If the cache is max'd out, walk the lru list for a buffer we
    339       1.6       cgd 	 * can flush.  If we find one, write it (if necessary) and take it
    340       1.6       cgd 	 * off any lists.  If we don't find anything we grow the cache anyway.
    341       1.1       cgd 	 * The cache never shrinks.
    342       1.1       cgd 	 */
    343       1.6       cgd 	for (bp = mp->lqh.cqh_first;
    344       1.6       cgd 	    bp != (void *)&mp->lqh; bp = bp->q.cqe_next)
    345       1.6       cgd 		if (!(bp->flags & MPOOL_PINNED)) {
    346       1.6       cgd 			/* Flush if dirty. */
    347       1.6       cgd 			if (bp->flags & MPOOL_DIRTY &&
    348       1.6       cgd 			    mpool_write(mp, bp) == RET_ERROR)
    349       1.1       cgd 				return (NULL);
    350       1.1       cgd #ifdef STATISTICS
    351       1.1       cgd 			++mp->pageflush;
    352       1.1       cgd #endif
    353       1.6       cgd 			/* Remove from the hash and lru queues. */
    354       1.6       cgd 			head = &mp->hqh[HASHKEY(bp->pgno)];
    355       1.6       cgd 			CIRCLEQ_REMOVE(head, bp, hq);
    356       1.6       cgd 			CIRCLEQ_REMOVE(&mp->lqh, bp, q);
    357       1.1       cgd #ifdef DEBUG
    358       1.6       cgd 			{ void *spage;
    359       1.6       cgd 				spage = bp->page;
    360       1.6       cgd 				memset(bp, 0xff, sizeof(BKT) + mp->pagesize);
    361       1.6       cgd 				bp->page = spage;
    362       1.1       cgd 			}
    363       1.1       cgd #endif
    364       1.6       cgd 			return (bp);
    365       1.1       cgd 		}
    366       1.1       cgd 
    367      1.10  christos new:	if ((bp = (BKT *)malloc((size_t)(sizeof(BKT) + mp->pagesize))) == NULL)
    368       1.1       cgd 		return (NULL);
    369       1.1       cgd #ifdef STATISTICS
    370       1.1       cgd 	++mp->pagealloc;
    371       1.1       cgd #endif
    372       1.6       cgd #if defined(DEBUG) || defined(PURIFY)
    373       1.6       cgd 	memset(bp, 0xff, sizeof(BKT) + mp->pagesize);
    374       1.1       cgd #endif
    375      1.10  christos 	bp->page = (char *)(void *)bp + sizeof(BKT);
    376       1.1       cgd 	++mp->curcache;
    377       1.6       cgd 	return (bp);
    378       1.1       cgd }
    379       1.1       cgd 
    380       1.1       cgd /*
    381       1.6       cgd  * mpool_write
    382       1.6       cgd  *	Write a page to disk.
    383       1.1       cgd  */
    384       1.1       cgd static int
    385       1.6       cgd mpool_write(mp, bp)
    386       1.1       cgd 	MPOOL *mp;
    387       1.6       cgd 	BKT *bp;
    388       1.1       cgd {
    389       1.1       cgd 	off_t off;
    390       1.1       cgd 
    391       1.1       cgd #ifdef STATISTICS
    392       1.1       cgd 	++mp->pagewrite;
    393       1.1       cgd #endif
    394       1.6       cgd 
    395       1.6       cgd 	/* Run through the user's filter. */
    396       1.6       cgd 	if (mp->pgout)
    397       1.6       cgd 		(mp->pgout)(mp->pgcookie, bp->pgno, bp->page);
    398       1.6       cgd 
    399       1.6       cgd 	off = mp->pagesize * bp->pgno;
    400      1.10  christos 	if (pwrite(mp->fd, bp->page, (size_t)mp->pagesize, off) != (int)mp->pagesize)
    401       1.1       cgd 		return (RET_ERROR);
    402  1.10.2.1        he 
    403  1.10.2.1        he 	/*
    404  1.10.2.1        he 	 * Re-run through the input filter since this page may soon be
    405  1.10.2.1        he 	 * accessed via the cache, and whatever the user's output filter
    406  1.10.2.1        he 	 * did may screw things up if we don't let the input filter
    407  1.10.2.1        he 	 * restore the in-core copy.
    408  1.10.2.1        he 	 */
    409  1.10.2.1        he 	if (mp->pgin)
    410  1.10.2.1        he 		(mp->pgin)(mp->pgcookie, bp->pgno, bp->page);
    411       1.6       cgd 
    412       1.6       cgd 	bp->flags &= ~MPOOL_DIRTY;
    413       1.1       cgd 	return (RET_SUCCESS);
    414       1.1       cgd }
    415       1.1       cgd 
    416       1.1       cgd /*
    417       1.6       cgd  * mpool_look
    418       1.6       cgd  *	Lookup a page in the cache.
    419       1.1       cgd  */
    420       1.1       cgd static BKT *
    421       1.1       cgd mpool_look(mp, pgno)
    422       1.1       cgd 	MPOOL *mp;
    423       1.1       cgd 	pgno_t pgno;
    424       1.1       cgd {
    425       1.6       cgd 	struct _hqh *head;
    426       1.6       cgd 	BKT *bp;
    427       1.1       cgd 
    428       1.6       cgd 	head = &mp->hqh[HASHKEY(pgno)];
    429       1.6       cgd 	for (bp = head->cqh_first; bp != (void *)head; bp = bp->hq.cqe_next)
    430       1.6       cgd 		if (bp->pgno == pgno) {
    431       1.1       cgd #ifdef STATISTICS
    432       1.1       cgd 			++mp->cachehit;
    433       1.1       cgd #endif
    434       1.6       cgd 			return (bp);
    435       1.1       cgd 		}
    436       1.1       cgd #ifdef STATISTICS
    437       1.1       cgd 	++mp->cachemiss;
    438       1.1       cgd #endif
    439       1.1       cgd 	return (NULL);
    440       1.1       cgd }
    441       1.1       cgd 
    442       1.1       cgd #ifdef STATISTICS
    443       1.1       cgd /*
    444       1.6       cgd  * mpool_stat
    445       1.6       cgd  *	Print out cache statistics.
    446       1.1       cgd  */
    447       1.1       cgd void
    448       1.1       cgd mpool_stat(mp)
    449       1.1       cgd 	MPOOL *mp;
    450       1.1       cgd {
    451       1.6       cgd 	BKT *bp;
    452       1.1       cgd 	int cnt;
    453       1.1       cgd 	char *sep;
    454       1.1       cgd 
    455       1.1       cgd 	(void)fprintf(stderr, "%lu pages in the file\n", mp->npages);
    456       1.1       cgd 	(void)fprintf(stderr,
    457       1.1       cgd 	    "page size %lu, cacheing %lu pages of %lu page max cache\n",
    458       1.1       cgd 	    mp->pagesize, mp->curcache, mp->maxcache);
    459       1.1       cgd 	(void)fprintf(stderr, "%lu page puts, %lu page gets, %lu page new\n",
    460       1.1       cgd 	    mp->pageput, mp->pageget, mp->pagenew);
    461       1.1       cgd 	(void)fprintf(stderr, "%lu page allocs, %lu page flushes\n",
    462       1.1       cgd 	    mp->pagealloc, mp->pageflush);
    463       1.1       cgd 	if (mp->cachehit + mp->cachemiss)
    464       1.1       cgd 		(void)fprintf(stderr,
    465       1.1       cgd 		    "%.0f%% cache hit rate (%lu hits, %lu misses)\n",
    466       1.1       cgd 		    ((double)mp->cachehit / (mp->cachehit + mp->cachemiss))
    467       1.1       cgd 		    * 100, mp->cachehit, mp->cachemiss);
    468       1.1       cgd 	(void)fprintf(stderr, "%lu page reads, %lu page writes\n",
    469       1.1       cgd 	    mp->pageread, mp->pagewrite);
    470       1.1       cgd 
    471       1.1       cgd 	sep = "";
    472       1.1       cgd 	cnt = 0;
    473       1.6       cgd 	for (bp = mp->lqh.cqh_first;
    474       1.6       cgd 	    bp != (void *)&mp->lqh; bp = bp->q.cqe_next) {
    475       1.6       cgd 		(void)fprintf(stderr, "%s%d", sep, bp->pgno);
    476       1.6       cgd 		if (bp->flags & MPOOL_DIRTY)
    477       1.1       cgd 			(void)fprintf(stderr, "d");
    478       1.6       cgd 		if (bp->flags & MPOOL_PINNED)
    479       1.1       cgd 			(void)fprintf(stderr, "P");
    480       1.1       cgd 		if (++cnt == 10) {
    481       1.1       cgd 			sep = "\n";
    482       1.1       cgd 			cnt = 0;
    483       1.1       cgd 		} else
    484       1.1       cgd 			sep = ", ";
    485       1.1       cgd 
    486       1.1       cgd 	}
    487       1.1       cgd 	(void)fprintf(stderr, "\n");
    488       1.1       cgd }
    489       1.1       cgd #endif
    490