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 if (lseek(mp->fd, off, SEEK_SET) != off)
196 if ((nr = _read(mp->fd, bp->page, mp->pagesize)) != mp->pagesize) {
202 /* Set the page number, pin the page. */
204 bp->flags = MPOOL_PINNED;
207 * Add the page to the head of the hash chain and the tail
210 head = &mp->hqh[HASHKEY(bp->pgno)];
211 TAILQ_INSERT_HEAD(head, bp, hq);
212 TAILQ_INSERT_TAIL(&mp->lqh, bp, q);
214 /* Run through the user's filter. */
215 if (mp->pgin != NULL)
216 (mp->pgin)(mp->pgcookie, bp->pgno, bp->page);
226 mpool_put(MPOOL *mp __unused, void *page, u_int flags)
233 bp = (BKT *)((char *)page - sizeof(BKT));
235 if (!(bp->flags & MPOOL_PINNED)) {
237 "mpool_put: page %d not pinned\n", bp->pgno);
241 bp->flags &= ~MPOOL_PINNED;
242 bp->flags |= flags & MPOOL_DIRTY;
243 return (RET_SUCCESS);
248 * Close the buffer pool.
251 mpool_close(MPOOL *mp)
255 /* Free up any space allocated to the lru pages. */
256 while (!TAILQ_EMPTY(&mp->lqh)) {
257 bp = TAILQ_FIRST(&mp->lqh);
258 TAILQ_REMOVE(&mp->lqh, bp, q);
262 /* Free the MPOOL cookie. */
264 return (RET_SUCCESS);
269 * Sync the pool to disk.
272 mpool_sync(MPOOL *mp)
276 /* Walk the lru chain, flushing any dirty pages to disk. */
277 TAILQ_FOREACH(bp, &mp->lqh, q)
278 if (bp->flags & MPOOL_DIRTY &&
279 mpool_write(mp, bp) == RET_ERROR)
282 /* Sync the file descriptor. */
283 return (_fsync(mp->fd) ? RET_ERROR : RET_SUCCESS);
288 * Get a page from the cache (or create one).
296 /* If under the max cached, always create a new page. */
297 if (mp->curcache < mp->maxcache)
301 * If the cache is max'd out, walk the lru list for a buffer we
302 * can flush. If we find one, write it (if necessary) and take it
303 * off any lists. If we don't find anything we grow the cache anyway.
304 * The cache never shrinks.
306 TAILQ_FOREACH(bp, &mp->lqh, q)
307 if (!(bp->flags & MPOOL_PINNED)) {
308 /* Flush if dirty. */
309 if (bp->flags & MPOOL_DIRTY &&
310 mpool_write(mp, bp) == RET_ERROR)
315 /* Remove from the hash and lru queues. */
316 head = &mp->hqh[HASHKEY(bp->pgno)];
317 TAILQ_REMOVE(head, bp, hq);
318 TAILQ_REMOVE(&mp->lqh, bp, q);
322 memset(bp, 0xff, sizeof(BKT) + mp->pagesize);
329 new: if ((bp = (BKT *)malloc(sizeof(BKT) + mp->pagesize)) == NULL)
334 #if defined(DEBUG) || defined(PURIFY)
335 memset(bp, 0xff, sizeof(BKT) + mp->pagesize);
337 bp->page = (char *)bp + sizeof(BKT);
344 * Write a page to disk.
347 mpool_write(MPOOL *mp, BKT *bp)
355 /* Run through the user's filter. */
357 (mp->pgout)(mp->pgcookie, bp->pgno, bp->page);
359 off = mp->pagesize * bp->pgno;
360 if (lseek(mp->fd, off, SEEK_SET) != off)
362 if (_write(mp->fd, bp->page, mp->pagesize) != mp->pagesize)
365 bp->flags &= ~MPOOL_DIRTY;
366 return (RET_SUCCESS);
371 * Lookup a page in the cache.
374 mpool_look(MPOOL *mp, pgno_t pgno)
379 head = &mp->hqh[HASHKEY(pgno)];
380 TAILQ_FOREACH(bp, head, hq)
381 if (bp->pgno == pgno) {
396 * Print out cache statistics.
399 mpool_stat(MPOOL *mp)
405 fprintf(stderr, "%lu pages in the file\n", mp->npages);
407 "page size %lu, cacheing %lu pages of %lu page max cache\n",
408 mp->pagesize, mp->curcache, mp->maxcache);
409 fprintf(stderr, "%lu page puts, %lu page gets, %lu page new\n",
410 mp->pageput, mp->pageget, mp->pagenew);
411 fprintf(stderr, "%lu page allocs, %lu page flushes\n",
412 mp->pagealloc, mp->pageflush);
413 if (mp->cachehit + mp->cachemiss)
415 "%.0f%% cache hit rate (%lu hits, %lu misses)\n",
416 ((double)mp->cachehit / (mp->cachehit + mp->cachemiss))
417 * 100, mp->cachehit, mp->cachemiss);
418 fprintf(stderr, "%lu page reads, %lu page writes\n",
419 mp->pageread, mp->pagewrite);
423 TAILQ_FOREACH(bp, &mp->lqh, q) {
424 fprintf(stderr, "%s%d", sep, bp->pgno);
425 if (bp->flags & MPOOL_DIRTY)
426 fprintf(stderr, "d");
427 if (bp->flags & MPOOL_PINNED)
428 fprintf(stderr, "P");
436 fprintf(stderr, "\n");