more page cache

This commit is contained in:
Hongze Cheng 2022-02-21 08:58:32 +00:00
parent 7248dc6868
commit 7184778c28
3 changed files with 696 additions and 730 deletions

View File

@ -39,18 +39,18 @@
** pointers).
*/
struct PCache {
PgHdr *pDirty, *pDirtyTail; /* List of dirty pages in LRU order */
PgHdr *pSynced; /* Last synced page in dirty page list */
int nRefSum; /* Sum of ref counts over all pages */
int szCache; /* Configured cache size */
int szSpill; /* Size before spilling occurs */
int szPage; /* Size of every page in this cache */
int szExtra; /* Size of extra space for each page */
u8 bPurgeable; /* True if pages are on backing store */
u8 eCreate; /* eCreate value for for xFetch() */
int (*xStress)(void *, PgHdr *); /* Call to try make a page clean */
void * pStress; /* Argument to xStress */
sqlite3_pcache *pCache; /* Pluggable cache module */
PgHdr *pDirty, *pDirtyTail; /* List of dirty pages in LRU order */
PgHdr *pSynced; /* Last synced page in dirty page list */
int nRefSum; /* Sum of ref counts over all pages */
int szCache; /* Configured cache size */
int szSpill; /* Size before spilling occurs */
int szPage; /* Size of every page in this cache */
int szExtra; /* Size of extra space for each page */
u8 bPurgeable; /* True if pages are on backing store */
u8 eCreate; /* eCreate value for for xFetch() */
int (*xStress)(void*,PgHdr*); /* Call to try make a page clean */
void *pStress; /* Argument to xStress */
sqlite3_pcache *pCache; /* Pluggable cache module */
};
/********************************** Test and Debug Logic **********************/
@ -63,39 +63,36 @@ struct PCache {
** is displayed for many operations, resulting in a lot of output.
*/
#if defined(SQLITE_DEBUG) && 0
int sqlite3PcacheTrace = 2; /* 0: off 1: simple 2: cache dumps */
int sqlite3PcacheMxDump = 9999; /* Max cache entries for pcacheDump() */
#define pcacheTrace(X) \
if (sqlite3PcacheTrace) { \
sqlite3DebugPrintf X; \
}
void pcacheDump(PCache *pCache) {
int N;
int i, j;
sqlite3_pcache_page *pLower;
PgHdr * pPg;
unsigned char * a;
int sqlite3PcacheTrace = 2; /* 0: off 1: simple 2: cache dumps */
int sqlite3PcacheMxDump = 9999; /* Max cache entries for pcacheDump() */
# define pcacheTrace(X) if(sqlite3PcacheTrace){sqlite3DebugPrintf X;}
void pcacheDump(PCache *pCache){
int N;
int i, j;
sqlite3_pcache_page *pLower;
PgHdr *pPg;
unsigned char *a;
if (sqlite3PcacheTrace < 2) return;
if (pCache->pCache == 0) return;
N = sqlite3PcachePagecount(pCache);
if (N > sqlite3PcacheMxDump) N = sqlite3PcacheMxDump;
for (i = 1; i <= N; i++) {
pLower = pcache2.xFetch(pCache->pCache, i, 0);
if (pLower == 0) continue;
pPg = (PgHdr *)pLower->pExtra;
printf("%3d: nRef %2d flgs %02x data ", i, pPg->nRef, pPg->flags);
a = (unsigned char *)pLower->pBuf;
for (j = 0; j < 12; j++) printf("%02x", a[j]);
printf("\n");
if (pPg->pPage == 0) {
pcache2.xUnpin(pCache->pCache, pLower, 0);
if( sqlite3PcacheTrace<2 ) return;
if( pCache->pCache==0 ) return;
N = sqlite3PcachePagecount(pCache);
if( N>sqlite3PcacheMxDump ) N = sqlite3PcacheMxDump;
for(i=1; i<=N; i++){
pLower = pcache2.xFetch(pCache->pCache, i, 0);
if( pLower==0 ) continue;
pPg = (PgHdr*)pLower->pExtra;
printf("%3d: nRef %2d flgs %02x data ", i, pPg->nRef, pPg->flags);
a = (unsigned char *)pLower->pBuf;
for(j=0; j<12; j++) printf("%02x", a[j]);
printf("\n");
if( pPg->pPage==0 ){
pcache2.xUnpin(pCache->pCache, pLower, 0);
}
}
}
}
#else
#define pcacheTrace(X)
#define pcacheDump(X)
#else
# define pcacheTrace(X)
# define pcacheDump(X)
#endif
/*
@ -108,20 +105,20 @@ void pcacheDump(PCache *pCache) {
** assert( sqlite3PcachePageSanity(pPg) );
*/
#ifdef SQLITE_DEBUG
int sqlite3PcachePageSanity(PgHdr *pPg) {
int sqlite3PcachePageSanity(PgHdr *pPg){
PCache *pCache;
assert(pPg != 0);
assert(pPg->pgno > 0 || pPg->pPager == 0); /* Page number is 1 or more */
assert( pPg!=0 );
assert( pPg->pgno>0 || pPg->pPager==0 ); /* Page number is 1 or more */
pCache = pPg->pCache;
assert(pCache != 0); /* Every page has an associated PCache */
if (pPg->flags & PGHDR_CLEAN) {
assert((pPg->flags & PGHDR_DIRTY) == 0); /* Cannot be both CLEAN and DIRTY */
assert(pCache->pDirty != pPg); /* CLEAN pages not on dirty list */
assert(pCache->pDirtyTail != pPg);
assert( pCache!=0 ); /* Every page has an associated PCache */
if( pPg->flags & PGHDR_CLEAN ){
assert( (pPg->flags & PGHDR_DIRTY)==0 );/* Cannot be both CLEAN and DIRTY */
assert( pCache->pDirty!=pPg ); /* CLEAN pages not on dirty list */
assert( pCache->pDirtyTail!=pPg );
}
/* WRITEABLE pages must also be DIRTY */
if (pPg->flags & PGHDR_WRITEABLE) {
assert(pPg->flags & PGHDR_DIRTY); /* WRITEABLE implies DIRTY */
if( pPg->flags & PGHDR_WRITEABLE ){
assert( pPg->flags & PGHDR_DIRTY ); /* WRITEABLE implies DIRTY */
}
/* NEED_SYNC can be set independently of WRITEABLE. This can happen,
** for example, when using the sqlite3PagerDontWrite() optimization:
@ -144,12 +141,13 @@ int sqlite3PcachePageSanity(PgHdr *pPg) {
}
#endif /* SQLITE_DEBUG */
/********************************** Linked List Management ********************/
/* Allowed values for second argument to pcacheManageDirtyList() */
#define PCACHE_DIRTYLIST_REMOVE 1 /* Remove pPage from dirty list */
#define PCACHE_DIRTYLIST_ADD 2 /* Add pPage to the dirty list */
#define PCACHE_DIRTYLIST_FRONT 3 /* Move pPage to the front of the list */
#define PCACHE_DIRTYLIST_REMOVE 1 /* Remove pPage from dirty list */
#define PCACHE_DIRTYLIST_ADD 2 /* Add pPage to the dirty list */
#define PCACHE_DIRTYLIST_FRONT 3 /* Move pPage to the front of the list */
/*
** Manage pPage's participation on the dirty list. Bits of the addRemove
@ -157,51 +155,53 @@ int sqlite3PcachePageSanity(PgHdr *pPg) {
** remove pPage from the dirty list. The 0x02 means add pPage back to
** the dirty list. Doing both moves pPage to the front of the dirty list.
*/
static void pcacheManageDirtyList(PgHdr *pPage, u8 addRemove) {
static void pcacheManageDirtyList(PgHdr *pPage, u8 addRemove){
PCache *p = pPage->pCache;
pcacheTrace(("%p.DIRTYLIST.%s %d\n", p, addRemove == 1 ? "REMOVE" : addRemove == 2 ? "ADD" : "FRONT", pPage->pgno));
if (addRemove & PCACHE_DIRTYLIST_REMOVE) {
assert(pPage->pDirtyNext || pPage == p->pDirtyTail);
assert(pPage->pDirtyPrev || pPage == p->pDirty);
pcacheTrace(("%p.DIRTYLIST.%s %d\n", p,
addRemove==1 ? "REMOVE" : addRemove==2 ? "ADD" : "FRONT",
pPage->pgno));
if( addRemove & PCACHE_DIRTYLIST_REMOVE ){
assert( pPage->pDirtyNext || pPage==p->pDirtyTail );
assert( pPage->pDirtyPrev || pPage==p->pDirty );
/* Update the PCache1.pSynced variable if necessary. */
if (p->pSynced == pPage) {
if( p->pSynced==pPage ){
p->pSynced = pPage->pDirtyPrev;
}
if (pPage->pDirtyNext) {
if( pPage->pDirtyNext ){
pPage->pDirtyNext->pDirtyPrev = pPage->pDirtyPrev;
} else {
assert(pPage == p->pDirtyTail);
}else{
assert( pPage==p->pDirtyTail );
p->pDirtyTail = pPage->pDirtyPrev;
}
if (pPage->pDirtyPrev) {
if( pPage->pDirtyPrev ){
pPage->pDirtyPrev->pDirtyNext = pPage->pDirtyNext;
} else {
}else{
/* If there are now no dirty pages in the cache, set eCreate to 2.
** This is an optimization that allows sqlite3PcacheFetch() to skip
** searching for a dirty page to eject from the cache when it might
** otherwise have to. */
assert(pPage == p->pDirty);
assert( pPage==p->pDirty );
p->pDirty = pPage->pDirtyNext;
assert(p->bPurgeable || p->eCreate == 2);
if (p->pDirty == 0) { /*OPTIMIZATION-IF-TRUE*/
assert(p->bPurgeable == 0 || p->eCreate == 1);
assert( p->bPurgeable || p->eCreate==2 );
if( p->pDirty==0 ){ /*OPTIMIZATION-IF-TRUE*/
assert( p->bPurgeable==0 || p->eCreate==1 );
p->eCreate = 2;
}
}
}
if (addRemove & PCACHE_DIRTYLIST_ADD) {
if( addRemove & PCACHE_DIRTYLIST_ADD ){
pPage->pDirtyPrev = 0;
pPage->pDirtyNext = p->pDirty;
if (pPage->pDirtyNext) {
assert(pPage->pDirtyNext->pDirtyPrev == 0);
if( pPage->pDirtyNext ){
assert( pPage->pDirtyNext->pDirtyPrev==0 );
pPage->pDirtyNext->pDirtyPrev = pPage;
} else {
}else{
p->pDirtyTail = pPage;
if (p->bPurgeable) {
assert(p->eCreate == 2);
if( p->bPurgeable ){
assert( p->eCreate==2 );
p->eCreate = 1;
}
}
@ -212,8 +212,9 @@ static void pcacheManageDirtyList(PgHdr *pPage, u8 addRemove) {
** optimization, as if pSynced points to a page with the NEED_SYNC
** flag set sqlite3PcacheFetchStress() searches through all newer
** entries of the dirty-list for a page with NEED_SYNC clear anyway. */
if (!p->pSynced && 0 == (pPage->flags & PGHDR_NEED_SYNC) /*OPTIMIZATION-IF-FALSE*/
) {
if( !p->pSynced
&& 0==(pPage->flags&PGHDR_NEED_SYNC) /*OPTIMIZATION-IF-FALSE*/
){
p->pSynced = pPage;
}
}
@ -224,8 +225,8 @@ static void pcacheManageDirtyList(PgHdr *pPage, u8 addRemove) {
** Wrapper around the pluggable caches xUnpin method. If the cache is
** being used for an in-memory database, this function is a no-op.
*/
static void pcacheUnpin(PgHdr *p) {
if (p->pCache->bPurgeable) {
static void pcacheUnpin(PgHdr *p){
if( p->pCache->bPurgeable ){
pcacheTrace(("%p.UNPIN %d\n", p->pCache, p->pgno));
pcache2.xUnpin(p->pCache->pCache, p->pPage, 0);
pcacheDump(p->pCache);
@ -236,19 +237,19 @@ static void pcacheUnpin(PgHdr *p) {
** Compute the number of pages of cache requested. p->szCache is the
** cache size requested by the "PRAGMA cache_size" statement.
*/
static int numberOfCachePages(PCache *p) {
if (p->szCache >= 0) {
static int numberOfCachePages(PCache *p){
if( p->szCache>=0 ){
/* IMPLEMENTATION-OF: R-42059-47211 If the argument N is positive then the
** suggested cache size is set to N. */
return p->szCache;
} else {
}else{
i64 n;
/* IMPLEMANTATION-OF: R-59858-46238 If the argument N is negative, then the
** number of cache pages is adjusted to be a number of pages that would
** use approximately abs(N*1024) bytes of memory based on the current
** page size. */
n = ((-1024 * (i64)p->szCache) / (p->szPage + p->szExtra));
if (n > 1000000000) n = 1000000000;
n = ((-1024*(i64)p->szCache)/(p->szPage+p->szExtra));
if( n>1000000000 ) n = 1000000000;
return (int)n;
}
}
@ -258,9 +259,11 @@ static int numberOfCachePages(PCache *p) {
** Initialize and shutdown the page cache subsystem. Neither of these
** functions are threadsafe.
*/
int sqlite3PcacheInitialize(void) { return pcache2.xInit(pcache2.pArg); }
void sqlite3PcacheShutdown(void) {
if (pcache2.xShutdown) {
int sqlite3PcacheInitialize(void){
return pcache2.xInit(pcache2.pArg);
}
void sqlite3PcacheShutdown(void){
if( pcache2.xShutdown ){
/* IMPLEMENTATION-OF: R-26000-56589 The xShutdown() method may be NULL. */
pcache2.xShutdown(pcache2.pArg);
}
@ -269,7 +272,7 @@ void sqlite3PcacheShutdown(void) {
/*
** Return the size in bytes of a PCache object.
*/
int sqlite3PcacheSize(void) { return sizeof(PCache); }
int sqlite3PcacheSize(void){ return sizeof(PCache); }
/*
** Create a new PCache object. Storage space to hold the object
@ -283,24 +286,25 @@ int sqlite3PcacheSize(void) { return sizeof(PCache); }
** to this module, the extra space really ends up being the MemPage
** structure in the pager.
*/
int sqlite3PcacheOpen(int szPage, /* Size of every page */
int szExtra, /* Extra space associated with each page */
int bPurgeable, /* True if pages are on backing store */
int (*xStress)(void *, PgHdr *), /* Call to try to make pages clean */
void * pStress, /* Argument to xStress */
PCache *p /* Preallocated space for the PCache */
) {
int sqlite3PcacheOpen(
int szPage, /* Size of every page */
int szExtra, /* Extra space associated with each page */
int bPurgeable, /* True if pages are on backing store */
int (*xStress)(void*,PgHdr*),/* Call to try to make pages clean */
void *pStress, /* Argument to xStress */
PCache *p /* Preallocated space for the PCache */
){
memset(p, 0, sizeof(PCache));
p->szPage = 1;
p->szExtra = szExtra;
assert(szExtra >= 8); /* First 8 bytes will be zeroed */
assert( szExtra>=8 ); /* First 8 bytes will be zeroed */
p->bPurgeable = bPurgeable;
p->eCreate = 2;
p->xStress = xStress;
p->pStress = pStress;
p->szCache = 100;
p->szSpill = 1;
pcacheTrace(("%p.OPEN szPage %d bPurgeable %d\n", p, szPage, bPurgeable));
pcacheTrace(("%p.OPEN szPage %d bPurgeable %d\n",p,szPage,bPurgeable));
return sqlite3PcacheSetPageSize(p, szPage);
}
@ -308,21 +312,24 @@ int sqlite3PcacheOpen(int szPage, /* Size of every page */
** Change the page size for PCache object. The caller must ensure that there
** are no outstanding page references when this function is called.
*/
int sqlite3PcacheSetPageSize(PCache *pCache, int szPage) {
assert(pCache->nRefSum == 0 && pCache->pDirty == 0);
if (pCache->szPage) {
int sqlite3PcacheSetPageSize(PCache *pCache, int szPage){
assert( pCache->nRefSum==0 && pCache->pDirty==0 );
if( pCache->szPage ){
sqlite3_pcache *pNew;
pNew = pcache2.xCreate(szPage, pCache->szExtra + ROUND8(sizeof(PgHdr)), pCache->bPurgeable);
if (pNew == 0) return SQLITE_NOMEM_BKPT;
pNew = pcache2.xCreate(
szPage, pCache->szExtra + ROUND8(sizeof(PgHdr)),
pCache->bPurgeable
);
if( pNew==0 ) return SQLITE_NOMEM_BKPT;
pcache2.xCachesize(pNew, numberOfCachePages(pCache));
if (pCache->pCache) {
if( pCache->pCache ){
pcache2.xDestroy(pCache->pCache);
}
pCache->pCache = pNew;
pCache->szPage = szPage;
pcacheTrace(("%p.PAGESIZE %d\n", pCache, szPage));
pcacheTrace(("%p.PAGESIZE %d\n",pCache,szPage));
}
return SQLITE_OK;
return 0;
}
/*
@ -349,17 +356,18 @@ int sqlite3PcacheSetPageSize(PCache *pCache, int szPage) {
** the stack on entry and pop them back off on exit, which saves a
** lot of pushing and popping.
*/
sqlite3_pcache_page *sqlite3PcacheFetch(PCache *pCache, /* Obtain the page from this cache */
Pgno pgno, /* Page number to obtain */
int createFlag /* If true, create page if it does not exist already */
) {
int eCreate;
sqlite3_pcache_page *sqlite3PcacheFetch(
PCache *pCache, /* Obtain the page from this cache */
Pgno pgno, /* Page number to obtain */
int createFlag /* If true, create page if it does not exist already */
){
int eCreate;
sqlite3_pcache_page *pRes;
assert(pCache != 0);
assert(pCache->pCache != 0);
assert(createFlag == 3 || createFlag == 0);
assert(pCache->eCreate == ((pCache->bPurgeable && pCache->pDirty) ? 1 : 2));
assert( pCache!=0 );
assert( pCache->pCache!=0 );
assert( createFlag==3 || createFlag==0 );
assert( pCache->eCreate==((pCache->bPurgeable && pCache->pDirty) ? 1 : 2) );
/* eCreate defines what to do if the page does not exist.
** 0 Do not allocate a new page. (createFlag==0)
@ -369,11 +377,12 @@ sqlite3_pcache_page *sqlite3PcacheFetch(PCache *pCache, /* Obtain the page fr
** (createFlag==1 AND !(bPurgeable AND pDirty)
*/
eCreate = createFlag & pCache->eCreate;
assert(eCreate == 0 || eCreate == 1 || eCreate == 2);
assert(createFlag == 0 || pCache->eCreate == eCreate);
assert(createFlag == 0 || eCreate == 1 + (!pCache->bPurgeable || !pCache->pDirty));
assert( eCreate==0 || eCreate==1 || eCreate==2 );
assert( createFlag==0 || pCache->eCreate==eCreate );
assert( createFlag==0 || eCreate==1+(!pCache->bPurgeable||!pCache->pDirty) );
pRes = pcache2.xFetch(pCache->pCache, pgno, eCreate);
pcacheTrace(("%p.FETCH %d%s (result: %p)\n", pCache, pgno, createFlag ? " create" : "", pRes));
pcacheTrace(("%p.FETCH %d%s (result: %p)\n",pCache,pgno,
createFlag?" create":"",pRes));
return pRes;
}
@ -388,14 +397,15 @@ sqlite3_pcache_page *sqlite3PcacheFetch(PCache *pCache, /* Obtain the page fr
**
** This routine should be invoked only after sqlite3PcacheFetch() fails.
*/
int sqlite3PcacheFetchStress(PCache * pCache, /* Obtain the page from this cache */
Pgno pgno, /* Page number to obtain */
sqlite3_pcache_page **ppPage /* Write result here */
) {
int sqlite3PcacheFetchStress(
PCache *pCache, /* Obtain the page from this cache */
Pgno pgno, /* Page number to obtain */
sqlite3_pcache_page **ppPage /* Write result here */
){
PgHdr *pPg;
if (pCache->eCreate == 2) return 0;
if( pCache->eCreate==2 ) return 0;
if (sqlite3PcachePagecount(pCache) > pCache->szSpill) {
if( sqlite3PcachePagecount(pCache)>pCache->szSpill ){
/* Find a dirty page to write-out and recycle. First try to find a
** page that does not require a journal-sync (one with PGHDR_NEED_SYNC
** cleared), but if that is not possible settle for any other
@ -405,29 +415,33 @@ int sqlite3PcacheFetchStress(PCache * pCache, /* Obtain the page fr
** flag is currently referenced, then the following may leave pSynced
** set incorrectly (pointing to other than the LRU page with NEED_SYNC
** cleared). This is Ok, as pSynced is just an optimization. */
for (pPg = pCache->pSynced; pPg && (pPg->nRef || (pPg->flags & PGHDR_NEED_SYNC)); pPg = pPg->pDirtyPrev)
;
for(pPg=pCache->pSynced;
pPg && (pPg->nRef || (pPg->flags&PGHDR_NEED_SYNC));
pPg=pPg->pDirtyPrev
);
pCache->pSynced = pPg;
if (!pPg) {
for (pPg = pCache->pDirtyTail; pPg && pPg->nRef; pPg = pPg->pDirtyPrev)
;
if( !pPg ){
for(pPg=pCache->pDirtyTail; pPg && pPg->nRef; pPg=pPg->pDirtyPrev);
}
if (pPg) {
if( pPg ){
int rc;
#ifdef SQLITE_LOG_CACHE_SPILL
sqlite3_log(SQLITE_FULL, "spill page %d making room for %d - cache used: %d/%d", pPg->pgno, pgno,
pcache2.xPagecount(pCache->pCache), numberOfCachePages(pCache));
sqlite3_log(SQLITE_FULL,
"spill page %d making room for %d - cache used: %d/%d",
pPg->pgno, pgno,
pcache2.xPagecount(pCache->pCache),
numberOfCachePages(pCache));
#endif
pcacheTrace(("%p.SPILL %d\n", pCache, pPg->pgno));
pcacheTrace(("%p.SPILL %d\n",pCache,pPg->pgno));
rc = pCache->xStress(pCache->pStress, pPg);
pcacheDump(pCache);
if (rc != SQLITE_OK && rc != SQLITE_BUSY) {
if( rc!=0 && rc!=SQLITE_BUSY ){
return rc;
}
}
}
*ppPage = pcache2.xFetch(pCache->pCache, pgno, 2);
return *ppPage == 0 ? SQLITE_NOMEM_BKPT : SQLITE_OK;
return *ppPage==0 ? SQLITE_NOMEM_BKPT : 0;
}
/*
@ -440,15 +454,15 @@ int sqlite3PcacheFetchStress(PCache * pCache, /* Obtain the page fr
** case.
*/
static SQLITE_NOINLINE PgHdr *pcacheFetchFinishWithInit(
PCache * pCache, /* Obtain the page from this cache */
Pgno pgno, /* Page number obtained */
sqlite3_pcache_page *pPage /* Page obtained by prior PcacheFetch() call */
) {
PCache *pCache, /* Obtain the page from this cache */
Pgno pgno, /* Page number obtained */
sqlite3_pcache_page *pPage /* Page obtained by prior PcacheFetch() call */
){
PgHdr *pPgHdr;
assert(pPage != 0);
pPgHdr = (PgHdr *)pPage->pExtra;
assert(pPgHdr->pPage == 0);
memset(&pPgHdr->pDirty, 0, sizeof(PgHdr) - offsetof(PgHdr, pDirty));
assert( pPage!=0 );
pPgHdr = (PgHdr*)pPage->pExtra;
assert( pPgHdr->pPage==0 );
memset(&pPgHdr->pDirty, 0, sizeof(PgHdr) - offsetof(PgHdr,pDirty));
pPgHdr->pPage = pPage;
pPgHdr->pData = pPage->pBuf;
pPgHdr->pExtra = (void *)&pPgHdr[1];
@ -456,7 +470,7 @@ static SQLITE_NOINLINE PgHdr *pcacheFetchFinishWithInit(
pPgHdr->pCache = pCache;
pPgHdr->pgno = pgno;
pPgHdr->flags = PGHDR_CLEAN;
return sqlite3PcacheFetchFinish(pCache, pgno, pPage);
return sqlite3PcacheFetchFinish(pCache,pgno,pPage);
}
/*
@ -465,21 +479,22 @@ static SQLITE_NOINLINE PgHdr *pcacheFetchFinishWithInit(
** must be called after sqlite3PcacheFetch() in order to get a usable
** result.
*/
PgHdr *sqlite3PcacheFetchFinish(PCache * pCache, /* Obtain the page from this cache */
Pgno pgno, /* Page number obtained */
sqlite3_pcache_page *pPage /* Page obtained by prior PcacheFetch() call */
) {
PgHdr *sqlite3PcacheFetchFinish(
PCache *pCache, /* Obtain the page from this cache */
Pgno pgno, /* Page number obtained */
sqlite3_pcache_page *pPage /* Page obtained by prior PcacheFetch() call */
){
PgHdr *pPgHdr;
assert(pPage != 0);
assert( pPage!=0 );
pPgHdr = (PgHdr *)pPage->pExtra;
if (!pPgHdr->pPage) {
if( !pPgHdr->pPage ){
return pcacheFetchFinishWithInit(pCache, pgno, pPage);
}
pCache->nRefSum++;
pPgHdr->nRef++;
assert(sqlite3PcachePageSanity(pPgHdr));
assert( sqlite3PcachePageSanity(pPgHdr) );
return pPgHdr;
}
@ -487,13 +502,13 @@ PgHdr *sqlite3PcacheFetchFinish(PCache * pCache, /* Obtain the page
** Decrement the reference count on a page. If the page is clean and the
** reference count drops to 0, then it is made eligible for recycling.
*/
void SQLITE_NOINLINE sqlite3PcacheRelease(PgHdr *p) {
assert(p->nRef > 0);
void SQLITE_NOINLINE sqlite3PcacheRelease(PgHdr *p){
assert( p->nRef>0 );
p->pCache->nRefSum--;
if ((--p->nRef) == 0) {
if (p->flags & PGHDR_CLEAN) {
if( (--p->nRef)==0 ){
if( p->flags&PGHDR_CLEAN ){
pcacheUnpin(p);
} else {
}else{
pcacheManageDirtyList(p, PCACHE_DIRTYLIST_FRONT);
}
}
@ -502,9 +517,9 @@ void SQLITE_NOINLINE sqlite3PcacheRelease(PgHdr *p) {
/*
** Increase the reference count of a supplied page by 1.
*/
void sqlite3PcacheRef(PgHdr *p) {
assert(p->nRef > 0);
assert(sqlite3PcachePageSanity(p));
void sqlite3PcacheRef(PgHdr *p){
assert(p->nRef>0);
assert( sqlite3PcachePageSanity(p) );
p->nRef++;
p->pCache->nRefSum++;
}
@ -514,10 +529,10 @@ void sqlite3PcacheRef(PgHdr *p) {
** page. This function deletes that reference, so after it returns the
** page pointed to by p is invalid.
*/
void sqlite3PcacheDrop(PgHdr *p) {
assert(p->nRef == 1);
assert(sqlite3PcachePageSanity(p));
if (p->flags & PGHDR_DIRTY) {
void sqlite3PcacheDrop(PgHdr *p){
assert( p->nRef==1 );
assert( sqlite3PcachePageSanity(p) );
if( p->flags&PGHDR_DIRTY ){
pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE);
}
p->pCache->nRefSum--;
@ -528,18 +543,18 @@ void sqlite3PcacheDrop(PgHdr *p) {
** Make sure the page is marked as dirty. If it isn't dirty already,
** make it so.
*/
void sqlite3PcacheMakeDirty(PgHdr *p) {
assert(p->nRef > 0);
assert(sqlite3PcachePageSanity(p));
if (p->flags & (PGHDR_CLEAN | PGHDR_DONT_WRITE)) { /*OPTIMIZATION-IF-FALSE*/
void sqlite3PcacheMakeDirty(PgHdr *p){
assert( p->nRef>0 );
assert( sqlite3PcachePageSanity(p) );
if( p->flags & (PGHDR_CLEAN|PGHDR_DONT_WRITE) ){ /*OPTIMIZATION-IF-FALSE*/
p->flags &= ~PGHDR_DONT_WRITE;
if (p->flags & PGHDR_CLEAN) {
p->flags ^= (PGHDR_DIRTY | PGHDR_CLEAN);
pcacheTrace(("%p.DIRTY %d\n", p->pCache, p->pgno));
assert((p->flags & (PGHDR_DIRTY | PGHDR_CLEAN)) == PGHDR_DIRTY);
if( p->flags & PGHDR_CLEAN ){
p->flags ^= (PGHDR_DIRTY|PGHDR_CLEAN);
pcacheTrace(("%p.DIRTY %d\n",p->pCache,p->pgno));
assert( (p->flags & (PGHDR_DIRTY|PGHDR_CLEAN))==PGHDR_DIRTY );
pcacheManageDirtyList(p, PCACHE_DIRTYLIST_ADD);
}
assert(sqlite3PcachePageSanity(p));
assert( sqlite3PcachePageSanity(p) );
}
}
@ -547,16 +562,16 @@ void sqlite3PcacheMakeDirty(PgHdr *p) {
** Make sure the page is marked as clean. If it isn't clean already,
** make it so.
*/
void sqlite3PcacheMakeClean(PgHdr *p) {
assert(sqlite3PcachePageSanity(p));
assert((p->flags & PGHDR_DIRTY) != 0);
assert((p->flags & PGHDR_CLEAN) == 0);
void sqlite3PcacheMakeClean(PgHdr *p){
assert( sqlite3PcachePageSanity(p) );
assert( (p->flags & PGHDR_DIRTY)!=0 );
assert( (p->flags & PGHDR_CLEAN)==0 );
pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE);
p->flags &= ~(PGHDR_DIRTY | PGHDR_NEED_SYNC | PGHDR_WRITEABLE);
p->flags &= ~(PGHDR_DIRTY|PGHDR_NEED_SYNC|PGHDR_WRITEABLE);
p->flags |= PGHDR_CLEAN;
pcacheTrace(("%p.CLEAN %d\n", p->pCache, p->pgno));
assert(sqlite3PcachePageSanity(p));
if (p->nRef == 0) {
pcacheTrace(("%p.CLEAN %d\n",p->pCache,p->pgno));
assert( sqlite3PcachePageSanity(p) );
if( p->nRef==0 ){
pcacheUnpin(p);
}
}
@ -564,10 +579,10 @@ void sqlite3PcacheMakeClean(PgHdr *p) {
/*
** Make every page in the cache clean.
*/
void sqlite3PcacheCleanAll(PCache *pCache) {
void sqlite3PcacheCleanAll(PCache *pCache){
PgHdr *p;
pcacheTrace(("%p.CLEAN-ALL\n", pCache));
while ((p = pCache->pDirty) != 0) {
pcacheTrace(("%p.CLEAN-ALL\n",pCache));
while( (p = pCache->pDirty)!=0 ){
sqlite3PcacheMakeClean(p);
}
}
@ -575,11 +590,11 @@ void sqlite3PcacheCleanAll(PCache *pCache) {
/*
** Clear the PGHDR_NEED_SYNC and PGHDR_WRITEABLE flag from all dirty pages.
*/
void sqlite3PcacheClearWritable(PCache *pCache) {
void sqlite3PcacheClearWritable(PCache *pCache){
PgHdr *p;
pcacheTrace(("%p.CLEAR-WRITEABLE\n", pCache));
for (p = pCache->pDirty; p; p = p->pDirtyNext) {
p->flags &= ~(PGHDR_NEED_SYNC | PGHDR_WRITEABLE);
pcacheTrace(("%p.CLEAR-WRITEABLE\n",pCache));
for(p=pCache->pDirty; p; p=p->pDirtyNext){
p->flags &= ~(PGHDR_NEED_SYNC|PGHDR_WRITEABLE);
}
pCache->pSynced = pCache->pDirtyTail;
}
@ -587,9 +602,9 @@ void sqlite3PcacheClearWritable(PCache *pCache) {
/*
** Clear the PGHDR_NEED_SYNC flag from all dirty pages.
*/
void sqlite3PcacheClearSyncFlags(PCache *pCache) {
void sqlite3PcacheClearSyncFlags(PCache *pCache){
PgHdr *p;
for (p = pCache->pDirty; p; p = p->pDirtyNext) {
for(p=pCache->pDirty; p; p=p->pDirtyNext){
p->flags &= ~PGHDR_NEED_SYNC;
}
pCache->pSynced = pCache->pDirtyTail;
@ -598,15 +613,15 @@ void sqlite3PcacheClearSyncFlags(PCache *pCache) {
/*
** Change the page number of page p to newPgno.
*/
void sqlite3PcacheMove(PgHdr *p, Pgno newPgno) {
void sqlite3PcacheMove(PgHdr *p, Pgno newPgno){
PCache *pCache = p->pCache;
assert(p->nRef > 0);
assert(newPgno > 0);
assert(sqlite3PcachePageSanity(p));
pcacheTrace(("%p.MOVE %d -> %d\n", pCache, p->pgno, newPgno));
pcache2.xRekey(pCache->pCache, p->pPage, p->pgno, newPgno);
assert( p->nRef>0 );
assert( newPgno>0 );
assert( sqlite3PcachePageSanity(p) );
pcacheTrace(("%p.MOVE %d -> %d\n",pCache,p->pgno,newPgno));
pcache2.xRekey(pCache->pCache, p->pPage, p->pgno,newPgno);
p->pgno = newPgno;
if ((p->flags & PGHDR_DIRTY) && (p->flags & PGHDR_NEED_SYNC)) {
if( (p->flags&PGHDR_DIRTY) && (p->flags&PGHDR_NEED_SYNC) ){
pcacheManageDirtyList(p, PCACHE_DIRTYLIST_FRONT);
}
}
@ -620,72 +635,74 @@ void sqlite3PcacheMove(PgHdr *p, Pgno newPgno) {
** function is 0, then the data area associated with page 1 is zeroed, but
** the page object is not dropped.
*/
void sqlite3PcacheTruncate(PCache *pCache, Pgno pgno) {
if (pCache->pCache) {
void sqlite3PcacheTruncate(PCache *pCache, Pgno pgno){
if( pCache->pCache ){
PgHdr *p;
PgHdr *pNext;
pcacheTrace(("%p.TRUNCATE %d\n", pCache, pgno));
for (p = pCache->pDirty; p; p = pNext) {
pcacheTrace(("%p.TRUNCATE %d\n",pCache,pgno));
for(p=pCache->pDirty; p; p=pNext){
pNext = p->pDirtyNext;
/* This routine never gets call with a positive pgno except right
** after sqlite3PcacheCleanAll(). So if there are dirty pages,
** it must be that pgno==0.
*/
assert(p->pgno > 0);
if (p->pgno > pgno) {
assert(p->flags & PGHDR_DIRTY);
assert( p->pgno>0 );
if( p->pgno>pgno ){
assert( p->flags&PGHDR_DIRTY );
sqlite3PcacheMakeClean(p);
}
}
if (pgno == 0 && pCache->nRefSum) {
if( pgno==0 && pCache->nRefSum ){
sqlite3_pcache_page *pPage1;
pPage1 = pcache2.xFetch(pCache->pCache, 1, 0);
if (ALWAYS(pPage1)) { /* Page 1 is always available in cache, because
** pCache->nRefSum>0 */
pPage1 = pcache2.xFetch(pCache->pCache,1,0);
if( ALWAYS(pPage1) ){ /* Page 1 is always available in cache, because
** pCache->nRefSum>0 */
memset(pPage1->pBuf, 0, pCache->szPage);
pgno = 1;
}
}
pcache2.xTruncate(pCache->pCache, pgno + 1);
pcache2.xTruncate(pCache->pCache, pgno+1);
}
}
/*
** Close a cache.
*/
void sqlite3PcacheClose(PCache *pCache) {
assert(pCache->pCache != 0);
pcacheTrace(("%p.CLOSE\n", pCache));
void sqlite3PcacheClose(PCache *pCache){
assert( pCache->pCache!=0 );
pcacheTrace(("%p.CLOSE\n",pCache));
pcache2.xDestroy(pCache->pCache);
}
/*
** Discard the contents of the cache.
*/
void sqlite3PcacheClear(PCache *pCache) { sqlite3PcacheTruncate(pCache, 0); }
void sqlite3PcacheClear(PCache *pCache){
sqlite3PcacheTruncate(pCache, 0);
}
/*
** Merge two lists of pages connected by pDirty and in pgno order.
** Do not bother fixing the pDirtyPrev pointers.
*/
static PgHdr *pcacheMergeDirtyList(PgHdr *pA, PgHdr *pB) {
static PgHdr *pcacheMergeDirtyList(PgHdr *pA, PgHdr *pB){
PgHdr result, *pTail;
pTail = &result;
assert(pA != 0 && pB != 0);
for (;;) {
if (pA->pgno < pB->pgno) {
assert( pA!=0 && pB!=0 );
for(;;){
if( pA->pgno<pB->pgno ){
pTail->pDirty = pA;
pTail = pA;
pA = pA->pDirty;
if (pA == 0) {
if( pA==0 ){
pTail->pDirty = pB;
break;
}
} else {
}else{
pTail->pDirty = pB;
pTail = pB;
pB = pB->pDirty;
if (pB == 0) {
if( pB==0 ){
pTail->pDirty = pA;
break;
}
@ -704,25 +721,25 @@ static PgHdr *pcacheMergeDirtyList(PgHdr *pA, PgHdr *pB) {
** One extra bucket is added to catch overflow in case something
** ever changes to make the previous sentence incorrect.
*/
#define N_SORT_BUCKET 32
static PgHdr *pcacheSortDirtyList(PgHdr *pIn) {
#define N_SORT_BUCKET 32
static PgHdr *pcacheSortDirtyList(PgHdr *pIn){
PgHdr *a[N_SORT_BUCKET], *p;
int i;
int i;
memset(a, 0, sizeof(a));
while (pIn) {
while( pIn ){
p = pIn;
pIn = p->pDirty;
p->pDirty = 0;
for (i = 0; ALWAYS(i < N_SORT_BUCKET - 1); i++) {
if (a[i] == 0) {
for(i=0; ALWAYS(i<N_SORT_BUCKET-1); i++){
if( a[i]==0 ){
a[i] = p;
break;
} else {
}else{
p = pcacheMergeDirtyList(a[i], p);
a[i] = 0;
}
}
if (NEVER(i == N_SORT_BUCKET - 1)) {
if( NEVER(i==N_SORT_BUCKET-1) ){
/* To get here, there need to be 2^(N_SORT_BUCKET) elements in
** the input list. But that is impossible.
*/
@ -730,8 +747,8 @@ static PgHdr *pcacheSortDirtyList(PgHdr *pIn) {
}
}
p = a[0];
for (i = 1; i < N_SORT_BUCKET; i++) {
if (a[i] == 0) continue;
for(i=1; i<N_SORT_BUCKET; i++){
if( a[i]==0 ) continue;
p = p ? pcacheMergeDirtyList(p, a[i]) : a[i];
}
return p;
@ -740,9 +757,9 @@ static PgHdr *pcacheSortDirtyList(PgHdr *pIn) {
/*
** Return a list of all dirty pages in the cache, sorted by page number.
*/
PgHdr *sqlite3PcacheDirtyList(PCache *pCache) {
PgHdr *sqlite3PcacheDirtyList(PCache *pCache){
PgHdr *p;
for (p = pCache->pDirty; p; p = p->pDirtyNext) {
for(p=pCache->pDirty; p; p=p->pDirtyNext){
p->pDirty = p->pDirtyNext;
}
return pcacheSortDirtyList(pCache->pDirty);
@ -754,18 +771,22 @@ PgHdr *sqlite3PcacheDirtyList(PCache *pCache) {
** This is not the total number of pages referenced, but the sum of the
** reference count for all pages.
*/
int sqlite3PcacheRefCount(PCache *pCache) { return pCache->nRefSum; }
int sqlite3PcacheRefCount(PCache *pCache){
return pCache->nRefSum;
}
/*
** Return the number of references to the page supplied as an argument.
*/
int sqlite3PcachePageRefcount(PgHdr *p) { return p->nRef; }
int sqlite3PcachePageRefcount(PgHdr *p){
return p->nRef;
}
/*
** Return the total number of pages in the cache.
*/
int sqlite3PcachePagecount(PCache *pCache) {
assert(pCache->pCache != 0);
int sqlite3PcachePagecount(PCache *pCache){
assert( pCache->pCache!=0 );
return pcache2.xPagecount(pCache->pCache);
}
@ -773,16 +794,19 @@ int sqlite3PcachePagecount(PCache *pCache) {
/*
** Get the suggested cache-size value.
*/
int sqlite3PcacheGetCachesize(PCache *pCache) { return numberOfCachePages(pCache); }
int sqlite3PcacheGetCachesize(PCache *pCache){
return numberOfCachePages(pCache);
}
#endif
/*
** Set the suggested cache-size value.
*/
void sqlite3PcacheSetCachesize(PCache *pCache, int mxPage) {
assert(pCache->pCache != 0);
void sqlite3PcacheSetCachesize(PCache *pCache, int mxPage){
assert( pCache->pCache!=0 );
pCache->szCache = mxPage;
pcache2.xCachesize(pCache->pCache, numberOfCachePages(pCache));
pcache2.xCachesize(pCache->pCache,
numberOfCachePages(pCache));
}
/*
@ -790,25 +814,25 @@ void sqlite3PcacheSetCachesize(PCache *pCache, int mxPage) {
** argument is zero. Return the effective cache-spill size, which will
** be the larger of the szSpill and szCache.
*/
int sqlite3PcacheSetSpillsize(PCache *p, int mxPage) {
int sqlite3PcacheSetSpillsize(PCache *p, int mxPage){
int res;
assert(p->pCache != 0);
if (mxPage) {
if (mxPage < 0) {
mxPage = (int)((-1024 * (i64)mxPage) / (p->szPage + p->szExtra));
assert( p->pCache!=0 );
if( mxPage ){
if( mxPage<0 ){
mxPage = (int)((-1024*(i64)mxPage)/(p->szPage+p->szExtra));
}
p->szSpill = mxPage;
}
res = numberOfCachePages(p);
if (res < p->szSpill) res = p->szSpill;
if( res<p->szSpill ) res = p->szSpill;
return res;
}
/*
** Free up as much memory as possible from the page cache.
*/
void sqlite3PcacheShrink(PCache *pCache) {
assert(pCache->pCache != 0);
void sqlite3PcacheShrink(PCache *pCache){
assert( pCache->pCache!=0 );
pcache2.xShrink(pCache->pCache);
}
@ -816,17 +840,17 @@ void sqlite3PcacheShrink(PCache *pCache) {
** Return the size of the header added by this middleware layer
** in the page-cache hierarchy.
*/
int sqlite3HeaderSizePcache(void) { return ROUND8(sizeof(PgHdr)); }
int sqlite3HeaderSizePcache(void){ return ROUND8(sizeof(PgHdr)); }
/*
** Return the number of dirty pages currently in the cache, as a percentage
** of the configured cache size.
*/
int sqlite3PCachePercentDirty(PCache *pCache) {
int sqlite3PCachePercentDirty(PCache *pCache){
PgHdr *pDirty;
int nDirty = 0;
int nCache = numberOfCachePages(pCache);
for (pDirty = pCache->pDirty; pDirty; pDirty = pDirty->pDirtyNext) nDirty++;
int nDirty = 0;
int nCache = numberOfCachePages(pCache);
for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext) nDirty++;
return nCache ? (int)(((i64)nDirty * 100) / nCache) : 0;
}
@ -834,7 +858,9 @@ int sqlite3PCachePercentDirty(PCache *pCache) {
/*
** Return true if there are one or more dirty pages in the cache. Else false.
*/
int sqlite3PCacheIsDirty(PCache *pCache) { return (pCache->pDirty != 0); }
int sqlite3PCacheIsDirty(PCache *pCache){
return (pCache->pDirty!=0);
}
#endif
#if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)
@ -843,9 +869,9 @@ int sqlite3PCacheIsDirty(PCache *pCache) { return (pCache->pDirty != 0); }
** callback. This is only used if the SQLITE_CHECK_PAGES macro is
** defined.
*/
void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *)) {
void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *)){
PgHdr *pDirty;
for (pDirty = pCache->pDirty; pDirty; pDirty = pDirty->pDirtyNext) {
for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext){
xIter(pDirty);
}
}

File diff suppressed because it is too large Load Diff

View File

@ -13,7 +13,10 @@
**
*/
#include <assert.h>
#include <pthread.h>
#include <stdint.h>
#include <string.h>
#ifndef SQLITEINT_H
#define SQLITEINT_H
@ -32,6 +35,10 @@ typedef struct sqlite3_pcache_page {
void *pExtra; /* Extra information associated with the page */
} sqlite3_pcache_page;
#define ROUNDDOWN8(x) ((x) & ~7)
#define ROUND8(x) (((x) + 7) & ~7)
typedef u32 Pgno;
typedef struct Pager Pager;