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