]>
git.saurik.com Git - apple/libc.git/blob - db/mpool/mpool-fbsd.c
4928ed2d1535c1cee8874a928410b47ab3d7f607
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. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #if defined(LIBC_SCCS) && !defined(lint)
35 static char sccsid
[] = "@(#)mpool.c 8.5 (Berkeley) 7/26/94";
36 #endif /* LIBC_SCCS and not lint */
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD: src/lib/libc/db/mpool/mpool.c,v 1.12 2004/09/10 05:41:41 kuriyama Exp $");
40 #include "namespace.h"
41 #include <sys/param.h>
42 #include <sys/queue.h>
50 #include "un-namespace.h"
54 #define __MPOOLINTERFACE_PRIVATE
57 static BKT
*mpool_bkt(MPOOL
*);
58 static BKT
*mpool_look(MPOOL
*, pgno_t
);
59 static int mpool_write(MPOOL
*, BKT
*);
63 * Initialize a memory pool.
66 mpool_open(key
, fd
, pagesize
, maxcache
)
69 pgno_t pagesize
, maxcache
;
76 * Get information about the file.
79 * We don't currently handle pipes, although we should.
83 if (!S_ISREG(sb
.st_mode
)) {
88 /* Allocate and initialize the MPOOL cookie. */
89 if ((mp
= (MPOOL
*)calloc(1, sizeof(MPOOL
))) == NULL
)
92 for (entry
= 0; entry
< HASHSIZE
; ++entry
)
93 TAILQ_INIT(&mp
->hqh
[entry
]);
94 mp
->maxcache
= maxcache
;
95 mp
->npages
= sb
.st_size
/ pagesize
;
96 mp
->pagesize
= pagesize
;
103 * Initialize input/output filters.
106 mpool_filter(mp
, pgin
, pgout
, pgcookie
)
108 void (*pgin
)(void *, pgno_t
, void *);
109 void (*pgout
)(void *, pgno_t
, void *);
114 mp
->pgcookie
= pgcookie
;
119 * Get a new page of memory.
122 mpool_new(mp
, pgnoaddr
)
129 if (mp
->npages
== MAX_PAGE_NUMBER
) {
130 (void)fprintf(stderr
, "mpool_new: page allocation overflow.\n");
131 LIBC_ABORT("page allocation overflow");
137 * Get a BKT from the cache. Assign a new page number, attach
138 * it to the head of the hash chain, the tail of the lru chain,
141 if ((bp
= mpool_bkt(mp
)) == NULL
)
143 *pgnoaddr
= bp
->pgno
= mp
->npages
++;
144 bp
->flags
= MPOOL_PINNED
;
146 head
= &mp
->hqh
[HASHKEY(bp
->pgno
)];
147 TAILQ_INSERT_HEAD(head
, bp
, hq
);
148 TAILQ_INSERT_TAIL(&mp
->lqh
, bp
, q
);
157 mpool_get(mp
, pgno
, flags
)
160 u_int flags
; /* XXX not used? */
167 /* Check for attempt to retrieve a non-existent page. */
168 if (pgno
>= mp
->npages
) {
177 /* Check for a page that is cached. */
178 if ((bp
= mpool_look(mp
, pgno
)) != NULL
) {
180 if (bp
->flags
& MPOOL_PINNED
) {
181 (void)fprintf(stderr
,
182 "mpool_get: page %d already pinned\n", bp
->pgno
);
183 LIBC_ABORT("page %d already pinned", bp
->pgno
);
187 * Move the page to the head of the hash chain and the tail
190 head
= &mp
->hqh
[HASHKEY(bp
->pgno
)];
191 TAILQ_REMOVE(head
, bp
, hq
);
192 TAILQ_INSERT_HEAD(head
, bp
, hq
);
193 TAILQ_REMOVE(&mp
->lqh
, bp
, q
);
194 TAILQ_INSERT_TAIL(&mp
->lqh
, bp
, q
);
196 /* Return a pinned page. */
197 bp
->flags
|= MPOOL_PINNED
;
201 /* Get a page from the cache. */
202 if ((bp
= mpool_bkt(mp
)) == NULL
)
205 /* Read in the contents. */
209 off
= mp
->pagesize
* pgno
;
210 nr
= pread(mp
->fd
, bp
->page
, mp
->pagesize
, off
);
211 if (nr
!= mp
->pagesize
) {
217 /* Set the page number, pin the page. */
219 bp
->flags
= MPOOL_PINNED
;
222 * Add the page to the head of the hash chain and the tail
225 head
= &mp
->hqh
[HASHKEY(bp
->pgno
)];
226 TAILQ_INSERT_HEAD(head
, bp
, hq
);
227 TAILQ_INSERT_TAIL(&mp
->lqh
, bp
, q
);
229 /* Run through the user's filter. */
230 if (mp
->pgin
!= NULL
)
231 (mp
->pgin
)(mp
->pgcookie
, bp
->pgno
, bp
->page
);
241 mpool_put(mp
, page
, flags
)
251 bp
= (BKT
*)((char *)page
- sizeof(BKT
));
253 if (!(bp
->flags
& MPOOL_PINNED
)) {
254 (void)fprintf(stderr
,
255 "mpool_put: page %d not pinned\n", bp
->pgno
);
256 LIBC_ABORT("page %d not pinned", bp
->pgno
);
259 bp
->flags
&= ~MPOOL_PINNED
;
260 bp
->flags
|= flags
& MPOOL_DIRTY
;
261 return (RET_SUCCESS
);
266 * Close the buffer pool.
274 /* Free up any space allocated to the lru pages. */
275 while (!TAILQ_EMPTY(&mp
->lqh
)) {
276 bp
= TAILQ_FIRST(&mp
->lqh
);
277 TAILQ_REMOVE(&mp
->lqh
, bp
, q
);
281 /* Free the MPOOL cookie. */
283 return (RET_SUCCESS
);
288 * Sync the pool to disk.
296 /* Walk the lru chain, flushing any dirty pages to disk. */
297 TAILQ_FOREACH(bp
, &mp
->lqh
, q
) {
298 if (bp
->flags
& MPOOL_DIRTY
)
299 if (mpool_write(mp
, bp
) == RET_ERROR
) {
302 /* 4874757: Re-run through the user's pgin filter. */
303 if (mp
->pgin
!= NULL
)
304 (mp
->pgin
)(mp
->pgcookie
, bp
->pgno
, bp
->page
);
308 /* Sync the file descriptor. */
309 return (_fsync(mp
->fd
) ? RET_ERROR
: RET_SUCCESS
);
314 * Get a page from the cache (or create one).
323 /* If under the max cached, always create a new page. */
324 if (mp
->curcache
< mp
->maxcache
)
328 * If the cache is max'd out, walk the lru list for a buffer we
329 * can flush. If we find one, write it (if necessary) and take it
330 * off any lists. If we don't find anything we grow the cache anyway.
331 * The cache never shrinks.
333 TAILQ_FOREACH(bp
, &mp
->lqh
, q
)
334 if (!(bp
->flags
& MPOOL_PINNED
)) {
335 /* Flush if dirty. */
336 if (bp
->flags
& MPOOL_DIRTY
&&
337 mpool_write(mp
, bp
) == RET_ERROR
)
342 /* Remove from the hash and lru queues. */
343 head
= &mp
->hqh
[HASHKEY(bp
->pgno
)];
344 TAILQ_REMOVE(head
, bp
, hq
);
345 TAILQ_REMOVE(&mp
->lqh
, bp
, q
);
349 memset(bp
, 0xff, sizeof(BKT
) + mp
->pagesize
);
356 new: if ((bp
= (BKT
*)malloc(sizeof(BKT
) + mp
->pagesize
)) == NULL
)
361 #if defined(DEBUG) || defined(PURIFY)
362 memset(bp
, 0xff, sizeof(BKT
) + mp
->pagesize
);
364 bp
->page
= (char *)bp
+ sizeof(BKT
);
371 * Write a page to disk.
384 /* Run through the user's filter. */
386 (mp
->pgout
)(mp
->pgcookie
, bp
->pgno
, bp
->page
);
388 off
= mp
->pagesize
* bp
->pgno
;
389 if (pwrite(mp
->fd
, bp
->page
, mp
->pagesize
, off
) != mp
->pagesize
)
392 bp
->flags
&= ~MPOOL_DIRTY
;
393 return (RET_SUCCESS
);
398 * Lookup a page in the cache.
408 head
= &mp
->hqh
[HASHKEY(pgno
)];
409 TAILQ_FOREACH(bp
, head
, hq
)
410 if (bp
->pgno
== pgno
) {
425 * Print out cache statistics.
435 (void)fprintf(stderr
, "%u pages in the file\n", mp
->npages
);
436 (void)fprintf(stderr
,
437 "page size %lu, cacheing %u pages of %u page max cache\n",
438 mp
->pagesize
, mp
->curcache
, mp
->maxcache
);
439 (void)fprintf(stderr
, "%lu page puts, %lu page gets, %lu page new\n",
440 mp
->pageput
, mp
->pageget
, mp
->pagenew
);
441 (void)fprintf(stderr
, "%lu page allocs, %lu page flushes\n",
442 mp
->pagealloc
, mp
->pageflush
);
443 if (mp
->cachehit
+ mp
->cachemiss
)
444 (void)fprintf(stderr
,
445 "%.0f%% cache hit rate (%lu hits, %lu misses)\n",
446 ((double)mp
->cachehit
/ (mp
->cachehit
+ mp
->cachemiss
))
447 * 100, mp
->cachehit
, mp
->cachemiss
);
448 (void)fprintf(stderr
, "%lu page reads, %lu page writes\n",
449 mp
->pageread
, mp
->pagewrite
);
453 TAILQ_FOREACH(bp
, &mp
->lqh
, q
) {
454 (void)fprintf(stderr
, "%s%d", sep
, bp
->pgno
);
455 if (bp
->flags
& MPOOL_DIRTY
)
456 (void)fprintf(stderr
, "d");
457 if (bp
->flags
& MPOOL_PINNED
)
458 (void)fprintf(stderr
, "P");
466 (void)fprintf(stderr
, "\n");