1 /* $NetBSD: mpool.c,v 1.19 2009/04/22 18:44:06 christos Exp $ */
4 * Copyright (c) 1990, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #if HAVE_NBTOOL_CONFIG_H
33 #include "nbtool_config.h"
36 #include <sys/cdefs.h>
37 __RCSID("$NetBSD: mpool.c,v 1.19 2009/04/22 18:44:06 christos Exp $");
39 #include "namespace.h"
40 #include <sys/queue.h>
51 #define __MPOOLINTERFACE_PRIVATE
55 __weak_alias(mpool_close
,_mpool_close
)
56 __weak_alias(mpool_filter
,_mpool_filter
)
57 __weak_alias(mpool_get
,_mpool_get
)
58 __weak_alias(mpool_new
,_mpool_new
)
59 __weak_alias(mpool_open
,_mpool_open
)
60 __weak_alias(mpool_put
,_mpool_put
)
61 __weak_alias(mpool_sync
,_mpool_sync
)
64 static BKT
*mpool_bkt(MPOOL
*);
65 static BKT
*mpool_look(MPOOL
*, pgno_t
);
66 static int mpool_write(MPOOL
*, BKT
*);
70 * Initialize a memory pool.
74 mpool_open(void *key
, int fd
, pgno_t pagesize
, pgno_t maxcache
)
81 * Get information about the file.
84 * We don't currently handle pipes, although we should.
88 if (!S_ISREG(sb
.st_mode
)) {
93 /* Allocate and initialize the MPOOL cookie. */
94 if ((mp
= (MPOOL
*)calloc(1, sizeof(MPOOL
))) == NULL
)
96 CIRCLEQ_INIT(&mp
->lqh
);
97 for (entry
= 0; entry
< HASHSIZE
; ++entry
)
98 CIRCLEQ_INIT(&mp
->hqh
[entry
]);
99 mp
->maxcache
= maxcache
;
100 mp
->npages
= (pgno_t
)(sb
.st_size
/ pagesize
);
101 mp
->pagesize
= pagesize
;
108 * Initialize input/output filters.
111 mpool_filter(MPOOL
*mp
, void (*pgin
)(void *, pgno_t
, void *),
112 void (*pgout
)(void *, pgno_t
, void *), void *pgcookie
)
116 mp
->pgcookie
= pgcookie
;
121 * Get a new page of memory.
124 mpool_new( MPOOL
*mp
, pgno_t
*pgnoaddr
)
129 if (mp
->npages
== MAX_PAGE_NUMBER
) {
130 (void)fprintf(stderr
, "mpool_new: page allocation overflow.\n");
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 CIRCLEQ_INSERT_HEAD(head
, bp
, hq
);
148 CIRCLEQ_INSERT_TAIL(&mp
->lqh
, bp
, q
);
158 mpool_get(MPOOL
*mp
, pgno_t pgno
, u_int flags
)
165 /* Check for attempt to retrieve a non-existent page. */
166 if (pgno
>= mp
->npages
) {
175 /* Check for a page that is cached. */
176 if ((bp
= mpool_look(mp
, pgno
)) != NULL
) {
178 if (bp
->flags
& MPOOL_PINNED
) {
179 (void)fprintf(stderr
,
180 "mpool_get: page %d already pinned\n", bp
->pgno
);
185 * Move the page to the head of the hash chain and the tail
188 head
= &mp
->hqh
[HASHKEY(bp
->pgno
)];
189 CIRCLEQ_REMOVE(head
, bp
, hq
);
190 CIRCLEQ_INSERT_HEAD(head
, bp
, hq
);
191 CIRCLEQ_REMOVE(&mp
->lqh
, bp
, q
);
192 CIRCLEQ_INSERT_TAIL(&mp
->lqh
, bp
, q
);
194 /* Return a pinned page. */
195 bp
->flags
|= MPOOL_PINNED
;
199 /* Get a page from the cache. */
200 if ((bp
= mpool_bkt(mp
)) == NULL
)
203 /* Read in the contents. */
207 off
= mp
->pagesize
* pgno
;
208 if ((nr
= pread(mp
->fd
, bp
->page
, (size_t)mp
->pagesize
, off
)) != (int)mp
->pagesize
) {
214 /* Set the page number, pin the page. */
216 bp
->flags
= MPOOL_PINNED
;
219 * Add the page to the head of the hash chain and the tail
222 head
= &mp
->hqh
[HASHKEY(bp
->pgno
)];
223 CIRCLEQ_INSERT_HEAD(head
, bp
, hq
);
224 CIRCLEQ_INSERT_TAIL(&mp
->lqh
, bp
, q
);
226 /* Run through the user's filter. */
227 if (mp
->pgin
!= NULL
)
228 (mp
->pgin
)(mp
->pgcookie
, bp
->pgno
, bp
->page
);
239 mpool_put(MPOOL
*mp
, void *page
, u_int flags
)
246 bp
= (BKT
*)(void *)((char *)page
- sizeof(BKT
));
248 if (!(bp
->flags
& MPOOL_PINNED
)) {
249 (void)fprintf(stderr
,
250 "mpool_put: page %d not pinned\n", bp
->pgno
);
254 bp
->flags
&= ~MPOOL_PINNED
;
255 bp
->flags
|= flags
& MPOOL_DIRTY
;
256 return (RET_SUCCESS
);
261 * Close the buffer pool.
264 mpool_close(MPOOL
*mp
)
268 /* Free up any space allocated to the lru pages. */
269 while ((bp
= mp
->lqh
.cqh_first
) != (void *)&mp
->lqh
) {
270 CIRCLEQ_REMOVE(&mp
->lqh
, mp
->lqh
.cqh_first
, q
);
274 /* Free the MPOOL cookie. */
276 return (RET_SUCCESS
);
281 * Sync the pool to disk.
284 mpool_sync(MPOOL
*mp
)
288 /* Walk the lru chain, flushing any dirty pages to disk. */
289 for (bp
= mp
->lqh
.cqh_first
;
290 bp
!= (void *)&mp
->lqh
; bp
= bp
->q
.cqe_next
)
291 if (bp
->flags
& MPOOL_DIRTY
&&
292 mpool_write(mp
, bp
) == RET_ERROR
)
295 /* Sync the file descriptor. */
296 return (fsync(mp
->fd
) ? RET_ERROR
: RET_SUCCESS
);
301 * Get a page from the cache (or create one).
309 /* If under the max cached, always create a new page. */
310 if (mp
->curcache
< mp
->maxcache
)
314 * If the cache is max'd out, walk the lru list for a buffer we
315 * can flush. If we find one, write it (if necessary) and take it
316 * off any lists. If we don't find anything we grow the cache anyway.
317 * The cache never shrinks.
319 for (bp
= mp
->lqh
.cqh_first
;
320 bp
!= (void *)&mp
->lqh
; bp
= bp
->q
.cqe_next
)
321 if (!(bp
->flags
& MPOOL_PINNED
)) {
322 /* Flush if dirty. */
323 if (bp
->flags
& MPOOL_DIRTY
&&
324 mpool_write(mp
, bp
) == RET_ERROR
)
329 /* Remove from the hash and lru queues. */
330 head
= &mp
->hqh
[HASHKEY(bp
->pgno
)];
331 CIRCLEQ_REMOVE(head
, bp
, hq
);
332 CIRCLEQ_REMOVE(&mp
->lqh
, bp
, q
);
335 void *spage
= bp
->page
;
336 (void)memset(bp
, 0xff,
337 (size_t)(sizeof(BKT
) + mp
->pagesize
));
344 new: if ((bp
= calloc(1, (size_t)(sizeof(BKT
) + mp
->pagesize
))) == NULL
)
349 #if defined(DEBUG) || defined(PURIFY)
350 (void)memset(bp
, 0xff, (size_t)(sizeof(BKT
) + mp
->pagesize
));
352 bp
->page
= (char *)(void *)bp
+ sizeof(BKT
);
359 * Write a page to disk.
362 mpool_write(MPOOL
*mp
, BKT
*bp
)
370 /* Run through the user's filter. */
372 (mp
->pgout
)(mp
->pgcookie
, bp
->pgno
, bp
->page
);
374 off
= mp
->pagesize
* bp
->pgno
;
375 if (pwrite(mp
->fd
, bp
->page
, (size_t)mp
->pagesize
, off
) != (int)mp
->pagesize
)
379 * Re-run through the input filter since this page may soon be
380 * accessed via the cache, and whatever the user's output filter
381 * did may screw things up if we don't let the input filter
382 * restore the in-core copy.
385 (mp
->pgin
)(mp
->pgcookie
, bp
->pgno
, bp
->page
);
387 bp
->flags
&= ~MPOOL_DIRTY
;
388 return (RET_SUCCESS
);
393 * Lookup a page in the cache.
396 mpool_look(MPOOL
*mp
, pgno_t pgno
)
401 head
= &mp
->hqh
[HASHKEY(pgno
)];
402 for (bp
= head
->cqh_first
; bp
!= (void *)head
; bp
= bp
->hq
.cqe_next
)
403 if (bp
->pgno
== pgno
) {
418 * Print out cache statistics.
428 (void)fprintf(stderr
, "%lu pages in the file\n", (u_long
)mp
->npages
);
429 (void)fprintf(stderr
,
430 "page size %lu, cacheing %lu pages of %lu page max cache\n",
431 (u_long
)mp
->pagesize
, (u_long
)mp
->curcache
, (u_long
)mp
->maxcache
);
432 (void)fprintf(stderr
, "%lu page puts, %lu page gets, %lu page new\n",
433 mp
->pageput
, mp
->pageget
, mp
->pagenew
);
434 (void)fprintf(stderr
, "%lu page allocs, %lu page flushes\n",
435 mp
->pagealloc
, mp
->pageflush
);
436 if (mp
->cachehit
+ mp
->cachemiss
)
437 (void)fprintf(stderr
,
438 "%.0f%% cache hit rate (%lu hits, %lu misses)\n",
439 ((double)mp
->cachehit
/ (mp
->cachehit
+ mp
->cachemiss
))
440 * 100, mp
->cachehit
, mp
->cachemiss
);
441 (void)fprintf(stderr
, "%lu page reads, %lu page writes\n",
442 mp
->pageread
, mp
->pagewrite
);
446 for (bp
= mp
->lqh
.cqh_first
;
447 bp
!= (void *)&mp
->lqh
; bp
= bp
->q
.cqe_next
) {
448 (void)fprintf(stderr
, "%s%d", sep
, bp
->pgno
);
449 if (bp
->flags
& MPOOL_DIRTY
)
450 (void)fprintf(stderr
, "d");
451 if (bp
->flags
& MPOOL_PINNED
)
452 (void)fprintf(stderr
, "P");
460 (void)fprintf(stderr
, "\n");