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