Lines Matching refs:pCache
9793 /************** Include pcache.h in the middle of sqliteInt.h ****************/
9794 /************** Begin file pcache.h ******************************************/
9813 typedef struct PCache PCache;
9820 sqlite3_pcache_page *pPage; /* Pcache object page handle */
9832 ** Elements above are public. All that follows is private to pcache.c
9836 PCache *pCache; /* Cache that owns this page */
9871 PCache *pToInit /* Preallocated space for the PCache */
9875 SQLITE_PRIVATE void sqlite3PcacheSetPageSize(PCache *, int);
9877 /* Return the size in bytes of a PCache object. Used to preallocate
9885 SQLITE_PRIVATE int sqlite3PcacheFetch(PCache*, Pgno, int createFlag, PgHdr**);
9891 SQLITE_PRIVATE void sqlite3PcacheCleanAll(PCache*); /* Mark all dirty list pages as clean */
9897 SQLITE_PRIVATE void sqlite3PcacheTruncate(PCache*, Pgno x);
9900 SQLITE_PRIVATE PgHdr *sqlite3PcacheDirtyList(PCache*);
9903 SQLITE_PRIVATE void sqlite3PcacheClose(PCache*);
9906 SQLITE_PRIVATE void sqlite3PcacheClearSyncFlags(PCache *);
9909 SQLITE_PRIVATE void sqlite3PcacheClear(PCache*);
9912 SQLITE_PRIVATE int sqlite3PcacheRefCount(PCache*);
9920 SQLITE_PRIVATE int sqlite3PcachePagecount(PCache*);
9927 SQLITE_PRIVATE void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *));
9936 SQLITE_PRIVATE void sqlite3PcacheSetCachesize(PCache *, int);
9938 SQLITE_PRIVATE int sqlite3PcacheGetCachesize(PCache *);
9942 SQLITE_PRIVATE void sqlite3PcacheShrink(PCache*);
9945 /* Try to return memory used by the pcache module to the main memory heap */
9957 pcache.h **********************************************/
37945 /************** Begin file pcache.c ******************************************/
37963 struct PCache {
37974 sqlite3_pcache *pCache; /* Pluggable cache module */
37994 ** Check that the pCache->pSynced variable is set correctly. If it
37998 ** expensive_assert( pcacheCheckSynced(pCache) );
38000 static int pcacheCheckSynced(PCache *pCache){
38002 for(p=pCache->pDirtyTail; p!=pCache->pSynced; p=p->pDirtyPrev){
38013 PCache *p = pPage->pCache;
38054 PCache *p = pPage->pCache;
38081 PCache *pCache = p->pCache;
38082 if( pCache->bPurgeable ){
38084 pCache->pPage1 = 0;
38086 sqlite3GlobalConfig.pcache2.xUnpin(pCache->pCache, p->pPage, 0);
38112 ** Return the size in bytes of a PCache object.
38114 SQLITE_PRIVATE int sqlite3PcacheSize(void){ return sizeof(PCache); }
38117 ** Create a new PCache object. Storage space to hold the object
38128 PCache *p /* Preallocated space for the PCache */
38130 memset(p, 0, sizeof(PCache));
38141 ** Change the page size for PCache object. The caller must ensure that there
38144 SQLITE_PRIVATE void sqlite3PcacheSetPageSize(PCache *pCache, int szPage){
38145 assert( pCache->nRef==0 && pCache->pDirty==0 );
38146 if( pCache->pCache ){
38147 sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
38148 pCache->pCache = 0;
38149 pCache->pPage1 = 0;
38151 pCache->szPage = szPage;
38157 static int numberOfCachePages(PCache *p){
38169 PCache *pCache, /* Obtain the page from this cache */
38178 assert( pCache!=0 );
38185 if( !pCache->pCache ){
38192 pCache->szPage, pCache->szExtra + sizeof(PgHdr), pCache->bPurgeable
38197 sqlite3GlobalConfig.pcache2.xCachesize(p, numberOfCachePages(pCache));
38198 pCache->pCache = p;
38208 eCreate = createFlag==0 ? 0 : pCache->eCreate;
38209 assert( (createFlag*(1+(!pCache->bPurgeable||!pCache->pDirty)))==eCreate );
38210 pPage = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, eCreate);
38219 expensive_assert( pcacheCheckSynced(pCache) );
38220 for(pPg=pCache->pSynced;
38224 pCache->pSynced = pPg;
38226 for(pPg=pCache->pDirtyTail; pPg && pPg->nRef; pPg=pPg->pDirtyPrev);
38234 sqlite3GlobalConfig.pcache.xPagecount(pCache->pCache),
38235 numberOfCachePages(pCache));
38237 rc = pCache->xStress(pCache->pStress, pPg);
38243 pPage = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, 2);
38254 memset(pPgHdr->pExtra, 0, pCache->szExtra);
38255 pPgHdr->pCache = pCache;
38258 assert( pPgHdr->pCache==pCache );
38264 pCache->nRef++;
38268 pCache->pPage1 = pPgHdr;
38283 PCache *pCache = p->pCache;
38284 pCache->nRef--;
38309 PCache *pCache;
38314 pCache = p->pCache;
38315 pCache->nRef--;
38317 pCache->pPage1 = 0;
38319 sqlite3GlobalConfig.pcache2.xUnpin(pCache->pCache, p->pPage, 1);
38352 SQLITE_PRIVATE void sqlite3PcacheCleanAll(PCache *pCache){
38354 while( (p = pCache->pDirty)!=0 ){
38362 SQLITE_PRIVATE void sqlite3PcacheClearSyncFlags(PCache *pCache){
38364 for(p=pCache->pDirty; p; p=p->pDirtyNext){
38367 pCache->pSynced = pCache->pDirtyTail;
38374 PCache *pCache = p->pCache;
38377 sqlite3GlobalConfig.pcache2.xRekey(pCache->pCache, p->pPage, p->pgno,newPgno);
38394 SQLITE_PRIVATE void sqlite3PcacheTruncate(PCache *pCache, Pgno pgno){
38395 if( pCache->pCache ){
38398 for(p=pCache->pDirty; p; p=pNext){
38410 if( pgno==0 && pCache->pPage1 ){
38411 memset(pCache->pPage1->pData, 0, pCache->szPage);
38414 sqlite3GlobalConfig.pcache2.xTruncate(pCache->pCache, pgno+1);
38421 SQLITE_PRIVATE void sqlite3PcacheClose(PCache *pCache){
38422 if( pCache->pCache ){
38423 sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
38430 SQLITE_PRIVATE void sqlite3PcacheClear(PCache *pCache){
38431 sqlite3PcacheTruncate(pCache, 0);
38507 SQLITE_PRIVATE PgHdr *sqlite3PcacheDirtyList(PCache *pCache){
38509 for(p=pCache->pDirty; p; p=p->pDirtyNext){
38512 return pcacheSortDirtyList(pCache->pDirty);
38518 SQLITE_PRIVATE int sqlite3PcacheRefCount(PCache *pCache){
38519 return pCache->nRef;
38532 SQLITE_PRIVATE int sqlite3PcachePagecount(PCache *pCache){
38534 if( pCache->pCache ){
38535 nPage = sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache);
38544 SQLITE_PRIVATE int sqlite3PcacheGetCachesize(PCache *pCache){
38545 return numberOfCachePages(pCache);
38552 SQLITE_PRIVATE void sqlite3PcacheSetCachesize(PCache *pCache, int mxPage){
38553 pCache->szCache = mxPage;
38554 if( pCache->pCache ){
38555 sqlite3GlobalConfig.pcache2.xCachesize(pCache->pCache,
38556 numberOfCachePages(pCache));
38563 SQLITE_PRIVATE void sqlite3PcacheShrink(PCache *pCache){
38564 if( pCache->pCache ){
38565 sqlite3GlobalConfig.pcache2.xShrink(pCache->pCache);
38575 SQLITE_PRIVATE void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *)){
38577 for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext){
38583 /************** End of pcache.c **********************************************/
38610 /* Each page cache (or PCache) belongs to a PGroup. A PGroup is a set
38617 ** (1) Every PCache is the sole member of its own PGroup. There is
38618 ** one PGroup per PCache.
38623 ** Mode 1 uses more memory (since PCache instances are not able to rob
38676 ** PgHdr1.pCache->szPage bytes is allocated directly before this structure
38684 PCache1 *pCache; /* Cache that currently owns this page */
38710 int nSlot; /* The number of pcache slots */
38716 int nFreeSlot; /* Number of unused pcache slots */
38732 ** Macros to enter and leave the PCache LRU mutex.
38845 ** Return the size of a pcache allocation
38862 ** Allocate a new page object initially associated with cache pCache.
38864 static PgHdr1 *pcache1AllocPage(PCache1 *pCache){
38871 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
38872 pcache1LeaveMutex(pCache->pGroup);
38874 pPg = pcache1Alloc(pCache->szPage);
38875 p = sqlite3Malloc(sizeof(PgHdr1) + pCache->szExtra);
38882 pPg = pcache1Alloc(sizeof(PgHdr1) + pCache->szPage + pCache->szExtra);
38883 p = (PgHdr1 *)&((u8 *)pPg)[pCache->szPage];
38885 pcache1EnterMutex(pCache->pGroup);
38890 if( pCache->bPurgeable ){
38891 pCache->pGroup->nCurrentPage++;
38907 PCache1 *pCache = p->pCache;
38908 assert( sqlite3_mutex_held(p->pCache->pGroup->mutex) );
38913 if( pCache->bPurgeable ){
38914 pCache->pGroup->nCurrentPage--;
38952 static int pcache1UnderMemoryPressure(PCache1 *pCache){
38953 if( pcache1.nSlot && (pCache->szPage+pCache->szExtra)<=pcache1.szSlot ){
38967 ** The PCache mutex must be held when this function is called.
39013 PCache1 *pCache;
39018 pCache = pPage->pCache;
39019 pGroup = pCache->pGroup;
39036 pCache->nRecyclable--;
39048 PCache1 *pCache = pPage->pCache;
39051 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
39052 h = pPage->iKey % pCache->nHash;
39053 for(pp=&pCache->apHash[h]; (*pp)!=pPage; pp=&(*pp)->pNext);
39056 pCache->nPage--;
39067 assert( p->pCache->pGroup==pGroup );
39076 ** Discard all pages from cache pCache with a page number (key value)
39080 ** The PCache mutex must be held when this function is called.
39083 PCache1 *pCache, /* The cache to truncate */
39086 TESTONLY( unsigned int nPage = 0; ) /* To assert pCache->nPage is correct */
39088 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
39089 for(h=0; h<pCache->nHash; h++){
39090 PgHdr1 **pp = &pCache->apHash[h];
39094 pCache->nPage--;
39104 assert( pCache->nPage==nPage );
39143 PCache1 *pCache; /* The newly created page cache */
39148 ** The separateCache variable is true if each PCache has its own private
39169 pCache = (PCache1 *)sqlite3MallocZero(sz);
39170 if( pCache ){
39172 pGroup = (PGroup*)&pCache[1];
39177 pCache->pGroup = pGroup;
39178 pCache->szPage = szPage;
39179 pCache->szExtra = szExtra;
39180 pCache->bPurgeable = (bPurgeable ? 1 : 0);
39182 pCache->nMin = 10;
39184 pGroup->nMinPage += pCache->nMin;
39189 return (sqlite3_pcache *)pCache;
39198 PCache1 *pCache = (PCache1 *)p;
39199 if( pCache->bPurgeable ){
39200 PGroup *pGroup = pCache->pGroup;
39202 pGroup->nMaxPage += (nMax - pCache->nMax);
39204 pCache->nMax = nMax;
39205 pCache->n90pct = pCache->nMax*9/10;
39217 PCache1 *pCache = (PCache1*)p;
39218 if( pCache->bPurgeable ){
39219 PGroup *pGroup = pCache->pGroup;
39235 PCache1 *pCache = (PCache1*)p;
39236 pcache1EnterMutex(pCache->pGroup);
39237 n = pCache->nPage;
39238 pcache1LeaveMutex(pCache->pGroup);
39254 ** the calling function (pcache.c) will never have a createFlag of 1 on
39302 PCache1 *pCache = (PCache1 *)p;
39307 assert( pCache->bPurgeable || createFlag!=1 );
39308 assert( pCache->bPurgeable || pCache->nMin==0 );
39309 assert( pCache->bPurgeable==0 || pCache->nMin==10 );
39310 assert( pCache->nMin==0 || pCache->bPurgeable );
39311 pcache1EnterMutex(pGroup = pCache->pGroup);
39314 if( pCache->nHash>0 ){
39315 unsigned int h = iKey % pCache->nHash;
39316 for(pPage=pCache->apHash[h]; pPage&&pPage->iKey!=iKey; pPage=pPage->pNext);
39336 pGroup = pCache->pGroup;
39340 assert( pCache->nPage >= pCache->nRecyclable );
39341 nPinned = pCache->nPage - pCache->nRecyclable;
39343 assert( pCache->n90pct == pCache->nMax*9/10 );
39346 || nPinned>=pCache->n90pct
39347 || pcache1UnderMemoryPressure(pCache)
39352 if( pCache->nPage>=pCache->nHash && pcache1ResizeHash(pCache) ){
39355 assert( pCache->nHash>0 && pCache->apHash );
39358 if( pCache->bPurgeable && pGroup->pLruTail && (
39359 (pCache->nPage+1>=pCache->nMax)
39361 || pcache1UnderMemoryPressure(pCache)
39368 pOther = pPage->pCache;
39371 ** and pCache. Assert that we can verify this by comparing sums. */
39372 assert( (pCache->szPage & (pCache->szPage-1))==0 && pCache->szPage>=512 );
39373 assert( pCache->szExtra<512 );
39377 if( pOther->szPage+pOther->szExtra != pCache->szPage+pCache->szExtra ){
39381 pGroup->nCurrentPage -= (pOther->bPurgeable - pCache->bPurgeable);
39390 pPage = pcache1AllocPage(pCache);
39395 unsigned int h = iKey % pCache->nHash;
39396 pCache->nPage++;
39398 pPage->pNext = pCache->apHash[h];
39399 pPage->pCache = pCache;
39404 pCache->apHash[h] = pPage;
39408 if( pPage && iKey>pCache->iMaxKey ){
39409 pCache->iMaxKey = iKey;
39426 PCache1 *pCache = (PCache1 *)p;
39428 PGroup *pGroup = pCache->pGroup;
39430 assert( pPage->pCache==pCache );
39453 pCache->nRecyclable++;
39457 pcache1LeaveMutex(pCache->pGroup);
39469 PCache1 *pCache = (PCache1 *)p;
39474 assert( pPage->pCache==pCache );
39476 pcache1EnterMutex(pCache->pGroup);
39478 h = iOld%pCache->nHash;
39479 pp = &pCache->apHash[h];
39485 h = iNew%pCache->nHash;
39487 pPage->pNext = pCache->apHash[h];
39488 pCache->apHash[h] = pPage;
39489 if( iNew>pCache->iMaxKey ){
39490 pCache->iMaxKey = iNew;
39493 pcache1LeaveMutex(pCache->pGroup);
39504 PCache1 *pCache = (PCache1 *)p;
39505 pcache1EnterMutex(pCache->pGroup);
39506 if( iLimit<=pCache->iMaxKey ){
39507 pcache1TruncateUnsafe(pCache, iLimit);
39508 pCache->iMaxKey = iLimit-1;
39510 pcache1LeaveMutex(pCache->pGroup);
39519 PCache1 *pCache = (PCache1 *)p;
39520 PGroup *pGroup = pCache->pGroup;
39521 assert( pCache->bPurgeable || (pCache->nMax==0 && pCache->nMin==0) );
39523 pcache1TruncateUnsafe(pCache, 0);
39524 assert( pGroup->nMaxPage >= pCache->nMax );
39525 pGroup->nMaxPage -= pCache->nMax;
39526 assert( pGroup->nMinPage >= pCache->nMin );
39527 pGroup->nMinPage -= pCache->nMin;
39531 sqlite3_free(pCache->apHash);
39532 sqlite3_free(pCache);
40798 ** the pcache module to the pagerStress() routine to write cached data
40804 ** comes up during savepoint rollback that requires the pcache module
40965 PCache *pPCache; /* Pointer to page cache object */
44633 ** This function is called by the pcache layer when it has reached some
44802 int pcacheSize = sqlite3PcacheSize(); /* Bytes to allocate for PCache */
44872 /* Allocate memory for the Pager structure, PCache object, the
44877 ** PCache object (sqlite3PcacheSize() bytes)
44886 ROUND8(pcacheSize) + /* PCache object */
44901 pPager->pPCache = (PCache*)(pPtr += ROUND8(sizeof(*pPager)));
45015 /* Initialize the PCache object. */
45491 ** read from the database file. In some cases, the pcache module may
45565 ** Otherwise, request the page from the PCache layer. */
45616 /* In this case the pcache already contains an initialized copy of
122374 ** The following mutex is what serializes access to the appdef pcache xInit