blob: 3f56d025e20741eec06bbec2088d4257c84bef27 [file] [log] [blame]
danielk1977bc2ca9e2008-11-13 14:28:28 +00001/*
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.
peter.d.reid60ec9142014-09-06 16:39:46 +000016** If the default page cache implementation is overridden, then neither of
danielk1977bc2ca9e2008-11-13 14:28:28 +000017** these two features are available.
drh01c5c002015-07-04 18:15:04 +000018**
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
drh7b341e62015-07-08 14:13:44 +000044** seeks to overcome the "clownshoe" problem (also called "internal
45** fragmentation" in academic literature) of allocating a few bytes more
drh01c5c002015-07-04 18:15:04 +000046** 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.
drhee70a842015-07-06 18:54:52 +000054**
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**
drha6082f62015-11-26 22:12:41 +000068** sqlite3_config(SQLITE_CONFIG_PAGECACHE, (void*)0, 0, N).
drhee70a842015-07-06 18:54:52 +000069**
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.
danielk1977bc2ca9e2008-11-13 14:28:28 +000082*/
danielk1977bc2ca9e2008-11-13 14:28:28 +000083#include "sqliteInt.h"
84
85typedef struct PCache1 PCache1;
86typedef struct PgHdr1 PgHdr1;
87typedef struct PgFreeslot PgFreeslot;
drh9f8cf9d2011-01-17 21:32:24 +000088typedef struct PGroup PGroup;
89
drh92af02c2015-09-04 04:31:56 +000090/*
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*/
96struct PgHdr1 {
97 sqlite3_pcache_page page; /* Base class. Must be first. pBuf & pExtra */
98 unsigned int iKey; /* Key value (page number) */
99 u8 isPinned; /* Page in use, not on the LRU list */
100 u8 isBulkLocal; /* This page from bulk local storage */
101 u8 isAnchor; /* This is the PGroup.lru element */
102 PgHdr1 *pNext; /* Next in hash table chain */
103 PCache1 *pCache; /* Cache that currently owns this page */
104 PgHdr1 *pLruNext; /* Next in LRU list of unpinned pages */
105 PgHdr1 *pLruPrev; /* Previous in LRU list of unpinned pages */
106};
107
drh9f8cf9d2011-01-17 21:32:24 +0000108/* Each page cache (or PCache) belongs to a PGroup. A PGroup is a set
peter.d.reid60ec9142014-09-06 16:39:46 +0000109** of one or more PCaches that are able to recycle each other's unpinned
drh9f8cf9d2011-01-17 21:32:24 +0000110** pages when they are under memory pressure. A PGroup is an instance of
111** the following object.
112**
113** This page cache implementation works in one of two modes:
114**
115** (1) Every PCache is the sole member of its own PGroup. There is
116** one PGroup per PCache.
117**
118** (2) There is a single global PGroup that all PCaches are a member
119** of.
120**
121** Mode 1 uses more memory (since PCache instances are not able to rob
122** unused pages from other PCaches) but it also operates without a mutex,
123** and is therefore often faster. Mode 2 requires a mutex in order to be
drh45d29302012-01-08 22:18:33 +0000124** threadsafe, but recycles pages more efficiently.
drh9f8cf9d2011-01-17 21:32:24 +0000125**
126** For mode (1), PGroup.mutex is NULL. For mode (2) there is only a single
127** PGroup which is the pcache1.grp global variable and its mutex is
128** SQLITE_MUTEX_STATIC_LRU.
129*/
130struct PGroup {
131 sqlite3_mutex *mutex; /* MUTEX_STATIC_LRU or NULL */
drha69085c2012-01-02 18:00:55 +0000132 unsigned int nMaxPage; /* Sum of nMax for purgeable caches */
133 unsigned int nMinPage; /* Sum of nMin for purgeable caches */
134 unsigned int mxPinned; /* nMaxpage + 10 - nMinPage */
135 unsigned int nCurrentPage; /* Number of purgeable pages allocated */
drh92af02c2015-09-04 04:31:56 +0000136 PgHdr1 lru; /* The beginning and end of the LRU list */
drh9f8cf9d2011-01-17 21:32:24 +0000137};
danielk1977bc2ca9e2008-11-13 14:28:28 +0000138
drh9d13f112010-08-24 18:06:35 +0000139/* Each page cache is an instance of the following object. Every
140** open database file (including each in-memory database and each
141** temporary or transient database) has a single page cache which
142** is an instance of this object.
143**
144** Pointers to structures of this type are cast and returned as
145** opaque sqlite3_pcache* handles.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000146*/
147struct PCache1 {
148 /* Cache configuration parameters. Page size (szPage) and the purgeable
149 ** flag (bPurgeable) are set when the cache is created. nMax may be
drh45d29302012-01-08 22:18:33 +0000150 ** modified at any time by a call to the pcache1Cachesize() method.
drh9f8cf9d2011-01-17 21:32:24 +0000151 ** The PGroup mutex must be held when accessing nMax.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000152 */
drh9f8cf9d2011-01-17 21:32:24 +0000153 PGroup *pGroup; /* PGroup this cache belongs to */
drhee70a842015-07-06 18:54:52 +0000154 int szPage; /* Size of database content section */
155 int szExtra; /* sizeof(MemPage)+sizeof(PgHdr) */
156 int szAlloc; /* Total size of one pcache line */
danielk1977bc2ca9e2008-11-13 14:28:28 +0000157 int bPurgeable; /* True if cache is purgeable */
danielk197744cd45c2008-11-15 11:22:45 +0000158 unsigned int nMin; /* Minimum number of pages reserved */
159 unsigned int nMax; /* Configured "cache_size" value */
drh25ca5682011-01-26 00:07:03 +0000160 unsigned int n90pct; /* nMax*9/10 */
drh2cbd78b2012-02-02 19:37:18 +0000161 unsigned int iMaxKey; /* Largest key seen since xTruncate() */
danielk1977bc2ca9e2008-11-13 14:28:28 +0000162
163 /* Hash table of all pages. The following variables may only be accessed
drh9f8cf9d2011-01-17 21:32:24 +0000164 ** when the accessor is holding the PGroup mutex.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000165 */
danielk197744cd45c2008-11-15 11:22:45 +0000166 unsigned int nRecyclable; /* Number of pages in the LRU list */
167 unsigned int nPage; /* Total number of pages in apHash */
168 unsigned int nHash; /* Number of slots in apHash[] */
danielk1977bc2ca9e2008-11-13 14:28:28 +0000169 PgHdr1 **apHash; /* Hash table for fast lookup by key */
drhee70a842015-07-06 18:54:52 +0000170 PgHdr1 *pFree; /* List of unused pcache-local pages */
171 void *pBulk; /* Bulk memory used by pcache-local */
danielk1977bc2ca9e2008-11-13 14:28:28 +0000172};
173
174/*
drhee70a842015-07-06 18:54:52 +0000175** Free slots in the allocator used to divide up the global page cache
176** buffer provided using the SQLITE_CONFIG_PAGECACHE mechanism.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000177*/
178struct PgFreeslot {
179 PgFreeslot *pNext; /* Next free slot */
180};
181
182/*
183** Global data used by this cache.
184*/
185static SQLITE_WSD struct PCacheGlobal {
drh9f8cf9d2011-01-17 21:32:24 +0000186 PGroup grp; /* The global PGroup for mode (2) */
danielk1977bc2ca9e2008-11-13 14:28:28 +0000187
drh9f8cf9d2011-01-17 21:32:24 +0000188 /* Variables related to SQLITE_CONFIG_PAGECACHE settings. The
189 ** szSlot, nSlot, pStart, pEnd, nReserve, and isInit values are all
190 ** fixed at sqlite3_initialize() time and do not require mutex protection.
191 ** The nFreeSlot and pFree values do require mutex protection.
192 */
193 int isInit; /* True if initialized */
drhdb7ae892015-07-06 20:57:22 +0000194 int separateCache; /* Use a new PGroup for each PCache */
drh957026a2015-07-16 18:18:19 +0000195 int nInitPage; /* Initial bulk allocation size */
drh9f8cf9d2011-01-17 21:32:24 +0000196 int szSlot; /* Size of each free slot */
197 int nSlot; /* The number of pcache slots */
198 int nReserve; /* Try to keep nFreeSlot above this */
drhee70a842015-07-06 18:54:52 +0000199 void *pStart, *pEnd; /* Bounds of global page cache memory */
drh9f8cf9d2011-01-17 21:32:24 +0000200 /* Above requires no mutex. Use mutex below for variable that follow. */
201 sqlite3_mutex *mutex; /* Mutex for accessing the following: */
drh9f8cf9d2011-01-17 21:32:24 +0000202 PgFreeslot *pFree; /* Free page blocks */
drh2cbd78b2012-02-02 19:37:18 +0000203 int nFreeSlot; /* Number of unused pcache slots */
drh9f8cf9d2011-01-17 21:32:24 +0000204 /* The following value requires a mutex to change. We skip the mutex on
205 ** reading because (1) most platforms read a 32-bit integer atomically and
206 ** (2) even if an incorrect value is read, no great harm is done since this
207 ** is really just an optimization. */
208 int bUnderPressure; /* True if low on PAGECACHE memory */
danielk197744cd45c2008-11-15 11:22:45 +0000209} pcache1_g;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000210
211/*
212** All code in this file should access the global structure above via the
213** alias "pcache1". This ensures that the WSD emulation is used when
214** compiling for systems that do not support real WSD.
215*/
216#define pcache1 (GLOBAL(struct PCacheGlobal, pcache1_g))
217
218/*
drh9f8cf9d2011-01-17 21:32:24 +0000219** Macros to enter and leave the PCache LRU mutex.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000220*/
drh982215a2015-06-13 11:10:55 +0000221#if !defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) || SQLITE_THREADSAFE==0
222# define pcache1EnterMutex(X) assert((X)->mutex==0)
223# define pcache1LeaveMutex(X) assert((X)->mutex==0)
224# define PCACHE1_MIGHT_USE_GROUP_MUTEX 0
225#else
226# define pcache1EnterMutex(X) sqlite3_mutex_enter((X)->mutex)
227# define pcache1LeaveMutex(X) sqlite3_mutex_leave((X)->mutex)
228# define PCACHE1_MIGHT_USE_GROUP_MUTEX 1
229#endif
danielk1977bc2ca9e2008-11-13 14:28:28 +0000230
231/******************************************************************************/
232/******** Page Allocation/SQLITE_CONFIG_PCACHE Related Functions **************/
233
drh92af02c2015-09-04 04:31:56 +0000234
danielk1977bc2ca9e2008-11-13 14:28:28 +0000235/*
236** This function is called during initialization if a static buffer is
237** supplied to use for the page-cache by passing the SQLITE_CONFIG_PAGECACHE
238** verb to sqlite3_config(). Parameter pBuf points to an allocation large
239** enough to contain 'n' buffers of 'sz' bytes each.
drh9f8cf9d2011-01-17 21:32:24 +0000240**
241** This routine is called from sqlite3_initialize() and so it is guaranteed
242** to be serialized already. There is no need for further mutexing.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000243*/
244void sqlite3PCacheBufferSetup(void *pBuf, int sz, int n){
drhf4622dc2009-05-22 11:10:24 +0000245 if( pcache1.isInit ){
246 PgFreeslot *p;
drhee70a842015-07-06 18:54:52 +0000247 if( pBuf==0 ) sz = n = 0;
drhf4622dc2009-05-22 11:10:24 +0000248 sz = ROUNDDOWN8(sz);
249 pcache1.szSlot = sz;
drh50d1b5f2010-08-27 12:21:06 +0000250 pcache1.nSlot = pcache1.nFreeSlot = n;
251 pcache1.nReserve = n>90 ? 10 : (n/10 + 1);
drhf4622dc2009-05-22 11:10:24 +0000252 pcache1.pStart = pBuf;
253 pcache1.pFree = 0;
drh9f8cf9d2011-01-17 21:32:24 +0000254 pcache1.bUnderPressure = 0;
drhf4622dc2009-05-22 11:10:24 +0000255 while( n-- ){
256 p = (PgFreeslot*)pBuf;
257 p->pNext = pcache1.pFree;
258 pcache1.pFree = p;
259 pBuf = (void*)&((char*)pBuf)[sz];
260 }
261 pcache1.pEnd = pBuf;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000262 }
danielk1977bc2ca9e2008-11-13 14:28:28 +0000263}
264
265/*
drh957026a2015-07-16 18:18:19 +0000266** Try to initialize the pCache->pFree and pCache->pBulk fields. Return
267** true if pCache->pFree ends up containing one or more free pages.
268*/
269static int pcache1InitBulk(PCache1 *pCache){
drh939d4bc2015-07-16 18:37:53 +0000270 i64 szBulk;
drh957026a2015-07-16 18:18:19 +0000271 char *zBulk;
272 if( pcache1.nInitPage==0 ) return 0;
273 /* Do not bother with a bulk allocation if the cache size very small */
274 if( pCache->nMax<3 ) return 0;
275 sqlite3BeginBenignMalloc();
276 if( pcache1.nInitPage>0 ){
drh939d4bc2015-07-16 18:37:53 +0000277 szBulk = pCache->szAlloc * (i64)pcache1.nInitPage;
drh957026a2015-07-16 18:18:19 +0000278 }else{
drh939d4bc2015-07-16 18:37:53 +0000279 szBulk = -1024 * (i64)pcache1.nInitPage;
drh957026a2015-07-16 18:18:19 +0000280 }
drh939d4bc2015-07-16 18:37:53 +0000281 if( szBulk > pCache->szAlloc*(i64)pCache->nMax ){
drh957026a2015-07-16 18:18:19 +0000282 szBulk = pCache->szAlloc*pCache->nMax;
283 }
284 zBulk = pCache->pBulk = sqlite3Malloc( szBulk );
285 sqlite3EndBenignMalloc();
286 if( zBulk ){
287 int nBulk = sqlite3MallocSize(zBulk)/pCache->szAlloc;
288 int i;
289 for(i=0; i<nBulk; i++){
290 PgHdr1 *pX = (PgHdr1*)&zBulk[pCache->szPage];
291 pX->page.pBuf = zBulk;
292 pX->page.pExtra = &pX[1];
293 pX->isBulkLocal = 1;
drh92af02c2015-09-04 04:31:56 +0000294 pX->isAnchor = 0;
drh957026a2015-07-16 18:18:19 +0000295 pX->pNext = pCache->pFree;
296 pCache->pFree = pX;
297 zBulk += pCache->szAlloc;
298 }
299 }
300 return pCache->pFree!=0;
301}
302
303/*
danielk1977bc2ca9e2008-11-13 14:28:28 +0000304** Malloc function used within this file to allocate space from the buffer
305** configured using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no
306** such buffer exists or there is no space left in it, this function falls
307** back to sqlite3Malloc().
drh9f8cf9d2011-01-17 21:32:24 +0000308**
309** Multiple threads can run this routine at the same time. Global variables
310** in pcache1 need to be protected via mutex.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000311*/
312static void *pcache1Alloc(int nByte){
drh9f8cf9d2011-01-17 21:32:24 +0000313 void *p = 0;
314 assert( sqlite3_mutex_notheld(pcache1.grp.mutex) );
drh9f8cf9d2011-01-17 21:32:24 +0000315 if( nByte<=pcache1.szSlot ){
316 sqlite3_mutex_enter(pcache1.mutex);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000317 p = (PgHdr1 *)pcache1.pFree;
drh9f8cf9d2011-01-17 21:32:24 +0000318 if( p ){
319 pcache1.pFree = pcache1.pFree->pNext;
320 pcache1.nFreeSlot--;
321 pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve;
322 assert( pcache1.nFreeSlot>=0 );
drhb02392e2015-10-15 15:28:56 +0000323 sqlite3StatusHighwater(SQLITE_STATUS_PAGECACHE_SIZE, nByte);
drhaf89fe62015-03-23 17:25:18 +0000324 sqlite3StatusUp(SQLITE_STATUS_PAGECACHE_USED, 1);
drh9f8cf9d2011-01-17 21:32:24 +0000325 }
326 sqlite3_mutex_leave(pcache1.mutex);
327 }
328 if( p==0 ){
329 /* Memory is not available in the SQLITE_CONFIG_PAGECACHE pool. Get
330 ** it from sqlite3Malloc instead.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000331 */
danielk1977bc2ca9e2008-11-13 14:28:28 +0000332 p = sqlite3Malloc(nByte);
drh4bd69522012-06-07 02:35:29 +0000333#ifndef SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS
danielk1977bc2ca9e2008-11-13 14:28:28 +0000334 if( p ){
335 int sz = sqlite3MallocSize(p);
drh9bf3da8e2011-01-26 13:24:40 +0000336 sqlite3_mutex_enter(pcache1.mutex);
drhb02392e2015-10-15 15:28:56 +0000337 sqlite3StatusHighwater(SQLITE_STATUS_PAGECACHE_SIZE, nByte);
drhaf89fe62015-03-23 17:25:18 +0000338 sqlite3StatusUp(SQLITE_STATUS_PAGECACHE_OVERFLOW, sz);
drh9bf3da8e2011-01-26 13:24:40 +0000339 sqlite3_mutex_leave(pcache1.mutex);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000340 }
drh4bd69522012-06-07 02:35:29 +0000341#endif
drh107b56e2010-03-12 16:32:53 +0000342 sqlite3MemdebugSetType(p, MEMTYPE_PCACHE);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000343 }
344 return p;
345}
346
347/*
348** Free an allocated buffer obtained from pcache1Alloc().
349*/
drhee70a842015-07-06 18:54:52 +0000350static void pcache1Free(void *p){
drh09419b42011-11-16 19:29:17 +0000351 int nFreed = 0;
drhee70a842015-07-06 18:54:52 +0000352 if( p==0 ) return;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000353 if( p>=pcache1.pStart && p<pcache1.pEnd ){
354 PgFreeslot *pSlot;
drh9f8cf9d2011-01-17 21:32:24 +0000355 sqlite3_mutex_enter(pcache1.mutex);
drhaf89fe62015-03-23 17:25:18 +0000356 sqlite3StatusDown(SQLITE_STATUS_PAGECACHE_USED, 1);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000357 pSlot = (PgFreeslot*)p;
358 pSlot->pNext = pcache1.pFree;
359 pcache1.pFree = pSlot;
drh50d1b5f2010-08-27 12:21:06 +0000360 pcache1.nFreeSlot++;
drh9f8cf9d2011-01-17 21:32:24 +0000361 pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve;
drh50d1b5f2010-08-27 12:21:06 +0000362 assert( pcache1.nFreeSlot<=pcache1.nSlot );
drh9f8cf9d2011-01-17 21:32:24 +0000363 sqlite3_mutex_leave(pcache1.mutex);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000364 }else{
drh107b56e2010-03-12 16:32:53 +0000365 assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) );
366 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
drh4bd69522012-06-07 02:35:29 +0000367#ifndef SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS
drhee70a842015-07-06 18:54:52 +0000368 nFreed = sqlite3MallocSize(p);
drh15ad92f2011-01-26 13:28:06 +0000369 sqlite3_mutex_enter(pcache1.mutex);
drhaf89fe62015-03-23 17:25:18 +0000370 sqlite3StatusDown(SQLITE_STATUS_PAGECACHE_OVERFLOW, nFreed);
drh15ad92f2011-01-26 13:28:06 +0000371 sqlite3_mutex_leave(pcache1.mutex);
drh4bd69522012-06-07 02:35:29 +0000372#endif
danielk1977bc2ca9e2008-11-13 14:28:28 +0000373 sqlite3_free(p);
374 }
375}
376
drhc8f503a2010-08-20 09:14:13 +0000377#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
378/*
drh9d13f112010-08-24 18:06:35 +0000379** Return the size of a pcache allocation
drhc8f503a2010-08-20 09:14:13 +0000380*/
381static int pcache1MemSize(void *p){
drhc8f503a2010-08-20 09:14:13 +0000382 if( p>=pcache1.pStart && p<pcache1.pEnd ){
383 return pcache1.szSlot;
384 }else{
385 int iSize;
386 assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) );
387 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
388 iSize = sqlite3MallocSize(p);
389 sqlite3MemdebugSetType(p, MEMTYPE_PCACHE);
390 return iSize;
391 }
392}
393#endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
394
dand2925702011-08-19 18:15:00 +0000395/*
danielk1977bc2ca9e2008-11-13 14:28:28 +0000396** Allocate a new page object initially associated with cache pCache.
397*/
drh3c0c4312015-09-01 19:51:37 +0000398static PgHdr1 *pcache1AllocPage(PCache1 *pCache, int benignMalloc){
danb5126dd2011-09-22 14:56:31 +0000399 PgHdr1 *p = 0;
400 void *pPg;
dand2925702011-08-19 18:15:00 +0000401
dand2925702011-08-19 18:15:00 +0000402 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
drh957026a2015-07-16 18:18:19 +0000403 if( pCache->pFree || (pCache->nPage==0 && pcache1InitBulk(pCache)) ){
drhee70a842015-07-06 18:54:52 +0000404 p = pCache->pFree;
405 pCache->pFree = p->pNext;
406 p->pNext = 0;
407 }else{
drhdb7ae892015-07-06 20:57:22 +0000408#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
drhee70a842015-07-06 18:54:52 +0000409 /* The group mutex must be released before pcache1Alloc() is called. This
drhdb7ae892015-07-06 20:57:22 +0000410 ** is because it might call sqlite3_release_memory(), which assumes that
drhee70a842015-07-06 18:54:52 +0000411 ** this mutex is not held. */
drhdb7ae892015-07-06 20:57:22 +0000412 assert( pcache1.separateCache==0 );
413 assert( pCache->pGroup==&pcache1.grp );
drhee70a842015-07-06 18:54:52 +0000414 pcache1LeaveMutex(pCache->pGroup);
drhdb7ae892015-07-06 20:57:22 +0000415#endif
drh8faee872015-09-19 18:08:13 +0000416 if( benignMalloc ){ sqlite3BeginBenignMalloc(); }
dan22e21ff2011-11-08 20:08:44 +0000417#ifdef SQLITE_PCACHE_SEPARATE_HEADER
drhee70a842015-07-06 18:54:52 +0000418 pPg = pcache1Alloc(pCache->szPage);
419 p = sqlite3Malloc(sizeof(PgHdr1) + pCache->szExtra);
420 if( !pPg || !p ){
421 pcache1Free(pPg);
422 sqlite3_free(p);
423 pPg = 0;
424 }
dan22e21ff2011-11-08 20:08:44 +0000425#else
drhee70a842015-07-06 18:54:52 +0000426 pPg = pcache1Alloc(pCache->szAlloc);
427 p = (PgHdr1 *)&((u8 *)pPg)[pCache->szPage];
dan22e21ff2011-11-08 20:08:44 +0000428#endif
drh8faee872015-09-19 18:08:13 +0000429 if( benignMalloc ){ sqlite3EndBenignMalloc(); }
drhdb7ae892015-07-06 20:57:22 +0000430#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
drhee70a842015-07-06 18:54:52 +0000431 pcache1EnterMutex(pCache->pGroup);
drhdb7ae892015-07-06 20:57:22 +0000432#endif
drhee70a842015-07-06 18:54:52 +0000433 if( pPg==0 ) return 0;
dan22e21ff2011-11-08 20:08:44 +0000434 p->page.pBuf = pPg;
435 p->page.pExtra = &p[1];
drhee70a842015-07-06 18:54:52 +0000436 p->isBulkLocal = 0;
drh92af02c2015-09-04 04:31:56 +0000437 p->isAnchor = 0;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000438 }
drhee70a842015-07-06 18:54:52 +0000439 if( pCache->bPurgeable ){
440 pCache->pGroup->nCurrentPage++;
441 }
442 return p;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000443}
444
445/*
446** Free a page object allocated by pcache1AllocPage().
447*/
448static void pcache1FreePage(PgHdr1 *p){
drhdb7ae892015-07-06 20:57:22 +0000449 PCache1 *pCache;
450 assert( p!=0 );
451 pCache = p->pCache;
452 assert( sqlite3_mutex_held(p->pCache->pGroup->mutex) );
453 if( p->isBulkLocal ){
454 p->pNext = pCache->pFree;
455 pCache->pFree = p;
456 }else{
457 pcache1Free(p->page.pBuf);
dan22e21ff2011-11-08 20:08:44 +0000458#ifdef SQLITE_PCACHE_SEPARATE_HEADER
drhdb7ae892015-07-06 20:57:22 +0000459 sqlite3_free(p);
dan22e21ff2011-11-08 20:08:44 +0000460#endif
drhdb7ae892015-07-06 20:57:22 +0000461 }
462 if( pCache->bPurgeable ){
463 pCache->pGroup->nCurrentPage--;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000464 }
465}
466
467/*
468** Malloc function used by SQLite to obtain space from the buffer configured
469** using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no such buffer
470** exists, this function falls back to sqlite3Malloc().
471*/
472void *sqlite3PageMalloc(int sz){
drh9f8cf9d2011-01-17 21:32:24 +0000473 return pcache1Alloc(sz);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000474}
475
476/*
477** Free an allocated buffer obtained from sqlite3PageMalloc().
478*/
479void sqlite3PageFree(void *p){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000480 pcache1Free(p);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000481}
482
drh50d1b5f2010-08-27 12:21:06 +0000483
484/*
485** Return true if it desirable to avoid allocating a new page cache
486** entry.
487**
488** If memory was allocated specifically to the page cache using
489** SQLITE_CONFIG_PAGECACHE but that memory has all been used, then
490** it is desirable to avoid allocating a new page cache entry because
491** presumably SQLITE_CONFIG_PAGECACHE was suppose to be sufficient
492** for all page cache needs and we should not need to spill the
493** allocation onto the heap.
494**
drh45d29302012-01-08 22:18:33 +0000495** Or, the heap is used for all page cache memory but the heap is
drh50d1b5f2010-08-27 12:21:06 +0000496** under memory pressure, then again it is desirable to avoid
497** allocating a new page cache entry in order to avoid stressing
498** the heap even further.
499*/
500static int pcache1UnderMemoryPressure(PCache1 *pCache){
dan22e21ff2011-11-08 20:08:44 +0000501 if( pcache1.nSlot && (pCache->szPage+pCache->szExtra)<=pcache1.szSlot ){
drh9f8cf9d2011-01-17 21:32:24 +0000502 return pcache1.bUnderPressure;
drh50d1b5f2010-08-27 12:21:06 +0000503 }else{
504 return sqlite3HeapNearlyFull();
505 }
506}
507
danielk1977bc2ca9e2008-11-13 14:28:28 +0000508/******************************************************************************/
509/******** General Implementation Functions ************************************/
510
511/*
512** This function is used to resize the hash table used by the cache passed
513** as the first argument.
514**
drh9f8cf9d2011-01-17 21:32:24 +0000515** The PCache mutex must be held when this function is called.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000516*/
drhefbf0442014-08-23 23:15:31 +0000517static void pcache1ResizeHash(PCache1 *p){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000518 PgHdr1 **apNew;
danielk197744cd45c2008-11-15 11:22:45 +0000519 unsigned int nNew;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000520 unsigned int i;
521
drh9f8cf9d2011-01-17 21:32:24 +0000522 assert( sqlite3_mutex_held(p->pGroup->mutex) );
danielk1977bc2ca9e2008-11-13 14:28:28 +0000523
524 nNew = p->nHash*2;
525 if( nNew<256 ){
526 nNew = 256;
527 }
528
drh9f8cf9d2011-01-17 21:32:24 +0000529 pcache1LeaveMutex(p->pGroup);
drh085bb7f2008-12-06 14:34:33 +0000530 if( p->nHash ){ sqlite3BeginBenignMalloc(); }
dan6809c962012-07-30 14:53:54 +0000531 apNew = (PgHdr1 **)sqlite3MallocZero(sizeof(PgHdr1 *)*nNew);
drh085bb7f2008-12-06 14:34:33 +0000532 if( p->nHash ){ sqlite3EndBenignMalloc(); }
drh9f8cf9d2011-01-17 21:32:24 +0000533 pcache1EnterMutex(p->pGroup);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000534 if( apNew ){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000535 for(i=0; i<p->nHash; i++){
536 PgHdr1 *pPage;
537 PgHdr1 *pNext = p->apHash[i];
drhb27b7f52008-12-10 18:03:45 +0000538 while( (pPage = pNext)!=0 ){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000539 unsigned int h = pPage->iKey % nNew;
540 pNext = pPage->pNext;
541 pPage->pNext = apNew[h];
542 apNew[h] = pPage;
543 }
544 }
545 sqlite3_free(p->apHash);
546 p->apHash = apNew;
547 p->nHash = nNew;
548 }
danielk1977bc2ca9e2008-11-13 14:28:28 +0000549}
550
551/*
552** This function is used internally to remove the page pPage from the
drh9f8cf9d2011-01-17 21:32:24 +0000553** PGroup LRU list, if is part of it. If pPage is not part of the PGroup
danielk1977bc2ca9e2008-11-13 14:28:28 +0000554** LRU list, then this function is a no-op.
555**
drh9f8cf9d2011-01-17 21:32:24 +0000556** The PGroup mutex must be held when this function is called.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000557*/
drh55a46c92015-06-12 13:49:26 +0000558static PgHdr1 *pcache1PinPage(PgHdr1 *pPage){
drh9f8cf9d2011-01-17 21:32:24 +0000559 PCache1 *pCache;
drh9f8cf9d2011-01-17 21:32:24 +0000560
drh5d56dd22013-12-13 18:50:40 +0000561 assert( pPage!=0 );
562 assert( pPage->isPinned==0 );
drh9f8cf9d2011-01-17 21:32:24 +0000563 pCache = pPage->pCache;
drh92af02c2015-09-04 04:31:56 +0000564 assert( pPage->pLruNext );
565 assert( pPage->pLruPrev );
drhb230a522015-06-12 13:04:51 +0000566 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
drh92af02c2015-09-04 04:31:56 +0000567 pPage->pLruPrev->pLruNext = pPage->pLruNext;
568 pPage->pLruNext->pLruPrev = pPage->pLruPrev;
drh5d56dd22013-12-13 18:50:40 +0000569 pPage->pLruNext = 0;
570 pPage->pLruPrev = 0;
571 pPage->isPinned = 1;
drh92af02c2015-09-04 04:31:56 +0000572 assert( pPage->isAnchor==0 );
573 assert( pCache->pGroup->lru.isAnchor==1 );
drh5d56dd22013-12-13 18:50:40 +0000574 pCache->nRecyclable--;
drh55a46c92015-06-12 13:49:26 +0000575 return pPage;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000576}
577
578
579/*
580** Remove the page supplied as an argument from the hash table
581** (PCache1.apHash structure) that it is currently stored in.
drh95c91e12015-06-29 00:21:00 +0000582** Also free the page if freePage is true.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000583**
drh9f8cf9d2011-01-17 21:32:24 +0000584** The PGroup mutex must be held when this function is called.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000585*/
drh95c91e12015-06-29 00:21:00 +0000586static void pcache1RemoveFromHash(PgHdr1 *pPage, int freeFlag){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000587 unsigned int h;
588 PCache1 *pCache = pPage->pCache;
589 PgHdr1 **pp;
590
drh9f8cf9d2011-01-17 21:32:24 +0000591 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
danielk1977bc2ca9e2008-11-13 14:28:28 +0000592 h = pPage->iKey % pCache->nHash;
593 for(pp=&pCache->apHash[h]; (*pp)!=pPage; pp=&(*pp)->pNext);
594 *pp = (*pp)->pNext;
595
596 pCache->nPage--;
drh95c91e12015-06-29 00:21:00 +0000597 if( freeFlag ) pcache1FreePage(pPage);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000598}
599
600/*
drh9f8cf9d2011-01-17 21:32:24 +0000601** If there are currently more than nMaxPage pages allocated, try
602** to recycle pages to reduce the number allocated to nMaxPage.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000603*/
drh957026a2015-07-16 18:18:19 +0000604static void pcache1EnforceMaxPage(PCache1 *pCache){
605 PGroup *pGroup = pCache->pGroup;
drh92af02c2015-09-04 04:31:56 +0000606 PgHdr1 *p;
drh9f8cf9d2011-01-17 21:32:24 +0000607 assert( sqlite3_mutex_held(pGroup->mutex) );
drh92af02c2015-09-04 04:31:56 +0000608 while( pGroup->nCurrentPage>pGroup->nMaxPage
609 && (p=pGroup->lru.pLruPrev)->isAnchor==0
610 ){
drh9f8cf9d2011-01-17 21:32:24 +0000611 assert( p->pCache->pGroup==pGroup );
drh5d56dd22013-12-13 18:50:40 +0000612 assert( p->isPinned==0 );
danielk1977bc2ca9e2008-11-13 14:28:28 +0000613 pcache1PinPage(p);
drh95c91e12015-06-29 00:21:00 +0000614 pcache1RemoveFromHash(p, 1);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000615 }
drh957026a2015-07-16 18:18:19 +0000616 if( pCache->nPage==0 && pCache->pBulk ){
617 sqlite3_free(pCache->pBulk);
618 pCache->pBulk = pCache->pFree = 0;
619 }
danielk1977bc2ca9e2008-11-13 14:28:28 +0000620}
621
622/*
623** Discard all pages from cache pCache with a page number (key value)
624** greater than or equal to iLimit. Any pinned pages that meet this
625** criteria are unpinned before they are discarded.
626**
drh9f8cf9d2011-01-17 21:32:24 +0000627** The PCache mutex must be held when this function is called.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000628*/
629static void pcache1TruncateUnsafe(
drh9f8cf9d2011-01-17 21:32:24 +0000630 PCache1 *pCache, /* The cache to truncate */
631 unsigned int iLimit /* Drop pages with this pgno or larger */
danielk1977bc2ca9e2008-11-13 14:28:28 +0000632){
drh9f8cf9d2011-01-17 21:32:24 +0000633 TESTONLY( unsigned int nPage = 0; ) /* To assert pCache->nPage is correct */
danielk1977bc2ca9e2008-11-13 14:28:28 +0000634 unsigned int h;
drh9f8cf9d2011-01-17 21:32:24 +0000635 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
danielk1977bc2ca9e2008-11-13 14:28:28 +0000636 for(h=0; h<pCache->nHash; h++){
637 PgHdr1 **pp = &pCache->apHash[h];
638 PgHdr1 *pPage;
drhb27b7f52008-12-10 18:03:45 +0000639 while( (pPage = *pp)!=0 ){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000640 if( pPage->iKey>=iLimit ){
danielk1977ea24ac42009-05-08 06:52:47 +0000641 pCache->nPage--;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000642 *pp = pPage->pNext;
drh5d56dd22013-12-13 18:50:40 +0000643 if( !pPage->isPinned ) pcache1PinPage(pPage);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000644 pcache1FreePage(pPage);
645 }else{
646 pp = &pPage->pNext;
danielk1977ea24ac42009-05-08 06:52:47 +0000647 TESTONLY( nPage++; )
danielk1977bc2ca9e2008-11-13 14:28:28 +0000648 }
649 }
650 }
danielk1977ea24ac42009-05-08 06:52:47 +0000651 assert( pCache->nPage==nPage );
danielk1977bc2ca9e2008-11-13 14:28:28 +0000652}
653
654/******************************************************************************/
655/******** sqlite3_pcache Methods **********************************************/
656
657/*
658** Implementation of the sqlite3_pcache.xInit method.
659*/
danielk197762c14b32008-11-19 09:05:26 +0000660static int pcache1Init(void *NotUsed){
661 UNUSED_PARAMETER(NotUsed);
drhf4622dc2009-05-22 11:10:24 +0000662 assert( pcache1.isInit==0 );
danielk1977bc2ca9e2008-11-13 14:28:28 +0000663 memset(&pcache1, 0, sizeof(pcache1));
drhdb7ae892015-07-06 20:57:22 +0000664
665
666 /*
667 ** The pcache1.separateCache variable is true if each PCache has its own
668 ** private PGroup (mode-1). pcache1.separateCache is false if the single
669 ** PGroup in pcache1.grp is used for all page caches (mode-2).
670 **
671 ** * Always use a unified cache (mode-2) if ENABLE_MEMORY_MANAGEMENT
672 **
673 ** * Use a unified cache in single-threaded applications that have
674 ** configured a start-time buffer for use as page-cache memory using
675 ** sqlite3_config(SQLITE_CONFIG_PAGECACHE, pBuf, sz, N) with non-NULL
676 ** pBuf argument.
677 **
678 ** * Otherwise use separate caches (mode-1)
679 */
680#if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT)
681 pcache1.separateCache = 0;
drhfd5ae962015-07-07 15:14:16 +0000682#elif SQLITE_THREADSAFE
drhdb7ae892015-07-06 20:57:22 +0000683 pcache1.separateCache = sqlite3GlobalConfig.pPage==0
684 || sqlite3GlobalConfig.bCoreMutex>0;
drhfd5ae962015-07-07 15:14:16 +0000685#else
686 pcache1.separateCache = sqlite3GlobalConfig.pPage==0;
drhdb7ae892015-07-06 20:57:22 +0000687#endif
688
drh982215a2015-06-13 11:10:55 +0000689#if SQLITE_THREADSAFE
danielk1977bc2ca9e2008-11-13 14:28:28 +0000690 if( sqlite3GlobalConfig.bCoreMutex ){
drh9f8cf9d2011-01-17 21:32:24 +0000691 pcache1.grp.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU);
drh40f98372011-01-18 15:17:57 +0000692 pcache1.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_PMEM);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000693 }
drh982215a2015-06-13 11:10:55 +0000694#endif
drh957026a2015-07-16 18:18:19 +0000695 if( pcache1.separateCache
696 && sqlite3GlobalConfig.nPage!=0
697 && sqlite3GlobalConfig.pPage==0
698 ){
699 pcache1.nInitPage = sqlite3GlobalConfig.nPage;
700 }else{
701 pcache1.nInitPage = 0;
702 }
drh41692e92011-01-25 04:34:51 +0000703 pcache1.grp.mxPinned = 10;
drhf4622dc2009-05-22 11:10:24 +0000704 pcache1.isInit = 1;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000705 return SQLITE_OK;
706}
707
708/*
709** Implementation of the sqlite3_pcache.xShutdown method.
shane7c7c3112009-08-17 15:31:23 +0000710** Note that the static mutex allocated in xInit does
711** not need to be freed.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000712*/
danielk197762c14b32008-11-19 09:05:26 +0000713static void pcache1Shutdown(void *NotUsed){
714 UNUSED_PARAMETER(NotUsed);
drhf4622dc2009-05-22 11:10:24 +0000715 assert( pcache1.isInit!=0 );
drhb0937192009-05-22 10:53:29 +0000716 memset(&pcache1, 0, sizeof(pcache1));
danielk1977bc2ca9e2008-11-13 14:28:28 +0000717}
718
drhefbf0442014-08-23 23:15:31 +0000719/* forward declaration */
720static void pcache1Destroy(sqlite3_pcache *p);
721
danielk1977bc2ca9e2008-11-13 14:28:28 +0000722/*
723** Implementation of the sqlite3_pcache.xCreate method.
724**
725** Allocate a new cache.
726*/
drhe5c40b12011-11-09 00:06:05 +0000727static sqlite3_pcache *pcache1Create(int szPage, int szExtra, int bPurgeable){
drh9f8cf9d2011-01-17 21:32:24 +0000728 PCache1 *pCache; /* The newly created page cache */
729 PGroup *pGroup; /* The group the new page cache will belong to */
730 int sz; /* Bytes of memory required to allocate the new cache */
danielk1977bc2ca9e2008-11-13 14:28:28 +0000731
drhe73c9142011-11-09 16:12:24 +0000732 assert( (szPage & (szPage-1))==0 && szPage>=512 && szPage<=65536 );
733 assert( szExtra < 300 );
734
drhdb7ae892015-07-06 20:57:22 +0000735 sz = sizeof(PCache1) + sizeof(PGroup)*pcache1.separateCache;
dan6809c962012-07-30 14:53:54 +0000736 pCache = (PCache1 *)sqlite3MallocZero(sz);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000737 if( pCache ){
drhdb7ae892015-07-06 20:57:22 +0000738 if( pcache1.separateCache ){
drh9f8cf9d2011-01-17 21:32:24 +0000739 pGroup = (PGroup*)&pCache[1];
drh41692e92011-01-25 04:34:51 +0000740 pGroup->mxPinned = 10;
drh9f8cf9d2011-01-17 21:32:24 +0000741 }else{
dan9dde7cb2011-06-09 17:53:43 +0000742 pGroup = &pcache1.grp;
drh9f8cf9d2011-01-17 21:32:24 +0000743 }
drh92af02c2015-09-04 04:31:56 +0000744 if( pGroup->lru.isAnchor==0 ){
745 pGroup->lru.isAnchor = 1;
746 pGroup->lru.pLruPrev = pGroup->lru.pLruNext = &pGroup->lru;
747 }
drh9f8cf9d2011-01-17 21:32:24 +0000748 pCache->pGroup = pGroup;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000749 pCache->szPage = szPage;
dan22e21ff2011-11-08 20:08:44 +0000750 pCache->szExtra = szExtra;
drhee70a842015-07-06 18:54:52 +0000751 pCache->szAlloc = szPage + szExtra + ROUND8(sizeof(PgHdr1));
danielk1977bc2ca9e2008-11-13 14:28:28 +0000752 pCache->bPurgeable = (bPurgeable ? 1 : 0);
drhefbf0442014-08-23 23:15:31 +0000753 pcache1EnterMutex(pGroup);
754 pcache1ResizeHash(pCache);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000755 if( bPurgeable ){
756 pCache->nMin = 10;
drh9f8cf9d2011-01-17 21:32:24 +0000757 pGroup->nMinPage += pCache->nMin;
drh41692e92011-01-25 04:34:51 +0000758 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
drhefbf0442014-08-23 23:15:31 +0000759 }
760 pcache1LeaveMutex(pGroup);
761 if( pCache->nHash==0 ){
762 pcache1Destroy((sqlite3_pcache*)pCache);
763 pCache = 0;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000764 }
765 }
766 return (sqlite3_pcache *)pCache;
767}
768
769/*
770** Implementation of the sqlite3_pcache.xCachesize method.
771**
772** Configure the cache_size limit for a cache.
773*/
774static void pcache1Cachesize(sqlite3_pcache *p, int nMax){
775 PCache1 *pCache = (PCache1 *)p;
776 if( pCache->bPurgeable ){
drh9f8cf9d2011-01-17 21:32:24 +0000777 PGroup *pGroup = pCache->pGroup;
778 pcache1EnterMutex(pGroup);
779 pGroup->nMaxPage += (nMax - pCache->nMax);
drh41692e92011-01-25 04:34:51 +0000780 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000781 pCache->nMax = nMax;
drh25ca5682011-01-26 00:07:03 +0000782 pCache->n90pct = pCache->nMax*9/10;
drh957026a2015-07-16 18:18:19 +0000783 pcache1EnforceMaxPage(pCache);
drh9f8cf9d2011-01-17 21:32:24 +0000784 pcache1LeaveMutex(pGroup);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000785 }
786}
787
788/*
drh09419b42011-11-16 19:29:17 +0000789** Implementation of the sqlite3_pcache.xShrink method.
790**
791** Free up as much memory as possible.
792*/
793static void pcache1Shrink(sqlite3_pcache *p){
794 PCache1 *pCache = (PCache1*)p;
795 if( pCache->bPurgeable ){
796 PGroup *pGroup = pCache->pGroup;
797 int savedMaxPage;
798 pcache1EnterMutex(pGroup);
799 savedMaxPage = pGroup->nMaxPage;
800 pGroup->nMaxPage = 0;
drh957026a2015-07-16 18:18:19 +0000801 pcache1EnforceMaxPage(pCache);
drh09419b42011-11-16 19:29:17 +0000802 pGroup->nMaxPage = savedMaxPage;
803 pcache1LeaveMutex(pGroup);
804 }
805}
806
807/*
danielk1977bc2ca9e2008-11-13 14:28:28 +0000808** Implementation of the sqlite3_pcache.xPagecount method.
809*/
810static int pcache1Pagecount(sqlite3_pcache *p){
811 int n;
drh9f8cf9d2011-01-17 21:32:24 +0000812 PCache1 *pCache = (PCache1*)p;
813 pcache1EnterMutex(pCache->pGroup);
814 n = pCache->nPage;
815 pcache1LeaveMutex(pCache->pGroup);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000816 return n;
817}
818
drhefbf0442014-08-23 23:15:31 +0000819
820/*
821** Implement steps 3, 4, and 5 of the pcache1Fetch() algorithm described
822** in the header of the pcache1Fetch() procedure.
823**
824** This steps are broken out into a separate procedure because they are
825** usually not needed, and by avoiding the stack initialization required
826** for these steps, the main pcache1Fetch() procedure can run faster.
827*/
828static SQLITE_NOINLINE PgHdr1 *pcache1FetchStage2(
829 PCache1 *pCache,
830 unsigned int iKey,
831 int createFlag
832){
833 unsigned int nPinned;
834 PGroup *pGroup = pCache->pGroup;
835 PgHdr1 *pPage = 0;
836
837 /* Step 3: Abort if createFlag is 1 but the cache is nearly full */
838 assert( pCache->nPage >= pCache->nRecyclable );
839 nPinned = pCache->nPage - pCache->nRecyclable;
840 assert( pGroup->mxPinned == pGroup->nMaxPage + 10 - pGroup->nMinPage );
841 assert( pCache->n90pct == pCache->nMax*9/10 );
842 if( createFlag==1 && (
843 nPinned>=pGroup->mxPinned
844 || nPinned>=pCache->n90pct
dan5bd8af72014-10-10 19:10:59 +0000845 || (pcache1UnderMemoryPressure(pCache) && pCache->nRecyclable<nPinned)
drhefbf0442014-08-23 23:15:31 +0000846 )){
847 return 0;
848 }
849
850 if( pCache->nPage>=pCache->nHash ) pcache1ResizeHash(pCache);
851 assert( pCache->nHash>0 && pCache->apHash );
852
853 /* Step 4. Try to recycle a page. */
drhc54357c2015-07-07 14:06:18 +0000854 if( pCache->bPurgeable
drh92af02c2015-09-04 04:31:56 +0000855 && !pGroup->lru.pLruPrev->isAnchor
drhc54357c2015-07-07 14:06:18 +0000856 && ((pCache->nPage+1>=pCache->nMax) || pcache1UnderMemoryPressure(pCache))
857 ){
drhefbf0442014-08-23 23:15:31 +0000858 PCache1 *pOther;
drh92af02c2015-09-04 04:31:56 +0000859 pPage = pGroup->lru.pLruPrev;
drhefbf0442014-08-23 23:15:31 +0000860 assert( pPage->isPinned==0 );
drh95c91e12015-06-29 00:21:00 +0000861 pcache1RemoveFromHash(pPage, 0);
drhefbf0442014-08-23 23:15:31 +0000862 pcache1PinPage(pPage);
863 pOther = pPage->pCache;
drhee70a842015-07-06 18:54:52 +0000864 if( pOther->szAlloc != pCache->szAlloc ){
drhefbf0442014-08-23 23:15:31 +0000865 pcache1FreePage(pPage);
866 pPage = 0;
867 }else{
868 pGroup->nCurrentPage -= (pOther->bPurgeable - pCache->bPurgeable);
869 }
870 }
871
872 /* Step 5. If a usable page buffer has still not been found,
873 ** attempt to allocate a new one.
874 */
875 if( !pPage ){
drh3c0c4312015-09-01 19:51:37 +0000876 pPage = pcache1AllocPage(pCache, createFlag==1);
drhefbf0442014-08-23 23:15:31 +0000877 }
878
879 if( pPage ){
880 unsigned int h = iKey % pCache->nHash;
881 pCache->nPage++;
882 pPage->iKey = iKey;
883 pPage->pNext = pCache->apHash[h];
884 pPage->pCache = pCache;
885 pPage->pLruPrev = 0;
886 pPage->pLruNext = 0;
887 pPage->isPinned = 1;
888 *(void **)pPage->page.pExtra = 0;
889 pCache->apHash[h] = pPage;
890 if( iKey>pCache->iMaxKey ){
891 pCache->iMaxKey = iKey;
892 }
893 }
894 return pPage;
895}
896
danielk1977bc2ca9e2008-11-13 14:28:28 +0000897/*
898** Implementation of the sqlite3_pcache.xFetch method.
899**
900** Fetch a page by key value.
901**
902** Whether or not a new page may be allocated by this function depends on
drhf18a61d2009-07-17 11:44:07 +0000903** the value of the createFlag argument. 0 means do not allocate a new
904** page. 1 means allocate a new page if space is easily available. 2
905** means to try really hard to allocate a new page.
906**
907** For a non-purgeable cache (a cache used as the storage for an in-memory
908** database) there is really no difference between createFlag 1 and 2. So
909** the calling function (pcache.c) will never have a createFlag of 1 on
drh45d29302012-01-08 22:18:33 +0000910** a non-purgeable cache.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000911**
912** There are three different approaches to obtaining space for a page,
913** depending on the value of parameter createFlag (which may be 0, 1 or 2).
914**
915** 1. Regardless of the value of createFlag, the cache is searched for a
916** copy of the requested page. If one is found, it is returned.
917**
918** 2. If createFlag==0 and the page is not already in the cache, NULL is
919** returned.
920**
drh50d1b5f2010-08-27 12:21:06 +0000921** 3. If createFlag is 1, and the page is not already in the cache, then
922** return NULL (do not allocate a new page) if any of the following
923** conditions are true:
danielk1977bc2ca9e2008-11-13 14:28:28 +0000924**
925** (a) the number of pages pinned by the cache is greater than
926** PCache1.nMax, or
drh50d1b5f2010-08-27 12:21:06 +0000927**
danielk1977bc2ca9e2008-11-13 14:28:28 +0000928** (b) the number of pages pinned by the cache is greater than
929** the sum of nMax for all purgeable caches, less the sum of
drh50d1b5f2010-08-27 12:21:06 +0000930** nMin for all other purgeable caches, or
danielk1977bc2ca9e2008-11-13 14:28:28 +0000931**
932** 4. If none of the first three conditions apply and the cache is marked
933** as purgeable, and if one of the following is true:
934**
935** (a) The number of pages allocated for the cache is already
936** PCache1.nMax, or
937**
938** (b) The number of pages allocated for all purgeable caches is
939** already equal to or greater than the sum of nMax for all
940** purgeable caches,
941**
drh50d1b5f2010-08-27 12:21:06 +0000942** (c) The system is under memory pressure and wants to avoid
943** unnecessary pages cache entry allocations
944**
danielk1977bc2ca9e2008-11-13 14:28:28 +0000945** then attempt to recycle a page from the LRU list. If it is the right
946** size, return the recycled buffer. Otherwise, free the buffer and
947** proceed to step 5.
948**
949** 5. Otherwise, allocate and return a new page buffer.
drh55a46c92015-06-12 13:49:26 +0000950**
951** There are two versions of this routine. pcache1FetchWithMutex() is
952** the general case. pcache1FetchNoMutex() is a faster implementation for
953** the common case where pGroup->mutex is NULL. The pcache1Fetch() wrapper
954** invokes the appropriate routine.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000955*/
drh55a46c92015-06-12 13:49:26 +0000956static PgHdr1 *pcache1FetchNoMutex(
dan22e21ff2011-11-08 20:08:44 +0000957 sqlite3_pcache *p,
958 unsigned int iKey,
959 int createFlag
960){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000961 PCache1 *pCache = (PCache1 *)p;
962 PgHdr1 *pPage = 0;
963
drh3a5676c2011-01-19 21:58:56 +0000964 /* Step 1: Search the hash table for an existing entry. */
drhefbf0442014-08-23 23:15:31 +0000965 pPage = pCache->apHash[iKey % pCache->nHash];
966 while( pPage && pPage->iKey!=iKey ){ pPage = pPage->pNext; }
danielk1977bc2ca9e2008-11-13 14:28:28 +0000967
drh95a0b372015-09-03 20:43:55 +0000968 /* Step 2: If the page was found in the hash table, then return it.
969 ** If the page was not in the hash table and createFlag is 0, abort.
970 ** Otherwise (page not in hash and createFlag!=0) continue with
971 ** subsequent steps to try to create the page. */
drh5d56dd22013-12-13 18:50:40 +0000972 if( pPage ){
drh55a46c92015-06-12 13:49:26 +0000973 if( !pPage->isPinned ){
974 return pcache1PinPage(pPage);
975 }else{
976 return pPage;
977 }
drhefbf0442014-08-23 23:15:31 +0000978 }else if( createFlag ){
979 /* Steps 3, 4, and 5 implemented by this subroutine */
drh55a46c92015-06-12 13:49:26 +0000980 return pcache1FetchStage2(pCache, iKey, createFlag);
981 }else{
982 return 0;
drh5d56dd22013-12-13 18:50:40 +0000983 }
drh55a46c92015-06-12 13:49:26 +0000984}
drh982215a2015-06-13 11:10:55 +0000985#if PCACHE1_MIGHT_USE_GROUP_MUTEX
drh55a46c92015-06-12 13:49:26 +0000986static PgHdr1 *pcache1FetchWithMutex(
987 sqlite3_pcache *p,
988 unsigned int iKey,
989 int createFlag
990){
991 PCache1 *pCache = (PCache1 *)p;
992 PgHdr1 *pPage;
993
994 pcache1EnterMutex(pCache->pGroup);
995 pPage = pcache1FetchNoMutex(p, iKey, createFlag);
drhefbf0442014-08-23 23:15:31 +0000996 assert( pPage==0 || pCache->iMaxKey>=iKey );
997 pcache1LeaveMutex(pCache->pGroup);
drh55a46c92015-06-12 13:49:26 +0000998 return pPage;
999}
drh982215a2015-06-13 11:10:55 +00001000#endif
drh55a46c92015-06-12 13:49:26 +00001001static sqlite3_pcache_page *pcache1Fetch(
1002 sqlite3_pcache *p,
1003 unsigned int iKey,
1004 int createFlag
1005){
drh982215a2015-06-13 11:10:55 +00001006#if PCACHE1_MIGHT_USE_GROUP_MUTEX || defined(SQLITE_DEBUG)
drh55a46c92015-06-12 13:49:26 +00001007 PCache1 *pCache = (PCache1 *)p;
drh982215a2015-06-13 11:10:55 +00001008#endif
drh55a46c92015-06-12 13:49:26 +00001009
1010 assert( offsetof(PgHdr1,page)==0 );
1011 assert( pCache->bPurgeable || createFlag!=1 );
1012 assert( pCache->bPurgeable || pCache->nMin==0 );
1013 assert( pCache->bPurgeable==0 || pCache->nMin==10 );
1014 assert( pCache->nMin==0 || pCache->bPurgeable );
1015 assert( pCache->nHash>0 );
drh982215a2015-06-13 11:10:55 +00001016#if PCACHE1_MIGHT_USE_GROUP_MUTEX
drh55a46c92015-06-12 13:49:26 +00001017 if( pCache->pGroup->mutex ){
1018 return (sqlite3_pcache_page*)pcache1FetchWithMutex(p, iKey, createFlag);
drh982215a2015-06-13 11:10:55 +00001019 }else
1020#endif
1021 {
drh55a46c92015-06-12 13:49:26 +00001022 return (sqlite3_pcache_page*)pcache1FetchNoMutex(p, iKey, createFlag);
1023 }
danielk1977bc2ca9e2008-11-13 14:28:28 +00001024}
1025
1026
1027/*
1028** Implementation of the sqlite3_pcache.xUnpin method.
1029**
1030** Mark a page as unpinned (eligible for asynchronous recycling).
1031*/
dan22e21ff2011-11-08 20:08:44 +00001032static void pcache1Unpin(
1033 sqlite3_pcache *p,
1034 sqlite3_pcache_page *pPg,
1035 int reuseUnlikely
1036){
danielk1977bc2ca9e2008-11-13 14:28:28 +00001037 PCache1 *pCache = (PCache1 *)p;
dan22e21ff2011-11-08 20:08:44 +00001038 PgHdr1 *pPage = (PgHdr1 *)pPg;
drh9f8cf9d2011-01-17 21:32:24 +00001039 PGroup *pGroup = pCache->pGroup;
drh69e931e2009-06-03 21:04:35 +00001040
1041 assert( pPage->pCache==pCache );
drh9f8cf9d2011-01-17 21:32:24 +00001042 pcache1EnterMutex(pGroup);
danielk1977bc2ca9e2008-11-13 14:28:28 +00001043
1044 /* It is an error to call this function if the page is already
drh9f8cf9d2011-01-17 21:32:24 +00001045 ** part of the PGroup LRU list.
danielk1977bc2ca9e2008-11-13 14:28:28 +00001046 */
1047 assert( pPage->pLruPrev==0 && pPage->pLruNext==0 );
drh5d56dd22013-12-13 18:50:40 +00001048 assert( pPage->isPinned==1 );
danielk1977bc2ca9e2008-11-13 14:28:28 +00001049
drh9f8cf9d2011-01-17 21:32:24 +00001050 if( reuseUnlikely || pGroup->nCurrentPage>pGroup->nMaxPage ){
drh95c91e12015-06-29 00:21:00 +00001051 pcache1RemoveFromHash(pPage, 1);
danielk1977bc2ca9e2008-11-13 14:28:28 +00001052 }else{
drh9f8cf9d2011-01-17 21:32:24 +00001053 /* Add the page to the PGroup LRU list. */
drh92af02c2015-09-04 04:31:56 +00001054 PgHdr1 **ppFirst = &pGroup->lru.pLruNext;
1055 pPage->pLruPrev = &pGroup->lru;
1056 (pPage->pLruNext = *ppFirst)->pLruPrev = pPage;
1057 *ppFirst = pPage;
danielk1977bc2ca9e2008-11-13 14:28:28 +00001058 pCache->nRecyclable++;
drh5d56dd22013-12-13 18:50:40 +00001059 pPage->isPinned = 0;
danielk1977bc2ca9e2008-11-13 14:28:28 +00001060 }
1061
drh9f8cf9d2011-01-17 21:32:24 +00001062 pcache1LeaveMutex(pCache->pGroup);
danielk1977bc2ca9e2008-11-13 14:28:28 +00001063}
1064
1065/*
1066** Implementation of the sqlite3_pcache.xRekey method.
1067*/
1068static void pcache1Rekey(
1069 sqlite3_pcache *p,
dan22e21ff2011-11-08 20:08:44 +00001070 sqlite3_pcache_page *pPg,
danielk1977bc2ca9e2008-11-13 14:28:28 +00001071 unsigned int iOld,
1072 unsigned int iNew
1073){
1074 PCache1 *pCache = (PCache1 *)p;
dan22e21ff2011-11-08 20:08:44 +00001075 PgHdr1 *pPage = (PgHdr1 *)pPg;
danielk1977bc2ca9e2008-11-13 14:28:28 +00001076 PgHdr1 **pp;
1077 unsigned int h;
1078 assert( pPage->iKey==iOld );
drh69e931e2009-06-03 21:04:35 +00001079 assert( pPage->pCache==pCache );
danielk1977bc2ca9e2008-11-13 14:28:28 +00001080
drh9f8cf9d2011-01-17 21:32:24 +00001081 pcache1EnterMutex(pCache->pGroup);
danielk1977bc2ca9e2008-11-13 14:28:28 +00001082
1083 h = iOld%pCache->nHash;
1084 pp = &pCache->apHash[h];
1085 while( (*pp)!=pPage ){
1086 pp = &(*pp)->pNext;
1087 }
1088 *pp = pPage->pNext;
1089
1090 h = iNew%pCache->nHash;
1091 pPage->iKey = iNew;
1092 pPage->pNext = pCache->apHash[h];
1093 pCache->apHash[h] = pPage;
drh98829a62009-11-20 13:18:14 +00001094 if( iNew>pCache->iMaxKey ){
danielk1977f90b7262009-01-07 15:18:20 +00001095 pCache->iMaxKey = iNew;
1096 }
1097
drh9f8cf9d2011-01-17 21:32:24 +00001098 pcache1LeaveMutex(pCache->pGroup);
danielk1977bc2ca9e2008-11-13 14:28:28 +00001099}
1100
1101/*
1102** Implementation of the sqlite3_pcache.xTruncate method.
1103**
1104** Discard all unpinned pages in the cache with a page number equal to
1105** or greater than parameter iLimit. Any pinned pages with a page number
1106** equal to or greater than iLimit are implicitly unpinned.
1107*/
1108static void pcache1Truncate(sqlite3_pcache *p, unsigned int iLimit){
1109 PCache1 *pCache = (PCache1 *)p;
drh9f8cf9d2011-01-17 21:32:24 +00001110 pcache1EnterMutex(pCache->pGroup);
danielk1977f90b7262009-01-07 15:18:20 +00001111 if( iLimit<=pCache->iMaxKey ){
1112 pcache1TruncateUnsafe(pCache, iLimit);
1113 pCache->iMaxKey = iLimit-1;
1114 }
drh9f8cf9d2011-01-17 21:32:24 +00001115 pcache1LeaveMutex(pCache->pGroup);
danielk1977bc2ca9e2008-11-13 14:28:28 +00001116}
1117
1118/*
1119** Implementation of the sqlite3_pcache.xDestroy method.
1120**
1121** Destroy a cache allocated using pcache1Create().
1122*/
1123static void pcache1Destroy(sqlite3_pcache *p){
1124 PCache1 *pCache = (PCache1 *)p;
drh9f8cf9d2011-01-17 21:32:24 +00001125 PGroup *pGroup = pCache->pGroup;
danb51d2fa2010-09-22 19:06:02 +00001126 assert( pCache->bPurgeable || (pCache->nMax==0 && pCache->nMin==0) );
drh9f8cf9d2011-01-17 21:32:24 +00001127 pcache1EnterMutex(pGroup);
danielk1977bc2ca9e2008-11-13 14:28:28 +00001128 pcache1TruncateUnsafe(pCache, 0);
drha69085c2012-01-02 18:00:55 +00001129 assert( pGroup->nMaxPage >= pCache->nMax );
drh9f8cf9d2011-01-17 21:32:24 +00001130 pGroup->nMaxPage -= pCache->nMax;
drha69085c2012-01-02 18:00:55 +00001131 assert( pGroup->nMinPage >= pCache->nMin );
drh9f8cf9d2011-01-17 21:32:24 +00001132 pGroup->nMinPage -= pCache->nMin;
drh41692e92011-01-25 04:34:51 +00001133 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
drh957026a2015-07-16 18:18:19 +00001134 pcache1EnforceMaxPage(pCache);
drh9f8cf9d2011-01-17 21:32:24 +00001135 pcache1LeaveMutex(pGroup);
drhee70a842015-07-06 18:54:52 +00001136 sqlite3_free(pCache->pBulk);
danielk1977bc2ca9e2008-11-13 14:28:28 +00001137 sqlite3_free(pCache->apHash);
1138 sqlite3_free(pCache);
1139}
1140
1141/*
1142** This function is called during initialization (sqlite3_initialize()) to
1143** install the default pluggable cache module, assuming the user has not
1144** already provided an alternative.
1145*/
1146void sqlite3PCacheSetDefault(void){
dan22e21ff2011-11-08 20:08:44 +00001147 static const sqlite3_pcache_methods2 defaultMethods = {
drh81ef0f92011-11-13 21:44:03 +00001148 1, /* iVersion */
danielk1977bc2ca9e2008-11-13 14:28:28 +00001149 0, /* pArg */
1150 pcache1Init, /* xInit */
1151 pcache1Shutdown, /* xShutdown */
1152 pcache1Create, /* xCreate */
1153 pcache1Cachesize, /* xCachesize */
1154 pcache1Pagecount, /* xPagecount */
1155 pcache1Fetch, /* xFetch */
1156 pcache1Unpin, /* xUnpin */
1157 pcache1Rekey, /* xRekey */
1158 pcache1Truncate, /* xTruncate */
drh09419b42011-11-16 19:29:17 +00001159 pcache1Destroy, /* xDestroy */
1160 pcache1Shrink /* xShrink */
danielk1977bc2ca9e2008-11-13 14:28:28 +00001161 };
dan22e21ff2011-11-08 20:08:44 +00001162 sqlite3_config(SQLITE_CONFIG_PCACHE2, &defaultMethods);
danielk1977bc2ca9e2008-11-13 14:28:28 +00001163}
1164
drhdef68892014-11-04 12:11:23 +00001165/*
1166** Return the size of the header on each page of this PCACHE implementation.
1167*/
drh37c057b2014-12-30 00:57:29 +00001168int sqlite3HeaderSizePcache1(void){ return ROUND8(sizeof(PgHdr1)); }
drhdef68892014-11-04 12:11:23 +00001169
drhaf89fe62015-03-23 17:25:18 +00001170/*
1171** Return the global mutex used by this PCACHE implementation. The
1172** sqlite3_status() routine needs access to this mutex.
1173*/
1174sqlite3_mutex *sqlite3Pcache1Mutex(void){
1175 return pcache1.mutex;
1176}
1177
danielk1977bc2ca9e2008-11-13 14:28:28 +00001178#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
1179/*
1180** This function is called to free superfluous dynamically allocated memory
1181** held by the pager system. Memory in use by any SQLite pager allocated
1182** by the current thread may be sqlite3_free()ed.
1183**
1184** nReq is the number of bytes of memory required. Once this much has
1185** been released, the function returns. The return value is the total number
1186** of bytes of memory released.
1187*/
1188int sqlite3PcacheReleaseMemory(int nReq){
1189 int nFree = 0;
drh9f8cf9d2011-01-17 21:32:24 +00001190 assert( sqlite3_mutex_notheld(pcache1.grp.mutex) );
1191 assert( sqlite3_mutex_notheld(pcache1.mutex) );
drhee70a842015-07-06 18:54:52 +00001192 if( sqlite3GlobalConfig.nPage==0 ){
danielk1977bc2ca9e2008-11-13 14:28:28 +00001193 PgHdr1 *p;
drh9f8cf9d2011-01-17 21:32:24 +00001194 pcache1EnterMutex(&pcache1.grp);
drh92af02c2015-09-04 04:31:56 +00001195 while( (nReq<0 || nFree<nReq)
drh88202502015-09-09 19:27:10 +00001196 && (p=pcache1.grp.lru.pLruPrev)!=0
1197 && p->isAnchor==0
drh92af02c2015-09-04 04:31:56 +00001198 ){
dan22e21ff2011-11-08 20:08:44 +00001199 nFree += pcache1MemSize(p->page.pBuf);
1200#ifdef SQLITE_PCACHE_SEPARATE_HEADER
1201 nFree += sqlite3MemSize(p);
1202#endif
drh5d56dd22013-12-13 18:50:40 +00001203 assert( p->isPinned==0 );
danielk1977bc2ca9e2008-11-13 14:28:28 +00001204 pcache1PinPage(p);
drh95c91e12015-06-29 00:21:00 +00001205 pcache1RemoveFromHash(p, 1);
danielk1977bc2ca9e2008-11-13 14:28:28 +00001206 }
drh9f8cf9d2011-01-17 21:32:24 +00001207 pcache1LeaveMutex(&pcache1.grp);
danielk1977bc2ca9e2008-11-13 14:28:28 +00001208 }
1209 return nFree;
1210}
1211#endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
1212
1213#ifdef SQLITE_TEST
1214/*
1215** This function is used by test procedures to inspect the internal state
1216** of the global cache.
1217*/
1218void sqlite3PcacheStats(
1219 int *pnCurrent, /* OUT: Total number of pages cached */
1220 int *pnMax, /* OUT: Global maximum cache size */
1221 int *pnMin, /* OUT: Sum of PCache1.nMin for purgeable caches */
1222 int *pnRecyclable /* OUT: Total number of pages available for recycling */
1223){
1224 PgHdr1 *p;
1225 int nRecyclable = 0;
drh0b19c962015-09-10 19:22:25 +00001226 for(p=pcache1.grp.lru.pLruNext; p && !p->isAnchor; p=p->pLruNext){
drh5d56dd22013-12-13 18:50:40 +00001227 assert( p->isPinned==0 );
danielk1977bc2ca9e2008-11-13 14:28:28 +00001228 nRecyclable++;
1229 }
drh9f8cf9d2011-01-17 21:32:24 +00001230 *pnCurrent = pcache1.grp.nCurrentPage;
drha69085c2012-01-02 18:00:55 +00001231 *pnMax = (int)pcache1.grp.nMaxPage;
1232 *pnMin = (int)pcache1.grp.nMinPage;
danielk1977bc2ca9e2008-11-13 14:28:28 +00001233 *pnRecyclable = nRecyclable;
1234}
1235#endif