2008-11-13 14:28:28 +00:00
|
|
|
/*
|
|
|
|
** 2008 November 05
|
|
|
|
**
|
|
|
|
** The author disclaims copyright to this source code. In place of
|
|
|
|
** a legal notice, here is a blessing:
|
|
|
|
**
|
|
|
|
** May you do good and not evil.
|
|
|
|
** May you find forgiveness for yourself and forgive others.
|
|
|
|
** May you share freely, never taking more than you give.
|
|
|
|
**
|
|
|
|
*************************************************************************
|
|
|
|
**
|
|
|
|
** This file implements the default page cache implementation (the
|
|
|
|
** sqlite3_pcache interface). It also contains part of the implementation
|
|
|
|
** of the SQLITE_CONFIG_PAGECACHE and sqlite3_release_memory() features.
|
2014-09-06 16:39:46 +00:00
|
|
|
** If the default page cache implementation is overridden, then neither of
|
2008-11-13 14:28:28 +00:00
|
|
|
** these two features are available.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "sqliteInt.h"
|
|
|
|
|
|
|
|
typedef struct PCache1 PCache1;
|
|
|
|
typedef struct PgHdr1 PgHdr1;
|
|
|
|
typedef struct PgFreeslot PgFreeslot;
|
2011-01-17 21:32:24 +00:00
|
|
|
typedef struct PGroup PGroup;
|
|
|
|
|
|
|
|
/* Each page cache (or PCache) belongs to a PGroup. A PGroup is a set
|
2014-09-06 16:39:46 +00:00
|
|
|
** of one or more PCaches that are able to recycle each other's unpinned
|
2011-01-17 21:32:24 +00:00
|
|
|
** pages when they are under memory pressure. A PGroup is an instance of
|
|
|
|
** the following object.
|
|
|
|
**
|
|
|
|
** This page cache implementation works in one of two modes:
|
|
|
|
**
|
|
|
|
** (1) Every PCache is the sole member of its own PGroup. There is
|
|
|
|
** one PGroup per PCache.
|
|
|
|
**
|
|
|
|
** (2) There is a single global PGroup that all PCaches are a member
|
|
|
|
** of.
|
|
|
|
**
|
|
|
|
** Mode 1 uses more memory (since PCache instances are not able to rob
|
|
|
|
** unused pages from other PCaches) but it also operates without a mutex,
|
|
|
|
** and is therefore often faster. Mode 2 requires a mutex in order to be
|
2012-01-08 22:18:33 +00:00
|
|
|
** threadsafe, but recycles pages more efficiently.
|
2011-01-17 21:32:24 +00:00
|
|
|
**
|
|
|
|
** For mode (1), PGroup.mutex is NULL. For mode (2) there is only a single
|
|
|
|
** PGroup which is the pcache1.grp global variable and its mutex is
|
|
|
|
** SQLITE_MUTEX_STATIC_LRU.
|
|
|
|
*/
|
|
|
|
struct PGroup {
|
|
|
|
sqlite3_mutex *mutex; /* MUTEX_STATIC_LRU or NULL */
|
2012-01-02 18:00:55 +00:00
|
|
|
unsigned int nMaxPage; /* Sum of nMax for purgeable caches */
|
|
|
|
unsigned int nMinPage; /* Sum of nMin for purgeable caches */
|
|
|
|
unsigned int mxPinned; /* nMaxpage + 10 - nMinPage */
|
|
|
|
unsigned int nCurrentPage; /* Number of purgeable pages allocated */
|
2011-01-17 21:32:24 +00:00
|
|
|
PgHdr1 *pLruHead, *pLruTail; /* LRU list of unpinned pages */
|
|
|
|
};
|
2008-11-13 14:28:28 +00:00
|
|
|
|
2010-08-24 18:06:35 +00:00
|
|
|
/* Each page cache is an instance of the following object. Every
|
|
|
|
** open database file (including each in-memory database and each
|
|
|
|
** temporary or transient database) has a single page cache which
|
|
|
|
** is an instance of this object.
|
|
|
|
**
|
|
|
|
** Pointers to structures of this type are cast and returned as
|
|
|
|
** opaque sqlite3_pcache* handles.
|
2008-11-13 14:28:28 +00:00
|
|
|
*/
|
|
|
|
struct PCache1 {
|
|
|
|
/* Cache configuration parameters. Page size (szPage) and the purgeable
|
|
|
|
** flag (bPurgeable) are set when the cache is created. nMax may be
|
2012-01-08 22:18:33 +00:00
|
|
|
** modified at any time by a call to the pcache1Cachesize() method.
|
2011-01-17 21:32:24 +00:00
|
|
|
** The PGroup mutex must be held when accessing nMax.
|
2008-11-13 14:28:28 +00:00
|
|
|
*/
|
2011-01-17 21:32:24 +00:00
|
|
|
PGroup *pGroup; /* PGroup this cache belongs to */
|
2008-11-13 14:28:28 +00:00
|
|
|
int szPage; /* Size of allocated pages in bytes */
|
2011-11-08 20:08:44 +00:00
|
|
|
int szExtra; /* Size of extra space in bytes */
|
2008-11-13 14:28:28 +00:00
|
|
|
int bPurgeable; /* True if cache is purgeable */
|
2008-11-15 11:22:45 +00:00
|
|
|
unsigned int nMin; /* Minimum number of pages reserved */
|
|
|
|
unsigned int nMax; /* Configured "cache_size" value */
|
2011-01-26 00:07:03 +00:00
|
|
|
unsigned int n90pct; /* nMax*9/10 */
|
2012-02-02 19:37:18 +00:00
|
|
|
unsigned int iMaxKey; /* Largest key seen since xTruncate() */
|
2008-11-13 14:28:28 +00:00
|
|
|
|
|
|
|
/* Hash table of all pages. The following variables may only be accessed
|
2011-01-17 21:32:24 +00:00
|
|
|
** when the accessor is holding the PGroup mutex.
|
2008-11-13 14:28:28 +00:00
|
|
|
*/
|
2008-11-15 11:22:45 +00:00
|
|
|
unsigned int nRecyclable; /* Number of pages in the LRU list */
|
|
|
|
unsigned int nPage; /* Total number of pages in apHash */
|
|
|
|
unsigned int nHash; /* Number of slots in apHash[] */
|
2008-11-13 14:28:28 +00:00
|
|
|
PgHdr1 **apHash; /* Hash table for fast lookup by key */
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Each cache entry is represented by an instance of the following
|
2011-11-08 20:08:44 +00:00
|
|
|
** structure. Unless SQLITE_PCACHE_SEPARATE_HEADER is defined, a buffer of
|
|
|
|
** PgHdr1.pCache->szPage bytes is allocated directly before this structure
|
|
|
|
** in memory.
|
2008-11-13 14:28:28 +00:00
|
|
|
*/
|
|
|
|
struct PgHdr1 {
|
2011-11-08 20:08:44 +00:00
|
|
|
sqlite3_pcache_page page;
|
2008-11-13 14:28:28 +00:00
|
|
|
unsigned int iKey; /* Key value (page number) */
|
2013-12-13 18:50:40 +00:00
|
|
|
u8 isPinned; /* Page in use, not on the LRU list */
|
2008-11-13 14:28:28 +00:00
|
|
|
PgHdr1 *pNext; /* Next in hash table chain */
|
|
|
|
PCache1 *pCache; /* Cache that currently owns this page */
|
|
|
|
PgHdr1 *pLruNext; /* Next in LRU list of unpinned pages */
|
|
|
|
PgHdr1 *pLruPrev; /* Previous in LRU list of unpinned pages */
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Free slots in the allocator used to divide up the buffer provided using
|
|
|
|
** the SQLITE_CONFIG_PAGECACHE mechanism.
|
|
|
|
*/
|
|
|
|
struct PgFreeslot {
|
|
|
|
PgFreeslot *pNext; /* Next free slot */
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Global data used by this cache.
|
|
|
|
*/
|
|
|
|
static SQLITE_WSD struct PCacheGlobal {
|
2011-01-17 21:32:24 +00:00
|
|
|
PGroup grp; /* The global PGroup for mode (2) */
|
|
|
|
|
|
|
|
/* Variables related to SQLITE_CONFIG_PAGECACHE settings. The
|
|
|
|
** szSlot, nSlot, pStart, pEnd, nReserve, and isInit values are all
|
|
|
|
** fixed at sqlite3_initialize() time and do not require mutex protection.
|
|
|
|
** The nFreeSlot and pFree values do require mutex protection.
|
|
|
|
*/
|
|
|
|
int isInit; /* True if initialized */
|
|
|
|
int szSlot; /* Size of each free slot */
|
|
|
|
int nSlot; /* The number of pcache slots */
|
|
|
|
int nReserve; /* Try to keep nFreeSlot above this */
|
|
|
|
void *pStart, *pEnd; /* Bounds of pagecache malloc range */
|
|
|
|
/* Above requires no mutex. Use mutex below for variable that follow. */
|
|
|
|
sqlite3_mutex *mutex; /* Mutex for accessing the following: */
|
|
|
|
PgFreeslot *pFree; /* Free page blocks */
|
2012-02-02 19:37:18 +00:00
|
|
|
int nFreeSlot; /* Number of unused pcache slots */
|
2011-01-17 21:32:24 +00:00
|
|
|
/* The following value requires a mutex to change. We skip the mutex on
|
|
|
|
** reading because (1) most platforms read a 32-bit integer atomically and
|
|
|
|
** (2) even if an incorrect value is read, no great harm is done since this
|
|
|
|
** is really just an optimization. */
|
|
|
|
int bUnderPressure; /* True if low on PAGECACHE memory */
|
2008-11-15 11:22:45 +00:00
|
|
|
} pcache1_g;
|
2008-11-13 14:28:28 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
** All code in this file should access the global structure above via the
|
|
|
|
** alias "pcache1". This ensures that the WSD emulation is used when
|
|
|
|
** compiling for systems that do not support real WSD.
|
|
|
|
*/
|
|
|
|
#define pcache1 (GLOBAL(struct PCacheGlobal, pcache1_g))
|
|
|
|
|
|
|
|
/*
|
2011-01-17 21:32:24 +00:00
|
|
|
** Macros to enter and leave the PCache LRU mutex.
|
2008-11-13 14:28:28 +00:00
|
|
|
*/
|
2011-01-17 21:32:24 +00:00
|
|
|
#define pcache1EnterMutex(X) sqlite3_mutex_enter((X)->mutex)
|
|
|
|
#define pcache1LeaveMutex(X) sqlite3_mutex_leave((X)->mutex)
|
2008-11-13 14:28:28 +00:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/******** Page Allocation/SQLITE_CONFIG_PCACHE Related Functions **************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
** This function is called during initialization if a static buffer is
|
|
|
|
** supplied to use for the page-cache by passing the SQLITE_CONFIG_PAGECACHE
|
|
|
|
** verb to sqlite3_config(). Parameter pBuf points to an allocation large
|
|
|
|
** enough to contain 'n' buffers of 'sz' bytes each.
|
2011-01-17 21:32:24 +00:00
|
|
|
**
|
|
|
|
** This routine is called from sqlite3_initialize() and so it is guaranteed
|
|
|
|
** to be serialized already. There is no need for further mutexing.
|
2008-11-13 14:28:28 +00:00
|
|
|
*/
|
|
|
|
void sqlite3PCacheBufferSetup(void *pBuf, int sz, int n){
|
2009-05-22 11:10:24 +00:00
|
|
|
if( pcache1.isInit ){
|
|
|
|
PgFreeslot *p;
|
|
|
|
sz = ROUNDDOWN8(sz);
|
|
|
|
pcache1.szSlot = sz;
|
2010-08-27 12:21:06 +00:00
|
|
|
pcache1.nSlot = pcache1.nFreeSlot = n;
|
|
|
|
pcache1.nReserve = n>90 ? 10 : (n/10 + 1);
|
2009-05-22 11:10:24 +00:00
|
|
|
pcache1.pStart = pBuf;
|
|
|
|
pcache1.pFree = 0;
|
2011-01-17 21:32:24 +00:00
|
|
|
pcache1.bUnderPressure = 0;
|
2009-05-22 11:10:24 +00:00
|
|
|
while( n-- ){
|
|
|
|
p = (PgFreeslot*)pBuf;
|
|
|
|
p->pNext = pcache1.pFree;
|
|
|
|
pcache1.pFree = p;
|
|
|
|
pBuf = (void*)&((char*)pBuf)[sz];
|
|
|
|
}
|
|
|
|
pcache1.pEnd = pBuf;
|
2008-11-13 14:28:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Malloc function used within this file to allocate space from the buffer
|
|
|
|
** configured using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no
|
|
|
|
** such buffer exists or there is no space left in it, this function falls
|
|
|
|
** back to sqlite3Malloc().
|
2011-01-17 21:32:24 +00:00
|
|
|
**
|
|
|
|
** Multiple threads can run this routine at the same time. Global variables
|
|
|
|
** in pcache1 need to be protected via mutex.
|
2008-11-13 14:28:28 +00:00
|
|
|
*/
|
|
|
|
static void *pcache1Alloc(int nByte){
|
2011-01-17 21:32:24 +00:00
|
|
|
void *p = 0;
|
|
|
|
assert( sqlite3_mutex_notheld(pcache1.grp.mutex) );
|
|
|
|
if( nByte<=pcache1.szSlot ){
|
|
|
|
sqlite3_mutex_enter(pcache1.mutex);
|
2008-11-13 14:28:28 +00:00
|
|
|
p = (PgHdr1 *)pcache1.pFree;
|
2011-01-17 21:32:24 +00:00
|
|
|
if( p ){
|
|
|
|
pcache1.pFree = pcache1.pFree->pNext;
|
|
|
|
pcache1.nFreeSlot--;
|
|
|
|
pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve;
|
|
|
|
assert( pcache1.nFreeSlot>=0 );
|
2015-03-23 17:25:18 +00:00
|
|
|
sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, nByte);
|
|
|
|
sqlite3StatusUp(SQLITE_STATUS_PAGECACHE_USED, 1);
|
2011-01-17 21:32:24 +00:00
|
|
|
}
|
|
|
|
sqlite3_mutex_leave(pcache1.mutex);
|
|
|
|
}
|
|
|
|
if( p==0 ){
|
|
|
|
/* Memory is not available in the SQLITE_CONFIG_PAGECACHE pool. Get
|
|
|
|
** it from sqlite3Malloc instead.
|
2008-11-13 14:28:28 +00:00
|
|
|
*/
|
|
|
|
p = sqlite3Malloc(nByte);
|
2012-06-07 02:35:29 +00:00
|
|
|
#ifndef SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS
|
2008-11-13 14:28:28 +00:00
|
|
|
if( p ){
|
|
|
|
int sz = sqlite3MallocSize(p);
|
2011-01-26 13:24:40 +00:00
|
|
|
sqlite3_mutex_enter(pcache1.mutex);
|
2015-03-23 17:25:18 +00:00
|
|
|
sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, nByte);
|
|
|
|
sqlite3StatusUp(SQLITE_STATUS_PAGECACHE_OVERFLOW, sz);
|
2011-01-26 13:24:40 +00:00
|
|
|
sqlite3_mutex_leave(pcache1.mutex);
|
2008-11-13 14:28:28 +00:00
|
|
|
}
|
2012-06-07 02:35:29 +00:00
|
|
|
#endif
|
2010-03-12 16:32:53 +00:00
|
|
|
sqlite3MemdebugSetType(p, MEMTYPE_PCACHE);
|
2008-11-13 14:28:28 +00:00
|
|
|
}
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Free an allocated buffer obtained from pcache1Alloc().
|
|
|
|
*/
|
2011-11-16 19:29:17 +00:00
|
|
|
static int pcache1Free(void *p){
|
|
|
|
int nFreed = 0;
|
|
|
|
if( p==0 ) return 0;
|
2008-11-13 14:28:28 +00:00
|
|
|
if( p>=pcache1.pStart && p<pcache1.pEnd ){
|
|
|
|
PgFreeslot *pSlot;
|
2011-01-17 21:32:24 +00:00
|
|
|
sqlite3_mutex_enter(pcache1.mutex);
|
2015-03-23 17:25:18 +00:00
|
|
|
sqlite3StatusDown(SQLITE_STATUS_PAGECACHE_USED, 1);
|
2008-11-13 14:28:28 +00:00
|
|
|
pSlot = (PgFreeslot*)p;
|
|
|
|
pSlot->pNext = pcache1.pFree;
|
|
|
|
pcache1.pFree = pSlot;
|
2010-08-27 12:21:06 +00:00
|
|
|
pcache1.nFreeSlot++;
|
2011-01-17 21:32:24 +00:00
|
|
|
pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve;
|
2010-08-27 12:21:06 +00:00
|
|
|
assert( pcache1.nFreeSlot<=pcache1.nSlot );
|
2011-01-17 21:32:24 +00:00
|
|
|
sqlite3_mutex_leave(pcache1.mutex);
|
2008-11-13 14:28:28 +00:00
|
|
|
}else{
|
2010-03-12 16:32:53 +00:00
|
|
|
assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) );
|
|
|
|
sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
|
2011-11-16 19:29:17 +00:00
|
|
|
nFreed = sqlite3MallocSize(p);
|
2012-06-07 02:35:29 +00:00
|
|
|
#ifndef SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS
|
2011-01-26 13:28:06 +00:00
|
|
|
sqlite3_mutex_enter(pcache1.mutex);
|
2015-03-23 17:25:18 +00:00
|
|
|
sqlite3StatusDown(SQLITE_STATUS_PAGECACHE_OVERFLOW, nFreed);
|
2011-01-26 13:28:06 +00:00
|
|
|
sqlite3_mutex_leave(pcache1.mutex);
|
2012-06-07 02:35:29 +00:00
|
|
|
#endif
|
2008-11-13 14:28:28 +00:00
|
|
|
sqlite3_free(p);
|
|
|
|
}
|
2011-11-16 19:29:17 +00:00
|
|
|
return nFreed;
|
2008-11-13 14:28:28 +00:00
|
|
|
}
|
|
|
|
|
2010-08-20 09:14:13 +00:00
|
|
|
#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
|
|
|
|
/*
|
2010-08-24 18:06:35 +00:00
|
|
|
** Return the size of a pcache allocation
|
2010-08-20 09:14:13 +00:00
|
|
|
*/
|
|
|
|
static int pcache1MemSize(void *p){
|
|
|
|
if( p>=pcache1.pStart && p<pcache1.pEnd ){
|
|
|
|
return pcache1.szSlot;
|
|
|
|
}else{
|
|
|
|
int iSize;
|
|
|
|
assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) );
|
|
|
|
sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
|
|
|
|
iSize = sqlite3MallocSize(p);
|
|
|
|
sqlite3MemdebugSetType(p, MEMTYPE_PCACHE);
|
|
|
|
return iSize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
|
|
|
|
|
2008-11-13 14:28:28 +00:00
|
|
|
/*
|
|
|
|
** Allocate a new page object initially associated with cache pCache.
|
|
|
|
*/
|
|
|
|
static PgHdr1 *pcache1AllocPage(PCache1 *pCache){
|
2011-09-22 14:56:31 +00:00
|
|
|
PgHdr1 *p = 0;
|
|
|
|
void *pPg;
|
2011-08-19 18:15:00 +00:00
|
|
|
|
|
|
|
/* The group mutex must be released before pcache1Alloc() is called. This
|
|
|
|
** is because it may call sqlite3_release_memory(), which assumes that
|
|
|
|
** this mutex is not held. */
|
|
|
|
assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
|
|
|
|
pcache1LeaveMutex(pCache->pGroup);
|
2011-11-08 20:08:44 +00:00
|
|
|
#ifdef SQLITE_PCACHE_SEPARATE_HEADER
|
|
|
|
pPg = pcache1Alloc(pCache->szPage);
|
|
|
|
p = sqlite3Malloc(sizeof(PgHdr1) + pCache->szExtra);
|
|
|
|
if( !pPg || !p ){
|
|
|
|
pcache1Free(pPg);
|
|
|
|
sqlite3_free(p);
|
|
|
|
pPg = 0;
|
|
|
|
}
|
|
|
|
#else
|
2014-12-30 13:04:25 +00:00
|
|
|
pPg = pcache1Alloc(ROUND8(sizeof(PgHdr1)) + pCache->szPage + pCache->szExtra);
|
2011-11-08 20:08:44 +00:00
|
|
|
p = (PgHdr1 *)&((u8 *)pPg)[pCache->szPage];
|
|
|
|
#endif
|
2011-08-19 18:15:00 +00:00
|
|
|
pcache1EnterMutex(pCache->pGroup);
|
2011-09-22 14:56:31 +00:00
|
|
|
|
2009-06-03 21:04:35 +00:00
|
|
|
if( pPg ){
|
2011-11-08 20:08:44 +00:00
|
|
|
p->page.pBuf = pPg;
|
|
|
|
p->page.pExtra = &p[1];
|
2008-11-13 14:28:28 +00:00
|
|
|
if( pCache->bPurgeable ){
|
2011-01-17 21:32:24 +00:00
|
|
|
pCache->pGroup->nCurrentPage++;
|
2008-11-13 14:28:28 +00:00
|
|
|
}
|
2011-11-08 20:08:44 +00:00
|
|
|
return p;
|
2008-11-13 14:28:28 +00:00
|
|
|
}
|
2011-11-08 20:08:44 +00:00
|
|
|
return 0;
|
2008-11-13 14:28:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Free a page object allocated by pcache1AllocPage().
|
2009-07-17 11:44:07 +00:00
|
|
|
**
|
|
|
|
** The pointer is allowed to be NULL, which is prudent. But it turns out
|
|
|
|
** that the current implementation happens to never call this routine
|
|
|
|
** with a NULL pointer, so we mark the NULL test with ALWAYS().
|
2008-11-13 14:28:28 +00:00
|
|
|
*/
|
|
|
|
static void pcache1FreePage(PgHdr1 *p){
|
2009-07-17 11:44:07 +00:00
|
|
|
if( ALWAYS(p) ){
|
2011-01-17 21:32:24 +00:00
|
|
|
PCache1 *pCache = p->pCache;
|
2011-08-19 18:15:00 +00:00
|
|
|
assert( sqlite3_mutex_held(p->pCache->pGroup->mutex) );
|
2011-11-08 20:08:44 +00:00
|
|
|
pcache1Free(p->page.pBuf);
|
|
|
|
#ifdef SQLITE_PCACHE_SEPARATE_HEADER
|
|
|
|
sqlite3_free(p);
|
|
|
|
#endif
|
2011-01-17 21:32:24 +00:00
|
|
|
if( pCache->bPurgeable ){
|
|
|
|
pCache->pGroup->nCurrentPage--;
|
2008-11-13 14:28:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Malloc function used by SQLite to obtain space from the buffer configured
|
|
|
|
** using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no such buffer
|
|
|
|
** exists, this function falls back to sqlite3Malloc().
|
|
|
|
*/
|
|
|
|
void *sqlite3PageMalloc(int sz){
|
2011-01-17 21:32:24 +00:00
|
|
|
return pcache1Alloc(sz);
|
2008-11-13 14:28:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Free an allocated buffer obtained from sqlite3PageMalloc().
|
|
|
|
*/
|
|
|
|
void sqlite3PageFree(void *p){
|
|
|
|
pcache1Free(p);
|
|
|
|
}
|
|
|
|
|
2010-08-27 12:21:06 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
** Return true if it desirable to avoid allocating a new page cache
|
|
|
|
** entry.
|
|
|
|
**
|
|
|
|
** If memory was allocated specifically to the page cache using
|
|
|
|
** SQLITE_CONFIG_PAGECACHE but that memory has all been used, then
|
|
|
|
** it is desirable to avoid allocating a new page cache entry because
|
|
|
|
** presumably SQLITE_CONFIG_PAGECACHE was suppose to be sufficient
|
|
|
|
** for all page cache needs and we should not need to spill the
|
|
|
|
** allocation onto the heap.
|
|
|
|
**
|
2012-01-08 22:18:33 +00:00
|
|
|
** Or, the heap is used for all page cache memory but the heap is
|
2010-08-27 12:21:06 +00:00
|
|
|
** under memory pressure, then again it is desirable to avoid
|
|
|
|
** allocating a new page cache entry in order to avoid stressing
|
|
|
|
** the heap even further.
|
|
|
|
*/
|
|
|
|
static int pcache1UnderMemoryPressure(PCache1 *pCache){
|
2011-11-08 20:08:44 +00:00
|
|
|
if( pcache1.nSlot && (pCache->szPage+pCache->szExtra)<=pcache1.szSlot ){
|
2011-01-17 21:32:24 +00:00
|
|
|
return pcache1.bUnderPressure;
|
2010-08-27 12:21:06 +00:00
|
|
|
}else{
|
|
|
|
return sqlite3HeapNearlyFull();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-13 14:28:28 +00:00
|
|
|
/******************************************************************************/
|
|
|
|
/******** General Implementation Functions ************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
** This function is used to resize the hash table used by the cache passed
|
|
|
|
** as the first argument.
|
|
|
|
**
|
2011-01-17 21:32:24 +00:00
|
|
|
** The PCache mutex must be held when this function is called.
|
2008-11-13 14:28:28 +00:00
|
|
|
*/
|
2014-08-23 23:15:31 +00:00
|
|
|
static void pcache1ResizeHash(PCache1 *p){
|
2008-11-13 14:28:28 +00:00
|
|
|
PgHdr1 **apNew;
|
2008-11-15 11:22:45 +00:00
|
|
|
unsigned int nNew;
|
2008-11-13 14:28:28 +00:00
|
|
|
unsigned int i;
|
|
|
|
|
2011-01-17 21:32:24 +00:00
|
|
|
assert( sqlite3_mutex_held(p->pGroup->mutex) );
|
2008-11-13 14:28:28 +00:00
|
|
|
|
|
|
|
nNew = p->nHash*2;
|
|
|
|
if( nNew<256 ){
|
|
|
|
nNew = 256;
|
|
|
|
}
|
|
|
|
|
2011-01-17 21:32:24 +00:00
|
|
|
pcache1LeaveMutex(p->pGroup);
|
2008-12-06 14:34:33 +00:00
|
|
|
if( p->nHash ){ sqlite3BeginBenignMalloc(); }
|
2012-07-30 14:53:54 +00:00
|
|
|
apNew = (PgHdr1 **)sqlite3MallocZero(sizeof(PgHdr1 *)*nNew);
|
2008-12-06 14:34:33 +00:00
|
|
|
if( p->nHash ){ sqlite3EndBenignMalloc(); }
|
2011-01-17 21:32:24 +00:00
|
|
|
pcache1EnterMutex(p->pGroup);
|
2008-11-13 14:28:28 +00:00
|
|
|
if( apNew ){
|
|
|
|
for(i=0; i<p->nHash; i++){
|
|
|
|
PgHdr1 *pPage;
|
|
|
|
PgHdr1 *pNext = p->apHash[i];
|
2008-12-10 18:03:45 +00:00
|
|
|
while( (pPage = pNext)!=0 ){
|
2008-11-13 14:28:28 +00:00
|
|
|
unsigned int h = pPage->iKey % nNew;
|
|
|
|
pNext = pPage->pNext;
|
|
|
|
pPage->pNext = apNew[h];
|
|
|
|
apNew[h] = pPage;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sqlite3_free(p->apHash);
|
|
|
|
p->apHash = apNew;
|
|
|
|
p->nHash = nNew;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** This function is used internally to remove the page pPage from the
|
2011-01-17 21:32:24 +00:00
|
|
|
** PGroup LRU list, if is part of it. If pPage is not part of the PGroup
|
2008-11-13 14:28:28 +00:00
|
|
|
** LRU list, then this function is a no-op.
|
|
|
|
**
|
2011-01-17 21:32:24 +00:00
|
|
|
** The PGroup mutex must be held when this function is called.
|
2008-11-13 14:28:28 +00:00
|
|
|
*/
|
|
|
|
static void pcache1PinPage(PgHdr1 *pPage){
|
2011-01-17 21:32:24 +00:00
|
|
|
PCache1 *pCache;
|
|
|
|
|
2013-12-13 18:50:40 +00:00
|
|
|
assert( pPage!=0 );
|
|
|
|
assert( pPage->isPinned==0 );
|
2011-01-17 21:32:24 +00:00
|
|
|
pCache = pPage->pCache;
|
2015-06-12 13:04:51 +00:00
|
|
|
assert( pPage->pLruNext || pPage==pCache->pGroup->pLruTail );
|
|
|
|
assert( pPage->pLruPrev || pPage==pCache->pGroup->pLruHead );
|
|
|
|
assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
|
2013-12-13 18:50:40 +00:00
|
|
|
if( pPage->pLruPrev ){
|
|
|
|
pPage->pLruPrev->pLruNext = pPage->pLruNext;
|
|
|
|
}else{
|
2015-06-12 13:04:51 +00:00
|
|
|
pCache->pGroup->pLruHead = pPage->pLruNext;
|
2013-12-13 18:50:40 +00:00
|
|
|
}
|
|
|
|
if( pPage->pLruNext ){
|
|
|
|
pPage->pLruNext->pLruPrev = pPage->pLruPrev;
|
|
|
|
}else{
|
2015-06-12 13:04:51 +00:00
|
|
|
pCache->pGroup->pLruTail = pPage->pLruPrev;
|
2008-11-13 14:28:28 +00:00
|
|
|
}
|
2013-12-13 18:50:40 +00:00
|
|
|
pPage->pLruNext = 0;
|
|
|
|
pPage->pLruPrev = 0;
|
|
|
|
pPage->isPinned = 1;
|
|
|
|
pCache->nRecyclable--;
|
2008-11-13 14:28:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Remove the page supplied as an argument from the hash table
|
|
|
|
** (PCache1.apHash structure) that it is currently stored in.
|
|
|
|
**
|
2011-01-17 21:32:24 +00:00
|
|
|
** The PGroup mutex must be held when this function is called.
|
2008-11-13 14:28:28 +00:00
|
|
|
*/
|
|
|
|
static void pcache1RemoveFromHash(PgHdr1 *pPage){
|
|
|
|
unsigned int h;
|
|
|
|
PCache1 *pCache = pPage->pCache;
|
|
|
|
PgHdr1 **pp;
|
|
|
|
|
2011-01-17 21:32:24 +00:00
|
|
|
assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
|
2008-11-13 14:28:28 +00:00
|
|
|
h = pPage->iKey % pCache->nHash;
|
|
|
|
for(pp=&pCache->apHash[h]; (*pp)!=pPage; pp=&(*pp)->pNext);
|
|
|
|
*pp = (*pp)->pNext;
|
|
|
|
|
|
|
|
pCache->nPage--;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2011-01-17 21:32:24 +00:00
|
|
|
** If there are currently more than nMaxPage pages allocated, try
|
|
|
|
** to recycle pages to reduce the number allocated to nMaxPage.
|
2008-11-13 14:28:28 +00:00
|
|
|
*/
|
2011-01-17 21:32:24 +00:00
|
|
|
static void pcache1EnforceMaxPage(PGroup *pGroup){
|
|
|
|
assert( sqlite3_mutex_held(pGroup->mutex) );
|
|
|
|
while( pGroup->nCurrentPage>pGroup->nMaxPage && pGroup->pLruTail ){
|
|
|
|
PgHdr1 *p = pGroup->pLruTail;
|
|
|
|
assert( p->pCache->pGroup==pGroup );
|
2013-12-13 18:50:40 +00:00
|
|
|
assert( p->isPinned==0 );
|
2008-11-13 14:28:28 +00:00
|
|
|
pcache1PinPage(p);
|
|
|
|
pcache1RemoveFromHash(p);
|
|
|
|
pcache1FreePage(p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Discard all pages from cache pCache with a page number (key value)
|
|
|
|
** greater than or equal to iLimit. Any pinned pages that meet this
|
|
|
|
** criteria are unpinned before they are discarded.
|
|
|
|
**
|
2011-01-17 21:32:24 +00:00
|
|
|
** The PCache mutex must be held when this function is called.
|
2008-11-13 14:28:28 +00:00
|
|
|
*/
|
|
|
|
static void pcache1TruncateUnsafe(
|
2011-01-17 21:32:24 +00:00
|
|
|
PCache1 *pCache, /* The cache to truncate */
|
|
|
|
unsigned int iLimit /* Drop pages with this pgno or larger */
|
2008-11-13 14:28:28 +00:00
|
|
|
){
|
2011-01-17 21:32:24 +00:00
|
|
|
TESTONLY( unsigned int nPage = 0; ) /* To assert pCache->nPage is correct */
|
2008-11-13 14:28:28 +00:00
|
|
|
unsigned int h;
|
2011-01-17 21:32:24 +00:00
|
|
|
assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
|
2008-11-13 14:28:28 +00:00
|
|
|
for(h=0; h<pCache->nHash; h++){
|
|
|
|
PgHdr1 **pp = &pCache->apHash[h];
|
|
|
|
PgHdr1 *pPage;
|
2008-12-10 18:03:45 +00:00
|
|
|
while( (pPage = *pp)!=0 ){
|
2008-11-13 14:28:28 +00:00
|
|
|
if( pPage->iKey>=iLimit ){
|
2009-05-08 06:52:47 +00:00
|
|
|
pCache->nPage--;
|
2008-11-13 14:28:28 +00:00
|
|
|
*pp = pPage->pNext;
|
2013-12-13 18:50:40 +00:00
|
|
|
if( !pPage->isPinned ) pcache1PinPage(pPage);
|
2008-11-13 14:28:28 +00:00
|
|
|
pcache1FreePage(pPage);
|
|
|
|
}else{
|
|
|
|
pp = &pPage->pNext;
|
2009-05-08 06:52:47 +00:00
|
|
|
TESTONLY( nPage++; )
|
2008-11-13 14:28:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-05-08 06:52:47 +00:00
|
|
|
assert( pCache->nPage==nPage );
|
2008-11-13 14:28:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/******** sqlite3_pcache Methods **********************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Implementation of the sqlite3_pcache.xInit method.
|
|
|
|
*/
|
2008-11-19 09:05:26 +00:00
|
|
|
static int pcache1Init(void *NotUsed){
|
|
|
|
UNUSED_PARAMETER(NotUsed);
|
2009-05-22 11:10:24 +00:00
|
|
|
assert( pcache1.isInit==0 );
|
2008-11-13 14:28:28 +00:00
|
|
|
memset(&pcache1, 0, sizeof(pcache1));
|
|
|
|
if( sqlite3GlobalConfig.bCoreMutex ){
|
2011-01-17 21:32:24 +00:00
|
|
|
pcache1.grp.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU);
|
2011-01-18 15:17:57 +00:00
|
|
|
pcache1.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_PMEM);
|
2008-11-13 14:28:28 +00:00
|
|
|
}
|
2011-01-25 04:34:51 +00:00
|
|
|
pcache1.grp.mxPinned = 10;
|
2009-05-22 11:10:24 +00:00
|
|
|
pcache1.isInit = 1;
|
2008-11-13 14:28:28 +00:00
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Implementation of the sqlite3_pcache.xShutdown method.
|
2009-08-17 15:31:23 +00:00
|
|
|
** Note that the static mutex allocated in xInit does
|
|
|
|
** not need to be freed.
|
2008-11-13 14:28:28 +00:00
|
|
|
*/
|
2008-11-19 09:05:26 +00:00
|
|
|
static void pcache1Shutdown(void *NotUsed){
|
|
|
|
UNUSED_PARAMETER(NotUsed);
|
2009-05-22 11:10:24 +00:00
|
|
|
assert( pcache1.isInit!=0 );
|
2009-05-22 10:53:29 +00:00
|
|
|
memset(&pcache1, 0, sizeof(pcache1));
|
2008-11-13 14:28:28 +00:00
|
|
|
}
|
|
|
|
|
2014-08-23 23:15:31 +00:00
|
|
|
/* forward declaration */
|
|
|
|
static void pcache1Destroy(sqlite3_pcache *p);
|
|
|
|
|
2008-11-13 14:28:28 +00:00
|
|
|
/*
|
|
|
|
** Implementation of the sqlite3_pcache.xCreate method.
|
|
|
|
**
|
|
|
|
** Allocate a new cache.
|
|
|
|
*/
|
2011-11-09 00:06:05 +00:00
|
|
|
static sqlite3_pcache *pcache1Create(int szPage, int szExtra, int bPurgeable){
|
2011-01-17 21:32:24 +00:00
|
|
|
PCache1 *pCache; /* The newly created page cache */
|
|
|
|
PGroup *pGroup; /* The group the new page cache will belong to */
|
|
|
|
int sz; /* Bytes of memory required to allocate the new cache */
|
|
|
|
|
|
|
|
/*
|
2013-05-28 12:11:54 +00:00
|
|
|
** The separateCache variable is true if each PCache has its own private
|
2011-01-17 21:32:24 +00:00
|
|
|
** PGroup. In other words, separateCache is true for mode (1) where no
|
|
|
|
** mutexing is required.
|
|
|
|
**
|
|
|
|
** * Always use a unified cache (mode-2) if ENABLE_MEMORY_MANAGEMENT
|
|
|
|
**
|
|
|
|
** * Always use a unified cache in single-threaded applications
|
|
|
|
**
|
|
|
|
** * Otherwise (if multi-threaded and ENABLE_MEMORY_MANAGEMENT is off)
|
|
|
|
** use separate caches (mode-1)
|
|
|
|
*/
|
|
|
|
#if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) || SQLITE_THREADSAFE==0
|
|
|
|
const int separateCache = 0;
|
|
|
|
#else
|
|
|
|
int separateCache = sqlite3GlobalConfig.bCoreMutex>0;
|
|
|
|
#endif
|
2008-11-13 14:28:28 +00:00
|
|
|
|
2011-11-09 16:12:24 +00:00
|
|
|
assert( (szPage & (szPage-1))==0 && szPage>=512 && szPage<=65536 );
|
|
|
|
assert( szExtra < 300 );
|
|
|
|
|
2011-01-17 21:32:24 +00:00
|
|
|
sz = sizeof(PCache1) + sizeof(PGroup)*separateCache;
|
2012-07-30 14:53:54 +00:00
|
|
|
pCache = (PCache1 *)sqlite3MallocZero(sz);
|
2008-11-13 14:28:28 +00:00
|
|
|
if( pCache ){
|
2011-01-17 21:32:24 +00:00
|
|
|
if( separateCache ){
|
|
|
|
pGroup = (PGroup*)&pCache[1];
|
2011-01-25 04:34:51 +00:00
|
|
|
pGroup->mxPinned = 10;
|
2011-01-17 21:32:24 +00:00
|
|
|
}else{
|
2011-06-09 17:53:43 +00:00
|
|
|
pGroup = &pcache1.grp;
|
2011-01-17 21:32:24 +00:00
|
|
|
}
|
|
|
|
pCache->pGroup = pGroup;
|
2008-11-13 14:28:28 +00:00
|
|
|
pCache->szPage = szPage;
|
2011-11-08 20:08:44 +00:00
|
|
|
pCache->szExtra = szExtra;
|
2008-11-13 14:28:28 +00:00
|
|
|
pCache->bPurgeable = (bPurgeable ? 1 : 0);
|
2014-08-23 23:15:31 +00:00
|
|
|
pcache1EnterMutex(pGroup);
|
|
|
|
pcache1ResizeHash(pCache);
|
2008-11-13 14:28:28 +00:00
|
|
|
if( bPurgeable ){
|
|
|
|
pCache->nMin = 10;
|
2011-01-17 21:32:24 +00:00
|
|
|
pGroup->nMinPage += pCache->nMin;
|
2011-01-25 04:34:51 +00:00
|
|
|
pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
|
2014-08-23 23:15:31 +00:00
|
|
|
}
|
|
|
|
pcache1LeaveMutex(pGroup);
|
|
|
|
if( pCache->nHash==0 ){
|
|
|
|
pcache1Destroy((sqlite3_pcache*)pCache);
|
|
|
|
pCache = 0;
|
2008-11-13 14:28:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return (sqlite3_pcache *)pCache;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Implementation of the sqlite3_pcache.xCachesize method.
|
|
|
|
**
|
|
|
|
** Configure the cache_size limit for a cache.
|
|
|
|
*/
|
|
|
|
static void pcache1Cachesize(sqlite3_pcache *p, int nMax){
|
|
|
|
PCache1 *pCache = (PCache1 *)p;
|
|
|
|
if( pCache->bPurgeable ){
|
2011-01-17 21:32:24 +00:00
|
|
|
PGroup *pGroup = pCache->pGroup;
|
|
|
|
pcache1EnterMutex(pGroup);
|
|
|
|
pGroup->nMaxPage += (nMax - pCache->nMax);
|
2011-01-25 04:34:51 +00:00
|
|
|
pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
|
2008-11-13 14:28:28 +00:00
|
|
|
pCache->nMax = nMax;
|
2011-01-26 00:07:03 +00:00
|
|
|
pCache->n90pct = pCache->nMax*9/10;
|
2011-01-17 21:32:24 +00:00
|
|
|
pcache1EnforceMaxPage(pGroup);
|
|
|
|
pcache1LeaveMutex(pGroup);
|
2008-11-13 14:28:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-16 19:29:17 +00:00
|
|
|
/*
|
|
|
|
** Implementation of the sqlite3_pcache.xShrink method.
|
|
|
|
**
|
|
|
|
** Free up as much memory as possible.
|
|
|
|
*/
|
|
|
|
static void pcache1Shrink(sqlite3_pcache *p){
|
|
|
|
PCache1 *pCache = (PCache1*)p;
|
|
|
|
if( pCache->bPurgeable ){
|
|
|
|
PGroup *pGroup = pCache->pGroup;
|
|
|
|
int savedMaxPage;
|
|
|
|
pcache1EnterMutex(pGroup);
|
|
|
|
savedMaxPage = pGroup->nMaxPage;
|
|
|
|
pGroup->nMaxPage = 0;
|
|
|
|
pcache1EnforceMaxPage(pGroup);
|
|
|
|
pGroup->nMaxPage = savedMaxPage;
|
|
|
|
pcache1LeaveMutex(pGroup);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-13 14:28:28 +00:00
|
|
|
/*
|
|
|
|
** Implementation of the sqlite3_pcache.xPagecount method.
|
|
|
|
*/
|
|
|
|
static int pcache1Pagecount(sqlite3_pcache *p){
|
|
|
|
int n;
|
2011-01-17 21:32:24 +00:00
|
|
|
PCache1 *pCache = (PCache1*)p;
|
|
|
|
pcache1EnterMutex(pCache->pGroup);
|
|
|
|
n = pCache->nPage;
|
|
|
|
pcache1LeaveMutex(pCache->pGroup);
|
2008-11-13 14:28:28 +00:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2014-08-23 23:15:31 +00:00
|
|
|
|
2008-11-13 14:28:28 +00:00
|
|
|
/*
|
2014-08-23 23:15:31 +00:00
|
|
|
** Implement steps 3, 4, and 5 of the pcache1Fetch() algorithm described
|
|
|
|
** in the header of the pcache1Fetch() procedure.
|
2008-11-13 14:28:28 +00:00
|
|
|
**
|
2014-08-23 23:15:31 +00:00
|
|
|
** This steps are broken out into a separate procedure because they are
|
|
|
|
** usually not needed, and by avoiding the stack initialization required
|
|
|
|
** for these steps, the main pcache1Fetch() procedure can run faster.
|
2008-11-13 14:28:28 +00:00
|
|
|
*/
|
2014-08-23 23:15:31 +00:00
|
|
|
static SQLITE_NOINLINE PgHdr1 *pcache1FetchStage2(
|
|
|
|
PCache1 *pCache,
|
2011-11-08 20:08:44 +00:00
|
|
|
unsigned int iKey,
|
|
|
|
int createFlag
|
|
|
|
){
|
2012-01-02 18:00:55 +00:00
|
|
|
unsigned int nPinned;
|
2014-08-23 23:15:31 +00:00
|
|
|
PGroup *pGroup = pCache->pGroup;
|
2008-11-13 14:28:28 +00:00
|
|
|
PgHdr1 *pPage = 0;
|
|
|
|
|
2011-01-19 21:58:56 +00:00
|
|
|
/* Step 3: Abort if createFlag is 1 but the cache is nearly full */
|
2012-01-02 18:00:55 +00:00
|
|
|
assert( pCache->nPage >= pCache->nRecyclable );
|
2008-11-13 14:28:28 +00:00
|
|
|
nPinned = pCache->nPage - pCache->nRecyclable;
|
2011-01-25 04:34:51 +00:00
|
|
|
assert( pGroup->mxPinned == pGroup->nMaxPage + 10 - pGroup->nMinPage );
|
2011-01-26 00:07:03 +00:00
|
|
|
assert( pCache->n90pct == pCache->nMax*9/10 );
|
2009-07-17 11:44:07 +00:00
|
|
|
if( createFlag==1 && (
|
2011-01-25 04:34:51 +00:00
|
|
|
nPinned>=pGroup->mxPinned
|
2012-01-02 18:00:55 +00:00
|
|
|
|| nPinned>=pCache->n90pct
|
2014-10-10 19:10:59 +00:00
|
|
|
|| (pcache1UnderMemoryPressure(pCache) && pCache->nRecyclable<nPinned)
|
2008-11-13 14:28:28 +00:00
|
|
|
)){
|
2014-08-23 23:15:31 +00:00
|
|
|
return 0;
|
2008-11-13 14:28:28 +00:00
|
|
|
}
|
|
|
|
|
2014-08-23 23:15:31 +00:00
|
|
|
if( pCache->nPage>=pCache->nHash ) pcache1ResizeHash(pCache);
|
2013-08-19 18:17:03 +00:00
|
|
|
assert( pCache->nHash>0 && pCache->apHash );
|
2008-11-13 14:28:28 +00:00
|
|
|
|
2011-01-19 21:58:56 +00:00
|
|
|
/* Step 4. Try to recycle a page. */
|
2011-01-17 21:32:24 +00:00
|
|
|
if( pCache->bPurgeable && pGroup->pLruTail && (
|
2010-08-27 12:21:06 +00:00
|
|
|
(pCache->nPage+1>=pCache->nMax)
|
2011-01-17 21:32:24 +00:00
|
|
|
|| pGroup->nCurrentPage>=pGroup->nMaxPage
|
2010-08-27 12:21:06 +00:00
|
|
|
|| pcache1UnderMemoryPressure(pCache)
|
2008-11-13 14:28:28 +00:00
|
|
|
)){
|
2011-11-09 16:12:24 +00:00
|
|
|
PCache1 *pOther;
|
2011-01-17 21:32:24 +00:00
|
|
|
pPage = pGroup->pLruTail;
|
2013-12-13 18:50:40 +00:00
|
|
|
assert( pPage->isPinned==0 );
|
2008-11-13 14:28:28 +00:00
|
|
|
pcache1RemoveFromHash(pPage);
|
|
|
|
pcache1PinPage(pPage);
|
2011-11-09 16:12:24 +00:00
|
|
|
pOther = pPage->pCache;
|
|
|
|
|
|
|
|
/* We want to verify that szPage and szExtra are the same for pOther
|
|
|
|
** and pCache. Assert that we can verify this by comparing sums. */
|
|
|
|
assert( (pCache->szPage & (pCache->szPage-1))==0 && pCache->szPage>=512 );
|
|
|
|
assert( pCache->szExtra<512 );
|
|
|
|
assert( (pOther->szPage & (pOther->szPage-1))==0 && pOther->szPage>=512 );
|
|
|
|
assert( pOther->szExtra<512 );
|
|
|
|
|
|
|
|
if( pOther->szPage+pOther->szExtra != pCache->szPage+pCache->szExtra ){
|
2008-11-13 14:28:28 +00:00
|
|
|
pcache1FreePage(pPage);
|
|
|
|
pPage = 0;
|
|
|
|
}else{
|
2011-11-09 16:12:24 +00:00
|
|
|
pGroup->nCurrentPage -= (pOther->bPurgeable - pCache->bPurgeable);
|
2008-11-13 14:28:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Step 5. If a usable page buffer has still not been found,
|
|
|
|
** attempt to allocate a new one.
|
|
|
|
*/
|
|
|
|
if( !pPage ){
|
2011-01-25 04:34:51 +00:00
|
|
|
if( createFlag==1 ) sqlite3BeginBenignMalloc();
|
2008-11-13 14:28:28 +00:00
|
|
|
pPage = pcache1AllocPage(pCache);
|
2011-01-25 04:34:51 +00:00
|
|
|
if( createFlag==1 ) sqlite3EndBenignMalloc();
|
2008-11-13 14:28:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( pPage ){
|
|
|
|
unsigned int h = iKey % pCache->nHash;
|
|
|
|
pCache->nPage++;
|
|
|
|
pPage->iKey = iKey;
|
|
|
|
pPage->pNext = pCache->apHash[h];
|
|
|
|
pPage->pCache = pCache;
|
2009-01-23 16:45:00 +00:00
|
|
|
pPage->pLruPrev = 0;
|
|
|
|
pPage->pLruNext = 0;
|
2013-12-13 18:50:40 +00:00
|
|
|
pPage->isPinned = 1;
|
2011-11-08 20:08:44 +00:00
|
|
|
*(void **)pPage->page.pExtra = 0;
|
2008-11-13 14:28:28 +00:00
|
|
|
pCache->apHash[h] = pPage;
|
2014-08-23 23:15:31 +00:00
|
|
|
if( iKey>pCache->iMaxKey ){
|
|
|
|
pCache->iMaxKey = iKey;
|
|
|
|
}
|
2008-11-13 14:28:28 +00:00
|
|
|
}
|
2014-08-23 23:15:31 +00:00
|
|
|
return pPage;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Implementation of the sqlite3_pcache.xFetch method.
|
|
|
|
**
|
|
|
|
** Fetch a page by key value.
|
|
|
|
**
|
|
|
|
** Whether or not a new page may be allocated by this function depends on
|
|
|
|
** the value of the createFlag argument. 0 means do not allocate a new
|
|
|
|
** page. 1 means allocate a new page if space is easily available. 2
|
|
|
|
** means to try really hard to allocate a new page.
|
|
|
|
**
|
|
|
|
** For a non-purgeable cache (a cache used as the storage for an in-memory
|
|
|
|
** database) there is really no difference between createFlag 1 and 2. So
|
|
|
|
** the calling function (pcache.c) will never have a createFlag of 1 on
|
|
|
|
** a non-purgeable cache.
|
|
|
|
**
|
|
|
|
** There are three different approaches to obtaining space for a page,
|
|
|
|
** depending on the value of parameter createFlag (which may be 0, 1 or 2).
|
|
|
|
**
|
|
|
|
** 1. Regardless of the value of createFlag, the cache is searched for a
|
|
|
|
** copy of the requested page. If one is found, it is returned.
|
|
|
|
**
|
|
|
|
** 2. If createFlag==0 and the page is not already in the cache, NULL is
|
|
|
|
** returned.
|
|
|
|
**
|
|
|
|
** 3. If createFlag is 1, and the page is not already in the cache, then
|
|
|
|
** return NULL (do not allocate a new page) if any of the following
|
|
|
|
** conditions are true:
|
|
|
|
**
|
|
|
|
** (a) the number of pages pinned by the cache is greater than
|
|
|
|
** PCache1.nMax, or
|
|
|
|
**
|
|
|
|
** (b) the number of pages pinned by the cache is greater than
|
|
|
|
** the sum of nMax for all purgeable caches, less the sum of
|
|
|
|
** nMin for all other purgeable caches, or
|
|
|
|
**
|
|
|
|
** 4. If none of the first three conditions apply and the cache is marked
|
|
|
|
** as purgeable, and if one of the following is true:
|
|
|
|
**
|
|
|
|
** (a) The number of pages allocated for the cache is already
|
|
|
|
** PCache1.nMax, or
|
|
|
|
**
|
|
|
|
** (b) The number of pages allocated for all purgeable caches is
|
|
|
|
** already equal to or greater than the sum of nMax for all
|
|
|
|
** purgeable caches,
|
|
|
|
**
|
|
|
|
** (c) The system is under memory pressure and wants to avoid
|
|
|
|
** unnecessary pages cache entry allocations
|
|
|
|
**
|
|
|
|
** then attempt to recycle a page from the LRU list. If it is the right
|
|
|
|
** size, return the recycled buffer. Otherwise, free the buffer and
|
|
|
|
** proceed to step 5.
|
|
|
|
**
|
|
|
|
** 5. Otherwise, allocate and return a new page buffer.
|
|
|
|
*/
|
|
|
|
static sqlite3_pcache_page *pcache1Fetch(
|
|
|
|
sqlite3_pcache *p,
|
|
|
|
unsigned int iKey,
|
|
|
|
int createFlag
|
|
|
|
){
|
|
|
|
PCache1 *pCache = (PCache1 *)p;
|
|
|
|
PgHdr1 *pPage = 0;
|
|
|
|
|
|
|
|
assert( offsetof(PgHdr1,page)==0 );
|
|
|
|
assert( pCache->bPurgeable || createFlag!=1 );
|
|
|
|
assert( pCache->bPurgeable || pCache->nMin==0 );
|
|
|
|
assert( pCache->bPurgeable==0 || pCache->nMin==10 );
|
|
|
|
assert( pCache->nMin==0 || pCache->bPurgeable );
|
|
|
|
assert( pCache->nHash>0 );
|
|
|
|
pcache1EnterMutex(pCache->pGroup);
|
|
|
|
|
|
|
|
/* Step 1: Search the hash table for an existing entry. */
|
|
|
|
pPage = pCache->apHash[iKey % pCache->nHash];
|
|
|
|
while( pPage && pPage->iKey!=iKey ){ pPage = pPage->pNext; }
|
2008-11-13 14:28:28 +00:00
|
|
|
|
2014-08-23 23:15:31 +00:00
|
|
|
/* Step 2: Abort if no existing page is found and createFlag is 0 */
|
|
|
|
if( pPage ){
|
|
|
|
if( !pPage->isPinned ) pcache1PinPage(pPage);
|
|
|
|
}else if( createFlag ){
|
|
|
|
/* Steps 3, 4, and 5 implemented by this subroutine */
|
|
|
|
pPage = pcache1FetchStage2(pCache, iKey, createFlag);
|
2009-01-07 15:18:20 +00:00
|
|
|
}
|
2014-08-23 23:15:31 +00:00
|
|
|
assert( pPage==0 || pCache->iMaxKey>=iKey );
|
|
|
|
pcache1LeaveMutex(pCache->pGroup);
|
2014-01-24 22:58:00 +00:00
|
|
|
return (sqlite3_pcache_page*)pPage;
|
2008-11-13 14:28:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Implementation of the sqlite3_pcache.xUnpin method.
|
|
|
|
**
|
|
|
|
** Mark a page as unpinned (eligible for asynchronous recycling).
|
|
|
|
*/
|
2011-11-08 20:08:44 +00:00
|
|
|
static void pcache1Unpin(
|
|
|
|
sqlite3_pcache *p,
|
|
|
|
sqlite3_pcache_page *pPg,
|
|
|
|
int reuseUnlikely
|
|
|
|
){
|
2008-11-13 14:28:28 +00:00
|
|
|
PCache1 *pCache = (PCache1 *)p;
|
2011-11-08 20:08:44 +00:00
|
|
|
PgHdr1 *pPage = (PgHdr1 *)pPg;
|
2011-01-17 21:32:24 +00:00
|
|
|
PGroup *pGroup = pCache->pGroup;
|
2009-06-03 21:04:35 +00:00
|
|
|
|
|
|
|
assert( pPage->pCache==pCache );
|
2011-01-17 21:32:24 +00:00
|
|
|
pcache1EnterMutex(pGroup);
|
2008-11-13 14:28:28 +00:00
|
|
|
|
|
|
|
/* It is an error to call this function if the page is already
|
2011-01-17 21:32:24 +00:00
|
|
|
** part of the PGroup LRU list.
|
2008-11-13 14:28:28 +00:00
|
|
|
*/
|
|
|
|
assert( pPage->pLruPrev==0 && pPage->pLruNext==0 );
|
2011-01-17 21:32:24 +00:00
|
|
|
assert( pGroup->pLruHead!=pPage && pGroup->pLruTail!=pPage );
|
2013-12-13 18:50:40 +00:00
|
|
|
assert( pPage->isPinned==1 );
|
2008-11-13 14:28:28 +00:00
|
|
|
|
2011-01-17 21:32:24 +00:00
|
|
|
if( reuseUnlikely || pGroup->nCurrentPage>pGroup->nMaxPage ){
|
2008-11-13 14:28:28 +00:00
|
|
|
pcache1RemoveFromHash(pPage);
|
|
|
|
pcache1FreePage(pPage);
|
|
|
|
}else{
|
2011-01-17 21:32:24 +00:00
|
|
|
/* Add the page to the PGroup LRU list. */
|
|
|
|
if( pGroup->pLruHead ){
|
|
|
|
pGroup->pLruHead->pLruPrev = pPage;
|
|
|
|
pPage->pLruNext = pGroup->pLruHead;
|
|
|
|
pGroup->pLruHead = pPage;
|
2008-11-13 14:28:28 +00:00
|
|
|
}else{
|
2011-01-17 21:32:24 +00:00
|
|
|
pGroup->pLruTail = pPage;
|
|
|
|
pGroup->pLruHead = pPage;
|
2008-11-13 14:28:28 +00:00
|
|
|
}
|
|
|
|
pCache->nRecyclable++;
|
2013-12-13 18:50:40 +00:00
|
|
|
pPage->isPinned = 0;
|
2008-11-13 14:28:28 +00:00
|
|
|
}
|
|
|
|
|
2011-01-17 21:32:24 +00:00
|
|
|
pcache1LeaveMutex(pCache->pGroup);
|
2008-11-13 14:28:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Implementation of the sqlite3_pcache.xRekey method.
|
|
|
|
*/
|
|
|
|
static void pcache1Rekey(
|
|
|
|
sqlite3_pcache *p,
|
2011-11-08 20:08:44 +00:00
|
|
|
sqlite3_pcache_page *pPg,
|
2008-11-13 14:28:28 +00:00
|
|
|
unsigned int iOld,
|
|
|
|
unsigned int iNew
|
|
|
|
){
|
|
|
|
PCache1 *pCache = (PCache1 *)p;
|
2011-11-08 20:08:44 +00:00
|
|
|
PgHdr1 *pPage = (PgHdr1 *)pPg;
|
2008-11-13 14:28:28 +00:00
|
|
|
PgHdr1 **pp;
|
|
|
|
unsigned int h;
|
|
|
|
assert( pPage->iKey==iOld );
|
2009-06-03 21:04:35 +00:00
|
|
|
assert( pPage->pCache==pCache );
|
2008-11-13 14:28:28 +00:00
|
|
|
|
2011-01-17 21:32:24 +00:00
|
|
|
pcache1EnterMutex(pCache->pGroup);
|
2008-11-13 14:28:28 +00:00
|
|
|
|
|
|
|
h = iOld%pCache->nHash;
|
|
|
|
pp = &pCache->apHash[h];
|
|
|
|
while( (*pp)!=pPage ){
|
|
|
|
pp = &(*pp)->pNext;
|
|
|
|
}
|
|
|
|
*pp = pPage->pNext;
|
|
|
|
|
|
|
|
h = iNew%pCache->nHash;
|
|
|
|
pPage->iKey = iNew;
|
|
|
|
pPage->pNext = pCache->apHash[h];
|
|
|
|
pCache->apHash[h] = pPage;
|
2009-11-20 13:18:14 +00:00
|
|
|
if( iNew>pCache->iMaxKey ){
|
2009-01-07 15:18:20 +00:00
|
|
|
pCache->iMaxKey = iNew;
|
|
|
|
}
|
|
|
|
|
2011-01-17 21:32:24 +00:00
|
|
|
pcache1LeaveMutex(pCache->pGroup);
|
2008-11-13 14:28:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Implementation of the sqlite3_pcache.xTruncate method.
|
|
|
|
**
|
|
|
|
** Discard all unpinned pages in the cache with a page number equal to
|
|
|
|
** or greater than parameter iLimit. Any pinned pages with a page number
|
|
|
|
** equal to or greater than iLimit are implicitly unpinned.
|
|
|
|
*/
|
|
|
|
static void pcache1Truncate(sqlite3_pcache *p, unsigned int iLimit){
|
|
|
|
PCache1 *pCache = (PCache1 *)p;
|
2011-01-17 21:32:24 +00:00
|
|
|
pcache1EnterMutex(pCache->pGroup);
|
2009-01-07 15:18:20 +00:00
|
|
|
if( iLimit<=pCache->iMaxKey ){
|
|
|
|
pcache1TruncateUnsafe(pCache, iLimit);
|
|
|
|
pCache->iMaxKey = iLimit-1;
|
|
|
|
}
|
2011-01-17 21:32:24 +00:00
|
|
|
pcache1LeaveMutex(pCache->pGroup);
|
2008-11-13 14:28:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Implementation of the sqlite3_pcache.xDestroy method.
|
|
|
|
**
|
|
|
|
** Destroy a cache allocated using pcache1Create().
|
|
|
|
*/
|
|
|
|
static void pcache1Destroy(sqlite3_pcache *p){
|
|
|
|
PCache1 *pCache = (PCache1 *)p;
|
2011-01-17 21:32:24 +00:00
|
|
|
PGroup *pGroup = pCache->pGroup;
|
2010-09-22 19:06:02 +00:00
|
|
|
assert( pCache->bPurgeable || (pCache->nMax==0 && pCache->nMin==0) );
|
2011-01-17 21:32:24 +00:00
|
|
|
pcache1EnterMutex(pGroup);
|
2008-11-13 14:28:28 +00:00
|
|
|
pcache1TruncateUnsafe(pCache, 0);
|
2012-01-02 18:00:55 +00:00
|
|
|
assert( pGroup->nMaxPage >= pCache->nMax );
|
2011-01-17 21:32:24 +00:00
|
|
|
pGroup->nMaxPage -= pCache->nMax;
|
2012-01-02 18:00:55 +00:00
|
|
|
assert( pGroup->nMinPage >= pCache->nMin );
|
2011-01-17 21:32:24 +00:00
|
|
|
pGroup->nMinPage -= pCache->nMin;
|
2011-01-25 04:34:51 +00:00
|
|
|
pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
|
2011-01-17 21:32:24 +00:00
|
|
|
pcache1EnforceMaxPage(pGroup);
|
|
|
|
pcache1LeaveMutex(pGroup);
|
2008-11-13 14:28:28 +00:00
|
|
|
sqlite3_free(pCache->apHash);
|
|
|
|
sqlite3_free(pCache);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** This function is called during initialization (sqlite3_initialize()) to
|
|
|
|
** install the default pluggable cache module, assuming the user has not
|
|
|
|
** already provided an alternative.
|
|
|
|
*/
|
|
|
|
void sqlite3PCacheSetDefault(void){
|
2011-11-08 20:08:44 +00:00
|
|
|
static const sqlite3_pcache_methods2 defaultMethods = {
|
2011-11-13 21:44:03 +00:00
|
|
|
1, /* iVersion */
|
2008-11-13 14:28:28 +00:00
|
|
|
0, /* pArg */
|
|
|
|
pcache1Init, /* xInit */
|
|
|
|
pcache1Shutdown, /* xShutdown */
|
|
|
|
pcache1Create, /* xCreate */
|
|
|
|
pcache1Cachesize, /* xCachesize */
|
|
|
|
pcache1Pagecount, /* xPagecount */
|
|
|
|
pcache1Fetch, /* xFetch */
|
|
|
|
pcache1Unpin, /* xUnpin */
|
|
|
|
pcache1Rekey, /* xRekey */
|
|
|
|
pcache1Truncate, /* xTruncate */
|
2011-11-16 19:29:17 +00:00
|
|
|
pcache1Destroy, /* xDestroy */
|
|
|
|
pcache1Shrink /* xShrink */
|
2008-11-13 14:28:28 +00:00
|
|
|
};
|
2011-11-08 20:08:44 +00:00
|
|
|
sqlite3_config(SQLITE_CONFIG_PCACHE2, &defaultMethods);
|
2008-11-13 14:28:28 +00:00
|
|
|
}
|
|
|
|
|
2014-11-04 12:11:23 +00:00
|
|
|
/*
|
|
|
|
** Return the size of the header on each page of this PCACHE implementation.
|
|
|
|
*/
|
2014-12-30 00:57:29 +00:00
|
|
|
int sqlite3HeaderSizePcache1(void){ return ROUND8(sizeof(PgHdr1)); }
|
2014-11-04 12:11:23 +00:00
|
|
|
|
2015-03-23 17:25:18 +00:00
|
|
|
/*
|
|
|
|
** Return the global mutex used by this PCACHE implementation. The
|
|
|
|
** sqlite3_status() routine needs access to this mutex.
|
|
|
|
*/
|
|
|
|
sqlite3_mutex *sqlite3Pcache1Mutex(void){
|
|
|
|
return pcache1.mutex;
|
|
|
|
}
|
|
|
|
|
2008-11-13 14:28:28 +00:00
|
|
|
#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
|
|
|
|
/*
|
|
|
|
** This function is called to free superfluous dynamically allocated memory
|
|
|
|
** held by the pager system. Memory in use by any SQLite pager allocated
|
|
|
|
** by the current thread may be sqlite3_free()ed.
|
|
|
|
**
|
|
|
|
** nReq is the number of bytes of memory required. Once this much has
|
|
|
|
** been released, the function returns. The return value is the total number
|
|
|
|
** of bytes of memory released.
|
|
|
|
*/
|
|
|
|
int sqlite3PcacheReleaseMemory(int nReq){
|
|
|
|
int nFree = 0;
|
2011-01-17 21:32:24 +00:00
|
|
|
assert( sqlite3_mutex_notheld(pcache1.grp.mutex) );
|
|
|
|
assert( sqlite3_mutex_notheld(pcache1.mutex) );
|
2008-11-13 14:28:28 +00:00
|
|
|
if( pcache1.pStart==0 ){
|
|
|
|
PgHdr1 *p;
|
2011-01-17 21:32:24 +00:00
|
|
|
pcache1EnterMutex(&pcache1.grp);
|
|
|
|
while( (nReq<0 || nFree<nReq) && ((p=pcache1.grp.pLruTail)!=0) ){
|
2011-11-08 20:08:44 +00:00
|
|
|
nFree += pcache1MemSize(p->page.pBuf);
|
|
|
|
#ifdef SQLITE_PCACHE_SEPARATE_HEADER
|
|
|
|
nFree += sqlite3MemSize(p);
|
|
|
|
#endif
|
2013-12-13 18:50:40 +00:00
|
|
|
assert( p->isPinned==0 );
|
2008-11-13 14:28:28 +00:00
|
|
|
pcache1PinPage(p);
|
|
|
|
pcache1RemoveFromHash(p);
|
|
|
|
pcache1FreePage(p);
|
|
|
|
}
|
2011-01-17 21:32:24 +00:00
|
|
|
pcache1LeaveMutex(&pcache1.grp);
|
2008-11-13 14:28:28 +00:00
|
|
|
}
|
|
|
|
return nFree;
|
|
|
|
}
|
|
|
|
#endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
|
|
|
|
|
|
|
|
#ifdef SQLITE_TEST
|
|
|
|
/*
|
|
|
|
** This function is used by test procedures to inspect the internal state
|
|
|
|
** of the global cache.
|
|
|
|
*/
|
|
|
|
void sqlite3PcacheStats(
|
|
|
|
int *pnCurrent, /* OUT: Total number of pages cached */
|
|
|
|
int *pnMax, /* OUT: Global maximum cache size */
|
|
|
|
int *pnMin, /* OUT: Sum of PCache1.nMin for purgeable caches */
|
|
|
|
int *pnRecyclable /* OUT: Total number of pages available for recycling */
|
|
|
|
){
|
|
|
|
PgHdr1 *p;
|
|
|
|
int nRecyclable = 0;
|
2011-01-17 21:32:24 +00:00
|
|
|
for(p=pcache1.grp.pLruHead; p; p=p->pLruNext){
|
2013-12-13 18:50:40 +00:00
|
|
|
assert( p->isPinned==0 );
|
2008-11-13 14:28:28 +00:00
|
|
|
nRecyclable++;
|
|
|
|
}
|
2011-01-17 21:32:24 +00:00
|
|
|
*pnCurrent = pcache1.grp.nCurrentPage;
|
2012-01-02 18:00:55 +00:00
|
|
|
*pnMax = (int)pcache1.grp.nMaxPage;
|
|
|
|
*pnMin = (int)pcache1.grp.nMinPage;
|
2008-11-13 14:28:28 +00:00
|
|
|
*pnRecyclable = nRecyclable;
|
|
|
|
}
|
|
|
|
#endif
|