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 * @(#)mpool.c 8.7 (Berkeley) 11/2/95
30 * $FreeBSD: head/lib/libc/db/mpool/mpool.c 194804 2009-06-24 01:15:10Z delphij $
33 #include "namespace.h"
34 #include <sys/param.h>
35 #include <sys/queue.h>
43 #include "un-namespace.h"
47 #define __MPOOLINTERFACE_PRIVATE
50 static BKT
*mpool_bkt(MPOOL
*);
51 static BKT
*mpool_look(MPOOL
*, pgno_t
);
52 static int mpool_write(MPOOL
*, BKT
*);
56 * Initialize a memory pool.
60 mpool_open(void *key
, int fd
, pgno_t pagesize
, pgno_t maxcache
)
67 * Get information about the file.
70 * We don't currently handle pipes, although we should.
74 if (!S_ISREG(sb
.st_mode
)) {
79 /* Allocate and initialize the MPOOL cookie. */
80 if ((mp
= (MPOOL
*)calloc(1, sizeof(MPOOL
))) == NULL
)
83 for (entry
= 0; entry
< HASHSIZE
; ++entry
)
84 TAILQ_INIT(&mp
->hqh
[entry
]);
85 mp
->maxcache
= maxcache
;
86 mp
->npages
= sb
.st_size
/ pagesize
;
87 mp
->pagesize
= pagesize
;
94 * Initialize input/output filters.
97 mpool_filter(MPOOL
*mp
, void (*pgin
)(void *, pgno_t
, void *),
98 void (*pgout
) (void *, pgno_t
, void *), void *pgcookie
)
102 mp
->pgcookie
= pgcookie
;
107 * Get a new page of memory.
110 mpool_new(MPOOL
*mp
, pgno_t
*pgnoaddr
, unsigned int flags
)
115 if (mp
->npages
== MAX_PAGE_NUMBER
) {
116 fprintf(stderr
, "mpool_new: page allocation overflow.\n");
123 * Get a BKT from the cache. Assign a new page number, attach
124 * it to the head of the hash chain, the tail of the lru chain,
127 if ((bp
= mpool_bkt(mp
)) == NULL
)
129 if (flags
== MPOOL_PAGE_REQUEST
) {
131 bp
->pgno
= *pgnoaddr
;
133 bp
->pgno
= *pgnoaddr
= mp
->npages
++;
135 bp
->flags
= MPOOL_PINNED
| MPOOL_INUSE
;
137 head
= &mp
->hqh
[HASHKEY(bp
->pgno
)];
138 TAILQ_INSERT_HEAD(head
, bp
, hq
);
139 TAILQ_INSERT_TAIL(&mp
->lqh
, bp
, q
);
144 mpool_delete(MPOOL
*mp
, void *page
)
149 bp
= (BKT
*)((char *)page
- sizeof(BKT
));
152 if (!(bp
->flags
& MPOOL_PINNED
)) {
154 "mpool_delete: page %d not pinned\n", bp
->pgno
);
159 /* Remove from the hash and lru queues. */
160 head
= &mp
->hqh
[HASHKEY(bp
->pgno
)];
161 TAILQ_REMOVE(head
, bp
, hq
);
162 TAILQ_REMOVE(&mp
->lqh
, bp
, q
);
166 return (RET_SUCCESS
);
175 mpool_get(MPOOL
*mp
, pgno_t pgno
,
176 unsigned int flags
) /* XXX not used? */
187 /* Check for a page that is cached. */
188 if ((bp
= mpool_look(mp
, pgno
)) != NULL
) {
190 if (!(flags
& MPOOL_IGNOREPIN
) && bp
->flags
& MPOOL_PINNED
) {
192 "mpool_get: page %d already pinned\n", bp
->pgno
);
197 * Move the page to the head of the hash chain and the tail
200 head
= &mp
->hqh
[HASHKEY(bp
->pgno
)];
201 TAILQ_REMOVE(head
, bp
, hq
);
202 TAILQ_INSERT_HEAD(head
, bp
, hq
);
203 TAILQ_REMOVE(&mp
->lqh
, bp
, q
);
204 TAILQ_INSERT_TAIL(&mp
->lqh
, bp
, q
);
206 /* Return a pinned page. */
207 bp
->flags
|= MPOOL_PINNED
;
211 /* Get a page from the cache. */
212 if ((bp
= mpool_bkt(mp
)) == NULL
)
215 /* Read in the contents. */
216 off
= mp
->pagesize
* pgno
;
217 if ((nr
= pread(mp
->fd
, bp
->page
, mp
->pagesize
, off
)) != (ssize_t
)mp
->pagesize
) {
220 /* errno is set for us by pread(). */
226 * A zero-length read means you need to create a
229 memset(bp
->page
, 0, mp
->pagesize
);
232 /* A partial read is definitely bad. */
243 /* Set the page number, pin the page. */
245 if (!(flags
& MPOOL_IGNOREPIN
))
246 bp
->flags
= MPOOL_PINNED
;
247 bp
->flags
|= MPOOL_INUSE
;
250 * Add the page to the head of the hash chain and the tail
253 head
= &mp
->hqh
[HASHKEY(bp
->pgno
)];
254 TAILQ_INSERT_HEAD(head
, bp
, hq
);
255 TAILQ_INSERT_TAIL(&mp
->lqh
, bp
, q
);
257 /* Run through the user's filter. */
258 if (mp
->pgin
!= NULL
)
259 (mp
->pgin
)(mp
->pgcookie
, bp
->pgno
, bp
->page
);
270 mpool_put(MPOOL
*mp
, void *page
, unsigned int flags
)
277 bp
= (BKT
*)((char *)page
- sizeof(BKT
));
279 if (!(bp
->flags
& MPOOL_PINNED
)) {
281 "mpool_put: page %d not pinned\n", bp
->pgno
);
285 bp
->flags
&= ~MPOOL_PINNED
;
286 if (flags
& MPOOL_DIRTY
)
287 bp
->flags
|= flags
& MPOOL_DIRTY
;
288 return (RET_SUCCESS
);
293 * Close the buffer pool.
296 mpool_close(MPOOL
*mp
)
300 /* Free up any space allocated to the lru pages. */
301 while (!TAILQ_EMPTY(&mp
->lqh
)) {
302 bp
= TAILQ_FIRST(&mp
->lqh
);
303 TAILQ_REMOVE(&mp
->lqh
, bp
, q
);
307 /* Free the MPOOL cookie. */
309 return (RET_SUCCESS
);
314 * Sync the pool to disk.
317 mpool_sync(MPOOL
*mp
)
321 /* Walk the lru chain, flushing any dirty pages to disk. */
322 TAILQ_FOREACH(bp
, &mp
->lqh
, q
)
323 if (bp
->flags
& MPOOL_DIRTY
&&
324 mpool_write(mp
, bp
) == RET_ERROR
)
327 /* Sync the file descriptor. */
328 return (_fsync(mp
->fd
) ? RET_ERROR
: RET_SUCCESS
);
333 * Get a page from the cache (or create one).
341 /* If under the max cached, always create a new page. */
342 if (mp
->curcache
< mp
->maxcache
)
346 * If the cache is max'd out, walk the lru list for a buffer we
347 * can flush. If we find one, write it (if necessary) and take it
348 * off any lists. If we don't find anything we grow the cache anyway.
349 * The cache never shrinks.
351 TAILQ_FOREACH(bp
, &mp
->lqh
, q
)
352 if (!(bp
->flags
& MPOOL_PINNED
)) {
353 /* Flush if dirty. */
354 if (bp
->flags
& MPOOL_DIRTY
&&
355 mpool_write(mp
, bp
) == RET_ERROR
)
360 /* Remove from the hash and lru queues. */
361 head
= &mp
->hqh
[HASHKEY(bp
->pgno
)];
362 TAILQ_REMOVE(head
, bp
, hq
);
363 TAILQ_REMOVE(&mp
->lqh
, bp
, q
);
367 memset(bp
, 0xff, sizeof(BKT
) + mp
->pagesize
);
375 new: if ((bp
= (BKT
*)calloc(1, sizeof(BKT
) + mp
->pagesize
)) == NULL
)
380 bp
->page
= (char *)bp
+ sizeof(BKT
);
388 * Write a page to disk.
391 mpool_write(MPOOL
*mp
, BKT
*bp
)
399 /* Run through the user's filter. */
401 (mp
->pgout
)(mp
->pgcookie
, bp
->pgno
, bp
->page
);
403 off
= mp
->pagesize
* bp
->pgno
;
404 if (pwrite(mp
->fd
, bp
->page
, mp
->pagesize
, off
) != (ssize_t
)mp
->pagesize
)
408 * Re-run through the input filter since this page may soon be
409 * accessed via the cache, and whatever the user's output filter
410 * did may screw things up if we don't let the input filter
411 * restore the in-core copy.
414 (mp
->pgin
)(mp
->pgcookie
, bp
->pgno
, bp
->page
);
416 bp
->flags
&= ~MPOOL_DIRTY
;
417 return (RET_SUCCESS
);
422 * Lookup a page in the cache.
425 mpool_look(MPOOL
*mp
, pgno_t pgno
)
430 head
= &mp
->hqh
[HASHKEY(pgno
)];
431 TAILQ_FOREACH(bp
, head
, hq
)
432 if ((bp
->pgno
== pgno
) &&
433 ((bp
->flags
& MPOOL_INUSE
) == MPOOL_INUSE
)) {
448 * Print out cache statistics.
451 mpool_stat(MPOOL
*mp
)
457 fprintf(stderr
, "%lu pages in the file\n", mp
->npages
);
459 "page size %lu, caching %lu pages of %lu page max cache\n",
460 mp
->pagesize
, mp
->curcache
, mp
->maxcache
);
461 fprintf(stderr
, "%lu page puts, %lu page gets, %lu page new\n",
462 mp
->pageput
, mp
->pageget
, mp
->pagenew
);
463 fprintf(stderr
, "%lu page allocs, %lu page flushes\n",
464 mp
->pagealloc
, mp
->pageflush
);
465 if (mp
->cachehit
+ mp
->cachemiss
)
467 "%.0f%% cache hit rate (%lu hits, %lu misses)\n",
468 ((double)mp
->cachehit
/ (mp
->cachehit
+ mp
->cachemiss
))
469 * 100, mp
->cachehit
, mp
->cachemiss
);
470 fprintf(stderr
, "%lu page reads, %lu page writes\n",
471 mp
->pageread
, mp
->pagewrite
);
475 TAILQ_FOREACH(bp
, &mp
->lqh
, q
) {
476 fprintf(stderr
, "%s%d", sep
, bp
->pgno
);
477 if (bp
->flags
& MPOOL_DIRTY
)
478 fprintf(stderr
, "d");
479 if (bp
->flags
& MPOOL_PINNED
)
480 fprintf(stderr
, "P");
487 fprintf(stderr
, "\n");