blob: 4147d2eff55bdd9ff7c6860c60181020166c5f50 [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
drh45d29302012-01-08 22:18:33 +000043** threadsafe, but recycles pages more efficiently.
drh9f8cf9d2011-01-17 21:32:24 +000044**
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 */
drha69085c2012-01-02 18:00:55 +000051 unsigned int nMaxPage; /* Sum of nMax for purgeable caches */
52 unsigned int nMinPage; /* Sum of nMin for purgeable caches */
53 unsigned int mxPinned; /* nMaxpage + 10 - nMinPage */
54 unsigned int nCurrentPage; /* Number of purgeable pages allocated */
drh9f8cf9d2011-01-17 21:32:24 +000055 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
drh45d29302012-01-08 22:18:33 +000069 ** 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 */
dan22e21ff2011-11-08 20:08:44 +000074 int szExtra; /* Size of extra space in bytes */
danielk1977bc2ca9e2008-11-13 14:28:28 +000075 int bPurgeable; /* True if cache is purgeable */
danielk197744cd45c2008-11-15 11:22:45 +000076 unsigned int nMin; /* Minimum number of pages reserved */
77 unsigned int nMax; /* Configured "cache_size" value */
drh25ca5682011-01-26 00:07:03 +000078 unsigned int n90pct; /* nMax*9/10 */
drh2cbd78b2012-02-02 19:37:18 +000079 unsigned int iMaxKey; /* Largest key seen since xTruncate() */
danielk1977bc2ca9e2008-11-13 14:28:28 +000080
81 /* Hash table of all pages. The following variables may only be accessed
drh9f8cf9d2011-01-17 21:32:24 +000082 ** when the accessor is holding the PGroup mutex.
danielk1977bc2ca9e2008-11-13 14:28:28 +000083 */
danielk197744cd45c2008-11-15 11:22:45 +000084 unsigned int nRecyclable; /* Number of pages in the LRU list */
85 unsigned int nPage; /* Total number of pages in apHash */
86 unsigned int nHash; /* Number of slots in apHash[] */
danielk1977bc2ca9e2008-11-13 14:28:28 +000087 PgHdr1 **apHash; /* Hash table for fast lookup by key */
88};
89
90/*
91** Each cache entry is represented by an instance of the following
dan22e21ff2011-11-08 20:08:44 +000092** 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.
danielk1977bc2ca9e2008-11-13 14:28:28 +000095*/
96struct PgHdr1 {
dan22e21ff2011-11-08 20:08:44 +000097 sqlite3_pcache_page page;
danielk1977bc2ca9e2008-11-13 14:28:28 +000098 unsigned int iKey; /* Key value (page number) */
99 PgHdr1 *pNext; /* Next in hash table chain */
100 PCache1 *pCache; /* Cache that currently owns this page */
101 PgHdr1 *pLruNext; /* Next in LRU list of unpinned pages */
102 PgHdr1 *pLruPrev; /* Previous in LRU list of unpinned pages */
103};
104
105/*
106** Free slots in the allocator used to divide up the buffer provided using
107** the SQLITE_CONFIG_PAGECACHE mechanism.
108*/
109struct PgFreeslot {
110 PgFreeslot *pNext; /* Next free slot */
111};
112
113/*
114** Global data used by this cache.
115*/
116static SQLITE_WSD struct PCacheGlobal {
drh9f8cf9d2011-01-17 21:32:24 +0000117 PGroup grp; /* The global PGroup for mode (2) */
danielk1977bc2ca9e2008-11-13 14:28:28 +0000118
drh9f8cf9d2011-01-17 21:32:24 +0000119 /* Variables related to SQLITE_CONFIG_PAGECACHE settings. The
120 ** szSlot, nSlot, pStart, pEnd, nReserve, and isInit values are all
121 ** fixed at sqlite3_initialize() time and do not require mutex protection.
122 ** The nFreeSlot and pFree values do require mutex protection.
123 */
124 int isInit; /* True if initialized */
125 int szSlot; /* Size of each free slot */
126 int nSlot; /* The number of pcache slots */
127 int nReserve; /* Try to keep nFreeSlot above this */
128 void *pStart, *pEnd; /* Bounds of pagecache malloc range */
129 /* Above requires no mutex. Use mutex below for variable that follow. */
130 sqlite3_mutex *mutex; /* Mutex for accessing the following: */
drh9f8cf9d2011-01-17 21:32:24 +0000131 PgFreeslot *pFree; /* Free page blocks */
drh2cbd78b2012-02-02 19:37:18 +0000132 int nFreeSlot; /* Number of unused pcache slots */
drh9f8cf9d2011-01-17 21:32:24 +0000133 /* The following value requires a mutex to change. We skip the mutex on
134 ** reading because (1) most platforms read a 32-bit integer atomically and
135 ** (2) even if an incorrect value is read, no great harm is done since this
136 ** is really just an optimization. */
137 int bUnderPressure; /* True if low on PAGECACHE memory */
danielk197744cd45c2008-11-15 11:22:45 +0000138} pcache1_g;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000139
140/*
141** All code in this file should access the global structure above via the
142** alias "pcache1". This ensures that the WSD emulation is used when
143** compiling for systems that do not support real WSD.
144*/
145#define pcache1 (GLOBAL(struct PCacheGlobal, pcache1_g))
146
147/*
drh9f8cf9d2011-01-17 21:32:24 +0000148** Macros to enter and leave the PCache LRU mutex.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000149*/
drh9f8cf9d2011-01-17 21:32:24 +0000150#define pcache1EnterMutex(X) sqlite3_mutex_enter((X)->mutex)
151#define pcache1LeaveMutex(X) sqlite3_mutex_leave((X)->mutex)
danielk1977bc2ca9e2008-11-13 14:28:28 +0000152
153/******************************************************************************/
154/******** Page Allocation/SQLITE_CONFIG_PCACHE Related Functions **************/
155
156/*
157** This function is called during initialization if a static buffer is
158** supplied to use for the page-cache by passing the SQLITE_CONFIG_PAGECACHE
159** verb to sqlite3_config(). Parameter pBuf points to an allocation large
160** enough to contain 'n' buffers of 'sz' bytes each.
drh9f8cf9d2011-01-17 21:32:24 +0000161**
162** This routine is called from sqlite3_initialize() and so it is guaranteed
163** to be serialized already. There is no need for further mutexing.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000164*/
165void sqlite3PCacheBufferSetup(void *pBuf, int sz, int n){
drhf4622dc2009-05-22 11:10:24 +0000166 if( pcache1.isInit ){
167 PgFreeslot *p;
168 sz = ROUNDDOWN8(sz);
169 pcache1.szSlot = sz;
drh50d1b5f2010-08-27 12:21:06 +0000170 pcache1.nSlot = pcache1.nFreeSlot = n;
171 pcache1.nReserve = n>90 ? 10 : (n/10 + 1);
drhf4622dc2009-05-22 11:10:24 +0000172 pcache1.pStart = pBuf;
173 pcache1.pFree = 0;
drh9f8cf9d2011-01-17 21:32:24 +0000174 pcache1.bUnderPressure = 0;
drhf4622dc2009-05-22 11:10:24 +0000175 while( n-- ){
176 p = (PgFreeslot*)pBuf;
177 p->pNext = pcache1.pFree;
178 pcache1.pFree = p;
179 pBuf = (void*)&((char*)pBuf)[sz];
180 }
181 pcache1.pEnd = pBuf;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000182 }
danielk1977bc2ca9e2008-11-13 14:28:28 +0000183}
184
185/*
186** Malloc function used within this file to allocate space from the buffer
187** configured using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no
188** such buffer exists or there is no space left in it, this function falls
189** back to sqlite3Malloc().
drh9f8cf9d2011-01-17 21:32:24 +0000190**
191** Multiple threads can run this routine at the same time. Global variables
192** in pcache1 need to be protected via mutex.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000193*/
194static void *pcache1Alloc(int nByte){
drh9f8cf9d2011-01-17 21:32:24 +0000195 void *p = 0;
196 assert( sqlite3_mutex_notheld(pcache1.grp.mutex) );
drh29dfbe32010-07-28 17:01:24 +0000197 sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, nByte);
drh9f8cf9d2011-01-17 21:32:24 +0000198 if( nByte<=pcache1.szSlot ){
199 sqlite3_mutex_enter(pcache1.mutex);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000200 p = (PgHdr1 *)pcache1.pFree;
drh9f8cf9d2011-01-17 21:32:24 +0000201 if( p ){
202 pcache1.pFree = pcache1.pFree->pNext;
203 pcache1.nFreeSlot--;
204 pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve;
205 assert( pcache1.nFreeSlot>=0 );
206 sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, 1);
207 }
208 sqlite3_mutex_leave(pcache1.mutex);
209 }
210 if( p==0 ){
211 /* Memory is not available in the SQLITE_CONFIG_PAGECACHE pool. Get
212 ** it from sqlite3Malloc instead.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000213 */
danielk1977bc2ca9e2008-11-13 14:28:28 +0000214 p = sqlite3Malloc(nByte);
drh4bd69522012-06-07 02:35:29 +0000215#ifndef SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS
danielk1977bc2ca9e2008-11-13 14:28:28 +0000216 if( p ){
217 int sz = sqlite3MallocSize(p);
drh9bf3da8e2011-01-26 13:24:40 +0000218 sqlite3_mutex_enter(pcache1.mutex);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000219 sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, sz);
drh9bf3da8e2011-01-26 13:24:40 +0000220 sqlite3_mutex_leave(pcache1.mutex);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000221 }
drh4bd69522012-06-07 02:35:29 +0000222#endif
drh107b56e2010-03-12 16:32:53 +0000223 sqlite3MemdebugSetType(p, MEMTYPE_PCACHE);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000224 }
225 return p;
226}
227
228/*
229** Free an allocated buffer obtained from pcache1Alloc().
230*/
drh09419b42011-11-16 19:29:17 +0000231static int pcache1Free(void *p){
232 int nFreed = 0;
233 if( p==0 ) return 0;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000234 if( p>=pcache1.pStart && p<pcache1.pEnd ){
235 PgFreeslot *pSlot;
drh9f8cf9d2011-01-17 21:32:24 +0000236 sqlite3_mutex_enter(pcache1.mutex);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000237 sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, -1);
238 pSlot = (PgFreeslot*)p;
239 pSlot->pNext = pcache1.pFree;
240 pcache1.pFree = pSlot;
drh50d1b5f2010-08-27 12:21:06 +0000241 pcache1.nFreeSlot++;
drh9f8cf9d2011-01-17 21:32:24 +0000242 pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve;
drh50d1b5f2010-08-27 12:21:06 +0000243 assert( pcache1.nFreeSlot<=pcache1.nSlot );
drh9f8cf9d2011-01-17 21:32:24 +0000244 sqlite3_mutex_leave(pcache1.mutex);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000245 }else{
drh107b56e2010-03-12 16:32:53 +0000246 assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) );
247 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
drh09419b42011-11-16 19:29:17 +0000248 nFreed = sqlite3MallocSize(p);
drh4bd69522012-06-07 02:35:29 +0000249#ifndef SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS
drh15ad92f2011-01-26 13:28:06 +0000250 sqlite3_mutex_enter(pcache1.mutex);
drh09419b42011-11-16 19:29:17 +0000251 sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, -nFreed);
drh15ad92f2011-01-26 13:28:06 +0000252 sqlite3_mutex_leave(pcache1.mutex);
drh4bd69522012-06-07 02:35:29 +0000253#endif
danielk1977bc2ca9e2008-11-13 14:28:28 +0000254 sqlite3_free(p);
255 }
drh09419b42011-11-16 19:29:17 +0000256 return nFreed;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000257}
258
drhc8f503a2010-08-20 09:14:13 +0000259#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
260/*
drh9d13f112010-08-24 18:06:35 +0000261** Return the size of a pcache allocation
drhc8f503a2010-08-20 09:14:13 +0000262*/
263static int pcache1MemSize(void *p){
drhc8f503a2010-08-20 09:14:13 +0000264 if( p>=pcache1.pStart && p<pcache1.pEnd ){
265 return pcache1.szSlot;
266 }else{
267 int iSize;
268 assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) );
269 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
270 iSize = sqlite3MallocSize(p);
271 sqlite3MemdebugSetType(p, MEMTYPE_PCACHE);
272 return iSize;
273 }
274}
275#endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
276
dand2925702011-08-19 18:15:00 +0000277/*
danielk1977bc2ca9e2008-11-13 14:28:28 +0000278** Allocate a new page object initially associated with cache pCache.
279*/
280static PgHdr1 *pcache1AllocPage(PCache1 *pCache){
danb5126dd2011-09-22 14:56:31 +0000281 PgHdr1 *p = 0;
282 void *pPg;
dand2925702011-08-19 18:15:00 +0000283
dand2925702011-08-19 18:15:00 +0000284 /* The group mutex must be released before pcache1Alloc() is called. This
285 ** is because it may call sqlite3_release_memory(), which assumes that
286 ** this mutex is not held. */
287 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
288 pcache1LeaveMutex(pCache->pGroup);
dan22e21ff2011-11-08 20:08:44 +0000289#ifdef SQLITE_PCACHE_SEPARATE_HEADER
290 pPg = pcache1Alloc(pCache->szPage);
291 p = sqlite3Malloc(sizeof(PgHdr1) + pCache->szExtra);
292 if( !pPg || !p ){
293 pcache1Free(pPg);
294 sqlite3_free(p);
295 pPg = 0;
296 }
297#else
298 pPg = pcache1Alloc(sizeof(PgHdr1) + pCache->szPage + pCache->szExtra);
299 p = (PgHdr1 *)&((u8 *)pPg)[pCache->szPage];
300#endif
dand2925702011-08-19 18:15:00 +0000301 pcache1EnterMutex(pCache->pGroup);
danb5126dd2011-09-22 14:56:31 +0000302
drh69e931e2009-06-03 21:04:35 +0000303 if( pPg ){
dan22e21ff2011-11-08 20:08:44 +0000304 p->page.pBuf = pPg;
305 p->page.pExtra = &p[1];
danielk1977bc2ca9e2008-11-13 14:28:28 +0000306 if( pCache->bPurgeable ){
drh9f8cf9d2011-01-17 21:32:24 +0000307 pCache->pGroup->nCurrentPage++;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000308 }
dan22e21ff2011-11-08 20:08:44 +0000309 return p;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000310 }
dan22e21ff2011-11-08 20:08:44 +0000311 return 0;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000312}
313
314/*
315** Free a page object allocated by pcache1AllocPage().
drhf18a61d2009-07-17 11:44:07 +0000316**
317** The pointer is allowed to be NULL, which is prudent. But it turns out
318** that the current implementation happens to never call this routine
319** with a NULL pointer, so we mark the NULL test with ALWAYS().
danielk1977bc2ca9e2008-11-13 14:28:28 +0000320*/
321static void pcache1FreePage(PgHdr1 *p){
drhf18a61d2009-07-17 11:44:07 +0000322 if( ALWAYS(p) ){
drh9f8cf9d2011-01-17 21:32:24 +0000323 PCache1 *pCache = p->pCache;
dand2925702011-08-19 18:15:00 +0000324 assert( sqlite3_mutex_held(p->pCache->pGroup->mutex) );
dan22e21ff2011-11-08 20:08:44 +0000325 pcache1Free(p->page.pBuf);
326#ifdef SQLITE_PCACHE_SEPARATE_HEADER
327 sqlite3_free(p);
328#endif
drh9f8cf9d2011-01-17 21:32:24 +0000329 if( pCache->bPurgeable ){
330 pCache->pGroup->nCurrentPage--;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000331 }
danielk1977bc2ca9e2008-11-13 14:28:28 +0000332 }
333}
334
335/*
336** Malloc function used by SQLite to obtain space from the buffer configured
337** using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no such buffer
338** exists, this function falls back to sqlite3Malloc().
339*/
340void *sqlite3PageMalloc(int sz){
drh9f8cf9d2011-01-17 21:32:24 +0000341 return pcache1Alloc(sz);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000342}
343
344/*
345** Free an allocated buffer obtained from sqlite3PageMalloc().
346*/
347void sqlite3PageFree(void *p){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000348 pcache1Free(p);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000349}
350
drh50d1b5f2010-08-27 12:21:06 +0000351
352/*
353** Return true if it desirable to avoid allocating a new page cache
354** entry.
355**
356** If memory was allocated specifically to the page cache using
357** SQLITE_CONFIG_PAGECACHE but that memory has all been used, then
358** it is desirable to avoid allocating a new page cache entry because
359** presumably SQLITE_CONFIG_PAGECACHE was suppose to be sufficient
360** for all page cache needs and we should not need to spill the
361** allocation onto the heap.
362**
drh45d29302012-01-08 22:18:33 +0000363** Or, the heap is used for all page cache memory but the heap is
drh50d1b5f2010-08-27 12:21:06 +0000364** under memory pressure, then again it is desirable to avoid
365** allocating a new page cache entry in order to avoid stressing
366** the heap even further.
367*/
368static int pcache1UnderMemoryPressure(PCache1 *pCache){
dan22e21ff2011-11-08 20:08:44 +0000369 if( pcache1.nSlot && (pCache->szPage+pCache->szExtra)<=pcache1.szSlot ){
drh9f8cf9d2011-01-17 21:32:24 +0000370 return pcache1.bUnderPressure;
drh50d1b5f2010-08-27 12:21:06 +0000371 }else{
372 return sqlite3HeapNearlyFull();
373 }
374}
375
danielk1977bc2ca9e2008-11-13 14:28:28 +0000376/******************************************************************************/
377/******** General Implementation Functions ************************************/
378
379/*
380** This function is used to resize the hash table used by the cache passed
381** as the first argument.
382**
drh9f8cf9d2011-01-17 21:32:24 +0000383** The PCache mutex must be held when this function is called.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000384*/
385static int pcache1ResizeHash(PCache1 *p){
386 PgHdr1 **apNew;
danielk197744cd45c2008-11-15 11:22:45 +0000387 unsigned int nNew;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000388 unsigned int i;
389
drh9f8cf9d2011-01-17 21:32:24 +0000390 assert( sqlite3_mutex_held(p->pGroup->mutex) );
danielk1977bc2ca9e2008-11-13 14:28:28 +0000391
392 nNew = p->nHash*2;
393 if( nNew<256 ){
394 nNew = 256;
395 }
396
drh9f8cf9d2011-01-17 21:32:24 +0000397 pcache1LeaveMutex(p->pGroup);
drh085bb7f2008-12-06 14:34:33 +0000398 if( p->nHash ){ sqlite3BeginBenignMalloc(); }
dan6809c962012-07-30 14:53:54 +0000399 apNew = (PgHdr1 **)sqlite3MallocZero(sizeof(PgHdr1 *)*nNew);
drh085bb7f2008-12-06 14:34:33 +0000400 if( p->nHash ){ sqlite3EndBenignMalloc(); }
drh9f8cf9d2011-01-17 21:32:24 +0000401 pcache1EnterMutex(p->pGroup);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000402 if( apNew ){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000403 for(i=0; i<p->nHash; i++){
404 PgHdr1 *pPage;
405 PgHdr1 *pNext = p->apHash[i];
drhb27b7f52008-12-10 18:03:45 +0000406 while( (pPage = pNext)!=0 ){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000407 unsigned int h = pPage->iKey % nNew;
408 pNext = pPage->pNext;
409 pPage->pNext = apNew[h];
410 apNew[h] = pPage;
411 }
412 }
413 sqlite3_free(p->apHash);
414 p->apHash = apNew;
415 p->nHash = nNew;
416 }
417
418 return (p->apHash ? SQLITE_OK : SQLITE_NOMEM);
419}
420
421/*
422** This function is used internally to remove the page pPage from the
drh9f8cf9d2011-01-17 21:32:24 +0000423** PGroup LRU list, if is part of it. If pPage is not part of the PGroup
danielk1977bc2ca9e2008-11-13 14:28:28 +0000424** LRU list, then this function is a no-op.
425**
drh9f8cf9d2011-01-17 21:32:24 +0000426** The PGroup mutex must be held when this function is called.
427**
428** If pPage is NULL then this routine is a no-op.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000429*/
430static void pcache1PinPage(PgHdr1 *pPage){
drh9f8cf9d2011-01-17 21:32:24 +0000431 PCache1 *pCache;
432 PGroup *pGroup;
433
434 if( pPage==0 ) return;
435 pCache = pPage->pCache;
436 pGroup = pCache->pGroup;
437 assert( sqlite3_mutex_held(pGroup->mutex) );
438 if( pPage->pLruNext || pPage==pGroup->pLruTail ){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000439 if( pPage->pLruPrev ){
440 pPage->pLruPrev->pLruNext = pPage->pLruNext;
441 }
442 if( pPage->pLruNext ){
443 pPage->pLruNext->pLruPrev = pPage->pLruPrev;
444 }
drh9f8cf9d2011-01-17 21:32:24 +0000445 if( pGroup->pLruHead==pPage ){
446 pGroup->pLruHead = pPage->pLruNext;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000447 }
drh9f8cf9d2011-01-17 21:32:24 +0000448 if( pGroup->pLruTail==pPage ){
449 pGroup->pLruTail = pPage->pLruPrev;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000450 }
451 pPage->pLruNext = 0;
452 pPage->pLruPrev = 0;
453 pPage->pCache->nRecyclable--;
454 }
455}
456
457
458/*
459** Remove the page supplied as an argument from the hash table
460** (PCache1.apHash structure) that it is currently stored in.
461**
drh9f8cf9d2011-01-17 21:32:24 +0000462** The PGroup mutex must be held when this function is called.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000463*/
464static void pcache1RemoveFromHash(PgHdr1 *pPage){
465 unsigned int h;
466 PCache1 *pCache = pPage->pCache;
467 PgHdr1 **pp;
468
drh9f8cf9d2011-01-17 21:32:24 +0000469 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
danielk1977bc2ca9e2008-11-13 14:28:28 +0000470 h = pPage->iKey % pCache->nHash;
471 for(pp=&pCache->apHash[h]; (*pp)!=pPage; pp=&(*pp)->pNext);
472 *pp = (*pp)->pNext;
473
474 pCache->nPage--;
475}
476
477/*
drh9f8cf9d2011-01-17 21:32:24 +0000478** If there are currently more than nMaxPage pages allocated, try
479** to recycle pages to reduce the number allocated to nMaxPage.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000480*/
drh9f8cf9d2011-01-17 21:32:24 +0000481static void pcache1EnforceMaxPage(PGroup *pGroup){
482 assert( sqlite3_mutex_held(pGroup->mutex) );
483 while( pGroup->nCurrentPage>pGroup->nMaxPage && pGroup->pLruTail ){
484 PgHdr1 *p = pGroup->pLruTail;
485 assert( p->pCache->pGroup==pGroup );
danielk1977bc2ca9e2008-11-13 14:28:28 +0000486 pcache1PinPage(p);
487 pcache1RemoveFromHash(p);
488 pcache1FreePage(p);
489 }
490}
491
492/*
493** Discard all pages from cache pCache with a page number (key value)
494** greater than or equal to iLimit. Any pinned pages that meet this
495** criteria are unpinned before they are discarded.
496**
drh9f8cf9d2011-01-17 21:32:24 +0000497** The PCache mutex must be held when this function is called.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000498*/
499static void pcache1TruncateUnsafe(
drh9f8cf9d2011-01-17 21:32:24 +0000500 PCache1 *pCache, /* The cache to truncate */
501 unsigned int iLimit /* Drop pages with this pgno or larger */
danielk1977bc2ca9e2008-11-13 14:28:28 +0000502){
drh9f8cf9d2011-01-17 21:32:24 +0000503 TESTONLY( unsigned int nPage = 0; ) /* To assert pCache->nPage is correct */
danielk1977bc2ca9e2008-11-13 14:28:28 +0000504 unsigned int h;
drh9f8cf9d2011-01-17 21:32:24 +0000505 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
danielk1977bc2ca9e2008-11-13 14:28:28 +0000506 for(h=0; h<pCache->nHash; h++){
507 PgHdr1 **pp = &pCache->apHash[h];
508 PgHdr1 *pPage;
drhb27b7f52008-12-10 18:03:45 +0000509 while( (pPage = *pp)!=0 ){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000510 if( pPage->iKey>=iLimit ){
danielk1977ea24ac42009-05-08 06:52:47 +0000511 pCache->nPage--;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000512 *pp = pPage->pNext;
danielk1977ea24ac42009-05-08 06:52:47 +0000513 pcache1PinPage(pPage);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000514 pcache1FreePage(pPage);
515 }else{
516 pp = &pPage->pNext;
danielk1977ea24ac42009-05-08 06:52:47 +0000517 TESTONLY( nPage++; )
danielk1977bc2ca9e2008-11-13 14:28:28 +0000518 }
519 }
520 }
danielk1977ea24ac42009-05-08 06:52:47 +0000521 assert( pCache->nPage==nPage );
danielk1977bc2ca9e2008-11-13 14:28:28 +0000522}
523
524/******************************************************************************/
525/******** sqlite3_pcache Methods **********************************************/
526
527/*
528** Implementation of the sqlite3_pcache.xInit method.
529*/
danielk197762c14b32008-11-19 09:05:26 +0000530static int pcache1Init(void *NotUsed){
531 UNUSED_PARAMETER(NotUsed);
drhf4622dc2009-05-22 11:10:24 +0000532 assert( pcache1.isInit==0 );
danielk1977bc2ca9e2008-11-13 14:28:28 +0000533 memset(&pcache1, 0, sizeof(pcache1));
534 if( sqlite3GlobalConfig.bCoreMutex ){
drh9f8cf9d2011-01-17 21:32:24 +0000535 pcache1.grp.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU);
drh40f98372011-01-18 15:17:57 +0000536 pcache1.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_PMEM);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000537 }
drh41692e92011-01-25 04:34:51 +0000538 pcache1.grp.mxPinned = 10;
drhf4622dc2009-05-22 11:10:24 +0000539 pcache1.isInit = 1;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000540 return SQLITE_OK;
541}
542
543/*
544** Implementation of the sqlite3_pcache.xShutdown method.
shane7c7c3112009-08-17 15:31:23 +0000545** Note that the static mutex allocated in xInit does
546** not need to be freed.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000547*/
danielk197762c14b32008-11-19 09:05:26 +0000548static void pcache1Shutdown(void *NotUsed){
549 UNUSED_PARAMETER(NotUsed);
drhf4622dc2009-05-22 11:10:24 +0000550 assert( pcache1.isInit!=0 );
drhb0937192009-05-22 10:53:29 +0000551 memset(&pcache1, 0, sizeof(pcache1));
danielk1977bc2ca9e2008-11-13 14:28:28 +0000552}
553
554/*
555** Implementation of the sqlite3_pcache.xCreate method.
556**
557** Allocate a new cache.
558*/
drhe5c40b12011-11-09 00:06:05 +0000559static sqlite3_pcache *pcache1Create(int szPage, int szExtra, int bPurgeable){
drh9f8cf9d2011-01-17 21:32:24 +0000560 PCache1 *pCache; /* The newly created page cache */
561 PGroup *pGroup; /* The group the new page cache will belong to */
562 int sz; /* Bytes of memory required to allocate the new cache */
danielk1977bc2ca9e2008-11-13 14:28:28 +0000563
drh9f8cf9d2011-01-17 21:32:24 +0000564 /*
565 ** The seperateCache variable is true if each PCache has its own private
566 ** PGroup. In other words, separateCache is true for mode (1) where no
567 ** mutexing is required.
568 **
569 ** * Always use a unified cache (mode-2) if ENABLE_MEMORY_MANAGEMENT
570 **
571 ** * Always use a unified cache in single-threaded applications
572 **
573 ** * Otherwise (if multi-threaded and ENABLE_MEMORY_MANAGEMENT is off)
574 ** use separate caches (mode-1)
575 */
576#if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) || SQLITE_THREADSAFE==0
577 const int separateCache = 0;
578#else
579 int separateCache = sqlite3GlobalConfig.bCoreMutex>0;
580#endif
581
drhe73c9142011-11-09 16:12:24 +0000582 assert( (szPage & (szPage-1))==0 && szPage>=512 && szPage<=65536 );
583 assert( szExtra < 300 );
584
drh9f8cf9d2011-01-17 21:32:24 +0000585 sz = sizeof(PCache1) + sizeof(PGroup)*separateCache;
dan6809c962012-07-30 14:53:54 +0000586 pCache = (PCache1 *)sqlite3MallocZero(sz);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000587 if( pCache ){
drh9f8cf9d2011-01-17 21:32:24 +0000588 if( separateCache ){
589 pGroup = (PGroup*)&pCache[1];
drh41692e92011-01-25 04:34:51 +0000590 pGroup->mxPinned = 10;
drh9f8cf9d2011-01-17 21:32:24 +0000591 }else{
dan9dde7cb2011-06-09 17:53:43 +0000592 pGroup = &pcache1.grp;
drh9f8cf9d2011-01-17 21:32:24 +0000593 }
594 pCache->pGroup = pGroup;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000595 pCache->szPage = szPage;
dan22e21ff2011-11-08 20:08:44 +0000596 pCache->szExtra = szExtra;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000597 pCache->bPurgeable = (bPurgeable ? 1 : 0);
598 if( bPurgeable ){
599 pCache->nMin = 10;
drh9f8cf9d2011-01-17 21:32:24 +0000600 pcache1EnterMutex(pGroup);
601 pGroup->nMinPage += pCache->nMin;
drh41692e92011-01-25 04:34:51 +0000602 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
drh9f8cf9d2011-01-17 21:32:24 +0000603 pcache1LeaveMutex(pGroup);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000604 }
605 }
606 return (sqlite3_pcache *)pCache;
607}
608
609/*
610** Implementation of the sqlite3_pcache.xCachesize method.
611**
612** Configure the cache_size limit for a cache.
613*/
614static void pcache1Cachesize(sqlite3_pcache *p, int nMax){
615 PCache1 *pCache = (PCache1 *)p;
616 if( pCache->bPurgeable ){
drh9f8cf9d2011-01-17 21:32:24 +0000617 PGroup *pGroup = pCache->pGroup;
618 pcache1EnterMutex(pGroup);
619 pGroup->nMaxPage += (nMax - pCache->nMax);
drh41692e92011-01-25 04:34:51 +0000620 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000621 pCache->nMax = nMax;
drh25ca5682011-01-26 00:07:03 +0000622 pCache->n90pct = pCache->nMax*9/10;
drh9f8cf9d2011-01-17 21:32:24 +0000623 pcache1EnforceMaxPage(pGroup);
624 pcache1LeaveMutex(pGroup);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000625 }
626}
627
628/*
drh09419b42011-11-16 19:29:17 +0000629** Implementation of the sqlite3_pcache.xShrink method.
630**
631** Free up as much memory as possible.
632*/
633static void pcache1Shrink(sqlite3_pcache *p){
634 PCache1 *pCache = (PCache1*)p;
635 if( pCache->bPurgeable ){
636 PGroup *pGroup = pCache->pGroup;
637 int savedMaxPage;
638 pcache1EnterMutex(pGroup);
639 savedMaxPage = pGroup->nMaxPage;
640 pGroup->nMaxPage = 0;
641 pcache1EnforceMaxPage(pGroup);
642 pGroup->nMaxPage = savedMaxPage;
643 pcache1LeaveMutex(pGroup);
644 }
645}
646
647/*
danielk1977bc2ca9e2008-11-13 14:28:28 +0000648** Implementation of the sqlite3_pcache.xPagecount method.
649*/
650static int pcache1Pagecount(sqlite3_pcache *p){
651 int n;
drh9f8cf9d2011-01-17 21:32:24 +0000652 PCache1 *pCache = (PCache1*)p;
653 pcache1EnterMutex(pCache->pGroup);
654 n = pCache->nPage;
655 pcache1LeaveMutex(pCache->pGroup);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000656 return n;
657}
658
659/*
660** Implementation of the sqlite3_pcache.xFetch method.
661**
662** Fetch a page by key value.
663**
664** Whether or not a new page may be allocated by this function depends on
drhf18a61d2009-07-17 11:44:07 +0000665** the value of the createFlag argument. 0 means do not allocate a new
666** page. 1 means allocate a new page if space is easily available. 2
667** means to try really hard to allocate a new page.
668**
669** For a non-purgeable cache (a cache used as the storage for an in-memory
670** database) there is really no difference between createFlag 1 and 2. So
671** the calling function (pcache.c) will never have a createFlag of 1 on
drh45d29302012-01-08 22:18:33 +0000672** a non-purgeable cache.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000673**
674** There are three different approaches to obtaining space for a page,
675** depending on the value of parameter createFlag (which may be 0, 1 or 2).
676**
677** 1. Regardless of the value of createFlag, the cache is searched for a
678** copy of the requested page. If one is found, it is returned.
679**
680** 2. If createFlag==0 and the page is not already in the cache, NULL is
681** returned.
682**
drh50d1b5f2010-08-27 12:21:06 +0000683** 3. If createFlag is 1, and the page is not already in the cache, then
684** return NULL (do not allocate a new page) if any of the following
685** conditions are true:
danielk1977bc2ca9e2008-11-13 14:28:28 +0000686**
687** (a) the number of pages pinned by the cache is greater than
688** PCache1.nMax, or
drh50d1b5f2010-08-27 12:21:06 +0000689**
danielk1977bc2ca9e2008-11-13 14:28:28 +0000690** (b) the number of pages pinned by the cache is greater than
691** the sum of nMax for all purgeable caches, less the sum of
drh50d1b5f2010-08-27 12:21:06 +0000692** nMin for all other purgeable caches, or
danielk1977bc2ca9e2008-11-13 14:28:28 +0000693**
694** 4. If none of the first three conditions apply and the cache is marked
695** as purgeable, and if one of the following is true:
696**
697** (a) The number of pages allocated for the cache is already
698** PCache1.nMax, or
699**
700** (b) The number of pages allocated for all purgeable caches is
701** already equal to or greater than the sum of nMax for all
702** purgeable caches,
703**
drh50d1b5f2010-08-27 12:21:06 +0000704** (c) The system is under memory pressure and wants to avoid
705** unnecessary pages cache entry allocations
706**
danielk1977bc2ca9e2008-11-13 14:28:28 +0000707** then attempt to recycle a page from the LRU list. If it is the right
708** size, return the recycled buffer. Otherwise, free the buffer and
709** proceed to step 5.
710**
711** 5. Otherwise, allocate and return a new page buffer.
712*/
dan22e21ff2011-11-08 20:08:44 +0000713static sqlite3_pcache_page *pcache1Fetch(
714 sqlite3_pcache *p,
715 unsigned int iKey,
716 int createFlag
717){
drha69085c2012-01-02 18:00:55 +0000718 unsigned int nPinned;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000719 PCache1 *pCache = (PCache1 *)p;
drh41692e92011-01-25 04:34:51 +0000720 PGroup *pGroup;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000721 PgHdr1 *pPage = 0;
722
drhf18a61d2009-07-17 11:44:07 +0000723 assert( pCache->bPurgeable || createFlag!=1 );
drh41692e92011-01-25 04:34:51 +0000724 assert( pCache->bPurgeable || pCache->nMin==0 );
725 assert( pCache->bPurgeable==0 || pCache->nMin==10 );
726 assert( pCache->nMin==0 || pCache->bPurgeable );
727 pcache1EnterMutex(pGroup = pCache->pGroup);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000728
drh3a5676c2011-01-19 21:58:56 +0000729 /* Step 1: Search the hash table for an existing entry. */
danielk1977bc2ca9e2008-11-13 14:28:28 +0000730 if( pCache->nHash>0 ){
731 unsigned int h = iKey % pCache->nHash;
732 for(pPage=pCache->apHash[h]; pPage&&pPage->iKey!=iKey; pPage=pPage->pNext);
733 }
734
drh3a5676c2011-01-19 21:58:56 +0000735 /* Step 2: Abort if no existing page is found and createFlag is 0 */
danielk1977bc2ca9e2008-11-13 14:28:28 +0000736 if( pPage || createFlag==0 ){
737 pcache1PinPage(pPage);
738 goto fetch_out;
739 }
740
drh41692e92011-01-25 04:34:51 +0000741 /* The pGroup local variable will normally be initialized by the
742 ** pcache1EnterMutex() macro above. But if SQLITE_MUTEX_OMIT is defined,
743 ** then pcache1EnterMutex() is a no-op, so we have to initialize the
744 ** local variable here. Delaying the initialization of pGroup is an
745 ** optimization: The common case is to exit the module before reaching
746 ** this point.
747 */
748#ifdef SQLITE_MUTEX_OMIT
749 pGroup = pCache->pGroup;
750#endif
751
drh3a5676c2011-01-19 21:58:56 +0000752 /* Step 3: Abort if createFlag is 1 but the cache is nearly full */
drha69085c2012-01-02 18:00:55 +0000753 assert( pCache->nPage >= pCache->nRecyclable );
danielk1977bc2ca9e2008-11-13 14:28:28 +0000754 nPinned = pCache->nPage - pCache->nRecyclable;
drh41692e92011-01-25 04:34:51 +0000755 assert( pGroup->mxPinned == pGroup->nMaxPage + 10 - pGroup->nMinPage );
drh25ca5682011-01-26 00:07:03 +0000756 assert( pCache->n90pct == pCache->nMax*9/10 );
drhf18a61d2009-07-17 11:44:07 +0000757 if( createFlag==1 && (
drh41692e92011-01-25 04:34:51 +0000758 nPinned>=pGroup->mxPinned
drha69085c2012-01-02 18:00:55 +0000759 || nPinned>=pCache->n90pct
drh50d1b5f2010-08-27 12:21:06 +0000760 || pcache1UnderMemoryPressure(pCache)
danielk1977bc2ca9e2008-11-13 14:28:28 +0000761 )){
762 goto fetch_out;
763 }
764
765 if( pCache->nPage>=pCache->nHash && pcache1ResizeHash(pCache) ){
766 goto fetch_out;
767 }
768
drh3a5676c2011-01-19 21:58:56 +0000769 /* Step 4. Try to recycle a page. */
drh9f8cf9d2011-01-17 21:32:24 +0000770 if( pCache->bPurgeable && pGroup->pLruTail && (
drh50d1b5f2010-08-27 12:21:06 +0000771 (pCache->nPage+1>=pCache->nMax)
drh9f8cf9d2011-01-17 21:32:24 +0000772 || pGroup->nCurrentPage>=pGroup->nMaxPage
drh50d1b5f2010-08-27 12:21:06 +0000773 || pcache1UnderMemoryPressure(pCache)
danielk1977bc2ca9e2008-11-13 14:28:28 +0000774 )){
drhe73c9142011-11-09 16:12:24 +0000775 PCache1 *pOther;
drh9f8cf9d2011-01-17 21:32:24 +0000776 pPage = pGroup->pLruTail;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000777 pcache1RemoveFromHash(pPage);
778 pcache1PinPage(pPage);
drhe73c9142011-11-09 16:12:24 +0000779 pOther = pPage->pCache;
780
781 /* We want to verify that szPage and szExtra are the same for pOther
782 ** and pCache. Assert that we can verify this by comparing sums. */
783 assert( (pCache->szPage & (pCache->szPage-1))==0 && pCache->szPage>=512 );
784 assert( pCache->szExtra<512 );
785 assert( (pOther->szPage & (pOther->szPage-1))==0 && pOther->szPage>=512 );
786 assert( pOther->szExtra<512 );
787
788 if( pOther->szPage+pOther->szExtra != pCache->szPage+pCache->szExtra ){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000789 pcache1FreePage(pPage);
790 pPage = 0;
791 }else{
drhe73c9142011-11-09 16:12:24 +0000792 pGroup->nCurrentPage -= (pOther->bPurgeable - pCache->bPurgeable);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000793 }
794 }
795
796 /* Step 5. If a usable page buffer has still not been found,
797 ** attempt to allocate a new one.
798 */
799 if( !pPage ){
drh41692e92011-01-25 04:34:51 +0000800 if( createFlag==1 ) sqlite3BeginBenignMalloc();
danielk1977bc2ca9e2008-11-13 14:28:28 +0000801 pPage = pcache1AllocPage(pCache);
drh41692e92011-01-25 04:34:51 +0000802 if( createFlag==1 ) sqlite3EndBenignMalloc();
danielk1977bc2ca9e2008-11-13 14:28:28 +0000803 }
804
805 if( pPage ){
806 unsigned int h = iKey % pCache->nHash;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000807 pCache->nPage++;
808 pPage->iKey = iKey;
809 pPage->pNext = pCache->apHash[h];
810 pPage->pCache = pCache;
danielk1977e1fd5082009-01-23 16:45:00 +0000811 pPage->pLruPrev = 0;
812 pPage->pLruNext = 0;
dan22e21ff2011-11-08 20:08:44 +0000813 *(void **)pPage->page.pExtra = 0;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000814 pCache->apHash[h] = pPage;
815 }
816
817fetch_out:
danielk1977f90b7262009-01-07 15:18:20 +0000818 if( pPage && iKey>pCache->iMaxKey ){
819 pCache->iMaxKey = iKey;
820 }
drh9f8cf9d2011-01-17 21:32:24 +0000821 pcache1LeaveMutex(pGroup);
dan22e21ff2011-11-08 20:08:44 +0000822 return &pPage->page;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000823}
824
825
826/*
827** Implementation of the sqlite3_pcache.xUnpin method.
828**
829** Mark a page as unpinned (eligible for asynchronous recycling).
830*/
dan22e21ff2011-11-08 20:08:44 +0000831static void pcache1Unpin(
832 sqlite3_pcache *p,
833 sqlite3_pcache_page *pPg,
834 int reuseUnlikely
835){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000836 PCache1 *pCache = (PCache1 *)p;
dan22e21ff2011-11-08 20:08:44 +0000837 PgHdr1 *pPage = (PgHdr1 *)pPg;
drh9f8cf9d2011-01-17 21:32:24 +0000838 PGroup *pGroup = pCache->pGroup;
drh69e931e2009-06-03 21:04:35 +0000839
840 assert( pPage->pCache==pCache );
drh9f8cf9d2011-01-17 21:32:24 +0000841 pcache1EnterMutex(pGroup);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000842
843 /* It is an error to call this function if the page is already
drh9f8cf9d2011-01-17 21:32:24 +0000844 ** part of the PGroup LRU list.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000845 */
846 assert( pPage->pLruPrev==0 && pPage->pLruNext==0 );
drh9f8cf9d2011-01-17 21:32:24 +0000847 assert( pGroup->pLruHead!=pPage && pGroup->pLruTail!=pPage );
danielk1977bc2ca9e2008-11-13 14:28:28 +0000848
drh9f8cf9d2011-01-17 21:32:24 +0000849 if( reuseUnlikely || pGroup->nCurrentPage>pGroup->nMaxPage ){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000850 pcache1RemoveFromHash(pPage);
851 pcache1FreePage(pPage);
852 }else{
drh9f8cf9d2011-01-17 21:32:24 +0000853 /* Add the page to the PGroup LRU list. */
854 if( pGroup->pLruHead ){
855 pGroup->pLruHead->pLruPrev = pPage;
856 pPage->pLruNext = pGroup->pLruHead;
857 pGroup->pLruHead = pPage;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000858 }else{
drh9f8cf9d2011-01-17 21:32:24 +0000859 pGroup->pLruTail = pPage;
860 pGroup->pLruHead = pPage;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000861 }
862 pCache->nRecyclable++;
863 }
864
drh9f8cf9d2011-01-17 21:32:24 +0000865 pcache1LeaveMutex(pCache->pGroup);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000866}
867
868/*
869** Implementation of the sqlite3_pcache.xRekey method.
870*/
871static void pcache1Rekey(
872 sqlite3_pcache *p,
dan22e21ff2011-11-08 20:08:44 +0000873 sqlite3_pcache_page *pPg,
danielk1977bc2ca9e2008-11-13 14:28:28 +0000874 unsigned int iOld,
875 unsigned int iNew
876){
877 PCache1 *pCache = (PCache1 *)p;
dan22e21ff2011-11-08 20:08:44 +0000878 PgHdr1 *pPage = (PgHdr1 *)pPg;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000879 PgHdr1 **pp;
880 unsigned int h;
881 assert( pPage->iKey==iOld );
drh69e931e2009-06-03 21:04:35 +0000882 assert( pPage->pCache==pCache );
danielk1977bc2ca9e2008-11-13 14:28:28 +0000883
drh9f8cf9d2011-01-17 21:32:24 +0000884 pcache1EnterMutex(pCache->pGroup);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000885
886 h = iOld%pCache->nHash;
887 pp = &pCache->apHash[h];
888 while( (*pp)!=pPage ){
889 pp = &(*pp)->pNext;
890 }
891 *pp = pPage->pNext;
892
893 h = iNew%pCache->nHash;
894 pPage->iKey = iNew;
895 pPage->pNext = pCache->apHash[h];
896 pCache->apHash[h] = pPage;
drh98829a62009-11-20 13:18:14 +0000897 if( iNew>pCache->iMaxKey ){
danielk1977f90b7262009-01-07 15:18:20 +0000898 pCache->iMaxKey = iNew;
899 }
900
drh9f8cf9d2011-01-17 21:32:24 +0000901 pcache1LeaveMutex(pCache->pGroup);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000902}
903
904/*
905** Implementation of the sqlite3_pcache.xTruncate method.
906**
907** Discard all unpinned pages in the cache with a page number equal to
908** or greater than parameter iLimit. Any pinned pages with a page number
909** equal to or greater than iLimit are implicitly unpinned.
910*/
911static void pcache1Truncate(sqlite3_pcache *p, unsigned int iLimit){
912 PCache1 *pCache = (PCache1 *)p;
drh9f8cf9d2011-01-17 21:32:24 +0000913 pcache1EnterMutex(pCache->pGroup);
danielk1977f90b7262009-01-07 15:18:20 +0000914 if( iLimit<=pCache->iMaxKey ){
915 pcache1TruncateUnsafe(pCache, iLimit);
916 pCache->iMaxKey = iLimit-1;
917 }
drh9f8cf9d2011-01-17 21:32:24 +0000918 pcache1LeaveMutex(pCache->pGroup);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000919}
920
921/*
922** Implementation of the sqlite3_pcache.xDestroy method.
923**
924** Destroy a cache allocated using pcache1Create().
925*/
926static void pcache1Destroy(sqlite3_pcache *p){
927 PCache1 *pCache = (PCache1 *)p;
drh9f8cf9d2011-01-17 21:32:24 +0000928 PGroup *pGroup = pCache->pGroup;
danb51d2fa2010-09-22 19:06:02 +0000929 assert( pCache->bPurgeable || (pCache->nMax==0 && pCache->nMin==0) );
drh9f8cf9d2011-01-17 21:32:24 +0000930 pcache1EnterMutex(pGroup);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000931 pcache1TruncateUnsafe(pCache, 0);
drha69085c2012-01-02 18:00:55 +0000932 assert( pGroup->nMaxPage >= pCache->nMax );
drh9f8cf9d2011-01-17 21:32:24 +0000933 pGroup->nMaxPage -= pCache->nMax;
drha69085c2012-01-02 18:00:55 +0000934 assert( pGroup->nMinPage >= pCache->nMin );
drh9f8cf9d2011-01-17 21:32:24 +0000935 pGroup->nMinPage -= pCache->nMin;
drh41692e92011-01-25 04:34:51 +0000936 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
drh9f8cf9d2011-01-17 21:32:24 +0000937 pcache1EnforceMaxPage(pGroup);
938 pcache1LeaveMutex(pGroup);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000939 sqlite3_free(pCache->apHash);
940 sqlite3_free(pCache);
941}
942
943/*
944** This function is called during initialization (sqlite3_initialize()) to
945** install the default pluggable cache module, assuming the user has not
946** already provided an alternative.
947*/
948void sqlite3PCacheSetDefault(void){
dan22e21ff2011-11-08 20:08:44 +0000949 static const sqlite3_pcache_methods2 defaultMethods = {
drh81ef0f92011-11-13 21:44:03 +0000950 1, /* iVersion */
danielk1977bc2ca9e2008-11-13 14:28:28 +0000951 0, /* pArg */
952 pcache1Init, /* xInit */
953 pcache1Shutdown, /* xShutdown */
954 pcache1Create, /* xCreate */
955 pcache1Cachesize, /* xCachesize */
956 pcache1Pagecount, /* xPagecount */
957 pcache1Fetch, /* xFetch */
958 pcache1Unpin, /* xUnpin */
959 pcache1Rekey, /* xRekey */
960 pcache1Truncate, /* xTruncate */
drh09419b42011-11-16 19:29:17 +0000961 pcache1Destroy, /* xDestroy */
962 pcache1Shrink /* xShrink */
danielk1977bc2ca9e2008-11-13 14:28:28 +0000963 };
dan22e21ff2011-11-08 20:08:44 +0000964 sqlite3_config(SQLITE_CONFIG_PCACHE2, &defaultMethods);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000965}
966
967#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
968/*
969** This function is called to free superfluous dynamically allocated memory
970** held by the pager system. Memory in use by any SQLite pager allocated
971** by the current thread may be sqlite3_free()ed.
972**
973** nReq is the number of bytes of memory required. Once this much has
974** been released, the function returns. The return value is the total number
975** of bytes of memory released.
976*/
977int sqlite3PcacheReleaseMemory(int nReq){
978 int nFree = 0;
drh9f8cf9d2011-01-17 21:32:24 +0000979 assert( sqlite3_mutex_notheld(pcache1.grp.mutex) );
980 assert( sqlite3_mutex_notheld(pcache1.mutex) );
danielk1977bc2ca9e2008-11-13 14:28:28 +0000981 if( pcache1.pStart==0 ){
982 PgHdr1 *p;
drh9f8cf9d2011-01-17 21:32:24 +0000983 pcache1EnterMutex(&pcache1.grp);
984 while( (nReq<0 || nFree<nReq) && ((p=pcache1.grp.pLruTail)!=0) ){
dan22e21ff2011-11-08 20:08:44 +0000985 nFree += pcache1MemSize(p->page.pBuf);
986#ifdef SQLITE_PCACHE_SEPARATE_HEADER
987 nFree += sqlite3MemSize(p);
988#endif
danielk1977bc2ca9e2008-11-13 14:28:28 +0000989 pcache1PinPage(p);
990 pcache1RemoveFromHash(p);
991 pcache1FreePage(p);
992 }
drh9f8cf9d2011-01-17 21:32:24 +0000993 pcache1LeaveMutex(&pcache1.grp);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000994 }
995 return nFree;
996}
997#endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
998
999#ifdef SQLITE_TEST
1000/*
1001** This function is used by test procedures to inspect the internal state
1002** of the global cache.
1003*/
1004void sqlite3PcacheStats(
1005 int *pnCurrent, /* OUT: Total number of pages cached */
1006 int *pnMax, /* OUT: Global maximum cache size */
1007 int *pnMin, /* OUT: Sum of PCache1.nMin for purgeable caches */
1008 int *pnRecyclable /* OUT: Total number of pages available for recycling */
1009){
1010 PgHdr1 *p;
1011 int nRecyclable = 0;
drh9f8cf9d2011-01-17 21:32:24 +00001012 for(p=pcache1.grp.pLruHead; p; p=p->pLruNext){
danielk1977bc2ca9e2008-11-13 14:28:28 +00001013 nRecyclable++;
1014 }
drh9f8cf9d2011-01-17 21:32:24 +00001015 *pnCurrent = pcache1.grp.nCurrentPage;
drha69085c2012-01-02 18:00:55 +00001016 *pnMax = (int)pcache1.grp.nMaxPage;
1017 *pnMin = (int)pcache1.grp.nMinPage;
danielk1977bc2ca9e2008-11-13 14:28:28 +00001018 *pnRecyclable = nRecyclable;
1019}
1020#endif