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