Snapshot of upstream SQLite 3.39.4
[sqlcipher.git] / src / pcache.c
blobe130affd2372aa383b463c00f72299c869a03a15
1 /*
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.
11 *************************************************************************
12 ** This file implements that page cache.
14 #include "sqliteInt.h"
17 ** 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.
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.
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).
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.
35 ** Therefore, pSynced is maintained so that it *almost* always points
36 ** 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).
41 struct PCache {
42 PgHdr *pDirty, *pDirtyTail; /* List of dirty pages in LRU order */
43 PgHdr *pSynced; /* Last synced page in dirty page list */
44 int nRefSum; /* Sum of ref counts over all pages */
45 int szCache; /* Configured cache size */
46 int szSpill; /* Size before spilling occurs */
47 int szPage; /* Size of every page in this cache */
48 int szExtra; /* Size of extra space for each page */
49 u8 bPurgeable; /* True if pages are on backing store */
50 u8 eCreate; /* eCreate value for for xFetch() */
51 int (*xStress)(void*,PgHdr*); /* Call to try make a page clean */
52 void *pStress; /* Argument to xStress */
53 sqlite3_pcache *pCache; /* Pluggable cache module */
56 /********************************** Test and Debug Logic **********************/
58 ** Debug tracing macros. Enable by by changing the "0" to "1" and
59 ** recompiling.
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.
65 #if defined(SQLITE_DEBUG) && 0
66 int sqlite3PcacheTrace = 2; /* 0: off 1: simple 2: cache dumps */
67 int sqlite3PcacheMxDump = 9999; /* Max cache entries for pcacheDump() */
68 # define pcacheTrace(X) if(sqlite3PcacheTrace){sqlite3DebugPrintf X;}
69 void pcacheDump(PCache *pCache){
70 int N;
71 int i, j;
72 sqlite3_pcache_page *pLower;
73 PgHdr *pPg;
74 unsigned char *a;
76 if( sqlite3PcacheTrace<2 ) return;
77 if( pCache->pCache==0 ) return;
78 N = sqlite3PcachePagecount(pCache);
79 if( N>sqlite3PcacheMxDump ) N = sqlite3PcacheMxDump;
80 for(i=1; i<=N; i++){
81 pLower = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, i, 0);
82 if( pLower==0 ) continue;
83 pPg = (PgHdr*)pLower->pExtra;
84 printf("%3d: nRef %2d flgs %02x data ", i, pPg->nRef, pPg->flags);
85 a = (unsigned char *)pLower->pBuf;
86 for(j=0; j<12; j++) printf("%02x", a[j]);
87 printf("\n");
88 if( pPg->pPage==0 ){
89 sqlite3GlobalConfig.pcache2.xUnpin(pCache->pCache, pLower, 0);
93 #else
94 # define pcacheTrace(X)
95 # define pcacheDump(X)
96 #endif
99 ** Check invariants on a PgHdr entry. Return true if everything is OK.
100 ** Return false if any invariant is violated.
102 ** This routine is for use inside of assert() statements only. For
103 ** example:
105 ** assert( sqlite3PcachePageSanity(pPg) );
107 #ifdef SQLITE_DEBUG
108 int sqlite3PcachePageSanity(PgHdr *pPg){
109 PCache *pCache;
110 assert( pPg!=0 );
111 assert( pPg->pgno>0 || pPg->pPager==0 ); /* Page number is 1 or more */
112 pCache = pPg->pCache;
113 assert( pCache!=0 ); /* Every page has an associated PCache */
114 if( pPg->flags & PGHDR_CLEAN ){
115 assert( (pPg->flags & PGHDR_DIRTY)==0 );/* Cannot be both CLEAN and DIRTY */
116 assert( pCache->pDirty!=pPg ); /* CLEAN pages not on dirty list */
117 assert( pCache->pDirtyTail!=pPg );
119 /* WRITEABLE pages must also be DIRTY */
120 if( pPg->flags & PGHDR_WRITEABLE ){
121 assert( pPg->flags & PGHDR_DIRTY ); /* WRITEABLE implies DIRTY */
123 /* NEED_SYNC can be set independently of WRITEABLE. This can happen,
124 ** for example, when using the sqlite3PagerDontWrite() optimization:
125 ** (1) Page X is journalled, and gets WRITEABLE and NEED_SEEK.
126 ** (2) Page X moved to freelist, WRITEABLE is cleared
127 ** (3) Page X reused, WRITEABLE is set again
128 ** If NEED_SYNC had been cleared in step 2, then it would not be reset
129 ** in step 3, and page might be written into the database without first
130 ** syncing the rollback journal, which might cause corruption on a power
131 ** loss.
133 ** Another example is when the database page size is smaller than the
134 ** disk sector size. When any page of a sector is journalled, all pages
135 ** in that sector are marked NEED_SYNC even if they are still CLEAN, just
136 ** in case they are later modified, since all pages in the same sector
137 ** must be journalled and synced before any of those pages can be safely
138 ** written.
140 return 1;
142 #endif /* SQLITE_DEBUG */
145 /********************************** Linked List Management ********************/
147 /* Allowed values for second argument to pcacheManageDirtyList() */
148 #define PCACHE_DIRTYLIST_REMOVE 1 /* Remove pPage from dirty list */
149 #define PCACHE_DIRTYLIST_ADD 2 /* Add pPage to the dirty list */
150 #define PCACHE_DIRTYLIST_FRONT 3 /* Move pPage to the front of the list */
153 ** Manage pPage's participation on the dirty list. Bits of the addRemove
154 ** argument determines what operation to do. The 0x01 bit means first
155 ** remove pPage from the dirty list. The 0x02 means add pPage back to
156 ** the dirty list. Doing both moves pPage to the front of the dirty list.
158 static void pcacheManageDirtyList(PgHdr *pPage, u8 addRemove){
159 PCache *p = pPage->pCache;
161 pcacheTrace(("%p.DIRTYLIST.%s %d\n", p,
162 addRemove==1 ? "REMOVE" : addRemove==2 ? "ADD" : "FRONT",
163 pPage->pgno));
164 if( addRemove & PCACHE_DIRTYLIST_REMOVE ){
165 assert( pPage->pDirtyNext || pPage==p->pDirtyTail );
166 assert( pPage->pDirtyPrev || pPage==p->pDirty );
168 /* Update the PCache1.pSynced variable if necessary. */
169 if( p->pSynced==pPage ){
170 p->pSynced = pPage->pDirtyPrev;
173 if( pPage->pDirtyNext ){
174 pPage->pDirtyNext->pDirtyPrev = pPage->pDirtyPrev;
175 }else{
176 assert( pPage==p->pDirtyTail );
177 p->pDirtyTail = pPage->pDirtyPrev;
179 if( pPage->pDirtyPrev ){
180 pPage->pDirtyPrev->pDirtyNext = pPage->pDirtyNext;
181 }else{
182 /* If there are now no dirty pages in the cache, set eCreate to 2.
183 ** This is an optimization that allows sqlite3PcacheFetch() to skip
184 ** searching for a dirty page to eject from the cache when it might
185 ** otherwise have to. */
186 assert( pPage==p->pDirty );
187 p->pDirty = pPage->pDirtyNext;
188 assert( p->bPurgeable || p->eCreate==2 );
189 if( p->pDirty==0 ){ /*OPTIMIZATION-IF-TRUE*/
190 assert( p->bPurgeable==0 || p->eCreate==1 );
191 p->eCreate = 2;
195 if( addRemove & PCACHE_DIRTYLIST_ADD ){
196 pPage->pDirtyPrev = 0;
197 pPage->pDirtyNext = p->pDirty;
198 if( pPage->pDirtyNext ){
199 assert( pPage->pDirtyNext->pDirtyPrev==0 );
200 pPage->pDirtyNext->pDirtyPrev = pPage;
201 }else{
202 p->pDirtyTail = pPage;
203 if( p->bPurgeable ){
204 assert( p->eCreate==2 );
205 p->eCreate = 1;
208 p->pDirty = pPage;
210 /* If pSynced is NULL and this page has a clear NEED_SYNC flag, set
211 ** pSynced to point to it. Checking the NEED_SYNC flag is an
212 ** optimization, as if pSynced points to a page with the NEED_SYNC
213 ** flag set sqlite3PcacheFetchStress() searches through all newer
214 ** entries of the dirty-list for a page with NEED_SYNC clear anyway. */
215 if( !p->pSynced
216 && 0==(pPage->flags&PGHDR_NEED_SYNC) /*OPTIMIZATION-IF-FALSE*/
218 p->pSynced = pPage;
221 pcacheDump(p);
225 ** Wrapper around the pluggable caches xUnpin method. If the cache is
226 ** being used for an in-memory database, this function is a no-op.
228 static void pcacheUnpin(PgHdr *p){
229 if( p->pCache->bPurgeable ){
230 pcacheTrace(("%p.UNPIN %d\n", p->pCache, p->pgno));
231 sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 0);
232 pcacheDump(p->pCache);
237 ** Compute the number of pages of cache requested. p->szCache is the
238 ** cache size requested by the "PRAGMA cache_size" statement.
240 static int numberOfCachePages(PCache *p){
241 if( p->szCache>=0 ){
242 /* IMPLEMENTATION-OF: R-42059-47211 If the argument N is positive then the
243 ** suggested cache size is set to N. */
244 return p->szCache;
245 }else{
246 i64 n;
247 /* IMPLEMANTATION-OF: R-59858-46238 If the argument N is negative, then the
248 ** number of cache pages is adjusted to be a number of pages that would
249 ** use approximately abs(N*1024) bytes of memory based on the current
250 ** page size. */
251 n = ((-1024*(i64)p->szCache)/(p->szPage+p->szExtra));
252 if( n>1000000000 ) n = 1000000000;
253 return (int)n;
257 /*************************************************** General Interfaces ******
259 ** Initialize and shutdown the page cache subsystem. Neither of these
260 ** functions are threadsafe.
262 int sqlite3PcacheInitialize(void){
263 if( sqlite3GlobalConfig.pcache2.xInit==0 ){
264 /* IMPLEMENTATION-OF: R-26801-64137 If the xInit() method is NULL, then the
265 ** built-in default page cache is used instead of the application defined
266 ** page cache. */
267 sqlite3PCacheSetDefault();
268 assert( sqlite3GlobalConfig.pcache2.xInit!=0 );
270 return sqlite3GlobalConfig.pcache2.xInit(sqlite3GlobalConfig.pcache2.pArg);
272 void sqlite3PcacheShutdown(void){
273 if( sqlite3GlobalConfig.pcache2.xShutdown ){
274 /* IMPLEMENTATION-OF: R-26000-56589 The xShutdown() method may be NULL. */
275 sqlite3GlobalConfig.pcache2.xShutdown(sqlite3GlobalConfig.pcache2.pArg);
280 ** Return the size in bytes of a PCache object.
282 int sqlite3PcacheSize(void){ return sizeof(PCache); }
285 ** Create a new PCache object. Storage space to hold the object
286 ** has already been allocated and is passed in as the p pointer.
287 ** The caller discovers how much space needs to be allocated by
288 ** calling sqlite3PcacheSize().
290 ** szExtra is some extra space allocated for each page. The first
291 ** 8 bytes of the extra space will be zeroed as the page is allocated,
292 ** but remaining content will be uninitialized. Though it is opaque
293 ** to this module, the extra space really ends up being the MemPage
294 ** structure in the pager.
296 int sqlite3PcacheOpen(
297 int szPage, /* Size of every page */
298 int szExtra, /* Extra space associated with each page */
299 int bPurgeable, /* True if pages are on backing store */
300 int (*xStress)(void*,PgHdr*),/* Call to try to make pages clean */
301 void *pStress, /* Argument to xStress */
302 PCache *p /* Preallocated space for the PCache */
304 memset(p, 0, sizeof(PCache));
305 p->szPage = 1;
306 p->szExtra = szExtra;
307 assert( szExtra>=8 ); /* First 8 bytes will be zeroed */
308 p->bPurgeable = bPurgeable;
309 p->eCreate = 2;
310 p->xStress = xStress;
311 p->pStress = pStress;
312 p->szCache = 100;
313 p->szSpill = 1;
314 pcacheTrace(("%p.OPEN szPage %d bPurgeable %d\n",p,szPage,bPurgeable));
315 return sqlite3PcacheSetPageSize(p, szPage);
319 ** Change the page size for PCache object. The caller must ensure that there
320 ** are no outstanding page references when this function is called.
322 int sqlite3PcacheSetPageSize(PCache *pCache, int szPage){
323 assert( pCache->nRefSum==0 && pCache->pDirty==0 );
324 if( pCache->szPage ){
325 sqlite3_pcache *pNew;
326 pNew = sqlite3GlobalConfig.pcache2.xCreate(
327 szPage, pCache->szExtra + ROUND8(sizeof(PgHdr)),
328 pCache->bPurgeable
330 if( pNew==0 ) return SQLITE_NOMEM_BKPT;
331 sqlite3GlobalConfig.pcache2.xCachesize(pNew, numberOfCachePages(pCache));
332 if( pCache->pCache ){
333 sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
335 pCache->pCache = pNew;
336 pCache->szPage = szPage;
337 pcacheTrace(("%p.PAGESIZE %d\n",pCache,szPage));
339 return SQLITE_OK;
343 ** Try to obtain a page from the cache.
345 ** This routine returns a pointer to an sqlite3_pcache_page object if
346 ** such an object is already in cache, or if a new one is created.
347 ** This routine returns a NULL pointer if the object was not in cache
348 ** and could not be created.
350 ** The createFlags should be 0 to check for existing pages and should
351 ** be 3 (not 1, but 3) to try to create a new page.
353 ** If the createFlag is 0, then NULL is always returned if the page
354 ** is not already in the cache. If createFlag is 1, then a new page
355 ** is created only if that can be done without spilling dirty pages
356 ** and without exceeding the cache size limit.
358 ** The caller needs to invoke sqlite3PcacheFetchFinish() to properly
359 ** initialize the sqlite3_pcache_page object and convert it into a
360 ** PgHdr object. The sqlite3PcacheFetch() and sqlite3PcacheFetchFinish()
361 ** routines are split this way for performance reasons. When separated
362 ** they can both (usually) operate without having to push values to
363 ** the stack on entry and pop them back off on exit, which saves a
364 ** lot of pushing and popping.
366 sqlite3_pcache_page *sqlite3PcacheFetch(
367 PCache *pCache, /* Obtain the page from this cache */
368 Pgno pgno, /* Page number to obtain */
369 int createFlag /* If true, create page if it does not exist already */
371 int eCreate;
372 sqlite3_pcache_page *pRes;
374 assert( pCache!=0 );
375 assert( pCache->pCache!=0 );
376 assert( createFlag==3 || createFlag==0 );
377 assert( pCache->eCreate==((pCache->bPurgeable && pCache->pDirty) ? 1 : 2) );
379 /* eCreate defines what to do if the page does not exist.
380 ** 0 Do not allocate a new page. (createFlag==0)
381 ** 1 Allocate a new page if doing so is inexpensive.
382 ** (createFlag==1 AND bPurgeable AND pDirty)
383 ** 2 Allocate a new page even it doing so is difficult.
384 ** (createFlag==1 AND !(bPurgeable AND pDirty)
386 eCreate = createFlag & pCache->eCreate;
387 assert( eCreate==0 || eCreate==1 || eCreate==2 );
388 assert( createFlag==0 || pCache->eCreate==eCreate );
389 assert( createFlag==0 || eCreate==1+(!pCache->bPurgeable||!pCache->pDirty) );
390 pRes = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, eCreate);
391 pcacheTrace(("%p.FETCH %d%s (result: %p)\n",pCache,pgno,
392 createFlag?" create":"",pRes));
393 return pRes;
397 ** If the sqlite3PcacheFetch() routine is unable to allocate a new
398 ** page because no clean pages are available for reuse and the cache
399 ** size limit has been reached, then this routine can be invoked to
400 ** try harder to allocate a page. This routine might invoke the stress
401 ** callback to spill dirty pages to the journal. It will then try to
402 ** allocate the new page and will only fail to allocate a new page on
403 ** an OOM error.
405 ** This routine should be invoked only after sqlite3PcacheFetch() fails.
407 int sqlite3PcacheFetchStress(
408 PCache *pCache, /* Obtain the page from this cache */
409 Pgno pgno, /* Page number to obtain */
410 sqlite3_pcache_page **ppPage /* Write result here */
412 PgHdr *pPg;
413 if( pCache->eCreate==2 ) return 0;
415 if( sqlite3PcachePagecount(pCache)>pCache->szSpill ){
416 /* Find a dirty page to write-out and recycle. First try to find a
417 ** page that does not require a journal-sync (one with PGHDR_NEED_SYNC
418 ** cleared), but if that is not possible settle for any other
419 ** unreferenced dirty page.
421 ** If the LRU page in the dirty list that has a clear PGHDR_NEED_SYNC
422 ** flag is currently referenced, then the following may leave pSynced
423 ** set incorrectly (pointing to other than the LRU page with NEED_SYNC
424 ** cleared). This is Ok, as pSynced is just an optimization. */
425 for(pPg=pCache->pSynced;
426 pPg && (pPg->nRef || (pPg->flags&PGHDR_NEED_SYNC));
427 pPg=pPg->pDirtyPrev
429 pCache->pSynced = pPg;
430 if( !pPg ){
431 for(pPg=pCache->pDirtyTail; pPg && pPg->nRef; pPg=pPg->pDirtyPrev);
433 if( pPg ){
434 int rc;
435 #ifdef SQLITE_LOG_CACHE_SPILL
436 sqlite3_log(SQLITE_FULL,
437 "spill page %d making room for %d - cache used: %d/%d",
438 pPg->pgno, pgno,
439 sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache),
440 numberOfCachePages(pCache));
441 #endif
442 pcacheTrace(("%p.SPILL %d\n",pCache,pPg->pgno));
443 rc = pCache->xStress(pCache->pStress, pPg);
444 pcacheDump(pCache);
445 if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
446 return rc;
450 *ppPage = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, 2);
451 return *ppPage==0 ? SQLITE_NOMEM_BKPT : SQLITE_OK;
455 ** This is a helper routine for sqlite3PcacheFetchFinish()
457 ** In the uncommon case where the page being fetched has not been
458 ** initialized, this routine is invoked to do the initialization.
459 ** This routine is broken out into a separate function since it
460 ** requires extra stack manipulation that can be avoided in the common
461 ** case.
463 static SQLITE_NOINLINE PgHdr *pcacheFetchFinishWithInit(
464 PCache *pCache, /* Obtain the page from this cache */
465 Pgno pgno, /* Page number obtained */
466 sqlite3_pcache_page *pPage /* Page obtained by prior PcacheFetch() call */
468 PgHdr *pPgHdr;
469 assert( pPage!=0 );
470 pPgHdr = (PgHdr*)pPage->pExtra;
471 assert( pPgHdr->pPage==0 );
472 memset(&pPgHdr->pDirty, 0, sizeof(PgHdr) - offsetof(PgHdr,pDirty));
473 pPgHdr->pPage = pPage;
474 pPgHdr->pData = pPage->pBuf;
475 pPgHdr->pExtra = (void *)&pPgHdr[1];
476 memset(pPgHdr->pExtra, 0, 8);
477 pPgHdr->pCache = pCache;
478 pPgHdr->pgno = pgno;
479 pPgHdr->flags = PGHDR_CLEAN;
480 return sqlite3PcacheFetchFinish(pCache,pgno,pPage);
484 ** This routine converts the sqlite3_pcache_page object returned by
485 ** sqlite3PcacheFetch() into an initialized PgHdr object. This routine
486 ** must be called after sqlite3PcacheFetch() in order to get a usable
487 ** result.
489 PgHdr *sqlite3PcacheFetchFinish(
490 PCache *pCache, /* Obtain the page from this cache */
491 Pgno pgno, /* Page number obtained */
492 sqlite3_pcache_page *pPage /* Page obtained by prior PcacheFetch() call */
494 PgHdr *pPgHdr;
496 assert( pPage!=0 );
497 pPgHdr = (PgHdr *)pPage->pExtra;
499 if( !pPgHdr->pPage ){
500 return pcacheFetchFinishWithInit(pCache, pgno, pPage);
502 pCache->nRefSum++;
503 pPgHdr->nRef++;
504 assert( sqlite3PcachePageSanity(pPgHdr) );
505 return pPgHdr;
509 ** Decrement the reference count on a page. If the page is clean and the
510 ** reference count drops to 0, then it is made eligible for recycling.
512 void SQLITE_NOINLINE sqlite3PcacheRelease(PgHdr *p){
513 assert( p->nRef>0 );
514 p->pCache->nRefSum--;
515 if( (--p->nRef)==0 ){
516 if( p->flags&PGHDR_CLEAN ){
517 pcacheUnpin(p);
518 }else{
519 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_FRONT);
525 ** Increase the reference count of a supplied page by 1.
527 void sqlite3PcacheRef(PgHdr *p){
528 assert(p->nRef>0);
529 assert( sqlite3PcachePageSanity(p) );
530 p->nRef++;
531 p->pCache->nRefSum++;
535 ** Drop a page from the cache. There must be exactly one reference to the
536 ** page. This function deletes that reference, so after it returns the
537 ** page pointed to by p is invalid.
539 void sqlite3PcacheDrop(PgHdr *p){
540 assert( p->nRef==1 );
541 assert( sqlite3PcachePageSanity(p) );
542 if( p->flags&PGHDR_DIRTY ){
543 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE);
545 p->pCache->nRefSum--;
546 sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 1);
550 ** Make sure the page is marked as dirty. If it isn't dirty already,
551 ** make it so.
553 void sqlite3PcacheMakeDirty(PgHdr *p){
554 assert( p->nRef>0 );
555 assert( sqlite3PcachePageSanity(p) );
556 if( p->flags & (PGHDR_CLEAN|PGHDR_DONT_WRITE) ){ /*OPTIMIZATION-IF-FALSE*/
557 p->flags &= ~PGHDR_DONT_WRITE;
558 if( p->flags & PGHDR_CLEAN ){
559 p->flags ^= (PGHDR_DIRTY|PGHDR_CLEAN);
560 pcacheTrace(("%p.DIRTY %d\n",p->pCache,p->pgno));
561 assert( (p->flags & (PGHDR_DIRTY|PGHDR_CLEAN))==PGHDR_DIRTY );
562 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_ADD);
564 assert( sqlite3PcachePageSanity(p) );
569 ** Make sure the page is marked as clean. If it isn't clean already,
570 ** make it so.
572 void sqlite3PcacheMakeClean(PgHdr *p){
573 assert( sqlite3PcachePageSanity(p) );
574 assert( (p->flags & PGHDR_DIRTY)!=0 );
575 assert( (p->flags & PGHDR_CLEAN)==0 );
576 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE);
577 p->flags &= ~(PGHDR_DIRTY|PGHDR_NEED_SYNC|PGHDR_WRITEABLE);
578 p->flags |= PGHDR_CLEAN;
579 pcacheTrace(("%p.CLEAN %d\n",p->pCache,p->pgno));
580 assert( sqlite3PcachePageSanity(p) );
581 if( p->nRef==0 ){
582 pcacheUnpin(p);
587 ** Make every page in the cache clean.
589 void sqlite3PcacheCleanAll(PCache *pCache){
590 PgHdr *p;
591 pcacheTrace(("%p.CLEAN-ALL\n",pCache));
592 while( (p = pCache->pDirty)!=0 ){
593 sqlite3PcacheMakeClean(p);
598 ** Clear the PGHDR_NEED_SYNC and PGHDR_WRITEABLE flag from all dirty pages.
600 void sqlite3PcacheClearWritable(PCache *pCache){
601 PgHdr *p;
602 pcacheTrace(("%p.CLEAR-WRITEABLE\n",pCache));
603 for(p=pCache->pDirty; p; p=p->pDirtyNext){
604 p->flags &= ~(PGHDR_NEED_SYNC|PGHDR_WRITEABLE);
606 pCache->pSynced = pCache->pDirtyTail;
610 ** Clear the PGHDR_NEED_SYNC flag from all dirty pages.
612 void sqlite3PcacheClearSyncFlags(PCache *pCache){
613 PgHdr *p;
614 for(p=pCache->pDirty; p; p=p->pDirtyNext){
615 p->flags &= ~PGHDR_NEED_SYNC;
617 pCache->pSynced = pCache->pDirtyTail;
621 ** Change the page number of page p to newPgno.
623 void sqlite3PcacheMove(PgHdr *p, Pgno newPgno){
624 PCache *pCache = p->pCache;
625 sqlite3_pcache_page *pOther;
626 assert( p->nRef>0 );
627 assert( newPgno>0 );
628 assert( sqlite3PcachePageSanity(p) );
629 pcacheTrace(("%p.MOVE %d -> %d\n",pCache,p->pgno,newPgno));
630 pOther = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, newPgno, 0);
631 if( pOther ){
632 PgHdr *pXPage = (PgHdr*)pOther->pExtra;
633 assert( pXPage->nRef==0 );
634 pXPage->nRef++;
635 pCache->nRefSum++;
636 sqlite3PcacheDrop(pXPage);
638 sqlite3GlobalConfig.pcache2.xRekey(pCache->pCache, p->pPage, p->pgno,newPgno);
639 p->pgno = newPgno;
640 if( (p->flags&PGHDR_DIRTY) && (p->flags&PGHDR_NEED_SYNC) ){
641 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_FRONT);
642 assert( sqlite3PcachePageSanity(p) );
647 ** Drop every cache entry whose page number is greater than "pgno". The
648 ** caller must ensure that there are no outstanding references to any pages
649 ** other than page 1 with a page number greater than pgno.
651 ** If there is a reference to page 1 and the pgno parameter passed to this
652 ** function is 0, then the data area associated with page 1 is zeroed, but
653 ** the page object is not dropped.
655 void sqlite3PcacheTruncate(PCache *pCache, Pgno pgno){
656 if( pCache->pCache ){
657 PgHdr *p;
658 PgHdr *pNext;
659 pcacheTrace(("%p.TRUNCATE %d\n",pCache,pgno));
660 for(p=pCache->pDirty; p; p=pNext){
661 pNext = p->pDirtyNext;
662 /* This routine never gets call with a positive pgno except right
663 ** after sqlite3PcacheCleanAll(). So if there are dirty pages,
664 ** it must be that pgno==0.
666 assert( p->pgno>0 );
667 if( p->pgno>pgno ){
668 assert( p->flags&PGHDR_DIRTY );
669 sqlite3PcacheMakeClean(p);
672 if( pgno==0 && pCache->nRefSum ){
673 sqlite3_pcache_page *pPage1;
674 pPage1 = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache,1,0);
675 if( ALWAYS(pPage1) ){ /* Page 1 is always available in cache, because
676 ** pCache->nRefSum>0 */
677 memset(pPage1->pBuf, 0, pCache->szPage);
678 pgno = 1;
681 sqlite3GlobalConfig.pcache2.xTruncate(pCache->pCache, pgno+1);
686 ** Close a cache.
688 void sqlite3PcacheClose(PCache *pCache){
689 assert( pCache->pCache!=0 );
690 pcacheTrace(("%p.CLOSE\n",pCache));
691 sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
695 ** Discard the contents of the cache.
697 void sqlite3PcacheClear(PCache *pCache){
698 sqlite3PcacheTruncate(pCache, 0);
702 ** Merge two lists of pages connected by pDirty and in pgno order.
703 ** Do not bother fixing the pDirtyPrev pointers.
705 static PgHdr *pcacheMergeDirtyList(PgHdr *pA, PgHdr *pB){
706 PgHdr result, *pTail;
707 pTail = &result;
708 assert( pA!=0 && pB!=0 );
709 for(;;){
710 if( pA->pgno<pB->pgno ){
711 pTail->pDirty = pA;
712 pTail = pA;
713 pA = pA->pDirty;
714 if( pA==0 ){
715 pTail->pDirty = pB;
716 break;
718 }else{
719 pTail->pDirty = pB;
720 pTail = pB;
721 pB = pB->pDirty;
722 if( pB==0 ){
723 pTail->pDirty = pA;
724 break;
728 return result.pDirty;
732 ** Sort the list of pages in accending order by pgno. Pages are
733 ** connected by pDirty pointers. The pDirtyPrev pointers are
734 ** corrupted by this sort.
736 ** Since there cannot be more than 2^31 distinct pages in a database,
737 ** there cannot be more than 31 buckets required by the merge sorter.
738 ** One extra bucket is added to catch overflow in case something
739 ** ever changes to make the previous sentence incorrect.
741 #define N_SORT_BUCKET 32
742 static PgHdr *pcacheSortDirtyList(PgHdr *pIn){
743 PgHdr *a[N_SORT_BUCKET], *p;
744 int i;
745 memset(a, 0, sizeof(a));
746 while( pIn ){
747 p = pIn;
748 pIn = p->pDirty;
749 p->pDirty = 0;
750 for(i=0; ALWAYS(i<N_SORT_BUCKET-1); i++){
751 if( a[i]==0 ){
752 a[i] = p;
753 break;
754 }else{
755 p = pcacheMergeDirtyList(a[i], p);
756 a[i] = 0;
759 if( NEVER(i==N_SORT_BUCKET-1) ){
760 /* To get here, there need to be 2^(N_SORT_BUCKET) elements in
761 ** the input list. But that is impossible.
763 a[i] = pcacheMergeDirtyList(a[i], p);
766 p = a[0];
767 for(i=1; i<N_SORT_BUCKET; i++){
768 if( a[i]==0 ) continue;
769 p = p ? pcacheMergeDirtyList(p, a[i]) : a[i];
771 return p;
775 ** Return a list of all dirty pages in the cache, sorted by page number.
777 PgHdr *sqlite3PcacheDirtyList(PCache *pCache){
778 PgHdr *p;
779 for(p=pCache->pDirty; p; p=p->pDirtyNext){
780 p->pDirty = p->pDirtyNext;
782 return pcacheSortDirtyList(pCache->pDirty);
786 ** Return the total number of references to all pages held by the cache.
788 ** This is not the total number of pages referenced, but the sum of the
789 ** reference count for all pages.
791 int sqlite3PcacheRefCount(PCache *pCache){
792 return pCache->nRefSum;
796 ** Return the number of references to the page supplied as an argument.
798 int sqlite3PcachePageRefcount(PgHdr *p){
799 return p->nRef;
803 ** Return the total number of pages in the cache.
805 int sqlite3PcachePagecount(PCache *pCache){
806 assert( pCache->pCache!=0 );
807 return sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache);
810 #ifdef SQLITE_TEST
812 ** Get the suggested cache-size value.
814 int sqlite3PcacheGetCachesize(PCache *pCache){
815 return numberOfCachePages(pCache);
817 #endif
820 ** Set the suggested cache-size value.
822 void sqlite3PcacheSetCachesize(PCache *pCache, int mxPage){
823 assert( pCache->pCache!=0 );
824 pCache->szCache = mxPage;
825 sqlite3GlobalConfig.pcache2.xCachesize(pCache->pCache,
826 numberOfCachePages(pCache));
830 ** Set the suggested cache-spill value. Make no changes if if the
831 ** argument is zero. Return the effective cache-spill size, which will
832 ** be the larger of the szSpill and szCache.
834 int sqlite3PcacheSetSpillsize(PCache *p, int mxPage){
835 int res;
836 assert( p->pCache!=0 );
837 if( mxPage ){
838 if( mxPage<0 ){
839 mxPage = (int)((-1024*(i64)mxPage)/(p->szPage+p->szExtra));
841 p->szSpill = mxPage;
843 res = numberOfCachePages(p);
844 if( res<p->szSpill ) res = p->szSpill;
845 return res;
849 ** Free up as much memory as possible from the page cache.
851 void sqlite3PcacheShrink(PCache *pCache){
852 assert( pCache->pCache!=0 );
853 sqlite3GlobalConfig.pcache2.xShrink(pCache->pCache);
857 ** Return the size of the header added by this middleware layer
858 ** in the page-cache hierarchy.
860 int sqlite3HeaderSizePcache(void){ return ROUND8(sizeof(PgHdr)); }
863 ** Return the number of dirty pages currently in the cache, as a percentage
864 ** of the configured cache size.
866 int sqlite3PCachePercentDirty(PCache *pCache){
867 PgHdr *pDirty;
868 int nDirty = 0;
869 int nCache = numberOfCachePages(pCache);
870 for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext) nDirty++;
871 return nCache ? (int)(((i64)nDirty * 100) / nCache) : 0;
874 #ifdef SQLITE_DIRECT_OVERFLOW_READ
876 ** Return true if there are one or more dirty pages in the cache. Else false.
878 int sqlite3PCacheIsDirty(PCache *pCache){
879 return (pCache->pDirty!=0);
881 #endif
883 #if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)
885 ** For all dirty pages currently in the cache, invoke the specified
886 ** callback. This is only used if the SQLITE_CHECK_PAGES macro is
887 ** defined.
889 void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *)){
890 PgHdr *pDirty;
891 for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext){
892 xIter(pDirty);
895 #endif