1 /* $OpenBSD: mpool.c,v 1.18 2006/01/25 14:40:03 millert Exp $ */ 2 3 /*- 4 * Copyright (c) 1990, 1993, 1994 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include <sys/param.h> 33 #include <sys/queue.h> 34 #include <sys/stat.h> 35 36 #include <errno.h> 37 #include <stdio.h> 38 #include <stdlib.h> 39 #include <string.h> 40 #include <unistd.h> 41 42 #include <db.h> 43 44 #define __MPOOLINTERFACE_PRIVATE 45 #include <mpool.h> 46 47 static BKT *mpool_bkt(MPOOL *); 48 static BKT *mpool_look(MPOOL *, pgno_t); 49 static int mpool_write(MPOOL *, BKT *); 50 51 /* 52 * mpool_open -- 53 * Initialize a memory pool. 54 */ 55 /* ARGSUSED */ 56 MPOOL * 57 mpool_open(void *key, int fd, pgno_t pagesize, pgno_t maxcache) 58 { 59 struct stat sb; 60 MPOOL *mp; 61 int entry; 62 63 /* 64 * Get information about the file. 65 * 66 * XXX 67 * We don't currently handle pipes, although we should. 68 */ 69 if (fstat(fd, &sb)) 70 return (NULL); 71 if (!S_ISREG(sb.st_mode)) { 72 errno = ESPIPE; 73 return (NULL); 74 } 75 76 /* Allocate and initialize the MPOOL cookie. */ 77 if ((mp = (MPOOL *)calloc(1, sizeof(MPOOL))) == NULL) 78 return (NULL); 79 CIRCLEQ_INIT(&mp->lqh); 80 for (entry = 0; entry < HASHSIZE; ++entry) 81 CIRCLEQ_INIT(&mp->hqh[entry]); 82 mp->maxcache = maxcache; 83 mp->npages = sb.st_size / pagesize; 84 mp->pagesize = pagesize; 85 mp->fd = fd; 86 return (mp); 87 } 88 89 /* 90 * mpool_filter -- 91 * Initialize input/output filters. 92 */ 93 void 94 mpool_filter(MPOOL *mp, void (*pgin) (void *, pgno_t, void *), 95 void (*pgout) (void *, pgno_t, void *), void *pgcookie) 96 { 97 mp->pgin = pgin; 98 mp->pgout = pgout; 99 mp->pgcookie = pgcookie; 100 } 101 102 /* 103 * mpool_new -- 104 * Get a new page of memory. 105 */ 106 void * 107 mpool_new(MPOOL *mp, pgno_t *pgnoaddr, u_int flags) 108 { 109 struct _hqh *head; 110 BKT *bp; 111 112 if (mp->npages == MAX_PAGE_NUMBER) { 113 (void)fprintf(stderr, "mpool_new: page allocation overflow.\n"); 114 abort(); 115 } 116 #ifdef STATISTICS 117 ++mp->pagenew; 118 #endif 119 /* 120 * Get a BKT from the cache. Assign a new page number, attach 121 * it to the head of the hash chain, the tail of the lru chain, 122 * and return. 123 */ 124 if ((bp = mpool_bkt(mp)) == NULL) 125 return (NULL); 126 if (flags == MPOOL_PAGE_REQUEST) { 127 mp->npages++; 128 bp->pgno = *pgnoaddr; 129 } else 130 bp->pgno = *pgnoaddr = mp->npages++; 131 132 bp->flags = MPOOL_PINNED | MPOOL_INUSE; 133 134 head = &mp->hqh[HASHKEY(bp->pgno)]; 135 CIRCLEQ_INSERT_HEAD(head, bp, hq); 136 CIRCLEQ_INSERT_TAIL(&mp->lqh, bp, q); 137 return (bp->page); 138 } 139 140 int 141 mpool_delete(MPOOL *mp, void *page) 142 { 143 struct _hqh *head; 144 BKT *bp; 145 146 bp = (BKT *)((char *)page - sizeof(BKT)); 147 148 #ifdef DEBUG 149 if (!(bp->flags & MPOOL_PINNED)) { 150 (void)fprintf(stderr, 151 "mpool_delete: page %d not pinned\n", bp->pgno); 152 abort(); 153 } 154 #endif 155 156 /* Remove from the hash and lru queues. */ 157 head = &mp->hqh[HASHKEY(bp->pgno)]; 158 CIRCLEQ_REMOVE(head, bp, hq); 159 CIRCLEQ_REMOVE(&mp->lqh, bp, q); 160 161 free(bp); 162 mp->curcache--; 163 return (RET_SUCCESS); 164 } 165 166 /* 167 * mpool_get 168 * Get a page. 169 */ 170 /* ARGSUSED */ 171 void * 172 mpool_get(MPOOL *mp, pgno_t pgno, 173 u_int flags) /* XXX not used? */ 174 { 175 struct _hqh *head; 176 BKT *bp; 177 off_t off; 178 int nr; 179 180 #ifdef STATISTICS 181 ++mp->pageget; 182 #endif 183 184 /* Check for a page that is cached. */ 185 if ((bp = mpool_look(mp, pgno)) != NULL) { 186 #ifdef DEBUG 187 if (!(flags & MPOOL_IGNOREPIN) && bp->flags & MPOOL_PINNED) { 188 (void)fprintf(stderr, 189 "mpool_get: page %d already pinned\n", bp->pgno); 190 abort(); 191 } 192 #endif 193 /* 194 * Move the page to the head of the hash chain and the tail 195 * of the lru chain. 196 */ 197 head = &mp->hqh[HASHKEY(bp->pgno)]; 198 CIRCLEQ_REMOVE(head, bp, hq); 199 CIRCLEQ_INSERT_HEAD(head, bp, hq); 200 CIRCLEQ_REMOVE(&mp->lqh, bp, q); 201 CIRCLEQ_INSERT_TAIL(&mp->lqh, bp, q); 202 203 /* Return a pinned page. */ 204 bp->flags |= MPOOL_PINNED; 205 return (bp->page); 206 } 207 208 /* Get a page from the cache. */ 209 if ((bp = mpool_bkt(mp)) == NULL) 210 return (NULL); 211 212 /* Read in the contents. */ 213 off = mp->pagesize * pgno; 214 if ((nr = pread(mp->fd, bp->page, mp->pagesize, off)) != mp->pagesize) { 215 switch (nr) { 216 case -1: 217 /* errno is set for us by pread(). */ 218 free(bp); 219 mp->curcache--; 220 return (NULL); 221 case 0: 222 /* 223 * A zero-length read means you need to create a 224 * new page. 225 */ 226 memset(bp->page, 0, mp->pagesize); 227 break; 228 default: 229 /* A partial read is definitely bad. */ 230 free(bp); 231 mp->curcache--; 232 errno = EINVAL; 233 return (NULL); 234 } 235 } 236 #ifdef STATISTICS 237 ++mp->pageread; 238 #endif 239 240 /* Set the page number, pin the page. */ 241 bp->pgno = pgno; 242 if (!(flags & MPOOL_IGNOREPIN)) 243 bp->flags = MPOOL_PINNED; 244 bp->flags |= MPOOL_INUSE; 245 246 /* 247 * Add the page to the head of the hash chain and the tail 248 * of the lru chain. 249 */ 250 head = &mp->hqh[HASHKEY(bp->pgno)]; 251 CIRCLEQ_INSERT_HEAD(head, bp, hq); 252 CIRCLEQ_INSERT_TAIL(&mp->lqh, bp, q); 253 254 /* Run through the user's filter. */ 255 if (mp->pgin != NULL) 256 (mp->pgin)(mp->pgcookie, bp->pgno, bp->page); 257 258 return (bp->page); 259 } 260 261 /* 262 * mpool_put 263 * Return a page. 264 */ 265 /* ARGSUSED */ 266 int 267 mpool_put(MPOOL *mp, void *page, u_int flags) 268 { 269 BKT *bp; 270 271 #ifdef STATISTICS 272 ++mp->pageput; 273 #endif 274 bp = (BKT *)((char *)page - sizeof(BKT)); 275 #ifdef DEBUG 276 if (!(bp->flags & MPOOL_PINNED)) { 277 (void)fprintf(stderr, 278 "mpool_put: page %d not pinned\n", bp->pgno); 279 abort(); 280 } 281 #endif 282 bp->flags &= ~MPOOL_PINNED; 283 if (flags & MPOOL_DIRTY) 284 bp->flags |= flags & MPOOL_DIRTY; 285 return (RET_SUCCESS); 286 } 287 288 /* 289 * mpool_close 290 * Close the buffer pool. 291 */ 292 int 293 mpool_close(MPOOL *mp) 294 { 295 BKT *bp; 296 297 /* Free up any space allocated to the lru pages. */ 298 while (!CIRCLEQ_EMPTY(&mp->lqh)) { 299 bp = CIRCLEQ_FIRST(&mp->lqh); 300 CIRCLEQ_REMOVE(&mp->lqh, bp, q); 301 free(bp); 302 } 303 304 /* Free the MPOOL cookie. */ 305 free(mp); 306 return (RET_SUCCESS); 307 } 308 309 /* 310 * mpool_sync 311 * Sync the pool to disk. 312 */ 313 int 314 mpool_sync(MPOOL *mp) 315 { 316 BKT *bp; 317 318 /* Walk the lru chain, flushing any dirty pages to disk. */ 319 CIRCLEQ_FOREACH(bp, &mp->lqh, q) 320 if (bp->flags & MPOOL_DIRTY && 321 mpool_write(mp, bp) == RET_ERROR) 322 return (RET_ERROR); 323 324 /* Sync the file descriptor. */ 325 return (fsync(mp->fd) ? RET_ERROR : RET_SUCCESS); 326 } 327 328 /* 329 * mpool_bkt 330 * Get a page from the cache (or create one). 331 */ 332 static BKT * 333 mpool_bkt(MPOOL *mp) 334 { 335 struct _hqh *head; 336 BKT *bp; 337 338 /* If under the max cached, always create a new page. */ 339 if (mp->curcache < mp->maxcache) 340 goto new; 341 342 /* 343 * If the cache is max'd out, walk the lru list for a buffer we 344 * can flush. If we find one, write it (if necessary) and take it 345 * off any lists. If we don't find anything we grow the cache anyway. 346 * The cache never shrinks. 347 */ 348 CIRCLEQ_FOREACH(bp, &mp->lqh, q) 349 if (!(bp->flags & MPOOL_PINNED)) { 350 /* Flush if dirty. */ 351 if (bp->flags & MPOOL_DIRTY && 352 mpool_write(mp, bp) == RET_ERROR) 353 return (NULL); 354 #ifdef STATISTICS 355 ++mp->pageflush; 356 #endif 357 /* Remove from the hash and lru queues. */ 358 head = &mp->hqh[HASHKEY(bp->pgno)]; 359 CIRCLEQ_REMOVE(head, bp, hq); 360 CIRCLEQ_REMOVE(&mp->lqh, bp, q); 361 #ifdef DEBUG 362 { void *spage; 363 spage = bp->page; 364 memset(bp, 0xff, sizeof(BKT) + mp->pagesize); 365 bp->page = spage; 366 } 367 #endif 368 bp->flags = 0; 369 return (bp); 370 } 371 372 new: if ((bp = (BKT *)malloc(sizeof(BKT) + mp->pagesize)) == NULL) 373 return (NULL); 374 #ifdef STATISTICS 375 ++mp->pagealloc; 376 #endif 377 memset(bp, 0xff, sizeof(BKT) + mp->pagesize); 378 bp->page = (char *)bp + sizeof(BKT); 379 bp->flags = 0; 380 ++mp->curcache; 381 return (bp); 382 } 383 384 /* 385 * mpool_write 386 * Write a page to disk. 387 */ 388 static int 389 mpool_write(MPOOL *mp, BKT *bp) 390 { 391 off_t off; 392 393 #ifdef STATISTICS 394 ++mp->pagewrite; 395 #endif 396 397 /* Run through the user's filter. */ 398 if (mp->pgout) 399 (mp->pgout)(mp->pgcookie, bp->pgno, bp->page); 400 401 off = mp->pagesize * bp->pgno; 402 if (pwrite(mp->fd, bp->page, mp->pagesize, off) != mp->pagesize) 403 return (RET_ERROR); 404 405 /* 406 * Re-run through the input filter since this page may soon be 407 * accessed via the cache, and whatever the user's output filter 408 * did may screw things up if we don't let the input filter 409 * restore the in-core copy. 410 */ 411 if (mp->pgin) 412 (mp->pgin)(mp->pgcookie, bp->pgno, bp->page); 413 414 bp->flags &= ~MPOOL_DIRTY; 415 return (RET_SUCCESS); 416 } 417 418 /* 419 * mpool_look 420 * Lookup a page in the cache. 421 */ 422 static BKT * 423 mpool_look(MPOOL *mp, pgno_t pgno) 424 { 425 struct _hqh *head; 426 BKT *bp; 427 428 head = &mp->hqh[HASHKEY(pgno)]; 429 CIRCLEQ_FOREACH(bp, head, hq) 430 if ((bp->pgno == pgno) && 431 ((bp->flags & MPOOL_INUSE) == MPOOL_INUSE)) { 432 #ifdef STATISTICS 433 ++mp->cachehit; 434 #endif 435 return (bp); 436 } 437 #ifdef STATISTICS 438 ++mp->cachemiss; 439 #endif 440 return (NULL); 441 } 442 443 #ifdef STATISTICS 444 /* 445 * mpool_stat 446 * Print out cache statistics. 447 */ 448 void 449 mpool_stat(MPOOL *mp) 450 { 451 BKT *bp; 452 int cnt; 453 char *sep; 454 455 (void)fprintf(stderr, "%lu pages in the file\n", mp->npages); 456 (void)fprintf(stderr, 457 "page size %lu, cacheing %lu pages of %lu page max cache\n", 458 mp->pagesize, mp->curcache, mp->maxcache); 459 (void)fprintf(stderr, "%lu page puts, %lu page gets, %lu page new\n", 460 mp->pageput, mp->pageget, mp->pagenew); 461 (void)fprintf(stderr, "%lu page allocs, %lu page flushes\n", 462 mp->pagealloc, mp->pageflush); 463 if (mp->cachehit + mp->cachemiss) 464 (void)fprintf(stderr, 465 "%.0f%% cache hit rate (%lu hits, %lu misses)\n", 466 ((double)mp->cachehit / (mp->cachehit + mp->cachemiss)) 467 * 100, mp->cachehit, mp->cachemiss); 468 (void)fprintf(stderr, "%lu page reads, %lu page writes\n", 469 mp->pageread, mp->pagewrite); 470 471 sep = ""; 472 cnt = 0; 473 CIRCLEQ_FOREACH(bp, &mp->lqh, q) { 474 (void)fprintf(stderr, "%s%d", sep, bp->pgno); 475 if (bp->flags & MPOOL_DIRTY) 476 (void)fprintf(stderr, "d"); 477 if (bp->flags & MPOOL_PINNED) 478 (void)fprintf(stderr, "P"); 479 if (++cnt == 10) { 480 sep = "\n"; 481 cnt = 0; 482 } else 483 sep = ", "; 484 485 } 486 (void)fprintf(stderr, "\n"); 487 } 488 #endif 489