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