blob: 14d1e7cde0fca5fec4ae9dd09872316a46a45875 [file] [log] [blame]
danielk19778c0a7912008-08-20 14:49:23 +00001/*
2** 2008 August 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** This file implements that page cache.
danielk19778c0a7912008-08-20 14:49:23 +000013*/
14#include "sqliteInt.h"
15
16/*
drh72e6a392016-05-11 23:54:14 +000017** A complete page cache is an instance of this structure. Every
18** entry in the cache holds a single page of the database file. The
19** btree layer only operates on the cached copy of the database pages.
20**
21** A page cache entry is "clean" if it exactly matches what is currently
22** on disk. A page is "dirty" if it has been modified and needs to be
23** persisted to disk.
danb2ef9002016-05-11 15:41:15 +000024**
25** pDirty, pDirtyTail, pSynced:
26** All dirty pages are linked into the doubly linked list using
27** PgHdr.pDirtyNext and pDirtyPrev. The list is maintained in LRU order
28** such that p was added to the list more recently than p->pDirtyNext.
29** PCache.pDirty points to the first (newest) element in the list and
30** pDirtyTail to the last (oldest).
31**
32** The PCache.pSynced variable is used to optimize searching for a dirty
33** page to eject from the cache mid-transaction. It is better to eject
34** a page that does not require a journal sync than one that does.
drh5eaebf32018-09-21 19:06:09 +000035** Therefore, pSynced is maintained so that it *almost* always points
danb2ef9002016-05-11 15:41:15 +000036** to either the oldest page in the pDirty/pDirtyTail list that has a
37** clear PGHDR_NEED_SYNC flag or to a page that is older than this one
38** (so that the right page to eject can be found by following pDirtyPrev
39** pointers).
danielk19778c0a7912008-08-20 14:49:23 +000040*/
41struct PCache {
danielk1977d491e1b2008-08-26 18:05:48 +000042 PgHdr *pDirty, *pDirtyTail; /* List of dirty pages in LRU order */
43 PgHdr *pSynced; /* Last synced page in dirty page list */
drh95a0b372015-09-03 20:43:55 +000044 int nRefSum; /* Sum of ref counts over all pages */
drh3b42abb2011-11-09 14:23:04 +000045 int szCache; /* Configured cache size */
drh9b0cf342015-11-12 14:57:19 +000046 int szSpill; /* Size before spilling occurs */
drha85f7e32008-08-28 02:26:07 +000047 int szPage; /* Size of every page in this cache */
48 int szExtra; /* Size of extra space for each page */
drhfe21a792014-02-03 17:04:29 +000049 u8 bPurgeable; /* True if pages are on backing store */
50 u8 eCreate; /* eCreate value for for xFetch() */
drha85f7e32008-08-28 02:26:07 +000051 int (*xStress)(void*,PgHdr*); /* Call to try make a page clean */
52 void *pStress; /* Argument to xStress */
danielk1977bc2ca9e2008-11-13 14:28:28 +000053 sqlite3_pcache *pCache; /* Pluggable cache module */
danielk19778c0a7912008-08-20 14:49:23 +000054};
55
drha0f6b122016-05-13 15:22:06 +000056/********************************** Test and Debug Logic **********************/
drh5c8e0922016-05-11 10:57:04 +000057/*
drha0f6b122016-05-13 15:22:06 +000058** Debug tracing macros. Enable by by changing the "0" to "1" and
59** recompiling.
60**
61** When sqlite3PcacheTrace is 1, single line trace messages are issued.
62** When sqlite3PcacheTrace is 2, a dump of the pcache showing all cache entries
63** is displayed for many operations, resulting in a lot of output.
drh5c8e0922016-05-11 10:57:04 +000064*/
65#if defined(SQLITE_DEBUG) && 0
drha0f6b122016-05-13 15:22:06 +000066 int sqlite3PcacheTrace = 2; /* 0: off 1: simple 2: cache dumps */
67 int sqlite3PcacheMxDump = 9999; /* Max cache entries for pcacheDump() */
drh5c8e0922016-05-11 10:57:04 +000068# define pcacheTrace(X) if(sqlite3PcacheTrace){sqlite3DebugPrintf X;}
drh7aeb2162016-05-13 04:24:25 +000069 void pcacheDump(PCache *pCache){
70 int N;
71 int i, j;
72 sqlite3_pcache_page *pLower;
73 PgHdr *pPg;
74 unsigned char *a;
75
76 if( sqlite3PcacheTrace<2 ) return;
77 if( pCache->pCache==0 ) return;
78 N = sqlite3PcachePagecount(pCache);
drha0f6b122016-05-13 15:22:06 +000079 if( N>sqlite3PcacheMxDump ) N = sqlite3PcacheMxDump;
drh7aeb2162016-05-13 04:24:25 +000080 for(i=1; i<=N; i++){
81 pLower = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, i, 0);
82 if( pLower==0 ) continue;
83 pPg = (PgHdr*)pLower->pExtra;
84 printf("%3d: nRef %2d flgs %02x data ", i, pPg->nRef, pPg->flags);
85 a = (unsigned char *)pLower->pBuf;
86 for(j=0; j<12; j++) printf("%02x", a[j]);
87 printf("\n");
88 if( pPg->pPage==0 ){
89 sqlite3GlobalConfig.pcache2.xUnpin(pCache->pCache, pLower, 0);
90 }
91 }
92 }
93 #else
drh5c8e0922016-05-11 10:57:04 +000094# define pcacheTrace(X)
drh7aeb2162016-05-13 04:24:25 +000095# define pcacheDump(X)
drh5c8e0922016-05-11 10:57:04 +000096#endif
97
drha0f6b122016-05-13 15:22:06 +000098/*
99** Check invariants on a PgHdr entry. Return true if everything is OK.
100** Return false if any invariant is violated.
101**
102** This routine is for use inside of assert() statements only. For
103** example:
104**
105** assert( sqlite3PcachePageSanity(pPg) );
106*/
drhd879e3e2017-02-13 13:35:55 +0000107#ifdef SQLITE_DEBUG
drha0f6b122016-05-13 15:22:06 +0000108int sqlite3PcachePageSanity(PgHdr *pPg){
109 PCache *pCache;
110 assert( pPg!=0 );
drhcbed6042016-12-13 18:34:01 +0000111 assert( pPg->pgno>0 || pPg->pPager==0 ); /* Page number is 1 or more */
drha0f6b122016-05-13 15:22:06 +0000112 pCache = pPg->pCache;
113 assert( pCache!=0 ); /* Every page has an associated PCache */
114 if( pPg->flags & PGHDR_CLEAN ){
115 assert( (pPg->flags & PGHDR_DIRTY)==0 );/* Cannot be both CLEAN and DIRTY */
116 assert( pCache->pDirty!=pPg ); /* CLEAN pages not on dirty list */
117 assert( pCache->pDirtyTail!=pPg );
118 }
119 /* WRITEABLE pages must also be DIRTY */
120 if( pPg->flags & PGHDR_WRITEABLE ){
121 assert( pPg->flags & PGHDR_DIRTY ); /* WRITEABLE implies DIRTY */
122 }
123 /* NEED_SYNC can be set independently of WRITEABLE. This can happen,
124 ** for example, when using the sqlite3PagerDontWrite() optimization:
125 ** (1) Page X is journalled, and gets WRITEABLE and NEED_SEEK.
126 ** (2) Page X moved to freelist, WRITEABLE is cleared
127 ** (3) Page X reused, WRITEABLE is set again
128 ** If NEED_SYNC had been cleared in step 2, then it would not be reset
129 ** in step 3, and page might be written into the database without first
130 ** syncing the rollback journal, which might cause corruption on a power
131 ** loss.
drh3b02a072016-05-13 17:22:33 +0000132 **
133 ** Another example is when the database page size is smaller than the
134 ** disk sector size. When any page of a sector is journalled, all pages
135 ** in that sector are marked NEED_SYNC even if they are still CLEAN, just
136 ** in case they are later modified, since all pages in the same sector
137 ** must be journalled and synced before any of those pages can be safely
138 ** written.
drha0f6b122016-05-13 15:22:06 +0000139 */
140 return 1;
141}
142#endif /* SQLITE_DEBUG */
143
144
danielk19778c0a7912008-08-20 14:49:23 +0000145/********************************** Linked List Management ********************/
146
drha8dcba92014-08-22 20:35:29 +0000147/* Allowed values for second argument to pcacheManageDirtyList() */
148#define PCACHE_DIRTYLIST_REMOVE 1 /* Remove pPage from dirty list */
149#define PCACHE_DIRTYLIST_ADD 2 /* Add pPage to the dirty list */
150#define PCACHE_DIRTYLIST_FRONT 3 /* Move pPage to the front of the list */
danielk19778c0a7912008-08-20 14:49:23 +0000151
152/*
drha8dcba92014-08-22 20:35:29 +0000153** Manage pPage's participation on the dirty list. Bits of the addRemove
154** argument determines what operation to do. The 0x01 bit means first
155** remove pPage from the dirty list. The 0x02 means add pPage back to
156** the dirty list. Doing both moves pPage to the front of the dirty list.
danielk19778c0a7912008-08-20 14:49:23 +0000157*/
drha8dcba92014-08-22 20:35:29 +0000158static void pcacheManageDirtyList(PgHdr *pPage, u8 addRemove){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000159 PCache *p = pPage->pCache;
danielk19778c0a7912008-08-20 14:49:23 +0000160
drh5c8e0922016-05-11 10:57:04 +0000161 pcacheTrace(("%p.DIRTYLIST.%s %d\n", p,
162 addRemove==1 ? "REMOVE" : addRemove==2 ? "ADD" : "FRONT",
163 pPage->pgno));
drha8dcba92014-08-22 20:35:29 +0000164 if( addRemove & PCACHE_DIRTYLIST_REMOVE ){
165 assert( pPage->pDirtyNext || pPage==p->pDirtyTail );
166 assert( pPage->pDirtyPrev || pPage==p->pDirty );
167
168 /* Update the PCache1.pSynced variable if necessary. */
169 if( p->pSynced==pPage ){
danb2ef9002016-05-11 15:41:15 +0000170 p->pSynced = pPage->pDirtyPrev;
drha8dcba92014-08-22 20:35:29 +0000171 }
172
173 if( pPage->pDirtyNext ){
174 pPage->pDirtyNext->pDirtyPrev = pPage->pDirtyPrev;
175 }else{
176 assert( pPage==p->pDirtyTail );
177 p->pDirtyTail = pPage->pDirtyPrev;
178 }
179 if( pPage->pDirtyPrev ){
180 pPage->pDirtyPrev->pDirtyNext = pPage->pDirtyNext;
181 }else{
dan401907e2016-05-11 20:03:23 +0000182 /* If there are now no dirty pages in the cache, set eCreate to 2.
183 ** This is an optimization that allows sqlite3PcacheFetch() to skip
184 ** searching for a dirty page to eject from the cache when it might
185 ** otherwise have to. */
drha8dcba92014-08-22 20:35:29 +0000186 assert( pPage==p->pDirty );
187 p->pDirty = pPage->pDirtyNext;
dan401907e2016-05-11 20:03:23 +0000188 assert( p->bPurgeable || p->eCreate==2 );
189 if( p->pDirty==0 ){ /*OPTIMIZATION-IF-TRUE*/
190 assert( p->bPurgeable==0 || p->eCreate==1 );
drha8dcba92014-08-22 20:35:29 +0000191 p->eCreate = 2;
192 }
193 }
danielk19778c0a7912008-08-20 14:49:23 +0000194 }
drha8dcba92014-08-22 20:35:29 +0000195 if( addRemove & PCACHE_DIRTYLIST_ADD ){
drhf0dae6d2017-09-01 12:18:41 +0000196 pPage->pDirtyPrev = 0;
drha8dcba92014-08-22 20:35:29 +0000197 pPage->pDirtyNext = p->pDirty;
198 if( pPage->pDirtyNext ){
199 assert( pPage->pDirtyNext->pDirtyPrev==0 );
200 pPage->pDirtyNext->pDirtyPrev = pPage;
drh36ce9192014-09-12 20:30:59 +0000201 }else{
202 p->pDirtyTail = pPage;
203 if( p->bPurgeable ){
204 assert( p->eCreate==2 );
205 p->eCreate = 1;
206 }
drha8dcba92014-08-22 20:35:29 +0000207 }
208 p->pDirty = pPage;
dan613723d2016-05-12 09:48:23 +0000209
210 /* If pSynced is NULL and this page has a clear NEED_SYNC flag, set
211 ** pSynced to point to it. Checking the NEED_SYNC flag is an
212 ** optimization, as if pSynced points to a page with the NEED_SYNC
213 ** flag set sqlite3PcacheFetchStress() searches through all newer
214 ** entries of the dirty-list for a page with NEED_SYNC clear anyway. */
215 if( !p->pSynced
216 && 0==(pPage->flags&PGHDR_NEED_SYNC) /*OPTIMIZATION-IF-FALSE*/
217 ){
drha8dcba92014-08-22 20:35:29 +0000218 p->pSynced = pPage;
219 }
danielk1977bc2ca9e2008-11-13 14:28:28 +0000220 }
drh7aeb2162016-05-13 04:24:25 +0000221 pcacheDump(p);
danielk19778c0a7912008-08-20 14:49:23 +0000222}
223
224/*
danielk1977bc2ca9e2008-11-13 14:28:28 +0000225** Wrapper around the pluggable caches xUnpin method. If the cache is
226** being used for an in-memory database, this function is a no-op.
danielk19778c0a7912008-08-20 14:49:23 +0000227*/
danielk1977bc2ca9e2008-11-13 14:28:28 +0000228static void pcacheUnpin(PgHdr *p){
drha8dcba92014-08-22 20:35:29 +0000229 if( p->pCache->bPurgeable ){
drh5c8e0922016-05-11 10:57:04 +0000230 pcacheTrace(("%p.UNPIN %d\n", p->pCache, p->pgno));
drha8dcba92014-08-22 20:35:29 +0000231 sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 0);
drh7aeb2162016-05-13 04:24:25 +0000232 pcacheDump(p->pCache);
danielk19778c0a7912008-08-20 14:49:23 +0000233 }
danielk19778c0a7912008-08-20 14:49:23 +0000234}
235
drhc3031c62014-08-26 15:06:49 +0000236/*
drh9b0cf342015-11-12 14:57:19 +0000237** Compute the number of pages of cache requested. p->szCache is the
drhe0e84292015-02-27 21:53:35 +0000238** cache size requested by the "PRAGMA cache_size" statement.
drhc3031c62014-08-26 15:06:49 +0000239*/
240static int numberOfCachePages(PCache *p){
241 if( p->szCache>=0 ){
drhe0e84292015-02-27 21:53:35 +0000242 /* IMPLEMENTATION-OF: R-42059-47211 If the argument N is positive then the
243 ** suggested cache size is set to N. */
drhc3031c62014-08-26 15:06:49 +0000244 return p->szCache;
245 }else{
drhe7e95392021-09-03 18:11:12 +0000246 i64 n;
drh0ce974d2019-06-12 22:46:04 +0000247 /* IMPLEMANTATION-OF: R-59858-46238 If the argument N is negative, then the
248 ** number of cache pages is adjusted to be a number of pages that would
249 ** use approximately abs(N*1024) bytes of memory based on the current
250 ** page size. */
drhe7e95392021-09-03 18:11:12 +0000251 n = ((-1024*(i64)p->szCache)/(p->szPage+p->szExtra));
252 if( n>1000000000 ) n = 1000000000;
253 return (int)n;
drhc3031c62014-08-26 15:06:49 +0000254 }
255}
256
danielk19778c0a7912008-08-20 14:49:23 +0000257/*************************************************** General Interfaces ******
258**
259** Initialize and shutdown the page cache subsystem. Neither of these
260** functions are threadsafe.
261*/
262int sqlite3PcacheInitialize(void){
dan22e21ff2011-11-08 20:08:44 +0000263 if( sqlite3GlobalConfig.pcache2.xInit==0 ){
drhf759bb82010-09-09 18:25:34 +0000264 /* IMPLEMENTATION-OF: R-26801-64137 If the xInit() method is NULL, then the
265 ** built-in default page cache is used instead of the application defined
266 ** page cache. */
danielk1977bc2ca9e2008-11-13 14:28:28 +0000267 sqlite3PCacheSetDefault();
drh55f66b32019-07-16 19:44:32 +0000268 assert( sqlite3GlobalConfig.pcache2.xInit!=0 );
danielk19778c0a7912008-08-20 14:49:23 +0000269 }
dan22e21ff2011-11-08 20:08:44 +0000270 return sqlite3GlobalConfig.pcache2.xInit(sqlite3GlobalConfig.pcache2.pArg);
danielk19778c0a7912008-08-20 14:49:23 +0000271}
272void sqlite3PcacheShutdown(void){
dan22e21ff2011-11-08 20:08:44 +0000273 if( sqlite3GlobalConfig.pcache2.xShutdown ){
drhf759bb82010-09-09 18:25:34 +0000274 /* IMPLEMENTATION-OF: R-26000-56589 The xShutdown() method may be NULL. */
dan22e21ff2011-11-08 20:08:44 +0000275 sqlite3GlobalConfig.pcache2.xShutdown(sqlite3GlobalConfig.pcache2.pArg);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000276 }
danielk19778c0a7912008-08-20 14:49:23 +0000277}
278
279/*
280** Return the size in bytes of a PCache object.
281*/
282int sqlite3PcacheSize(void){ return sizeof(PCache); }
283
284/*
danielk1977bc2ca9e2008-11-13 14:28:28 +0000285** Create a new PCache object. Storage space to hold the object
286** has already been allocated and is passed in as the p pointer.
287** The caller discovers how much space needs to be allocated by
288** calling sqlite3PcacheSize().
drha2ee5892016-12-09 16:02:00 +0000289**
290** szExtra is some extra space allocated for each page. The first
291** 8 bytes of the extra space will be zeroed as the page is allocated,
292** but remaining content will be uninitialized. Though it is opaque
293** to this module, the extra space really ends up being the MemPage
294** structure in the pager.
danielk19778c0a7912008-08-20 14:49:23 +0000295*/
drhc3031c62014-08-26 15:06:49 +0000296int sqlite3PcacheOpen(
danielk19778c0a7912008-08-20 14:49:23 +0000297 int szPage, /* Size of every page */
298 int szExtra, /* Extra space associated with each page */
299 int bPurgeable, /* True if pages are on backing store */
danielk1977a858aa22008-08-22 16:22:17 +0000300 int (*xStress)(void*,PgHdr*),/* Call to try to make pages clean */
danielk19778c0a7912008-08-20 14:49:23 +0000301 void *pStress, /* Argument to xStress */
302 PCache *p /* Preallocated space for the PCache */
303){
danielk19778c0a7912008-08-20 14:49:23 +0000304 memset(p, 0, sizeof(PCache));
drhc3031c62014-08-26 15:06:49 +0000305 p->szPage = 1;
danielk19778c0a7912008-08-20 14:49:23 +0000306 p->szExtra = szExtra;
drha2ee5892016-12-09 16:02:00 +0000307 assert( szExtra>=8 ); /* First 8 bytes will be zeroed */
danielk19778c0a7912008-08-20 14:49:23 +0000308 p->bPurgeable = bPurgeable;
drhfe21a792014-02-03 17:04:29 +0000309 p->eCreate = 2;
danielk19778c0a7912008-08-20 14:49:23 +0000310 p->xStress = xStress;
311 p->pStress = pStress;
drh3b42abb2011-11-09 14:23:04 +0000312 p->szCache = 100;
drh9b0cf342015-11-12 14:57:19 +0000313 p->szSpill = 1;
drh5c8e0922016-05-11 10:57:04 +0000314 pcacheTrace(("%p.OPEN szPage %d bPurgeable %d\n",p,szPage,bPurgeable));
drhc3031c62014-08-26 15:06:49 +0000315 return sqlite3PcacheSetPageSize(p, szPage);
danielk19778c0a7912008-08-20 14:49:23 +0000316}
317
drh41d30272008-08-20 21:47:45 +0000318/*
danielk1977bc2ca9e2008-11-13 14:28:28 +0000319** Change the page size for PCache object. The caller must ensure that there
320** are no outstanding page references when this function is called.
drh41d30272008-08-20 21:47:45 +0000321*/
drhc3031c62014-08-26 15:06:49 +0000322int sqlite3PcacheSetPageSize(PCache *pCache, int szPage){
drh95a0b372015-09-03 20:43:55 +0000323 assert( pCache->nRefSum==0 && pCache->pDirty==0 );
drhc3031c62014-08-26 15:06:49 +0000324 if( pCache->szPage ){
325 sqlite3_pcache *pNew;
326 pNew = sqlite3GlobalConfig.pcache2.xCreate(
drh51dc84e2014-12-30 13:04:25 +0000327 szPage, pCache->szExtra + ROUND8(sizeof(PgHdr)),
328 pCache->bPurgeable
drhc3031c62014-08-26 15:06:49 +0000329 );
mistachkinfad30392016-02-13 23:43:46 +0000330 if( pNew==0 ) return SQLITE_NOMEM_BKPT;
drhc3031c62014-08-26 15:06:49 +0000331 sqlite3GlobalConfig.pcache2.xCachesize(pNew, numberOfCachePages(pCache));
332 if( pCache->pCache ){
333 sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
334 }
335 pCache->pCache = pNew;
drhc3031c62014-08-26 15:06:49 +0000336 pCache->szPage = szPage;
drh5c8e0922016-05-11 10:57:04 +0000337 pcacheTrace(("%p.PAGESIZE %d\n",pCache,szPage));
danielk1977bc2ca9e2008-11-13 14:28:28 +0000338 }
drhc3031c62014-08-26 15:06:49 +0000339 return SQLITE_OK;
drh3b42abb2011-11-09 14:23:04 +0000340}
341
342/*
danielk19778c0a7912008-08-20 14:49:23 +0000343** Try to obtain a page from the cache.
drhbc59ac02014-08-27 23:18:01 +0000344**
345** This routine returns a pointer to an sqlite3_pcache_page object if
346** such an object is already in cache, or if a new one is created.
347** This routine returns a NULL pointer if the object was not in cache
348** and could not be created.
349**
350** The createFlags should be 0 to check for existing pages and should
351** be 3 (not 1, but 3) to try to create a new page.
352**
353** If the createFlag is 0, then NULL is always returned if the page
354** is not already in the cache. If createFlag is 1, then a new page
355** is created only if that can be done without spilling dirty pages
356** and without exceeding the cache size limit.
357**
358** The caller needs to invoke sqlite3PcacheFetchFinish() to properly
359** initialize the sqlite3_pcache_page object and convert it into a
360** PgHdr object. The sqlite3PcacheFetch() and sqlite3PcacheFetchFinish()
361** routines are split this way for performance reasons. When separated
362** they can both (usually) operate without having to push values to
363** the stack on entry and pop them back off on exit, which saves a
364** lot of pushing and popping.
danielk19778c0a7912008-08-20 14:49:23 +0000365*/
drhbc59ac02014-08-27 23:18:01 +0000366sqlite3_pcache_page *sqlite3PcacheFetch(
danielk19778c0a7912008-08-20 14:49:23 +0000367 PCache *pCache, /* Obtain the page from this cache */
368 Pgno pgno, /* Page number to obtain */
drhbc59ac02014-08-27 23:18:01 +0000369 int createFlag /* If true, create page if it does not exist already */
danielk19778c0a7912008-08-20 14:49:23 +0000370){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000371 int eCreate;
drh5c8e0922016-05-11 10:57:04 +0000372 sqlite3_pcache_page *pRes;
danielk1977f599a192008-08-28 10:21:16 +0000373
danielk19778c0a7912008-08-20 14:49:23 +0000374 assert( pCache!=0 );
drhc3031c62014-08-26 15:06:49 +0000375 assert( pCache->pCache!=0 );
376 assert( createFlag==3 || createFlag==0 );
dan401907e2016-05-11 20:03:23 +0000377 assert( pCache->eCreate==((pCache->bPurgeable && pCache->pDirty) ? 1 : 2) );
danielk19778c0a7912008-08-20 14:49:23 +0000378
drhfe21a792014-02-03 17:04:29 +0000379 /* eCreate defines what to do if the page does not exist.
380 ** 0 Do not allocate a new page. (createFlag==0)
381 ** 1 Allocate a new page if doing so is inexpensive.
382 ** (createFlag==1 AND bPurgeable AND pDirty)
383 ** 2 Allocate a new page even it doing so is difficult.
384 ** (createFlag==1 AND !(bPurgeable AND pDirty)
385 */
drhc3031c62014-08-26 15:06:49 +0000386 eCreate = createFlag & pCache->eCreate;
387 assert( eCreate==0 || eCreate==1 || eCreate==2 );
388 assert( createFlag==0 || pCache->eCreate==eCreate );
389 assert( createFlag==0 || eCreate==1+(!pCache->bPurgeable||!pCache->pDirty) );
drh5c8e0922016-05-11 10:57:04 +0000390 pRes = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, eCreate);
391 pcacheTrace(("%p.FETCH %d%s (result: %p)\n",pCache,pgno,
392 createFlag?" create":"",pRes));
393 return pRes;
drhbc59ac02014-08-27 23:18:01 +0000394}
danielk1977bc2ca9e2008-11-13 14:28:28 +0000395
drhbc59ac02014-08-27 23:18:01 +0000396/*
397** If the sqlite3PcacheFetch() routine is unable to allocate a new
dan41113b62016-04-05 21:07:58 +0000398** page because no clean pages are available for reuse and the cache
drhbc59ac02014-08-27 23:18:01 +0000399** size limit has been reached, then this routine can be invoked to
400** try harder to allocate a page. This routine might invoke the stress
401** callback to spill dirty pages to the journal. It will then try to
402** allocate the new page and will only fail to allocate a new page on
403** an OOM error.
404**
405** This routine should be invoked only after sqlite3PcacheFetch() fails.
406*/
407int sqlite3PcacheFetchStress(
408 PCache *pCache, /* Obtain the page from this cache */
409 Pgno pgno, /* Page number to obtain */
410 sqlite3_pcache_page **ppPage /* Write result here */
411){
412 PgHdr *pPg;
413 if( pCache->eCreate==2 ) return 0;
414
drh9b0cf342015-11-12 14:57:19 +0000415 if( sqlite3PcachePagecount(pCache)>pCache->szSpill ){
416 /* Find a dirty page to write-out and recycle. First try to find a
417 ** page that does not require a journal-sync (one with PGHDR_NEED_SYNC
418 ** cleared), but if that is not possible settle for any other
419 ** unreferenced dirty page.
danb2ef9002016-05-11 15:41:15 +0000420 **
421 ** If the LRU page in the dirty list that has a clear PGHDR_NEED_SYNC
422 ** flag is currently referenced, then the following may leave pSynced
423 ** set incorrectly (pointing to other than the LRU page with NEED_SYNC
424 ** cleared). This is Ok, as pSynced is just an optimization. */
drh9b0cf342015-11-12 14:57:19 +0000425 for(pPg=pCache->pSynced;
426 pPg && (pPg->nRef || (pPg->flags&PGHDR_NEED_SYNC));
427 pPg=pPg->pDirtyPrev
428 );
429 pCache->pSynced = pPg;
430 if( !pPg ){
431 for(pPg=pCache->pDirtyTail; pPg && pPg->nRef; pPg=pPg->pDirtyPrev);
432 }
433 if( pPg ){
434 int rc;
drhc97125e2011-05-28 15:53:07 +0000435#ifdef SQLITE_LOG_CACHE_SPILL
drh9b0cf342015-11-12 14:57:19 +0000436 sqlite3_log(SQLITE_FULL,
437 "spill page %d making room for %d - cache used: %d/%d",
438 pPg->pgno, pgno,
dan58db4c72018-03-12 21:09:16 +0000439 sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache),
drhbc59ac02014-08-27 23:18:01 +0000440 numberOfCachePages(pCache));
drhc97125e2011-05-28 15:53:07 +0000441#endif
drh5c8e0922016-05-11 10:57:04 +0000442 pcacheTrace(("%p.SPILL %d\n",pCache,pPg->pgno));
drh9b0cf342015-11-12 14:57:19 +0000443 rc = pCache->xStress(pCache->pStress, pPg);
drh7aeb2162016-05-13 04:24:25 +0000444 pcacheDump(pCache);
drh9b0cf342015-11-12 14:57:19 +0000445 if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
446 return rc;
447 }
danielk1977f599a192008-08-28 10:21:16 +0000448 }
danielk19778c0a7912008-08-20 14:49:23 +0000449 }
drhbc59ac02014-08-27 23:18:01 +0000450 *ppPage = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, 2);
mistachkinfad30392016-02-13 23:43:46 +0000451 return *ppPage==0 ? SQLITE_NOMEM_BKPT : SQLITE_OK;
drhbc59ac02014-08-27 23:18:01 +0000452}
453
454/*
455** This is a helper routine for sqlite3PcacheFetchFinish()
456**
457** In the uncommon case where the page being fetched has not been
458** initialized, this routine is invoked to do the initialization.
459** This routine is broken out into a separate function since it
460** requires extra stack manipulation that can be avoided in the common
461** case.
462*/
463static SQLITE_NOINLINE PgHdr *pcacheFetchFinishWithInit(
464 PCache *pCache, /* Obtain the page from this cache */
465 Pgno pgno, /* Page number obtained */
466 sqlite3_pcache_page *pPage /* Page obtained by prior PcacheFetch() call */
467){
468 PgHdr *pPgHdr;
469 assert( pPage!=0 );
470 pPgHdr = (PgHdr*)pPage->pExtra;
471 assert( pPgHdr->pPage==0 );
drh216b70f2016-10-01 20:43:41 +0000472 memset(&pPgHdr->pDirty, 0, sizeof(PgHdr) - offsetof(PgHdr,pDirty));
drhbc59ac02014-08-27 23:18:01 +0000473 pPgHdr->pPage = pPage;
474 pPgHdr->pData = pPage->pBuf;
475 pPgHdr->pExtra = (void *)&pPgHdr[1];
drha2ee5892016-12-09 16:02:00 +0000476 memset(pPgHdr->pExtra, 0, 8);
drhbc59ac02014-08-27 23:18:01 +0000477 pPgHdr->pCache = pCache;
478 pPgHdr->pgno = pgno;
drhc78ae912015-06-29 04:21:15 +0000479 pPgHdr->flags = PGHDR_CLEAN;
drhbc59ac02014-08-27 23:18:01 +0000480 return sqlite3PcacheFetchFinish(pCache,pgno,pPage);
481}
482
483/*
484** This routine converts the sqlite3_pcache_page object returned by
485** sqlite3PcacheFetch() into an initialized PgHdr object. This routine
486** must be called after sqlite3PcacheFetch() in order to get a usable
487** result.
488*/
489PgHdr *sqlite3PcacheFetchFinish(
490 PCache *pCache, /* Obtain the page from this cache */
491 Pgno pgno, /* Page number obtained */
492 sqlite3_pcache_page *pPage /* Page obtained by prior PcacheFetch() call */
493){
494 PgHdr *pPgHdr;
495
drhd8c0ba32015-06-30 03:57:59 +0000496 assert( pPage!=0 );
drhbc59ac02014-08-27 23:18:01 +0000497 pPgHdr = (PgHdr *)pPage->pExtra;
498
499 if( !pPgHdr->pPage ){
500 return pcacheFetchFinishWithInit(pCache, pgno, pPage);
501 }
drh95a0b372015-09-03 20:43:55 +0000502 pCache->nRefSum++;
drhbc59ac02014-08-27 23:18:01 +0000503 pPgHdr->nRef++;
drha0f6b122016-05-13 15:22:06 +0000504 assert( sqlite3PcachePageSanity(pPgHdr) );
drhbc59ac02014-08-27 23:18:01 +0000505 return pPgHdr;
danielk19778c0a7912008-08-20 14:49:23 +0000506}
507
508/*
danielk1977bc2ca9e2008-11-13 14:28:28 +0000509** Decrement the reference count on a page. If the page is clean and the
peter.d.reid60ec9142014-09-06 16:39:46 +0000510** reference count drops to 0, then it is made eligible for recycling.
danielk19778c0a7912008-08-20 14:49:23 +0000511*/
drha8dcba92014-08-22 20:35:29 +0000512void SQLITE_NOINLINE sqlite3PcacheRelease(PgHdr *p){
danielk19778c0a7912008-08-20 14:49:23 +0000513 assert( p->nRef>0 );
drh95a0b372015-09-03 20:43:55 +0000514 p->pCache->nRefSum--;
515 if( (--p->nRef)==0 ){
drhc78ae912015-06-29 04:21:15 +0000516 if( p->flags&PGHDR_CLEAN ){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000517 pcacheUnpin(p);
drhdfcdc662017-09-01 12:57:33 +0000518 }else{
drha8dcba92014-08-22 20:35:29 +0000519 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_FRONT);
danielk1977d491e1b2008-08-26 18:05:48 +0000520 }
danielk1977502b7432008-08-25 14:49:42 +0000521 }
danielk19778c0a7912008-08-20 14:49:23 +0000522}
523
danielk1977bc2ca9e2008-11-13 14:28:28 +0000524/*
525** Increase the reference count of a supplied page by 1.
526*/
danielk19778c0a7912008-08-20 14:49:23 +0000527void sqlite3PcacheRef(PgHdr *p){
danielk1977502b7432008-08-25 14:49:42 +0000528 assert(p->nRef>0);
drha0f6b122016-05-13 15:22:06 +0000529 assert( sqlite3PcachePageSanity(p) );
danielk1977502b7432008-08-25 14:49:42 +0000530 p->nRef++;
drh95a0b372015-09-03 20:43:55 +0000531 p->pCache->nRefSum++;
danielk19778c0a7912008-08-20 14:49:23 +0000532}
533
534/*
danielk19774abdfa42008-08-27 09:44:39 +0000535** Drop a page from the cache. There must be exactly one reference to the
536** page. This function deletes that reference, so after it returns the
537** page pointed to by p is invalid.
danielk19778c0a7912008-08-20 14:49:23 +0000538*/
539void sqlite3PcacheDrop(PgHdr *p){
danielk19778c0a7912008-08-20 14:49:23 +0000540 assert( p->nRef==1 );
drha0f6b122016-05-13 15:22:06 +0000541 assert( sqlite3PcachePageSanity(p) );
danielk1977bc2ca9e2008-11-13 14:28:28 +0000542 if( p->flags&PGHDR_DIRTY ){
drha8dcba92014-08-22 20:35:29 +0000543 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000544 }
drh95a0b372015-09-03 20:43:55 +0000545 p->pCache->nRefSum--;
drha8dcba92014-08-22 20:35:29 +0000546 sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 1);
danielk19778c0a7912008-08-20 14:49:23 +0000547}
548
549/*
danielk1977bc2ca9e2008-11-13 14:28:28 +0000550** Make sure the page is marked as dirty. If it isn't dirty already,
danielk19778c0a7912008-08-20 14:49:23 +0000551** make it so.
552*/
553void sqlite3PcacheMakeDirty(PgHdr *p){
danielk19778c0a7912008-08-20 14:49:23 +0000554 assert( p->nRef>0 );
drha0f6b122016-05-13 15:22:06 +0000555 assert( sqlite3PcachePageSanity(p) );
dan82c04472016-05-12 17:06:04 +0000556 if( p->flags & (PGHDR_CLEAN|PGHDR_DONT_WRITE) ){ /*OPTIMIZATION-IF-FALSE*/
drhc78ae912015-06-29 04:21:15 +0000557 p->flags &= ~PGHDR_DONT_WRITE;
558 if( p->flags & PGHDR_CLEAN ){
559 p->flags ^= (PGHDR_DIRTY|PGHDR_CLEAN);
drh5c8e0922016-05-11 10:57:04 +0000560 pcacheTrace(("%p.DIRTY %d\n",p->pCache,p->pgno));
drhc78ae912015-06-29 04:21:15 +0000561 assert( (p->flags & (PGHDR_DIRTY|PGHDR_CLEAN))==PGHDR_DIRTY );
562 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_ADD);
563 }
drha0f6b122016-05-13 15:22:06 +0000564 assert( sqlite3PcachePageSanity(p) );
danielk1977d491e1b2008-08-26 18:05:48 +0000565 }
danielk1977f599a192008-08-28 10:21:16 +0000566}
567
568/*
danielk1977bc2ca9e2008-11-13 14:28:28 +0000569** Make sure the page is marked as clean. If it isn't clean already,
danielk1977f599a192008-08-28 10:21:16 +0000570** make it so.
571*/
572void sqlite3PcacheMakeClean(PgHdr *p){
drha0f6b122016-05-13 15:22:06 +0000573 assert( sqlite3PcachePageSanity(p) );
drh9d9c41e2017-10-31 03:40:15 +0000574 assert( (p->flags & PGHDR_DIRTY)!=0 );
575 assert( (p->flags & PGHDR_CLEAN)==0 );
576 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE);
577 p->flags &= ~(PGHDR_DIRTY|PGHDR_NEED_SYNC|PGHDR_WRITEABLE);
578 p->flags |= PGHDR_CLEAN;
579 pcacheTrace(("%p.CLEAN %d\n",p->pCache,p->pgno));
580 assert( sqlite3PcachePageSanity(p) );
581 if( p->nRef==0 ){
582 pcacheUnpin(p);
danielk1977f599a192008-08-28 10:21:16 +0000583 }
danielk19778c0a7912008-08-20 14:49:23 +0000584}
585
586/*
587** Make every page in the cache clean.
588*/
589void sqlite3PcacheCleanAll(PCache *pCache){
590 PgHdr *p;
drh7aeb2162016-05-13 04:24:25 +0000591 pcacheTrace(("%p.CLEAN-ALL\n",pCache));
danielk19778c0a7912008-08-20 14:49:23 +0000592 while( (p = pCache->pDirty)!=0 ){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000593 sqlite3PcacheMakeClean(p);
danielk19778c0a7912008-08-20 14:49:23 +0000594 }
595}
596
597/*
dan41113b62016-04-05 21:07:58 +0000598** Clear the PGHDR_NEED_SYNC and PGHDR_WRITEABLE flag from all dirty pages.
599*/
600void sqlite3PcacheClearWritable(PCache *pCache){
601 PgHdr *p;
drh7aeb2162016-05-13 04:24:25 +0000602 pcacheTrace(("%p.CLEAR-WRITEABLE\n",pCache));
dan41113b62016-04-05 21:07:58 +0000603 for(p=pCache->pDirty; p; p=p->pDirtyNext){
604 p->flags &= ~(PGHDR_NEED_SYNC|PGHDR_WRITEABLE);
605 }
606 pCache->pSynced = pCache->pDirtyTail;
607}
608
609/*
danielk1977bc2ca9e2008-11-13 14:28:28 +0000610** Clear the PGHDR_NEED_SYNC flag from all dirty pages.
611*/
612void sqlite3PcacheClearSyncFlags(PCache *pCache){
613 PgHdr *p;
614 for(p=pCache->pDirty; p; p=p->pDirtyNext){
615 p->flags &= ~PGHDR_NEED_SYNC;
616 }
617 pCache->pSynced = pCache->pDirtyTail;
618}
619
620/*
621** Change the page number of page p to newPgno.
danielk19778c0a7912008-08-20 14:49:23 +0000622*/
623void sqlite3PcacheMove(PgHdr *p, Pgno newPgno){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000624 PCache *pCache = p->pCache;
danielk1977d491e1b2008-08-26 18:05:48 +0000625 assert( p->nRef>0 );
danielk1977bc2ca9e2008-11-13 14:28:28 +0000626 assert( newPgno>0 );
drha0f6b122016-05-13 15:22:06 +0000627 assert( sqlite3PcachePageSanity(p) );
drh5c8e0922016-05-11 10:57:04 +0000628 pcacheTrace(("%p.MOVE %d -> %d\n",pCache,p->pgno,newPgno));
dan22e21ff2011-11-08 20:08:44 +0000629 sqlite3GlobalConfig.pcache2.xRekey(pCache->pCache, p->pPage, p->pgno,newPgno);
danielk19778c0a7912008-08-20 14:49:23 +0000630 p->pgno = newPgno;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000631 if( (p->flags&PGHDR_DIRTY) && (p->flags&PGHDR_NEED_SYNC) ){
drha8dcba92014-08-22 20:35:29 +0000632 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_FRONT);
danielk19778c0a7912008-08-20 14:49:23 +0000633 }
danielk19778c0a7912008-08-20 14:49:23 +0000634}
635
636/*
danielk1977bc2ca9e2008-11-13 14:28:28 +0000637** Drop every cache entry whose page number is greater than "pgno". The
638** caller must ensure that there are no outstanding references to any pages
639** other than page 1 with a page number greater than pgno.
640**
641** If there is a reference to page 1 and the pgno parameter passed to this
642** function is 0, then the data area associated with page 1 is zeroed, but
643** the page object is not dropped.
danielk19778c0a7912008-08-20 14:49:23 +0000644*/
645void sqlite3PcacheTruncate(PCache *pCache, Pgno pgno){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000646 if( pCache->pCache ){
647 PgHdr *p;
648 PgHdr *pNext;
drh5c8e0922016-05-11 10:57:04 +0000649 pcacheTrace(("%p.TRUNCATE %d\n",pCache,pgno));
danielk1977bc2ca9e2008-11-13 14:28:28 +0000650 for(p=pCache->pDirty; p; p=pNext){
651 pNext = p->pDirtyNext;
drhf3609ee2010-03-19 19:23:51 +0000652 /* This routine never gets call with a positive pgno except right
653 ** after sqlite3PcacheCleanAll(). So if there are dirty pages,
654 ** it must be that pgno==0.
655 */
656 assert( p->pgno>0 );
dan41113b62016-04-05 21:07:58 +0000657 if( p->pgno>pgno ){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000658 assert( p->flags&PGHDR_DIRTY );
659 sqlite3PcacheMakeClean(p);
danielk19778c0a7912008-08-20 14:49:23 +0000660 }
661 }
drh95a0b372015-09-03 20:43:55 +0000662 if( pgno==0 && pCache->nRefSum ){
drh39065c62015-06-26 02:41:31 +0000663 sqlite3_pcache_page *pPage1;
664 pPage1 = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache,1,0);
665 if( ALWAYS(pPage1) ){ /* Page 1 is always available in cache, because
drh95a0b372015-09-03 20:43:55 +0000666 ** pCache->nRefSum>0 */
drh39065c62015-06-26 02:41:31 +0000667 memset(pPage1->pBuf, 0, pCache->szPage);
668 pgno = 1;
669 }
danielk1977bc2ca9e2008-11-13 14:28:28 +0000670 }
dan22e21ff2011-11-08 20:08:44 +0000671 sqlite3GlobalConfig.pcache2.xTruncate(pCache->pCache, pgno+1);
danielk1977062d4cb2008-08-29 09:10:02 +0000672 }
673}
danielk19778c0a7912008-08-20 14:49:23 +0000674
675/*
676** Close a cache.
677*/
678void sqlite3PcacheClose(PCache *pCache){
drhc3031c62014-08-26 15:06:49 +0000679 assert( pCache->pCache!=0 );
drh5c8e0922016-05-11 10:57:04 +0000680 pcacheTrace(("%p.CLOSE\n",pCache));
drhc3031c62014-08-26 15:06:49 +0000681 sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
danielk19778c0a7912008-08-20 14:49:23 +0000682}
683
684/*
685** Discard the contents of the cache.
686*/
danielk1977bea2a942009-01-20 17:06:27 +0000687void sqlite3PcacheClear(PCache *pCache){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000688 sqlite3PcacheTruncate(pCache, 0);
danielk19778c0a7912008-08-20 14:49:23 +0000689}
690
691/*
692** Merge two lists of pages connected by pDirty and in pgno order.
mistachkin2380f3f2016-05-20 20:58:30 +0000693** Do not bother fixing the pDirtyPrev pointers.
danielk19778c0a7912008-08-20 14:49:23 +0000694*/
695static PgHdr *pcacheMergeDirtyList(PgHdr *pA, PgHdr *pB){
696 PgHdr result, *pTail;
697 pTail = &result;
drhb982bfe2016-05-20 14:54:54 +0000698 assert( pA!=0 && pB!=0 );
699 for(;;){
danielk19778c0a7912008-08-20 14:49:23 +0000700 if( pA->pgno<pB->pgno ){
701 pTail->pDirty = pA;
702 pTail = pA;
703 pA = pA->pDirty;
drhb982bfe2016-05-20 14:54:54 +0000704 if( pA==0 ){
705 pTail->pDirty = pB;
706 break;
707 }
danielk19778c0a7912008-08-20 14:49:23 +0000708 }else{
709 pTail->pDirty = pB;
710 pTail = pB;
711 pB = pB->pDirty;
drhb982bfe2016-05-20 14:54:54 +0000712 if( pB==0 ){
713 pTail->pDirty = pA;
714 break;
715 }
danielk19778c0a7912008-08-20 14:49:23 +0000716 }
717 }
danielk19778c0a7912008-08-20 14:49:23 +0000718 return result.pDirty;
719}
720
721/*
722** Sort the list of pages in accending order by pgno. Pages are
danielk1977bc2ca9e2008-11-13 14:28:28 +0000723** connected by pDirty pointers. The pDirtyPrev pointers are
danielk19778c0a7912008-08-20 14:49:23 +0000724** corrupted by this sort.
drhe64ca7b2009-07-16 18:21:17 +0000725**
726** Since there cannot be more than 2^31 distinct pages in a database,
727** there cannot be more than 31 buckets required by the merge sorter.
728** One extra bucket is added to catch overflow in case something
729** ever changes to make the previous sentence incorrect.
danielk19778c0a7912008-08-20 14:49:23 +0000730*/
drhe64ca7b2009-07-16 18:21:17 +0000731#define N_SORT_BUCKET 32
danielk19778c0a7912008-08-20 14:49:23 +0000732static PgHdr *pcacheSortDirtyList(PgHdr *pIn){
drhe64ca7b2009-07-16 18:21:17 +0000733 PgHdr *a[N_SORT_BUCKET], *p;
danielk19778c0a7912008-08-20 14:49:23 +0000734 int i;
735 memset(a, 0, sizeof(a));
736 while( pIn ){
737 p = pIn;
738 pIn = p->pDirty;
739 p->pDirty = 0;
drhe64ca7b2009-07-16 18:21:17 +0000740 for(i=0; ALWAYS(i<N_SORT_BUCKET-1); i++){
danielk19778c0a7912008-08-20 14:49:23 +0000741 if( a[i]==0 ){
742 a[i] = p;
743 break;
744 }else{
745 p = pcacheMergeDirtyList(a[i], p);
746 a[i] = 0;
747 }
748 }
drhe64ca7b2009-07-16 18:21:17 +0000749 if( NEVER(i==N_SORT_BUCKET-1) ){
750 /* To get here, there need to be 2^(N_SORT_BUCKET) elements in
751 ** the input list. But that is impossible.
danielk19778c0a7912008-08-20 14:49:23 +0000752 */
753 a[i] = pcacheMergeDirtyList(a[i], p);
754 }
755 }
756 p = a[0];
757 for(i=1; i<N_SORT_BUCKET; i++){
drhb982bfe2016-05-20 14:54:54 +0000758 if( a[i]==0 ) continue;
759 p = p ? pcacheMergeDirtyList(p, a[i]) : a[i];
danielk19778c0a7912008-08-20 14:49:23 +0000760 }
761 return p;
762}
763
764/*
765** Return a list of all dirty pages in the cache, sorted by page number.
766*/
767PgHdr *sqlite3PcacheDirtyList(PCache *pCache){
768 PgHdr *p;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000769 for(p=pCache->pDirty; p; p=p->pDirtyNext){
770 p->pDirty = p->pDirtyNext;
danielk19778c0a7912008-08-20 14:49:23 +0000771 }
772 return pcacheSortDirtyList(pCache->pDirty);
773}
774
danielk19778c0a7912008-08-20 14:49:23 +0000775/*
drh95a0b372015-09-03 20:43:55 +0000776** Return the total number of references to all pages held by the cache.
777**
778** This is not the total number of pages referenced, but the sum of the
779** reference count for all pages.
danielk19778c0a7912008-08-20 14:49:23 +0000780*/
781int sqlite3PcacheRefCount(PCache *pCache){
drh95a0b372015-09-03 20:43:55 +0000782 return pCache->nRefSum;
danielk19778c0a7912008-08-20 14:49:23 +0000783}
784
danielk1977bc2ca9e2008-11-13 14:28:28 +0000785/*
786** Return the number of references to the page supplied as an argument.
787*/
danielk197771d5d2c2008-09-29 11:49:47 +0000788int sqlite3PcachePageRefcount(PgHdr *p){
789 return p->nRef;
790}
791
danielk19778c0a7912008-08-20 14:49:23 +0000792/*
793** Return the total number of pages in the cache.
794*/
795int sqlite3PcachePagecount(PCache *pCache){
drhc3031c62014-08-26 15:06:49 +0000796 assert( pCache->pCache!=0 );
797 return sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache);
danielk19778c0a7912008-08-20 14:49:23 +0000798}
799
danielk1977f3d3c272008-11-19 16:52:44 +0000800#ifdef SQLITE_TEST
danielk19778c0a7912008-08-20 14:49:23 +0000801/*
danielk1977bc2ca9e2008-11-13 14:28:28 +0000802** Get the suggested cache-size value.
danielk19778c0a7912008-08-20 14:49:23 +0000803*/
804int sqlite3PcacheGetCachesize(PCache *pCache){
drh3b42abb2011-11-09 14:23:04 +0000805 return numberOfCachePages(pCache);
danielk19778c0a7912008-08-20 14:49:23 +0000806}
danielk1977f3d3c272008-11-19 16:52:44 +0000807#endif
danielk19778c0a7912008-08-20 14:49:23 +0000808
809/*
810** Set the suggested cache-size value.
811*/
812void sqlite3PcacheSetCachesize(PCache *pCache, int mxPage){
drhc3031c62014-08-26 15:06:49 +0000813 assert( pCache->pCache!=0 );
drh3b42abb2011-11-09 14:23:04 +0000814 pCache->szCache = mxPage;
drhc3031c62014-08-26 15:06:49 +0000815 sqlite3GlobalConfig.pcache2.xCachesize(pCache->pCache,
816 numberOfCachePages(pCache));
danielk19778c0a7912008-08-20 14:49:23 +0000817}
818
drh09419b42011-11-16 19:29:17 +0000819/*
drh9b0cf342015-11-12 14:57:19 +0000820** Set the suggested cache-spill value. Make no changes if if the
821** argument is zero. Return the effective cache-spill size, which will
822** be the larger of the szSpill and szCache.
823*/
824int sqlite3PcacheSetSpillsize(PCache *p, int mxPage){
825 int res;
826 assert( p->pCache!=0 );
827 if( mxPage ){
828 if( mxPage<0 ){
drh4f9c8ec2015-11-12 15:47:48 +0000829 mxPage = (int)((-1024*(i64)mxPage)/(p->szPage+p->szExtra));
drh9b0cf342015-11-12 14:57:19 +0000830 }
831 p->szSpill = mxPage;
832 }
833 res = numberOfCachePages(p);
834 if( res<p->szSpill ) res = p->szSpill;
835 return res;
836}
837
838/*
drh09419b42011-11-16 19:29:17 +0000839** Free up as much memory as possible from the page cache.
840*/
841void sqlite3PcacheShrink(PCache *pCache){
drhc3031c62014-08-26 15:06:49 +0000842 assert( pCache->pCache!=0 );
843 sqlite3GlobalConfig.pcache2.xShrink(pCache->pCache);
drh09419b42011-11-16 19:29:17 +0000844}
845
drhdef68892014-11-04 12:11:23 +0000846/*
847** Return the size of the header added by this middleware layer
848** in the page-cache hierarchy.
849*/
drh37c057b2014-12-30 00:57:29 +0000850int sqlite3HeaderSizePcache(void){ return ROUND8(sizeof(PgHdr)); }
drhdef68892014-11-04 12:11:23 +0000851
dan0f524552016-04-13 16:52:11 +0000852/*
853** Return the number of dirty pages currently in the cache, as a percentage
854** of the configured cache size.
855*/
856int sqlite3PCachePercentDirty(PCache *pCache){
857 PgHdr *pDirty;
858 int nDirty = 0;
859 int nCache = numberOfCachePages(pCache);
860 for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext) nDirty++;
drhb5895e52016-04-18 13:30:50 +0000861 return nCache ? (int)(((i64)nDirty * 100) / nCache) : 0;
dan0f524552016-04-13 16:52:11 +0000862}
drhdef68892014-11-04 12:11:23 +0000863
dan09236752018-11-22 19:10:14 +0000864#ifdef SQLITE_DIRECT_OVERFLOW_READ
865/*
866** Return true if there are one or more dirty pages in the cache. Else false.
867*/
868int sqlite3PCacheIsDirty(PCache *pCache){
869 return (pCache->pDirty!=0);
870}
871#endif
872
danielk1977750e87d2009-07-25 11:46:48 +0000873#if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)
danielk197767e3da72008-08-21 12:19:44 +0000874/*
danielk1977bc2ca9e2008-11-13 14:28:28 +0000875** For all dirty pages currently in the cache, invoke the specified
876** callback. This is only used if the SQLITE_CHECK_PAGES macro is
877** defined.
danielk197767e3da72008-08-21 12:19:44 +0000878*/
danielk1977bc2ca9e2008-11-13 14:28:28 +0000879void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *)){
880 PgHdr *pDirty;
881 for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext){
882 xIter(pDirty);
danielk197767e3da72008-08-21 12:19:44 +0000883 }
danielk1977062d4cb2008-08-29 09:10:02 +0000884}
885#endif