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