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