000001  /*
000002  ** 2008 August 05
000003  **
000004  ** The author disclaims copyright to this source code.  In place of
000005  ** a legal notice, here is a blessing:
000006  **
000007  **    May you do good and not evil.
000008  **    May you find forgiveness for yourself and forgive others.
000009  **    May you share freely, never taking more than you give.
000010  **
000011  *************************************************************************
000012  ** This file implements that page cache.
000013  */
000014  #include "sqliteInt.h"
000015  
000016  /*
000017  ** A complete page cache is an instance of this structure.  Every
000018  ** entry in the cache holds a single page of the database file.  The
000019  ** btree layer only operates on the cached copy of the database pages.
000020  **
000021  ** A page cache entry is "clean" if it exactly matches what is currently
000022  ** on disk.  A page is "dirty" if it has been modified and needs to be
000023  ** persisted to disk.
000024  **
000025  ** pDirty, pDirtyTail, pSynced:
000026  **   All dirty pages are linked into the doubly linked list using
000027  **   PgHdr.pDirtyNext and pDirtyPrev. The list is maintained in LRU order
000028  **   such that p was added to the list more recently than p->pDirtyNext.
000029  **   PCache.pDirty points to the first (newest) element in the list and
000030  **   pDirtyTail to the last (oldest).
000031  **
000032  **   The PCache.pSynced variable is used to optimize searching for a dirty
000033  **   page to eject from the cache mid-transaction. It is better to eject
000034  **   a page that does not require a journal sync than one that does. 
000035  **   Therefore, pSynced is maintained to that it *almost* always points
000036  **   to either the oldest page in the pDirty/pDirtyTail list that has a
000037  **   clear PGHDR_NEED_SYNC flag or to a page that is older than this one
000038  **   (so that the right page to eject can be found by following pDirtyPrev
000039  **   pointers).
000040  */
000041  struct PCache {
000042    PgHdr *pDirty, *pDirtyTail;         /* List of dirty pages in LRU order */
000043    PgHdr *pSynced;                     /* Last synced page in dirty page list */
000044    int nRefSum;                        /* Sum of ref counts over all pages */
000045    int szCache;                        /* Configured cache size */
000046    int szSpill;                        /* Size before spilling occurs */
000047    int szPage;                         /* Size of every page in this cache */
000048    int szExtra;                        /* Size of extra space for each page */
000049    u8 bPurgeable;                      /* True if pages are on backing store */
000050    u8 eCreate;                         /* eCreate value for for xFetch() */
000051    int (*xStress)(void*,PgHdr*);       /* Call to try make a page clean */
000052    void *pStress;                      /* Argument to xStress */
000053    sqlite3_pcache *pCache;             /* Pluggable cache module */
000054  };
000055  
000056  /********************************** Test and Debug Logic **********************/
000057  /*
000058  ** Debug tracing macros.  Enable by by changing the "0" to "1" and
000059  ** recompiling.
000060  **
000061  ** When sqlite3PcacheTrace is 1, single line trace messages are issued.
000062  ** When sqlite3PcacheTrace is 2, a dump of the pcache showing all cache entries
000063  ** is displayed for many operations, resulting in a lot of output.
000064  */
000065  #if defined(SQLITE_DEBUG) && 0
000066    int sqlite3PcacheTrace = 2;       /* 0: off  1: simple  2: cache dumps */
000067    int sqlite3PcacheMxDump = 9999;   /* Max cache entries for pcacheDump() */
000068  # define pcacheTrace(X) if(sqlite3PcacheTrace){sqlite3DebugPrintf X;}
000069    void pcacheDump(PCache *pCache){
000070      int N;
000071      int i, j;
000072      sqlite3_pcache_page *pLower;
000073      PgHdr *pPg;
000074      unsigned char *a;
000075    
000076      if( sqlite3PcacheTrace<2 ) return;
000077      if( pCache->pCache==0 ) return;
000078      N = sqlite3PcachePagecount(pCache);
000079      if( N>sqlite3PcacheMxDump ) N = sqlite3PcacheMxDump;
000080      for(i=1; i<=N; i++){
000081         pLower = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, i, 0);
000082         if( pLower==0 ) continue;
000083         pPg = (PgHdr*)pLower->pExtra;
000084         printf("%3d: nRef %2d flgs %02x data ", i, pPg->nRef, pPg->flags);
000085         a = (unsigned char *)pLower->pBuf;
000086         for(j=0; j<12; j++) printf("%02x", a[j]);
000087         printf("\n");
000088         if( pPg->pPage==0 ){
000089           sqlite3GlobalConfig.pcache2.xUnpin(pCache->pCache, pLower, 0);
000090         }
000091      }
000092    }
000093    #else
000094  # define pcacheTrace(X)
000095  # define pcacheDump(X)
000096  #endif
000097  
000098  /*
000099  ** Check invariants on a PgHdr entry.  Return true if everything is OK.
000100  ** Return false if any invariant is violated.
000101  **
000102  ** This routine is for use inside of assert() statements only.  For
000103  ** example:
000104  **
000105  **          assert( sqlite3PcachePageSanity(pPg) );
000106  */
000107  #if SQLITE_DEBUG
000108  int sqlite3PcachePageSanity(PgHdr *pPg){
000109    PCache *pCache;
000110    assert( pPg!=0 );
000111    assert( pPg->pgno>0 || pPg->pPager==0 );    /* Page number is 1 or more */
000112    pCache = pPg->pCache;
000113    assert( pCache!=0 );      /* Every page has an associated PCache */
000114    if( pPg->flags & PGHDR_CLEAN ){
000115      assert( (pPg->flags & PGHDR_DIRTY)==0 );/* Cannot be both CLEAN and DIRTY */
000116      assert( pCache->pDirty!=pPg );          /* CLEAN pages not on dirty list */
000117      assert( pCache->pDirtyTail!=pPg );
000118    }
000119    /* WRITEABLE pages must also be DIRTY */
000120    if( pPg->flags & PGHDR_WRITEABLE ){
000121      assert( pPg->flags & PGHDR_DIRTY );     /* WRITEABLE implies DIRTY */
000122    }
000123    /* NEED_SYNC can be set independently of WRITEABLE.  This can happen,
000124    ** for example, when using the sqlite3PagerDontWrite() optimization:
000125    **    (1)  Page X is journalled, and gets WRITEABLE and NEED_SEEK.
000126    **    (2)  Page X moved to freelist, WRITEABLE is cleared
000127    **    (3)  Page X reused, WRITEABLE is set again
000128    ** If NEED_SYNC had been cleared in step 2, then it would not be reset
000129    ** in step 3, and page might be written into the database without first
000130    ** syncing the rollback journal, which might cause corruption on a power
000131    ** loss.
000132    **
000133    ** Another example is when the database page size is smaller than the
000134    ** disk sector size.  When any page of a sector is journalled, all pages
000135    ** in that sector are marked NEED_SYNC even if they are still CLEAN, just
000136    ** in case they are later modified, since all pages in the same sector
000137    ** must be journalled and synced before any of those pages can be safely
000138    ** written.
000139    */
000140    return 1;
000141  }
000142  #endif /* SQLITE_DEBUG */
000143  
000144  
000145  /********************************** Linked List Management ********************/
000146  
000147  /* Allowed values for second argument to pcacheManageDirtyList() */
000148  #define PCACHE_DIRTYLIST_REMOVE   1    /* Remove pPage from dirty list */
000149  #define PCACHE_DIRTYLIST_ADD      2    /* Add pPage to the dirty list */
000150  #define PCACHE_DIRTYLIST_FRONT    3    /* Move pPage to the front of the list */
000151  
000152  /*
000153  ** Manage pPage's participation on the dirty list.  Bits of the addRemove
000154  ** argument determines what operation to do.  The 0x01 bit means first
000155  ** remove pPage from the dirty list.  The 0x02 means add pPage back to
000156  ** the dirty list.  Doing both moves pPage to the front of the dirty list.
000157  */
000158  static void pcacheManageDirtyList(PgHdr *pPage, u8 addRemove){
000159    PCache *p = pPage->pCache;
000160  
000161    pcacheTrace(("%p.DIRTYLIST.%s %d\n", p,
000162                  addRemove==1 ? "REMOVE" : addRemove==2 ? "ADD" : "FRONT",
000163                  pPage->pgno));
000164    if( addRemove & PCACHE_DIRTYLIST_REMOVE ){
000165      assert( pPage->pDirtyNext || pPage==p->pDirtyTail );
000166      assert( pPage->pDirtyPrev || pPage==p->pDirty );
000167    
000168      /* Update the PCache1.pSynced variable if necessary. */
000169      if( p->pSynced==pPage ){
000170        p->pSynced = pPage->pDirtyPrev;
000171      }
000172    
000173      if( pPage->pDirtyNext ){
000174        pPage->pDirtyNext->pDirtyPrev = pPage->pDirtyPrev;
000175      }else{
000176        assert( pPage==p->pDirtyTail );
000177        p->pDirtyTail = pPage->pDirtyPrev;
000178      }
000179      if( pPage->pDirtyPrev ){
000180        pPage->pDirtyPrev->pDirtyNext = pPage->pDirtyNext;
000181      }else{
000182        /* If there are now no dirty pages in the cache, set eCreate to 2. 
000183        ** This is an optimization that allows sqlite3PcacheFetch() to skip
000184        ** searching for a dirty page to eject from the cache when it might
000185        ** otherwise have to.  */
000186        assert( pPage==p->pDirty );
000187        p->pDirty = pPage->pDirtyNext;
000188        assert( p->bPurgeable || p->eCreate==2 );
000189        if( p->pDirty==0 ){         /*OPTIMIZATION-IF-TRUE*/
000190          assert( p->bPurgeable==0 || p->eCreate==1 );
000191          p->eCreate = 2;
000192        }
000193      }
000194      pPage->pDirtyNext = 0;
000195      pPage->pDirtyPrev = 0;
000196    }
000197    if( addRemove & PCACHE_DIRTYLIST_ADD ){
000198      assert( pPage->pDirtyNext==0 && pPage->pDirtyPrev==0 && p->pDirty!=pPage );
000199    
000200      pPage->pDirtyNext = p->pDirty;
000201      if( pPage->pDirtyNext ){
000202        assert( pPage->pDirtyNext->pDirtyPrev==0 );
000203        pPage->pDirtyNext->pDirtyPrev = pPage;
000204      }else{
000205        p->pDirtyTail = pPage;
000206        if( p->bPurgeable ){
000207          assert( p->eCreate==2 );
000208          p->eCreate = 1;
000209        }
000210      }
000211      p->pDirty = pPage;
000212  
000213      /* If pSynced is NULL and this page has a clear NEED_SYNC flag, set
000214      ** pSynced to point to it. Checking the NEED_SYNC flag is an 
000215      ** optimization, as if pSynced points to a page with the NEED_SYNC
000216      ** flag set sqlite3PcacheFetchStress() searches through all newer 
000217      ** entries of the dirty-list for a page with NEED_SYNC clear anyway.  */
000218      if( !p->pSynced 
000219       && 0==(pPage->flags&PGHDR_NEED_SYNC)   /*OPTIMIZATION-IF-FALSE*/
000220      ){
000221        p->pSynced = pPage;
000222      }
000223    }
000224    pcacheDump(p);
000225  }
000226  
000227  /*
000228  ** Wrapper around the pluggable caches xUnpin method. If the cache is
000229  ** being used for an in-memory database, this function is a no-op.
000230  */
000231  static void pcacheUnpin(PgHdr *p){
000232    if( p->pCache->bPurgeable ){
000233      pcacheTrace(("%p.UNPIN %d\n", p->pCache, p->pgno));
000234      sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 0);
000235      pcacheDump(p->pCache);
000236    }
000237  }
000238  
000239  /*
000240  ** Compute the number of pages of cache requested.   p->szCache is the
000241  ** cache size requested by the "PRAGMA cache_size" statement.
000242  */
000243  static int numberOfCachePages(PCache *p){
000244    if( p->szCache>=0 ){
000245      /* IMPLEMENTATION-OF: R-42059-47211 If the argument N is positive then the
000246      ** suggested cache size is set to N. */
000247      return p->szCache;
000248    }else{
000249      /* IMPLEMENTATION-OF: R-61436-13639 If the argument N is negative, then
000250      ** the number of cache pages is adjusted to use approximately abs(N*1024)
000251      ** bytes of memory. */
000252      return (int)((-1024*(i64)p->szCache)/(p->szPage+p->szExtra));
000253    }
000254  }
000255  
000256  /*************************************************** General Interfaces ******
000257  **
000258  ** Initialize and shutdown the page cache subsystem. Neither of these 
000259  ** functions are threadsafe.
000260  */
000261  int sqlite3PcacheInitialize(void){
000262    if( sqlite3GlobalConfig.pcache2.xInit==0 ){
000263      /* IMPLEMENTATION-OF: R-26801-64137 If the xInit() method is NULL, then the
000264      ** built-in default page cache is used instead of the application defined
000265      ** page cache. */
000266      sqlite3PCacheSetDefault();
000267    }
000268    return sqlite3GlobalConfig.pcache2.xInit(sqlite3GlobalConfig.pcache2.pArg);
000269  }
000270  void sqlite3PcacheShutdown(void){
000271    if( sqlite3GlobalConfig.pcache2.xShutdown ){
000272      /* IMPLEMENTATION-OF: R-26000-56589 The xShutdown() method may be NULL. */
000273      sqlite3GlobalConfig.pcache2.xShutdown(sqlite3GlobalConfig.pcache2.pArg);
000274    }
000275  }
000276  
000277  /*
000278  ** Return the size in bytes of a PCache object.
000279  */
000280  int sqlite3PcacheSize(void){ return sizeof(PCache); }
000281  
000282  /*
000283  ** Create a new PCache object. Storage space to hold the object
000284  ** has already been allocated and is passed in as the p pointer. 
000285  ** The caller discovers how much space needs to be allocated by 
000286  ** calling sqlite3PcacheSize().
000287  **
000288  ** szExtra is some extra space allocated for each page.  The first
000289  ** 8 bytes of the extra space will be zeroed as the page is allocated,
000290  ** but remaining content will be uninitialized.  Though it is opaque
000291  ** to this module, the extra space really ends up being the MemPage
000292  ** structure in the pager.
000293  */
000294  int sqlite3PcacheOpen(
000295    int szPage,                  /* Size of every page */
000296    int szExtra,                 /* Extra space associated with each page */
000297    int bPurgeable,              /* True if pages are on backing store */
000298    int (*xStress)(void*,PgHdr*),/* Call to try to make pages clean */
000299    void *pStress,               /* Argument to xStress */
000300    PCache *p                    /* Preallocated space for the PCache */
000301  ){
000302    memset(p, 0, sizeof(PCache));
000303    p->szPage = 1;
000304    p->szExtra = szExtra;
000305    assert( szExtra>=8 );  /* First 8 bytes will be zeroed */
000306    p->bPurgeable = bPurgeable;
000307    p->eCreate = 2;
000308    p->xStress = xStress;
000309    p->pStress = pStress;
000310    p->szCache = 100;
000311    p->szSpill = 1;
000312    pcacheTrace(("%p.OPEN szPage %d bPurgeable %d\n",p,szPage,bPurgeable));
000313    return sqlite3PcacheSetPageSize(p, szPage);
000314  }
000315  
000316  /*
000317  ** Change the page size for PCache object. The caller must ensure that there
000318  ** are no outstanding page references when this function is called.
000319  */
000320  int sqlite3PcacheSetPageSize(PCache *pCache, int szPage){
000321    assert( pCache->nRefSum==0 && pCache->pDirty==0 );
000322    if( pCache->szPage ){
000323      sqlite3_pcache *pNew;
000324      pNew = sqlite3GlobalConfig.pcache2.xCreate(
000325                  szPage, pCache->szExtra + ROUND8(sizeof(PgHdr)),
000326                  pCache->bPurgeable
000327      );
000328      if( pNew==0 ) return SQLITE_NOMEM_BKPT;
000329      sqlite3GlobalConfig.pcache2.xCachesize(pNew, numberOfCachePages(pCache));
000330      if( pCache->pCache ){
000331        sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
000332      }
000333      pCache->pCache = pNew;
000334      pCache->szPage = szPage;
000335      pcacheTrace(("%p.PAGESIZE %d\n",pCache,szPage));
000336    }
000337    return SQLITE_OK;
000338  }
000339  
000340  /*
000341  ** Try to obtain a page from the cache.
000342  **
000343  ** This routine returns a pointer to an sqlite3_pcache_page object if
000344  ** such an object is already in cache, or if a new one is created.
000345  ** This routine returns a NULL pointer if the object was not in cache
000346  ** and could not be created.
000347  **
000348  ** The createFlags should be 0 to check for existing pages and should
000349  ** be 3 (not 1, but 3) to try to create a new page.
000350  **
000351  ** If the createFlag is 0, then NULL is always returned if the page
000352  ** is not already in the cache.  If createFlag is 1, then a new page
000353  ** is created only if that can be done without spilling dirty pages
000354  ** and without exceeding the cache size limit.
000355  **
000356  ** The caller needs to invoke sqlite3PcacheFetchFinish() to properly
000357  ** initialize the sqlite3_pcache_page object and convert it into a
000358  ** PgHdr object.  The sqlite3PcacheFetch() and sqlite3PcacheFetchFinish()
000359  ** routines are split this way for performance reasons. When separated
000360  ** they can both (usually) operate without having to push values to
000361  ** the stack on entry and pop them back off on exit, which saves a
000362  ** lot of pushing and popping.
000363  */
000364  sqlite3_pcache_page *sqlite3PcacheFetch(
000365    PCache *pCache,       /* Obtain the page from this cache */
000366    Pgno pgno,            /* Page number to obtain */
000367    int createFlag        /* If true, create page if it does not exist already */
000368  ){
000369    int eCreate;
000370    sqlite3_pcache_page *pRes;
000371  
000372    assert( pCache!=0 );
000373    assert( pCache->pCache!=0 );
000374    assert( createFlag==3 || createFlag==0 );
000375    assert( pCache->eCreate==((pCache->bPurgeable && pCache->pDirty) ? 1 : 2) );
000376  
000377    /* eCreate defines what to do if the page does not exist.
000378    **    0     Do not allocate a new page.  (createFlag==0)
000379    **    1     Allocate a new page if doing so is inexpensive.
000380    **          (createFlag==1 AND bPurgeable AND pDirty)
000381    **    2     Allocate a new page even it doing so is difficult.
000382    **          (createFlag==1 AND !(bPurgeable AND pDirty)
000383    */
000384    eCreate = createFlag & pCache->eCreate;
000385    assert( eCreate==0 || eCreate==1 || eCreate==2 );
000386    assert( createFlag==0 || pCache->eCreate==eCreate );
000387    assert( createFlag==0 || eCreate==1+(!pCache->bPurgeable||!pCache->pDirty) );
000388    pRes = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, eCreate);
000389    pcacheTrace(("%p.FETCH %d%s (result: %p)\n",pCache,pgno,
000390                 createFlag?" create":"",pRes));
000391    return pRes;
000392  }
000393  
000394  /*
000395  ** If the sqlite3PcacheFetch() routine is unable to allocate a new
000396  ** page because no clean pages are available for reuse and the cache
000397  ** size limit has been reached, then this routine can be invoked to 
000398  ** try harder to allocate a page.  This routine might invoke the stress
000399  ** callback to spill dirty pages to the journal.  It will then try to
000400  ** allocate the new page and will only fail to allocate a new page on
000401  ** an OOM error.
000402  **
000403  ** This routine should be invoked only after sqlite3PcacheFetch() fails.
000404  */
000405  int sqlite3PcacheFetchStress(
000406    PCache *pCache,                 /* Obtain the page from this cache */
000407    Pgno pgno,                      /* Page number to obtain */
000408    sqlite3_pcache_page **ppPage    /* Write result here */
000409  ){
000410    PgHdr *pPg;
000411    if( pCache->eCreate==2 ) return 0;
000412  
000413    if( sqlite3PcachePagecount(pCache)>pCache->szSpill ){
000414      /* Find a dirty page to write-out and recycle. First try to find a 
000415      ** page that does not require a journal-sync (one with PGHDR_NEED_SYNC
000416      ** cleared), but if that is not possible settle for any other 
000417      ** unreferenced dirty page.
000418      **
000419      ** If the LRU page in the dirty list that has a clear PGHDR_NEED_SYNC
000420      ** flag is currently referenced, then the following may leave pSynced
000421      ** set incorrectly (pointing to other than the LRU page with NEED_SYNC
000422      ** cleared). This is Ok, as pSynced is just an optimization.  */
000423      for(pPg=pCache->pSynced; 
000424          pPg && (pPg->nRef || (pPg->flags&PGHDR_NEED_SYNC)); 
000425          pPg=pPg->pDirtyPrev
000426      );
000427      pCache->pSynced = pPg;
000428      if( !pPg ){
000429        for(pPg=pCache->pDirtyTail; pPg && pPg->nRef; pPg=pPg->pDirtyPrev);
000430      }
000431      if( pPg ){
000432        int rc;
000433  #ifdef SQLITE_LOG_CACHE_SPILL
000434        sqlite3_log(SQLITE_FULL, 
000435                    "spill page %d making room for %d - cache used: %d/%d",
000436                    pPg->pgno, pgno,
000437                    sqlite3GlobalConfig.pcache.xPagecount(pCache->pCache),
000438                  numberOfCachePages(pCache));
000439  #endif
000440        pcacheTrace(("%p.SPILL %d\n",pCache,pPg->pgno));
000441        rc = pCache->xStress(pCache->pStress, pPg);
000442        pcacheDump(pCache);
000443        if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
000444          return rc;
000445        }
000446      }
000447    }
000448    *ppPage = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, 2);
000449    return *ppPage==0 ? SQLITE_NOMEM_BKPT : SQLITE_OK;
000450  }
000451  
000452  /*
000453  ** This is a helper routine for sqlite3PcacheFetchFinish()
000454  **
000455  ** In the uncommon case where the page being fetched has not been
000456  ** initialized, this routine is invoked to do the initialization.
000457  ** This routine is broken out into a separate function since it
000458  ** requires extra stack manipulation that can be avoided in the common
000459  ** case.
000460  */
000461  static SQLITE_NOINLINE PgHdr *pcacheFetchFinishWithInit(
000462    PCache *pCache,             /* Obtain the page from this cache */
000463    Pgno pgno,                  /* Page number obtained */
000464    sqlite3_pcache_page *pPage  /* Page obtained by prior PcacheFetch() call */
000465  ){
000466    PgHdr *pPgHdr;
000467    assert( pPage!=0 );
000468    pPgHdr = (PgHdr*)pPage->pExtra;
000469    assert( pPgHdr->pPage==0 );
000470    memset(&pPgHdr->pDirty, 0, sizeof(PgHdr) - offsetof(PgHdr,pDirty));
000471    pPgHdr->pPage = pPage;
000472    pPgHdr->pData = pPage->pBuf;
000473    pPgHdr->pExtra = (void *)&pPgHdr[1];
000474    memset(pPgHdr->pExtra, 0, 8);
000475    pPgHdr->pCache = pCache;
000476    pPgHdr->pgno = pgno;
000477    pPgHdr->flags = PGHDR_CLEAN;
000478    return sqlite3PcacheFetchFinish(pCache,pgno,pPage);
000479  }
000480  
000481  /*
000482  ** This routine converts the sqlite3_pcache_page object returned by
000483  ** sqlite3PcacheFetch() into an initialized PgHdr object.  This routine
000484  ** must be called after sqlite3PcacheFetch() in order to get a usable
000485  ** result.
000486  */
000487  PgHdr *sqlite3PcacheFetchFinish(
000488    PCache *pCache,             /* Obtain the page from this cache */
000489    Pgno pgno,                  /* Page number obtained */
000490    sqlite3_pcache_page *pPage  /* Page obtained by prior PcacheFetch() call */
000491  ){
000492    PgHdr *pPgHdr;
000493  
000494    assert( pPage!=0 );
000495    pPgHdr = (PgHdr *)pPage->pExtra;
000496  
000497    if( !pPgHdr->pPage ){
000498      return pcacheFetchFinishWithInit(pCache, pgno, pPage);
000499    }
000500    pCache->nRefSum++;
000501    pPgHdr->nRef++;
000502    assert( sqlite3PcachePageSanity(pPgHdr) );
000503    return pPgHdr;
000504  }
000505  
000506  /*
000507  ** Decrement the reference count on a page. If the page is clean and the
000508  ** reference count drops to 0, then it is made eligible for recycling.
000509  */
000510  void SQLITE_NOINLINE sqlite3PcacheRelease(PgHdr *p){
000511    assert( p->nRef>0 );
000512    p->pCache->nRefSum--;
000513    if( (--p->nRef)==0 ){
000514      if( p->flags&PGHDR_CLEAN ){
000515        pcacheUnpin(p);
000516      }else if( p->pDirtyPrev!=0 ){ /*OPTIMIZATION-IF-FALSE*/
000517        /* Move the page to the head of the dirty list. If p->pDirtyPrev==0,
000518        ** then page p is already at the head of the dirty list and the
000519        ** following call would be a no-op. Hence the OPTIMIZATION-IF-FALSE
000520        ** tag above.  */
000521        pcacheManageDirtyList(p, PCACHE_DIRTYLIST_FRONT);
000522      }
000523    }
000524  }
000525  
000526  /*
000527  ** Increase the reference count of a supplied page by 1.
000528  */
000529  void sqlite3PcacheRef(PgHdr *p){
000530    assert(p->nRef>0);
000531    assert( sqlite3PcachePageSanity(p) );
000532    p->nRef++;
000533    p->pCache->nRefSum++;
000534  }
000535  
000536  /*
000537  ** Drop a page from the cache. There must be exactly one reference to the
000538  ** page. This function deletes that reference, so after it returns the
000539  ** page pointed to by p is invalid.
000540  */
000541  void sqlite3PcacheDrop(PgHdr *p){
000542    assert( p->nRef==1 );
000543    assert( sqlite3PcachePageSanity(p) );
000544    if( p->flags&PGHDR_DIRTY ){
000545      pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE);
000546    }
000547    p->pCache->nRefSum--;
000548    sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 1);
000549  }
000550  
000551  /*
000552  ** Make sure the page is marked as dirty. If it isn't dirty already,
000553  ** make it so.
000554  */
000555  void sqlite3PcacheMakeDirty(PgHdr *p){
000556    assert( p->nRef>0 );
000557    assert( sqlite3PcachePageSanity(p) );
000558    if( p->flags & (PGHDR_CLEAN|PGHDR_DONT_WRITE) ){    /*OPTIMIZATION-IF-FALSE*/
000559      p->flags &= ~PGHDR_DONT_WRITE;
000560      if( p->flags & PGHDR_CLEAN ){
000561        p->flags ^= (PGHDR_DIRTY|PGHDR_CLEAN);
000562        pcacheTrace(("%p.DIRTY %d\n",p->pCache,p->pgno));
000563        assert( (p->flags & (PGHDR_DIRTY|PGHDR_CLEAN))==PGHDR_DIRTY );
000564        pcacheManageDirtyList(p, PCACHE_DIRTYLIST_ADD);
000565      }
000566      assert( sqlite3PcachePageSanity(p) );
000567    }
000568  }
000569  
000570  /*
000571  ** Make sure the page is marked as clean. If it isn't clean already,
000572  ** make it so.
000573  */
000574  void sqlite3PcacheMakeClean(PgHdr *p){
000575    assert( sqlite3PcachePageSanity(p) );
000576    if( ALWAYS((p->flags & PGHDR_DIRTY)!=0) ){
000577      assert( (p->flags & PGHDR_CLEAN)==0 );
000578      pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE);
000579      p->flags &= ~(PGHDR_DIRTY|PGHDR_NEED_SYNC|PGHDR_WRITEABLE);
000580      p->flags |= PGHDR_CLEAN;
000581      pcacheTrace(("%p.CLEAN %d\n",p->pCache,p->pgno));
000582      assert( sqlite3PcachePageSanity(p) );
000583      if( p->nRef==0 ){
000584        pcacheUnpin(p);
000585      }
000586    }
000587  }
000588  
000589  /*
000590  ** Make every page in the cache clean.
000591  */
000592  void sqlite3PcacheCleanAll(PCache *pCache){
000593    PgHdr *p;
000594    pcacheTrace(("%p.CLEAN-ALL\n",pCache));
000595    while( (p = pCache->pDirty)!=0 ){
000596      sqlite3PcacheMakeClean(p);
000597    }
000598  }
000599  
000600  /*
000601  ** Clear the PGHDR_NEED_SYNC and PGHDR_WRITEABLE flag from all dirty pages.
000602  */
000603  void sqlite3PcacheClearWritable(PCache *pCache){
000604    PgHdr *p;
000605    pcacheTrace(("%p.CLEAR-WRITEABLE\n",pCache));
000606    for(p=pCache->pDirty; p; p=p->pDirtyNext){
000607      p->flags &= ~(PGHDR_NEED_SYNC|PGHDR_WRITEABLE);
000608    }
000609    pCache->pSynced = pCache->pDirtyTail;
000610  }
000611  
000612  /*
000613  ** Clear the PGHDR_NEED_SYNC flag from all dirty pages.
000614  */
000615  void sqlite3PcacheClearSyncFlags(PCache *pCache){
000616    PgHdr *p;
000617    for(p=pCache->pDirty; p; p=p->pDirtyNext){
000618      p->flags &= ~PGHDR_NEED_SYNC;
000619    }
000620    pCache->pSynced = pCache->pDirtyTail;
000621  }
000622  
000623  /*
000624  ** Change the page number of page p to newPgno. 
000625  */
000626  void sqlite3PcacheMove(PgHdr *p, Pgno newPgno){
000627    PCache *pCache = p->pCache;
000628    assert( p->nRef>0 );
000629    assert( newPgno>0 );
000630    assert( sqlite3PcachePageSanity(p) );
000631    pcacheTrace(("%p.MOVE %d -> %d\n",pCache,p->pgno,newPgno));
000632    sqlite3GlobalConfig.pcache2.xRekey(pCache->pCache, p->pPage, p->pgno,newPgno);
000633    p->pgno = newPgno;
000634    if( (p->flags&PGHDR_DIRTY) && (p->flags&PGHDR_NEED_SYNC) ){
000635      pcacheManageDirtyList(p, PCACHE_DIRTYLIST_FRONT);
000636    }
000637  }
000638  
000639  /*
000640  ** Drop every cache entry whose page number is greater than "pgno". The
000641  ** caller must ensure that there are no outstanding references to any pages
000642  ** other than page 1 with a page number greater than pgno.
000643  **
000644  ** If there is a reference to page 1 and the pgno parameter passed to this
000645  ** function is 0, then the data area associated with page 1 is zeroed, but
000646  ** the page object is not dropped.
000647  */
000648  void sqlite3PcacheTruncate(PCache *pCache, Pgno pgno){
000649    if( pCache->pCache ){
000650      PgHdr *p;
000651      PgHdr *pNext;
000652      pcacheTrace(("%p.TRUNCATE %d\n",pCache,pgno));
000653      for(p=pCache->pDirty; p; p=pNext){
000654        pNext = p->pDirtyNext;
000655        /* This routine never gets call with a positive pgno except right
000656        ** after sqlite3PcacheCleanAll().  So if there are dirty pages,
000657        ** it must be that pgno==0.
000658        */
000659        assert( p->pgno>0 );
000660        if( p->pgno>pgno ){
000661          assert( p->flags&PGHDR_DIRTY );
000662          sqlite3PcacheMakeClean(p);
000663        }
000664      }
000665      if( pgno==0 && pCache->nRefSum ){
000666        sqlite3_pcache_page *pPage1;
000667        pPage1 = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache,1,0);
000668        if( ALWAYS(pPage1) ){  /* Page 1 is always available in cache, because
000669                               ** pCache->nRefSum>0 */
000670          memset(pPage1->pBuf, 0, pCache->szPage);
000671          pgno = 1;
000672        }
000673      }
000674      sqlite3GlobalConfig.pcache2.xTruncate(pCache->pCache, pgno+1);
000675    }
000676  }
000677  
000678  /*
000679  ** Close a cache.
000680  */
000681  void sqlite3PcacheClose(PCache *pCache){
000682    assert( pCache->pCache!=0 );
000683    pcacheTrace(("%p.CLOSE\n",pCache));
000684    sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
000685  }
000686  
000687  /* 
000688  ** Discard the contents of the cache.
000689  */
000690  void sqlite3PcacheClear(PCache *pCache){
000691    sqlite3PcacheTruncate(pCache, 0);
000692  }
000693  
000694  /*
000695  ** Merge two lists of pages connected by pDirty and in pgno order.
000696  ** Do not bother fixing the pDirtyPrev pointers.
000697  */
000698  static PgHdr *pcacheMergeDirtyList(PgHdr *pA, PgHdr *pB){
000699    PgHdr result, *pTail;
000700    pTail = &result;
000701    assert( pA!=0 && pB!=0 );
000702    for(;;){
000703      if( pA->pgno<pB->pgno ){
000704        pTail->pDirty = pA;
000705        pTail = pA;
000706        pA = pA->pDirty;
000707        if( pA==0 ){
000708          pTail->pDirty = pB;
000709          break;
000710        }
000711      }else{
000712        pTail->pDirty = pB;
000713        pTail = pB;
000714        pB = pB->pDirty;
000715        if( pB==0 ){
000716          pTail->pDirty = pA;
000717          break;
000718        }
000719      }
000720    }
000721    return result.pDirty;
000722  }
000723  
000724  /*
000725  ** Sort the list of pages in accending order by pgno.  Pages are
000726  ** connected by pDirty pointers.  The pDirtyPrev pointers are
000727  ** corrupted by this sort.
000728  **
000729  ** Since there cannot be more than 2^31 distinct pages in a database,
000730  ** there cannot be more than 31 buckets required by the merge sorter.
000731  ** One extra bucket is added to catch overflow in case something
000732  ** ever changes to make the previous sentence incorrect.
000733  */
000734  #define N_SORT_BUCKET  32
000735  static PgHdr *pcacheSortDirtyList(PgHdr *pIn){
000736    PgHdr *a[N_SORT_BUCKET], *p;
000737    int i;
000738    memset(a, 0, sizeof(a));
000739    while( pIn ){
000740      p = pIn;
000741      pIn = p->pDirty;
000742      p->pDirty = 0;
000743      for(i=0; ALWAYS(i<N_SORT_BUCKET-1); i++){
000744        if( a[i]==0 ){
000745          a[i] = p;
000746          break;
000747        }else{
000748          p = pcacheMergeDirtyList(a[i], p);
000749          a[i] = 0;
000750        }
000751      }
000752      if( NEVER(i==N_SORT_BUCKET-1) ){
000753        /* To get here, there need to be 2^(N_SORT_BUCKET) elements in
000754        ** the input list.  But that is impossible.
000755        */
000756        a[i] = pcacheMergeDirtyList(a[i], p);
000757      }
000758    }
000759    p = a[0];
000760    for(i=1; i<N_SORT_BUCKET; i++){
000761      if( a[i]==0 ) continue;
000762      p = p ? pcacheMergeDirtyList(p, a[i]) : a[i];
000763    }
000764    return p;
000765  }
000766  
000767  /*
000768  ** Return a list of all dirty pages in the cache, sorted by page number.
000769  */
000770  PgHdr *sqlite3PcacheDirtyList(PCache *pCache){
000771    PgHdr *p;
000772    for(p=pCache->pDirty; p; p=p->pDirtyNext){
000773      p->pDirty = p->pDirtyNext;
000774    }
000775    return pcacheSortDirtyList(pCache->pDirty);
000776  }
000777  
000778  /* 
000779  ** Return the total number of references to all pages held by the cache.
000780  **
000781  ** This is not the total number of pages referenced, but the sum of the
000782  ** reference count for all pages.
000783  */
000784  int sqlite3PcacheRefCount(PCache *pCache){
000785    return pCache->nRefSum;
000786  }
000787  
000788  /*
000789  ** Return the number of references to the page supplied as an argument.
000790  */
000791  int sqlite3PcachePageRefcount(PgHdr *p){
000792    return p->nRef;
000793  }
000794  
000795  /* 
000796  ** Return the total number of pages in the cache.
000797  */
000798  int sqlite3PcachePagecount(PCache *pCache){
000799    assert( pCache->pCache!=0 );
000800    return sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache);
000801  }
000802  
000803  #ifdef SQLITE_TEST
000804  /*
000805  ** Get the suggested cache-size value.
000806  */
000807  int sqlite3PcacheGetCachesize(PCache *pCache){
000808    return numberOfCachePages(pCache);
000809  }
000810  #endif
000811  
000812  /*
000813  ** Set the suggested cache-size value.
000814  */
000815  void sqlite3PcacheSetCachesize(PCache *pCache, int mxPage){
000816    assert( pCache->pCache!=0 );
000817    pCache->szCache = mxPage;
000818    sqlite3GlobalConfig.pcache2.xCachesize(pCache->pCache,
000819                                           numberOfCachePages(pCache));
000820  }
000821  
000822  /*
000823  ** Set the suggested cache-spill value.  Make no changes if if the
000824  ** argument is zero.  Return the effective cache-spill size, which will
000825  ** be the larger of the szSpill and szCache.
000826  */
000827  int sqlite3PcacheSetSpillsize(PCache *p, int mxPage){
000828    int res;
000829    assert( p->pCache!=0 );
000830    if( mxPage ){
000831      if( mxPage<0 ){
000832        mxPage = (int)((-1024*(i64)mxPage)/(p->szPage+p->szExtra));
000833      }
000834      p->szSpill = mxPage;
000835    }
000836    res = numberOfCachePages(p);
000837    if( res<p->szSpill ) res = p->szSpill; 
000838    return res;
000839  }
000840  
000841  /*
000842  ** Free up as much memory as possible from the page cache.
000843  */
000844  void sqlite3PcacheShrink(PCache *pCache){
000845    assert( pCache->pCache!=0 );
000846    sqlite3GlobalConfig.pcache2.xShrink(pCache->pCache);
000847  }
000848  
000849  /*
000850  ** Return the size of the header added by this middleware layer
000851  ** in the page-cache hierarchy.
000852  */
000853  int sqlite3HeaderSizePcache(void){ return ROUND8(sizeof(PgHdr)); }
000854  
000855  /*
000856  ** Return the number of dirty pages currently in the cache, as a percentage
000857  ** of the configured cache size.
000858  */
000859  int sqlite3PCachePercentDirty(PCache *pCache){
000860    PgHdr *pDirty;
000861    int nDirty = 0;
000862    int nCache = numberOfCachePages(pCache);
000863    for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext) nDirty++;
000864    return nCache ? (int)(((i64)nDirty * 100) / nCache) : 0;
000865  }
000866  
000867  #if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)
000868  /*
000869  ** For all dirty pages currently in the cache, invoke the specified
000870  ** callback. This is only used if the SQLITE_CHECK_PAGES macro is
000871  ** defined.
000872  */
000873  void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *)){
000874    PgHdr *pDirty;
000875    for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext){
000876      xIter(pDirty);
000877    }
000878  }
000879  #endif