blob: 5f502d57ba71c275d092d56716083a2dbd583ff1 [file] [log] [blame]
danielk1977bc2ca9e2008-11-13 14:28:28 +00001/*
2** 2008 November 05
3**
4** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
6**
7** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
10**
11*************************************************************************
12**
13** This file implements the default page cache implementation (the
14** sqlite3_pcache interface). It also contains part of the implementation
15** of the SQLITE_CONFIG_PAGECACHE and sqlite3_release_memory() features.
peter.d.reid60ec9142014-09-06 16:39:46 +000016** If the default page cache implementation is overridden, then neither of
danielk1977bc2ca9e2008-11-13 14:28:28 +000017** these two features are available.
drh01c5c002015-07-04 18:15:04 +000018**
19** A Page cache line looks like this:
20**
21** -------------------------------------------------------------
22** | database page content | PgHdr1 | MemPage | PgHdr |
23** -------------------------------------------------------------
24**
25** The database page content is up front (so that buffer overreads tend to
26** flow harmlessly into the PgHdr1, MemPage, and PgHdr extensions). MemPage
27** is the extension added by the btree.c module containing information such
28** as the database page number and how that database page is used. PgHdr
29** is added by the pcache.c layer and contains information used to keep track
30** of which pages are "dirty". PgHdr1 is an extension added by this
31** module (pcache1.c). The PgHdr1 header is a subclass of sqlite3_pcache_page.
32** PgHdr1 contains information needed to look up a page by its page number.
33** The superclass sqlite3_pcache_page.pBuf points to the start of the
34** database page content and sqlite3_pcache_page.pExtra points to PgHdr.
35**
36** The size of the extension (MemPage+PgHdr+PgHdr1) can be determined at
37** runtime using sqlite3_config(SQLITE_CONFIG_PCACHE_HDRSZ, &size). The
38** sizes of the extensions sum to 272 bytes on x64 for 3.8.10, but this
39** size can vary according to architecture, compile-time options, and
40** SQLite library version number.
41**
42** If SQLITE_PCACHE_SEPARATE_HEADER is defined, then the extension is obtained
43** using a separate memory allocation from the database page content. This
drh7b341e62015-07-08 14:13:44 +000044** seeks to overcome the "clownshoe" problem (also called "internal
45** fragmentation" in academic literature) of allocating a few bytes more
drh01c5c002015-07-04 18:15:04 +000046** than a power of two with the memory allocator rounding up to the next
47** power of two, and leaving the rounded-up space unused.
48**
49** This module tracks pointers to PgHdr1 objects. Only pcache.c communicates
50** with this module. Information is passed back and forth as PgHdr1 pointers.
51**
52** The pcache.c and pager.c modules deal pointers to PgHdr objects.
53** The btree.c module deals with pointers to MemPage objects.
drhee70a842015-07-06 18:54:52 +000054**
55** SOURCE OF PAGE CACHE MEMORY:
56**
57** Memory for a page might come from any of three sources:
58**
59** (1) The general-purpose memory allocator - sqlite3Malloc()
60** (2) Global page-cache memory provided using sqlite3_config() with
61** SQLITE_CONFIG_PAGECACHE.
62** (3) PCache-local bulk allocation.
63**
64** The third case is a chunk of heap memory (defaulting to 100 pages worth)
65** that is allocated when the page cache is created. The size of the local
66** bulk allocation can be adjusted using
67**
drha6082f62015-11-26 22:12:41 +000068** sqlite3_config(SQLITE_CONFIG_PAGECACHE, (void*)0, 0, N).
drhee70a842015-07-06 18:54:52 +000069**
70** If N is positive, then N pages worth of memory are allocated using a single
71** sqlite3Malloc() call and that memory is used for the first N pages allocated.
72** Or if N is negative, then -1024*N bytes of memory are allocated and used
73** for as many pages as can be accomodated.
74**
75** Only one of (2) or (3) can be used. Once the memory available to (2) or
76** (3) is exhausted, subsequent allocations fail over to the general-purpose
77** memory allocator (1).
78**
79** Earlier versions of SQLite used only methods (1) and (2). But experiments
80** show that method (3) with N==100 provides about a 5% performance boost for
81** common workloads.
danielk1977bc2ca9e2008-11-13 14:28:28 +000082*/
danielk1977bc2ca9e2008-11-13 14:28:28 +000083#include "sqliteInt.h"
84
85typedef struct PCache1 PCache1;
86typedef struct PgHdr1 PgHdr1;
87typedef struct PgFreeslot PgFreeslot;
drh9f8cf9d2011-01-17 21:32:24 +000088typedef struct PGroup PGroup;
89
drh92af02c2015-09-04 04:31:56 +000090/*
91** Each cache entry is represented by an instance of the following
92** structure. Unless SQLITE_PCACHE_SEPARATE_HEADER is defined, a buffer of
93** PgHdr1.pCache->szPage bytes is allocated directly before this structure
94** in memory.
dane385d332019-02-06 20:49:49 +000095**
96** Note: Variables isBulkLocal and isAnchor were once type "u8". That works,
97** but causes a 2-byte gap in the structure for most architectures (since
98** pointers must be either 4 or 8-byte aligned). As this structure is located
99** in memory directly after the associated page data, if the database is
100** corrupt, code at the b-tree layer may overread the page buffer and
101** read part of this structure before the corruption is detected. This
102** can cause a valgrind error if the unitialized gap is accessed. Using u16
103** ensures there is no such gap, and therefore no bytes of unitialized memory
104** in the structure.
drh92af02c2015-09-04 04:31:56 +0000105*/
106struct PgHdr1 {
107 sqlite3_pcache_page page; /* Base class. Must be first. pBuf & pExtra */
108 unsigned int iKey; /* Key value (page number) */
dane385d332019-02-06 20:49:49 +0000109 u16 isBulkLocal; /* This page from bulk local storage */
110 u16 isAnchor; /* This is the PGroup.lru element */
drh92af02c2015-09-04 04:31:56 +0000111 PgHdr1 *pNext; /* Next in hash table chain */
112 PCache1 *pCache; /* Cache that currently owns this page */
113 PgHdr1 *pLruNext; /* Next in LRU list of unpinned pages */
114 PgHdr1 *pLruPrev; /* Previous in LRU list of unpinned pages */
drhde72d2a2018-12-03 01:58:02 +0000115 /* NB: pLruPrev is only valid if pLruNext!=0 */
drh92af02c2015-09-04 04:31:56 +0000116};
117
drheabb67f2017-08-05 15:49:03 +0000118/*
drh26505e52018-11-28 11:09:09 +0000119** A page is pinned if it is not on the LRU list. To be "pinned" means
120** that the page is in active use and must not be deallocated.
drheabb67f2017-08-05 15:49:03 +0000121*/
122#define PAGE_IS_PINNED(p) ((p)->pLruNext==0)
123#define PAGE_IS_UNPINNED(p) ((p)->pLruNext!=0)
124
drh9f8cf9d2011-01-17 21:32:24 +0000125/* Each page cache (or PCache) belongs to a PGroup. A PGroup is a set
peter.d.reid60ec9142014-09-06 16:39:46 +0000126** of one or more PCaches that are able to recycle each other's unpinned
drh9f8cf9d2011-01-17 21:32:24 +0000127** pages when they are under memory pressure. A PGroup is an instance of
128** the following object.
129**
130** This page cache implementation works in one of two modes:
131**
132** (1) Every PCache is the sole member of its own PGroup. There is
133** one PGroup per PCache.
134**
135** (2) There is a single global PGroup that all PCaches are a member
136** of.
137**
138** Mode 1 uses more memory (since PCache instances are not able to rob
139** unused pages from other PCaches) but it also operates without a mutex,
140** and is therefore often faster. Mode 2 requires a mutex in order to be
drh45d29302012-01-08 22:18:33 +0000141** threadsafe, but recycles pages more efficiently.
drh9f8cf9d2011-01-17 21:32:24 +0000142**
143** For mode (1), PGroup.mutex is NULL. For mode (2) there is only a single
144** PGroup which is the pcache1.grp global variable and its mutex is
145** SQLITE_MUTEX_STATIC_LRU.
146*/
147struct PGroup {
148 sqlite3_mutex *mutex; /* MUTEX_STATIC_LRU or NULL */
drha69085c2012-01-02 18:00:55 +0000149 unsigned int nMaxPage; /* Sum of nMax for purgeable caches */
150 unsigned int nMinPage; /* Sum of nMin for purgeable caches */
151 unsigned int mxPinned; /* nMaxpage + 10 - nMinPage */
drh617b7b42017-08-30 04:44:59 +0000152 unsigned int nPurgeable; /* Number of purgeable pages allocated */
drh92af02c2015-09-04 04:31:56 +0000153 PgHdr1 lru; /* The beginning and end of the LRU list */
drh9f8cf9d2011-01-17 21:32:24 +0000154};
danielk1977bc2ca9e2008-11-13 14:28:28 +0000155
drh9d13f112010-08-24 18:06:35 +0000156/* Each page cache is an instance of the following object. Every
157** open database file (including each in-memory database and each
158** temporary or transient database) has a single page cache which
159** is an instance of this object.
160**
161** Pointers to structures of this type are cast and returned as
162** opaque sqlite3_pcache* handles.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000163*/
164struct PCache1 {
165 /* Cache configuration parameters. Page size (szPage) and the purgeable
drh617b7b42017-08-30 04:44:59 +0000166 ** flag (bPurgeable) and the pnPurgeable pointer are all set when the
167 ** cache is created and are never changed thereafter. nMax may be
drh45d29302012-01-08 22:18:33 +0000168 ** modified at any time by a call to the pcache1Cachesize() method.
drh9f8cf9d2011-01-17 21:32:24 +0000169 ** The PGroup mutex must be held when accessing nMax.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000170 */
drh9f8cf9d2011-01-17 21:32:24 +0000171 PGroup *pGroup; /* PGroup this cache belongs to */
drh617b7b42017-08-30 04:44:59 +0000172 unsigned int *pnPurgeable; /* Pointer to pGroup->nPurgeable */
drhee70a842015-07-06 18:54:52 +0000173 int szPage; /* Size of database content section */
174 int szExtra; /* sizeof(MemPage)+sizeof(PgHdr) */
175 int szAlloc; /* Total size of one pcache line */
danielk1977bc2ca9e2008-11-13 14:28:28 +0000176 int bPurgeable; /* True if cache is purgeable */
danielk197744cd45c2008-11-15 11:22:45 +0000177 unsigned int nMin; /* Minimum number of pages reserved */
178 unsigned int nMax; /* Configured "cache_size" value */
drh25ca5682011-01-26 00:07:03 +0000179 unsigned int n90pct; /* nMax*9/10 */
drh2cbd78b2012-02-02 19:37:18 +0000180 unsigned int iMaxKey; /* Largest key seen since xTruncate() */
drh1757fed2019-01-09 14:49:58 +0000181 unsigned int nPurgeableDummy; /* pnPurgeable points here when not used*/
danielk1977bc2ca9e2008-11-13 14:28:28 +0000182
183 /* Hash table of all pages. The following variables may only be accessed
drh9f8cf9d2011-01-17 21:32:24 +0000184 ** when the accessor is holding the PGroup mutex.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000185 */
danielk197744cd45c2008-11-15 11:22:45 +0000186 unsigned int nRecyclable; /* Number of pages in the LRU list */
187 unsigned int nPage; /* Total number of pages in apHash */
188 unsigned int nHash; /* Number of slots in apHash[] */
danielk1977bc2ca9e2008-11-13 14:28:28 +0000189 PgHdr1 **apHash; /* Hash table for fast lookup by key */
drhee70a842015-07-06 18:54:52 +0000190 PgHdr1 *pFree; /* List of unused pcache-local pages */
191 void *pBulk; /* Bulk memory used by pcache-local */
danielk1977bc2ca9e2008-11-13 14:28:28 +0000192};
193
194/*
drhee70a842015-07-06 18:54:52 +0000195** Free slots in the allocator used to divide up the global page cache
196** buffer provided using the SQLITE_CONFIG_PAGECACHE mechanism.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000197*/
198struct PgFreeslot {
199 PgFreeslot *pNext; /* Next free slot */
200};
201
202/*
203** Global data used by this cache.
204*/
205static SQLITE_WSD struct PCacheGlobal {
drh9f8cf9d2011-01-17 21:32:24 +0000206 PGroup grp; /* The global PGroup for mode (2) */
danielk1977bc2ca9e2008-11-13 14:28:28 +0000207
drh9f8cf9d2011-01-17 21:32:24 +0000208 /* Variables related to SQLITE_CONFIG_PAGECACHE settings. The
209 ** szSlot, nSlot, pStart, pEnd, nReserve, and isInit values are all
210 ** fixed at sqlite3_initialize() time and do not require mutex protection.
211 ** The nFreeSlot and pFree values do require mutex protection.
212 */
213 int isInit; /* True if initialized */
drhdb7ae892015-07-06 20:57:22 +0000214 int separateCache; /* Use a new PGroup for each PCache */
drh957026a2015-07-16 18:18:19 +0000215 int nInitPage; /* Initial bulk allocation size */
drh9f8cf9d2011-01-17 21:32:24 +0000216 int szSlot; /* Size of each free slot */
217 int nSlot; /* The number of pcache slots */
218 int nReserve; /* Try to keep nFreeSlot above this */
drhee70a842015-07-06 18:54:52 +0000219 void *pStart, *pEnd; /* Bounds of global page cache memory */
drh9f8cf9d2011-01-17 21:32:24 +0000220 /* Above requires no mutex. Use mutex below for variable that follow. */
221 sqlite3_mutex *mutex; /* Mutex for accessing the following: */
drh9f8cf9d2011-01-17 21:32:24 +0000222 PgFreeslot *pFree; /* Free page blocks */
drh2cbd78b2012-02-02 19:37:18 +0000223 int nFreeSlot; /* Number of unused pcache slots */
drh9f8cf9d2011-01-17 21:32:24 +0000224 /* The following value requires a mutex to change. We skip the mutex on
225 ** reading because (1) most platforms read a 32-bit integer atomically and
226 ** (2) even if an incorrect value is read, no great harm is done since this
227 ** is really just an optimization. */
228 int bUnderPressure; /* True if low on PAGECACHE memory */
danielk197744cd45c2008-11-15 11:22:45 +0000229} pcache1_g;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000230
231/*
232** All code in this file should access the global structure above via the
233** alias "pcache1". This ensures that the WSD emulation is used when
234** compiling for systems that do not support real WSD.
235*/
236#define pcache1 (GLOBAL(struct PCacheGlobal, pcache1_g))
237
238/*
drh9f8cf9d2011-01-17 21:32:24 +0000239** Macros to enter and leave the PCache LRU mutex.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000240*/
drh982215a2015-06-13 11:10:55 +0000241#if !defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) || SQLITE_THREADSAFE==0
242# define pcache1EnterMutex(X) assert((X)->mutex==0)
243# define pcache1LeaveMutex(X) assert((X)->mutex==0)
244# define PCACHE1_MIGHT_USE_GROUP_MUTEX 0
245#else
246# define pcache1EnterMutex(X) sqlite3_mutex_enter((X)->mutex)
247# define pcache1LeaveMutex(X) sqlite3_mutex_leave((X)->mutex)
248# define PCACHE1_MIGHT_USE_GROUP_MUTEX 1
249#endif
danielk1977bc2ca9e2008-11-13 14:28:28 +0000250
251/******************************************************************************/
252/******** Page Allocation/SQLITE_CONFIG_PCACHE Related Functions **************/
253
drh92af02c2015-09-04 04:31:56 +0000254
danielk1977bc2ca9e2008-11-13 14:28:28 +0000255/*
256** This function is called during initialization if a static buffer is
257** supplied to use for the page-cache by passing the SQLITE_CONFIG_PAGECACHE
258** verb to sqlite3_config(). Parameter pBuf points to an allocation large
259** enough to contain 'n' buffers of 'sz' bytes each.
drh9f8cf9d2011-01-17 21:32:24 +0000260**
261** This routine is called from sqlite3_initialize() and so it is guaranteed
262** to be serialized already. There is no need for further mutexing.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000263*/
264void sqlite3PCacheBufferSetup(void *pBuf, int sz, int n){
drhf4622dc2009-05-22 11:10:24 +0000265 if( pcache1.isInit ){
266 PgFreeslot *p;
drhee70a842015-07-06 18:54:52 +0000267 if( pBuf==0 ) sz = n = 0;
drh52df6f52017-08-28 16:11:05 +0000268 if( n==0 ) sz = 0;
drhf4622dc2009-05-22 11:10:24 +0000269 sz = ROUNDDOWN8(sz);
270 pcache1.szSlot = sz;
drh50d1b5f2010-08-27 12:21:06 +0000271 pcache1.nSlot = pcache1.nFreeSlot = n;
272 pcache1.nReserve = n>90 ? 10 : (n/10 + 1);
drhf4622dc2009-05-22 11:10:24 +0000273 pcache1.pStart = pBuf;
274 pcache1.pFree = 0;
drh9f8cf9d2011-01-17 21:32:24 +0000275 pcache1.bUnderPressure = 0;
drhf4622dc2009-05-22 11:10:24 +0000276 while( n-- ){
277 p = (PgFreeslot*)pBuf;
278 p->pNext = pcache1.pFree;
279 pcache1.pFree = p;
280 pBuf = (void*)&((char*)pBuf)[sz];
281 }
282 pcache1.pEnd = pBuf;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000283 }
danielk1977bc2ca9e2008-11-13 14:28:28 +0000284}
285
286/*
drh957026a2015-07-16 18:18:19 +0000287** Try to initialize the pCache->pFree and pCache->pBulk fields. Return
288** true if pCache->pFree ends up containing one or more free pages.
289*/
290static int pcache1InitBulk(PCache1 *pCache){
drh939d4bc2015-07-16 18:37:53 +0000291 i64 szBulk;
drh957026a2015-07-16 18:18:19 +0000292 char *zBulk;
293 if( pcache1.nInitPage==0 ) return 0;
294 /* Do not bother with a bulk allocation if the cache size very small */
295 if( pCache->nMax<3 ) return 0;
296 sqlite3BeginBenignMalloc();
297 if( pcache1.nInitPage>0 ){
drh939d4bc2015-07-16 18:37:53 +0000298 szBulk = pCache->szAlloc * (i64)pcache1.nInitPage;
drh957026a2015-07-16 18:18:19 +0000299 }else{
drh939d4bc2015-07-16 18:37:53 +0000300 szBulk = -1024 * (i64)pcache1.nInitPage;
drh957026a2015-07-16 18:18:19 +0000301 }
drh939d4bc2015-07-16 18:37:53 +0000302 if( szBulk > pCache->szAlloc*(i64)pCache->nMax ){
drh989412a2016-10-13 12:56:18 +0000303 szBulk = pCache->szAlloc*(i64)pCache->nMax;
drh957026a2015-07-16 18:18:19 +0000304 }
305 zBulk = pCache->pBulk = sqlite3Malloc( szBulk );
306 sqlite3EndBenignMalloc();
307 if( zBulk ){
308 int nBulk = sqlite3MallocSize(zBulk)/pCache->szAlloc;
drh4eb8d7f2017-03-29 17:06:14 +0000309 do{
drh957026a2015-07-16 18:18:19 +0000310 PgHdr1 *pX = (PgHdr1*)&zBulk[pCache->szPage];
311 pX->page.pBuf = zBulk;
312 pX->page.pExtra = &pX[1];
313 pX->isBulkLocal = 1;
drh92af02c2015-09-04 04:31:56 +0000314 pX->isAnchor = 0;
drh957026a2015-07-16 18:18:19 +0000315 pX->pNext = pCache->pFree;
dane385d332019-02-06 20:49:49 +0000316 pX->pLruPrev = 0; /* Initializing this saves a valgrind error */
drh957026a2015-07-16 18:18:19 +0000317 pCache->pFree = pX;
318 zBulk += pCache->szAlloc;
drh4eb8d7f2017-03-29 17:06:14 +0000319 }while( --nBulk );
drh957026a2015-07-16 18:18:19 +0000320 }
321 return pCache->pFree!=0;
322}
323
324/*
danielk1977bc2ca9e2008-11-13 14:28:28 +0000325** Malloc function used within this file to allocate space from the buffer
326** configured using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no
327** such buffer exists or there is no space left in it, this function falls
328** back to sqlite3Malloc().
drh9f8cf9d2011-01-17 21:32:24 +0000329**
330** Multiple threads can run this routine at the same time. Global variables
331** in pcache1 need to be protected via mutex.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000332*/
333static void *pcache1Alloc(int nByte){
drh9f8cf9d2011-01-17 21:32:24 +0000334 void *p = 0;
335 assert( sqlite3_mutex_notheld(pcache1.grp.mutex) );
drh9f8cf9d2011-01-17 21:32:24 +0000336 if( nByte<=pcache1.szSlot ){
337 sqlite3_mutex_enter(pcache1.mutex);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000338 p = (PgHdr1 *)pcache1.pFree;
drh9f8cf9d2011-01-17 21:32:24 +0000339 if( p ){
340 pcache1.pFree = pcache1.pFree->pNext;
341 pcache1.nFreeSlot--;
342 pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve;
343 assert( pcache1.nFreeSlot>=0 );
drhb02392e2015-10-15 15:28:56 +0000344 sqlite3StatusHighwater(SQLITE_STATUS_PAGECACHE_SIZE, nByte);
drhaf89fe62015-03-23 17:25:18 +0000345 sqlite3StatusUp(SQLITE_STATUS_PAGECACHE_USED, 1);
drh9f8cf9d2011-01-17 21:32:24 +0000346 }
347 sqlite3_mutex_leave(pcache1.mutex);
348 }
349 if( p==0 ){
350 /* Memory is not available in the SQLITE_CONFIG_PAGECACHE pool. Get
351 ** it from sqlite3Malloc instead.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000352 */
danielk1977bc2ca9e2008-11-13 14:28:28 +0000353 p = sqlite3Malloc(nByte);
drh4bd69522012-06-07 02:35:29 +0000354#ifndef SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS
danielk1977bc2ca9e2008-11-13 14:28:28 +0000355 if( p ){
356 int sz = sqlite3MallocSize(p);
drh9bf3da8e2011-01-26 13:24:40 +0000357 sqlite3_mutex_enter(pcache1.mutex);
drhb02392e2015-10-15 15:28:56 +0000358 sqlite3StatusHighwater(SQLITE_STATUS_PAGECACHE_SIZE, nByte);
drhaf89fe62015-03-23 17:25:18 +0000359 sqlite3StatusUp(SQLITE_STATUS_PAGECACHE_OVERFLOW, sz);
drh9bf3da8e2011-01-26 13:24:40 +0000360 sqlite3_mutex_leave(pcache1.mutex);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000361 }
drh4bd69522012-06-07 02:35:29 +0000362#endif
drh107b56e2010-03-12 16:32:53 +0000363 sqlite3MemdebugSetType(p, MEMTYPE_PCACHE);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000364 }
365 return p;
366}
367
368/*
369** Free an allocated buffer obtained from pcache1Alloc().
370*/
drhee70a842015-07-06 18:54:52 +0000371static void pcache1Free(void *p){
drhee70a842015-07-06 18:54:52 +0000372 if( p==0 ) return;
drh8b0ba7b2015-12-16 13:07:35 +0000373 if( SQLITE_WITHIN(p, pcache1.pStart, pcache1.pEnd) ){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000374 PgFreeslot *pSlot;
drh9f8cf9d2011-01-17 21:32:24 +0000375 sqlite3_mutex_enter(pcache1.mutex);
drhaf89fe62015-03-23 17:25:18 +0000376 sqlite3StatusDown(SQLITE_STATUS_PAGECACHE_USED, 1);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000377 pSlot = (PgFreeslot*)p;
378 pSlot->pNext = pcache1.pFree;
379 pcache1.pFree = pSlot;
drh50d1b5f2010-08-27 12:21:06 +0000380 pcache1.nFreeSlot++;
drh9f8cf9d2011-01-17 21:32:24 +0000381 pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve;
drh50d1b5f2010-08-27 12:21:06 +0000382 assert( pcache1.nFreeSlot<=pcache1.nSlot );
drh9f8cf9d2011-01-17 21:32:24 +0000383 sqlite3_mutex_leave(pcache1.mutex);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000384 }else{
drh107b56e2010-03-12 16:32:53 +0000385 assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) );
386 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
drh4bd69522012-06-07 02:35:29 +0000387#ifndef SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS
drh9493caf2016-03-17 23:16:37 +0000388 {
389 int nFreed = 0;
390 nFreed = sqlite3MallocSize(p);
391 sqlite3_mutex_enter(pcache1.mutex);
392 sqlite3StatusDown(SQLITE_STATUS_PAGECACHE_OVERFLOW, nFreed);
393 sqlite3_mutex_leave(pcache1.mutex);
394 }
drh4bd69522012-06-07 02:35:29 +0000395#endif
danielk1977bc2ca9e2008-11-13 14:28:28 +0000396 sqlite3_free(p);
397 }
398}
399
drhc8f503a2010-08-20 09:14:13 +0000400#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
401/*
drh9d13f112010-08-24 18:06:35 +0000402** Return the size of a pcache allocation
drhc8f503a2010-08-20 09:14:13 +0000403*/
404static int pcache1MemSize(void *p){
drhc8f503a2010-08-20 09:14:13 +0000405 if( p>=pcache1.pStart && p<pcache1.pEnd ){
406 return pcache1.szSlot;
407 }else{
408 int iSize;
409 assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) );
410 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
411 iSize = sqlite3MallocSize(p);
412 sqlite3MemdebugSetType(p, MEMTYPE_PCACHE);
413 return iSize;
414 }
415}
416#endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
417
dand2925702011-08-19 18:15:00 +0000418/*
danielk1977bc2ca9e2008-11-13 14:28:28 +0000419** Allocate a new page object initially associated with cache pCache.
420*/
drh3c0c4312015-09-01 19:51:37 +0000421static PgHdr1 *pcache1AllocPage(PCache1 *pCache, int benignMalloc){
danb5126dd2011-09-22 14:56:31 +0000422 PgHdr1 *p = 0;
423 void *pPg;
dand2925702011-08-19 18:15:00 +0000424
dand2925702011-08-19 18:15:00 +0000425 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
drh957026a2015-07-16 18:18:19 +0000426 if( pCache->pFree || (pCache->nPage==0 && pcache1InitBulk(pCache)) ){
drhee70a842015-07-06 18:54:52 +0000427 p = pCache->pFree;
428 pCache->pFree = p->pNext;
429 p->pNext = 0;
430 }else{
drhdb7ae892015-07-06 20:57:22 +0000431#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
drhee70a842015-07-06 18:54:52 +0000432 /* The group mutex must be released before pcache1Alloc() is called. This
drhdb7ae892015-07-06 20:57:22 +0000433 ** is because it might call sqlite3_release_memory(), which assumes that
drhee70a842015-07-06 18:54:52 +0000434 ** this mutex is not held. */
drhdb7ae892015-07-06 20:57:22 +0000435 assert( pcache1.separateCache==0 );
436 assert( pCache->pGroup==&pcache1.grp );
drhee70a842015-07-06 18:54:52 +0000437 pcache1LeaveMutex(pCache->pGroup);
drhdb7ae892015-07-06 20:57:22 +0000438#endif
drh8faee872015-09-19 18:08:13 +0000439 if( benignMalloc ){ sqlite3BeginBenignMalloc(); }
dan22e21ff2011-11-08 20:08:44 +0000440#ifdef SQLITE_PCACHE_SEPARATE_HEADER
drhee70a842015-07-06 18:54:52 +0000441 pPg = pcache1Alloc(pCache->szPage);
442 p = sqlite3Malloc(sizeof(PgHdr1) + pCache->szExtra);
443 if( !pPg || !p ){
444 pcache1Free(pPg);
445 sqlite3_free(p);
446 pPg = 0;
447 }
dan22e21ff2011-11-08 20:08:44 +0000448#else
drhee70a842015-07-06 18:54:52 +0000449 pPg = pcache1Alloc(pCache->szAlloc);
450 p = (PgHdr1 *)&((u8 *)pPg)[pCache->szPage];
dan22e21ff2011-11-08 20:08:44 +0000451#endif
drh8faee872015-09-19 18:08:13 +0000452 if( benignMalloc ){ sqlite3EndBenignMalloc(); }
drhdb7ae892015-07-06 20:57:22 +0000453#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
drhee70a842015-07-06 18:54:52 +0000454 pcache1EnterMutex(pCache->pGroup);
drhdb7ae892015-07-06 20:57:22 +0000455#endif
drhee70a842015-07-06 18:54:52 +0000456 if( pPg==0 ) return 0;
dan22e21ff2011-11-08 20:08:44 +0000457 p->page.pBuf = pPg;
458 p->page.pExtra = &p[1];
drhee70a842015-07-06 18:54:52 +0000459 p->isBulkLocal = 0;
drh92af02c2015-09-04 04:31:56 +0000460 p->isAnchor = 0;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000461 }
drh617b7b42017-08-30 04:44:59 +0000462 (*pCache->pnPurgeable)++;
drhee70a842015-07-06 18:54:52 +0000463 return p;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000464}
465
466/*
467** Free a page object allocated by pcache1AllocPage().
468*/
469static void pcache1FreePage(PgHdr1 *p){
drhdb7ae892015-07-06 20:57:22 +0000470 PCache1 *pCache;
471 assert( p!=0 );
472 pCache = p->pCache;
473 assert( sqlite3_mutex_held(p->pCache->pGroup->mutex) );
474 if( p->isBulkLocal ){
475 p->pNext = pCache->pFree;
476 pCache->pFree = p;
477 }else{
478 pcache1Free(p->page.pBuf);
dan22e21ff2011-11-08 20:08:44 +0000479#ifdef SQLITE_PCACHE_SEPARATE_HEADER
drhdb7ae892015-07-06 20:57:22 +0000480 sqlite3_free(p);
dan22e21ff2011-11-08 20:08:44 +0000481#endif
drhdb7ae892015-07-06 20:57:22 +0000482 }
drh617b7b42017-08-30 04:44:59 +0000483 (*pCache->pnPurgeable)--;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000484}
485
486/*
487** Malloc function used by SQLite to obtain space from the buffer configured
488** using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no such buffer
489** exists, this function falls back to sqlite3Malloc().
490*/
491void *sqlite3PageMalloc(int sz){
drhd6401e82018-12-14 13:35:48 +0000492 /* During rebalance operations on a corrupt database file, it is sometimes
493 ** (rarely) possible to overread the temporary page buffer by a few bytes.
494 ** Enlarge the allocation slightly so that this does not cause problems. */
drhd7a5e492018-12-14 16:20:54 +0000495 return pcache1Alloc(sz);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000496}
497
498/*
499** Free an allocated buffer obtained from sqlite3PageMalloc().
500*/
501void sqlite3PageFree(void *p){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000502 pcache1Free(p);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000503}
504
drh50d1b5f2010-08-27 12:21:06 +0000505
506/*
507** Return true if it desirable to avoid allocating a new page cache
508** entry.
509**
510** If memory was allocated specifically to the page cache using
511** SQLITE_CONFIG_PAGECACHE but that memory has all been used, then
512** it is desirable to avoid allocating a new page cache entry because
513** presumably SQLITE_CONFIG_PAGECACHE was suppose to be sufficient
514** for all page cache needs and we should not need to spill the
515** allocation onto the heap.
516**
drh45d29302012-01-08 22:18:33 +0000517** Or, the heap is used for all page cache memory but the heap is
drh50d1b5f2010-08-27 12:21:06 +0000518** under memory pressure, then again it is desirable to avoid
519** allocating a new page cache entry in order to avoid stressing
520** the heap even further.
521*/
522static int pcache1UnderMemoryPressure(PCache1 *pCache){
dan22e21ff2011-11-08 20:08:44 +0000523 if( pcache1.nSlot && (pCache->szPage+pCache->szExtra)<=pcache1.szSlot ){
drh9f8cf9d2011-01-17 21:32:24 +0000524 return pcache1.bUnderPressure;
drh50d1b5f2010-08-27 12:21:06 +0000525 }else{
526 return sqlite3HeapNearlyFull();
527 }
528}
529
danielk1977bc2ca9e2008-11-13 14:28:28 +0000530/******************************************************************************/
531/******** General Implementation Functions ************************************/
532
533/*
534** This function is used to resize the hash table used by the cache passed
535** as the first argument.
536**
drh9f8cf9d2011-01-17 21:32:24 +0000537** The PCache mutex must be held when this function is called.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000538*/
drhefbf0442014-08-23 23:15:31 +0000539static void pcache1ResizeHash(PCache1 *p){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000540 PgHdr1 **apNew;
danielk197744cd45c2008-11-15 11:22:45 +0000541 unsigned int nNew;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000542 unsigned int i;
543
drh9f8cf9d2011-01-17 21:32:24 +0000544 assert( sqlite3_mutex_held(p->pGroup->mutex) );
danielk1977bc2ca9e2008-11-13 14:28:28 +0000545
546 nNew = p->nHash*2;
547 if( nNew<256 ){
548 nNew = 256;
549 }
550
drh9f8cf9d2011-01-17 21:32:24 +0000551 pcache1LeaveMutex(p->pGroup);
drh085bb7f2008-12-06 14:34:33 +0000552 if( p->nHash ){ sqlite3BeginBenignMalloc(); }
dan6809c962012-07-30 14:53:54 +0000553 apNew = (PgHdr1 **)sqlite3MallocZero(sizeof(PgHdr1 *)*nNew);
drh085bb7f2008-12-06 14:34:33 +0000554 if( p->nHash ){ sqlite3EndBenignMalloc(); }
drh9f8cf9d2011-01-17 21:32:24 +0000555 pcache1EnterMutex(p->pGroup);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000556 if( apNew ){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000557 for(i=0; i<p->nHash; i++){
558 PgHdr1 *pPage;
559 PgHdr1 *pNext = p->apHash[i];
drhb27b7f52008-12-10 18:03:45 +0000560 while( (pPage = pNext)!=0 ){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000561 unsigned int h = pPage->iKey % nNew;
562 pNext = pPage->pNext;
563 pPage->pNext = apNew[h];
564 apNew[h] = pPage;
565 }
566 }
567 sqlite3_free(p->apHash);
568 p->apHash = apNew;
569 p->nHash = nNew;
570 }
danielk1977bc2ca9e2008-11-13 14:28:28 +0000571}
572
573/*
574** This function is used internally to remove the page pPage from the
drh9f8cf9d2011-01-17 21:32:24 +0000575** PGroup LRU list, if is part of it. If pPage is not part of the PGroup
danielk1977bc2ca9e2008-11-13 14:28:28 +0000576** LRU list, then this function is a no-op.
577**
drh9f8cf9d2011-01-17 21:32:24 +0000578** The PGroup mutex must be held when this function is called.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000579*/
drh55a46c92015-06-12 13:49:26 +0000580static PgHdr1 *pcache1PinPage(PgHdr1 *pPage){
drh5d56dd22013-12-13 18:50:40 +0000581 assert( pPage!=0 );
drheabb67f2017-08-05 15:49:03 +0000582 assert( PAGE_IS_UNPINNED(pPage) );
drh92af02c2015-09-04 04:31:56 +0000583 assert( pPage->pLruNext );
584 assert( pPage->pLruPrev );
drheabb67f2017-08-05 15:49:03 +0000585 assert( sqlite3_mutex_held(pPage->pCache->pGroup->mutex) );
drh92af02c2015-09-04 04:31:56 +0000586 pPage->pLruPrev->pLruNext = pPage->pLruNext;
587 pPage->pLruNext->pLruPrev = pPage->pLruPrev;
drh5d56dd22013-12-13 18:50:40 +0000588 pPage->pLruNext = 0;
drhde72d2a2018-12-03 01:58:02 +0000589 /* pPage->pLruPrev = 0;
590 ** No need to clear pLruPrev as it is never accessed if pLruNext is 0 */
drh92af02c2015-09-04 04:31:56 +0000591 assert( pPage->isAnchor==0 );
drheabb67f2017-08-05 15:49:03 +0000592 assert( pPage->pCache->pGroup->lru.isAnchor==1 );
593 pPage->pCache->nRecyclable--;
drh55a46c92015-06-12 13:49:26 +0000594 return pPage;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000595}
596
597
598/*
599** Remove the page supplied as an argument from the hash table
600** (PCache1.apHash structure) that it is currently stored in.
drh95c91e12015-06-29 00:21:00 +0000601** Also free the page if freePage is true.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000602**
drh9f8cf9d2011-01-17 21:32:24 +0000603** The PGroup mutex must be held when this function is called.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000604*/
drh95c91e12015-06-29 00:21:00 +0000605static void pcache1RemoveFromHash(PgHdr1 *pPage, int freeFlag){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000606 unsigned int h;
607 PCache1 *pCache = pPage->pCache;
608 PgHdr1 **pp;
609
drh9f8cf9d2011-01-17 21:32:24 +0000610 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
danielk1977bc2ca9e2008-11-13 14:28:28 +0000611 h = pPage->iKey % pCache->nHash;
612 for(pp=&pCache->apHash[h]; (*pp)!=pPage; pp=&(*pp)->pNext);
613 *pp = (*pp)->pNext;
614
615 pCache->nPage--;
drh95c91e12015-06-29 00:21:00 +0000616 if( freeFlag ) pcache1FreePage(pPage);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000617}
618
619/*
drh9f8cf9d2011-01-17 21:32:24 +0000620** If there are currently more than nMaxPage pages allocated, try
621** to recycle pages to reduce the number allocated to nMaxPage.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000622*/
drh957026a2015-07-16 18:18:19 +0000623static void pcache1EnforceMaxPage(PCache1 *pCache){
624 PGroup *pGroup = pCache->pGroup;
drh92af02c2015-09-04 04:31:56 +0000625 PgHdr1 *p;
drh9f8cf9d2011-01-17 21:32:24 +0000626 assert( sqlite3_mutex_held(pGroup->mutex) );
drh617b7b42017-08-30 04:44:59 +0000627 while( pGroup->nPurgeable>pGroup->nMaxPage
drh92af02c2015-09-04 04:31:56 +0000628 && (p=pGroup->lru.pLruPrev)->isAnchor==0
629 ){
drh9f8cf9d2011-01-17 21:32:24 +0000630 assert( p->pCache->pGroup==pGroup );
drheabb67f2017-08-05 15:49:03 +0000631 assert( PAGE_IS_UNPINNED(p) );
danielk1977bc2ca9e2008-11-13 14:28:28 +0000632 pcache1PinPage(p);
drh95c91e12015-06-29 00:21:00 +0000633 pcache1RemoveFromHash(p, 1);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000634 }
drh957026a2015-07-16 18:18:19 +0000635 if( pCache->nPage==0 && pCache->pBulk ){
636 sqlite3_free(pCache->pBulk);
637 pCache->pBulk = pCache->pFree = 0;
638 }
danielk1977bc2ca9e2008-11-13 14:28:28 +0000639}
640
641/*
642** Discard all pages from cache pCache with a page number (key value)
643** greater than or equal to iLimit. Any pinned pages that meet this
644** criteria are unpinned before they are discarded.
645**
drh9f8cf9d2011-01-17 21:32:24 +0000646** The PCache mutex must be held when this function is called.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000647*/
648static void pcache1TruncateUnsafe(
drh9f8cf9d2011-01-17 21:32:24 +0000649 PCache1 *pCache, /* The cache to truncate */
650 unsigned int iLimit /* Drop pages with this pgno or larger */
danielk1977bc2ca9e2008-11-13 14:28:28 +0000651){
drhd9fabbc2016-08-10 11:50:12 +0000652 TESTONLY( int nPage = 0; ) /* To assert pCache->nPage is correct */
653 unsigned int h, iStop;
drh9f8cf9d2011-01-17 21:32:24 +0000654 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
drhd9fabbc2016-08-10 11:50:12 +0000655 assert( pCache->iMaxKey >= iLimit );
656 assert( pCache->nHash > 0 );
drhf5dbe7f2016-08-10 15:02:49 +0000657 if( pCache->iMaxKey - iLimit < pCache->nHash ){
drhd9fabbc2016-08-10 11:50:12 +0000658 /* If we are just shaving the last few pages off the end of the
659 ** cache, then there is no point in scanning the entire hash table.
660 ** Only scan those hash slots that might contain pages that need to
661 ** be removed. */
drhf5dbe7f2016-08-10 15:02:49 +0000662 h = iLimit % pCache->nHash;
663 iStop = pCache->iMaxKey % pCache->nHash;
drhd9fabbc2016-08-10 11:50:12 +0000664 TESTONLY( nPage = -10; ) /* Disable the pCache->nPage validity check */
665 }else{
666 /* This is the general case where many pages are being removed.
667 ** It is necessary to scan the entire hash table */
drhf5dbe7f2016-08-10 15:02:49 +0000668 h = pCache->nHash/2;
669 iStop = h - 1;
drhd9fabbc2016-08-10 11:50:12 +0000670 }
671 for(;;){
672 PgHdr1 **pp;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000673 PgHdr1 *pPage;
drhd9fabbc2016-08-10 11:50:12 +0000674 assert( h<pCache->nHash );
675 pp = &pCache->apHash[h];
drhb27b7f52008-12-10 18:03:45 +0000676 while( (pPage = *pp)!=0 ){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000677 if( pPage->iKey>=iLimit ){
danielk1977ea24ac42009-05-08 06:52:47 +0000678 pCache->nPage--;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000679 *pp = pPage->pNext;
drheabb67f2017-08-05 15:49:03 +0000680 if( PAGE_IS_UNPINNED(pPage) ) pcache1PinPage(pPage);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000681 pcache1FreePage(pPage);
682 }else{
683 pp = &pPage->pNext;
drhd9fabbc2016-08-10 11:50:12 +0000684 TESTONLY( if( nPage>=0 ) nPage++; )
danielk1977bc2ca9e2008-11-13 14:28:28 +0000685 }
686 }
drhd9fabbc2016-08-10 11:50:12 +0000687 if( h==iStop ) break;
drhf5dbe7f2016-08-10 15:02:49 +0000688 h = (h+1) % pCache->nHash;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000689 }
drhd9fabbc2016-08-10 11:50:12 +0000690 assert( nPage<0 || pCache->nPage==(unsigned)nPage );
danielk1977bc2ca9e2008-11-13 14:28:28 +0000691}
692
693/******************************************************************************/
694/******** sqlite3_pcache Methods **********************************************/
695
696/*
697** Implementation of the sqlite3_pcache.xInit method.
698*/
danielk197762c14b32008-11-19 09:05:26 +0000699static int pcache1Init(void *NotUsed){
700 UNUSED_PARAMETER(NotUsed);
drhf4622dc2009-05-22 11:10:24 +0000701 assert( pcache1.isInit==0 );
danielk1977bc2ca9e2008-11-13 14:28:28 +0000702 memset(&pcache1, 0, sizeof(pcache1));
drhdb7ae892015-07-06 20:57:22 +0000703
704
705 /*
706 ** The pcache1.separateCache variable is true if each PCache has its own
707 ** private PGroup (mode-1). pcache1.separateCache is false if the single
708 ** PGroup in pcache1.grp is used for all page caches (mode-2).
709 **
710 ** * Always use a unified cache (mode-2) if ENABLE_MEMORY_MANAGEMENT
711 **
712 ** * Use a unified cache in single-threaded applications that have
713 ** configured a start-time buffer for use as page-cache memory using
714 ** sqlite3_config(SQLITE_CONFIG_PAGECACHE, pBuf, sz, N) with non-NULL
715 ** pBuf argument.
716 **
717 ** * Otherwise use separate caches (mode-1)
718 */
719#if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT)
720 pcache1.separateCache = 0;
drhfd5ae962015-07-07 15:14:16 +0000721#elif SQLITE_THREADSAFE
drhdb7ae892015-07-06 20:57:22 +0000722 pcache1.separateCache = sqlite3GlobalConfig.pPage==0
723 || sqlite3GlobalConfig.bCoreMutex>0;
drhfd5ae962015-07-07 15:14:16 +0000724#else
725 pcache1.separateCache = sqlite3GlobalConfig.pPage==0;
drhdb7ae892015-07-06 20:57:22 +0000726#endif
727
drh982215a2015-06-13 11:10:55 +0000728#if SQLITE_THREADSAFE
danielk1977bc2ca9e2008-11-13 14:28:28 +0000729 if( sqlite3GlobalConfig.bCoreMutex ){
drh97a7e5e2016-04-26 18:58:54 +0000730 pcache1.grp.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU);
731 pcache1.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_PMEM);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000732 }
drh982215a2015-06-13 11:10:55 +0000733#endif
drh957026a2015-07-16 18:18:19 +0000734 if( pcache1.separateCache
735 && sqlite3GlobalConfig.nPage!=0
736 && sqlite3GlobalConfig.pPage==0
737 ){
738 pcache1.nInitPage = sqlite3GlobalConfig.nPage;
739 }else{
740 pcache1.nInitPage = 0;
741 }
drh41692e92011-01-25 04:34:51 +0000742 pcache1.grp.mxPinned = 10;
drhf4622dc2009-05-22 11:10:24 +0000743 pcache1.isInit = 1;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000744 return SQLITE_OK;
745}
746
747/*
748** Implementation of the sqlite3_pcache.xShutdown method.
shane7c7c3112009-08-17 15:31:23 +0000749** Note that the static mutex allocated in xInit does
750** not need to be freed.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000751*/
danielk197762c14b32008-11-19 09:05:26 +0000752static void pcache1Shutdown(void *NotUsed){
753 UNUSED_PARAMETER(NotUsed);
drhf4622dc2009-05-22 11:10:24 +0000754 assert( pcache1.isInit!=0 );
drhb0937192009-05-22 10:53:29 +0000755 memset(&pcache1, 0, sizeof(pcache1));
danielk1977bc2ca9e2008-11-13 14:28:28 +0000756}
757
drhefbf0442014-08-23 23:15:31 +0000758/* forward declaration */
759static void pcache1Destroy(sqlite3_pcache *p);
760
danielk1977bc2ca9e2008-11-13 14:28:28 +0000761/*
762** Implementation of the sqlite3_pcache.xCreate method.
763**
764** Allocate a new cache.
765*/
drhe5c40b12011-11-09 00:06:05 +0000766static sqlite3_pcache *pcache1Create(int szPage, int szExtra, int bPurgeable){
drh9f8cf9d2011-01-17 21:32:24 +0000767 PCache1 *pCache; /* The newly created page cache */
768 PGroup *pGroup; /* The group the new page cache will belong to */
769 int sz; /* Bytes of memory required to allocate the new cache */
danielk1977bc2ca9e2008-11-13 14:28:28 +0000770
drhe73c9142011-11-09 16:12:24 +0000771 assert( (szPage & (szPage-1))==0 && szPage>=512 && szPage<=65536 );
772 assert( szExtra < 300 );
773
drhdb7ae892015-07-06 20:57:22 +0000774 sz = sizeof(PCache1) + sizeof(PGroup)*pcache1.separateCache;
dan6809c962012-07-30 14:53:54 +0000775 pCache = (PCache1 *)sqlite3MallocZero(sz);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000776 if( pCache ){
drhdb7ae892015-07-06 20:57:22 +0000777 if( pcache1.separateCache ){
drh9f8cf9d2011-01-17 21:32:24 +0000778 pGroup = (PGroup*)&pCache[1];
drh41692e92011-01-25 04:34:51 +0000779 pGroup->mxPinned = 10;
drh9f8cf9d2011-01-17 21:32:24 +0000780 }else{
dan9dde7cb2011-06-09 17:53:43 +0000781 pGroup = &pcache1.grp;
drh9f8cf9d2011-01-17 21:32:24 +0000782 }
drh92af02c2015-09-04 04:31:56 +0000783 if( pGroup->lru.isAnchor==0 ){
784 pGroup->lru.isAnchor = 1;
785 pGroup->lru.pLruPrev = pGroup->lru.pLruNext = &pGroup->lru;
786 }
drh9f8cf9d2011-01-17 21:32:24 +0000787 pCache->pGroup = pGroup;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000788 pCache->szPage = szPage;
dan22e21ff2011-11-08 20:08:44 +0000789 pCache->szExtra = szExtra;
drhee70a842015-07-06 18:54:52 +0000790 pCache->szAlloc = szPage + szExtra + ROUND8(sizeof(PgHdr1));
danielk1977bc2ca9e2008-11-13 14:28:28 +0000791 pCache->bPurgeable = (bPurgeable ? 1 : 0);
drhefbf0442014-08-23 23:15:31 +0000792 pcache1EnterMutex(pGroup);
793 pcache1ResizeHash(pCache);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000794 if( bPurgeable ){
795 pCache->nMin = 10;
drh9f8cf9d2011-01-17 21:32:24 +0000796 pGroup->nMinPage += pCache->nMin;
drh41692e92011-01-25 04:34:51 +0000797 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
drh617b7b42017-08-30 04:44:59 +0000798 pCache->pnPurgeable = &pGroup->nPurgeable;
799 }else{
drh1757fed2019-01-09 14:49:58 +0000800 pCache->pnPurgeable = &pCache->nPurgeableDummy;
drhefbf0442014-08-23 23:15:31 +0000801 }
802 pcache1LeaveMutex(pGroup);
803 if( pCache->nHash==0 ){
804 pcache1Destroy((sqlite3_pcache*)pCache);
805 pCache = 0;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000806 }
807 }
808 return (sqlite3_pcache *)pCache;
809}
810
811/*
812** Implementation of the sqlite3_pcache.xCachesize method.
813**
814** Configure the cache_size limit for a cache.
815*/
816static void pcache1Cachesize(sqlite3_pcache *p, int nMax){
817 PCache1 *pCache = (PCache1 *)p;
818 if( pCache->bPurgeable ){
drh9f8cf9d2011-01-17 21:32:24 +0000819 PGroup *pGroup = pCache->pGroup;
820 pcache1EnterMutex(pGroup);
821 pGroup->nMaxPage += (nMax - pCache->nMax);
drh41692e92011-01-25 04:34:51 +0000822 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000823 pCache->nMax = nMax;
drh25ca5682011-01-26 00:07:03 +0000824 pCache->n90pct = pCache->nMax*9/10;
drh957026a2015-07-16 18:18:19 +0000825 pcache1EnforceMaxPage(pCache);
drh9f8cf9d2011-01-17 21:32:24 +0000826 pcache1LeaveMutex(pGroup);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000827 }
828}
829
830/*
drh09419b42011-11-16 19:29:17 +0000831** Implementation of the sqlite3_pcache.xShrink method.
832**
833** Free up as much memory as possible.
834*/
835static void pcache1Shrink(sqlite3_pcache *p){
836 PCache1 *pCache = (PCache1*)p;
837 if( pCache->bPurgeable ){
838 PGroup *pGroup = pCache->pGroup;
839 int savedMaxPage;
840 pcache1EnterMutex(pGroup);
841 savedMaxPage = pGroup->nMaxPage;
842 pGroup->nMaxPage = 0;
drh957026a2015-07-16 18:18:19 +0000843 pcache1EnforceMaxPage(pCache);
drh09419b42011-11-16 19:29:17 +0000844 pGroup->nMaxPage = savedMaxPage;
845 pcache1LeaveMutex(pGroup);
846 }
847}
848
849/*
danielk1977bc2ca9e2008-11-13 14:28:28 +0000850** Implementation of the sqlite3_pcache.xPagecount method.
851*/
852static int pcache1Pagecount(sqlite3_pcache *p){
853 int n;
drh9f8cf9d2011-01-17 21:32:24 +0000854 PCache1 *pCache = (PCache1*)p;
855 pcache1EnterMutex(pCache->pGroup);
856 n = pCache->nPage;
857 pcache1LeaveMutex(pCache->pGroup);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000858 return n;
859}
860
drhefbf0442014-08-23 23:15:31 +0000861
862/*
863** Implement steps 3, 4, and 5 of the pcache1Fetch() algorithm described
864** in the header of the pcache1Fetch() procedure.
865**
866** This steps are broken out into a separate procedure because they are
867** usually not needed, and by avoiding the stack initialization required
868** for these steps, the main pcache1Fetch() procedure can run faster.
869*/
870static SQLITE_NOINLINE PgHdr1 *pcache1FetchStage2(
871 PCache1 *pCache,
872 unsigned int iKey,
873 int createFlag
874){
875 unsigned int nPinned;
876 PGroup *pGroup = pCache->pGroup;
877 PgHdr1 *pPage = 0;
878
879 /* Step 3: Abort if createFlag is 1 but the cache is nearly full */
880 assert( pCache->nPage >= pCache->nRecyclable );
881 nPinned = pCache->nPage - pCache->nRecyclable;
882 assert( pGroup->mxPinned == pGroup->nMaxPage + 10 - pGroup->nMinPage );
883 assert( pCache->n90pct == pCache->nMax*9/10 );
884 if( createFlag==1 && (
885 nPinned>=pGroup->mxPinned
886 || nPinned>=pCache->n90pct
dan5bd8af72014-10-10 19:10:59 +0000887 || (pcache1UnderMemoryPressure(pCache) && pCache->nRecyclable<nPinned)
drhefbf0442014-08-23 23:15:31 +0000888 )){
889 return 0;
890 }
891
892 if( pCache->nPage>=pCache->nHash ) pcache1ResizeHash(pCache);
893 assert( pCache->nHash>0 && pCache->apHash );
894
895 /* Step 4. Try to recycle a page. */
drhc54357c2015-07-07 14:06:18 +0000896 if( pCache->bPurgeable
drh92af02c2015-09-04 04:31:56 +0000897 && !pGroup->lru.pLruPrev->isAnchor
drhc54357c2015-07-07 14:06:18 +0000898 && ((pCache->nPage+1>=pCache->nMax) || pcache1UnderMemoryPressure(pCache))
899 ){
drhefbf0442014-08-23 23:15:31 +0000900 PCache1 *pOther;
drh92af02c2015-09-04 04:31:56 +0000901 pPage = pGroup->lru.pLruPrev;
drheabb67f2017-08-05 15:49:03 +0000902 assert( PAGE_IS_UNPINNED(pPage) );
drh95c91e12015-06-29 00:21:00 +0000903 pcache1RemoveFromHash(pPage, 0);
drhefbf0442014-08-23 23:15:31 +0000904 pcache1PinPage(pPage);
905 pOther = pPage->pCache;
drhee70a842015-07-06 18:54:52 +0000906 if( pOther->szAlloc != pCache->szAlloc ){
drhefbf0442014-08-23 23:15:31 +0000907 pcache1FreePage(pPage);
908 pPage = 0;
909 }else{
drh617b7b42017-08-30 04:44:59 +0000910 pGroup->nPurgeable -= (pOther->bPurgeable - pCache->bPurgeable);
drhefbf0442014-08-23 23:15:31 +0000911 }
912 }
913
914 /* Step 5. If a usable page buffer has still not been found,
915 ** attempt to allocate a new one.
916 */
917 if( !pPage ){
drh3c0c4312015-09-01 19:51:37 +0000918 pPage = pcache1AllocPage(pCache, createFlag==1);
drhefbf0442014-08-23 23:15:31 +0000919 }
920
921 if( pPage ){
922 unsigned int h = iKey % pCache->nHash;
923 pCache->nPage++;
924 pPage->iKey = iKey;
925 pPage->pNext = pCache->apHash[h];
926 pPage->pCache = pCache;
drhefbf0442014-08-23 23:15:31 +0000927 pPage->pLruNext = 0;
drhde72d2a2018-12-03 01:58:02 +0000928 /* pPage->pLruPrev = 0;
929 ** No need to clear pLruPrev since it is not accessed when pLruNext==0 */
drhefbf0442014-08-23 23:15:31 +0000930 *(void **)pPage->page.pExtra = 0;
931 pCache->apHash[h] = pPage;
932 if( iKey>pCache->iMaxKey ){
933 pCache->iMaxKey = iKey;
934 }
935 }
936 return pPage;
937}
938
danielk1977bc2ca9e2008-11-13 14:28:28 +0000939/*
940** Implementation of the sqlite3_pcache.xFetch method.
941**
942** Fetch a page by key value.
943**
944** Whether or not a new page may be allocated by this function depends on
drhf18a61d2009-07-17 11:44:07 +0000945** the value of the createFlag argument. 0 means do not allocate a new
946** page. 1 means allocate a new page if space is easily available. 2
947** means to try really hard to allocate a new page.
948**
949** For a non-purgeable cache (a cache used as the storage for an in-memory
950** database) there is really no difference between createFlag 1 and 2. So
951** the calling function (pcache.c) will never have a createFlag of 1 on
drh45d29302012-01-08 22:18:33 +0000952** a non-purgeable cache.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000953**
954** There are three different approaches to obtaining space for a page,
955** depending on the value of parameter createFlag (which may be 0, 1 or 2).
956**
957** 1. Regardless of the value of createFlag, the cache is searched for a
958** copy of the requested page. If one is found, it is returned.
959**
960** 2. If createFlag==0 and the page is not already in the cache, NULL is
961** returned.
962**
drh50d1b5f2010-08-27 12:21:06 +0000963** 3. If createFlag is 1, and the page is not already in the cache, then
964** return NULL (do not allocate a new page) if any of the following
965** conditions are true:
danielk1977bc2ca9e2008-11-13 14:28:28 +0000966**
967** (a) the number of pages pinned by the cache is greater than
968** PCache1.nMax, or
drh50d1b5f2010-08-27 12:21:06 +0000969**
danielk1977bc2ca9e2008-11-13 14:28:28 +0000970** (b) the number of pages pinned by the cache is greater than
971** the sum of nMax for all purgeable caches, less the sum of
drh50d1b5f2010-08-27 12:21:06 +0000972** nMin for all other purgeable caches, or
danielk1977bc2ca9e2008-11-13 14:28:28 +0000973**
974** 4. If none of the first three conditions apply and the cache is marked
975** as purgeable, and if one of the following is true:
976**
977** (a) The number of pages allocated for the cache is already
978** PCache1.nMax, or
979**
980** (b) The number of pages allocated for all purgeable caches is
981** already equal to or greater than the sum of nMax for all
982** purgeable caches,
983**
drh50d1b5f2010-08-27 12:21:06 +0000984** (c) The system is under memory pressure and wants to avoid
985** unnecessary pages cache entry allocations
986**
danielk1977bc2ca9e2008-11-13 14:28:28 +0000987** then attempt to recycle a page from the LRU list. If it is the right
988** size, return the recycled buffer. Otherwise, free the buffer and
989** proceed to step 5.
990**
991** 5. Otherwise, allocate and return a new page buffer.
drh55a46c92015-06-12 13:49:26 +0000992**
993** There are two versions of this routine. pcache1FetchWithMutex() is
994** the general case. pcache1FetchNoMutex() is a faster implementation for
995** the common case where pGroup->mutex is NULL. The pcache1Fetch() wrapper
996** invokes the appropriate routine.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000997*/
drh55a46c92015-06-12 13:49:26 +0000998static PgHdr1 *pcache1FetchNoMutex(
dan22e21ff2011-11-08 20:08:44 +0000999 sqlite3_pcache *p,
1000 unsigned int iKey,
1001 int createFlag
1002){
danielk1977bc2ca9e2008-11-13 14:28:28 +00001003 PCache1 *pCache = (PCache1 *)p;
1004 PgHdr1 *pPage = 0;
1005
drh3a5676c2011-01-19 21:58:56 +00001006 /* Step 1: Search the hash table for an existing entry. */
drhefbf0442014-08-23 23:15:31 +00001007 pPage = pCache->apHash[iKey % pCache->nHash];
1008 while( pPage && pPage->iKey!=iKey ){ pPage = pPage->pNext; }
danielk1977bc2ca9e2008-11-13 14:28:28 +00001009
drh95a0b372015-09-03 20:43:55 +00001010 /* Step 2: If the page was found in the hash table, then return it.
1011 ** If the page was not in the hash table and createFlag is 0, abort.
1012 ** Otherwise (page not in hash and createFlag!=0) continue with
1013 ** subsequent steps to try to create the page. */
drh5d56dd22013-12-13 18:50:40 +00001014 if( pPage ){
drheabb67f2017-08-05 15:49:03 +00001015 if( PAGE_IS_UNPINNED(pPage) ){
drh55a46c92015-06-12 13:49:26 +00001016 return pcache1PinPage(pPage);
1017 }else{
1018 return pPage;
1019 }
drhefbf0442014-08-23 23:15:31 +00001020 }else if( createFlag ){
1021 /* Steps 3, 4, and 5 implemented by this subroutine */
drh55a46c92015-06-12 13:49:26 +00001022 return pcache1FetchStage2(pCache, iKey, createFlag);
1023 }else{
1024 return 0;
drh5d56dd22013-12-13 18:50:40 +00001025 }
drh55a46c92015-06-12 13:49:26 +00001026}
drh982215a2015-06-13 11:10:55 +00001027#if PCACHE1_MIGHT_USE_GROUP_MUTEX
drh55a46c92015-06-12 13:49:26 +00001028static PgHdr1 *pcache1FetchWithMutex(
1029 sqlite3_pcache *p,
1030 unsigned int iKey,
1031 int createFlag
1032){
1033 PCache1 *pCache = (PCache1 *)p;
1034 PgHdr1 *pPage;
1035
1036 pcache1EnterMutex(pCache->pGroup);
1037 pPage = pcache1FetchNoMutex(p, iKey, createFlag);
drhefbf0442014-08-23 23:15:31 +00001038 assert( pPage==0 || pCache->iMaxKey>=iKey );
1039 pcache1LeaveMutex(pCache->pGroup);
drh55a46c92015-06-12 13:49:26 +00001040 return pPage;
1041}
drh982215a2015-06-13 11:10:55 +00001042#endif
drh55a46c92015-06-12 13:49:26 +00001043static sqlite3_pcache_page *pcache1Fetch(
1044 sqlite3_pcache *p,
1045 unsigned int iKey,
1046 int createFlag
1047){
drh982215a2015-06-13 11:10:55 +00001048#if PCACHE1_MIGHT_USE_GROUP_MUTEX || defined(SQLITE_DEBUG)
drh55a46c92015-06-12 13:49:26 +00001049 PCache1 *pCache = (PCache1 *)p;
drh982215a2015-06-13 11:10:55 +00001050#endif
drh55a46c92015-06-12 13:49:26 +00001051
1052 assert( offsetof(PgHdr1,page)==0 );
1053 assert( pCache->bPurgeable || createFlag!=1 );
1054 assert( pCache->bPurgeable || pCache->nMin==0 );
1055 assert( pCache->bPurgeable==0 || pCache->nMin==10 );
1056 assert( pCache->nMin==0 || pCache->bPurgeable );
1057 assert( pCache->nHash>0 );
drh982215a2015-06-13 11:10:55 +00001058#if PCACHE1_MIGHT_USE_GROUP_MUTEX
drh55a46c92015-06-12 13:49:26 +00001059 if( pCache->pGroup->mutex ){
1060 return (sqlite3_pcache_page*)pcache1FetchWithMutex(p, iKey, createFlag);
drh982215a2015-06-13 11:10:55 +00001061 }else
1062#endif
1063 {
drh55a46c92015-06-12 13:49:26 +00001064 return (sqlite3_pcache_page*)pcache1FetchNoMutex(p, iKey, createFlag);
1065 }
danielk1977bc2ca9e2008-11-13 14:28:28 +00001066}
1067
1068
1069/*
1070** Implementation of the sqlite3_pcache.xUnpin method.
1071**
1072** Mark a page as unpinned (eligible for asynchronous recycling).
1073*/
dan22e21ff2011-11-08 20:08:44 +00001074static void pcache1Unpin(
1075 sqlite3_pcache *p,
1076 sqlite3_pcache_page *pPg,
1077 int reuseUnlikely
1078){
danielk1977bc2ca9e2008-11-13 14:28:28 +00001079 PCache1 *pCache = (PCache1 *)p;
dan22e21ff2011-11-08 20:08:44 +00001080 PgHdr1 *pPage = (PgHdr1 *)pPg;
drh9f8cf9d2011-01-17 21:32:24 +00001081 PGroup *pGroup = pCache->pGroup;
drh69e931e2009-06-03 21:04:35 +00001082
1083 assert( pPage->pCache==pCache );
drh9f8cf9d2011-01-17 21:32:24 +00001084 pcache1EnterMutex(pGroup);
danielk1977bc2ca9e2008-11-13 14:28:28 +00001085
1086 /* It is an error to call this function if the page is already
drh9f8cf9d2011-01-17 21:32:24 +00001087 ** part of the PGroup LRU list.
danielk1977bc2ca9e2008-11-13 14:28:28 +00001088 */
drhde72d2a2018-12-03 01:58:02 +00001089 assert( pPage->pLruNext==0 );
drheabb67f2017-08-05 15:49:03 +00001090 assert( PAGE_IS_PINNED(pPage) );
danielk1977bc2ca9e2008-11-13 14:28:28 +00001091
drh617b7b42017-08-30 04:44:59 +00001092 if( reuseUnlikely || pGroup->nPurgeable>pGroup->nMaxPage ){
drh95c91e12015-06-29 00:21:00 +00001093 pcache1RemoveFromHash(pPage, 1);
danielk1977bc2ca9e2008-11-13 14:28:28 +00001094 }else{
drh9f8cf9d2011-01-17 21:32:24 +00001095 /* Add the page to the PGroup LRU list. */
drh92af02c2015-09-04 04:31:56 +00001096 PgHdr1 **ppFirst = &pGroup->lru.pLruNext;
1097 pPage->pLruPrev = &pGroup->lru;
1098 (pPage->pLruNext = *ppFirst)->pLruPrev = pPage;
1099 *ppFirst = pPage;
danielk1977bc2ca9e2008-11-13 14:28:28 +00001100 pCache->nRecyclable++;
1101 }
1102
drh9f8cf9d2011-01-17 21:32:24 +00001103 pcache1LeaveMutex(pCache->pGroup);
danielk1977bc2ca9e2008-11-13 14:28:28 +00001104}
1105
1106/*
1107** Implementation of the sqlite3_pcache.xRekey method.
1108*/
1109static void pcache1Rekey(
1110 sqlite3_pcache *p,
dan22e21ff2011-11-08 20:08:44 +00001111 sqlite3_pcache_page *pPg,
danielk1977bc2ca9e2008-11-13 14:28:28 +00001112 unsigned int iOld,
1113 unsigned int iNew
1114){
1115 PCache1 *pCache = (PCache1 *)p;
dan22e21ff2011-11-08 20:08:44 +00001116 PgHdr1 *pPage = (PgHdr1 *)pPg;
danielk1977bc2ca9e2008-11-13 14:28:28 +00001117 PgHdr1 **pp;
1118 unsigned int h;
1119 assert( pPage->iKey==iOld );
drh69e931e2009-06-03 21:04:35 +00001120 assert( pPage->pCache==pCache );
danielk1977bc2ca9e2008-11-13 14:28:28 +00001121
drh9f8cf9d2011-01-17 21:32:24 +00001122 pcache1EnterMutex(pCache->pGroup);
danielk1977bc2ca9e2008-11-13 14:28:28 +00001123
1124 h = iOld%pCache->nHash;
1125 pp = &pCache->apHash[h];
1126 while( (*pp)!=pPage ){
1127 pp = &(*pp)->pNext;
1128 }
1129 *pp = pPage->pNext;
1130
1131 h = iNew%pCache->nHash;
1132 pPage->iKey = iNew;
1133 pPage->pNext = pCache->apHash[h];
1134 pCache->apHash[h] = pPage;
drh98829a62009-11-20 13:18:14 +00001135 if( iNew>pCache->iMaxKey ){
danielk1977f90b7262009-01-07 15:18:20 +00001136 pCache->iMaxKey = iNew;
1137 }
1138
drh9f8cf9d2011-01-17 21:32:24 +00001139 pcache1LeaveMutex(pCache->pGroup);
danielk1977bc2ca9e2008-11-13 14:28:28 +00001140}
1141
1142/*
1143** Implementation of the sqlite3_pcache.xTruncate method.
1144**
1145** Discard all unpinned pages in the cache with a page number equal to
1146** or greater than parameter iLimit. Any pinned pages with a page number
1147** equal to or greater than iLimit are implicitly unpinned.
1148*/
1149static void pcache1Truncate(sqlite3_pcache *p, unsigned int iLimit){
1150 PCache1 *pCache = (PCache1 *)p;
drh9f8cf9d2011-01-17 21:32:24 +00001151 pcache1EnterMutex(pCache->pGroup);
danielk1977f90b7262009-01-07 15:18:20 +00001152 if( iLimit<=pCache->iMaxKey ){
1153 pcache1TruncateUnsafe(pCache, iLimit);
1154 pCache->iMaxKey = iLimit-1;
1155 }
drh9f8cf9d2011-01-17 21:32:24 +00001156 pcache1LeaveMutex(pCache->pGroup);
danielk1977bc2ca9e2008-11-13 14:28:28 +00001157}
1158
1159/*
1160** Implementation of the sqlite3_pcache.xDestroy method.
1161**
1162** Destroy a cache allocated using pcache1Create().
1163*/
1164static void pcache1Destroy(sqlite3_pcache *p){
1165 PCache1 *pCache = (PCache1 *)p;
drh9f8cf9d2011-01-17 21:32:24 +00001166 PGroup *pGroup = pCache->pGroup;
danb51d2fa2010-09-22 19:06:02 +00001167 assert( pCache->bPurgeable || (pCache->nMax==0 && pCache->nMin==0) );
drh9f8cf9d2011-01-17 21:32:24 +00001168 pcache1EnterMutex(pGroup);
drhd9fabbc2016-08-10 11:50:12 +00001169 if( pCache->nPage ) pcache1TruncateUnsafe(pCache, 0);
drha69085c2012-01-02 18:00:55 +00001170 assert( pGroup->nMaxPage >= pCache->nMax );
drh9f8cf9d2011-01-17 21:32:24 +00001171 pGroup->nMaxPage -= pCache->nMax;
drha69085c2012-01-02 18:00:55 +00001172 assert( pGroup->nMinPage >= pCache->nMin );
drh9f8cf9d2011-01-17 21:32:24 +00001173 pGroup->nMinPage -= pCache->nMin;
drh41692e92011-01-25 04:34:51 +00001174 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
drh957026a2015-07-16 18:18:19 +00001175 pcache1EnforceMaxPage(pCache);
drh9f8cf9d2011-01-17 21:32:24 +00001176 pcache1LeaveMutex(pGroup);
drhee70a842015-07-06 18:54:52 +00001177 sqlite3_free(pCache->pBulk);
danielk1977bc2ca9e2008-11-13 14:28:28 +00001178 sqlite3_free(pCache->apHash);
1179 sqlite3_free(pCache);
1180}
1181
1182/*
1183** This function is called during initialization (sqlite3_initialize()) to
1184** install the default pluggable cache module, assuming the user has not
1185** already provided an alternative.
1186*/
1187void sqlite3PCacheSetDefault(void){
dan22e21ff2011-11-08 20:08:44 +00001188 static const sqlite3_pcache_methods2 defaultMethods = {
drh81ef0f92011-11-13 21:44:03 +00001189 1, /* iVersion */
danielk1977bc2ca9e2008-11-13 14:28:28 +00001190 0, /* pArg */
1191 pcache1Init, /* xInit */
1192 pcache1Shutdown, /* xShutdown */
1193 pcache1Create, /* xCreate */
1194 pcache1Cachesize, /* xCachesize */
1195 pcache1Pagecount, /* xPagecount */
1196 pcache1Fetch, /* xFetch */
1197 pcache1Unpin, /* xUnpin */
1198 pcache1Rekey, /* xRekey */
1199 pcache1Truncate, /* xTruncate */
drh09419b42011-11-16 19:29:17 +00001200 pcache1Destroy, /* xDestroy */
1201 pcache1Shrink /* xShrink */
danielk1977bc2ca9e2008-11-13 14:28:28 +00001202 };
dan22e21ff2011-11-08 20:08:44 +00001203 sqlite3_config(SQLITE_CONFIG_PCACHE2, &defaultMethods);
danielk1977bc2ca9e2008-11-13 14:28:28 +00001204}
1205
drhdef68892014-11-04 12:11:23 +00001206/*
1207** Return the size of the header on each page of this PCACHE implementation.
1208*/
drh37c057b2014-12-30 00:57:29 +00001209int sqlite3HeaderSizePcache1(void){ return ROUND8(sizeof(PgHdr1)); }
drhdef68892014-11-04 12:11:23 +00001210
drhaf89fe62015-03-23 17:25:18 +00001211/*
1212** Return the global mutex used by this PCACHE implementation. The
1213** sqlite3_status() routine needs access to this mutex.
1214*/
1215sqlite3_mutex *sqlite3Pcache1Mutex(void){
1216 return pcache1.mutex;
1217}
1218
danielk1977bc2ca9e2008-11-13 14:28:28 +00001219#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
1220/*
1221** This function is called to free superfluous dynamically allocated memory
1222** held by the pager system. Memory in use by any SQLite pager allocated
1223** by the current thread may be sqlite3_free()ed.
1224**
1225** nReq is the number of bytes of memory required. Once this much has
1226** been released, the function returns. The return value is the total number
1227** of bytes of memory released.
1228*/
1229int sqlite3PcacheReleaseMemory(int nReq){
1230 int nFree = 0;
drh9f8cf9d2011-01-17 21:32:24 +00001231 assert( sqlite3_mutex_notheld(pcache1.grp.mutex) );
1232 assert( sqlite3_mutex_notheld(pcache1.mutex) );
drhbf962282017-03-29 15:18:40 +00001233 if( sqlite3GlobalConfig.pPage==0 ){
danielk1977bc2ca9e2008-11-13 14:28:28 +00001234 PgHdr1 *p;
drh9f8cf9d2011-01-17 21:32:24 +00001235 pcache1EnterMutex(&pcache1.grp);
drh92af02c2015-09-04 04:31:56 +00001236 while( (nReq<0 || nFree<nReq)
drh88202502015-09-09 19:27:10 +00001237 && (p=pcache1.grp.lru.pLruPrev)!=0
1238 && p->isAnchor==0
drh92af02c2015-09-04 04:31:56 +00001239 ){
dan22e21ff2011-11-08 20:08:44 +00001240 nFree += pcache1MemSize(p->page.pBuf);
1241#ifdef SQLITE_PCACHE_SEPARATE_HEADER
1242 nFree += sqlite3MemSize(p);
1243#endif
drheabb67f2017-08-05 15:49:03 +00001244 assert( PAGE_IS_UNPINNED(p) );
danielk1977bc2ca9e2008-11-13 14:28:28 +00001245 pcache1PinPage(p);
drh95c91e12015-06-29 00:21:00 +00001246 pcache1RemoveFromHash(p, 1);
danielk1977bc2ca9e2008-11-13 14:28:28 +00001247 }
drh9f8cf9d2011-01-17 21:32:24 +00001248 pcache1LeaveMutex(&pcache1.grp);
danielk1977bc2ca9e2008-11-13 14:28:28 +00001249 }
1250 return nFree;
1251}
1252#endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
1253
1254#ifdef SQLITE_TEST
1255/*
1256** This function is used by test procedures to inspect the internal state
1257** of the global cache.
1258*/
1259void sqlite3PcacheStats(
1260 int *pnCurrent, /* OUT: Total number of pages cached */
1261 int *pnMax, /* OUT: Global maximum cache size */
1262 int *pnMin, /* OUT: Sum of PCache1.nMin for purgeable caches */
1263 int *pnRecyclable /* OUT: Total number of pages available for recycling */
1264){
1265 PgHdr1 *p;
1266 int nRecyclable = 0;
drh0b19c962015-09-10 19:22:25 +00001267 for(p=pcache1.grp.lru.pLruNext; p && !p->isAnchor; p=p->pLruNext){
drheabb67f2017-08-05 15:49:03 +00001268 assert( PAGE_IS_UNPINNED(p) );
danielk1977bc2ca9e2008-11-13 14:28:28 +00001269 nRecyclable++;
1270 }
drh617b7b42017-08-30 04:44:59 +00001271 *pnCurrent = pcache1.grp.nPurgeable;
drha69085c2012-01-02 18:00:55 +00001272 *pnMax = (int)pcache1.grp.nMaxPage;
1273 *pnMin = (int)pcache1.grp.nMinPage;
danielk1977bc2ca9e2008-11-13 14:28:28 +00001274 *pnRecyclable = nRecyclable;
1275}
1276#endif