]>
git.saurik.com Git - apple/libc.git/blob - db/mpool/mpool.c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * Copyright (c) 1990, 1993
27 * The Regents of the University of California. All rights reserved.
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
32 * 1. Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in the
36 * documentation and/or other materials provided with the distribution.
37 * 3. All advertising materials mentioning features or use of this software
38 * must display the following acknowledgement:
39 * This product includes software developed by the University of
40 * California, Berkeley and its contributors.
41 * 4. Neither the name of the University nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
45 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 #include <sys/param.h>
69 #define __MPOOLINTERFACE_PRIVATE
72 static BKT
*mpool_bkt
__P((MPOOL
*));
73 static BKT
*mpool_look
__P((MPOOL
*, pgno_t
));
74 static int mpool_write
__P((MPOOL
*, BKT
*));
76 static void __mpoolerr
__P((const char *fmt
, ...));
80 * MPOOL_OPEN -- initialize a memory pool.
83 * key: Shared buffer key.
84 * fd: File descriptor.
85 * pagesize: File page size.
86 * maxcache: Max number of cached pages.
89 * MPOOL pointer, NULL on error.
92 mpool_open(key
, fd
, pagesize
, maxcache
)
95 pgno_t pagesize
, maxcache
;
104 * We should only set st_size to 0 for pipes -- 4.4BSD has the fix so
105 * that stat(2) returns true for ISSOCK on pipes. Until then, this is
108 if (!S_ISREG(sb
.st_mode
)) {
113 if ((mp
= (MPOOL
*)malloc(sizeof(MPOOL
))) == NULL
)
115 mp
->free
.cnext
= mp
->free
.cprev
= (BKT
*)&mp
->free
;
116 mp
->lru
.cnext
= mp
->lru
.cprev
= (BKT
*)&mp
->lru
;
117 for (entry
= 0; entry
< HASHSIZE
; ++entry
)
118 mp
->hashtable
[entry
].hnext
= mp
->hashtable
[entry
].hprev
=
119 mp
->hashtable
[entry
].cnext
= mp
->hashtable
[entry
].cprev
=
120 (BKT
*)&mp
->hashtable
[entry
];
122 mp
->maxcache
= maxcache
;
123 mp
->pagesize
= pagesize
;
124 mp
->npages
= sb
.st_size
/ pagesize
;
127 mp
->pgin
= mp
->pgout
= NULL
;
130 mp
->cachehit
= mp
->cachemiss
= mp
->pagealloc
= mp
->pageflush
=
131 mp
->pageget
= mp
->pagenew
= mp
->pageput
= mp
->pageread
=
138 * MPOOL_FILTER -- initialize input/output filters.
141 * pgin: Page in conversion routine.
142 * pgout: Page out conversion routine.
143 * pgcookie: Cookie for page in/out routines.
146 mpool_filter(mp
, pgin
, pgout
, pgcookie
)
148 void (*pgin
) __P((void *, pgno_t
, void *));
149 void (*pgout
) __P((void *, pgno_t
, void *));
154 mp
->pgcookie
= pgcookie
;
158 * MPOOL_NEW -- get a new page
162 * pgnoadddr: place to store new page number
164 * RET_ERROR, RET_SUCCESS
167 mpool_new(mp
, pgnoaddr
)
178 * Get a BKT from the cache. Assign a new page number, attach it to
179 * the hash and lru chains and return.
181 if ((b
= mpool_bkt(mp
)) == NULL
)
183 *pgnoaddr
= b
->pgno
= mp
->npages
++;
184 b
->flags
= MPOOL_PINNED
;
186 inschain(b
, &mp
->lru
);
191 * MPOOL_GET -- get a page from the pool
199 * RET_ERROR, RET_SUCCESS
202 mpool_get(mp
, pgno
, flags
)
205 u_int flags
; /* XXX not used? */
213 * If asking for a specific page that is already in the cache, find
216 if (b
= mpool_look(mp
, pgno
)) {
221 if (b
->flags
& MPOOL_PINNED
)
222 __mpoolerr("mpool_get: page %d already pinned",
226 inschain(b
, &mp
->lru
);
227 b
->flags
|= MPOOL_PINNED
;
231 /* Not allowed to retrieve a non-existent page. */
232 if (pgno
>= mp
->npages
) {
237 /* Get a page from the cache. */
238 if ((b
= mpool_bkt(mp
)) == NULL
)
241 b
->flags
= MPOOL_PINNED
;
246 /* Read in the contents. */
247 off
= mp
->pagesize
* pgno
;
248 if (lseek(mp
->fd
, off
, SEEK_SET
) != off
)
250 if ((nr
= read(mp
->fd
, b
->page
, mp
->pagesize
)) != mp
->pagesize
) {
256 (mp
->pgin
)(mp
->pgcookie
, b
->pgno
, b
->page
);
259 inschain(b
, &mp
->lru
);
267 * MPOOL_PUT -- return a page to the pool
275 * RET_ERROR, RET_SUCCESS
278 mpool_put(mp
, page
, flags
)
291 baddr
= (BKT
*)((char *)page
- sizeof(BKT
));
293 if (!(baddr
->flags
& MPOOL_PINNED
))
294 __mpoolerr("mpool_put: page %d not pinned", b
->pgno
);
295 for (b
= mp
->lru
.cnext
; b
!= (BKT
*)&mp
->lru
; b
= b
->cnext
) {
296 if (b
== (BKT
*)&mp
->lru
)
297 __mpoolerr("mpool_put: %0x: bad address", baddr
);
302 baddr
->flags
&= ~MPOOL_PINNED
;
303 baddr
->flags
|= flags
& MPOOL_DIRTY
;
304 return (RET_SUCCESS
);
308 * MPOOL_CLOSE -- close the buffer pool
314 * RET_ERROR, RET_SUCCESS
322 /* Free up any space allocated to the lru pages. */
323 for (b
= mp
->lru
.cprev
; b
!= (BKT
*)&mp
->lru
; b
= next
) {
328 return (RET_SUCCESS
);
332 * MPOOL_SYNC -- sync the file to disk.
338 * RET_ERROR, RET_SUCCESS
346 for (b
= mp
->lru
.cprev
; b
!= (BKT
*)&mp
->lru
; b
= b
->cprev
)
347 if (b
->flags
& MPOOL_DIRTY
&& mpool_write(mp
, b
) == RET_ERROR
)
349 return (fsync(mp
->fd
) ? RET_ERROR
: RET_SUCCESS
);
353 * MPOOL_BKT -- get/create a BKT from the cache
359 * NULL on failure and a pointer to the BKT on success
367 if (mp
->curcache
< mp
->maxcache
)
371 * If the cache is maxxed out, search the lru list for a buffer we
372 * can flush. If we find one, write it if necessary and take it off
373 * any lists. If we don't find anything we grow the cache anyway.
374 * The cache never shrinks.
376 for (b
= mp
->lru
.cprev
; b
!= (BKT
*)&mp
->lru
; b
= b
->cprev
)
377 if (!(b
->flags
& MPOOL_PINNED
)) {
378 if (b
->flags
& MPOOL_DIRTY
&&
379 mpool_write(mp
, b
) == RET_ERROR
)
390 memset(b
, 0xff, sizeof(BKT
) + mp
->pagesize
);
397 new: if ((b
= (BKT
*)malloc(sizeof(BKT
) + mp
->pagesize
)) == NULL
)
403 memset(b
, 0xff, sizeof(BKT
) + mp
->pagesize
);
405 b
->page
= (char *)b
+ sizeof(BKT
);
411 * MPOOL_WRITE -- sync a page to disk
417 * RET_ERROR, RET_SUCCESS
427 (mp
->pgout
)(mp
->pgcookie
, b
->pgno
, b
->page
);
432 off
= mp
->pagesize
* b
->pgno
;
433 if (lseek(mp
->fd
, off
, SEEK_SET
) != off
)
435 if (write(mp
->fd
, b
->page
, mp
->pagesize
) != mp
->pagesize
)
437 b
->flags
&= ~MPOOL_DIRTY
;
438 return (RET_SUCCESS
);
442 * MPOOL_LOOK -- lookup a page
449 * NULL on failure and a pointer to the BKT on success
460 * If find the buffer, put it first on the hash chain so can
461 * find it again quickly.
463 tb
= &mp
->hashtable
[HASHKEY(pgno
)];
464 for (b
= tb
->hnext
; b
!= (BKT
*)tb
; b
= b
->hnext
)
465 if (b
->pgno
== pgno
) {
479 * MPOOL_STAT -- cache statistics
492 (void)fprintf(stderr
, "%lu pages in the file\n", mp
->npages
);
493 (void)fprintf(stderr
,
494 "page size %lu, cacheing %lu pages of %lu page max cache\n",
495 mp
->pagesize
, mp
->curcache
, mp
->maxcache
);
496 (void)fprintf(stderr
, "%lu page puts, %lu page gets, %lu page new\n",
497 mp
->pageput
, mp
->pageget
, mp
->pagenew
);
498 (void)fprintf(stderr
, "%lu page allocs, %lu page flushes\n",
499 mp
->pagealloc
, mp
->pageflush
);
500 if (mp
->cachehit
+ mp
->cachemiss
)
501 (void)fprintf(stderr
,
502 "%.0f%% cache hit rate (%lu hits, %lu misses)\n",
503 ((double)mp
->cachehit
/ (mp
->cachehit
+ mp
->cachemiss
))
504 * 100, mp
->cachehit
, mp
->cachemiss
);
505 (void)fprintf(stderr
, "%lu page reads, %lu page writes\n",
506 mp
->pageread
, mp
->pagewrite
);
510 for (b
= mp
->lru
.cnext
; b
!= (BKT
*)&mp
->lru
; b
= b
->cnext
) {
511 (void)fprintf(stderr
, "%s%d", sep
, b
->pgno
);
512 if (b
->flags
& MPOOL_DIRTY
)
513 (void)fprintf(stderr
, "d");
514 if (b
->flags
& MPOOL_PINNED
)
515 (void)fprintf(stderr
, "P");
523 (void)fprintf(stderr
, "\n");
536 __mpoolerr(const char *fmt
, ...)
538 __mpoolerr(fmt
, va_alist
)
549 (void)vfprintf(stderr
, fmt
, ap
);
551 (void)fprintf(stderr
, "\n");