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