blob: 5ac9d34a1e3e9541fcae821828316c08a8c30c51 [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/*
17** A complete page cache is an instance of this structure.
18*/
19struct PCache {
danielk1977d491e1b2008-08-26 18:05:48 +000020 PgHdr *pDirty, *pDirtyTail; /* List of dirty pages in LRU order */
21 PgHdr *pSynced; /* Last synced page in dirty page list */
drh95a0b372015-09-03 20:43:55 +000022 int nRefSum; /* Sum of ref counts over all pages */
drh3b42abb2011-11-09 14:23:04 +000023 int szCache; /* Configured cache size */
drh9b0cf342015-11-12 14:57:19 +000024 int szSpill; /* Size before spilling occurs */
drha85f7e32008-08-28 02:26:07 +000025 int szPage; /* Size of every page in this cache */
26 int szExtra; /* Size of extra space for each page */
drhfe21a792014-02-03 17:04:29 +000027 u8 bPurgeable; /* True if pages are on backing store */
28 u8 eCreate; /* eCreate value for for xFetch() */
drha85f7e32008-08-28 02:26:07 +000029 int (*xStress)(void*,PgHdr*); /* Call to try make a page clean */
30 void *pStress; /* Argument to xStress */
danielk1977bc2ca9e2008-11-13 14:28:28 +000031 sqlite3_pcache *pCache; /* Pluggable cache module */
danielk19778c0a7912008-08-20 14:49:23 +000032};
33
danielk19778c0a7912008-08-20 14:49:23 +000034/********************************** Linked List Management ********************/
35
drha8dcba92014-08-22 20:35:29 +000036/* Allowed values for second argument to pcacheManageDirtyList() */
37#define PCACHE_DIRTYLIST_REMOVE 1 /* Remove pPage from dirty list */
38#define PCACHE_DIRTYLIST_ADD 2 /* Add pPage to the dirty list */
39#define PCACHE_DIRTYLIST_FRONT 3 /* Move pPage to the front of the list */
danielk19778c0a7912008-08-20 14:49:23 +000040
41/*
drha8dcba92014-08-22 20:35:29 +000042** Manage pPage's participation on the dirty list. Bits of the addRemove
43** argument determines what operation to do. The 0x01 bit means first
44** remove pPage from the dirty list. The 0x02 means add pPage back to
45** the dirty list. Doing both moves pPage to the front of the dirty list.
danielk19778c0a7912008-08-20 14:49:23 +000046*/
drha8dcba92014-08-22 20:35:29 +000047static void pcacheManageDirtyList(PgHdr *pPage, u8 addRemove){
danielk1977bc2ca9e2008-11-13 14:28:28 +000048 PCache *p = pPage->pCache;
danielk19778c0a7912008-08-20 14:49:23 +000049
drha8dcba92014-08-22 20:35:29 +000050 if( addRemove & PCACHE_DIRTYLIST_REMOVE ){
51 assert( pPage->pDirtyNext || pPage==p->pDirtyTail );
52 assert( pPage->pDirtyPrev || pPage==p->pDirty );
53
54 /* Update the PCache1.pSynced variable if necessary. */
55 if( p->pSynced==pPage ){
56 PgHdr *pSynced = pPage->pDirtyPrev;
57 while( pSynced && (pSynced->flags&PGHDR_NEED_SYNC) ){
58 pSynced = pSynced->pDirtyPrev;
59 }
60 p->pSynced = pSynced;
61 }
62
63 if( pPage->pDirtyNext ){
64 pPage->pDirtyNext->pDirtyPrev = pPage->pDirtyPrev;
65 }else{
66 assert( pPage==p->pDirtyTail );
67 p->pDirtyTail = pPage->pDirtyPrev;
68 }
69 if( pPage->pDirtyPrev ){
70 pPage->pDirtyPrev->pDirtyNext = pPage->pDirtyNext;
71 }else{
72 assert( pPage==p->pDirty );
73 p->pDirty = pPage->pDirtyNext;
74 if( p->pDirty==0 && p->bPurgeable ){
75 assert( p->eCreate==1 );
76 p->eCreate = 2;
77 }
78 }
79 pPage->pDirtyNext = 0;
80 pPage->pDirtyPrev = 0;
danielk19778c0a7912008-08-20 14:49:23 +000081 }
drha8dcba92014-08-22 20:35:29 +000082 if( addRemove & PCACHE_DIRTYLIST_ADD ){
83 assert( pPage->pDirtyNext==0 && pPage->pDirtyPrev==0 && p->pDirty!=pPage );
84
85 pPage->pDirtyNext = p->pDirty;
86 if( pPage->pDirtyNext ){
87 assert( pPage->pDirtyNext->pDirtyPrev==0 );
88 pPage->pDirtyNext->pDirtyPrev = pPage;
drh36ce9192014-09-12 20:30:59 +000089 }else{
90 p->pDirtyTail = pPage;
91 if( p->bPurgeable ){
92 assert( p->eCreate==2 );
93 p->eCreate = 1;
94 }
drha8dcba92014-08-22 20:35:29 +000095 }
96 p->pDirty = pPage;
drha8dcba92014-08-22 20:35:29 +000097 if( !p->pSynced && 0==(pPage->flags&PGHDR_NEED_SYNC) ){
98 p->pSynced = pPage;
99 }
danielk1977bc2ca9e2008-11-13 14:28:28 +0000100 }
danielk19778c0a7912008-08-20 14:49:23 +0000101}
102
103/*
danielk1977bc2ca9e2008-11-13 14:28:28 +0000104** Wrapper around the pluggable caches xUnpin method. If the cache is
105** being used for an in-memory database, this function is a no-op.
danielk19778c0a7912008-08-20 14:49:23 +0000106*/
danielk1977bc2ca9e2008-11-13 14:28:28 +0000107static void pcacheUnpin(PgHdr *p){
drha8dcba92014-08-22 20:35:29 +0000108 if( p->pCache->bPurgeable ){
drha8dcba92014-08-22 20:35:29 +0000109 sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 0);
danielk19778c0a7912008-08-20 14:49:23 +0000110 }
danielk19778c0a7912008-08-20 14:49:23 +0000111}
112
drhc3031c62014-08-26 15:06:49 +0000113/*
drh9b0cf342015-11-12 14:57:19 +0000114** Compute the number of pages of cache requested. p->szCache is the
drhe0e84292015-02-27 21:53:35 +0000115** cache size requested by the "PRAGMA cache_size" statement.
drhc3031c62014-08-26 15:06:49 +0000116*/
117static int numberOfCachePages(PCache *p){
118 if( p->szCache>=0 ){
drhe0e84292015-02-27 21:53:35 +0000119 /* IMPLEMENTATION-OF: R-42059-47211 If the argument N is positive then the
120 ** suggested cache size is set to N. */
drhc3031c62014-08-26 15:06:49 +0000121 return p->szCache;
122 }else{
drhe0e84292015-02-27 21:53:35 +0000123 /* IMPLEMENTATION-OF: R-61436-13639 If the argument N is negative, then
124 ** the number of cache pages is adjusted to use approximately abs(N*1024)
125 ** bytes of memory. */
drhc3031c62014-08-26 15:06:49 +0000126 return (int)((-1024*(i64)p->szCache)/(p->szPage+p->szExtra));
127 }
128}
129
danielk19778c0a7912008-08-20 14:49:23 +0000130/*************************************************** General Interfaces ******
131**
132** Initialize and shutdown the page cache subsystem. Neither of these
133** functions are threadsafe.
134*/
135int sqlite3PcacheInitialize(void){
dan22e21ff2011-11-08 20:08:44 +0000136 if( sqlite3GlobalConfig.pcache2.xInit==0 ){
drhf759bb82010-09-09 18:25:34 +0000137 /* IMPLEMENTATION-OF: R-26801-64137 If the xInit() method is NULL, then the
138 ** built-in default page cache is used instead of the application defined
139 ** page cache. */
danielk1977bc2ca9e2008-11-13 14:28:28 +0000140 sqlite3PCacheSetDefault();
danielk19778c0a7912008-08-20 14:49:23 +0000141 }
dan22e21ff2011-11-08 20:08:44 +0000142 return sqlite3GlobalConfig.pcache2.xInit(sqlite3GlobalConfig.pcache2.pArg);
danielk19778c0a7912008-08-20 14:49:23 +0000143}
144void sqlite3PcacheShutdown(void){
dan22e21ff2011-11-08 20:08:44 +0000145 if( sqlite3GlobalConfig.pcache2.xShutdown ){
drhf759bb82010-09-09 18:25:34 +0000146 /* IMPLEMENTATION-OF: R-26000-56589 The xShutdown() method may be NULL. */
dan22e21ff2011-11-08 20:08:44 +0000147 sqlite3GlobalConfig.pcache2.xShutdown(sqlite3GlobalConfig.pcache2.pArg);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000148 }
danielk19778c0a7912008-08-20 14:49:23 +0000149}
150
151/*
152** Return the size in bytes of a PCache object.
153*/
154int sqlite3PcacheSize(void){ return sizeof(PCache); }
155
156/*
danielk1977bc2ca9e2008-11-13 14:28:28 +0000157** Create a new PCache object. Storage space to hold the object
158** has already been allocated and is passed in as the p pointer.
159** The caller discovers how much space needs to be allocated by
160** calling sqlite3PcacheSize().
danielk19778c0a7912008-08-20 14:49:23 +0000161*/
drhc3031c62014-08-26 15:06:49 +0000162int sqlite3PcacheOpen(
danielk19778c0a7912008-08-20 14:49:23 +0000163 int szPage, /* Size of every page */
164 int szExtra, /* Extra space associated with each page */
165 int bPurgeable, /* True if pages are on backing store */
danielk1977a858aa22008-08-22 16:22:17 +0000166 int (*xStress)(void*,PgHdr*),/* Call to try to make pages clean */
danielk19778c0a7912008-08-20 14:49:23 +0000167 void *pStress, /* Argument to xStress */
168 PCache *p /* Preallocated space for the PCache */
169){
danielk19778c0a7912008-08-20 14:49:23 +0000170 memset(p, 0, sizeof(PCache));
drhc3031c62014-08-26 15:06:49 +0000171 p->szPage = 1;
danielk19778c0a7912008-08-20 14:49:23 +0000172 p->szExtra = szExtra;
173 p->bPurgeable = bPurgeable;
drhfe21a792014-02-03 17:04:29 +0000174 p->eCreate = 2;
danielk19778c0a7912008-08-20 14:49:23 +0000175 p->xStress = xStress;
176 p->pStress = pStress;
drh3b42abb2011-11-09 14:23:04 +0000177 p->szCache = 100;
drh9b0cf342015-11-12 14:57:19 +0000178 p->szSpill = 1;
drhc3031c62014-08-26 15:06:49 +0000179 return sqlite3PcacheSetPageSize(p, szPage);
danielk19778c0a7912008-08-20 14:49:23 +0000180}
181
drh41d30272008-08-20 21:47:45 +0000182/*
danielk1977bc2ca9e2008-11-13 14:28:28 +0000183** Change the page size for PCache object. The caller must ensure that there
184** are no outstanding page references when this function is called.
drh41d30272008-08-20 21:47:45 +0000185*/
drhc3031c62014-08-26 15:06:49 +0000186int sqlite3PcacheSetPageSize(PCache *pCache, int szPage){
drh95a0b372015-09-03 20:43:55 +0000187 assert( pCache->nRefSum==0 && pCache->pDirty==0 );
drhc3031c62014-08-26 15:06:49 +0000188 if( pCache->szPage ){
189 sqlite3_pcache *pNew;
190 pNew = sqlite3GlobalConfig.pcache2.xCreate(
drh51dc84e2014-12-30 13:04:25 +0000191 szPage, pCache->szExtra + ROUND8(sizeof(PgHdr)),
192 pCache->bPurgeable
drhc3031c62014-08-26 15:06:49 +0000193 );
194 if( pNew==0 ) return SQLITE_NOMEM;
195 sqlite3GlobalConfig.pcache2.xCachesize(pNew, numberOfCachePages(pCache));
196 if( pCache->pCache ){
197 sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
198 }
199 pCache->pCache = pNew;
drhc3031c62014-08-26 15:06:49 +0000200 pCache->szPage = szPage;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000201 }
drhc3031c62014-08-26 15:06:49 +0000202 return SQLITE_OK;
drh3b42abb2011-11-09 14:23:04 +0000203}
204
205/*
danielk19778c0a7912008-08-20 14:49:23 +0000206** Try to obtain a page from the cache.
drhbc59ac02014-08-27 23:18:01 +0000207**
208** This routine returns a pointer to an sqlite3_pcache_page object if
209** such an object is already in cache, or if a new one is created.
210** This routine returns a NULL pointer if the object was not in cache
211** and could not be created.
212**
213** The createFlags should be 0 to check for existing pages and should
214** be 3 (not 1, but 3) to try to create a new page.
215**
216** If the createFlag is 0, then NULL is always returned if the page
217** is not already in the cache. If createFlag is 1, then a new page
218** is created only if that can be done without spilling dirty pages
219** and without exceeding the cache size limit.
220**
221** The caller needs to invoke sqlite3PcacheFetchFinish() to properly
222** initialize the sqlite3_pcache_page object and convert it into a
223** PgHdr object. The sqlite3PcacheFetch() and sqlite3PcacheFetchFinish()
224** routines are split this way for performance reasons. When separated
225** they can both (usually) operate without having to push values to
226** the stack on entry and pop them back off on exit, which saves a
227** lot of pushing and popping.
danielk19778c0a7912008-08-20 14:49:23 +0000228*/
drhbc59ac02014-08-27 23:18:01 +0000229sqlite3_pcache_page *sqlite3PcacheFetch(
danielk19778c0a7912008-08-20 14:49:23 +0000230 PCache *pCache, /* Obtain the page from this cache */
231 Pgno pgno, /* Page number to obtain */
drhbc59ac02014-08-27 23:18:01 +0000232 int createFlag /* If true, create page if it does not exist already */
danielk19778c0a7912008-08-20 14:49:23 +0000233){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000234 int eCreate;
danielk1977f599a192008-08-28 10:21:16 +0000235
danielk19778c0a7912008-08-20 14:49:23 +0000236 assert( pCache!=0 );
drhc3031c62014-08-26 15:06:49 +0000237 assert( pCache->pCache!=0 );
238 assert( createFlag==3 || createFlag==0 );
danielk19778c0a7912008-08-20 14:49:23 +0000239 assert( pgno>0 );
danielk19778c0a7912008-08-20 14:49:23 +0000240
drhfe21a792014-02-03 17:04:29 +0000241 /* eCreate defines what to do if the page does not exist.
242 ** 0 Do not allocate a new page. (createFlag==0)
243 ** 1 Allocate a new page if doing so is inexpensive.
244 ** (createFlag==1 AND bPurgeable AND pDirty)
245 ** 2 Allocate a new page even it doing so is difficult.
246 ** (createFlag==1 AND !(bPurgeable AND pDirty)
247 */
drhc3031c62014-08-26 15:06:49 +0000248 eCreate = createFlag & pCache->eCreate;
249 assert( eCreate==0 || eCreate==1 || eCreate==2 );
250 assert( createFlag==0 || pCache->eCreate==eCreate );
251 assert( createFlag==0 || eCreate==1+(!pCache->bPurgeable||!pCache->pDirty) );
drhbc59ac02014-08-27 23:18:01 +0000252 return sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, eCreate);
253}
danielk1977bc2ca9e2008-11-13 14:28:28 +0000254
drhbc59ac02014-08-27 23:18:01 +0000255/*
256** If the sqlite3PcacheFetch() routine is unable to allocate a new
257** page because new clean pages are available for reuse and the cache
258** size limit has been reached, then this routine can be invoked to
259** try harder to allocate a page. This routine might invoke the stress
260** callback to spill dirty pages to the journal. It will then try to
261** allocate the new page and will only fail to allocate a new page on
262** an OOM error.
263**
264** This routine should be invoked only after sqlite3PcacheFetch() fails.
265*/
266int sqlite3PcacheFetchStress(
267 PCache *pCache, /* Obtain the page from this cache */
268 Pgno pgno, /* Page number to obtain */
269 sqlite3_pcache_page **ppPage /* Write result here */
270){
271 PgHdr *pPg;
272 if( pCache->eCreate==2 ) return 0;
273
drh9b0cf342015-11-12 14:57:19 +0000274 if( sqlite3PcachePagecount(pCache)>pCache->szSpill ){
275 /* Find a dirty page to write-out and recycle. First try to find a
276 ** page that does not require a journal-sync (one with PGHDR_NEED_SYNC
277 ** cleared), but if that is not possible settle for any other
278 ** unreferenced dirty page.
279 */
280 for(pPg=pCache->pSynced;
281 pPg && (pPg->nRef || (pPg->flags&PGHDR_NEED_SYNC));
282 pPg=pPg->pDirtyPrev
283 );
284 pCache->pSynced = pPg;
285 if( !pPg ){
286 for(pPg=pCache->pDirtyTail; pPg && pPg->nRef; pPg=pPg->pDirtyPrev);
287 }
288 if( pPg ){
289 int rc;
drhc97125e2011-05-28 15:53:07 +0000290#ifdef SQLITE_LOG_CACHE_SPILL
drh9b0cf342015-11-12 14:57:19 +0000291 sqlite3_log(SQLITE_FULL,
292 "spill page %d making room for %d - cache used: %d/%d",
293 pPg->pgno, pgno,
294 sqlite3GlobalConfig.pcache.xPagecount(pCache->pCache),
drhbc59ac02014-08-27 23:18:01 +0000295 numberOfCachePages(pCache));
drhc97125e2011-05-28 15:53:07 +0000296#endif
drh9b0cf342015-11-12 14:57:19 +0000297 rc = pCache->xStress(pCache->pStress, pPg);
298 if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
299 return rc;
300 }
danielk1977f599a192008-08-28 10:21:16 +0000301 }
danielk19778c0a7912008-08-20 14:49:23 +0000302 }
drhbc59ac02014-08-27 23:18:01 +0000303 *ppPage = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, 2);
304 return *ppPage==0 ? SQLITE_NOMEM : SQLITE_OK;
305}
306
307/*
308** This is a helper routine for sqlite3PcacheFetchFinish()
309**
310** In the uncommon case where the page being fetched has not been
311** initialized, this routine is invoked to do the initialization.
312** This routine is broken out into a separate function since it
313** requires extra stack manipulation that can be avoided in the common
314** case.
315*/
316static SQLITE_NOINLINE PgHdr *pcacheFetchFinishWithInit(
317 PCache *pCache, /* Obtain the page from this cache */
318 Pgno pgno, /* Page number obtained */
319 sqlite3_pcache_page *pPage /* Page obtained by prior PcacheFetch() call */
320){
321 PgHdr *pPgHdr;
322 assert( pPage!=0 );
323 pPgHdr = (PgHdr*)pPage->pExtra;
324 assert( pPgHdr->pPage==0 );
drhd8c0ba32015-06-30 03:57:59 +0000325 memset(pPgHdr, 0, sizeof(PgHdr));
drhbc59ac02014-08-27 23:18:01 +0000326 pPgHdr->pPage = pPage;
327 pPgHdr->pData = pPage->pBuf;
328 pPgHdr->pExtra = (void *)&pPgHdr[1];
329 memset(pPgHdr->pExtra, 0, pCache->szExtra);
330 pPgHdr->pCache = pCache;
331 pPgHdr->pgno = pgno;
drhc78ae912015-06-29 04:21:15 +0000332 pPgHdr->flags = PGHDR_CLEAN;
drhbc59ac02014-08-27 23:18:01 +0000333 return sqlite3PcacheFetchFinish(pCache,pgno,pPage);
334}
335
336/*
337** This routine converts the sqlite3_pcache_page object returned by
338** sqlite3PcacheFetch() into an initialized PgHdr object. This routine
339** must be called after sqlite3PcacheFetch() in order to get a usable
340** result.
341*/
342PgHdr *sqlite3PcacheFetchFinish(
343 PCache *pCache, /* Obtain the page from this cache */
344 Pgno pgno, /* Page number obtained */
345 sqlite3_pcache_page *pPage /* Page obtained by prior PcacheFetch() call */
346){
347 PgHdr *pPgHdr;
348
drhd8c0ba32015-06-30 03:57:59 +0000349 assert( pPage!=0 );
drhbc59ac02014-08-27 23:18:01 +0000350 pPgHdr = (PgHdr *)pPage->pExtra;
351
352 if( !pPgHdr->pPage ){
353 return pcacheFetchFinishWithInit(pCache, pgno, pPage);
354 }
drh95a0b372015-09-03 20:43:55 +0000355 pCache->nRefSum++;
drhbc59ac02014-08-27 23:18:01 +0000356 pPgHdr->nRef++;
drhbc59ac02014-08-27 23:18:01 +0000357 return pPgHdr;
danielk19778c0a7912008-08-20 14:49:23 +0000358}
359
360/*
danielk1977bc2ca9e2008-11-13 14:28:28 +0000361** Decrement the reference count on a page. If the page is clean and the
peter.d.reid60ec9142014-09-06 16:39:46 +0000362** reference count drops to 0, then it is made eligible for recycling.
danielk19778c0a7912008-08-20 14:49:23 +0000363*/
drha8dcba92014-08-22 20:35:29 +0000364void SQLITE_NOINLINE sqlite3PcacheRelease(PgHdr *p){
danielk19778c0a7912008-08-20 14:49:23 +0000365 assert( p->nRef>0 );
drh95a0b372015-09-03 20:43:55 +0000366 p->pCache->nRefSum--;
367 if( (--p->nRef)==0 ){
drhc78ae912015-06-29 04:21:15 +0000368 if( p->flags&PGHDR_CLEAN ){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000369 pcacheUnpin(p);
drh36ce9192014-09-12 20:30:59 +0000370 }else if( p->pDirtyPrev!=0 ){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000371 /* Move the page to the head of the dirty list. */
drha8dcba92014-08-22 20:35:29 +0000372 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_FRONT);
danielk1977d491e1b2008-08-26 18:05:48 +0000373 }
danielk1977502b7432008-08-25 14:49:42 +0000374 }
danielk19778c0a7912008-08-20 14:49:23 +0000375}
376
danielk1977bc2ca9e2008-11-13 14:28:28 +0000377/*
378** Increase the reference count of a supplied page by 1.
379*/
danielk19778c0a7912008-08-20 14:49:23 +0000380void sqlite3PcacheRef(PgHdr *p){
danielk1977502b7432008-08-25 14:49:42 +0000381 assert(p->nRef>0);
382 p->nRef++;
drh95a0b372015-09-03 20:43:55 +0000383 p->pCache->nRefSum++;
danielk19778c0a7912008-08-20 14:49:23 +0000384}
385
386/*
danielk19774abdfa42008-08-27 09:44:39 +0000387** Drop a page from the cache. There must be exactly one reference to the
388** page. This function deletes that reference, so after it returns the
389** page pointed to by p is invalid.
danielk19778c0a7912008-08-20 14:49:23 +0000390*/
391void sqlite3PcacheDrop(PgHdr *p){
danielk19778c0a7912008-08-20 14:49:23 +0000392 assert( p->nRef==1 );
danielk1977bc2ca9e2008-11-13 14:28:28 +0000393 if( p->flags&PGHDR_DIRTY ){
drha8dcba92014-08-22 20:35:29 +0000394 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000395 }
drh95a0b372015-09-03 20:43:55 +0000396 p->pCache->nRefSum--;
drha8dcba92014-08-22 20:35:29 +0000397 sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 1);
danielk19778c0a7912008-08-20 14:49:23 +0000398}
399
400/*
danielk1977bc2ca9e2008-11-13 14:28:28 +0000401** Make sure the page is marked as dirty. If it isn't dirty already,
danielk19778c0a7912008-08-20 14:49:23 +0000402** make it so.
403*/
404void sqlite3PcacheMakeDirty(PgHdr *p){
danielk19778c0a7912008-08-20 14:49:23 +0000405 assert( p->nRef>0 );
drhc78ae912015-06-29 04:21:15 +0000406 if( p->flags & (PGHDR_CLEAN|PGHDR_DONT_WRITE) ){
407 p->flags &= ~PGHDR_DONT_WRITE;
408 if( p->flags & PGHDR_CLEAN ){
409 p->flags ^= (PGHDR_DIRTY|PGHDR_CLEAN);
410 assert( (p->flags & (PGHDR_DIRTY|PGHDR_CLEAN))==PGHDR_DIRTY );
411 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_ADD);
412 }
danielk1977d491e1b2008-08-26 18:05:48 +0000413 }
danielk1977f599a192008-08-28 10:21:16 +0000414}
415
416/*
danielk1977bc2ca9e2008-11-13 14:28:28 +0000417** Make sure the page is marked as clean. If it isn't clean already,
danielk1977f599a192008-08-28 10:21:16 +0000418** make it so.
419*/
420void sqlite3PcacheMakeClean(PgHdr *p){
421 if( (p->flags & PGHDR_DIRTY) ){
drhc78ae912015-06-29 04:21:15 +0000422 assert( (p->flags & PGHDR_CLEAN)==0 );
drha8dcba92014-08-22 20:35:29 +0000423 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE);
drh1aacbdb2015-06-29 18:29:10 +0000424 p->flags &= ~(PGHDR_DIRTY|PGHDR_NEED_SYNC|PGHDR_WRITEABLE);
drhc78ae912015-06-29 04:21:15 +0000425 p->flags |= PGHDR_CLEAN;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000426 if( p->nRef==0 ){
427 pcacheUnpin(p);
428 }
danielk1977f599a192008-08-28 10:21:16 +0000429 }
danielk19778c0a7912008-08-20 14:49:23 +0000430}
431
432/*
433** Make every page in the cache clean.
434*/
435void sqlite3PcacheCleanAll(PCache *pCache){
436 PgHdr *p;
danielk19778c0a7912008-08-20 14:49:23 +0000437 while( (p = pCache->pDirty)!=0 ){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000438 sqlite3PcacheMakeClean(p);
danielk19778c0a7912008-08-20 14:49:23 +0000439 }
440}
441
442/*
danielk1977bc2ca9e2008-11-13 14:28:28 +0000443** Clear the PGHDR_NEED_SYNC flag from all dirty pages.
444*/
445void sqlite3PcacheClearSyncFlags(PCache *pCache){
446 PgHdr *p;
447 for(p=pCache->pDirty; p; p=p->pDirtyNext){
448 p->flags &= ~PGHDR_NEED_SYNC;
449 }
450 pCache->pSynced = pCache->pDirtyTail;
451}
452
453/*
454** Change the page number of page p to newPgno.
danielk19778c0a7912008-08-20 14:49:23 +0000455*/
456void sqlite3PcacheMove(PgHdr *p, Pgno newPgno){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000457 PCache *pCache = p->pCache;
danielk1977d491e1b2008-08-26 18:05:48 +0000458 assert( p->nRef>0 );
danielk1977bc2ca9e2008-11-13 14:28:28 +0000459 assert( newPgno>0 );
dan22e21ff2011-11-08 20:08:44 +0000460 sqlite3GlobalConfig.pcache2.xRekey(pCache->pCache, p->pPage, p->pgno,newPgno);
danielk19778c0a7912008-08-20 14:49:23 +0000461 p->pgno = newPgno;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000462 if( (p->flags&PGHDR_DIRTY) && (p->flags&PGHDR_NEED_SYNC) ){
drha8dcba92014-08-22 20:35:29 +0000463 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_FRONT);
danielk19778c0a7912008-08-20 14:49:23 +0000464 }
danielk19778c0a7912008-08-20 14:49:23 +0000465}
466
467/*
danielk1977bc2ca9e2008-11-13 14:28:28 +0000468** Drop every cache entry whose page number is greater than "pgno". The
469** caller must ensure that there are no outstanding references to any pages
470** other than page 1 with a page number greater than pgno.
471**
472** If there is a reference to page 1 and the pgno parameter passed to this
473** function is 0, then the data area associated with page 1 is zeroed, but
474** the page object is not dropped.
danielk19778c0a7912008-08-20 14:49:23 +0000475*/
476void sqlite3PcacheTruncate(PCache *pCache, Pgno pgno){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000477 if( pCache->pCache ){
478 PgHdr *p;
479 PgHdr *pNext;
480 for(p=pCache->pDirty; p; p=pNext){
481 pNext = p->pDirtyNext;
drhf3609ee2010-03-19 19:23:51 +0000482 /* This routine never gets call with a positive pgno except right
483 ** after sqlite3PcacheCleanAll(). So if there are dirty pages,
484 ** it must be that pgno==0.
485 */
486 assert( p->pgno>0 );
487 if( ALWAYS(p->pgno>pgno) ){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000488 assert( p->flags&PGHDR_DIRTY );
489 sqlite3PcacheMakeClean(p);
danielk19778c0a7912008-08-20 14:49:23 +0000490 }
491 }
drh95a0b372015-09-03 20:43:55 +0000492 if( pgno==0 && pCache->nRefSum ){
drh39065c62015-06-26 02:41:31 +0000493 sqlite3_pcache_page *pPage1;
494 pPage1 = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache,1,0);
495 if( ALWAYS(pPage1) ){ /* Page 1 is always available in cache, because
drh95a0b372015-09-03 20:43:55 +0000496 ** pCache->nRefSum>0 */
drh39065c62015-06-26 02:41:31 +0000497 memset(pPage1->pBuf, 0, pCache->szPage);
498 pgno = 1;
499 }
danielk1977bc2ca9e2008-11-13 14:28:28 +0000500 }
dan22e21ff2011-11-08 20:08:44 +0000501 sqlite3GlobalConfig.pcache2.xTruncate(pCache->pCache, pgno+1);
danielk1977062d4cb2008-08-29 09:10:02 +0000502 }
503}
danielk19778c0a7912008-08-20 14:49:23 +0000504
505/*
506** Close a cache.
507*/
508void sqlite3PcacheClose(PCache *pCache){
drhc3031c62014-08-26 15:06:49 +0000509 assert( pCache->pCache!=0 );
510 sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
danielk19778c0a7912008-08-20 14:49:23 +0000511}
512
513/*
514** Discard the contents of the cache.
515*/
danielk1977bea2a942009-01-20 17:06:27 +0000516void sqlite3PcacheClear(PCache *pCache){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000517 sqlite3PcacheTruncate(pCache, 0);
danielk19778c0a7912008-08-20 14:49:23 +0000518}
519
520/*
521** Merge two lists of pages connected by pDirty and in pgno order.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000522** Do not both fixing the pDirtyPrev pointers.
danielk19778c0a7912008-08-20 14:49:23 +0000523*/
524static PgHdr *pcacheMergeDirtyList(PgHdr *pA, PgHdr *pB){
525 PgHdr result, *pTail;
526 pTail = &result;
527 while( pA && pB ){
528 if( pA->pgno<pB->pgno ){
529 pTail->pDirty = pA;
530 pTail = pA;
531 pA = pA->pDirty;
532 }else{
533 pTail->pDirty = pB;
534 pTail = pB;
535 pB = pB->pDirty;
536 }
537 }
538 if( pA ){
539 pTail->pDirty = pA;
540 }else if( pB ){
541 pTail->pDirty = pB;
542 }else{
543 pTail->pDirty = 0;
544 }
545 return result.pDirty;
546}
547
548/*
549** Sort the list of pages in accending order by pgno. Pages are
danielk1977bc2ca9e2008-11-13 14:28:28 +0000550** connected by pDirty pointers. The pDirtyPrev pointers are
danielk19778c0a7912008-08-20 14:49:23 +0000551** corrupted by this sort.
drhe64ca7b2009-07-16 18:21:17 +0000552**
553** Since there cannot be more than 2^31 distinct pages in a database,
554** there cannot be more than 31 buckets required by the merge sorter.
555** One extra bucket is added to catch overflow in case something
556** ever changes to make the previous sentence incorrect.
danielk19778c0a7912008-08-20 14:49:23 +0000557*/
drhe64ca7b2009-07-16 18:21:17 +0000558#define N_SORT_BUCKET 32
danielk19778c0a7912008-08-20 14:49:23 +0000559static PgHdr *pcacheSortDirtyList(PgHdr *pIn){
drhe64ca7b2009-07-16 18:21:17 +0000560 PgHdr *a[N_SORT_BUCKET], *p;
danielk19778c0a7912008-08-20 14:49:23 +0000561 int i;
562 memset(a, 0, sizeof(a));
563 while( pIn ){
564 p = pIn;
565 pIn = p->pDirty;
566 p->pDirty = 0;
drhe64ca7b2009-07-16 18:21:17 +0000567 for(i=0; ALWAYS(i<N_SORT_BUCKET-1); i++){
danielk19778c0a7912008-08-20 14:49:23 +0000568 if( a[i]==0 ){
569 a[i] = p;
570 break;
571 }else{
572 p = pcacheMergeDirtyList(a[i], p);
573 a[i] = 0;
574 }
575 }
drhe64ca7b2009-07-16 18:21:17 +0000576 if( NEVER(i==N_SORT_BUCKET-1) ){
577 /* To get here, there need to be 2^(N_SORT_BUCKET) elements in
578 ** the input list. But that is impossible.
danielk19778c0a7912008-08-20 14:49:23 +0000579 */
580 a[i] = pcacheMergeDirtyList(a[i], p);
581 }
582 }
583 p = a[0];
584 for(i=1; i<N_SORT_BUCKET; i++){
585 p = pcacheMergeDirtyList(p, a[i]);
586 }
587 return p;
588}
589
590/*
591** Return a list of all dirty pages in the cache, sorted by page number.
592*/
593PgHdr *sqlite3PcacheDirtyList(PCache *pCache){
594 PgHdr *p;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000595 for(p=pCache->pDirty; p; p=p->pDirtyNext){
596 p->pDirty = p->pDirtyNext;
danielk19778c0a7912008-08-20 14:49:23 +0000597 }
598 return pcacheSortDirtyList(pCache->pDirty);
599}
600
danielk19778c0a7912008-08-20 14:49:23 +0000601/*
drh95a0b372015-09-03 20:43:55 +0000602** Return the total number of references to all pages held by the cache.
603**
604** This is not the total number of pages referenced, but the sum of the
605** reference count for all pages.
danielk19778c0a7912008-08-20 14:49:23 +0000606*/
607int sqlite3PcacheRefCount(PCache *pCache){
drh95a0b372015-09-03 20:43:55 +0000608 return pCache->nRefSum;
danielk19778c0a7912008-08-20 14:49:23 +0000609}
610
danielk1977bc2ca9e2008-11-13 14:28:28 +0000611/*
612** Return the number of references to the page supplied as an argument.
613*/
danielk197771d5d2c2008-09-29 11:49:47 +0000614int sqlite3PcachePageRefcount(PgHdr *p){
615 return p->nRef;
616}
617
danielk19778c0a7912008-08-20 14:49:23 +0000618/*
619** Return the total number of pages in the cache.
620*/
621int sqlite3PcachePagecount(PCache *pCache){
drhc3031c62014-08-26 15:06:49 +0000622 assert( pCache->pCache!=0 );
623 return sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache);
danielk19778c0a7912008-08-20 14:49:23 +0000624}
625
danielk1977f3d3c272008-11-19 16:52:44 +0000626#ifdef SQLITE_TEST
danielk19778c0a7912008-08-20 14:49:23 +0000627/*
danielk1977bc2ca9e2008-11-13 14:28:28 +0000628** Get the suggested cache-size value.
danielk19778c0a7912008-08-20 14:49:23 +0000629*/
630int sqlite3PcacheGetCachesize(PCache *pCache){
drh3b42abb2011-11-09 14:23:04 +0000631 return numberOfCachePages(pCache);
danielk19778c0a7912008-08-20 14:49:23 +0000632}
danielk1977f3d3c272008-11-19 16:52:44 +0000633#endif
danielk19778c0a7912008-08-20 14:49:23 +0000634
635/*
636** Set the suggested cache-size value.
637*/
638void sqlite3PcacheSetCachesize(PCache *pCache, int mxPage){
drhc3031c62014-08-26 15:06:49 +0000639 assert( pCache->pCache!=0 );
drh3b42abb2011-11-09 14:23:04 +0000640 pCache->szCache = mxPage;
drhc3031c62014-08-26 15:06:49 +0000641 sqlite3GlobalConfig.pcache2.xCachesize(pCache->pCache,
642 numberOfCachePages(pCache));
danielk19778c0a7912008-08-20 14:49:23 +0000643}
644
drh09419b42011-11-16 19:29:17 +0000645/*
drh9b0cf342015-11-12 14:57:19 +0000646** Set the suggested cache-spill value. Make no changes if if the
647** argument is zero. Return the effective cache-spill size, which will
648** be the larger of the szSpill and szCache.
649*/
650int sqlite3PcacheSetSpillsize(PCache *p, int mxPage){
651 int res;
652 assert( p->pCache!=0 );
653 if( mxPage ){
654 if( mxPage<0 ){
drh4f9c8ec2015-11-12 15:47:48 +0000655 mxPage = (int)((-1024*(i64)mxPage)/(p->szPage+p->szExtra));
drh9b0cf342015-11-12 14:57:19 +0000656 }
657 p->szSpill = mxPage;
658 }
659 res = numberOfCachePages(p);
660 if( res<p->szSpill ) res = p->szSpill;
661 return res;
662}
663
664/*
drh09419b42011-11-16 19:29:17 +0000665** Free up as much memory as possible from the page cache.
666*/
667void sqlite3PcacheShrink(PCache *pCache){
drhc3031c62014-08-26 15:06:49 +0000668 assert( pCache->pCache!=0 );
669 sqlite3GlobalConfig.pcache2.xShrink(pCache->pCache);
drh09419b42011-11-16 19:29:17 +0000670}
671
drhdef68892014-11-04 12:11:23 +0000672/*
673** Return the size of the header added by this middleware layer
674** in the page-cache hierarchy.
675*/
drh37c057b2014-12-30 00:57:29 +0000676int sqlite3HeaderSizePcache(void){ return ROUND8(sizeof(PgHdr)); }
drhdef68892014-11-04 12:11:23 +0000677
678
danielk1977750e87d2009-07-25 11:46:48 +0000679#if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)
danielk197767e3da72008-08-21 12:19:44 +0000680/*
danielk1977bc2ca9e2008-11-13 14:28:28 +0000681** For all dirty pages currently in the cache, invoke the specified
682** callback. This is only used if the SQLITE_CHECK_PAGES macro is
683** defined.
danielk197767e3da72008-08-21 12:19:44 +0000684*/
danielk1977bc2ca9e2008-11-13 14:28:28 +0000685void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *)){
686 PgHdr *pDirty;
687 for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext){
688 xIter(pDirty);
danielk197767e3da72008-08-21 12:19:44 +0000689 }
danielk1977062d4cb2008-08-29 09:10:02 +0000690}
691#endif