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 */
38 #include <sys/param.h>
39 #include <sys/queue.h>
50 #define __MPOOLINTERFACE_PRIVATE
54 /* In the GNU C library we must not pollute the namespace because libdb is
55 needed by libnss_db. */
56 #define mpool_open __mpool_open
57 #define mpool_filter __mpool_filter
58 #define mpool_new __mpool_new
59 #define mpool_get __mpool_get
60 #define mpool_put __mpool_put
61 #define mpool_sync __mpool_sync
62 #define mpool_close __mpool_close
65 static BKT
*mpool_bkt
__P((MPOOL
*));
66 static BKT
*mpool_look
__P((MPOOL
*, pgno_t
));
67 static int mpool_write
__P((MPOOL
*, BKT
*));
71 * Initialize a memory pool.
74 mpool_open(key
, fd
, pagesize
, maxcache
)
77 pgno_t pagesize
, maxcache
;
84 * Get information about the file.
87 * We don't currently handle pipes, although we should.
91 if (!S_ISREG(sb
.st_mode
)) {
96 /* Allocate and initialize the MPOOL cookie. */
97 if ((mp
= (MPOOL
*)calloc(1, sizeof(MPOOL
))) == NULL
)
99 CIRCLEQ_INIT(&mp
->lqh
);
100 for (entry
= 0; entry
< HASHSIZE
; ++entry
)
101 CIRCLEQ_INIT(&mp
->hqh
[entry
]);
102 mp
->maxcache
= maxcache
;
103 mp
->npages
= sb
.st_size
/ pagesize
;
104 mp
->pagesize
= pagesize
;
111 * Initialize input/output filters.
114 mpool_filter(mp
, pgin
, pgout
, pgcookie
)
116 void (*pgin
) __P((void *, pgno_t
, void *));
117 void (*pgout
) __P((void *, pgno_t
, void *));
122 mp
->pgcookie
= pgcookie
;
127 * Get a new page of memory.
130 mpool_new(mp
, pgnoaddr
)
137 if (mp
->npages
== MAX_PAGE_NUMBER
) {
138 (void)fprintf(stderr
, "mpool_new: page allocation overflow.\n");
145 * Get a BKT from the cache. Assign a new page number, attach
146 * it to the head of the hash chain, the tail of the lru chain,
149 if ((bp
= mpool_bkt(mp
)) == NULL
)
151 *pgnoaddr
= bp
->pgno
= mp
->npages
++;
152 bp
->flags
= MPOOL_PINNED
;
154 head
= &mp
->hqh
[HASHKEY(bp
->pgno
)];
155 CIRCLEQ_INSERT_HEAD(head
, bp
, hq
);
156 CIRCLEQ_INSERT_TAIL(&mp
->lqh
, bp
, q
);
165 mpool_get(mp
, pgno
, flags
)
168 u_int flags
; /* XXX not used? */
175 /* Check for attempt to retrieve a non-existent page. */
176 if (pgno
>= mp
->npages
) {
185 /* Check for a page that is cached. */
186 if ((bp
= mpool_look(mp
, pgno
)) != NULL
) {
188 if (bp
->flags
& MPOOL_PINNED
) {
189 (void)fprintf(stderr
,
190 "mpool_get: page %d already pinned\n", bp
->pgno
);
195 * Move the page to the head of the hash chain and the tail
198 head
= &mp
->hqh
[HASHKEY(bp
->pgno
)];
199 CIRCLEQ_REMOVE(head
, bp
, hq
);
200 CIRCLEQ_INSERT_HEAD(head
, bp
, hq
);
201 CIRCLEQ_REMOVE(&mp
->lqh
, bp
, q
);
202 CIRCLEQ_INSERT_TAIL(&mp
->lqh
, bp
, q
);
204 /* Return a pinned page. */
205 bp
->flags
|= MPOOL_PINNED
;
209 /* Get a page from the cache. */
210 if ((bp
= mpool_bkt(mp
)) == NULL
)
213 /* Read in the contents. */
217 off
= mp
->pagesize
* pgno
;
218 if (lseek(mp
->fd
, off
, SEEK_SET
) != off
)
220 if ((u_long
) (nr
= read(mp
->fd
, bp
->page
, mp
->pagesize
))
227 /* Set the page number, pin the page. */
229 bp
->flags
= MPOOL_PINNED
;
232 * Add the page to the head of the hash chain and the tail
235 head
= &mp
->hqh
[HASHKEY(bp
->pgno
)];
236 CIRCLEQ_INSERT_HEAD(head
, bp
, hq
);
237 CIRCLEQ_INSERT_TAIL(&mp
->lqh
, bp
, q
);
239 /* Run through the user's filter. */
240 if (mp
->pgin
!= NULL
)
241 (mp
->pgin
)(mp
->pgcookie
, bp
->pgno
, bp
->page
);
251 mpool_put(mp
, page
, flags
)
261 bp
= (BKT
*)((char *)page
- sizeof(BKT
));
263 if (!(bp
->flags
& MPOOL_PINNED
)) {
264 (void)fprintf(stderr
,
265 "mpool_put: page %d not pinned\n", bp
->pgno
);
269 bp
->flags
&= ~MPOOL_PINNED
;
270 bp
->flags
|= flags
& MPOOL_DIRTY
;
271 return (RET_SUCCESS
);
276 * Close the buffer pool.
284 /* Free up any space allocated to the lru pages. */
285 while ((bp
= mp
->lqh
.cqh_first
) != (void *)&mp
->lqh
) {
286 CIRCLEQ_REMOVE(&mp
->lqh
, mp
->lqh
.cqh_first
, q
);
290 /* Free the MPOOL cookie. */
292 return (RET_SUCCESS
);
297 * Sync the pool to disk.
305 /* Walk the lru chain, flushing any dirty pages to disk. */
306 for (bp
= mp
->lqh
.cqh_first
;
307 bp
!= (void *)&mp
->lqh
; bp
= bp
->q
.cqe_next
)
308 if (bp
->flags
& MPOOL_DIRTY
&&
309 mpool_write(mp
, bp
) == RET_ERROR
)
312 /* Sync the file descriptor. */
313 return (fsync(mp
->fd
) ? RET_ERROR
: RET_SUCCESS
);
324 weak_alias (__mpool_open
, mpool_open
)
325 weak_alias (__mpool_filter
, mpool_filter
)
326 weak_alias (__mpool_new
, mpool_new
)
327 weak_alias (__mpool_get
, mpool_get
)
328 weak_alias (__mpool_put
, mpool_put
)
329 weak_alias (__mpool_close
, mpool_close
)
330 weak_alias (__mpool_sync
, mpool_sync
)
335 * Get a page from the cache (or create one).
344 /* If under the max cached, always create a new page. */
345 if (mp
->curcache
< mp
->maxcache
)
349 * If the cache is max'd out, walk the lru list for a buffer we
350 * can flush. If we find one, write it (if necessary) and take it
351 * off any lists. If we don't find anything we grow the cache anyway.
352 * The cache never shrinks.
354 for (bp
= mp
->lqh
.cqh_first
;
355 bp
!= (void *)&mp
->lqh
; bp
= bp
->q
.cqe_next
)
356 if (!(bp
->flags
& MPOOL_PINNED
)) {
357 /* Flush if dirty. */
358 if (bp
->flags
& MPOOL_DIRTY
&&
359 mpool_write(mp
, bp
) == RET_ERROR
)
364 /* Remove from the hash and lru queues. */
365 head
= &mp
->hqh
[HASHKEY(bp
->pgno
)];
366 CIRCLEQ_REMOVE(head
, bp
, hq
);
367 CIRCLEQ_REMOVE(&mp
->lqh
, bp
, q
);
371 memset(bp
, 0xff, sizeof(BKT
) + mp
->pagesize
);
378 new: if ((bp
= (BKT
*)malloc(sizeof(BKT
) + mp
->pagesize
)) == NULL
)
383 #if defined(DEBUG) || defined(PURIFY)
384 memset(bp
, 0xff, sizeof(BKT
) + mp
->pagesize
);
386 bp
->page
= (char *)bp
+ sizeof(BKT
);
393 * Write a page to disk.
406 /* Run through the user's filter. */
408 (mp
->pgout
)(mp
->pgcookie
, bp
->pgno
, bp
->page
);
410 off
= mp
->pagesize
* bp
->pgno
;
411 if (lseek(mp
->fd
, off
, SEEK_SET
) != off
)
413 if ((u_long
) write(mp
->fd
, bp
->page
, mp
->pagesize
) != mp
->pagesize
)
416 bp
->flags
&= ~MPOOL_DIRTY
;
417 return (RET_SUCCESS
);
422 * Lookup a page in the cache.
432 head
= &mp
->hqh
[HASHKEY(pgno
)];
433 for (bp
= head
->cqh_first
; bp
!= (void *)head
; bp
= bp
->hq
.cqe_next
)
434 if (bp
->pgno
== pgno
) {
449 * Print out cache statistics.
459 (void)fprintf(stderr
, "%lu pages in the file\n", mp
->npages
);
460 (void)fprintf(stderr
,
461 "page size %lu, cacheing %lu pages of %lu page max cache\n",
462 mp
->pagesize
, mp
->curcache
, mp
->maxcache
);
463 (void)fprintf(stderr
, "%lu page puts, %lu page gets, %lu page new\n",
464 mp
->pageput
, mp
->pageget
, mp
->pagenew
);
465 (void)fprintf(stderr
, "%lu page allocs, %lu page flushes\n",
466 mp
->pagealloc
, mp
->pageflush
);
467 if (mp
->cachehit
+ mp
->cachemiss
)
468 (void)fprintf(stderr
,
469 "%.0f%% cache hit rate (%lu hits, %lu misses)\n",
470 ((double)mp
->cachehit
/ (mp
->cachehit
+ mp
->cachemiss
))
471 * 100, mp
->cachehit
, mp
->cachemiss
);
472 (void)fprintf(stderr
, "%lu page reads, %lu page writes\n",
473 mp
->pageread
, mp
->pagewrite
);
477 for (bp
= mp
->lqh
.cqh_first
;
478 bp
!= (void *)&mp
->lqh
; bp
= bp
->q
.cqe_next
) {
479 (void)fprintf(stderr
, "%s%d", sep
, bp
->pgno
);
480 if (bp
->flags
& MPOOL_DIRTY
)
481 (void)fprintf(stderr
, "d");
482 if (bp
->flags
& MPOOL_PINNED
)
483 (void)fprintf(stderr
, "P");
491 (void)fprintf(stderr
, "\n");