1 /* 2 ** 2008 August 05 3 ** 4 ** The author disclaims copyright to this source code. In place of 5 ** a legal notice, here is a blessing: 6 ** 7 ** May you do good and not evil. 8 ** May you find forgiveness for yourself and forgive others. 9 ** May you share freely, never taking more than you give. 10 ** 11 ************************************************************************* 12 ** This file implements that page cache. 13 */ 14 #include "sqliteInt.h" 15 16 /* 17 ** A complete page cache is an instance of this structure. 18 */ 19 struct PCache { 20 PgHdr *pDirty, *pDirtyTail; /* List of dirty pages in LRU order */ 21 PgHdr *pSynced; /* Last synced page in dirty page list */ 22 int nRef; /* Number of referenced pages */ 23 int szCache; /* Configured cache size */ 24 int szPage; /* Size of every page in this cache */ 25 int szExtra; /* Size of extra space for each page */ 26 u8 bPurgeable; /* True if pages are on backing store */ 27 u8 eCreate; /* eCreate value for for xFetch() */ 28 int (*xStress)(void*,PgHdr*); /* Call to try make a page clean */ 29 void *pStress; /* Argument to xStress */ 30 sqlite3_pcache *pCache; /* Pluggable cache module */ 31 PgHdr *pPage1; /* Reference to page 1 */ 32 }; 33 34 /* 35 ** Some of the assert() macros in this code are too expensive to run 36 ** even during normal debugging. Use them only rarely on long-running 37 ** tests. Enable the expensive asserts using the 38 ** -DSQLITE_ENABLE_EXPENSIVE_ASSERT=1 compile-time option. 39 */ 40 #ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT 41 # define expensive_assert(X) assert(X) 42 #else 43 # define expensive_assert(X) 44 #endif 45 46 /********************************** Linked List Management ********************/ 47 48 #if !defined(NDEBUG) && defined(SQLITE_ENABLE_EXPENSIVE_ASSERT) 49 /* 50 ** Check that the pCache->pSynced variable is set correctly. If it 51 ** is not, either fail an assert or return zero. Otherwise, return 52 ** non-zero. This is only used in debugging builds, as follows: 53 ** 54 ** expensive_assert( pcacheCheckSynced(pCache) ); 55 */ 56 static int pcacheCheckSynced(PCache *pCache){ 57 PgHdr *p; 58 for(p=pCache->pDirtyTail; p!=pCache->pSynced; p=p->pDirtyPrev){ 59 assert( p->nRef || (p->flags&PGHDR_NEED_SYNC) ); 60 } 61 return (p==0 || p->nRef || (p->flags&PGHDR_NEED_SYNC)==0); 62 } 63 #endif /* !NDEBUG && SQLITE_ENABLE_EXPENSIVE_ASSERT */ 64 65 /* 66 ** Remove page pPage from the list of dirty pages. 67 */ 68 static void pcacheRemoveFromDirtyList(PgHdr *pPage){ 69 PCache *p = pPage->pCache; 70 71 assert( pPage->pDirtyNext || pPage==p->pDirtyTail ); 72 assert( pPage->pDirtyPrev || pPage==p->pDirty ); 73 74 /* Update the PCache1.pSynced variable if necessary. */ 75 if( p->pSynced==pPage ){ 76 PgHdr *pSynced = pPage->pDirtyPrev; 77 while( pSynced && (pSynced->flags&PGHDR_NEED_SYNC) ){ 78 pSynced = pSynced->pDirtyPrev; 79 } 80 p->pSynced = pSynced; 81 } 82 83 if( pPage->pDirtyNext ){ 84 pPage->pDirtyNext->pDirtyPrev = pPage->pDirtyPrev; 85 }else{ 86 assert( pPage==p->pDirtyTail ); 87 p->pDirtyTail = pPage->pDirtyPrev; 88 } 89 if( pPage->pDirtyPrev ){ 90 pPage->pDirtyPrev->pDirtyNext = pPage->pDirtyNext; 91 }else{ 92 assert( pPage==p->pDirty ); 93 p->pDirty = pPage->pDirtyNext; 94 if( p->pDirty==0 && p->bPurgeable ){ 95 assert( p->eCreate==1 ); 96 p->eCreate = 2; 97 } 98 } 99 pPage->pDirtyNext = 0; 100 pPage->pDirtyPrev = 0; 101 102 expensive_assert( pcacheCheckSynced(p) ); 103 } 104 105 /* 106 ** Add page pPage to the head of the dirty list (PCache1.pDirty is set to 107 ** pPage). 108 */ 109 static void pcacheAddToDirtyList(PgHdr *pPage){ 110 PCache *p = pPage->pCache; 111 112 assert( pPage->pDirtyNext==0 && pPage->pDirtyPrev==0 && p->pDirty!=pPage ); 113 114 pPage->pDirtyNext = p->pDirty; 115 if( pPage->pDirtyNext ){ 116 assert( pPage->pDirtyNext->pDirtyPrev==0 ); 117 pPage->pDirtyNext->pDirtyPrev = pPage; 118 }else if( p->bPurgeable ){ 119 assert( p->eCreate==2 ); 120 p->eCreate = 1; 121 } 122 p->pDirty = pPage; 123 if( !p->pDirtyTail ){ 124 p->pDirtyTail = pPage; 125 } 126 if( !p->pSynced && 0==(pPage->flags&PGHDR_NEED_SYNC) ){ 127 p->pSynced = pPage; 128 } 129 expensive_assert( pcacheCheckSynced(p) ); 130 } 131 132 /* 133 ** Wrapper around the pluggable caches xUnpin method. If the cache is 134 ** being used for an in-memory database, this function is a no-op. 135 */ 136 static void pcacheUnpin(PgHdr *p){ 137 PCache *pCache = p->pCache; 138 if( pCache->bPurgeable ){ 139 if( p->pgno==1 ){ 140 pCache->pPage1 = 0; 141 } 142 sqlite3GlobalConfig.pcache2.xUnpin(pCache->pCache, p->pPage, 0); 143 } 144 } 145 146 /*************************************************** General Interfaces ****** 147 ** 148 ** Initialize and shutdown the page cache subsystem. Neither of these 149 ** functions are threadsafe. 150 */ 151 int sqlite3PcacheInitialize(void){ 152 if( sqlite3GlobalConfig.pcache2.xInit==0 ){ 153 /* IMPLEMENTATION-OF: R-26801-64137 If the xInit() method is NULL, then the 154 ** built-in default page cache is used instead of the application defined 155 ** page cache. */ 156 sqlite3PCacheSetDefault(); 157 } 158 return sqlite3GlobalConfig.pcache2.xInit(sqlite3GlobalConfig.pcache2.pArg); 159 } 160 void sqlite3PcacheShutdown(void){ 161 if( sqlite3GlobalConfig.pcache2.xShutdown ){ 162 /* IMPLEMENTATION-OF: R-26000-56589 The xShutdown() method may be NULL. */ 163 sqlite3GlobalConfig.pcache2.xShutdown(sqlite3GlobalConfig.pcache2.pArg); 164 } 165 } 166 167 /* 168 ** Return the size in bytes of a PCache object. 169 */ 170 int sqlite3PcacheSize(void){ return sizeof(PCache); } 171 172 /* 173 ** Create a new PCache object. Storage space to hold the object 174 ** has already been allocated and is passed in as the p pointer. 175 ** The caller discovers how much space needs to be allocated by 176 ** calling sqlite3PcacheSize(). 177 */ 178 void sqlite3PcacheOpen( 179 int szPage, /* Size of every page */ 180 int szExtra, /* Extra space associated with each page */ 181 int bPurgeable, /* True if pages are on backing store */ 182 int (*xStress)(void*,PgHdr*),/* Call to try to make pages clean */ 183 void *pStress, /* Argument to xStress */ 184 PCache *p /* Preallocated space for the PCache */ 185 ){ 186 memset(p, 0, sizeof(PCache)); 187 p->szPage = szPage; 188 p->szExtra = szExtra; 189 p->bPurgeable = bPurgeable; 190 p->eCreate = 2; 191 p->xStress = xStress; 192 p->pStress = pStress; 193 p->szCache = 100; 194 } 195 196 /* 197 ** Change the page size for PCache object. The caller must ensure that there 198 ** are no outstanding page references when this function is called. 199 */ 200 void sqlite3PcacheSetPageSize(PCache *pCache, int szPage){ 201 assert( pCache->nRef==0 && pCache->pDirty==0 ); 202 if( pCache->pCache ){ 203 sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache); 204 pCache->pCache = 0; 205 pCache->pPage1 = 0; 206 } 207 pCache->szPage = szPage; 208 } 209 210 /* 211 ** Compute the number of pages of cache requested. 212 */ 213 static int numberOfCachePages(PCache *p){ 214 if( p->szCache>=0 ){ 215 return p->szCache; 216 }else{ 217 return (int)((-1024*(i64)p->szCache)/(p->szPage+p->szExtra)); 218 } 219 } 220 221 /* 222 ** Try to obtain a page from the cache. 223 */ 224 int sqlite3PcacheFetch( 225 PCache *pCache, /* Obtain the page from this cache */ 226 Pgno pgno, /* Page number to obtain */ 227 int createFlag, /* If true, create page if it does not exist already */ 228 PgHdr **ppPage /* Write the page here */ 229 ){ 230 sqlite3_pcache_page *pPage; 231 PgHdr *pPgHdr = 0; 232 int eCreate; 233 234 assert( pCache!=0 ); 235 assert( createFlag==1 || createFlag==0 ); 236 assert( pgno>0 ); 237 238 /* If the pluggable cache (sqlite3_pcache*) has not been allocated, 239 ** allocate it now. 240 */ 241 if( !pCache->pCache ){ 242 sqlite3_pcache *p; 243 if( !createFlag ){ 244 *ppPage = 0; 245 return SQLITE_OK; 246 } 247 p = sqlite3GlobalConfig.pcache2.xCreate( 248 pCache->szPage, pCache->szExtra + sizeof(PgHdr), pCache->bPurgeable 249 ); 250 if( !p ){ 251 return SQLITE_NOMEM; 252 } 253 sqlite3GlobalConfig.pcache2.xCachesize(p, numberOfCachePages(pCache)); 254 pCache->pCache = p; 255 } 256 257 /* eCreate defines what to do if the page does not exist. 258 ** 0 Do not allocate a new page. (createFlag==0) 259 ** 1 Allocate a new page if doing so is inexpensive. 260 ** (createFlag==1 AND bPurgeable AND pDirty) 261 ** 2 Allocate a new page even it doing so is difficult. 262 ** (createFlag==1 AND !(bPurgeable AND pDirty) 263 */ 264 eCreate = createFlag==0 ? 0 : pCache->eCreate; 265 assert( (createFlag*(1+(!pCache->bPurgeable||!pCache->pDirty)))==eCreate ); 266 pPage = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, eCreate); 267 if( !pPage && eCreate==1 ){ 268 PgHdr *pPg; 269 270 /* Find a dirty page to write-out and recycle. First try to find a 271 ** page that does not require a journal-sync (one with PGHDR_NEED_SYNC 272 ** cleared), but if that is not possible settle for any other 273 ** unreferenced dirty page. 274 */ 275 expensive_assert( pcacheCheckSynced(pCache) ); 276 for(pPg=pCache->pSynced; 277 pPg && (pPg->nRef || (pPg->flags&PGHDR_NEED_SYNC)); 278 pPg=pPg->pDirtyPrev 279 ); 280 pCache->pSynced = pPg; 281 if( !pPg ){ 282 for(pPg=pCache->pDirtyTail; pPg && pPg->nRef; pPg=pPg->pDirtyPrev); 283 } 284 if( pPg ){ 285 int rc; 286 #ifdef SQLITE_LOG_CACHE_SPILL 287 sqlite3_log(SQLITE_FULL, 288 "spill page %d making room for %d - cache used: %d/%d", 289 pPg->pgno, pgno, 290 sqlite3GlobalConfig.pcache.xPagecount(pCache->pCache), 291 numberOfCachePages(pCache)); 292 #endif 293 rc = pCache->xStress(pCache->pStress, pPg); 294 if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){ 295 return rc; 296 } 297 } 298 299 pPage = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, 2); 300 } 301 302 if( pPage ){ 303 pPgHdr = (PgHdr *)pPage->pExtra; 304 305 if( !pPgHdr->pPage ){ 306 memset(pPgHdr, 0, sizeof(PgHdr)); 307 pPgHdr->pPage = pPage; 308 pPgHdr->pData = pPage->pBuf; 309 pPgHdr->pExtra = (void *)&pPgHdr[1]; 310 memset(pPgHdr->pExtra, 0, pCache->szExtra); 311 pPgHdr->pCache = pCache; 312 pPgHdr->pgno = pgno; 313 } 314 assert( pPgHdr->pCache==pCache ); 315 assert( pPgHdr->pgno==pgno ); 316 assert( pPgHdr->pData==pPage->pBuf ); 317 assert( pPgHdr->pExtra==(void *)&pPgHdr[1] ); 318 319 if( 0==pPgHdr->nRef ){ 320 pCache->nRef++; 321 } 322 pPgHdr->nRef++; 323 if( pgno==1 ){ 324 pCache->pPage1 = pPgHdr; 325 } 326 } 327 *ppPage = pPgHdr; 328 return (pPgHdr==0 && eCreate) ? SQLITE_NOMEM : SQLITE_OK; 329 } 330 331 /* 332 ** Decrement the reference count on a page. If the page is clean and the 333 ** reference count drops to 0, then it is made elible for recycling. 334 */ 335 void sqlite3PcacheRelease(PgHdr *p){ 336 assert( p->nRef>0 ); 337 p->nRef--; 338 if( p->nRef==0 ){ 339 PCache *pCache = p->pCache; 340 pCache->nRef--; 341 if( (p->flags&PGHDR_DIRTY)==0 ){ 342 pcacheUnpin(p); 343 }else{ 344 /* Move the page to the head of the dirty list. */ 345 pcacheRemoveFromDirtyList(p); 346 pcacheAddToDirtyList(p); 347 } 348 } 349 } 350 351 /* 352 ** Increase the reference count of a supplied page by 1. 353 */ 354 void sqlite3PcacheRef(PgHdr *p){ 355 assert(p->nRef>0); 356 p->nRef++; 357 } 358 359 /* 360 ** Drop a page from the cache. There must be exactly one reference to the 361 ** page. This function deletes that reference, so after it returns the 362 ** page pointed to by p is invalid. 363 */ 364 void sqlite3PcacheDrop(PgHdr *p){ 365 PCache *pCache; 366 assert( p->nRef==1 ); 367 if( p->flags&PGHDR_DIRTY ){ 368 pcacheRemoveFromDirtyList(p); 369 } 370 pCache = p->pCache; 371 pCache->nRef--; 372 if( p->pgno==1 ){ 373 pCache->pPage1 = 0; 374 } 375 sqlite3GlobalConfig.pcache2.xUnpin(pCache->pCache, p->pPage, 1); 376 } 377 378 /* 379 ** Make sure the page is marked as dirty. If it isn't dirty already, 380 ** make it so. 381 */ 382 void sqlite3PcacheMakeDirty(PgHdr *p){ 383 p->flags &= ~PGHDR_DONT_WRITE; 384 assert( p->nRef>0 ); 385 if( 0==(p->flags & PGHDR_DIRTY) ){ 386 p->flags |= PGHDR_DIRTY; 387 pcacheAddToDirtyList( p); 388 } 389 } 390 391 /* 392 ** Make sure the page is marked as clean. If it isn't clean already, 393 ** make it so. 394 */ 395 void sqlite3PcacheMakeClean(PgHdr *p){ 396 if( (p->flags & PGHDR_DIRTY) ){ 397 pcacheRemoveFromDirtyList(p); 398 p->flags &= ~(PGHDR_DIRTY|PGHDR_NEED_SYNC); 399 if( p->nRef==0 ){ 400 pcacheUnpin(p); 401 } 402 } 403 } 404 405 /* 406 ** Make every page in the cache clean. 407 */ 408 void sqlite3PcacheCleanAll(PCache *pCache){ 409 PgHdr *p; 410 while( (p = pCache->pDirty)!=0 ){ 411 sqlite3PcacheMakeClean(p); 412 } 413 } 414 415 /* 416 ** Clear the PGHDR_NEED_SYNC flag from all dirty pages. 417 */ 418 void sqlite3PcacheClearSyncFlags(PCache *pCache){ 419 PgHdr *p; 420 for(p=pCache->pDirty; p; p=p->pDirtyNext){ 421 p->flags &= ~PGHDR_NEED_SYNC; 422 } 423 pCache->pSynced = pCache->pDirtyTail; 424 } 425 426 /* 427 ** Change the page number of page p to newPgno. 428 */ 429 void sqlite3PcacheMove(PgHdr *p, Pgno newPgno){ 430 PCache *pCache = p->pCache; 431 assert( p->nRef>0 ); 432 assert( newPgno>0 ); 433 sqlite3GlobalConfig.pcache2.xRekey(pCache->pCache, p->pPage, p->pgno,newPgno); 434 p->pgno = newPgno; 435 if( (p->flags&PGHDR_DIRTY) && (p->flags&PGHDR_NEED_SYNC) ){ 436 pcacheRemoveFromDirtyList(p); 437 pcacheAddToDirtyList(p); 438 } 439 } 440 441 /* 442 ** Drop every cache entry whose page number is greater than "pgno". The 443 ** caller must ensure that there are no outstanding references to any pages 444 ** other than page 1 with a page number greater than pgno. 445 ** 446 ** If there is a reference to page 1 and the pgno parameter passed to this 447 ** function is 0, then the data area associated with page 1 is zeroed, but 448 ** the page object is not dropped. 449 */ 450 void sqlite3PcacheTruncate(PCache *pCache, Pgno pgno){ 451 if( pCache->pCache ){ 452 PgHdr *p; 453 PgHdr *pNext; 454 for(p=pCache->pDirty; p; p=pNext){ 455 pNext = p->pDirtyNext; 456 /* This routine never gets call with a positive pgno except right 457 ** after sqlite3PcacheCleanAll(). So if there are dirty pages, 458 ** it must be that pgno==0. 459 */ 460 assert( p->pgno>0 ); 461 if( ALWAYS(p->pgno>pgno) ){ 462 assert( p->flags&PGHDR_DIRTY ); 463 sqlite3PcacheMakeClean(p); 464 } 465 } 466 if( pgno==0 && pCache->pPage1 ){ 467 memset(pCache->pPage1->pData, 0, pCache->szPage); 468 pgno = 1; 469 } 470 sqlite3GlobalConfig.pcache2.xTruncate(pCache->pCache, pgno+1); 471 } 472 } 473 474 /* 475 ** Close a cache. 476 */ 477 void sqlite3PcacheClose(PCache *pCache){ 478 if( pCache->pCache ){ 479 sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache); 480 } 481 } 482 483 /* 484 ** Discard the contents of the cache. 485 */ 486 void sqlite3PcacheClear(PCache *pCache){ 487 sqlite3PcacheTruncate(pCache, 0); 488 } 489 490 /* 491 ** Merge two lists of pages connected by pDirty and in pgno order. 492 ** Do not both fixing the pDirtyPrev pointers. 493 */ 494 static PgHdr *pcacheMergeDirtyList(PgHdr *pA, PgHdr *pB){ 495 PgHdr result, *pTail; 496 pTail = &result; 497 while( pA && pB ){ 498 if( pA->pgno<pB->pgno ){ 499 pTail->pDirty = pA; 500 pTail = pA; 501 pA = pA->pDirty; 502 }else{ 503 pTail->pDirty = pB; 504 pTail = pB; 505 pB = pB->pDirty; 506 } 507 } 508 if( pA ){ 509 pTail->pDirty = pA; 510 }else if( pB ){ 511 pTail->pDirty = pB; 512 }else{ 513 pTail->pDirty = 0; 514 } 515 return result.pDirty; 516 } 517 518 /* 519 ** Sort the list of pages in accending order by pgno. Pages are 520 ** connected by pDirty pointers. The pDirtyPrev pointers are 521 ** corrupted by this sort. 522 ** 523 ** Since there cannot be more than 2^31 distinct pages in a database, 524 ** there cannot be more than 31 buckets required by the merge sorter. 525 ** One extra bucket is added to catch overflow in case something 526 ** ever changes to make the previous sentence incorrect. 527 */ 528 #define N_SORT_BUCKET 32 529 static PgHdr *pcacheSortDirtyList(PgHdr *pIn){ 530 PgHdr *a[N_SORT_BUCKET], *p; 531 int i; 532 memset(a, 0, sizeof(a)); 533 while( pIn ){ 534 p = pIn; 535 pIn = p->pDirty; 536 p->pDirty = 0; 537 for(i=0; ALWAYS(i<N_SORT_BUCKET-1); i++){ 538 if( a[i]==0 ){ 539 a[i] = p; 540 break; 541 }else{ 542 p = pcacheMergeDirtyList(a[i], p); 543 a[i] = 0; 544 } 545 } 546 if( NEVER(i==N_SORT_BUCKET-1) ){ 547 /* To get here, there need to be 2^(N_SORT_BUCKET) elements in 548 ** the input list. But that is impossible. 549 */ 550 a[i] = pcacheMergeDirtyList(a[i], p); 551 } 552 } 553 p = a[0]; 554 for(i=1; i<N_SORT_BUCKET; i++){ 555 p = pcacheMergeDirtyList(p, a[i]); 556 } 557 return p; 558 } 559 560 /* 561 ** Return a list of all dirty pages in the cache, sorted by page number. 562 */ 563 PgHdr *sqlite3PcacheDirtyList(PCache *pCache){ 564 PgHdr *p; 565 for(p=pCache->pDirty; p; p=p->pDirtyNext){ 566 p->pDirty = p->pDirtyNext; 567 } 568 return pcacheSortDirtyList(pCache->pDirty); 569 } 570 571 /* 572 ** Return the total number of referenced pages held by the cache. 573 */ 574 int sqlite3PcacheRefCount(PCache *pCache){ 575 return pCache->nRef; 576 } 577 578 /* 579 ** Return the number of references to the page supplied as an argument. 580 */ 581 int sqlite3PcachePageRefcount(PgHdr *p){ 582 return p->nRef; 583 } 584 585 /* 586 ** Return the total number of pages in the cache. 587 */ 588 int sqlite3PcachePagecount(PCache *pCache){ 589 int nPage = 0; 590 if( pCache->pCache ){ 591 nPage = sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache); 592 } 593 return nPage; 594 } 595 596 #ifdef SQLITE_TEST 597 /* 598 ** Get the suggested cache-size value. 599 */ 600 int sqlite3PcacheGetCachesize(PCache *pCache){ 601 return numberOfCachePages(pCache); 602 } 603 #endif 604 605 /* 606 ** Set the suggested cache-size value. 607 */ 608 void sqlite3PcacheSetCachesize(PCache *pCache, int mxPage){ 609 pCache->szCache = mxPage; 610 if( pCache->pCache ){ 611 sqlite3GlobalConfig.pcache2.xCachesize(pCache->pCache, 612 numberOfCachePages(pCache)); 613 } 614 } 615 616 /* 617 ** Free up as much memory as possible from the page cache. 618 */ 619 void sqlite3PcacheShrink(PCache *pCache){ 620 if( pCache->pCache ){ 621 sqlite3GlobalConfig.pcache2.xShrink(pCache->pCache); 622 } 623 } 624 625 #if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG) 626 /* 627 ** For all dirty pages currently in the cache, invoke the specified 628 ** callback. This is only used if the SQLITE_CHECK_PAGES macro is 629 ** defined. 630 */ 631 void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *)){ 632 PgHdr *pDirty; 633 for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext){ 634 xIter(pDirty); 635 } 636 } 637 #endif 638