blob: 9c59574ace715fa6324c0692286c11943c3be896 [file] [log] [blame]
danielk1977bc2ca9e2008-11-13 14:28:28 +00001/*
2** 2008 November 05
3**
4** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
6**
7** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
10**
11*************************************************************************
12**
13** This file implements the default page cache implementation (the
14** sqlite3_pcache interface). It also contains part of the implementation
15** of the SQLITE_CONFIG_PAGECACHE and sqlite3_release_memory() features.
peter.d.reid60ec9142014-09-06 16:39:46 +000016** If the default page cache implementation is overridden, then neither of
danielk1977bc2ca9e2008-11-13 14:28:28 +000017** these two features are available.
drh01c5c002015-07-04 18:15:04 +000018**
19** A Page cache line looks like this:
20**
21** -------------------------------------------------------------
22** | database page content | PgHdr1 | MemPage | PgHdr |
23** -------------------------------------------------------------
24**
25** The database page content is up front (so that buffer overreads tend to
26** flow harmlessly into the PgHdr1, MemPage, and PgHdr extensions). MemPage
27** is the extension added by the btree.c module containing information such
28** as the database page number and how that database page is used. PgHdr
29** is added by the pcache.c layer and contains information used to keep track
30** of which pages are "dirty". PgHdr1 is an extension added by this
31** module (pcache1.c). The PgHdr1 header is a subclass of sqlite3_pcache_page.
32** PgHdr1 contains information needed to look up a page by its page number.
33** The superclass sqlite3_pcache_page.pBuf points to the start of the
34** database page content and sqlite3_pcache_page.pExtra points to PgHdr.
35**
36** The size of the extension (MemPage+PgHdr+PgHdr1) can be determined at
37** runtime using sqlite3_config(SQLITE_CONFIG_PCACHE_HDRSZ, &size). The
38** sizes of the extensions sum to 272 bytes on x64 for 3.8.10, but this
39** size can vary according to architecture, compile-time options, and
40** SQLite library version number.
41**
42** If SQLITE_PCACHE_SEPARATE_HEADER is defined, then the extension is obtained
43** using a separate memory allocation from the database page content. This
drh7b341e62015-07-08 14:13:44 +000044** seeks to overcome the "clownshoe" problem (also called "internal
45** fragmentation" in academic literature) of allocating a few bytes more
drh01c5c002015-07-04 18:15:04 +000046** than a power of two with the memory allocator rounding up to the next
47** power of two, and leaving the rounded-up space unused.
48**
49** This module tracks pointers to PgHdr1 objects. Only pcache.c communicates
50** with this module. Information is passed back and forth as PgHdr1 pointers.
51**
52** The pcache.c and pager.c modules deal pointers to PgHdr objects.
53** The btree.c module deals with pointers to MemPage objects.
drhee70a842015-07-06 18:54:52 +000054**
55** SOURCE OF PAGE CACHE MEMORY:
56**
57** Memory for a page might come from any of three sources:
58**
59** (1) The general-purpose memory allocator - sqlite3Malloc()
60** (2) Global page-cache memory provided using sqlite3_config() with
61** SQLITE_CONFIG_PAGECACHE.
62** (3) PCache-local bulk allocation.
63**
64** The third case is a chunk of heap memory (defaulting to 100 pages worth)
65** that is allocated when the page cache is created. The size of the local
66** bulk allocation can be adjusted using
67**
drha6082f62015-11-26 22:12:41 +000068** sqlite3_config(SQLITE_CONFIG_PAGECACHE, (void*)0, 0, N).
drhee70a842015-07-06 18:54:52 +000069**
70** If N is positive, then N pages worth of memory are allocated using a single
71** sqlite3Malloc() call and that memory is used for the first N pages allocated.
72** Or if N is negative, then -1024*N bytes of memory are allocated and used
73** for as many pages as can be accomodated.
74**
75** Only one of (2) or (3) can be used. Once the memory available to (2) or
76** (3) is exhausted, subsequent allocations fail over to the general-purpose
77** memory allocator (1).
78**
79** Earlier versions of SQLite used only methods (1) and (2). But experiments
80** show that method (3) with N==100 provides about a 5% performance boost for
81** common workloads.
danielk1977bc2ca9e2008-11-13 14:28:28 +000082*/
danielk1977bc2ca9e2008-11-13 14:28:28 +000083#include "sqliteInt.h"
84
85typedef struct PCache1 PCache1;
86typedef struct PgHdr1 PgHdr1;
87typedef struct PgFreeslot PgFreeslot;
drh9f8cf9d2011-01-17 21:32:24 +000088typedef struct PGroup PGroup;
89
drh92af02c2015-09-04 04:31:56 +000090/*
91** Each cache entry is represented by an instance of the following
92** structure. Unless SQLITE_PCACHE_SEPARATE_HEADER is defined, a buffer of
93** PgHdr1.pCache->szPage bytes is allocated directly before this structure
94** in memory.
95*/
96struct PgHdr1 {
97 sqlite3_pcache_page page; /* Base class. Must be first. pBuf & pExtra */
98 unsigned int iKey; /* Key value (page number) */
drh92af02c2015-09-04 04:31:56 +000099 u8 isBulkLocal; /* This page from bulk local storage */
100 u8 isAnchor; /* This is the PGroup.lru element */
101 PgHdr1 *pNext; /* Next in hash table chain */
102 PCache1 *pCache; /* Cache that currently owns this page */
103 PgHdr1 *pLruNext; /* Next in LRU list of unpinned pages */
104 PgHdr1 *pLruPrev; /* Previous in LRU list of unpinned pages */
105};
106
drheabb67f2017-08-05 15:49:03 +0000107/*
108** A page is pinned if it is no on the LRU list
109*/
110#define PAGE_IS_PINNED(p) ((p)->pLruNext==0)
111#define PAGE_IS_UNPINNED(p) ((p)->pLruNext!=0)
112
drh9f8cf9d2011-01-17 21:32:24 +0000113/* Each page cache (or PCache) belongs to a PGroup. A PGroup is a set
peter.d.reid60ec9142014-09-06 16:39:46 +0000114** of one or more PCaches that are able to recycle each other's unpinned
drh9f8cf9d2011-01-17 21:32:24 +0000115** pages when they are under memory pressure. A PGroup is an instance of
116** the following object.
117**
118** This page cache implementation works in one of two modes:
119**
120** (1) Every PCache is the sole member of its own PGroup. There is
121** one PGroup per PCache.
122**
123** (2) There is a single global PGroup that all PCaches are a member
124** of.
125**
126** Mode 1 uses more memory (since PCache instances are not able to rob
127** unused pages from other PCaches) but it also operates without a mutex,
128** and is therefore often faster. Mode 2 requires a mutex in order to be
drh45d29302012-01-08 22:18:33 +0000129** threadsafe, but recycles pages more efficiently.
drh9f8cf9d2011-01-17 21:32:24 +0000130**
131** For mode (1), PGroup.mutex is NULL. For mode (2) there is only a single
132** PGroup which is the pcache1.grp global variable and its mutex is
133** SQLITE_MUTEX_STATIC_LRU.
134*/
135struct PGroup {
136 sqlite3_mutex *mutex; /* MUTEX_STATIC_LRU or NULL */
drha69085c2012-01-02 18:00:55 +0000137 unsigned int nMaxPage; /* Sum of nMax for purgeable caches */
138 unsigned int nMinPage; /* Sum of nMin for purgeable caches */
139 unsigned int mxPinned; /* nMaxpage + 10 - nMinPage */
140 unsigned int nCurrentPage; /* Number of purgeable pages allocated */
drh92af02c2015-09-04 04:31:56 +0000141 PgHdr1 lru; /* The beginning and end of the LRU list */
drh9f8cf9d2011-01-17 21:32:24 +0000142};
danielk1977bc2ca9e2008-11-13 14:28:28 +0000143
drh9d13f112010-08-24 18:06:35 +0000144/* Each page cache is an instance of the following object. Every
145** open database file (including each in-memory database and each
146** temporary or transient database) has a single page cache which
147** is an instance of this object.
148**
149** Pointers to structures of this type are cast and returned as
150** opaque sqlite3_pcache* handles.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000151*/
152struct PCache1 {
153 /* Cache configuration parameters. Page size (szPage) and the purgeable
154 ** flag (bPurgeable) are set when the cache is created. nMax may be
drh45d29302012-01-08 22:18:33 +0000155 ** modified at any time by a call to the pcache1Cachesize() method.
drh9f8cf9d2011-01-17 21:32:24 +0000156 ** The PGroup mutex must be held when accessing nMax.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000157 */
drh9f8cf9d2011-01-17 21:32:24 +0000158 PGroup *pGroup; /* PGroup this cache belongs to */
drhee70a842015-07-06 18:54:52 +0000159 int szPage; /* Size of database content section */
160 int szExtra; /* sizeof(MemPage)+sizeof(PgHdr) */
161 int szAlloc; /* Total size of one pcache line */
danielk1977bc2ca9e2008-11-13 14:28:28 +0000162 int bPurgeable; /* True if cache is purgeable */
danielk197744cd45c2008-11-15 11:22:45 +0000163 unsigned int nMin; /* Minimum number of pages reserved */
164 unsigned int nMax; /* Configured "cache_size" value */
drh25ca5682011-01-26 00:07:03 +0000165 unsigned int n90pct; /* nMax*9/10 */
drh2cbd78b2012-02-02 19:37:18 +0000166 unsigned int iMaxKey; /* Largest key seen since xTruncate() */
danielk1977bc2ca9e2008-11-13 14:28:28 +0000167
168 /* Hash table of all pages. The following variables may only be accessed
drh9f8cf9d2011-01-17 21:32:24 +0000169 ** when the accessor is holding the PGroup mutex.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000170 */
danielk197744cd45c2008-11-15 11:22:45 +0000171 unsigned int nRecyclable; /* Number of pages in the LRU list */
172 unsigned int nPage; /* Total number of pages in apHash */
173 unsigned int nHash; /* Number of slots in apHash[] */
danielk1977bc2ca9e2008-11-13 14:28:28 +0000174 PgHdr1 **apHash; /* Hash table for fast lookup by key */
drhee70a842015-07-06 18:54:52 +0000175 PgHdr1 *pFree; /* List of unused pcache-local pages */
176 void *pBulk; /* Bulk memory used by pcache-local */
danielk1977bc2ca9e2008-11-13 14:28:28 +0000177};
178
179/*
drhee70a842015-07-06 18:54:52 +0000180** Free slots in the allocator used to divide up the global page cache
181** buffer provided using the SQLITE_CONFIG_PAGECACHE mechanism.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000182*/
183struct PgFreeslot {
184 PgFreeslot *pNext; /* Next free slot */
185};
186
187/*
188** Global data used by this cache.
189*/
190static SQLITE_WSD struct PCacheGlobal {
drh9f8cf9d2011-01-17 21:32:24 +0000191 PGroup grp; /* The global PGroup for mode (2) */
danielk1977bc2ca9e2008-11-13 14:28:28 +0000192
drh9f8cf9d2011-01-17 21:32:24 +0000193 /* Variables related to SQLITE_CONFIG_PAGECACHE settings. The
194 ** szSlot, nSlot, pStart, pEnd, nReserve, and isInit values are all
195 ** fixed at sqlite3_initialize() time and do not require mutex protection.
196 ** The nFreeSlot and pFree values do require mutex protection.
197 */
198 int isInit; /* True if initialized */
drhdb7ae892015-07-06 20:57:22 +0000199 int separateCache; /* Use a new PGroup for each PCache */
drh957026a2015-07-16 18:18:19 +0000200 int nInitPage; /* Initial bulk allocation size */
drh9f8cf9d2011-01-17 21:32:24 +0000201 int szSlot; /* Size of each free slot */
202 int nSlot; /* The number of pcache slots */
203 int nReserve; /* Try to keep nFreeSlot above this */
drhee70a842015-07-06 18:54:52 +0000204 void *pStart, *pEnd; /* Bounds of global page cache memory */
drh9f8cf9d2011-01-17 21:32:24 +0000205 /* Above requires no mutex. Use mutex below for variable that follow. */
206 sqlite3_mutex *mutex; /* Mutex for accessing the following: */
drh9f8cf9d2011-01-17 21:32:24 +0000207 PgFreeslot *pFree; /* Free page blocks */
drh2cbd78b2012-02-02 19:37:18 +0000208 int nFreeSlot; /* Number of unused pcache slots */
drh9f8cf9d2011-01-17 21:32:24 +0000209 /* The following value requires a mutex to change. We skip the mutex on
210 ** reading because (1) most platforms read a 32-bit integer atomically and
211 ** (2) even if an incorrect value is read, no great harm is done since this
212 ** is really just an optimization. */
213 int bUnderPressure; /* True if low on PAGECACHE memory */
danielk197744cd45c2008-11-15 11:22:45 +0000214} pcache1_g;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000215
216/*
217** All code in this file should access the global structure above via the
218** alias "pcache1". This ensures that the WSD emulation is used when
219** compiling for systems that do not support real WSD.
220*/
221#define pcache1 (GLOBAL(struct PCacheGlobal, pcache1_g))
222
223/*
drh9f8cf9d2011-01-17 21:32:24 +0000224** Macros to enter and leave the PCache LRU mutex.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000225*/
drh982215a2015-06-13 11:10:55 +0000226#if !defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) || SQLITE_THREADSAFE==0
227# define pcache1EnterMutex(X) assert((X)->mutex==0)
228# define pcache1LeaveMutex(X) assert((X)->mutex==0)
229# define PCACHE1_MIGHT_USE_GROUP_MUTEX 0
230#else
231# define pcache1EnterMutex(X) sqlite3_mutex_enter((X)->mutex)
232# define pcache1LeaveMutex(X) sqlite3_mutex_leave((X)->mutex)
233# define PCACHE1_MIGHT_USE_GROUP_MUTEX 1
234#endif
danielk1977bc2ca9e2008-11-13 14:28:28 +0000235
236/******************************************************************************/
237/******** Page Allocation/SQLITE_CONFIG_PCACHE Related Functions **************/
238
drh92af02c2015-09-04 04:31:56 +0000239
danielk1977bc2ca9e2008-11-13 14:28:28 +0000240/*
241** This function is called during initialization if a static buffer is
242** supplied to use for the page-cache by passing the SQLITE_CONFIG_PAGECACHE
243** verb to sqlite3_config(). Parameter pBuf points to an allocation large
244** enough to contain 'n' buffers of 'sz' bytes each.
drh9f8cf9d2011-01-17 21:32:24 +0000245**
246** This routine is called from sqlite3_initialize() and so it is guaranteed
247** to be serialized already. There is no need for further mutexing.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000248*/
249void sqlite3PCacheBufferSetup(void *pBuf, int sz, int n){
drhf4622dc2009-05-22 11:10:24 +0000250 if( pcache1.isInit ){
251 PgFreeslot *p;
drhee70a842015-07-06 18:54:52 +0000252 if( pBuf==0 ) sz = n = 0;
drhf4622dc2009-05-22 11:10:24 +0000253 sz = ROUNDDOWN8(sz);
254 pcache1.szSlot = sz;
drh50d1b5f2010-08-27 12:21:06 +0000255 pcache1.nSlot = pcache1.nFreeSlot = n;
256 pcache1.nReserve = n>90 ? 10 : (n/10 + 1);
drhf4622dc2009-05-22 11:10:24 +0000257 pcache1.pStart = pBuf;
258 pcache1.pFree = 0;
drh9f8cf9d2011-01-17 21:32:24 +0000259 pcache1.bUnderPressure = 0;
drhf4622dc2009-05-22 11:10:24 +0000260 while( n-- ){
261 p = (PgFreeslot*)pBuf;
262 p->pNext = pcache1.pFree;
263 pcache1.pFree = p;
264 pBuf = (void*)&((char*)pBuf)[sz];
265 }
266 pcache1.pEnd = pBuf;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000267 }
danielk1977bc2ca9e2008-11-13 14:28:28 +0000268}
269
270/*
drh957026a2015-07-16 18:18:19 +0000271** Try to initialize the pCache->pFree and pCache->pBulk fields. Return
272** true if pCache->pFree ends up containing one or more free pages.
273*/
274static int pcache1InitBulk(PCache1 *pCache){
drh939d4bc2015-07-16 18:37:53 +0000275 i64 szBulk;
drh957026a2015-07-16 18:18:19 +0000276 char *zBulk;
277 if( pcache1.nInitPage==0 ) return 0;
278 /* Do not bother with a bulk allocation if the cache size very small */
279 if( pCache->nMax<3 ) return 0;
280 sqlite3BeginBenignMalloc();
281 if( pcache1.nInitPage>0 ){
drh939d4bc2015-07-16 18:37:53 +0000282 szBulk = pCache->szAlloc * (i64)pcache1.nInitPage;
drh957026a2015-07-16 18:18:19 +0000283 }else{
drh939d4bc2015-07-16 18:37:53 +0000284 szBulk = -1024 * (i64)pcache1.nInitPage;
drh957026a2015-07-16 18:18:19 +0000285 }
drh939d4bc2015-07-16 18:37:53 +0000286 if( szBulk > pCache->szAlloc*(i64)pCache->nMax ){
drh989412a2016-10-13 12:56:18 +0000287 szBulk = pCache->szAlloc*(i64)pCache->nMax;
drh957026a2015-07-16 18:18:19 +0000288 }
289 zBulk = pCache->pBulk = sqlite3Malloc( szBulk );
290 sqlite3EndBenignMalloc();
291 if( zBulk ){
292 int nBulk = sqlite3MallocSize(zBulk)/pCache->szAlloc;
drh4eb8d7f2017-03-29 17:06:14 +0000293 do{
drh957026a2015-07-16 18:18:19 +0000294 PgHdr1 *pX = (PgHdr1*)&zBulk[pCache->szPage];
295 pX->page.pBuf = zBulk;
296 pX->page.pExtra = &pX[1];
297 pX->isBulkLocal = 1;
drh92af02c2015-09-04 04:31:56 +0000298 pX->isAnchor = 0;
drh957026a2015-07-16 18:18:19 +0000299 pX->pNext = pCache->pFree;
300 pCache->pFree = pX;
301 zBulk += pCache->szAlloc;
drh4eb8d7f2017-03-29 17:06:14 +0000302 }while( --nBulk );
drh957026a2015-07-16 18:18:19 +0000303 }
304 return pCache->pFree!=0;
305}
306
307/*
danielk1977bc2ca9e2008-11-13 14:28:28 +0000308** Malloc function used within this file to allocate space from the buffer
309** configured using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no
310** such buffer exists or there is no space left in it, this function falls
311** back to sqlite3Malloc().
drh9f8cf9d2011-01-17 21:32:24 +0000312**
313** Multiple threads can run this routine at the same time. Global variables
314** in pcache1 need to be protected via mutex.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000315*/
316static void *pcache1Alloc(int nByte){
drh9f8cf9d2011-01-17 21:32:24 +0000317 void *p = 0;
318 assert( sqlite3_mutex_notheld(pcache1.grp.mutex) );
drh9f8cf9d2011-01-17 21:32:24 +0000319 if( nByte<=pcache1.szSlot ){
320 sqlite3_mutex_enter(pcache1.mutex);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000321 p = (PgHdr1 *)pcache1.pFree;
drh9f8cf9d2011-01-17 21:32:24 +0000322 if( p ){
323 pcache1.pFree = pcache1.pFree->pNext;
324 pcache1.nFreeSlot--;
325 pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve;
326 assert( pcache1.nFreeSlot>=0 );
drhb02392e2015-10-15 15:28:56 +0000327 sqlite3StatusHighwater(SQLITE_STATUS_PAGECACHE_SIZE, nByte);
drhaf89fe62015-03-23 17:25:18 +0000328 sqlite3StatusUp(SQLITE_STATUS_PAGECACHE_USED, 1);
drh9f8cf9d2011-01-17 21:32:24 +0000329 }
330 sqlite3_mutex_leave(pcache1.mutex);
331 }
332 if( p==0 ){
333 /* Memory is not available in the SQLITE_CONFIG_PAGECACHE pool. Get
334 ** it from sqlite3Malloc instead.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000335 */
danielk1977bc2ca9e2008-11-13 14:28:28 +0000336 p = sqlite3Malloc(nByte);
drh4bd69522012-06-07 02:35:29 +0000337#ifndef SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS
danielk1977bc2ca9e2008-11-13 14:28:28 +0000338 if( p ){
339 int sz = sqlite3MallocSize(p);
drh9bf3da8e2011-01-26 13:24:40 +0000340 sqlite3_mutex_enter(pcache1.mutex);
drhb02392e2015-10-15 15:28:56 +0000341 sqlite3StatusHighwater(SQLITE_STATUS_PAGECACHE_SIZE, nByte);
drhaf89fe62015-03-23 17:25:18 +0000342 sqlite3StatusUp(SQLITE_STATUS_PAGECACHE_OVERFLOW, sz);
drh9bf3da8e2011-01-26 13:24:40 +0000343 sqlite3_mutex_leave(pcache1.mutex);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000344 }
drh4bd69522012-06-07 02:35:29 +0000345#endif
drh107b56e2010-03-12 16:32:53 +0000346 sqlite3MemdebugSetType(p, MEMTYPE_PCACHE);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000347 }
348 return p;
349}
350
351/*
352** Free an allocated buffer obtained from pcache1Alloc().
353*/
drhee70a842015-07-06 18:54:52 +0000354static void pcache1Free(void *p){
drhee70a842015-07-06 18:54:52 +0000355 if( p==0 ) return;
drh8b0ba7b2015-12-16 13:07:35 +0000356 if( SQLITE_WITHIN(p, pcache1.pStart, pcache1.pEnd) ){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000357 PgFreeslot *pSlot;
drh9f8cf9d2011-01-17 21:32:24 +0000358 sqlite3_mutex_enter(pcache1.mutex);
drhaf89fe62015-03-23 17:25:18 +0000359 sqlite3StatusDown(SQLITE_STATUS_PAGECACHE_USED, 1);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000360 pSlot = (PgFreeslot*)p;
361 pSlot->pNext = pcache1.pFree;
362 pcache1.pFree = pSlot;
drh50d1b5f2010-08-27 12:21:06 +0000363 pcache1.nFreeSlot++;
drh9f8cf9d2011-01-17 21:32:24 +0000364 pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve;
drh50d1b5f2010-08-27 12:21:06 +0000365 assert( pcache1.nFreeSlot<=pcache1.nSlot );
drh9f8cf9d2011-01-17 21:32:24 +0000366 sqlite3_mutex_leave(pcache1.mutex);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000367 }else{
drh107b56e2010-03-12 16:32:53 +0000368 assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) );
369 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
drh4bd69522012-06-07 02:35:29 +0000370#ifndef SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS
drh9493caf2016-03-17 23:16:37 +0000371 {
372 int nFreed = 0;
373 nFreed = sqlite3MallocSize(p);
374 sqlite3_mutex_enter(pcache1.mutex);
375 sqlite3StatusDown(SQLITE_STATUS_PAGECACHE_OVERFLOW, nFreed);
376 sqlite3_mutex_leave(pcache1.mutex);
377 }
drh4bd69522012-06-07 02:35:29 +0000378#endif
danielk1977bc2ca9e2008-11-13 14:28:28 +0000379 sqlite3_free(p);
380 }
381}
382
drhc8f503a2010-08-20 09:14:13 +0000383#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
384/*
drh9d13f112010-08-24 18:06:35 +0000385** Return the size of a pcache allocation
drhc8f503a2010-08-20 09:14:13 +0000386*/
387static int pcache1MemSize(void *p){
drhc8f503a2010-08-20 09:14:13 +0000388 if( p>=pcache1.pStart && p<pcache1.pEnd ){
389 return pcache1.szSlot;
390 }else{
391 int iSize;
392 assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) );
393 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
394 iSize = sqlite3MallocSize(p);
395 sqlite3MemdebugSetType(p, MEMTYPE_PCACHE);
396 return iSize;
397 }
398}
399#endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
400
dand2925702011-08-19 18:15:00 +0000401/*
danielk1977bc2ca9e2008-11-13 14:28:28 +0000402** Allocate a new page object initially associated with cache pCache.
403*/
drh3c0c4312015-09-01 19:51:37 +0000404static PgHdr1 *pcache1AllocPage(PCache1 *pCache, int benignMalloc){
danb5126dd2011-09-22 14:56:31 +0000405 PgHdr1 *p = 0;
406 void *pPg;
dand2925702011-08-19 18:15:00 +0000407
dand2925702011-08-19 18:15:00 +0000408 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
drh957026a2015-07-16 18:18:19 +0000409 if( pCache->pFree || (pCache->nPage==0 && pcache1InitBulk(pCache)) ){
drhee70a842015-07-06 18:54:52 +0000410 p = pCache->pFree;
411 pCache->pFree = p->pNext;
412 p->pNext = 0;
413 }else{
drhdb7ae892015-07-06 20:57:22 +0000414#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
drhee70a842015-07-06 18:54:52 +0000415 /* The group mutex must be released before pcache1Alloc() is called. This
drhdb7ae892015-07-06 20:57:22 +0000416 ** is because it might call sqlite3_release_memory(), which assumes that
drhee70a842015-07-06 18:54:52 +0000417 ** this mutex is not held. */
drhdb7ae892015-07-06 20:57:22 +0000418 assert( pcache1.separateCache==0 );
419 assert( pCache->pGroup==&pcache1.grp );
drhee70a842015-07-06 18:54:52 +0000420 pcache1LeaveMutex(pCache->pGroup);
drhdb7ae892015-07-06 20:57:22 +0000421#endif
drh8faee872015-09-19 18:08:13 +0000422 if( benignMalloc ){ sqlite3BeginBenignMalloc(); }
dan22e21ff2011-11-08 20:08:44 +0000423#ifdef SQLITE_PCACHE_SEPARATE_HEADER
drhee70a842015-07-06 18:54:52 +0000424 pPg = pcache1Alloc(pCache->szPage);
425 p = sqlite3Malloc(sizeof(PgHdr1) + pCache->szExtra);
426 if( !pPg || !p ){
427 pcache1Free(pPg);
428 sqlite3_free(p);
429 pPg = 0;
430 }
dan22e21ff2011-11-08 20:08:44 +0000431#else
drhee70a842015-07-06 18:54:52 +0000432 pPg = pcache1Alloc(pCache->szAlloc);
433 p = (PgHdr1 *)&((u8 *)pPg)[pCache->szPage];
dan22e21ff2011-11-08 20:08:44 +0000434#endif
drh8faee872015-09-19 18:08:13 +0000435 if( benignMalloc ){ sqlite3EndBenignMalloc(); }
drhdb7ae892015-07-06 20:57:22 +0000436#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
drhee70a842015-07-06 18:54:52 +0000437 pcache1EnterMutex(pCache->pGroup);
drhdb7ae892015-07-06 20:57:22 +0000438#endif
drhee70a842015-07-06 18:54:52 +0000439 if( pPg==0 ) return 0;
dan22e21ff2011-11-08 20:08:44 +0000440 p->page.pBuf = pPg;
441 p->page.pExtra = &p[1];
drhee70a842015-07-06 18:54:52 +0000442 p->isBulkLocal = 0;
drh92af02c2015-09-04 04:31:56 +0000443 p->isAnchor = 0;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000444 }
drhee70a842015-07-06 18:54:52 +0000445 if( pCache->bPurgeable ){
446 pCache->pGroup->nCurrentPage++;
447 }
448 return p;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000449}
450
451/*
452** Free a page object allocated by pcache1AllocPage().
453*/
454static void pcache1FreePage(PgHdr1 *p){
drhdb7ae892015-07-06 20:57:22 +0000455 PCache1 *pCache;
456 assert( p!=0 );
457 pCache = p->pCache;
458 assert( sqlite3_mutex_held(p->pCache->pGroup->mutex) );
459 if( p->isBulkLocal ){
460 p->pNext = pCache->pFree;
461 pCache->pFree = p;
462 }else{
463 pcache1Free(p->page.pBuf);
dan22e21ff2011-11-08 20:08:44 +0000464#ifdef SQLITE_PCACHE_SEPARATE_HEADER
drhdb7ae892015-07-06 20:57:22 +0000465 sqlite3_free(p);
dan22e21ff2011-11-08 20:08:44 +0000466#endif
drhdb7ae892015-07-06 20:57:22 +0000467 }
468 if( pCache->bPurgeable ){
469 pCache->pGroup->nCurrentPage--;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000470 }
471}
472
473/*
474** Malloc function used by SQLite to obtain space from the buffer configured
475** using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no such buffer
476** exists, this function falls back to sqlite3Malloc().
477*/
478void *sqlite3PageMalloc(int sz){
drh9f8cf9d2011-01-17 21:32:24 +0000479 return pcache1Alloc(sz);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000480}
481
482/*
483** Free an allocated buffer obtained from sqlite3PageMalloc().
484*/
485void sqlite3PageFree(void *p){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000486 pcache1Free(p);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000487}
488
drh50d1b5f2010-08-27 12:21:06 +0000489
490/*
491** Return true if it desirable to avoid allocating a new page cache
492** entry.
493**
494** If memory was allocated specifically to the page cache using
495** SQLITE_CONFIG_PAGECACHE but that memory has all been used, then
496** it is desirable to avoid allocating a new page cache entry because
497** presumably SQLITE_CONFIG_PAGECACHE was suppose to be sufficient
498** for all page cache needs and we should not need to spill the
499** allocation onto the heap.
500**
drh45d29302012-01-08 22:18:33 +0000501** Or, the heap is used for all page cache memory but the heap is
drh50d1b5f2010-08-27 12:21:06 +0000502** under memory pressure, then again it is desirable to avoid
503** allocating a new page cache entry in order to avoid stressing
504** the heap even further.
505*/
506static int pcache1UnderMemoryPressure(PCache1 *pCache){
dan22e21ff2011-11-08 20:08:44 +0000507 if( pcache1.nSlot && (pCache->szPage+pCache->szExtra)<=pcache1.szSlot ){
drh9f8cf9d2011-01-17 21:32:24 +0000508 return pcache1.bUnderPressure;
drh50d1b5f2010-08-27 12:21:06 +0000509 }else{
510 return sqlite3HeapNearlyFull();
511 }
512}
513
danielk1977bc2ca9e2008-11-13 14:28:28 +0000514/******************************************************************************/
515/******** General Implementation Functions ************************************/
516
517/*
518** This function is used to resize the hash table used by the cache passed
519** as the first argument.
520**
drh9f8cf9d2011-01-17 21:32:24 +0000521** The PCache mutex must be held when this function is called.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000522*/
drhefbf0442014-08-23 23:15:31 +0000523static void pcache1ResizeHash(PCache1 *p){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000524 PgHdr1 **apNew;
danielk197744cd45c2008-11-15 11:22:45 +0000525 unsigned int nNew;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000526 unsigned int i;
527
drh9f8cf9d2011-01-17 21:32:24 +0000528 assert( sqlite3_mutex_held(p->pGroup->mutex) );
danielk1977bc2ca9e2008-11-13 14:28:28 +0000529
530 nNew = p->nHash*2;
531 if( nNew<256 ){
532 nNew = 256;
533 }
534
drh9f8cf9d2011-01-17 21:32:24 +0000535 pcache1LeaveMutex(p->pGroup);
drh085bb7f2008-12-06 14:34:33 +0000536 if( p->nHash ){ sqlite3BeginBenignMalloc(); }
dan6809c962012-07-30 14:53:54 +0000537 apNew = (PgHdr1 **)sqlite3MallocZero(sizeof(PgHdr1 *)*nNew);
drh085bb7f2008-12-06 14:34:33 +0000538 if( p->nHash ){ sqlite3EndBenignMalloc(); }
drh9f8cf9d2011-01-17 21:32:24 +0000539 pcache1EnterMutex(p->pGroup);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000540 if( apNew ){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000541 for(i=0; i<p->nHash; i++){
542 PgHdr1 *pPage;
543 PgHdr1 *pNext = p->apHash[i];
drhb27b7f52008-12-10 18:03:45 +0000544 while( (pPage = pNext)!=0 ){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000545 unsigned int h = pPage->iKey % nNew;
546 pNext = pPage->pNext;
547 pPage->pNext = apNew[h];
548 apNew[h] = pPage;
549 }
550 }
551 sqlite3_free(p->apHash);
552 p->apHash = apNew;
553 p->nHash = nNew;
554 }
danielk1977bc2ca9e2008-11-13 14:28:28 +0000555}
556
557/*
558** This function is used internally to remove the page pPage from the
drh9f8cf9d2011-01-17 21:32:24 +0000559** PGroup LRU list, if is part of it. If pPage is not part of the PGroup
danielk1977bc2ca9e2008-11-13 14:28:28 +0000560** LRU list, then this function is a no-op.
561**
drh9f8cf9d2011-01-17 21:32:24 +0000562** The PGroup mutex must be held when this function is called.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000563*/
drh55a46c92015-06-12 13:49:26 +0000564static PgHdr1 *pcache1PinPage(PgHdr1 *pPage){
drh5d56dd22013-12-13 18:50:40 +0000565 assert( pPage!=0 );
drheabb67f2017-08-05 15:49:03 +0000566 assert( PAGE_IS_UNPINNED(pPage) );
drh92af02c2015-09-04 04:31:56 +0000567 assert( pPage->pLruNext );
568 assert( pPage->pLruPrev );
drheabb67f2017-08-05 15:49:03 +0000569 assert( sqlite3_mutex_held(pPage->pCache->pGroup->mutex) );
drh92af02c2015-09-04 04:31:56 +0000570 pPage->pLruPrev->pLruNext = pPage->pLruNext;
571 pPage->pLruNext->pLruPrev = pPage->pLruPrev;
drh5d56dd22013-12-13 18:50:40 +0000572 pPage->pLruNext = 0;
573 pPage->pLruPrev = 0;
drh92af02c2015-09-04 04:31:56 +0000574 assert( pPage->isAnchor==0 );
drheabb67f2017-08-05 15:49:03 +0000575 assert( pPage->pCache->pGroup->lru.isAnchor==1 );
576 pPage->pCache->nRecyclable--;
drh55a46c92015-06-12 13:49:26 +0000577 return pPage;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000578}
579
580
581/*
582** Remove the page supplied as an argument from the hash table
583** (PCache1.apHash structure) that it is currently stored in.
drh95c91e12015-06-29 00:21:00 +0000584** Also free the page if freePage is true.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000585**
drh9f8cf9d2011-01-17 21:32:24 +0000586** The PGroup mutex must be held when this function is called.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000587*/
drh95c91e12015-06-29 00:21:00 +0000588static void pcache1RemoveFromHash(PgHdr1 *pPage, int freeFlag){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000589 unsigned int h;
590 PCache1 *pCache = pPage->pCache;
591 PgHdr1 **pp;
592
drh9f8cf9d2011-01-17 21:32:24 +0000593 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
danielk1977bc2ca9e2008-11-13 14:28:28 +0000594 h = pPage->iKey % pCache->nHash;
595 for(pp=&pCache->apHash[h]; (*pp)!=pPage; pp=&(*pp)->pNext);
596 *pp = (*pp)->pNext;
597
598 pCache->nPage--;
drh95c91e12015-06-29 00:21:00 +0000599 if( freeFlag ) pcache1FreePage(pPage);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000600}
601
602/*
drh9f8cf9d2011-01-17 21:32:24 +0000603** If there are currently more than nMaxPage pages allocated, try
604** to recycle pages to reduce the number allocated to nMaxPage.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000605*/
drh957026a2015-07-16 18:18:19 +0000606static void pcache1EnforceMaxPage(PCache1 *pCache){
607 PGroup *pGroup = pCache->pGroup;
drh92af02c2015-09-04 04:31:56 +0000608 PgHdr1 *p;
drh9f8cf9d2011-01-17 21:32:24 +0000609 assert( sqlite3_mutex_held(pGroup->mutex) );
drh92af02c2015-09-04 04:31:56 +0000610 while( pGroup->nCurrentPage>pGroup->nMaxPage
611 && (p=pGroup->lru.pLruPrev)->isAnchor==0
612 ){
drh9f8cf9d2011-01-17 21:32:24 +0000613 assert( p->pCache->pGroup==pGroup );
drheabb67f2017-08-05 15:49:03 +0000614 assert( PAGE_IS_UNPINNED(p) );
danielk1977bc2ca9e2008-11-13 14:28:28 +0000615 pcache1PinPage(p);
drh95c91e12015-06-29 00:21:00 +0000616 pcache1RemoveFromHash(p, 1);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000617 }
drh957026a2015-07-16 18:18:19 +0000618 if( pCache->nPage==0 && pCache->pBulk ){
619 sqlite3_free(pCache->pBulk);
620 pCache->pBulk = pCache->pFree = 0;
621 }
danielk1977bc2ca9e2008-11-13 14:28:28 +0000622}
623
624/*
625** Discard all pages from cache pCache with a page number (key value)
626** greater than or equal to iLimit. Any pinned pages that meet this
627** criteria are unpinned before they are discarded.
628**
drh9f8cf9d2011-01-17 21:32:24 +0000629** The PCache mutex must be held when this function is called.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000630*/
631static void pcache1TruncateUnsafe(
drh9f8cf9d2011-01-17 21:32:24 +0000632 PCache1 *pCache, /* The cache to truncate */
633 unsigned int iLimit /* Drop pages with this pgno or larger */
danielk1977bc2ca9e2008-11-13 14:28:28 +0000634){
drhd9fabbc2016-08-10 11:50:12 +0000635 TESTONLY( int nPage = 0; ) /* To assert pCache->nPage is correct */
636 unsigned int h, iStop;
drh9f8cf9d2011-01-17 21:32:24 +0000637 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
drhd9fabbc2016-08-10 11:50:12 +0000638 assert( pCache->iMaxKey >= iLimit );
639 assert( pCache->nHash > 0 );
drhf5dbe7f2016-08-10 15:02:49 +0000640 if( pCache->iMaxKey - iLimit < pCache->nHash ){
drhd9fabbc2016-08-10 11:50:12 +0000641 /* If we are just shaving the last few pages off the end of the
642 ** cache, then there is no point in scanning the entire hash table.
643 ** Only scan those hash slots that might contain pages that need to
644 ** be removed. */
drhf5dbe7f2016-08-10 15:02:49 +0000645 h = iLimit % pCache->nHash;
646 iStop = pCache->iMaxKey % pCache->nHash;
drhd9fabbc2016-08-10 11:50:12 +0000647 TESTONLY( nPage = -10; ) /* Disable the pCache->nPage validity check */
648 }else{
649 /* This is the general case where many pages are being removed.
650 ** It is necessary to scan the entire hash table */
drhf5dbe7f2016-08-10 15:02:49 +0000651 h = pCache->nHash/2;
652 iStop = h - 1;
drhd9fabbc2016-08-10 11:50:12 +0000653 }
654 for(;;){
655 PgHdr1 **pp;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000656 PgHdr1 *pPage;
drhd9fabbc2016-08-10 11:50:12 +0000657 assert( h<pCache->nHash );
658 pp = &pCache->apHash[h];
drhb27b7f52008-12-10 18:03:45 +0000659 while( (pPage = *pp)!=0 ){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000660 if( pPage->iKey>=iLimit ){
danielk1977ea24ac42009-05-08 06:52:47 +0000661 pCache->nPage--;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000662 *pp = pPage->pNext;
drheabb67f2017-08-05 15:49:03 +0000663 if( PAGE_IS_UNPINNED(pPage) ) pcache1PinPage(pPage);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000664 pcache1FreePage(pPage);
665 }else{
666 pp = &pPage->pNext;
drhd9fabbc2016-08-10 11:50:12 +0000667 TESTONLY( if( nPage>=0 ) nPage++; )
danielk1977bc2ca9e2008-11-13 14:28:28 +0000668 }
669 }
drhd9fabbc2016-08-10 11:50:12 +0000670 if( h==iStop ) break;
drhf5dbe7f2016-08-10 15:02:49 +0000671 h = (h+1) % pCache->nHash;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000672 }
drhd9fabbc2016-08-10 11:50:12 +0000673 assert( nPage<0 || pCache->nPage==(unsigned)nPage );
danielk1977bc2ca9e2008-11-13 14:28:28 +0000674}
675
676/******************************************************************************/
677/******** sqlite3_pcache Methods **********************************************/
678
679/*
680** Implementation of the sqlite3_pcache.xInit method.
681*/
danielk197762c14b32008-11-19 09:05:26 +0000682static int pcache1Init(void *NotUsed){
683 UNUSED_PARAMETER(NotUsed);
drhf4622dc2009-05-22 11:10:24 +0000684 assert( pcache1.isInit==0 );
danielk1977bc2ca9e2008-11-13 14:28:28 +0000685 memset(&pcache1, 0, sizeof(pcache1));
drhdb7ae892015-07-06 20:57:22 +0000686
687
688 /*
689 ** The pcache1.separateCache variable is true if each PCache has its own
690 ** private PGroup (mode-1). pcache1.separateCache is false if the single
691 ** PGroup in pcache1.grp is used for all page caches (mode-2).
692 **
693 ** * Always use a unified cache (mode-2) if ENABLE_MEMORY_MANAGEMENT
694 **
695 ** * Use a unified cache in single-threaded applications that have
696 ** configured a start-time buffer for use as page-cache memory using
697 ** sqlite3_config(SQLITE_CONFIG_PAGECACHE, pBuf, sz, N) with non-NULL
698 ** pBuf argument.
699 **
700 ** * Otherwise use separate caches (mode-1)
701 */
702#if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT)
703 pcache1.separateCache = 0;
drhfd5ae962015-07-07 15:14:16 +0000704#elif SQLITE_THREADSAFE
drhdb7ae892015-07-06 20:57:22 +0000705 pcache1.separateCache = sqlite3GlobalConfig.pPage==0
706 || sqlite3GlobalConfig.bCoreMutex>0;
drhfd5ae962015-07-07 15:14:16 +0000707#else
708 pcache1.separateCache = sqlite3GlobalConfig.pPage==0;
drhdb7ae892015-07-06 20:57:22 +0000709#endif
710
drh982215a2015-06-13 11:10:55 +0000711#if SQLITE_THREADSAFE
danielk1977bc2ca9e2008-11-13 14:28:28 +0000712 if( sqlite3GlobalConfig.bCoreMutex ){
drh97a7e5e2016-04-26 18:58:54 +0000713 pcache1.grp.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU);
714 pcache1.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_PMEM);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000715 }
drh982215a2015-06-13 11:10:55 +0000716#endif
drh957026a2015-07-16 18:18:19 +0000717 if( pcache1.separateCache
718 && sqlite3GlobalConfig.nPage!=0
719 && sqlite3GlobalConfig.pPage==0
720 ){
721 pcache1.nInitPage = sqlite3GlobalConfig.nPage;
722 }else{
723 pcache1.nInitPage = 0;
724 }
drh41692e92011-01-25 04:34:51 +0000725 pcache1.grp.mxPinned = 10;
drhf4622dc2009-05-22 11:10:24 +0000726 pcache1.isInit = 1;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000727 return SQLITE_OK;
728}
729
730/*
731** Implementation of the sqlite3_pcache.xShutdown method.
shane7c7c3112009-08-17 15:31:23 +0000732** Note that the static mutex allocated in xInit does
733** not need to be freed.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000734*/
danielk197762c14b32008-11-19 09:05:26 +0000735static void pcache1Shutdown(void *NotUsed){
736 UNUSED_PARAMETER(NotUsed);
drhf4622dc2009-05-22 11:10:24 +0000737 assert( pcache1.isInit!=0 );
drhb0937192009-05-22 10:53:29 +0000738 memset(&pcache1, 0, sizeof(pcache1));
danielk1977bc2ca9e2008-11-13 14:28:28 +0000739}
740
drhefbf0442014-08-23 23:15:31 +0000741/* forward declaration */
742static void pcache1Destroy(sqlite3_pcache *p);
743
danielk1977bc2ca9e2008-11-13 14:28:28 +0000744/*
745** Implementation of the sqlite3_pcache.xCreate method.
746**
747** Allocate a new cache.
748*/
drhe5c40b12011-11-09 00:06:05 +0000749static sqlite3_pcache *pcache1Create(int szPage, int szExtra, int bPurgeable){
drh9f8cf9d2011-01-17 21:32:24 +0000750 PCache1 *pCache; /* The newly created page cache */
751 PGroup *pGroup; /* The group the new page cache will belong to */
752 int sz; /* Bytes of memory required to allocate the new cache */
danielk1977bc2ca9e2008-11-13 14:28:28 +0000753
drhe73c9142011-11-09 16:12:24 +0000754 assert( (szPage & (szPage-1))==0 && szPage>=512 && szPage<=65536 );
755 assert( szExtra < 300 );
756
drhdb7ae892015-07-06 20:57:22 +0000757 sz = sizeof(PCache1) + sizeof(PGroup)*pcache1.separateCache;
dan6809c962012-07-30 14:53:54 +0000758 pCache = (PCache1 *)sqlite3MallocZero(sz);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000759 if( pCache ){
drhdb7ae892015-07-06 20:57:22 +0000760 if( pcache1.separateCache ){
drh9f8cf9d2011-01-17 21:32:24 +0000761 pGroup = (PGroup*)&pCache[1];
drh41692e92011-01-25 04:34:51 +0000762 pGroup->mxPinned = 10;
drh9f8cf9d2011-01-17 21:32:24 +0000763 }else{
dan9dde7cb2011-06-09 17:53:43 +0000764 pGroup = &pcache1.grp;
drh9f8cf9d2011-01-17 21:32:24 +0000765 }
drh92af02c2015-09-04 04:31:56 +0000766 if( pGroup->lru.isAnchor==0 ){
767 pGroup->lru.isAnchor = 1;
768 pGroup->lru.pLruPrev = pGroup->lru.pLruNext = &pGroup->lru;
769 }
drh9f8cf9d2011-01-17 21:32:24 +0000770 pCache->pGroup = pGroup;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000771 pCache->szPage = szPage;
dan22e21ff2011-11-08 20:08:44 +0000772 pCache->szExtra = szExtra;
drhee70a842015-07-06 18:54:52 +0000773 pCache->szAlloc = szPage + szExtra + ROUND8(sizeof(PgHdr1));
danielk1977bc2ca9e2008-11-13 14:28:28 +0000774 pCache->bPurgeable = (bPurgeable ? 1 : 0);
drhefbf0442014-08-23 23:15:31 +0000775 pcache1EnterMutex(pGroup);
776 pcache1ResizeHash(pCache);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000777 if( bPurgeable ){
778 pCache->nMin = 10;
drh9f8cf9d2011-01-17 21:32:24 +0000779 pGroup->nMinPage += pCache->nMin;
drh41692e92011-01-25 04:34:51 +0000780 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
drhefbf0442014-08-23 23:15:31 +0000781 }
782 pcache1LeaveMutex(pGroup);
783 if( pCache->nHash==0 ){
784 pcache1Destroy((sqlite3_pcache*)pCache);
785 pCache = 0;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000786 }
787 }
788 return (sqlite3_pcache *)pCache;
789}
790
791/*
792** Implementation of the sqlite3_pcache.xCachesize method.
793**
794** Configure the cache_size limit for a cache.
795*/
796static void pcache1Cachesize(sqlite3_pcache *p, int nMax){
797 PCache1 *pCache = (PCache1 *)p;
798 if( pCache->bPurgeable ){
drh9f8cf9d2011-01-17 21:32:24 +0000799 PGroup *pGroup = pCache->pGroup;
800 pcache1EnterMutex(pGroup);
801 pGroup->nMaxPage += (nMax - pCache->nMax);
drh41692e92011-01-25 04:34:51 +0000802 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000803 pCache->nMax = nMax;
drh25ca5682011-01-26 00:07:03 +0000804 pCache->n90pct = pCache->nMax*9/10;
drh957026a2015-07-16 18:18:19 +0000805 pcache1EnforceMaxPage(pCache);
drh9f8cf9d2011-01-17 21:32:24 +0000806 pcache1LeaveMutex(pGroup);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000807 }
808}
809
810/*
drh09419b42011-11-16 19:29:17 +0000811** Implementation of the sqlite3_pcache.xShrink method.
812**
813** Free up as much memory as possible.
814*/
815static void pcache1Shrink(sqlite3_pcache *p){
816 PCache1 *pCache = (PCache1*)p;
817 if( pCache->bPurgeable ){
818 PGroup *pGroup = pCache->pGroup;
819 int savedMaxPage;
820 pcache1EnterMutex(pGroup);
821 savedMaxPage = pGroup->nMaxPage;
822 pGroup->nMaxPage = 0;
drh957026a2015-07-16 18:18:19 +0000823 pcache1EnforceMaxPage(pCache);
drh09419b42011-11-16 19:29:17 +0000824 pGroup->nMaxPage = savedMaxPage;
825 pcache1LeaveMutex(pGroup);
826 }
827}
828
829/*
danielk1977bc2ca9e2008-11-13 14:28:28 +0000830** Implementation of the sqlite3_pcache.xPagecount method.
831*/
832static int pcache1Pagecount(sqlite3_pcache *p){
833 int n;
drh9f8cf9d2011-01-17 21:32:24 +0000834 PCache1 *pCache = (PCache1*)p;
835 pcache1EnterMutex(pCache->pGroup);
836 n = pCache->nPage;
837 pcache1LeaveMutex(pCache->pGroup);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000838 return n;
839}
840
drhefbf0442014-08-23 23:15:31 +0000841
842/*
843** Implement steps 3, 4, and 5 of the pcache1Fetch() algorithm described
844** in the header of the pcache1Fetch() procedure.
845**
846** This steps are broken out into a separate procedure because they are
847** usually not needed, and by avoiding the stack initialization required
848** for these steps, the main pcache1Fetch() procedure can run faster.
849*/
850static SQLITE_NOINLINE PgHdr1 *pcache1FetchStage2(
851 PCache1 *pCache,
852 unsigned int iKey,
853 int createFlag
854){
855 unsigned int nPinned;
856 PGroup *pGroup = pCache->pGroup;
857 PgHdr1 *pPage = 0;
858
859 /* Step 3: Abort if createFlag is 1 but the cache is nearly full */
860 assert( pCache->nPage >= pCache->nRecyclable );
861 nPinned = pCache->nPage - pCache->nRecyclable;
862 assert( pGroup->mxPinned == pGroup->nMaxPage + 10 - pGroup->nMinPage );
863 assert( pCache->n90pct == pCache->nMax*9/10 );
864 if( createFlag==1 && (
865 nPinned>=pGroup->mxPinned
866 || nPinned>=pCache->n90pct
dan5bd8af72014-10-10 19:10:59 +0000867 || (pcache1UnderMemoryPressure(pCache) && pCache->nRecyclable<nPinned)
drhefbf0442014-08-23 23:15:31 +0000868 )){
869 return 0;
870 }
871
872 if( pCache->nPage>=pCache->nHash ) pcache1ResizeHash(pCache);
873 assert( pCache->nHash>0 && pCache->apHash );
874
875 /* Step 4. Try to recycle a page. */
drhc54357c2015-07-07 14:06:18 +0000876 if( pCache->bPurgeable
drh92af02c2015-09-04 04:31:56 +0000877 && !pGroup->lru.pLruPrev->isAnchor
drhc54357c2015-07-07 14:06:18 +0000878 && ((pCache->nPage+1>=pCache->nMax) || pcache1UnderMemoryPressure(pCache))
879 ){
drhefbf0442014-08-23 23:15:31 +0000880 PCache1 *pOther;
drh92af02c2015-09-04 04:31:56 +0000881 pPage = pGroup->lru.pLruPrev;
drheabb67f2017-08-05 15:49:03 +0000882 assert( PAGE_IS_UNPINNED(pPage) );
drh95c91e12015-06-29 00:21:00 +0000883 pcache1RemoveFromHash(pPage, 0);
drhefbf0442014-08-23 23:15:31 +0000884 pcache1PinPage(pPage);
885 pOther = pPage->pCache;
drhee70a842015-07-06 18:54:52 +0000886 if( pOther->szAlloc != pCache->szAlloc ){
drhefbf0442014-08-23 23:15:31 +0000887 pcache1FreePage(pPage);
888 pPage = 0;
889 }else{
890 pGroup->nCurrentPage -= (pOther->bPurgeable - pCache->bPurgeable);
891 }
892 }
893
894 /* Step 5. If a usable page buffer has still not been found,
895 ** attempt to allocate a new one.
896 */
897 if( !pPage ){
drh3c0c4312015-09-01 19:51:37 +0000898 pPage = pcache1AllocPage(pCache, createFlag==1);
drhefbf0442014-08-23 23:15:31 +0000899 }
900
901 if( pPage ){
902 unsigned int h = iKey % pCache->nHash;
903 pCache->nPage++;
904 pPage->iKey = iKey;
905 pPage->pNext = pCache->apHash[h];
906 pPage->pCache = pCache;
907 pPage->pLruPrev = 0;
908 pPage->pLruNext = 0;
drhefbf0442014-08-23 23:15:31 +0000909 *(void **)pPage->page.pExtra = 0;
910 pCache->apHash[h] = pPage;
911 if( iKey>pCache->iMaxKey ){
912 pCache->iMaxKey = iKey;
913 }
914 }
915 return pPage;
916}
917
danielk1977bc2ca9e2008-11-13 14:28:28 +0000918/*
919** Implementation of the sqlite3_pcache.xFetch method.
920**
921** Fetch a page by key value.
922**
923** Whether or not a new page may be allocated by this function depends on
drhf18a61d2009-07-17 11:44:07 +0000924** the value of the createFlag argument. 0 means do not allocate a new
925** page. 1 means allocate a new page if space is easily available. 2
926** means to try really hard to allocate a new page.
927**
928** For a non-purgeable cache (a cache used as the storage for an in-memory
929** database) there is really no difference between createFlag 1 and 2. So
930** the calling function (pcache.c) will never have a createFlag of 1 on
drh45d29302012-01-08 22:18:33 +0000931** a non-purgeable cache.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000932**
933** There are three different approaches to obtaining space for a page,
934** depending on the value of parameter createFlag (which may be 0, 1 or 2).
935**
936** 1. Regardless of the value of createFlag, the cache is searched for a
937** copy of the requested page. If one is found, it is returned.
938**
939** 2. If createFlag==0 and the page is not already in the cache, NULL is
940** returned.
941**
drh50d1b5f2010-08-27 12:21:06 +0000942** 3. If createFlag is 1, and the page is not already in the cache, then
943** return NULL (do not allocate a new page) if any of the following
944** conditions are true:
danielk1977bc2ca9e2008-11-13 14:28:28 +0000945**
946** (a) the number of pages pinned by the cache is greater than
947** PCache1.nMax, or
drh50d1b5f2010-08-27 12:21:06 +0000948**
danielk1977bc2ca9e2008-11-13 14:28:28 +0000949** (b) the number of pages pinned by the cache is greater than
950** the sum of nMax for all purgeable caches, less the sum of
drh50d1b5f2010-08-27 12:21:06 +0000951** nMin for all other purgeable caches, or
danielk1977bc2ca9e2008-11-13 14:28:28 +0000952**
953** 4. If none of the first three conditions apply and the cache is marked
954** as purgeable, and if one of the following is true:
955**
956** (a) The number of pages allocated for the cache is already
957** PCache1.nMax, or
958**
959** (b) The number of pages allocated for all purgeable caches is
960** already equal to or greater than the sum of nMax for all
961** purgeable caches,
962**
drh50d1b5f2010-08-27 12:21:06 +0000963** (c) The system is under memory pressure and wants to avoid
964** unnecessary pages cache entry allocations
965**
danielk1977bc2ca9e2008-11-13 14:28:28 +0000966** then attempt to recycle a page from the LRU list. If it is the right
967** size, return the recycled buffer. Otherwise, free the buffer and
968** proceed to step 5.
969**
970** 5. Otherwise, allocate and return a new page buffer.
drh55a46c92015-06-12 13:49:26 +0000971**
972** There are two versions of this routine. pcache1FetchWithMutex() is
973** the general case. pcache1FetchNoMutex() is a faster implementation for
974** the common case where pGroup->mutex is NULL. The pcache1Fetch() wrapper
975** invokes the appropriate routine.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000976*/
drh55a46c92015-06-12 13:49:26 +0000977static PgHdr1 *pcache1FetchNoMutex(
dan22e21ff2011-11-08 20:08:44 +0000978 sqlite3_pcache *p,
979 unsigned int iKey,
980 int createFlag
981){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000982 PCache1 *pCache = (PCache1 *)p;
983 PgHdr1 *pPage = 0;
984
drh3a5676c2011-01-19 21:58:56 +0000985 /* Step 1: Search the hash table for an existing entry. */
drhefbf0442014-08-23 23:15:31 +0000986 pPage = pCache->apHash[iKey % pCache->nHash];
987 while( pPage && pPage->iKey!=iKey ){ pPage = pPage->pNext; }
danielk1977bc2ca9e2008-11-13 14:28:28 +0000988
drh95a0b372015-09-03 20:43:55 +0000989 /* Step 2: If the page was found in the hash table, then return it.
990 ** If the page was not in the hash table and createFlag is 0, abort.
991 ** Otherwise (page not in hash and createFlag!=0) continue with
992 ** subsequent steps to try to create the page. */
drh5d56dd22013-12-13 18:50:40 +0000993 if( pPage ){
drheabb67f2017-08-05 15:49:03 +0000994 if( PAGE_IS_UNPINNED(pPage) ){
drh55a46c92015-06-12 13:49:26 +0000995 return pcache1PinPage(pPage);
996 }else{
997 return pPage;
998 }
drhefbf0442014-08-23 23:15:31 +0000999 }else if( createFlag ){
1000 /* Steps 3, 4, and 5 implemented by this subroutine */
drh55a46c92015-06-12 13:49:26 +00001001 return pcache1FetchStage2(pCache, iKey, createFlag);
1002 }else{
1003 return 0;
drh5d56dd22013-12-13 18:50:40 +00001004 }
drh55a46c92015-06-12 13:49:26 +00001005}
drh982215a2015-06-13 11:10:55 +00001006#if PCACHE1_MIGHT_USE_GROUP_MUTEX
drh55a46c92015-06-12 13:49:26 +00001007static PgHdr1 *pcache1FetchWithMutex(
1008 sqlite3_pcache *p,
1009 unsigned int iKey,
1010 int createFlag
1011){
1012 PCache1 *pCache = (PCache1 *)p;
1013 PgHdr1 *pPage;
1014
1015 pcache1EnterMutex(pCache->pGroup);
1016 pPage = pcache1FetchNoMutex(p, iKey, createFlag);
drhefbf0442014-08-23 23:15:31 +00001017 assert( pPage==0 || pCache->iMaxKey>=iKey );
1018 pcache1LeaveMutex(pCache->pGroup);
drh55a46c92015-06-12 13:49:26 +00001019 return pPage;
1020}
drh982215a2015-06-13 11:10:55 +00001021#endif
drh55a46c92015-06-12 13:49:26 +00001022static sqlite3_pcache_page *pcache1Fetch(
1023 sqlite3_pcache *p,
1024 unsigned int iKey,
1025 int createFlag
1026){
drh982215a2015-06-13 11:10:55 +00001027#if PCACHE1_MIGHT_USE_GROUP_MUTEX || defined(SQLITE_DEBUG)
drh55a46c92015-06-12 13:49:26 +00001028 PCache1 *pCache = (PCache1 *)p;
drh982215a2015-06-13 11:10:55 +00001029#endif
drh55a46c92015-06-12 13:49:26 +00001030
1031 assert( offsetof(PgHdr1,page)==0 );
1032 assert( pCache->bPurgeable || createFlag!=1 );
1033 assert( pCache->bPurgeable || pCache->nMin==0 );
1034 assert( pCache->bPurgeable==0 || pCache->nMin==10 );
1035 assert( pCache->nMin==0 || pCache->bPurgeable );
1036 assert( pCache->nHash>0 );
drh982215a2015-06-13 11:10:55 +00001037#if PCACHE1_MIGHT_USE_GROUP_MUTEX
drh55a46c92015-06-12 13:49:26 +00001038 if( pCache->pGroup->mutex ){
1039 return (sqlite3_pcache_page*)pcache1FetchWithMutex(p, iKey, createFlag);
drh982215a2015-06-13 11:10:55 +00001040 }else
1041#endif
1042 {
drh55a46c92015-06-12 13:49:26 +00001043 return (sqlite3_pcache_page*)pcache1FetchNoMutex(p, iKey, createFlag);
1044 }
danielk1977bc2ca9e2008-11-13 14:28:28 +00001045}
1046
1047
1048/*
1049** Implementation of the sqlite3_pcache.xUnpin method.
1050**
1051** Mark a page as unpinned (eligible for asynchronous recycling).
1052*/
dan22e21ff2011-11-08 20:08:44 +00001053static void pcache1Unpin(
1054 sqlite3_pcache *p,
1055 sqlite3_pcache_page *pPg,
1056 int reuseUnlikely
1057){
danielk1977bc2ca9e2008-11-13 14:28:28 +00001058 PCache1 *pCache = (PCache1 *)p;
dan22e21ff2011-11-08 20:08:44 +00001059 PgHdr1 *pPage = (PgHdr1 *)pPg;
drh9f8cf9d2011-01-17 21:32:24 +00001060 PGroup *pGroup = pCache->pGroup;
drh69e931e2009-06-03 21:04:35 +00001061
1062 assert( pPage->pCache==pCache );
drh9f8cf9d2011-01-17 21:32:24 +00001063 pcache1EnterMutex(pGroup);
danielk1977bc2ca9e2008-11-13 14:28:28 +00001064
1065 /* It is an error to call this function if the page is already
drh9f8cf9d2011-01-17 21:32:24 +00001066 ** part of the PGroup LRU list.
danielk1977bc2ca9e2008-11-13 14:28:28 +00001067 */
1068 assert( pPage->pLruPrev==0 && pPage->pLruNext==0 );
drheabb67f2017-08-05 15:49:03 +00001069 assert( PAGE_IS_PINNED(pPage) );
danielk1977bc2ca9e2008-11-13 14:28:28 +00001070
drh9f8cf9d2011-01-17 21:32:24 +00001071 if( reuseUnlikely || pGroup->nCurrentPage>pGroup->nMaxPage ){
drh95c91e12015-06-29 00:21:00 +00001072 pcache1RemoveFromHash(pPage, 1);
danielk1977bc2ca9e2008-11-13 14:28:28 +00001073 }else{
drh9f8cf9d2011-01-17 21:32:24 +00001074 /* Add the page to the PGroup LRU list. */
drh92af02c2015-09-04 04:31:56 +00001075 PgHdr1 **ppFirst = &pGroup->lru.pLruNext;
1076 pPage->pLruPrev = &pGroup->lru;
1077 (pPage->pLruNext = *ppFirst)->pLruPrev = pPage;
1078 *ppFirst = pPage;
danielk1977bc2ca9e2008-11-13 14:28:28 +00001079 pCache->nRecyclable++;
1080 }
1081
drh9f8cf9d2011-01-17 21:32:24 +00001082 pcache1LeaveMutex(pCache->pGroup);
danielk1977bc2ca9e2008-11-13 14:28:28 +00001083}
1084
1085/*
1086** Implementation of the sqlite3_pcache.xRekey method.
1087*/
1088static void pcache1Rekey(
1089 sqlite3_pcache *p,
dan22e21ff2011-11-08 20:08:44 +00001090 sqlite3_pcache_page *pPg,
danielk1977bc2ca9e2008-11-13 14:28:28 +00001091 unsigned int iOld,
1092 unsigned int iNew
1093){
1094 PCache1 *pCache = (PCache1 *)p;
dan22e21ff2011-11-08 20:08:44 +00001095 PgHdr1 *pPage = (PgHdr1 *)pPg;
danielk1977bc2ca9e2008-11-13 14:28:28 +00001096 PgHdr1 **pp;
1097 unsigned int h;
1098 assert( pPage->iKey==iOld );
drh69e931e2009-06-03 21:04:35 +00001099 assert( pPage->pCache==pCache );
danielk1977bc2ca9e2008-11-13 14:28:28 +00001100
drh9f8cf9d2011-01-17 21:32:24 +00001101 pcache1EnterMutex(pCache->pGroup);
danielk1977bc2ca9e2008-11-13 14:28:28 +00001102
1103 h = iOld%pCache->nHash;
1104 pp = &pCache->apHash[h];
1105 while( (*pp)!=pPage ){
1106 pp = &(*pp)->pNext;
1107 }
1108 *pp = pPage->pNext;
1109
1110 h = iNew%pCache->nHash;
1111 pPage->iKey = iNew;
1112 pPage->pNext = pCache->apHash[h];
1113 pCache->apHash[h] = pPage;
drh98829a62009-11-20 13:18:14 +00001114 if( iNew>pCache->iMaxKey ){
danielk1977f90b7262009-01-07 15:18:20 +00001115 pCache->iMaxKey = iNew;
1116 }
1117
drh9f8cf9d2011-01-17 21:32:24 +00001118 pcache1LeaveMutex(pCache->pGroup);
danielk1977bc2ca9e2008-11-13 14:28:28 +00001119}
1120
1121/*
1122** Implementation of the sqlite3_pcache.xTruncate method.
1123**
1124** Discard all unpinned pages in the cache with a page number equal to
1125** or greater than parameter iLimit. Any pinned pages with a page number
1126** equal to or greater than iLimit are implicitly unpinned.
1127*/
1128static void pcache1Truncate(sqlite3_pcache *p, unsigned int iLimit){
1129 PCache1 *pCache = (PCache1 *)p;
drh9f8cf9d2011-01-17 21:32:24 +00001130 pcache1EnterMutex(pCache->pGroup);
danielk1977f90b7262009-01-07 15:18:20 +00001131 if( iLimit<=pCache->iMaxKey ){
1132 pcache1TruncateUnsafe(pCache, iLimit);
1133 pCache->iMaxKey = iLimit-1;
1134 }
drh9f8cf9d2011-01-17 21:32:24 +00001135 pcache1LeaveMutex(pCache->pGroup);
danielk1977bc2ca9e2008-11-13 14:28:28 +00001136}
1137
1138/*
1139** Implementation of the sqlite3_pcache.xDestroy method.
1140**
1141** Destroy a cache allocated using pcache1Create().
1142*/
1143static void pcache1Destroy(sqlite3_pcache *p){
1144 PCache1 *pCache = (PCache1 *)p;
drh9f8cf9d2011-01-17 21:32:24 +00001145 PGroup *pGroup = pCache->pGroup;
danb51d2fa2010-09-22 19:06:02 +00001146 assert( pCache->bPurgeable || (pCache->nMax==0 && pCache->nMin==0) );
drh9f8cf9d2011-01-17 21:32:24 +00001147 pcache1EnterMutex(pGroup);
drhd9fabbc2016-08-10 11:50:12 +00001148 if( pCache->nPage ) pcache1TruncateUnsafe(pCache, 0);
drha69085c2012-01-02 18:00:55 +00001149 assert( pGroup->nMaxPage >= pCache->nMax );
drh9f8cf9d2011-01-17 21:32:24 +00001150 pGroup->nMaxPage -= pCache->nMax;
drha69085c2012-01-02 18:00:55 +00001151 assert( pGroup->nMinPage >= pCache->nMin );
drh9f8cf9d2011-01-17 21:32:24 +00001152 pGroup->nMinPage -= pCache->nMin;
drh41692e92011-01-25 04:34:51 +00001153 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
drh957026a2015-07-16 18:18:19 +00001154 pcache1EnforceMaxPage(pCache);
drh9f8cf9d2011-01-17 21:32:24 +00001155 pcache1LeaveMutex(pGroup);
drhee70a842015-07-06 18:54:52 +00001156 sqlite3_free(pCache->pBulk);
danielk1977bc2ca9e2008-11-13 14:28:28 +00001157 sqlite3_free(pCache->apHash);
1158 sqlite3_free(pCache);
1159}
1160
1161/*
1162** This function is called during initialization (sqlite3_initialize()) to
1163** install the default pluggable cache module, assuming the user has not
1164** already provided an alternative.
1165*/
1166void sqlite3PCacheSetDefault(void){
dan22e21ff2011-11-08 20:08:44 +00001167 static const sqlite3_pcache_methods2 defaultMethods = {
drh81ef0f92011-11-13 21:44:03 +00001168 1, /* iVersion */
danielk1977bc2ca9e2008-11-13 14:28:28 +00001169 0, /* pArg */
1170 pcache1Init, /* xInit */
1171 pcache1Shutdown, /* xShutdown */
1172 pcache1Create, /* xCreate */
1173 pcache1Cachesize, /* xCachesize */
1174 pcache1Pagecount, /* xPagecount */
1175 pcache1Fetch, /* xFetch */
1176 pcache1Unpin, /* xUnpin */
1177 pcache1Rekey, /* xRekey */
1178 pcache1Truncate, /* xTruncate */
drh09419b42011-11-16 19:29:17 +00001179 pcache1Destroy, /* xDestroy */
1180 pcache1Shrink /* xShrink */
danielk1977bc2ca9e2008-11-13 14:28:28 +00001181 };
dan22e21ff2011-11-08 20:08:44 +00001182 sqlite3_config(SQLITE_CONFIG_PCACHE2, &defaultMethods);
danielk1977bc2ca9e2008-11-13 14:28:28 +00001183}
1184
drhdef68892014-11-04 12:11:23 +00001185/*
1186** Return the size of the header on each page of this PCACHE implementation.
1187*/
drh37c057b2014-12-30 00:57:29 +00001188int sqlite3HeaderSizePcache1(void){ return ROUND8(sizeof(PgHdr1)); }
drhdef68892014-11-04 12:11:23 +00001189
drhaf89fe62015-03-23 17:25:18 +00001190/*
1191** Return the global mutex used by this PCACHE implementation. The
1192** sqlite3_status() routine needs access to this mutex.
1193*/
1194sqlite3_mutex *sqlite3Pcache1Mutex(void){
1195 return pcache1.mutex;
1196}
1197
danielk1977bc2ca9e2008-11-13 14:28:28 +00001198#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
1199/*
1200** This function is called to free superfluous dynamically allocated memory
1201** held by the pager system. Memory in use by any SQLite pager allocated
1202** by the current thread may be sqlite3_free()ed.
1203**
1204** nReq is the number of bytes of memory required. Once this much has
1205** been released, the function returns. The return value is the total number
1206** of bytes of memory released.
1207*/
1208int sqlite3PcacheReleaseMemory(int nReq){
1209 int nFree = 0;
drh9f8cf9d2011-01-17 21:32:24 +00001210 assert( sqlite3_mutex_notheld(pcache1.grp.mutex) );
1211 assert( sqlite3_mutex_notheld(pcache1.mutex) );
drhbf962282017-03-29 15:18:40 +00001212 if( sqlite3GlobalConfig.pPage==0 ){
danielk1977bc2ca9e2008-11-13 14:28:28 +00001213 PgHdr1 *p;
drh9f8cf9d2011-01-17 21:32:24 +00001214 pcache1EnterMutex(&pcache1.grp);
drh92af02c2015-09-04 04:31:56 +00001215 while( (nReq<0 || nFree<nReq)
drh88202502015-09-09 19:27:10 +00001216 && (p=pcache1.grp.lru.pLruPrev)!=0
1217 && p->isAnchor==0
drh92af02c2015-09-04 04:31:56 +00001218 ){
dan22e21ff2011-11-08 20:08:44 +00001219 nFree += pcache1MemSize(p->page.pBuf);
1220#ifdef SQLITE_PCACHE_SEPARATE_HEADER
1221 nFree += sqlite3MemSize(p);
1222#endif
drheabb67f2017-08-05 15:49:03 +00001223 assert( PAGE_IS_UNPINNED(p) );
danielk1977bc2ca9e2008-11-13 14:28:28 +00001224 pcache1PinPage(p);
drh95c91e12015-06-29 00:21:00 +00001225 pcache1RemoveFromHash(p, 1);
danielk1977bc2ca9e2008-11-13 14:28:28 +00001226 }
drh9f8cf9d2011-01-17 21:32:24 +00001227 pcache1LeaveMutex(&pcache1.grp);
danielk1977bc2ca9e2008-11-13 14:28:28 +00001228 }
1229 return nFree;
1230}
1231#endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
1232
1233#ifdef SQLITE_TEST
1234/*
1235** This function is used by test procedures to inspect the internal state
1236** of the global cache.
1237*/
1238void sqlite3PcacheStats(
1239 int *pnCurrent, /* OUT: Total number of pages cached */
1240 int *pnMax, /* OUT: Global maximum cache size */
1241 int *pnMin, /* OUT: Sum of PCache1.nMin for purgeable caches */
1242 int *pnRecyclable /* OUT: Total number of pages available for recycling */
1243){
1244 PgHdr1 *p;
1245 int nRecyclable = 0;
drh0b19c962015-09-10 19:22:25 +00001246 for(p=pcache1.grp.lru.pLruNext; p && !p->isAnchor; p=p->pLruNext){
drheabb67f2017-08-05 15:49:03 +00001247 assert( PAGE_IS_UNPINNED(p) );
danielk1977bc2ca9e2008-11-13 14:28:28 +00001248 nRecyclable++;
1249 }
drh9f8cf9d2011-01-17 21:32:24 +00001250 *pnCurrent = pcache1.grp.nCurrentPage;
drha69085c2012-01-02 18:00:55 +00001251 *pnMax = (int)pcache1.grp.nMaxPage;
1252 *pnMin = (int)pcache1.grp.nMinPage;
danielk1977bc2ca9e2008-11-13 14:28:28 +00001253 *pnRecyclable = nRecyclable;
1254}
1255#endif