| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
#if defined(LIBC_SCCS) && !defined(lint) |
| 35 |
static char sccsid[] = "@(#)mpool.c 8.5 (Berkeley) 7/26/94"; |
| 36 |
#endif |
| 37 |
|
| 38 |
#include <sys/param.h> |
| 39 |
#include <sys/queue.h> |
| 40 |
#include <sys/stat.h> |
| 41 |
|
| 42 |
#include <errno.h> |
| 43 |
#include <stdio.h> |
| 44 |
#include <stdlib.h> |
| 45 |
#include <string.h> |
| 46 |
#include <unistd.h> |
| 47 |
|
| 48 |
#include "../include/db.h" |
| 49 |
|
| 50 |
#define __MPOOLINTERFACE_PRIVATE |
| 51 |
#include <mpool.h> |
| 52 |
|
| 53 |
#undef __APPLE__ |
| 54 |
|
| 55 |
#ifndef __APPLE__ |
| 56 |
#define mpool_open __mpool_open |
| 57 |
#define mpool_filter __mpool_filter |
| 58 |
#define mpool_new __mpool_new |
| 59 |
#define mpool_get __mpool_get |
| 60 |
#define mpool_put __mpool_put |
| 61 |
#define mpool_sync __mpool_sync |
| 62 |
#define mpool_close __mpool_close |
| 63 |
#endif |
| 64 |
|
| 65 |
static BKT *mpool_bkt __P((MPOOL *)); |
| 66 |
static BKT *mpool_look __P((MPOOL *, pgno_t)); |
| 67 |
static int mpool_write __P((MPOOL *, BKT *)); |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
MPOOL * |
| 74 |
mpool_open(key, fd, pagesize, maxcache) |
| 75 |
void *key; |
| 76 |
int fd; |
| 77 |
pgno_t pagesize, maxcache; |
| 78 |
{ |
| 79 |
struct stat sb; |
| 80 |
MPOOL *mp; |
| 81 |
int entry; |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
if (fstat(fd, &sb)) |
| 90 |
return (NULL); |
| 91 |
if (!S_ISREG(sb.st_mode)) { |
| 92 |
errno = ESPIPE; |
| 93 |
return (NULL); |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
if ((mp = (MPOOL *)calloc(1, sizeof(MPOOL))) == NULL) |
| 98 |
return (NULL); |
| 99 |
CIRCLEQ_INIT(&mp->lqh); |
| 100 |
for (entry = 0; entry < HASHSIZE; ++entry) |
| 101 |
CIRCLEQ_INIT(&mp->hqh[entry]); |
| 102 |
mp->maxcache = maxcache; |
| 103 |
mp->npages = sb.st_size / pagesize; |
| 104 |
mp->pagesize = pagesize; |
| 105 |
mp->fd = fd; |
| 106 |
return (mp); |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
void |
| 114 |
mpool_filter(mp, pgin, pgout, pgcookie) |
| 115 |
MPOOL *mp; |
| 116 |
void (*pgin) __P((void *, pgno_t, void *)); |
| 117 |
void (*pgout) __P((void *, pgno_t, void *)); |
| 118 |
void *pgcookie; |
| 119 |
{ |
| 120 |
mp->pgin = pgin; |
| 121 |
mp->pgout = pgout; |
| 122 |
mp->pgcookie = pgcookie; |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
void * |
| 130 |
mpool_new(mp, pgnoaddr) |
| 131 |
MPOOL *mp; |
| 132 |
pgno_t *pgnoaddr; |
| 133 |
{ |
| 134 |
struct _hqh *head; |
| 135 |
BKT *bp; |
| 136 |
|
| 137 |
if (mp->npages == MAX_PAGE_NUMBER) { |
| 138 |
(void)fprintf(stderr, "mpool_new: page allocation overflow.\n"); |
| 139 |
abort(); |
| 140 |
} |
| 141 |
#ifdef STATISTICS |
| 142 |
++mp->pagenew; |
| 143 |
#endif |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
if ((bp = mpool_bkt(mp)) == NULL) |
| 150 |
return (NULL); |
| 151 |
*pgnoaddr = bp->pgno = mp->npages++; |
| 152 |
bp->flags = MPOOL_PINNED; |
| 153 |
|
| 154 |
head = &mp->hqh[HASHKEY(bp->pgno)]; |
| 155 |
CIRCLEQ_INSERT_HEAD(head, bp, hq); |
| 156 |
CIRCLEQ_INSERT_TAIL(&mp->lqh, bp, q); |
| 157 |
return (bp->page); |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
void * |
| 165 |
mpool_get(mp, pgno, flags) |
| 166 |
MPOOL *mp; |
| 167 |
pgno_t pgno; |
| 168 |
u_int flags; |
| 169 |
{ |
| 170 |
struct _hqh *head; |
| 171 |
BKT *bp; |
| 172 |
off_t off; |
| 173 |
int nr; |
| 174 |
|
| 175 |
|
| 176 |
if (pgno >= mp->npages) { |
| 177 |
errno = EINVAL; |
| 178 |
return (NULL); |
| 179 |
} |
| 180 |
|
| 181 |
#ifdef STATISTICS |
| 182 |
++mp->pageget; |
| 183 |
#endif |
| 184 |
|
| 185 |
|
| 186 |
if ((bp = mpool_look(mp, pgno)) != NULL) { |
| 187 |
#ifdef DEBUG |
| 188 |
if (bp->flags & MPOOL_PINNED) { |
| 189 |
(void)fprintf(stderr, |
| 190 |
"mpool_get: page %d already pinned\n", bp->pgno); |
| 191 |
abort(); |
| 192 |
} |
| 193 |
#endif |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
head = &mp->hqh[HASHKEY(bp->pgno)]; |
| 199 |
CIRCLEQ_REMOVE(head, bp, hq); |
| 200 |
CIRCLEQ_INSERT_HEAD(head, bp, hq); |
| 201 |
CIRCLEQ_REMOVE(&mp->lqh, bp, q); |
| 202 |
CIRCLEQ_INSERT_TAIL(&mp->lqh, bp, q); |
| 203 |
|
| 204 |
|
| 205 |
bp->flags |= MPOOL_PINNED; |
| 206 |
return (bp->page); |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
if ((bp = mpool_bkt(mp)) == NULL) |
| 211 |
return (NULL); |
| 212 |
|
| 213 |
|
| 214 |
#ifdef STATISTICS |
| 215 |
++mp->pageread; |
| 216 |
#endif |
| 217 |
off = mp->pagesize * pgno; |
| 218 |
if (lseek(mp->fd, off, SEEK_SET) != off) |
| 219 |
return (NULL); |
| 220 |
if ((u_long) (nr = read(mp->fd, bp->page, mp->pagesize)) |
| 221 |
!= mp->pagesize) { |
| 222 |
if (nr >= 0) |
| 223 |
errno = EFTYPE; |
| 224 |
return (NULL); |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
bp->pgno = pgno; |
| 229 |
bp->flags = MPOOL_PINNED; |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
head = &mp->hqh[HASHKEY(bp->pgno)]; |
| 236 |
CIRCLEQ_INSERT_HEAD(head, bp, hq); |
| 237 |
CIRCLEQ_INSERT_TAIL(&mp->lqh, bp, q); |
| 238 |
|
| 239 |
|
| 240 |
if (mp->pgin != NULL) |
| 241 |
(mp->pgin)(mp->pgcookie, bp->pgno, bp->page); |
| 242 |
|
| 243 |
return (bp->page); |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
int |
| 251 |
mpool_put(mp, page, flags) |
| 252 |
MPOOL *mp; |
| 253 |
void *page; |
| 254 |
u_int flags; |
| 255 |
{ |
| 256 |
BKT *bp; |
| 257 |
|
| 258 |
#ifdef STATISTICS |
| 259 |
++mp->pageput; |
| 260 |
#endif |
| 261 |
bp = (BKT *)((char *)page - sizeof(BKT)); |
| 262 |
#ifdef DEBUG |
| 263 |
if (!(bp->flags & MPOOL_PINNED)) { |
| 264 |
(void)fprintf(stderr, |
| 265 |
"mpool_put: page %d not pinned\n", bp->pgno); |
| 266 |
abort(); |
| 267 |
} |
| 268 |
#endif |
| 269 |
bp->flags &= ~MPOOL_PINNED; |
| 270 |
bp->flags |= flags & MPOOL_DIRTY; |
| 271 |
return (RET_SUCCESS); |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
int |
| 279 |
mpool_close(mp) |
| 280 |
MPOOL *mp; |
| 281 |
{ |
| 282 |
BKT *bp; |
| 283 |
|
| 284 |
|
| 285 |
while ((bp = mp->lqh.cqh_first) != (void *)&mp->lqh) { |
| 286 |
CIRCLEQ_REMOVE(&mp->lqh, mp->lqh.cqh_first, q); |
| 287 |
free(bp); |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
free(mp); |
| 292 |
return (RET_SUCCESS); |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
int |
| 300 |
mpool_sync(mp) |
| 301 |
MPOOL *mp; |
| 302 |
{ |
| 303 |
BKT *bp; |
| 304 |
|
| 305 |
|
| 306 |
for (bp = mp->lqh.cqh_first; |
| 307 |
bp != (void *)&mp->lqh; bp = bp->q.cqe_next) |
| 308 |
if (bp->flags & MPOOL_DIRTY && |
| 309 |
mpool_write(mp, bp) == RET_ERROR) |
| 310 |
return (RET_ERROR); |
| 311 |
|
| 312 |
|
| 313 |
return (fsync(mp->fd) ? RET_ERROR : RET_SUCCESS); |
| 314 |
} |
| 315 |
|
| 316 |
#define __APPLE__ |
| 317 |
|
| 318 |
#ifndef __APPLE__ |
| 319 |
#undef mpool_open |
| 320 |
#undef mpool_filter |
| 321 |
#undef mpool_new |
| 322 |
#undef mpool_get |
| 323 |
#undef mpool_put |
| 324 |
#undef mpool_close |
| 325 |
#undef mpool_sync |
| 326 |
|
| 327 |
#define weak_alias(original, alias) \ |
| 328 |
asm (".weak " #alias "\n" #alias " = " #original); |
| 329 |
weak_alias (__mpool_open, mpool_open) |
| 330 |
weak_alias (__mpool_filter, mpool_filter) |
| 331 |
weak_alias (__mpool_new, mpool_new) |
| 332 |
weak_alias (__mpool_get, mpool_get) |
| 333 |
weak_alias (__mpool_put, mpool_put) |
| 334 |
weak_alias (__mpool_close, mpool_close) |
| 335 |
weak_alias (__mpool_sync, mpool_sync) |
| 336 |
#endif |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
static BKT * |
| 343 |
mpool_bkt(mp) |
| 344 |
MPOOL *mp; |
| 345 |
{ |
| 346 |
struct _hqh *head; |
| 347 |
BKT *bp; |
| 348 |
|
| 349 |
|
| 350 |
if (mp->curcache < mp->maxcache) |
| 351 |
goto new; |
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
for (bp = mp->lqh.cqh_first; |
| 360 |
bp != (void *)&mp->lqh; bp = bp->q.cqe_next) |
| 361 |
if (!(bp->flags & MPOOL_PINNED)) { |
| 362 |
|
| 363 |
if (bp->flags & MPOOL_DIRTY && |
| 364 |
mpool_write(mp, bp) == RET_ERROR) |
| 365 |
return (NULL); |
| 366 |
#ifdef STATISTICS |
| 367 |
++mp->pageflush; |
| 368 |
#endif |
| 369 |
|
| 370 |
head = &mp->hqh[HASHKEY(bp->pgno)]; |
| 371 |
CIRCLEQ_REMOVE(head, bp, hq); |
| 372 |
CIRCLEQ_REMOVE(&mp->lqh, bp, q); |
| 373 |
#ifdef DEBUG |
| 374 |
{ void *spage; |
| 375 |
spage = bp->page; |
| 376 |
memset(bp, 0xff, sizeof(BKT) + mp->pagesize); |
| 377 |
bp->page = spage; |
| 378 |
} |
| 379 |
#endif |
| 380 |
return (bp); |
| 381 |
} |
| 382 |
|
| 383 |
new: if ((bp = (BKT *) calloc(1, sizeof(BKT) + mp->pagesize)) == NULL) |
| 384 |
return (NULL); |
| 385 |
#ifdef STATISTICS |
| 386 |
++mp->pagealloc; |
| 387 |
#endif |
| 388 |
#if defined(DEBUG) || defined(PURIFY) |
| 389 |
memset(bp, 0xff, sizeof(BKT) + mp->pagesize); |
| 390 |
#endif |
| 391 |
bp->page = (char *)bp + sizeof(BKT); |
| 392 |
++mp->curcache; |
| 393 |
return (bp); |
| 394 |
} |
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
static int |
| 401 |
mpool_write(mp, bp) |
| 402 |
MPOOL *mp; |
| 403 |
BKT *bp; |
| 404 |
{ |
| 405 |
off_t off; |
| 406 |
|
| 407 |
#ifdef STATISTICS |
| 408 |
++mp->pagewrite; |
| 409 |
#endif |
| 410 |
|
| 411 |
|
| 412 |
if (mp->pgout) |
| 413 |
(mp->pgout)(mp->pgcookie, bp->pgno, bp->page); |
| 414 |
|
| 415 |
off = mp->pagesize * bp->pgno; |
| 416 |
if (lseek(mp->fd, off, SEEK_SET) != off) |
| 417 |
return (RET_ERROR); |
| 418 |
if ((u_long) write(mp->fd, bp->page, mp->pagesize) != mp->pagesize) |
| 419 |
return (RET_ERROR); |
| 420 |
|
| 421 |
bp->flags &= ~MPOOL_DIRTY; |
| 422 |
return (RET_SUCCESS); |
| 423 |
} |
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
static BKT * |
| 430 |
mpool_look(mp, pgno) |
| 431 |
MPOOL *mp; |
| 432 |
pgno_t pgno; |
| 433 |
{ |
| 434 |
struct _hqh *head; |
| 435 |
BKT *bp; |
| 436 |
|
| 437 |
head = &mp->hqh[HASHKEY(pgno)]; |
| 438 |
for (bp = head->cqh_first; bp != (void *)head; bp = bp->hq.cqe_next) |
| 439 |
if (bp->pgno == pgno) { |
| 440 |
#ifdef STATISTICS |
| 441 |
++mp->cachehit; |
| 442 |
#endif |
| 443 |
return (bp); |
| 444 |
} |
| 445 |
#ifdef STATISTICS |
| 446 |
++mp->cachemiss; |
| 447 |
#endif |
| 448 |
return (NULL); |
| 449 |
} |
| 450 |
|
| 451 |
#ifdef STATISTICS |
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
void |
| 457 |
mpool_stat(mp) |
| 458 |
MPOOL *mp; |
| 459 |
{ |
| 460 |
BKT *bp; |
| 461 |
int cnt; |
| 462 |
char *sep; |
| 463 |
|
| 464 |
(void)fprintf(stderr, "%lu pages in the file\n", mp->npages); |
| 465 |
(void)fprintf(stderr, |
| 466 |
"page size %lu, caching %lu pages of %lu page max cache\n", |
| 467 |
mp->pagesize, mp->curcache, mp->maxcache); |
| 468 |
(void)fprintf(stderr, "%lu page puts, %lu page gets, %lu page new\n", |
| 469 |
mp->pageput, mp->pageget, mp->pagenew); |
| 470 |
(void)fprintf(stderr, "%lu page allocs, %lu page flushes\n", |
| 471 |
mp->pagealloc, mp->pageflush); |
| 472 |
if (mp->cachehit + mp->cachemiss) |
| 473 |
(void)fprintf(stderr, |
| 474 |
"%.0f%% cache hit rate (%lu hits, %lu misses)\n", |
| 475 |
((double)mp->cachehit / (mp->cachehit + mp->cachemiss)) |
| 476 |
* 100, mp->cachehit, mp->cachemiss); |
| 477 |
(void)fprintf(stderr, "%lu page reads, %lu page writes\n", |
| 478 |
mp->pageread, mp->pagewrite); |
| 479 |
|
| 480 |
sep = ""; |
| 481 |
cnt = 0; |
| 482 |
for (bp = mp->lqh.cqh_first; |
| 483 |
bp != (void *)&mp->lqh; bp = bp->q.cqe_next) { |
| 484 |
(void)fprintf(stderr, "%s%d", sep, bp->pgno); |
| 485 |
if (bp->flags & MPOOL_DIRTY) |
| 486 |
(void)fprintf(stderr, "d"); |
| 487 |
if (bp->flags & MPOOL_PINNED) |
| 488 |
(void)fprintf(stderr, "P"); |
| 489 |
if (++cnt == 10) { |
| 490 |
sep = "\n"; |
| 491 |
cnt = 0; |
| 492 |
} else |
| 493 |
sep = ", "; |
| 494 |
|
| 495 |
} |
| 496 |
(void)fprintf(stderr, "\n"); |
| 497 |
} |
| 498 |
#endif |