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