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