home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Libraries / Berkeley DB 1.6 / mpool / mpool.c next >
Encoding:
C/C++ Source or Header  |  1993-07-18  |  11.5 KB  |  543 lines  |  [TEXT/????]

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