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 so 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  #ifdef 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    }
000195    if( addRemove & PCACHE_DIRTYLIST_ADD ){
000196      pPage->pDirtyPrev = 0;
000197      pPage->pDirtyNext = p->pDirty;
000198      if( pPage->pDirtyNext ){
000199        assert( pPage->pDirtyNext->pDirtyPrev==0 );
000200        pPage->pDirtyNext->pDirtyPrev = pPage;
000201      }else{
000202        p->pDirtyTail = pPage;
000203        if( p->bPurgeable ){
000204          assert( p->eCreate==2 );
000205          p->eCreate = 1;
000206        }
000207      }
000208      p->pDirty = pPage;
000209  
000210      /* If pSynced is NULL and this page has a clear NEED_SYNC flag, set
000211      ** pSynced to point to it. Checking the NEED_SYNC flag is an 
000212      ** optimization, as if pSynced points to a page with the NEED_SYNC
000213      ** flag set sqlite3PcacheFetchStress() searches through all newer 
000214      ** entries of the dirty-list for a page with NEED_SYNC clear anyway.  */
000215      if( !p->pSynced 
000216       && 0==(pPage->flags&PGHDR_NEED_SYNC)   /*OPTIMIZATION-IF-FALSE*/
000217      ){
000218        p->pSynced = pPage;
000219      }
000220    }
000221    pcacheDump(p);
000222  }
000223  
000224  /*
000225  ** Wrapper around the pluggable caches xUnpin method. If the cache is
000226  ** being used for an in-memory database, this function is a no-op.
000227  */
000228  static void pcacheUnpin(PgHdr *p){
000229    if( p->pCache->bPurgeable ){
000230      pcacheTrace(("%p.UNPIN %d\n", p->pCache, p->pgno));
000231      sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 0);
000232      pcacheDump(p->pCache);
000233    }
000234  }
000235  
000236  /*
000237  ** Compute the number of pages of cache requested.   p->szCache is the
000238  ** cache size requested by the "PRAGMA cache_size" statement.
000239  */
000240  static int numberOfCachePages(PCache *p){
000241    if( p->szCache>=0 ){
000242      /* IMPLEMENTATION-OF: R-42059-47211 If the argument N is positive then the
000243      ** suggested cache size is set to N. */
000244      return p->szCache;
000245    }else{
000246      /* IMPLEMANTATION-OF: R-59858-46238 If the argument N is negative, then the
000247      ** number of cache pages is adjusted to be a number of pages that would
000248      ** use approximately abs(N*1024) bytes of memory based on the current
000249      ** page size. */
000250      return (int)((-1024*(i64)p->szCache)/(p->szPage+p->szExtra));
000251    }
000252  }
000253  
000254  /*************************************************** General Interfaces ******
000255  **
000256  ** Initialize and shutdown the page cache subsystem. Neither of these 
000257  ** functions are threadsafe.
000258  */
000259  int sqlite3PcacheInitialize(void){
000260    if( sqlite3GlobalConfig.pcache2.xInit==0 ){
000261      /* IMPLEMENTATION-OF: R-26801-64137 If the xInit() method is NULL, then the
000262      ** built-in default page cache is used instead of the application defined
000263      ** page cache. */
000264      sqlite3PCacheSetDefault();
000265      assert( sqlite3GlobalConfig.pcache2.xInit!=0 );
000266    }
000267    return sqlite3GlobalConfig.pcache2.xInit(sqlite3GlobalConfig.pcache2.pArg);
000268  }
000269  void sqlite3PcacheShutdown(void){
000270    if( sqlite3GlobalConfig.pcache2.xShutdown ){
000271      /* IMPLEMENTATION-OF: R-26000-56589 The xShutdown() method may be NULL. */
000272      sqlite3GlobalConfig.pcache2.xShutdown(sqlite3GlobalConfig.pcache2.pArg);
000273    }
000274  }
000275  
000276  /*
000277  ** Return the size in bytes of a PCache object.
000278  */
000279  int sqlite3PcacheSize(void){ return sizeof(PCache); }
000280  
000281  /*
000282  ** Create a new PCache object. Storage space to hold the object
000283  ** has already been allocated and is passed in as the p pointer. 
000284  ** The caller discovers how much space needs to be allocated by 
000285  ** calling sqlite3PcacheSize().
000286  **
000287  ** szExtra is some extra space allocated for each page.  The first
000288  ** 8 bytes of the extra space will be zeroed as the page is allocated,
000289  ** but remaining content will be uninitialized.  Though it is opaque
000290  ** to this module, the extra space really ends up being the MemPage
000291  ** structure in the pager.
000292  */
000293  int sqlite3PcacheOpen(
000294    int szPage,                  /* Size of every page */
000295    int szExtra,                 /* Extra space associated with each page */
000296    int bPurgeable,              /* True if pages are on backing store */
000297    int (*xStress)(void*,PgHdr*),/* Call to try to make pages clean */
000298    void *pStress,               /* Argument to xStress */
000299    PCache *p                    /* Preallocated space for the PCache */
000300  ){
000301    memset(p, 0, sizeof(PCache));
000302    p->szPage = 1;
000303    p->szExtra = szExtra;
000304    assert( szExtra>=8 );  /* First 8 bytes will be zeroed */
000305    p->bPurgeable = bPurgeable;
000306    p->eCreate = 2;
000307    p->xStress = xStress;
000308    p->pStress = pStress;
000309    p->szCache = 100;
000310    p->szSpill = 1;
000311    pcacheTrace(("%p.OPEN szPage %d bPurgeable %d\n",p,szPage,bPurgeable));
000312    return sqlite3PcacheSetPageSize(p, szPage);
000313  }
000314  
000315  /*
000316  ** Change the page size for PCache object. The caller must ensure that there
000317  ** are no outstanding page references when this function is called.
000318  */
000319  int sqlite3PcacheSetPageSize(PCache *pCache, int szPage){
000320    assert( pCache->nRefSum==0 && pCache->pDirty==0 );
000321    if( pCache->szPage ){
000322      sqlite3_pcache *pNew;
000323      pNew = sqlite3GlobalConfig.pcache2.xCreate(
000324                  szPage, pCache->szExtra + ROUND8(sizeof(PgHdr)),
000325                  pCache->bPurgeable
000326      );
000327      if( pNew==0 ) return SQLITE_NOMEM_BKPT;
000328      sqlite3GlobalConfig.pcache2.xCachesize(pNew, numberOfCachePages(pCache));
000329      if( pCache->pCache ){
000330        sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
000331      }
000332      pCache->pCache = pNew;
000333      pCache->szPage = szPage;
000334      pcacheTrace(("%p.PAGESIZE %d\n",pCache,szPage));
000335    }
000336    return SQLITE_OK;
000337  }
000338  
000339  /*
000340  ** Try to obtain a page from the cache.
000341  **
000342  ** This routine returns a pointer to an sqlite3_pcache_page object if
000343  ** such an object is already in cache, or if a new one is created.
000344  ** This routine returns a NULL pointer if the object was not in cache
000345  ** and could not be created.
000346  **
000347  ** The createFlags should be 0 to check for existing pages and should
000348  ** be 3 (not 1, but 3) to try to create a new page.
000349  **
000350  ** If the createFlag is 0, then NULL is always returned if the page
000351  ** is not already in the cache.  If createFlag is 1, then a new page
000352  ** is created only if that can be done without spilling dirty pages
000353  ** and without exceeding the cache size limit.
000354  **
000355  ** The caller needs to invoke sqlite3PcacheFetchFinish() to properly
000356  ** initialize the sqlite3_pcache_page object and convert it into a
000357  ** PgHdr object.  The sqlite3PcacheFetch() and sqlite3PcacheFetchFinish()
000358  ** routines are split this way for performance reasons. When separated
000359  ** they can both (usually) operate without having to push values to
000360  ** the stack on entry and pop them back off on exit, which saves a
000361  ** lot of pushing and popping.
000362  */
000363  sqlite3_pcache_page *sqlite3PcacheFetch(
000364    PCache *pCache,       /* Obtain the page from this cache */
000365    Pgno pgno,            /* Page number to obtain */
000366    int createFlag        /* If true, create page if it does not exist already */
000367  ){
000368    int eCreate;
000369    sqlite3_pcache_page *pRes;
000370  
000371    assert( pCache!=0 );
000372    assert( pCache->pCache!=0 );
000373    assert( createFlag==3 || createFlag==0 );
000374    assert( pCache->eCreate==((pCache->bPurgeable && pCache->pDirty) ? 1 : 2) );
000375  
000376    /* eCreate defines what to do if the page does not exist.
000377    **    0     Do not allocate a new page.  (createFlag==0)
000378    **    1     Allocate a new page if doing so is inexpensive.
000379    **          (createFlag==1 AND bPurgeable AND pDirty)
000380    **    2     Allocate a new page even it doing so is difficult.
000381    **          (createFlag==1 AND !(bPurgeable AND pDirty)
000382    */
000383    eCreate = createFlag & pCache->eCreate;
000384    assert( eCreate==0 || eCreate==1 || eCreate==2 );
000385    assert( createFlag==0 || pCache->eCreate==eCreate );
000386    assert( createFlag==0 || eCreate==1+(!pCache->bPurgeable||!pCache->pDirty) );
000387    pRes = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, eCreate);
000388    pcacheTrace(("%p.FETCH %d%s (result: %p)\n",pCache,pgno,
000389                 createFlag?" create":"",pRes));
000390    return pRes;
000391  }
000392  
000393  /*
000394  ** If the sqlite3PcacheFetch() routine is unable to allocate a new
000395  ** page because no clean pages are available for reuse and the cache
000396  ** size limit has been reached, then this routine can be invoked to 
000397  ** try harder to allocate a page.  This routine might invoke the stress
000398  ** callback to spill dirty pages to the journal.  It will then try to
000399  ** allocate the new page and will only fail to allocate a new page on
000400  ** an OOM error.
000401  **
000402  ** This routine should be invoked only after sqlite3PcacheFetch() fails.
000403  */
000404  int sqlite3PcacheFetchStress(
000405    PCache *pCache,                 /* Obtain the page from this cache */
000406    Pgno pgno,                      /* Page number to obtain */
000407    sqlite3_pcache_page **ppPage    /* Write result here */
000408  ){
000409    PgHdr *pPg;
000410    if( pCache->eCreate==2 ) return 0;
000411  
000412    if( sqlite3PcachePagecount(pCache)>pCache->szSpill ){
000413      /* Find a dirty page to write-out and recycle. First try to find a 
000414      ** page that does not require a journal-sync (one with PGHDR_NEED_SYNC
000415      ** cleared), but if that is not possible settle for any other 
000416      ** unreferenced dirty page.
000417      **
000418      ** If the LRU page in the dirty list that has a clear PGHDR_NEED_SYNC
000419      ** flag is currently referenced, then the following may leave pSynced
000420      ** set incorrectly (pointing to other than the LRU page with NEED_SYNC
000421      ** cleared). This is Ok, as pSynced is just an optimization.  */
000422      for(pPg=pCache->pSynced; 
000423          pPg && (pPg->nRef || (pPg->flags&PGHDR_NEED_SYNC)); 
000424          pPg=pPg->pDirtyPrev
000425      );
000426      pCache->pSynced = pPg;
000427      if( !pPg ){
000428        for(pPg=pCache->pDirtyTail; pPg && pPg->nRef; pPg=pPg->pDirtyPrev);
000429      }
000430      if( pPg ){
000431        int rc;
000432  #ifdef SQLITE_LOG_CACHE_SPILL
000433        sqlite3_log(SQLITE_FULL, 
000434                    "spill page %d making room for %d - cache used: %d/%d",
000435                    pPg->pgno, pgno,
000436                    sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache),
000437                  numberOfCachePages(pCache));
000438  #endif
000439        pcacheTrace(("%p.SPILL %d\n",pCache,pPg->pgno));
000440        rc = pCache->xStress(pCache->pStress, pPg);
000441        pcacheDump(pCache);
000442        if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
000443          return rc;
000444        }
000445      }
000446    }
000447    *ppPage = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, 2);
000448    return *ppPage==0 ? SQLITE_NOMEM_BKPT : SQLITE_OK;
000449  }
000450  
000451  /*
000452  ** This is a helper routine for sqlite3PcacheFetchFinish()
000453  **
000454  ** In the uncommon case where the page being fetched has not been
000455  ** initialized, this routine is invoked to do the initialization.
000456  ** This routine is broken out into a separate function since it
000457  ** requires extra stack manipulation that can be avoided in the common
000458  ** case.
000459  */
000460  static SQLITE_NOINLINE PgHdr *pcacheFetchFinishWithInit(
000461    PCache *pCache,             /* Obtain the page from this cache */
000462    Pgno pgno,                  /* Page number obtained */
000463    sqlite3_pcache_page *pPage  /* Page obtained by prior PcacheFetch() call */
000464  ){
000465    PgHdr *pPgHdr;
000466    assert( pPage!=0 );
000467    pPgHdr = (PgHdr*)pPage->pExtra;
000468    assert( pPgHdr->pPage==0 );
000469    memset(&pPgHdr->pDirty, 0, sizeof(PgHdr) - offsetof(PgHdr,pDirty));
000470    pPgHdr->pPage = pPage;
000471    pPgHdr->pData = pPage->pBuf;
000472    pPgHdr->pExtra = (void *)&pPgHdr[1];
000473    memset(pPgHdr->pExtra, 0, 8);
000474    pPgHdr->pCache = pCache;
000475    pPgHdr->pgno = pgno;
000476    pPgHdr->flags = PGHDR_CLEAN;
000477    return sqlite3PcacheFetchFinish(pCache,pgno,pPage);
000478  }
000479  
000480  /*
000481  ** This routine converts the sqlite3_pcache_page object returned by
000482  ** sqlite3PcacheFetch() into an initialized PgHdr object.  This routine
000483  ** must be called after sqlite3PcacheFetch() in order to get a usable
000484  ** result.
000485  */
000486  PgHdr *sqlite3PcacheFetchFinish(
000487    PCache *pCache,             /* Obtain the page from this cache */
000488    Pgno pgno,                  /* Page number obtained */
000489    sqlite3_pcache_page *pPage  /* Page obtained by prior PcacheFetch() call */
000490  ){
000491    PgHdr *pPgHdr;
000492  
000493    assert( pPage!=0 );
000494    pPgHdr = (PgHdr *)pPage->pExtra;
000495  
000496    if( !pPgHdr->pPage ){
000497      return pcacheFetchFinishWithInit(pCache, pgno, pPage);
000498    }
000499    pCache->nRefSum++;
000500    pPgHdr->nRef++;
000501    assert( sqlite3PcachePageSanity(pPgHdr) );
000502    return pPgHdr;
000503  }
000504  
000505  /*
000506  ** Decrement the reference count on a page. If the page is clean and the
000507  ** reference count drops to 0, then it is made eligible for recycling.
000508  */
000509  void SQLITE_NOINLINE sqlite3PcacheRelease(PgHdr *p){
000510    assert( p->nRef>0 );
000511    p->pCache->nRefSum--;
000512    if( (--p->nRef)==0 ){
000513      if( p->flags&PGHDR_CLEAN ){
000514        pcacheUnpin(p);
000515      }else{
000516        pcacheManageDirtyList(p, PCACHE_DIRTYLIST_FRONT);
000517      }
000518    }
000519  }
000520  
000521  /*
000522  ** Increase the reference count of a supplied page by 1.
000523  */
000524  void sqlite3PcacheRef(PgHdr *p){
000525    assert(p->nRef>0);
000526    assert( sqlite3PcachePageSanity(p) );
000527    p->nRef++;
000528    p->pCache->nRefSum++;
000529  }
000530  
000531  /*
000532  ** Drop a page from the cache. There must be exactly one reference to the
000533  ** page. This function deletes that reference, so after it returns the
000534  ** page pointed to by p is invalid.
000535  */
000536  void sqlite3PcacheDrop(PgHdr *p){
000537    assert( p->nRef==1 );
000538    assert( sqlite3PcachePageSanity(p) );
000539    if( p->flags&PGHDR_DIRTY ){
000540      pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE);
000541    }
000542    p->pCache->nRefSum--;
000543    sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 1);
000544  }
000545  
000546  /*
000547  ** Make sure the page is marked as dirty. If it isn't dirty already,
000548  ** make it so.
000549  */
000550  void sqlite3PcacheMakeDirty(PgHdr *p){
000551    assert( p->nRef>0 );
000552    assert( sqlite3PcachePageSanity(p) );
000553    if( p->flags & (PGHDR_CLEAN|PGHDR_DONT_WRITE) ){    /*OPTIMIZATION-IF-FALSE*/
000554      p->flags &= ~PGHDR_DONT_WRITE;
000555      if( p->flags & PGHDR_CLEAN ){
000556        p->flags ^= (PGHDR_DIRTY|PGHDR_CLEAN);
000557        pcacheTrace(("%p.DIRTY %d\n",p->pCache,p->pgno));
000558        assert( (p->flags & (PGHDR_DIRTY|PGHDR_CLEAN))==PGHDR_DIRTY );
000559        pcacheManageDirtyList(p, PCACHE_DIRTYLIST_ADD);
000560      }
000561      assert( sqlite3PcachePageSanity(p) );
000562    }
000563  }
000564  
000565  /*
000566  ** Make sure the page is marked as clean. If it isn't clean already,
000567  ** make it so.
000568  */
000569  void sqlite3PcacheMakeClean(PgHdr *p){
000570    assert( sqlite3PcachePageSanity(p) );
000571    assert( (p->flags & PGHDR_DIRTY)!=0 );
000572    assert( (p->flags & PGHDR_CLEAN)==0 );
000573    pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE);
000574    p->flags &= ~(PGHDR_DIRTY|PGHDR_NEED_SYNC|PGHDR_WRITEABLE);
000575    p->flags |= PGHDR_CLEAN;
000576    pcacheTrace(("%p.CLEAN %d\n",p->pCache,p->pgno));
000577    assert( sqlite3PcachePageSanity(p) );
000578    if( p->nRef==0 ){
000579      pcacheUnpin(p);
000580    }
000581  }
000582  
000583  /*
000584  ** Make every page in the cache clean.
000585  */
000586  void sqlite3PcacheCleanAll(PCache *pCache){
000587    PgHdr *p;
000588    pcacheTrace(("%p.CLEAN-ALL\n",pCache));
000589    while( (p = pCache->pDirty)!=0 ){
000590      sqlite3PcacheMakeClean(p);
000591    }
000592  }
000593  
000594  /*
000595  ** Clear the PGHDR_NEED_SYNC and PGHDR_WRITEABLE flag from all dirty pages.
000596  */
000597  void sqlite3PcacheClearWritable(PCache *pCache){
000598    PgHdr *p;
000599    pcacheTrace(("%p.CLEAR-WRITEABLE\n",pCache));
000600    for(p=pCache->pDirty; p; p=p->pDirtyNext){
000601      p->flags &= ~(PGHDR_NEED_SYNC|PGHDR_WRITEABLE);
000602    }
000603    pCache->pSynced = pCache->pDirtyTail;
000604  }
000605  
000606  /*
000607  ** Clear the PGHDR_NEED_SYNC flag from all dirty pages.
000608  */
000609  void sqlite3PcacheClearSyncFlags(PCache *pCache){
000610    PgHdr *p;
000611    for(p=pCache->pDirty; p; p=p->pDirtyNext){
000612      p->flags &= ~PGHDR_NEED_SYNC;
000613    }
000614    pCache->pSynced = pCache->pDirtyTail;
000615  }
000616  
000617  /*
000618  ** Change the page number of page p to newPgno. 
000619  */
000620  void sqlite3PcacheMove(PgHdr *p, Pgno newPgno){
000621    PCache *pCache = p->pCache;
000622    assert( p->nRef>0 );
000623    assert( newPgno>0 );
000624    assert( sqlite3PcachePageSanity(p) );
000625    pcacheTrace(("%p.MOVE %d -> %d\n",pCache,p->pgno,newPgno));
000626    sqlite3GlobalConfig.pcache2.xRekey(pCache->pCache, p->pPage, p->pgno,newPgno);
000627    p->pgno = newPgno;
000628    if( (p->flags&PGHDR_DIRTY) && (p->flags&PGHDR_NEED_SYNC) ){
000629      pcacheManageDirtyList(p, PCACHE_DIRTYLIST_FRONT);
000630    }
000631  }
000632  
000633  /*
000634  ** Drop every cache entry whose page number is greater than "pgno". The
000635  ** caller must ensure that there are no outstanding references to any pages
000636  ** other than page 1 with a page number greater than pgno.
000637  **
000638  ** If there is a reference to page 1 and the pgno parameter passed to this
000639  ** function is 0, then the data area associated with page 1 is zeroed, but
000640  ** the page object is not dropped.
000641  */
000642  void sqlite3PcacheTruncate(PCache *pCache, Pgno pgno){
000643    if( pCache->pCache ){
000644      PgHdr *p;
000645      PgHdr *pNext;
000646      pcacheTrace(("%p.TRUNCATE %d\n",pCache,pgno));
000647      for(p=pCache->pDirty; p; p=pNext){
000648        pNext = p->pDirtyNext;
000649        /* This routine never gets call with a positive pgno except right
000650        ** after sqlite3PcacheCleanAll().  So if there are dirty pages,
000651        ** it must be that pgno==0.
000652        */
000653        assert( p->pgno>0 );
000654        if( p->pgno>pgno ){
000655          assert( p->flags&PGHDR_DIRTY );
000656          sqlite3PcacheMakeClean(p);
000657        }
000658      }
000659      if( pgno==0 && pCache->nRefSum ){
000660        sqlite3_pcache_page *pPage1;
000661        pPage1 = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache,1,0);
000662        if( ALWAYS(pPage1) ){  /* Page 1 is always available in cache, because
000663                               ** pCache->nRefSum>0 */
000664          memset(pPage1->pBuf, 0, pCache->szPage);
000665          pgno = 1;
000666        }
000667      }
000668      sqlite3GlobalConfig.pcache2.xTruncate(pCache->pCache, pgno+1);
000669    }
000670  }
000671  
000672  /*
000673  ** Close a cache.
000674  */
000675  void sqlite3PcacheClose(PCache *pCache){
000676    assert( pCache->pCache!=0 );
000677    pcacheTrace(("%p.CLOSE\n",pCache));
000678    sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
000679  }
000680  
000681  /* 
000682  ** Discard the contents of the cache.
000683  */
000684  void sqlite3PcacheClear(PCache *pCache){
000685    sqlite3PcacheTruncate(pCache, 0);
000686  }
000687  
000688  /*
000689  ** Merge two lists of pages connected by pDirty and in pgno order.
000690  ** Do not bother fixing the pDirtyPrev pointers.
000691  */
000692  static PgHdr *pcacheMergeDirtyList(PgHdr *pA, PgHdr *pB){
000693    PgHdr result, *pTail;
000694    pTail = &result;
000695    assert( pA!=0 && pB!=0 );
000696    for(;;){
000697      if( pA->pgno<pB->pgno ){
000698        pTail->pDirty = pA;
000699        pTail = pA;
000700        pA = pA->pDirty;
000701        if( pA==0 ){
000702          pTail->pDirty = pB;
000703          break;
000704        }
000705      }else{
000706        pTail->pDirty = pB;
000707        pTail = pB;
000708        pB = pB->pDirty;
000709        if( pB==0 ){
000710          pTail->pDirty = pA;
000711          break;
000712        }
000713      }
000714    }
000715    return result.pDirty;
000716  }
000717  
000718  /*
000719  ** Sort the list of pages in accending order by pgno.  Pages are
000720  ** connected by pDirty pointers.  The pDirtyPrev pointers are
000721  ** corrupted by this sort.
000722  **
000723  ** Since there cannot be more than 2^31 distinct pages in a database,
000724  ** there cannot be more than 31 buckets required by the merge sorter.
000725  ** One extra bucket is added to catch overflow in case something
000726  ** ever changes to make the previous sentence incorrect.
000727  */
000728  #define N_SORT_BUCKET  32
000729  static PgHdr *pcacheSortDirtyList(PgHdr *pIn){
000730    PgHdr *a[N_SORT_BUCKET], *p;
000731    int i;
000732    memset(a, 0, sizeof(a));
000733    while( pIn ){
000734      p = pIn;
000735      pIn = p->pDirty;
000736      p->pDirty = 0;
000737      for(i=0; ALWAYS(i<N_SORT_BUCKET-1); i++){
000738        if( a[i]==0 ){
000739          a[i] = p;
000740          break;
000741        }else{
000742          p = pcacheMergeDirtyList(a[i], p);
000743          a[i] = 0;
000744        }
000745      }
000746      if( NEVER(i==N_SORT_BUCKET-1) ){
000747        /* To get here, there need to be 2^(N_SORT_BUCKET) elements in
000748        ** the input list.  But that is impossible.
000749        */
000750        a[i] = pcacheMergeDirtyList(a[i], p);
000751      }
000752    }
000753    p = a[0];
000754    for(i=1; i<N_SORT_BUCKET; i++){
000755      if( a[i]==0 ) continue;
000756      p = p ? pcacheMergeDirtyList(p, a[i]) : a[i];
000757    }
000758    return p;
000759  }
000760  
000761  /*
000762  ** Return a list of all dirty pages in the cache, sorted by page number.
000763  */
000764  PgHdr *sqlite3PcacheDirtyList(PCache *pCache){
000765    PgHdr *p;
000766    for(p=pCache->pDirty; p; p=p->pDirtyNext){
000767      p->pDirty = p->pDirtyNext;
000768    }
000769    return pcacheSortDirtyList(pCache->pDirty);
000770  }
000771  
000772  /* 
000773  ** Return the total number of references to all pages held by the cache.
000774  **
000775  ** This is not the total number of pages referenced, but the sum of the
000776  ** reference count for all pages.
000777  */
000778  int sqlite3PcacheRefCount(PCache *pCache){
000779    return pCache->nRefSum;
000780  }
000781  
000782  /*
000783  ** Return the number of references to the page supplied as an argument.
000784  */
000785  int sqlite3PcachePageRefcount(PgHdr *p){
000786    return p->nRef;
000787  }
000788  
000789  /* 
000790  ** Return the total number of pages in the cache.
000791  */
000792  int sqlite3PcachePagecount(PCache *pCache){
000793    assert( pCache->pCache!=0 );
000794    return sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache);
000795  }
000796  
000797  #ifdef SQLITE_TEST
000798  /*
000799  ** Get the suggested cache-size value.
000800  */
000801  int sqlite3PcacheGetCachesize(PCache *pCache){
000802    return numberOfCachePages(pCache);
000803  }
000804  #endif
000805  
000806  /*
000807  ** Set the suggested cache-size value.
000808  */
000809  void sqlite3PcacheSetCachesize(PCache *pCache, int mxPage){
000810    assert( pCache->pCache!=0 );
000811    pCache->szCache = mxPage;
000812    sqlite3GlobalConfig.pcache2.xCachesize(pCache->pCache,
000813                                           numberOfCachePages(pCache));
000814  }
000815  
000816  /*
000817  ** Set the suggested cache-spill value.  Make no changes if if the
000818  ** argument is zero.  Return the effective cache-spill size, which will
000819  ** be the larger of the szSpill and szCache.
000820  */
000821  int sqlite3PcacheSetSpillsize(PCache *p, int mxPage){
000822    int res;
000823    assert( p->pCache!=0 );
000824    if( mxPage ){
000825      if( mxPage<0 ){
000826        mxPage = (int)((-1024*(i64)mxPage)/(p->szPage+p->szExtra));
000827      }
000828      p->szSpill = mxPage;
000829    }
000830    res = numberOfCachePages(p);
000831    if( res<p->szSpill ) res = p->szSpill; 
000832    return res;
000833  }
000834  
000835  /*
000836  ** Free up as much memory as possible from the page cache.
000837  */
000838  void sqlite3PcacheShrink(PCache *pCache){
000839    assert( pCache->pCache!=0 );
000840    sqlite3GlobalConfig.pcache2.xShrink(pCache->pCache);
000841  }
000842  
000843  /*
000844  ** Return the size of the header added by this middleware layer
000845  ** in the page-cache hierarchy.
000846  */
000847  int sqlite3HeaderSizePcache(void){ return ROUND8(sizeof(PgHdr)); }
000848  
000849  /*
000850  ** Return the number of dirty pages currently in the cache, as a percentage
000851  ** of the configured cache size.
000852  */
000853  int sqlite3PCachePercentDirty(PCache *pCache){
000854    PgHdr *pDirty;
000855    int nDirty = 0;
000856    int nCache = numberOfCachePages(pCache);
000857    for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext) nDirty++;
000858    return nCache ? (int)(((i64)nDirty * 100) / nCache) : 0;
000859  }
000860  
000861  #ifdef SQLITE_DIRECT_OVERFLOW_READ
000862  /* 
000863  ** Return true if there are one or more dirty pages in the cache. Else false.
000864  */
000865  int sqlite3PCacheIsDirty(PCache *pCache){
000866    return (pCache->pDirty!=0);
000867  }
000868  #endif
000869  
000870  #if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)
000871  /*
000872  ** For all dirty pages currently in the cache, invoke the specified
000873  ** callback. This is only used if the SQLITE_CHECK_PAGES macro is
000874  ** defined.
000875  */
000876  void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *)){
000877    PgHdr *pDirty;
000878    for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext){
000879      xIter(pDirty);
000880    }
000881  }
000882  #endif