]>
git.saurik.com Git - apple/libc.git/blob - db/mpool/FreeBSD/mpool.c
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: src/lib/libc/db/mpool/mpool.c,v 1.16 2009/03/28 04:00:46 delphij Exp $");
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(mp
, pgnoaddr
)
120 if (mp
->npages
== MAX_PAGE_NUMBER
) {
121 (void)fprintf(stderr
, "mpool_new: page allocation overflow.\n");
122 LIBC_ABORT("page allocation overflow");
128 * Get a BKT from the cache. Assign a new page number, attach
129 * it to the head of the hash chain, the tail of the lru chain,
132 if ((bp
= mpool_bkt(mp
)) == NULL
)
134 *pgnoaddr
= bp
->pgno
= mp
->npages
++;
135 bp
->flags
= MPOOL_PINNED
;
137 head
= &mp
->hqh
[HASHKEY(bp
->pgno
)];
138 TAILQ_INSERT_HEAD(head
, bp
, hq
);
139 TAILQ_INSERT_TAIL(&mp
->lqh
, bp
, q
);
149 mpool_get(MPOOL
*mp
, pgno_t pgno
,
150 u_int flags
) /* XXX not used? */
157 /* Check for attempt to retrieve a non-existent page. */
158 if (pgno
>= mp
->npages
) {
167 /* Check for a page that is cached. */
168 if ((bp
= mpool_look(mp
, pgno
)) != NULL
) {
170 if (bp
->flags
& MPOOL_PINNED
) {
171 (void)fprintf(stderr
,
172 "mpool_get: page %d already pinned\n", bp
->pgno
);
173 LIBC_ABORT("page %d already pinned", bp
->pgno
);
177 * Move the page to the head of the hash chain and the tail
180 head
= &mp
->hqh
[HASHKEY(bp
->pgno
)];
181 TAILQ_REMOVE(head
, bp
, hq
);
182 TAILQ_INSERT_HEAD(head
, bp
, hq
);
183 TAILQ_REMOVE(&mp
->lqh
, bp
, q
);
184 TAILQ_INSERT_TAIL(&mp
->lqh
, bp
, q
);
186 /* Return a pinned page. */
187 bp
->flags
|= MPOOL_PINNED
;
191 /* Get a page from the cache. */
192 if ((bp
= mpool_bkt(mp
)) == NULL
)
195 /* Read in the contents. */
199 off
= mp
->pagesize
* pgno
;
200 nr
= pread(mp
->fd
, bp
->page
, mp
->pagesize
, off
);
201 if (nr
!= mp
->pagesize
) {
207 /* Set the page number, pin the page. */
209 bp
->flags
= MPOOL_PINNED
;
212 * Add the page to the head of the hash chain and the tail
215 head
= &mp
->hqh
[HASHKEY(bp
->pgno
)];
216 TAILQ_INSERT_HEAD(head
, bp
, hq
);
217 TAILQ_INSERT_TAIL(&mp
->lqh
, bp
, q
);
219 /* Run through the user's filter. */
220 if (mp
->pgin
!= NULL
)
221 (mp
->pgin
)(mp
->pgcookie
, bp
->pgno
, bp
->page
);
232 mpool_put(MPOOL
*mp
, void *page
, u_int flags
)
239 bp
= (BKT
*)((char *)page
- sizeof(BKT
));
241 if (!(bp
->flags
& MPOOL_PINNED
)) {
242 (void)fprintf(stderr
,
243 "mpool_put: page %d not pinned\n", bp
->pgno
);
244 LIBC_ABORT("page %d not pinned", bp
->pgno
);
247 bp
->flags
&= ~MPOOL_PINNED
;
248 bp
->flags
|= flags
& MPOOL_DIRTY
;
249 return (RET_SUCCESS
);
254 * Close the buffer pool.
257 mpool_close(MPOOL
*mp
)
261 /* Free up any space allocated to the lru pages. */
262 while (!TAILQ_EMPTY(&mp
->lqh
)) {
263 bp
= TAILQ_FIRST(&mp
->lqh
);
264 TAILQ_REMOVE(&mp
->lqh
, bp
, q
);
268 /* Free the MPOOL cookie. */
270 return (RET_SUCCESS
);
275 * Sync the pool to disk.
278 mpool_sync(MPOOL
*mp
)
282 /* Walk the lru chain, flushing any dirty pages to disk. */
283 TAILQ_FOREACH(bp
, &mp
->lqh
, q
) {
284 if (bp
->flags
& MPOOL_DIRTY
)
285 if (mpool_write(mp
, bp
) == RET_ERROR
) {
288 /* 4874757: Re-run through the user's pgin filter. */
289 if (mp
->pgin
!= NULL
)
290 (mp
->pgin
)(mp
->pgcookie
, bp
->pgno
, bp
->page
);
294 /* Sync the file descriptor. */
295 return (_fsync(mp
->fd
) ? RET_ERROR
: RET_SUCCESS
);
300 * Get a page from the cache (or create one).
308 /* If under the max cached, always create a new page. */
309 if (mp
->curcache
< mp
->maxcache
)
313 * If the cache is max'd out, walk the lru list for a buffer we
314 * can flush. If we find one, write it (if necessary) and take it
315 * off any lists. If we don't find anything we grow the cache anyway.
316 * The cache never shrinks.
318 TAILQ_FOREACH(bp
, &mp
->lqh
, q
)
319 if (!(bp
->flags
& MPOOL_PINNED
)) {
320 /* Flush if dirty. */
321 if (bp
->flags
& MPOOL_DIRTY
&&
322 mpool_write(mp
, bp
) == RET_ERROR
)
327 /* Remove from the hash and lru queues. */
328 head
= &mp
->hqh
[HASHKEY(bp
->pgno
)];
329 TAILQ_REMOVE(head
, bp
, hq
);
330 TAILQ_REMOVE(&mp
->lqh
, bp
, q
);
334 memset(bp
, 0xff, sizeof(BKT
) + mp
->pagesize
);
341 new: if ((bp
= (BKT
*)calloc(1, sizeof(BKT
) + mp
->pagesize
)) == NULL
)
346 bp
->page
= (char *)bp
+ sizeof(BKT
);
353 * Write a page to disk.
356 mpool_write(MPOOL
*mp
, BKT
*bp
)
364 /* Run through the user's filter. */
366 (mp
->pgout
)(mp
->pgcookie
, bp
->pgno
, bp
->page
);
368 off
= mp
->pagesize
* bp
->pgno
;
369 if (pwrite(mp
->fd
, bp
->page
, mp
->pagesize
, off
) != mp
->pagesize
)
372 bp
->flags
&= ~MPOOL_DIRTY
;
373 return (RET_SUCCESS
);
378 * Lookup a page in the cache.
381 mpool_look(MPOOL
*mp
, pgno_t pgno
)
386 head
= &mp
->hqh
[HASHKEY(pgno
)];
387 TAILQ_FOREACH(bp
, head
, hq
)
388 if (bp
->pgno
== pgno
) {
403 * Print out cache statistics.
406 mpool_stat(MPOOL
*mp
)
412 (void)fprintf(stderr
, "%lu pages in the file\n", mp
->npages
);
413 (void)fprintf(stderr
,
414 "page size %lu, cacheing %lu pages of %lu page max cache\n",
415 mp
->pagesize
, mp
->curcache
, mp
->maxcache
);
416 (void)fprintf(stderr
, "%lu page puts, %lu page gets, %lu page new\n",
417 mp
->pageput
, mp
->pageget
, mp
->pagenew
);
418 (void)fprintf(stderr
, "%lu page allocs, %lu page flushes\n",
419 mp
->pagealloc
, mp
->pageflush
);
420 if (mp
->cachehit
+ mp
->cachemiss
)
421 (void)fprintf(stderr
,
422 "%.0f%% cache hit rate (%lu hits, %lu misses)\n",
423 ((double)mp
->cachehit
/ (mp
->cachehit
+ mp
->cachemiss
))
424 * 100, mp
->cachehit
, mp
->cachemiss
);
425 (void)fprintf(stderr
, "%lu page reads, %lu page writes\n",
426 mp
->pageread
, mp
->pagewrite
);
430 TAILQ_FOREACH(bp
, &mp
->lqh
, q
) {
431 (void)fprintf(stderr
, "%s%d", sep
, bp
->pgno
);
432 if (bp
->flags
& MPOOL_DIRTY
)
433 (void)fprintf(stderr
, "d");
434 if (bp
->flags
& MPOOL_PINNED
)
435 (void)fprintf(stderr
, "P");
443 (void)fprintf(stderr
, "\n");