]> git.saurik.com Git - apple/libc.git/blame - db/mpool/FreeBSD/mpool.c
Libc-594.9.5.tar.gz
[apple/libc.git] / db / mpool / FreeBSD / mpool.c
CommitLineData
9385eb3d
A
1/*-
2 * Copyright (c) 1990, 1993, 1994
e9ce8d39
A
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
20 *
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
31 * SUCH DAMAGE.
32 */
33
9385eb3d
A
34#if defined(LIBC_SCCS) && !defined(lint)
35static char sccsid[] = "@(#)mpool.c 8.5 (Berkeley) 7/26/94";
36#endif /* LIBC_SCCS and not lint */
37#include <sys/cdefs.h>
3d9156a7 38__FBSDID("$FreeBSD: src/lib/libc/db/mpool/mpool.c,v 1.12 2004/09/10 05:41:41 kuriyama Exp $");
e9ce8d39 39
59e0d9fe 40#include "namespace.h"
e9ce8d39 41#include <sys/param.h>
9385eb3d 42#include <sys/queue.h>
e9ce8d39
A
43#include <sys/stat.h>
44
45#include <errno.h>
46#include <stdio.h>
47#include <stdlib.h>
48#include <string.h>
49#include <unistd.h>
59e0d9fe 50#include "un-namespace.h"
e9ce8d39
A
51
52#include <db.h>
9385eb3d 53
e9ce8d39 54#define __MPOOLINTERFACE_PRIVATE
9385eb3d 55#include <mpool.h>
e9ce8d39 56
9385eb3d
A
57static BKT *mpool_bkt(MPOOL *);
58static BKT *mpool_look(MPOOL *, pgno_t);
59static int mpool_write(MPOOL *, BKT *);
e9ce8d39
A
60
61/*
9385eb3d
A
62 * mpool_open --
63 * Initialize a memory pool.
e9ce8d39
A
64 */
65MPOOL *
66mpool_open(key, fd, pagesize, maxcache)
9385eb3d 67 void *key;
e9ce8d39
A
68 int fd;
69 pgno_t pagesize, maxcache;
70{
71 struct stat sb;
72 MPOOL *mp;
73 int entry;
74
9385eb3d
A
75 /*
76 * Get information about the file.
77 *
78 * XXX
79 * We don't currently handle pipes, although we should.
80 */
59e0d9fe 81 if (_fstat(fd, &sb))
e9ce8d39 82 return (NULL);
e9ce8d39
A
83 if (!S_ISREG(sb.st_mode)) {
84 errno = ESPIPE;
85 return (NULL);
86 }
87
9385eb3d
A
88 /* Allocate and initialize the MPOOL cookie. */
89 if ((mp = (MPOOL *)calloc(1, sizeof(MPOOL))) == NULL)
e9ce8d39 90 return (NULL);
9385eb3d 91 TAILQ_INIT(&mp->lqh);
e9ce8d39 92 for (entry = 0; entry < HASHSIZE; ++entry)
9385eb3d 93 TAILQ_INIT(&mp->hqh[entry]);
e9ce8d39 94 mp->maxcache = maxcache;
e9ce8d39 95 mp->npages = sb.st_size / pagesize;
9385eb3d 96 mp->pagesize = pagesize;
e9ce8d39 97 mp->fd = fd;
e9ce8d39
A
98 return (mp);
99}
100
101/*
9385eb3d
A
102 * mpool_filter --
103 * Initialize input/output filters.
e9ce8d39
A
104 */
105void
106mpool_filter(mp, pgin, pgout, pgcookie)
107 MPOOL *mp;
9385eb3d
A
108 void (*pgin)(void *, pgno_t, void *);
109 void (*pgout)(void *, pgno_t, void *);
e9ce8d39
A
110 void *pgcookie;
111{
112 mp->pgin = pgin;
113 mp->pgout = pgout;
114 mp->pgcookie = pgcookie;
115}
116
117/*
9385eb3d
A
118 * mpool_new --
119 * Get a new page of memory.
e9ce8d39
A
120 */
121void *
122mpool_new(mp, pgnoaddr)
123 MPOOL *mp;
124 pgno_t *pgnoaddr;
125{
9385eb3d
A
126 struct _hqh *head;
127 BKT *bp;
e9ce8d39 128
9385eb3d
A
129 if (mp->npages == MAX_PAGE_NUMBER) {
130 (void)fprintf(stderr, "mpool_new: page allocation overflow.\n");
131 abort();
132 }
e9ce8d39
A
133#ifdef STATISTICS
134 ++mp->pagenew;
135#endif
136 /*
9385eb3d
A
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,
139 * and return.
e9ce8d39 140 */
9385eb3d 141 if ((bp = mpool_bkt(mp)) == NULL)
e9ce8d39 142 return (NULL);
9385eb3d
A
143 *pgnoaddr = bp->pgno = mp->npages++;
144 bp->flags = MPOOL_PINNED;
145
146 head = &mp->hqh[HASHKEY(bp->pgno)];
147 TAILQ_INSERT_HEAD(head, bp, hq);
148 TAILQ_INSERT_TAIL(&mp->lqh, bp, q);
149 return (bp->page);
e9ce8d39
A
150}
151
152/*
9385eb3d
A
153 * mpool_get
154 * Get a page.
e9ce8d39
A
155 */
156void *
157mpool_get(mp, pgno, flags)
158 MPOOL *mp;
159 pgno_t pgno;
9385eb3d 160 u_int flags; /* XXX not used? */
e9ce8d39 161{
9385eb3d
A
162 struct _hqh *head;
163 BKT *bp;
e9ce8d39
A
164 off_t off;
165 int nr;
166
9385eb3d
A
167 /* Check for attempt to retrieve a non-existent page. */
168 if (pgno >= mp->npages) {
169 errno = EINVAL;
170 return (NULL);
171 }
172
e9ce8d39 173#ifdef STATISTICS
9385eb3d 174 ++mp->pageget;
e9ce8d39 175#endif
9385eb3d
A
176
177 /* Check for a page that is cached. */
178 if ((bp = mpool_look(mp, pgno)) != NULL) {
e9ce8d39 179#ifdef DEBUG
9385eb3d
A
180 if (bp->flags & MPOOL_PINNED) {
181 (void)fprintf(stderr,
182 "mpool_get: page %d already pinned\n", bp->pgno);
183 abort();
184 }
e9ce8d39 185#endif
9385eb3d
A
186 /*
187 * Move the page to the head of the hash chain and the tail
188 * of the lru chain.
189 */
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);
195
196 /* Return a pinned page. */
197 bp->flags |= MPOOL_PINNED;
198 return (bp->page);
e9ce8d39
A
199 }
200
201 /* Get a page from the cache. */
9385eb3d 202 if ((bp = mpool_bkt(mp)) == NULL)
e9ce8d39 203 return (NULL);
e9ce8d39 204
9385eb3d 205 /* Read in the contents. */
e9ce8d39
A
206#ifdef STATISTICS
207 ++mp->pageread;
208#endif
e9ce8d39 209 off = mp->pagesize * pgno;
3d9156a7
A
210 nr = pread(mp->fd, bp->page, mp->pagesize, off);
211 if (nr != mp->pagesize) {
e9ce8d39
A
212 if (nr >= 0)
213 errno = EFTYPE;
214 return (NULL);
215 }
e9ce8d39 216
9385eb3d
A
217 /* Set the page number, pin the page. */
218 bp->pgno = pgno;
219 bp->flags = MPOOL_PINNED;
220
221 /*
222 * Add the page to the head of the hash chain and the tail
223 * of the lru chain.
224 */
225 head = &mp->hqh[HASHKEY(bp->pgno)];
226 TAILQ_INSERT_HEAD(head, bp, hq);
227 TAILQ_INSERT_TAIL(&mp->lqh, bp, q);
228
229 /* Run through the user's filter. */
230 if (mp->pgin != NULL)
231 (mp->pgin)(mp->pgcookie, bp->pgno, bp->page);
232
233 return (bp->page);
e9ce8d39
A
234}
235
236/*
9385eb3d
A
237 * mpool_put
238 * Return a page.
e9ce8d39
A
239 */
240int
241mpool_put(mp, page, flags)
242 MPOOL *mp;
243 void *page;
244 u_int flags;
245{
9385eb3d 246 BKT *bp;
e9ce8d39
A
247
248#ifdef STATISTICS
249 ++mp->pageput;
250#endif
9385eb3d 251 bp = (BKT *)((char *)page - sizeof(BKT));
e9ce8d39 252#ifdef DEBUG
9385eb3d
A
253 if (!(bp->flags & MPOOL_PINNED)) {
254 (void)fprintf(stderr,
255 "mpool_put: page %d not pinned\n", bp->pgno);
256 abort();
e9ce8d39
A
257 }
258#endif
9385eb3d
A
259 bp->flags &= ~MPOOL_PINNED;
260 bp->flags |= flags & MPOOL_DIRTY;
e9ce8d39
A
261 return (RET_SUCCESS);
262}
263
264/*
9385eb3d
A
265 * mpool_close
266 * Close the buffer pool.
e9ce8d39
A
267 */
268int
269mpool_close(mp)
270 MPOOL *mp;
271{
9385eb3d 272 BKT *bp;
e9ce8d39
A
273
274 /* Free up any space allocated to the lru pages. */
9385eb3d
A
275 while (!TAILQ_EMPTY(&mp->lqh)) {
276 bp = TAILQ_FIRST(&mp->lqh);
277 TAILQ_REMOVE(&mp->lqh, bp, q);
278 free(bp);
e9ce8d39 279 }
9385eb3d
A
280
281 /* Free the MPOOL cookie. */
e9ce8d39
A
282 free(mp);
283 return (RET_SUCCESS);
284}
285
286/*
9385eb3d
A
287 * mpool_sync
288 * Sync the pool to disk.
e9ce8d39
A
289 */
290int
291mpool_sync(mp)
292 MPOOL *mp;
293{
9385eb3d 294 BKT *bp;
e9ce8d39 295
9385eb3d
A
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 mpool_write(mp, bp) == RET_ERROR)
e9ce8d39 300 return (RET_ERROR);
9385eb3d
A
301
302 /* Sync the file descriptor. */
59e0d9fe 303 return (_fsync(mp->fd) ? RET_ERROR : RET_SUCCESS);
e9ce8d39
A
304}
305
306/*
9385eb3d
A
307 * mpool_bkt
308 * Get a page from the cache (or create one).
e9ce8d39
A
309 */
310static BKT *
311mpool_bkt(mp)
312 MPOOL *mp;
313{
9385eb3d
A
314 struct _hqh *head;
315 BKT *bp;
e9ce8d39 316
9385eb3d 317 /* If under the max cached, always create a new page. */
e9ce8d39
A
318 if (mp->curcache < mp->maxcache)
319 goto new;
320
321 /*
9385eb3d
A
322 * If the cache is max'd out, walk the lru list for a buffer we
323 * can flush. If we find one, write it (if necessary) and take it
324 * off any lists. If we don't find anything we grow the cache anyway.
e9ce8d39
A
325 * The cache never shrinks.
326 */
9385eb3d
A
327 TAILQ_FOREACH(bp, &mp->lqh, q)
328 if (!(bp->flags & MPOOL_PINNED)) {
329 /* Flush if dirty. */
330 if (bp->flags & MPOOL_DIRTY &&
331 mpool_write(mp, bp) == RET_ERROR)
e9ce8d39 332 return (NULL);
e9ce8d39
A
333#ifdef STATISTICS
334 ++mp->pageflush;
335#endif
9385eb3d
A
336 /* Remove from the hash and lru queues. */
337 head = &mp->hqh[HASHKEY(bp->pgno)];
338 TAILQ_REMOVE(head, bp, hq);
339 TAILQ_REMOVE(&mp->lqh, bp, q);
e9ce8d39 340#ifdef DEBUG
9385eb3d
A
341 { void *spage;
342 spage = bp->page;
343 memset(bp, 0xff, sizeof(BKT) + mp->pagesize);
344 bp->page = spage;
e9ce8d39
A
345 }
346#endif
9385eb3d 347 return (bp);
e9ce8d39
A
348 }
349
9385eb3d 350new: if ((bp = (BKT *)malloc(sizeof(BKT) + mp->pagesize)) == NULL)
e9ce8d39
A
351 return (NULL);
352#ifdef STATISTICS
353 ++mp->pagealloc;
354#endif
9385eb3d
A
355#if defined(DEBUG) || defined(PURIFY)
356 memset(bp, 0xff, sizeof(BKT) + mp->pagesize);
e9ce8d39 357#endif
9385eb3d 358 bp->page = (char *)bp + sizeof(BKT);
e9ce8d39 359 ++mp->curcache;
9385eb3d 360 return (bp);
e9ce8d39
A
361}
362
363/*
9385eb3d
A
364 * mpool_write
365 * Write a page to disk.
e9ce8d39
A
366 */
367static int
9385eb3d 368mpool_write(mp, bp)
e9ce8d39 369 MPOOL *mp;
9385eb3d 370 BKT *bp;
e9ce8d39
A
371{
372 off_t off;
373
e9ce8d39
A
374#ifdef STATISTICS
375 ++mp->pagewrite;
376#endif
9385eb3d
A
377
378 /* Run through the user's filter. */
379 if (mp->pgout)
380 (mp->pgout)(mp->pgcookie, bp->pgno, bp->page);
381
382 off = mp->pagesize * bp->pgno;
3d9156a7 383 if (pwrite(mp->fd, bp->page, mp->pagesize, off) != mp->pagesize)
e9ce8d39 384 return (RET_ERROR);
9385eb3d
A
385
386 bp->flags &= ~MPOOL_DIRTY;
e9ce8d39
A
387 return (RET_SUCCESS);
388}
389
390/*
9385eb3d
A
391 * mpool_look
392 * Lookup a page in the cache.
e9ce8d39
A
393 */
394static BKT *
395mpool_look(mp, pgno)
396 MPOOL *mp;
397 pgno_t pgno;
398{
9385eb3d
A
399 struct _hqh *head;
400 BKT *bp;
e9ce8d39 401
9385eb3d
A
402 head = &mp->hqh[HASHKEY(pgno)];
403 TAILQ_FOREACH(bp, head, hq)
404 if (bp->pgno == pgno) {
e9ce8d39
A
405#ifdef STATISTICS
406 ++mp->cachehit;
407#endif
9385eb3d 408 return (bp);
e9ce8d39
A
409 }
410#ifdef STATISTICS
411 ++mp->cachemiss;
412#endif
413 return (NULL);
414}
415
416#ifdef STATISTICS
417/*
9385eb3d
A
418 * mpool_stat
419 * Print out cache statistics.
e9ce8d39
A
420 */
421void
422mpool_stat(mp)
423 MPOOL *mp;
424{
9385eb3d 425 BKT *bp;
e9ce8d39
A
426 int cnt;
427 char *sep;
428
3d9156a7 429 (void)fprintf(stderr, "%u pages in the file\n", mp->npages);
e9ce8d39 430 (void)fprintf(stderr,
3d9156a7 431 "page size %lu, cacheing %u pages of %u page max cache\n",
e9ce8d39
A
432 mp->pagesize, mp->curcache, mp->maxcache);
433 (void)fprintf(stderr, "%lu page puts, %lu page gets, %lu page new\n",
434 mp->pageput, mp->pageget, mp->pagenew);
435 (void)fprintf(stderr, "%lu page allocs, %lu page flushes\n",
436 mp->pagealloc, mp->pageflush);
437 if (mp->cachehit + mp->cachemiss)
438 (void)fprintf(stderr,
439 "%.0f%% cache hit rate (%lu hits, %lu misses)\n",
440 ((double)mp->cachehit / (mp->cachehit + mp->cachemiss))
441 * 100, mp->cachehit, mp->cachemiss);
442 (void)fprintf(stderr, "%lu page reads, %lu page writes\n",
443 mp->pageread, mp->pagewrite);
444
445 sep = "";
446 cnt = 0;
9385eb3d
A
447 TAILQ_FOREACH(bp, &mp->lqh, q) {
448 (void)fprintf(stderr, "%s%d", sep, bp->pgno);
449 if (bp->flags & MPOOL_DIRTY)
e9ce8d39 450 (void)fprintf(stderr, "d");
9385eb3d 451 if (bp->flags & MPOOL_PINNED)
e9ce8d39
A
452 (void)fprintf(stderr, "P");
453 if (++cnt == 10) {
454 sep = "\n";
455 cnt = 0;
456 } else
457 sep = ", ";
458
459 }
460 (void)fprintf(stderr, "\n");
461}
462#endif