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