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