Expand PMF_FN_* macros.
[netbsd-mini2440.git] / lib / libc / DB / mpool / mpool.c
blob071f6500460942504bbe1af1a1f31004ff7701f7
1 /*-
2 * Copyright (c) 1990 The Regents of the University of California.
3 * All rights reserved.
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.
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.
34 #if defined(LIBC_SCCS) && !defined(lint)
35 static char sccsid[] = "@(#)mpool.c 5.5 (Berkeley) 2/19/93";
36 #endif /* LIBC_SCCS and not lint */
38 #include <sys/param.h>
39 #include <sys/stat.h>
41 #include <errno.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <unistd.h>
47 #include <db.h>
48 #define __MPOOLINTERFACE_PRIVATE
49 #include "mpool.h"
51 static BKT *mpool_bkt __P((MPOOL *));
52 static BKT *mpool_look __P((MPOOL *, pgno_t));
53 static int mpool_write __P((MPOOL *, BKT *));
54 #ifdef DEBUG
55 static void err __P((const char *fmt, ...));
56 #endif
59 * MPOOL_OPEN -- initialize a memory pool.
61 * Parameters:
62 * key: Shared buffer key.
63 * fd: File descriptor.
64 * pagesize: File page size.
65 * maxcache: Max number of cached pages.
67 * Returns:
68 * MPOOL pointer, NULL on error.
70 MPOOL *
71 mpool_open(key, fd, pagesize, maxcache)
72 DBT *key;
73 int fd;
74 pgno_t pagesize, maxcache;
76 struct stat sb;
77 MPOOL *mp;
78 int entry;
80 if (fstat(fd, &sb))
81 return (NULL);
82 /* XXX
83 * We should only set st_size to 0 for pipes -- 4.4BSD has the fix so
84 * that stat(2) returns true for ISSOCK on pipes. Until then, this is
85 * fairly close.
87 if (!S_ISREG(sb.st_mode)) {
88 errno = ESPIPE;
89 return (NULL);
92 if ((mp = malloc(sizeof(MPOOL))) == NULL)
93 return (NULL);
94 mp->free.cnext = mp->free.cprev = (BKT *)&mp->free;
95 mp->lru.cnext = mp->lru.cprev = (BKT *)&mp->lru;
96 for (entry = 0; entry < HASHSIZE; ++entry)
97 mp->hashtable[entry].hnext = mp->hashtable[entry].hprev =
98 mp->hashtable[entry].cnext = mp->hashtable[entry].cprev =
99 (BKT *)&mp->hashtable[entry];
100 mp->curcache = 0;
101 mp->maxcache = maxcache;
102 mp->pagesize = pagesize;
103 mp->npages = sb.st_size / pagesize;
104 mp->fd = fd;
105 mp->pgcookie = NULL;
106 mp->pgin = mp->pgout = NULL;
108 #ifdef STATISTICS
109 mp->cachehit = mp->cachemiss = mp->pagealloc = mp->pageflush =
110 mp->pageget = mp->pagenew = mp->pageput = mp->pageread =
111 mp->pagewrite = 0;
112 #endif
113 return (mp);
117 * MPOOL_FILTER -- initialize input/output filters.
119 * Parameters:
120 * pgin: Page in conversion routine.
121 * pgout: Page out conversion routine.
122 * pgcookie: Cookie for page in/out routines.
124 void
125 mpool_filter(mp, pgin, pgout, pgcookie)
126 MPOOL *mp;
127 void (*pgin) __P((void *, pgno_t, void *));
128 void (*pgout) __P((void *, pgno_t, void *));
129 void *pgcookie;
131 mp->pgin = pgin;
132 mp->pgout = pgout;
133 mp->pgcookie = pgcookie;
137 * MPOOL_NEW -- get a new page
139 * Parameters:
140 * mp: mpool cookie
141 * pgnoadddr: place to store new page number
142 * Returns:
143 * RET_ERROR, RET_SUCCESS
145 void *
146 mpool_new(mp, pgnoaddr)
147 MPOOL *mp;
148 pgno_t *pgnoaddr;
150 BKT *b;
151 BKTHDR *hp;
153 #ifdef STATISTICS
154 ++mp->pagenew;
155 #endif
157 * Get a BKT from the cache. Assign a new page number, attach it to
158 * the hash and lru chains and return.
160 if ((b = mpool_bkt(mp)) == NULL)
161 return (NULL);
162 *pgnoaddr = b->pgno = mp->npages++;
163 b->flags = MPOOL_PINNED;
164 inshash(b, b->pgno);
165 inschain(b, &mp->lru);
166 return (b->page);
170 * MPOOL_GET -- get a page from the pool
172 * Parameters:
173 * mp: mpool cookie
174 * pgno: page number
175 * flags: not used
177 * Returns:
178 * RET_ERROR, RET_SUCCESS
180 void *
181 mpool_get(mp, pgno, flags)
182 MPOOL *mp;
183 pgno_t pgno;
184 u_int flags; /* XXX not used? */
186 BKT *b;
187 BKTHDR *hp;
188 off_t off;
189 int nr;
192 * If asking for a specific page that is already in the cache, find
193 * it and return it.
195 if (b = mpool_look(mp, pgno)) {
196 #ifdef STATISTICS
197 ++mp->pageget;
198 #endif
199 #ifdef DEBUG
200 if (b->flags & MPOOL_PINNED)
201 err("mpool_get: page %d already pinned", b->pgno);
202 #endif
203 rmchain(b);
204 inschain(b, &mp->lru);
205 b->flags |= MPOOL_PINNED;
206 return (b->page);
209 /* Not allowed to retrieve a non-existent page. */
210 if (pgno >= mp->npages) {
211 errno = EINVAL;
212 return (NULL);
215 /* Get a page from the cache. */
216 if ((b = mpool_bkt(mp)) == NULL)
217 return (NULL);
218 b->pgno = pgno;
219 b->flags = MPOOL_PINNED;
221 #ifdef STATISTICS
222 ++mp->pageread;
223 #endif
224 /* Read in the contents. */
225 off = mp->pagesize * pgno;
226 if (lseek(mp->fd, off, SEEK_SET) != off)
227 return (NULL);
228 if ((nr = read(mp->fd, b->page, mp->pagesize)) != mp->pagesize) {
229 if (nr >= 0)
230 errno = EFTYPE;
231 return (NULL);
233 if (mp->pgin)
234 (mp->pgin)(mp->pgcookie, b->pgno, b->page);
236 inshash(b, b->pgno);
237 inschain(b, &mp->lru);
238 #ifdef STATISTICS
239 ++mp->pageget;
240 #endif
241 return (b->page);
245 * MPOOL_PUT -- return a page to the pool
247 * Parameters:
248 * mp: mpool cookie
249 * page: page pointer
250 * pgno: page number
252 * Returns:
253 * RET_ERROR, RET_SUCCESS
256 mpool_put(mp, page, flags)
257 MPOOL *mp;
258 void *page;
259 u_int flags;
261 BKT *baddr;
262 #ifdef DEBUG
263 BKT *b;
264 #endif
266 #ifdef STATISTICS
267 ++mp->pageput;
268 #endif
269 baddr = (BKT *)((char *)page - sizeof(BKT));
270 #ifdef DEBUG
271 if (!(baddr->flags & MPOOL_PINNED))
272 err("mpool_put: page %d not pinned", b->pgno);
273 for (b = mp->lru.cnext; b != (BKT *)&mp->lru; b = b->cnext) {
274 if (b == (BKT *)&mp->lru)
275 err("mpool_put: %0x: bad address", baddr);
276 if (b == baddr)
277 break;
279 #endif
280 baddr->flags &= ~MPOOL_PINNED;
281 baddr->flags |= flags & MPOOL_DIRTY;
282 return (RET_SUCCESS);
286 * MPOOL_CLOSE -- close the buffer pool
288 * Parameters:
289 * mp: mpool cookie
291 * Returns:
292 * RET_ERROR, RET_SUCCESS
295 mpool_close(mp)
296 MPOOL *mp;
298 BKT *b, *next;
300 /* Free up any space allocated to the lru pages. */
301 for (b = mp->lru.cprev; b != (BKT *)&mp->lru; b = next) {
302 next = b->cprev;
303 free(b);
305 free(mp);
306 return (RET_SUCCESS);
310 * MPOOL_SYNC -- sync the file to disk.
312 * Parameters:
313 * mp: mpool cookie
315 * Returns:
316 * RET_ERROR, RET_SUCCESS
319 mpool_sync(mp)
320 MPOOL *mp;
322 BKT *b;
324 for (b = mp->lru.cprev; b != (BKT *)&mp->lru; b = b->cprev)
325 if (b->flags & MPOOL_DIRTY && mpool_write(mp, b) == RET_ERROR)
326 return (RET_ERROR);
327 return (fsync(mp->fd) ? RET_ERROR : RET_SUCCESS);
331 * MPOOL_BKT -- get/create a BKT from the cache
333 * Parameters:
334 * mp: mpool cookie
336 * Returns:
337 * NULL on failure and a pointer to the BKT on success
339 static BKT *
340 mpool_bkt(mp)
341 MPOOL *mp;
343 BKT *b;
345 if (mp->curcache < mp->maxcache)
346 goto new;
349 * If the cache is maxxed out, search the lru list for a buffer we
350 * can flush. If we find one, write it if necessary and take it off
351 * any lists. If we don't find anything we grow the cache anyway.
352 * The cache never shrinks.
354 for (b = mp->lru.cprev; b != (BKT *)&mp->lru; b = b->cprev)
355 if (!(b->flags & MPOOL_PINNED)) {
356 if (b->flags & MPOOL_DIRTY &&
357 mpool_write(mp, b) == RET_ERROR)
358 return (NULL);
359 rmhash(b);
360 rmchain(b);
361 #ifdef STATISTICS
362 ++mp->pageflush;
363 #endif
364 #ifdef DEBUG
366 void *spage;
367 spage = b->page;
368 memset(b, 0xff, sizeof(BKT) + mp->pagesize);
369 b->page = spage;
371 #endif
372 return (b);
375 new: if ((b = malloc(sizeof(BKT) + mp->pagesize)) == NULL)
376 return (NULL);
377 #ifdef STATISTICS
378 ++mp->pagealloc;
379 #endif
380 #ifdef DEBUG
381 memset(b, 0xff, sizeof(BKT) + mp->pagesize);
382 #endif
383 b->page = (char *)b + sizeof(BKT);
384 ++mp->curcache;
385 return (b);
389 * MPOOL_WRITE -- sync a page to disk
391 * Parameters:
392 * mp: mpool cookie
394 * Returns:
395 * RET_ERROR, RET_SUCCESS
397 static int
398 mpool_write(mp, b)
399 MPOOL *mp;
400 BKT *b;
402 off_t off;
404 if (mp->pgout)
405 (mp->pgout)(mp->pgcookie, b->pgno, b->page);
407 #ifdef STATISTICS
408 ++mp->pagewrite;
409 #endif
410 off = mp->pagesize * b->pgno;
411 if (lseek(mp->fd, off, SEEK_SET) != off)
412 return (RET_ERROR);
413 if (write(mp->fd, b->page, mp->pagesize) != mp->pagesize)
414 return (RET_ERROR);
415 b->flags &= ~MPOOL_DIRTY;
416 return (RET_SUCCESS);
420 * MPOOL_LOOK -- lookup a page
422 * Parameters:
423 * mp: mpool cookie
424 * pgno: page number
426 * Returns:
427 * NULL on failure and a pointer to the BKT on success
429 static BKT *
430 mpool_look(mp, pgno)
431 MPOOL *mp;
432 pgno_t pgno;
434 register BKT *b;
435 register BKTHDR *tb;
437 /* XXX
438 * If find the buffer, put it first on the hash chain so can
439 * find it again quickly.
441 tb = &mp->hashtable[HASHKEY(pgno)];
442 for (b = tb->hnext; b != (BKT *)tb; b = b->hnext)
443 if (b->pgno == pgno) {
444 #ifdef STATISTICS
445 ++mp->cachehit;
446 #endif
447 return (b);
449 #ifdef STATISTICS
450 ++mp->cachemiss;
451 #endif
452 return (NULL);
455 #ifdef STATISTICS
457 * MPOOL_STAT -- cache statistics
459 * Parameters:
460 * mp: mpool cookie
462 void
463 mpool_stat(mp)
464 MPOOL *mp;
466 BKT *b;
467 int cnt;
468 char *sep;
470 (void)fprintf(stderr, "%lu pages in the file\n", mp->npages);
471 (void)fprintf(stderr,
472 "page size %lu, cacheing %lu pages of %lu page max cache\n",
473 mp->pagesize, mp->curcache, mp->maxcache);
474 (void)fprintf(stderr, "%lu page puts, %lu page gets, %lu page new\n",
475 mp->pageput, mp->pageget, mp->pagenew);
476 (void)fprintf(stderr, "%lu page allocs, %lu page flushes\n",
477 mp->pagealloc, mp->pageflush);
478 if (mp->cachehit + mp->cachemiss)
479 (void)fprintf(stderr,
480 "%.0f%% cache hit rate (%lu hits, %lu misses)\n",
481 ((double)mp->cachehit / (mp->cachehit + mp->cachemiss))
482 * 100, mp->cachehit, mp->cachemiss);
483 (void)fprintf(stderr, "%lu page reads, %lu page writes\n",
484 mp->pageread, mp->pagewrite);
486 sep = "";
487 cnt = 0;
488 for (b = mp->lru.cnext; b != (BKT *)&mp->lru; b = b->cnext) {
489 (void)fprintf(stderr, "%s%d", sep, b->pgno);
490 if (b->flags & MPOOL_DIRTY)
491 (void)fprintf(stderr, "d");
492 if (b->flags & MPOOL_PINNED)
493 (void)fprintf(stderr, "P");
494 if (++cnt == 10) {
495 sep = "\n";
496 cnt = 0;
497 } else
498 sep = ", ";
501 (void)fprintf(stderr, "\n");
503 #endif
505 #ifdef DEBUG
506 #if __STDC__
507 #include <stdarg.h>
508 #else
509 #include <varargs.h>
510 #endif
512 static void
513 #if __STDC__
514 err(const char *fmt, ...)
515 #else
516 err(fmt, va_alist)
517 char *fmt;
518 va_dcl
519 #endif
521 va_list ap;
522 #if __STDC__
523 va_start(ap, fmt);
524 #else
525 va_start(ap);
526 #endif
527 (void)vfprintf(stderr, fmt, ap);
528 va_end(ap);
529 (void)fprintf(stderr, "\n");
530 abort();
531 /* NOTREACHED */
533 #endif