4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
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.
11 *************************************************************************
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.
19 ** A Page cache line looks like this:
21 ** -------------------------------------------------------------
22 ** | database page content | PgHdr1 | MemPage | PgHdr |
23 ** -------------------------------------------------------------
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.
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.
42 ** Historical note: It used to be that if the SQLITE_PCACHE_SEPARATE_HEADER
43 ** was defined, then the page content would be held in a separate memory
44 ** allocation from the PgHdr1. This was intended to avoid clownshoe memory
45 ** allocations. However, the btree layer needs a small (16-byte) overrun
46 ** area after the page content buffer. The header serves as that overrun
47 ** area. Therefore SQLITE_PCACHE_SEPARATE_HEADER was discontinued to avoid
48 ** any possibility of a memory error.
50 ** This module tracks pointers to PgHdr1 objects. Only pcache.c communicates
51 ** with this module. Information is passed back and forth as PgHdr1 pointers.
53 ** The pcache.c and pager.c modules deal pointers to PgHdr objects.
54 ** The btree.c module deals with pointers to MemPage objects.
56 ** SOURCE OF PAGE CACHE MEMORY:
58 ** Memory for a page might come from any of three sources:
60 ** (1) The general-purpose memory allocator - sqlite3Malloc()
61 ** (2) Global page-cache memory provided using sqlite3_config() with
62 ** SQLITE_CONFIG_PAGECACHE.
63 ** (3) PCache-local bulk allocation.
65 ** The third case is a chunk of heap memory (defaulting to 100 pages worth)
66 ** that is allocated when the page cache is created. The size of the local
67 ** bulk allocation can be adjusted using
69 ** sqlite3_config(SQLITE_CONFIG_PAGECACHE, (void*)0, 0, N).
71 ** If N is positive, then N pages worth of memory are allocated using a single
72 ** sqlite3Malloc() call and that memory is used for the first N pages allocated.
73 ** Or if N is negative, then -1024*N bytes of memory are allocated and used
74 ** for as many pages as can be accommodated.
76 ** Only one of (2) or (3) can be used. Once the memory available to (2) or
77 ** (3) is exhausted, subsequent allocations fail over to the general-purpose
78 ** memory allocator (1).
80 ** Earlier versions of SQLite used only methods (1) and (2). But experiments
81 ** show that method (3) with N==100 provides about a 5% performance boost for
84 #include "sqliteInt.h"
86 typedef struct PCache1 PCache1
;
87 typedef struct PgHdr1 PgHdr1
;
88 typedef struct PgFreeslot PgFreeslot
;
89 typedef struct PGroup PGroup
;
92 ** Each cache entry is represented by an instance of the following
93 ** structure. A buffer of PgHdr1.pCache->szPage bytes is allocated
94 ** directly before this structure and is used to cache the page content.
96 ** When reading a corrupt database file, it is possible that SQLite might
97 ** read a few bytes (no more than 16 bytes) past the end of the page buffer.
98 ** It will only read past the end of the page buffer, never write. This
99 ** object is positioned immediately after the page buffer to serve as an
100 ** overrun area, so that overreads are harmless.
102 ** Variables isBulkLocal and isAnchor were once type "u8". That works,
103 ** but causes a 2-byte gap in the structure for most architectures (since
104 ** pointers must be either 4 or 8-byte aligned). As this structure is located
105 ** in memory directly after the associated page data, if the database is
106 ** corrupt, code at the b-tree layer may overread the page buffer and
107 ** read part of this structure before the corruption is detected. This
108 ** can cause a valgrind error if the uninitialized gap is accessed. Using u16
109 ** ensures there is no such gap, and therefore no bytes of uninitialized
110 ** memory in the structure.
112 ** The pLruNext and pLruPrev pointers form a double-linked circular list
113 ** of all pages that are unpinned. The PGroup.lru element (which should be
114 ** the only element on the list with PgHdr1.isAnchor set to 1) forms the
115 ** beginning and the end of the list.
118 sqlite3_pcache_page page
; /* Base class. Must be first. pBuf & pExtra */
119 unsigned int iKey
; /* Key value (page number) */
120 u16 isBulkLocal
; /* This page from bulk local storage */
121 u16 isAnchor
; /* This is the PGroup.lru element */
122 PgHdr1
*pNext
; /* Next in hash table chain */
123 PCache1
*pCache
; /* Cache that currently owns this page */
124 PgHdr1
*pLruNext
; /* Next in circular LRU list of unpinned pages */
125 PgHdr1
*pLruPrev
; /* Previous in LRU list of unpinned pages */
126 /* NB: pLruPrev is only valid if pLruNext!=0 */
130 ** A page is pinned if it is not on the LRU list. To be "pinned" means
131 ** that the page is in active use and must not be deallocated.
133 #define PAGE_IS_PINNED(p) ((p)->pLruNext==0)
134 #define PAGE_IS_UNPINNED(p) ((p)->pLruNext!=0)
136 /* Each page cache (or PCache) belongs to a PGroup. A PGroup is a set
137 ** of one or more PCaches that are able to recycle each other's unpinned
138 ** pages when they are under memory pressure. A PGroup is an instance of
139 ** the following object.
141 ** This page cache implementation works in one of two modes:
143 ** (1) Every PCache is the sole member of its own PGroup. There is
144 ** one PGroup per PCache.
146 ** (2) There is a single global PGroup that all PCaches are a member
149 ** Mode 1 uses more memory (since PCache instances are not able to rob
150 ** unused pages from other PCaches) but it also operates without a mutex,
151 ** and is therefore often faster. Mode 2 requires a mutex in order to be
152 ** threadsafe, but recycles pages more efficiently.
154 ** For mode (1), PGroup.mutex is NULL. For mode (2) there is only a single
155 ** PGroup which is the pcache1.grp global variable and its mutex is
156 ** SQLITE_MUTEX_STATIC_LRU.
159 sqlite3_mutex
*mutex
; /* MUTEX_STATIC_LRU or NULL */
160 unsigned int nMaxPage
; /* Sum of nMax for purgeable caches */
161 unsigned int nMinPage
; /* Sum of nMin for purgeable caches */
162 unsigned int mxPinned
; /* nMaxpage + 10 - nMinPage */
163 unsigned int nPurgeable
; /* Number of purgeable pages allocated */
164 PgHdr1 lru
; /* The beginning and end of the LRU list */
167 /* Each page cache is an instance of the following object. Every
168 ** open database file (including each in-memory database and each
169 ** temporary or transient database) has a single page cache which
170 ** is an instance of this object.
172 ** Pointers to structures of this type are cast and returned as
173 ** opaque sqlite3_pcache* handles.
176 /* Cache configuration parameters. Page size (szPage) and the purgeable
177 ** flag (bPurgeable) and the pnPurgeable pointer are all set when the
178 ** cache is created and are never changed thereafter. nMax may be
179 ** modified at any time by a call to the pcache1Cachesize() method.
180 ** The PGroup mutex must be held when accessing nMax.
182 PGroup
*pGroup
; /* PGroup this cache belongs to */
183 unsigned int *pnPurgeable
; /* Pointer to pGroup->nPurgeable */
184 int szPage
; /* Size of database content section */
185 int szExtra
; /* sizeof(MemPage)+sizeof(PgHdr) */
186 int szAlloc
; /* Total size of one pcache line */
187 int bPurgeable
; /* True if cache is purgeable */
188 unsigned int nMin
; /* Minimum number of pages reserved */
189 unsigned int nMax
; /* Configured "cache_size" value */
190 unsigned int n90pct
; /* nMax*9/10 */
191 unsigned int iMaxKey
; /* Largest key seen since xTruncate() */
192 unsigned int nPurgeableDummy
; /* pnPurgeable points here when not used*/
194 /* Hash table of all pages. The following variables may only be accessed
195 ** when the accessor is holding the PGroup mutex.
197 unsigned int nRecyclable
; /* Number of pages in the LRU list */
198 unsigned int nPage
; /* Total number of pages in apHash */
199 unsigned int nHash
; /* Number of slots in apHash[] */
200 PgHdr1
**apHash
; /* Hash table for fast lookup by key */
201 PgHdr1
*pFree
; /* List of unused pcache-local pages */
202 void *pBulk
; /* Bulk memory used by pcache-local */
206 ** Free slots in the allocator used to divide up the global page cache
207 ** buffer provided using the SQLITE_CONFIG_PAGECACHE mechanism.
210 PgFreeslot
*pNext
; /* Next free slot */
214 ** Global data used by this cache.
216 static SQLITE_WSD
struct PCacheGlobal
{
217 PGroup grp
; /* The global PGroup for mode (2) */
219 /* Variables related to SQLITE_CONFIG_PAGECACHE settings. The
220 ** szSlot, nSlot, pStart, pEnd, nReserve, and isInit values are all
221 ** fixed at sqlite3_initialize() time and do not require mutex protection.
222 ** The nFreeSlot and pFree values do require mutex protection.
224 int isInit
; /* True if initialized */
225 int separateCache
; /* Use a new PGroup for each PCache */
226 int nInitPage
; /* Initial bulk allocation size */
227 int szSlot
; /* Size of each free slot */
228 int nSlot
; /* The number of pcache slots */
229 int nReserve
; /* Try to keep nFreeSlot above this */
230 void *pStart
, *pEnd
; /* Bounds of global page cache memory */
231 /* Above requires no mutex. Use mutex below for variable that follow. */
232 sqlite3_mutex
*mutex
; /* Mutex for accessing the following: */
233 PgFreeslot
*pFree
; /* Free page blocks */
234 int nFreeSlot
; /* Number of unused pcache slots */
235 /* The following value requires a mutex to change. We skip the mutex on
236 ** reading because (1) most platforms read a 32-bit integer atomically and
237 ** (2) even if an incorrect value is read, no great harm is done since this
238 ** is really just an optimization. */
239 int bUnderPressure
; /* True if low on PAGECACHE memory */
243 ** All code in this file should access the global structure above via the
244 ** alias "pcache1". This ensures that the WSD emulation is used when
245 ** compiling for systems that do not support real WSD.
247 #define pcache1 (GLOBAL(struct PCacheGlobal, pcache1_g))
250 ** Macros to enter and leave the PCache LRU mutex.
252 #if !defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) || SQLITE_THREADSAFE==0
253 # define pcache1EnterMutex(X) assert((X)->mutex==0)
254 # define pcache1LeaveMutex(X) assert((X)->mutex==0)
255 # define PCACHE1_MIGHT_USE_GROUP_MUTEX 0
257 # define pcache1EnterMutex(X) sqlite3_mutex_enter((X)->mutex)
258 # define pcache1LeaveMutex(X) sqlite3_mutex_leave((X)->mutex)
259 # define PCACHE1_MIGHT_USE_GROUP_MUTEX 1
262 /******************************************************************************/
263 /******** Page Allocation/SQLITE_CONFIG_PCACHE Related Functions **************/
267 ** This function is called during initialization if a static buffer is
268 ** supplied to use for the page-cache by passing the SQLITE_CONFIG_PAGECACHE
269 ** verb to sqlite3_config(). Parameter pBuf points to an allocation large
270 ** enough to contain 'n' buffers of 'sz' bytes each.
272 ** This routine is called from sqlite3_initialize() and so it is guaranteed
273 ** to be serialized already. There is no need for further mutexing.
275 void sqlite3PCacheBufferSetup(void *pBuf
, int sz
, int n
){
276 if( pcache1
.isInit
){
278 if( pBuf
==0 ) sz
= n
= 0;
282 pcache1
.nSlot
= pcache1
.nFreeSlot
= n
;
283 pcache1
.nReserve
= n
>90 ? 10 : (n
/10 + 1);
284 pcache1
.pStart
= pBuf
;
286 pcache1
.bUnderPressure
= 0;
288 p
= (PgFreeslot
*)pBuf
;
289 p
->pNext
= pcache1
.pFree
;
291 pBuf
= (void*)&((char*)pBuf
)[sz
];
298 ** Try to initialize the pCache->pFree and pCache->pBulk fields. Return
299 ** true if pCache->pFree ends up containing one or more free pages.
301 static int pcache1InitBulk(PCache1
*pCache
){
304 if( pcache1
.nInitPage
==0 ) return 0;
305 /* Do not bother with a bulk allocation if the cache size very small */
306 if( pCache
->nMax
<3 ) return 0;
307 sqlite3BeginBenignMalloc();
308 if( pcache1
.nInitPage
>0 ){
309 szBulk
= pCache
->szAlloc
* (i64
)pcache1
.nInitPage
;
311 szBulk
= -1024 * (i64
)pcache1
.nInitPage
;
313 if( szBulk
> pCache
->szAlloc
*(i64
)pCache
->nMax
){
314 szBulk
= pCache
->szAlloc
*(i64
)pCache
->nMax
;
316 zBulk
= pCache
->pBulk
= sqlite3Malloc( szBulk
);
317 sqlite3EndBenignMalloc();
319 int nBulk
= sqlite3MallocSize(zBulk
)/pCache
->szAlloc
;
321 PgHdr1
*pX
= (PgHdr1
*)&zBulk
[pCache
->szPage
];
322 pX
->page
.pBuf
= zBulk
;
323 pX
->page
.pExtra
= (u8
*)pX
+ ROUND8(sizeof(*pX
));
324 assert( EIGHT_BYTE_ALIGNMENT( pX
->page
.pExtra
) );
327 pX
->pNext
= pCache
->pFree
;
328 pX
->pLruPrev
= 0; /* Initializing this saves a valgrind error */
330 zBulk
+= pCache
->szAlloc
;
333 return pCache
->pFree
!=0;
337 ** Malloc function used within this file to allocate space from the buffer
338 ** configured using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no
339 ** such buffer exists or there is no space left in it, this function falls
340 ** back to sqlite3Malloc().
342 ** Multiple threads can run this routine at the same time. Global variables
343 ** in pcache1 need to be protected via mutex.
345 static void *pcache1Alloc(int nByte
){
347 assert( sqlite3_mutex_notheld(pcache1
.grp
.mutex
) );
348 if( nByte
<=pcache1
.szSlot
){
349 sqlite3_mutex_enter(pcache1
.mutex
);
350 p
= (PgHdr1
*)pcache1
.pFree
;
352 pcache1
.pFree
= pcache1
.pFree
->pNext
;
354 pcache1
.bUnderPressure
= pcache1
.nFreeSlot
<pcache1
.nReserve
;
355 assert( pcache1
.nFreeSlot
>=0 );
356 sqlite3StatusHighwater(SQLITE_STATUS_PAGECACHE_SIZE
, nByte
);
357 sqlite3StatusUp(SQLITE_STATUS_PAGECACHE_USED
, 1);
359 sqlite3_mutex_leave(pcache1
.mutex
);
362 /* Memory is not available in the SQLITE_CONFIG_PAGECACHE pool. Get
363 ** it from sqlite3Malloc instead.
365 p
= sqlite3Malloc(nByte
);
366 #ifndef SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS
368 int sz
= sqlite3MallocSize(p
);
369 sqlite3_mutex_enter(pcache1
.mutex
);
370 sqlite3StatusHighwater(SQLITE_STATUS_PAGECACHE_SIZE
, nByte
);
371 sqlite3StatusUp(SQLITE_STATUS_PAGECACHE_OVERFLOW
, sz
);
372 sqlite3_mutex_leave(pcache1
.mutex
);
375 sqlite3MemdebugSetType(p
, MEMTYPE_PCACHE
);
381 ** Free an allocated buffer obtained from pcache1Alloc().
383 static void pcache1Free(void *p
){
385 if( SQLITE_WITHIN(p
, pcache1
.pStart
, pcache1
.pEnd
) ){
387 sqlite3_mutex_enter(pcache1
.mutex
);
388 sqlite3StatusDown(SQLITE_STATUS_PAGECACHE_USED
, 1);
389 pSlot
= (PgFreeslot
*)p
;
390 pSlot
->pNext
= pcache1
.pFree
;
391 pcache1
.pFree
= pSlot
;
393 pcache1
.bUnderPressure
= pcache1
.nFreeSlot
<pcache1
.nReserve
;
394 assert( pcache1
.nFreeSlot
<=pcache1
.nSlot
);
395 sqlite3_mutex_leave(pcache1
.mutex
);
397 assert( sqlite3MemdebugHasType(p
, MEMTYPE_PCACHE
) );
398 sqlite3MemdebugSetType(p
, MEMTYPE_HEAP
);
399 #ifndef SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS
402 nFreed
= sqlite3MallocSize(p
);
403 sqlite3_mutex_enter(pcache1
.mutex
);
404 sqlite3StatusDown(SQLITE_STATUS_PAGECACHE_OVERFLOW
, nFreed
);
405 sqlite3_mutex_leave(pcache1
.mutex
);
412 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
414 ** Return the size of a pcache allocation
416 static int pcache1MemSize(void *p
){
417 if( p
>=pcache1
.pStart
&& p
<pcache1
.pEnd
){
418 return pcache1
.szSlot
;
421 assert( sqlite3MemdebugHasType(p
, MEMTYPE_PCACHE
) );
422 sqlite3MemdebugSetType(p
, MEMTYPE_HEAP
);
423 iSize
= sqlite3MallocSize(p
);
424 sqlite3MemdebugSetType(p
, MEMTYPE_PCACHE
);
428 #endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
431 ** Allocate a new page object initially associated with cache pCache.
433 static PgHdr1
*pcache1AllocPage(PCache1
*pCache
, int benignMalloc
){
437 assert( sqlite3_mutex_held(pCache
->pGroup
->mutex
) );
438 if( pCache
->pFree
|| (pCache
->nPage
==0 && pcache1InitBulk(pCache
)) ){
439 assert( pCache
->pFree
!=0 );
441 pCache
->pFree
= p
->pNext
;
444 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
445 /* The group mutex must be released before pcache1Alloc() is called. This
446 ** is because it might call sqlite3_release_memory(), which assumes that
447 ** this mutex is not held. */
448 assert( pcache1
.separateCache
==0 );
449 assert( pCache
->pGroup
==&pcache1
.grp
);
450 pcache1LeaveMutex(pCache
->pGroup
);
452 if( benignMalloc
){ sqlite3BeginBenignMalloc(); }
453 pPg
= pcache1Alloc(pCache
->szAlloc
);
454 if( benignMalloc
){ sqlite3EndBenignMalloc(); }
455 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
456 pcache1EnterMutex(pCache
->pGroup
);
458 if( pPg
==0 ) return 0;
459 p
= (PgHdr1
*)&((u8
*)pPg
)[pCache
->szPage
];
461 p
->page
.pExtra
= (u8
*)p
+ ROUND8(sizeof(*p
));
462 assert( EIGHT_BYTE_ALIGNMENT( p
->page
.pExtra
) );
465 p
->pLruPrev
= 0; /* Initializing this saves a valgrind error */
467 (*pCache
->pnPurgeable
)++;
472 ** Free a page object allocated by pcache1AllocPage().
474 static void pcache1FreePage(PgHdr1
*p
){
478 assert( sqlite3_mutex_held(p
->pCache
->pGroup
->mutex
) );
479 if( p
->isBulkLocal
){
480 p
->pNext
= pCache
->pFree
;
483 pcache1Free(p
->page
.pBuf
);
485 (*pCache
->pnPurgeable
)--;
489 ** Malloc function used by SQLite to obtain space from the buffer configured
490 ** using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no such buffer
491 ** exists, this function falls back to sqlite3Malloc().
493 void *sqlite3PageMalloc(int sz
){
494 assert( sz
<=65536+8 ); /* These allocations are never very large */
495 return pcache1Alloc(sz
);
499 ** Free an allocated buffer obtained from sqlite3PageMalloc().
501 void sqlite3PageFree(void *p
){
507 ** Return true if it desirable to avoid allocating a new page cache
510 ** If memory was allocated specifically to the page cache using
511 ** SQLITE_CONFIG_PAGECACHE but that memory has all been used, then
512 ** it is desirable to avoid allocating a new page cache entry because
513 ** presumably SQLITE_CONFIG_PAGECACHE was suppose to be sufficient
514 ** for all page cache needs and we should not need to spill the
515 ** allocation onto the heap.
517 ** Or, the heap is used for all page cache memory but the heap is
518 ** under memory pressure, then again it is desirable to avoid
519 ** allocating a new page cache entry in order to avoid stressing
520 ** the heap even further.
522 static int pcache1UnderMemoryPressure(PCache1
*pCache
){
523 if( pcache1
.nSlot
&& (pCache
->szPage
+pCache
->szExtra
)<=pcache1
.szSlot
){
524 return pcache1
.bUnderPressure
;
526 return sqlite3HeapNearlyFull();
530 /******************************************************************************/
531 /******** General Implementation Functions ************************************/
534 ** This function is used to resize the hash table used by the cache passed
535 ** as the first argument.
537 ** The PCache mutex must be held when this function is called.
539 static void pcache1ResizeHash(PCache1
*p
){
544 assert( sqlite3_mutex_held(p
->pGroup
->mutex
) );
551 pcache1LeaveMutex(p
->pGroup
);
552 if( p
->nHash
){ sqlite3BeginBenignMalloc(); }
553 apNew
= (PgHdr1
**)sqlite3MallocZero(sizeof(PgHdr1
*)*nNew
);
554 if( p
->nHash
){ sqlite3EndBenignMalloc(); }
555 pcache1EnterMutex(p
->pGroup
);
557 for(i
=0; i
<p
->nHash
; i
++){
559 PgHdr1
*pNext
= p
->apHash
[i
];
560 while( (pPage
= pNext
)!=0 ){
561 unsigned int h
= pPage
->iKey
% nNew
;
562 pNext
= pPage
->pNext
;
563 pPage
->pNext
= apNew
[h
];
567 sqlite3_free(p
->apHash
);
574 ** This function is used internally to remove the page pPage from the
575 ** PGroup LRU list, if is part of it. If pPage is not part of the PGroup
576 ** LRU list, then this function is a no-op.
578 ** The PGroup mutex must be held when this function is called.
580 static PgHdr1
*pcache1PinPage(PgHdr1
*pPage
){
582 assert( PAGE_IS_UNPINNED(pPage
) );
583 assert( pPage
->pLruNext
);
584 assert( pPage
->pLruPrev
);
585 assert( sqlite3_mutex_held(pPage
->pCache
->pGroup
->mutex
) );
586 pPage
->pLruPrev
->pLruNext
= pPage
->pLruNext
;
587 pPage
->pLruNext
->pLruPrev
= pPage
->pLruPrev
;
589 /* pPage->pLruPrev = 0;
590 ** No need to clear pLruPrev as it is never accessed if pLruNext is 0 */
591 assert( pPage
->isAnchor
==0 );
592 assert( pPage
->pCache
->pGroup
->lru
.isAnchor
==1 );
593 pPage
->pCache
->nRecyclable
--;
599 ** Remove the page supplied as an argument from the hash table
600 ** (PCache1.apHash structure) that it is currently stored in.
601 ** Also free the page if freePage is true.
603 ** The PGroup mutex must be held when this function is called.
605 static void pcache1RemoveFromHash(PgHdr1
*pPage
, int freeFlag
){
607 PCache1
*pCache
= pPage
->pCache
;
610 assert( sqlite3_mutex_held(pCache
->pGroup
->mutex
) );
611 h
= pPage
->iKey
% pCache
->nHash
;
612 for(pp
=&pCache
->apHash
[h
]; (*pp
)!=pPage
; pp
=&(*pp
)->pNext
);
616 if( freeFlag
) pcache1FreePage(pPage
);
620 ** If there are currently more than nMaxPage pages allocated, try
621 ** to recycle pages to reduce the number allocated to nMaxPage.
623 static void pcache1EnforceMaxPage(PCache1
*pCache
){
624 PGroup
*pGroup
= pCache
->pGroup
;
626 assert( sqlite3_mutex_held(pGroup
->mutex
) );
627 while( pGroup
->nPurgeable
>pGroup
->nMaxPage
628 && (p
=pGroup
->lru
.pLruPrev
)->isAnchor
==0
630 assert( p
->pCache
->pGroup
==pGroup
);
631 assert( PAGE_IS_UNPINNED(p
) );
633 pcache1RemoveFromHash(p
, 1);
635 if( pCache
->nPage
==0 && pCache
->pBulk
){
636 sqlite3_free(pCache
->pBulk
);
637 pCache
->pBulk
= pCache
->pFree
= 0;
642 ** Discard all pages from cache pCache with a page number (key value)
643 ** greater than or equal to iLimit. Any pinned pages that meet this
644 ** criteria are unpinned before they are discarded.
646 ** The PCache mutex must be held when this function is called.
648 static void pcache1TruncateUnsafe(
649 PCache1
*pCache
, /* The cache to truncate */
650 unsigned int iLimit
/* Drop pages with this pgno or larger */
652 TESTONLY( int nPage
= 0; ) /* To assert pCache->nPage is correct */
653 unsigned int h
, iStop
;
654 assert( sqlite3_mutex_held(pCache
->pGroup
->mutex
) );
655 assert( pCache
->iMaxKey
>= iLimit
);
656 assert( pCache
->nHash
> 0 );
657 if( pCache
->iMaxKey
- iLimit
< pCache
->nHash
){
658 /* If we are just shaving the last few pages off the end of the
659 ** cache, then there is no point in scanning the entire hash table.
660 ** Only scan those hash slots that might contain pages that need to
662 h
= iLimit
% pCache
->nHash
;
663 iStop
= pCache
->iMaxKey
% pCache
->nHash
;
664 TESTONLY( nPage
= -10; ) /* Disable the pCache->nPage validity check */
666 /* This is the general case where many pages are being removed.
667 ** It is necessary to scan the entire hash table */
674 assert( h
<pCache
->nHash
);
675 pp
= &pCache
->apHash
[h
];
676 while( (pPage
= *pp
)!=0 ){
677 if( pPage
->iKey
>=iLimit
){
680 if( PAGE_IS_UNPINNED(pPage
) ) pcache1PinPage(pPage
);
681 pcache1FreePage(pPage
);
684 TESTONLY( if( nPage
>=0 ) nPage
++; )
687 if( h
==iStop
) break;
688 h
= (h
+1) % pCache
->nHash
;
690 assert( nPage
<0 || pCache
->nPage
==(unsigned)nPage
);
693 /******************************************************************************/
694 /******** sqlite3_pcache Methods **********************************************/
697 ** Implementation of the sqlite3_pcache.xInit method.
699 static int pcache1Init(void *NotUsed
){
700 UNUSED_PARAMETER(NotUsed
);
701 assert( pcache1
.isInit
==0 );
702 memset(&pcache1
, 0, sizeof(pcache1
));
706 ** The pcache1.separateCache variable is true if each PCache has its own
707 ** private PGroup (mode-1). pcache1.separateCache is false if the single
708 ** PGroup in pcache1.grp is used for all page caches (mode-2).
710 ** * Always use a unified cache (mode-2) if ENABLE_MEMORY_MANAGEMENT
712 ** * Use a unified cache in single-threaded applications that have
713 ** configured a start-time buffer for use as page-cache memory using
714 ** sqlite3_config(SQLITE_CONFIG_PAGECACHE, pBuf, sz, N) with non-NULL
717 ** * Otherwise use separate caches (mode-1)
719 #if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT)
720 pcache1
.separateCache
= 0;
721 #elif SQLITE_THREADSAFE
722 pcache1
.separateCache
= sqlite3GlobalConfig
.pPage
==0
723 || sqlite3GlobalConfig
.bCoreMutex
>0;
725 pcache1
.separateCache
= sqlite3GlobalConfig
.pPage
==0;
728 #if SQLITE_THREADSAFE
729 if( sqlite3GlobalConfig
.bCoreMutex
){
730 pcache1
.grp
.mutex
= sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU
);
731 pcache1
.mutex
= sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_PMEM
);
734 if( pcache1
.separateCache
735 && sqlite3GlobalConfig
.nPage
!=0
736 && sqlite3GlobalConfig
.pPage
==0
738 pcache1
.nInitPage
= sqlite3GlobalConfig
.nPage
;
740 pcache1
.nInitPage
= 0;
742 pcache1
.grp
.mxPinned
= 10;
748 ** Implementation of the sqlite3_pcache.xShutdown method.
749 ** Note that the static mutex allocated in xInit does
750 ** not need to be freed.
752 static void pcache1Shutdown(void *NotUsed
){
753 UNUSED_PARAMETER(NotUsed
);
754 assert( pcache1
.isInit
!=0 );
755 memset(&pcache1
, 0, sizeof(pcache1
));
758 /* forward declaration */
759 static void pcache1Destroy(sqlite3_pcache
*p
);
762 ** Implementation of the sqlite3_pcache.xCreate method.
764 ** Allocate a new cache.
766 static sqlite3_pcache
*pcache1Create(int szPage
, int szExtra
, int bPurgeable
){
767 PCache1
*pCache
; /* The newly created page cache */
768 PGroup
*pGroup
; /* The group the new page cache will belong to */
769 int sz
; /* Bytes of memory required to allocate the new cache */
771 assert( (szPage
& (szPage
-1))==0 && szPage
>=512 && szPage
<=65536 );
772 assert( szExtra
< 300 );
774 sz
= sizeof(PCache1
) + sizeof(PGroup
)*pcache1
.separateCache
;
775 pCache
= (PCache1
*)sqlite3MallocZero(sz
);
777 if( pcache1
.separateCache
){
778 pGroup
= (PGroup
*)&pCache
[1];
779 pGroup
->mxPinned
= 10;
781 pGroup
= &pcache1
.grp
;
783 pcache1EnterMutex(pGroup
);
784 if( pGroup
->lru
.isAnchor
==0 ){
785 pGroup
->lru
.isAnchor
= 1;
786 pGroup
->lru
.pLruPrev
= pGroup
->lru
.pLruNext
= &pGroup
->lru
;
788 pCache
->pGroup
= pGroup
;
789 pCache
->szPage
= szPage
;
790 pCache
->szExtra
= szExtra
;
791 pCache
->szAlloc
= szPage
+ szExtra
+ ROUND8(sizeof(PgHdr1
));
792 pCache
->bPurgeable
= (bPurgeable
? 1 : 0);
793 pcache1ResizeHash(pCache
);
796 pGroup
->nMinPage
+= pCache
->nMin
;
797 pGroup
->mxPinned
= pGroup
->nMaxPage
+ 10 - pGroup
->nMinPage
;
798 pCache
->pnPurgeable
= &pGroup
->nPurgeable
;
800 pCache
->pnPurgeable
= &pCache
->nPurgeableDummy
;
802 pcache1LeaveMutex(pGroup
);
803 if( pCache
->nHash
==0 ){
804 pcache1Destroy((sqlite3_pcache
*)pCache
);
808 return (sqlite3_pcache
*)pCache
;
812 ** Implementation of the sqlite3_pcache.xCachesize method.
814 ** Configure the cache_size limit for a cache.
816 static void pcache1Cachesize(sqlite3_pcache
*p
, int nMax
){
817 PCache1
*pCache
= (PCache1
*)p
;
820 if( pCache
->bPurgeable
){
821 PGroup
*pGroup
= pCache
->pGroup
;
822 pcache1EnterMutex(pGroup
);
824 if( n
> 0x7fff0000 - pGroup
->nMaxPage
+ pCache
->nMax
){
825 n
= 0x7fff0000 - pGroup
->nMaxPage
+ pCache
->nMax
;
827 pGroup
->nMaxPage
+= (n
- pCache
->nMax
);
828 pGroup
->mxPinned
= pGroup
->nMaxPage
+ 10 - pGroup
->nMinPage
;
830 pCache
->n90pct
= pCache
->nMax
*9/10;
831 pcache1EnforceMaxPage(pCache
);
832 pcache1LeaveMutex(pGroup
);
837 ** Implementation of the sqlite3_pcache.xShrink method.
839 ** Free up as much memory as possible.
841 static void pcache1Shrink(sqlite3_pcache
*p
){
842 PCache1
*pCache
= (PCache1
*)p
;
843 if( pCache
->bPurgeable
){
844 PGroup
*pGroup
= pCache
->pGroup
;
845 unsigned int savedMaxPage
;
846 pcache1EnterMutex(pGroup
);
847 savedMaxPage
= pGroup
->nMaxPage
;
848 pGroup
->nMaxPage
= 0;
849 pcache1EnforceMaxPage(pCache
);
850 pGroup
->nMaxPage
= savedMaxPage
;
851 pcache1LeaveMutex(pGroup
);
856 ** Implementation of the sqlite3_pcache.xPagecount method.
858 static int pcache1Pagecount(sqlite3_pcache
*p
){
860 PCache1
*pCache
= (PCache1
*)p
;
861 pcache1EnterMutex(pCache
->pGroup
);
863 pcache1LeaveMutex(pCache
->pGroup
);
869 ** Implement steps 3, 4, and 5 of the pcache1Fetch() algorithm described
870 ** in the header of the pcache1Fetch() procedure.
872 ** This steps are broken out into a separate procedure because they are
873 ** usually not needed, and by avoiding the stack initialization required
874 ** for these steps, the main pcache1Fetch() procedure can run faster.
876 static SQLITE_NOINLINE PgHdr1
*pcache1FetchStage2(
881 unsigned int nPinned
;
882 PGroup
*pGroup
= pCache
->pGroup
;
885 /* Step 3: Abort if createFlag is 1 but the cache is nearly full */
886 assert( pCache
->nPage
>= pCache
->nRecyclable
);
887 nPinned
= pCache
->nPage
- pCache
->nRecyclable
;
888 assert( pGroup
->mxPinned
== pGroup
->nMaxPage
+ 10 - pGroup
->nMinPage
);
889 assert( pCache
->n90pct
== pCache
->nMax
*9/10 );
890 if( createFlag
==1 && (
891 nPinned
>=pGroup
->mxPinned
892 || nPinned
>=pCache
->n90pct
893 || (pcache1UnderMemoryPressure(pCache
) && pCache
->nRecyclable
<nPinned
)
898 if( pCache
->nPage
>=pCache
->nHash
) pcache1ResizeHash(pCache
);
899 assert( pCache
->nHash
>0 && pCache
->apHash
);
901 /* Step 4. Try to recycle a page. */
902 if( pCache
->bPurgeable
903 && !pGroup
->lru
.pLruPrev
->isAnchor
904 && ((pCache
->nPage
+1>=pCache
->nMax
) || pcache1UnderMemoryPressure(pCache
))
907 pPage
= pGroup
->lru
.pLruPrev
;
908 assert( PAGE_IS_UNPINNED(pPage
) );
909 pcache1RemoveFromHash(pPage
, 0);
910 pcache1PinPage(pPage
);
911 pOther
= pPage
->pCache
;
912 if( pOther
->szAlloc
!= pCache
->szAlloc
){
913 pcache1FreePage(pPage
);
916 pGroup
->nPurgeable
-= (pOther
->bPurgeable
- pCache
->bPurgeable
);
920 /* Step 5. If a usable page buffer has still not been found,
921 ** attempt to allocate a new one.
924 pPage
= pcache1AllocPage(pCache
, createFlag
==1);
928 unsigned int h
= iKey
% pCache
->nHash
;
931 pPage
->pNext
= pCache
->apHash
[h
];
932 pPage
->pCache
= pCache
;
934 /* pPage->pLruPrev = 0;
935 ** No need to clear pLruPrev since it is not accessed when pLruNext==0 */
936 *(void **)pPage
->page
.pExtra
= 0;
937 pCache
->apHash
[h
] = pPage
;
938 if( iKey
>pCache
->iMaxKey
){
939 pCache
->iMaxKey
= iKey
;
946 ** Implementation of the sqlite3_pcache.xFetch method.
948 ** Fetch a page by key value.
950 ** Whether or not a new page may be allocated by this function depends on
951 ** the value of the createFlag argument. 0 means do not allocate a new
952 ** page. 1 means allocate a new page if space is easily available. 2
953 ** means to try really hard to allocate a new page.
955 ** For a non-purgeable cache (a cache used as the storage for an in-memory
956 ** database) there is really no difference between createFlag 1 and 2. So
957 ** the calling function (pcache.c) will never have a createFlag of 1 on
958 ** a non-purgeable cache.
960 ** There are three different approaches to obtaining space for a page,
961 ** depending on the value of parameter createFlag (which may be 0, 1 or 2).
963 ** 1. Regardless of the value of createFlag, the cache is searched for a
964 ** copy of the requested page. If one is found, it is returned.
966 ** 2. If createFlag==0 and the page is not already in the cache, NULL is
969 ** 3. If createFlag is 1, and the page is not already in the cache, then
970 ** return NULL (do not allocate a new page) if any of the following
971 ** conditions are true:
973 ** (a) the number of pages pinned by the cache is greater than
976 ** (b) the number of pages pinned by the cache is greater than
977 ** the sum of nMax for all purgeable caches, less the sum of
978 ** nMin for all other purgeable caches, or
980 ** 4. If none of the first three conditions apply and the cache is marked
981 ** as purgeable, and if one of the following is true:
983 ** (a) The number of pages allocated for the cache is already
986 ** (b) The number of pages allocated for all purgeable caches is
987 ** already equal to or greater than the sum of nMax for all
990 ** (c) The system is under memory pressure and wants to avoid
991 ** unnecessary pages cache entry allocations
993 ** then attempt to recycle a page from the LRU list. If it is the right
994 ** size, return the recycled buffer. Otherwise, free the buffer and
995 ** proceed to step 5.
997 ** 5. Otherwise, allocate and return a new page buffer.
999 ** There are two versions of this routine. pcache1FetchWithMutex() is
1000 ** the general case. pcache1FetchNoMutex() is a faster implementation for
1001 ** the common case where pGroup->mutex is NULL. The pcache1Fetch() wrapper
1002 ** invokes the appropriate routine.
1004 static PgHdr1
*pcache1FetchNoMutex(
1009 PCache1
*pCache
= (PCache1
*)p
;
1012 /* Step 1: Search the hash table for an existing entry. */
1013 pPage
= pCache
->apHash
[iKey
% pCache
->nHash
];
1014 while( pPage
&& pPage
->iKey
!=iKey
){ pPage
= pPage
->pNext
; }
1016 /* Step 2: If the page was found in the hash table, then return it.
1017 ** If the page was not in the hash table and createFlag is 0, abort.
1018 ** Otherwise (page not in hash and createFlag!=0) continue with
1019 ** subsequent steps to try to create the page. */
1021 if( PAGE_IS_UNPINNED(pPage
) ){
1022 return pcache1PinPage(pPage
);
1026 }else if( createFlag
){
1027 /* Steps 3, 4, and 5 implemented by this subroutine */
1028 return pcache1FetchStage2(pCache
, iKey
, createFlag
);
1033 #if PCACHE1_MIGHT_USE_GROUP_MUTEX
1034 static PgHdr1
*pcache1FetchWithMutex(
1039 PCache1
*pCache
= (PCache1
*)p
;
1042 pcache1EnterMutex(pCache
->pGroup
);
1043 pPage
= pcache1FetchNoMutex(p
, iKey
, createFlag
);
1044 assert( pPage
==0 || pCache
->iMaxKey
>=iKey
);
1045 pcache1LeaveMutex(pCache
->pGroup
);
1049 static sqlite3_pcache_page
*pcache1Fetch(
1054 #if PCACHE1_MIGHT_USE_GROUP_MUTEX || defined(SQLITE_DEBUG)
1055 PCache1
*pCache
= (PCache1
*)p
;
1058 assert( offsetof(PgHdr1
,page
)==0 );
1059 assert( pCache
->bPurgeable
|| createFlag
!=1 );
1060 assert( pCache
->bPurgeable
|| pCache
->nMin
==0 );
1061 assert( pCache
->bPurgeable
==0 || pCache
->nMin
==10 );
1062 assert( pCache
->nMin
==0 || pCache
->bPurgeable
);
1063 assert( pCache
->nHash
>0 );
1064 #if PCACHE1_MIGHT_USE_GROUP_MUTEX
1065 if( pCache
->pGroup
->mutex
){
1066 return (sqlite3_pcache_page
*)pcache1FetchWithMutex(p
, iKey
, createFlag
);
1070 return (sqlite3_pcache_page
*)pcache1FetchNoMutex(p
, iKey
, createFlag
);
1076 ** Implementation of the sqlite3_pcache.xUnpin method.
1078 ** Mark a page as unpinned (eligible for asynchronous recycling).
1080 static void pcache1Unpin(
1082 sqlite3_pcache_page
*pPg
,
1085 PCache1
*pCache
= (PCache1
*)p
;
1086 PgHdr1
*pPage
= (PgHdr1
*)pPg
;
1087 PGroup
*pGroup
= pCache
->pGroup
;
1089 assert( pPage
->pCache
==pCache
);
1090 pcache1EnterMutex(pGroup
);
1092 /* It is an error to call this function if the page is already
1093 ** part of the PGroup LRU list.
1095 assert( pPage
->pLruNext
==0 );
1096 assert( PAGE_IS_PINNED(pPage
) );
1098 if( reuseUnlikely
|| pGroup
->nPurgeable
>pGroup
->nMaxPage
){
1099 pcache1RemoveFromHash(pPage
, 1);
1101 /* Add the page to the PGroup LRU list. */
1102 PgHdr1
**ppFirst
= &pGroup
->lru
.pLruNext
;
1103 pPage
->pLruPrev
= &pGroup
->lru
;
1104 (pPage
->pLruNext
= *ppFirst
)->pLruPrev
= pPage
;
1106 pCache
->nRecyclable
++;
1109 pcache1LeaveMutex(pCache
->pGroup
);
1113 ** Implementation of the sqlite3_pcache.xRekey method.
1115 static void pcache1Rekey(
1117 sqlite3_pcache_page
*pPg
,
1121 PCache1
*pCache
= (PCache1
*)p
;
1122 PgHdr1
*pPage
= (PgHdr1
*)pPg
;
1124 unsigned int hOld
, hNew
;
1125 assert( pPage
->iKey
==iOld
);
1126 assert( pPage
->pCache
==pCache
);
1127 assert( iOld
!=iNew
); /* The page number really is changing */
1129 pcache1EnterMutex(pCache
->pGroup
);
1131 assert( pcache1FetchNoMutex(p
, iOld
, 0)==pPage
); /* pPg really is iOld */
1132 hOld
= iOld
%pCache
->nHash
;
1133 pp
= &pCache
->apHash
[hOld
];
1134 while( (*pp
)!=pPage
){
1139 assert( pcache1FetchNoMutex(p
, iNew
, 0)==0 ); /* iNew not in cache */
1140 hNew
= iNew
%pCache
->nHash
;
1142 pPage
->pNext
= pCache
->apHash
[hNew
];
1143 pCache
->apHash
[hNew
] = pPage
;
1144 if( iNew
>pCache
->iMaxKey
){
1145 pCache
->iMaxKey
= iNew
;
1148 pcache1LeaveMutex(pCache
->pGroup
);
1152 ** Implementation of the sqlite3_pcache.xTruncate method.
1154 ** Discard all unpinned pages in the cache with a page number equal to
1155 ** or greater than parameter iLimit. Any pinned pages with a page number
1156 ** equal to or greater than iLimit are implicitly unpinned.
1158 static void pcache1Truncate(sqlite3_pcache
*p
, unsigned int iLimit
){
1159 PCache1
*pCache
= (PCache1
*)p
;
1160 pcache1EnterMutex(pCache
->pGroup
);
1161 if( iLimit
<=pCache
->iMaxKey
){
1162 pcache1TruncateUnsafe(pCache
, iLimit
);
1163 pCache
->iMaxKey
= iLimit
-1;
1165 pcache1LeaveMutex(pCache
->pGroup
);
1169 ** Implementation of the sqlite3_pcache.xDestroy method.
1171 ** Destroy a cache allocated using pcache1Create().
1173 static void pcache1Destroy(sqlite3_pcache
*p
){
1174 PCache1
*pCache
= (PCache1
*)p
;
1175 PGroup
*pGroup
= pCache
->pGroup
;
1176 assert( pCache
->bPurgeable
|| (pCache
->nMax
==0 && pCache
->nMin
==0) );
1177 pcache1EnterMutex(pGroup
);
1178 if( pCache
->nPage
) pcache1TruncateUnsafe(pCache
, 0);
1179 assert( pGroup
->nMaxPage
>= pCache
->nMax
);
1180 pGroup
->nMaxPage
-= pCache
->nMax
;
1181 assert( pGroup
->nMinPage
>= pCache
->nMin
);
1182 pGroup
->nMinPage
-= pCache
->nMin
;
1183 pGroup
->mxPinned
= pGroup
->nMaxPage
+ 10 - pGroup
->nMinPage
;
1184 pcache1EnforceMaxPage(pCache
);
1185 pcache1LeaveMutex(pGroup
);
1186 sqlite3_free(pCache
->pBulk
);
1187 sqlite3_free(pCache
->apHash
);
1188 sqlite3_free(pCache
);
1192 ** This function is called during initialization (sqlite3_initialize()) to
1193 ** install the default pluggable cache module, assuming the user has not
1194 ** already provided an alternative.
1196 void sqlite3PCacheSetDefault(void){
1197 static const sqlite3_pcache_methods2 defaultMethods
= {
1200 pcache1Init
, /* xInit */
1201 pcache1Shutdown
, /* xShutdown */
1202 pcache1Create
, /* xCreate */
1203 pcache1Cachesize
, /* xCachesize */
1204 pcache1Pagecount
, /* xPagecount */
1205 pcache1Fetch
, /* xFetch */
1206 pcache1Unpin
, /* xUnpin */
1207 pcache1Rekey
, /* xRekey */
1208 pcache1Truncate
, /* xTruncate */
1209 pcache1Destroy
, /* xDestroy */
1210 pcache1Shrink
/* xShrink */
1212 sqlite3_config(SQLITE_CONFIG_PCACHE2
, &defaultMethods
);
1216 ** Return the size of the header on each page of this PCACHE implementation.
1218 int sqlite3HeaderSizePcache1(void){ return ROUND8(sizeof(PgHdr1
)); }
1221 ** Return the global mutex used by this PCACHE implementation. The
1222 ** sqlite3_status() routine needs access to this mutex.
1224 sqlite3_mutex
*sqlite3Pcache1Mutex(void){
1225 return pcache1
.mutex
;
1228 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
1230 ** This function is called to free superfluous dynamically allocated memory
1231 ** held by the pager system. Memory in use by any SQLite pager allocated
1232 ** by the current thread may be sqlite3_free()ed.
1234 ** nReq is the number of bytes of memory required. Once this much has
1235 ** been released, the function returns. The return value is the total number
1236 ** of bytes of memory released.
1238 int sqlite3PcacheReleaseMemory(int nReq
){
1240 assert( sqlite3_mutex_notheld(pcache1
.grp
.mutex
) );
1241 assert( sqlite3_mutex_notheld(pcache1
.mutex
) );
1242 if( sqlite3GlobalConfig
.pPage
==0 ){
1244 pcache1EnterMutex(&pcache1
.grp
);
1245 while( (nReq
<0 || nFree
<nReq
)
1246 && (p
=pcache1
.grp
.lru
.pLruPrev
)!=0
1249 nFree
+= pcache1MemSize(p
->page
.pBuf
);
1250 assert( PAGE_IS_UNPINNED(p
) );
1252 pcache1RemoveFromHash(p
, 1);
1254 pcache1LeaveMutex(&pcache1
.grp
);
1258 #endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
1262 ** This function is used by test procedures to inspect the internal state
1263 ** of the global cache.
1265 void sqlite3PcacheStats(
1266 int *pnCurrent
, /* OUT: Total number of pages cached */
1267 int *pnMax
, /* OUT: Global maximum cache size */
1268 int *pnMin
, /* OUT: Sum of PCache1.nMin for purgeable caches */
1269 int *pnRecyclable
/* OUT: Total number of pages available for recycling */
1272 int nRecyclable
= 0;
1273 for(p
=pcache1
.grp
.lru
.pLruNext
; p
&& !p
->isAnchor
; p
=p
->pLruNext
){
1274 assert( PAGE_IS_UNPINNED(p
) );
1277 *pnCurrent
= pcache1
.grp
.nPurgeable
;
1278 *pnMax
= (int)pcache1
.grp
.nMaxPage
;
1279 *pnMin
= (int)pcache1
.grp
.nMinPage
;
1280 *pnRecyclable
= nRecyclable
;