#include <sys/param.h>
#include <sys/stat.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <db.h>
#define __MPOOLINTERFACE_PRIVATE
#include "mpool.h"
static BKT *mpool_bkt __P((MPOOL *));
static BKT *mpool_look __P((MPOOL *, pgno_t));
static int mpool_write __P((MPOOL *, BKT *));
#ifdef DEBUG
static void __mpoolerr __P((const char *fmt, ...));
#endif
MPOOL *
mpool_open(key, fd, pagesize, maxcache)
DBT *key;
int fd;
pgno_t pagesize, maxcache;
{
struct stat sb;
MPOOL *mp;
int entry;
if (fstat(fd, &sb))
return (NULL);
if (!S_ISREG(sb.st_mode)) {
errno = ESPIPE;
return (NULL);
}
if ((mp = (MPOOL *)malloc(sizeof(MPOOL))) == NULL)
return (NULL);
mp->free.cnext = mp->free.cprev = (BKT *)&mp->free;
mp->lru.cnext = mp->lru.cprev = (BKT *)&mp->lru;
for (entry = 0; entry < HASHSIZE; ++entry)
mp->hashtable[entry].hnext = mp->hashtable[entry].hprev =
mp->hashtable[entry].cnext = mp->hashtable[entry].cprev =
(BKT *)&mp->hashtable[entry];
mp->curcache = 0;
mp->maxcache = maxcache;
mp->pagesize = pagesize;
mp->npages = sb.st_size / pagesize;
mp->fd = fd;
mp->pgcookie = NULL;
mp->pgin = mp->pgout = NULL;
#ifdef STATISTICS
mp->cachehit = mp->cachemiss = mp->pagealloc = mp->pageflush =
mp->pageget = mp->pagenew = mp->pageput = mp->pageread =
mp->pagewrite = 0;
#endif
return (mp);
}
void
mpool_filter(mp, pgin, pgout, pgcookie)
MPOOL *mp;
void (*pgin) __P((void *, pgno_t, void *));
void (*pgout) __P((void *, pgno_t, void *));
void *pgcookie;
{
mp->pgin = pgin;
mp->pgout = pgout;
mp->pgcookie = pgcookie;
}
void *
mpool_new(mp, pgnoaddr)
MPOOL *mp;
pgno_t *pgnoaddr;
{
BKT *b;
BKTHDR *hp;
#ifdef STATISTICS
++mp->pagenew;
#endif
if ((b = mpool_bkt(mp)) == NULL)
return (NULL);
*pgnoaddr = b->pgno = mp->npages++;
b->flags = MPOOL_PINNED;
inshash(b, b->pgno);
inschain(b, &mp->lru);
return (b->page);
}
void *
mpool_get(mp, pgno, flags)
MPOOL *mp;
pgno_t pgno;
u_int flags;
{
BKT *b;
BKTHDR *hp;
off_t off;
int nr;
if (b = mpool_look(mp, pgno)) {
#ifdef STATISTICS
++mp->pageget;
#endif
#ifdef DEBUG
if (b->flags & MPOOL_PINNED)
__mpoolerr("mpool_get: page %d already pinned",
b->pgno);
#endif
rmchain(b);
inschain(b, &mp->lru);
b->flags |= MPOOL_PINNED;
return (b->page);
}
if (pgno >= mp->npages) {
errno = EINVAL;
return (NULL);
}
if ((b = mpool_bkt(mp)) == NULL)
return (NULL);
b->pgno = pgno;
b->flags = MPOOL_PINNED;
#ifdef STATISTICS
++mp->pageread;
#endif
off = mp->pagesize * pgno;
if (lseek(mp->fd, off, SEEK_SET) != off)
return (NULL);
if ((nr = read(mp->fd, b->page, mp->pagesize)) != mp->pagesize) {
if (nr >= 0)
errno = EFTYPE;
return (NULL);
}
if (mp->pgin)
(mp->pgin)(mp->pgcookie, b->pgno, b->page);
inshash(b, b->pgno);
inschain(b, &mp->lru);
#ifdef STATISTICS
++mp->pageget;
#endif
return (b->page);
}
int
mpool_put(mp, page, flags)
MPOOL *mp;
void *page;
u_int flags;
{
BKT *baddr;
#ifdef DEBUG
BKT *b;
#endif
#ifdef STATISTICS
++mp->pageput;
#endif
baddr = (BKT *)((char *)page - sizeof(BKT));
#ifdef DEBUG
if (!(baddr->flags & MPOOL_PINNED))
__mpoolerr("mpool_put: page %d not pinned", b->pgno);
for (b = mp->lru.cnext; b != (BKT *)&mp->lru; b = b->cnext) {
if (b == (BKT *)&mp->lru)
__mpoolerr("mpool_put: %0x: bad address", baddr);
if (b == baddr)
break;
}
#endif
baddr->flags &= ~MPOOL_PINNED;
baddr->flags |= flags & MPOOL_DIRTY;
return (RET_SUCCESS);
}
int
mpool_close(mp)
MPOOL *mp;
{
BKT *b, *next;
for (b = mp->lru.cprev; b != (BKT *)&mp->lru; b = next) {
next = b->cprev;
free(b);
}
free(mp);
return (RET_SUCCESS);
}
int
mpool_sync(mp)
MPOOL *mp;
{
BKT *b;
for (b = mp->lru.cprev; b != (BKT *)&mp->lru; b = b->cprev)
if (b->flags & MPOOL_DIRTY && mpool_write(mp, b) == RET_ERROR)
return (RET_ERROR);
return (fsync(mp->fd) ? RET_ERROR : RET_SUCCESS);
}
static BKT *
mpool_bkt(mp)
MPOOL *mp;
{
BKT *b;
if (mp->curcache < mp->maxcache)
goto new;
for (b = mp->lru.cprev; b != (BKT *)&mp->lru; b = b->cprev)
if (!(b->flags & MPOOL_PINNED)) {
if (b->flags & MPOOL_DIRTY &&
mpool_write(mp, b) == RET_ERROR)
return (NULL);
rmhash(b);
rmchain(b);
#ifdef STATISTICS
++mp->pageflush;
#endif
#ifdef DEBUG
{
void *spage;
spage = b->page;
memset(b, 0xff, sizeof(BKT) + mp->pagesize);
b->page = spage;
}
#endif
return (b);
}
new: if ((b = (BKT *)malloc(sizeof(BKT) + mp->pagesize)) == NULL)
return (NULL);
#ifdef STATISTICS
++mp->pagealloc;
#endif
#ifdef DEBUG
memset(b, 0xff, sizeof(BKT) + mp->pagesize);
#endif
b->page = (char *)b + sizeof(BKT);
++mp->curcache;
return (b);
}
static int
mpool_write(mp, b)
MPOOL *mp;
BKT *b;
{
off_t off;
if (mp->pgout)
(mp->pgout)(mp->pgcookie, b->pgno, b->page);
#ifdef STATISTICS
++mp->pagewrite;
#endif
off = mp->pagesize * b->pgno;
if (lseek(mp->fd, off, SEEK_SET) != off)
return (RET_ERROR);
if (write(mp->fd, b->page, mp->pagesize) != mp->pagesize)
return (RET_ERROR);
b->flags &= ~MPOOL_DIRTY;
return (RET_SUCCESS);
}
static BKT *
mpool_look(mp, pgno)
MPOOL *mp;
pgno_t pgno;
{
register BKT *b;
register BKTHDR *tb;
tb = &mp->hashtable[HASHKEY(pgno)];
for (b = tb->hnext; b != (BKT *)tb; b = b->hnext)
if (b->pgno == pgno) {
#ifdef STATISTICS
++mp->cachehit;
#endif
return (b);
}
#ifdef STATISTICS
++mp->cachemiss;
#endif
return (NULL);
}
#ifdef STATISTICS
void
mpool_stat(mp)
MPOOL *mp;
{
BKT *b;
int cnt;
char *sep;
(void)fprintf(stderr, "%lu pages in the file\n", mp->npages);
(void)fprintf(stderr,
"page size %lu, cacheing %lu pages of %lu page max cache\n",
mp->pagesize, mp->curcache, mp->maxcache);
(void)fprintf(stderr, "%lu page puts, %lu page gets, %lu page new\n",
mp->pageput, mp->pageget, mp->pagenew);
(void)fprintf(stderr, "%lu page allocs, %lu page flushes\n",
mp->pagealloc, mp->pageflush);
if (mp->cachehit + mp->cachemiss)
(void)fprintf(stderr,
"%.0f%% cache hit rate (%lu hits, %lu misses)\n",
((double)mp->cachehit / (mp->cachehit + mp->cachemiss))
* 100, mp->cachehit, mp->cachemiss);
(void)fprintf(stderr, "%lu page reads, %lu page writes\n",
mp->pageread, mp->pagewrite);
sep = "";
cnt = 0;
for (b = mp->lru.cnext; b != (BKT *)&mp->lru; b = b->cnext) {
(void)fprintf(stderr, "%s%d", sep, b->pgno);
if (b->flags & MPOOL_DIRTY)
(void)fprintf(stderr, "d");
if (b->flags & MPOOL_PINNED)
(void)fprintf(stderr, "P");
if (++cnt == 10) {
sep = "\n";
cnt = 0;
} else
sep = ", ";
}
(void)fprintf(stderr, "\n");
}
#endif
#ifdef DEBUG
#if __STDC__
#include <stdarg.h>
#else
#include <varargs.h>
#endif
static void
#if __STDC__
__mpoolerr(const char *fmt, ...)
#else
__mpoolerr(fmt, va_alist)
char *fmt;
va_dcl
#endif
{
va_list ap;
#if __STDC__
va_start(ap, fmt);
#else
va_start(ap);
#endif
(void)vfprintf(stderr, fmt, ap);
va_end(ap);
(void)fprintf(stderr, "\n");
abort();
}
#endif