Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/sqlite/blob/master/src/pcache.c # sqlite [Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `sqlite / src / pcache.c` 936 lines ยท 31437 bytes Raw Blame History 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. Every 18 ** entry in the cache holds a single page of the database file. The 19 ** btree layer only operates on the cached copy of the database pages. 20 ** 21 ** A page cache entry is "clean" if it exactly matches what is currently 22 ** on disk. A page is "dirty" if it has been modified and needs to be 23 ** persisted to disk. 24 ** 25 ** pDirty, pDirtyTail, pSynced: 26 ** All dirty pages are linked into the doubly linked list using 27 ** PgHdr.pDirtyNext and pDirtyPrev. The list is maintained in LRU order 28 ** such that p was added to the list more recently than p->pDirtyNext. 29 ** PCache.pDirty points to the first (newest) element in the list and 30 ** pDirtyTail to the last (oldest). 31 ** 32 ** The PCache.pSynced variable is used to optimize searching for a dirty 33 ** page to eject from the cache mid-transaction. It is better to eject 34 ** a page that does not require a journal sync than one that does. 35 ** Therefore, pSynced is maintained so that it *almost* always points 36 ** to either the oldest page in the pDirty/pDirtyTail list that has a 37 ** clear PGHDR_NEED_SYNC flag or to a page that is older than this one 38 ** (so that the right page to eject can be found by following pDirtyPrev 39 ** pointers). 40 */ 41 struct PCache { 42 PgHdr *pDirty, *pDirtyTail; /* List of dirty pages in LRU order */ 43 PgHdr *pSynced; /* Last synced page in dirty page list */ 44 i64 nRefSum; /* Sum of ref counts over all pages */ 45 int szCache; /* Configured cache size */ 46 int szSpill; /* Size before spilling occurs */ 47 int szPage; /* Size of every page in this cache */ 48 int szExtra; /* Size of extra space for each page */ 49 u8 bPurgeable; /* True if pages are on backing store */ 50 u8 eCreate; /* eCreate value for for xFetch() */ 51 int (*xStress)(void*,PgHdr*); /* Call to try make a page clean */ 52 void *pStress; /* Argument to xStress */ 53 sqlite3_pcache *pCache; /* Pluggable cache module */ 54 }; 55 56 /********************************** Test and Debug Logic **********************/ 57 /* 58 ** Debug tracing macros. Enable by by changing the "0" to "1" and 59 ** recompiling. 60 ** 61 ** When sqlite3PcacheTrace is 1, single line trace messages are issued. 62 ** When sqlite3PcacheTrace is 2, a dump of the pcache showing all cache entries 63 ** is displayed for many operations, resulting in a lot of output. 64 */ 65 #if defined(SQLITE_DEBUG) && 0 66 int sqlite3PcacheTrace = 2; /* 0: off 1: simple 2: cache dumps */ 67 int sqlite3PcacheMxDump = 9999; /* Max cache entries for pcacheDump() */ 68 # define pcacheTrace(X) if(sqlite3PcacheTrace){sqlite3DebugPrintf X;} 69 static void pcachePageTrace(int i, sqlite3_pcache_page *pLower){ 70 PgHdr *pPg; 71 unsigned char *a; 72 int j; 73 if( pLower==0 ){ 74 printf("%3d: NULL\n", i); 75 }else{ 76 pPg = (PgHdr*)pLower->pExtra; 77 printf("%3d: nRef %2lld flgs %02x data ", i, pPg->nRef, pPg->flags); 78 a = (unsigned char *)pLower->pBuf; 79 for(j=0; j<12; j++) printf("%02x", a[j]); 80 printf(" ptr %p\n", pPg); 81 } 82 } 83 static void pcacheDump(PCache *pCache){ 84 int N; 85 int i; 86 sqlite3_pcache_page *pLower; 87 88 if( sqlite3PcacheTrace<2 ) return; 89 if( pCache->pCache==0 ) return; 90 N = sqlite3PcachePagecount(pCache); 91 if( N>sqlite3PcacheMxDump ) N = sqlite3PcacheMxDump; 92 for(i=1; i<=N; i++){ 93 pLower = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, i, 0); 94 pcachePageTrace(i, pLower); 95 if( pLower && ((PgHdr*)pLower)->pPage==0 ){ 96 sqlite3GlobalConfig.pcache2.xUnpin(pCache->pCache, pLower, 0); 97 } 98 } 99 } 100 #else 101 # define pcacheTrace(X) 102 # define pcachePageTrace(PGNO, X) 103 # define pcacheDump(X) 104 #endif 105 106 /* 107 ** Return 1 if pPg is on the dirty list for pCache. Return 0 if not. 108 ** This routine runs inside of assert() statements only. 109 */ 110 #if defined(SQLITE_ENABLE_EXPENSIVE_ASSERT) 111 static int pageOnDirtyList(PCache *pCache, PgHdr *pPg){ 112 PgHdr *p; 113 for(p=pCache->pDirty; p; p=p->pDirtyNext){ 114 if( p==pPg ) return 1; 115 } 116 return 0; 117 } 118 static int pageNotOnDirtyList(PCache *pCache, PgHdr *pPg){ 119 PgHdr *p; 120 for(p=pCache->pDirty; p; p=p->pDirtyNext){ 121 if( p==pPg ) return 0; 122 } 123 return 1; 124 } 125 #else 126 # define pageOnDirtyList(A,B) 1 127 # define pageNotOnDirtyList(A,B) 1 128 #endif 129 130 /* 131 ** Check invariants on a PgHdr entry. Return true if everything is OK. 132 ** Return false if any invariant is violated. 133 ** 134 ** This routine is for use inside of assert() statements only. For 135 ** example: 136 ** 137 ** assert( sqlite3PcachePageSanity(pPg) ); 138 */ 139 #ifdef SQLITE_DEBUG 140 int sqlite3PcachePageSanity(PgHdr *pPg){ 141 PCache *pCache; 142 assert( pPg!=0 ); 143 assert( pPg->pgno>0 || pPg->pPager==0 ); /* Page number is 1 or more */ 144 pCache = pPg->pCache; 145 assert( pCache!=0 ); /* Every page has an associated PCache */ 146 if( pPg->flags & PGHDR_CLEAN ){ 147 assert( (pPg->flags & PGHDR_DIRTY)==0 );/* Cannot be both CLEAN and DIRTY */ 148 assert( pageNotOnDirtyList(pCache, pPg) );/* CLEAN pages not on dirtylist */ 149 }else{ 150 assert( (pPg->flags & PGHDR_DIRTY)!=0 );/* If not CLEAN must be DIRTY */ 151 assert( pPg->pDirtyNext==0 || pPg->pDirtyNext->pDirtyPrev==pPg ); 152 assert( pPg->pDirtyPrev==0 || pPg->pDirtyPrev->pDirtyNext==pPg ); 153 assert( pPg->pDirtyPrev!=0 || pCache->pDirty==pPg ); 154 assert( pageOnDirtyList(pCache, pPg) ); 155 } 156 /* WRITEABLE pages must also be DIRTY */ 157 if( pPg->flags & PGHDR_WRITEABLE ){ 158 assert( pPg->flags & PGHDR_DIRTY ); /* WRITEABLE implies DIRTY */ 159 } 160 /* NEED_SYNC can be set independently of WRITEABLE. This can happen, 161 ** for example, when using the sqlite3PagerDontWrite() optimization: 162 ** (1) Page X is journalled, and gets WRITEABLE and NEED_SEEK. 163 ** (2) Page X moved to freelist, WRITEABLE is cleared 164 ** (3) Page X reused, WRITEABLE is set again 165 ** If NEED_SYNC had been cleared in step 2, then it would not be reset 166 ** in step 3, and page might be written into the database without first 167 ** syncing the rollback journal, which might cause corruption on a power 168 ** loss. 169 ** 170 ** Another example is when the database page size is smaller than the 171 ** disk sector size. When any page of a sector is journalled, all pages 172 ** in that sector are marked NEED_SYNC even if they are still CLEAN, just 173 ** in case they are later modified, since all pages in the same sector 174 ** must be journalled and synced before any of those pages can be safely 175 ** written. 176 */ 177 return 1; 178 } 179 #endif /* SQLITE_DEBUG */ 180 181 182 /********************************** Linked List Management ********************/ 183 184 /* Allowed values for second argument to pcacheManageDirtyList() */ 185 #define PCACHE_DIRTYLIST_REMOVE 1 /* Remove pPage from dirty list */ 186 #define PCACHE_DIRTYLIST_ADD 2 /* Add pPage to the dirty list */ 187 #define PCACHE_DIRTYLIST_FRONT 3 /* Move pPage to the front of the list */ 188 189 /* 190 ** Manage pPage's participation on the dirty list. Bits of the addRemove 191 ** argument determines what operation to do. The 0x01 bit means first 192 ** remove pPage from the dirty list. The 0x02 means add pPage back to 193 ** the dirty list. Doing both moves pPage to the front of the dirty list. 194 */ 195 static void pcacheManageDirtyList(PgHdr *pPage, u8 addRemove){ 196 PCache *p = pPage->pCache; 197 198 pcacheTrace(("%p.DIRTYLIST.%s %d\n", p, 199 addRemove==1 ? "REMOVE" : addRemove==2 ? "ADD" : "FRONT", 200 pPage->pgno)); 201 if( addRemove & PCACHE_DIRTYLIST_REMOVE ){ 202 assert( pPage->pDirtyNext || pPage==p->pDirtyTail ); 203 assert( pPage->pDirtyPrev || pPage==p->pDirty ); 204 205 /* Update the PCache1.pSynced variable if necessary. */ 206 if( p->pSynced==pPage ){ 207 p->pSynced = pPage->pDirtyPrev; 208 } 209 210 if( pPage->pDirtyNext ){ 211 pPage->pDirtyNext->pDirtyPrev = pPage->pDirtyPrev; 212 }else{ 213 assert( pPage==p->pDirtyTail ); 214 p->pDirtyTail = pPage->pDirtyPrev; 215 } 216 if( pPage->pDirtyPrev ){ 217 pPage->pDirtyPrev->pDirtyNext = pPage->pDirtyNext; 218 }else{ 219 /* If there are now no dirty pages in the cache, set eCreate to 2. 220 ** This is an optimization that allows sqlite3PcacheFetch() to skip 221 ** searching for a dirty page to eject from the cache when it might 222 ** otherwise have to. */ 223 assert( pPage==p->pDirty ); 224 p->pDirty = pPage->pDirtyNext; 225 assert( p->bPurgeable || p->eCreate==2 ); 226 if( p->pDirty==0 ){ /*OPTIMIZATION-IF-TRUE*/ 227 assert( p->bPurgeable==0 || p->eCreate==1 ); 228 p->eCreate = 2; 229 } 230 } 231 } 232 if( addRemove & PCACHE_DIRTYLIST_ADD ){ 233 pPage->pDirtyPrev = 0; 234 pPage->pDirtyNext = p->pDirty; 235 if( pPage->pDirtyNext ){ 236 assert( pPage->pDirtyNext->pDirtyPrev==0 ); 237 pPage->pDirtyNext->pDirtyPrev = pPage; 238 }else{ 239 p->pDirtyTail = pPage; 240 if( p->bPurgeable ){ 241 assert( p->eCreate==2 ); 242 p->eCreate = 1; 243 } 244 } 245 p->pDirty = pPage; 246 247 /* If pSynced is NULL and this page has a clear NEED_SYNC flag, set 248 ** pSynced to point to it. Checking the NEED_SYNC flag is an 249 ** optimization, as if pSynced points to a page with the NEED_SYNC 250 ** flag set sqlite3PcacheFetchStress() searches through all newer 251 ** entries of the dirty-list for a page with NEED_SYNC clear anyway. */ 252 if( !p->pSynced 253 && 0==(pPage->flags&PGHDR_NEED_SYNC) /*OPTIMIZATION-IF-FALSE*/ 254 ){ 255 p->pSynced = pPage; 256 } 257 } 258 pcacheDump(p); 259 } 260 261 /* 262 ** Wrapper around the pluggable caches xUnpin method. If the cache is 263 ** being used for an in-memory database, this function is a no-op. 264 */ 265 static void pcacheUnpin(PgHdr *p){ 266 if( p->pCache->bPurgeable ){ 267 pcacheTrace(("%p.UNPIN %d\n", p->pCache, p->pgno)); 268 sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 0); 269 pcacheDump(p->pCache); 270 } 271 } 272 273 /* 274 ** Compute the number of pages of cache requested. p->szCache is the 275 ** cache size requested by the "PRAGMA cache_size" statement. 276 */ 277 static int numberOfCachePages(PCache *p){ 278 if( p->szCache>=0 ){ 279 /* IMPLEMENTATION-OF: R-42059-47211 If the argument N is positive then the 280 ** suggested cache size is set to N. */ 281 return p->szCache; 282 }else{ 283 i64 n; 284 /* IMPLEMENTATION-OF: R-59858-46238 If the argument N is negative, then the 285 ** number of cache pages is adjusted to be a number of pages that would 286 ** use approximately abs(N*1024) bytes of memory based on the current 287 ** page size. */ 288 n = ((-1024*(i64)p->szCache)/(p->szPage+p->szExtra)); 289 if( n>`1000000000` ) n = `1000000000`; 290 return (int)n; 291 } 292 } 293 294 /*************************************************** General Interfaces ****** 295 ** 296 ** Initialize and shutdown the page cache subsystem. Neither of these 297 ** functions are threadsafe. 298 */ 299 int sqlite3PcacheInitialize(void){ 300 if( sqlite3GlobalConfig.pcache2.xInit==0 ){ 301 /* IMPLEMENTATION-OF: R-26801-64137 If the xInit() method is NULL, then the 302 ** built-in default page cache is used instead of the application defined 303 ** page cache. */ 304 sqlite3PCacheSetDefault(); 305 assert( sqlite3GlobalConfig.pcache2.xInit!=0 ); 306 } 307 return sqlite3GlobalConfig.pcache2.xInit(sqlite3GlobalConfig.pcache2.pArg); 308 } 309 void sqlite3PcacheShutdown(void){ 310 if( sqlite3GlobalConfig.pcache2.xShutdown ){ 311 /* IMPLEMENTATION-OF: R-26000-56589 The xShutdown() method may be NULL. */ 312 sqlite3GlobalConfig.pcache2.xShutdown(sqlite3GlobalConfig.pcache2.pArg); 313 } 314 } 315 316 /* 317 ** Return the size in bytes of a PCache object. 318 */ 319 int sqlite3PcacheSize(void){ return sizeof(PCache); } 320 321 /* 322 ** Create a new PCache object. Storage space to hold the object 323 ** has already been allocated and is passed in as the p pointer. 324 ** The caller discovers how much space needs to be allocated by 325 ** calling sqlite3PcacheSize(). 326 ** 327 ** szExtra is some extra space allocated for each page. The first 328 ** 8 bytes of the extra space will be zeroed as the page is allocated, 329 ** but remaining content will be uninitialized. Though it is opaque 330 ** to this module, the extra space really ends up being the MemPage 331 ** structure in the pager. 332 */ 333 int sqlite3PcacheOpen( 334 int szPage, /* Size of every page */ 335 int szExtra, /* Extra space associated with each page */ 336 int bPurgeable, /* True if pages are on backing store */ 337 int (*xStress)(void*,PgHdr*),/* Call to try to make pages clean */ 338 void *pStress, /* Argument to xStress */ 339 PCache *p /* Preallocated space for the PCache */ 340 ){ 341 memset(p, 0, sizeof(PCache)); 342 p->szPage = 1; 343 p->szExtra = szExtra; 344 assert( szExtra>=8 ); /* First 8 bytes will be zeroed */ 345 p->bPurgeable = bPurgeable; 346 p->eCreate = 2; 347 p->xStress = xStress; 348 p->pStress = pStress; 349 p->szCache = 100; 350 p->szSpill = 1; 351 pcacheTrace(("%p.OPEN szPage %d bPurgeable %d\n",p,szPage,bPurgeable)); 352 return sqlite3PcacheSetPageSize(p, szPage); 353 } 354 355 /* 356 ** Change the page size for PCache object. The caller must ensure that there 357 ** are no outstanding page references when this function is called. 358 */ 359 int sqlite3PcacheSetPageSize(PCache *pCache, int szPage){ 360 assert( pCache->nRefSum==0 && pCache->pDirty==0 ); 361 if( pCache->szPage ){ 362 sqlite3_pcache *pNew; 363 pNew = sqlite3GlobalConfig.pcache2.xCreate( 364 szPage, pCache->szExtra + ROUND8(sizeof(PgHdr)), 365 pCache->bPurgeable 366 ); 367 if( pNew==0 ) return SQLITE_NOMEM_BKPT; 368 sqlite3GlobalConfig.pcache2.xCachesize(pNew, numberOfCachePages(pCache)); 369 if( pCache->pCache ){ 370 sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache); 371 } 372 pCache->pCache = pNew; 373 pCache->szPage = szPage; 374 pcacheTrace(("%p.PAGESIZE %d\n",pCache,szPage)); 375 } 376 return SQLITE_OK; 377 } 378 379 /* 380 ** Try to obtain a page from the cache. 381 ** 382 ** This routine returns a pointer to an sqlite3_pcache_page object if 383 ** such an object is already in cache, or if a new one is created. 384 ** This routine returns a NULL pointer if the object was not in cache 385 ** and could not be created. 386 ** 387 ** The createFlags should be 0 to check for existing pages and should 388 ** be 3 (not 1, but 3) to try to create a new page. 389 ** 390 ** If the createFlag is 0, then NULL is always returned if the page 391 ** is not already in the cache. If createFlag is 1, then a new page 392 ** is created only if that can be done without spilling dirty pages 393 ** and without exceeding the cache size limit. 394 ** 395 ** The caller needs to invoke sqlite3PcacheFetchFinish() to properly 396 ** initialize the sqlite3_pcache_page object and convert it into a 397 ** PgHdr object. The sqlite3PcacheFetch() and sqlite3PcacheFetchFinish() 398 ** routines are split this way for performance reasons. When separated 399 ** they can both (usually) operate without having to push values to 400 ** the stack on entry and pop them back off on exit, which saves a 401 ** lot of pushing and popping. 402 */ 403 sqlite3_pcache_page *sqlite3PcacheFetch( 404 PCache *pCache, /* Obtain the page from this cache */ 405 Pgno pgno, /* Page number to obtain */ 406 int createFlag /* If true, create page if it does not exist already */ 407 ){ 408 int eCreate; 409 sqlite3_pcache_page *pRes; 410 411 assert( pCache!=0 ); 412 assert( pCache->pCache!=0 ); 413 assert( createFlag==3 || createFlag==0 ); 414 assert( pCache->eCreate==((pCache->bPurgeable && pCache->pDirty) ? 1 : 2) ); 415 416 /* eCreate defines what to do if the page does not exist. 417 ** 0 Do not allocate a new page. (createFlag==0) 418 ** 1 Allocate a new page if doing so is inexpensive. 419 ** (createFlag==1 AND bPurgeable AND pDirty) 420 ** 2 Allocate a new page even it doing so is difficult. 421 ** (createFlag==1 AND !(bPurgeable AND pDirty) 422 */ 423 eCreate = createFlag & pCache->eCreate; 424 assert( eCreate==0 || eCreate==1 || eCreate==2 ); 425 assert( createFlag==0 || pCache->eCreate==eCreate ); 426 assert( createFlag==0 || eCreate==1+(!pCache->bPurgeable||!pCache->pDirty) ); 427 pRes = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, eCreate); 428 pcacheTrace(("%p.FETCH %d%s (result: %p) ",pCache,pgno, 429 createFlag?" create":"",pRes)); 430 pcachePageTrace(pgno, pRes); 431 return pRes; 432 } 433 434 /* 435 ** If the sqlite3PcacheFetch() routine is unable to allocate a new 436 ** page because no clean pages are available for reuse and the cache 437 ** size limit has been reached, then this routine can be invoked to 438 ** try harder to allocate a page. This routine might invoke the stress 439 ** callback to spill dirty pages to the journal. It will then try to 440 ** allocate the new page and will only fail to allocate a new page on 441 ** an OOM error. 442 ** 443 ** This routine should be invoked only after sqlite3PcacheFetch() fails. 444 */ 445 int sqlite3PcacheFetchStress( 446 PCache *pCache, /* Obtain the page from this cache */ 447 Pgno pgno, /* Page number to obtain */ 448 sqlite3_pcache_page **ppPage /* Write result here */ 449 ){ 450 PgHdr *pPg; 451 if( pCache->eCreate==2 ) return 0; 452 453 if( sqlite3PcachePagecount(pCache)>pCache->szSpill ){ 454 /* Find a dirty page to write-out and recycle. First try to find a 455 ** page that does not require a journal-sync (one with PGHDR_NEED_SYNC 456 ** cleared), but if that is not possible settle for any other 457 ** unreferenced dirty page. 458 ** 459 ** If the LRU page in the dirty list that has a clear PGHDR_NEED_SYNC 460 ** flag is currently referenced, then the following may leave pSynced 461 ** set incorrectly (pointing to other than the LRU page with NEED_SYNC 462 ** cleared). This is Ok, as pSynced is just an optimization. */ 463 for(pPg=pCache->pSynced; 464 pPg && (pPg->nRef || (pPg->flags&PGHDR_NEED_SYNC)); 465 pPg=pPg->pDirtyPrev 466 ); 467 pCache->pSynced = pPg; 468 if( !pPg ){ 469 for(pPg=pCache->pDirtyTail; pPg && pPg->nRef; pPg=pPg->pDirtyPrev); 470 } 471 if( pPg ){ 472 int rc; 473 #ifdef SQLITE_LOG_CACHE_SPILL 474 sqlite3_log(SQLITE_FULL, 475 "spill page %d making room for %d - cache used: %d/%d", 476 pPg->pgno, pgno, 477 sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache), 478 numberOfCachePages(pCache)); 479 #endif 480 pcacheTrace(("%p.SPILL %d\n",pCache,pPg->pgno)); 481 rc = pCache->xStress(pCache->pStress, pPg); 482 pcacheDump(pCache); 483 if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){ 484 return rc; 485 } 486 } 487 } 488 *ppPage = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, 2); 489 return *ppPage==0 ? SQLITE_NOMEM_BKPT : SQLITE_OK; 490 } 491 492 /* 493 ** This is a helper routine for sqlite3PcacheFetchFinish() 494 ** 495 ** In the uncommon case where the page being fetched has not been 496 ** initialized, this routine is invoked to do the initialization. 497 ** This routine is broken out into a separate function since it 498 ** requires extra stack manipulation that can be avoided in the common 499 ** case. 500 */ 501 static SQLITE_NOINLINE PgHdr *pcacheFetchFinishWithInit( 502 PCache *pCache, /* Obtain the page from this cache */ 503 Pgno pgno, /* Page number obtained */ 504 sqlite3_pcache_page *pPage /* Page obtained by prior PcacheFetch() call */ 505 ){ 506 PgHdr *pPgHdr; 507 assert( pPage!=0 ); 508 pPgHdr = (PgHdr*)pPage->pExtra; 509 assert( pPgHdr->pPage==0 ); 510 memset(&pPgHdr->pDirty, 0, sizeof(PgHdr) - offsetof(PgHdr,pDirty)); 511 pPgHdr->pPage = pPage; 512 pPgHdr->pData = pPage->pBuf; 513 pPgHdr->pExtra = (void *)&pPgHdr[1]; 514 memset(pPgHdr->pExtra, 0, 8); 515 assert( EIGHT_BYTE_ALIGNMENT( pPgHdr->pExtra ) ); 516 pPgHdr->pCache = pCache; 517 pPgHdr->pgno = pgno; 518 pPgHdr->flags = PGHDR_CLEAN; 519 return sqlite3PcacheFetchFinish(pCache,pgno,pPage); 520 } 521 522 /* 523 ** This routine converts the sqlite3_pcache_page object returned by 524 ** sqlite3PcacheFetch() into an initialized PgHdr object. This routine 525 ** must be called after sqlite3PcacheFetch() in order to get a usable 526 ** result. 527 */ 528 PgHdr *sqlite3PcacheFetchFinish( 529 PCache *pCache, /* Obtain the page from this cache */ 530 Pgno pgno, /* Page number obtained */ 531 sqlite3_pcache_page *pPage /* Page obtained by prior PcacheFetch() call */ 532 ){ 533 PgHdr *pPgHdr; 534 535 assert( pPage!=0 ); 536 pPgHdr = (PgHdr *)pPage->pExtra; 537 538 if( !pPgHdr->pPage ){ 539 return pcacheFetchFinishWithInit(pCache, pgno, pPage); 540 } 541 pCache->nRefSum++; 542 pPgHdr->nRef++; 543 assert( sqlite3PcachePageSanity(pPgHdr) ); 544 return pPgHdr; 545 } 546 547 /* 548 ** Decrement the reference count on a page. If the page is clean and the 549 ** reference count drops to 0, then it is made eligible for recycling. 550 */ 551 void SQLITE_NOINLINE sqlite3PcacheRelease(PgHdr *p){ 552 assert( p->nRef>0 ); 553 p->pCache->nRefSum--; 554 if( (--p->nRef)==0 ){ 555 if( p->flags&PGHDR_CLEAN ){ 556 pcacheUnpin(p); 557 }else{ 558 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_FRONT); 559 assert( sqlite3PcachePageSanity(p) ); 560 } 561 } 562 } 563 564 /* 565 ** Increase the reference count of a supplied page by 1. 566 */ 567 void sqlite3PcacheRef(PgHdr *p){ 568 assert(p->nRef>0); 569 assert( sqlite3PcachePageSanity(p) ); 570 p->nRef++; 571 p->pCache->nRefSum++; 572 } 573 574 /* 575 ** Drop a page from the cache. There must be exactly one reference to the 576 ** page. This function deletes that reference, so after it returns the 577 ** page pointed to by p is invalid. 578 */ 579 void sqlite3PcacheDrop(PgHdr *p){ 580 assert( p->nRef==1 ); 581 assert( sqlite3PcachePageSanity(p) ); 582 if( p->flags&PGHDR_DIRTY ){ 583 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE); 584 } 585 p->pCache->nRefSum--; 586 sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 1); 587 } 588 589 /* 590 ** Make sure the page is marked as dirty. If it isn't dirty already, 591 ** make it so. 592 */ 593 void sqlite3PcacheMakeDirty(PgHdr *p){ 594 assert( p->nRef>0 ); 595 assert( sqlite3PcachePageSanity(p) ); 596 if( p->flags & (PGHDR_CLEAN|PGHDR_DONT_WRITE) ){ /*OPTIMIZATION-IF-FALSE*/ 597 p->flags &= ~PGHDR_DONT_WRITE; 598 if( p->flags & PGHDR_CLEAN ){ 599 p->flags ^= (PGHDR_DIRTY|PGHDR_CLEAN); 600 pcacheTrace(("%p.DIRTY %d\n",p->pCache,p->pgno)); 601 assert( (p->flags & (PGHDR_DIRTY|PGHDR_CLEAN))==PGHDR_DIRTY ); 602 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_ADD); 603 assert( sqlite3PcachePageSanity(p) ); 604 } 605 assert( sqlite3PcachePageSanity(p) ); 606 } 607 } 608 609 /* 610 ** Make sure the page is marked as clean. If it isn't clean already, 611 ** make it so. 612 */ 613 void sqlite3PcacheMakeClean(PgHdr *p){ 614 assert( sqlite3PcachePageSanity(p) ); 615 assert( (p->flags & PGHDR_DIRTY)!=0 ); 616 assert( (p->flags & PGHDR_CLEAN)==0 ); 617 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE); 618 p->flags &= ~(PGHDR_DIRTY|PGHDR_NEED_SYNC|PGHDR_WRITEABLE); 619 p->flags |= PGHDR_CLEAN; 620 pcacheTrace(("%p.CLEAN %d\n",p->pCache,p->pgno)); 621 assert( sqlite3PcachePageSanity(p) ); 622 if( p->nRef==0 ){ 623 pcacheUnpin(p); 624 } 625 } 626 627 /* 628 ** Make every page in the cache clean. 629 */ 630 void sqlite3PcacheCleanAll(PCache *pCache){ 631 PgHdr *p; 632 pcacheTrace(("%p.CLEAN-ALL\n",pCache)); 633 while( (p = pCache->pDirty)!=0 ){ 634 sqlite3PcacheMakeClean(p); 635 } 636 } 637 638 /* 639 ** Clear the PGHDR_NEED_SYNC and PGHDR_WRITEABLE flag from all dirty pages. 640 */ 641 void sqlite3PcacheClearWritable(PCache *pCache){ 642 PgHdr *p; 643 pcacheTrace(("%p.CLEAR-WRITEABLE\n",pCache)); 644 for(p=pCache->pDirty; p; p=p->pDirtyNext){ 645 p->flags &= ~(PGHDR_NEED_SYNC|PGHDR_WRITEABLE); 646 } 647 pCache->pSynced = pCache->pDirtyTail; 648 } 649 650 /* 651 ** Clear the PGHDR_NEED_SYNC flag from all dirty pages. 652 */ 653 void sqlite3PcacheClearSyncFlags(PCache *pCache){ 654 PgHdr *p; 655 for(p=pCache->pDirty; p; p=p->pDirtyNext){ 656 p->flags &= ~PGHDR_NEED_SYNC; 657 } 658 pCache->pSynced = pCache->pDirtyTail; 659 } 660 661 /* 662 ** Change the page number of page p to newPgno. 663 */ 664 void sqlite3PcacheMove(PgHdr *p, Pgno newPgno){ 665 PCache *pCache = p->pCache; 666 sqlite3_pcache_page *pOther; 667 assert( p->nRef>0 ); 668 assert( newPgno>0 ); 669 assert( sqlite3PcachePageSanity(p) ); 670 pcacheTrace(("%p.MOVE %d -> %d\n",pCache,p->pgno,newPgno)); 671 pOther = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, newPgno, 0); 672 if( pOther ){ 673 PgHdr *pXPage = (PgHdr*)pOther->pExtra; 674 assert( pXPage->nRef==0 ); 675 pXPage->nRef++; 676 pCache->nRefSum++; 677 sqlite3PcacheDrop(pXPage); 678 } 679 sqlite3GlobalConfig.pcache2.xRekey(pCache->pCache, p->pPage, p->pgno,newPgno); 680 p->pgno = newPgno; 681 if( (p->flags&PGHDR_DIRTY) && (p->flags&PGHDR_NEED_SYNC) ){ 682 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_FRONT); 683 assert( sqlite3PcachePageSanity(p) ); 684 } 685 } 686 687 /* 688 ** Drop every cache entry whose page number is greater than "pgno". The 689 ** caller must ensure that there are no outstanding references to any pages 690 ** other than page 1 with a page number greater than pgno. 691 ** 692 ** If there is a reference to page 1 and the pgno parameter passed to this 693 ** function is 0, then the data area associated with page 1 is zeroed, but 694 ** the page object is not dropped. 695 */ 696 void sqlite3PcacheTruncate(PCache *pCache, Pgno pgno){ 697 if( pCache->pCache ){ 698 PgHdr *p; 699 PgHdr *pNext; 700 pcacheTrace(("%p.TRUNCATE %d\n",pCache,pgno)); 701 for(p=pCache->pDirty; p; p=pNext){ 702 pNext = p->pDirtyNext; 703 /* This routine never gets call with a positive pgno except right 704 ** after sqlite3PcacheCleanAll(). So if there are dirty pages, 705 ** it must be that pgno==0. 706 */ 707 assert( p->pgno>0 ); 708 if( p->pgno>pgno ){ 709 assert( p->flags&PGHDR_DIRTY ); 710 sqlite3PcacheMakeClean(p); 711 } 712 } 713 if( pgno==0 && pCache->nRefSum ){ 714 sqlite3_pcache_page *pPage1; 715 pPage1 = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache,1,0); 716 if( ALWAYS(pPage1) ){ /* Page 1 is always available in cache, because 717 ** pCache->nRefSum>0 */ 718 memset(pPage1->pBuf, 0, pCache->szPage); 719 pgno = 1; 720 } 721 } 722 sqlite3GlobalConfig.pcache2.xTruncate(pCache->pCache, pgno+1); 723 } 724 } 725 726 /* 727 ** Close a cache. 728 */ 729 void sqlite3PcacheClose(PCache *pCache){ 730 assert( pCache->pCache!=0 ); 731 pcacheTrace(("%p.CLOSE\n",pCache)); 732 sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache); 733 } 734 735 /* 736 ** Discard the contents of the cache. 737 */ 738 void sqlite3PcacheClear(PCache *pCache){ 739 sqlite3PcacheTruncate(pCache, 0); 740 } 741 742 /* 743 ** Merge two lists of pages connected by pDirty and in pgno order. 744 ** Do not bother fixing the pDirtyPrev pointers. 745 */ 746 static PgHdr *pcacheMergeDirtyList(PgHdr *pA, PgHdr *pB){ 747 PgHdr result, *pTail; 748 pTail = &result; 749 assert( pA!=0 && pB!=0 ); 750 for(;;){ 751 if( pA->pgnopgno ){ 752 pTail->pDirty = pA; 753 pTail = pA; 754 pA = pA->pDirty; 755 if( pA==0 ){ 756 pTail->pDirty = pB; 757 break; 758 } 759 }else{ 760 pTail->pDirty = pB; 761 pTail = pB; 762 pB = pB->pDirty; 763 if( pB==0 ){ 764 pTail->pDirty = pA; 765 break; 766 } 767 } 768 } 769 return result.pDirty; 770 } 771 772 /* 773 ** Sort the list of pages in ascending order by pgno. Pages are 774 ** connected by pDirty pointers. The pDirtyPrev pointers are 775 ** corrupted by this sort. 776 ** 777 ** Since there cannot be more than 2^31 distinct pages in a database, 778 ** there cannot be more than 31 buckets required by the merge sorter. 779 ** One extra bucket is added to catch overflow in case something 780 ** ever changes to make the previous sentence incorrect. 781 */ 782 #define N_SORT_BUCKET 32 783 static PgHdr *pcacheSortDirtyList(PgHdr *pIn){ 784 PgHdr *a[N_SORT_BUCKET], *p; 785 int i; 786 memset(a, 0, sizeof(a)); 787 while( pIn ){ 788 p = pIn; 789 pIn = p->pDirty; 790 p->pDirty = 0; 791 for(i=0; ALWAYS(ipDirty; p; p=p->pDirtyNext){ 821 p->pDirty = p->pDirtyNext; 822 } 823 return pcacheSortDirtyList(pCache->pDirty); 824 } 825 826 /* 827 ** Return the total number of references to all pages held by the cache. 828 ** 829 ** This is not the total number of pages referenced, but the sum of the 830 ** reference count for all pages. 831 */ 832 i64 sqlite3PcacheRefCount(PCache *pCache){ 833 return pCache->nRefSum; 834 } 835 836 /* 837 ** Return the number of references to the page supplied as an argument. 838 */ 839 i64 sqlite3PcachePageRefcount(PgHdr *p){ 840 return p->nRef; 841 } 842 843 /* 844 ** Return the total number of pages in the cache. 845 */ 846 int sqlite3PcachePagecount(PCache *pCache){ 847 assert( pCache->pCache!=0 ); 848 return sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache); 849 } 850 851 #ifdef SQLITE_TEST 852 /* 853 ** Get the suggested cache-size value. 854 */ 855 int sqlite3PcacheGetCachesize(PCache *pCache){ 856 return numberOfCachePages(pCache); 857 } 858 #endif 859 860 /* 861 ** Set the suggested cache-size value. 862 */ 863 void sqlite3PcacheSetCachesize(PCache *pCache, int mxPage){ 864 assert( pCache->pCache!=0 ); 865 pCache->szCache = mxPage; 866 sqlite3GlobalConfig.pcache2.xCachesize(pCache->pCache, 867 numberOfCachePages(pCache)); 868 } 869 870 /* 871 ** Set the suggested cache-spill value. Make no changes if if the 872 ** argument is zero. Return the effective cache-spill size, which will 873 ** be the larger of the szSpill and szCache. 874 */ 875 int sqlite3PcacheSetSpillsize(PCache *p, int mxPage){ 876 int res; 877 assert( p->pCache!=0 ); 878 if( mxPage ){ 879 if( mxPage<0 ){ 880 mxPage = (int)((-1024*(i64)mxPage)/(p->szPage+p->szExtra)); 881 } 882 p->szSpill = mxPage; 883 } 884 res = numberOfCachePages(p); 885 if( resszSpill ) res = p->szSpill; 886 return res; 887 } 888 889 /* 890 ** Free up as much memory as possible from the page cache. 891 */ 892 void sqlite3PcacheShrink(PCache *pCache){ 893 assert( pCache->pCache!=0 ); 894 sqlite3GlobalConfig.pcache2.xShrink(pCache->pCache); 895 } 896 897 /* 898 ** Return the size of the header added by this middleware layer 899 ** in the page-cache hierarchy. 900 */ 901 int sqlite3HeaderSizePcache(void){ return ROUND8(sizeof(PgHdr)); } 902 903 /* 904 ** Return the number of dirty pages currently in the cache, as a percentage 905 ** of the configured cache size. 906 */ 907 int sqlite3PCachePercentDirty(PCache *pCache){ 908 PgHdr *pDirty; 909 int nDirty = 0; 910 int nCache = numberOfCachePages(pCache); 911 for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext) nDirty++; 912 return nCache ? (int)(((i64)nDirty * 100) / nCache) : 0; 913 } 914 915 #ifdef SQLITE_DIRECT_OVERFLOW_READ 916 /* 917 ** Return true if there are one or more dirty pages in the cache. Else false. 918 */ 919 int sqlite3PCacheIsDirty(PCache *pCache){ 920 return (pCache->pDirty!=0); 921 } 922 #endif 923 924 #if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG) 925 /* 926 ** For all dirty pages currently in the cache, invoke the specified 927 ** callback. This is only used if the SQLITE_CHECK_PAGES macro is 928 ** defined. 929 */ 930 void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *)){ 931 PgHdr *pDirty; 932 for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext){ 933 xIter(pDirty); 934 } 935 } 936 #endif