blob: 296f552aa532fa358dece32cfac31630c48fbff6 [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**
drhc8e9f682022-08-09 14:28:54 +000042** Historical note: It used to be that if the SQLITE_PCACHE_SEPARATE_HEADER
43** was defined, then the page content would be held in a separate memory
44** allocation from the PgHdr1. This was intended to avoid clownshoe memory
45** allocations. However, the btree layer needs a small (16-byte) overrun
46** area after the page content buffer. The header serves as that overrun
47** area. Therefore SQLITE_PCACHE_SEPARATE_HEADER was discontinued to avoid
48** any possibility of a memory error.
drh01c5c002015-07-04 18:15:04 +000049**
50** This module tracks pointers to PgHdr1 objects. Only pcache.c communicates
51** with this module. Information is passed back and forth as PgHdr1 pointers.
52**
53** The pcache.c and pager.c modules deal pointers to PgHdr objects.
54** The btree.c module deals with pointers to MemPage objects.
drhee70a842015-07-06 18:54:52 +000055**
56** SOURCE OF PAGE CACHE MEMORY:
57**
58** Memory for a page might come from any of three sources:
59**
60** (1) The general-purpose memory allocator - sqlite3Malloc()
61** (2) Global page-cache memory provided using sqlite3_config() with
62** SQLITE_CONFIG_PAGECACHE.
63** (3) PCache-local bulk allocation.
64**
65** The third case is a chunk of heap memory (defaulting to 100 pages worth)
66** that is allocated when the page cache is created. The size of the local
67** bulk allocation can be adjusted using
68**
drha6082f62015-11-26 22:12:41 +000069** sqlite3_config(SQLITE_CONFIG_PAGECACHE, (void*)0, 0, N).
drhee70a842015-07-06 18:54:52 +000070**
71** If N is positive, then N pages worth of memory are allocated using a single
72** sqlite3Malloc() call and that memory is used for the first N pages allocated.
73** Or if N is negative, then -1024*N bytes of memory are allocated and used
74** for as many pages as can be accomodated.
75**
76** Only one of (2) or (3) can be used. Once the memory available to (2) or
77** (3) is exhausted, subsequent allocations fail over to the general-purpose
78** memory allocator (1).
79**
80** Earlier versions of SQLite used only methods (1) and (2). But experiments
81** show that method (3) with N==100 provides about a 5% performance boost for
82** common workloads.
danielk1977bc2ca9e2008-11-13 14:28:28 +000083*/
danielk1977bc2ca9e2008-11-13 14:28:28 +000084#include "sqliteInt.h"
85
86typedef struct PCache1 PCache1;
87typedef struct PgHdr1 PgHdr1;
88typedef struct PgFreeslot PgFreeslot;
drh9f8cf9d2011-01-17 21:32:24 +000089typedef struct PGroup PGroup;
90
drh92af02c2015-09-04 04:31:56 +000091/*
92** Each cache entry is represented by an instance of the following
drhc8e9f682022-08-09 14:28:54 +000093** structure. A buffer of PgHdr1.pCache->szPage bytes is allocated
94** directly before this structure and is used to cache the page content.
dane385d332019-02-06 20:49:49 +000095**
drhc8e9f682022-08-09 14:28:54 +000096** When reading a corrupt database file, it is possible that SQLite might
97** read a few bytes (no more than 16 bytes) past the end of the page buffer.
98** It will only read past the end of the page buffer, never write. This
99** object is positioned immediately after the page buffer to serve as an
100** overrun area, so that overreads are harmless.
101**
102** Variables isBulkLocal and isAnchor were once type "u8". That works,
dane385d332019-02-06 20:49:49 +0000103** but causes a 2-byte gap in the structure for most architectures (since
104** pointers must be either 4 or 8-byte aligned). As this structure is located
105** in memory directly after the associated page data, if the database is
106** corrupt, code at the b-tree layer may overread the page buffer and
107** read part of this structure before the corruption is detected. This
108** can cause a valgrind error if the unitialized gap is accessed. Using u16
drh78d15f02022-08-30 16:54:41 +0000109** ensures there is no such gap, and therefore no bytes of uninitialized
110** memory in the structure.
111**
112** The pLruNext and pLruPrev pointers form a double-linked circular list
113** of all pages that are unpinned. The PGroup.lru element (which should be
114** the only element on the list with PgHdr1.isAnchor set to 1) forms the
115** beginning and the end of the list.
drh92af02c2015-09-04 04:31:56 +0000116*/
117struct PgHdr1 {
drh78d15f02022-08-30 16:54:41 +0000118 sqlite3_pcache_page page; /* Base class. Must be first. pBuf & pExtra */
119 unsigned int iKey; /* Key value (page number) */
120 u16 isBulkLocal; /* This page from bulk local storage */
121 u16 isAnchor; /* This is the PGroup.lru element */
122 PgHdr1 *pNext; /* Next in hash table chain */
123 PCache1 *pCache; /* Cache that currently owns this page */
124 PgHdr1 *pLruNext; /* Next in circular LRU list of unpinned pages */
125 PgHdr1 *pLruPrev; /* Previous in LRU list of unpinned pages */
126 /* NB: pLruPrev is only valid if pLruNext!=0 */
drh92af02c2015-09-04 04:31:56 +0000127};
128
drheabb67f2017-08-05 15:49:03 +0000129/*
drh26505e52018-11-28 11:09:09 +0000130** A page is pinned if it is not on the LRU list. To be "pinned" means
131** that the page is in active use and must not be deallocated.
drheabb67f2017-08-05 15:49:03 +0000132*/
133#define PAGE_IS_PINNED(p) ((p)->pLruNext==0)
134#define PAGE_IS_UNPINNED(p) ((p)->pLruNext!=0)
135
drh9f8cf9d2011-01-17 21:32:24 +0000136/* Each page cache (or PCache) belongs to a PGroup. A PGroup is a set
peter.d.reid60ec9142014-09-06 16:39:46 +0000137** of one or more PCaches that are able to recycle each other's unpinned
drh9f8cf9d2011-01-17 21:32:24 +0000138** pages when they are under memory pressure. A PGroup is an instance of
139** the following object.
140**
141** This page cache implementation works in one of two modes:
142**
143** (1) Every PCache is the sole member of its own PGroup. There is
144** one PGroup per PCache.
145**
146** (2) There is a single global PGroup that all PCaches are a member
147** of.
148**
149** Mode 1 uses more memory (since PCache instances are not able to rob
150** unused pages from other PCaches) but it also operates without a mutex,
151** and is therefore often faster. Mode 2 requires a mutex in order to be
drh45d29302012-01-08 22:18:33 +0000152** threadsafe, but recycles pages more efficiently.
drh9f8cf9d2011-01-17 21:32:24 +0000153**
154** For mode (1), PGroup.mutex is NULL. For mode (2) there is only a single
155** PGroup which is the pcache1.grp global variable and its mutex is
156** SQLITE_MUTEX_STATIC_LRU.
157*/
158struct PGroup {
159 sqlite3_mutex *mutex; /* MUTEX_STATIC_LRU or NULL */
drha69085c2012-01-02 18:00:55 +0000160 unsigned int nMaxPage; /* Sum of nMax for purgeable caches */
161 unsigned int nMinPage; /* Sum of nMin for purgeable caches */
162 unsigned int mxPinned; /* nMaxpage + 10 - nMinPage */
drh617b7b42017-08-30 04:44:59 +0000163 unsigned int nPurgeable; /* Number of purgeable pages allocated */
drh92af02c2015-09-04 04:31:56 +0000164 PgHdr1 lru; /* The beginning and end of the LRU list */
drh9f8cf9d2011-01-17 21:32:24 +0000165};
danielk1977bc2ca9e2008-11-13 14:28:28 +0000166
drh9d13f112010-08-24 18:06:35 +0000167/* Each page cache is an instance of the following object. Every
168** open database file (including each in-memory database and each
169** temporary or transient database) has a single page cache which
170** is an instance of this object.
171**
172** Pointers to structures of this type are cast and returned as
173** opaque sqlite3_pcache* handles.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000174*/
175struct PCache1 {
176 /* Cache configuration parameters. Page size (szPage) and the purgeable
drh617b7b42017-08-30 04:44:59 +0000177 ** flag (bPurgeable) and the pnPurgeable pointer are all set when the
178 ** cache is created and are never changed thereafter. nMax may be
drh45d29302012-01-08 22:18:33 +0000179 ** modified at any time by a call to the pcache1Cachesize() method.
drh9f8cf9d2011-01-17 21:32:24 +0000180 ** The PGroup mutex must be held when accessing nMax.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000181 */
drh9f8cf9d2011-01-17 21:32:24 +0000182 PGroup *pGroup; /* PGroup this cache belongs to */
drh617b7b42017-08-30 04:44:59 +0000183 unsigned int *pnPurgeable; /* Pointer to pGroup->nPurgeable */
drhee70a842015-07-06 18:54:52 +0000184 int szPage; /* Size of database content section */
185 int szExtra; /* sizeof(MemPage)+sizeof(PgHdr) */
186 int szAlloc; /* Total size of one pcache line */
danielk1977bc2ca9e2008-11-13 14:28:28 +0000187 int bPurgeable; /* True if cache is purgeable */
danielk197744cd45c2008-11-15 11:22:45 +0000188 unsigned int nMin; /* Minimum number of pages reserved */
189 unsigned int nMax; /* Configured "cache_size" value */
drh25ca5682011-01-26 00:07:03 +0000190 unsigned int n90pct; /* nMax*9/10 */
drh2cbd78b2012-02-02 19:37:18 +0000191 unsigned int iMaxKey; /* Largest key seen since xTruncate() */
drh1757fed2019-01-09 14:49:58 +0000192 unsigned int nPurgeableDummy; /* pnPurgeable points here when not used*/
danielk1977bc2ca9e2008-11-13 14:28:28 +0000193
194 /* Hash table of all pages. The following variables may only be accessed
drh9f8cf9d2011-01-17 21:32:24 +0000195 ** when the accessor is holding the PGroup mutex.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000196 */
danielk197744cd45c2008-11-15 11:22:45 +0000197 unsigned int nRecyclable; /* Number of pages in the LRU list */
198 unsigned int nPage; /* Total number of pages in apHash */
199 unsigned int nHash; /* Number of slots in apHash[] */
danielk1977bc2ca9e2008-11-13 14:28:28 +0000200 PgHdr1 **apHash; /* Hash table for fast lookup by key */
drhee70a842015-07-06 18:54:52 +0000201 PgHdr1 *pFree; /* List of unused pcache-local pages */
202 void *pBulk; /* Bulk memory used by pcache-local */
danielk1977bc2ca9e2008-11-13 14:28:28 +0000203};
204
205/*
drhee70a842015-07-06 18:54:52 +0000206** Free slots in the allocator used to divide up the global page cache
207** buffer provided using the SQLITE_CONFIG_PAGECACHE mechanism.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000208*/
209struct PgFreeslot {
210 PgFreeslot *pNext; /* Next free slot */
211};
212
213/*
214** Global data used by this cache.
215*/
216static SQLITE_WSD struct PCacheGlobal {
drh9f8cf9d2011-01-17 21:32:24 +0000217 PGroup grp; /* The global PGroup for mode (2) */
danielk1977bc2ca9e2008-11-13 14:28:28 +0000218
drh9f8cf9d2011-01-17 21:32:24 +0000219 /* Variables related to SQLITE_CONFIG_PAGECACHE settings. The
220 ** szSlot, nSlot, pStart, pEnd, nReserve, and isInit values are all
221 ** fixed at sqlite3_initialize() time and do not require mutex protection.
222 ** The nFreeSlot and pFree values do require mutex protection.
223 */
224 int isInit; /* True if initialized */
drhdb7ae892015-07-06 20:57:22 +0000225 int separateCache; /* Use a new PGroup for each PCache */
drh957026a2015-07-16 18:18:19 +0000226 int nInitPage; /* Initial bulk allocation size */
drh9f8cf9d2011-01-17 21:32:24 +0000227 int szSlot; /* Size of each free slot */
228 int nSlot; /* The number of pcache slots */
229 int nReserve; /* Try to keep nFreeSlot above this */
drhee70a842015-07-06 18:54:52 +0000230 void *pStart, *pEnd; /* Bounds of global page cache memory */
drh9f8cf9d2011-01-17 21:32:24 +0000231 /* Above requires no mutex. Use mutex below for variable that follow. */
232 sqlite3_mutex *mutex; /* Mutex for accessing the following: */
drh9f8cf9d2011-01-17 21:32:24 +0000233 PgFreeslot *pFree; /* Free page blocks */
drh2cbd78b2012-02-02 19:37:18 +0000234 int nFreeSlot; /* Number of unused pcache slots */
drh9f8cf9d2011-01-17 21:32:24 +0000235 /* The following value requires a mutex to change. We skip the mutex on
236 ** reading because (1) most platforms read a 32-bit integer atomically and
237 ** (2) even if an incorrect value is read, no great harm is done since this
238 ** is really just an optimization. */
239 int bUnderPressure; /* True if low on PAGECACHE memory */
danielk197744cd45c2008-11-15 11:22:45 +0000240} pcache1_g;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000241
242/*
243** All code in this file should access the global structure above via the
244** alias "pcache1". This ensures that the WSD emulation is used when
245** compiling for systems that do not support real WSD.
246*/
247#define pcache1 (GLOBAL(struct PCacheGlobal, pcache1_g))
248
249/*
drh9f8cf9d2011-01-17 21:32:24 +0000250** Macros to enter and leave the PCache LRU mutex.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000251*/
drh982215a2015-06-13 11:10:55 +0000252#if !defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) || SQLITE_THREADSAFE==0
253# define pcache1EnterMutex(X) assert((X)->mutex==0)
254# define pcache1LeaveMutex(X) assert((X)->mutex==0)
255# define PCACHE1_MIGHT_USE_GROUP_MUTEX 0
256#else
257# define pcache1EnterMutex(X) sqlite3_mutex_enter((X)->mutex)
258# define pcache1LeaveMutex(X) sqlite3_mutex_leave((X)->mutex)
259# define PCACHE1_MIGHT_USE_GROUP_MUTEX 1
260#endif
danielk1977bc2ca9e2008-11-13 14:28:28 +0000261
262/******************************************************************************/
263/******** Page Allocation/SQLITE_CONFIG_PCACHE Related Functions **************/
264
drh92af02c2015-09-04 04:31:56 +0000265
danielk1977bc2ca9e2008-11-13 14:28:28 +0000266/*
267** This function is called during initialization if a static buffer is
268** supplied to use for the page-cache by passing the SQLITE_CONFIG_PAGECACHE
269** verb to sqlite3_config(). Parameter pBuf points to an allocation large
270** enough to contain 'n' buffers of 'sz' bytes each.
drh9f8cf9d2011-01-17 21:32:24 +0000271**
272** This routine is called from sqlite3_initialize() and so it is guaranteed
273** to be serialized already. There is no need for further mutexing.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000274*/
275void sqlite3PCacheBufferSetup(void *pBuf, int sz, int n){
drhf4622dc2009-05-22 11:10:24 +0000276 if( pcache1.isInit ){
277 PgFreeslot *p;
drhee70a842015-07-06 18:54:52 +0000278 if( pBuf==0 ) sz = n = 0;
drh52df6f52017-08-28 16:11:05 +0000279 if( n==0 ) sz = 0;
drhf4622dc2009-05-22 11:10:24 +0000280 sz = ROUNDDOWN8(sz);
281 pcache1.szSlot = sz;
drh50d1b5f2010-08-27 12:21:06 +0000282 pcache1.nSlot = pcache1.nFreeSlot = n;
283 pcache1.nReserve = n>90 ? 10 : (n/10 + 1);
drhf4622dc2009-05-22 11:10:24 +0000284 pcache1.pStart = pBuf;
285 pcache1.pFree = 0;
drh9f8cf9d2011-01-17 21:32:24 +0000286 pcache1.bUnderPressure = 0;
drhf4622dc2009-05-22 11:10:24 +0000287 while( n-- ){
288 p = (PgFreeslot*)pBuf;
289 p->pNext = pcache1.pFree;
290 pcache1.pFree = p;
291 pBuf = (void*)&((char*)pBuf)[sz];
292 }
293 pcache1.pEnd = pBuf;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000294 }
danielk1977bc2ca9e2008-11-13 14:28:28 +0000295}
296
297/*
drh957026a2015-07-16 18:18:19 +0000298** Try to initialize the pCache->pFree and pCache->pBulk fields. Return
299** true if pCache->pFree ends up containing one or more free pages.
300*/
301static int pcache1InitBulk(PCache1 *pCache){
drh939d4bc2015-07-16 18:37:53 +0000302 i64 szBulk;
drh957026a2015-07-16 18:18:19 +0000303 char *zBulk;
304 if( pcache1.nInitPage==0 ) return 0;
305 /* Do not bother with a bulk allocation if the cache size very small */
306 if( pCache->nMax<3 ) return 0;
307 sqlite3BeginBenignMalloc();
308 if( pcache1.nInitPage>0 ){
drh939d4bc2015-07-16 18:37:53 +0000309 szBulk = pCache->szAlloc * (i64)pcache1.nInitPage;
drh957026a2015-07-16 18:18:19 +0000310 }else{
drh939d4bc2015-07-16 18:37:53 +0000311 szBulk = -1024 * (i64)pcache1.nInitPage;
drh957026a2015-07-16 18:18:19 +0000312 }
drh939d4bc2015-07-16 18:37:53 +0000313 if( szBulk > pCache->szAlloc*(i64)pCache->nMax ){
drh989412a2016-10-13 12:56:18 +0000314 szBulk = pCache->szAlloc*(i64)pCache->nMax;
drh957026a2015-07-16 18:18:19 +0000315 }
316 zBulk = pCache->pBulk = sqlite3Malloc( szBulk );
317 sqlite3EndBenignMalloc();
318 if( zBulk ){
319 int nBulk = sqlite3MallocSize(zBulk)/pCache->szAlloc;
drh4eb8d7f2017-03-29 17:06:14 +0000320 do{
drh957026a2015-07-16 18:18:19 +0000321 PgHdr1 *pX = (PgHdr1*)&zBulk[pCache->szPage];
322 pX->page.pBuf = zBulk;
323 pX->page.pExtra = &pX[1];
324 pX->isBulkLocal = 1;
drh92af02c2015-09-04 04:31:56 +0000325 pX->isAnchor = 0;
drh957026a2015-07-16 18:18:19 +0000326 pX->pNext = pCache->pFree;
dane385d332019-02-06 20:49:49 +0000327 pX->pLruPrev = 0; /* Initializing this saves a valgrind error */
drh957026a2015-07-16 18:18:19 +0000328 pCache->pFree = pX;
329 zBulk += pCache->szAlloc;
drh4eb8d7f2017-03-29 17:06:14 +0000330 }while( --nBulk );
drh957026a2015-07-16 18:18:19 +0000331 }
332 return pCache->pFree!=0;
333}
334
335/*
danielk1977bc2ca9e2008-11-13 14:28:28 +0000336** Malloc function used within this file to allocate space from the buffer
337** configured using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no
338** such buffer exists or there is no space left in it, this function falls
339** back to sqlite3Malloc().
drh9f8cf9d2011-01-17 21:32:24 +0000340**
341** Multiple threads can run this routine at the same time. Global variables
342** in pcache1 need to be protected via mutex.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000343*/
344static void *pcache1Alloc(int nByte){
drh9f8cf9d2011-01-17 21:32:24 +0000345 void *p = 0;
346 assert( sqlite3_mutex_notheld(pcache1.grp.mutex) );
drh9f8cf9d2011-01-17 21:32:24 +0000347 if( nByte<=pcache1.szSlot ){
348 sqlite3_mutex_enter(pcache1.mutex);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000349 p = (PgHdr1 *)pcache1.pFree;
drh9f8cf9d2011-01-17 21:32:24 +0000350 if( p ){
351 pcache1.pFree = pcache1.pFree->pNext;
352 pcache1.nFreeSlot--;
353 pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve;
354 assert( pcache1.nFreeSlot>=0 );
drhb02392e2015-10-15 15:28:56 +0000355 sqlite3StatusHighwater(SQLITE_STATUS_PAGECACHE_SIZE, nByte);
drhaf89fe62015-03-23 17:25:18 +0000356 sqlite3StatusUp(SQLITE_STATUS_PAGECACHE_USED, 1);
drh9f8cf9d2011-01-17 21:32:24 +0000357 }
358 sqlite3_mutex_leave(pcache1.mutex);
359 }
360 if( p==0 ){
361 /* Memory is not available in the SQLITE_CONFIG_PAGECACHE pool. Get
362 ** it from sqlite3Malloc instead.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000363 */
danielk1977bc2ca9e2008-11-13 14:28:28 +0000364 p = sqlite3Malloc(nByte);
drh4bd69522012-06-07 02:35:29 +0000365#ifndef SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS
danielk1977bc2ca9e2008-11-13 14:28:28 +0000366 if( p ){
367 int sz = sqlite3MallocSize(p);
drh9bf3da8e2011-01-26 13:24:40 +0000368 sqlite3_mutex_enter(pcache1.mutex);
drhb02392e2015-10-15 15:28:56 +0000369 sqlite3StatusHighwater(SQLITE_STATUS_PAGECACHE_SIZE, nByte);
drhaf89fe62015-03-23 17:25:18 +0000370 sqlite3StatusUp(SQLITE_STATUS_PAGECACHE_OVERFLOW, sz);
drh9bf3da8e2011-01-26 13:24:40 +0000371 sqlite3_mutex_leave(pcache1.mutex);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000372 }
drh4bd69522012-06-07 02:35:29 +0000373#endif
drh107b56e2010-03-12 16:32:53 +0000374 sqlite3MemdebugSetType(p, MEMTYPE_PCACHE);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000375 }
376 return p;
377}
378
379/*
380** Free an allocated buffer obtained from pcache1Alloc().
381*/
drhee70a842015-07-06 18:54:52 +0000382static void pcache1Free(void *p){
drhee70a842015-07-06 18:54:52 +0000383 if( p==0 ) return;
drh8b0ba7b2015-12-16 13:07:35 +0000384 if( SQLITE_WITHIN(p, pcache1.pStart, pcache1.pEnd) ){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000385 PgFreeslot *pSlot;
drh9f8cf9d2011-01-17 21:32:24 +0000386 sqlite3_mutex_enter(pcache1.mutex);
drhaf89fe62015-03-23 17:25:18 +0000387 sqlite3StatusDown(SQLITE_STATUS_PAGECACHE_USED, 1);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000388 pSlot = (PgFreeslot*)p;
389 pSlot->pNext = pcache1.pFree;
390 pcache1.pFree = pSlot;
drh50d1b5f2010-08-27 12:21:06 +0000391 pcache1.nFreeSlot++;
drh9f8cf9d2011-01-17 21:32:24 +0000392 pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve;
drh50d1b5f2010-08-27 12:21:06 +0000393 assert( pcache1.nFreeSlot<=pcache1.nSlot );
drh9f8cf9d2011-01-17 21:32:24 +0000394 sqlite3_mutex_leave(pcache1.mutex);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000395 }else{
drh107b56e2010-03-12 16:32:53 +0000396 assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) );
397 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
drh4bd69522012-06-07 02:35:29 +0000398#ifndef SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS
drh9493caf2016-03-17 23:16:37 +0000399 {
400 int nFreed = 0;
401 nFreed = sqlite3MallocSize(p);
402 sqlite3_mutex_enter(pcache1.mutex);
403 sqlite3StatusDown(SQLITE_STATUS_PAGECACHE_OVERFLOW, nFreed);
404 sqlite3_mutex_leave(pcache1.mutex);
405 }
drh4bd69522012-06-07 02:35:29 +0000406#endif
danielk1977bc2ca9e2008-11-13 14:28:28 +0000407 sqlite3_free(p);
408 }
409}
410
drhc8f503a2010-08-20 09:14:13 +0000411#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
412/*
drh9d13f112010-08-24 18:06:35 +0000413** Return the size of a pcache allocation
drhc8f503a2010-08-20 09:14:13 +0000414*/
415static int pcache1MemSize(void *p){
drhc8f503a2010-08-20 09:14:13 +0000416 if( p>=pcache1.pStart && p<pcache1.pEnd ){
417 return pcache1.szSlot;
418 }else{
419 int iSize;
420 assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) );
421 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
422 iSize = sqlite3MallocSize(p);
423 sqlite3MemdebugSetType(p, MEMTYPE_PCACHE);
424 return iSize;
425 }
426}
427#endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
428
dand2925702011-08-19 18:15:00 +0000429/*
danielk1977bc2ca9e2008-11-13 14:28:28 +0000430** Allocate a new page object initially associated with cache pCache.
431*/
drh3c0c4312015-09-01 19:51:37 +0000432static PgHdr1 *pcache1AllocPage(PCache1 *pCache, int benignMalloc){
danb5126dd2011-09-22 14:56:31 +0000433 PgHdr1 *p = 0;
434 void *pPg;
dand2925702011-08-19 18:15:00 +0000435
dand2925702011-08-19 18:15:00 +0000436 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
drh957026a2015-07-16 18:18:19 +0000437 if( pCache->pFree || (pCache->nPage==0 && pcache1InitBulk(pCache)) ){
drh55f66b32019-07-16 19:44:32 +0000438 assert( pCache->pFree!=0 );
drhee70a842015-07-06 18:54:52 +0000439 p = pCache->pFree;
440 pCache->pFree = p->pNext;
441 p->pNext = 0;
442 }else{
drhdb7ae892015-07-06 20:57:22 +0000443#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
drhee70a842015-07-06 18:54:52 +0000444 /* The group mutex must be released before pcache1Alloc() is called. This
drhdb7ae892015-07-06 20:57:22 +0000445 ** is because it might call sqlite3_release_memory(), which assumes that
drhee70a842015-07-06 18:54:52 +0000446 ** this mutex is not held. */
drhdb7ae892015-07-06 20:57:22 +0000447 assert( pcache1.separateCache==0 );
448 assert( pCache->pGroup==&pcache1.grp );
drhee70a842015-07-06 18:54:52 +0000449 pcache1LeaveMutex(pCache->pGroup);
drhdb7ae892015-07-06 20:57:22 +0000450#endif
drh8faee872015-09-19 18:08:13 +0000451 if( benignMalloc ){ sqlite3BeginBenignMalloc(); }
drhee70a842015-07-06 18:54:52 +0000452 pPg = pcache1Alloc(pCache->szAlloc);
drh8faee872015-09-19 18:08:13 +0000453 if( benignMalloc ){ sqlite3EndBenignMalloc(); }
drhdb7ae892015-07-06 20:57:22 +0000454#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
drhee70a842015-07-06 18:54:52 +0000455 pcache1EnterMutex(pCache->pGroup);
drhdb7ae892015-07-06 20:57:22 +0000456#endif
drhee70a842015-07-06 18:54:52 +0000457 if( pPg==0 ) return 0;
drh0f1fa5d2019-12-13 21:24:46 +0000458 p = (PgHdr1 *)&((u8 *)pPg)[pCache->szPage];
dan22e21ff2011-11-08 20:08:44 +0000459 p->page.pBuf = pPg;
460 p->page.pExtra = &p[1];
drhee70a842015-07-06 18:54:52 +0000461 p->isBulkLocal = 0;
drh92af02c2015-09-04 04:31:56 +0000462 p->isAnchor = 0;
dan1e06c702021-02-28 08:24:56 +0000463 p->pLruPrev = 0; /* Initializing this saves a valgrind error */
danielk1977bc2ca9e2008-11-13 14:28:28 +0000464 }
drh617b7b42017-08-30 04:44:59 +0000465 (*pCache->pnPurgeable)++;
drhee70a842015-07-06 18:54:52 +0000466 return p;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000467}
468
469/*
470** Free a page object allocated by pcache1AllocPage().
471*/
472static void pcache1FreePage(PgHdr1 *p){
drhdb7ae892015-07-06 20:57:22 +0000473 PCache1 *pCache;
474 assert( p!=0 );
475 pCache = p->pCache;
476 assert( sqlite3_mutex_held(p->pCache->pGroup->mutex) );
477 if( p->isBulkLocal ){
478 p->pNext = pCache->pFree;
479 pCache->pFree = p;
480 }else{
481 pcache1Free(p->page.pBuf);
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){
drhd4de9f72019-04-14 00:34:20 +0000492 assert( sz<=65536+8 ); /* These allocations are never very large */
drhd7a5e492018-12-14 16:20:54 +0000493 return pcache1Alloc(sz);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000494}
495
496/*
497** Free an allocated buffer obtained from sqlite3PageMalloc().
498*/
499void sqlite3PageFree(void *p){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000500 pcache1Free(p);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000501}
502
drh50d1b5f2010-08-27 12:21:06 +0000503
504/*
505** Return true if it desirable to avoid allocating a new page cache
506** entry.
507**
508** If memory was allocated specifically to the page cache using
509** SQLITE_CONFIG_PAGECACHE but that memory has all been used, then
510** it is desirable to avoid allocating a new page cache entry because
511** presumably SQLITE_CONFIG_PAGECACHE was suppose to be sufficient
512** for all page cache needs and we should not need to spill the
513** allocation onto the heap.
514**
drh45d29302012-01-08 22:18:33 +0000515** Or, the heap is used for all page cache memory but the heap is
drh50d1b5f2010-08-27 12:21:06 +0000516** under memory pressure, then again it is desirable to avoid
517** allocating a new page cache entry in order to avoid stressing
518** the heap even further.
519*/
520static int pcache1UnderMemoryPressure(PCache1 *pCache){
dan22e21ff2011-11-08 20:08:44 +0000521 if( pcache1.nSlot && (pCache->szPage+pCache->szExtra)<=pcache1.szSlot ){
drh9f8cf9d2011-01-17 21:32:24 +0000522 return pcache1.bUnderPressure;
drh50d1b5f2010-08-27 12:21:06 +0000523 }else{
524 return sqlite3HeapNearlyFull();
525 }
526}
527
danielk1977bc2ca9e2008-11-13 14:28:28 +0000528/******************************************************************************/
529/******** General Implementation Functions ************************************/
530
531/*
532** This function is used to resize the hash table used by the cache passed
533** as the first argument.
534**
drh9f8cf9d2011-01-17 21:32:24 +0000535** The PCache mutex must be held when this function is called.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000536*/
drhefbf0442014-08-23 23:15:31 +0000537static void pcache1ResizeHash(PCache1 *p){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000538 PgHdr1 **apNew;
danielk197744cd45c2008-11-15 11:22:45 +0000539 unsigned int nNew;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000540 unsigned int i;
541
drh9f8cf9d2011-01-17 21:32:24 +0000542 assert( sqlite3_mutex_held(p->pGroup->mutex) );
danielk1977bc2ca9e2008-11-13 14:28:28 +0000543
544 nNew = p->nHash*2;
545 if( nNew<256 ){
546 nNew = 256;
547 }
548
drh9f8cf9d2011-01-17 21:32:24 +0000549 pcache1LeaveMutex(p->pGroup);
drh085bb7f2008-12-06 14:34:33 +0000550 if( p->nHash ){ sqlite3BeginBenignMalloc(); }
dan6809c962012-07-30 14:53:54 +0000551 apNew = (PgHdr1 **)sqlite3MallocZero(sizeof(PgHdr1 *)*nNew);
drh085bb7f2008-12-06 14:34:33 +0000552 if( p->nHash ){ sqlite3EndBenignMalloc(); }
drh9f8cf9d2011-01-17 21:32:24 +0000553 pcache1EnterMutex(p->pGroup);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000554 if( apNew ){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000555 for(i=0; i<p->nHash; i++){
556 PgHdr1 *pPage;
557 PgHdr1 *pNext = p->apHash[i];
drhb27b7f52008-12-10 18:03:45 +0000558 while( (pPage = pNext)!=0 ){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000559 unsigned int h = pPage->iKey % nNew;
560 pNext = pPage->pNext;
561 pPage->pNext = apNew[h];
562 apNew[h] = pPage;
563 }
564 }
565 sqlite3_free(p->apHash);
566 p->apHash = apNew;
567 p->nHash = nNew;
568 }
danielk1977bc2ca9e2008-11-13 14:28:28 +0000569}
570
571/*
572** This function is used internally to remove the page pPage from the
drh9f8cf9d2011-01-17 21:32:24 +0000573** PGroup LRU list, if is part of it. If pPage is not part of the PGroup
danielk1977bc2ca9e2008-11-13 14:28:28 +0000574** LRU list, then this function is a no-op.
575**
drh9f8cf9d2011-01-17 21:32:24 +0000576** The PGroup mutex must be held when this function is called.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000577*/
drh55a46c92015-06-12 13:49:26 +0000578static PgHdr1 *pcache1PinPage(PgHdr1 *pPage){
drh5d56dd22013-12-13 18:50:40 +0000579 assert( pPage!=0 );
drheabb67f2017-08-05 15:49:03 +0000580 assert( PAGE_IS_UNPINNED(pPage) );
drh92af02c2015-09-04 04:31:56 +0000581 assert( pPage->pLruNext );
582 assert( pPage->pLruPrev );
drheabb67f2017-08-05 15:49:03 +0000583 assert( sqlite3_mutex_held(pPage->pCache->pGroup->mutex) );
drh92af02c2015-09-04 04:31:56 +0000584 pPage->pLruPrev->pLruNext = pPage->pLruNext;
585 pPage->pLruNext->pLruPrev = pPage->pLruPrev;
drh5d56dd22013-12-13 18:50:40 +0000586 pPage->pLruNext = 0;
drhde72d2a2018-12-03 01:58:02 +0000587 /* pPage->pLruPrev = 0;
588 ** No need to clear pLruPrev as it is never accessed if pLruNext is 0 */
drh92af02c2015-09-04 04:31:56 +0000589 assert( pPage->isAnchor==0 );
drheabb67f2017-08-05 15:49:03 +0000590 assert( pPage->pCache->pGroup->lru.isAnchor==1 );
591 pPage->pCache->nRecyclable--;
drh55a46c92015-06-12 13:49:26 +0000592 return pPage;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000593}
594
595
596/*
597** Remove the page supplied as an argument from the hash table
598** (PCache1.apHash structure) that it is currently stored in.
drh95c91e12015-06-29 00:21:00 +0000599** Also free the page if freePage is true.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000600**
drh9f8cf9d2011-01-17 21:32:24 +0000601** The PGroup mutex must be held when this function is called.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000602*/
drh95c91e12015-06-29 00:21:00 +0000603static void pcache1RemoveFromHash(PgHdr1 *pPage, int freeFlag){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000604 unsigned int h;
605 PCache1 *pCache = pPage->pCache;
606 PgHdr1 **pp;
607
drh9f8cf9d2011-01-17 21:32:24 +0000608 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
danielk1977bc2ca9e2008-11-13 14:28:28 +0000609 h = pPage->iKey % pCache->nHash;
610 for(pp=&pCache->apHash[h]; (*pp)!=pPage; pp=&(*pp)->pNext);
611 *pp = (*pp)->pNext;
612
613 pCache->nPage--;
drh95c91e12015-06-29 00:21:00 +0000614 if( freeFlag ) pcache1FreePage(pPage);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000615}
616
617/*
drh9f8cf9d2011-01-17 21:32:24 +0000618** If there are currently more than nMaxPage pages allocated, try
619** to recycle pages to reduce the number allocated to nMaxPage.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000620*/
drh957026a2015-07-16 18:18:19 +0000621static void pcache1EnforceMaxPage(PCache1 *pCache){
622 PGroup *pGroup = pCache->pGroup;
drh92af02c2015-09-04 04:31:56 +0000623 PgHdr1 *p;
drh9f8cf9d2011-01-17 21:32:24 +0000624 assert( sqlite3_mutex_held(pGroup->mutex) );
drh617b7b42017-08-30 04:44:59 +0000625 while( pGroup->nPurgeable>pGroup->nMaxPage
drh92af02c2015-09-04 04:31:56 +0000626 && (p=pGroup->lru.pLruPrev)->isAnchor==0
627 ){
drh9f8cf9d2011-01-17 21:32:24 +0000628 assert( p->pCache->pGroup==pGroup );
drheabb67f2017-08-05 15:49:03 +0000629 assert( PAGE_IS_UNPINNED(p) );
danielk1977bc2ca9e2008-11-13 14:28:28 +0000630 pcache1PinPage(p);
drh95c91e12015-06-29 00:21:00 +0000631 pcache1RemoveFromHash(p, 1);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000632 }
drh957026a2015-07-16 18:18:19 +0000633 if( pCache->nPage==0 && pCache->pBulk ){
634 sqlite3_free(pCache->pBulk);
635 pCache->pBulk = pCache->pFree = 0;
636 }
danielk1977bc2ca9e2008-11-13 14:28:28 +0000637}
638
639/*
640** Discard all pages from cache pCache with a page number (key value)
641** greater than or equal to iLimit. Any pinned pages that meet this
642** criteria are unpinned before they are discarded.
643**
drh9f8cf9d2011-01-17 21:32:24 +0000644** The PCache mutex must be held when this function is called.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000645*/
646static void pcache1TruncateUnsafe(
drh9f8cf9d2011-01-17 21:32:24 +0000647 PCache1 *pCache, /* The cache to truncate */
648 unsigned int iLimit /* Drop pages with this pgno or larger */
danielk1977bc2ca9e2008-11-13 14:28:28 +0000649){
drhd9fabbc2016-08-10 11:50:12 +0000650 TESTONLY( int nPage = 0; ) /* To assert pCache->nPage is correct */
651 unsigned int h, iStop;
dan9180b332023-01-19 21:05:02 +0000652
653#define TRUNCATE_UNSAFE_TIMEOUT 1500000
654 u64 tmTruncate = 0;
655 unsigned int nTmPage = 0;
656
drh9f8cf9d2011-01-17 21:32:24 +0000657 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
drhd9fabbc2016-08-10 11:50:12 +0000658 assert( pCache->iMaxKey >= iLimit );
659 assert( pCache->nHash > 0 );
drhf5dbe7f2016-08-10 15:02:49 +0000660 if( pCache->iMaxKey - iLimit < pCache->nHash ){
drhd9fabbc2016-08-10 11:50:12 +0000661 /* If we are just shaving the last few pages off the end of the
662 ** cache, then there is no point in scanning the entire hash table.
663 ** Only scan those hash slots that might contain pages that need to
664 ** be removed. */
drhf5dbe7f2016-08-10 15:02:49 +0000665 h = iLimit % pCache->nHash;
666 iStop = pCache->iMaxKey % pCache->nHash;
drhd9fabbc2016-08-10 11:50:12 +0000667 TESTONLY( nPage = -10; ) /* Disable the pCache->nPage validity check */
668 }else{
669 /* This is the general case where many pages are being removed.
670 ** It is necessary to scan the entire hash table */
drhf5dbe7f2016-08-10 15:02:49 +0000671 h = pCache->nHash/2;
672 iStop = h - 1;
drhd9fabbc2016-08-10 11:50:12 +0000673 }
dan9180b332023-01-19 21:05:02 +0000674
675 if( iLimit<=1 ){
676 tmTruncate = sqlite3STimeNow();
677 nTmPage = pCache->nPage;
678 }
679
drhd9fabbc2016-08-10 11:50:12 +0000680 for(;;){
681 PgHdr1 **pp;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000682 PgHdr1 *pPage;
drhd9fabbc2016-08-10 11:50:12 +0000683 assert( h<pCache->nHash );
684 pp = &pCache->apHash[h];
dan9180b332023-01-19 21:05:02 +0000685
drhb27b7f52008-12-10 18:03:45 +0000686 while( (pPage = *pp)!=0 ){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000687 if( pPage->iKey>=iLimit ){
danielk1977ea24ac42009-05-08 06:52:47 +0000688 pCache->nPage--;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000689 *pp = pPage->pNext;
drheabb67f2017-08-05 15:49:03 +0000690 if( PAGE_IS_UNPINNED(pPage) ) pcache1PinPage(pPage);
dan9e865902023-01-25 17:09:16 +0000691 if( pcache1.separateCache ){
692 pPage->pNext = pCache->pFree;
693 pCache->pFree = pPage;
694 (*pCache->pnPurgeable)--;
695 }else{
696 pcache1FreePage(pPage);
697 }
danielk1977bc2ca9e2008-11-13 14:28:28 +0000698 }else{
699 pp = &pPage->pNext;
drhd9fabbc2016-08-10 11:50:12 +0000700 TESTONLY( if( nPage>=0 ) nPage++; )
danielk1977bc2ca9e2008-11-13 14:28:28 +0000701 }
702 }
drhd9fabbc2016-08-10 11:50:12 +0000703 if( h==iStop ) break;
drhf5dbe7f2016-08-10 15:02:49 +0000704 h = (h+1) % pCache->nHash;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000705 }
dan9180b332023-01-19 21:05:02 +0000706
707 if( iLimit<=1 ){
708 tmTruncate = sqlite3STimeNow() - tmTruncate;
709 if( tmTruncate>TRUNCATE_UNSAFE_TIMEOUT ){
710 sqlite3_log(SQLITE_WARNING,
dand6285d32023-01-25 17:24:34 +0000711 "slow pcache.xTruncate(1) (v=7): (%d) nPage: %d -> %d",
dan9180b332023-01-19 21:05:02 +0000712 (int)tmTruncate, (int)nTmPage, (int)pCache->nPage
713 );
714 }
715 }
716
drhd9fabbc2016-08-10 11:50:12 +0000717 assert( nPage<0 || pCache->nPage==(unsigned)nPage );
danielk1977bc2ca9e2008-11-13 14:28:28 +0000718}
719
720/******************************************************************************/
721/******** sqlite3_pcache Methods **********************************************/
722
723/*
724** Implementation of the sqlite3_pcache.xInit method.
725*/
danielk197762c14b32008-11-19 09:05:26 +0000726static int pcache1Init(void *NotUsed){
727 UNUSED_PARAMETER(NotUsed);
drhf4622dc2009-05-22 11:10:24 +0000728 assert( pcache1.isInit==0 );
danielk1977bc2ca9e2008-11-13 14:28:28 +0000729 memset(&pcache1, 0, sizeof(pcache1));
drhdb7ae892015-07-06 20:57:22 +0000730
731
732 /*
733 ** The pcache1.separateCache variable is true if each PCache has its own
734 ** private PGroup (mode-1). pcache1.separateCache is false if the single
735 ** PGroup in pcache1.grp is used for all page caches (mode-2).
736 **
737 ** * Always use a unified cache (mode-2) if ENABLE_MEMORY_MANAGEMENT
738 **
739 ** * Use a unified cache in single-threaded applications that have
740 ** configured a start-time buffer for use as page-cache memory using
741 ** sqlite3_config(SQLITE_CONFIG_PAGECACHE, pBuf, sz, N) with non-NULL
742 ** pBuf argument.
743 **
744 ** * Otherwise use separate caches (mode-1)
745 */
746#if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT)
747 pcache1.separateCache = 0;
drhfd5ae962015-07-07 15:14:16 +0000748#elif SQLITE_THREADSAFE
drhdb7ae892015-07-06 20:57:22 +0000749 pcache1.separateCache = sqlite3GlobalConfig.pPage==0
750 || sqlite3GlobalConfig.bCoreMutex>0;
drhfd5ae962015-07-07 15:14:16 +0000751#else
752 pcache1.separateCache = sqlite3GlobalConfig.pPage==0;
drhdb7ae892015-07-06 20:57:22 +0000753#endif
754
drh982215a2015-06-13 11:10:55 +0000755#if SQLITE_THREADSAFE
danielk1977bc2ca9e2008-11-13 14:28:28 +0000756 if( sqlite3GlobalConfig.bCoreMutex ){
drh97a7e5e2016-04-26 18:58:54 +0000757 pcache1.grp.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU);
758 pcache1.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_PMEM);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000759 }
drh982215a2015-06-13 11:10:55 +0000760#endif
drh957026a2015-07-16 18:18:19 +0000761 if( pcache1.separateCache
762 && sqlite3GlobalConfig.nPage!=0
763 && sqlite3GlobalConfig.pPage==0
764 ){
765 pcache1.nInitPage = sqlite3GlobalConfig.nPage;
766 }else{
767 pcache1.nInitPage = 0;
768 }
drh41692e92011-01-25 04:34:51 +0000769 pcache1.grp.mxPinned = 10;
drhf4622dc2009-05-22 11:10:24 +0000770 pcache1.isInit = 1;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000771 return SQLITE_OK;
772}
773
774/*
775** Implementation of the sqlite3_pcache.xShutdown method.
shane7c7c3112009-08-17 15:31:23 +0000776** Note that the static mutex allocated in xInit does
777** not need to be freed.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000778*/
danielk197762c14b32008-11-19 09:05:26 +0000779static void pcache1Shutdown(void *NotUsed){
780 UNUSED_PARAMETER(NotUsed);
drhf4622dc2009-05-22 11:10:24 +0000781 assert( pcache1.isInit!=0 );
drhb0937192009-05-22 10:53:29 +0000782 memset(&pcache1, 0, sizeof(pcache1));
danielk1977bc2ca9e2008-11-13 14:28:28 +0000783}
784
drhefbf0442014-08-23 23:15:31 +0000785/* forward declaration */
786static void pcache1Destroy(sqlite3_pcache *p);
787
danielk1977bc2ca9e2008-11-13 14:28:28 +0000788/*
789** Implementation of the sqlite3_pcache.xCreate method.
790**
791** Allocate a new cache.
792*/
drhe5c40b12011-11-09 00:06:05 +0000793static sqlite3_pcache *pcache1Create(int szPage, int szExtra, int bPurgeable){
drh9f8cf9d2011-01-17 21:32:24 +0000794 PCache1 *pCache; /* The newly created page cache */
795 PGroup *pGroup; /* The group the new page cache will belong to */
796 int sz; /* Bytes of memory required to allocate the new cache */
danielk1977bc2ca9e2008-11-13 14:28:28 +0000797
drhe73c9142011-11-09 16:12:24 +0000798 assert( (szPage & (szPage-1))==0 && szPage>=512 && szPage<=65536 );
799 assert( szExtra < 300 );
800
drhdb7ae892015-07-06 20:57:22 +0000801 sz = sizeof(PCache1) + sizeof(PGroup)*pcache1.separateCache;
dan6809c962012-07-30 14:53:54 +0000802 pCache = (PCache1 *)sqlite3MallocZero(sz);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000803 if( pCache ){
drhdb7ae892015-07-06 20:57:22 +0000804 if( pcache1.separateCache ){
drh9f8cf9d2011-01-17 21:32:24 +0000805 pGroup = (PGroup*)&pCache[1];
drh41692e92011-01-25 04:34:51 +0000806 pGroup->mxPinned = 10;
drh9f8cf9d2011-01-17 21:32:24 +0000807 }else{
dan9dde7cb2011-06-09 17:53:43 +0000808 pGroup = &pcache1.grp;
drh9f8cf9d2011-01-17 21:32:24 +0000809 }
dana082cd72019-07-04 16:05:26 +0000810 pcache1EnterMutex(pGroup);
drh92af02c2015-09-04 04:31:56 +0000811 if( pGroup->lru.isAnchor==0 ){
812 pGroup->lru.isAnchor = 1;
813 pGroup->lru.pLruPrev = pGroup->lru.pLruNext = &pGroup->lru;
814 }
drh9f8cf9d2011-01-17 21:32:24 +0000815 pCache->pGroup = pGroup;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000816 pCache->szPage = szPage;
dan22e21ff2011-11-08 20:08:44 +0000817 pCache->szExtra = szExtra;
drhee70a842015-07-06 18:54:52 +0000818 pCache->szAlloc = szPage + szExtra + ROUND8(sizeof(PgHdr1));
danielk1977bc2ca9e2008-11-13 14:28:28 +0000819 pCache->bPurgeable = (bPurgeable ? 1 : 0);
drhefbf0442014-08-23 23:15:31 +0000820 pcache1ResizeHash(pCache);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000821 if( bPurgeable ){
822 pCache->nMin = 10;
drh9f8cf9d2011-01-17 21:32:24 +0000823 pGroup->nMinPage += pCache->nMin;
drh41692e92011-01-25 04:34:51 +0000824 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
drh617b7b42017-08-30 04:44:59 +0000825 pCache->pnPurgeable = &pGroup->nPurgeable;
826 }else{
drh1757fed2019-01-09 14:49:58 +0000827 pCache->pnPurgeable = &pCache->nPurgeableDummy;
drhefbf0442014-08-23 23:15:31 +0000828 }
829 pcache1LeaveMutex(pGroup);
830 if( pCache->nHash==0 ){
831 pcache1Destroy((sqlite3_pcache*)pCache);
832 pCache = 0;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000833 }
834 }
835 return (sqlite3_pcache *)pCache;
836}
837
838/*
839** Implementation of the sqlite3_pcache.xCachesize method.
840**
841** Configure the cache_size limit for a cache.
842*/
843static void pcache1Cachesize(sqlite3_pcache *p, int nMax){
844 PCache1 *pCache = (PCache1 *)p;
drhf9d349a2021-08-09 19:54:27 +0000845 u32 n;
846 assert( nMax>=0 );
danielk1977bc2ca9e2008-11-13 14:28:28 +0000847 if( pCache->bPurgeable ){
drh9f8cf9d2011-01-17 21:32:24 +0000848 PGroup *pGroup = pCache->pGroup;
849 pcache1EnterMutex(pGroup);
drhf9d349a2021-08-09 19:54:27 +0000850 n = (u32)nMax;
851 if( n > 0x7fff0000 - pGroup->nMaxPage + pCache->nMax ){
852 n = 0x7fff0000 - pGroup->nMaxPage + pCache->nMax;
drh8a728822021-08-09 18:07:06 +0000853 }
drhf9d349a2021-08-09 19:54:27 +0000854 pGroup->nMaxPage += (n - pCache->nMax);
drh41692e92011-01-25 04:34:51 +0000855 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
drhf9d349a2021-08-09 19:54:27 +0000856 pCache->nMax = n;
drh25ca5682011-01-26 00:07:03 +0000857 pCache->n90pct = pCache->nMax*9/10;
drh957026a2015-07-16 18:18:19 +0000858 pcache1EnforceMaxPage(pCache);
drh9f8cf9d2011-01-17 21:32:24 +0000859 pcache1LeaveMutex(pGroup);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000860 }
861}
862
863/*
drh09419b42011-11-16 19:29:17 +0000864** Implementation of the sqlite3_pcache.xShrink method.
865**
866** Free up as much memory as possible.
867*/
868static void pcache1Shrink(sqlite3_pcache *p){
869 PCache1 *pCache = (PCache1*)p;
870 if( pCache->bPurgeable ){
871 PGroup *pGroup = pCache->pGroup;
drh8a728822021-08-09 18:07:06 +0000872 unsigned int savedMaxPage;
drh09419b42011-11-16 19:29:17 +0000873 pcache1EnterMutex(pGroup);
874 savedMaxPage = pGroup->nMaxPage;
875 pGroup->nMaxPage = 0;
drh957026a2015-07-16 18:18:19 +0000876 pcache1EnforceMaxPage(pCache);
drh09419b42011-11-16 19:29:17 +0000877 pGroup->nMaxPage = savedMaxPage;
878 pcache1LeaveMutex(pGroup);
879 }
880}
881
882/*
danielk1977bc2ca9e2008-11-13 14:28:28 +0000883** Implementation of the sqlite3_pcache.xPagecount method.
884*/
885static int pcache1Pagecount(sqlite3_pcache *p){
886 int n;
drh9f8cf9d2011-01-17 21:32:24 +0000887 PCache1 *pCache = (PCache1*)p;
888 pcache1EnterMutex(pCache->pGroup);
889 n = pCache->nPage;
890 pcache1LeaveMutex(pCache->pGroup);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000891 return n;
892}
893
drhefbf0442014-08-23 23:15:31 +0000894
895/*
896** Implement steps 3, 4, and 5 of the pcache1Fetch() algorithm described
897** in the header of the pcache1Fetch() procedure.
898**
899** This steps are broken out into a separate procedure because they are
900** usually not needed, and by avoiding the stack initialization required
901** for these steps, the main pcache1Fetch() procedure can run faster.
902*/
903static SQLITE_NOINLINE PgHdr1 *pcache1FetchStage2(
904 PCache1 *pCache,
905 unsigned int iKey,
906 int createFlag
907){
908 unsigned int nPinned;
909 PGroup *pGroup = pCache->pGroup;
910 PgHdr1 *pPage = 0;
911
912 /* Step 3: Abort if createFlag is 1 but the cache is nearly full */
913 assert( pCache->nPage >= pCache->nRecyclable );
914 nPinned = pCache->nPage - pCache->nRecyclable;
915 assert( pGroup->mxPinned == pGroup->nMaxPage + 10 - pGroup->nMinPage );
916 assert( pCache->n90pct == pCache->nMax*9/10 );
917 if( createFlag==1 && (
918 nPinned>=pGroup->mxPinned
919 || nPinned>=pCache->n90pct
dan5bd8af72014-10-10 19:10:59 +0000920 || (pcache1UnderMemoryPressure(pCache) && pCache->nRecyclable<nPinned)
drhefbf0442014-08-23 23:15:31 +0000921 )){
922 return 0;
923 }
924
925 if( pCache->nPage>=pCache->nHash ) pcache1ResizeHash(pCache);
926 assert( pCache->nHash>0 && pCache->apHash );
927
928 /* Step 4. Try to recycle a page. */
drhc54357c2015-07-07 14:06:18 +0000929 if( pCache->bPurgeable
drh92af02c2015-09-04 04:31:56 +0000930 && !pGroup->lru.pLruPrev->isAnchor
drhc54357c2015-07-07 14:06:18 +0000931 && ((pCache->nPage+1>=pCache->nMax) || pcache1UnderMemoryPressure(pCache))
932 ){
drhefbf0442014-08-23 23:15:31 +0000933 PCache1 *pOther;
drh92af02c2015-09-04 04:31:56 +0000934 pPage = pGroup->lru.pLruPrev;
drheabb67f2017-08-05 15:49:03 +0000935 assert( PAGE_IS_UNPINNED(pPage) );
drh95c91e12015-06-29 00:21:00 +0000936 pcache1RemoveFromHash(pPage, 0);
drhefbf0442014-08-23 23:15:31 +0000937 pcache1PinPage(pPage);
938 pOther = pPage->pCache;
drhee70a842015-07-06 18:54:52 +0000939 if( pOther->szAlloc != pCache->szAlloc ){
drhefbf0442014-08-23 23:15:31 +0000940 pcache1FreePage(pPage);
941 pPage = 0;
942 }else{
drh617b7b42017-08-30 04:44:59 +0000943 pGroup->nPurgeable -= (pOther->bPurgeable - pCache->bPurgeable);
drhefbf0442014-08-23 23:15:31 +0000944 }
945 }
946
947 /* Step 5. If a usable page buffer has still not been found,
948 ** attempt to allocate a new one.
949 */
950 if( !pPage ){
drh3c0c4312015-09-01 19:51:37 +0000951 pPage = pcache1AllocPage(pCache, createFlag==1);
drhefbf0442014-08-23 23:15:31 +0000952 }
953
954 if( pPage ){
955 unsigned int h = iKey % pCache->nHash;
956 pCache->nPage++;
957 pPage->iKey = iKey;
958 pPage->pNext = pCache->apHash[h];
959 pPage->pCache = pCache;
drhefbf0442014-08-23 23:15:31 +0000960 pPage->pLruNext = 0;
drhde72d2a2018-12-03 01:58:02 +0000961 /* pPage->pLruPrev = 0;
962 ** No need to clear pLruPrev since it is not accessed when pLruNext==0 */
drhefbf0442014-08-23 23:15:31 +0000963 *(void **)pPage->page.pExtra = 0;
964 pCache->apHash[h] = pPage;
965 if( iKey>pCache->iMaxKey ){
966 pCache->iMaxKey = iKey;
967 }
968 }
969 return pPage;
970}
971
danielk1977bc2ca9e2008-11-13 14:28:28 +0000972/*
973** Implementation of the sqlite3_pcache.xFetch method.
974**
975** Fetch a page by key value.
976**
977** Whether or not a new page may be allocated by this function depends on
drhf18a61d2009-07-17 11:44:07 +0000978** the value of the createFlag argument. 0 means do not allocate a new
979** page. 1 means allocate a new page if space is easily available. 2
980** means to try really hard to allocate a new page.
981**
982** For a non-purgeable cache (a cache used as the storage for an in-memory
983** database) there is really no difference between createFlag 1 and 2. So
984** the calling function (pcache.c) will never have a createFlag of 1 on
drh45d29302012-01-08 22:18:33 +0000985** a non-purgeable cache.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000986**
987** There are three different approaches to obtaining space for a page,
988** depending on the value of parameter createFlag (which may be 0, 1 or 2).
989**
990** 1. Regardless of the value of createFlag, the cache is searched for a
991** copy of the requested page. If one is found, it is returned.
992**
993** 2. If createFlag==0 and the page is not already in the cache, NULL is
994** returned.
995**
drh50d1b5f2010-08-27 12:21:06 +0000996** 3. If createFlag is 1, and the page is not already in the cache, then
997** return NULL (do not allocate a new page) if any of the following
998** conditions are true:
danielk1977bc2ca9e2008-11-13 14:28:28 +0000999**
1000** (a) the number of pages pinned by the cache is greater than
1001** PCache1.nMax, or
drh50d1b5f2010-08-27 12:21:06 +00001002**
danielk1977bc2ca9e2008-11-13 14:28:28 +00001003** (b) the number of pages pinned by the cache is greater than
1004** the sum of nMax for all purgeable caches, less the sum of
drh50d1b5f2010-08-27 12:21:06 +00001005** nMin for all other purgeable caches, or
danielk1977bc2ca9e2008-11-13 14:28:28 +00001006**
1007** 4. If none of the first three conditions apply and the cache is marked
1008** as purgeable, and if one of the following is true:
1009**
1010** (a) The number of pages allocated for the cache is already
1011** PCache1.nMax, or
1012**
1013** (b) The number of pages allocated for all purgeable caches is
1014** already equal to or greater than the sum of nMax for all
1015** purgeable caches,
1016**
drh50d1b5f2010-08-27 12:21:06 +00001017** (c) The system is under memory pressure and wants to avoid
1018** unnecessary pages cache entry allocations
1019**
danielk1977bc2ca9e2008-11-13 14:28:28 +00001020** then attempt to recycle a page from the LRU list. If it is the right
1021** size, return the recycled buffer. Otherwise, free the buffer and
1022** proceed to step 5.
1023**
1024** 5. Otherwise, allocate and return a new page buffer.
drh55a46c92015-06-12 13:49:26 +00001025**
1026** There are two versions of this routine. pcache1FetchWithMutex() is
1027** the general case. pcache1FetchNoMutex() is a faster implementation for
1028** the common case where pGroup->mutex is NULL. The pcache1Fetch() wrapper
1029** invokes the appropriate routine.
danielk1977bc2ca9e2008-11-13 14:28:28 +00001030*/
drh55a46c92015-06-12 13:49:26 +00001031static PgHdr1 *pcache1FetchNoMutex(
dan22e21ff2011-11-08 20:08:44 +00001032 sqlite3_pcache *p,
1033 unsigned int iKey,
1034 int createFlag
1035){
danielk1977bc2ca9e2008-11-13 14:28:28 +00001036 PCache1 *pCache = (PCache1 *)p;
1037 PgHdr1 *pPage = 0;
1038
drh3a5676c2011-01-19 21:58:56 +00001039 /* Step 1: Search the hash table for an existing entry. */
drhefbf0442014-08-23 23:15:31 +00001040 pPage = pCache->apHash[iKey % pCache->nHash];
1041 while( pPage && pPage->iKey!=iKey ){ pPage = pPage->pNext; }
danielk1977bc2ca9e2008-11-13 14:28:28 +00001042
drh95a0b372015-09-03 20:43:55 +00001043 /* Step 2: If the page was found in the hash table, then return it.
1044 ** If the page was not in the hash table and createFlag is 0, abort.
1045 ** Otherwise (page not in hash and createFlag!=0) continue with
1046 ** subsequent steps to try to create the page. */
drh5d56dd22013-12-13 18:50:40 +00001047 if( pPage ){
drheabb67f2017-08-05 15:49:03 +00001048 if( PAGE_IS_UNPINNED(pPage) ){
drh55a46c92015-06-12 13:49:26 +00001049 return pcache1PinPage(pPage);
1050 }else{
1051 return pPage;
1052 }
drhefbf0442014-08-23 23:15:31 +00001053 }else if( createFlag ){
1054 /* Steps 3, 4, and 5 implemented by this subroutine */
drh55a46c92015-06-12 13:49:26 +00001055 return pcache1FetchStage2(pCache, iKey, createFlag);
1056 }else{
1057 return 0;
drh5d56dd22013-12-13 18:50:40 +00001058 }
drh55a46c92015-06-12 13:49:26 +00001059}
drh982215a2015-06-13 11:10:55 +00001060#if PCACHE1_MIGHT_USE_GROUP_MUTEX
drh55a46c92015-06-12 13:49:26 +00001061static PgHdr1 *pcache1FetchWithMutex(
1062 sqlite3_pcache *p,
1063 unsigned int iKey,
1064 int createFlag
1065){
1066 PCache1 *pCache = (PCache1 *)p;
1067 PgHdr1 *pPage;
1068
1069 pcache1EnterMutex(pCache->pGroup);
1070 pPage = pcache1FetchNoMutex(p, iKey, createFlag);
drhefbf0442014-08-23 23:15:31 +00001071 assert( pPage==0 || pCache->iMaxKey>=iKey );
1072 pcache1LeaveMutex(pCache->pGroup);
drh55a46c92015-06-12 13:49:26 +00001073 return pPage;
1074}
drh982215a2015-06-13 11:10:55 +00001075#endif
drh55a46c92015-06-12 13:49:26 +00001076static sqlite3_pcache_page *pcache1Fetch(
1077 sqlite3_pcache *p,
1078 unsigned int iKey,
1079 int createFlag
1080){
drh982215a2015-06-13 11:10:55 +00001081#if PCACHE1_MIGHT_USE_GROUP_MUTEX || defined(SQLITE_DEBUG)
drh55a46c92015-06-12 13:49:26 +00001082 PCache1 *pCache = (PCache1 *)p;
drh982215a2015-06-13 11:10:55 +00001083#endif
drh55a46c92015-06-12 13:49:26 +00001084
1085 assert( offsetof(PgHdr1,page)==0 );
1086 assert( pCache->bPurgeable || createFlag!=1 );
1087 assert( pCache->bPurgeable || pCache->nMin==0 );
1088 assert( pCache->bPurgeable==0 || pCache->nMin==10 );
1089 assert( pCache->nMin==0 || pCache->bPurgeable );
1090 assert( pCache->nHash>0 );
drh982215a2015-06-13 11:10:55 +00001091#if PCACHE1_MIGHT_USE_GROUP_MUTEX
drh55a46c92015-06-12 13:49:26 +00001092 if( pCache->pGroup->mutex ){
1093 return (sqlite3_pcache_page*)pcache1FetchWithMutex(p, iKey, createFlag);
drh982215a2015-06-13 11:10:55 +00001094 }else
1095#endif
1096 {
drh55a46c92015-06-12 13:49:26 +00001097 return (sqlite3_pcache_page*)pcache1FetchNoMutex(p, iKey, createFlag);
1098 }
danielk1977bc2ca9e2008-11-13 14:28:28 +00001099}
1100
1101
1102/*
1103** Implementation of the sqlite3_pcache.xUnpin method.
1104**
1105** Mark a page as unpinned (eligible for asynchronous recycling).
1106*/
dan22e21ff2011-11-08 20:08:44 +00001107static void pcache1Unpin(
1108 sqlite3_pcache *p,
1109 sqlite3_pcache_page *pPg,
1110 int reuseUnlikely
1111){
danielk1977bc2ca9e2008-11-13 14:28:28 +00001112 PCache1 *pCache = (PCache1 *)p;
dan22e21ff2011-11-08 20:08:44 +00001113 PgHdr1 *pPage = (PgHdr1 *)pPg;
drh9f8cf9d2011-01-17 21:32:24 +00001114 PGroup *pGroup = pCache->pGroup;
drh69e931e2009-06-03 21:04:35 +00001115
1116 assert( pPage->pCache==pCache );
drh9f8cf9d2011-01-17 21:32:24 +00001117 pcache1EnterMutex(pGroup);
danielk1977bc2ca9e2008-11-13 14:28:28 +00001118
1119 /* It is an error to call this function if the page is already
drh9f8cf9d2011-01-17 21:32:24 +00001120 ** part of the PGroup LRU list.
danielk1977bc2ca9e2008-11-13 14:28:28 +00001121 */
drhde72d2a2018-12-03 01:58:02 +00001122 assert( pPage->pLruNext==0 );
drheabb67f2017-08-05 15:49:03 +00001123 assert( PAGE_IS_PINNED(pPage) );
danielk1977bc2ca9e2008-11-13 14:28:28 +00001124
drh617b7b42017-08-30 04:44:59 +00001125 if( reuseUnlikely || pGroup->nPurgeable>pGroup->nMaxPage ){
drh95c91e12015-06-29 00:21:00 +00001126 pcache1RemoveFromHash(pPage, 1);
danielk1977bc2ca9e2008-11-13 14:28:28 +00001127 }else{
drh9f8cf9d2011-01-17 21:32:24 +00001128 /* Add the page to the PGroup LRU list. */
drh92af02c2015-09-04 04:31:56 +00001129 PgHdr1 **ppFirst = &pGroup->lru.pLruNext;
1130 pPage->pLruPrev = &pGroup->lru;
1131 (pPage->pLruNext = *ppFirst)->pLruPrev = pPage;
1132 *ppFirst = pPage;
danielk1977bc2ca9e2008-11-13 14:28:28 +00001133 pCache->nRecyclable++;
1134 }
1135
drh9f8cf9d2011-01-17 21:32:24 +00001136 pcache1LeaveMutex(pCache->pGroup);
danielk1977bc2ca9e2008-11-13 14:28:28 +00001137}
1138
1139/*
1140** Implementation of the sqlite3_pcache.xRekey method.
1141*/
1142static void pcache1Rekey(
1143 sqlite3_pcache *p,
dan22e21ff2011-11-08 20:08:44 +00001144 sqlite3_pcache_page *pPg,
danielk1977bc2ca9e2008-11-13 14:28:28 +00001145 unsigned int iOld,
1146 unsigned int iNew
1147){
1148 PCache1 *pCache = (PCache1 *)p;
dan22e21ff2011-11-08 20:08:44 +00001149 PgHdr1 *pPage = (PgHdr1 *)pPg;
danielk1977bc2ca9e2008-11-13 14:28:28 +00001150 PgHdr1 **pp;
drh4e9bf5a2022-09-02 14:29:54 +00001151 unsigned int hOld, hNew;
danielk1977bc2ca9e2008-11-13 14:28:28 +00001152 assert( pPage->iKey==iOld );
drh69e931e2009-06-03 21:04:35 +00001153 assert( pPage->pCache==pCache );
drh9c3a1142022-08-31 15:04:42 +00001154 assert( iOld!=iNew ); /* The page number really is changing */
danielk1977bc2ca9e2008-11-13 14:28:28 +00001155
drh9f8cf9d2011-01-17 21:32:24 +00001156 pcache1EnterMutex(pCache->pGroup);
drh8c983dd2022-09-07 19:28:18 +00001157
drh9c3a1142022-08-31 15:04:42 +00001158 assert( pcache1FetchNoMutex(p, iOld, 0)==pPage ); /* pPg really is iOld */
drh4e9bf5a2022-09-02 14:29:54 +00001159 hOld = iOld%pCache->nHash;
1160 pp = &pCache->apHash[hOld];
danielk1977bc2ca9e2008-11-13 14:28:28 +00001161 while( (*pp)!=pPage ){
1162 pp = &(*pp)->pNext;
1163 }
1164 *pp = pPage->pNext;
1165
drhb53e0992022-09-07 20:11:22 +00001166 assert( pcache1FetchNoMutex(p, iNew, 0)==0 ); /* iNew not in cache */
drh4e9bf5a2022-09-02 14:29:54 +00001167 hNew = iNew%pCache->nHash;
danielk1977bc2ca9e2008-11-13 14:28:28 +00001168 pPage->iKey = iNew;
drh4e9bf5a2022-09-02 14:29:54 +00001169 pPage->pNext = pCache->apHash[hNew];
1170 pCache->apHash[hNew] = pPage;
drh98829a62009-11-20 13:18:14 +00001171 if( iNew>pCache->iMaxKey ){
danielk1977f90b7262009-01-07 15:18:20 +00001172 pCache->iMaxKey = iNew;
1173 }
1174
drh9f8cf9d2011-01-17 21:32:24 +00001175 pcache1LeaveMutex(pCache->pGroup);
danielk1977bc2ca9e2008-11-13 14:28:28 +00001176}
1177
1178/*
1179** Implementation of the sqlite3_pcache.xTruncate method.
1180**
1181** Discard all unpinned pages in the cache with a page number equal to
1182** or greater than parameter iLimit. Any pinned pages with a page number
1183** equal to or greater than iLimit are implicitly unpinned.
1184*/
1185static void pcache1Truncate(sqlite3_pcache *p, unsigned int iLimit){
1186 PCache1 *pCache = (PCache1 *)p;
drh9f8cf9d2011-01-17 21:32:24 +00001187 pcache1EnterMutex(pCache->pGroup);
danielk1977f90b7262009-01-07 15:18:20 +00001188 if( iLimit<=pCache->iMaxKey ){
1189 pcache1TruncateUnsafe(pCache, iLimit);
1190 pCache->iMaxKey = iLimit-1;
1191 }
drh9f8cf9d2011-01-17 21:32:24 +00001192 pcache1LeaveMutex(pCache->pGroup);
danielk1977bc2ca9e2008-11-13 14:28:28 +00001193}
1194
1195/*
1196** Implementation of the sqlite3_pcache.xDestroy method.
1197**
1198** Destroy a cache allocated using pcache1Create().
1199*/
1200static void pcache1Destroy(sqlite3_pcache *p){
1201 PCache1 *pCache = (PCache1 *)p;
drh9f8cf9d2011-01-17 21:32:24 +00001202 PGroup *pGroup = pCache->pGroup;
danb51d2fa2010-09-22 19:06:02 +00001203 assert( pCache->bPurgeable || (pCache->nMax==0 && pCache->nMin==0) );
drh9f8cf9d2011-01-17 21:32:24 +00001204 pcache1EnterMutex(pGroup);
drhd9fabbc2016-08-10 11:50:12 +00001205 if( pCache->nPage ) pcache1TruncateUnsafe(pCache, 0);
dan9e865902023-01-25 17:09:16 +00001206 if( pcache1.separateCache ){
1207 PgHdr1 *p1 = pCache->pFree;
1208 while( p1 ){
1209 PgHdr1 *pNext = p1->pNext;
1210 if( p1->isBulkLocal==0 ) pcache1Free(p1->page.pBuf);
1211 p1 = pNext;
1212 }
1213 }
drha69085c2012-01-02 18:00:55 +00001214 assert( pGroup->nMaxPage >= pCache->nMax );
drh9f8cf9d2011-01-17 21:32:24 +00001215 pGroup->nMaxPage -= pCache->nMax;
drha69085c2012-01-02 18:00:55 +00001216 assert( pGroup->nMinPage >= pCache->nMin );
drh9f8cf9d2011-01-17 21:32:24 +00001217 pGroup->nMinPage -= pCache->nMin;
drh41692e92011-01-25 04:34:51 +00001218 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
drh957026a2015-07-16 18:18:19 +00001219 pcache1EnforceMaxPage(pCache);
drh9f8cf9d2011-01-17 21:32:24 +00001220 pcache1LeaveMutex(pGroup);
drhee70a842015-07-06 18:54:52 +00001221 sqlite3_free(pCache->pBulk);
danielk1977bc2ca9e2008-11-13 14:28:28 +00001222 sqlite3_free(pCache->apHash);
1223 sqlite3_free(pCache);
1224}
1225
1226/*
1227** This function is called during initialization (sqlite3_initialize()) to
1228** install the default pluggable cache module, assuming the user has not
1229** already provided an alternative.
1230*/
1231void sqlite3PCacheSetDefault(void){
dan22e21ff2011-11-08 20:08:44 +00001232 static const sqlite3_pcache_methods2 defaultMethods = {
drh81ef0f92011-11-13 21:44:03 +00001233 1, /* iVersion */
danielk1977bc2ca9e2008-11-13 14:28:28 +00001234 0, /* pArg */
1235 pcache1Init, /* xInit */
1236 pcache1Shutdown, /* xShutdown */
1237 pcache1Create, /* xCreate */
1238 pcache1Cachesize, /* xCachesize */
1239 pcache1Pagecount, /* xPagecount */
1240 pcache1Fetch, /* xFetch */
1241 pcache1Unpin, /* xUnpin */
1242 pcache1Rekey, /* xRekey */
1243 pcache1Truncate, /* xTruncate */
drh09419b42011-11-16 19:29:17 +00001244 pcache1Destroy, /* xDestroy */
1245 pcache1Shrink /* xShrink */
danielk1977bc2ca9e2008-11-13 14:28:28 +00001246 };
dan22e21ff2011-11-08 20:08:44 +00001247 sqlite3_config(SQLITE_CONFIG_PCACHE2, &defaultMethods);
danielk1977bc2ca9e2008-11-13 14:28:28 +00001248}
1249
drhdef68892014-11-04 12:11:23 +00001250/*
1251** Return the size of the header on each page of this PCACHE implementation.
1252*/
drh37c057b2014-12-30 00:57:29 +00001253int sqlite3HeaderSizePcache1(void){ return ROUND8(sizeof(PgHdr1)); }
drhdef68892014-11-04 12:11:23 +00001254
drhaf89fe62015-03-23 17:25:18 +00001255/*
1256** Return the global mutex used by this PCACHE implementation. The
1257** sqlite3_status() routine needs access to this mutex.
1258*/
1259sqlite3_mutex *sqlite3Pcache1Mutex(void){
1260 return pcache1.mutex;
1261}
1262
danielk1977bc2ca9e2008-11-13 14:28:28 +00001263#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
1264/*
1265** This function is called to free superfluous dynamically allocated memory
1266** held by the pager system. Memory in use by any SQLite pager allocated
1267** by the current thread may be sqlite3_free()ed.
1268**
1269** nReq is the number of bytes of memory required. Once this much has
1270** been released, the function returns. The return value is the total number
1271** of bytes of memory released.
1272*/
1273int sqlite3PcacheReleaseMemory(int nReq){
1274 int nFree = 0;
drh9f8cf9d2011-01-17 21:32:24 +00001275 assert( sqlite3_mutex_notheld(pcache1.grp.mutex) );
1276 assert( sqlite3_mutex_notheld(pcache1.mutex) );
drhbf962282017-03-29 15:18:40 +00001277 if( sqlite3GlobalConfig.pPage==0 ){
danielk1977bc2ca9e2008-11-13 14:28:28 +00001278 PgHdr1 *p;
drh9f8cf9d2011-01-17 21:32:24 +00001279 pcache1EnterMutex(&pcache1.grp);
drh92af02c2015-09-04 04:31:56 +00001280 while( (nReq<0 || nFree<nReq)
drh88202502015-09-09 19:27:10 +00001281 && (p=pcache1.grp.lru.pLruPrev)!=0
1282 && p->isAnchor==0
drh92af02c2015-09-04 04:31:56 +00001283 ){
dan22e21ff2011-11-08 20:08:44 +00001284 nFree += pcache1MemSize(p->page.pBuf);
drheabb67f2017-08-05 15:49:03 +00001285 assert( PAGE_IS_UNPINNED(p) );
danielk1977bc2ca9e2008-11-13 14:28:28 +00001286 pcache1PinPage(p);
drh95c91e12015-06-29 00:21:00 +00001287 pcache1RemoveFromHash(p, 1);
danielk1977bc2ca9e2008-11-13 14:28:28 +00001288 }
drh9f8cf9d2011-01-17 21:32:24 +00001289 pcache1LeaveMutex(&pcache1.grp);
danielk1977bc2ca9e2008-11-13 14:28:28 +00001290 }
1291 return nFree;
1292}
1293#endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
1294
1295#ifdef SQLITE_TEST
1296/*
1297** This function is used by test procedures to inspect the internal state
1298** of the global cache.
1299*/
1300void sqlite3PcacheStats(
1301 int *pnCurrent, /* OUT: Total number of pages cached */
1302 int *pnMax, /* OUT: Global maximum cache size */
1303 int *pnMin, /* OUT: Sum of PCache1.nMin for purgeable caches */
1304 int *pnRecyclable /* OUT: Total number of pages available for recycling */
1305){
1306 PgHdr1 *p;
1307 int nRecyclable = 0;
drh0b19c962015-09-10 19:22:25 +00001308 for(p=pcache1.grp.lru.pLruNext; p && !p->isAnchor; p=p->pLruNext){
drheabb67f2017-08-05 15:49:03 +00001309 assert( PAGE_IS_UNPINNED(p) );
danielk1977bc2ca9e2008-11-13 14:28:28 +00001310 nRecyclable++;
1311 }
drh617b7b42017-08-30 04:44:59 +00001312 *pnCurrent = pcache1.grp.nPurgeable;
drha69085c2012-01-02 18:00:55 +00001313 *pnMax = (int)pcache1.grp.nMaxPage;
1314 *pnMin = (int)pcache1.grp.nMinPage;
danielk1977bc2ca9e2008-11-13 14:28:28 +00001315 *pnRecyclable = nRecyclable;
1316}
1317#endif