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.5 (Berkeley) 7/26/94";
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.
62 mpool_open(key, fd, pagesize, maxcache)
65 pgno_t pagesize, maxcache;
72 * Get information about the file.
75 * We don't currently handle pipes, although we should.
79 if (!S_ISREG(sb.st_mode)) {
84 /* Allocate and initialize the MPOOL cookie. */
85 if ((mp = (MPOOL *)calloc(1, sizeof(MPOOL))) == NULL)
88 for (entry = 0; entry < HASHSIZE; ++entry)
89 TAILQ_INIT(&mp->hqh[entry]);
90 mp->maxcache = maxcache;
91 mp->npages = sb.st_size / pagesize;
92 mp->pagesize = pagesize;
99 * Initialize input/output filters.
102 mpool_filter(mp, pgin, pgout, pgcookie)
104 void (*pgin)(void *, pgno_t, void *);
105 void (*pgout)(void *, pgno_t, void *);
110 mp->pgcookie = pgcookie;
115 * Get a new page of memory.
118 mpool_new(mp, pgnoaddr)
125 if (mp->npages == MAX_PAGE_NUMBER) {
126 (void)fprintf(stderr, "mpool_new: page allocation overflow.\n");
133 * Get a BKT from the cache. Assign a new page number, attach
134 * it to the head of the hash chain, the tail of the lru chain,
137 if ((bp = mpool_bkt(mp)) == NULL)
139 *pgnoaddr = bp->pgno = mp->npages++;
140 bp->flags = MPOOL_PINNED;
142 head = &mp->hqh[HASHKEY(bp->pgno)];
143 TAILQ_INSERT_HEAD(head, bp, hq);
144 TAILQ_INSERT_TAIL(&mp->lqh, bp, q);
153 mpool_get(mp, pgno, flags)
156 u_int flags; /* XXX not used? */
163 /* Check for attempt to retrieve a non-existent page. */
164 if (pgno >= mp->npages) {
173 /* Check for a page that is cached. */
174 if ((bp = mpool_look(mp, pgno)) != NULL) {
176 if (bp->flags & MPOOL_PINNED) {
177 (void)fprintf(stderr,
178 "mpool_get: page %d already pinned\n", bp->pgno);
183 * Move the page to the head of the hash chain and the tail
186 head = &mp->hqh[HASHKEY(bp->pgno)];
187 TAILQ_REMOVE(head, bp, hq);
188 TAILQ_INSERT_HEAD(head, bp, hq);
189 TAILQ_REMOVE(&mp->lqh, bp, q);
190 TAILQ_INSERT_TAIL(&mp->lqh, bp, q);
192 /* Return a pinned page. */
193 bp->flags |= MPOOL_PINNED;
197 /* Get a page from the cache. */
198 if ((bp = mpool_bkt(mp)) == NULL)
201 /* Read in the contents. */
205 off = mp->pagesize * pgno;
206 nr = pread(mp->fd, bp->page, mp->pagesize, off);
207 if (nr != mp->pagesize) {
213 /* Set the page number, pin the page. */
215 bp->flags = MPOOL_PINNED;
218 * Add the page to the head of the hash chain and the tail
221 head = &mp->hqh[HASHKEY(bp->pgno)];
222 TAILQ_INSERT_HEAD(head, bp, hq);
223 TAILQ_INSERT_TAIL(&mp->lqh, bp, q);
225 /* Run through the user's filter. */
226 if (mp->pgin != NULL)
227 (mp->pgin)(mp->pgcookie, bp->pgno, bp->page);
237 mpool_put(mp, page, flags)
247 bp = (BKT *)((char *)page - sizeof(BKT));
249 if (!(bp->flags & MPOOL_PINNED)) {
250 (void)fprintf(stderr,
251 "mpool_put: page %d not pinned\n", bp->pgno);
255 bp->flags &= ~MPOOL_PINNED;
256 bp->flags |= flags & MPOOL_DIRTY;
257 return (RET_SUCCESS);
262 * Close the buffer pool.
270 /* Free up any space allocated to the lru pages. */
271 while (!TAILQ_EMPTY(&mp->lqh)) {
272 bp = TAILQ_FIRST(&mp->lqh);
273 TAILQ_REMOVE(&mp->lqh, bp, q);
277 /* Free the MPOOL cookie. */
279 return (RET_SUCCESS);
284 * Sync the pool to disk.
292 /* Walk the lru chain, flushing any dirty pages to disk. */
293 TAILQ_FOREACH(bp, &mp->lqh, q)
294 if (bp->flags & MPOOL_DIRTY &&
295 mpool_write(mp, bp) == RET_ERROR)
298 /* Sync the file descriptor. */
299 return (_fsync(mp->fd) ? RET_ERROR : RET_SUCCESS);
304 * Get a page from the cache (or create one).
313 /* If under the max cached, always create a new page. */
314 if (mp->curcache < mp->maxcache)
318 * If the cache is max'd out, walk the lru list for a buffer we
319 * can flush. If we find one, write it (if necessary) and take it
320 * off any lists. If we don't find anything we grow the cache anyway.
321 * The cache never shrinks.
323 TAILQ_FOREACH(bp, &mp->lqh, q)
324 if (!(bp->flags & MPOOL_PINNED)) {
325 /* Flush if dirty. */
326 if (bp->flags & MPOOL_DIRTY &&
327 mpool_write(mp, bp) == RET_ERROR)
332 /* Remove from the hash and lru queues. */
333 head = &mp->hqh[HASHKEY(bp->pgno)];
334 TAILQ_REMOVE(head, bp, hq);
335 TAILQ_REMOVE(&mp->lqh, bp, q);
339 memset(bp, 0xff, sizeof(BKT) + mp->pagesize);
346 new: if ((bp = (BKT *)calloc(1, sizeof(BKT) + mp->pagesize)) == NULL)
351 bp->page = (char *)bp + sizeof(BKT);
358 * Write a page to disk.
371 /* Run through the user's filter. */
373 (mp->pgout)(mp->pgcookie, bp->pgno, bp->page);
375 off = mp->pagesize * bp->pgno;
376 if (pwrite(mp->fd, bp->page, mp->pagesize, off) != mp->pagesize)
379 bp->flags &= ~MPOOL_DIRTY;
380 return (RET_SUCCESS);
385 * Lookup a page in the cache.
395 head = &mp->hqh[HASHKEY(pgno)];
396 TAILQ_FOREACH(bp, head, hq)
397 if (bp->pgno == pgno) {
412 * Print out cache statistics.
422 (void)fprintf(stderr, "%u pages in the file\n", mp->npages);
423 (void)fprintf(stderr,
424 "page size %lu, cacheing %u pages of %u page max cache\n",
425 mp->pagesize, mp->curcache, mp->maxcache);
426 (void)fprintf(stderr, "%lu page puts, %lu page gets, %lu page new\n",
427 mp->pageput, mp->pageget, mp->pagenew);
428 (void)fprintf(stderr, "%lu page allocs, %lu page flushes\n",
429 mp->pagealloc, mp->pageflush);
430 if (mp->cachehit + mp->cachemiss)
431 (void)fprintf(stderr,
432 "%.0f%% cache hit rate (%lu hits, %lu misses)\n",
433 ((double)mp->cachehit / (mp->cachehit + mp->cachemiss))
434 * 100, mp->cachehit, mp->cachemiss);
435 (void)fprintf(stderr, "%lu page reads, %lu page writes\n",
436 mp->pageread, mp->pagewrite);
440 TAILQ_FOREACH(bp, &mp->lqh, q) {
441 (void)fprintf(stderr, "%s%d", sep, bp->pgno);
442 if (bp->flags & MPOOL_DIRTY)
443 (void)fprintf(stderr, "d");
444 if (bp->flags & MPOOL_PINNED)
445 (void)fprintf(stderr, "P");
453 (void)fprintf(stderr, "\n");