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. |
drh | 01c5c00 | 2015-07-04 18:15:04 +0000 | [diff] [blame] | 18 | ** |
| 19 | ** A Page cache line looks like this: |
| 20 | ** |
| 21 | ** ------------------------------------------------------------- |
| 22 | ** | database page content | PgHdr1 | MemPage | PgHdr | |
| 23 | ** ------------------------------------------------------------- |
| 24 | ** |
| 25 | ** The database page content is up front (so that buffer overreads tend to |
| 26 | ** flow harmlessly into the PgHdr1, MemPage, and PgHdr extensions). MemPage |
| 27 | ** is the extension added by the btree.c module containing information such |
| 28 | ** as the database page number and how that database page is used. PgHdr |
| 29 | ** is added by the pcache.c layer and contains information used to keep track |
| 30 | ** of which pages are "dirty". PgHdr1 is an extension added by this |
| 31 | ** module (pcache1.c). The PgHdr1 header is a subclass of sqlite3_pcache_page. |
| 32 | ** PgHdr1 contains information needed to look up a page by its page number. |
| 33 | ** The superclass sqlite3_pcache_page.pBuf points to the start of the |
| 34 | ** database page content and sqlite3_pcache_page.pExtra points to PgHdr. |
| 35 | ** |
| 36 | ** The size of the extension (MemPage+PgHdr+PgHdr1) can be determined at |
| 37 | ** runtime using sqlite3_config(SQLITE_CONFIG_PCACHE_HDRSZ, &size). The |
| 38 | ** sizes of the extensions sum to 272 bytes on x64 for 3.8.10, but this |
| 39 | ** size can vary according to architecture, compile-time options, and |
| 40 | ** SQLite library version number. |
| 41 | ** |
| 42 | ** If SQLITE_PCACHE_SEPARATE_HEADER is defined, then the extension is obtained |
| 43 | ** using a separate memory allocation from the database page content. This |
drh | 7b341e6 | 2015-07-08 14:13:44 +0000 | [diff] [blame] | 44 | ** seeks to overcome the "clownshoe" problem (also called "internal |
| 45 | ** fragmentation" in academic literature) of allocating a few bytes more |
drh | 01c5c00 | 2015-07-04 18:15:04 +0000 | [diff] [blame] | 46 | ** than a power of two with the memory allocator rounding up to the next |
| 47 | ** power of two, and leaving the rounded-up space unused. |
| 48 | ** |
| 49 | ** This module tracks pointers to PgHdr1 objects. Only pcache.c communicates |
| 50 | ** with this module. Information is passed back and forth as PgHdr1 pointers. |
| 51 | ** |
| 52 | ** The pcache.c and pager.c modules deal pointers to PgHdr objects. |
| 53 | ** The btree.c module deals with pointers to MemPage objects. |
drh | ee70a84 | 2015-07-06 18:54:52 +0000 | [diff] [blame] | 54 | ** |
| 55 | ** SOURCE OF PAGE CACHE MEMORY: |
| 56 | ** |
| 57 | ** Memory for a page might come from any of three sources: |
| 58 | ** |
| 59 | ** (1) The general-purpose memory allocator - sqlite3Malloc() |
| 60 | ** (2) Global page-cache memory provided using sqlite3_config() with |
| 61 | ** SQLITE_CONFIG_PAGECACHE. |
| 62 | ** (3) PCache-local bulk allocation. |
| 63 | ** |
| 64 | ** The third case is a chunk of heap memory (defaulting to 100 pages worth) |
| 65 | ** that is allocated when the page cache is created. The size of the local |
| 66 | ** bulk allocation can be adjusted using |
| 67 | ** |
| 68 | ** sqlite3_config(SQLITE_CONFIG_PCACHE, 0, 0, N). |
| 69 | ** |
| 70 | ** If N is positive, then N pages worth of memory are allocated using a single |
| 71 | ** sqlite3Malloc() call and that memory is used for the first N pages allocated. |
| 72 | ** Or if N is negative, then -1024*N bytes of memory are allocated and used |
| 73 | ** for as many pages as can be accomodated. |
| 74 | ** |
| 75 | ** Only one of (2) or (3) can be used. Once the memory available to (2) or |
| 76 | ** (3) is exhausted, subsequent allocations fail over to the general-purpose |
| 77 | ** memory allocator (1). |
| 78 | ** |
| 79 | ** Earlier versions of SQLite used only methods (1) and (2). But experiments |
| 80 | ** show that method (3) with N==100 provides about a 5% performance boost for |
| 81 | ** common workloads. |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 82 | */ |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 83 | #include "sqliteInt.h" |
| 84 | |
| 85 | typedef struct PCache1 PCache1; |
| 86 | typedef struct PgHdr1 PgHdr1; |
| 87 | typedef struct PgFreeslot PgFreeslot; |
drh | 9f8cf9d | 2011-01-17 21:32:24 +0000 | [diff] [blame] | 88 | typedef struct PGroup PGroup; |
| 89 | |
| 90 | /* 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] | 91 | ** 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] | 92 | ** pages when they are under memory pressure. A PGroup is an instance of |
| 93 | ** the following object. |
| 94 | ** |
| 95 | ** This page cache implementation works in one of two modes: |
| 96 | ** |
| 97 | ** (1) Every PCache is the sole member of its own PGroup. There is |
| 98 | ** one PGroup per PCache. |
| 99 | ** |
| 100 | ** (2) There is a single global PGroup that all PCaches are a member |
| 101 | ** of. |
| 102 | ** |
| 103 | ** Mode 1 uses more memory (since PCache instances are not able to rob |
| 104 | ** unused pages from other PCaches) but it also operates without a mutex, |
| 105 | ** 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] | 106 | ** threadsafe, but recycles pages more efficiently. |
drh | 9f8cf9d | 2011-01-17 21:32:24 +0000 | [diff] [blame] | 107 | ** |
| 108 | ** For mode (1), PGroup.mutex is NULL. For mode (2) there is only a single |
| 109 | ** PGroup which is the pcache1.grp global variable and its mutex is |
| 110 | ** SQLITE_MUTEX_STATIC_LRU. |
| 111 | */ |
| 112 | struct PGroup { |
| 113 | sqlite3_mutex *mutex; /* MUTEX_STATIC_LRU or NULL */ |
drh | a69085c | 2012-01-02 18:00:55 +0000 | [diff] [blame] | 114 | unsigned int nMaxPage; /* Sum of nMax for purgeable caches */ |
| 115 | unsigned int nMinPage; /* Sum of nMin for purgeable caches */ |
| 116 | unsigned int mxPinned; /* nMaxpage + 10 - nMinPage */ |
| 117 | unsigned int nCurrentPage; /* Number of purgeable pages allocated */ |
drh | 9f8cf9d | 2011-01-17 21:32:24 +0000 | [diff] [blame] | 118 | PgHdr1 *pLruHead, *pLruTail; /* LRU list of unpinned pages */ |
| 119 | }; |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 120 | |
drh | 9d13f11 | 2010-08-24 18:06:35 +0000 | [diff] [blame] | 121 | /* Each page cache is an instance of the following object. Every |
| 122 | ** open database file (including each in-memory database and each |
| 123 | ** temporary or transient database) has a single page cache which |
| 124 | ** is an instance of this object. |
| 125 | ** |
| 126 | ** Pointers to structures of this type are cast and returned as |
| 127 | ** opaque sqlite3_pcache* handles. |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 128 | */ |
| 129 | struct PCache1 { |
| 130 | /* Cache configuration parameters. Page size (szPage) and the purgeable |
| 131 | ** flag (bPurgeable) are set when the cache is created. nMax may be |
drh | 45d2930 | 2012-01-08 22:18:33 +0000 | [diff] [blame] | 132 | ** modified at any time by a call to the pcache1Cachesize() method. |
drh | 9f8cf9d | 2011-01-17 21:32:24 +0000 | [diff] [blame] | 133 | ** The PGroup mutex must be held when accessing nMax. |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 134 | */ |
drh | 9f8cf9d | 2011-01-17 21:32:24 +0000 | [diff] [blame] | 135 | PGroup *pGroup; /* PGroup this cache belongs to */ |
drh | ee70a84 | 2015-07-06 18:54:52 +0000 | [diff] [blame] | 136 | int szPage; /* Size of database content section */ |
| 137 | int szExtra; /* sizeof(MemPage)+sizeof(PgHdr) */ |
| 138 | int szAlloc; /* Total size of one pcache line */ |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 139 | int bPurgeable; /* True if cache is purgeable */ |
danielk1977 | 44cd45c | 2008-11-15 11:22:45 +0000 | [diff] [blame] | 140 | unsigned int nMin; /* Minimum number of pages reserved */ |
| 141 | unsigned int nMax; /* Configured "cache_size" value */ |
drh | 25ca568 | 2011-01-26 00:07:03 +0000 | [diff] [blame] | 142 | unsigned int n90pct; /* nMax*9/10 */ |
drh | 2cbd78b | 2012-02-02 19:37:18 +0000 | [diff] [blame] | 143 | unsigned int iMaxKey; /* Largest key seen since xTruncate() */ |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 144 | |
| 145 | /* Hash table of all pages. The following variables may only be accessed |
drh | 9f8cf9d | 2011-01-17 21:32:24 +0000 | [diff] [blame] | 146 | ** when the accessor is holding the PGroup mutex. |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 147 | */ |
danielk1977 | 44cd45c | 2008-11-15 11:22:45 +0000 | [diff] [blame] | 148 | unsigned int nRecyclable; /* Number of pages in the LRU list */ |
| 149 | unsigned int nPage; /* Total number of pages in apHash */ |
| 150 | unsigned int nHash; /* Number of slots in apHash[] */ |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 151 | PgHdr1 **apHash; /* Hash table for fast lookup by key */ |
drh | ee70a84 | 2015-07-06 18:54:52 +0000 | [diff] [blame] | 152 | PgHdr1 *pFree; /* List of unused pcache-local pages */ |
| 153 | void *pBulk; /* Bulk memory used by pcache-local */ |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 154 | }; |
| 155 | |
| 156 | /* |
| 157 | ** Each cache entry is represented by an instance of the following |
dan | 22e21ff | 2011-11-08 20:08:44 +0000 | [diff] [blame] | 158 | ** structure. Unless SQLITE_PCACHE_SEPARATE_HEADER is defined, a buffer of |
| 159 | ** PgHdr1.pCache->szPage bytes is allocated directly before this structure |
| 160 | ** in memory. |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 161 | */ |
| 162 | struct PgHdr1 { |
dan | 22e21ff | 2011-11-08 20:08:44 +0000 | [diff] [blame] | 163 | sqlite3_pcache_page page; |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 164 | unsigned int iKey; /* Key value (page number) */ |
drh | 5d56dd2 | 2013-12-13 18:50:40 +0000 | [diff] [blame] | 165 | u8 isPinned; /* Page in use, not on the LRU list */ |
drh | ee70a84 | 2015-07-06 18:54:52 +0000 | [diff] [blame] | 166 | u8 isBulkLocal; /* This page from bulk local storage */ |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 167 | PgHdr1 *pNext; /* Next in hash table chain */ |
| 168 | PCache1 *pCache; /* Cache that currently owns this page */ |
| 169 | PgHdr1 *pLruNext; /* Next in LRU list of unpinned pages */ |
| 170 | PgHdr1 *pLruPrev; /* Previous in LRU list of unpinned pages */ |
| 171 | }; |
| 172 | |
| 173 | /* |
drh | ee70a84 | 2015-07-06 18:54:52 +0000 | [diff] [blame] | 174 | ** Free slots in the allocator used to divide up the global page cache |
| 175 | ** buffer provided using the SQLITE_CONFIG_PAGECACHE mechanism. |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 176 | */ |
| 177 | struct PgFreeslot { |
| 178 | PgFreeslot *pNext; /* Next free slot */ |
| 179 | }; |
| 180 | |
| 181 | /* |
| 182 | ** Global data used by this cache. |
| 183 | */ |
| 184 | static SQLITE_WSD struct PCacheGlobal { |
drh | 9f8cf9d | 2011-01-17 21:32:24 +0000 | [diff] [blame] | 185 | PGroup grp; /* The global PGroup for mode (2) */ |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 186 | |
drh | 9f8cf9d | 2011-01-17 21:32:24 +0000 | [diff] [blame] | 187 | /* Variables related to SQLITE_CONFIG_PAGECACHE settings. The |
| 188 | ** szSlot, nSlot, pStart, pEnd, nReserve, and isInit values are all |
| 189 | ** fixed at sqlite3_initialize() time and do not require mutex protection. |
| 190 | ** The nFreeSlot and pFree values do require mutex protection. |
| 191 | */ |
| 192 | int isInit; /* True if initialized */ |
drh | db7ae89 | 2015-07-06 20:57:22 +0000 | [diff] [blame] | 193 | int separateCache; /* Use a new PGroup for each PCache */ |
drh | 957026a | 2015-07-16 18:18:19 +0000 | [diff] [blame] | 194 | int nInitPage; /* Initial bulk allocation size */ |
drh | 9f8cf9d | 2011-01-17 21:32:24 +0000 | [diff] [blame] | 195 | int szSlot; /* Size of each free slot */ |
| 196 | int nSlot; /* The number of pcache slots */ |
| 197 | int nReserve; /* Try to keep nFreeSlot above this */ |
drh | ee70a84 | 2015-07-06 18:54:52 +0000 | [diff] [blame] | 198 | void *pStart, *pEnd; /* Bounds of global page cache memory */ |
drh | 9f8cf9d | 2011-01-17 21:32:24 +0000 | [diff] [blame] | 199 | /* Above requires no mutex. Use mutex below for variable that follow. */ |
| 200 | sqlite3_mutex *mutex; /* Mutex for accessing the following: */ |
drh | 9f8cf9d | 2011-01-17 21:32:24 +0000 | [diff] [blame] | 201 | PgFreeslot *pFree; /* Free page blocks */ |
drh | 2cbd78b | 2012-02-02 19:37:18 +0000 | [diff] [blame] | 202 | int nFreeSlot; /* Number of unused pcache slots */ |
drh | 9f8cf9d | 2011-01-17 21:32:24 +0000 | [diff] [blame] | 203 | /* The following value requires a mutex to change. We skip the mutex on |
| 204 | ** reading because (1) most platforms read a 32-bit integer atomically and |
| 205 | ** (2) even if an incorrect value is read, no great harm is done since this |
| 206 | ** is really just an optimization. */ |
| 207 | int bUnderPressure; /* True if low on PAGECACHE memory */ |
danielk1977 | 44cd45c | 2008-11-15 11:22:45 +0000 | [diff] [blame] | 208 | } pcache1_g; |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 209 | |
| 210 | /* |
| 211 | ** All code in this file should access the global structure above via the |
| 212 | ** alias "pcache1". This ensures that the WSD emulation is used when |
| 213 | ** compiling for systems that do not support real WSD. |
| 214 | */ |
| 215 | #define pcache1 (GLOBAL(struct PCacheGlobal, pcache1_g)) |
| 216 | |
| 217 | /* |
drh | 9f8cf9d | 2011-01-17 21:32:24 +0000 | [diff] [blame] | 218 | ** Macros to enter and leave the PCache LRU mutex. |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 219 | */ |
drh | 982215a | 2015-06-13 11:10:55 +0000 | [diff] [blame] | 220 | #if !defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) || SQLITE_THREADSAFE==0 |
| 221 | # define pcache1EnterMutex(X) assert((X)->mutex==0) |
| 222 | # define pcache1LeaveMutex(X) assert((X)->mutex==0) |
| 223 | # define PCACHE1_MIGHT_USE_GROUP_MUTEX 0 |
| 224 | #else |
| 225 | # define pcache1EnterMutex(X) sqlite3_mutex_enter((X)->mutex) |
| 226 | # define pcache1LeaveMutex(X) sqlite3_mutex_leave((X)->mutex) |
| 227 | # define PCACHE1_MIGHT_USE_GROUP_MUTEX 1 |
| 228 | #endif |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 229 | |
| 230 | /******************************************************************************/ |
| 231 | /******** Page Allocation/SQLITE_CONFIG_PCACHE Related Functions **************/ |
| 232 | |
| 233 | /* |
| 234 | ** This function is called during initialization if a static buffer is |
| 235 | ** supplied to use for the page-cache by passing the SQLITE_CONFIG_PAGECACHE |
| 236 | ** verb to sqlite3_config(). Parameter pBuf points to an allocation large |
| 237 | ** enough to contain 'n' buffers of 'sz' bytes each. |
drh | 9f8cf9d | 2011-01-17 21:32:24 +0000 | [diff] [blame] | 238 | ** |
| 239 | ** This routine is called from sqlite3_initialize() and so it is guaranteed |
| 240 | ** to be serialized already. There is no need for further mutexing. |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 241 | */ |
| 242 | void sqlite3PCacheBufferSetup(void *pBuf, int sz, int n){ |
drh | f4622dc | 2009-05-22 11:10:24 +0000 | [diff] [blame] | 243 | if( pcache1.isInit ){ |
| 244 | PgFreeslot *p; |
drh | ee70a84 | 2015-07-06 18:54:52 +0000 | [diff] [blame] | 245 | if( pBuf==0 ) sz = n = 0; |
drh | f4622dc | 2009-05-22 11:10:24 +0000 | [diff] [blame] | 246 | sz = ROUNDDOWN8(sz); |
| 247 | pcache1.szSlot = sz; |
drh | 50d1b5f | 2010-08-27 12:21:06 +0000 | [diff] [blame] | 248 | pcache1.nSlot = pcache1.nFreeSlot = n; |
| 249 | pcache1.nReserve = n>90 ? 10 : (n/10 + 1); |
drh | f4622dc | 2009-05-22 11:10:24 +0000 | [diff] [blame] | 250 | pcache1.pStart = pBuf; |
| 251 | pcache1.pFree = 0; |
drh | 9f8cf9d | 2011-01-17 21:32:24 +0000 | [diff] [blame] | 252 | pcache1.bUnderPressure = 0; |
drh | f4622dc | 2009-05-22 11:10:24 +0000 | [diff] [blame] | 253 | while( n-- ){ |
| 254 | p = (PgFreeslot*)pBuf; |
| 255 | p->pNext = pcache1.pFree; |
| 256 | pcache1.pFree = p; |
| 257 | pBuf = (void*)&((char*)pBuf)[sz]; |
| 258 | } |
| 259 | pcache1.pEnd = pBuf; |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 260 | } |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | /* |
drh | 957026a | 2015-07-16 18:18:19 +0000 | [diff] [blame] | 264 | ** Try to initialize the pCache->pFree and pCache->pBulk fields. Return |
| 265 | ** true if pCache->pFree ends up containing one or more free pages. |
| 266 | */ |
| 267 | static int pcache1InitBulk(PCache1 *pCache){ |
drh | 939d4bc | 2015-07-16 18:37:53 +0000 | [diff] [blame] | 268 | i64 szBulk; |
drh | 957026a | 2015-07-16 18:18:19 +0000 | [diff] [blame] | 269 | char *zBulk; |
| 270 | if( pcache1.nInitPage==0 ) return 0; |
| 271 | /* Do not bother with a bulk allocation if the cache size very small */ |
| 272 | if( pCache->nMax<3 ) return 0; |
| 273 | sqlite3BeginBenignMalloc(); |
| 274 | if( pcache1.nInitPage>0 ){ |
drh | 939d4bc | 2015-07-16 18:37:53 +0000 | [diff] [blame] | 275 | szBulk = pCache->szAlloc * (i64)pcache1.nInitPage; |
drh | 957026a | 2015-07-16 18:18:19 +0000 | [diff] [blame] | 276 | }else{ |
drh | 939d4bc | 2015-07-16 18:37:53 +0000 | [diff] [blame] | 277 | szBulk = -1024 * (i64)pcache1.nInitPage; |
drh | 957026a | 2015-07-16 18:18:19 +0000 | [diff] [blame] | 278 | } |
drh | 939d4bc | 2015-07-16 18:37:53 +0000 | [diff] [blame] | 279 | if( szBulk > pCache->szAlloc*(i64)pCache->nMax ){ |
drh | 957026a | 2015-07-16 18:18:19 +0000 | [diff] [blame] | 280 | szBulk = pCache->szAlloc*pCache->nMax; |
| 281 | } |
| 282 | zBulk = pCache->pBulk = sqlite3Malloc( szBulk ); |
| 283 | sqlite3EndBenignMalloc(); |
| 284 | if( zBulk ){ |
| 285 | int nBulk = sqlite3MallocSize(zBulk)/pCache->szAlloc; |
| 286 | int i; |
| 287 | for(i=0; i<nBulk; i++){ |
| 288 | PgHdr1 *pX = (PgHdr1*)&zBulk[pCache->szPage]; |
| 289 | pX->page.pBuf = zBulk; |
| 290 | pX->page.pExtra = &pX[1]; |
| 291 | pX->isBulkLocal = 1; |
| 292 | pX->pNext = pCache->pFree; |
| 293 | pCache->pFree = pX; |
| 294 | zBulk += pCache->szAlloc; |
| 295 | } |
| 296 | } |
| 297 | return pCache->pFree!=0; |
| 298 | } |
| 299 | |
| 300 | /* |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 301 | ** Malloc function used within this file to allocate space from the buffer |
| 302 | ** configured using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no |
| 303 | ** such buffer exists or there is no space left in it, this function falls |
| 304 | ** back to sqlite3Malloc(). |
drh | 9f8cf9d | 2011-01-17 21:32:24 +0000 | [diff] [blame] | 305 | ** |
| 306 | ** Multiple threads can run this routine at the same time. Global variables |
| 307 | ** in pcache1 need to be protected via mutex. |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 308 | */ |
| 309 | static void *pcache1Alloc(int nByte){ |
drh | 9f8cf9d | 2011-01-17 21:32:24 +0000 | [diff] [blame] | 310 | void *p = 0; |
| 311 | assert( sqlite3_mutex_notheld(pcache1.grp.mutex) ); |
drh | 9f8cf9d | 2011-01-17 21:32:24 +0000 | [diff] [blame] | 312 | if( nByte<=pcache1.szSlot ){ |
| 313 | sqlite3_mutex_enter(pcache1.mutex); |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 314 | p = (PgHdr1 *)pcache1.pFree; |
drh | 9f8cf9d | 2011-01-17 21:32:24 +0000 | [diff] [blame] | 315 | if( p ){ |
| 316 | pcache1.pFree = pcache1.pFree->pNext; |
| 317 | pcache1.nFreeSlot--; |
| 318 | pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve; |
| 319 | assert( pcache1.nFreeSlot>=0 ); |
drh | af89fe6 | 2015-03-23 17:25:18 +0000 | [diff] [blame] | 320 | sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, nByte); |
| 321 | sqlite3StatusUp(SQLITE_STATUS_PAGECACHE_USED, 1); |
drh | 9f8cf9d | 2011-01-17 21:32:24 +0000 | [diff] [blame] | 322 | } |
| 323 | sqlite3_mutex_leave(pcache1.mutex); |
| 324 | } |
| 325 | if( p==0 ){ |
| 326 | /* Memory is not available in the SQLITE_CONFIG_PAGECACHE pool. Get |
| 327 | ** it from sqlite3Malloc instead. |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 328 | */ |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 329 | p = sqlite3Malloc(nByte); |
drh | 4bd6952 | 2012-06-07 02:35:29 +0000 | [diff] [blame] | 330 | #ifndef SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 331 | if( p ){ |
| 332 | int sz = sqlite3MallocSize(p); |
drh | 9bf3da8e | 2011-01-26 13:24:40 +0000 | [diff] [blame] | 333 | sqlite3_mutex_enter(pcache1.mutex); |
drh | af89fe6 | 2015-03-23 17:25:18 +0000 | [diff] [blame] | 334 | sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, nByte); |
| 335 | sqlite3StatusUp(SQLITE_STATUS_PAGECACHE_OVERFLOW, sz); |
drh | 9bf3da8e | 2011-01-26 13:24:40 +0000 | [diff] [blame] | 336 | sqlite3_mutex_leave(pcache1.mutex); |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 337 | } |
drh | 4bd6952 | 2012-06-07 02:35:29 +0000 | [diff] [blame] | 338 | #endif |
drh | 107b56e | 2010-03-12 16:32:53 +0000 | [diff] [blame] | 339 | sqlite3MemdebugSetType(p, MEMTYPE_PCACHE); |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 340 | } |
| 341 | return p; |
| 342 | } |
| 343 | |
| 344 | /* |
| 345 | ** Free an allocated buffer obtained from pcache1Alloc(). |
| 346 | */ |
drh | ee70a84 | 2015-07-06 18:54:52 +0000 | [diff] [blame] | 347 | static void pcache1Free(void *p){ |
drh | 09419b4 | 2011-11-16 19:29:17 +0000 | [diff] [blame] | 348 | int nFreed = 0; |
drh | ee70a84 | 2015-07-06 18:54:52 +0000 | [diff] [blame] | 349 | if( p==0 ) return; |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 350 | if( p>=pcache1.pStart && p<pcache1.pEnd ){ |
| 351 | PgFreeslot *pSlot; |
drh | 9f8cf9d | 2011-01-17 21:32:24 +0000 | [diff] [blame] | 352 | sqlite3_mutex_enter(pcache1.mutex); |
drh | af89fe6 | 2015-03-23 17:25:18 +0000 | [diff] [blame] | 353 | sqlite3StatusDown(SQLITE_STATUS_PAGECACHE_USED, 1); |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 354 | pSlot = (PgFreeslot*)p; |
| 355 | pSlot->pNext = pcache1.pFree; |
| 356 | pcache1.pFree = pSlot; |
drh | 50d1b5f | 2010-08-27 12:21:06 +0000 | [diff] [blame] | 357 | pcache1.nFreeSlot++; |
drh | 9f8cf9d | 2011-01-17 21:32:24 +0000 | [diff] [blame] | 358 | pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve; |
drh | 50d1b5f | 2010-08-27 12:21:06 +0000 | [diff] [blame] | 359 | assert( pcache1.nFreeSlot<=pcache1.nSlot ); |
drh | 9f8cf9d | 2011-01-17 21:32:24 +0000 | [diff] [blame] | 360 | sqlite3_mutex_leave(pcache1.mutex); |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 361 | }else{ |
drh | 107b56e | 2010-03-12 16:32:53 +0000 | [diff] [blame] | 362 | assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) ); |
| 363 | sqlite3MemdebugSetType(p, MEMTYPE_HEAP); |
drh | 4bd6952 | 2012-06-07 02:35:29 +0000 | [diff] [blame] | 364 | #ifndef SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS |
drh | ee70a84 | 2015-07-06 18:54:52 +0000 | [diff] [blame] | 365 | nFreed = sqlite3MallocSize(p); |
drh | 15ad92f | 2011-01-26 13:28:06 +0000 | [diff] [blame] | 366 | sqlite3_mutex_enter(pcache1.mutex); |
drh | af89fe6 | 2015-03-23 17:25:18 +0000 | [diff] [blame] | 367 | sqlite3StatusDown(SQLITE_STATUS_PAGECACHE_OVERFLOW, nFreed); |
drh | 15ad92f | 2011-01-26 13:28:06 +0000 | [diff] [blame] | 368 | sqlite3_mutex_leave(pcache1.mutex); |
drh | 4bd6952 | 2012-06-07 02:35:29 +0000 | [diff] [blame] | 369 | #endif |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 370 | sqlite3_free(p); |
| 371 | } |
| 372 | } |
| 373 | |
drh | c8f503a | 2010-08-20 09:14:13 +0000 | [diff] [blame] | 374 | #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT |
| 375 | /* |
drh | 9d13f11 | 2010-08-24 18:06:35 +0000 | [diff] [blame] | 376 | ** Return the size of a pcache allocation |
drh | c8f503a | 2010-08-20 09:14:13 +0000 | [diff] [blame] | 377 | */ |
| 378 | static int pcache1MemSize(void *p){ |
drh | c8f503a | 2010-08-20 09:14:13 +0000 | [diff] [blame] | 379 | if( p>=pcache1.pStart && p<pcache1.pEnd ){ |
| 380 | return pcache1.szSlot; |
| 381 | }else{ |
| 382 | int iSize; |
| 383 | assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) ); |
| 384 | sqlite3MemdebugSetType(p, MEMTYPE_HEAP); |
| 385 | iSize = sqlite3MallocSize(p); |
| 386 | sqlite3MemdebugSetType(p, MEMTYPE_PCACHE); |
| 387 | return iSize; |
| 388 | } |
| 389 | } |
| 390 | #endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */ |
| 391 | |
dan | d292570 | 2011-08-19 18:15:00 +0000 | [diff] [blame] | 392 | /* |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 393 | ** Allocate a new page object initially associated with cache pCache. |
| 394 | */ |
| 395 | static PgHdr1 *pcache1AllocPage(PCache1 *pCache){ |
dan | b5126dd | 2011-09-22 14:56:31 +0000 | [diff] [blame] | 396 | PgHdr1 *p = 0; |
| 397 | void *pPg; |
dan | d292570 | 2011-08-19 18:15:00 +0000 | [diff] [blame] | 398 | |
dan | d292570 | 2011-08-19 18:15:00 +0000 | [diff] [blame] | 399 | assert( sqlite3_mutex_held(pCache->pGroup->mutex) ); |
drh | 957026a | 2015-07-16 18:18:19 +0000 | [diff] [blame] | 400 | if( pCache->pFree || (pCache->nPage==0 && pcache1InitBulk(pCache)) ){ |
drh | ee70a84 | 2015-07-06 18:54:52 +0000 | [diff] [blame] | 401 | p = pCache->pFree; |
| 402 | pCache->pFree = p->pNext; |
| 403 | p->pNext = 0; |
| 404 | }else{ |
drh | db7ae89 | 2015-07-06 20:57:22 +0000 | [diff] [blame] | 405 | #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT |
drh | ee70a84 | 2015-07-06 18:54:52 +0000 | [diff] [blame] | 406 | /* The group mutex must be released before pcache1Alloc() is called. This |
drh | db7ae89 | 2015-07-06 20:57:22 +0000 | [diff] [blame] | 407 | ** is because it might call sqlite3_release_memory(), which assumes that |
drh | ee70a84 | 2015-07-06 18:54:52 +0000 | [diff] [blame] | 408 | ** this mutex is not held. */ |
drh | db7ae89 | 2015-07-06 20:57:22 +0000 | [diff] [blame] | 409 | assert( pcache1.separateCache==0 ); |
| 410 | assert( pCache->pGroup==&pcache1.grp ); |
drh | ee70a84 | 2015-07-06 18:54:52 +0000 | [diff] [blame] | 411 | pcache1LeaveMutex(pCache->pGroup); |
drh | db7ae89 | 2015-07-06 20:57:22 +0000 | [diff] [blame] | 412 | #endif |
dan | 22e21ff | 2011-11-08 20:08:44 +0000 | [diff] [blame] | 413 | #ifdef SQLITE_PCACHE_SEPARATE_HEADER |
drh | ee70a84 | 2015-07-06 18:54:52 +0000 | [diff] [blame] | 414 | pPg = pcache1Alloc(pCache->szPage); |
| 415 | p = sqlite3Malloc(sizeof(PgHdr1) + pCache->szExtra); |
| 416 | if( !pPg || !p ){ |
| 417 | pcache1Free(pPg); |
| 418 | sqlite3_free(p); |
| 419 | pPg = 0; |
| 420 | } |
dan | 22e21ff | 2011-11-08 20:08:44 +0000 | [diff] [blame] | 421 | #else |
drh | ee70a84 | 2015-07-06 18:54:52 +0000 | [diff] [blame] | 422 | pPg = pcache1Alloc(pCache->szAlloc); |
| 423 | p = (PgHdr1 *)&((u8 *)pPg)[pCache->szPage]; |
dan | 22e21ff | 2011-11-08 20:08:44 +0000 | [diff] [blame] | 424 | #endif |
drh | db7ae89 | 2015-07-06 20:57:22 +0000 | [diff] [blame] | 425 | #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT |
drh | ee70a84 | 2015-07-06 18:54:52 +0000 | [diff] [blame] | 426 | pcache1EnterMutex(pCache->pGroup); |
drh | db7ae89 | 2015-07-06 20:57:22 +0000 | [diff] [blame] | 427 | #endif |
drh | ee70a84 | 2015-07-06 18:54:52 +0000 | [diff] [blame] | 428 | if( pPg==0 ) return 0; |
dan | 22e21ff | 2011-11-08 20:08:44 +0000 | [diff] [blame] | 429 | p->page.pBuf = pPg; |
| 430 | p->page.pExtra = &p[1]; |
drh | ee70a84 | 2015-07-06 18:54:52 +0000 | [diff] [blame] | 431 | p->isBulkLocal = 0; |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 432 | } |
drh | ee70a84 | 2015-07-06 18:54:52 +0000 | [diff] [blame] | 433 | if( pCache->bPurgeable ){ |
| 434 | pCache->pGroup->nCurrentPage++; |
| 435 | } |
| 436 | return p; |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | /* |
| 440 | ** Free a page object allocated by pcache1AllocPage(). |
| 441 | */ |
| 442 | static void pcache1FreePage(PgHdr1 *p){ |
drh | db7ae89 | 2015-07-06 20:57:22 +0000 | [diff] [blame] | 443 | PCache1 *pCache; |
| 444 | assert( p!=0 ); |
| 445 | pCache = p->pCache; |
| 446 | assert( sqlite3_mutex_held(p->pCache->pGroup->mutex) ); |
| 447 | if( p->isBulkLocal ){ |
| 448 | p->pNext = pCache->pFree; |
| 449 | pCache->pFree = p; |
| 450 | }else{ |
| 451 | pcache1Free(p->page.pBuf); |
dan | 22e21ff | 2011-11-08 20:08:44 +0000 | [diff] [blame] | 452 | #ifdef SQLITE_PCACHE_SEPARATE_HEADER |
drh | db7ae89 | 2015-07-06 20:57:22 +0000 | [diff] [blame] | 453 | sqlite3_free(p); |
dan | 22e21ff | 2011-11-08 20:08:44 +0000 | [diff] [blame] | 454 | #endif |
drh | db7ae89 | 2015-07-06 20:57:22 +0000 | [diff] [blame] | 455 | } |
| 456 | if( pCache->bPurgeable ){ |
| 457 | pCache->pGroup->nCurrentPage--; |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 458 | } |
| 459 | } |
| 460 | |
| 461 | /* |
| 462 | ** Malloc function used by SQLite to obtain space from the buffer configured |
| 463 | ** using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no such buffer |
| 464 | ** exists, this function falls back to sqlite3Malloc(). |
| 465 | */ |
| 466 | void *sqlite3PageMalloc(int sz){ |
drh | 9f8cf9d | 2011-01-17 21:32:24 +0000 | [diff] [blame] | 467 | return pcache1Alloc(sz); |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | /* |
| 471 | ** Free an allocated buffer obtained from sqlite3PageMalloc(). |
| 472 | */ |
| 473 | void sqlite3PageFree(void *p){ |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 474 | pcache1Free(p); |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 475 | } |
| 476 | |
drh | 50d1b5f | 2010-08-27 12:21:06 +0000 | [diff] [blame] | 477 | |
| 478 | /* |
| 479 | ** Return true if it desirable to avoid allocating a new page cache |
| 480 | ** entry. |
| 481 | ** |
| 482 | ** If memory was allocated specifically to the page cache using |
| 483 | ** SQLITE_CONFIG_PAGECACHE but that memory has all been used, then |
| 484 | ** it is desirable to avoid allocating a new page cache entry because |
| 485 | ** presumably SQLITE_CONFIG_PAGECACHE was suppose to be sufficient |
| 486 | ** for all page cache needs and we should not need to spill the |
| 487 | ** allocation onto the heap. |
| 488 | ** |
drh | 45d2930 | 2012-01-08 22:18:33 +0000 | [diff] [blame] | 489 | ** 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] | 490 | ** under memory pressure, then again it is desirable to avoid |
| 491 | ** allocating a new page cache entry in order to avoid stressing |
| 492 | ** the heap even further. |
| 493 | */ |
| 494 | static int pcache1UnderMemoryPressure(PCache1 *pCache){ |
dan | 22e21ff | 2011-11-08 20:08:44 +0000 | [diff] [blame] | 495 | if( pcache1.nSlot && (pCache->szPage+pCache->szExtra)<=pcache1.szSlot ){ |
drh | 9f8cf9d | 2011-01-17 21:32:24 +0000 | [diff] [blame] | 496 | return pcache1.bUnderPressure; |
drh | 50d1b5f | 2010-08-27 12:21:06 +0000 | [diff] [blame] | 497 | }else{ |
| 498 | return sqlite3HeapNearlyFull(); |
| 499 | } |
| 500 | } |
| 501 | |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 502 | /******************************************************************************/ |
| 503 | /******** General Implementation Functions ************************************/ |
| 504 | |
| 505 | /* |
| 506 | ** This function is used to resize the hash table used by the cache passed |
| 507 | ** as the first argument. |
| 508 | ** |
drh | 9f8cf9d | 2011-01-17 21:32:24 +0000 | [diff] [blame] | 509 | ** The PCache mutex must be held when this function is called. |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 510 | */ |
drh | efbf044 | 2014-08-23 23:15:31 +0000 | [diff] [blame] | 511 | static void pcache1ResizeHash(PCache1 *p){ |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 512 | PgHdr1 **apNew; |
danielk1977 | 44cd45c | 2008-11-15 11:22:45 +0000 | [diff] [blame] | 513 | unsigned int nNew; |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 514 | unsigned int i; |
| 515 | |
drh | 9f8cf9d | 2011-01-17 21:32:24 +0000 | [diff] [blame] | 516 | assert( sqlite3_mutex_held(p->pGroup->mutex) ); |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 517 | |
| 518 | nNew = p->nHash*2; |
| 519 | if( nNew<256 ){ |
| 520 | nNew = 256; |
| 521 | } |
| 522 | |
drh | 9f8cf9d | 2011-01-17 21:32:24 +0000 | [diff] [blame] | 523 | pcache1LeaveMutex(p->pGroup); |
drh | 085bb7f | 2008-12-06 14:34:33 +0000 | [diff] [blame] | 524 | if( p->nHash ){ sqlite3BeginBenignMalloc(); } |
dan | 6809c96 | 2012-07-30 14:53:54 +0000 | [diff] [blame] | 525 | apNew = (PgHdr1 **)sqlite3MallocZero(sizeof(PgHdr1 *)*nNew); |
drh | 085bb7f | 2008-12-06 14:34:33 +0000 | [diff] [blame] | 526 | if( p->nHash ){ sqlite3EndBenignMalloc(); } |
drh | 9f8cf9d | 2011-01-17 21:32:24 +0000 | [diff] [blame] | 527 | pcache1EnterMutex(p->pGroup); |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 528 | if( apNew ){ |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 529 | for(i=0; i<p->nHash; i++){ |
| 530 | PgHdr1 *pPage; |
| 531 | PgHdr1 *pNext = p->apHash[i]; |
drh | b27b7f5 | 2008-12-10 18:03:45 +0000 | [diff] [blame] | 532 | while( (pPage = pNext)!=0 ){ |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 533 | unsigned int h = pPage->iKey % nNew; |
| 534 | pNext = pPage->pNext; |
| 535 | pPage->pNext = apNew[h]; |
| 536 | apNew[h] = pPage; |
| 537 | } |
| 538 | } |
| 539 | sqlite3_free(p->apHash); |
| 540 | p->apHash = apNew; |
| 541 | p->nHash = nNew; |
| 542 | } |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | /* |
| 546 | ** This function is used internally to remove the page pPage from the |
drh | 9f8cf9d | 2011-01-17 21:32:24 +0000 | [diff] [blame] | 547 | ** 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] | 548 | ** LRU list, then this function is a no-op. |
| 549 | ** |
drh | 9f8cf9d | 2011-01-17 21:32:24 +0000 | [diff] [blame] | 550 | ** The PGroup mutex must be held when this function is called. |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 551 | */ |
drh | 55a46c9 | 2015-06-12 13:49:26 +0000 | [diff] [blame] | 552 | static PgHdr1 *pcache1PinPage(PgHdr1 *pPage){ |
drh | 9f8cf9d | 2011-01-17 21:32:24 +0000 | [diff] [blame] | 553 | PCache1 *pCache; |
drh | 9f8cf9d | 2011-01-17 21:32:24 +0000 | [diff] [blame] | 554 | |
drh | 5d56dd2 | 2013-12-13 18:50:40 +0000 | [diff] [blame] | 555 | assert( pPage!=0 ); |
| 556 | assert( pPage->isPinned==0 ); |
drh | 9f8cf9d | 2011-01-17 21:32:24 +0000 | [diff] [blame] | 557 | pCache = pPage->pCache; |
drh | b230a52 | 2015-06-12 13:04:51 +0000 | [diff] [blame] | 558 | assert( pPage->pLruNext || pPage==pCache->pGroup->pLruTail ); |
| 559 | assert( pPage->pLruPrev || pPage==pCache->pGroup->pLruHead ); |
| 560 | assert( sqlite3_mutex_held(pCache->pGroup->mutex) ); |
drh | 5d56dd2 | 2013-12-13 18:50:40 +0000 | [diff] [blame] | 561 | if( pPage->pLruPrev ){ |
| 562 | pPage->pLruPrev->pLruNext = pPage->pLruNext; |
| 563 | }else{ |
drh | b230a52 | 2015-06-12 13:04:51 +0000 | [diff] [blame] | 564 | pCache->pGroup->pLruHead = pPage->pLruNext; |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 565 | } |
drh | 5d56dd2 | 2013-12-13 18:50:40 +0000 | [diff] [blame] | 566 | if( pPage->pLruNext ){ |
| 567 | pPage->pLruNext->pLruPrev = pPage->pLruPrev; |
| 568 | }else{ |
drh | b230a52 | 2015-06-12 13:04:51 +0000 | [diff] [blame] | 569 | pCache->pGroup->pLruTail = pPage->pLruPrev; |
drh | 5d56dd2 | 2013-12-13 18:50:40 +0000 | [diff] [blame] | 570 | } |
| 571 | pPage->pLruNext = 0; |
| 572 | pPage->pLruPrev = 0; |
| 573 | pPage->isPinned = 1; |
| 574 | pCache->nRecyclable--; |
drh | 55a46c9 | 2015-06-12 13:49:26 +0000 | [diff] [blame] | 575 | return pPage; |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | |
| 579 | /* |
| 580 | ** Remove the page supplied as an argument from the hash table |
| 581 | ** (PCache1.apHash structure) that it is currently stored in. |
drh | 95c91e1 | 2015-06-29 00:21:00 +0000 | [diff] [blame] | 582 | ** Also free the page if freePage is true. |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 583 | ** |
drh | 9f8cf9d | 2011-01-17 21:32:24 +0000 | [diff] [blame] | 584 | ** The PGroup mutex must be held when this function is called. |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 585 | */ |
drh | 95c91e1 | 2015-06-29 00:21:00 +0000 | [diff] [blame] | 586 | static void pcache1RemoveFromHash(PgHdr1 *pPage, int freeFlag){ |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 587 | unsigned int h; |
| 588 | PCache1 *pCache = pPage->pCache; |
| 589 | PgHdr1 **pp; |
| 590 | |
drh | 9f8cf9d | 2011-01-17 21:32:24 +0000 | [diff] [blame] | 591 | assert( sqlite3_mutex_held(pCache->pGroup->mutex) ); |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 592 | h = pPage->iKey % pCache->nHash; |
| 593 | for(pp=&pCache->apHash[h]; (*pp)!=pPage; pp=&(*pp)->pNext); |
| 594 | *pp = (*pp)->pNext; |
| 595 | |
| 596 | pCache->nPage--; |
drh | 95c91e1 | 2015-06-29 00:21:00 +0000 | [diff] [blame] | 597 | if( freeFlag ) pcache1FreePage(pPage); |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | /* |
drh | 9f8cf9d | 2011-01-17 21:32:24 +0000 | [diff] [blame] | 601 | ** If there are currently more than nMaxPage pages allocated, try |
| 602 | ** to recycle pages to reduce the number allocated to nMaxPage. |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 603 | */ |
drh | 957026a | 2015-07-16 18:18:19 +0000 | [diff] [blame] | 604 | static void pcache1EnforceMaxPage(PCache1 *pCache){ |
| 605 | PGroup *pGroup = pCache->pGroup; |
drh | 9f8cf9d | 2011-01-17 21:32:24 +0000 | [diff] [blame] | 606 | assert( sqlite3_mutex_held(pGroup->mutex) ); |
| 607 | while( pGroup->nCurrentPage>pGroup->nMaxPage && pGroup->pLruTail ){ |
| 608 | PgHdr1 *p = pGroup->pLruTail; |
| 609 | assert( p->pCache->pGroup==pGroup ); |
drh | 5d56dd2 | 2013-12-13 18:50:40 +0000 | [diff] [blame] | 610 | assert( p->isPinned==0 ); |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 611 | pcache1PinPage(p); |
drh | 95c91e1 | 2015-06-29 00:21:00 +0000 | [diff] [blame] | 612 | pcache1RemoveFromHash(p, 1); |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 613 | } |
drh | 957026a | 2015-07-16 18:18:19 +0000 | [diff] [blame] | 614 | if( pCache->nPage==0 && pCache->pBulk ){ |
| 615 | sqlite3_free(pCache->pBulk); |
| 616 | pCache->pBulk = pCache->pFree = 0; |
| 617 | } |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | /* |
| 621 | ** Discard all pages from cache pCache with a page number (key value) |
| 622 | ** greater than or equal to iLimit. Any pinned pages that meet this |
| 623 | ** criteria are unpinned before they are discarded. |
| 624 | ** |
drh | 9f8cf9d | 2011-01-17 21:32:24 +0000 | [diff] [blame] | 625 | ** The PCache mutex must be held when this function is called. |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 626 | */ |
| 627 | static void pcache1TruncateUnsafe( |
drh | 9f8cf9d | 2011-01-17 21:32:24 +0000 | [diff] [blame] | 628 | PCache1 *pCache, /* The cache to truncate */ |
| 629 | unsigned int iLimit /* Drop pages with this pgno or larger */ |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 630 | ){ |
drh | 9f8cf9d | 2011-01-17 21:32:24 +0000 | [diff] [blame] | 631 | TESTONLY( unsigned int nPage = 0; ) /* To assert pCache->nPage is correct */ |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 632 | unsigned int h; |
drh | 9f8cf9d | 2011-01-17 21:32:24 +0000 | [diff] [blame] | 633 | assert( sqlite3_mutex_held(pCache->pGroup->mutex) ); |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 634 | for(h=0; h<pCache->nHash; h++){ |
| 635 | PgHdr1 **pp = &pCache->apHash[h]; |
| 636 | PgHdr1 *pPage; |
drh | b27b7f5 | 2008-12-10 18:03:45 +0000 | [diff] [blame] | 637 | while( (pPage = *pp)!=0 ){ |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 638 | if( pPage->iKey>=iLimit ){ |
danielk1977 | ea24ac4 | 2009-05-08 06:52:47 +0000 | [diff] [blame] | 639 | pCache->nPage--; |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 640 | *pp = pPage->pNext; |
drh | 5d56dd2 | 2013-12-13 18:50:40 +0000 | [diff] [blame] | 641 | if( !pPage->isPinned ) pcache1PinPage(pPage); |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 642 | pcache1FreePage(pPage); |
| 643 | }else{ |
| 644 | pp = &pPage->pNext; |
danielk1977 | ea24ac4 | 2009-05-08 06:52:47 +0000 | [diff] [blame] | 645 | TESTONLY( nPage++; ) |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 646 | } |
| 647 | } |
| 648 | } |
danielk1977 | ea24ac4 | 2009-05-08 06:52:47 +0000 | [diff] [blame] | 649 | assert( pCache->nPage==nPage ); |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | /******************************************************************************/ |
| 653 | /******** sqlite3_pcache Methods **********************************************/ |
| 654 | |
| 655 | /* |
| 656 | ** Implementation of the sqlite3_pcache.xInit method. |
| 657 | */ |
danielk1977 | 62c14b3 | 2008-11-19 09:05:26 +0000 | [diff] [blame] | 658 | static int pcache1Init(void *NotUsed){ |
| 659 | UNUSED_PARAMETER(NotUsed); |
drh | f4622dc | 2009-05-22 11:10:24 +0000 | [diff] [blame] | 660 | assert( pcache1.isInit==0 ); |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 661 | memset(&pcache1, 0, sizeof(pcache1)); |
drh | db7ae89 | 2015-07-06 20:57:22 +0000 | [diff] [blame] | 662 | |
| 663 | |
| 664 | /* |
| 665 | ** The pcache1.separateCache variable is true if each PCache has its own |
| 666 | ** private PGroup (mode-1). pcache1.separateCache is false if the single |
| 667 | ** PGroup in pcache1.grp is used for all page caches (mode-2). |
| 668 | ** |
| 669 | ** * Always use a unified cache (mode-2) if ENABLE_MEMORY_MANAGEMENT |
| 670 | ** |
| 671 | ** * Use a unified cache in single-threaded applications that have |
| 672 | ** configured a start-time buffer for use as page-cache memory using |
| 673 | ** sqlite3_config(SQLITE_CONFIG_PAGECACHE, pBuf, sz, N) with non-NULL |
| 674 | ** pBuf argument. |
| 675 | ** |
| 676 | ** * Otherwise use separate caches (mode-1) |
| 677 | */ |
| 678 | #if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) |
| 679 | pcache1.separateCache = 0; |
drh | fd5ae96 | 2015-07-07 15:14:16 +0000 | [diff] [blame] | 680 | #elif SQLITE_THREADSAFE |
drh | db7ae89 | 2015-07-06 20:57:22 +0000 | [diff] [blame] | 681 | pcache1.separateCache = sqlite3GlobalConfig.pPage==0 |
| 682 | || sqlite3GlobalConfig.bCoreMutex>0; |
drh | fd5ae96 | 2015-07-07 15:14:16 +0000 | [diff] [blame] | 683 | #else |
| 684 | pcache1.separateCache = sqlite3GlobalConfig.pPage==0; |
drh | db7ae89 | 2015-07-06 20:57:22 +0000 | [diff] [blame] | 685 | #endif |
| 686 | |
drh | 982215a | 2015-06-13 11:10:55 +0000 | [diff] [blame] | 687 | #if SQLITE_THREADSAFE |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 688 | if( sqlite3GlobalConfig.bCoreMutex ){ |
drh | 9f8cf9d | 2011-01-17 21:32:24 +0000 | [diff] [blame] | 689 | pcache1.grp.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU); |
drh | 40f9837 | 2011-01-18 15:17:57 +0000 | [diff] [blame] | 690 | pcache1.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_PMEM); |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 691 | } |
drh | 982215a | 2015-06-13 11:10:55 +0000 | [diff] [blame] | 692 | #endif |
drh | 957026a | 2015-07-16 18:18:19 +0000 | [diff] [blame] | 693 | if( pcache1.separateCache |
| 694 | && sqlite3GlobalConfig.nPage!=0 |
| 695 | && sqlite3GlobalConfig.pPage==0 |
| 696 | ){ |
| 697 | pcache1.nInitPage = sqlite3GlobalConfig.nPage; |
| 698 | }else{ |
| 699 | pcache1.nInitPage = 0; |
| 700 | } |
drh | 41692e9 | 2011-01-25 04:34:51 +0000 | [diff] [blame] | 701 | pcache1.grp.mxPinned = 10; |
drh | f4622dc | 2009-05-22 11:10:24 +0000 | [diff] [blame] | 702 | pcache1.isInit = 1; |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 703 | return SQLITE_OK; |
| 704 | } |
| 705 | |
| 706 | /* |
| 707 | ** Implementation of the sqlite3_pcache.xShutdown method. |
shane | 7c7c311 | 2009-08-17 15:31:23 +0000 | [diff] [blame] | 708 | ** Note that the static mutex allocated in xInit does |
| 709 | ** not need to be freed. |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 710 | */ |
danielk1977 | 62c14b3 | 2008-11-19 09:05:26 +0000 | [diff] [blame] | 711 | static void pcache1Shutdown(void *NotUsed){ |
| 712 | UNUSED_PARAMETER(NotUsed); |
drh | f4622dc | 2009-05-22 11:10:24 +0000 | [diff] [blame] | 713 | assert( pcache1.isInit!=0 ); |
drh | b093719 | 2009-05-22 10:53:29 +0000 | [diff] [blame] | 714 | memset(&pcache1, 0, sizeof(pcache1)); |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 715 | } |
| 716 | |
drh | efbf044 | 2014-08-23 23:15:31 +0000 | [diff] [blame] | 717 | /* forward declaration */ |
| 718 | static void pcache1Destroy(sqlite3_pcache *p); |
| 719 | |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 720 | /* |
| 721 | ** Implementation of the sqlite3_pcache.xCreate method. |
| 722 | ** |
| 723 | ** Allocate a new cache. |
| 724 | */ |
drh | e5c40b1 | 2011-11-09 00:06:05 +0000 | [diff] [blame] | 725 | static sqlite3_pcache *pcache1Create(int szPage, int szExtra, int bPurgeable){ |
drh | 9f8cf9d | 2011-01-17 21:32:24 +0000 | [diff] [blame] | 726 | PCache1 *pCache; /* The newly created page cache */ |
| 727 | PGroup *pGroup; /* The group the new page cache will belong to */ |
| 728 | int sz; /* Bytes of memory required to allocate the new cache */ |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 729 | |
drh | e73c914 | 2011-11-09 16:12:24 +0000 | [diff] [blame] | 730 | assert( (szPage & (szPage-1))==0 && szPage>=512 && szPage<=65536 ); |
| 731 | assert( szExtra < 300 ); |
| 732 | |
drh | db7ae89 | 2015-07-06 20:57:22 +0000 | [diff] [blame] | 733 | sz = sizeof(PCache1) + sizeof(PGroup)*pcache1.separateCache; |
dan | 6809c96 | 2012-07-30 14:53:54 +0000 | [diff] [blame] | 734 | pCache = (PCache1 *)sqlite3MallocZero(sz); |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 735 | if( pCache ){ |
drh | db7ae89 | 2015-07-06 20:57:22 +0000 | [diff] [blame] | 736 | if( pcache1.separateCache ){ |
drh | 9f8cf9d | 2011-01-17 21:32:24 +0000 | [diff] [blame] | 737 | pGroup = (PGroup*)&pCache[1]; |
drh | 41692e9 | 2011-01-25 04:34:51 +0000 | [diff] [blame] | 738 | pGroup->mxPinned = 10; |
drh | 9f8cf9d | 2011-01-17 21:32:24 +0000 | [diff] [blame] | 739 | }else{ |
dan | 9dde7cb | 2011-06-09 17:53:43 +0000 | [diff] [blame] | 740 | pGroup = &pcache1.grp; |
drh | 9f8cf9d | 2011-01-17 21:32:24 +0000 | [diff] [blame] | 741 | } |
| 742 | pCache->pGroup = pGroup; |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 743 | pCache->szPage = szPage; |
dan | 22e21ff | 2011-11-08 20:08:44 +0000 | [diff] [blame] | 744 | pCache->szExtra = szExtra; |
drh | ee70a84 | 2015-07-06 18:54:52 +0000 | [diff] [blame] | 745 | pCache->szAlloc = szPage + szExtra + ROUND8(sizeof(PgHdr1)); |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 746 | pCache->bPurgeable = (bPurgeable ? 1 : 0); |
drh | efbf044 | 2014-08-23 23:15:31 +0000 | [diff] [blame] | 747 | pcache1EnterMutex(pGroup); |
| 748 | pcache1ResizeHash(pCache); |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 749 | if( bPurgeable ){ |
| 750 | pCache->nMin = 10; |
drh | 9f8cf9d | 2011-01-17 21:32:24 +0000 | [diff] [blame] | 751 | pGroup->nMinPage += pCache->nMin; |
drh | 41692e9 | 2011-01-25 04:34:51 +0000 | [diff] [blame] | 752 | pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage; |
drh | efbf044 | 2014-08-23 23:15:31 +0000 | [diff] [blame] | 753 | } |
| 754 | pcache1LeaveMutex(pGroup); |
| 755 | if( pCache->nHash==0 ){ |
| 756 | pcache1Destroy((sqlite3_pcache*)pCache); |
| 757 | pCache = 0; |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 758 | } |
| 759 | } |
| 760 | return (sqlite3_pcache *)pCache; |
| 761 | } |
| 762 | |
| 763 | /* |
| 764 | ** Implementation of the sqlite3_pcache.xCachesize method. |
| 765 | ** |
| 766 | ** Configure the cache_size limit for a cache. |
| 767 | */ |
| 768 | static void pcache1Cachesize(sqlite3_pcache *p, int nMax){ |
| 769 | PCache1 *pCache = (PCache1 *)p; |
| 770 | if( pCache->bPurgeable ){ |
drh | 9f8cf9d | 2011-01-17 21:32:24 +0000 | [diff] [blame] | 771 | PGroup *pGroup = pCache->pGroup; |
| 772 | pcache1EnterMutex(pGroup); |
| 773 | pGroup->nMaxPage += (nMax - pCache->nMax); |
drh | 41692e9 | 2011-01-25 04:34:51 +0000 | [diff] [blame] | 774 | pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage; |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 775 | pCache->nMax = nMax; |
drh | 25ca568 | 2011-01-26 00:07:03 +0000 | [diff] [blame] | 776 | pCache->n90pct = pCache->nMax*9/10; |
drh | 957026a | 2015-07-16 18:18:19 +0000 | [diff] [blame] | 777 | pcache1EnforceMaxPage(pCache); |
drh | 9f8cf9d | 2011-01-17 21:32:24 +0000 | [diff] [blame] | 778 | pcache1LeaveMutex(pGroup); |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 779 | } |
| 780 | } |
| 781 | |
| 782 | /* |
drh | 09419b4 | 2011-11-16 19:29:17 +0000 | [diff] [blame] | 783 | ** Implementation of the sqlite3_pcache.xShrink method. |
| 784 | ** |
| 785 | ** Free up as much memory as possible. |
| 786 | */ |
| 787 | static void pcache1Shrink(sqlite3_pcache *p){ |
| 788 | PCache1 *pCache = (PCache1*)p; |
| 789 | if( pCache->bPurgeable ){ |
| 790 | PGroup *pGroup = pCache->pGroup; |
| 791 | int savedMaxPage; |
| 792 | pcache1EnterMutex(pGroup); |
| 793 | savedMaxPage = pGroup->nMaxPage; |
| 794 | pGroup->nMaxPage = 0; |
drh | 957026a | 2015-07-16 18:18:19 +0000 | [diff] [blame] | 795 | pcache1EnforceMaxPage(pCache); |
drh | 09419b4 | 2011-11-16 19:29:17 +0000 | [diff] [blame] | 796 | pGroup->nMaxPage = savedMaxPage; |
| 797 | pcache1LeaveMutex(pGroup); |
| 798 | } |
| 799 | } |
| 800 | |
| 801 | /* |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 802 | ** Implementation of the sqlite3_pcache.xPagecount method. |
| 803 | */ |
| 804 | static int pcache1Pagecount(sqlite3_pcache *p){ |
| 805 | int n; |
drh | 9f8cf9d | 2011-01-17 21:32:24 +0000 | [diff] [blame] | 806 | PCache1 *pCache = (PCache1*)p; |
| 807 | pcache1EnterMutex(pCache->pGroup); |
| 808 | n = pCache->nPage; |
| 809 | pcache1LeaveMutex(pCache->pGroup); |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 810 | return n; |
| 811 | } |
| 812 | |
drh | efbf044 | 2014-08-23 23:15:31 +0000 | [diff] [blame] | 813 | |
| 814 | /* |
| 815 | ** Implement steps 3, 4, and 5 of the pcache1Fetch() algorithm described |
| 816 | ** in the header of the pcache1Fetch() procedure. |
| 817 | ** |
| 818 | ** This steps are broken out into a separate procedure because they are |
| 819 | ** usually not needed, and by avoiding the stack initialization required |
| 820 | ** for these steps, the main pcache1Fetch() procedure can run faster. |
| 821 | */ |
| 822 | static SQLITE_NOINLINE PgHdr1 *pcache1FetchStage2( |
| 823 | PCache1 *pCache, |
| 824 | unsigned int iKey, |
| 825 | int createFlag |
| 826 | ){ |
| 827 | unsigned int nPinned; |
| 828 | PGroup *pGroup = pCache->pGroup; |
| 829 | PgHdr1 *pPage = 0; |
| 830 | |
| 831 | /* Step 3: Abort if createFlag is 1 but the cache is nearly full */ |
| 832 | assert( pCache->nPage >= pCache->nRecyclable ); |
| 833 | nPinned = pCache->nPage - pCache->nRecyclable; |
| 834 | assert( pGroup->mxPinned == pGroup->nMaxPage + 10 - pGroup->nMinPage ); |
| 835 | assert( pCache->n90pct == pCache->nMax*9/10 ); |
| 836 | if( createFlag==1 && ( |
| 837 | nPinned>=pGroup->mxPinned |
| 838 | || nPinned>=pCache->n90pct |
dan | 5bd8af7 | 2014-10-10 19:10:59 +0000 | [diff] [blame] | 839 | || (pcache1UnderMemoryPressure(pCache) && pCache->nRecyclable<nPinned) |
drh | efbf044 | 2014-08-23 23:15:31 +0000 | [diff] [blame] | 840 | )){ |
| 841 | return 0; |
| 842 | } |
| 843 | |
| 844 | if( pCache->nPage>=pCache->nHash ) pcache1ResizeHash(pCache); |
| 845 | assert( pCache->nHash>0 && pCache->apHash ); |
| 846 | |
| 847 | /* Step 4. Try to recycle a page. */ |
drh | c54357c | 2015-07-07 14:06:18 +0000 | [diff] [blame] | 848 | if( pCache->bPurgeable |
| 849 | && pGroup->pLruTail |
| 850 | && ((pCache->nPage+1>=pCache->nMax) || pcache1UnderMemoryPressure(pCache)) |
| 851 | ){ |
drh | efbf044 | 2014-08-23 23:15:31 +0000 | [diff] [blame] | 852 | PCache1 *pOther; |
| 853 | pPage = pGroup->pLruTail; |
| 854 | assert( pPage->isPinned==0 ); |
drh | 95c91e1 | 2015-06-29 00:21:00 +0000 | [diff] [blame] | 855 | pcache1RemoveFromHash(pPage, 0); |
drh | efbf044 | 2014-08-23 23:15:31 +0000 | [diff] [blame] | 856 | pcache1PinPage(pPage); |
| 857 | pOther = pPage->pCache; |
drh | ee70a84 | 2015-07-06 18:54:52 +0000 | [diff] [blame] | 858 | if( pOther->szAlloc != pCache->szAlloc ){ |
drh | efbf044 | 2014-08-23 23:15:31 +0000 | [diff] [blame] | 859 | pcache1FreePage(pPage); |
| 860 | pPage = 0; |
| 861 | }else{ |
| 862 | pGroup->nCurrentPage -= (pOther->bPurgeable - pCache->bPurgeable); |
| 863 | } |
| 864 | } |
| 865 | |
| 866 | /* Step 5. If a usable page buffer has still not been found, |
| 867 | ** attempt to allocate a new one. |
| 868 | */ |
| 869 | if( !pPage ){ |
drh | f5ed7ad | 2015-06-15 14:43:25 +0000 | [diff] [blame] | 870 | if( createFlag==1 ){ sqlite3BeginBenignMalloc(); } |
drh | efbf044 | 2014-08-23 23:15:31 +0000 | [diff] [blame] | 871 | pPage = pcache1AllocPage(pCache); |
drh | f5ed7ad | 2015-06-15 14:43:25 +0000 | [diff] [blame] | 872 | if( createFlag==1 ){ sqlite3EndBenignMalloc(); } |
drh | efbf044 | 2014-08-23 23:15:31 +0000 | [diff] [blame] | 873 | } |
| 874 | |
| 875 | if( pPage ){ |
| 876 | unsigned int h = iKey % pCache->nHash; |
| 877 | pCache->nPage++; |
| 878 | pPage->iKey = iKey; |
| 879 | pPage->pNext = pCache->apHash[h]; |
| 880 | pPage->pCache = pCache; |
| 881 | pPage->pLruPrev = 0; |
| 882 | pPage->pLruNext = 0; |
| 883 | pPage->isPinned = 1; |
| 884 | *(void **)pPage->page.pExtra = 0; |
| 885 | pCache->apHash[h] = pPage; |
| 886 | if( iKey>pCache->iMaxKey ){ |
| 887 | pCache->iMaxKey = iKey; |
| 888 | } |
| 889 | } |
| 890 | return pPage; |
| 891 | } |
| 892 | |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 893 | /* |
| 894 | ** Implementation of the sqlite3_pcache.xFetch method. |
| 895 | ** |
| 896 | ** Fetch a page by key value. |
| 897 | ** |
| 898 | ** 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] | 899 | ** the value of the createFlag argument. 0 means do not allocate a new |
| 900 | ** page. 1 means allocate a new page if space is easily available. 2 |
| 901 | ** means to try really hard to allocate a new page. |
| 902 | ** |
| 903 | ** For a non-purgeable cache (a cache used as the storage for an in-memory |
| 904 | ** database) there is really no difference between createFlag 1 and 2. So |
| 905 | ** the calling function (pcache.c) will never have a createFlag of 1 on |
drh | 45d2930 | 2012-01-08 22:18:33 +0000 | [diff] [blame] | 906 | ** a non-purgeable cache. |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 907 | ** |
| 908 | ** There are three different approaches to obtaining space for a page, |
| 909 | ** depending on the value of parameter createFlag (which may be 0, 1 or 2). |
| 910 | ** |
| 911 | ** 1. Regardless of the value of createFlag, the cache is searched for a |
| 912 | ** copy of the requested page. If one is found, it is returned. |
| 913 | ** |
| 914 | ** 2. If createFlag==0 and the page is not already in the cache, NULL is |
| 915 | ** returned. |
| 916 | ** |
drh | 50d1b5f | 2010-08-27 12:21:06 +0000 | [diff] [blame] | 917 | ** 3. If createFlag is 1, and the page is not already in the cache, then |
| 918 | ** return NULL (do not allocate a new page) if any of the following |
| 919 | ** conditions are true: |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 920 | ** |
| 921 | ** (a) the number of pages pinned by the cache is greater than |
| 922 | ** PCache1.nMax, or |
drh | 50d1b5f | 2010-08-27 12:21:06 +0000 | [diff] [blame] | 923 | ** |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 924 | ** (b) the number of pages pinned by the cache is greater than |
| 925 | ** the sum of nMax for all purgeable caches, less the sum of |
drh | 50d1b5f | 2010-08-27 12:21:06 +0000 | [diff] [blame] | 926 | ** nMin for all other purgeable caches, or |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 927 | ** |
| 928 | ** 4. If none of the first three conditions apply and the cache is marked |
| 929 | ** as purgeable, and if one of the following is true: |
| 930 | ** |
| 931 | ** (a) The number of pages allocated for the cache is already |
| 932 | ** PCache1.nMax, or |
| 933 | ** |
| 934 | ** (b) The number of pages allocated for all purgeable caches is |
| 935 | ** already equal to or greater than the sum of nMax for all |
| 936 | ** purgeable caches, |
| 937 | ** |
drh | 50d1b5f | 2010-08-27 12:21:06 +0000 | [diff] [blame] | 938 | ** (c) The system is under memory pressure and wants to avoid |
| 939 | ** unnecessary pages cache entry allocations |
| 940 | ** |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 941 | ** then attempt to recycle a page from the LRU list. If it is the right |
| 942 | ** size, return the recycled buffer. Otherwise, free the buffer and |
| 943 | ** proceed to step 5. |
| 944 | ** |
| 945 | ** 5. Otherwise, allocate and return a new page buffer. |
drh | 55a46c9 | 2015-06-12 13:49:26 +0000 | [diff] [blame] | 946 | ** |
| 947 | ** There are two versions of this routine. pcache1FetchWithMutex() is |
| 948 | ** the general case. pcache1FetchNoMutex() is a faster implementation for |
| 949 | ** the common case where pGroup->mutex is NULL. The pcache1Fetch() wrapper |
| 950 | ** invokes the appropriate routine. |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 951 | */ |
drh | 55a46c9 | 2015-06-12 13:49:26 +0000 | [diff] [blame] | 952 | static PgHdr1 *pcache1FetchNoMutex( |
dan | 22e21ff | 2011-11-08 20:08:44 +0000 | [diff] [blame] | 953 | sqlite3_pcache *p, |
| 954 | unsigned int iKey, |
| 955 | int createFlag |
| 956 | ){ |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 957 | PCache1 *pCache = (PCache1 *)p; |
| 958 | PgHdr1 *pPage = 0; |
| 959 | |
drh | 3a5676c | 2011-01-19 21:58:56 +0000 | [diff] [blame] | 960 | /* Step 1: Search the hash table for an existing entry. */ |
drh | efbf044 | 2014-08-23 23:15:31 +0000 | [diff] [blame] | 961 | pPage = pCache->apHash[iKey % pCache->nHash]; |
| 962 | while( pPage && pPage->iKey!=iKey ){ pPage = pPage->pNext; } |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 963 | |
drh | 3a5676c | 2011-01-19 21:58:56 +0000 | [diff] [blame] | 964 | /* Step 2: Abort if no existing page is found and createFlag is 0 */ |
drh | 5d56dd2 | 2013-12-13 18:50:40 +0000 | [diff] [blame] | 965 | if( pPage ){ |
drh | 55a46c9 | 2015-06-12 13:49:26 +0000 | [diff] [blame] | 966 | if( !pPage->isPinned ){ |
| 967 | return pcache1PinPage(pPage); |
| 968 | }else{ |
| 969 | return pPage; |
| 970 | } |
drh | efbf044 | 2014-08-23 23:15:31 +0000 | [diff] [blame] | 971 | }else if( createFlag ){ |
| 972 | /* Steps 3, 4, and 5 implemented by this subroutine */ |
drh | 55a46c9 | 2015-06-12 13:49:26 +0000 | [diff] [blame] | 973 | return pcache1FetchStage2(pCache, iKey, createFlag); |
| 974 | }else{ |
| 975 | return 0; |
drh | 5d56dd2 | 2013-12-13 18:50:40 +0000 | [diff] [blame] | 976 | } |
drh | 55a46c9 | 2015-06-12 13:49:26 +0000 | [diff] [blame] | 977 | } |
drh | 982215a | 2015-06-13 11:10:55 +0000 | [diff] [blame] | 978 | #if PCACHE1_MIGHT_USE_GROUP_MUTEX |
drh | 55a46c9 | 2015-06-12 13:49:26 +0000 | [diff] [blame] | 979 | static PgHdr1 *pcache1FetchWithMutex( |
| 980 | sqlite3_pcache *p, |
| 981 | unsigned int iKey, |
| 982 | int createFlag |
| 983 | ){ |
| 984 | PCache1 *pCache = (PCache1 *)p; |
| 985 | PgHdr1 *pPage; |
| 986 | |
| 987 | pcache1EnterMutex(pCache->pGroup); |
| 988 | pPage = pcache1FetchNoMutex(p, iKey, createFlag); |
drh | efbf044 | 2014-08-23 23:15:31 +0000 | [diff] [blame] | 989 | assert( pPage==0 || pCache->iMaxKey>=iKey ); |
| 990 | pcache1LeaveMutex(pCache->pGroup); |
drh | 55a46c9 | 2015-06-12 13:49:26 +0000 | [diff] [blame] | 991 | return pPage; |
| 992 | } |
drh | 982215a | 2015-06-13 11:10:55 +0000 | [diff] [blame] | 993 | #endif |
drh | 55a46c9 | 2015-06-12 13:49:26 +0000 | [diff] [blame] | 994 | static sqlite3_pcache_page *pcache1Fetch( |
| 995 | sqlite3_pcache *p, |
| 996 | unsigned int iKey, |
| 997 | int createFlag |
| 998 | ){ |
drh | 982215a | 2015-06-13 11:10:55 +0000 | [diff] [blame] | 999 | #if PCACHE1_MIGHT_USE_GROUP_MUTEX || defined(SQLITE_DEBUG) |
drh | 55a46c9 | 2015-06-12 13:49:26 +0000 | [diff] [blame] | 1000 | PCache1 *pCache = (PCache1 *)p; |
drh | 982215a | 2015-06-13 11:10:55 +0000 | [diff] [blame] | 1001 | #endif |
drh | 55a46c9 | 2015-06-12 13:49:26 +0000 | [diff] [blame] | 1002 | |
| 1003 | assert( offsetof(PgHdr1,page)==0 ); |
| 1004 | assert( pCache->bPurgeable || createFlag!=1 ); |
| 1005 | assert( pCache->bPurgeable || pCache->nMin==0 ); |
| 1006 | assert( pCache->bPurgeable==0 || pCache->nMin==10 ); |
| 1007 | assert( pCache->nMin==0 || pCache->bPurgeable ); |
| 1008 | assert( pCache->nHash>0 ); |
drh | 982215a | 2015-06-13 11:10:55 +0000 | [diff] [blame] | 1009 | #if PCACHE1_MIGHT_USE_GROUP_MUTEX |
drh | 55a46c9 | 2015-06-12 13:49:26 +0000 | [diff] [blame] | 1010 | if( pCache->pGroup->mutex ){ |
| 1011 | return (sqlite3_pcache_page*)pcache1FetchWithMutex(p, iKey, createFlag); |
drh | 982215a | 2015-06-13 11:10:55 +0000 | [diff] [blame] | 1012 | }else |
| 1013 | #endif |
| 1014 | { |
drh | 55a46c9 | 2015-06-12 13:49:26 +0000 | [diff] [blame] | 1015 | return (sqlite3_pcache_page*)pcache1FetchNoMutex(p, iKey, createFlag); |
| 1016 | } |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 1017 | } |
| 1018 | |
| 1019 | |
| 1020 | /* |
| 1021 | ** Implementation of the sqlite3_pcache.xUnpin method. |
| 1022 | ** |
| 1023 | ** Mark a page as unpinned (eligible for asynchronous recycling). |
| 1024 | */ |
dan | 22e21ff | 2011-11-08 20:08:44 +0000 | [diff] [blame] | 1025 | static void pcache1Unpin( |
| 1026 | sqlite3_pcache *p, |
| 1027 | sqlite3_pcache_page *pPg, |
| 1028 | int reuseUnlikely |
| 1029 | ){ |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 1030 | PCache1 *pCache = (PCache1 *)p; |
dan | 22e21ff | 2011-11-08 20:08:44 +0000 | [diff] [blame] | 1031 | PgHdr1 *pPage = (PgHdr1 *)pPg; |
drh | 9f8cf9d | 2011-01-17 21:32:24 +0000 | [diff] [blame] | 1032 | PGroup *pGroup = pCache->pGroup; |
drh | 69e931e | 2009-06-03 21:04:35 +0000 | [diff] [blame] | 1033 | |
| 1034 | assert( pPage->pCache==pCache ); |
drh | 9f8cf9d | 2011-01-17 21:32:24 +0000 | [diff] [blame] | 1035 | pcache1EnterMutex(pGroup); |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 1036 | |
| 1037 | /* It is an error to call this function if the page is already |
drh | 9f8cf9d | 2011-01-17 21:32:24 +0000 | [diff] [blame] | 1038 | ** part of the PGroup LRU list. |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 1039 | */ |
| 1040 | assert( pPage->pLruPrev==0 && pPage->pLruNext==0 ); |
drh | 9f8cf9d | 2011-01-17 21:32:24 +0000 | [diff] [blame] | 1041 | assert( pGroup->pLruHead!=pPage && pGroup->pLruTail!=pPage ); |
drh | 5d56dd2 | 2013-12-13 18:50:40 +0000 | [diff] [blame] | 1042 | assert( pPage->isPinned==1 ); |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 1043 | |
drh | 9f8cf9d | 2011-01-17 21:32:24 +0000 | [diff] [blame] | 1044 | if( reuseUnlikely || pGroup->nCurrentPage>pGroup->nMaxPage ){ |
drh | 95c91e1 | 2015-06-29 00:21:00 +0000 | [diff] [blame] | 1045 | pcache1RemoveFromHash(pPage, 1); |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 1046 | }else{ |
drh | 9f8cf9d | 2011-01-17 21:32:24 +0000 | [diff] [blame] | 1047 | /* Add the page to the PGroup LRU list. */ |
| 1048 | if( pGroup->pLruHead ){ |
| 1049 | pGroup->pLruHead->pLruPrev = pPage; |
| 1050 | pPage->pLruNext = pGroup->pLruHead; |
| 1051 | pGroup->pLruHead = pPage; |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 1052 | }else{ |
drh | 9f8cf9d | 2011-01-17 21:32:24 +0000 | [diff] [blame] | 1053 | pGroup->pLruTail = pPage; |
| 1054 | pGroup->pLruHead = pPage; |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 1055 | } |
| 1056 | pCache->nRecyclable++; |
drh | 5d56dd2 | 2013-12-13 18:50:40 +0000 | [diff] [blame] | 1057 | pPage->isPinned = 0; |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 1058 | } |
| 1059 | |
drh | 9f8cf9d | 2011-01-17 21:32:24 +0000 | [diff] [blame] | 1060 | pcache1LeaveMutex(pCache->pGroup); |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 1061 | } |
| 1062 | |
| 1063 | /* |
| 1064 | ** Implementation of the sqlite3_pcache.xRekey method. |
| 1065 | */ |
| 1066 | static void pcache1Rekey( |
| 1067 | sqlite3_pcache *p, |
dan | 22e21ff | 2011-11-08 20:08:44 +0000 | [diff] [blame] | 1068 | sqlite3_pcache_page *pPg, |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 1069 | unsigned int iOld, |
| 1070 | unsigned int iNew |
| 1071 | ){ |
| 1072 | PCache1 *pCache = (PCache1 *)p; |
dan | 22e21ff | 2011-11-08 20:08:44 +0000 | [diff] [blame] | 1073 | PgHdr1 *pPage = (PgHdr1 *)pPg; |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 1074 | PgHdr1 **pp; |
| 1075 | unsigned int h; |
| 1076 | assert( pPage->iKey==iOld ); |
drh | 69e931e | 2009-06-03 21:04:35 +0000 | [diff] [blame] | 1077 | assert( pPage->pCache==pCache ); |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 1078 | |
drh | 9f8cf9d | 2011-01-17 21:32:24 +0000 | [diff] [blame] | 1079 | pcache1EnterMutex(pCache->pGroup); |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 1080 | |
| 1081 | h = iOld%pCache->nHash; |
| 1082 | pp = &pCache->apHash[h]; |
| 1083 | while( (*pp)!=pPage ){ |
| 1084 | pp = &(*pp)->pNext; |
| 1085 | } |
| 1086 | *pp = pPage->pNext; |
| 1087 | |
| 1088 | h = iNew%pCache->nHash; |
| 1089 | pPage->iKey = iNew; |
| 1090 | pPage->pNext = pCache->apHash[h]; |
| 1091 | pCache->apHash[h] = pPage; |
drh | 98829a6 | 2009-11-20 13:18:14 +0000 | [diff] [blame] | 1092 | if( iNew>pCache->iMaxKey ){ |
danielk1977 | f90b726 | 2009-01-07 15:18:20 +0000 | [diff] [blame] | 1093 | pCache->iMaxKey = iNew; |
| 1094 | } |
| 1095 | |
drh | 9f8cf9d | 2011-01-17 21:32:24 +0000 | [diff] [blame] | 1096 | pcache1LeaveMutex(pCache->pGroup); |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 1097 | } |
| 1098 | |
| 1099 | /* |
| 1100 | ** Implementation of the sqlite3_pcache.xTruncate method. |
| 1101 | ** |
| 1102 | ** Discard all unpinned pages in the cache with a page number equal to |
| 1103 | ** or greater than parameter iLimit. Any pinned pages with a page number |
| 1104 | ** equal to or greater than iLimit are implicitly unpinned. |
| 1105 | */ |
| 1106 | static void pcache1Truncate(sqlite3_pcache *p, unsigned int iLimit){ |
| 1107 | PCache1 *pCache = (PCache1 *)p; |
drh | 9f8cf9d | 2011-01-17 21:32:24 +0000 | [diff] [blame] | 1108 | pcache1EnterMutex(pCache->pGroup); |
danielk1977 | f90b726 | 2009-01-07 15:18:20 +0000 | [diff] [blame] | 1109 | if( iLimit<=pCache->iMaxKey ){ |
| 1110 | pcache1TruncateUnsafe(pCache, iLimit); |
| 1111 | pCache->iMaxKey = iLimit-1; |
| 1112 | } |
drh | 9f8cf9d | 2011-01-17 21:32:24 +0000 | [diff] [blame] | 1113 | pcache1LeaveMutex(pCache->pGroup); |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 1114 | } |
| 1115 | |
| 1116 | /* |
| 1117 | ** Implementation of the sqlite3_pcache.xDestroy method. |
| 1118 | ** |
| 1119 | ** Destroy a cache allocated using pcache1Create(). |
| 1120 | */ |
| 1121 | static void pcache1Destroy(sqlite3_pcache *p){ |
| 1122 | PCache1 *pCache = (PCache1 *)p; |
drh | 9f8cf9d | 2011-01-17 21:32:24 +0000 | [diff] [blame] | 1123 | PGroup *pGroup = pCache->pGroup; |
dan | b51d2fa | 2010-09-22 19:06:02 +0000 | [diff] [blame] | 1124 | assert( pCache->bPurgeable || (pCache->nMax==0 && pCache->nMin==0) ); |
drh | 9f8cf9d | 2011-01-17 21:32:24 +0000 | [diff] [blame] | 1125 | pcache1EnterMutex(pGroup); |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 1126 | pcache1TruncateUnsafe(pCache, 0); |
drh | a69085c | 2012-01-02 18:00:55 +0000 | [diff] [blame] | 1127 | assert( pGroup->nMaxPage >= pCache->nMax ); |
drh | 9f8cf9d | 2011-01-17 21:32:24 +0000 | [diff] [blame] | 1128 | pGroup->nMaxPage -= pCache->nMax; |
drh | a69085c | 2012-01-02 18:00:55 +0000 | [diff] [blame] | 1129 | assert( pGroup->nMinPage >= pCache->nMin ); |
drh | 9f8cf9d | 2011-01-17 21:32:24 +0000 | [diff] [blame] | 1130 | pGroup->nMinPage -= pCache->nMin; |
drh | 41692e9 | 2011-01-25 04:34:51 +0000 | [diff] [blame] | 1131 | pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage; |
drh | 957026a | 2015-07-16 18:18:19 +0000 | [diff] [blame] | 1132 | pcache1EnforceMaxPage(pCache); |
drh | 9f8cf9d | 2011-01-17 21:32:24 +0000 | [diff] [blame] | 1133 | pcache1LeaveMutex(pGroup); |
drh | ee70a84 | 2015-07-06 18:54:52 +0000 | [diff] [blame] | 1134 | sqlite3_free(pCache->pBulk); |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 1135 | sqlite3_free(pCache->apHash); |
| 1136 | sqlite3_free(pCache); |
| 1137 | } |
| 1138 | |
| 1139 | /* |
| 1140 | ** This function is called during initialization (sqlite3_initialize()) to |
| 1141 | ** install the default pluggable cache module, assuming the user has not |
| 1142 | ** already provided an alternative. |
| 1143 | */ |
| 1144 | void sqlite3PCacheSetDefault(void){ |
dan | 22e21ff | 2011-11-08 20:08:44 +0000 | [diff] [blame] | 1145 | static const sqlite3_pcache_methods2 defaultMethods = { |
drh | 81ef0f9 | 2011-11-13 21:44:03 +0000 | [diff] [blame] | 1146 | 1, /* iVersion */ |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 1147 | 0, /* pArg */ |
| 1148 | pcache1Init, /* xInit */ |
| 1149 | pcache1Shutdown, /* xShutdown */ |
| 1150 | pcache1Create, /* xCreate */ |
| 1151 | pcache1Cachesize, /* xCachesize */ |
| 1152 | pcache1Pagecount, /* xPagecount */ |
| 1153 | pcache1Fetch, /* xFetch */ |
| 1154 | pcache1Unpin, /* xUnpin */ |
| 1155 | pcache1Rekey, /* xRekey */ |
| 1156 | pcache1Truncate, /* xTruncate */ |
drh | 09419b4 | 2011-11-16 19:29:17 +0000 | [diff] [blame] | 1157 | pcache1Destroy, /* xDestroy */ |
| 1158 | pcache1Shrink /* xShrink */ |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 1159 | }; |
dan | 22e21ff | 2011-11-08 20:08:44 +0000 | [diff] [blame] | 1160 | sqlite3_config(SQLITE_CONFIG_PCACHE2, &defaultMethods); |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 1161 | } |
| 1162 | |
drh | def6889 | 2014-11-04 12:11:23 +0000 | [diff] [blame] | 1163 | /* |
| 1164 | ** Return the size of the header on each page of this PCACHE implementation. |
| 1165 | */ |
drh | 37c057b | 2014-12-30 00:57:29 +0000 | [diff] [blame] | 1166 | int sqlite3HeaderSizePcache1(void){ return ROUND8(sizeof(PgHdr1)); } |
drh | def6889 | 2014-11-04 12:11:23 +0000 | [diff] [blame] | 1167 | |
drh | af89fe6 | 2015-03-23 17:25:18 +0000 | [diff] [blame] | 1168 | /* |
| 1169 | ** Return the global mutex used by this PCACHE implementation. The |
| 1170 | ** sqlite3_status() routine needs access to this mutex. |
| 1171 | */ |
| 1172 | sqlite3_mutex *sqlite3Pcache1Mutex(void){ |
| 1173 | return pcache1.mutex; |
| 1174 | } |
| 1175 | |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 1176 | #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT |
| 1177 | /* |
| 1178 | ** This function is called to free superfluous dynamically allocated memory |
| 1179 | ** held by the pager system. Memory in use by any SQLite pager allocated |
| 1180 | ** by the current thread may be sqlite3_free()ed. |
| 1181 | ** |
| 1182 | ** nReq is the number of bytes of memory required. Once this much has |
| 1183 | ** been released, the function returns. The return value is the total number |
| 1184 | ** of bytes of memory released. |
| 1185 | */ |
| 1186 | int sqlite3PcacheReleaseMemory(int nReq){ |
| 1187 | int nFree = 0; |
drh | 9f8cf9d | 2011-01-17 21:32:24 +0000 | [diff] [blame] | 1188 | assert( sqlite3_mutex_notheld(pcache1.grp.mutex) ); |
| 1189 | assert( sqlite3_mutex_notheld(pcache1.mutex) ); |
drh | ee70a84 | 2015-07-06 18:54:52 +0000 | [diff] [blame] | 1190 | if( sqlite3GlobalConfig.nPage==0 ){ |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 1191 | PgHdr1 *p; |
drh | 9f8cf9d | 2011-01-17 21:32:24 +0000 | [diff] [blame] | 1192 | pcache1EnterMutex(&pcache1.grp); |
| 1193 | while( (nReq<0 || nFree<nReq) && ((p=pcache1.grp.pLruTail)!=0) ){ |
dan | 22e21ff | 2011-11-08 20:08:44 +0000 | [diff] [blame] | 1194 | nFree += pcache1MemSize(p->page.pBuf); |
| 1195 | #ifdef SQLITE_PCACHE_SEPARATE_HEADER |
| 1196 | nFree += sqlite3MemSize(p); |
| 1197 | #endif |
drh | 5d56dd2 | 2013-12-13 18:50:40 +0000 | [diff] [blame] | 1198 | assert( p->isPinned==0 ); |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 1199 | pcache1PinPage(p); |
drh | 95c91e1 | 2015-06-29 00:21:00 +0000 | [diff] [blame] | 1200 | pcache1RemoveFromHash(p, 1); |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 1201 | } |
drh | 9f8cf9d | 2011-01-17 21:32:24 +0000 | [diff] [blame] | 1202 | pcache1LeaveMutex(&pcache1.grp); |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 1203 | } |
| 1204 | return nFree; |
| 1205 | } |
| 1206 | #endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */ |
| 1207 | |
| 1208 | #ifdef SQLITE_TEST |
| 1209 | /* |
| 1210 | ** This function is used by test procedures to inspect the internal state |
| 1211 | ** of the global cache. |
| 1212 | */ |
| 1213 | void sqlite3PcacheStats( |
| 1214 | int *pnCurrent, /* OUT: Total number of pages cached */ |
| 1215 | int *pnMax, /* OUT: Global maximum cache size */ |
| 1216 | int *pnMin, /* OUT: Sum of PCache1.nMin for purgeable caches */ |
| 1217 | int *pnRecyclable /* OUT: Total number of pages available for recycling */ |
| 1218 | ){ |
| 1219 | PgHdr1 *p; |
| 1220 | int nRecyclable = 0; |
drh | 9f8cf9d | 2011-01-17 21:32:24 +0000 | [diff] [blame] | 1221 | for(p=pcache1.grp.pLruHead; p; p=p->pLruNext){ |
drh | 5d56dd2 | 2013-12-13 18:50:40 +0000 | [diff] [blame] | 1222 | assert( p->isPinned==0 ); |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 1223 | nRecyclable++; |
| 1224 | } |
drh | 9f8cf9d | 2011-01-17 21:32:24 +0000 | [diff] [blame] | 1225 | *pnCurrent = pcache1.grp.nCurrentPage; |
drh | a69085c | 2012-01-02 18:00:55 +0000 | [diff] [blame] | 1226 | *pnMax = (int)pcache1.grp.nMaxPage; |
| 1227 | *pnMin = (int)pcache1.grp.nMinPage; |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 1228 | *pnRecyclable = nRecyclable; |
| 1229 | } |
| 1230 | #endif |