1 /* $OpenBSD: mpool.c,v 1.19 2013/12/02 02:28:21 krw 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 TAILQ_INIT(&mp->lqh); 80 for (entry = 0; entry < HASHSIZE; ++entry) 81 TAILQ_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 TAILQ_INSERT_HEAD(head, bp, hq); 136 TAILQ_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 TAILQ_REMOVE(head, bp, hq); 159 TAILQ_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 TAILQ_REMOVE(head, bp, hq); 199 TAILQ_INSERT_HEAD(head, bp, hq); 200 TAILQ_REMOVE(&mp->lqh, bp, q); 201 TAILQ_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 TAILQ_INSERT_HEAD(head, bp, hq); 252 TAILQ_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 ((bp = TAILQ_FIRST(&mp->lqh))) { 299 TAILQ_REMOVE(&mp->lqh, bp, q); 300 free(bp); 301 } 302 303 /* Free the MPOOL cookie. */ 304 free(mp); 305 return (RET_SUCCESS); 306 } 307 308 /* 309 * mpool_sync 310 * Sync the pool to disk. 311 */ 312 int 313 mpool_sync(MPOOL *mp) 314 { 315 BKT *bp; 316 317 /* Walk the lru chain, flushing any dirty pages to disk. */ 318 TAILQ_FOREACH(bp, &mp->lqh, q) 319 if (bp->flags & MPOOL_DIRTY && 320 mpool_write(mp, bp) == RET_ERROR) 321 return (RET_ERROR); 322 323 /* Sync the file descriptor. */ 324 return (fsync(mp->fd) ? RET_ERROR : RET_SUCCESS); 325 } 326 327 /* 328 * mpool_bkt 329 * Get a page from the cache (or create one). 330 */ 331 static BKT * 332 mpool_bkt(MPOOL *mp) 333 { 334 struct _hqh *head; 335 BKT *bp; 336 337 /* If under the max cached, always create a new page. */ 338 if (mp->curcache < mp->maxcache) 339 goto new; 340 341 /* 342 * If the cache is max'd out, walk the lru list for a buffer we 343 * can flush. If we find one, write it (if necessary) and take it 344 * off any lists. If we don't find anything we grow the cache anyway. 345 * The cache never shrinks. 346 */ 347 TAILQ_FOREACH(bp, &mp->lqh, q) 348 if (!(bp->flags & MPOOL_PINNED)) { 349 /* Flush if dirty. */ 350 if (bp->flags & MPOOL_DIRTY && 351 mpool_write(mp, bp) == RET_ERROR) 352 return (NULL); 353 #ifdef STATISTICS 354 ++mp->pageflush; 355 #endif 356 /* Remove from the hash and lru queues. */ 357 head = &mp->hqh[HASHKEY(bp->pgno)]; 358 TAILQ_REMOVE(head, bp, hq); 359 TAILQ_REMOVE(&mp->lqh, bp, q); 360 #ifdef DEBUG 361 { void *spage; 362 spage = bp->page; 363 memset(bp, 0xff, sizeof(BKT) + mp->pagesize); 364 bp->page = spage; 365 } 366 #endif 367 bp->flags = 0; 368 return (bp); 369 } 370 371 new: if ((bp = (BKT *)malloc(sizeof(BKT) + mp->pagesize)) == NULL) 372 return (NULL); 373 #ifdef STATISTICS 374 ++mp->pagealloc; 375 #endif 376 memset(bp, 0xff, sizeof(BKT) + mp->pagesize); 377 bp->page = (char *)bp + sizeof(BKT); 378 bp->flags = 0; 379 ++mp->curcache; 380 return (bp); 381 } 382 383 /* 384 * mpool_write 385 * Write a page to disk. 386 */ 387 static int 388 mpool_write(MPOOL *mp, BKT *bp) 389 { 390 off_t off; 391 392 #ifdef STATISTICS 393 ++mp->pagewrite; 394 #endif 395 396 /* Run through the user's filter. */ 397 if (mp->pgout) 398 (mp->pgout)(mp->pgcookie, bp->pgno, bp->page); 399 400 off = mp->pagesize * bp->pgno; 401 if (pwrite(mp->fd, bp->page, mp->pagesize, off) != mp->pagesize) 402 return (RET_ERROR); 403 404 /* 405 * Re-run through the input filter since this page may soon be 406 * accessed via the cache, and whatever the user's output filter 407 * did may screw things up if we don't let the input filter 408 * restore the in-core copy. 409 */ 410 if (mp->pgin) 411 (mp->pgin)(mp->pgcookie, bp->pgno, bp->page); 412 413 bp->flags &= ~MPOOL_DIRTY; 414 return (RET_SUCCESS); 415 } 416 417 /* 418 * mpool_look 419 * Lookup a page in the cache. 420 */ 421 static BKT * 422 mpool_look(MPOOL *mp, pgno_t pgno) 423 { 424 struct _hqh *head; 425 BKT *bp; 426 427 head = &mp->hqh[HASHKEY(pgno)]; 428 TAILQ_FOREACH(bp, head, hq) 429 if ((bp->pgno == pgno) && 430 ((bp->flags & MPOOL_INUSE) == MPOOL_INUSE)) { 431 #ifdef STATISTICS 432 ++mp->cachehit; 433 #endif 434 return (bp); 435 } 436 #ifdef STATISTICS 437 ++mp->cachemiss; 438 #endif 439 return (NULL); 440 } 441 442 #ifdef STATISTICS 443 /* 444 * mpool_stat 445 * Print out cache statistics. 446 */ 447 void 448 mpool_stat(MPOOL *mp) 449 { 450 BKT *bp; 451 int cnt; 452 char *sep; 453 454 (void)fprintf(stderr, "%lu pages in the file\n", mp->npages); 455 (void)fprintf(stderr, 456 "page size %lu, cacheing %lu pages of %lu page max cache\n", 457 mp->pagesize, mp->curcache, mp->maxcache); 458 (void)fprintf(stderr, "%lu page puts, %lu page gets, %lu page new\n", 459 mp->pageput, mp->pageget, mp->pagenew); 460 (void)fprintf(stderr, "%lu page allocs, %lu page flushes\n", 461 mp->pagealloc, mp->pageflush); 462 if (mp->cachehit + mp->cachemiss) 463 (void)fprintf(stderr, 464 "%.0f%% cache hit rate (%lu hits, %lu misses)\n", 465 ((double)mp->cachehit / (mp->cachehit + mp->cachemiss)) 466 * 100, mp->cachehit, mp->cachemiss); 467 (void)fprintf(stderr, "%lu page reads, %lu page writes\n", 468 mp->pageread, mp->pagewrite); 469 470 sep = ""; 471 cnt = 0; 472 TAILQ_FOREACH(bp, &mp->lqh, q) { 473 (void)fprintf(stderr, "%s%d", sep, bp->pgno); 474 if (bp->flags & MPOOL_DIRTY) 475 (void)fprintf(stderr, "d"); 476 if (bp->flags & MPOOL_PINNED) 477 (void)fprintf(stderr, "P"); 478 if (++cnt == 10) { 479 sep = "\n"; 480 cnt = 0; 481 } else 482 sep = ", "; 483 484 } 485 (void)fprintf(stderr, "\n"); 486 } 487 #endif 488