Lines Matching refs:pCache
8476 /************** Include pcache.h in the middle of sqliteInt.h ****************/
8477 /************** Begin file pcache.h ******************************************/
8496 typedef struct PCache PCache;
8514 ** Elements above are public. All that follows is private to pcache.c
8518 PCache *pCache; /* Cache that owns this page */
8551 PCache *pToInit /* Preallocated space for the PCache */
8555 SQLITE_PRIVATE void sqlite3PcacheSetPageSize(PCache *, int);
8557 /* Return the size in bytes of a PCache object. Used to preallocate
8565 SQLITE_PRIVATE int sqlite3PcacheFetch(PCache*, Pgno, int createFlag, PgHdr**);
8571 SQLITE_PRIVATE void sqlite3PcacheCleanAll(PCache*); /* Mark all dirty list pages as clean */
8577 SQLITE_PRIVATE void sqlite3PcacheTruncate(PCache*, Pgno x);
8580 SQLITE_PRIVATE PgHdr *sqlite3PcacheDirtyList(PCache*);
8583 SQLITE_PRIVATE void sqlite3PcacheClose(PCache*);
8586 SQLITE_PRIVATE void sqlite3PcacheClearSyncFlags(PCache *);
8589 SQLITE_PRIVATE void sqlite3PcacheClear(PCache*);
8592 SQLITE_PRIVATE int sqlite3PcacheRefCount(PCache*);
8600 SQLITE_PRIVATE int sqlite3PcachePagecount(PCache*);
8607 SQLITE_PRIVATE void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *));
8616 SQLITE_PRIVATE void sqlite3PcacheSetCachesize(PCache *, int);
8618 SQLITE_PRIVATE int sqlite3PcacheGetCachesize(PCache *);
8622 /* Try to return memory used by the pcache module to the main memory heap */
8634 /************** End of pcache.h **********************************************/
10773 sqlite3_pcache_methods pcache; /* Low-level page-cache interface */
11730 {0,0,0,0,0,0,0,0,0,0,0}, /* pcache */
34326 /************** Begin file pcache.c ******************************************/
34344 struct PCache {
34354 sqlite3_pcache *pCache; /* Pluggable cache module */
34374 ** Check that the pCache->pSynced variable is set correctly. If it
34378 ** expensive_assert( pcacheCheckSynced(pCache) );
34380 static int pcacheCheckSynced(PCache *pCache){
34382 for(p=pCache->pDirtyTail; p!=pCache->pSynced; p=p->pDirtyPrev){
34393 PCache *p = pPage->pCache;
34430 PCache *p = pPage->pCache;
34454 PCache *pCache = p->pCache;
34455 if( pCache->bPurgeable ){
34457 pCache->pPage1 = 0;
34459 sqlite3GlobalConfig.pcache.xUnpin(pCache->pCache, p, 0);
34469 if( sqlite3GlobalConfig.pcache.xInit==0 ){
34475 return sqlite3GlobalConfig.pcache.xInit(sqlite3GlobalConfig.pcache.pArg);
34478 if( sqlite3GlobalConfig.pcache.xShutdown ){
34480 sqlite3GlobalConfig.pcache.xShutdown(sqlite3GlobalConfig.pcache.pArg);
34485 ** Return the size in bytes of a PCache object.
34487 SQLITE_PRIVATE int sqlite3PcacheSize(void){ return sizeof(PCache); }
34490 ** Create a new PCache object. Storage space to hold the object
34501 PCache *p /* Preallocated space for the PCache */
34503 memset(p, 0, sizeof(PCache));
34513 ** Change the page size for PCache object. The caller must ensure that there
34516 SQLITE_PRIVATE void sqlite3PcacheSetPageSize(PCache *pCache, int szPage){
34517 assert( pCache->nRef==0 && pCache->pDirty==0 );
34518 if( pCache->pCache ){
34519 sqlite3GlobalConfig.pcache.xDestroy(pCache->pCache);
34520 pCache->pCache = 0;
34521 pCache->pPage1 = 0;
34523 pCache->szPage = szPage;
34530 PCache *pCache, /* Obtain the page from this cache */
34538 assert( pCache!=0 );
34545 if( !pCache->pCache && createFlag ){
34548 nByte = pCache->szPage + pCache->szExtra + sizeof(PgHdr);
34549 p = sqlite3GlobalConfig.pcache.xCreate(nByte, pCache->bPurgeable);
34553 sqlite3GlobalConfig.pcache.xCachesize(p, pCache->nMax);
34554 pCache->pCache = p;
34557 eCreate = createFlag * (1 + (!pCache->bPurgeable || !pCache->pDirty));
34558 if( pCache->pCache ){
34559 pPage = sqlite3GlobalConfig.pcache.xFetch(pCache->pCache, pgno, eCreate);
34570 expensive_assert( pcacheCheckSynced(pCache) );
34571 for(pPg=pCache->pSynced;
34575 pCache->pSynced = pPg;
34577 for(pPg=pCache->pDirtyTail; pPg && pPg->nRef; pPg=pPg->pDirtyPrev);
34581 rc = pCache->xStress(pCache->pStress, pPg);
34587 pPage = sqlite3GlobalConfig.pcache.xFetch(pCache->pCache, pgno, 2);
34594 pPage->pExtra = (void*)&((char *)pPage->pData)[pCache->szPage];
34595 memset(pPage->pExtra, 0, pCache->szExtra);
34596 pPage->pCache = pCache;
34599 assert( pPage->pCache==pCache );
34602 assert( pPage->pExtra==(void *)&((char *)&pPage[1])[pCache->szPage] );
34605 pCache->nRef++;
34609 pCache->pPage1 = pPage;
34624 PCache *pCache = p->pCache;
34625 pCache->nRef--;
34650 PCache *pCache;
34655 pCache = p->pCache;
34656 pCache->nRef--;
34658 pCache->pPage1 = 0;
34660 sqlite3GlobalConfig.pcache.xUnpin(pCache->pCache, p, 1);
34693 SQLITE_PRIVATE void sqlite3PcacheCleanAll(PCache *pCache){
34695 while( (p = pCache->pDirty)!=0 ){
34703 SQLITE_PRIVATE void sqlite3PcacheClearSyncFlags(PCache *pCache){
34705 for(p=pCache->pDirty; p; p=p->pDirtyNext){
34708 pCache->pSynced = pCache->pDirtyTail;
34715 PCache *pCache = p->pCache;
34718 sqlite3GlobalConfig.pcache.xRekey(pCache->pCache, p, p->pgno, newPgno);
34735 SQLITE_PRIVATE void sqlite3PcacheTruncate(PCache *pCache, Pgno pgno){
34736 if( pCache->pCache ){
34739 for(p=pCache->pDirty; p; p=pNext){
34751 if( pgno==0 && pCache->pPage1 ){
34752 memset(pCache->pPage1->pData, 0, pCache->szPage);
34755 sqlite3GlobalConfig.pcache.xTruncate(pCache->pCache, pgno+1);
34762 SQLITE_PRIVATE void sqlite3PcacheClose(PCache *pCache){
34763 if( pCache->pCache ){
34764 sqlite3GlobalConfig.pcache.xDestroy(pCache->pCache);
34771 SQLITE_PRIVATE void sqlite3PcacheClear(PCache *pCache){
34772 sqlite3PcacheTruncate(pCache, 0);
34848 SQLITE_PRIVATE PgHdr *sqlite3PcacheDirtyList(PCache *pCache){
34850 for(p=pCache->pDirty; p; p=p->pDirtyNext){
34853 return pcacheSortDirtyList(pCache->pDirty);
34859 SQLITE_PRIVATE int sqlite3PcacheRefCount(PCache *pCache){
34860 return pCache->nRef;
34873 SQLITE_PRIVATE int sqlite3PcachePagecount(PCache *pCache){
34875 if( pCache->pCache ){
34876 nPage = sqlite3GlobalConfig.pcache.xPagecount(pCache->pCache);
34885 SQLITE_PRIVATE int sqlite3PcacheGetCachesize(PCache *pCache){
34886 return pCache->nMax;
34893 SQLITE_PRIVATE void sqlite3PcacheSetCachesize(PCache *pCache, int mxPage){
34894 pCache->nMax = mxPage;
34895 if( pCache->pCache ){
34896 sqlite3GlobalConfig.pcache.xCachesize(pCache->pCache, mxPage);
34906 SQLITE_PRIVATE void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *)){
34908 for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext){
34914 /************** End of pcache.c **********************************************/
34941 /* Each page cache (or PCache) belongs to a PGroup. A PGroup is a set
34948 ** (1) Every PCache is the sole member of its own PGroup. There is
34949 ** one PGroup per PCache.
34954 ** Mode 1 uses more memory (since PCache instances are not able to rob
35006 ** structure. A buffer of PgHdr1.pCache->szPage bytes is allocated
35013 PCache1 *pCache; /* Cache that currently owns this page */
35039 int nSlot; /* The number of pcache slots */
35044 int nFreeSlot; /* Number of unused pcache slots */
35070 ** assert( PGHDR1_TO_PAGE(PAGE_TO_PGHDR1(pCache, X))==X );
35072 #define PGHDR1_TO_PAGE(p) (void*)(((char*)p) - p->pCache->szPage)
35076 ** Macros to enter and leave the PCache LRU mutex.
35184 ** Return the size of a pcache allocation
35201 ** Allocate a new page object initially associated with cache pCache.
35203 static PgHdr1 *pcache1AllocPage(PCache1 *pCache){
35204 int nByte = sizeof(PgHdr1) + pCache->szPage;
35208 p = PAGE_TO_PGHDR1(pCache, pPg);
35209 if( pCache->bPurgeable ){
35210 pCache->pGroup->nCurrentPage++;
35227 PCache1 *pCache = p->pCache;
35228 if( pCache->bPurgeable ){
35229 pCache->pGroup->nCurrentPage--;
35268 static int pcache1UnderMemoryPressure(PCache1 *pCache){
35269 if( pcache1.nSlot && pCache->szPage<=pcache1.szSlot ){
35283 ** The PCache mutex must be held when this function is called.
35332 PCache1 *pCache;
35336 pCache = pPage->pCache;
35337 pGroup = pCache->pGroup;
35354 pPage->pCache->nRecyclable--;
35367 PCache1 *pCache = pPage->pCache;
35370 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
35371 h = pPage->iKey % pCache->nHash;
35372 for(pp=&pCache->apHash[h]; (*pp)!=pPage; pp=&(*pp)->pNext);
35375 pCache->nPage--;
35386 assert( p->pCache->pGroup==pGroup );
35394 ** Discard all pages from cache pCache with a page number (key value)
35398 ** The PCache mutex must be held when this function is called.
35401 PCache1 *pCache, /* The cache to truncate */
35404 TESTONLY( unsigned int nPage = 0; ) /* To assert pCache->nPage is correct */
35406 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
35407 for(h=0; h<pCache->nHash; h++){
35408 PgHdr1 **pp = &pCache->apHash[h];
35412 pCache->nPage--;
35422 assert( pCache->nPage==nPage );
35461 PCache1 *pCache; /* The newly created page cache */
35466 ** The separateCache variable is true if each PCache has its own private
35488 pCache = (PCache1 *)sqlite3_malloc(sz);
35489 if( pCache ){
35490 memset(pCache, 0, sz);
35492 pGroup = (PGroup*)&pCache[1];
35497 pCache->pGroup = pGroup;
35498 pCache->szPage = szPage;
35499 pCache->bPurgeable = (bPurgeable ? 1 : 0);
35501 pCache->nMin = 10;
35503 pGroup->nMinPage += pCache->nMin;
35508 return (sqlite3_pcache *)pCache;
35517 PCache1 *pCache = (PCache1 *)p;
35518 if( pCache->bPurgeable ){
35519 PGroup *pGroup = pCache->pGroup;
35521 pGroup->nMaxPage += (nMax - pCache->nMax);
35523 pCache->nMax = nMax;
35524 pCache->n90pct = pCache->nMax*9/10;
35535 PCache1 *pCache = (PCache1*)p;
35536 pcache1EnterMutex(pCache->pGroup);
35537 n = pCache->nPage;
35538 pcache1LeaveMutex(pCache->pGroup);
35554 ** the calling function (pcache.c) will never have a createFlag of 1 on
35598 PCache1 *pCache = (PCache1 *)p;
35602 assert( pCache->bPurgeable || createFlag!=1 );
35603 assert( pCache->bPurgeable || pCache->nMin==0 );
35604 assert( pCache->bPurgeable==0 || pCache->nMin==10 );
35605 assert( pCache->nMin==0 || pCache->bPurgeable );
35606 pcache1EnterMutex(pGroup = pCache->pGroup);
35609 if( pCache->nHash>0 ){
35610 unsigned int h = iKey % pCache->nHash;
35611 for(pPage=pCache->apHash[h]; pPage&&pPage->iKey!=iKey; pPage=pPage->pNext);
35628 pGroup = pCache->pGroup;
35633 nPinned = pCache->nPage - pCache->nRecyclable;
35636 assert( pCache->n90pct == pCache->nMax*9/10 );
35639 || nPinned>=(int)pCache->n90pct
35640 || pcache1UnderMemoryPressure(pCache)
35645 if( pCache->nPage>=pCache->nHash && pcache1ResizeHash(pCache) ){
35650 if( pCache->bPurgeable && pGroup->pLruTail && (
35651 (pCache->nPage+1>=pCache->nMax)
35653 || pcache1UnderMemoryPressure(pCache)
35659 if( (pOtherCache = pPage->pCache)->szPage!=pCache->szPage ){
35664 (pOtherCache->bPurgeable - pCache->bPurgeable);
35674 pPage = pcache1AllocPage(pCache);
35680 unsigned int h = iKey % pCache->nHash;
35681 pCache->nPage++;
35683 pPage->pNext = pCache->apHash[h];
35684 pPage->pCache = pCache;
35688 pCache->apHash[h] = pPage;
35692 if( pPage && iKey>pCache->iMaxKey ){
35693 pCache->iMaxKey = iKey;
35706 PCache1 *pCache = (PCache1 *)p;
35707 PgHdr1 *pPage = PAGE_TO_PGHDR1(pCache, pPg);
35708 PGroup *pGroup = pCache->pGroup;
35710 assert( pPage->pCache==pCache );
35732 pCache->nRecyclable++;
35735 pcache1LeaveMutex(pCache->pGroup);
35747 PCache1 *pCache = (PCache1 *)p;
35748 PgHdr1 *pPage = PAGE_TO_PGHDR1(pCache, pPg);
35752 assert( pPage->pCache==pCache );
35754 pcache1EnterMutex(pCache->pGroup);
35756 h = iOld%pCache->nHash;
35757 pp = &pCache->apHash[h];
35763 h = iNew%pCache->nHash;
35765 pPage->pNext = pCache->apHash[h];
35766 pCache->apHash[h] = pPage;
35767 if( iNew>pCache->iMaxKey ){
35768 pCache->iMaxKey = iNew;
35771 pcache1LeaveMutex(pCache->pGroup);
35782 PCache1 *pCache = (PCache1 *)p;
35783 pcache1EnterMutex(pCache->pGroup);
35784 if( iLimit<=pCache->iMaxKey ){
35785 pcache1TruncateUnsafe(pCache, iLimit);
35786 pCache->iMaxKey = iLimit-1;
35788 pcache1LeaveMutex(pCache->pGroup);
35797 PCache1 *pCache = (PCache1 *)p;
35798 PGroup *pGroup = pCache->pGroup;
35799 assert( pCache->bPurgeable || (pCache->nMax==0 && pCache->nMin==0) );
35801 pcache1TruncateUnsafe(pCache, 0);
35802 pGroup->nMaxPage -= pCache->nMax;
35803 pGroup->nMinPage -= pCache->nMin;
35807 sqlite3_free(pCache->apHash);
35808 sqlite3_free(pCache);
36955 ** (calls made by the pcache module to the pagerStress() routine to
36960 ** comes up during savepoint rollback that requires the pcache module
37115 PCache *pPCache; /* Pointer to page cache object */
40566 ** This function is called by the pcache layer when it has reached some
40730 int pcacheSize = sqlite3PcacheSize(); /* Bytes to allocate for PCache */
40785 /* Allocate memory for the Pager structure, PCache object, the
40790 ** PCache object (sqlite3PcacheSize() bytes)
40799 ROUND8(pcacheSize) + /* PCache object */
40814 pPager->pPCache = (PCache*)(pPtr += ROUND8(sizeof(*pPager)));
40913 /* Initialize the PCache object. */
41344 ** read from the database file. In some cases, the pcache module may
41404 ** Otherwise, request the page from the PCache layer. */
41422 /* In this case the pcache already contains an initialized copy of
107425 ** The following mutex is what serializes access to the appdef pcache xInit
107604 sqlite3GlobalConfig.pcache = *va_arg(ap, sqlite3_pcache_methods*);
107609 if( sqlite3GlobalConfig.pcache.xInit==0 ){
107612 *va_arg(ap, sqlite3_pcache_methods*) = sqlite3GlobalConfig.pcache;
109888 ** Return the size of a pcache header in bytes.