blob: ad443954bffd0901fcc3e0e93a3b0788d7b997c4 [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.
16** If the default page cache implementation is overriden, then neither of
17** these two features are available.
danielk1977bc2ca9e2008-11-13 14:28:28 +000018*/
19
20#include "sqliteInt.h"
21
22typedef struct PCache1 PCache1;
23typedef struct PgHdr1 PgHdr1;
24typedef struct PgFreeslot PgFreeslot;
drh9f8cf9d2011-01-17 21:32:24 +000025typedef struct PGroup PGroup;
26
27/* Each page cache (or PCache) belongs to a PGroup. A PGroup is a set
28** of one or more PCaches that are able to recycle each others unpinned
29** pages when they are under memory pressure. A PGroup is an instance of
30** the following object.
31**
32** This page cache implementation works in one of two modes:
33**
34** (1) Every PCache is the sole member of its own PGroup. There is
35** one PGroup per PCache.
36**
37** (2) There is a single global PGroup that all PCaches are a member
38** of.
39**
40** Mode 1 uses more memory (since PCache instances are not able to rob
41** unused pages from other PCaches) but it also operates without a mutex,
42** and is therefore often faster. Mode 2 requires a mutex in order to be
43** threadsafe, but is able recycle pages more efficient.
44**
45** For mode (1), PGroup.mutex is NULL. For mode (2) there is only a single
46** PGroup which is the pcache1.grp global variable and its mutex is
47** SQLITE_MUTEX_STATIC_LRU.
48*/
49struct PGroup {
50 sqlite3_mutex *mutex; /* MUTEX_STATIC_LRU or NULL */
51 int nMaxPage; /* Sum of nMax for purgeable caches */
52 int nMinPage; /* Sum of nMin for purgeable caches */
drh41692e92011-01-25 04:34:51 +000053 int mxPinned; /* nMaxpage + 10 - nMinPage */
drh9f8cf9d2011-01-17 21:32:24 +000054 int nCurrentPage; /* Number of purgeable pages allocated */
55 PgHdr1 *pLruHead, *pLruTail; /* LRU list of unpinned pages */
56};
danielk1977bc2ca9e2008-11-13 14:28:28 +000057
drh9d13f112010-08-24 18:06:35 +000058/* Each page cache is an instance of the following object. Every
59** open database file (including each in-memory database and each
60** temporary or transient database) has a single page cache which
61** is an instance of this object.
62**
63** Pointers to structures of this type are cast and returned as
64** opaque sqlite3_pcache* handles.
danielk1977bc2ca9e2008-11-13 14:28:28 +000065*/
66struct PCache1 {
67 /* Cache configuration parameters. Page size (szPage) and the purgeable
68 ** flag (bPurgeable) are set when the cache is created. nMax may be
69 ** modified at any time by a call to the pcache1CacheSize() method.
drh9f8cf9d2011-01-17 21:32:24 +000070 ** The PGroup mutex must be held when accessing nMax.
danielk1977bc2ca9e2008-11-13 14:28:28 +000071 */
drh9f8cf9d2011-01-17 21:32:24 +000072 PGroup *pGroup; /* PGroup this cache belongs to */
danielk1977bc2ca9e2008-11-13 14:28:28 +000073 int szPage; /* Size of allocated pages in bytes */
74 int bPurgeable; /* True if cache is purgeable */
danielk197744cd45c2008-11-15 11:22:45 +000075 unsigned int nMin; /* Minimum number of pages reserved */
76 unsigned int nMax; /* Configured "cache_size" value */
drh25ca5682011-01-26 00:07:03 +000077 unsigned int n90pct; /* nMax*9/10 */
danielk1977bc2ca9e2008-11-13 14:28:28 +000078
79 /* Hash table of all pages. The following variables may only be accessed
drh9f8cf9d2011-01-17 21:32:24 +000080 ** when the accessor is holding the PGroup mutex.
danielk1977bc2ca9e2008-11-13 14:28:28 +000081 */
danielk197744cd45c2008-11-15 11:22:45 +000082 unsigned int nRecyclable; /* Number of pages in the LRU list */
83 unsigned int nPage; /* Total number of pages in apHash */
84 unsigned int nHash; /* Number of slots in apHash[] */
danielk1977bc2ca9e2008-11-13 14:28:28 +000085 PgHdr1 **apHash; /* Hash table for fast lookup by key */
danielk1977f90b7262009-01-07 15:18:20 +000086
87 unsigned int iMaxKey; /* Largest key seen since xTruncate() */
danielk1977bc2ca9e2008-11-13 14:28:28 +000088};
89
90/*
91** Each cache entry is represented by an instance of the following
92** structure. A buffer of PgHdr1.pCache->szPage bytes is allocated
drh69e931e2009-06-03 21:04:35 +000093** directly before this structure in memory (see the PGHDR1_TO_PAGE()
danielk1977bc2ca9e2008-11-13 14:28:28 +000094** macro below).
95*/
96struct PgHdr1 {
97 unsigned int iKey; /* Key value (page number) */
98 PgHdr1 *pNext; /* Next in hash table chain */
99 PCache1 *pCache; /* Cache that currently owns this page */
100 PgHdr1 *pLruNext; /* Next in LRU list of unpinned pages */
101 PgHdr1 *pLruPrev; /* Previous in LRU list of unpinned pages */
102};
103
104/*
105** Free slots in the allocator used to divide up the buffer provided using
106** the SQLITE_CONFIG_PAGECACHE mechanism.
107*/
108struct PgFreeslot {
109 PgFreeslot *pNext; /* Next free slot */
110};
111
112/*
113** Global data used by this cache.
114*/
115static SQLITE_WSD struct PCacheGlobal {
drh9f8cf9d2011-01-17 21:32:24 +0000116 PGroup grp; /* The global PGroup for mode (2) */
danielk1977bc2ca9e2008-11-13 14:28:28 +0000117
drh9f8cf9d2011-01-17 21:32:24 +0000118 /* Variables related to SQLITE_CONFIG_PAGECACHE settings. The
119 ** szSlot, nSlot, pStart, pEnd, nReserve, and isInit values are all
120 ** fixed at sqlite3_initialize() time and do not require mutex protection.
121 ** The nFreeSlot and pFree values do require mutex protection.
122 */
123 int isInit; /* True if initialized */
124 int szSlot; /* Size of each free slot */
125 int nSlot; /* The number of pcache slots */
126 int nReserve; /* Try to keep nFreeSlot above this */
127 void *pStart, *pEnd; /* Bounds of pagecache malloc range */
128 /* Above requires no mutex. Use mutex below for variable that follow. */
129 sqlite3_mutex *mutex; /* Mutex for accessing the following: */
130 int nFreeSlot; /* Number of unused pcache slots */
131 PgFreeslot *pFree; /* Free page blocks */
132 /* The following value requires a mutex to change. We skip the mutex on
133 ** reading because (1) most platforms read a 32-bit integer atomically and
134 ** (2) even if an incorrect value is read, no great harm is done since this
135 ** is really just an optimization. */
136 int bUnderPressure; /* True if low on PAGECACHE memory */
danielk197744cd45c2008-11-15 11:22:45 +0000137} pcache1_g;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000138
139/*
140** All code in this file should access the global structure above via the
141** alias "pcache1". This ensures that the WSD emulation is used when
142** compiling for systems that do not support real WSD.
143*/
144#define pcache1 (GLOBAL(struct PCacheGlobal, pcache1_g))
145
146/*
147** When a PgHdr1 structure is allocated, the associated PCache1.szPage
drh69e931e2009-06-03 21:04:35 +0000148** bytes of data are located directly before it in memory (i.e. the total
danielk1977bc2ca9e2008-11-13 14:28:28 +0000149** size of the allocation is sizeof(PgHdr1)+PCache1.szPage byte). The
150** PGHDR1_TO_PAGE() macro takes a pointer to a PgHdr1 structure as
151** an argument and returns a pointer to the associated block of szPage
152** bytes. The PAGE_TO_PGHDR1() macro does the opposite: its argument is
153** a pointer to a block of szPage bytes of data and the return value is
154** a pointer to the associated PgHdr1 structure.
155**
drh69e931e2009-06-03 21:04:35 +0000156** assert( PGHDR1_TO_PAGE(PAGE_TO_PGHDR1(pCache, X))==X );
danielk1977bc2ca9e2008-11-13 14:28:28 +0000157*/
drh69e931e2009-06-03 21:04:35 +0000158#define PGHDR1_TO_PAGE(p) (void*)(((char*)p) - p->pCache->szPage)
159#define PAGE_TO_PGHDR1(c, p) (PgHdr1*)(((char*)p) + c->szPage)
danielk1977bc2ca9e2008-11-13 14:28:28 +0000160
161/*
drh9f8cf9d2011-01-17 21:32:24 +0000162** Macros to enter and leave the PCache LRU mutex.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000163*/
drh9f8cf9d2011-01-17 21:32:24 +0000164#define pcache1EnterMutex(X) sqlite3_mutex_enter((X)->mutex)
165#define pcache1LeaveMutex(X) sqlite3_mutex_leave((X)->mutex)
danielk1977bc2ca9e2008-11-13 14:28:28 +0000166
167/******************************************************************************/
168/******** Page Allocation/SQLITE_CONFIG_PCACHE Related Functions **************/
169
170/*
171** This function is called during initialization if a static buffer is
172** supplied to use for the page-cache by passing the SQLITE_CONFIG_PAGECACHE
173** verb to sqlite3_config(). Parameter pBuf points to an allocation large
174** enough to contain 'n' buffers of 'sz' bytes each.
drh9f8cf9d2011-01-17 21:32:24 +0000175**
176** This routine is called from sqlite3_initialize() and so it is guaranteed
177** to be serialized already. There is no need for further mutexing.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000178*/
179void sqlite3PCacheBufferSetup(void *pBuf, int sz, int n){
drhf4622dc2009-05-22 11:10:24 +0000180 if( pcache1.isInit ){
181 PgFreeslot *p;
182 sz = ROUNDDOWN8(sz);
183 pcache1.szSlot = sz;
drh50d1b5f2010-08-27 12:21:06 +0000184 pcache1.nSlot = pcache1.nFreeSlot = n;
185 pcache1.nReserve = n>90 ? 10 : (n/10 + 1);
drhf4622dc2009-05-22 11:10:24 +0000186 pcache1.pStart = pBuf;
187 pcache1.pFree = 0;
drh9f8cf9d2011-01-17 21:32:24 +0000188 pcache1.bUnderPressure = 0;
drhf4622dc2009-05-22 11:10:24 +0000189 while( n-- ){
190 p = (PgFreeslot*)pBuf;
191 p->pNext = pcache1.pFree;
192 pcache1.pFree = p;
193 pBuf = (void*)&((char*)pBuf)[sz];
194 }
195 pcache1.pEnd = pBuf;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000196 }
danielk1977bc2ca9e2008-11-13 14:28:28 +0000197}
198
199/*
200** Malloc function used within this file to allocate space from the buffer
201** configured using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no
202** such buffer exists or there is no space left in it, this function falls
203** back to sqlite3Malloc().
drh9f8cf9d2011-01-17 21:32:24 +0000204**
205** Multiple threads can run this routine at the same time. Global variables
206** in pcache1 need to be protected via mutex.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000207*/
208static void *pcache1Alloc(int nByte){
drh9f8cf9d2011-01-17 21:32:24 +0000209 void *p = 0;
210 assert( sqlite3_mutex_notheld(pcache1.grp.mutex) );
drh29dfbe32010-07-28 17:01:24 +0000211 sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, nByte);
drh9f8cf9d2011-01-17 21:32:24 +0000212 if( nByte<=pcache1.szSlot ){
213 sqlite3_mutex_enter(pcache1.mutex);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000214 p = (PgHdr1 *)pcache1.pFree;
drh9f8cf9d2011-01-17 21:32:24 +0000215 if( p ){
216 pcache1.pFree = pcache1.pFree->pNext;
217 pcache1.nFreeSlot--;
218 pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve;
219 assert( pcache1.nFreeSlot>=0 );
220 sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, 1);
221 }
222 sqlite3_mutex_leave(pcache1.mutex);
223 }
224 if( p==0 ){
225 /* Memory is not available in the SQLITE_CONFIG_PAGECACHE pool. Get
226 ** it from sqlite3Malloc instead.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000227 */
danielk1977bc2ca9e2008-11-13 14:28:28 +0000228 p = sqlite3Malloc(nByte);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000229 if( p ){
230 int sz = sqlite3MallocSize(p);
drh9bf3da8e2011-01-26 13:24:40 +0000231 sqlite3_mutex_enter(pcache1.mutex);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000232 sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, sz);
drh9bf3da8e2011-01-26 13:24:40 +0000233 sqlite3_mutex_leave(pcache1.mutex);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000234 }
drh107b56e2010-03-12 16:32:53 +0000235 sqlite3MemdebugSetType(p, MEMTYPE_PCACHE);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000236 }
237 return p;
238}
239
240/*
241** Free an allocated buffer obtained from pcache1Alloc().
242*/
243static void pcache1Free(void *p){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000244 if( p==0 ) return;
245 if( p>=pcache1.pStart && p<pcache1.pEnd ){
246 PgFreeslot *pSlot;
drh9f8cf9d2011-01-17 21:32:24 +0000247 sqlite3_mutex_enter(pcache1.mutex);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000248 sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, -1);
249 pSlot = (PgFreeslot*)p;
250 pSlot->pNext = pcache1.pFree;
251 pcache1.pFree = pSlot;
drh50d1b5f2010-08-27 12:21:06 +0000252 pcache1.nFreeSlot++;
drh9f8cf9d2011-01-17 21:32:24 +0000253 pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve;
drh50d1b5f2010-08-27 12:21:06 +0000254 assert( pcache1.nFreeSlot<=pcache1.nSlot );
drh9f8cf9d2011-01-17 21:32:24 +0000255 sqlite3_mutex_leave(pcache1.mutex);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000256 }else{
drh107b56e2010-03-12 16:32:53 +0000257 int iSize;
258 assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) );
259 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
260 iSize = sqlite3MallocSize(p);
drh15ad92f2011-01-26 13:28:06 +0000261 sqlite3_mutex_enter(pcache1.mutex);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000262 sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, -iSize);
drh15ad92f2011-01-26 13:28:06 +0000263 sqlite3_mutex_leave(pcache1.mutex);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000264 sqlite3_free(p);
265 }
266}
267
drhc8f503a2010-08-20 09:14:13 +0000268#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
269/*
drh9d13f112010-08-24 18:06:35 +0000270** Return the size of a pcache allocation
drhc8f503a2010-08-20 09:14:13 +0000271*/
272static int pcache1MemSize(void *p){
drhc8f503a2010-08-20 09:14:13 +0000273 if( p>=pcache1.pStart && p<pcache1.pEnd ){
274 return pcache1.szSlot;
275 }else{
276 int iSize;
277 assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) );
278 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
279 iSize = sqlite3MallocSize(p);
280 sqlite3MemdebugSetType(p, MEMTYPE_PCACHE);
281 return iSize;
282 }
283}
284#endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
285
danielk1977bc2ca9e2008-11-13 14:28:28 +0000286/*
287** Allocate a new page object initially associated with cache pCache.
288*/
289static PgHdr1 *pcache1AllocPage(PCache1 *pCache){
290 int nByte = sizeof(PgHdr1) + pCache->szPage;
drh69e931e2009-06-03 21:04:35 +0000291 void *pPg = pcache1Alloc(nByte);
292 PgHdr1 *p;
293 if( pPg ){
294 p = PAGE_TO_PGHDR1(pCache, pPg);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000295 if( pCache->bPurgeable ){
drh9f8cf9d2011-01-17 21:32:24 +0000296 pCache->pGroup->nCurrentPage++;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000297 }
drh69e931e2009-06-03 21:04:35 +0000298 }else{
299 p = 0;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000300 }
301 return p;
302}
303
304/*
305** Free a page object allocated by pcache1AllocPage().
drhf18a61d2009-07-17 11:44:07 +0000306**
307** The pointer is allowed to be NULL, which is prudent. But it turns out
308** that the current implementation happens to never call this routine
309** with a NULL pointer, so we mark the NULL test with ALWAYS().
danielk1977bc2ca9e2008-11-13 14:28:28 +0000310*/
311static void pcache1FreePage(PgHdr1 *p){
drhf18a61d2009-07-17 11:44:07 +0000312 if( ALWAYS(p) ){
drh9f8cf9d2011-01-17 21:32:24 +0000313 PCache1 *pCache = p->pCache;
314 if( pCache->bPurgeable ){
315 pCache->pGroup->nCurrentPage--;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000316 }
drh69e931e2009-06-03 21:04:35 +0000317 pcache1Free(PGHDR1_TO_PAGE(p));
danielk1977bc2ca9e2008-11-13 14:28:28 +0000318 }
319}
320
321/*
322** Malloc function used by SQLite to obtain space from the buffer configured
323** using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no such buffer
324** exists, this function falls back to sqlite3Malloc().
325*/
326void *sqlite3PageMalloc(int sz){
drh9f8cf9d2011-01-17 21:32:24 +0000327 return pcache1Alloc(sz);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000328}
329
330/*
331** Free an allocated buffer obtained from sqlite3PageMalloc().
332*/
333void sqlite3PageFree(void *p){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000334 pcache1Free(p);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000335}
336
drh50d1b5f2010-08-27 12:21:06 +0000337
338/*
339** Return true if it desirable to avoid allocating a new page cache
340** entry.
341**
342** If memory was allocated specifically to the page cache using
343** SQLITE_CONFIG_PAGECACHE but that memory has all been used, then
344** it is desirable to avoid allocating a new page cache entry because
345** presumably SQLITE_CONFIG_PAGECACHE was suppose to be sufficient
346** for all page cache needs and we should not need to spill the
347** allocation onto the heap.
348**
349** Or, the heap is used for all page cache memory put the heap is
350** under memory pressure, then again it is desirable to avoid
351** allocating a new page cache entry in order to avoid stressing
352** the heap even further.
353*/
354static int pcache1UnderMemoryPressure(PCache1 *pCache){
drh50d1b5f2010-08-27 12:21:06 +0000355 if( pcache1.nSlot && pCache->szPage<=pcache1.szSlot ){
drh9f8cf9d2011-01-17 21:32:24 +0000356 return pcache1.bUnderPressure;
drh50d1b5f2010-08-27 12:21:06 +0000357 }else{
358 return sqlite3HeapNearlyFull();
359 }
360}
361
danielk1977bc2ca9e2008-11-13 14:28:28 +0000362/******************************************************************************/
363/******** General Implementation Functions ************************************/
364
365/*
366** This function is used to resize the hash table used by the cache passed
367** as the first argument.
368**
drh9f8cf9d2011-01-17 21:32:24 +0000369** The PCache mutex must be held when this function is called.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000370*/
371static int pcache1ResizeHash(PCache1 *p){
372 PgHdr1 **apNew;
danielk197744cd45c2008-11-15 11:22:45 +0000373 unsigned int nNew;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000374 unsigned int i;
375
drh9f8cf9d2011-01-17 21:32:24 +0000376 assert( sqlite3_mutex_held(p->pGroup->mutex) );
danielk1977bc2ca9e2008-11-13 14:28:28 +0000377
378 nNew = p->nHash*2;
379 if( nNew<256 ){
380 nNew = 256;
381 }
382
drh9f8cf9d2011-01-17 21:32:24 +0000383 pcache1LeaveMutex(p->pGroup);
drh085bb7f2008-12-06 14:34:33 +0000384 if( p->nHash ){ sqlite3BeginBenignMalloc(); }
danielk1977bc2ca9e2008-11-13 14:28:28 +0000385 apNew = (PgHdr1 **)sqlite3_malloc(sizeof(PgHdr1 *)*nNew);
drh085bb7f2008-12-06 14:34:33 +0000386 if( p->nHash ){ sqlite3EndBenignMalloc(); }
drh9f8cf9d2011-01-17 21:32:24 +0000387 pcache1EnterMutex(p->pGroup);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000388 if( apNew ){
389 memset(apNew, 0, sizeof(PgHdr1 *)*nNew);
390 for(i=0; i<p->nHash; i++){
391 PgHdr1 *pPage;
392 PgHdr1 *pNext = p->apHash[i];
drhb27b7f52008-12-10 18:03:45 +0000393 while( (pPage = pNext)!=0 ){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000394 unsigned int h = pPage->iKey % nNew;
395 pNext = pPage->pNext;
396 pPage->pNext = apNew[h];
397 apNew[h] = pPage;
398 }
399 }
400 sqlite3_free(p->apHash);
401 p->apHash = apNew;
402 p->nHash = nNew;
403 }
404
405 return (p->apHash ? SQLITE_OK : SQLITE_NOMEM);
406}
407
408/*
409** This function is used internally to remove the page pPage from the
drh9f8cf9d2011-01-17 21:32:24 +0000410** PGroup LRU list, if is part of it. If pPage is not part of the PGroup
danielk1977bc2ca9e2008-11-13 14:28:28 +0000411** LRU list, then this function is a no-op.
412**
drh9f8cf9d2011-01-17 21:32:24 +0000413** The PGroup mutex must be held when this function is called.
414**
415** If pPage is NULL then this routine is a no-op.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000416*/
417static void pcache1PinPage(PgHdr1 *pPage){
drh9f8cf9d2011-01-17 21:32:24 +0000418 PCache1 *pCache;
419 PGroup *pGroup;
420
421 if( pPage==0 ) return;
422 pCache = pPage->pCache;
423 pGroup = pCache->pGroup;
424 assert( sqlite3_mutex_held(pGroup->mutex) );
425 if( pPage->pLruNext || pPage==pGroup->pLruTail ){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000426 if( pPage->pLruPrev ){
427 pPage->pLruPrev->pLruNext = pPage->pLruNext;
428 }
429 if( pPage->pLruNext ){
430 pPage->pLruNext->pLruPrev = pPage->pLruPrev;
431 }
drh9f8cf9d2011-01-17 21:32:24 +0000432 if( pGroup->pLruHead==pPage ){
433 pGroup->pLruHead = pPage->pLruNext;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000434 }
drh9f8cf9d2011-01-17 21:32:24 +0000435 if( pGroup->pLruTail==pPage ){
436 pGroup->pLruTail = pPage->pLruPrev;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000437 }
438 pPage->pLruNext = 0;
439 pPage->pLruPrev = 0;
440 pPage->pCache->nRecyclable--;
441 }
442}
443
444
445/*
446** Remove the page supplied as an argument from the hash table
447** (PCache1.apHash structure) that it is currently stored in.
448**
drh9f8cf9d2011-01-17 21:32:24 +0000449** The PGroup mutex must be held when this function is called.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000450*/
451static void pcache1RemoveFromHash(PgHdr1 *pPage){
452 unsigned int h;
453 PCache1 *pCache = pPage->pCache;
454 PgHdr1 **pp;
455
drh9f8cf9d2011-01-17 21:32:24 +0000456 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
danielk1977bc2ca9e2008-11-13 14:28:28 +0000457 h = pPage->iKey % pCache->nHash;
458 for(pp=&pCache->apHash[h]; (*pp)!=pPage; pp=&(*pp)->pNext);
459 *pp = (*pp)->pNext;
460
461 pCache->nPage--;
462}
463
464/*
drh9f8cf9d2011-01-17 21:32:24 +0000465** If there are currently more than nMaxPage pages allocated, try
466** to recycle pages to reduce the number allocated to nMaxPage.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000467*/
drh9f8cf9d2011-01-17 21:32:24 +0000468static void pcache1EnforceMaxPage(PGroup *pGroup){
469 assert( sqlite3_mutex_held(pGroup->mutex) );
470 while( pGroup->nCurrentPage>pGroup->nMaxPage && pGroup->pLruTail ){
471 PgHdr1 *p = pGroup->pLruTail;
472 assert( p->pCache->pGroup==pGroup );
danielk1977bc2ca9e2008-11-13 14:28:28 +0000473 pcache1PinPage(p);
474 pcache1RemoveFromHash(p);
475 pcache1FreePage(p);
476 }
477}
478
479/*
480** Discard all pages from cache pCache with a page number (key value)
481** greater than or equal to iLimit. Any pinned pages that meet this
482** criteria are unpinned before they are discarded.
483**
drh9f8cf9d2011-01-17 21:32:24 +0000484** The PCache mutex must be held when this function is called.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000485*/
486static void pcache1TruncateUnsafe(
drh9f8cf9d2011-01-17 21:32:24 +0000487 PCache1 *pCache, /* The cache to truncate */
488 unsigned int iLimit /* Drop pages with this pgno or larger */
danielk1977bc2ca9e2008-11-13 14:28:28 +0000489){
drh9f8cf9d2011-01-17 21:32:24 +0000490 TESTONLY( unsigned int nPage = 0; ) /* To assert pCache->nPage is correct */
danielk1977bc2ca9e2008-11-13 14:28:28 +0000491 unsigned int h;
drh9f8cf9d2011-01-17 21:32:24 +0000492 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
danielk1977bc2ca9e2008-11-13 14:28:28 +0000493 for(h=0; h<pCache->nHash; h++){
494 PgHdr1 **pp = &pCache->apHash[h];
495 PgHdr1 *pPage;
drhb27b7f52008-12-10 18:03:45 +0000496 while( (pPage = *pp)!=0 ){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000497 if( pPage->iKey>=iLimit ){
danielk1977ea24ac42009-05-08 06:52:47 +0000498 pCache->nPage--;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000499 *pp = pPage->pNext;
danielk1977ea24ac42009-05-08 06:52:47 +0000500 pcache1PinPage(pPage);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000501 pcache1FreePage(pPage);
502 }else{
503 pp = &pPage->pNext;
danielk1977ea24ac42009-05-08 06:52:47 +0000504 TESTONLY( nPage++; )
danielk1977bc2ca9e2008-11-13 14:28:28 +0000505 }
506 }
507 }
danielk1977ea24ac42009-05-08 06:52:47 +0000508 assert( pCache->nPage==nPage );
danielk1977bc2ca9e2008-11-13 14:28:28 +0000509}
510
511/******************************************************************************/
512/******** sqlite3_pcache Methods **********************************************/
513
514/*
515** Implementation of the sqlite3_pcache.xInit method.
516*/
danielk197762c14b32008-11-19 09:05:26 +0000517static int pcache1Init(void *NotUsed){
518 UNUSED_PARAMETER(NotUsed);
drhf4622dc2009-05-22 11:10:24 +0000519 assert( pcache1.isInit==0 );
danielk1977bc2ca9e2008-11-13 14:28:28 +0000520 memset(&pcache1, 0, sizeof(pcache1));
521 if( sqlite3GlobalConfig.bCoreMutex ){
drh9f8cf9d2011-01-17 21:32:24 +0000522 pcache1.grp.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU);
drh40f98372011-01-18 15:17:57 +0000523 pcache1.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_PMEM);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000524 }
drh41692e92011-01-25 04:34:51 +0000525 pcache1.grp.mxPinned = 10;
drhf4622dc2009-05-22 11:10:24 +0000526 pcache1.isInit = 1;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000527 return SQLITE_OK;
528}
529
530/*
531** Implementation of the sqlite3_pcache.xShutdown method.
shane7c7c3112009-08-17 15:31:23 +0000532** Note that the static mutex allocated in xInit does
533** not need to be freed.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000534*/
danielk197762c14b32008-11-19 09:05:26 +0000535static void pcache1Shutdown(void *NotUsed){
536 UNUSED_PARAMETER(NotUsed);
drhf4622dc2009-05-22 11:10:24 +0000537 assert( pcache1.isInit!=0 );
drhb0937192009-05-22 10:53:29 +0000538 memset(&pcache1, 0, sizeof(pcache1));
danielk1977bc2ca9e2008-11-13 14:28:28 +0000539}
540
541/*
542** Implementation of the sqlite3_pcache.xCreate method.
543**
544** Allocate a new cache.
545*/
546static sqlite3_pcache *pcache1Create(int szPage, int bPurgeable){
drh9f8cf9d2011-01-17 21:32:24 +0000547 PCache1 *pCache; /* The newly created page cache */
548 PGroup *pGroup; /* The group the new page cache will belong to */
549 int sz; /* Bytes of memory required to allocate the new cache */
danielk1977bc2ca9e2008-11-13 14:28:28 +0000550
drh9f8cf9d2011-01-17 21:32:24 +0000551 /*
552 ** The seperateCache variable is true if each PCache has its own private
553 ** PGroup. In other words, separateCache is true for mode (1) where no
554 ** mutexing is required.
555 **
556 ** * Always use a unified cache (mode-2) if ENABLE_MEMORY_MANAGEMENT
557 **
558 ** * Always use a unified cache in single-threaded applications
559 **
560 ** * Otherwise (if multi-threaded and ENABLE_MEMORY_MANAGEMENT is off)
561 ** use separate caches (mode-1)
562 */
563#if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) || SQLITE_THREADSAFE==0
564 const int separateCache = 0;
565#else
566 int separateCache = sqlite3GlobalConfig.bCoreMutex>0;
567#endif
568
569 sz = sizeof(PCache1) + sizeof(PGroup)*separateCache;
570 pCache = (PCache1 *)sqlite3_malloc(sz);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000571 if( pCache ){
drh9f8cf9d2011-01-17 21:32:24 +0000572 memset(pCache, 0, sz);
573 if( separateCache ){
574 pGroup = (PGroup*)&pCache[1];
drh41692e92011-01-25 04:34:51 +0000575 pGroup->mxPinned = 10;
drh9f8cf9d2011-01-17 21:32:24 +0000576 }else{
577 pGroup = &pcache1_g.grp;
578 }
579 pCache->pGroup = pGroup;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000580 pCache->szPage = szPage;
581 pCache->bPurgeable = (bPurgeable ? 1 : 0);
582 if( bPurgeable ){
583 pCache->nMin = 10;
drh9f8cf9d2011-01-17 21:32:24 +0000584 pcache1EnterMutex(pGroup);
585 pGroup->nMinPage += pCache->nMin;
drh41692e92011-01-25 04:34:51 +0000586 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
drh9f8cf9d2011-01-17 21:32:24 +0000587 pcache1LeaveMutex(pGroup);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000588 }
589 }
590 return (sqlite3_pcache *)pCache;
591}
592
593/*
594** Implementation of the sqlite3_pcache.xCachesize method.
595**
596** Configure the cache_size limit for a cache.
597*/
598static void pcache1Cachesize(sqlite3_pcache *p, int nMax){
599 PCache1 *pCache = (PCache1 *)p;
600 if( pCache->bPurgeable ){
drh9f8cf9d2011-01-17 21:32:24 +0000601 PGroup *pGroup = pCache->pGroup;
602 pcache1EnterMutex(pGroup);
603 pGroup->nMaxPage += (nMax - pCache->nMax);
drh41692e92011-01-25 04:34:51 +0000604 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000605 pCache->nMax = nMax;
drh25ca5682011-01-26 00:07:03 +0000606 pCache->n90pct = pCache->nMax*9/10;
drh9f8cf9d2011-01-17 21:32:24 +0000607 pcache1EnforceMaxPage(pGroup);
608 pcache1LeaveMutex(pGroup);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000609 }
610}
611
612/*
613** Implementation of the sqlite3_pcache.xPagecount method.
614*/
615static int pcache1Pagecount(sqlite3_pcache *p){
616 int n;
drh9f8cf9d2011-01-17 21:32:24 +0000617 PCache1 *pCache = (PCache1*)p;
618 pcache1EnterMutex(pCache->pGroup);
619 n = pCache->nPage;
620 pcache1LeaveMutex(pCache->pGroup);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000621 return n;
622}
623
624/*
625** Implementation of the sqlite3_pcache.xFetch method.
626**
627** Fetch a page by key value.
628**
629** Whether or not a new page may be allocated by this function depends on
drhf18a61d2009-07-17 11:44:07 +0000630** the value of the createFlag argument. 0 means do not allocate a new
631** page. 1 means allocate a new page if space is easily available. 2
632** means to try really hard to allocate a new page.
633**
634** For a non-purgeable cache (a cache used as the storage for an in-memory
635** database) there is really no difference between createFlag 1 and 2. So
636** the calling function (pcache.c) will never have a createFlag of 1 on
637** a non-purgable cache.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000638**
639** There are three different approaches to obtaining space for a page,
640** depending on the value of parameter createFlag (which may be 0, 1 or 2).
641**
642** 1. Regardless of the value of createFlag, the cache is searched for a
643** copy of the requested page. If one is found, it is returned.
644**
645** 2. If createFlag==0 and the page is not already in the cache, NULL is
646** returned.
647**
drh50d1b5f2010-08-27 12:21:06 +0000648** 3. If createFlag is 1, and the page is not already in the cache, then
649** return NULL (do not allocate a new page) if any of the following
650** conditions are true:
danielk1977bc2ca9e2008-11-13 14:28:28 +0000651**
652** (a) the number of pages pinned by the cache is greater than
653** PCache1.nMax, or
drh50d1b5f2010-08-27 12:21:06 +0000654**
danielk1977bc2ca9e2008-11-13 14:28:28 +0000655** (b) the number of pages pinned by the cache is greater than
656** the sum of nMax for all purgeable caches, less the sum of
drh50d1b5f2010-08-27 12:21:06 +0000657** nMin for all other purgeable caches, or
danielk1977bc2ca9e2008-11-13 14:28:28 +0000658**
659** 4. If none of the first three conditions apply and the cache is marked
660** as purgeable, and if one of the following is true:
661**
662** (a) The number of pages allocated for the cache is already
663** PCache1.nMax, or
664**
665** (b) The number of pages allocated for all purgeable caches is
666** already equal to or greater than the sum of nMax for all
667** purgeable caches,
668**
drh50d1b5f2010-08-27 12:21:06 +0000669** (c) The system is under memory pressure and wants to avoid
670** unnecessary pages cache entry allocations
671**
danielk1977bc2ca9e2008-11-13 14:28:28 +0000672** then attempt to recycle a page from the LRU list. If it is the right
673** size, return the recycled buffer. Otherwise, free the buffer and
674** proceed to step 5.
675**
676** 5. Otherwise, allocate and return a new page buffer.
677*/
678static void *pcache1Fetch(sqlite3_pcache *p, unsigned int iKey, int createFlag){
drh1e4040a2011-01-25 18:30:51 +0000679 int nPinned;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000680 PCache1 *pCache = (PCache1 *)p;
drh41692e92011-01-25 04:34:51 +0000681 PGroup *pGroup;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000682 PgHdr1 *pPage = 0;
683
drhf18a61d2009-07-17 11:44:07 +0000684 assert( pCache->bPurgeable || createFlag!=1 );
drh41692e92011-01-25 04:34:51 +0000685 assert( pCache->bPurgeable || pCache->nMin==0 );
686 assert( pCache->bPurgeable==0 || pCache->nMin==10 );
687 assert( pCache->nMin==0 || pCache->bPurgeable );
688 pcache1EnterMutex(pGroup = pCache->pGroup);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000689
drh3a5676c2011-01-19 21:58:56 +0000690 /* Step 1: Search the hash table for an existing entry. */
danielk1977bc2ca9e2008-11-13 14:28:28 +0000691 if( pCache->nHash>0 ){
692 unsigned int h = iKey % pCache->nHash;
693 for(pPage=pCache->apHash[h]; pPage&&pPage->iKey!=iKey; pPage=pPage->pNext);
694 }
695
drh3a5676c2011-01-19 21:58:56 +0000696 /* Step 2: Abort if no existing page is found and createFlag is 0 */
danielk1977bc2ca9e2008-11-13 14:28:28 +0000697 if( pPage || createFlag==0 ){
698 pcache1PinPage(pPage);
699 goto fetch_out;
700 }
701
drh41692e92011-01-25 04:34:51 +0000702 /* The pGroup local variable will normally be initialized by the
703 ** pcache1EnterMutex() macro above. But if SQLITE_MUTEX_OMIT is defined,
704 ** then pcache1EnterMutex() is a no-op, so we have to initialize the
705 ** local variable here. Delaying the initialization of pGroup is an
706 ** optimization: The common case is to exit the module before reaching
707 ** this point.
708 */
709#ifdef SQLITE_MUTEX_OMIT
710 pGroup = pCache->pGroup;
711#endif
712
713
drh3a5676c2011-01-19 21:58:56 +0000714 /* Step 3: Abort if createFlag is 1 but the cache is nearly full */
danielk1977bc2ca9e2008-11-13 14:28:28 +0000715 nPinned = pCache->nPage - pCache->nRecyclable;
drh25ca5682011-01-26 00:07:03 +0000716 assert( nPinned>=0 );
drh41692e92011-01-25 04:34:51 +0000717 assert( pGroup->mxPinned == pGroup->nMaxPage + 10 - pGroup->nMinPage );
drh25ca5682011-01-26 00:07:03 +0000718 assert( pCache->n90pct == pCache->nMax*9/10 );
drhf18a61d2009-07-17 11:44:07 +0000719 if( createFlag==1 && (
drh41692e92011-01-25 04:34:51 +0000720 nPinned>=pGroup->mxPinned
drh25ca5682011-01-26 00:07:03 +0000721 || nPinned>=(int)pCache->n90pct
drh50d1b5f2010-08-27 12:21:06 +0000722 || pcache1UnderMemoryPressure(pCache)
danielk1977bc2ca9e2008-11-13 14:28:28 +0000723 )){
724 goto fetch_out;
725 }
726
727 if( pCache->nPage>=pCache->nHash && pcache1ResizeHash(pCache) ){
728 goto fetch_out;
729 }
730
drh3a5676c2011-01-19 21:58:56 +0000731 /* Step 4. Try to recycle a page. */
drh9f8cf9d2011-01-17 21:32:24 +0000732 if( pCache->bPurgeable && pGroup->pLruTail && (
drh50d1b5f2010-08-27 12:21:06 +0000733 (pCache->nPage+1>=pCache->nMax)
drh9f8cf9d2011-01-17 21:32:24 +0000734 || pGroup->nCurrentPage>=pGroup->nMaxPage
drh50d1b5f2010-08-27 12:21:06 +0000735 || pcache1UnderMemoryPressure(pCache)
danielk1977bc2ca9e2008-11-13 14:28:28 +0000736 )){
drh41692e92011-01-25 04:34:51 +0000737 PCache1 *pOtherCache;
drh9f8cf9d2011-01-17 21:32:24 +0000738 pPage = pGroup->pLruTail;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000739 pcache1RemoveFromHash(pPage);
740 pcache1PinPage(pPage);
drh41692e92011-01-25 04:34:51 +0000741 if( (pOtherCache = pPage->pCache)->szPage!=pCache->szPage ){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000742 pcache1FreePage(pPage);
743 pPage = 0;
744 }else{
drh9f8cf9d2011-01-17 21:32:24 +0000745 pGroup->nCurrentPage -=
drh41692e92011-01-25 04:34:51 +0000746 (pOtherCache->bPurgeable - pCache->bPurgeable);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000747 }
748 }
749
750 /* Step 5. If a usable page buffer has still not been found,
751 ** attempt to allocate a new one.
752 */
753 if( !pPage ){
drh41692e92011-01-25 04:34:51 +0000754 if( createFlag==1 ) sqlite3BeginBenignMalloc();
drh9f8cf9d2011-01-17 21:32:24 +0000755 pcache1LeaveMutex(pGroup);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000756 pPage = pcache1AllocPage(pCache);
drh9f8cf9d2011-01-17 21:32:24 +0000757 pcache1EnterMutex(pGroup);
drh41692e92011-01-25 04:34:51 +0000758 if( createFlag==1 ) sqlite3EndBenignMalloc();
danielk1977bc2ca9e2008-11-13 14:28:28 +0000759 }
760
761 if( pPage ){
762 unsigned int h = iKey % pCache->nHash;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000763 pCache->nPage++;
764 pPage->iKey = iKey;
765 pPage->pNext = pCache->apHash[h];
766 pPage->pCache = pCache;
danielk1977e1fd5082009-01-23 16:45:00 +0000767 pPage->pLruPrev = 0;
768 pPage->pLruNext = 0;
drh69e931e2009-06-03 21:04:35 +0000769 *(void **)(PGHDR1_TO_PAGE(pPage)) = 0;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000770 pCache->apHash[h] = pPage;
771 }
772
773fetch_out:
danielk1977f90b7262009-01-07 15:18:20 +0000774 if( pPage && iKey>pCache->iMaxKey ){
775 pCache->iMaxKey = iKey;
776 }
drh9f8cf9d2011-01-17 21:32:24 +0000777 pcache1LeaveMutex(pGroup);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000778 return (pPage ? PGHDR1_TO_PAGE(pPage) : 0);
779}
780
781
782/*
783** Implementation of the sqlite3_pcache.xUnpin method.
784**
785** Mark a page as unpinned (eligible for asynchronous recycling).
786*/
787static void pcache1Unpin(sqlite3_pcache *p, void *pPg, int reuseUnlikely){
788 PCache1 *pCache = (PCache1 *)p;
drh69e931e2009-06-03 21:04:35 +0000789 PgHdr1 *pPage = PAGE_TO_PGHDR1(pCache, pPg);
drh9f8cf9d2011-01-17 21:32:24 +0000790 PGroup *pGroup = pCache->pGroup;
drh69e931e2009-06-03 21:04:35 +0000791
792 assert( pPage->pCache==pCache );
drh9f8cf9d2011-01-17 21:32:24 +0000793 pcache1EnterMutex(pGroup);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000794
795 /* It is an error to call this function if the page is already
drh9f8cf9d2011-01-17 21:32:24 +0000796 ** part of the PGroup LRU list.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000797 */
798 assert( pPage->pLruPrev==0 && pPage->pLruNext==0 );
drh9f8cf9d2011-01-17 21:32:24 +0000799 assert( pGroup->pLruHead!=pPage && pGroup->pLruTail!=pPage );
danielk1977bc2ca9e2008-11-13 14:28:28 +0000800
drh9f8cf9d2011-01-17 21:32:24 +0000801 if( reuseUnlikely || pGroup->nCurrentPage>pGroup->nMaxPage ){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000802 pcache1RemoveFromHash(pPage);
803 pcache1FreePage(pPage);
804 }else{
drh9f8cf9d2011-01-17 21:32:24 +0000805 /* Add the page to the PGroup LRU list. */
806 if( pGroup->pLruHead ){
807 pGroup->pLruHead->pLruPrev = pPage;
808 pPage->pLruNext = pGroup->pLruHead;
809 pGroup->pLruHead = pPage;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000810 }else{
drh9f8cf9d2011-01-17 21:32:24 +0000811 pGroup->pLruTail = pPage;
812 pGroup->pLruHead = pPage;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000813 }
814 pCache->nRecyclable++;
815 }
816
drh9f8cf9d2011-01-17 21:32:24 +0000817 pcache1LeaveMutex(pCache->pGroup);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000818}
819
820/*
821** Implementation of the sqlite3_pcache.xRekey method.
822*/
823static void pcache1Rekey(
824 sqlite3_pcache *p,
825 void *pPg,
826 unsigned int iOld,
827 unsigned int iNew
828){
829 PCache1 *pCache = (PCache1 *)p;
drh69e931e2009-06-03 21:04:35 +0000830 PgHdr1 *pPage = PAGE_TO_PGHDR1(pCache, pPg);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000831 PgHdr1 **pp;
832 unsigned int h;
833 assert( pPage->iKey==iOld );
drh69e931e2009-06-03 21:04:35 +0000834 assert( pPage->pCache==pCache );
danielk1977bc2ca9e2008-11-13 14:28:28 +0000835
drh9f8cf9d2011-01-17 21:32:24 +0000836 pcache1EnterMutex(pCache->pGroup);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000837
838 h = iOld%pCache->nHash;
839 pp = &pCache->apHash[h];
840 while( (*pp)!=pPage ){
841 pp = &(*pp)->pNext;
842 }
843 *pp = pPage->pNext;
844
845 h = iNew%pCache->nHash;
846 pPage->iKey = iNew;
847 pPage->pNext = pCache->apHash[h];
848 pCache->apHash[h] = pPage;
drh98829a62009-11-20 13:18:14 +0000849 if( iNew>pCache->iMaxKey ){
danielk1977f90b7262009-01-07 15:18:20 +0000850 pCache->iMaxKey = iNew;
851 }
852
drh9f8cf9d2011-01-17 21:32:24 +0000853 pcache1LeaveMutex(pCache->pGroup);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000854}
855
856/*
857** Implementation of the sqlite3_pcache.xTruncate method.
858**
859** Discard all unpinned pages in the cache with a page number equal to
860** or greater than parameter iLimit. Any pinned pages with a page number
861** equal to or greater than iLimit are implicitly unpinned.
862*/
863static void pcache1Truncate(sqlite3_pcache *p, unsigned int iLimit){
864 PCache1 *pCache = (PCache1 *)p;
drh9f8cf9d2011-01-17 21:32:24 +0000865 pcache1EnterMutex(pCache->pGroup);
danielk1977f90b7262009-01-07 15:18:20 +0000866 if( iLimit<=pCache->iMaxKey ){
867 pcache1TruncateUnsafe(pCache, iLimit);
868 pCache->iMaxKey = iLimit-1;
869 }
drh9f8cf9d2011-01-17 21:32:24 +0000870 pcache1LeaveMutex(pCache->pGroup);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000871}
872
873/*
874** Implementation of the sqlite3_pcache.xDestroy method.
875**
876** Destroy a cache allocated using pcache1Create().
877*/
878static void pcache1Destroy(sqlite3_pcache *p){
879 PCache1 *pCache = (PCache1 *)p;
drh9f8cf9d2011-01-17 21:32:24 +0000880 PGroup *pGroup = pCache->pGroup;
danb51d2fa2010-09-22 19:06:02 +0000881 assert( pCache->bPurgeable || (pCache->nMax==0 && pCache->nMin==0) );
drh9f8cf9d2011-01-17 21:32:24 +0000882 pcache1EnterMutex(pGroup);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000883 pcache1TruncateUnsafe(pCache, 0);
drh9f8cf9d2011-01-17 21:32:24 +0000884 pGroup->nMaxPage -= pCache->nMax;
885 pGroup->nMinPage -= pCache->nMin;
drh41692e92011-01-25 04:34:51 +0000886 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
drh9f8cf9d2011-01-17 21:32:24 +0000887 pcache1EnforceMaxPage(pGroup);
888 pcache1LeaveMutex(pGroup);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000889 sqlite3_free(pCache->apHash);
890 sqlite3_free(pCache);
891}
892
893/*
894** This function is called during initialization (sqlite3_initialize()) to
895** install the default pluggable cache module, assuming the user has not
896** already provided an alternative.
897*/
898void sqlite3PCacheSetDefault(void){
dan558814f2010-06-02 05:53:53 +0000899 static const sqlite3_pcache_methods defaultMethods = {
danielk1977bc2ca9e2008-11-13 14:28:28 +0000900 0, /* pArg */
901 pcache1Init, /* xInit */
902 pcache1Shutdown, /* xShutdown */
903 pcache1Create, /* xCreate */
904 pcache1Cachesize, /* xCachesize */
905 pcache1Pagecount, /* xPagecount */
906 pcache1Fetch, /* xFetch */
907 pcache1Unpin, /* xUnpin */
908 pcache1Rekey, /* xRekey */
909 pcache1Truncate, /* xTruncate */
910 pcache1Destroy /* xDestroy */
911 };
912 sqlite3_config(SQLITE_CONFIG_PCACHE, &defaultMethods);
913}
914
915#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
916/*
917** This function is called to free superfluous dynamically allocated memory
918** held by the pager system. Memory in use by any SQLite pager allocated
919** by the current thread may be sqlite3_free()ed.
920**
921** nReq is the number of bytes of memory required. Once this much has
922** been released, the function returns. The return value is the total number
923** of bytes of memory released.
924*/
925int sqlite3PcacheReleaseMemory(int nReq){
926 int nFree = 0;
drh9f8cf9d2011-01-17 21:32:24 +0000927 assert( sqlite3_mutex_notheld(pcache1.grp.mutex) );
928 assert( sqlite3_mutex_notheld(pcache1.mutex) );
danielk1977bc2ca9e2008-11-13 14:28:28 +0000929 if( pcache1.pStart==0 ){
930 PgHdr1 *p;
drh9f8cf9d2011-01-17 21:32:24 +0000931 pcache1EnterMutex(&pcache1.grp);
932 while( (nReq<0 || nFree<nReq) && ((p=pcache1.grp.pLruTail)!=0) ){
drhc8f503a2010-08-20 09:14:13 +0000933 nFree += pcache1MemSize(PGHDR1_TO_PAGE(p));
danielk1977bc2ca9e2008-11-13 14:28:28 +0000934 pcache1PinPage(p);
935 pcache1RemoveFromHash(p);
936 pcache1FreePage(p);
937 }
drh9f8cf9d2011-01-17 21:32:24 +0000938 pcache1LeaveMutex(&pcache1.grp);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000939 }
940 return nFree;
941}
942#endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
943
944#ifdef SQLITE_TEST
945/*
946** This function is used by test procedures to inspect the internal state
947** of the global cache.
948*/
949void sqlite3PcacheStats(
950 int *pnCurrent, /* OUT: Total number of pages cached */
951 int *pnMax, /* OUT: Global maximum cache size */
952 int *pnMin, /* OUT: Sum of PCache1.nMin for purgeable caches */
953 int *pnRecyclable /* OUT: Total number of pages available for recycling */
954){
955 PgHdr1 *p;
956 int nRecyclable = 0;
drh9f8cf9d2011-01-17 21:32:24 +0000957 for(p=pcache1.grp.pLruHead; p; p=p->pLruNext){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000958 nRecyclable++;
959 }
drh9f8cf9d2011-01-17 21:32:24 +0000960 *pnCurrent = pcache1.grp.nCurrentPage;
961 *pnMax = pcache1.grp.nMaxPage;
962 *pnMin = pcache1.grp.nMinPage;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000963 *pnRecyclable = nRecyclable;
964}
965#endif