blob: 0407e06b2fafa14c4390fdbd6011f978926592e9 [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;}
drhf6e3e312022-08-27 14:43:34 +000069 static void pcachePageTrace(int i, sqlite3_pcache_page *pLower){
drh7aeb2162016-05-13 04:24:25 +000070 PgHdr *pPg;
71 unsigned char *a;
drhf6e3e312022-08-27 14:43:34 +000072 int j;
73 pPg = (PgHdr*)pLower->pExtra;
74 printf("%3d: nRef %2d flgs %02x data ", i, pPg->nRef, pPg->flags);
75 a = (unsigned char *)pLower->pBuf;
76 for(j=0; j<12; j++) printf("%02x", a[j]);
77 printf(" ptr %p\n", pPg);
78 }
79 static void pcacheDump(PCache *pCache){
80 int N;
81 int i;
82 sqlite3_pcache_page *pLower;
drh7aeb2162016-05-13 04:24:25 +000083
84 if( sqlite3PcacheTrace<2 ) return;
85 if( pCache->pCache==0 ) return;
86 N = sqlite3PcachePagecount(pCache);
drha0f6b122016-05-13 15:22:06 +000087 if( N>sqlite3PcacheMxDump ) N = sqlite3PcacheMxDump;
drh7aeb2162016-05-13 04:24:25 +000088 for(i=1; i<=N; i++){
89 pLower = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, i, 0);
90 if( pLower==0 ) continue;
drhf6e3e312022-08-27 14:43:34 +000091 pcachePageTrace(i, pLower);
92 if( ((PgHdr*)pLower)->pPage==0 ){
drh7aeb2162016-05-13 04:24:25 +000093 sqlite3GlobalConfig.pcache2.xUnpin(pCache->pCache, pLower, 0);
94 }
95 }
96 }
drhf6e3e312022-08-27 14:43:34 +000097#else
drh5c8e0922016-05-11 10:57:04 +000098# define pcacheTrace(X)
drhf6e3e312022-08-27 14:43:34 +000099# define pcachePageTrace(PGNO, X)
drh7aeb2162016-05-13 04:24:25 +0000100# define pcacheDump(X)
drh5c8e0922016-05-11 10:57:04 +0000101#endif
102
drha0f6b122016-05-13 15:22:06 +0000103/*
drhf0f025b2022-08-27 16:11:00 +0000104** Return 1 if pPg is on the dirty list for pCache. Return 0 if not.
105** This routine runs inside of assert() statements only.
106*/
107#ifdef SQLITE_DEBUG
108static int pageOnDirtyList(PCache *pCache, PgHdr *pPg){
109 PgHdr *p;
110 for(p=pCache->pDirty; p; p=p->pDirtyNext){
111 if( p==pPg ) return 1;
112 }
113 return 0;
114}
115#endif
116
117/*
drha0f6b122016-05-13 15:22:06 +0000118** Check invariants on a PgHdr entry. Return true if everything is OK.
119** Return false if any invariant is violated.
120**
121** This routine is for use inside of assert() statements only. For
122** example:
123**
124** assert( sqlite3PcachePageSanity(pPg) );
125*/
drhd879e3e2017-02-13 13:35:55 +0000126#ifdef SQLITE_DEBUG
drha0f6b122016-05-13 15:22:06 +0000127int sqlite3PcachePageSanity(PgHdr *pPg){
128 PCache *pCache;
129 assert( pPg!=0 );
drhcbed6042016-12-13 18:34:01 +0000130 assert( pPg->pgno>0 || pPg->pPager==0 ); /* Page number is 1 or more */
drha0f6b122016-05-13 15:22:06 +0000131 pCache = pPg->pCache;
132 assert( pCache!=0 ); /* Every page has an associated PCache */
133 if( pPg->flags & PGHDR_CLEAN ){
134 assert( (pPg->flags & PGHDR_DIRTY)==0 );/* Cannot be both CLEAN and DIRTY */
drhf0f025b2022-08-27 16:11:00 +0000135 assert( !pageOnDirtyList(pCache, pPg) );/* CLEAN pages not on dirty list */
drhf6e3e312022-08-27 14:43:34 +0000136 }else{
137 assert( (pPg->flags & PGHDR_DIRTY)!=0 );/* If not CLEAN must be DIRTY */
138 assert( pPg->pDirtyNext==0 || pPg->pDirtyNext->pDirtyPrev==pPg );
139 assert( pPg->pDirtyPrev==0 || pPg->pDirtyPrev->pDirtyNext==pPg );
140 assert( pPg->pDirtyPrev!=0 || pCache->pDirty==pPg );
drhf0f025b2022-08-27 16:11:00 +0000141 assert( pageOnDirtyList(pCache, pPg) );
drha0f6b122016-05-13 15:22:06 +0000142 }
143 /* WRITEABLE pages must also be DIRTY */
144 if( pPg->flags & PGHDR_WRITEABLE ){
145 assert( pPg->flags & PGHDR_DIRTY ); /* WRITEABLE implies DIRTY */
146 }
147 /* NEED_SYNC can be set independently of WRITEABLE. This can happen,
148 ** for example, when using the sqlite3PagerDontWrite() optimization:
149 ** (1) Page X is journalled, and gets WRITEABLE and NEED_SEEK.
150 ** (2) Page X moved to freelist, WRITEABLE is cleared
151 ** (3) Page X reused, WRITEABLE is set again
152 ** If NEED_SYNC had been cleared in step 2, then it would not be reset
153 ** in step 3, and page might be written into the database without first
154 ** syncing the rollback journal, which might cause corruption on a power
155 ** loss.
drh3b02a072016-05-13 17:22:33 +0000156 **
157 ** Another example is when the database page size is smaller than the
158 ** disk sector size. When any page of a sector is journalled, all pages
159 ** in that sector are marked NEED_SYNC even if they are still CLEAN, just
160 ** in case they are later modified, since all pages in the same sector
161 ** must be journalled and synced before any of those pages can be safely
162 ** written.
drha0f6b122016-05-13 15:22:06 +0000163 */
164 return 1;
165}
166#endif /* SQLITE_DEBUG */
167
168
danielk19778c0a7912008-08-20 14:49:23 +0000169/********************************** Linked List Management ********************/
170
drha8dcba92014-08-22 20:35:29 +0000171/* Allowed values for second argument to pcacheManageDirtyList() */
172#define PCACHE_DIRTYLIST_REMOVE 1 /* Remove pPage from dirty list */
173#define PCACHE_DIRTYLIST_ADD 2 /* Add pPage to the dirty list */
174#define PCACHE_DIRTYLIST_FRONT 3 /* Move pPage to the front of the list */
danielk19778c0a7912008-08-20 14:49:23 +0000175
176/*
drha8dcba92014-08-22 20:35:29 +0000177** Manage pPage's participation on the dirty list. Bits of the addRemove
178** argument determines what operation to do. The 0x01 bit means first
179** remove pPage from the dirty list. The 0x02 means add pPage back to
180** the dirty list. Doing both moves pPage to the front of the dirty list.
danielk19778c0a7912008-08-20 14:49:23 +0000181*/
drha8dcba92014-08-22 20:35:29 +0000182static void pcacheManageDirtyList(PgHdr *pPage, u8 addRemove){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000183 PCache *p = pPage->pCache;
danielk19778c0a7912008-08-20 14:49:23 +0000184
drh5c8e0922016-05-11 10:57:04 +0000185 pcacheTrace(("%p.DIRTYLIST.%s %d\n", p,
186 addRemove==1 ? "REMOVE" : addRemove==2 ? "ADD" : "FRONT",
187 pPage->pgno));
drha8dcba92014-08-22 20:35:29 +0000188 if( addRemove & PCACHE_DIRTYLIST_REMOVE ){
189 assert( pPage->pDirtyNext || pPage==p->pDirtyTail );
190 assert( pPage->pDirtyPrev || pPage==p->pDirty );
191
192 /* Update the PCache1.pSynced variable if necessary. */
193 if( p->pSynced==pPage ){
danb2ef9002016-05-11 15:41:15 +0000194 p->pSynced = pPage->pDirtyPrev;
drha8dcba92014-08-22 20:35:29 +0000195 }
196
197 if( pPage->pDirtyNext ){
198 pPage->pDirtyNext->pDirtyPrev = pPage->pDirtyPrev;
199 }else{
200 assert( pPage==p->pDirtyTail );
201 p->pDirtyTail = pPage->pDirtyPrev;
202 }
203 if( pPage->pDirtyPrev ){
204 pPage->pDirtyPrev->pDirtyNext = pPage->pDirtyNext;
205 }else{
dan401907e2016-05-11 20:03:23 +0000206 /* If there are now no dirty pages in the cache, set eCreate to 2.
207 ** This is an optimization that allows sqlite3PcacheFetch() to skip
208 ** searching for a dirty page to eject from the cache when it might
209 ** otherwise have to. */
drha8dcba92014-08-22 20:35:29 +0000210 assert( pPage==p->pDirty );
211 p->pDirty = pPage->pDirtyNext;
dan401907e2016-05-11 20:03:23 +0000212 assert( p->bPurgeable || p->eCreate==2 );
213 if( p->pDirty==0 ){ /*OPTIMIZATION-IF-TRUE*/
214 assert( p->bPurgeable==0 || p->eCreate==1 );
drha8dcba92014-08-22 20:35:29 +0000215 p->eCreate = 2;
216 }
217 }
danielk19778c0a7912008-08-20 14:49:23 +0000218 }
drha8dcba92014-08-22 20:35:29 +0000219 if( addRemove & PCACHE_DIRTYLIST_ADD ){
drhf0dae6d2017-09-01 12:18:41 +0000220 pPage->pDirtyPrev = 0;
drha8dcba92014-08-22 20:35:29 +0000221 pPage->pDirtyNext = p->pDirty;
222 if( pPage->pDirtyNext ){
223 assert( pPage->pDirtyNext->pDirtyPrev==0 );
224 pPage->pDirtyNext->pDirtyPrev = pPage;
drh36ce9192014-09-12 20:30:59 +0000225 }else{
226 p->pDirtyTail = pPage;
227 if( p->bPurgeable ){
228 assert( p->eCreate==2 );
229 p->eCreate = 1;
230 }
drha8dcba92014-08-22 20:35:29 +0000231 }
232 p->pDirty = pPage;
dan613723d2016-05-12 09:48:23 +0000233
234 /* If pSynced is NULL and this page has a clear NEED_SYNC flag, set
235 ** pSynced to point to it. Checking the NEED_SYNC flag is an
236 ** optimization, as if pSynced points to a page with the NEED_SYNC
237 ** flag set sqlite3PcacheFetchStress() searches through all newer
238 ** entries of the dirty-list for a page with NEED_SYNC clear anyway. */
239 if( !p->pSynced
240 && 0==(pPage->flags&PGHDR_NEED_SYNC) /*OPTIMIZATION-IF-FALSE*/
241 ){
drha8dcba92014-08-22 20:35:29 +0000242 p->pSynced = pPage;
243 }
danielk1977bc2ca9e2008-11-13 14:28:28 +0000244 }
drh7aeb2162016-05-13 04:24:25 +0000245 pcacheDump(p);
danielk19778c0a7912008-08-20 14:49:23 +0000246}
247
248/*
danielk1977bc2ca9e2008-11-13 14:28:28 +0000249** Wrapper around the pluggable caches xUnpin method. If the cache is
250** being used for an in-memory database, this function is a no-op.
danielk19778c0a7912008-08-20 14:49:23 +0000251*/
danielk1977bc2ca9e2008-11-13 14:28:28 +0000252static void pcacheUnpin(PgHdr *p){
drha8dcba92014-08-22 20:35:29 +0000253 if( p->pCache->bPurgeable ){
drh5c8e0922016-05-11 10:57:04 +0000254 pcacheTrace(("%p.UNPIN %d\n", p->pCache, p->pgno));
drha8dcba92014-08-22 20:35:29 +0000255 sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 0);
drh7aeb2162016-05-13 04:24:25 +0000256 pcacheDump(p->pCache);
danielk19778c0a7912008-08-20 14:49:23 +0000257 }
danielk19778c0a7912008-08-20 14:49:23 +0000258}
259
drhc3031c62014-08-26 15:06:49 +0000260/*
drh9b0cf342015-11-12 14:57:19 +0000261** Compute the number of pages of cache requested. p->szCache is the
drhe0e84292015-02-27 21:53:35 +0000262** cache size requested by the "PRAGMA cache_size" statement.
drhc3031c62014-08-26 15:06:49 +0000263*/
264static int numberOfCachePages(PCache *p){
265 if( p->szCache>=0 ){
drhe0e84292015-02-27 21:53:35 +0000266 /* IMPLEMENTATION-OF: R-42059-47211 If the argument N is positive then the
267 ** suggested cache size is set to N. */
drhc3031c62014-08-26 15:06:49 +0000268 return p->szCache;
269 }else{
drhe7e95392021-09-03 18:11:12 +0000270 i64 n;
drh0ce974d2019-06-12 22:46:04 +0000271 /* IMPLEMANTATION-OF: R-59858-46238 If the argument N is negative, then the
272 ** number of cache pages is adjusted to be a number of pages that would
273 ** use approximately abs(N*1024) bytes of memory based on the current
274 ** page size. */
drhe7e95392021-09-03 18:11:12 +0000275 n = ((-1024*(i64)p->szCache)/(p->szPage+p->szExtra));
276 if( n>1000000000 ) n = 1000000000;
277 return (int)n;
drhc3031c62014-08-26 15:06:49 +0000278 }
279}
280
danielk19778c0a7912008-08-20 14:49:23 +0000281/*************************************************** General Interfaces ******
282**
283** Initialize and shutdown the page cache subsystem. Neither of these
284** functions are threadsafe.
285*/
286int sqlite3PcacheInitialize(void){
dan22e21ff2011-11-08 20:08:44 +0000287 if( sqlite3GlobalConfig.pcache2.xInit==0 ){
drhf759bb82010-09-09 18:25:34 +0000288 /* IMPLEMENTATION-OF: R-26801-64137 If the xInit() method is NULL, then the
289 ** built-in default page cache is used instead of the application defined
290 ** page cache. */
danielk1977bc2ca9e2008-11-13 14:28:28 +0000291 sqlite3PCacheSetDefault();
drh55f66b32019-07-16 19:44:32 +0000292 assert( sqlite3GlobalConfig.pcache2.xInit!=0 );
danielk19778c0a7912008-08-20 14:49:23 +0000293 }
dan22e21ff2011-11-08 20:08:44 +0000294 return sqlite3GlobalConfig.pcache2.xInit(sqlite3GlobalConfig.pcache2.pArg);
danielk19778c0a7912008-08-20 14:49:23 +0000295}
296void sqlite3PcacheShutdown(void){
dan22e21ff2011-11-08 20:08:44 +0000297 if( sqlite3GlobalConfig.pcache2.xShutdown ){
drhf759bb82010-09-09 18:25:34 +0000298 /* IMPLEMENTATION-OF: R-26000-56589 The xShutdown() method may be NULL. */
dan22e21ff2011-11-08 20:08:44 +0000299 sqlite3GlobalConfig.pcache2.xShutdown(sqlite3GlobalConfig.pcache2.pArg);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000300 }
danielk19778c0a7912008-08-20 14:49:23 +0000301}
302
303/*
304** Return the size in bytes of a PCache object.
305*/
306int sqlite3PcacheSize(void){ return sizeof(PCache); }
307
308/*
danielk1977bc2ca9e2008-11-13 14:28:28 +0000309** Create a new PCache object. Storage space to hold the object
310** has already been allocated and is passed in as the p pointer.
311** The caller discovers how much space needs to be allocated by
312** calling sqlite3PcacheSize().
drha2ee5892016-12-09 16:02:00 +0000313**
314** szExtra is some extra space allocated for each page. The first
315** 8 bytes of the extra space will be zeroed as the page is allocated,
316** but remaining content will be uninitialized. Though it is opaque
317** to this module, the extra space really ends up being the MemPage
318** structure in the pager.
danielk19778c0a7912008-08-20 14:49:23 +0000319*/
drhc3031c62014-08-26 15:06:49 +0000320int sqlite3PcacheOpen(
danielk19778c0a7912008-08-20 14:49:23 +0000321 int szPage, /* Size of every page */
322 int szExtra, /* Extra space associated with each page */
323 int bPurgeable, /* True if pages are on backing store */
danielk1977a858aa22008-08-22 16:22:17 +0000324 int (*xStress)(void*,PgHdr*),/* Call to try to make pages clean */
danielk19778c0a7912008-08-20 14:49:23 +0000325 void *pStress, /* Argument to xStress */
326 PCache *p /* Preallocated space for the PCache */
327){
danielk19778c0a7912008-08-20 14:49:23 +0000328 memset(p, 0, sizeof(PCache));
drhc3031c62014-08-26 15:06:49 +0000329 p->szPage = 1;
danielk19778c0a7912008-08-20 14:49:23 +0000330 p->szExtra = szExtra;
drha2ee5892016-12-09 16:02:00 +0000331 assert( szExtra>=8 ); /* First 8 bytes will be zeroed */
danielk19778c0a7912008-08-20 14:49:23 +0000332 p->bPurgeable = bPurgeable;
drhfe21a792014-02-03 17:04:29 +0000333 p->eCreate = 2;
danielk19778c0a7912008-08-20 14:49:23 +0000334 p->xStress = xStress;
335 p->pStress = pStress;
drh3b42abb2011-11-09 14:23:04 +0000336 p->szCache = 100;
drh9b0cf342015-11-12 14:57:19 +0000337 p->szSpill = 1;
drh5c8e0922016-05-11 10:57:04 +0000338 pcacheTrace(("%p.OPEN szPage %d bPurgeable %d\n",p,szPage,bPurgeable));
drhc3031c62014-08-26 15:06:49 +0000339 return sqlite3PcacheSetPageSize(p, szPage);
danielk19778c0a7912008-08-20 14:49:23 +0000340}
341
drh41d30272008-08-20 21:47:45 +0000342/*
danielk1977bc2ca9e2008-11-13 14:28:28 +0000343** Change the page size for PCache object. The caller must ensure that there
344** are no outstanding page references when this function is called.
drh41d30272008-08-20 21:47:45 +0000345*/
drhc3031c62014-08-26 15:06:49 +0000346int sqlite3PcacheSetPageSize(PCache *pCache, int szPage){
drh95a0b372015-09-03 20:43:55 +0000347 assert( pCache->nRefSum==0 && pCache->pDirty==0 );
drhc3031c62014-08-26 15:06:49 +0000348 if( pCache->szPage ){
349 sqlite3_pcache *pNew;
350 pNew = sqlite3GlobalConfig.pcache2.xCreate(
drh51dc84e2014-12-30 13:04:25 +0000351 szPage, pCache->szExtra + ROUND8(sizeof(PgHdr)),
352 pCache->bPurgeable
drhc3031c62014-08-26 15:06:49 +0000353 );
mistachkinfad30392016-02-13 23:43:46 +0000354 if( pNew==0 ) return SQLITE_NOMEM_BKPT;
drhc3031c62014-08-26 15:06:49 +0000355 sqlite3GlobalConfig.pcache2.xCachesize(pNew, numberOfCachePages(pCache));
356 if( pCache->pCache ){
357 sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
358 }
359 pCache->pCache = pNew;
drhc3031c62014-08-26 15:06:49 +0000360 pCache->szPage = szPage;
drh5c8e0922016-05-11 10:57:04 +0000361 pcacheTrace(("%p.PAGESIZE %d\n",pCache,szPage));
danielk1977bc2ca9e2008-11-13 14:28:28 +0000362 }
drhc3031c62014-08-26 15:06:49 +0000363 return SQLITE_OK;
drh3b42abb2011-11-09 14:23:04 +0000364}
365
366/*
danielk19778c0a7912008-08-20 14:49:23 +0000367** Try to obtain a page from the cache.
drhbc59ac02014-08-27 23:18:01 +0000368**
369** This routine returns a pointer to an sqlite3_pcache_page object if
370** such an object is already in cache, or if a new one is created.
371** This routine returns a NULL pointer if the object was not in cache
372** and could not be created.
373**
374** The createFlags should be 0 to check for existing pages and should
375** be 3 (not 1, but 3) to try to create a new page.
376**
377** If the createFlag is 0, then NULL is always returned if the page
378** is not already in the cache. If createFlag is 1, then a new page
379** is created only if that can be done without spilling dirty pages
380** and without exceeding the cache size limit.
381**
382** The caller needs to invoke sqlite3PcacheFetchFinish() to properly
383** initialize the sqlite3_pcache_page object and convert it into a
384** PgHdr object. The sqlite3PcacheFetch() and sqlite3PcacheFetchFinish()
385** routines are split this way for performance reasons. When separated
386** they can both (usually) operate without having to push values to
387** the stack on entry and pop them back off on exit, which saves a
388** lot of pushing and popping.
danielk19778c0a7912008-08-20 14:49:23 +0000389*/
drhbc59ac02014-08-27 23:18:01 +0000390sqlite3_pcache_page *sqlite3PcacheFetch(
danielk19778c0a7912008-08-20 14:49:23 +0000391 PCache *pCache, /* Obtain the page from this cache */
392 Pgno pgno, /* Page number to obtain */
drhbc59ac02014-08-27 23:18:01 +0000393 int createFlag /* If true, create page if it does not exist already */
danielk19778c0a7912008-08-20 14:49:23 +0000394){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000395 int eCreate;
drh5c8e0922016-05-11 10:57:04 +0000396 sqlite3_pcache_page *pRes;
danielk1977f599a192008-08-28 10:21:16 +0000397
danielk19778c0a7912008-08-20 14:49:23 +0000398 assert( pCache!=0 );
drhc3031c62014-08-26 15:06:49 +0000399 assert( pCache->pCache!=0 );
400 assert( createFlag==3 || createFlag==0 );
dan401907e2016-05-11 20:03:23 +0000401 assert( pCache->eCreate==((pCache->bPurgeable && pCache->pDirty) ? 1 : 2) );
danielk19778c0a7912008-08-20 14:49:23 +0000402
drhfe21a792014-02-03 17:04:29 +0000403 /* eCreate defines what to do if the page does not exist.
404 ** 0 Do not allocate a new page. (createFlag==0)
405 ** 1 Allocate a new page if doing so is inexpensive.
406 ** (createFlag==1 AND bPurgeable AND pDirty)
407 ** 2 Allocate a new page even it doing so is difficult.
408 ** (createFlag==1 AND !(bPurgeable AND pDirty)
409 */
drhc3031c62014-08-26 15:06:49 +0000410 eCreate = createFlag & pCache->eCreate;
411 assert( eCreate==0 || eCreate==1 || eCreate==2 );
412 assert( createFlag==0 || pCache->eCreate==eCreate );
413 assert( createFlag==0 || eCreate==1+(!pCache->bPurgeable||!pCache->pDirty) );
drh5c8e0922016-05-11 10:57:04 +0000414 pRes = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, eCreate);
drhf6e3e312022-08-27 14:43:34 +0000415 pcacheTrace(("%p.FETCH %d%s (result: %p) ",pCache,pgno,
drh5c8e0922016-05-11 10:57:04 +0000416 createFlag?" create":"",pRes));
drhf6e3e312022-08-27 14:43:34 +0000417 pcachePageTrace(pgno, pRes);
drh5c8e0922016-05-11 10:57:04 +0000418 return pRes;
drhbc59ac02014-08-27 23:18:01 +0000419}
danielk1977bc2ca9e2008-11-13 14:28:28 +0000420
drhbc59ac02014-08-27 23:18:01 +0000421/*
422** If the sqlite3PcacheFetch() routine is unable to allocate a new
dan41113b62016-04-05 21:07:58 +0000423** page because no clean pages are available for reuse and the cache
drhbc59ac02014-08-27 23:18:01 +0000424** size limit has been reached, then this routine can be invoked to
425** try harder to allocate a page. This routine might invoke the stress
426** callback to spill dirty pages to the journal. It will then try to
427** allocate the new page and will only fail to allocate a new page on
428** an OOM error.
429**
430** This routine should be invoked only after sqlite3PcacheFetch() fails.
431*/
432int sqlite3PcacheFetchStress(
433 PCache *pCache, /* Obtain the page from this cache */
434 Pgno pgno, /* Page number to obtain */
435 sqlite3_pcache_page **ppPage /* Write result here */
436){
437 PgHdr *pPg;
438 if( pCache->eCreate==2 ) return 0;
439
drh9b0cf342015-11-12 14:57:19 +0000440 if( sqlite3PcachePagecount(pCache)>pCache->szSpill ){
441 /* Find a dirty page to write-out and recycle. First try to find a
442 ** page that does not require a journal-sync (one with PGHDR_NEED_SYNC
443 ** cleared), but if that is not possible settle for any other
444 ** unreferenced dirty page.
danb2ef9002016-05-11 15:41:15 +0000445 **
446 ** If the LRU page in the dirty list that has a clear PGHDR_NEED_SYNC
447 ** flag is currently referenced, then the following may leave pSynced
448 ** set incorrectly (pointing to other than the LRU page with NEED_SYNC
449 ** cleared). This is Ok, as pSynced is just an optimization. */
drh9b0cf342015-11-12 14:57:19 +0000450 for(pPg=pCache->pSynced;
451 pPg && (pPg->nRef || (pPg->flags&PGHDR_NEED_SYNC));
452 pPg=pPg->pDirtyPrev
453 );
454 pCache->pSynced = pPg;
455 if( !pPg ){
456 for(pPg=pCache->pDirtyTail; pPg && pPg->nRef; pPg=pPg->pDirtyPrev);
457 }
458 if( pPg ){
459 int rc;
drhc97125e2011-05-28 15:53:07 +0000460#ifdef SQLITE_LOG_CACHE_SPILL
drh9b0cf342015-11-12 14:57:19 +0000461 sqlite3_log(SQLITE_FULL,
462 "spill page %d making room for %d - cache used: %d/%d",
463 pPg->pgno, pgno,
dan58db4c72018-03-12 21:09:16 +0000464 sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache),
drhbc59ac02014-08-27 23:18:01 +0000465 numberOfCachePages(pCache));
drhc97125e2011-05-28 15:53:07 +0000466#endif
drh5c8e0922016-05-11 10:57:04 +0000467 pcacheTrace(("%p.SPILL %d\n",pCache,pPg->pgno));
drh9b0cf342015-11-12 14:57:19 +0000468 rc = pCache->xStress(pCache->pStress, pPg);
drh7aeb2162016-05-13 04:24:25 +0000469 pcacheDump(pCache);
drh9b0cf342015-11-12 14:57:19 +0000470 if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
471 return rc;
472 }
danielk1977f599a192008-08-28 10:21:16 +0000473 }
danielk19778c0a7912008-08-20 14:49:23 +0000474 }
drhbc59ac02014-08-27 23:18:01 +0000475 *ppPage = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, 2);
mistachkinfad30392016-02-13 23:43:46 +0000476 return *ppPage==0 ? SQLITE_NOMEM_BKPT : SQLITE_OK;
drhbc59ac02014-08-27 23:18:01 +0000477}
478
479/*
480** This is a helper routine for sqlite3PcacheFetchFinish()
481**
482** In the uncommon case where the page being fetched has not been
483** initialized, this routine is invoked to do the initialization.
484** This routine is broken out into a separate function since it
485** requires extra stack manipulation that can be avoided in the common
486** case.
487*/
488static SQLITE_NOINLINE PgHdr *pcacheFetchFinishWithInit(
489 PCache *pCache, /* Obtain the page from this cache */
490 Pgno pgno, /* Page number obtained */
491 sqlite3_pcache_page *pPage /* Page obtained by prior PcacheFetch() call */
492){
493 PgHdr *pPgHdr;
494 assert( pPage!=0 );
495 pPgHdr = (PgHdr*)pPage->pExtra;
496 assert( pPgHdr->pPage==0 );
drh216b70f2016-10-01 20:43:41 +0000497 memset(&pPgHdr->pDirty, 0, sizeof(PgHdr) - offsetof(PgHdr,pDirty));
drhbc59ac02014-08-27 23:18:01 +0000498 pPgHdr->pPage = pPage;
499 pPgHdr->pData = pPage->pBuf;
500 pPgHdr->pExtra = (void *)&pPgHdr[1];
drha2ee5892016-12-09 16:02:00 +0000501 memset(pPgHdr->pExtra, 0, 8);
drhbc59ac02014-08-27 23:18:01 +0000502 pPgHdr->pCache = pCache;
503 pPgHdr->pgno = pgno;
drhc78ae912015-06-29 04:21:15 +0000504 pPgHdr->flags = PGHDR_CLEAN;
drhbc59ac02014-08-27 23:18:01 +0000505 return sqlite3PcacheFetchFinish(pCache,pgno,pPage);
506}
507
508/*
509** This routine converts the sqlite3_pcache_page object returned by
510** sqlite3PcacheFetch() into an initialized PgHdr object. This routine
511** must be called after sqlite3PcacheFetch() in order to get a usable
512** result.
513*/
514PgHdr *sqlite3PcacheFetchFinish(
515 PCache *pCache, /* Obtain the page from this cache */
516 Pgno pgno, /* Page number obtained */
517 sqlite3_pcache_page *pPage /* Page obtained by prior PcacheFetch() call */
518){
519 PgHdr *pPgHdr;
520
drhd8c0ba32015-06-30 03:57:59 +0000521 assert( pPage!=0 );
drhbc59ac02014-08-27 23:18:01 +0000522 pPgHdr = (PgHdr *)pPage->pExtra;
523
524 if( !pPgHdr->pPage ){
525 return pcacheFetchFinishWithInit(pCache, pgno, pPage);
526 }
drh95a0b372015-09-03 20:43:55 +0000527 pCache->nRefSum++;
drhbc59ac02014-08-27 23:18:01 +0000528 pPgHdr->nRef++;
drha0f6b122016-05-13 15:22:06 +0000529 assert( sqlite3PcachePageSanity(pPgHdr) );
drhbc59ac02014-08-27 23:18:01 +0000530 return pPgHdr;
danielk19778c0a7912008-08-20 14:49:23 +0000531}
532
533/*
danielk1977bc2ca9e2008-11-13 14:28:28 +0000534** Decrement the reference count on a page. If the page is clean and the
peter.d.reid60ec9142014-09-06 16:39:46 +0000535** reference count drops to 0, then it is made eligible for recycling.
danielk19778c0a7912008-08-20 14:49:23 +0000536*/
drha8dcba92014-08-22 20:35:29 +0000537void SQLITE_NOINLINE sqlite3PcacheRelease(PgHdr *p){
danielk19778c0a7912008-08-20 14:49:23 +0000538 assert( p->nRef>0 );
drh95a0b372015-09-03 20:43:55 +0000539 p->pCache->nRefSum--;
540 if( (--p->nRef)==0 ){
drhc78ae912015-06-29 04:21:15 +0000541 if( p->flags&PGHDR_CLEAN ){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000542 pcacheUnpin(p);
drhdfcdc662017-09-01 12:57:33 +0000543 }else{
drha8dcba92014-08-22 20:35:29 +0000544 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_FRONT);
drhf0f025b2022-08-27 16:11:00 +0000545 assert( sqlite3PcachePageSanity(p) );
danielk1977d491e1b2008-08-26 18:05:48 +0000546 }
danielk1977502b7432008-08-25 14:49:42 +0000547 }
danielk19778c0a7912008-08-20 14:49:23 +0000548}
549
danielk1977bc2ca9e2008-11-13 14:28:28 +0000550/*
551** Increase the reference count of a supplied page by 1.
552*/
danielk19778c0a7912008-08-20 14:49:23 +0000553void sqlite3PcacheRef(PgHdr *p){
danielk1977502b7432008-08-25 14:49:42 +0000554 assert(p->nRef>0);
drha0f6b122016-05-13 15:22:06 +0000555 assert( sqlite3PcachePageSanity(p) );
danielk1977502b7432008-08-25 14:49:42 +0000556 p->nRef++;
drh95a0b372015-09-03 20:43:55 +0000557 p->pCache->nRefSum++;
danielk19778c0a7912008-08-20 14:49:23 +0000558}
559
560/*
danielk19774abdfa42008-08-27 09:44:39 +0000561** Drop a page from the cache. There must be exactly one reference to the
562** page. This function deletes that reference, so after it returns the
563** page pointed to by p is invalid.
danielk19778c0a7912008-08-20 14:49:23 +0000564*/
565void sqlite3PcacheDrop(PgHdr *p){
danielk19778c0a7912008-08-20 14:49:23 +0000566 assert( p->nRef==1 );
drha0f6b122016-05-13 15:22:06 +0000567 assert( sqlite3PcachePageSanity(p) );
danielk1977bc2ca9e2008-11-13 14:28:28 +0000568 if( p->flags&PGHDR_DIRTY ){
drha8dcba92014-08-22 20:35:29 +0000569 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000570 }
drh95a0b372015-09-03 20:43:55 +0000571 p->pCache->nRefSum--;
drha8dcba92014-08-22 20:35:29 +0000572 sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 1);
danielk19778c0a7912008-08-20 14:49:23 +0000573}
574
575/*
danielk1977bc2ca9e2008-11-13 14:28:28 +0000576** Make sure the page is marked as dirty. If it isn't dirty already,
danielk19778c0a7912008-08-20 14:49:23 +0000577** make it so.
578*/
579void sqlite3PcacheMakeDirty(PgHdr *p){
drhc63e4092022-03-21 18:48:31 +0000580 assert( p->nRef>0 );
drha0f6b122016-05-13 15:22:06 +0000581 assert( sqlite3PcachePageSanity(p) );
dan82c04472016-05-12 17:06:04 +0000582 if( p->flags & (PGHDR_CLEAN|PGHDR_DONT_WRITE) ){ /*OPTIMIZATION-IF-FALSE*/
drhc78ae912015-06-29 04:21:15 +0000583 p->flags &= ~PGHDR_DONT_WRITE;
584 if( p->flags & PGHDR_CLEAN ){
585 p->flags ^= (PGHDR_DIRTY|PGHDR_CLEAN);
drh5c8e0922016-05-11 10:57:04 +0000586 pcacheTrace(("%p.DIRTY %d\n",p->pCache,p->pgno));
drhc78ae912015-06-29 04:21:15 +0000587 assert( (p->flags & (PGHDR_DIRTY|PGHDR_CLEAN))==PGHDR_DIRTY );
588 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_ADD);
drhf0f025b2022-08-27 16:11:00 +0000589 assert( sqlite3PcachePageSanity(p) );
drhc78ae912015-06-29 04:21:15 +0000590 }
drha0f6b122016-05-13 15:22:06 +0000591 assert( sqlite3PcachePageSanity(p) );
danielk1977d491e1b2008-08-26 18:05:48 +0000592 }
danielk1977f599a192008-08-28 10:21:16 +0000593}
594
595/*
danielk1977bc2ca9e2008-11-13 14:28:28 +0000596** Make sure the page is marked as clean. If it isn't clean already,
danielk1977f599a192008-08-28 10:21:16 +0000597** make it so.
598*/
599void sqlite3PcacheMakeClean(PgHdr *p){
drha0f6b122016-05-13 15:22:06 +0000600 assert( sqlite3PcachePageSanity(p) );
drh9d9c41e2017-10-31 03:40:15 +0000601 assert( (p->flags & PGHDR_DIRTY)!=0 );
602 assert( (p->flags & PGHDR_CLEAN)==0 );
603 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE);
604 p->flags &= ~(PGHDR_DIRTY|PGHDR_NEED_SYNC|PGHDR_WRITEABLE);
605 p->flags |= PGHDR_CLEAN;
606 pcacheTrace(("%p.CLEAN %d\n",p->pCache,p->pgno));
607 assert( sqlite3PcachePageSanity(p) );
608 if( p->nRef==0 ){
609 pcacheUnpin(p);
danielk1977f599a192008-08-28 10:21:16 +0000610 }
danielk19778c0a7912008-08-20 14:49:23 +0000611}
612
613/*
614** Make every page in the cache clean.
615*/
616void sqlite3PcacheCleanAll(PCache *pCache){
617 PgHdr *p;
drh7aeb2162016-05-13 04:24:25 +0000618 pcacheTrace(("%p.CLEAN-ALL\n",pCache));
danielk19778c0a7912008-08-20 14:49:23 +0000619 while( (p = pCache->pDirty)!=0 ){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000620 sqlite3PcacheMakeClean(p);
danielk19778c0a7912008-08-20 14:49:23 +0000621 }
622}
623
624/*
dan41113b62016-04-05 21:07:58 +0000625** Clear the PGHDR_NEED_SYNC and PGHDR_WRITEABLE flag from all dirty pages.
626*/
627void sqlite3PcacheClearWritable(PCache *pCache){
628 PgHdr *p;
drh7aeb2162016-05-13 04:24:25 +0000629 pcacheTrace(("%p.CLEAR-WRITEABLE\n",pCache));
dan41113b62016-04-05 21:07:58 +0000630 for(p=pCache->pDirty; p; p=p->pDirtyNext){
631 p->flags &= ~(PGHDR_NEED_SYNC|PGHDR_WRITEABLE);
632 }
633 pCache->pSynced = pCache->pDirtyTail;
634}
635
636/*
danielk1977bc2ca9e2008-11-13 14:28:28 +0000637** Clear the PGHDR_NEED_SYNC flag from all dirty pages.
638*/
639void sqlite3PcacheClearSyncFlags(PCache *pCache){
640 PgHdr *p;
641 for(p=pCache->pDirty; p; p=p->pDirtyNext){
642 p->flags &= ~PGHDR_NEED_SYNC;
643 }
644 pCache->pSynced = pCache->pDirtyTail;
645}
646
647/*
648** Change the page number of page p to newPgno.
danielk19778c0a7912008-08-20 14:49:23 +0000649*/
650void sqlite3PcacheMove(PgHdr *p, Pgno newPgno){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000651 PCache *pCache = p->pCache;
drh5fc3a8a2022-09-02 15:09:55 +0000652 sqlite3_pcache_page *pOther;
danielk1977d491e1b2008-08-26 18:05:48 +0000653 assert( p->nRef>0 );
danielk1977bc2ca9e2008-11-13 14:28:28 +0000654 assert( newPgno>0 );
drha0f6b122016-05-13 15:22:06 +0000655 assert( sqlite3PcachePageSanity(p) );
drh5c8e0922016-05-11 10:57:04 +0000656 pcacheTrace(("%p.MOVE %d -> %d\n",pCache,p->pgno,newPgno));
drh5fc3a8a2022-09-02 15:09:55 +0000657 pOther = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, newPgno, 0);
drh5fc3a8a2022-09-02 15:09:55 +0000658 if( pOther ){
drh8c983dd2022-09-07 19:28:18 +0000659 PgHdr *pXPage = (PgHdr*)pOther->pExtra;
660 assert( pXPage->nRef==0 );
661 pXPage->nRef++;
662 pCache->nRefSum++;
663 sqlite3PcacheDrop(pXPage);
drh5fc3a8a2022-09-02 15:09:55 +0000664 }
drh8c983dd2022-09-07 19:28:18 +0000665 sqlite3GlobalConfig.pcache2.xRekey(pCache->pCache, p->pPage, p->pgno,newPgno);
danielk19778c0a7912008-08-20 14:49:23 +0000666 p->pgno = newPgno;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000667 if( (p->flags&PGHDR_DIRTY) && (p->flags&PGHDR_NEED_SYNC) ){
drha8dcba92014-08-22 20:35:29 +0000668 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_FRONT);
drhf0f025b2022-08-27 16:11:00 +0000669 assert( sqlite3PcachePageSanity(p) );
danielk19778c0a7912008-08-20 14:49:23 +0000670 }
danielk19778c0a7912008-08-20 14:49:23 +0000671}
672
673/*
danielk1977bc2ca9e2008-11-13 14:28:28 +0000674** Drop every cache entry whose page number is greater than "pgno". The
675** caller must ensure that there are no outstanding references to any pages
676** other than page 1 with a page number greater than pgno.
677**
678** If there is a reference to page 1 and the pgno parameter passed to this
679** function is 0, then the data area associated with page 1 is zeroed, but
680** the page object is not dropped.
danielk19778c0a7912008-08-20 14:49:23 +0000681*/
682void sqlite3PcacheTruncate(PCache *pCache, Pgno pgno){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000683 if( pCache->pCache ){
684 PgHdr *p;
685 PgHdr *pNext;
drh5c8e0922016-05-11 10:57:04 +0000686 pcacheTrace(("%p.TRUNCATE %d\n",pCache,pgno));
danielk1977bc2ca9e2008-11-13 14:28:28 +0000687 for(p=pCache->pDirty; p; p=pNext){
688 pNext = p->pDirtyNext;
drhf3609ee2010-03-19 19:23:51 +0000689 /* This routine never gets call with a positive pgno except right
690 ** after sqlite3PcacheCleanAll(). So if there are dirty pages,
691 ** it must be that pgno==0.
692 */
693 assert( p->pgno>0 );
dan41113b62016-04-05 21:07:58 +0000694 if( p->pgno>pgno ){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000695 assert( p->flags&PGHDR_DIRTY );
696 sqlite3PcacheMakeClean(p);
danielk19778c0a7912008-08-20 14:49:23 +0000697 }
698 }
drh95a0b372015-09-03 20:43:55 +0000699 if( pgno==0 && pCache->nRefSum ){
drh39065c62015-06-26 02:41:31 +0000700 sqlite3_pcache_page *pPage1;
701 pPage1 = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache,1,0);
702 if( ALWAYS(pPage1) ){ /* Page 1 is always available in cache, because
drh95a0b372015-09-03 20:43:55 +0000703 ** pCache->nRefSum>0 */
drh39065c62015-06-26 02:41:31 +0000704 memset(pPage1->pBuf, 0, pCache->szPage);
705 pgno = 1;
706 }
danielk1977bc2ca9e2008-11-13 14:28:28 +0000707 }
dan22e21ff2011-11-08 20:08:44 +0000708 sqlite3GlobalConfig.pcache2.xTruncate(pCache->pCache, pgno+1);
danielk1977062d4cb2008-08-29 09:10:02 +0000709 }
710}
danielk19778c0a7912008-08-20 14:49:23 +0000711
712/*
713** Close a cache.
714*/
715void sqlite3PcacheClose(PCache *pCache){
drhc3031c62014-08-26 15:06:49 +0000716 assert( pCache->pCache!=0 );
drh5c8e0922016-05-11 10:57:04 +0000717 pcacheTrace(("%p.CLOSE\n",pCache));
drhc3031c62014-08-26 15:06:49 +0000718 sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
danielk19778c0a7912008-08-20 14:49:23 +0000719}
720
721/*
722** Discard the contents of the cache.
723*/
danielk1977bea2a942009-01-20 17:06:27 +0000724void sqlite3PcacheClear(PCache *pCache){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000725 sqlite3PcacheTruncate(pCache, 0);
danielk19778c0a7912008-08-20 14:49:23 +0000726}
727
728/*
729** Merge two lists of pages connected by pDirty and in pgno order.
mistachkin2380f3f2016-05-20 20:58:30 +0000730** Do not bother fixing the pDirtyPrev pointers.
danielk19778c0a7912008-08-20 14:49:23 +0000731*/
732static PgHdr *pcacheMergeDirtyList(PgHdr *pA, PgHdr *pB){
733 PgHdr result, *pTail;
734 pTail = &result;
drhb982bfe2016-05-20 14:54:54 +0000735 assert( pA!=0 && pB!=0 );
736 for(;;){
danielk19778c0a7912008-08-20 14:49:23 +0000737 if( pA->pgno<pB->pgno ){
738 pTail->pDirty = pA;
739 pTail = pA;
740 pA = pA->pDirty;
drhb982bfe2016-05-20 14:54:54 +0000741 if( pA==0 ){
742 pTail->pDirty = pB;
743 break;
744 }
danielk19778c0a7912008-08-20 14:49:23 +0000745 }else{
746 pTail->pDirty = pB;
747 pTail = pB;
748 pB = pB->pDirty;
drhb982bfe2016-05-20 14:54:54 +0000749 if( pB==0 ){
750 pTail->pDirty = pA;
751 break;
752 }
danielk19778c0a7912008-08-20 14:49:23 +0000753 }
754 }
danielk19778c0a7912008-08-20 14:49:23 +0000755 return result.pDirty;
756}
757
758/*
759** Sort the list of pages in accending order by pgno. Pages are
danielk1977bc2ca9e2008-11-13 14:28:28 +0000760** connected by pDirty pointers. The pDirtyPrev pointers are
danielk19778c0a7912008-08-20 14:49:23 +0000761** corrupted by this sort.
drhe64ca7b2009-07-16 18:21:17 +0000762**
763** Since there cannot be more than 2^31 distinct pages in a database,
764** there cannot be more than 31 buckets required by the merge sorter.
765** One extra bucket is added to catch overflow in case something
766** ever changes to make the previous sentence incorrect.
danielk19778c0a7912008-08-20 14:49:23 +0000767*/
drhe64ca7b2009-07-16 18:21:17 +0000768#define N_SORT_BUCKET 32
danielk19778c0a7912008-08-20 14:49:23 +0000769static PgHdr *pcacheSortDirtyList(PgHdr *pIn){
drhe64ca7b2009-07-16 18:21:17 +0000770 PgHdr *a[N_SORT_BUCKET], *p;
danielk19778c0a7912008-08-20 14:49:23 +0000771 int i;
772 memset(a, 0, sizeof(a));
773 while( pIn ){
774 p = pIn;
775 pIn = p->pDirty;
776 p->pDirty = 0;
drhe64ca7b2009-07-16 18:21:17 +0000777 for(i=0; ALWAYS(i<N_SORT_BUCKET-1); i++){
danielk19778c0a7912008-08-20 14:49:23 +0000778 if( a[i]==0 ){
779 a[i] = p;
780 break;
781 }else{
782 p = pcacheMergeDirtyList(a[i], p);
783 a[i] = 0;
784 }
785 }
drhe64ca7b2009-07-16 18:21:17 +0000786 if( NEVER(i==N_SORT_BUCKET-1) ){
787 /* To get here, there need to be 2^(N_SORT_BUCKET) elements in
788 ** the input list. But that is impossible.
danielk19778c0a7912008-08-20 14:49:23 +0000789 */
790 a[i] = pcacheMergeDirtyList(a[i], p);
791 }
792 }
793 p = a[0];
794 for(i=1; i<N_SORT_BUCKET; i++){
drhb982bfe2016-05-20 14:54:54 +0000795 if( a[i]==0 ) continue;
796 p = p ? pcacheMergeDirtyList(p, a[i]) : a[i];
danielk19778c0a7912008-08-20 14:49:23 +0000797 }
798 return p;
799}
800
801/*
802** Return a list of all dirty pages in the cache, sorted by page number.
803*/
804PgHdr *sqlite3PcacheDirtyList(PCache *pCache){
805 PgHdr *p;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000806 for(p=pCache->pDirty; p; p=p->pDirtyNext){
807 p->pDirty = p->pDirtyNext;
danielk19778c0a7912008-08-20 14:49:23 +0000808 }
809 return pcacheSortDirtyList(pCache->pDirty);
810}
811
danielk19778c0a7912008-08-20 14:49:23 +0000812/*
drh95a0b372015-09-03 20:43:55 +0000813** Return the total number of references to all pages held by the cache.
814**
815** This is not the total number of pages referenced, but the sum of the
816** reference count for all pages.
danielk19778c0a7912008-08-20 14:49:23 +0000817*/
818int sqlite3PcacheRefCount(PCache *pCache){
drh95a0b372015-09-03 20:43:55 +0000819 return pCache->nRefSum;
danielk19778c0a7912008-08-20 14:49:23 +0000820}
821
danielk1977bc2ca9e2008-11-13 14:28:28 +0000822/*
823** Return the number of references to the page supplied as an argument.
824*/
danielk197771d5d2c2008-09-29 11:49:47 +0000825int sqlite3PcachePageRefcount(PgHdr *p){
826 return p->nRef;
827}
828
danielk19778c0a7912008-08-20 14:49:23 +0000829/*
830** Return the total number of pages in the cache.
831*/
832int sqlite3PcachePagecount(PCache *pCache){
drhc3031c62014-08-26 15:06:49 +0000833 assert( pCache->pCache!=0 );
834 return sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache);
danielk19778c0a7912008-08-20 14:49:23 +0000835}
836
danielk1977f3d3c272008-11-19 16:52:44 +0000837#ifdef SQLITE_TEST
danielk19778c0a7912008-08-20 14:49:23 +0000838/*
danielk1977bc2ca9e2008-11-13 14:28:28 +0000839** Get the suggested cache-size value.
danielk19778c0a7912008-08-20 14:49:23 +0000840*/
841int sqlite3PcacheGetCachesize(PCache *pCache){
drh3b42abb2011-11-09 14:23:04 +0000842 return numberOfCachePages(pCache);
danielk19778c0a7912008-08-20 14:49:23 +0000843}
danielk1977f3d3c272008-11-19 16:52:44 +0000844#endif
danielk19778c0a7912008-08-20 14:49:23 +0000845
846/*
847** Set the suggested cache-size value.
848*/
849void sqlite3PcacheSetCachesize(PCache *pCache, int mxPage){
drhc3031c62014-08-26 15:06:49 +0000850 assert( pCache->pCache!=0 );
drh3b42abb2011-11-09 14:23:04 +0000851 pCache->szCache = mxPage;
drhc3031c62014-08-26 15:06:49 +0000852 sqlite3GlobalConfig.pcache2.xCachesize(pCache->pCache,
853 numberOfCachePages(pCache));
danielk19778c0a7912008-08-20 14:49:23 +0000854}
855
drh09419b42011-11-16 19:29:17 +0000856/*
drh9b0cf342015-11-12 14:57:19 +0000857** Set the suggested cache-spill value. Make no changes if if the
858** argument is zero. Return the effective cache-spill size, which will
859** be the larger of the szSpill and szCache.
860*/
861int sqlite3PcacheSetSpillsize(PCache *p, int mxPage){
862 int res;
863 assert( p->pCache!=0 );
864 if( mxPage ){
865 if( mxPage<0 ){
drh4f9c8ec2015-11-12 15:47:48 +0000866 mxPage = (int)((-1024*(i64)mxPage)/(p->szPage+p->szExtra));
drh9b0cf342015-11-12 14:57:19 +0000867 }
868 p->szSpill = mxPage;
869 }
870 res = numberOfCachePages(p);
871 if( res<p->szSpill ) res = p->szSpill;
872 return res;
873}
874
875/*
drh09419b42011-11-16 19:29:17 +0000876** Free up as much memory as possible from the page cache.
877*/
878void sqlite3PcacheShrink(PCache *pCache){
drhc3031c62014-08-26 15:06:49 +0000879 assert( pCache->pCache!=0 );
880 sqlite3GlobalConfig.pcache2.xShrink(pCache->pCache);
drh09419b42011-11-16 19:29:17 +0000881}
882
drhdef68892014-11-04 12:11:23 +0000883/*
884** Return the size of the header added by this middleware layer
885** in the page-cache hierarchy.
886*/
drh37c057b2014-12-30 00:57:29 +0000887int sqlite3HeaderSizePcache(void){ return ROUND8(sizeof(PgHdr)); }
drhdef68892014-11-04 12:11:23 +0000888
dan0f524552016-04-13 16:52:11 +0000889/*
890** Return the number of dirty pages currently in the cache, as a percentage
891** of the configured cache size.
892*/
893int sqlite3PCachePercentDirty(PCache *pCache){
894 PgHdr *pDirty;
895 int nDirty = 0;
896 int nCache = numberOfCachePages(pCache);
897 for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext) nDirty++;
drhb5895e52016-04-18 13:30:50 +0000898 return nCache ? (int)(((i64)nDirty * 100) / nCache) : 0;
dan0f524552016-04-13 16:52:11 +0000899}
drhdef68892014-11-04 12:11:23 +0000900
dan09236752018-11-22 19:10:14 +0000901#ifdef SQLITE_DIRECT_OVERFLOW_READ
902/*
903** Return true if there are one or more dirty pages in the cache. Else false.
904*/
905int sqlite3PCacheIsDirty(PCache *pCache){
906 return (pCache->pDirty!=0);
907}
908#endif
909
danielk1977750e87d2009-07-25 11:46:48 +0000910#if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)
danielk197767e3da72008-08-21 12:19:44 +0000911/*
danielk1977bc2ca9e2008-11-13 14:28:28 +0000912** For all dirty pages currently in the cache, invoke the specified
913** callback. This is only used if the SQLITE_CHECK_PAGES macro is
914** defined.
danielk197767e3da72008-08-21 12:19:44 +0000915*/
danielk1977bc2ca9e2008-11-13 14:28:28 +0000916void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *)){
917 PgHdr *pDirty;
918 for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext){
919 xIter(pDirty);
danielk197767e3da72008-08-21 12:19:44 +0000920 }
danielk1977062d4cb2008-08-29 09:10:02 +0000921}
922#endif