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 * 4. 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
30 #if defined(LIBC_SCCS) && !defined(lint)
31 static char sccsid[] = "@(#)mpool.c 8.7 (Berkeley) 11/2/95";
32 #endif /* LIBC_SCCS and not lint */
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include "namespace.h"
37 #include <sys/param.h>
38 #include <sys/queue.h>
46 #include "un-namespace.h"
50 #define __MPOOLINTERFACE_PRIVATE
53 static BKT *mpool_bkt(MPOOL *);
54 static BKT *mpool_look(MPOOL *, pgno_t);
55 static int mpool_write(MPOOL *, BKT *);
59 * Initialize a memory pool.
63 mpool_open(void *key, int fd, pgno_t pagesize, pgno_t maxcache)
70 * Get information about the file.
73 * We don't currently handle pipes, although we should.
77 if (!S_ISREG(sb.st_mode)) {
82 /* Allocate and initialize the MPOOL cookie. */
83 if ((mp = (MPOOL *)calloc(1, sizeof(MPOOL))) == NULL)
86 for (entry = 0; entry < HASHSIZE; ++entry)
87 TAILQ_INIT(&mp->hqh[entry]);
88 mp->maxcache = maxcache;
89 mp->npages = sb.st_size / pagesize;
90 mp->pagesize = pagesize;
97 * Initialize input/output filters.
100 mpool_filter(MPOOL *mp, void (*pgin) (void *, pgno_t, void *),
101 void (*pgout) (void *, pgno_t, void *), void *pgcookie)
105 mp->pgcookie = pgcookie;
110 * Get a new page of memory.
113 mpool_new(MPOOL *mp, pgno_t *pgnoaddr, u_int flags)
118 if (mp->npages == MAX_PAGE_NUMBER) {
119 (void)fprintf(stderr, "mpool_new: page allocation overflow.\n");
126 * Get a BKT from the cache. Assign a new page number, attach
127 * it to the head of the hash chain, the tail of the lru chain,
130 if ((bp = mpool_bkt(mp)) == NULL)
132 if (flags == MPOOL_PAGE_REQUEST) {
134 bp->pgno = *pgnoaddr;
136 bp->pgno = *pgnoaddr = mp->npages++;
138 bp->flags = MPOOL_PINNED | MPOOL_INUSE;
140 head = &mp->hqh[HASHKEY(bp->pgno)];
141 TAILQ_INSERT_HEAD(head, bp, hq);
142 TAILQ_INSERT_TAIL(&mp->lqh, bp, q);
147 mpool_delete(MPOOL *mp, void *page)
152 bp = (BKT *)((char *)page - sizeof(BKT));
155 if (!(bp->flags & MPOOL_PINNED)) {
156 (void)fprintf(stderr,
157 "mpool_delete: page %d not pinned\n", bp->pgno);
162 /* Remove from the hash and lru queues. */
163 head = &mp->hqh[HASHKEY(bp->pgno)];
164 TAILQ_REMOVE(head, bp, hq);
165 TAILQ_REMOVE(&mp->lqh, bp, q);
169 return (RET_SUCCESS);
178 mpool_get(MPOOL *mp, pgno_t pgno,
179 u_int flags) /* XXX not used? */
190 /* Check for a page that is cached. */
191 if ((bp = mpool_look(mp, pgno)) != NULL) {
193 if (!(flags & MPOOL_IGNOREPIN) && bp->flags & MPOOL_PINNED) {
194 (void)fprintf(stderr,
195 "mpool_get: page %d already pinned\n", bp->pgno);
200 * Move the page to the head of the hash chain and the tail
203 head = &mp->hqh[HASHKEY(bp->pgno)];
204 TAILQ_REMOVE(head, bp, hq);
205 TAILQ_INSERT_HEAD(head, bp, hq);
206 TAILQ_REMOVE(&mp->lqh, bp, q);
207 TAILQ_INSERT_TAIL(&mp->lqh, bp, q);
209 /* Return a pinned page. */
210 bp->flags |= MPOOL_PINNED;
214 /* Get a page from the cache. */
215 if ((bp = mpool_bkt(mp)) == NULL)
218 /* Read in the contents. */
219 off = mp->pagesize * pgno;
220 if ((nr = pread(mp->fd, bp->page, mp->pagesize, off)) != (ssize_t)mp->pagesize) {
223 /* errno is set for us by pread(). */
229 * A zero-length read means you need to create a
232 memset(bp->page, 0, mp->pagesize);
235 /* A partial read is definitely bad. */
246 /* Set the page number, pin the page. */
248 if (!(flags & MPOOL_IGNOREPIN))
249 bp->flags = MPOOL_PINNED;
250 bp->flags |= MPOOL_INUSE;
253 * Add the page to the head of the hash chain and the tail
256 head = &mp->hqh[HASHKEY(bp->pgno)];
257 TAILQ_INSERT_HEAD(head, bp, hq);
258 TAILQ_INSERT_TAIL(&mp->lqh, bp, q);
260 /* Run through the user's filter. */
261 if (mp->pgin != NULL)
262 (mp->pgin)(mp->pgcookie, bp->pgno, bp->page);
273 mpool_put(MPOOL *mp, void *page, u_int flags)
280 bp = (BKT *)((char *)page - sizeof(BKT));
282 if (!(bp->flags & MPOOL_PINNED)) {
283 (void)fprintf(stderr,
284 "mpool_put: page %d not pinned\n", bp->pgno);
288 bp->flags &= ~MPOOL_PINNED;
289 if (flags & MPOOL_DIRTY)
290 bp->flags |= flags & MPOOL_DIRTY;
291 return (RET_SUCCESS);
296 * Close the buffer pool.
299 mpool_close(MPOOL *mp)
303 /* Free up any space allocated to the lru pages. */
304 while (!TAILQ_EMPTY(&mp->lqh)) {
305 bp = TAILQ_FIRST(&mp->lqh);
306 TAILQ_REMOVE(&mp->lqh, bp, q);
310 /* Free the MPOOL cookie. */
312 return (RET_SUCCESS);
317 * Sync the pool to disk.
320 mpool_sync(MPOOL *mp)
324 /* Walk the lru chain, flushing any dirty pages to disk. */
325 TAILQ_FOREACH(bp, &mp->lqh, q)
326 if (bp->flags & MPOOL_DIRTY &&
327 mpool_write(mp, bp) == RET_ERROR)
330 /* Sync the file descriptor. */
331 return (_fsync(mp->fd) ? RET_ERROR : RET_SUCCESS);
336 * Get a page from the cache (or create one).
344 /* If under the max cached, always create a new page. */
345 if (mp->curcache < mp->maxcache)
349 * If the cache is max'd out, walk the lru list for a buffer we
350 * can flush. If we find one, write it (if necessary) and take it
351 * off any lists. If we don't find anything we grow the cache anyway.
352 * The cache never shrinks.
354 TAILQ_FOREACH(bp, &mp->lqh, q)
355 if (!(bp->flags & MPOOL_PINNED)) {
356 /* Flush if dirty. */
357 if (bp->flags & MPOOL_DIRTY &&
358 mpool_write(mp, bp) == RET_ERROR)
363 /* Remove from the hash and lru queues. */
364 head = &mp->hqh[HASHKEY(bp->pgno)];
365 TAILQ_REMOVE(head, bp, hq);
366 TAILQ_REMOVE(&mp->lqh, bp, q);
370 memset(bp, 0xff, sizeof(BKT) + mp->pagesize);
378 new: if ((bp = (BKT *)calloc(1, sizeof(BKT) + mp->pagesize)) == NULL)
383 bp->page = (char *)bp + sizeof(BKT);
391 * Write a page to disk.
394 mpool_write(MPOOL *mp, BKT *bp)
402 /* Run through the user's filter. */
404 (mp->pgout)(mp->pgcookie, bp->pgno, bp->page);
406 off = mp->pagesize * bp->pgno;
407 if (pwrite(mp->fd, bp->page, mp->pagesize, off) != (ssize_t)mp->pagesize)
411 * Re-run through the input filter since this page may soon be
412 * accessed via the cache, and whatever the user's output filter
413 * did may screw things up if we don't let the input filter
414 * restore the in-core copy.
417 (mp->pgin)(mp->pgcookie, bp->pgno, bp->page);
419 bp->flags &= ~MPOOL_DIRTY;
420 return (RET_SUCCESS);
425 * Lookup a page in the cache.
428 mpool_look(MPOOL *mp, pgno_t pgno)
433 head = &mp->hqh[HASHKEY(pgno)];
434 TAILQ_FOREACH(bp, head, hq)
435 if ((bp->pgno == pgno) &&
436 ((bp->flags & MPOOL_INUSE) == MPOOL_INUSE)) {
451 * Print out cache statistics.
454 mpool_stat(MPOOL *mp)
460 (void)fprintf(stderr, "%lu pages in the file\n", mp->npages);
461 (void)fprintf(stderr,
462 "page size %lu, cacheing %lu pages of %lu page max cache\n",
463 mp->pagesize, mp->curcache, mp->maxcache);
464 (void)fprintf(stderr, "%lu page puts, %lu page gets, %lu page new\n",
465 mp->pageput, mp->pageget, mp->pagenew);
466 (void)fprintf(stderr, "%lu page allocs, %lu page flushes\n",
467 mp->pagealloc, mp->pageflush);
468 if (mp->cachehit + mp->cachemiss)
469 (void)fprintf(stderr,
470 "%.0f%% cache hit rate (%lu hits, %lu misses)\n",
471 ((double)mp->cachehit / (mp->cachehit + mp->cachemiss))
472 * 100, mp->cachehit, mp->cachemiss);
473 (void)fprintf(stderr, "%lu page reads, %lu page writes\n",
474 mp->pageread, mp->pagewrite);
478 TAILQ_FOREACH(bp, &mp->lqh, q) {
479 (void)fprintf(stderr, "%s%d", sep, bp->pgno);
480 if (bp->flags & MPOOL_DIRTY)
481 (void)fprintf(stderr, "d");
482 if (bp->flags & MPOOL_PINNED)
483 (void)fprintf(stderr, "P");
491 (void)fprintf(stderr, "\n");