Home | History | Annotate | Line # | Download | only in mpool
mpool.c revision 1.4
      1  1.1  cgd /*-
      2  1.1  cgd  * Copyright (c) 1990, 1993
      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.4  cgd static char sccsid[] = "@(#)mpool.c	8.2 (Berkeley) 2/21/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  cgd #include <sys/stat.h>
     40  1.1  cgd 
     41  1.1  cgd #include <errno.h>
     42  1.1  cgd #include <stdio.h>
     43  1.1  cgd #include <stdlib.h>
     44  1.1  cgd #include <string.h>
     45  1.1  cgd #include <unistd.h>
     46  1.1  cgd 
     47  1.1  cgd #include <db.h>
     48  1.1  cgd #define	__MPOOLINTERFACE_PRIVATE
     49  1.1  cgd #include "mpool.h"
     50  1.1  cgd 
     51  1.1  cgd static BKT *mpool_bkt __P((MPOOL *));
     52  1.1  cgd static BKT *mpool_look __P((MPOOL *, pgno_t));
     53  1.1  cgd static int  mpool_write __P((MPOOL *, BKT *));
     54  1.1  cgd #ifdef DEBUG
     55  1.1  cgd static void __mpoolerr __P((const char *fmt, ...));
     56  1.1  cgd #endif
     57  1.1  cgd 
     58  1.1  cgd /*
     59  1.1  cgd  * MPOOL_OPEN -- initialize a memory pool.
     60  1.1  cgd  *
     61  1.1  cgd  * Parameters:
     62  1.1  cgd  *	key:		Shared buffer key.
     63  1.1  cgd  *	fd:		File descriptor.
     64  1.1  cgd  *	pagesize:	File page size.
     65  1.1  cgd  *	maxcache:	Max number of cached pages.
     66  1.1  cgd  *
     67  1.1  cgd  * Returns:
     68  1.1  cgd  *	MPOOL pointer, NULL on error.
     69  1.1  cgd  */
     70  1.1  cgd MPOOL *
     71  1.1  cgd mpool_open(key, fd, pagesize, maxcache)
     72  1.1  cgd 	DBT *key;
     73  1.1  cgd 	int fd;
     74  1.1  cgd 	pgno_t pagesize, maxcache;
     75  1.1  cgd {
     76  1.1  cgd 	struct stat sb;
     77  1.1  cgd 	MPOOL *mp;
     78  1.1  cgd 	int entry;
     79  1.1  cgd 
     80  1.1  cgd 	if (fstat(fd, &sb))
     81  1.1  cgd 		return (NULL);
     82  1.1  cgd 	/* XXX
     83  1.1  cgd 	 * We should only set st_size to 0 for pipes -- 4.4BSD has the fix so
     84  1.1  cgd 	 * that stat(2) returns true for ISSOCK on pipes.  Until then, this is
     85  1.1  cgd 	 * fairly close.
     86  1.1  cgd 	 */
     87  1.1  cgd 	if (!S_ISREG(sb.st_mode)) {
     88  1.1  cgd 		errno = ESPIPE;
     89  1.1  cgd 		return (NULL);
     90  1.1  cgd 	}
     91  1.1  cgd 
     92  1.4  cgd 	if ((mp = (MPOOL *)malloc(sizeof(MPOOL))) == NULL)
     93  1.1  cgd 		return (NULL);
     94  1.1  cgd 	mp->free.cnext = mp->free.cprev = (BKT *)&mp->free;
     95  1.1  cgd 	mp->lru.cnext = mp->lru.cprev = (BKT *)&mp->lru;
     96  1.1  cgd 	for (entry = 0; entry < HASHSIZE; ++entry)
     97  1.1  cgd 		mp->hashtable[entry].hnext = mp->hashtable[entry].hprev =
     98  1.1  cgd 		    mp->hashtable[entry].cnext = mp->hashtable[entry].cprev =
     99  1.1  cgd 		    (BKT *)&mp->hashtable[entry];
    100  1.1  cgd 	mp->curcache = 0;
    101  1.1  cgd 	mp->maxcache = maxcache;
    102  1.1  cgd 	mp->pagesize = pagesize;
    103  1.1  cgd 	mp->npages = sb.st_size / pagesize;
    104  1.1  cgd 	mp->fd = fd;
    105  1.1  cgd 	mp->pgcookie = NULL;
    106  1.1  cgd 	mp->pgin = mp->pgout = NULL;
    107  1.1  cgd 
    108  1.1  cgd #ifdef STATISTICS
    109  1.1  cgd 	mp->cachehit = mp->cachemiss = mp->pagealloc = mp->pageflush =
    110  1.1  cgd 	    mp->pageget = mp->pagenew = mp->pageput = mp->pageread =
    111  1.1  cgd 	    mp->pagewrite = 0;
    112  1.1  cgd #endif
    113  1.1  cgd 	return (mp);
    114  1.1  cgd }
    115  1.1  cgd 
    116  1.1  cgd /*
    117  1.1  cgd  * MPOOL_FILTER -- initialize input/output filters.
    118  1.1  cgd  *
    119  1.1  cgd  * Parameters:
    120  1.1  cgd  *	pgin:		Page in conversion routine.
    121  1.1  cgd  *	pgout:		Page out conversion routine.
    122  1.1  cgd  *	pgcookie:	Cookie for page in/out routines.
    123  1.1  cgd  */
    124  1.1  cgd void
    125  1.1  cgd mpool_filter(mp, pgin, pgout, pgcookie)
    126  1.1  cgd 	MPOOL *mp;
    127  1.1  cgd 	void (*pgin) __P((void *, pgno_t, void *));
    128  1.1  cgd 	void (*pgout) __P((void *, pgno_t, void *));
    129  1.1  cgd 	void *pgcookie;
    130  1.1  cgd {
    131  1.1  cgd 	mp->pgin = pgin;
    132  1.1  cgd 	mp->pgout = pgout;
    133  1.1  cgd 	mp->pgcookie = pgcookie;
    134  1.1  cgd }
    135  1.1  cgd 
    136  1.1  cgd /*
    137  1.1  cgd  * MPOOL_NEW -- get a new page
    138  1.1  cgd  *
    139  1.1  cgd  * Parameters:
    140  1.1  cgd  *	mp:		mpool cookie
    141  1.1  cgd  *	pgnoadddr:	place to store new page number
    142  1.1  cgd  * Returns:
    143  1.1  cgd  *	RET_ERROR, RET_SUCCESS
    144  1.1  cgd  */
    145  1.1  cgd void *
    146  1.1  cgd mpool_new(mp, pgnoaddr)
    147  1.1  cgd 	MPOOL *mp;
    148  1.1  cgd 	pgno_t *pgnoaddr;
    149  1.1  cgd {
    150  1.1  cgd 	BKT *b;
    151  1.1  cgd 	BKTHDR *hp;
    152  1.1  cgd 
    153  1.1  cgd #ifdef STATISTICS
    154  1.1  cgd 	++mp->pagenew;
    155  1.1  cgd #endif
    156  1.1  cgd 	/*
    157  1.1  cgd 	 * Get a BKT from the cache.  Assign a new page number, attach it to
    158  1.1  cgd 	 * the hash and lru chains and return.
    159  1.1  cgd 	 */
    160  1.1  cgd 	if ((b = mpool_bkt(mp)) == NULL)
    161  1.1  cgd 		return (NULL);
    162  1.1  cgd 	*pgnoaddr = b->pgno = mp->npages++;
    163  1.1  cgd 	b->flags = MPOOL_PINNED;
    164  1.1  cgd 	inshash(b, b->pgno);
    165  1.1  cgd 	inschain(b, &mp->lru);
    166  1.1  cgd 	return (b->page);
    167  1.1  cgd }
    168  1.1  cgd 
    169  1.1  cgd /*
    170  1.1  cgd  * MPOOL_GET -- get a page from the pool
    171  1.1  cgd  *
    172  1.1  cgd  * Parameters:
    173  1.1  cgd  *	mp:	mpool cookie
    174  1.1  cgd  *	pgno:	page number
    175  1.1  cgd  *	flags:	not used
    176  1.1  cgd  *
    177  1.1  cgd  * Returns:
    178  1.1  cgd  *	RET_ERROR, RET_SUCCESS
    179  1.1  cgd  */
    180  1.1  cgd void *
    181  1.1  cgd mpool_get(mp, pgno, flags)
    182  1.1  cgd 	MPOOL *mp;
    183  1.1  cgd 	pgno_t pgno;
    184  1.1  cgd 	u_int flags;		/* XXX not used? */
    185  1.1  cgd {
    186  1.1  cgd 	BKT *b;
    187  1.1  cgd 	BKTHDR *hp;
    188  1.1  cgd 	off_t off;
    189  1.1  cgd 	int nr;
    190  1.1  cgd 
    191  1.1  cgd 	/*
    192  1.1  cgd 	 * If asking for a specific page that is already in the cache, find
    193  1.1  cgd 	 * it and return it.
    194  1.1  cgd 	 */
    195  1.1  cgd 	if (b = mpool_look(mp, pgno)) {
    196  1.1  cgd #ifdef STATISTICS
    197  1.1  cgd 		++mp->pageget;
    198  1.1  cgd #endif
    199  1.1  cgd #ifdef DEBUG
    200  1.1  cgd 		if (b->flags & MPOOL_PINNED)
    201  1.1  cgd 			__mpoolerr("mpool_get: page %d already pinned",
    202  1.1  cgd 			    b->pgno);
    203  1.1  cgd #endif
    204  1.1  cgd 		rmchain(b);
    205  1.1  cgd 		inschain(b, &mp->lru);
    206  1.1  cgd 		b->flags |= MPOOL_PINNED;
    207  1.1  cgd 		return (b->page);
    208  1.1  cgd 	}
    209  1.1  cgd 
    210  1.1  cgd 	/* Not allowed to retrieve a non-existent page. */
    211  1.1  cgd 	if (pgno >= mp->npages) {
    212  1.1  cgd 		errno = EINVAL;
    213  1.1  cgd 		return (NULL);
    214  1.1  cgd 	}
    215  1.1  cgd 
    216  1.1  cgd 	/* Get a page from the cache. */
    217  1.1  cgd 	if ((b = mpool_bkt(mp)) == NULL)
    218  1.1  cgd 		return (NULL);
    219  1.1  cgd 	b->pgno = pgno;
    220  1.1  cgd 	b->flags = MPOOL_PINNED;
    221  1.1  cgd 
    222  1.1  cgd #ifdef STATISTICS
    223  1.1  cgd 	++mp->pageread;
    224  1.1  cgd #endif
    225  1.1  cgd 	/* Read in the contents. */
    226  1.1  cgd 	off = mp->pagesize * pgno;
    227  1.1  cgd 	if (lseek(mp->fd, off, SEEK_SET) != off)
    228  1.1  cgd 		return (NULL);
    229  1.1  cgd 	if ((nr = read(mp->fd, b->page, mp->pagesize)) != mp->pagesize) {
    230  1.1  cgd 		if (nr >= 0)
    231  1.1  cgd 			errno = EFTYPE;
    232  1.1  cgd 		return (NULL);
    233  1.1  cgd 	}
    234  1.1  cgd 	if (mp->pgin)
    235  1.1  cgd 		(mp->pgin)(mp->pgcookie, b->pgno, b->page);
    236  1.1  cgd 
    237  1.1  cgd 	inshash(b, b->pgno);
    238  1.1  cgd 	inschain(b, &mp->lru);
    239  1.1  cgd #ifdef STATISTICS
    240  1.1  cgd 	++mp->pageget;
    241  1.1  cgd #endif
    242  1.1  cgd 	return (b->page);
    243  1.1  cgd }
    244  1.1  cgd 
    245  1.1  cgd /*
    246  1.1  cgd  * MPOOL_PUT -- return a page to the pool
    247  1.1  cgd  *
    248  1.1  cgd  * Parameters:
    249  1.1  cgd  *	mp:	mpool cookie
    250  1.1  cgd  *	page:	page pointer
    251  1.1  cgd  *	pgno:	page number
    252  1.1  cgd  *
    253  1.1  cgd  * Returns:
    254  1.1  cgd  *	RET_ERROR, RET_SUCCESS
    255  1.1  cgd  */
    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.1  cgd 	BKT *baddr;
    263  1.1  cgd #ifdef DEBUG
    264  1.1  cgd 	BKT *b;
    265  1.1  cgd #endif
    266  1.1  cgd 
    267  1.1  cgd #ifdef STATISTICS
    268  1.1  cgd 	++mp->pageput;
    269  1.1  cgd #endif
    270  1.1  cgd 	baddr = (BKT *)((char *)page - sizeof(BKT));
    271  1.1  cgd #ifdef DEBUG
    272  1.1  cgd 	if (!(baddr->flags & MPOOL_PINNED))
    273  1.1  cgd 		__mpoolerr("mpool_put: page %d not pinned", b->pgno);
    274  1.1  cgd 	for (b = mp->lru.cnext; b != (BKT *)&mp->lru; b = b->cnext) {
    275  1.1  cgd 		if (b == (BKT *)&mp->lru)
    276  1.1  cgd 			__mpoolerr("mpool_put: %0x: bad address", baddr);
    277  1.1  cgd 		if (b == baddr)
    278  1.1  cgd 			break;
    279  1.1  cgd 	}
    280  1.1  cgd #endif
    281  1.1  cgd 	baddr->flags &= ~MPOOL_PINNED;
    282  1.1  cgd 	baddr->flags |= flags & MPOOL_DIRTY;
    283  1.1  cgd 	return (RET_SUCCESS);
    284  1.1  cgd }
    285  1.1  cgd 
    286  1.1  cgd /*
    287  1.1  cgd  * MPOOL_CLOSE -- close the buffer pool
    288  1.1  cgd  *
    289  1.1  cgd  * Parameters:
    290  1.1  cgd  *	mp:	mpool cookie
    291  1.1  cgd  *
    292  1.1  cgd  * Returns:
    293  1.1  cgd  *	RET_ERROR, RET_SUCCESS
    294  1.1  cgd  */
    295  1.1  cgd int
    296  1.1  cgd mpool_close(mp)
    297  1.1  cgd 	MPOOL *mp;
    298  1.1  cgd {
    299  1.1  cgd 	BKT *b, *next;
    300  1.1  cgd 
    301  1.1  cgd 	/* Free up any space allocated to the lru pages. */
    302  1.1  cgd 	for (b = mp->lru.cprev; b != (BKT *)&mp->lru; b = next) {
    303  1.1  cgd 		next = b->cprev;
    304  1.1  cgd 		free(b);
    305  1.1  cgd 	}
    306  1.1  cgd 	free(mp);
    307  1.1  cgd 	return (RET_SUCCESS);
    308  1.1  cgd }
    309  1.1  cgd 
    310  1.1  cgd /*
    311  1.1  cgd  * MPOOL_SYNC -- sync the file to disk.
    312  1.1  cgd  *
    313  1.1  cgd  * Parameters:
    314  1.1  cgd  *	mp:	mpool cookie
    315  1.1  cgd  *
    316  1.1  cgd  * Returns:
    317  1.1  cgd  *	RET_ERROR, RET_SUCCESS
    318  1.1  cgd  */
    319  1.1  cgd int
    320  1.1  cgd mpool_sync(mp)
    321  1.1  cgd 	MPOOL *mp;
    322  1.1  cgd {
    323  1.1  cgd 	BKT *b;
    324  1.1  cgd 
    325  1.1  cgd 	for (b = mp->lru.cprev; b != (BKT *)&mp->lru; b = b->cprev)
    326  1.1  cgd 		if (b->flags & MPOOL_DIRTY && mpool_write(mp, b) == RET_ERROR)
    327  1.1  cgd 			return (RET_ERROR);
    328  1.1  cgd 	return (fsync(mp->fd) ? RET_ERROR : RET_SUCCESS);
    329  1.1  cgd }
    330  1.1  cgd 
    331  1.1  cgd /*
    332  1.1  cgd  * MPOOL_BKT -- get/create a BKT from the cache
    333  1.1  cgd  *
    334  1.1  cgd  * Parameters:
    335  1.1  cgd  *	mp:	mpool cookie
    336  1.1  cgd  *
    337  1.1  cgd  * Returns:
    338  1.1  cgd  *	NULL on failure and a pointer to the BKT on success
    339  1.1  cgd  */
    340  1.1  cgd static BKT *
    341  1.1  cgd mpool_bkt(mp)
    342  1.1  cgd 	MPOOL *mp;
    343  1.1  cgd {
    344  1.1  cgd 	BKT *b;
    345  1.1  cgd 
    346  1.1  cgd 	if (mp->curcache < mp->maxcache)
    347  1.1  cgd 		goto new;
    348  1.1  cgd 
    349  1.1  cgd 	/*
    350  1.1  cgd 	 * If the cache is maxxed out, search the lru list for a buffer we
    351  1.1  cgd 	 * can flush.  If we find one, write it if necessary and take it off
    352  1.1  cgd 	 * any lists.  If we don't find anything we grow the cache anyway.
    353  1.1  cgd 	 * The cache never shrinks.
    354  1.1  cgd 	 */
    355  1.1  cgd 	for (b = mp->lru.cprev; b != (BKT *)&mp->lru; b = b->cprev)
    356  1.1  cgd 		if (!(b->flags & MPOOL_PINNED)) {
    357  1.1  cgd 			if (b->flags & MPOOL_DIRTY &&
    358  1.1  cgd 			    mpool_write(mp, b) == RET_ERROR)
    359  1.1  cgd 				return (NULL);
    360  1.1  cgd 			rmhash(b);
    361  1.1  cgd 			rmchain(b);
    362  1.1  cgd #ifdef STATISTICS
    363  1.1  cgd 			++mp->pageflush;
    364  1.1  cgd #endif
    365  1.1  cgd #ifdef DEBUG
    366  1.1  cgd 			{
    367  1.1  cgd 				void *spage;
    368  1.1  cgd 				spage = b->page;
    369  1.1  cgd 				memset(b, 0xff, sizeof(BKT) + mp->pagesize);
    370  1.1  cgd 				b->page = spage;
    371  1.1  cgd 			}
    372  1.1  cgd #endif
    373  1.1  cgd 			return (b);
    374  1.1  cgd 		}
    375  1.1  cgd 
    376  1.4  cgd new:	if ((b = (BKT *)malloc(sizeof(BKT) + mp->pagesize)) == NULL)
    377  1.1  cgd 		return (NULL);
    378  1.1  cgd #ifdef STATISTICS
    379  1.1  cgd 	++mp->pagealloc;
    380  1.1  cgd #endif
    381  1.1  cgd #ifdef DEBUG
    382  1.1  cgd 	memset(b, 0xff, sizeof(BKT) + mp->pagesize);
    383  1.1  cgd #endif
    384  1.1  cgd 	b->page = (char *)b + sizeof(BKT);
    385  1.1  cgd 	++mp->curcache;
    386  1.1  cgd 	return (b);
    387  1.1  cgd }
    388  1.1  cgd 
    389  1.1  cgd /*
    390  1.1  cgd  * MPOOL_WRITE -- sync a page to disk
    391  1.1  cgd  *
    392  1.1  cgd  * Parameters:
    393  1.1  cgd  *	mp:	mpool cookie
    394  1.1  cgd  *
    395  1.1  cgd  * Returns:
    396  1.1  cgd  *	RET_ERROR, RET_SUCCESS
    397  1.1  cgd  */
    398  1.1  cgd static int
    399  1.1  cgd mpool_write(mp, b)
    400  1.1  cgd 	MPOOL *mp;
    401  1.1  cgd 	BKT *b;
    402  1.1  cgd {
    403  1.1  cgd 	off_t off;
    404  1.1  cgd 
    405  1.1  cgd 	if (mp->pgout)
    406  1.1  cgd 		(mp->pgout)(mp->pgcookie, b->pgno, b->page);
    407  1.1  cgd 
    408  1.1  cgd #ifdef STATISTICS
    409  1.1  cgd 	++mp->pagewrite;
    410  1.1  cgd #endif
    411  1.1  cgd 	off = mp->pagesize * b->pgno;
    412  1.1  cgd 	if (lseek(mp->fd, off, SEEK_SET) != off)
    413  1.1  cgd 		return (RET_ERROR);
    414  1.1  cgd 	if (write(mp->fd, b->page, mp->pagesize) != mp->pagesize)
    415  1.1  cgd 		return (RET_ERROR);
    416  1.1  cgd 	b->flags &= ~MPOOL_DIRTY;
    417  1.1  cgd 	return (RET_SUCCESS);
    418  1.1  cgd }
    419  1.1  cgd 
    420  1.1  cgd /*
    421  1.1  cgd  * MPOOL_LOOK -- lookup a page
    422  1.1  cgd  *
    423  1.1  cgd  * Parameters:
    424  1.1  cgd  *	mp:	mpool cookie
    425  1.1  cgd  *	pgno:	page number
    426  1.1  cgd  *
    427  1.1  cgd  * Returns:
    428  1.1  cgd  *	NULL on failure and a pointer to the BKT on success
    429  1.1  cgd  */
    430  1.1  cgd static BKT *
    431  1.1  cgd mpool_look(mp, pgno)
    432  1.1  cgd 	MPOOL *mp;
    433  1.1  cgd 	pgno_t pgno;
    434  1.1  cgd {
    435  1.1  cgd 	register BKT *b;
    436  1.1  cgd 	register BKTHDR *tb;
    437  1.1  cgd 
    438  1.1  cgd 	/* XXX
    439  1.1  cgd 	 * If find the buffer, put it first on the hash chain so can
    440  1.1  cgd 	 * find it again quickly.
    441  1.1  cgd 	 */
    442  1.1  cgd 	tb = &mp->hashtable[HASHKEY(pgno)];
    443  1.1  cgd 	for (b = tb->hnext; b != (BKT *)tb; b = b->hnext)
    444  1.1  cgd 		if (b->pgno == pgno) {
    445  1.1  cgd #ifdef STATISTICS
    446  1.1  cgd 			++mp->cachehit;
    447  1.1  cgd #endif
    448  1.1  cgd 			return (b);
    449  1.1  cgd 		}
    450  1.1  cgd #ifdef STATISTICS
    451  1.1  cgd 	++mp->cachemiss;
    452  1.1  cgd #endif
    453  1.1  cgd 	return (NULL);
    454  1.1  cgd }
    455  1.1  cgd 
    456  1.1  cgd #ifdef STATISTICS
    457  1.1  cgd /*
    458  1.1  cgd  * MPOOL_STAT -- cache statistics
    459  1.1  cgd  *
    460  1.1  cgd  * Parameters:
    461  1.1  cgd  *	mp:	mpool cookie
    462  1.1  cgd  */
    463  1.1  cgd void
    464  1.1  cgd mpool_stat(mp)
    465  1.1  cgd 	MPOOL *mp;
    466  1.1  cgd {
    467  1.1  cgd 	BKT *b;
    468  1.1  cgd 	int cnt;
    469  1.1  cgd 	char *sep;
    470  1.1  cgd 
    471  1.1  cgd 	(void)fprintf(stderr, "%lu pages in the file\n", mp->npages);
    472  1.1  cgd 	(void)fprintf(stderr,
    473  1.1  cgd 	    "page size %lu, cacheing %lu pages of %lu page max cache\n",
    474  1.1  cgd 	    mp->pagesize, mp->curcache, mp->maxcache);
    475  1.1  cgd 	(void)fprintf(stderr, "%lu page puts, %lu page gets, %lu page new\n",
    476  1.1  cgd 	    mp->pageput, mp->pageget, mp->pagenew);
    477  1.1  cgd 	(void)fprintf(stderr, "%lu page allocs, %lu page flushes\n",
    478  1.1  cgd 	    mp->pagealloc, mp->pageflush);
    479  1.1  cgd 	if (mp->cachehit + mp->cachemiss)
    480  1.1  cgd 		(void)fprintf(stderr,
    481  1.1  cgd 		    "%.0f%% cache hit rate (%lu hits, %lu misses)\n",
    482  1.1  cgd 		    ((double)mp->cachehit / (mp->cachehit + mp->cachemiss))
    483  1.1  cgd 		    * 100, mp->cachehit, mp->cachemiss);
    484  1.1  cgd 	(void)fprintf(stderr, "%lu page reads, %lu page writes\n",
    485  1.1  cgd 	    mp->pageread, mp->pagewrite);
    486  1.1  cgd 
    487  1.1  cgd 	sep = "";
    488  1.1  cgd 	cnt = 0;
    489  1.1  cgd 	for (b = mp->lru.cnext; b != (BKT *)&mp->lru; b = b->cnext) {
    490  1.1  cgd 		(void)fprintf(stderr, "%s%d", sep, b->pgno);
    491  1.1  cgd 		if (b->flags & MPOOL_DIRTY)
    492  1.1  cgd 			(void)fprintf(stderr, "d");
    493  1.1  cgd 		if (b->flags & MPOOL_PINNED)
    494  1.1  cgd 			(void)fprintf(stderr, "P");
    495  1.1  cgd 		if (++cnt == 10) {
    496  1.1  cgd 			sep = "\n";
    497  1.1  cgd 			cnt = 0;
    498  1.1  cgd 		} else
    499  1.1  cgd 			sep = ", ";
    500  1.1  cgd 
    501  1.1  cgd 	}
    502  1.1  cgd 	(void)fprintf(stderr, "\n");
    503  1.1  cgd }
    504  1.1  cgd #endif
    505  1.1  cgd 
    506  1.1  cgd #ifdef DEBUG
    507  1.1  cgd #if __STDC__
    508  1.1  cgd #include <stdarg.h>
    509  1.1  cgd #else
    510  1.1  cgd #include <varargs.h>
    511  1.1  cgd #endif
    512  1.1  cgd 
    513  1.1  cgd static void
    514  1.1  cgd #if __STDC__
    515  1.1  cgd __mpoolerr(const char *fmt, ...)
    516  1.1  cgd #else
    517  1.1  cgd __mpoolerr(fmt, va_alist)
    518  1.1  cgd 	char *fmt;
    519  1.1  cgd 	va_dcl
    520  1.1  cgd #endif
    521  1.1  cgd {
    522  1.1  cgd 	va_list ap;
    523  1.1  cgd #if __STDC__
    524  1.1  cgd 	va_start(ap, fmt);
    525  1.1  cgd #else
    526  1.1  cgd 	va_start(ap);
    527  1.1  cgd #endif
    528  1.1  cgd 	(void)vfprintf(stderr, fmt, ap);
    529  1.1  cgd 	va_end(ap);
    530  1.1  cgd 	(void)fprintf(stderr, "\n");
    531  1.1  cgd 	abort();
    532  1.1  cgd 	/* NOTREACHED */
    533  1.1  cgd }
    534  1.1  cgd #endif
    535