Team Ai
Modelpublic

AryaWu/sqlite

sourceHugging Faceupdated 10mo agoView on Hugging Face
0likes
pcache.c937 linesDownload Raw Back to src
1/*2** 2008 August 053**4** The author disclaims copyright to this source code.  In place of5** a legal notice, here is a blessing:6**7**    May you do good and not evil.8**    May you find forgiveness for yourself and forgive others.9**    May you share freely, never taking more than you give.10**11*************************************************************************12** This file implements that page cache.13*/14#include "sqliteInt.h"15 16/*17** A complete page cache is an instance of this structure.  Every18** entry in the cache holds a single page of the database file.  The19** btree layer only operates on the cached copy of the database pages.20**21** A page cache entry is "clean" if it exactly matches what is currently22** on disk.  A page is "dirty" if it has been modified and needs to be23** persisted to disk.24**25** pDirty, pDirtyTail, pSynced:26**   All dirty pages are linked into the doubly linked list using27**   PgHdr.pDirtyNext and pDirtyPrev. The list is maintained in LRU order28**   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 and30**   pDirtyTail to the last (oldest).31**32**   The PCache.pSynced variable is used to optimize searching for a dirty33**   page to eject from the cache mid-transaction. It is better to eject34**   a page that does not require a journal sync than one that does. 35**   Therefore, pSynced is maintained so that it *almost* always points36**   to either the oldest page in the pDirty/pDirtyTail list that has a37**   clear PGHDR_NEED_SYNC flag or to a page that is older than this one38**   (so that the right page to eject can be found by following pDirtyPrev39**   pointers).40*/41struct PCache {42  PgHdr *pDirty, *pDirtyTail;         /* List of dirty pages in LRU order */43  PgHdr *pSynced;                     /* Last synced page in dirty page list */44  i64 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 */54};55 56/********************************** Test and Debug Logic **********************/57/*58** Debug tracing macros.  Enable by by changing the "0" to "1" and59** recompiling.60**61** When sqlite3PcacheTrace is 1, single line trace messages are issued.62** When sqlite3PcacheTrace is 2, a dump of the pcache showing all cache entries63** is displayed for many operations, resulting in a lot of output.64*/65#if defined(SQLITE_DEBUG) && 066  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  static void pcachePageTrace(int i, sqlite3_pcache_page *pLower){70    PgHdr *pPg;71    unsigned char *a;72    int j;73    if( pLower==0 ){74      printf("%3d: NULL\n", i);75    }else{76      pPg = (PgHdr*)pLower->pExtra;77      printf("%3d: nRef %2lld flgs %02x data ", i, pPg->nRef, pPg->flags);78      a = (unsigned char *)pLower->pBuf;79      for(j=0; j<12; j++) printf("%02x", a[j]);80      printf(" ptr %p\n", pPg);81    }82  }83  static void pcacheDump(PCache *pCache){84    int N;85    int i;86    sqlite3_pcache_page *pLower;87  88    if( sqlite3PcacheTrace<2 ) return;89    if( pCache->pCache==0 ) return;90    N = sqlite3PcachePagecount(pCache);91    if( N>sqlite3PcacheMxDump ) N = sqlite3PcacheMxDump;92    for(i=1; i<=N; i++){93       pLower = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, i, 0);94       pcachePageTrace(i, pLower);95       if( pLower && ((PgHdr*)pLower)->pPage==0 ){96         sqlite3GlobalConfig.pcache2.xUnpin(pCache->pCache, pLower, 0);97       }98    }99  }100#else101# define pcacheTrace(X)102# define pcachePageTrace(PGNO, X)103# define pcacheDump(X)104#endif105 106/*107** Return 1 if pPg is on the dirty list for pCache.  Return 0 if not.108** This routine runs inside of assert() statements only.109*/110#if defined(SQLITE_ENABLE_EXPENSIVE_ASSERT)111static int pageOnDirtyList(PCache *pCache, PgHdr *pPg){112  PgHdr *p;113  for(p=pCache->pDirty; p; p=p->pDirtyNext){114    if( p==pPg ) return 1;115  }116  return 0;117}118static int pageNotOnDirtyList(PCache *pCache, PgHdr *pPg){119  PgHdr *p;120  for(p=pCache->pDirty; p; p=p->pDirtyNext){121    if( p==pPg ) return 0;122  }123  return 1;124}125#else126# define pageOnDirtyList(A,B)    1127# define pageNotOnDirtyList(A,B) 1128#endif129 130/*131** Check invariants on a PgHdr entry.  Return true if everything is OK.132** Return false if any invariant is violated.133**134** This routine is for use inside of assert() statements only.  For135** example:136**137**          assert( sqlite3PcachePageSanity(pPg) );138*/139#ifdef SQLITE_DEBUG140int sqlite3PcachePageSanity(PgHdr *pPg){141  PCache *pCache;142  assert( pPg!=0 );143  assert( pPg->pgno>0 || pPg->pPager==0 );    /* Page number is 1 or more */144  pCache = pPg->pCache;145  assert( pCache!=0 );      /* Every page has an associated PCache */146  if( pPg->flags & PGHDR_CLEAN ){147    assert( (pPg->flags & PGHDR_DIRTY)==0 );/* Cannot be both CLEAN and DIRTY */148    assert( pageNotOnDirtyList(pCache, pPg) );/* CLEAN pages not on dirtylist */149  }else{150    assert( (pPg->flags & PGHDR_DIRTY)!=0 );/* If not CLEAN must be DIRTY */151    assert( pPg->pDirtyNext==0 || pPg->pDirtyNext->pDirtyPrev==pPg );152    assert( pPg->pDirtyPrev==0 || pPg->pDirtyPrev->pDirtyNext==pPg );153    assert( pPg->pDirtyPrev!=0 || pCache->pDirty==pPg );154    assert( pageOnDirtyList(pCache, pPg) );155  }156  /* WRITEABLE pages must also be DIRTY */157  if( pPg->flags & PGHDR_WRITEABLE ){158    assert( pPg->flags & PGHDR_DIRTY );     /* WRITEABLE implies DIRTY */159  }160  /* NEED_SYNC can be set independently of WRITEABLE.  This can happen,161  ** for example, when using the sqlite3PagerDontWrite() optimization:162  **    (1)  Page X is journalled, and gets WRITEABLE and NEED_SEEK.163  **    (2)  Page X moved to freelist, WRITEABLE is cleared164  **    (3)  Page X reused, WRITEABLE is set again165  ** If NEED_SYNC had been cleared in step 2, then it would not be reset166  ** in step 3, and page might be written into the database without first167  ** syncing the rollback journal, which might cause corruption on a power168  ** loss.169  **170  ** Another example is when the database page size is smaller than the171  ** disk sector size.  When any page of a sector is journalled, all pages172  ** in that sector are marked NEED_SYNC even if they are still CLEAN, just173  ** in case they are later modified, since all pages in the same sector174  ** must be journalled and synced before any of those pages can be safely175  ** written.176  */177  return 1;178}179#endif /* SQLITE_DEBUG */180 181 182/********************************** Linked List Management ********************/183 184/* Allowed values for second argument to pcacheManageDirtyList() */185#define PCACHE_DIRTYLIST_REMOVE   1    /* Remove pPage from dirty list */186#define PCACHE_DIRTYLIST_ADD      2    /* Add pPage to the dirty list */187#define PCACHE_DIRTYLIST_FRONT    3    /* Move pPage to the front of the list */188 189/*190** Manage pPage's participation on the dirty list.  Bits of the addRemove191** argument determines what operation to do.  The 0x01 bit means first192** remove pPage from the dirty list.  The 0x02 means add pPage back to193** the dirty list.  Doing both moves pPage to the front of the dirty list.194*/195static void pcacheManageDirtyList(PgHdr *pPage, u8 addRemove){196  PCache *p = pPage->pCache;197 198  pcacheTrace(("%p.DIRTYLIST.%s %d\n", p,199                addRemove==1 ? "REMOVE" : addRemove==2 ? "ADD" : "FRONT",200                pPage->pgno));201  if( addRemove & PCACHE_DIRTYLIST_REMOVE ){202    assert( pPage->pDirtyNext || pPage==p->pDirtyTail );203    assert( pPage->pDirtyPrev || pPage==p->pDirty );204  205    /* Update the PCache1.pSynced variable if necessary. */206    if( p->pSynced==pPage ){207      p->pSynced = pPage->pDirtyPrev;208    }209  210    if( pPage->pDirtyNext ){211      pPage->pDirtyNext->pDirtyPrev = pPage->pDirtyPrev;212    }else{213      assert( pPage==p->pDirtyTail );214      p->pDirtyTail = pPage->pDirtyPrev;215    }216    if( pPage->pDirtyPrev ){217      pPage->pDirtyPrev->pDirtyNext = pPage->pDirtyNext;218    }else{219      /* If there are now no dirty pages in the cache, set eCreate to 2. 220      ** This is an optimization that allows sqlite3PcacheFetch() to skip221      ** searching for a dirty page to eject from the cache when it might222      ** otherwise have to.  */223      assert( pPage==p->pDirty );224      p->pDirty = pPage->pDirtyNext;225      assert( p->bPurgeable || p->eCreate==2 );226      if( p->pDirty==0 ){         /*OPTIMIZATION-IF-TRUE*/227        assert( p->bPurgeable==0 || p->eCreate==1 );228        p->eCreate = 2;229      }230    }231  }232  if( addRemove & PCACHE_DIRTYLIST_ADD ){233    pPage->pDirtyPrev = 0;234    pPage->pDirtyNext = p->pDirty;235    if( pPage->pDirtyNext ){236      assert( pPage->pDirtyNext->pDirtyPrev==0 );237      pPage->pDirtyNext->pDirtyPrev = pPage;238    }else{239      p->pDirtyTail = pPage;240      if( p->bPurgeable ){241        assert( p->eCreate==2 );242        p->eCreate = 1;243      }244    }245    p->pDirty = pPage;246 247    /* If pSynced is NULL and this page has a clear NEED_SYNC flag, set248    ** pSynced to point to it. Checking the NEED_SYNC flag is an 249    ** optimization, as if pSynced points to a page with the NEED_SYNC250    ** flag set sqlite3PcacheFetchStress() searches through all newer 251    ** entries of the dirty-list for a page with NEED_SYNC clear anyway.  */252    if( !p->pSynced 253     && 0==(pPage->flags&PGHDR_NEED_SYNC)   /*OPTIMIZATION-IF-FALSE*/254    ){255      p->pSynced = pPage;256    }257  }258  pcacheDump(p);259}260 261/*262** Wrapper around the pluggable caches xUnpin method. If the cache is263** being used for an in-memory database, this function is a no-op.264*/265static void pcacheUnpin(PgHdr *p){266  if( p->pCache->bPurgeable ){267    pcacheTrace(("%p.UNPIN %d\n", p->pCache, p->pgno));268    sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 0);269    pcacheDump(p->pCache);270  }271}272 273/*274** Compute the number of pages of cache requested.   p->szCache is the275** cache size requested by the "PRAGMA cache_size" statement.276*/277static int numberOfCachePages(PCache *p){278  if( p->szCache>=0 ){279    /* IMPLEMENTATION-OF: R-42059-47211 If the argument N is positive then the280    ** suggested cache size is set to N. */281    return p->szCache;282  }else{283    i64 n;284    /* IMPLEMENTATION-OF: R-59858-46238 If the argument N is negative, then the285    ** number of cache pages is adjusted to be a number of pages that would286    ** use approximately abs(N*1024) bytes of memory based on the current287    ** page size. */288    n = ((-1024*(i64)p->szCache)/(p->szPage+p->szExtra));289    if( n>1000000000 ) n = 1000000000;290    return (int)n;291  }292}293 294/*************************************************** General Interfaces ******295**296** Initialize and shutdown the page cache subsystem. Neither of these 297** functions are threadsafe.298*/299int sqlite3PcacheInitialize(void){300  if( sqlite3GlobalConfig.pcache2.xInit==0 ){301    /* IMPLEMENTATION-OF: R-26801-64137 If the xInit() method is NULL, then the302    ** built-in default page cache is used instead of the application defined303    ** page cache. */304    sqlite3PCacheSetDefault();305    assert( sqlite3GlobalConfig.pcache2.xInit!=0 );306  }307  return sqlite3GlobalConfig.pcache2.xInit(sqlite3GlobalConfig.pcache2.pArg);308}309void sqlite3PcacheShutdown(void){310  if( sqlite3GlobalConfig.pcache2.xShutdown ){311    /* IMPLEMENTATION-OF: R-26000-56589 The xShutdown() method may be NULL. */312    sqlite3GlobalConfig.pcache2.xShutdown(sqlite3GlobalConfig.pcache2.pArg);313  }314}315 316/*317** Return the size in bytes of a PCache object.318*/319int sqlite3PcacheSize(void){ return sizeof(PCache); }320 321/*322** Create a new PCache object. Storage space to hold the object323** has already been allocated and is passed in as the p pointer. 324** The caller discovers how much space needs to be allocated by 325** calling sqlite3PcacheSize().326**327** szExtra is some extra space allocated for each page.  The first328** 8 bytes of the extra space will be zeroed as the page is allocated,329** but remaining content will be uninitialized.  Though it is opaque330** to this module, the extra space really ends up being the MemPage331** structure in the pager.332*/333int sqlite3PcacheOpen(334  int szPage,                  /* Size of every page */335  int szExtra,                 /* Extra space associated with each page */336  int bPurgeable,              /* True if pages are on backing store */337  int (*xStress)(void*,PgHdr*),/* Call to try to make pages clean */338  void *pStress,               /* Argument to xStress */339  PCache *p                    /* Preallocated space for the PCache */340){341  memset(p, 0, sizeof(PCache));342  p->szPage = 1;343  p->szExtra = szExtra;344  assert( szExtra>=8 );  /* First 8 bytes will be zeroed */345  p->bPurgeable = bPurgeable;346  p->eCreate = 2;347  p->xStress = xStress;348  p->pStress = pStress;349  p->szCache = 100;350  p->szSpill = 1;351  pcacheTrace(("%p.OPEN szPage %d bPurgeable %d\n",p,szPage,bPurgeable));352  return sqlite3PcacheSetPageSize(p, szPage);353}354 355/*356** Change the page size for PCache object. The caller must ensure that there357** are no outstanding page references when this function is called.358*/359int sqlite3PcacheSetPageSize(PCache *pCache, int szPage){360  assert( pCache->nRefSum==0 && pCache->pDirty==0 );361  if( pCache->szPage ){362    sqlite3_pcache *pNew;363    pNew = sqlite3GlobalConfig.pcache2.xCreate(364                szPage, pCache->szExtra + ROUND8(sizeof(PgHdr)),365                pCache->bPurgeable366    );367    if( pNew==0 ) return SQLITE_NOMEM_BKPT;368    sqlite3GlobalConfig.pcache2.xCachesize(pNew, numberOfCachePages(pCache));369    if( pCache->pCache ){370      sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);371    }372    pCache->pCache = pNew;373    pCache->szPage = szPage;374    pcacheTrace(("%p.PAGESIZE %d\n",pCache,szPage));375  }376  return SQLITE_OK;377}378 379/*380** Try to obtain a page from the cache.381**382** This routine returns a pointer to an sqlite3_pcache_page object if383** such an object is already in cache, or if a new one is created.384** This routine returns a NULL pointer if the object was not in cache385** and could not be created.386**387** The createFlags should be 0 to check for existing pages and should388** be 3 (not 1, but 3) to try to create a new page.389**390** If the createFlag is 0, then NULL is always returned if the page391** is not already in the cache.  If createFlag is 1, then a new page392** is created only if that can be done without spilling dirty pages393** and without exceeding the cache size limit.394**395** The caller needs to invoke sqlite3PcacheFetchFinish() to properly396** initialize the sqlite3_pcache_page object and convert it into a397** PgHdr object.  The sqlite3PcacheFetch() and sqlite3PcacheFetchFinish()398** routines are split this way for performance reasons. When separated399** they can both (usually) operate without having to push values to400** the stack on entry and pop them back off on exit, which saves a401** lot of pushing and popping.402*/403sqlite3_pcache_page *sqlite3PcacheFetch(404  PCache *pCache,       /* Obtain the page from this cache */405  Pgno pgno,            /* Page number to obtain */406  int createFlag        /* If true, create page if it does not exist already */407){408  int eCreate;409  sqlite3_pcache_page *pRes;410 411  assert( pCache!=0 );412  assert( pCache->pCache!=0 );413  assert( createFlag==3 || createFlag==0 );414  assert( pCache->eCreate==((pCache->bPurgeable && pCache->pDirty) ? 1 : 2) );415 416  /* eCreate defines what to do if the page does not exist.417  **    0     Do not allocate a new page.  (createFlag==0)418  **    1     Allocate a new page if doing so is inexpensive.419  **          (createFlag==1 AND bPurgeable AND pDirty)420  **    2     Allocate a new page even it doing so is difficult.421  **          (createFlag==1 AND !(bPurgeable AND pDirty)422  */423  eCreate = createFlag & pCache->eCreate;424  assert( eCreate==0 || eCreate==1 || eCreate==2 );425  assert( createFlag==0 || pCache->eCreate==eCreate );426  assert( createFlag==0 || eCreate==1+(!pCache->bPurgeable||!pCache->pDirty) );427  pRes = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, eCreate);428  pcacheTrace(("%p.FETCH %d%s (result: %p) ",pCache,pgno,429               createFlag?" create":"",pRes));430  pcachePageTrace(pgno, pRes);431  return pRes;432}433 434/*435** If the sqlite3PcacheFetch() routine is unable to allocate a new436** page because no clean pages are available for reuse and the cache437** size limit has been reached, then this routine can be invoked to 438** try harder to allocate a page.  This routine might invoke the stress439** callback to spill dirty pages to the journal.  It will then try to440** allocate the new page and will only fail to allocate a new page on441** an OOM error.442**443** This routine should be invoked only after sqlite3PcacheFetch() fails.444*/445int sqlite3PcacheFetchStress(446  PCache *pCache,                 /* Obtain the page from this cache */447  Pgno pgno,                      /* Page number to obtain */448  sqlite3_pcache_page **ppPage    /* Write result here */449){450  PgHdr *pPg;451  if( pCache->eCreate==2 ) return 0;452 453  if( sqlite3PcachePagecount(pCache)>pCache->szSpill ){454    /* Find a dirty page to write-out and recycle. First try to find a 455    ** page that does not require a journal-sync (one with PGHDR_NEED_SYNC456    ** cleared), but if that is not possible settle for any other 457    ** unreferenced dirty page.458    **459    ** If the LRU page in the dirty list that has a clear PGHDR_NEED_SYNC460    ** flag is currently referenced, then the following may leave pSynced461    ** set incorrectly (pointing to other than the LRU page with NEED_SYNC462    ** cleared). This is Ok, as pSynced is just an optimization.  */463    for(pPg=pCache->pSynced; 464        pPg && (pPg->nRef || (pPg->flags&PGHDR_NEED_SYNC)); 465        pPg=pPg->pDirtyPrev466    );467    pCache->pSynced = pPg;468    if( !pPg ){469      for(pPg=pCache->pDirtyTail; pPg && pPg->nRef; pPg=pPg->pDirtyPrev);470    }471    if( pPg ){472      int rc;473#ifdef SQLITE_LOG_CACHE_SPILL474      sqlite3_log(SQLITE_FULL, 475                  "spill page %d making room for %d - cache used: %d/%d",476                  pPg->pgno, pgno,477                  sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache),478                numberOfCachePages(pCache));479#endif480      pcacheTrace(("%p.SPILL %d\n",pCache,pPg->pgno));481      rc = pCache->xStress(pCache->pStress, pPg);482      pcacheDump(pCache);483      if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){484        return rc;485      }486    }487  }488  *ppPage = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, 2);489  return *ppPage==0 ? SQLITE_NOMEM_BKPT : SQLITE_OK;490}491 492/*493** This is a helper routine for sqlite3PcacheFetchFinish()494**495** In the uncommon case where the page being fetched has not been496** initialized, this routine is invoked to do the initialization.497** This routine is broken out into a separate function since it498** requires extra stack manipulation that can be avoided in the common499** case.500*/501static SQLITE_NOINLINE PgHdr *pcacheFetchFinishWithInit(502  PCache *pCache,             /* Obtain the page from this cache */503  Pgno pgno,                  /* Page number obtained */504  sqlite3_pcache_page *pPage  /* Page obtained by prior PcacheFetch() call */505){506  PgHdr *pPgHdr;507  assert( pPage!=0 );508  pPgHdr = (PgHdr*)pPage->pExtra;509  assert( pPgHdr->pPage==0 );510  memset(&pPgHdr->pDirty, 0, sizeof(PgHdr) - offsetof(PgHdr,pDirty));511  pPgHdr->pPage = pPage;512  pPgHdr->pData = pPage->pBuf;513  pPgHdr->pExtra = (void *)&pPgHdr[1];514  memset(pPgHdr->pExtra, 0, 8);515  assert( EIGHT_BYTE_ALIGNMENT( pPgHdr->pExtra ) );516  pPgHdr->pCache = pCache;517  pPgHdr->pgno = pgno;518  pPgHdr->flags = PGHDR_CLEAN;519  return sqlite3PcacheFetchFinish(pCache,pgno,pPage);520}521 522/*523** This routine converts the sqlite3_pcache_page object returned by524** sqlite3PcacheFetch() into an initialized PgHdr object.  This routine525** must be called after sqlite3PcacheFetch() in order to get a usable526** result.527*/528PgHdr *sqlite3PcacheFetchFinish(529  PCache *pCache,             /* Obtain the page from this cache */530  Pgno pgno,                  /* Page number obtained */531  sqlite3_pcache_page *pPage  /* Page obtained by prior PcacheFetch() call */532){533  PgHdr *pPgHdr;534 535  assert( pPage!=0 );536  pPgHdr = (PgHdr *)pPage->pExtra;537 538  if( !pPgHdr->pPage ){539    return pcacheFetchFinishWithInit(pCache, pgno, pPage);540  }541  pCache->nRefSum++;542  pPgHdr->nRef++;543  assert( sqlite3PcachePageSanity(pPgHdr) );544  return pPgHdr;545}546 547/*548** Decrement the reference count on a page. If the page is clean and the549** reference count drops to 0, then it is made eligible for recycling.550*/551void SQLITE_NOINLINE sqlite3PcacheRelease(PgHdr *p){552  assert( p->nRef>0 );553  p->pCache->nRefSum--;554  if( (--p->nRef)==0 ){555    if( p->flags&PGHDR_CLEAN ){556      pcacheUnpin(p);557    }else{558      pcacheManageDirtyList(p, PCACHE_DIRTYLIST_FRONT);559      assert( sqlite3PcachePageSanity(p) );560    }561  }562}563 564/*565** Increase the reference count of a supplied page by 1.566*/567void sqlite3PcacheRef(PgHdr *p){568  assert(p->nRef>0);569  assert( sqlite3PcachePageSanity(p) );570  p->nRef++;571  p->pCache->nRefSum++;572}573 574/*575** Drop a page from the cache. There must be exactly one reference to the576** page. This function deletes that reference, so after it returns the577** page pointed to by p is invalid.578*/579void sqlite3PcacheDrop(PgHdr *p){580  assert( p->nRef==1 );581  assert( sqlite3PcachePageSanity(p) );582  if( p->flags&PGHDR_DIRTY ){583    pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE);584  }585  p->pCache->nRefSum--;586  sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 1);587}588 589/*590** Make sure the page is marked as dirty. If it isn't dirty already,591** make it so.592*/593void sqlite3PcacheMakeDirty(PgHdr *p){594  assert( p->nRef>0 );595  assert( sqlite3PcachePageSanity(p) );596  if( p->flags & (PGHDR_CLEAN|PGHDR_DONT_WRITE) ){    /*OPTIMIZATION-IF-FALSE*/597    p->flags &= ~PGHDR_DONT_WRITE;598    if( p->flags & PGHDR_CLEAN ){599      p->flags ^= (PGHDR_DIRTY|PGHDR_CLEAN);600      pcacheTrace(("%p.DIRTY %d\n",p->pCache,p->pgno));601      assert( (p->flags & (PGHDR_DIRTY|PGHDR_CLEAN))==PGHDR_DIRTY );602      pcacheManageDirtyList(p, PCACHE_DIRTYLIST_ADD);603      assert( sqlite3PcachePageSanity(p) );604    }605    assert( sqlite3PcachePageSanity(p) );606  }607}608 609/*610** Make sure the page is marked as clean. If it isn't clean already,611** make it so.612*/613void sqlite3PcacheMakeClean(PgHdr *p){614  assert( sqlite3PcachePageSanity(p) );615  assert( (p->flags & PGHDR_DIRTY)!=0 );616  assert( (p->flags & PGHDR_CLEAN)==0 );617  pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE);618  p->flags &= ~(PGHDR_DIRTY|PGHDR_NEED_SYNC|PGHDR_WRITEABLE);619  p->flags |= PGHDR_CLEAN;620  pcacheTrace(("%p.CLEAN %d\n",p->pCache,p->pgno));621  assert( sqlite3PcachePageSanity(p) );622  if( p->nRef==0 ){623    pcacheUnpin(p);624  }625}626 627/*628** Make every page in the cache clean.629*/630void sqlite3PcacheCleanAll(PCache *pCache){631  PgHdr *p;632  pcacheTrace(("%p.CLEAN-ALL\n",pCache));633  while( (p = pCache->pDirty)!=0 ){634    sqlite3PcacheMakeClean(p);635  }636}637 638/*639** Clear the PGHDR_NEED_SYNC and PGHDR_WRITEABLE flag from all dirty pages.640*/641void sqlite3PcacheClearWritable(PCache *pCache){642  PgHdr *p;643  pcacheTrace(("%p.CLEAR-WRITEABLE\n",pCache));644  for(p=pCache->pDirty; p; p=p->pDirtyNext){645    p->flags &= ~(PGHDR_NEED_SYNC|PGHDR_WRITEABLE);646  }647  pCache->pSynced = pCache->pDirtyTail;648}649 650/*651** Clear the PGHDR_NEED_SYNC flag from all dirty pages.652*/653void sqlite3PcacheClearSyncFlags(PCache *pCache){654  PgHdr *p;655  for(p=pCache->pDirty; p; p=p->pDirtyNext){656    p->flags &= ~PGHDR_NEED_SYNC;657  }658  pCache->pSynced = pCache->pDirtyTail;659}660 661/*662** Change the page number of page p to newPgno. 663*/664void sqlite3PcacheMove(PgHdr *p, Pgno newPgno){665  PCache *pCache = p->pCache;666  sqlite3_pcache_page *pOther;667  assert( p->nRef>0 );668  assert( newPgno>0 );669  assert( sqlite3PcachePageSanity(p) );670  pcacheTrace(("%p.MOVE %d -> %d\n",pCache,p->pgno,newPgno));671  pOther = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, newPgno, 0);672  if( pOther ){673    PgHdr *pXPage = (PgHdr*)pOther->pExtra;674    assert( pXPage->nRef==0 );675    pXPage->nRef++;676    pCache->nRefSum++;677    sqlite3PcacheDrop(pXPage);678  }679  sqlite3GlobalConfig.pcache2.xRekey(pCache->pCache, p->pPage, p->pgno,newPgno);680  p->pgno = newPgno;681  if( (p->flags&PGHDR_DIRTY) && (p->flags&PGHDR_NEED_SYNC) ){682    pcacheManageDirtyList(p, PCACHE_DIRTYLIST_FRONT);683    assert( sqlite3PcachePageSanity(p) );684  }685}686 687/*688** Drop every cache entry whose page number is greater than "pgno". The689** caller must ensure that there are no outstanding references to any pages690** other than page 1 with a page number greater than pgno.691**692** If there is a reference to page 1 and the pgno parameter passed to this693** function is 0, then the data area associated with page 1 is zeroed, but694** the page object is not dropped.695*/696void sqlite3PcacheTruncate(PCache *pCache, Pgno pgno){697  if( pCache->pCache ){698    PgHdr *p;699    PgHdr *pNext;700    pcacheTrace(("%p.TRUNCATE %d\n",pCache,pgno));701    for(p=pCache->pDirty; p; p=pNext){702      pNext = p->pDirtyNext;703      /* This routine never gets call with a positive pgno except right704      ** after sqlite3PcacheCleanAll().  So if there are dirty pages,705      ** it must be that pgno==0.706      */707      assert( p->pgno>0 );708      if( p->pgno>pgno ){709        assert( p->flags&PGHDR_DIRTY );710        sqlite3PcacheMakeClean(p);711      }712    }713    if( pgno==0 && pCache->nRefSum ){714      sqlite3_pcache_page *pPage1;715      pPage1 = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache,1,0);716      if( ALWAYS(pPage1) ){  /* Page 1 is always available in cache, because717                             ** pCache->nRefSum>0 */718        memset(pPage1->pBuf, 0, pCache->szPage);719        pgno = 1;720      }721    }722    sqlite3GlobalConfig.pcache2.xTruncate(pCache->pCache, pgno+1);723  }724}725 726/*727** Close a cache.728*/729void sqlite3PcacheClose(PCache *pCache){730  assert( pCache->pCache!=0 );731  pcacheTrace(("%p.CLOSE\n",pCache));732  sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);733}734 735/* 736** Discard the contents of the cache.737*/738void sqlite3PcacheClear(PCache *pCache){739  sqlite3PcacheTruncate(pCache, 0);740}741 742/*743** Merge two lists of pages connected by pDirty and in pgno order.744** Do not bother fixing the pDirtyPrev pointers.745*/746static PgHdr *pcacheMergeDirtyList(PgHdr *pA, PgHdr *pB){747  PgHdr result, *pTail;748  pTail = &result;749  assert( pA!=0 && pB!=0 );750  for(;;){751    if( pA->pgno<pB->pgno ){752      pTail->pDirty = pA;753      pTail = pA;754      pA = pA->pDirty;755      if( pA==0 ){756        pTail->pDirty = pB;757        break;758      }759    }else{760      pTail->pDirty = pB;761      pTail = pB;762      pB = pB->pDirty;763      if( pB==0 ){764        pTail->pDirty = pA;765        break;766      }767    }768  }769  return result.pDirty;770}771 772/*773** Sort the list of pages in ascending order by pgno.  Pages are774** connected by pDirty pointers.  The pDirtyPrev pointers are775** corrupted by this sort.776**777** Since there cannot be more than 2^31 distinct pages in a database,778** there cannot be more than 31 buckets required by the merge sorter.779** One extra bucket is added to catch overflow in case something780** ever changes to make the previous sentence incorrect.781*/782#define N_SORT_BUCKET  32783static PgHdr *pcacheSortDirtyList(PgHdr *pIn){784  PgHdr *a[N_SORT_BUCKET], *p;785  int i;786  memset(a, 0, sizeof(a));787  while( pIn ){788    p = pIn;789    pIn = p->pDirty;790    p->pDirty = 0;791    for(i=0; ALWAYS(i<N_SORT_BUCKET-1); i++){792      if( a[i]==0 ){793        a[i] = p;794        break;795      }else{796        p = pcacheMergeDirtyList(a[i], p);797        a[i] = 0;798      }799    }800    if( NEVER(i==N_SORT_BUCKET-1) ){801      /* To get here, there need to be 2^(N_SORT_BUCKET) elements in802      ** the input list.  But that is impossible.803      */804      a[i] = pcacheMergeDirtyList(a[i], p);805    }806  }807  p = a[0];808  for(i=1; i<N_SORT_BUCKET; i++){809    if( a[i]==0 ) continue;810    p = p ? pcacheMergeDirtyList(p, a[i]) : a[i];811  }812  return p;813}814 815/*816** Return a list of all dirty pages in the cache, sorted by page number.817*/818PgHdr *sqlite3PcacheDirtyList(PCache *pCache){819  PgHdr *p;820  for(p=pCache->pDirty; p; p=p->pDirtyNext){821    p->pDirty = p->pDirtyNext;822  }823  return pcacheSortDirtyList(pCache->pDirty);824}825 826/* 827** Return the total number of references to all pages held by the cache.828**829** This is not the total number of pages referenced, but the sum of the830** reference count for all pages.831*/832i64 sqlite3PcacheRefCount(PCache *pCache){833  return pCache->nRefSum;834}835 836/*837** Return the number of references to the page supplied as an argument.838*/839i64 sqlite3PcachePageRefcount(PgHdr *p){840  return p->nRef;841}842 843/* 844** Return the total number of pages in the cache.845*/846int sqlite3PcachePagecount(PCache *pCache){847  assert( pCache->pCache!=0 );848  return sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache);849}850 851#ifdef SQLITE_TEST852/*853** Get the suggested cache-size value.854*/855int sqlite3PcacheGetCachesize(PCache *pCache){856  return numberOfCachePages(pCache);857}858#endif859 860/*861** Set the suggested cache-size value.862*/863void sqlite3PcacheSetCachesize(PCache *pCache, int mxPage){864  assert( pCache->pCache!=0 );865  pCache->szCache = mxPage;866  sqlite3GlobalConfig.pcache2.xCachesize(pCache->pCache,867                                         numberOfCachePages(pCache));868}869 870/*871** Set the suggested cache-spill value.  Make no changes if if the872** argument is zero.  Return the effective cache-spill size, which will873** be the larger of the szSpill and szCache.874*/875int sqlite3PcacheSetSpillsize(PCache *p, int mxPage){876  int res;877  assert( p->pCache!=0 );878  if( mxPage ){879    if( mxPage<0 ){880      mxPage = (int)((-1024*(i64)mxPage)/(p->szPage+p->szExtra));881    }882    p->szSpill = mxPage;883  }884  res = numberOfCachePages(p);885  if( res<p->szSpill ) res = p->szSpill; 886  return res;887}888 889/*890** Free up as much memory as possible from the page cache.891*/892void sqlite3PcacheShrink(PCache *pCache){893  assert( pCache->pCache!=0 );894  sqlite3GlobalConfig.pcache2.xShrink(pCache->pCache);895}896 897/*898** Return the size of the header added by this middleware layer899** in the page-cache hierarchy.900*/901int sqlite3HeaderSizePcache(void){ return ROUND8(sizeof(PgHdr)); }902 903/*904** Return the number of dirty pages currently in the cache, as a percentage905** of the configured cache size.906*/907int sqlite3PCachePercentDirty(PCache *pCache){908  PgHdr *pDirty;909  int nDirty = 0;910  int nCache = numberOfCachePages(pCache);911  for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext) nDirty++;912  return nCache ? (int)(((i64)nDirty * 100) / nCache) : 0;913}914 915#ifdef SQLITE_DIRECT_OVERFLOW_READ916/* 917** Return true if there are one or more dirty pages in the cache. Else false.918*/919int sqlite3PCacheIsDirty(PCache *pCache){920  return (pCache->pDirty!=0);921}922#endif923 924#if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)925/*926** For all dirty pages currently in the cache, invoke the specified927** callback. This is only used if the SQLITE_CHECK_PAGES macro is928** defined.929*/930void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *)){931  PgHdr *pDirty;932  for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext){933    xIter(pDirty);934  }935}936#endif937