blob: e18bf93be07e58a6b5a67721d31406d7e89cbea9 [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 */
danielk1977bc2ca9e2008-11-13 14:28:28 +000022 int nRef; /* Number of referenced pages */
drh3b42abb2011-11-09 14:23:04 +000023 int szCache; /* Configured cache size */
drha85f7e32008-08-28 02:26:07 +000024 int szPage; /* Size of every page in this cache */
25 int szExtra; /* Size of extra space for each page */
drhfe21a792014-02-03 17:04:29 +000026 u8 bPurgeable; /* True if pages are on backing store */
27 u8 eCreate; /* eCreate value for for xFetch() */
drha85f7e32008-08-28 02:26:07 +000028 int (*xStress)(void*,PgHdr*); /* Call to try make a page clean */
29 void *pStress; /* Argument to xStress */
danielk1977bc2ca9e2008-11-13 14:28:28 +000030 sqlite3_pcache *pCache; /* Pluggable cache module */
danielk1977bea2a942009-01-20 17:06:27 +000031 PgHdr *pPage1; /* Reference to page 1 */
danielk19778c0a7912008-08-20 14:49:23 +000032};
33
34/*
drha85f7e32008-08-28 02:26:07 +000035** Some of the assert() macros in this code are too expensive to run
36** even during normal debugging. Use them only rarely on long-running
37** tests. Enable the expensive asserts using the
38** -DSQLITE_ENABLE_EXPENSIVE_ASSERT=1 compile-time option.
39*/
40#ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
41# define expensive_assert(X) assert(X)
42#else
43# define expensive_assert(X)
44#endif
danielk19778c0a7912008-08-20 14:49:23 +000045
danielk19778c0a7912008-08-20 14:49:23 +000046/********************************** Linked List Management ********************/
47
drha85f7e32008-08-28 02:26:07 +000048#if !defined(NDEBUG) && defined(SQLITE_ENABLE_EXPENSIVE_ASSERT)
drh41d30272008-08-20 21:47:45 +000049/*
danielk1977d491e1b2008-08-26 18:05:48 +000050** Check that the pCache->pSynced variable is set correctly. If it
51** is not, either fail an assert or return zero. Otherwise, return
52** non-zero. This is only used in debugging builds, as follows:
53**
drha85f7e32008-08-28 02:26:07 +000054** expensive_assert( pcacheCheckSynced(pCache) );
danielk1977d491e1b2008-08-26 18:05:48 +000055*/
56static int pcacheCheckSynced(PCache *pCache){
danielk1977bc2ca9e2008-11-13 14:28:28 +000057 PgHdr *p;
58 for(p=pCache->pDirtyTail; p!=pCache->pSynced; p=p->pDirtyPrev){
danielk1977d491e1b2008-08-26 18:05:48 +000059 assert( p->nRef || (p->flags&PGHDR_NEED_SYNC) );
60 }
61 return (p==0 || p->nRef || (p->flags&PGHDR_NEED_SYNC)==0);
62}
drha85f7e32008-08-28 02:26:07 +000063#endif /* !NDEBUG && SQLITE_ENABLE_EXPENSIVE_ASSERT */
danielk1977d491e1b2008-08-26 18:05:48 +000064
danielk19778c0a7912008-08-20 14:49:23 +000065/*
danielk1977bc2ca9e2008-11-13 14:28:28 +000066** Remove page pPage from the list of dirty pages.
danielk19778c0a7912008-08-20 14:49:23 +000067*/
danielk1977bc2ca9e2008-11-13 14:28:28 +000068static void pcacheRemoveFromDirtyList(PgHdr *pPage){
69 PCache *p = pPage->pCache;
danielk19778c0a7912008-08-20 14:49:23 +000070
danielk1977bc2ca9e2008-11-13 14:28:28 +000071 assert( pPage->pDirtyNext || pPage==p->pDirtyTail );
72 assert( pPage->pDirtyPrev || pPage==p->pDirty );
danielk19778c0a7912008-08-20 14:49:23 +000073
danielk1977bc2ca9e2008-11-13 14:28:28 +000074 /* Update the PCache1.pSynced variable if necessary. */
75 if( p->pSynced==pPage ){
76 PgHdr *pSynced = pPage->pDirtyPrev;
77 while( pSynced && (pSynced->flags&PGHDR_NEED_SYNC) ){
78 pSynced = pSynced->pDirtyPrev;
danielk1977d491e1b2008-08-26 18:05:48 +000079 }
danielk1977bc2ca9e2008-11-13 14:28:28 +000080 p->pSynced = pSynced;
danielk1977d491e1b2008-08-26 18:05:48 +000081 }
danielk19778c0a7912008-08-20 14:49:23 +000082
danielk1977bc2ca9e2008-11-13 14:28:28 +000083 if( pPage->pDirtyNext ){
84 pPage->pDirtyNext->pDirtyPrev = pPage->pDirtyPrev;
danielk19778c0a7912008-08-20 14:49:23 +000085 }else{
danielk1977bc2ca9e2008-11-13 14:28:28 +000086 assert( pPage==p->pDirtyTail );
87 p->pDirtyTail = pPage->pDirtyPrev;
danielk19778c0a7912008-08-20 14:49:23 +000088 }
danielk1977bc2ca9e2008-11-13 14:28:28 +000089 if( pPage->pDirtyPrev ){
90 pPage->pDirtyPrev->pDirtyNext = pPage->pDirtyNext;
danielk19778c0a7912008-08-20 14:49:23 +000091 }else{
danielk1977bc2ca9e2008-11-13 14:28:28 +000092 assert( pPage==p->pDirty );
93 p->pDirty = pPage->pDirtyNext;
drhfe21a792014-02-03 17:04:29 +000094 if( p->pDirty==0 && p->bPurgeable ){
95 assert( p->eCreate==1 );
96 p->eCreate = 2;
97 }
danielk19778c0a7912008-08-20 14:49:23 +000098 }
danielk1977bc2ca9e2008-11-13 14:28:28 +000099 pPage->pDirtyNext = 0;
100 pPage->pDirtyPrev = 0;
101
102 expensive_assert( pcacheCheckSynced(p) );
danielk19778c0a7912008-08-20 14:49:23 +0000103}
104
105/*
danielk1977bc2ca9e2008-11-13 14:28:28 +0000106** Add page pPage to the head of the dirty list (PCache1.pDirty is set to
107** pPage).
danielk19778c0a7912008-08-20 14:49:23 +0000108*/
danielk1977bc2ca9e2008-11-13 14:28:28 +0000109static void pcacheAddToDirtyList(PgHdr *pPage){
110 PCache *p = pPage->pCache;
danielk19778c0a7912008-08-20 14:49:23 +0000111
danielk1977bc2ca9e2008-11-13 14:28:28 +0000112 assert( pPage->pDirtyNext==0 && pPage->pDirtyPrev==0 && p->pDirty!=pPage );
113
114 pPage->pDirtyNext = p->pDirty;
115 if( pPage->pDirtyNext ){
116 assert( pPage->pDirtyNext->pDirtyPrev==0 );
117 pPage->pDirtyNext->pDirtyPrev = pPage;
drhfe21a792014-02-03 17:04:29 +0000118 }else if( p->bPurgeable ){
119 assert( p->eCreate==2 );
120 p->eCreate = 1;
danielk19778c0a7912008-08-20 14:49:23 +0000121 }
danielk1977bc2ca9e2008-11-13 14:28:28 +0000122 p->pDirty = pPage;
123 if( !p->pDirtyTail ){
124 p->pDirtyTail = pPage;
125 }
126 if( !p->pSynced && 0==(pPage->flags&PGHDR_NEED_SYNC) ){
127 p->pSynced = pPage;
128 }
129 expensive_assert( pcacheCheckSynced(p) );
danielk19778c0a7912008-08-20 14:49:23 +0000130}
131
132/*
danielk1977bc2ca9e2008-11-13 14:28:28 +0000133** Wrapper around the pluggable caches xUnpin method. If the cache is
134** being used for an in-memory database, this function is a no-op.
danielk19778c0a7912008-08-20 14:49:23 +0000135*/
danielk1977bc2ca9e2008-11-13 14:28:28 +0000136static void pcacheUnpin(PgHdr *p){
137 PCache *pCache = p->pCache;
danielk1977801880f2008-08-21 15:54:01 +0000138 if( pCache->bPurgeable ){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000139 if( p->pgno==1 ){
140 pCache->pPage1 = 0;
danielk1977d491e1b2008-08-26 18:05:48 +0000141 }
dan22e21ff2011-11-08 20:08:44 +0000142 sqlite3GlobalConfig.pcache2.xUnpin(pCache->pCache, p->pPage, 0);
danielk19778c0a7912008-08-20 14:49:23 +0000143 }
danielk19778c0a7912008-08-20 14:49:23 +0000144}
145
146/*************************************************** General Interfaces ******
147**
148** Initialize and shutdown the page cache subsystem. Neither of these
149** functions are threadsafe.
150*/
151int sqlite3PcacheInitialize(void){
dan22e21ff2011-11-08 20:08:44 +0000152 if( sqlite3GlobalConfig.pcache2.xInit==0 ){
drhf759bb82010-09-09 18:25:34 +0000153 /* IMPLEMENTATION-OF: R-26801-64137 If the xInit() method is NULL, then the
154 ** built-in default page cache is used instead of the application defined
155 ** page cache. */
danielk1977bc2ca9e2008-11-13 14:28:28 +0000156 sqlite3PCacheSetDefault();
danielk19778c0a7912008-08-20 14:49:23 +0000157 }
dan22e21ff2011-11-08 20:08:44 +0000158 return sqlite3GlobalConfig.pcache2.xInit(sqlite3GlobalConfig.pcache2.pArg);
danielk19778c0a7912008-08-20 14:49:23 +0000159}
160void sqlite3PcacheShutdown(void){
dan22e21ff2011-11-08 20:08:44 +0000161 if( sqlite3GlobalConfig.pcache2.xShutdown ){
drhf759bb82010-09-09 18:25:34 +0000162 /* IMPLEMENTATION-OF: R-26000-56589 The xShutdown() method may be NULL. */
dan22e21ff2011-11-08 20:08:44 +0000163 sqlite3GlobalConfig.pcache2.xShutdown(sqlite3GlobalConfig.pcache2.pArg);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000164 }
danielk19778c0a7912008-08-20 14:49:23 +0000165}
166
167/*
168** Return the size in bytes of a PCache object.
169*/
170int sqlite3PcacheSize(void){ return sizeof(PCache); }
171
172/*
danielk1977bc2ca9e2008-11-13 14:28:28 +0000173** Create a new PCache object. Storage space to hold the object
174** has already been allocated and is passed in as the p pointer.
175** The caller discovers how much space needs to be allocated by
176** calling sqlite3PcacheSize().
danielk19778c0a7912008-08-20 14:49:23 +0000177*/
178void sqlite3PcacheOpen(
179 int szPage, /* Size of every page */
180 int szExtra, /* Extra space associated with each page */
181 int bPurgeable, /* True if pages are on backing store */
danielk1977a858aa22008-08-22 16:22:17 +0000182 int (*xStress)(void*,PgHdr*),/* Call to try to make pages clean */
danielk19778c0a7912008-08-20 14:49:23 +0000183 void *pStress, /* Argument to xStress */
184 PCache *p /* Preallocated space for the PCache */
185){
danielk19778c0a7912008-08-20 14:49:23 +0000186 memset(p, 0, sizeof(PCache));
187 p->szPage = szPage;
188 p->szExtra = szExtra;
189 p->bPurgeable = bPurgeable;
drhfe21a792014-02-03 17:04:29 +0000190 p->eCreate = 2;
danielk19778c0a7912008-08-20 14:49:23 +0000191 p->xStress = xStress;
192 p->pStress = pStress;
drh3b42abb2011-11-09 14:23:04 +0000193 p->szCache = 100;
danielk19778c0a7912008-08-20 14:49:23 +0000194}
195
drh41d30272008-08-20 21:47:45 +0000196/*
danielk1977bc2ca9e2008-11-13 14:28:28 +0000197** Change the page size for PCache object. The caller must ensure that there
198** are no outstanding page references when this function is called.
drh41d30272008-08-20 21:47:45 +0000199*/
danielk19778c0a7912008-08-20 14:49:23 +0000200void sqlite3PcacheSetPageSize(PCache *pCache, int szPage){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000201 assert( pCache->nRef==0 && pCache->pDirty==0 );
202 if( pCache->pCache ){
dan22e21ff2011-11-08 20:08:44 +0000203 sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000204 pCache->pCache = 0;
drh2ec050c2010-03-02 23:34:54 +0000205 pCache->pPage1 = 0;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000206 }
danielk19778c0a7912008-08-20 14:49:23 +0000207 pCache->szPage = szPage;
208}
209
210/*
drh3b42abb2011-11-09 14:23:04 +0000211** Compute the number of pages of cache requested.
212*/
213static int numberOfCachePages(PCache *p){
214 if( p->szCache>=0 ){
215 return p->szCache;
216 }else{
drh5dc2bcd2012-01-02 15:45:12 +0000217 return (int)((-1024*(i64)p->szCache)/(p->szPage+p->szExtra));
drh3b42abb2011-11-09 14:23:04 +0000218 }
219}
220
221/*
danielk19778c0a7912008-08-20 14:49:23 +0000222** Try to obtain a page from the cache.
223*/
224int sqlite3PcacheFetch(
225 PCache *pCache, /* Obtain the page from this cache */
226 Pgno pgno, /* Page number to obtain */
227 int createFlag, /* If true, create page if it does not exist already */
228 PgHdr **ppPage /* Write the page here */
229){
drhfe21a792014-02-03 17:04:29 +0000230 sqlite3_pcache_page *pPage;
dan22e21ff2011-11-08 20:08:44 +0000231 PgHdr *pPgHdr = 0;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000232 int eCreate;
danielk1977f599a192008-08-28 10:21:16 +0000233
danielk19778c0a7912008-08-20 14:49:23 +0000234 assert( pCache!=0 );
danielk197789bc4bc2009-07-21 19:25:24 +0000235 assert( createFlag==1 || createFlag==0 );
danielk19778c0a7912008-08-20 14:49:23 +0000236 assert( pgno>0 );
danielk19778c0a7912008-08-20 14:49:23 +0000237
danielk1977bc2ca9e2008-11-13 14:28:28 +0000238 /* If the pluggable cache (sqlite3_pcache*) has not been allocated,
239 ** allocate it now.
240 */
drhfe21a792014-02-03 17:04:29 +0000241 if( !pCache->pCache ){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000242 sqlite3_pcache *p;
drhfe21a792014-02-03 17:04:29 +0000243 if( !createFlag ){
244 *ppPage = 0;
245 return SQLITE_OK;
246 }
dan22e21ff2011-11-08 20:08:44 +0000247 p = sqlite3GlobalConfig.pcache2.xCreate(
drhe5c40b12011-11-09 00:06:05 +0000248 pCache->szPage, pCache->szExtra + sizeof(PgHdr), pCache->bPurgeable
dan22e21ff2011-11-08 20:08:44 +0000249 );
danielk1977bc2ca9e2008-11-13 14:28:28 +0000250 if( !p ){
251 return SQLITE_NOMEM;
252 }
drh3b42abb2011-11-09 14:23:04 +0000253 sqlite3GlobalConfig.pcache2.xCachesize(p, numberOfCachePages(pCache));
danielk1977bc2ca9e2008-11-13 14:28:28 +0000254 pCache->pCache = p;
255 }
danielk1977f599a192008-08-28 10:21:16 +0000256
drhfe21a792014-02-03 17:04:29 +0000257 /* eCreate defines what to do if the page does not exist.
258 ** 0 Do not allocate a new page. (createFlag==0)
259 ** 1 Allocate a new page if doing so is inexpensive.
260 ** (createFlag==1 AND bPurgeable AND pDirty)
261 ** 2 Allocate a new page even it doing so is difficult.
262 ** (createFlag==1 AND !(bPurgeable AND pDirty)
263 */
264 eCreate = createFlag==0 ? 0 : pCache->eCreate;
265 assert( (createFlag*(1+(!pCache->bPurgeable||!pCache->pDirty)))==eCreate );
266 pPage = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, eCreate);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000267 if( !pPage && eCreate==1 ){
268 PgHdr *pPg;
269
270 /* Find a dirty page to write-out and recycle. First try to find a
271 ** page that does not require a journal-sync (one with PGHDR_NEED_SYNC
272 ** cleared), but if that is not possible settle for any other
273 ** unreferenced dirty page.
274 */
275 expensive_assert( pcacheCheckSynced(pCache) );
276 for(pPg=pCache->pSynced;
277 pPg && (pPg->nRef || (pPg->flags&PGHDR_NEED_SYNC));
278 pPg=pPg->pDirtyPrev
279 );
drha9638962010-02-04 17:38:31 +0000280 pCache->pSynced = pPg;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000281 if( !pPg ){
282 for(pPg=pCache->pDirtyTail; pPg && pPg->nRef; pPg=pPg->pDirtyPrev);
283 }
284 if( pPg ){
285 int rc;
drhc97125e2011-05-28 15:53:07 +0000286#ifdef SQLITE_LOG_CACHE_SPILL
287 sqlite3_log(SQLITE_FULL,
288 "spill page %d making room for %d - cache used: %d/%d",
289 pPg->pgno, pgno,
290 sqlite3GlobalConfig.pcache.xPagecount(pCache->pCache),
drh3b42abb2011-11-09 14:23:04 +0000291 numberOfCachePages(pCache));
drhc97125e2011-05-28 15:53:07 +0000292#endif
danielk1977bc2ca9e2008-11-13 14:28:28 +0000293 rc = pCache->xStress(pCache->pStress, pPg);
294 if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
295 return rc;
danielk19778c0a7912008-08-20 14:49:23 +0000296 }
297 }
danielk1977bc2ca9e2008-11-13 14:28:28 +0000298
dan22e21ff2011-11-08 20:08:44 +0000299 pPage = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, 2);
danielk19778c0a7912008-08-20 14:49:23 +0000300 }
301
danielk1977bc2ca9e2008-11-13 14:28:28 +0000302 if( pPage ){
dan22e21ff2011-11-08 20:08:44 +0000303 pPgHdr = (PgHdr *)pPage->pExtra;
danielk1977e1fd5082009-01-23 16:45:00 +0000304
dan22e21ff2011-11-08 20:08:44 +0000305 if( !pPgHdr->pPage ){
306 memset(pPgHdr, 0, sizeof(PgHdr));
307 pPgHdr->pPage = pPage;
308 pPgHdr->pData = pPage->pBuf;
309 pPgHdr->pExtra = (void *)&pPgHdr[1];
310 memset(pPgHdr->pExtra, 0, pCache->szExtra);
311 pPgHdr->pCache = pCache;
312 pPgHdr->pgno = pgno;
313 }
314 assert( pPgHdr->pCache==pCache );
315 assert( pPgHdr->pgno==pgno );
316 assert( pPgHdr->pData==pPage->pBuf );
317 assert( pPgHdr->pExtra==(void *)&pPgHdr[1] );
318
319 if( 0==pPgHdr->nRef ){
danielk1977f599a192008-08-28 10:21:16 +0000320 pCache->nRef++;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000321 }
dan22e21ff2011-11-08 20:08:44 +0000322 pPgHdr->nRef++;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000323 if( pgno==1 ){
dan22e21ff2011-11-08 20:08:44 +0000324 pCache->pPage1 = pPgHdr;
danielk1977f599a192008-08-28 10:21:16 +0000325 }
danielk19778c0a7912008-08-20 14:49:23 +0000326 }
dan22e21ff2011-11-08 20:08:44 +0000327 *ppPage = pPgHdr;
328 return (pPgHdr==0 && eCreate) ? SQLITE_NOMEM : SQLITE_OK;
danielk19778c0a7912008-08-20 14:49:23 +0000329}
330
331/*
danielk1977bc2ca9e2008-11-13 14:28:28 +0000332** Decrement the reference count on a page. If the page is clean and the
333** reference count drops to 0, then it is made elible for recycling.
danielk19778c0a7912008-08-20 14:49:23 +0000334*/
335void sqlite3PcacheRelease(PgHdr *p){
336 assert( p->nRef>0 );
danielk1977502b7432008-08-25 14:49:42 +0000337 p->nRef--;
338 if( p->nRef==0 ){
339 PCache *pCache = p->pCache;
danielk1977d491e1b2008-08-26 18:05:48 +0000340 pCache->nRef--;
341 if( (p->flags&PGHDR_DIRTY)==0 ){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000342 pcacheUnpin(p);
danielk1977d491e1b2008-08-26 18:05:48 +0000343 }else{
danielk1977bc2ca9e2008-11-13 14:28:28 +0000344 /* Move the page to the head of the dirty list. */
345 pcacheRemoveFromDirtyList(p);
346 pcacheAddToDirtyList(p);
danielk1977d491e1b2008-08-26 18:05:48 +0000347 }
danielk1977502b7432008-08-25 14:49:42 +0000348 }
danielk19778c0a7912008-08-20 14:49:23 +0000349}
350
danielk1977bc2ca9e2008-11-13 14:28:28 +0000351/*
352** Increase the reference count of a supplied page by 1.
353*/
danielk19778c0a7912008-08-20 14:49:23 +0000354void sqlite3PcacheRef(PgHdr *p){
danielk1977502b7432008-08-25 14:49:42 +0000355 assert(p->nRef>0);
356 p->nRef++;
danielk19778c0a7912008-08-20 14:49:23 +0000357}
358
359/*
danielk19774abdfa42008-08-27 09:44:39 +0000360** Drop a page from the cache. There must be exactly one reference to the
361** page. This function deletes that reference, so after it returns the
362** page pointed to by p is invalid.
danielk19778c0a7912008-08-20 14:49:23 +0000363*/
364void sqlite3PcacheDrop(PgHdr *p){
365 PCache *pCache;
danielk19778c0a7912008-08-20 14:49:23 +0000366 assert( p->nRef==1 );
danielk1977bc2ca9e2008-11-13 14:28:28 +0000367 if( p->flags&PGHDR_DIRTY ){
368 pcacheRemoveFromDirtyList(p);
369 }
danielk19778c0a7912008-08-20 14:49:23 +0000370 pCache = p->pCache;
371 pCache->nRef--;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000372 if( p->pgno==1 ){
373 pCache->pPage1 = 0;
374 }
dan22e21ff2011-11-08 20:08:44 +0000375 sqlite3GlobalConfig.pcache2.xUnpin(pCache->pCache, p->pPage, 1);
danielk19778c0a7912008-08-20 14:49:23 +0000376}
377
378/*
danielk1977bc2ca9e2008-11-13 14:28:28 +0000379** Make sure the page is marked as dirty. If it isn't dirty already,
danielk19778c0a7912008-08-20 14:49:23 +0000380** make it so.
381*/
382void sqlite3PcacheMakeDirty(PgHdr *p){
danielk197733e32162008-08-23 18:53:08 +0000383 p->flags &= ~PGHDR_DONT_WRITE;
danielk19778c0a7912008-08-20 14:49:23 +0000384 assert( p->nRef>0 );
danielk1977bc2ca9e2008-11-13 14:28:28 +0000385 if( 0==(p->flags & PGHDR_DIRTY) ){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000386 p->flags |= PGHDR_DIRTY;
387 pcacheAddToDirtyList( p);
danielk1977d491e1b2008-08-26 18:05:48 +0000388 }
danielk1977f599a192008-08-28 10:21:16 +0000389}
390
391/*
danielk1977bc2ca9e2008-11-13 14:28:28 +0000392** Make sure the page is marked as clean. If it isn't clean already,
danielk1977f599a192008-08-28 10:21:16 +0000393** make it so.
394*/
395void sqlite3PcacheMakeClean(PgHdr *p){
396 if( (p->flags & PGHDR_DIRTY) ){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000397 pcacheRemoveFromDirtyList(p);
398 p->flags &= ~(PGHDR_DIRTY|PGHDR_NEED_SYNC);
399 if( p->nRef==0 ){
400 pcacheUnpin(p);
401 }
danielk1977f599a192008-08-28 10:21:16 +0000402 }
danielk19778c0a7912008-08-20 14:49:23 +0000403}
404
405/*
406** Make every page in the cache clean.
407*/
408void sqlite3PcacheCleanAll(PCache *pCache){
409 PgHdr *p;
danielk19778c0a7912008-08-20 14:49:23 +0000410 while( (p = pCache->pDirty)!=0 ){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000411 sqlite3PcacheMakeClean(p);
danielk19778c0a7912008-08-20 14:49:23 +0000412 }
413}
414
415/*
danielk1977bc2ca9e2008-11-13 14:28:28 +0000416** Clear the PGHDR_NEED_SYNC flag from all dirty pages.
417*/
418void sqlite3PcacheClearSyncFlags(PCache *pCache){
419 PgHdr *p;
420 for(p=pCache->pDirty; p; p=p->pDirtyNext){
421 p->flags &= ~PGHDR_NEED_SYNC;
422 }
423 pCache->pSynced = pCache->pDirtyTail;
424}
425
426/*
427** Change the page number of page p to newPgno.
danielk19778c0a7912008-08-20 14:49:23 +0000428*/
429void sqlite3PcacheMove(PgHdr *p, Pgno newPgno){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000430 PCache *pCache = p->pCache;
danielk1977d491e1b2008-08-26 18:05:48 +0000431 assert( p->nRef>0 );
danielk1977bc2ca9e2008-11-13 14:28:28 +0000432 assert( newPgno>0 );
dan22e21ff2011-11-08 20:08:44 +0000433 sqlite3GlobalConfig.pcache2.xRekey(pCache->pCache, p->pPage, p->pgno,newPgno);
danielk19778c0a7912008-08-20 14:49:23 +0000434 p->pgno = newPgno;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000435 if( (p->flags&PGHDR_DIRTY) && (p->flags&PGHDR_NEED_SYNC) ){
436 pcacheRemoveFromDirtyList(p);
437 pcacheAddToDirtyList(p);
danielk19778c0a7912008-08-20 14:49:23 +0000438 }
danielk19778c0a7912008-08-20 14:49:23 +0000439}
440
441/*
danielk1977bc2ca9e2008-11-13 14:28:28 +0000442** Drop every cache entry whose page number is greater than "pgno". The
443** caller must ensure that there are no outstanding references to any pages
444** other than page 1 with a page number greater than pgno.
445**
446** If there is a reference to page 1 and the pgno parameter passed to this
447** function is 0, then the data area associated with page 1 is zeroed, but
448** the page object is not dropped.
danielk19778c0a7912008-08-20 14:49:23 +0000449*/
450void sqlite3PcacheTruncate(PCache *pCache, Pgno pgno){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000451 if( pCache->pCache ){
452 PgHdr *p;
453 PgHdr *pNext;
454 for(p=pCache->pDirty; p; p=pNext){
455 pNext = p->pDirtyNext;
drhf3609ee2010-03-19 19:23:51 +0000456 /* This routine never gets call with a positive pgno except right
457 ** after sqlite3PcacheCleanAll(). So if there are dirty pages,
458 ** it must be that pgno==0.
459 */
460 assert( p->pgno>0 );
461 if( ALWAYS(p->pgno>pgno) ){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000462 assert( p->flags&PGHDR_DIRTY );
463 sqlite3PcacheMakeClean(p);
danielk19778c0a7912008-08-20 14:49:23 +0000464 }
465 }
danielk1977bc2ca9e2008-11-13 14:28:28 +0000466 if( pgno==0 && pCache->pPage1 ){
467 memset(pCache->pPage1->pData, 0, pCache->szPage);
468 pgno = 1;
469 }
dan22e21ff2011-11-08 20:08:44 +0000470 sqlite3GlobalConfig.pcache2.xTruncate(pCache->pCache, pgno+1);
danielk1977062d4cb2008-08-29 09:10:02 +0000471 }
472}
danielk19778c0a7912008-08-20 14:49:23 +0000473
474/*
475** Close a cache.
476*/
477void sqlite3PcacheClose(PCache *pCache){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000478 if( pCache->pCache ){
dan22e21ff2011-11-08 20:08:44 +0000479 sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
danielk19778c0a7912008-08-20 14:49:23 +0000480 }
481}
482
483/*
484** Discard the contents of the cache.
485*/
danielk1977bea2a942009-01-20 17:06:27 +0000486void sqlite3PcacheClear(PCache *pCache){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000487 sqlite3PcacheTruncate(pCache, 0);
danielk19778c0a7912008-08-20 14:49:23 +0000488}
489
490/*
491** Merge two lists of pages connected by pDirty and in pgno order.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000492** Do not both fixing the pDirtyPrev pointers.
danielk19778c0a7912008-08-20 14:49:23 +0000493*/
494static PgHdr *pcacheMergeDirtyList(PgHdr *pA, PgHdr *pB){
495 PgHdr result, *pTail;
496 pTail = &result;
497 while( pA && pB ){
498 if( pA->pgno<pB->pgno ){
499 pTail->pDirty = pA;
500 pTail = pA;
501 pA = pA->pDirty;
502 }else{
503 pTail->pDirty = pB;
504 pTail = pB;
505 pB = pB->pDirty;
506 }
507 }
508 if( pA ){
509 pTail->pDirty = pA;
510 }else if( pB ){
511 pTail->pDirty = pB;
512 }else{
513 pTail->pDirty = 0;
514 }
515 return result.pDirty;
516}
517
518/*
519** Sort the list of pages in accending order by pgno. Pages are
danielk1977bc2ca9e2008-11-13 14:28:28 +0000520** connected by pDirty pointers. The pDirtyPrev pointers are
danielk19778c0a7912008-08-20 14:49:23 +0000521** corrupted by this sort.
drhe64ca7b2009-07-16 18:21:17 +0000522**
523** Since there cannot be more than 2^31 distinct pages in a database,
524** there cannot be more than 31 buckets required by the merge sorter.
525** One extra bucket is added to catch overflow in case something
526** ever changes to make the previous sentence incorrect.
danielk19778c0a7912008-08-20 14:49:23 +0000527*/
drhe64ca7b2009-07-16 18:21:17 +0000528#define N_SORT_BUCKET 32
danielk19778c0a7912008-08-20 14:49:23 +0000529static PgHdr *pcacheSortDirtyList(PgHdr *pIn){
drhe64ca7b2009-07-16 18:21:17 +0000530 PgHdr *a[N_SORT_BUCKET], *p;
danielk19778c0a7912008-08-20 14:49:23 +0000531 int i;
532 memset(a, 0, sizeof(a));
533 while( pIn ){
534 p = pIn;
535 pIn = p->pDirty;
536 p->pDirty = 0;
drhe64ca7b2009-07-16 18:21:17 +0000537 for(i=0; ALWAYS(i<N_SORT_BUCKET-1); i++){
danielk19778c0a7912008-08-20 14:49:23 +0000538 if( a[i]==0 ){
539 a[i] = p;
540 break;
541 }else{
542 p = pcacheMergeDirtyList(a[i], p);
543 a[i] = 0;
544 }
545 }
drhe64ca7b2009-07-16 18:21:17 +0000546 if( NEVER(i==N_SORT_BUCKET-1) ){
547 /* To get here, there need to be 2^(N_SORT_BUCKET) elements in
548 ** the input list. But that is impossible.
danielk19778c0a7912008-08-20 14:49:23 +0000549 */
550 a[i] = pcacheMergeDirtyList(a[i], p);
551 }
552 }
553 p = a[0];
554 for(i=1; i<N_SORT_BUCKET; i++){
555 p = pcacheMergeDirtyList(p, a[i]);
556 }
557 return p;
558}
559
560/*
561** Return a list of all dirty pages in the cache, sorted by page number.
562*/
563PgHdr *sqlite3PcacheDirtyList(PCache *pCache){
564 PgHdr *p;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000565 for(p=pCache->pDirty; p; p=p->pDirtyNext){
566 p->pDirty = p->pDirtyNext;
danielk19778c0a7912008-08-20 14:49:23 +0000567 }
568 return pcacheSortDirtyList(pCache->pDirty);
569}
570
danielk19778c0a7912008-08-20 14:49:23 +0000571/*
danielk1977bc2ca9e2008-11-13 14:28:28 +0000572** Return the total number of referenced pages held by the cache.
danielk19778c0a7912008-08-20 14:49:23 +0000573*/
574int sqlite3PcacheRefCount(PCache *pCache){
575 return pCache->nRef;
576}
577
danielk1977bc2ca9e2008-11-13 14:28:28 +0000578/*
579** Return the number of references to the page supplied as an argument.
580*/
danielk197771d5d2c2008-09-29 11:49:47 +0000581int sqlite3PcachePageRefcount(PgHdr *p){
582 return p->nRef;
583}
584
danielk19778c0a7912008-08-20 14:49:23 +0000585/*
586** Return the total number of pages in the cache.
587*/
588int sqlite3PcachePagecount(PCache *pCache){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000589 int nPage = 0;
590 if( pCache->pCache ){
dan22e21ff2011-11-08 20:08:44 +0000591 nPage = sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache);
danielk19778c0a7912008-08-20 14:49:23 +0000592 }
danielk1977bc2ca9e2008-11-13 14:28:28 +0000593 return nPage;
danielk19778c0a7912008-08-20 14:49:23 +0000594}
595
danielk1977f3d3c272008-11-19 16:52:44 +0000596#ifdef SQLITE_TEST
danielk19778c0a7912008-08-20 14:49:23 +0000597/*
danielk1977bc2ca9e2008-11-13 14:28:28 +0000598** Get the suggested cache-size value.
danielk19778c0a7912008-08-20 14:49:23 +0000599*/
600int sqlite3PcacheGetCachesize(PCache *pCache){
drh3b42abb2011-11-09 14:23:04 +0000601 return numberOfCachePages(pCache);
danielk19778c0a7912008-08-20 14:49:23 +0000602}
danielk1977f3d3c272008-11-19 16:52:44 +0000603#endif
danielk19778c0a7912008-08-20 14:49:23 +0000604
605/*
606** Set the suggested cache-size value.
607*/
608void sqlite3PcacheSetCachesize(PCache *pCache, int mxPage){
drh3b42abb2011-11-09 14:23:04 +0000609 pCache->szCache = mxPage;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000610 if( pCache->pCache ){
drh3b42abb2011-11-09 14:23:04 +0000611 sqlite3GlobalConfig.pcache2.xCachesize(pCache->pCache,
612 numberOfCachePages(pCache));
danielk1977bc2ca9e2008-11-13 14:28:28 +0000613 }
danielk19778c0a7912008-08-20 14:49:23 +0000614}
615
drh09419b42011-11-16 19:29:17 +0000616/*
617** Free up as much memory as possible from the page cache.
618*/
619void sqlite3PcacheShrink(PCache *pCache){
620 if( pCache->pCache ){
621 sqlite3GlobalConfig.pcache2.xShrink(pCache->pCache);
622 }
623}
624
danielk1977750e87d2009-07-25 11:46:48 +0000625#if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)
danielk197767e3da72008-08-21 12:19:44 +0000626/*
danielk1977bc2ca9e2008-11-13 14:28:28 +0000627** For all dirty pages currently in the cache, invoke the specified
628** callback. This is only used if the SQLITE_CHECK_PAGES macro is
629** defined.
danielk197767e3da72008-08-21 12:19:44 +0000630*/
danielk1977bc2ca9e2008-11-13 14:28:28 +0000631void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *)){
632 PgHdr *pDirty;
633 for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext){
634 xIter(pDirty);
danielk197767e3da72008-08-21 12:19:44 +0000635 }
danielk1977062d4cb2008-08-29 09:10:02 +0000636}
637#endif