Annotation of embedaddon/sqlite3/src/pcache1.c, revision 1.1.1.1

1.1       misho       1: /*
                      2: ** 2008 November 05
                      3: **
                      4: ** The author disclaims copyright to this source code.  In place of
                      5: ** 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: **
                     13: ** This file implements the default page cache implementation (the
                     14: ** sqlite3_pcache interface). It also contains part of the implementation
                     15: ** of the SQLITE_CONFIG_PAGECACHE and sqlite3_release_memory() features.
                     16: ** If the default page cache implementation is overriden, then neither of
                     17: ** these two features are available.
                     18: */
                     19: 
                     20: #include "sqliteInt.h"
                     21: 
                     22: typedef struct PCache1 PCache1;
                     23: typedef struct PgHdr1 PgHdr1;
                     24: typedef struct PgFreeslot PgFreeslot;
                     25: typedef struct PGroup PGroup;
                     26: 
                     27: /* Each page cache (or PCache) belongs to a PGroup.  A PGroup is a set 
                     28: ** of one or more PCaches that are able to recycle each others unpinned
                     29: ** pages when they are under memory pressure.  A PGroup is an instance of
                     30: ** the following object.
                     31: **
                     32: ** This page cache implementation works in one of two modes:
                     33: **
                     34: **   (1)  Every PCache is the sole member of its own PGroup.  There is
                     35: **        one PGroup per PCache.
                     36: **
                     37: **   (2)  There is a single global PGroup that all PCaches are a member
                     38: **        of.
                     39: **
                     40: ** Mode 1 uses more memory (since PCache instances are not able to rob
                     41: ** unused pages from other PCaches) but it also operates without a mutex,
                     42: ** and is therefore often faster.  Mode 2 requires a mutex in order to be
                     43: ** threadsafe, but recycles pages more efficiently.
                     44: **
                     45: ** For mode (1), PGroup.mutex is NULL.  For mode (2) there is only a single
                     46: ** PGroup which is the pcache1.grp global variable and its mutex is
                     47: ** SQLITE_MUTEX_STATIC_LRU.
                     48: */
                     49: struct PGroup {
                     50:   sqlite3_mutex *mutex;          /* MUTEX_STATIC_LRU or NULL */
                     51:   unsigned int nMaxPage;         /* Sum of nMax for purgeable caches */
                     52:   unsigned int nMinPage;         /* Sum of nMin for purgeable caches */
                     53:   unsigned int mxPinned;         /* nMaxpage + 10 - nMinPage */
                     54:   unsigned int nCurrentPage;     /* Number of purgeable pages allocated */
                     55:   PgHdr1 *pLruHead, *pLruTail;   /* LRU list of unpinned pages */
                     56: };
                     57: 
                     58: /* Each page cache is an instance of the following object.  Every
                     59: ** open database file (including each in-memory database and each
                     60: ** temporary or transient database) has a single page cache which
                     61: ** is an instance of this object.
                     62: **
                     63: ** Pointers to structures of this type are cast and returned as 
                     64: ** opaque sqlite3_pcache* handles.
                     65: */
                     66: struct PCache1 {
                     67:   /* Cache configuration parameters. Page size (szPage) and the purgeable
                     68:   ** flag (bPurgeable) are set when the cache is created. nMax may be 
                     69:   ** modified at any time by a call to the pcache1Cachesize() method.
                     70:   ** The PGroup mutex must be held when accessing nMax.
                     71:   */
                     72:   PGroup *pGroup;                     /* PGroup this cache belongs to */
                     73:   int szPage;                         /* Size of allocated pages in bytes */
                     74:   int szExtra;                        /* Size of extra space in bytes */
                     75:   int bPurgeable;                     /* True if cache is purgeable */
                     76:   unsigned int nMin;                  /* Minimum number of pages reserved */
                     77:   unsigned int nMax;                  /* Configured "cache_size" value */
                     78:   unsigned int n90pct;                /* nMax*9/10 */
                     79: 
                     80:   /* Hash table of all pages. The following variables may only be accessed
                     81:   ** when the accessor is holding the PGroup mutex.
                     82:   */
                     83:   unsigned int nRecyclable;           /* Number of pages in the LRU list */
                     84:   unsigned int nPage;                 /* Total number of pages in apHash */
                     85:   unsigned int nHash;                 /* Number of slots in apHash[] */
                     86:   PgHdr1 **apHash;                    /* Hash table for fast lookup by key */
                     87: 
                     88:   unsigned int iMaxKey;               /* Largest key seen since xTruncate() */
                     89: };
                     90: 
                     91: /*
                     92: ** Each cache entry is represented by an instance of the following 
                     93: ** structure. Unless SQLITE_PCACHE_SEPARATE_HEADER is defined, a buffer of
                     94: ** PgHdr1.pCache->szPage bytes is allocated directly before this structure 
                     95: ** in memory.
                     96: */
                     97: struct PgHdr1 {
                     98:   sqlite3_pcache_page page;
                     99:   unsigned int iKey;             /* Key value (page number) */
                    100:   PgHdr1 *pNext;                 /* Next in hash table chain */
                    101:   PCache1 *pCache;               /* Cache that currently owns this page */
                    102:   PgHdr1 *pLruNext;              /* Next in LRU list of unpinned pages */
                    103:   PgHdr1 *pLruPrev;              /* Previous in LRU list of unpinned pages */
                    104: };
                    105: 
                    106: /*
                    107: ** Free slots in the allocator used to divide up the buffer provided using
                    108: ** the SQLITE_CONFIG_PAGECACHE mechanism.
                    109: */
                    110: struct PgFreeslot {
                    111:   PgFreeslot *pNext;  /* Next free slot */
                    112: };
                    113: 
                    114: /*
                    115: ** Global data used by this cache.
                    116: */
                    117: static SQLITE_WSD struct PCacheGlobal {
                    118:   PGroup grp;                    /* The global PGroup for mode (2) */
                    119: 
                    120:   /* Variables related to SQLITE_CONFIG_PAGECACHE settings.  The
                    121:   ** szSlot, nSlot, pStart, pEnd, nReserve, and isInit values are all
                    122:   ** fixed at sqlite3_initialize() time and do not require mutex protection.
                    123:   ** The nFreeSlot and pFree values do require mutex protection.
                    124:   */
                    125:   int isInit;                    /* True if initialized */
                    126:   int szSlot;                    /* Size of each free slot */
                    127:   int nSlot;                     /* The number of pcache slots */
                    128:   int nReserve;                  /* Try to keep nFreeSlot above this */
                    129:   void *pStart, *pEnd;           /* Bounds of pagecache malloc range */
                    130:   /* Above requires no mutex.  Use mutex below for variable that follow. */
                    131:   sqlite3_mutex *mutex;          /* Mutex for accessing the following: */
                    132:   int nFreeSlot;                 /* Number of unused pcache slots */
                    133:   PgFreeslot *pFree;             /* Free page blocks */
                    134:   /* The following value requires a mutex to change.  We skip the mutex on
                    135:   ** reading because (1) most platforms read a 32-bit integer atomically and
                    136:   ** (2) even if an incorrect value is read, no great harm is done since this
                    137:   ** is really just an optimization. */
                    138:   int bUnderPressure;            /* True if low on PAGECACHE memory */
                    139: } pcache1_g;
                    140: 
                    141: /*
                    142: ** All code in this file should access the global structure above via the
                    143: ** alias "pcache1". This ensures that the WSD emulation is used when
                    144: ** compiling for systems that do not support real WSD.
                    145: */
                    146: #define pcache1 (GLOBAL(struct PCacheGlobal, pcache1_g))
                    147: 
                    148: /*
                    149: ** Macros to enter and leave the PCache LRU mutex.
                    150: */
                    151: #define pcache1EnterMutex(X) sqlite3_mutex_enter((X)->mutex)
                    152: #define pcache1LeaveMutex(X) sqlite3_mutex_leave((X)->mutex)
                    153: 
                    154: /******************************************************************************/
                    155: /******** Page Allocation/SQLITE_CONFIG_PCACHE Related Functions **************/
                    156: 
                    157: /*
                    158: ** This function is called during initialization if a static buffer is 
                    159: ** supplied to use for the page-cache by passing the SQLITE_CONFIG_PAGECACHE
                    160: ** verb to sqlite3_config(). Parameter pBuf points to an allocation large
                    161: ** enough to contain 'n' buffers of 'sz' bytes each.
                    162: **
                    163: ** This routine is called from sqlite3_initialize() and so it is guaranteed
                    164: ** to be serialized already.  There is no need for further mutexing.
                    165: */
                    166: void sqlite3PCacheBufferSetup(void *pBuf, int sz, int n){
                    167:   if( pcache1.isInit ){
                    168:     PgFreeslot *p;
                    169:     sz = ROUNDDOWN8(sz);
                    170:     pcache1.szSlot = sz;
                    171:     pcache1.nSlot = pcache1.nFreeSlot = n;
                    172:     pcache1.nReserve = n>90 ? 10 : (n/10 + 1);
                    173:     pcache1.pStart = pBuf;
                    174:     pcache1.pFree = 0;
                    175:     pcache1.bUnderPressure = 0;
                    176:     while( n-- ){
                    177:       p = (PgFreeslot*)pBuf;
                    178:       p->pNext = pcache1.pFree;
                    179:       pcache1.pFree = p;
                    180:       pBuf = (void*)&((char*)pBuf)[sz];
                    181:     }
                    182:     pcache1.pEnd = pBuf;
                    183:   }
                    184: }
                    185: 
                    186: /*
                    187: ** Malloc function used within this file to allocate space from the buffer
                    188: ** configured using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no 
                    189: ** such buffer exists or there is no space left in it, this function falls 
                    190: ** back to sqlite3Malloc().
                    191: **
                    192: ** Multiple threads can run this routine at the same time.  Global variables
                    193: ** in pcache1 need to be protected via mutex.
                    194: */
                    195: static void *pcache1Alloc(int nByte){
                    196:   void *p = 0;
                    197:   assert( sqlite3_mutex_notheld(pcache1.grp.mutex) );
                    198:   sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, nByte);
                    199:   if( nByte<=pcache1.szSlot ){
                    200:     sqlite3_mutex_enter(pcache1.mutex);
                    201:     p = (PgHdr1 *)pcache1.pFree;
                    202:     if( p ){
                    203:       pcache1.pFree = pcache1.pFree->pNext;
                    204:       pcache1.nFreeSlot--;
                    205:       pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve;
                    206:       assert( pcache1.nFreeSlot>=0 );
                    207:       sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, 1);
                    208:     }
                    209:     sqlite3_mutex_leave(pcache1.mutex);
                    210:   }
                    211:   if( p==0 ){
                    212:     /* Memory is not available in the SQLITE_CONFIG_PAGECACHE pool.  Get
                    213:     ** it from sqlite3Malloc instead.
                    214:     */
                    215:     p = sqlite3Malloc(nByte);
                    216:     if( p ){
                    217:       int sz = sqlite3MallocSize(p);
                    218:       sqlite3_mutex_enter(pcache1.mutex);
                    219:       sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, sz);
                    220:       sqlite3_mutex_leave(pcache1.mutex);
                    221:     }
                    222:     sqlite3MemdebugSetType(p, MEMTYPE_PCACHE);
                    223:   }
                    224:   return p;
                    225: }
                    226: 
                    227: /*
                    228: ** Free an allocated buffer obtained from pcache1Alloc().
                    229: */
                    230: static int pcache1Free(void *p){
                    231:   int nFreed = 0;
                    232:   if( p==0 ) return 0;
                    233:   if( p>=pcache1.pStart && p<pcache1.pEnd ){
                    234:     PgFreeslot *pSlot;
                    235:     sqlite3_mutex_enter(pcache1.mutex);
                    236:     sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, -1);
                    237:     pSlot = (PgFreeslot*)p;
                    238:     pSlot->pNext = pcache1.pFree;
                    239:     pcache1.pFree = pSlot;
                    240:     pcache1.nFreeSlot++;
                    241:     pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve;
                    242:     assert( pcache1.nFreeSlot<=pcache1.nSlot );
                    243:     sqlite3_mutex_leave(pcache1.mutex);
                    244:   }else{
                    245:     assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) );
                    246:     sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
                    247:     nFreed = sqlite3MallocSize(p);
                    248:     sqlite3_mutex_enter(pcache1.mutex);
                    249:     sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, -nFreed);
                    250:     sqlite3_mutex_leave(pcache1.mutex);
                    251:     sqlite3_free(p);
                    252:   }
                    253:   return nFreed;
                    254: }
                    255: 
                    256: #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
                    257: /*
                    258: ** Return the size of a pcache allocation
                    259: */
                    260: static int pcache1MemSize(void *p){
                    261:   if( p>=pcache1.pStart && p<pcache1.pEnd ){
                    262:     return pcache1.szSlot;
                    263:   }else{
                    264:     int iSize;
                    265:     assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) );
                    266:     sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
                    267:     iSize = sqlite3MallocSize(p);
                    268:     sqlite3MemdebugSetType(p, MEMTYPE_PCACHE);
                    269:     return iSize;
                    270:   }
                    271: }
                    272: #endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
                    273: 
                    274: /*
                    275: ** Allocate a new page object initially associated with cache pCache.
                    276: */
                    277: static PgHdr1 *pcache1AllocPage(PCache1 *pCache){
                    278:   PgHdr1 *p = 0;
                    279:   void *pPg;
                    280: 
                    281:   /* The group mutex must be released before pcache1Alloc() is called. This
                    282:   ** is because it may call sqlite3_release_memory(), which assumes that 
                    283:   ** this mutex is not held. */
                    284:   assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
                    285:   pcache1LeaveMutex(pCache->pGroup);
                    286: #ifdef SQLITE_PCACHE_SEPARATE_HEADER
                    287:   pPg = pcache1Alloc(pCache->szPage);
                    288:   p = sqlite3Malloc(sizeof(PgHdr1) + pCache->szExtra);
                    289:   if( !pPg || !p ){
                    290:     pcache1Free(pPg);
                    291:     sqlite3_free(p);
                    292:     pPg = 0;
                    293:   }
                    294: #else
                    295:   pPg = pcache1Alloc(sizeof(PgHdr1) + pCache->szPage + pCache->szExtra);
                    296:   p = (PgHdr1 *)&((u8 *)pPg)[pCache->szPage];
                    297: #endif
                    298:   pcache1EnterMutex(pCache->pGroup);
                    299: 
                    300:   if( pPg ){
                    301:     p->page.pBuf = pPg;
                    302:     p->page.pExtra = &p[1];
                    303:     if( pCache->bPurgeable ){
                    304:       pCache->pGroup->nCurrentPage++;
                    305:     }
                    306:     return p;
                    307:   }
                    308:   return 0;
                    309: }
                    310: 
                    311: /*
                    312: ** Free a page object allocated by pcache1AllocPage().
                    313: **
                    314: ** The pointer is allowed to be NULL, which is prudent.  But it turns out
                    315: ** that the current implementation happens to never call this routine
                    316: ** with a NULL pointer, so we mark the NULL test with ALWAYS().
                    317: */
                    318: static void pcache1FreePage(PgHdr1 *p){
                    319:   if( ALWAYS(p) ){
                    320:     PCache1 *pCache = p->pCache;
                    321:     assert( sqlite3_mutex_held(p->pCache->pGroup->mutex) );
                    322:     pcache1Free(p->page.pBuf);
                    323: #ifdef SQLITE_PCACHE_SEPARATE_HEADER
                    324:     sqlite3_free(p);
                    325: #endif
                    326:     if( pCache->bPurgeable ){
                    327:       pCache->pGroup->nCurrentPage--;
                    328:     }
                    329:   }
                    330: }
                    331: 
                    332: /*
                    333: ** Malloc function used by SQLite to obtain space from the buffer configured
                    334: ** using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no such buffer
                    335: ** exists, this function falls back to sqlite3Malloc().
                    336: */
                    337: void *sqlite3PageMalloc(int sz){
                    338:   return pcache1Alloc(sz);
                    339: }
                    340: 
                    341: /*
                    342: ** Free an allocated buffer obtained from sqlite3PageMalloc().
                    343: */
                    344: void sqlite3PageFree(void *p){
                    345:   pcache1Free(p);
                    346: }
                    347: 
                    348: 
                    349: /*
                    350: ** Return true if it desirable to avoid allocating a new page cache
                    351: ** entry.
                    352: **
                    353: ** If memory was allocated specifically to the page cache using
                    354: ** SQLITE_CONFIG_PAGECACHE but that memory has all been used, then
                    355: ** it is desirable to avoid allocating a new page cache entry because
                    356: ** presumably SQLITE_CONFIG_PAGECACHE was suppose to be sufficient
                    357: ** for all page cache needs and we should not need to spill the
                    358: ** allocation onto the heap.
                    359: **
                    360: ** Or, the heap is used for all page cache memory but the heap is
                    361: ** under memory pressure, then again it is desirable to avoid
                    362: ** allocating a new page cache entry in order to avoid stressing
                    363: ** the heap even further.
                    364: */
                    365: static int pcache1UnderMemoryPressure(PCache1 *pCache){
                    366:   if( pcache1.nSlot && (pCache->szPage+pCache->szExtra)<=pcache1.szSlot ){
                    367:     return pcache1.bUnderPressure;
                    368:   }else{
                    369:     return sqlite3HeapNearlyFull();
                    370:   }
                    371: }
                    372: 
                    373: /******************************************************************************/
                    374: /******** General Implementation Functions ************************************/
                    375: 
                    376: /*
                    377: ** This function is used to resize the hash table used by the cache passed
                    378: ** as the first argument.
                    379: **
                    380: ** The PCache mutex must be held when this function is called.
                    381: */
                    382: static int pcache1ResizeHash(PCache1 *p){
                    383:   PgHdr1 **apNew;
                    384:   unsigned int nNew;
                    385:   unsigned int i;
                    386: 
                    387:   assert( sqlite3_mutex_held(p->pGroup->mutex) );
                    388: 
                    389:   nNew = p->nHash*2;
                    390:   if( nNew<256 ){
                    391:     nNew = 256;
                    392:   }
                    393: 
                    394:   pcache1LeaveMutex(p->pGroup);
                    395:   if( p->nHash ){ sqlite3BeginBenignMalloc(); }
                    396:   apNew = (PgHdr1 **)sqlite3_malloc(sizeof(PgHdr1 *)*nNew);
                    397:   if( p->nHash ){ sqlite3EndBenignMalloc(); }
                    398:   pcache1EnterMutex(p->pGroup);
                    399:   if( apNew ){
                    400:     memset(apNew, 0, sizeof(PgHdr1 *)*nNew);
                    401:     for(i=0; i<p->nHash; i++){
                    402:       PgHdr1 *pPage;
                    403:       PgHdr1 *pNext = p->apHash[i];
                    404:       while( (pPage = pNext)!=0 ){
                    405:         unsigned int h = pPage->iKey % nNew;
                    406:         pNext = pPage->pNext;
                    407:         pPage->pNext = apNew[h];
                    408:         apNew[h] = pPage;
                    409:       }
                    410:     }
                    411:     sqlite3_free(p->apHash);
                    412:     p->apHash = apNew;
                    413:     p->nHash = nNew;
                    414:   }
                    415: 
                    416:   return (p->apHash ? SQLITE_OK : SQLITE_NOMEM);
                    417: }
                    418: 
                    419: /*
                    420: ** This function is used internally to remove the page pPage from the 
                    421: ** PGroup LRU list, if is part of it. If pPage is not part of the PGroup
                    422: ** LRU list, then this function is a no-op.
                    423: **
                    424: ** The PGroup mutex must be held when this function is called.
                    425: **
                    426: ** If pPage is NULL then this routine is a no-op.
                    427: */
                    428: static void pcache1PinPage(PgHdr1 *pPage){
                    429:   PCache1 *pCache;
                    430:   PGroup *pGroup;
                    431: 
                    432:   if( pPage==0 ) return;
                    433:   pCache = pPage->pCache;
                    434:   pGroup = pCache->pGroup;
                    435:   assert( sqlite3_mutex_held(pGroup->mutex) );
                    436:   if( pPage->pLruNext || pPage==pGroup->pLruTail ){
                    437:     if( pPage->pLruPrev ){
                    438:       pPage->pLruPrev->pLruNext = pPage->pLruNext;
                    439:     }
                    440:     if( pPage->pLruNext ){
                    441:       pPage->pLruNext->pLruPrev = pPage->pLruPrev;
                    442:     }
                    443:     if( pGroup->pLruHead==pPage ){
                    444:       pGroup->pLruHead = pPage->pLruNext;
                    445:     }
                    446:     if( pGroup->pLruTail==pPage ){
                    447:       pGroup->pLruTail = pPage->pLruPrev;
                    448:     }
                    449:     pPage->pLruNext = 0;
                    450:     pPage->pLruPrev = 0;
                    451:     pPage->pCache->nRecyclable--;
                    452:   }
                    453: }
                    454: 
                    455: 
                    456: /*
                    457: ** Remove the page supplied as an argument from the hash table 
                    458: ** (PCache1.apHash structure) that it is currently stored in.
                    459: **
                    460: ** The PGroup mutex must be held when this function is called.
                    461: */
                    462: static void pcache1RemoveFromHash(PgHdr1 *pPage){
                    463:   unsigned int h;
                    464:   PCache1 *pCache = pPage->pCache;
                    465:   PgHdr1 **pp;
                    466: 
                    467:   assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
                    468:   h = pPage->iKey % pCache->nHash;
                    469:   for(pp=&pCache->apHash[h]; (*pp)!=pPage; pp=&(*pp)->pNext);
                    470:   *pp = (*pp)->pNext;
                    471: 
                    472:   pCache->nPage--;
                    473: }
                    474: 
                    475: /*
                    476: ** If there are currently more than nMaxPage pages allocated, try
                    477: ** to recycle pages to reduce the number allocated to nMaxPage.
                    478: */
                    479: static void pcache1EnforceMaxPage(PGroup *pGroup){
                    480:   assert( sqlite3_mutex_held(pGroup->mutex) );
                    481:   while( pGroup->nCurrentPage>pGroup->nMaxPage && pGroup->pLruTail ){
                    482:     PgHdr1 *p = pGroup->pLruTail;
                    483:     assert( p->pCache->pGroup==pGroup );
                    484:     pcache1PinPage(p);
                    485:     pcache1RemoveFromHash(p);
                    486:     pcache1FreePage(p);
                    487:   }
                    488: }
                    489: 
                    490: /*
                    491: ** Discard all pages from cache pCache with a page number (key value) 
                    492: ** greater than or equal to iLimit. Any pinned pages that meet this 
                    493: ** criteria are unpinned before they are discarded.
                    494: **
                    495: ** The PCache mutex must be held when this function is called.
                    496: */
                    497: static void pcache1TruncateUnsafe(
                    498:   PCache1 *pCache,             /* The cache to truncate */
                    499:   unsigned int iLimit          /* Drop pages with this pgno or larger */
                    500: ){
                    501:   TESTONLY( unsigned int nPage = 0; )  /* To assert pCache->nPage is correct */
                    502:   unsigned int h;
                    503:   assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
                    504:   for(h=0; h<pCache->nHash; h++){
                    505:     PgHdr1 **pp = &pCache->apHash[h]; 
                    506:     PgHdr1 *pPage;
                    507:     while( (pPage = *pp)!=0 ){
                    508:       if( pPage->iKey>=iLimit ){
                    509:         pCache->nPage--;
                    510:         *pp = pPage->pNext;
                    511:         pcache1PinPage(pPage);
                    512:         pcache1FreePage(pPage);
                    513:       }else{
                    514:         pp = &pPage->pNext;
                    515:         TESTONLY( nPage++; )
                    516:       }
                    517:     }
                    518:   }
                    519:   assert( pCache->nPage==nPage );
                    520: }
                    521: 
                    522: /******************************************************************************/
                    523: /******** sqlite3_pcache Methods **********************************************/
                    524: 
                    525: /*
                    526: ** Implementation of the sqlite3_pcache.xInit method.
                    527: */
                    528: static int pcache1Init(void *NotUsed){
                    529:   UNUSED_PARAMETER(NotUsed);
                    530:   assert( pcache1.isInit==0 );
                    531:   memset(&pcache1, 0, sizeof(pcache1));
                    532:   if( sqlite3GlobalConfig.bCoreMutex ){
                    533:     pcache1.grp.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU);
                    534:     pcache1.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_PMEM);
                    535:   }
                    536:   pcache1.grp.mxPinned = 10;
                    537:   pcache1.isInit = 1;
                    538:   return SQLITE_OK;
                    539: }
                    540: 
                    541: /*
                    542: ** Implementation of the sqlite3_pcache.xShutdown method.
                    543: ** Note that the static mutex allocated in xInit does 
                    544: ** not need to be freed.
                    545: */
                    546: static void pcache1Shutdown(void *NotUsed){
                    547:   UNUSED_PARAMETER(NotUsed);
                    548:   assert( pcache1.isInit!=0 );
                    549:   memset(&pcache1, 0, sizeof(pcache1));
                    550: }
                    551: 
                    552: /*
                    553: ** Implementation of the sqlite3_pcache.xCreate method.
                    554: **
                    555: ** Allocate a new cache.
                    556: */
                    557: static sqlite3_pcache *pcache1Create(int szPage, int szExtra, int bPurgeable){
                    558:   PCache1 *pCache;      /* The newly created page cache */
                    559:   PGroup *pGroup;       /* The group the new page cache will belong to */
                    560:   int sz;               /* Bytes of memory required to allocate the new cache */
                    561: 
                    562:   /*
                    563:   ** The seperateCache variable is true if each PCache has its own private
                    564:   ** PGroup.  In other words, separateCache is true for mode (1) where no
                    565:   ** mutexing is required.
                    566:   **
                    567:   **   *  Always use a unified cache (mode-2) if ENABLE_MEMORY_MANAGEMENT
                    568:   **
                    569:   **   *  Always use a unified cache in single-threaded applications
                    570:   **
                    571:   **   *  Otherwise (if multi-threaded and ENABLE_MEMORY_MANAGEMENT is off)
                    572:   **      use separate caches (mode-1)
                    573:   */
                    574: #if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) || SQLITE_THREADSAFE==0
                    575:   const int separateCache = 0;
                    576: #else
                    577:   int separateCache = sqlite3GlobalConfig.bCoreMutex>0;
                    578: #endif
                    579: 
                    580:   assert( (szPage & (szPage-1))==0 && szPage>=512 && szPage<=65536 );
                    581:   assert( szExtra < 300 );
                    582: 
                    583:   sz = sizeof(PCache1) + sizeof(PGroup)*separateCache;
                    584:   pCache = (PCache1 *)sqlite3_malloc(sz);
                    585:   if( pCache ){
                    586:     memset(pCache, 0, sz);
                    587:     if( separateCache ){
                    588:       pGroup = (PGroup*)&pCache[1];
                    589:       pGroup->mxPinned = 10;
                    590:     }else{
                    591:       pGroup = &pcache1.grp;
                    592:     }
                    593:     pCache->pGroup = pGroup;
                    594:     pCache->szPage = szPage;
                    595:     pCache->szExtra = szExtra;
                    596:     pCache->bPurgeable = (bPurgeable ? 1 : 0);
                    597:     if( bPurgeable ){
                    598:       pCache->nMin = 10;
                    599:       pcache1EnterMutex(pGroup);
                    600:       pGroup->nMinPage += pCache->nMin;
                    601:       pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
                    602:       pcache1LeaveMutex(pGroup);
                    603:     }
                    604:   }
                    605:   return (sqlite3_pcache *)pCache;
                    606: }
                    607: 
                    608: /*
                    609: ** Implementation of the sqlite3_pcache.xCachesize method. 
                    610: **
                    611: ** Configure the cache_size limit for a cache.
                    612: */
                    613: static void pcache1Cachesize(sqlite3_pcache *p, int nMax){
                    614:   PCache1 *pCache = (PCache1 *)p;
                    615:   if( pCache->bPurgeable ){
                    616:     PGroup *pGroup = pCache->pGroup;
                    617:     pcache1EnterMutex(pGroup);
                    618:     pGroup->nMaxPage += (nMax - pCache->nMax);
                    619:     pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
                    620:     pCache->nMax = nMax;
                    621:     pCache->n90pct = pCache->nMax*9/10;
                    622:     pcache1EnforceMaxPage(pGroup);
                    623:     pcache1LeaveMutex(pGroup);
                    624:   }
                    625: }
                    626: 
                    627: /*
                    628: ** Implementation of the sqlite3_pcache.xShrink method. 
                    629: **
                    630: ** Free up as much memory as possible.
                    631: */
                    632: static void pcache1Shrink(sqlite3_pcache *p){
                    633:   PCache1 *pCache = (PCache1*)p;
                    634:   if( pCache->bPurgeable ){
                    635:     PGroup *pGroup = pCache->pGroup;
                    636:     int savedMaxPage;
                    637:     pcache1EnterMutex(pGroup);
                    638:     savedMaxPage = pGroup->nMaxPage;
                    639:     pGroup->nMaxPage = 0;
                    640:     pcache1EnforceMaxPage(pGroup);
                    641:     pGroup->nMaxPage = savedMaxPage;
                    642:     pcache1LeaveMutex(pGroup);
                    643:   }
                    644: }
                    645: 
                    646: /*
                    647: ** Implementation of the sqlite3_pcache.xPagecount method. 
                    648: */
                    649: static int pcache1Pagecount(sqlite3_pcache *p){
                    650:   int n;
                    651:   PCache1 *pCache = (PCache1*)p;
                    652:   pcache1EnterMutex(pCache->pGroup);
                    653:   n = pCache->nPage;
                    654:   pcache1LeaveMutex(pCache->pGroup);
                    655:   return n;
                    656: }
                    657: 
                    658: /*
                    659: ** Implementation of the sqlite3_pcache.xFetch method. 
                    660: **
                    661: ** Fetch a page by key value.
                    662: **
                    663: ** Whether or not a new page may be allocated by this function depends on
                    664: ** the value of the createFlag argument.  0 means do not allocate a new
                    665: ** page.  1 means allocate a new page if space is easily available.  2 
                    666: ** means to try really hard to allocate a new page.
                    667: **
                    668: ** For a non-purgeable cache (a cache used as the storage for an in-memory
                    669: ** database) there is really no difference between createFlag 1 and 2.  So
                    670: ** the calling function (pcache.c) will never have a createFlag of 1 on
                    671: ** a non-purgeable cache.
                    672: **
                    673: ** There are three different approaches to obtaining space for a page,
                    674: ** depending on the value of parameter createFlag (which may be 0, 1 or 2).
                    675: **
                    676: **   1. Regardless of the value of createFlag, the cache is searched for a 
                    677: **      copy of the requested page. If one is found, it is returned.
                    678: **
                    679: **   2. If createFlag==0 and the page is not already in the cache, NULL is
                    680: **      returned.
                    681: **
                    682: **   3. If createFlag is 1, and the page is not already in the cache, then
                    683: **      return NULL (do not allocate a new page) if any of the following
                    684: **      conditions are true:
                    685: **
                    686: **       (a) the number of pages pinned by the cache is greater than
                    687: **           PCache1.nMax, or
                    688: **
                    689: **       (b) the number of pages pinned by the cache is greater than
                    690: **           the sum of nMax for all purgeable caches, less the sum of 
                    691: **           nMin for all other purgeable caches, or
                    692: **
                    693: **   4. If none of the first three conditions apply and the cache is marked
                    694: **      as purgeable, and if one of the following is true:
                    695: **
                    696: **       (a) The number of pages allocated for the cache is already 
                    697: **           PCache1.nMax, or
                    698: **
                    699: **       (b) The number of pages allocated for all purgeable caches is
                    700: **           already equal to or greater than the sum of nMax for all
                    701: **           purgeable caches,
                    702: **
                    703: **       (c) The system is under memory pressure and wants to avoid
                    704: **           unnecessary pages cache entry allocations
                    705: **
                    706: **      then attempt to recycle a page from the LRU list. If it is the right
                    707: **      size, return the recycled buffer. Otherwise, free the buffer and
                    708: **      proceed to step 5. 
                    709: **
                    710: **   5. Otherwise, allocate and return a new page buffer.
                    711: */
                    712: static sqlite3_pcache_page *pcache1Fetch(
                    713:   sqlite3_pcache *p, 
                    714:   unsigned int iKey, 
                    715:   int createFlag
                    716: ){
                    717:   unsigned int nPinned;
                    718:   PCache1 *pCache = (PCache1 *)p;
                    719:   PGroup *pGroup;
                    720:   PgHdr1 *pPage = 0;
                    721: 
                    722:   assert( pCache->bPurgeable || createFlag!=1 );
                    723:   assert( pCache->bPurgeable || pCache->nMin==0 );
                    724:   assert( pCache->bPurgeable==0 || pCache->nMin==10 );
                    725:   assert( pCache->nMin==0 || pCache->bPurgeable );
                    726:   pcache1EnterMutex(pGroup = pCache->pGroup);
                    727: 
                    728:   /* Step 1: Search the hash table for an existing entry. */
                    729:   if( pCache->nHash>0 ){
                    730:     unsigned int h = iKey % pCache->nHash;
                    731:     for(pPage=pCache->apHash[h]; pPage&&pPage->iKey!=iKey; pPage=pPage->pNext);
                    732:   }
                    733: 
                    734:   /* Step 2: Abort if no existing page is found and createFlag is 0 */
                    735:   if( pPage || createFlag==0 ){
                    736:     pcache1PinPage(pPage);
                    737:     goto fetch_out;
                    738:   }
                    739: 
                    740:   /* The pGroup local variable will normally be initialized by the
                    741:   ** pcache1EnterMutex() macro above.  But if SQLITE_MUTEX_OMIT is defined,
                    742:   ** then pcache1EnterMutex() is a no-op, so we have to initialize the
                    743:   ** local variable here.  Delaying the initialization of pGroup is an
                    744:   ** optimization:  The common case is to exit the module before reaching
                    745:   ** this point.
                    746:   */
                    747: #ifdef SQLITE_MUTEX_OMIT
                    748:   pGroup = pCache->pGroup;
                    749: #endif
                    750: 
                    751:   /* Step 3: Abort if createFlag is 1 but the cache is nearly full */
                    752:   assert( pCache->nPage >= pCache->nRecyclable );
                    753:   nPinned = pCache->nPage - pCache->nRecyclable;
                    754:   assert( pGroup->mxPinned == pGroup->nMaxPage + 10 - pGroup->nMinPage );
                    755:   assert( pCache->n90pct == pCache->nMax*9/10 );
                    756:   if( createFlag==1 && (
                    757:         nPinned>=pGroup->mxPinned
                    758:      || nPinned>=pCache->n90pct
                    759:      || pcache1UnderMemoryPressure(pCache)
                    760:   )){
                    761:     goto fetch_out;
                    762:   }
                    763: 
                    764:   if( pCache->nPage>=pCache->nHash && pcache1ResizeHash(pCache) ){
                    765:     goto fetch_out;
                    766:   }
                    767: 
                    768:   /* Step 4. Try to recycle a page. */
                    769:   if( pCache->bPurgeable && pGroup->pLruTail && (
                    770:          (pCache->nPage+1>=pCache->nMax)
                    771:       || pGroup->nCurrentPage>=pGroup->nMaxPage
                    772:       || pcache1UnderMemoryPressure(pCache)
                    773:   )){
                    774:     PCache1 *pOther;
                    775:     pPage = pGroup->pLruTail;
                    776:     pcache1RemoveFromHash(pPage);
                    777:     pcache1PinPage(pPage);
                    778:     pOther = pPage->pCache;
                    779: 
                    780:     /* We want to verify that szPage and szExtra are the same for pOther
                    781:     ** and pCache.  Assert that we can verify this by comparing sums. */
                    782:     assert( (pCache->szPage & (pCache->szPage-1))==0 && pCache->szPage>=512 );
                    783:     assert( pCache->szExtra<512 );
                    784:     assert( (pOther->szPage & (pOther->szPage-1))==0 && pOther->szPage>=512 );
                    785:     assert( pOther->szExtra<512 );
                    786: 
                    787:     if( pOther->szPage+pOther->szExtra != pCache->szPage+pCache->szExtra ){
                    788:       pcache1FreePage(pPage);
                    789:       pPage = 0;
                    790:     }else{
                    791:       pGroup->nCurrentPage -= (pOther->bPurgeable - pCache->bPurgeable);
                    792:     }
                    793:   }
                    794: 
                    795:   /* Step 5. If a usable page buffer has still not been found, 
                    796:   ** attempt to allocate a new one. 
                    797:   */
                    798:   if( !pPage ){
                    799:     if( createFlag==1 ) sqlite3BeginBenignMalloc();
                    800:     pPage = pcache1AllocPage(pCache);
                    801:     if( createFlag==1 ) sqlite3EndBenignMalloc();
                    802:   }
                    803: 
                    804:   if( pPage ){
                    805:     unsigned int h = iKey % pCache->nHash;
                    806:     pCache->nPage++;
                    807:     pPage->iKey = iKey;
                    808:     pPage->pNext = pCache->apHash[h];
                    809:     pPage->pCache = pCache;
                    810:     pPage->pLruPrev = 0;
                    811:     pPage->pLruNext = 0;
                    812:     *(void **)pPage->page.pExtra = 0;
                    813:     pCache->apHash[h] = pPage;
                    814:   }
                    815: 
                    816: fetch_out:
                    817:   if( pPage && iKey>pCache->iMaxKey ){
                    818:     pCache->iMaxKey = iKey;
                    819:   }
                    820:   pcache1LeaveMutex(pGroup);
                    821:   return &pPage->page;
                    822: }
                    823: 
                    824: 
                    825: /*
                    826: ** Implementation of the sqlite3_pcache.xUnpin method.
                    827: **
                    828: ** Mark a page as unpinned (eligible for asynchronous recycling).
                    829: */
                    830: static void pcache1Unpin(
                    831:   sqlite3_pcache *p, 
                    832:   sqlite3_pcache_page *pPg, 
                    833:   int reuseUnlikely
                    834: ){
                    835:   PCache1 *pCache = (PCache1 *)p;
                    836:   PgHdr1 *pPage = (PgHdr1 *)pPg;
                    837:   PGroup *pGroup = pCache->pGroup;
                    838:  
                    839:   assert( pPage->pCache==pCache );
                    840:   pcache1EnterMutex(pGroup);
                    841: 
                    842:   /* It is an error to call this function if the page is already 
                    843:   ** part of the PGroup LRU list.
                    844:   */
                    845:   assert( pPage->pLruPrev==0 && pPage->pLruNext==0 );
                    846:   assert( pGroup->pLruHead!=pPage && pGroup->pLruTail!=pPage );
                    847: 
                    848:   if( reuseUnlikely || pGroup->nCurrentPage>pGroup->nMaxPage ){
                    849:     pcache1RemoveFromHash(pPage);
                    850:     pcache1FreePage(pPage);
                    851:   }else{
                    852:     /* Add the page to the PGroup LRU list. */
                    853:     if( pGroup->pLruHead ){
                    854:       pGroup->pLruHead->pLruPrev = pPage;
                    855:       pPage->pLruNext = pGroup->pLruHead;
                    856:       pGroup->pLruHead = pPage;
                    857:     }else{
                    858:       pGroup->pLruTail = pPage;
                    859:       pGroup->pLruHead = pPage;
                    860:     }
                    861:     pCache->nRecyclable++;
                    862:   }
                    863: 
                    864:   pcache1LeaveMutex(pCache->pGroup);
                    865: }
                    866: 
                    867: /*
                    868: ** Implementation of the sqlite3_pcache.xRekey method. 
                    869: */
                    870: static void pcache1Rekey(
                    871:   sqlite3_pcache *p,
                    872:   sqlite3_pcache_page *pPg,
                    873:   unsigned int iOld,
                    874:   unsigned int iNew
                    875: ){
                    876:   PCache1 *pCache = (PCache1 *)p;
                    877:   PgHdr1 *pPage = (PgHdr1 *)pPg;
                    878:   PgHdr1 **pp;
                    879:   unsigned int h; 
                    880:   assert( pPage->iKey==iOld );
                    881:   assert( pPage->pCache==pCache );
                    882: 
                    883:   pcache1EnterMutex(pCache->pGroup);
                    884: 
                    885:   h = iOld%pCache->nHash;
                    886:   pp = &pCache->apHash[h];
                    887:   while( (*pp)!=pPage ){
                    888:     pp = &(*pp)->pNext;
                    889:   }
                    890:   *pp = pPage->pNext;
                    891: 
                    892:   h = iNew%pCache->nHash;
                    893:   pPage->iKey = iNew;
                    894:   pPage->pNext = pCache->apHash[h];
                    895:   pCache->apHash[h] = pPage;
                    896:   if( iNew>pCache->iMaxKey ){
                    897:     pCache->iMaxKey = iNew;
                    898:   }
                    899: 
                    900:   pcache1LeaveMutex(pCache->pGroup);
                    901: }
                    902: 
                    903: /*
                    904: ** Implementation of the sqlite3_pcache.xTruncate method. 
                    905: **
                    906: ** Discard all unpinned pages in the cache with a page number equal to
                    907: ** or greater than parameter iLimit. Any pinned pages with a page number
                    908: ** equal to or greater than iLimit are implicitly unpinned.
                    909: */
                    910: static void pcache1Truncate(sqlite3_pcache *p, unsigned int iLimit){
                    911:   PCache1 *pCache = (PCache1 *)p;
                    912:   pcache1EnterMutex(pCache->pGroup);
                    913:   if( iLimit<=pCache->iMaxKey ){
                    914:     pcache1TruncateUnsafe(pCache, iLimit);
                    915:     pCache->iMaxKey = iLimit-1;
                    916:   }
                    917:   pcache1LeaveMutex(pCache->pGroup);
                    918: }
                    919: 
                    920: /*
                    921: ** Implementation of the sqlite3_pcache.xDestroy method. 
                    922: **
                    923: ** Destroy a cache allocated using pcache1Create().
                    924: */
                    925: static void pcache1Destroy(sqlite3_pcache *p){
                    926:   PCache1 *pCache = (PCache1 *)p;
                    927:   PGroup *pGroup = pCache->pGroup;
                    928:   assert( pCache->bPurgeable || (pCache->nMax==0 && pCache->nMin==0) );
                    929:   pcache1EnterMutex(pGroup);
                    930:   pcache1TruncateUnsafe(pCache, 0);
                    931:   assert( pGroup->nMaxPage >= pCache->nMax );
                    932:   pGroup->nMaxPage -= pCache->nMax;
                    933:   assert( pGroup->nMinPage >= pCache->nMin );
                    934:   pGroup->nMinPage -= pCache->nMin;
                    935:   pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
                    936:   pcache1EnforceMaxPage(pGroup);
                    937:   pcache1LeaveMutex(pGroup);
                    938:   sqlite3_free(pCache->apHash);
                    939:   sqlite3_free(pCache);
                    940: }
                    941: 
                    942: /*
                    943: ** This function is called during initialization (sqlite3_initialize()) to
                    944: ** install the default pluggable cache module, assuming the user has not
                    945: ** already provided an alternative.
                    946: */
                    947: void sqlite3PCacheSetDefault(void){
                    948:   static const sqlite3_pcache_methods2 defaultMethods = {
                    949:     1,                       /* iVersion */
                    950:     0,                       /* pArg */
                    951:     pcache1Init,             /* xInit */
                    952:     pcache1Shutdown,         /* xShutdown */
                    953:     pcache1Create,           /* xCreate */
                    954:     pcache1Cachesize,        /* xCachesize */
                    955:     pcache1Pagecount,        /* xPagecount */
                    956:     pcache1Fetch,            /* xFetch */
                    957:     pcache1Unpin,            /* xUnpin */
                    958:     pcache1Rekey,            /* xRekey */
                    959:     pcache1Truncate,         /* xTruncate */
                    960:     pcache1Destroy,          /* xDestroy */
                    961:     pcache1Shrink            /* xShrink */
                    962:   };
                    963:   sqlite3_config(SQLITE_CONFIG_PCACHE2, &defaultMethods);
                    964: }
                    965: 
                    966: #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
                    967: /*
                    968: ** This function is called to free superfluous dynamically allocated memory
                    969: ** held by the pager system. Memory in use by any SQLite pager allocated
                    970: ** by the current thread may be sqlite3_free()ed.
                    971: **
                    972: ** nReq is the number of bytes of memory required. Once this much has
                    973: ** been released, the function returns. The return value is the total number 
                    974: ** of bytes of memory released.
                    975: */
                    976: int sqlite3PcacheReleaseMemory(int nReq){
                    977:   int nFree = 0;
                    978:   assert( sqlite3_mutex_notheld(pcache1.grp.mutex) );
                    979:   assert( sqlite3_mutex_notheld(pcache1.mutex) );
                    980:   if( pcache1.pStart==0 ){
                    981:     PgHdr1 *p;
                    982:     pcache1EnterMutex(&pcache1.grp);
                    983:     while( (nReq<0 || nFree<nReq) && ((p=pcache1.grp.pLruTail)!=0) ){
                    984:       nFree += pcache1MemSize(p->page.pBuf);
                    985: #ifdef SQLITE_PCACHE_SEPARATE_HEADER
                    986:       nFree += sqlite3MemSize(p);
                    987: #endif
                    988:       pcache1PinPage(p);
                    989:       pcache1RemoveFromHash(p);
                    990:       pcache1FreePage(p);
                    991:     }
                    992:     pcache1LeaveMutex(&pcache1.grp);
                    993:   }
                    994:   return nFree;
                    995: }
                    996: #endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
                    997: 
                    998: #ifdef SQLITE_TEST
                    999: /*
                   1000: ** This function is used by test procedures to inspect the internal state
                   1001: ** of the global cache.
                   1002: */
                   1003: void sqlite3PcacheStats(
                   1004:   int *pnCurrent,      /* OUT: Total number of pages cached */
                   1005:   int *pnMax,          /* OUT: Global maximum cache size */
                   1006:   int *pnMin,          /* OUT: Sum of PCache1.nMin for purgeable caches */
                   1007:   int *pnRecyclable    /* OUT: Total number of pages available for recycling */
                   1008: ){
                   1009:   PgHdr1 *p;
                   1010:   int nRecyclable = 0;
                   1011:   for(p=pcache1.grp.pLruHead; p; p=p->pLruNext){
                   1012:     nRecyclable++;
                   1013:   }
                   1014:   *pnCurrent = pcache1.grp.nCurrentPage;
                   1015:   *pnMax = (int)pcache1.grp.nMaxPage;
                   1016:   *pnMin = (int)pcache1.grp.nMinPage;
                   1017:   *pnRecyclable = nRecyclable;
                   1018: }
                   1019: #endif

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>