2 * Copyright (c) 1990, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * $FreeBSD: src/lib/libc/db/mpool/mpool.c,v 1.5.2.1 2001/03/05 23:05:01 obrien Exp $
30 * $DragonFly: src/lib/libc/db/mpool/mpool.c,v 1.7 2005/11/19 20:46:32 swildner Exp $
32 * @(#)mpool.c 8.5 (Berkeley) 7/26/94
35 #include "namespace.h"
36 #include <sys/param.h>
37 #include <sys/queue.h>
45 #include "un-namespace.h"
49 #define __MPOOLINTERFACE_PRIVATE
52 static BKT *mpool_bkt (MPOOL *);
53 static BKT *mpool_look (MPOOL *, pgno_t);
54 static int mpool_write (MPOOL *, BKT *);
58 * Initialize a memory pool.
61 mpool_open(void *key __unused, int fd, pgno_t pagesize, pgno_t maxcache)
68 * Get information about the file.
71 * We don't currently handle pipes, although we should.
75 if (!S_ISREG(sb.st_mode)) {
80 /* Allocate and initialize the MPOOL cookie. */
81 if ((mp = (MPOOL *)calloc(1, sizeof(MPOOL))) == NULL)
84 for (entry = 0; entry < HASHSIZE; ++entry)
85 TAILQ_INIT(&mp->hqh[entry]);
86 mp->maxcache = maxcache;
87 mp->npages = sb.st_size / pagesize;
88 mp->pagesize = pagesize;
95 * Initialize input/output filters.
98 mpool_filter(MPOOL *mp, void (*pgin)(void *, pgno_t, void *),
99 void (*pgout)(void *, pgno_t, void *), void *pgcookie)
103 mp->pgcookie = pgcookie;
108 * Get a new page of memory.
111 mpool_new(MPOOL *mp, pgno_t *pgnoaddr)
116 if (mp->npages == MAX_PAGE_NUMBER) {
117 fprintf(stderr, "mpool_new: page allocation overflow.\n");
124 * Get a BKT from the cache. Assign a new page number, attach
125 * it to the head of the hash chain, the tail of the lru chain,
128 if ((bp = mpool_bkt(mp)) == NULL)
130 *pgnoaddr = bp->pgno = mp->npages++;
131 bp->flags = MPOOL_PINNED;
133 head = &mp->hqh[HASHKEY(bp->pgno)];
134 TAILQ_INSERT_HEAD(head, bp, hq);
135 TAILQ_INSERT_TAIL(&mp->lqh, bp, q);
144 mpool_get(MPOOL *mp, pgno_t pgno, u_int flags __unused)
151 /* Check for attempt to retrieve a non-existent page. */
152 if (pgno >= mp->npages) {
161 /* Check for a page that is cached. */
162 if ((bp = mpool_look(mp, pgno)) != NULL) {
164 if (bp->flags & MPOOL_PINNED) {
166 "mpool_get: page %d already pinned\n", bp->pgno);
171 * Move the page to the head of the hash chain and the tail
174 head = &mp->hqh[HASHKEY(bp->pgno)];
175 TAILQ_REMOVE(head, bp, hq);
176 TAILQ_INSERT_HEAD(head, bp, hq);
177 TAILQ_REMOVE(&mp->lqh, bp, q);
178 TAILQ_INSERT_TAIL(&mp->lqh, bp, q);
180 /* Return a pinned page. */
181 bp->flags |= MPOOL_PINNED;
185 /* Get a page from the cache. */
186 if ((bp = mpool_bkt(mp)) == NULL)
189 /* Read in the contents. */
193 off = mp->pagesize * pgno;
194 nr = pread(mp->fd, bp->page, mp->pagesize, off);
195 if (nr != mp->pagesize) {
201 /* Set the page number, pin the page. */
203 bp->flags = MPOOL_PINNED;
206 * Add the page to the head of the hash chain and the tail
209 head = &mp->hqh[HASHKEY(bp->pgno)];
210 TAILQ_INSERT_HEAD(head, bp, hq);
211 TAILQ_INSERT_TAIL(&mp->lqh, bp, q);
213 /* Run through the user's filter. */
214 if (mp->pgin != NULL)
215 (mp->pgin)(mp->pgcookie, bp->pgno, bp->page);
225 mpool_put(MPOOL *mp __unused, void *page, u_int flags)
232 bp = (BKT *)((char *)page - sizeof(BKT));
234 if (!(bp->flags & MPOOL_PINNED)) {
236 "mpool_put: page %d not pinned\n", bp->pgno);
240 bp->flags &= ~MPOOL_PINNED;
241 bp->flags |= flags & MPOOL_DIRTY;
242 return (RET_SUCCESS);
247 * Close the buffer pool.
250 mpool_close(MPOOL *mp)
254 /* Free up any space allocated to the lru pages. */
255 while (!TAILQ_EMPTY(&mp->lqh)) {
256 bp = TAILQ_FIRST(&mp->lqh);
257 TAILQ_REMOVE(&mp->lqh, bp, q);
261 /* Free the MPOOL cookie. */
263 return (RET_SUCCESS);
268 * Sync the pool to disk.
271 mpool_sync(MPOOL *mp)
275 /* Walk the lru chain, flushing any dirty pages to disk. */
276 TAILQ_FOREACH(bp, &mp->lqh, q)
277 if (bp->flags & MPOOL_DIRTY &&
278 mpool_write(mp, bp) == RET_ERROR)
281 /* Sync the file descriptor. */
282 return (_fsync(mp->fd) ? RET_ERROR : RET_SUCCESS);
287 * Get a page from the cache (or create one).
295 /* If under the max cached, always create a new page. */
296 if (mp->curcache < mp->maxcache)
300 * If the cache is max'd out, walk the lru list for a buffer we
301 * can flush. If we find one, write it (if necessary) and take it
302 * off any lists. If we don't find anything we grow the cache anyway.
303 * The cache never shrinks.
305 TAILQ_FOREACH(bp, &mp->lqh, q)
306 if (!(bp->flags & MPOOL_PINNED)) {
307 /* Flush if dirty. */
308 if (bp->flags & MPOOL_DIRTY &&
309 mpool_write(mp, bp) == RET_ERROR)
314 /* Remove from the hash and lru queues. */
315 head = &mp->hqh[HASHKEY(bp->pgno)];
316 TAILQ_REMOVE(head, bp, hq);
317 TAILQ_REMOVE(&mp->lqh, bp, q);
321 memset(bp, 0xff, sizeof(BKT) + mp->pagesize);
328 new: if ((bp = (BKT *)calloc(1, sizeof(BKT) + mp->pagesize)) == NULL)
333 #if defined(DEBUG) || defined(PURIFY)
334 memset(bp, 0xff, sizeof(BKT) + mp->pagesize);
336 bp->page = (char *)bp + sizeof(BKT);
343 * Write a page to disk.
346 mpool_write(MPOOL *mp, BKT *bp)
354 /* Run through the user's filter. */
356 (mp->pgout)(mp->pgcookie, bp->pgno, bp->page);
358 off = mp->pagesize * bp->pgno;
359 if (pwrite(mp->fd, bp->page, mp->pagesize, off) != mp->pagesize)
362 bp->flags &= ~MPOOL_DIRTY;
363 return (RET_SUCCESS);
368 * Lookup a page in the cache.
371 mpool_look(MPOOL *mp, pgno_t pgno)
376 head = &mp->hqh[HASHKEY(pgno)];
377 TAILQ_FOREACH(bp, head, hq)
378 if (bp->pgno == pgno) {
393 * Print out cache statistics.
396 mpool_stat(MPOOL *mp)
402 fprintf(stderr, "%u pages in the file\n", mp->npages);
404 "page size %lu, caching %u pages of %u page max cache\n",
405 mp->pagesize, mp->curcache, mp->maxcache);
406 fprintf(stderr, "%lu page puts, %lu page gets, %lu page new\n",
407 mp->pageput, mp->pageget, mp->pagenew);
408 fprintf(stderr, "%lu page allocs, %lu page flushes\n",
409 mp->pagealloc, mp->pageflush);
410 if (mp->cachehit + mp->cachemiss)
412 "%.0f%% cache hit rate (%lu hits, %lu misses)\n",
413 ((double)mp->cachehit / (mp->cachehit + mp->cachemiss))
414 * 100, mp->cachehit, mp->cachemiss);
415 fprintf(stderr, "%lu page reads, %lu page writes\n",
416 mp->pageread, mp->pagewrite);
420 TAILQ_FOREACH(bp, &mp->lqh, q) {
421 fprintf(stderr, "%s%d", sep, bp->pgno);
422 if (bp->flags & MPOOL_DIRTY)
423 fprintf(stderr, "d");
424 if (bp->flags & MPOOL_PINNED)
425 fprintf(stderr, "P");
433 fprintf(stderr, "\n");