blob: a8755a31415cb74207a1a80fbc0d1a7045451923 [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.
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
peter.d.reid60ec9142014-09-06 16:39:46 +000028** of one or more PCaches that are able to recycle each other's unpinned
drh9f8cf9d2011-01-17 21:32:24 +000029** 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) */
drh5d56dd22013-12-13 18:50:40 +000099 u8 isPinned; /* Page in use, not on the LRU list */
danielk1977bc2ca9e2008-11-13 14:28:28 +0000100 PgHdr1 *pNext; /* Next in hash table chain */
101 PCache1 *pCache; /* Cache that currently owns this page */
102 PgHdr1 *pLruNext; /* Next in LRU list of unpinned pages */
103 PgHdr1 *pLruPrev; /* Previous in LRU list of unpinned pages */
104};
105
106/*
107** Free slots in the allocator used to divide up the buffer provided using
108** the SQLITE_CONFIG_PAGECACHE mechanism.
109*/
110struct PgFreeslot {
111 PgFreeslot *pNext; /* Next free slot */
112};
113
114/*
115** Global data used by this cache.
116*/
117static SQLITE_WSD struct PCacheGlobal {
drh9f8cf9d2011-01-17 21:32:24 +0000118 PGroup grp; /* The global PGroup for mode (2) */
danielk1977bc2ca9e2008-11-13 14:28:28 +0000119
drh9f8cf9d2011-01-17 21:32:24 +0000120 /* Variables related to SQLITE_CONFIG_PAGECACHE settings. The
121 ** szSlot, nSlot, pStart, pEnd, nReserve, and isInit values are all
122 ** fixed at sqlite3_initialize() time and do not require mutex protection.
123 ** The nFreeSlot and pFree values do require mutex protection.
124 */
125 int isInit; /* True if initialized */
126 int szSlot; /* Size of each free slot */
127 int nSlot; /* The number of pcache slots */
128 int nReserve; /* Try to keep nFreeSlot above this */
129 void *pStart, *pEnd; /* Bounds of pagecache malloc range */
130 /* Above requires no mutex. Use mutex below for variable that follow. */
131 sqlite3_mutex *mutex; /* Mutex for accessing the following: */
drh9f8cf9d2011-01-17 21:32:24 +0000132 PgFreeslot *pFree; /* Free page blocks */
drh2cbd78b2012-02-02 19:37:18 +0000133 int nFreeSlot; /* Number of unused pcache slots */
drh9f8cf9d2011-01-17 21:32:24 +0000134 /* The following value requires a mutex to change. We skip the mutex on
135 ** reading because (1) most platforms read a 32-bit integer atomically and
136 ** (2) even if an incorrect value is read, no great harm is done since this
137 ** is really just an optimization. */
138 int bUnderPressure; /* True if low on PAGECACHE memory */
danielk197744cd45c2008-11-15 11:22:45 +0000139} pcache1_g;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000140
141/*
142** All code in this file should access the global structure above via the
143** alias "pcache1". This ensures that the WSD emulation is used when
144** compiling for systems that do not support real WSD.
145*/
146#define pcache1 (GLOBAL(struct PCacheGlobal, pcache1_g))
147
148/*
drh9f8cf9d2011-01-17 21:32:24 +0000149** Macros to enter and leave the PCache LRU mutex.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000150*/
drh9f8cf9d2011-01-17 21:32:24 +0000151#define pcache1EnterMutex(X) sqlite3_mutex_enter((X)->mutex)
152#define pcache1LeaveMutex(X) sqlite3_mutex_leave((X)->mutex)
danielk1977bc2ca9e2008-11-13 14:28:28 +0000153
154/******************************************************************************/
155/******** Page Allocation/SQLITE_CONFIG_PCACHE Related Functions **************/
156
157/*
158** This function is called during initialization if a static buffer is
159** supplied to use for the page-cache by passing the SQLITE_CONFIG_PAGECACHE
160** verb to sqlite3_config(). Parameter pBuf points to an allocation large
161** enough to contain 'n' buffers of 'sz' bytes each.
drh9f8cf9d2011-01-17 21:32:24 +0000162**
163** This routine is called from sqlite3_initialize() and so it is guaranteed
164** to be serialized already. There is no need for further mutexing.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000165*/
166void sqlite3PCacheBufferSetup(void *pBuf, int sz, int n){
drhf4622dc2009-05-22 11:10:24 +0000167 if( pcache1.isInit ){
168 PgFreeslot *p;
169 sz = ROUNDDOWN8(sz);
170 pcache1.szSlot = sz;
drh50d1b5f2010-08-27 12:21:06 +0000171 pcache1.nSlot = pcache1.nFreeSlot = n;
172 pcache1.nReserve = n>90 ? 10 : (n/10 + 1);
drhf4622dc2009-05-22 11:10:24 +0000173 pcache1.pStart = pBuf;
174 pcache1.pFree = 0;
drh9f8cf9d2011-01-17 21:32:24 +0000175 pcache1.bUnderPressure = 0;
drhf4622dc2009-05-22 11:10:24 +0000176 while( n-- ){
177 p = (PgFreeslot*)pBuf;
178 p->pNext = pcache1.pFree;
179 pcache1.pFree = p;
180 pBuf = (void*)&((char*)pBuf)[sz];
181 }
182 pcache1.pEnd = pBuf;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000183 }
danielk1977bc2ca9e2008-11-13 14:28:28 +0000184}
185
186/*
187** Malloc function used within this file to allocate space from the buffer
188** configured using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no
189** such buffer exists or there is no space left in it, this function falls
190** back to sqlite3Malloc().
drh9f8cf9d2011-01-17 21:32:24 +0000191**
192** Multiple threads can run this routine at the same time. Global variables
193** in pcache1 need to be protected via mutex.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000194*/
195static void *pcache1Alloc(int nByte){
drh9f8cf9d2011-01-17 21:32:24 +0000196 void *p = 0;
197 assert( sqlite3_mutex_notheld(pcache1.grp.mutex) );
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 );
drhaf89fe62015-03-23 17:25:18 +0000206 sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, nByte);
207 sqlite3StatusUp(SQLITE_STATUS_PAGECACHE_USED, 1);
drh9f8cf9d2011-01-17 21:32:24 +0000208 }
209 sqlite3_mutex_leave(pcache1.mutex);
210 }
211 if( p==0 ){
212 /* Memory is not available in the SQLITE_CONFIG_PAGECACHE pool. Get
213 ** it from sqlite3Malloc instead.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000214 */
danielk1977bc2ca9e2008-11-13 14:28:28 +0000215 p = sqlite3Malloc(nByte);
drh4bd69522012-06-07 02:35:29 +0000216#ifndef SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS
danielk1977bc2ca9e2008-11-13 14:28:28 +0000217 if( p ){
218 int sz = sqlite3MallocSize(p);
drh9bf3da8e2011-01-26 13:24:40 +0000219 sqlite3_mutex_enter(pcache1.mutex);
drhaf89fe62015-03-23 17:25:18 +0000220 sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, nByte);
221 sqlite3StatusUp(SQLITE_STATUS_PAGECACHE_OVERFLOW, sz);
drh9bf3da8e2011-01-26 13:24:40 +0000222 sqlite3_mutex_leave(pcache1.mutex);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000223 }
drh4bd69522012-06-07 02:35:29 +0000224#endif
drh107b56e2010-03-12 16:32:53 +0000225 sqlite3MemdebugSetType(p, MEMTYPE_PCACHE);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000226 }
227 return p;
228}
229
230/*
231** Free an allocated buffer obtained from pcache1Alloc().
232*/
drh09419b42011-11-16 19:29:17 +0000233static int pcache1Free(void *p){
234 int nFreed = 0;
235 if( p==0 ) return 0;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000236 if( p>=pcache1.pStart && p<pcache1.pEnd ){
237 PgFreeslot *pSlot;
drh9f8cf9d2011-01-17 21:32:24 +0000238 sqlite3_mutex_enter(pcache1.mutex);
drhaf89fe62015-03-23 17:25:18 +0000239 sqlite3StatusDown(SQLITE_STATUS_PAGECACHE_USED, 1);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000240 pSlot = (PgFreeslot*)p;
241 pSlot->pNext = pcache1.pFree;
242 pcache1.pFree = pSlot;
drh50d1b5f2010-08-27 12:21:06 +0000243 pcache1.nFreeSlot++;
drh9f8cf9d2011-01-17 21:32:24 +0000244 pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve;
drh50d1b5f2010-08-27 12:21:06 +0000245 assert( pcache1.nFreeSlot<=pcache1.nSlot );
drh9f8cf9d2011-01-17 21:32:24 +0000246 sqlite3_mutex_leave(pcache1.mutex);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000247 }else{
drh107b56e2010-03-12 16:32:53 +0000248 assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) );
249 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
drh09419b42011-11-16 19:29:17 +0000250 nFreed = sqlite3MallocSize(p);
drh4bd69522012-06-07 02:35:29 +0000251#ifndef SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS
drh15ad92f2011-01-26 13:28:06 +0000252 sqlite3_mutex_enter(pcache1.mutex);
drhaf89fe62015-03-23 17:25:18 +0000253 sqlite3StatusDown(SQLITE_STATUS_PAGECACHE_OVERFLOW, nFreed);
drh15ad92f2011-01-26 13:28:06 +0000254 sqlite3_mutex_leave(pcache1.mutex);
drh4bd69522012-06-07 02:35:29 +0000255#endif
danielk1977bc2ca9e2008-11-13 14:28:28 +0000256 sqlite3_free(p);
257 }
drh09419b42011-11-16 19:29:17 +0000258 return nFreed;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000259}
260
drhc8f503a2010-08-20 09:14:13 +0000261#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
262/*
drh9d13f112010-08-24 18:06:35 +0000263** Return the size of a pcache allocation
drhc8f503a2010-08-20 09:14:13 +0000264*/
265static int pcache1MemSize(void *p){
drhc8f503a2010-08-20 09:14:13 +0000266 if( p>=pcache1.pStart && p<pcache1.pEnd ){
267 return pcache1.szSlot;
268 }else{
269 int iSize;
270 assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) );
271 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
272 iSize = sqlite3MallocSize(p);
273 sqlite3MemdebugSetType(p, MEMTYPE_PCACHE);
274 return iSize;
275 }
276}
277#endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
278
dand2925702011-08-19 18:15:00 +0000279/*
danielk1977bc2ca9e2008-11-13 14:28:28 +0000280** Allocate a new page object initially associated with cache pCache.
281*/
282static PgHdr1 *pcache1AllocPage(PCache1 *pCache){
danb5126dd2011-09-22 14:56:31 +0000283 PgHdr1 *p = 0;
284 void *pPg;
dand2925702011-08-19 18:15:00 +0000285
dand2925702011-08-19 18:15:00 +0000286 /* The group mutex must be released before pcache1Alloc() is called. This
287 ** is because it may call sqlite3_release_memory(), which assumes that
288 ** this mutex is not held. */
289 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
290 pcache1LeaveMutex(pCache->pGroup);
dan22e21ff2011-11-08 20:08:44 +0000291#ifdef SQLITE_PCACHE_SEPARATE_HEADER
292 pPg = pcache1Alloc(pCache->szPage);
293 p = sqlite3Malloc(sizeof(PgHdr1) + pCache->szExtra);
294 if( !pPg || !p ){
295 pcache1Free(pPg);
296 sqlite3_free(p);
297 pPg = 0;
298 }
299#else
drh51dc84e2014-12-30 13:04:25 +0000300 pPg = pcache1Alloc(ROUND8(sizeof(PgHdr1)) + pCache->szPage + pCache->szExtra);
dan22e21ff2011-11-08 20:08:44 +0000301 p = (PgHdr1 *)&((u8 *)pPg)[pCache->szPage];
302#endif
dand2925702011-08-19 18:15:00 +0000303 pcache1EnterMutex(pCache->pGroup);
danb5126dd2011-09-22 14:56:31 +0000304
drh69e931e2009-06-03 21:04:35 +0000305 if( pPg ){
dan22e21ff2011-11-08 20:08:44 +0000306 p->page.pBuf = pPg;
307 p->page.pExtra = &p[1];
danielk1977bc2ca9e2008-11-13 14:28:28 +0000308 if( pCache->bPurgeable ){
drh9f8cf9d2011-01-17 21:32:24 +0000309 pCache->pGroup->nCurrentPage++;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000310 }
dan22e21ff2011-11-08 20:08:44 +0000311 return p;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000312 }
dan22e21ff2011-11-08 20:08:44 +0000313 return 0;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000314}
315
316/*
317** Free a page object allocated by pcache1AllocPage().
drhf18a61d2009-07-17 11:44:07 +0000318**
319** The pointer is allowed to be NULL, which is prudent. But it turns out
320** that the current implementation happens to never call this routine
321** with a NULL pointer, so we mark the NULL test with ALWAYS().
danielk1977bc2ca9e2008-11-13 14:28:28 +0000322*/
323static void pcache1FreePage(PgHdr1 *p){
drhf18a61d2009-07-17 11:44:07 +0000324 if( ALWAYS(p) ){
drh9f8cf9d2011-01-17 21:32:24 +0000325 PCache1 *pCache = p->pCache;
dand2925702011-08-19 18:15:00 +0000326 assert( sqlite3_mutex_held(p->pCache->pGroup->mutex) );
dan22e21ff2011-11-08 20:08:44 +0000327 pcache1Free(p->page.pBuf);
328#ifdef SQLITE_PCACHE_SEPARATE_HEADER
329 sqlite3_free(p);
330#endif
drh9f8cf9d2011-01-17 21:32:24 +0000331 if( pCache->bPurgeable ){
332 pCache->pGroup->nCurrentPage--;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000333 }
danielk1977bc2ca9e2008-11-13 14:28:28 +0000334 }
335}
336
337/*
338** Malloc function used by SQLite to obtain space from the buffer configured
339** using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no such buffer
340** exists, this function falls back to sqlite3Malloc().
341*/
342void *sqlite3PageMalloc(int sz){
drh9f8cf9d2011-01-17 21:32:24 +0000343 return pcache1Alloc(sz);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000344}
345
346/*
347** Free an allocated buffer obtained from sqlite3PageMalloc().
348*/
349void sqlite3PageFree(void *p){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000350 pcache1Free(p);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000351}
352
drh50d1b5f2010-08-27 12:21:06 +0000353
354/*
355** Return true if it desirable to avoid allocating a new page cache
356** entry.
357**
358** If memory was allocated specifically to the page cache using
359** SQLITE_CONFIG_PAGECACHE but that memory has all been used, then
360** it is desirable to avoid allocating a new page cache entry because
361** presumably SQLITE_CONFIG_PAGECACHE was suppose to be sufficient
362** for all page cache needs and we should not need to spill the
363** allocation onto the heap.
364**
drh45d29302012-01-08 22:18:33 +0000365** Or, the heap is used for all page cache memory but the heap is
drh50d1b5f2010-08-27 12:21:06 +0000366** under memory pressure, then again it is desirable to avoid
367** allocating a new page cache entry in order to avoid stressing
368** the heap even further.
369*/
370static int pcache1UnderMemoryPressure(PCache1 *pCache){
dan22e21ff2011-11-08 20:08:44 +0000371 if( pcache1.nSlot && (pCache->szPage+pCache->szExtra)<=pcache1.szSlot ){
drh9f8cf9d2011-01-17 21:32:24 +0000372 return pcache1.bUnderPressure;
drh50d1b5f2010-08-27 12:21:06 +0000373 }else{
374 return sqlite3HeapNearlyFull();
375 }
376}
377
danielk1977bc2ca9e2008-11-13 14:28:28 +0000378/******************************************************************************/
379/******** General Implementation Functions ************************************/
380
381/*
382** This function is used to resize the hash table used by the cache passed
383** as the first argument.
384**
drh9f8cf9d2011-01-17 21:32:24 +0000385** The PCache mutex must be held when this function is called.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000386*/
drhefbf0442014-08-23 23:15:31 +0000387static void pcache1ResizeHash(PCache1 *p){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000388 PgHdr1 **apNew;
danielk197744cd45c2008-11-15 11:22:45 +0000389 unsigned int nNew;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000390 unsigned int i;
391
drh9f8cf9d2011-01-17 21:32:24 +0000392 assert( sqlite3_mutex_held(p->pGroup->mutex) );
danielk1977bc2ca9e2008-11-13 14:28:28 +0000393
394 nNew = p->nHash*2;
395 if( nNew<256 ){
396 nNew = 256;
397 }
398
drh9f8cf9d2011-01-17 21:32:24 +0000399 pcache1LeaveMutex(p->pGroup);
drh085bb7f2008-12-06 14:34:33 +0000400 if( p->nHash ){ sqlite3BeginBenignMalloc(); }
dan6809c962012-07-30 14:53:54 +0000401 apNew = (PgHdr1 **)sqlite3MallocZero(sizeof(PgHdr1 *)*nNew);
drh085bb7f2008-12-06 14:34:33 +0000402 if( p->nHash ){ sqlite3EndBenignMalloc(); }
drh9f8cf9d2011-01-17 21:32:24 +0000403 pcache1EnterMutex(p->pGroup);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000404 if( apNew ){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000405 for(i=0; i<p->nHash; i++){
406 PgHdr1 *pPage;
407 PgHdr1 *pNext = p->apHash[i];
drhb27b7f52008-12-10 18:03:45 +0000408 while( (pPage = pNext)!=0 ){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000409 unsigned int h = pPage->iKey % nNew;
410 pNext = pPage->pNext;
411 pPage->pNext = apNew[h];
412 apNew[h] = pPage;
413 }
414 }
415 sqlite3_free(p->apHash);
416 p->apHash = apNew;
417 p->nHash = nNew;
418 }
danielk1977bc2ca9e2008-11-13 14:28:28 +0000419}
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.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000427*/
428static void pcache1PinPage(PgHdr1 *pPage){
drh9f8cf9d2011-01-17 21:32:24 +0000429 PCache1 *pCache;
430 PGroup *pGroup;
431
drh5d56dd22013-12-13 18:50:40 +0000432 assert( pPage!=0 );
433 assert( pPage->isPinned==0 );
drh9f8cf9d2011-01-17 21:32:24 +0000434 pCache = pPage->pCache;
435 pGroup = pCache->pGroup;
drh5d56dd22013-12-13 18:50:40 +0000436 assert( pPage->pLruNext || pPage==pGroup->pLruTail );
437 assert( pPage->pLruPrev || pPage==pGroup->pLruHead );
drh9f8cf9d2011-01-17 21:32:24 +0000438 assert( sqlite3_mutex_held(pGroup->mutex) );
drh5d56dd22013-12-13 18:50:40 +0000439 if( pPage->pLruPrev ){
440 pPage->pLruPrev->pLruNext = pPage->pLruNext;
441 }else{
442 pGroup->pLruHead = pPage->pLruNext;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000443 }
drh5d56dd22013-12-13 18:50:40 +0000444 if( pPage->pLruNext ){
445 pPage->pLruNext->pLruPrev = pPage->pLruPrev;
446 }else{
447 pGroup->pLruTail = pPage->pLruPrev;
448 }
449 pPage->pLruNext = 0;
450 pPage->pLruPrev = 0;
451 pPage->isPinned = 1;
452 pCache->nRecyclable--;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000453}
454
455
456/*
457** Remove the page supplied as an argument from the hash table
458** (PCache1.apHash structure) that it is currently stored in.
459**
drh9f8cf9d2011-01-17 21:32:24 +0000460** The PGroup mutex must be held when this function is called.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000461*/
462static void pcache1RemoveFromHash(PgHdr1 *pPage){
463 unsigned int h;
464 PCache1 *pCache = pPage->pCache;
465 PgHdr1 **pp;
466
drh9f8cf9d2011-01-17 21:32:24 +0000467 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
danielk1977bc2ca9e2008-11-13 14:28:28 +0000468 h = pPage->iKey % pCache->nHash;
469 for(pp=&pCache->apHash[h]; (*pp)!=pPage; pp=&(*pp)->pNext);
470 *pp = (*pp)->pNext;
471
472 pCache->nPage--;
473}
474
475/*
drh9f8cf9d2011-01-17 21:32:24 +0000476** If there are currently more than nMaxPage pages allocated, try
477** to recycle pages to reduce the number allocated to nMaxPage.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000478*/
drh9f8cf9d2011-01-17 21:32:24 +0000479static void pcache1EnforceMaxPage(PGroup *pGroup){
480 assert( sqlite3_mutex_held(pGroup->mutex) );
481 while( pGroup->nCurrentPage>pGroup->nMaxPage && pGroup->pLruTail ){
482 PgHdr1 *p = pGroup->pLruTail;
483 assert( p->pCache->pGroup==pGroup );
drh5d56dd22013-12-13 18:50:40 +0000484 assert( p->isPinned==0 );
danielk1977bc2ca9e2008-11-13 14:28:28 +0000485 pcache1PinPage(p);
486 pcache1RemoveFromHash(p);
487 pcache1FreePage(p);
488 }
489}
490
491/*
492** Discard all pages from cache pCache with a page number (key value)
493** greater than or equal to iLimit. Any pinned pages that meet this
494** criteria are unpinned before they are discarded.
495**
drh9f8cf9d2011-01-17 21:32:24 +0000496** The PCache mutex must be held when this function is called.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000497*/
498static void pcache1TruncateUnsafe(
drh9f8cf9d2011-01-17 21:32:24 +0000499 PCache1 *pCache, /* The cache to truncate */
500 unsigned int iLimit /* Drop pages with this pgno or larger */
danielk1977bc2ca9e2008-11-13 14:28:28 +0000501){
drh9f8cf9d2011-01-17 21:32:24 +0000502 TESTONLY( unsigned int nPage = 0; ) /* To assert pCache->nPage is correct */
danielk1977bc2ca9e2008-11-13 14:28:28 +0000503 unsigned int h;
drh9f8cf9d2011-01-17 21:32:24 +0000504 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
danielk1977bc2ca9e2008-11-13 14:28:28 +0000505 for(h=0; h<pCache->nHash; h++){
506 PgHdr1 **pp = &pCache->apHash[h];
507 PgHdr1 *pPage;
drhb27b7f52008-12-10 18:03:45 +0000508 while( (pPage = *pp)!=0 ){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000509 if( pPage->iKey>=iLimit ){
danielk1977ea24ac42009-05-08 06:52:47 +0000510 pCache->nPage--;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000511 *pp = pPage->pNext;
drh5d56dd22013-12-13 18:50:40 +0000512 if( !pPage->isPinned ) pcache1PinPage(pPage);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000513 pcache1FreePage(pPage);
514 }else{
515 pp = &pPage->pNext;
danielk1977ea24ac42009-05-08 06:52:47 +0000516 TESTONLY( nPage++; )
danielk1977bc2ca9e2008-11-13 14:28:28 +0000517 }
518 }
519 }
danielk1977ea24ac42009-05-08 06:52:47 +0000520 assert( pCache->nPage==nPage );
danielk1977bc2ca9e2008-11-13 14:28:28 +0000521}
522
523/******************************************************************************/
524/******** sqlite3_pcache Methods **********************************************/
525
526/*
527** Implementation of the sqlite3_pcache.xInit method.
528*/
danielk197762c14b32008-11-19 09:05:26 +0000529static int pcache1Init(void *NotUsed){
530 UNUSED_PARAMETER(NotUsed);
drhf4622dc2009-05-22 11:10:24 +0000531 assert( pcache1.isInit==0 );
danielk1977bc2ca9e2008-11-13 14:28:28 +0000532 memset(&pcache1, 0, sizeof(pcache1));
533 if( sqlite3GlobalConfig.bCoreMutex ){
drh9f8cf9d2011-01-17 21:32:24 +0000534 pcache1.grp.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU);
drh40f98372011-01-18 15:17:57 +0000535 pcache1.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_PMEM);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000536 }
drh41692e92011-01-25 04:34:51 +0000537 pcache1.grp.mxPinned = 10;
drhf4622dc2009-05-22 11:10:24 +0000538 pcache1.isInit = 1;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000539 return SQLITE_OK;
540}
541
542/*
543** Implementation of the sqlite3_pcache.xShutdown method.
shane7c7c3112009-08-17 15:31:23 +0000544** Note that the static mutex allocated in xInit does
545** not need to be freed.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000546*/
danielk197762c14b32008-11-19 09:05:26 +0000547static void pcache1Shutdown(void *NotUsed){
548 UNUSED_PARAMETER(NotUsed);
drhf4622dc2009-05-22 11:10:24 +0000549 assert( pcache1.isInit!=0 );
drhb0937192009-05-22 10:53:29 +0000550 memset(&pcache1, 0, sizeof(pcache1));
danielk1977bc2ca9e2008-11-13 14:28:28 +0000551}
552
drhefbf0442014-08-23 23:15:31 +0000553/* forward declaration */
554static void pcache1Destroy(sqlite3_pcache *p);
555
danielk1977bc2ca9e2008-11-13 14:28:28 +0000556/*
557** Implementation of the sqlite3_pcache.xCreate method.
558**
559** Allocate a new cache.
560*/
drhe5c40b12011-11-09 00:06:05 +0000561static sqlite3_pcache *pcache1Create(int szPage, int szExtra, int bPurgeable){
drh9f8cf9d2011-01-17 21:32:24 +0000562 PCache1 *pCache; /* The newly created page cache */
563 PGroup *pGroup; /* The group the new page cache will belong to */
564 int sz; /* Bytes of memory required to allocate the new cache */
danielk1977bc2ca9e2008-11-13 14:28:28 +0000565
drh9f8cf9d2011-01-17 21:32:24 +0000566 /*
drhf7b54962013-05-28 12:11:54 +0000567 ** The separateCache variable is true if each PCache has its own private
drh9f8cf9d2011-01-17 21:32:24 +0000568 ** PGroup. In other words, separateCache is true for mode (1) where no
569 ** mutexing is required.
570 **
571 ** * Always use a unified cache (mode-2) if ENABLE_MEMORY_MANAGEMENT
572 **
573 ** * Always use a unified cache in single-threaded applications
574 **
575 ** * Otherwise (if multi-threaded and ENABLE_MEMORY_MANAGEMENT is off)
576 ** use separate caches (mode-1)
577 */
578#if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) || SQLITE_THREADSAFE==0
579 const int separateCache = 0;
580#else
581 int separateCache = sqlite3GlobalConfig.bCoreMutex>0;
582#endif
583
drhe73c9142011-11-09 16:12:24 +0000584 assert( (szPage & (szPage-1))==0 && szPage>=512 && szPage<=65536 );
585 assert( szExtra < 300 );
586
drh9f8cf9d2011-01-17 21:32:24 +0000587 sz = sizeof(PCache1) + sizeof(PGroup)*separateCache;
dan6809c962012-07-30 14:53:54 +0000588 pCache = (PCache1 *)sqlite3MallocZero(sz);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000589 if( pCache ){
drh9f8cf9d2011-01-17 21:32:24 +0000590 if( separateCache ){
591 pGroup = (PGroup*)&pCache[1];
drh41692e92011-01-25 04:34:51 +0000592 pGroup->mxPinned = 10;
drh9f8cf9d2011-01-17 21:32:24 +0000593 }else{
dan9dde7cb2011-06-09 17:53:43 +0000594 pGroup = &pcache1.grp;
drh9f8cf9d2011-01-17 21:32:24 +0000595 }
596 pCache->pGroup = pGroup;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000597 pCache->szPage = szPage;
dan22e21ff2011-11-08 20:08:44 +0000598 pCache->szExtra = szExtra;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000599 pCache->bPurgeable = (bPurgeable ? 1 : 0);
drhefbf0442014-08-23 23:15:31 +0000600 pcache1EnterMutex(pGroup);
601 pcache1ResizeHash(pCache);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000602 if( bPurgeable ){
603 pCache->nMin = 10;
drh9f8cf9d2011-01-17 21:32:24 +0000604 pGroup->nMinPage += pCache->nMin;
drh41692e92011-01-25 04:34:51 +0000605 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
drhefbf0442014-08-23 23:15:31 +0000606 }
607 pcache1LeaveMutex(pGroup);
608 if( pCache->nHash==0 ){
609 pcache1Destroy((sqlite3_pcache*)pCache);
610 pCache = 0;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000611 }
612 }
613 return (sqlite3_pcache *)pCache;
614}
615
616/*
617** Implementation of the sqlite3_pcache.xCachesize method.
618**
619** Configure the cache_size limit for a cache.
620*/
621static void pcache1Cachesize(sqlite3_pcache *p, int nMax){
622 PCache1 *pCache = (PCache1 *)p;
623 if( pCache->bPurgeable ){
drh9f8cf9d2011-01-17 21:32:24 +0000624 PGroup *pGroup = pCache->pGroup;
625 pcache1EnterMutex(pGroup);
626 pGroup->nMaxPage += (nMax - pCache->nMax);
drh41692e92011-01-25 04:34:51 +0000627 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000628 pCache->nMax = nMax;
drh25ca5682011-01-26 00:07:03 +0000629 pCache->n90pct = pCache->nMax*9/10;
drh9f8cf9d2011-01-17 21:32:24 +0000630 pcache1EnforceMaxPage(pGroup);
631 pcache1LeaveMutex(pGroup);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000632 }
633}
634
635/*
drh09419b42011-11-16 19:29:17 +0000636** Implementation of the sqlite3_pcache.xShrink method.
637**
638** Free up as much memory as possible.
639*/
640static void pcache1Shrink(sqlite3_pcache *p){
641 PCache1 *pCache = (PCache1*)p;
642 if( pCache->bPurgeable ){
643 PGroup *pGroup = pCache->pGroup;
644 int savedMaxPage;
645 pcache1EnterMutex(pGroup);
646 savedMaxPage = pGroup->nMaxPage;
647 pGroup->nMaxPage = 0;
648 pcache1EnforceMaxPage(pGroup);
649 pGroup->nMaxPage = savedMaxPage;
650 pcache1LeaveMutex(pGroup);
651 }
652}
653
654/*
danielk1977bc2ca9e2008-11-13 14:28:28 +0000655** Implementation of the sqlite3_pcache.xPagecount method.
656*/
657static int pcache1Pagecount(sqlite3_pcache *p){
658 int n;
drh9f8cf9d2011-01-17 21:32:24 +0000659 PCache1 *pCache = (PCache1*)p;
660 pcache1EnterMutex(pCache->pGroup);
661 n = pCache->nPage;
662 pcache1LeaveMutex(pCache->pGroup);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000663 return n;
664}
665
drhefbf0442014-08-23 23:15:31 +0000666
667/*
668** Implement steps 3, 4, and 5 of the pcache1Fetch() algorithm described
669** in the header of the pcache1Fetch() procedure.
670**
671** This steps are broken out into a separate procedure because they are
672** usually not needed, and by avoiding the stack initialization required
673** for these steps, the main pcache1Fetch() procedure can run faster.
674*/
675static SQLITE_NOINLINE PgHdr1 *pcache1FetchStage2(
676 PCache1 *pCache,
677 unsigned int iKey,
678 int createFlag
679){
680 unsigned int nPinned;
681 PGroup *pGroup = pCache->pGroup;
682 PgHdr1 *pPage = 0;
683
684 /* Step 3: Abort if createFlag is 1 but the cache is nearly full */
685 assert( pCache->nPage >= pCache->nRecyclable );
686 nPinned = pCache->nPage - pCache->nRecyclable;
687 assert( pGroup->mxPinned == pGroup->nMaxPage + 10 - pGroup->nMinPage );
688 assert( pCache->n90pct == pCache->nMax*9/10 );
689 if( createFlag==1 && (
690 nPinned>=pGroup->mxPinned
691 || nPinned>=pCache->n90pct
dan5bd8af72014-10-10 19:10:59 +0000692 || (pcache1UnderMemoryPressure(pCache) && pCache->nRecyclable<nPinned)
drhefbf0442014-08-23 23:15:31 +0000693 )){
694 return 0;
695 }
696
697 if( pCache->nPage>=pCache->nHash ) pcache1ResizeHash(pCache);
698 assert( pCache->nHash>0 && pCache->apHash );
699
700 /* Step 4. Try to recycle a page. */
701 if( pCache->bPurgeable && pGroup->pLruTail && (
702 (pCache->nPage+1>=pCache->nMax)
703 || pGroup->nCurrentPage>=pGroup->nMaxPage
704 || pcache1UnderMemoryPressure(pCache)
705 )){
706 PCache1 *pOther;
707 pPage = pGroup->pLruTail;
708 assert( pPage->isPinned==0 );
709 pcache1RemoveFromHash(pPage);
710 pcache1PinPage(pPage);
711 pOther = pPage->pCache;
712
713 /* We want to verify that szPage and szExtra are the same for pOther
714 ** and pCache. Assert that we can verify this by comparing sums. */
715 assert( (pCache->szPage & (pCache->szPage-1))==0 && pCache->szPage>=512 );
716 assert( pCache->szExtra<512 );
717 assert( (pOther->szPage & (pOther->szPage-1))==0 && pOther->szPage>=512 );
718 assert( pOther->szExtra<512 );
719
720 if( pOther->szPage+pOther->szExtra != pCache->szPage+pCache->szExtra ){
721 pcache1FreePage(pPage);
722 pPage = 0;
723 }else{
724 pGroup->nCurrentPage -= (pOther->bPurgeable - pCache->bPurgeable);
725 }
726 }
727
728 /* Step 5. If a usable page buffer has still not been found,
729 ** attempt to allocate a new one.
730 */
731 if( !pPage ){
732 if( createFlag==1 ) sqlite3BeginBenignMalloc();
733 pPage = pcache1AllocPage(pCache);
734 if( createFlag==1 ) sqlite3EndBenignMalloc();
735 }
736
737 if( pPage ){
738 unsigned int h = iKey % pCache->nHash;
739 pCache->nPage++;
740 pPage->iKey = iKey;
741 pPage->pNext = pCache->apHash[h];
742 pPage->pCache = pCache;
743 pPage->pLruPrev = 0;
744 pPage->pLruNext = 0;
745 pPage->isPinned = 1;
746 *(void **)pPage->page.pExtra = 0;
747 pCache->apHash[h] = pPage;
748 if( iKey>pCache->iMaxKey ){
749 pCache->iMaxKey = iKey;
750 }
751 }
752 return pPage;
753}
754
danielk1977bc2ca9e2008-11-13 14:28:28 +0000755/*
756** Implementation of the sqlite3_pcache.xFetch method.
757**
758** Fetch a page by key value.
759**
760** Whether or not a new page may be allocated by this function depends on
drhf18a61d2009-07-17 11:44:07 +0000761** the value of the createFlag argument. 0 means do not allocate a new
762** page. 1 means allocate a new page if space is easily available. 2
763** means to try really hard to allocate a new page.
764**
765** For a non-purgeable cache (a cache used as the storage for an in-memory
766** database) there is really no difference between createFlag 1 and 2. So
767** the calling function (pcache.c) will never have a createFlag of 1 on
drh45d29302012-01-08 22:18:33 +0000768** a non-purgeable cache.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000769**
770** There are three different approaches to obtaining space for a page,
771** depending on the value of parameter createFlag (which may be 0, 1 or 2).
772**
773** 1. Regardless of the value of createFlag, the cache is searched for a
774** copy of the requested page. If one is found, it is returned.
775**
776** 2. If createFlag==0 and the page is not already in the cache, NULL is
777** returned.
778**
drh50d1b5f2010-08-27 12:21:06 +0000779** 3. If createFlag is 1, and the page is not already in the cache, then
780** return NULL (do not allocate a new page) if any of the following
781** conditions are true:
danielk1977bc2ca9e2008-11-13 14:28:28 +0000782**
783** (a) the number of pages pinned by the cache is greater than
784** PCache1.nMax, or
drh50d1b5f2010-08-27 12:21:06 +0000785**
danielk1977bc2ca9e2008-11-13 14:28:28 +0000786** (b) the number of pages pinned by the cache is greater than
787** the sum of nMax for all purgeable caches, less the sum of
drh50d1b5f2010-08-27 12:21:06 +0000788** nMin for all other purgeable caches, or
danielk1977bc2ca9e2008-11-13 14:28:28 +0000789**
790** 4. If none of the first three conditions apply and the cache is marked
791** as purgeable, and if one of the following is true:
792**
793** (a) The number of pages allocated for the cache is already
794** PCache1.nMax, or
795**
796** (b) The number of pages allocated for all purgeable caches is
797** already equal to or greater than the sum of nMax for all
798** purgeable caches,
799**
drh50d1b5f2010-08-27 12:21:06 +0000800** (c) The system is under memory pressure and wants to avoid
801** unnecessary pages cache entry allocations
802**
danielk1977bc2ca9e2008-11-13 14:28:28 +0000803** then attempt to recycle a page from the LRU list. If it is the right
804** size, return the recycled buffer. Otherwise, free the buffer and
805** proceed to step 5.
806**
807** 5. Otherwise, allocate and return a new page buffer.
808*/
dan22e21ff2011-11-08 20:08:44 +0000809static sqlite3_pcache_page *pcache1Fetch(
810 sqlite3_pcache *p,
811 unsigned int iKey,
812 int createFlag
813){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000814 PCache1 *pCache = (PCache1 *)p;
815 PgHdr1 *pPage = 0;
816
drh693e6712014-01-24 22:58:00 +0000817 assert( offsetof(PgHdr1,page)==0 );
drhf18a61d2009-07-17 11:44:07 +0000818 assert( pCache->bPurgeable || createFlag!=1 );
drh41692e92011-01-25 04:34:51 +0000819 assert( pCache->bPurgeable || pCache->nMin==0 );
820 assert( pCache->bPurgeable==0 || pCache->nMin==10 );
821 assert( pCache->nMin==0 || pCache->bPurgeable );
drhefbf0442014-08-23 23:15:31 +0000822 assert( pCache->nHash>0 );
823 pcache1EnterMutex(pCache->pGroup);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000824
drh3a5676c2011-01-19 21:58:56 +0000825 /* Step 1: Search the hash table for an existing entry. */
drhefbf0442014-08-23 23:15:31 +0000826 pPage = pCache->apHash[iKey % pCache->nHash];
827 while( pPage && pPage->iKey!=iKey ){ pPage = pPage->pNext; }
danielk1977bc2ca9e2008-11-13 14:28:28 +0000828
drh3a5676c2011-01-19 21:58:56 +0000829 /* Step 2: Abort if no existing page is found and createFlag is 0 */
drh5d56dd22013-12-13 18:50:40 +0000830 if( pPage ){
831 if( !pPage->isPinned ) pcache1PinPage(pPage);
drhefbf0442014-08-23 23:15:31 +0000832 }else if( createFlag ){
833 /* Steps 3, 4, and 5 implemented by this subroutine */
834 pPage = pcache1FetchStage2(pCache, iKey, createFlag);
drh5d56dd22013-12-13 18:50:40 +0000835 }
drhefbf0442014-08-23 23:15:31 +0000836 assert( pPage==0 || pCache->iMaxKey>=iKey );
837 pcache1LeaveMutex(pCache->pGroup);
drh693e6712014-01-24 22:58:00 +0000838 return (sqlite3_pcache_page*)pPage;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000839}
840
841
842/*
843** Implementation of the sqlite3_pcache.xUnpin method.
844**
845** Mark a page as unpinned (eligible for asynchronous recycling).
846*/
dan22e21ff2011-11-08 20:08:44 +0000847static void pcache1Unpin(
848 sqlite3_pcache *p,
849 sqlite3_pcache_page *pPg,
850 int reuseUnlikely
851){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000852 PCache1 *pCache = (PCache1 *)p;
dan22e21ff2011-11-08 20:08:44 +0000853 PgHdr1 *pPage = (PgHdr1 *)pPg;
drh9f8cf9d2011-01-17 21:32:24 +0000854 PGroup *pGroup = pCache->pGroup;
drh69e931e2009-06-03 21:04:35 +0000855
856 assert( pPage->pCache==pCache );
drh9f8cf9d2011-01-17 21:32:24 +0000857 pcache1EnterMutex(pGroup);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000858
859 /* It is an error to call this function if the page is already
drh9f8cf9d2011-01-17 21:32:24 +0000860 ** part of the PGroup LRU list.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000861 */
862 assert( pPage->pLruPrev==0 && pPage->pLruNext==0 );
drh9f8cf9d2011-01-17 21:32:24 +0000863 assert( pGroup->pLruHead!=pPage && pGroup->pLruTail!=pPage );
drh5d56dd22013-12-13 18:50:40 +0000864 assert( pPage->isPinned==1 );
danielk1977bc2ca9e2008-11-13 14:28:28 +0000865
drh9f8cf9d2011-01-17 21:32:24 +0000866 if( reuseUnlikely || pGroup->nCurrentPage>pGroup->nMaxPage ){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000867 pcache1RemoveFromHash(pPage);
868 pcache1FreePage(pPage);
869 }else{
drh9f8cf9d2011-01-17 21:32:24 +0000870 /* Add the page to the PGroup LRU list. */
871 if( pGroup->pLruHead ){
872 pGroup->pLruHead->pLruPrev = pPage;
873 pPage->pLruNext = pGroup->pLruHead;
874 pGroup->pLruHead = pPage;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000875 }else{
drh9f8cf9d2011-01-17 21:32:24 +0000876 pGroup->pLruTail = pPage;
877 pGroup->pLruHead = pPage;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000878 }
879 pCache->nRecyclable++;
drh5d56dd22013-12-13 18:50:40 +0000880 pPage->isPinned = 0;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000881 }
882
drh9f8cf9d2011-01-17 21:32:24 +0000883 pcache1LeaveMutex(pCache->pGroup);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000884}
885
886/*
887** Implementation of the sqlite3_pcache.xRekey method.
888*/
889static void pcache1Rekey(
890 sqlite3_pcache *p,
dan22e21ff2011-11-08 20:08:44 +0000891 sqlite3_pcache_page *pPg,
danielk1977bc2ca9e2008-11-13 14:28:28 +0000892 unsigned int iOld,
893 unsigned int iNew
894){
895 PCache1 *pCache = (PCache1 *)p;
dan22e21ff2011-11-08 20:08:44 +0000896 PgHdr1 *pPage = (PgHdr1 *)pPg;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000897 PgHdr1 **pp;
898 unsigned int h;
899 assert( pPage->iKey==iOld );
drh69e931e2009-06-03 21:04:35 +0000900 assert( pPage->pCache==pCache );
danielk1977bc2ca9e2008-11-13 14:28:28 +0000901
drh9f8cf9d2011-01-17 21:32:24 +0000902 pcache1EnterMutex(pCache->pGroup);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000903
904 h = iOld%pCache->nHash;
905 pp = &pCache->apHash[h];
906 while( (*pp)!=pPage ){
907 pp = &(*pp)->pNext;
908 }
909 *pp = pPage->pNext;
910
911 h = iNew%pCache->nHash;
912 pPage->iKey = iNew;
913 pPage->pNext = pCache->apHash[h];
914 pCache->apHash[h] = pPage;
drh98829a62009-11-20 13:18:14 +0000915 if( iNew>pCache->iMaxKey ){
danielk1977f90b7262009-01-07 15:18:20 +0000916 pCache->iMaxKey = iNew;
917 }
918
drh9f8cf9d2011-01-17 21:32:24 +0000919 pcache1LeaveMutex(pCache->pGroup);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000920}
921
922/*
923** Implementation of the sqlite3_pcache.xTruncate method.
924**
925** Discard all unpinned pages in the cache with a page number equal to
926** or greater than parameter iLimit. Any pinned pages with a page number
927** equal to or greater than iLimit are implicitly unpinned.
928*/
929static void pcache1Truncate(sqlite3_pcache *p, unsigned int iLimit){
930 PCache1 *pCache = (PCache1 *)p;
drh9f8cf9d2011-01-17 21:32:24 +0000931 pcache1EnterMutex(pCache->pGroup);
danielk1977f90b7262009-01-07 15:18:20 +0000932 if( iLimit<=pCache->iMaxKey ){
933 pcache1TruncateUnsafe(pCache, iLimit);
934 pCache->iMaxKey = iLimit-1;
935 }
drh9f8cf9d2011-01-17 21:32:24 +0000936 pcache1LeaveMutex(pCache->pGroup);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000937}
938
939/*
940** Implementation of the sqlite3_pcache.xDestroy method.
941**
942** Destroy a cache allocated using pcache1Create().
943*/
944static void pcache1Destroy(sqlite3_pcache *p){
945 PCache1 *pCache = (PCache1 *)p;
drh9f8cf9d2011-01-17 21:32:24 +0000946 PGroup *pGroup = pCache->pGroup;
danb51d2fa2010-09-22 19:06:02 +0000947 assert( pCache->bPurgeable || (pCache->nMax==0 && pCache->nMin==0) );
drh9f8cf9d2011-01-17 21:32:24 +0000948 pcache1EnterMutex(pGroup);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000949 pcache1TruncateUnsafe(pCache, 0);
drha69085c2012-01-02 18:00:55 +0000950 assert( pGroup->nMaxPage >= pCache->nMax );
drh9f8cf9d2011-01-17 21:32:24 +0000951 pGroup->nMaxPage -= pCache->nMax;
drha69085c2012-01-02 18:00:55 +0000952 assert( pGroup->nMinPage >= pCache->nMin );
drh9f8cf9d2011-01-17 21:32:24 +0000953 pGroup->nMinPage -= pCache->nMin;
drh41692e92011-01-25 04:34:51 +0000954 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
drh9f8cf9d2011-01-17 21:32:24 +0000955 pcache1EnforceMaxPage(pGroup);
956 pcache1LeaveMutex(pGroup);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000957 sqlite3_free(pCache->apHash);
958 sqlite3_free(pCache);
959}
960
961/*
962** This function is called during initialization (sqlite3_initialize()) to
963** install the default pluggable cache module, assuming the user has not
964** already provided an alternative.
965*/
966void sqlite3PCacheSetDefault(void){
dan22e21ff2011-11-08 20:08:44 +0000967 static const sqlite3_pcache_methods2 defaultMethods = {
drh81ef0f92011-11-13 21:44:03 +0000968 1, /* iVersion */
danielk1977bc2ca9e2008-11-13 14:28:28 +0000969 0, /* pArg */
970 pcache1Init, /* xInit */
971 pcache1Shutdown, /* xShutdown */
972 pcache1Create, /* xCreate */
973 pcache1Cachesize, /* xCachesize */
974 pcache1Pagecount, /* xPagecount */
975 pcache1Fetch, /* xFetch */
976 pcache1Unpin, /* xUnpin */
977 pcache1Rekey, /* xRekey */
978 pcache1Truncate, /* xTruncate */
drh09419b42011-11-16 19:29:17 +0000979 pcache1Destroy, /* xDestroy */
980 pcache1Shrink /* xShrink */
danielk1977bc2ca9e2008-11-13 14:28:28 +0000981 };
dan22e21ff2011-11-08 20:08:44 +0000982 sqlite3_config(SQLITE_CONFIG_PCACHE2, &defaultMethods);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000983}
984
drhdef68892014-11-04 12:11:23 +0000985/*
986** Return the size of the header on each page of this PCACHE implementation.
987*/
drh37c057b2014-12-30 00:57:29 +0000988int sqlite3HeaderSizePcache1(void){ return ROUND8(sizeof(PgHdr1)); }
drhdef68892014-11-04 12:11:23 +0000989
drhaf89fe62015-03-23 17:25:18 +0000990/*
991** Return the global mutex used by this PCACHE implementation. The
992** sqlite3_status() routine needs access to this mutex.
993*/
994sqlite3_mutex *sqlite3Pcache1Mutex(void){
995 return pcache1.mutex;
996}
997
danielk1977bc2ca9e2008-11-13 14:28:28 +0000998#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
999/*
1000** This function is called to free superfluous dynamically allocated memory
1001** held by the pager system. Memory in use by any SQLite pager allocated
1002** by the current thread may be sqlite3_free()ed.
1003**
1004** nReq is the number of bytes of memory required. Once this much has
1005** been released, the function returns. The return value is the total number
1006** of bytes of memory released.
1007*/
1008int sqlite3PcacheReleaseMemory(int nReq){
1009 int nFree = 0;
drh9f8cf9d2011-01-17 21:32:24 +00001010 assert( sqlite3_mutex_notheld(pcache1.grp.mutex) );
1011 assert( sqlite3_mutex_notheld(pcache1.mutex) );
danielk1977bc2ca9e2008-11-13 14:28:28 +00001012 if( pcache1.pStart==0 ){
1013 PgHdr1 *p;
drh9f8cf9d2011-01-17 21:32:24 +00001014 pcache1EnterMutex(&pcache1.grp);
1015 while( (nReq<0 || nFree<nReq) && ((p=pcache1.grp.pLruTail)!=0) ){
dan22e21ff2011-11-08 20:08:44 +00001016 nFree += pcache1MemSize(p->page.pBuf);
1017#ifdef SQLITE_PCACHE_SEPARATE_HEADER
1018 nFree += sqlite3MemSize(p);
1019#endif
drh5d56dd22013-12-13 18:50:40 +00001020 assert( p->isPinned==0 );
danielk1977bc2ca9e2008-11-13 14:28:28 +00001021 pcache1PinPage(p);
1022 pcache1RemoveFromHash(p);
1023 pcache1FreePage(p);
1024 }
drh9f8cf9d2011-01-17 21:32:24 +00001025 pcache1LeaveMutex(&pcache1.grp);
danielk1977bc2ca9e2008-11-13 14:28:28 +00001026 }
1027 return nFree;
1028}
1029#endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
1030
1031#ifdef SQLITE_TEST
1032/*
1033** This function is used by test procedures to inspect the internal state
1034** of the global cache.
1035*/
1036void sqlite3PcacheStats(
1037 int *pnCurrent, /* OUT: Total number of pages cached */
1038 int *pnMax, /* OUT: Global maximum cache size */
1039 int *pnMin, /* OUT: Sum of PCache1.nMin for purgeable caches */
1040 int *pnRecyclable /* OUT: Total number of pages available for recycling */
1041){
1042 PgHdr1 *p;
1043 int nRecyclable = 0;
drh9f8cf9d2011-01-17 21:32:24 +00001044 for(p=pcache1.grp.pLruHead; p; p=p->pLruNext){
drh5d56dd22013-12-13 18:50:40 +00001045 assert( p->isPinned==0 );
danielk1977bc2ca9e2008-11-13 14:28:28 +00001046 nRecyclable++;
1047 }
drh9f8cf9d2011-01-17 21:32:24 +00001048 *pnCurrent = pcache1.grp.nCurrentPage;
drha69085c2012-01-02 18:00:55 +00001049 *pnMax = (int)pcache1.grp.nMaxPage;
1050 *pnMin = (int)pcache1.grp.nMinPage;
danielk1977bc2ca9e2008-11-13 14:28:28 +00001051 *pnRecyclable = nRecyclable;
1052}
1053#endif