1 /* 2 ** 2008 November 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 ** 13 ** This file implements the default page cache implementation (the 14 ** sqlite3_pcache interface). It also contains part of the implementation 15 ** of the SQLITE_CONFIG_PAGECACHE and sqlite3_release_memory() features. 16 ** If the default page cache implementation is overridden, then neither of 17 ** these two features are available. 18 */ 19 20 #include "sqliteInt.h" 21 22 typedef struct PCache1 PCache1; 23 typedef struct PgHdr1 PgHdr1; 24 typedef struct PgFreeslot PgFreeslot; 25 typedef struct PGroup PGroup; 26 27 /* Each page cache (or PCache) belongs to a PGroup. A PGroup is a set 28 ** of one or more PCaches that are able to recycle each other's unpinned 29 ** pages when they are under memory pressure. A PGroup is an instance of 30 ** the following object. 31 ** 32 ** This page cache implementation works in one of two modes: 33 ** 34 ** (1) Every PCache is the sole member of its own PGroup. There is 35 ** one PGroup per PCache. 36 ** 37 ** (2) There is a single global PGroup that all PCaches are a member 38 ** of. 39 ** 40 ** Mode 1 uses more memory (since PCache instances are not able to rob 41 ** unused pages from other PCaches) but it also operates without a mutex, 42 ** and is therefore often faster. Mode 2 requires a mutex in order to be 43 ** threadsafe, but recycles pages more efficiently. 44 ** 45 ** For mode (1), PGroup.mutex is NULL. For mode (2) there is only a single 46 ** PGroup which is the pcache1.grp global variable and its mutex is 47 ** SQLITE_MUTEX_STATIC_LRU. 48 */ 49 struct PGroup { 50 sqlite3_mutex *mutex; /* MUTEX_STATIC_LRU or NULL */ 51 unsigned int nMaxPage; /* Sum of nMax for purgeable caches */ 52 unsigned int nMinPage; /* Sum of nMin for purgeable caches */ 53 unsigned int mxPinned; /* nMaxpage + 10 - nMinPage */ 54 unsigned int nCurrentPage; /* Number of purgeable pages allocated */ 55 PgHdr1 *pLruHead, *pLruTail; /* LRU list of unpinned pages */ 56 }; 57 58 /* Each page cache is an instance of the following object. Every 59 ** open database file (including each in-memory database and each 60 ** temporary or transient database) has a single page cache which 61 ** is an instance of this object. 62 ** 63 ** Pointers to structures of this type are cast and returned as 64 ** opaque sqlite3_pcache* handles. 65 */ 66 struct PCache1 { 67 /* Cache configuration parameters. Page size (szPage) and the purgeable 68 ** flag (bPurgeable) are set when the cache is created. nMax may be 69 ** modified at any time by a call to the pcache1Cachesize() method. 70 ** The PGroup mutex must be held when accessing nMax. 71 */ 72 PGroup *pGroup; /* PGroup this cache belongs to */ 73 int szPage; /* Size of allocated pages in bytes */ 74 int szExtra; /* Size of extra space in bytes */ 75 int bPurgeable; /* True if cache is purgeable */ 76 unsigned int nMin; /* Minimum number of pages reserved */ 77 unsigned int nMax; /* Configured "cache_size" value */ 78 unsigned int n90pct; /* nMax*9/10 */ 79 unsigned int iMaxKey; /* Largest key seen since xTruncate() */ 80 81 /* Hash table of all pages. The following variables may only be accessed 82 ** when the accessor is holding the PGroup mutex. 83 */ 84 unsigned int nRecyclable; /* Number of pages in the LRU list */ 85 unsigned int nPage; /* Total number of pages in apHash */ 86 unsigned int nHash; /* Number of slots in apHash[] */ 87 PgHdr1 **apHash; /* Hash table for fast lookup by key */ 88 }; 89 90 /* 91 ** Each cache entry is represented by an instance of the following 92 ** structure. Unless SQLITE_PCACHE_SEPARATE_HEADER is defined, a buffer of 93 ** PgHdr1.pCache->szPage bytes is allocated directly before this structure 94 ** in memory. 95 */ 96 struct PgHdr1 { 97 sqlite3_pcache_page page; 98 unsigned int iKey; /* Key value (page number) */ 99 u8 isPinned; /* Page in use, not on the LRU list */ 100 PgHdr1 *pNext; /* Next in hash table chain */ 101 PCache1 *pCache; /* Cache that currently owns this page */ 102 PgHdr1 *pLruNext; /* Next in LRU list of unpinned pages */ 103 PgHdr1 *pLruPrev; /* Previous in LRU list of unpinned pages */ 104 }; 105 106 /* 107 ** Free slots in the allocator used to divide up the buffer provided using 108 ** the SQLITE_CONFIG_PAGECACHE mechanism. 109 */ 110 struct PgFreeslot { 111 PgFreeslot *pNext; /* Next free slot */ 112 }; 113 114 /* 115 ** Global data used by this cache. 116 */ 117 static SQLITE_WSD struct PCacheGlobal { 118 PGroup grp; /* The global PGroup for mode (2) */ 119 120 /* Variables related to SQLITE_CONFIG_PAGECACHE settings. The 121 ** szSlot, nSlot, pStart, pEnd, nReserve, and isInit values are all 122 ** fixed at sqlite3_initialize() time and do not require mutex protection. 123 ** The nFreeSlot and pFree values do require mutex protection. 124 */ 125 int isInit; /* True if initialized */ 126 int szSlot; /* Size of each free slot */ 127 int nSlot; /* The number of pcache slots */ 128 int nReserve; /* Try to keep nFreeSlot above this */ 129 void *pStart, *pEnd; /* Bounds of pagecache malloc range */ 130 /* Above requires no mutex. Use mutex below for variable that follow. */ 131 sqlite3_mutex *mutex; /* Mutex for accessing the following: */ 132 PgFreeslot *pFree; /* Free page blocks */ 133 int nFreeSlot; /* Number of unused pcache slots */ 134 /* The following value requires a mutex to change. We skip the mutex on 135 ** reading because (1) most platforms read a 32-bit integer atomically and 136 ** (2) even if an incorrect value is read, no great harm is done since this 137 ** is really just an optimization. */ 138 int bUnderPressure; /* True if low on PAGECACHE memory */ 139 } pcache1_g; 140 141 /* 142 ** All code in this file should access the global structure above via the 143 ** alias "pcache1". This ensures that the WSD emulation is used when 144 ** compiling for systems that do not support real WSD. 145 */ 146 #define pcache1 (GLOBAL(struct PCacheGlobal, pcache1_g)) 147 148 /* 149 ** Macros to enter and leave the PCache LRU mutex. 150 */ 151 #if !defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) || SQLITE_THREADSAFE==0 152 # define pcache1EnterMutex(X) assert((X)->mutex==0) 153 # define pcache1LeaveMutex(X) assert((X)->mutex==0) 154 # define PCACHE1_MIGHT_USE_GROUP_MUTEX 0 155 #else 156 # define pcache1EnterMutex(X) sqlite3_mutex_enter((X)->mutex) 157 # define pcache1LeaveMutex(X) sqlite3_mutex_leave((X)->mutex) 158 # define PCACHE1_MIGHT_USE_GROUP_MUTEX 1 159 #endif 160 161 /******************************************************************************/ 162 /******** Page Allocation/SQLITE_CONFIG_PCACHE Related Functions **************/ 163 164 /* 165 ** This function is called during initialization if a static buffer is 166 ** supplied to use for the page-cache by passing the SQLITE_CONFIG_PAGECACHE 167 ** verb to sqlite3_config(). Parameter pBuf points to an allocation large 168 ** enough to contain 'n' buffers of 'sz' bytes each. 169 ** 170 ** This routine is called from sqlite3_initialize() and so it is guaranteed 171 ** to be serialized already. There is no need for further mutexing. 172 */ 173 void sqlite3PCacheBufferSetup(void *pBuf, int sz, int n){ 174 if( pcache1.isInit ){ 175 PgFreeslot *p; 176 sz = ROUNDDOWN8(sz); 177 pcache1.szSlot = sz; 178 pcache1.nSlot = pcache1.nFreeSlot = n; 179 pcache1.nReserve = n>90 ? 10 : (n/10 + 1); 180 pcache1.pStart = pBuf; 181 pcache1.pFree = 0; 182 pcache1.bUnderPressure = 0; 183 while( n-- ){ 184 p = (PgFreeslot*)pBuf; 185 p->pNext = pcache1.pFree; 186 pcache1.pFree = p; 187 pBuf = (void*)&((char*)pBuf)[sz]; 188 } 189 pcache1.pEnd = pBuf; 190 } 191 } 192 193 /* 194 ** Malloc function used within this file to allocate space from the buffer 195 ** configured using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no 196 ** such buffer exists or there is no space left in it, this function falls 197 ** back to sqlite3Malloc(). 198 ** 199 ** Multiple threads can run this routine at the same time. Global variables 200 ** in pcache1 need to be protected via mutex. 201 */ 202 static void *pcache1Alloc(int nByte){ 203 void *p = 0; 204 assert( sqlite3_mutex_notheld(pcache1.grp.mutex) ); 205 if( nByte<=pcache1.szSlot ){ 206 sqlite3_mutex_enter(pcache1.mutex); 207 p = (PgHdr1 *)pcache1.pFree; 208 if( p ){ 209 pcache1.pFree = pcache1.pFree->pNext; 210 pcache1.nFreeSlot--; 211 pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve; 212 assert( pcache1.nFreeSlot>=0 ); 213 sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, nByte); 214 sqlite3StatusUp(SQLITE_STATUS_PAGECACHE_USED, 1); 215 } 216 sqlite3_mutex_leave(pcache1.mutex); 217 } 218 if( p==0 ){ 219 /* Memory is not available in the SQLITE_CONFIG_PAGECACHE pool. Get 220 ** it from sqlite3Malloc instead. 221 */ 222 p = sqlite3Malloc(nByte); 223 #ifndef SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS 224 if( p ){ 225 int sz = sqlite3MallocSize(p); 226 sqlite3_mutex_enter(pcache1.mutex); 227 sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, nByte); 228 sqlite3StatusUp(SQLITE_STATUS_PAGECACHE_OVERFLOW, sz); 229 sqlite3_mutex_leave(pcache1.mutex); 230 } 231 #endif 232 sqlite3MemdebugSetType(p, MEMTYPE_PCACHE); 233 } 234 return p; 235 } 236 237 /* 238 ** Free an allocated buffer obtained from pcache1Alloc(). 239 */ 240 static int pcache1Free(void *p){ 241 int nFreed = 0; 242 if( p==0 ) return 0; 243 if( p>=pcache1.pStart && p<pcache1.pEnd ){ 244 PgFreeslot *pSlot; 245 sqlite3_mutex_enter(pcache1.mutex); 246 sqlite3StatusDown(SQLITE_STATUS_PAGECACHE_USED, 1); 247 pSlot = (PgFreeslot*)p; 248 pSlot->pNext = pcache1.pFree; 249 pcache1.pFree = pSlot; 250 pcache1.nFreeSlot++; 251 pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve; 252 assert( pcache1.nFreeSlot<=pcache1.nSlot ); 253 sqlite3_mutex_leave(pcache1.mutex); 254 }else{ 255 assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) ); 256 sqlite3MemdebugSetType(p, MEMTYPE_HEAP); 257 nFreed = sqlite3MallocSize(p); 258 #ifndef SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS 259 sqlite3_mutex_enter(pcache1.mutex); 260 sqlite3StatusDown(SQLITE_STATUS_PAGECACHE_OVERFLOW, nFreed); 261 sqlite3_mutex_leave(pcache1.mutex); 262 #endif 263 sqlite3_free(p); 264 } 265 return nFreed; 266 } 267 268 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT 269 /* 270 ** Return the size of a pcache allocation 271 */ 272 static int pcache1MemSize(void *p){ 273 if( p>=pcache1.pStart && p<pcache1.pEnd ){ 274 return pcache1.szSlot; 275 }else{ 276 int iSize; 277 assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) ); 278 sqlite3MemdebugSetType(p, MEMTYPE_HEAP); 279 iSize = sqlite3MallocSize(p); 280 sqlite3MemdebugSetType(p, MEMTYPE_PCACHE); 281 return iSize; 282 } 283 } 284 #endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */ 285 286 /* 287 ** Allocate a new page object initially associated with cache pCache. 288 */ 289 static PgHdr1 *pcache1AllocPage(PCache1 *pCache){ 290 PgHdr1 *p = 0; 291 void *pPg; 292 293 /* The group mutex must be released before pcache1Alloc() is called. This 294 ** is because it may call sqlite3_release_memory(), which assumes that 295 ** this mutex is not held. */ 296 assert( sqlite3_mutex_held(pCache->pGroup->mutex) ); 297 pcache1LeaveMutex(pCache->pGroup); 298 #ifdef SQLITE_PCACHE_SEPARATE_HEADER 299 pPg = pcache1Alloc(pCache->szPage); 300 p = sqlite3Malloc(sizeof(PgHdr1) + pCache->szExtra); 301 if( !pPg || !p ){ 302 pcache1Free(pPg); 303 sqlite3_free(p); 304 pPg = 0; 305 } 306 #else 307 pPg = pcache1Alloc(ROUND8(sizeof(PgHdr1)) + pCache->szPage + pCache->szExtra); 308 p = (PgHdr1 *)&((u8 *)pPg)[pCache->szPage]; 309 #endif 310 pcache1EnterMutex(pCache->pGroup); 311 312 if( pPg ){ 313 p->page.pBuf = pPg; 314 p->page.pExtra = &p[1]; 315 if( pCache->bPurgeable ){ 316 pCache->pGroup->nCurrentPage++; 317 } 318 return p; 319 } 320 return 0; 321 } 322 323 /* 324 ** Free a page object allocated by pcache1AllocPage(). 325 ** 326 ** The pointer is allowed to be NULL, which is prudent. But it turns out 327 ** that the current implementation happens to never call this routine 328 ** with a NULL pointer, so we mark the NULL test with ALWAYS(). 329 */ 330 static void pcache1FreePage(PgHdr1 *p){ 331 if( ALWAYS(p) ){ 332 PCache1 *pCache = p->pCache; 333 assert( sqlite3_mutex_held(p->pCache->pGroup->mutex) ); 334 pcache1Free(p->page.pBuf); 335 #ifdef SQLITE_PCACHE_SEPARATE_HEADER 336 sqlite3_free(p); 337 #endif 338 if( pCache->bPurgeable ){ 339 pCache->pGroup->nCurrentPage--; 340 } 341 } 342 } 343 344 /* 345 ** Malloc function used by SQLite to obtain space from the buffer configured 346 ** using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no such buffer 347 ** exists, this function falls back to sqlite3Malloc(). 348 */ 349 void *sqlite3PageMalloc(int sz){ 350 return pcache1Alloc(sz); 351 } 352 353 /* 354 ** Free an allocated buffer obtained from sqlite3PageMalloc(). 355 */ 356 void sqlite3PageFree(void *p){ 357 pcache1Free(p); 358 } 359 360 361 /* 362 ** Return true if it desirable to avoid allocating a new page cache 363 ** entry. 364 ** 365 ** If memory was allocated specifically to the page cache using 366 ** SQLITE_CONFIG_PAGECACHE but that memory has all been used, then 367 ** it is desirable to avoid allocating a new page cache entry because 368 ** presumably SQLITE_CONFIG_PAGECACHE was suppose to be sufficient 369 ** for all page cache needs and we should not need to spill the 370 ** allocation onto the heap. 371 ** 372 ** Or, the heap is used for all page cache memory but the heap is 373 ** under memory pressure, then again it is desirable to avoid 374 ** allocating a new page cache entry in order to avoid stressing 375 ** the heap even further. 376 */ 377 static int pcache1UnderMemoryPressure(PCache1 *pCache){ 378 if( pcache1.nSlot && (pCache->szPage+pCache->szExtra)<=pcache1.szSlot ){ 379 return pcache1.bUnderPressure; 380 }else{ 381 return sqlite3HeapNearlyFull(); 382 } 383 } 384 385 /******************************************************************************/ 386 /******** General Implementation Functions ************************************/ 387 388 /* 389 ** This function is used to resize the hash table used by the cache passed 390 ** as the first argument. 391 ** 392 ** The PCache mutex must be held when this function is called. 393 */ 394 static void pcache1ResizeHash(PCache1 *p){ 395 PgHdr1 **apNew; 396 unsigned int nNew; 397 unsigned int i; 398 399 assert( sqlite3_mutex_held(p->pGroup->mutex) ); 400 401 nNew = p->nHash*2; 402 if( nNew<256 ){ 403 nNew = 256; 404 } 405 406 pcache1LeaveMutex(p->pGroup); 407 if( p->nHash ){ sqlite3BeginBenignMalloc(); } 408 apNew = (PgHdr1 **)sqlite3MallocZero(sizeof(PgHdr1 *)*nNew); 409 if( p->nHash ){ sqlite3EndBenignMalloc(); } 410 pcache1EnterMutex(p->pGroup); 411 if( apNew ){ 412 for(i=0; i<p->nHash; i++){ 413 PgHdr1 *pPage; 414 PgHdr1 *pNext = p->apHash[i]; 415 while( (pPage = pNext)!=0 ){ 416 unsigned int h = pPage->iKey % nNew; 417 pNext = pPage->pNext; 418 pPage->pNext = apNew[h]; 419 apNew[h] = pPage; 420 } 421 } 422 sqlite3_free(p->apHash); 423 p->apHash = apNew; 424 p->nHash = nNew; 425 } 426 } 427 428 /* 429 ** This function is used internally to remove the page pPage from the 430 ** PGroup LRU list, if is part of it. If pPage is not part of the PGroup 431 ** LRU list, then this function is a no-op. 432 ** 433 ** The PGroup mutex must be held when this function is called. 434 */ 435 static PgHdr1 *pcache1PinPage(PgHdr1 *pPage){ 436 PCache1 *pCache; 437 438 assert( pPage!=0 ); 439 assert( pPage->isPinned==0 ); 440 pCache = pPage->pCache; 441 assert( pPage->pLruNext || pPage==pCache->pGroup->pLruTail ); 442 assert( pPage->pLruPrev || pPage==pCache->pGroup->pLruHead ); 443 assert( sqlite3_mutex_held(pCache->pGroup->mutex) ); 444 if( pPage->pLruPrev ){ 445 pPage->pLruPrev->pLruNext = pPage->pLruNext; 446 }else{ 447 pCache->pGroup->pLruHead = pPage->pLruNext; 448 } 449 if( pPage->pLruNext ){ 450 pPage->pLruNext->pLruPrev = pPage->pLruPrev; 451 }else{ 452 pCache->pGroup->pLruTail = pPage->pLruPrev; 453 } 454 pPage->pLruNext = 0; 455 pPage->pLruPrev = 0; 456 pPage->isPinned = 1; 457 pCache->nRecyclable--; 458 return pPage; 459 } 460 461 462 /* 463 ** Remove the page supplied as an argument from the hash table 464 ** (PCache1.apHash structure) that it is currently stored in. 465 ** Also free the page if freePage is true. 466 ** 467 ** The PGroup mutex must be held when this function is called. 468 */ 469 static void pcache1RemoveFromHash(PgHdr1 *pPage, int freeFlag){ 470 unsigned int h; 471 PCache1 *pCache = pPage->pCache; 472 PgHdr1 **pp; 473 474 assert( sqlite3_mutex_held(pCache->pGroup->mutex) ); 475 h = pPage->iKey % pCache->nHash; 476 for(pp=&pCache->apHash[h]; (*pp)!=pPage; pp=&(*pp)->pNext); 477 *pp = (*pp)->pNext; 478 479 pCache->nPage--; 480 if( freeFlag ) pcache1FreePage(pPage); 481 } 482 483 /* 484 ** If there are currently more than nMaxPage pages allocated, try 485 ** to recycle pages to reduce the number allocated to nMaxPage. 486 */ 487 static void pcache1EnforceMaxPage(PGroup *pGroup){ 488 assert( sqlite3_mutex_held(pGroup->mutex) ); 489 while( pGroup->nCurrentPage>pGroup->nMaxPage && pGroup->pLruTail ){ 490 PgHdr1 *p = pGroup->pLruTail; 491 assert( p->pCache->pGroup==pGroup ); 492 assert( p->isPinned==0 ); 493 pcache1PinPage(p); 494 pcache1RemoveFromHash(p, 1); 495 } 496 } 497 498 /* 499 ** Discard all pages from cache pCache with a page number (key value) 500 ** greater than or equal to iLimit. Any pinned pages that meet this 501 ** criteria are unpinned before they are discarded. 502 ** 503 ** The PCache mutex must be held when this function is called. 504 */ 505 static void pcache1TruncateUnsafe( 506 PCache1 *pCache, /* The cache to truncate */ 507 unsigned int iLimit /* Drop pages with this pgno or larger */ 508 ){ 509 TESTONLY( unsigned int nPage = 0; ) /* To assert pCache->nPage is correct */ 510 unsigned int h; 511 assert( sqlite3_mutex_held(pCache->pGroup->mutex) ); 512 for(h=0; h<pCache->nHash; h++){ 513 PgHdr1 **pp = &pCache->apHash[h]; 514 PgHdr1 *pPage; 515 while( (pPage = *pp)!=0 ){ 516 if( pPage->iKey>=iLimit ){ 517 pCache->nPage--; 518 *pp = pPage->pNext; 519 if( !pPage->isPinned ) pcache1PinPage(pPage); 520 pcache1FreePage(pPage); 521 }else{ 522 pp = &pPage->pNext; 523 TESTONLY( nPage++; ) 524 } 525 } 526 } 527 assert( pCache->nPage==nPage ); 528 } 529 530 /******************************************************************************/ 531 /******** sqlite3_pcache Methods **********************************************/ 532 533 /* 534 ** Implementation of the sqlite3_pcache.xInit method. 535 */ 536 static int pcache1Init(void *NotUsed){ 537 UNUSED_PARAMETER(NotUsed); 538 assert( pcache1.isInit==0 ); 539 memset(&pcache1, 0, sizeof(pcache1)); 540 #if SQLITE_THREADSAFE 541 if( sqlite3GlobalConfig.bCoreMutex ){ 542 pcache1.grp.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU); 543 pcache1.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_PMEM); 544 } 545 #endif 546 pcache1.grp.mxPinned = 10; 547 pcache1.isInit = 1; 548 return SQLITE_OK; 549 } 550 551 /* 552 ** Implementation of the sqlite3_pcache.xShutdown method. 553 ** Note that the static mutex allocated in xInit does 554 ** not need to be freed. 555 */ 556 static void pcache1Shutdown(void *NotUsed){ 557 UNUSED_PARAMETER(NotUsed); 558 assert( pcache1.isInit!=0 ); 559 memset(&pcache1, 0, sizeof(pcache1)); 560 } 561 562 /* forward declaration */ 563 static void pcache1Destroy(sqlite3_pcache *p); 564 565 /* 566 ** Implementation of the sqlite3_pcache.xCreate method. 567 ** 568 ** Allocate a new cache. 569 */ 570 static sqlite3_pcache *pcache1Create(int szPage, int szExtra, int bPurgeable){ 571 PCache1 *pCache; /* The newly created page cache */ 572 PGroup *pGroup; /* The group the new page cache will belong to */ 573 int sz; /* Bytes of memory required to allocate the new cache */ 574 575 /* 576 ** The separateCache variable is true if each PCache has its own private 577 ** PGroup. In other words, separateCache is true for mode (1) where no 578 ** mutexing is required. 579 ** 580 ** * Always use a unified cache (mode-2) if ENABLE_MEMORY_MANAGEMENT 581 ** 582 ** * Always use a unified cache in single-threaded applications 583 ** 584 ** * Otherwise (if multi-threaded and ENABLE_MEMORY_MANAGEMENT is off) 585 ** use separate caches (mode-1) 586 */ 587 #if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) || SQLITE_THREADSAFE==0 588 const int separateCache = 0; 589 #else 590 int separateCache = sqlite3GlobalConfig.bCoreMutex>0; 591 #endif 592 593 assert( (szPage & (szPage-1))==0 && szPage>=512 && szPage<=65536 ); 594 assert( szExtra < 300 ); 595 596 sz = sizeof(PCache1) + sizeof(PGroup)*separateCache; 597 pCache = (PCache1 *)sqlite3MallocZero(sz); 598 if( pCache ){ 599 if( separateCache ){ 600 pGroup = (PGroup*)&pCache[1]; 601 pGroup->mxPinned = 10; 602 }else{ 603 pGroup = &pcache1.grp; 604 } 605 pCache->pGroup = pGroup; 606 pCache->szPage = szPage; 607 pCache->szExtra = szExtra; 608 pCache->bPurgeable = (bPurgeable ? 1 : 0); 609 pcache1EnterMutex(pGroup); 610 pcache1ResizeHash(pCache); 611 if( bPurgeable ){ 612 pCache->nMin = 10; 613 pGroup->nMinPage += pCache->nMin; 614 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage; 615 } 616 pcache1LeaveMutex(pGroup); 617 if( pCache->nHash==0 ){ 618 pcache1Destroy((sqlite3_pcache*)pCache); 619 pCache = 0; 620 } 621 } 622 return (sqlite3_pcache *)pCache; 623 } 624 625 /* 626 ** Implementation of the sqlite3_pcache.xCachesize method. 627 ** 628 ** Configure the cache_size limit for a cache. 629 */ 630 static void pcache1Cachesize(sqlite3_pcache *p, int nMax){ 631 PCache1 *pCache = (PCache1 *)p; 632 if( pCache->bPurgeable ){ 633 PGroup *pGroup = pCache->pGroup; 634 pcache1EnterMutex(pGroup); 635 pGroup->nMaxPage += (nMax - pCache->nMax); 636 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage; 637 pCache->nMax = nMax; 638 pCache->n90pct = pCache->nMax*9/10; 639 pcache1EnforceMaxPage(pGroup); 640 pcache1LeaveMutex(pGroup); 641 } 642 } 643 644 /* 645 ** Implementation of the sqlite3_pcache.xShrink method. 646 ** 647 ** Free up as much memory as possible. 648 */ 649 static void pcache1Shrink(sqlite3_pcache *p){ 650 PCache1 *pCache = (PCache1*)p; 651 if( pCache->bPurgeable ){ 652 PGroup *pGroup = pCache->pGroup; 653 int savedMaxPage; 654 pcache1EnterMutex(pGroup); 655 savedMaxPage = pGroup->nMaxPage; 656 pGroup->nMaxPage = 0; 657 pcache1EnforceMaxPage(pGroup); 658 pGroup->nMaxPage = savedMaxPage; 659 pcache1LeaveMutex(pGroup); 660 } 661 } 662 663 /* 664 ** Implementation of the sqlite3_pcache.xPagecount method. 665 */ 666 static int pcache1Pagecount(sqlite3_pcache *p){ 667 int n; 668 PCache1 *pCache = (PCache1*)p; 669 pcache1EnterMutex(pCache->pGroup); 670 n = pCache->nPage; 671 pcache1LeaveMutex(pCache->pGroup); 672 return n; 673 } 674 675 676 /* 677 ** Implement steps 3, 4, and 5 of the pcache1Fetch() algorithm described 678 ** in the header of the pcache1Fetch() procedure. 679 ** 680 ** This steps are broken out into a separate procedure because they are 681 ** usually not needed, and by avoiding the stack initialization required 682 ** for these steps, the main pcache1Fetch() procedure can run faster. 683 */ 684 static SQLITE_NOINLINE PgHdr1 *pcache1FetchStage2( 685 PCache1 *pCache, 686 unsigned int iKey, 687 int createFlag 688 ){ 689 unsigned int nPinned; 690 PGroup *pGroup = pCache->pGroup; 691 PgHdr1 *pPage = 0; 692 693 /* Step 3: Abort if createFlag is 1 but the cache is nearly full */ 694 assert( pCache->nPage >= pCache->nRecyclable ); 695 nPinned = pCache->nPage - pCache->nRecyclable; 696 assert( pGroup->mxPinned == pGroup->nMaxPage + 10 - pGroup->nMinPage ); 697 assert( pCache->n90pct == pCache->nMax*9/10 ); 698 if( createFlag==1 && ( 699 nPinned>=pGroup->mxPinned 700 || nPinned>=pCache->n90pct 701 || (pcache1UnderMemoryPressure(pCache) && pCache->nRecyclable<nPinned) 702 )){ 703 return 0; 704 } 705 706 if( pCache->nPage>=pCache->nHash ) pcache1ResizeHash(pCache); 707 assert( pCache->nHash>0 && pCache->apHash ); 708 709 /* Step 4. Try to recycle a page. */ 710 if( pCache->bPurgeable && pGroup->pLruTail && ( 711 (pCache->nPage+1>=pCache->nMax) 712 || pGroup->nCurrentPage>=pGroup->nMaxPage 713 || pcache1UnderMemoryPressure(pCache) 714 )){ 715 PCache1 *pOther; 716 pPage = pGroup->pLruTail; 717 assert( pPage->isPinned==0 ); 718 pcache1RemoveFromHash(pPage, 0); 719 pcache1PinPage(pPage); 720 pOther = pPage->pCache; 721 722 /* We want to verify that szPage and szExtra are the same for pOther 723 ** and pCache. Assert that we can verify this by comparing sums. */ 724 assert( (pCache->szPage & (pCache->szPage-1))==0 && pCache->szPage>=512 ); 725 assert( pCache->szExtra<512 ); 726 assert( (pOther->szPage & (pOther->szPage-1))==0 && pOther->szPage>=512 ); 727 assert( pOther->szExtra<512 ); 728 729 if( pOther->szPage+pOther->szExtra != pCache->szPage+pCache->szExtra ){ 730 pcache1FreePage(pPage); 731 pPage = 0; 732 }else{ 733 pGroup->nCurrentPage -= (pOther->bPurgeable - pCache->bPurgeable); 734 } 735 } 736 737 /* Step 5. If a usable page buffer has still not been found, 738 ** attempt to allocate a new one. 739 */ 740 if( !pPage ){ 741 if( createFlag==1 ){ sqlite3BeginBenignMalloc(); } 742 pPage = pcache1AllocPage(pCache); 743 if( createFlag==1 ){ sqlite3EndBenignMalloc(); } 744 } 745 746 if( pPage ){ 747 unsigned int h = iKey % pCache->nHash; 748 pCache->nPage++; 749 pPage->iKey = iKey; 750 pPage->pNext = pCache->apHash[h]; 751 pPage->pCache = pCache; 752 pPage->pLruPrev = 0; 753 pPage->pLruNext = 0; 754 pPage->isPinned = 1; 755 *(void **)pPage->page.pExtra = 0; 756 pCache->apHash[h] = pPage; 757 if( iKey>pCache->iMaxKey ){ 758 pCache->iMaxKey = iKey; 759 } 760 } 761 return pPage; 762 } 763 764 /* 765 ** Implementation of the sqlite3_pcache.xFetch method. 766 ** 767 ** Fetch a page by key value. 768 ** 769 ** Whether or not a new page may be allocated by this function depends on 770 ** the value of the createFlag argument. 0 means do not allocate a new 771 ** page. 1 means allocate a new page if space is easily available. 2 772 ** means to try really hard to allocate a new page. 773 ** 774 ** For a non-purgeable cache (a cache used as the storage for an in-memory 775 ** database) there is really no difference between createFlag 1 and 2. So 776 ** the calling function (pcache.c) will never have a createFlag of 1 on 777 ** a non-purgeable cache. 778 ** 779 ** There are three different approaches to obtaining space for a page, 780 ** depending on the value of parameter createFlag (which may be 0, 1 or 2). 781 ** 782 ** 1. Regardless of the value of createFlag, the cache is searched for a 783 ** copy of the requested page. If one is found, it is returned. 784 ** 785 ** 2. If createFlag==0 and the page is not already in the cache, NULL is 786 ** returned. 787 ** 788 ** 3. If createFlag is 1, and the page is not already in the cache, then 789 ** return NULL (do not allocate a new page) if any of the following 790 ** conditions are true: 791 ** 792 ** (a) the number of pages pinned by the cache is greater than 793 ** PCache1.nMax, or 794 ** 795 ** (b) the number of pages pinned by the cache is greater than 796 ** the sum of nMax for all purgeable caches, less the sum of 797 ** nMin for all other purgeable caches, or 798 ** 799 ** 4. If none of the first three conditions apply and the cache is marked 800 ** as purgeable, and if one of the following is true: 801 ** 802 ** (a) The number of pages allocated for the cache is already 803 ** PCache1.nMax, or 804 ** 805 ** (b) The number of pages allocated for all purgeable caches is 806 ** already equal to or greater than the sum of nMax for all 807 ** purgeable caches, 808 ** 809 ** (c) The system is under memory pressure and wants to avoid 810 ** unnecessary pages cache entry allocations 811 ** 812 ** then attempt to recycle a page from the LRU list. If it is the right 813 ** size, return the recycled buffer. Otherwise, free the buffer and 814 ** proceed to step 5. 815 ** 816 ** 5. Otherwise, allocate and return a new page buffer. 817 ** 818 ** There are two versions of this routine. pcache1FetchWithMutex() is 819 ** the general case. pcache1FetchNoMutex() is a faster implementation for 820 ** the common case where pGroup->mutex is NULL. The pcache1Fetch() wrapper 821 ** invokes the appropriate routine. 822 */ 823 static PgHdr1 *pcache1FetchNoMutex( 824 sqlite3_pcache *p, 825 unsigned int iKey, 826 int createFlag 827 ){ 828 PCache1 *pCache = (PCache1 *)p; 829 PgHdr1 *pPage = 0; 830 831 /* Step 1: Search the hash table for an existing entry. */ 832 pPage = pCache->apHash[iKey % pCache->nHash]; 833 while( pPage && pPage->iKey!=iKey ){ pPage = pPage->pNext; } 834 835 /* Step 2: Abort if no existing page is found and createFlag is 0 */ 836 if( pPage ){ 837 if( !pPage->isPinned ){ 838 return pcache1PinPage(pPage); 839 }else{ 840 return pPage; 841 } 842 }else if( createFlag ){ 843 /* Steps 3, 4, and 5 implemented by this subroutine */ 844 return pcache1FetchStage2(pCache, iKey, createFlag); 845 }else{ 846 return 0; 847 } 848 } 849 #if PCACHE1_MIGHT_USE_GROUP_MUTEX 850 static PgHdr1 *pcache1FetchWithMutex( 851 sqlite3_pcache *p, 852 unsigned int iKey, 853 int createFlag 854 ){ 855 PCache1 *pCache = (PCache1 *)p; 856 PgHdr1 *pPage; 857 858 pcache1EnterMutex(pCache->pGroup); 859 pPage = pcache1FetchNoMutex(p, iKey, createFlag); 860 assert( pPage==0 || pCache->iMaxKey>=iKey ); 861 pcache1LeaveMutex(pCache->pGroup); 862 return pPage; 863 } 864 #endif 865 static sqlite3_pcache_page *pcache1Fetch( 866 sqlite3_pcache *p, 867 unsigned int iKey, 868 int createFlag 869 ){ 870 #if PCACHE1_MIGHT_USE_GROUP_MUTEX || defined(SQLITE_DEBUG) 871 PCache1 *pCache = (PCache1 *)p; 872 #endif 873 874 assert( offsetof(PgHdr1,page)==0 ); 875 assert( pCache->bPurgeable || createFlag!=1 ); 876 assert( pCache->bPurgeable || pCache->nMin==0 ); 877 assert( pCache->bPurgeable==0 || pCache->nMin==10 ); 878 assert( pCache->nMin==0 || pCache->bPurgeable ); 879 assert( pCache->nHash>0 ); 880 #if PCACHE1_MIGHT_USE_GROUP_MUTEX 881 if( pCache->pGroup->mutex ){ 882 return (sqlite3_pcache_page*)pcache1FetchWithMutex(p, iKey, createFlag); 883 }else 884 #endif 885 { 886 return (sqlite3_pcache_page*)pcache1FetchNoMutex(p, iKey, createFlag); 887 } 888 } 889 890 891 /* 892 ** Implementation of the sqlite3_pcache.xUnpin method. 893 ** 894 ** Mark a page as unpinned (eligible for asynchronous recycling). 895 */ 896 static void pcache1Unpin( 897 sqlite3_pcache *p, 898 sqlite3_pcache_page *pPg, 899 int reuseUnlikely 900 ){ 901 PCache1 *pCache = (PCache1 *)p; 902 PgHdr1 *pPage = (PgHdr1 *)pPg; 903 PGroup *pGroup = pCache->pGroup; 904 905 assert( pPage->pCache==pCache ); 906 pcache1EnterMutex(pGroup); 907 908 /* It is an error to call this function if the page is already 909 ** part of the PGroup LRU list. 910 */ 911 assert( pPage->pLruPrev==0 && pPage->pLruNext==0 ); 912 assert( pGroup->pLruHead!=pPage && pGroup->pLruTail!=pPage ); 913 assert( pPage->isPinned==1 ); 914 915 if( reuseUnlikely || pGroup->nCurrentPage>pGroup->nMaxPage ){ 916 pcache1RemoveFromHash(pPage, 1); 917 }else{ 918 /* Add the page to the PGroup LRU list. */ 919 if( pGroup->pLruHead ){ 920 pGroup->pLruHead->pLruPrev = pPage; 921 pPage->pLruNext = pGroup->pLruHead; 922 pGroup->pLruHead = pPage; 923 }else{ 924 pGroup->pLruTail = pPage; 925 pGroup->pLruHead = pPage; 926 } 927 pCache->nRecyclable++; 928 pPage->isPinned = 0; 929 } 930 931 pcache1LeaveMutex(pCache->pGroup); 932 } 933 934 /* 935 ** Implementation of the sqlite3_pcache.xRekey method. 936 */ 937 static void pcache1Rekey( 938 sqlite3_pcache *p, 939 sqlite3_pcache_page *pPg, 940 unsigned int iOld, 941 unsigned int iNew 942 ){ 943 PCache1 *pCache = (PCache1 *)p; 944 PgHdr1 *pPage = (PgHdr1 *)pPg; 945 PgHdr1 **pp; 946 unsigned int h; 947 assert( pPage->iKey==iOld ); 948 assert( pPage->pCache==pCache ); 949 950 pcache1EnterMutex(pCache->pGroup); 951 952 h = iOld%pCache->nHash; 953 pp = &pCache->apHash[h]; 954 while( (*pp)!=pPage ){ 955 pp = &(*pp)->pNext; 956 } 957 *pp = pPage->pNext; 958 959 h = iNew%pCache->nHash; 960 pPage->iKey = iNew; 961 pPage->pNext = pCache->apHash[h]; 962 pCache->apHash[h] = pPage; 963 if( iNew>pCache->iMaxKey ){ 964 pCache->iMaxKey = iNew; 965 } 966 967 pcache1LeaveMutex(pCache->pGroup); 968 } 969 970 /* 971 ** Implementation of the sqlite3_pcache.xTruncate method. 972 ** 973 ** Discard all unpinned pages in the cache with a page number equal to 974 ** or greater than parameter iLimit. Any pinned pages with a page number 975 ** equal to or greater than iLimit are implicitly unpinned. 976 */ 977 static void pcache1Truncate(sqlite3_pcache *p, unsigned int iLimit){ 978 PCache1 *pCache = (PCache1 *)p; 979 pcache1EnterMutex(pCache->pGroup); 980 if( iLimit<=pCache->iMaxKey ){ 981 pcache1TruncateUnsafe(pCache, iLimit); 982 pCache->iMaxKey = iLimit-1; 983 } 984 pcache1LeaveMutex(pCache->pGroup); 985 } 986 987 /* 988 ** Implementation of the sqlite3_pcache.xDestroy method. 989 ** 990 ** Destroy a cache allocated using pcache1Create(). 991 */ 992 static void pcache1Destroy(sqlite3_pcache *p){ 993 PCache1 *pCache = (PCache1 *)p; 994 PGroup *pGroup = pCache->pGroup; 995 assert( pCache->bPurgeable || (pCache->nMax==0 && pCache->nMin==0) ); 996 pcache1EnterMutex(pGroup); 997 pcache1TruncateUnsafe(pCache, 0); 998 assert( pGroup->nMaxPage >= pCache->nMax ); 999 pGroup->nMaxPage -= pCache->nMax; 1000 assert( pGroup->nMinPage >= pCache->nMin ); 1001 pGroup->nMinPage -= pCache->nMin; 1002 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage; 1003 pcache1EnforceMaxPage(pGroup); 1004 pcache1LeaveMutex(pGroup); 1005 sqlite3_free(pCache->apHash); 1006 sqlite3_free(pCache); 1007 } 1008 1009 /* 1010 ** This function is called during initialization (sqlite3_initialize()) to 1011 ** install the default pluggable cache module, assuming the user has not 1012 ** already provided an alternative. 1013 */ 1014 void sqlite3PCacheSetDefault(void){ 1015 static const sqlite3_pcache_methods2 defaultMethods = { 1016 1, /* iVersion */ 1017 0, /* pArg */ 1018 pcache1Init, /* xInit */ 1019 pcache1Shutdown, /* xShutdown */ 1020 pcache1Create, /* xCreate */ 1021 pcache1Cachesize, /* xCachesize */ 1022 pcache1Pagecount, /* xPagecount */ 1023 pcache1Fetch, /* xFetch */ 1024 pcache1Unpin, /* xUnpin */ 1025 pcache1Rekey, /* xRekey */ 1026 pcache1Truncate, /* xTruncate */ 1027 pcache1Destroy, /* xDestroy */ 1028 pcache1Shrink /* xShrink */ 1029 }; 1030 sqlite3_config(SQLITE_CONFIG_PCACHE2, &defaultMethods); 1031 } 1032 1033 /* 1034 ** Return the size of the header on each page of this PCACHE implementation. 1035 */ 1036 int sqlite3HeaderSizePcache1(void){ return ROUND8(sizeof(PgHdr1)); } 1037 1038 /* 1039 ** Return the global mutex used by this PCACHE implementation. The 1040 ** sqlite3_status() routine needs access to this mutex. 1041 */ 1042 sqlite3_mutex *sqlite3Pcache1Mutex(void){ 1043 return pcache1.mutex; 1044 } 1045 1046 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT 1047 /* 1048 ** This function is called to free superfluous dynamically allocated memory 1049 ** held by the pager system. Memory in use by any SQLite pager allocated 1050 ** by the current thread may be sqlite3_free()ed. 1051 ** 1052 ** nReq is the number of bytes of memory required. Once this much has 1053 ** been released, the function returns. The return value is the total number 1054 ** of bytes of memory released. 1055 */ 1056 int sqlite3PcacheReleaseMemory(int nReq){ 1057 int nFree = 0; 1058 assert( sqlite3_mutex_notheld(pcache1.grp.mutex) ); 1059 assert( sqlite3_mutex_notheld(pcache1.mutex) ); 1060 if( pcache1.pStart==0 ){ 1061 PgHdr1 *p; 1062 pcache1EnterMutex(&pcache1.grp); 1063 while( (nReq<0 || nFree<nReq) && ((p=pcache1.grp.pLruTail)!=0) ){ 1064 nFree += pcache1MemSize(p->page.pBuf); 1065 #ifdef SQLITE_PCACHE_SEPARATE_HEADER 1066 nFree += sqlite3MemSize(p); 1067 #endif 1068 assert( p->isPinned==0 ); 1069 pcache1PinPage(p); 1070 pcache1RemoveFromHash(p, 1); 1071 } 1072 pcache1LeaveMutex(&pcache1.grp); 1073 } 1074 return nFree; 1075 } 1076 #endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */ 1077 1078 #ifdef SQLITE_TEST 1079 /* 1080 ** This function is used by test procedures to inspect the internal state 1081 ** of the global cache. 1082 */ 1083 void sqlite3PcacheStats( 1084 int *pnCurrent, /* OUT: Total number of pages cached */ 1085 int *pnMax, /* OUT: Global maximum cache size */ 1086 int *pnMin, /* OUT: Sum of PCache1.nMin for purgeable caches */ 1087 int *pnRecyclable /* OUT: Total number of pages available for recycling */ 1088 ){ 1089 PgHdr1 *p; 1090 int nRecyclable = 0; 1091 for(p=pcache1.grp.pLruHead; p; p=p->pLruNext){ 1092 assert( p->isPinned==0 ); 1093 nRecyclable++; 1094 } 1095 *pnCurrent = pcache1.grp.nCurrentPage; 1096 *pnMax = (int)pcache1.grp.nMaxPage; 1097 *pnMin = (int)pcache1.grp.nMinPage; 1098 *pnRecyclable = nRecyclable; 1099 } 1100 #endif 1101