1 /*- 2 * Copyright (c) 1990, 1993, 1994 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.7 (Berkeley) 11/2/95"; 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 #include "db-int.h" 48 #include "mpool.h" 49 50 static BKT *mpool_bkt __P((MPOOL *)); 51 static BKT *mpool_look __P((MPOOL *, db_pgno_t)); 52 static int mpool_write __P((MPOOL *, BKT *)); 53 54 /* 55 * mpool_open -- 56 * Initialize a memory pool. 57 */ 58 MPOOL * 59 mpool_open(key, fd, pagesize, maxcache) 60 void *key; 61 int fd; 62 db_pgno_t pagesize, maxcache; 63 { 64 struct stat sb; 65 MPOOL *mp; 66 int entry; 67 68 /* 69 * Get information about the file. 70 * 71 * XXX 72 * We don't currently handle pipes, although we should. 73 */ 74 if (fstat(fd, &sb)) 75 return (NULL); 76 if (!S_ISREG(sb.st_mode)) { 77 errno = ESPIPE; 78 return (NULL); 79 } 80 81 /* Allocate and initialize the MPOOL cookie. */ 82 if ((mp = (MPOOL *)calloc(1, sizeof(MPOOL))) == NULL) 83 return (NULL); 84 TAILQ_INIT(&mp->lqh); 85 for (entry = 0; entry < HASHSIZE; ++entry) 86 TAILQ_INIT(&mp->hqh[entry]); 87 mp->maxcache = maxcache; 88 mp->npages = sb.st_size / pagesize; 89 mp->pagesize = pagesize; 90 mp->fd = fd; 91 return (mp); 92 } 93 94 /* 95 * mpool_filter -- 96 * Initialize input/output filters. 97 */ 98 void 99 mpool_filter(mp, pgin, pgout, pgcookie) 100 MPOOL *mp; 101 void (*pgin) __P((void *, db_pgno_t, void *)); 102 void (*pgout) __P((void *, db_pgno_t, void *)); 103 void *pgcookie; 104 { 105 mp->pgin = pgin; 106 mp->pgout = pgout; 107 mp->pgcookie = pgcookie; 108 } 109 110 /* 111 * mpool_new -- 112 * Get a new page of memory. 113 */ 114 void * 115 mpool_new(mp, pgnoaddr, flags) 116 MPOOL *mp; 117 db_pgno_t *pgnoaddr; 118 u_int flags; 119 { 120 struct _hqh *head; 121 BKT *bp; 122 123 if (mp->npages == MAX_PAGE_NUMBER) { 124 (void)fprintf(stderr, "mpool_new: page allocation overflow.\n"); 125 abort(); 126 } 127 #ifdef STATISTICS 128 ++mp->pagenew; 129 #endif 130 /* 131 * Get a BKT from the cache. Assign a new page number, attach 132 * it to the head of the hash chain, the tail of the lru chain, 133 * and return. 134 */ 135 if ((bp = mpool_bkt(mp)) == NULL) 136 return (NULL); 137 if (flags == MPOOL_PAGE_REQUEST) { 138 mp->npages++; 139 bp->pgno = *pgnoaddr; 140 } else 141 bp->pgno = *pgnoaddr = mp->npages++; 142 143 bp->flags = MPOOL_PINNED | MPOOL_INUSE; 144 145 head = &mp->hqh[HASHKEY(bp->pgno)]; 146 TAILQ_INSERT_HEAD(head, bp, hq); 147 TAILQ_INSERT_TAIL(&mp->lqh, bp, q); 148 return (bp->page); 149 } 150 151 int 152 mpool_delete(mp, page) 153 MPOOL *mp; 154 void *page; 155 { 156 struct _hqh *head; 157 BKT *bp; 158 159 bp = (void *)((char *)page - sizeof(BKT)); 160 161 #ifdef DEBUG 162 if (!(bp->flags & MPOOL_PINNED)) { 163 (void)fprintf(stderr, 164 "mpool_delete: page %d not pinned\n", bp->pgno); 165 abort(); 166 } 167 #endif 168 169 /* Remove from the hash and lru queues. */ 170 head = &mp->hqh[HASHKEY(bp->pgno)]; 171 TAILQ_REMOVE(head, bp, hq); 172 TAILQ_REMOVE(&mp->lqh, bp, q); 173 174 free(bp); 175 return (RET_SUCCESS); 176 } 177 178 /* 179 * mpool_get 180 * Get a page. 181 */ 182 void * 183 mpool_get(mp, pgno, flags) 184 MPOOL *mp; 185 db_pgno_t pgno; 186 u_int flags; /* XXX not used? */ 187 { 188 struct _hqh *head; 189 BKT *bp; 190 off_t off; 191 int nr; 192 193 #ifdef STATISTICS 194 ++mp->pageget; 195 #endif 196 197 /* Check for a page that is cached. */ 198 if ((bp = mpool_look(mp, pgno)) != NULL) { 199 #ifdef DEBUG 200 if (!(flags & MPOOL_IGNOREPIN) && bp->flags & MPOOL_PINNED) { 201 (void)fprintf(stderr, 202 "mpool_get: page %d already pinned\n", bp->pgno); 203 abort(); 204 } 205 #endif 206 /* 207 * Move the page to the head of the hash chain and the tail 208 * of the lru chain. 209 */ 210 head = &mp->hqh[HASHKEY(bp->pgno)]; 211 TAILQ_REMOVE(head, bp, hq); 212 TAILQ_INSERT_HEAD(head, bp, hq); 213 TAILQ_REMOVE(&mp->lqh, bp, q); 214 TAILQ_INSERT_TAIL(&mp->lqh, bp, q); 215 216 /* Return a pinned page. */ 217 if (!(flags & MPOOL_IGNOREPIN)) 218 bp->flags |= MPOOL_PINNED; 219 return (bp->page); 220 } 221 222 /* Get a page from the cache. */ 223 if ((bp = mpool_bkt(mp)) == NULL) 224 return (NULL); 225 226 /* Read in the contents. */ 227 #ifdef STATISTICS 228 ++mp->pageread; 229 #endif 230 off = mp->pagesize * pgno; 231 if (off / mp->pagesize != pgno) { 232 /* Run past the end of the file, or at least the part we 233 can address without large-file support? */ 234 errno = E2BIG; 235 return NULL; 236 } 237 if (lseek(mp->fd, off, SEEK_SET) != off) 238 return (NULL); 239 240 if ((nr = read(mp->fd, bp->page, mp->pagesize)) != 241 (ssize_t)mp->pagesize) { 242 if (nr > 0) { 243 /* A partial read is definitely bad. */ 244 errno = EINVAL; 245 return (NULL); 246 } else { 247 /* 248 * A zero-length reads, means you need to create a 249 * new page. 250 */ 251 memset(bp->page, 0, mp->pagesize); 252 } 253 } 254 255 /* Set the page number, pin the page. */ 256 bp->pgno = pgno; 257 if (!(flags & MPOOL_IGNOREPIN)) 258 bp->flags = MPOOL_PINNED; 259 bp->flags |= MPOOL_INUSE; 260 261 /* 262 * Add the page to the head of the hash chain and the tail 263 * of the lru chain. 264 */ 265 head = &mp->hqh[HASHKEY(bp->pgno)]; 266 TAILQ_INSERT_HEAD(head, bp, hq); 267 TAILQ_INSERT_TAIL(&mp->lqh, bp, q); 268 269 /* Run through the user's filter. */ 270 if (mp->pgin != NULL) 271 (mp->pgin)(mp->pgcookie, bp->pgno, bp->page); 272 273 return (bp->page); 274 } 275 276 /* 277 * mpool_put 278 * Return a page. 279 */ 280 int 281 mpool_put(mp, page, flags) 282 MPOOL *mp; 283 void *page; 284 u_int flags; 285 { 286 BKT *bp; 287 288 #ifdef STATISTICS 289 ++mp->pageput; 290 #endif 291 bp = (void *)((char *)page - sizeof(BKT)); 292 #ifdef DEBUG 293 if (!(bp->flags & MPOOL_PINNED)) { 294 (void)fprintf(stderr, 295 "mpool_put: page %d not pinned\n", bp->pgno); 296 abort(); 297 } 298 #endif 299 bp->flags &= ~MPOOL_PINNED; 300 if (flags & MPOOL_DIRTY) 301 bp->flags |= flags & MPOOL_DIRTY; 302 return (RET_SUCCESS); 303 } 304 305 /* 306 * mpool_close 307 * Close the buffer pool. 308 */ 309 int 310 mpool_close(mp) 311 MPOOL *mp; 312 { 313 BKT *bp; 314 315 /* Free up any space allocated to the lru pages. */ 316 while ((bp = mp->lqh.tqh_first) != NULL) { 317 TAILQ_REMOVE(&mp->lqh, mp->lqh.tqh_first, q); 318 free(bp); 319 } 320 321 /* Free the MPOOL cookie. */ 322 free(mp); 323 return (RET_SUCCESS); 324 } 325 326 /* 327 * mpool_sync 328 * Sync the pool to disk. 329 */ 330 int 331 mpool_sync(mp) 332 MPOOL *mp; 333 { 334 BKT *bp; 335 336 /* Walk the lru chain, flushing any dirty pages to disk. */ 337 for (bp = mp->lqh.tqh_first; bp != NULL; bp = bp->q.tqe_next) 338 if (bp->flags & MPOOL_DIRTY && 339 mpool_write(mp, bp) == RET_ERROR) 340 return (RET_ERROR); 341 342 /* Sync the file descriptor. */ 343 return (fsync(mp->fd) ? RET_ERROR : RET_SUCCESS); 344 } 345 346 /* 347 * mpool_bkt 348 * Get a page from the cache (or create one). 349 */ 350 static BKT * 351 mpool_bkt(mp) 352 MPOOL *mp; 353 { 354 struct _hqh *head; 355 BKT *bp; 356 357 /* If under the max cached, always create a new page. */ 358 if (mp->curcache < mp->maxcache) 359 goto new; 360 361 /* 362 * If the cache is max'd out, walk the lru list for a buffer we 363 * can flush. If we find one, write it (if necessary) and take it 364 * off any lists. If we don't find anything we grow the cache anyway. 365 * The cache never shrinks. 366 */ 367 for (bp = mp->lqh.tqh_first; bp != NULL; bp = bp->q.tqe_next) 368 if (!(bp->flags & MPOOL_PINNED)) { 369 /* Flush if dirty. */ 370 if (bp->flags & MPOOL_DIRTY && 371 mpool_write(mp, bp) == RET_ERROR) 372 return (NULL); 373 #ifdef STATISTICS 374 ++mp->pageflush; 375 #endif 376 /* Remove from the hash and lru queues. */ 377 head = &mp->hqh[HASHKEY(bp->pgno)]; 378 TAILQ_REMOVE(head, bp, hq); 379 TAILQ_REMOVE(&mp->lqh, bp, q); 380 #if defined(DEBUG) && !defined(DEBUG_IDX0SPLIT) 381 { void *spage; 382 spage = bp->page; 383 memset(bp, 0xff, sizeof(BKT) + mp->pagesize); 384 bp->page = spage; 385 } 386 #endif 387 bp->flags = 0; 388 return (bp); 389 } 390 391 new: if ((bp = (BKT *)malloc(sizeof(BKT) + mp->pagesize)) == NULL) 392 return (NULL); 393 #ifdef STATISTICS 394 ++mp->pagealloc; 395 #endif 396 #if defined(DEBUG) || defined(PURIFY) || 1 397 memset(bp, 0xff, sizeof(BKT) + mp->pagesize); 398 #endif 399 bp->page = (char *)bp + sizeof(BKT); 400 bp->flags = 0; 401 ++mp->curcache; 402 return (bp); 403 } 404 405 /* 406 * mpool_write 407 * Write a page to disk. 408 */ 409 static int 410 mpool_write(mp, bp) 411 MPOOL *mp; 412 BKT *bp; 413 { 414 off_t off; 415 416 #ifdef STATISTICS 417 ++mp->pagewrite; 418 #endif 419 420 /* Run through the user's filter. */ 421 if (mp->pgout) 422 (mp->pgout)(mp->pgcookie, bp->pgno, bp->page); 423 424 off = mp->pagesize * bp->pgno; 425 if (off / mp->pagesize != bp->pgno) { 426 /* Run past the end of the file, or at least the part we 427 can address without large-file support? */ 428 errno = E2BIG; 429 return RET_ERROR; 430 } 431 if (lseek(mp->fd, off, SEEK_SET) != off) 432 return (RET_ERROR); 433 if (write(mp->fd, bp->page, mp->pagesize) != 434 (ssize_t)mp->pagesize) 435 return (RET_ERROR); 436 437 /* 438 * Re-run through the input filter since this page may soon be 439 * accessed via the cache, and whatever the user's output filter 440 * did may screw things up if we don't let the input filter 441 * restore the in-core copy. 442 */ 443 if (mp->pgin) 444 (mp->pgin)(mp->pgcookie, bp->pgno, bp->page); 445 bp->flags &= ~MPOOL_DIRTY; 446 return (RET_SUCCESS); 447 } 448 449 /* 450 * mpool_look 451 * Lookup a page in the cache. 452 */ 453 static BKT * 454 mpool_look(mp, pgno) 455 MPOOL *mp; 456 db_pgno_t pgno; 457 { 458 struct _hqh *head; 459 BKT *bp; 460 461 head = &mp->hqh[HASHKEY(pgno)]; 462 for (bp = head->tqh_first; bp != NULL; bp = bp->hq.tqe_next) 463 if ((bp->pgno == pgno) && (bp->flags & MPOOL_INUSE)) { 464 #ifdef STATISTICS 465 ++mp->cachehit; 466 #endif 467 return (bp); 468 } 469 #ifdef STATISTICS 470 ++mp->cachemiss; 471 #endif 472 return (NULL); 473 } 474 475 #ifdef STATISTICS 476 /* 477 * mpool_stat 478 * Print out cache statistics. 479 */ 480 void 481 mpool_stat(mp) 482 MPOOL *mp; 483 { 484 BKT *bp; 485 int cnt; 486 char *sep; 487 488 (void)fprintf(stderr, "%lu pages in the file\n", mp->npages); 489 (void)fprintf(stderr, 490 "page size %lu, cacheing %lu pages of %lu page max cache\n", 491 mp->pagesize, mp->curcache, mp->maxcache); 492 (void)fprintf(stderr, "%lu page puts, %lu page gets, %lu page new\n", 493 mp->pageput, mp->pageget, mp->pagenew); 494 (void)fprintf(stderr, "%lu page allocs, %lu page flushes\n", 495 mp->pagealloc, mp->pageflush); 496 if (mp->cachehit + mp->cachemiss) 497 (void)fprintf(stderr, 498 "%.0f%% cache hit rate (%lu hits, %lu misses)\n", 499 ((double)mp->cachehit / (mp->cachehit + mp->cachemiss)) 500 * 100, mp->cachehit, mp->cachemiss); 501 (void)fprintf(stderr, "%lu page reads, %lu page writes\n", 502 mp->pageread, mp->pagewrite); 503 504 sep = ""; 505 cnt = 0; 506 for (bp = mp->lqh.tqh_first; bp != NULL; bp = bp->q.tqe_next) { 507 (void)fprintf(stderr, "%s%d", sep, bp->pgno); 508 if (bp->flags & MPOOL_DIRTY) 509 (void)fprintf(stderr, "d"); 510 if (bp->flags & MPOOL_PINNED) 511 (void)fprintf(stderr, "P"); 512 if (++cnt == 10) { 513 sep = "\n"; 514 cnt = 0; 515 } else 516 sep = ", "; 517 518 } 519 (void)fprintf(stderr, "\n"); 520 } 521 #else 522 void 523 mpool_stat(mp) 524 MPOOL *mp; 525 { 526 } 527 #endif 528