danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 1 | /* |
| 2 | ** 2008 August 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 | ** This file implements that page cache. |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 13 | */ |
| 14 | #include "sqliteInt.h" |
| 15 | |
| 16 | /* |
drh | 72e6a39 | 2016-05-11 23:54:14 +0000 | [diff] [blame] | 17 | ** A complete page cache is an instance of this structure. Every |
| 18 | ** entry in the cache holds a single page of the database file. The |
| 19 | ** btree layer only operates on the cached copy of the database pages. |
| 20 | ** |
| 21 | ** A page cache entry is "clean" if it exactly matches what is currently |
| 22 | ** on disk. A page is "dirty" if it has been modified and needs to be |
| 23 | ** persisted to disk. |
dan | b2ef900 | 2016-05-11 15:41:15 +0000 | [diff] [blame] | 24 | ** |
| 25 | ** pDirty, pDirtyTail, pSynced: |
| 26 | ** All dirty pages are linked into the doubly linked list using |
| 27 | ** PgHdr.pDirtyNext and pDirtyPrev. The list is maintained in LRU order |
| 28 | ** such that p was added to the list more recently than p->pDirtyNext. |
| 29 | ** PCache.pDirty points to the first (newest) element in the list and |
| 30 | ** pDirtyTail to the last (oldest). |
| 31 | ** |
| 32 | ** The PCache.pSynced variable is used to optimize searching for a dirty |
| 33 | ** page to eject from the cache mid-transaction. It is better to eject |
| 34 | ** a page that does not require a journal sync than one that does. |
| 35 | ** Therefore, pSynced is maintained to that it *almost* always points |
| 36 | ** to either the oldest page in the pDirty/pDirtyTail list that has a |
| 37 | ** clear PGHDR_NEED_SYNC flag or to a page that is older than this one |
| 38 | ** (so that the right page to eject can be found by following pDirtyPrev |
| 39 | ** pointers). |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 40 | */ |
| 41 | struct PCache { |
danielk1977 | d491e1b | 2008-08-26 18:05:48 +0000 | [diff] [blame] | 42 | PgHdr *pDirty, *pDirtyTail; /* List of dirty pages in LRU order */ |
| 43 | PgHdr *pSynced; /* Last synced page in dirty page list */ |
drh | 95a0b37 | 2015-09-03 20:43:55 +0000 | [diff] [blame] | 44 | int nRefSum; /* Sum of ref counts over all pages */ |
drh | 3b42abb | 2011-11-09 14:23:04 +0000 | [diff] [blame] | 45 | int szCache; /* Configured cache size */ |
drh | 9b0cf34 | 2015-11-12 14:57:19 +0000 | [diff] [blame] | 46 | int szSpill; /* Size before spilling occurs */ |
drh | a85f7e3 | 2008-08-28 02:26:07 +0000 | [diff] [blame] | 47 | int szPage; /* Size of every page in this cache */ |
| 48 | int szExtra; /* Size of extra space for each page */ |
drh | fe21a79 | 2014-02-03 17:04:29 +0000 | [diff] [blame] | 49 | u8 bPurgeable; /* True if pages are on backing store */ |
| 50 | u8 eCreate; /* eCreate value for for xFetch() */ |
drh | a85f7e3 | 2008-08-28 02:26:07 +0000 | [diff] [blame] | 51 | int (*xStress)(void*,PgHdr*); /* Call to try make a page clean */ |
| 52 | void *pStress; /* Argument to xStress */ |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 53 | sqlite3_pcache *pCache; /* Pluggable cache module */ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 54 | }; |
| 55 | |
drh | a0f6b12 | 2016-05-13 15:22:06 +0000 | [diff] [blame] | 56 | /********************************** Test and Debug Logic **********************/ |
drh | 5c8e092 | 2016-05-11 10:57:04 +0000 | [diff] [blame] | 57 | /* |
drh | a0f6b12 | 2016-05-13 15:22:06 +0000 | [diff] [blame] | 58 | ** Debug tracing macros. Enable by by changing the "0" to "1" and |
| 59 | ** recompiling. |
| 60 | ** |
| 61 | ** When sqlite3PcacheTrace is 1, single line trace messages are issued. |
| 62 | ** When sqlite3PcacheTrace is 2, a dump of the pcache showing all cache entries |
| 63 | ** is displayed for many operations, resulting in a lot of output. |
drh | 5c8e092 | 2016-05-11 10:57:04 +0000 | [diff] [blame] | 64 | */ |
| 65 | #if defined(SQLITE_DEBUG) && 0 |
drh | a0f6b12 | 2016-05-13 15:22:06 +0000 | [diff] [blame] | 66 | int sqlite3PcacheTrace = 2; /* 0: off 1: simple 2: cache dumps */ |
| 67 | int sqlite3PcacheMxDump = 9999; /* Max cache entries for pcacheDump() */ |
drh | 5c8e092 | 2016-05-11 10:57:04 +0000 | [diff] [blame] | 68 | # define pcacheTrace(X) if(sqlite3PcacheTrace){sqlite3DebugPrintf X;} |
drh | 7aeb216 | 2016-05-13 04:24:25 +0000 | [diff] [blame] | 69 | void pcacheDump(PCache *pCache){ |
| 70 | int N; |
| 71 | int i, j; |
| 72 | sqlite3_pcache_page *pLower; |
| 73 | PgHdr *pPg; |
| 74 | unsigned char *a; |
| 75 | |
| 76 | if( sqlite3PcacheTrace<2 ) return; |
| 77 | if( pCache->pCache==0 ) return; |
| 78 | N = sqlite3PcachePagecount(pCache); |
drh | a0f6b12 | 2016-05-13 15:22:06 +0000 | [diff] [blame] | 79 | if( N>sqlite3PcacheMxDump ) N = sqlite3PcacheMxDump; |
drh | 7aeb216 | 2016-05-13 04:24:25 +0000 | [diff] [blame] | 80 | for(i=1; i<=N; i++){ |
| 81 | pLower = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, i, 0); |
| 82 | if( pLower==0 ) continue; |
| 83 | pPg = (PgHdr*)pLower->pExtra; |
| 84 | printf("%3d: nRef %2d flgs %02x data ", i, pPg->nRef, pPg->flags); |
| 85 | a = (unsigned char *)pLower->pBuf; |
| 86 | for(j=0; j<12; j++) printf("%02x", a[j]); |
| 87 | printf("\n"); |
| 88 | if( pPg->pPage==0 ){ |
| 89 | sqlite3GlobalConfig.pcache2.xUnpin(pCache->pCache, pLower, 0); |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | #else |
drh | 5c8e092 | 2016-05-11 10:57:04 +0000 | [diff] [blame] | 94 | # define pcacheTrace(X) |
drh | 7aeb216 | 2016-05-13 04:24:25 +0000 | [diff] [blame] | 95 | # define pcacheDump(X) |
drh | 5c8e092 | 2016-05-11 10:57:04 +0000 | [diff] [blame] | 96 | #endif |
| 97 | |
drh | a0f6b12 | 2016-05-13 15:22:06 +0000 | [diff] [blame] | 98 | /* |
| 99 | ** Check invariants on a PgHdr entry. Return true if everything is OK. |
| 100 | ** Return false if any invariant is violated. |
| 101 | ** |
| 102 | ** This routine is for use inside of assert() statements only. For |
| 103 | ** example: |
| 104 | ** |
| 105 | ** assert( sqlite3PcachePageSanity(pPg) ); |
| 106 | */ |
drh | d879e3e | 2017-02-13 13:35:55 +0000 | [diff] [blame] | 107 | #ifdef SQLITE_DEBUG |
drh | a0f6b12 | 2016-05-13 15:22:06 +0000 | [diff] [blame] | 108 | int sqlite3PcachePageSanity(PgHdr *pPg){ |
| 109 | PCache *pCache; |
| 110 | assert( pPg!=0 ); |
drh | cbed604 | 2016-12-13 18:34:01 +0000 | [diff] [blame] | 111 | assert( pPg->pgno>0 || pPg->pPager==0 ); /* Page number is 1 or more */ |
drh | a0f6b12 | 2016-05-13 15:22:06 +0000 | [diff] [blame] | 112 | pCache = pPg->pCache; |
| 113 | assert( pCache!=0 ); /* Every page has an associated PCache */ |
| 114 | if( pPg->flags & PGHDR_CLEAN ){ |
| 115 | assert( (pPg->flags & PGHDR_DIRTY)==0 );/* Cannot be both CLEAN and DIRTY */ |
| 116 | assert( pCache->pDirty!=pPg ); /* CLEAN pages not on dirty list */ |
| 117 | assert( pCache->pDirtyTail!=pPg ); |
| 118 | } |
| 119 | /* WRITEABLE pages must also be DIRTY */ |
| 120 | if( pPg->flags & PGHDR_WRITEABLE ){ |
| 121 | assert( pPg->flags & PGHDR_DIRTY ); /* WRITEABLE implies DIRTY */ |
| 122 | } |
| 123 | /* NEED_SYNC can be set independently of WRITEABLE. This can happen, |
| 124 | ** for example, when using the sqlite3PagerDontWrite() optimization: |
| 125 | ** (1) Page X is journalled, and gets WRITEABLE and NEED_SEEK. |
| 126 | ** (2) Page X moved to freelist, WRITEABLE is cleared |
| 127 | ** (3) Page X reused, WRITEABLE is set again |
| 128 | ** If NEED_SYNC had been cleared in step 2, then it would not be reset |
| 129 | ** in step 3, and page might be written into the database without first |
| 130 | ** syncing the rollback journal, which might cause corruption on a power |
| 131 | ** loss. |
drh | 3b02a07 | 2016-05-13 17:22:33 +0000 | [diff] [blame] | 132 | ** |
| 133 | ** Another example is when the database page size is smaller than the |
| 134 | ** disk sector size. When any page of a sector is journalled, all pages |
| 135 | ** in that sector are marked NEED_SYNC even if they are still CLEAN, just |
| 136 | ** in case they are later modified, since all pages in the same sector |
| 137 | ** must be journalled and synced before any of those pages can be safely |
| 138 | ** written. |
drh | a0f6b12 | 2016-05-13 15:22:06 +0000 | [diff] [blame] | 139 | */ |
| 140 | return 1; |
| 141 | } |
| 142 | #endif /* SQLITE_DEBUG */ |
| 143 | |
| 144 | |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 145 | /********************************** Linked List Management ********************/ |
| 146 | |
drh | a8dcba9 | 2014-08-22 20:35:29 +0000 | [diff] [blame] | 147 | /* Allowed values for second argument to pcacheManageDirtyList() */ |
| 148 | #define PCACHE_DIRTYLIST_REMOVE 1 /* Remove pPage from dirty list */ |
| 149 | #define PCACHE_DIRTYLIST_ADD 2 /* Add pPage to the dirty list */ |
| 150 | #define PCACHE_DIRTYLIST_FRONT 3 /* Move pPage to the front of the list */ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 151 | |
| 152 | /* |
drh | a8dcba9 | 2014-08-22 20:35:29 +0000 | [diff] [blame] | 153 | ** Manage pPage's participation on the dirty list. Bits of the addRemove |
| 154 | ** argument determines what operation to do. The 0x01 bit means first |
| 155 | ** remove pPage from the dirty list. The 0x02 means add pPage back to |
| 156 | ** the dirty list. Doing both moves pPage to the front of the dirty list. |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 157 | */ |
drh | a8dcba9 | 2014-08-22 20:35:29 +0000 | [diff] [blame] | 158 | static void pcacheManageDirtyList(PgHdr *pPage, u8 addRemove){ |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 159 | PCache *p = pPage->pCache; |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 160 | |
drh | 5c8e092 | 2016-05-11 10:57:04 +0000 | [diff] [blame] | 161 | pcacheTrace(("%p.DIRTYLIST.%s %d\n", p, |
| 162 | addRemove==1 ? "REMOVE" : addRemove==2 ? "ADD" : "FRONT", |
| 163 | pPage->pgno)); |
drh | a8dcba9 | 2014-08-22 20:35:29 +0000 | [diff] [blame] | 164 | if( addRemove & PCACHE_DIRTYLIST_REMOVE ){ |
| 165 | assert( pPage->pDirtyNext || pPage==p->pDirtyTail ); |
| 166 | assert( pPage->pDirtyPrev || pPage==p->pDirty ); |
| 167 | |
| 168 | /* Update the PCache1.pSynced variable if necessary. */ |
| 169 | if( p->pSynced==pPage ){ |
dan | b2ef900 | 2016-05-11 15:41:15 +0000 | [diff] [blame] | 170 | p->pSynced = pPage->pDirtyPrev; |
drh | a8dcba9 | 2014-08-22 20:35:29 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | if( pPage->pDirtyNext ){ |
| 174 | pPage->pDirtyNext->pDirtyPrev = pPage->pDirtyPrev; |
| 175 | }else{ |
| 176 | assert( pPage==p->pDirtyTail ); |
| 177 | p->pDirtyTail = pPage->pDirtyPrev; |
| 178 | } |
| 179 | if( pPage->pDirtyPrev ){ |
| 180 | pPage->pDirtyPrev->pDirtyNext = pPage->pDirtyNext; |
| 181 | }else{ |
dan | 401907e | 2016-05-11 20:03:23 +0000 | [diff] [blame] | 182 | /* If there are now no dirty pages in the cache, set eCreate to 2. |
| 183 | ** This is an optimization that allows sqlite3PcacheFetch() to skip |
| 184 | ** searching for a dirty page to eject from the cache when it might |
| 185 | ** otherwise have to. */ |
drh | a8dcba9 | 2014-08-22 20:35:29 +0000 | [diff] [blame] | 186 | assert( pPage==p->pDirty ); |
| 187 | p->pDirty = pPage->pDirtyNext; |
dan | 401907e | 2016-05-11 20:03:23 +0000 | [diff] [blame] | 188 | assert( p->bPurgeable || p->eCreate==2 ); |
| 189 | if( p->pDirty==0 ){ /*OPTIMIZATION-IF-TRUE*/ |
| 190 | assert( p->bPurgeable==0 || p->eCreate==1 ); |
drh | a8dcba9 | 2014-08-22 20:35:29 +0000 | [diff] [blame] | 191 | p->eCreate = 2; |
| 192 | } |
| 193 | } |
| 194 | pPage->pDirtyNext = 0; |
| 195 | pPage->pDirtyPrev = 0; |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 196 | } |
drh | a8dcba9 | 2014-08-22 20:35:29 +0000 | [diff] [blame] | 197 | if( addRemove & PCACHE_DIRTYLIST_ADD ){ |
| 198 | assert( pPage->pDirtyNext==0 && pPage->pDirtyPrev==0 && p->pDirty!=pPage ); |
| 199 | |
| 200 | pPage->pDirtyNext = p->pDirty; |
| 201 | if( pPage->pDirtyNext ){ |
| 202 | assert( pPage->pDirtyNext->pDirtyPrev==0 ); |
| 203 | pPage->pDirtyNext->pDirtyPrev = pPage; |
drh | 36ce919 | 2014-09-12 20:30:59 +0000 | [diff] [blame] | 204 | }else{ |
| 205 | p->pDirtyTail = pPage; |
| 206 | if( p->bPurgeable ){ |
| 207 | assert( p->eCreate==2 ); |
| 208 | p->eCreate = 1; |
| 209 | } |
drh | a8dcba9 | 2014-08-22 20:35:29 +0000 | [diff] [blame] | 210 | } |
| 211 | p->pDirty = pPage; |
dan | 613723d | 2016-05-12 09:48:23 +0000 | [diff] [blame] | 212 | |
| 213 | /* If pSynced is NULL and this page has a clear NEED_SYNC flag, set |
| 214 | ** pSynced to point to it. Checking the NEED_SYNC flag is an |
| 215 | ** optimization, as if pSynced points to a page with the NEED_SYNC |
| 216 | ** flag set sqlite3PcacheFetchStress() searches through all newer |
| 217 | ** entries of the dirty-list for a page with NEED_SYNC clear anyway. */ |
| 218 | if( !p->pSynced |
| 219 | && 0==(pPage->flags&PGHDR_NEED_SYNC) /*OPTIMIZATION-IF-FALSE*/ |
| 220 | ){ |
drh | a8dcba9 | 2014-08-22 20:35:29 +0000 | [diff] [blame] | 221 | p->pSynced = pPage; |
| 222 | } |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 223 | } |
drh | 7aeb216 | 2016-05-13 04:24:25 +0000 | [diff] [blame] | 224 | pcacheDump(p); |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | /* |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 228 | ** Wrapper around the pluggable caches xUnpin method. If the cache is |
| 229 | ** being used for an in-memory database, this function is a no-op. |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 230 | */ |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 231 | static void pcacheUnpin(PgHdr *p){ |
drh | a8dcba9 | 2014-08-22 20:35:29 +0000 | [diff] [blame] | 232 | if( p->pCache->bPurgeable ){ |
drh | 5c8e092 | 2016-05-11 10:57:04 +0000 | [diff] [blame] | 233 | pcacheTrace(("%p.UNPIN %d\n", p->pCache, p->pgno)); |
drh | a8dcba9 | 2014-08-22 20:35:29 +0000 | [diff] [blame] | 234 | sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 0); |
drh | 7aeb216 | 2016-05-13 04:24:25 +0000 | [diff] [blame] | 235 | pcacheDump(p->pCache); |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 236 | } |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 237 | } |
| 238 | |
drh | c3031c6 | 2014-08-26 15:06:49 +0000 | [diff] [blame] | 239 | /* |
drh | 9b0cf34 | 2015-11-12 14:57:19 +0000 | [diff] [blame] | 240 | ** Compute the number of pages of cache requested. p->szCache is the |
drh | e0e8429 | 2015-02-27 21:53:35 +0000 | [diff] [blame] | 241 | ** cache size requested by the "PRAGMA cache_size" statement. |
drh | c3031c6 | 2014-08-26 15:06:49 +0000 | [diff] [blame] | 242 | */ |
| 243 | static int numberOfCachePages(PCache *p){ |
| 244 | if( p->szCache>=0 ){ |
drh | e0e8429 | 2015-02-27 21:53:35 +0000 | [diff] [blame] | 245 | /* IMPLEMENTATION-OF: R-42059-47211 If the argument N is positive then the |
| 246 | ** suggested cache size is set to N. */ |
drh | c3031c6 | 2014-08-26 15:06:49 +0000 | [diff] [blame] | 247 | return p->szCache; |
| 248 | }else{ |
drh | e0e8429 | 2015-02-27 21:53:35 +0000 | [diff] [blame] | 249 | /* IMPLEMENTATION-OF: R-61436-13639 If the argument N is negative, then |
| 250 | ** the number of cache pages is adjusted to use approximately abs(N*1024) |
| 251 | ** bytes of memory. */ |
drh | c3031c6 | 2014-08-26 15:06:49 +0000 | [diff] [blame] | 252 | return (int)((-1024*(i64)p->szCache)/(p->szPage+p->szExtra)); |
| 253 | } |
| 254 | } |
| 255 | |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 256 | /*************************************************** General Interfaces ****** |
| 257 | ** |
| 258 | ** Initialize and shutdown the page cache subsystem. Neither of these |
| 259 | ** functions are threadsafe. |
| 260 | */ |
| 261 | int sqlite3PcacheInitialize(void){ |
dan | 22e21ff | 2011-11-08 20:08:44 +0000 | [diff] [blame] | 262 | if( sqlite3GlobalConfig.pcache2.xInit==0 ){ |
drh | f759bb8 | 2010-09-09 18:25:34 +0000 | [diff] [blame] | 263 | /* IMPLEMENTATION-OF: R-26801-64137 If the xInit() method is NULL, then the |
| 264 | ** built-in default page cache is used instead of the application defined |
| 265 | ** page cache. */ |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 266 | sqlite3PCacheSetDefault(); |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 267 | } |
dan | 22e21ff | 2011-11-08 20:08:44 +0000 | [diff] [blame] | 268 | return sqlite3GlobalConfig.pcache2.xInit(sqlite3GlobalConfig.pcache2.pArg); |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 269 | } |
| 270 | void sqlite3PcacheShutdown(void){ |
dan | 22e21ff | 2011-11-08 20:08:44 +0000 | [diff] [blame] | 271 | if( sqlite3GlobalConfig.pcache2.xShutdown ){ |
drh | f759bb8 | 2010-09-09 18:25:34 +0000 | [diff] [blame] | 272 | /* IMPLEMENTATION-OF: R-26000-56589 The xShutdown() method may be NULL. */ |
dan | 22e21ff | 2011-11-08 20:08:44 +0000 | [diff] [blame] | 273 | sqlite3GlobalConfig.pcache2.xShutdown(sqlite3GlobalConfig.pcache2.pArg); |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 274 | } |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | /* |
| 278 | ** Return the size in bytes of a PCache object. |
| 279 | */ |
| 280 | int sqlite3PcacheSize(void){ return sizeof(PCache); } |
| 281 | |
| 282 | /* |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 283 | ** Create a new PCache object. Storage space to hold the object |
| 284 | ** has already been allocated and is passed in as the p pointer. |
| 285 | ** The caller discovers how much space needs to be allocated by |
| 286 | ** calling sqlite3PcacheSize(). |
drh | a2ee589 | 2016-12-09 16:02:00 +0000 | [diff] [blame] | 287 | ** |
| 288 | ** szExtra is some extra space allocated for each page. The first |
| 289 | ** 8 bytes of the extra space will be zeroed as the page is allocated, |
| 290 | ** but remaining content will be uninitialized. Though it is opaque |
| 291 | ** to this module, the extra space really ends up being the MemPage |
| 292 | ** structure in the pager. |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 293 | */ |
drh | c3031c6 | 2014-08-26 15:06:49 +0000 | [diff] [blame] | 294 | int sqlite3PcacheOpen( |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 295 | int szPage, /* Size of every page */ |
| 296 | int szExtra, /* Extra space associated with each page */ |
| 297 | int bPurgeable, /* True if pages are on backing store */ |
danielk1977 | a858aa2 | 2008-08-22 16:22:17 +0000 | [diff] [blame] | 298 | int (*xStress)(void*,PgHdr*),/* Call to try to make pages clean */ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 299 | void *pStress, /* Argument to xStress */ |
| 300 | PCache *p /* Preallocated space for the PCache */ |
| 301 | ){ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 302 | memset(p, 0, sizeof(PCache)); |
drh | c3031c6 | 2014-08-26 15:06:49 +0000 | [diff] [blame] | 303 | p->szPage = 1; |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 304 | p->szExtra = szExtra; |
drh | a2ee589 | 2016-12-09 16:02:00 +0000 | [diff] [blame] | 305 | assert( szExtra>=8 ); /* First 8 bytes will be zeroed */ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 306 | p->bPurgeable = bPurgeable; |
drh | fe21a79 | 2014-02-03 17:04:29 +0000 | [diff] [blame] | 307 | p->eCreate = 2; |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 308 | p->xStress = xStress; |
| 309 | p->pStress = pStress; |
drh | 3b42abb | 2011-11-09 14:23:04 +0000 | [diff] [blame] | 310 | p->szCache = 100; |
drh | 9b0cf34 | 2015-11-12 14:57:19 +0000 | [diff] [blame] | 311 | p->szSpill = 1; |
drh | 5c8e092 | 2016-05-11 10:57:04 +0000 | [diff] [blame] | 312 | pcacheTrace(("%p.OPEN szPage %d bPurgeable %d\n",p,szPage,bPurgeable)); |
drh | c3031c6 | 2014-08-26 15:06:49 +0000 | [diff] [blame] | 313 | return sqlite3PcacheSetPageSize(p, szPage); |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 314 | } |
| 315 | |
drh | 41d3027 | 2008-08-20 21:47:45 +0000 | [diff] [blame] | 316 | /* |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 317 | ** Change the page size for PCache object. The caller must ensure that there |
| 318 | ** are no outstanding page references when this function is called. |
drh | 41d3027 | 2008-08-20 21:47:45 +0000 | [diff] [blame] | 319 | */ |
drh | c3031c6 | 2014-08-26 15:06:49 +0000 | [diff] [blame] | 320 | int sqlite3PcacheSetPageSize(PCache *pCache, int szPage){ |
drh | 95a0b37 | 2015-09-03 20:43:55 +0000 | [diff] [blame] | 321 | assert( pCache->nRefSum==0 && pCache->pDirty==0 ); |
drh | c3031c6 | 2014-08-26 15:06:49 +0000 | [diff] [blame] | 322 | if( pCache->szPage ){ |
| 323 | sqlite3_pcache *pNew; |
| 324 | pNew = sqlite3GlobalConfig.pcache2.xCreate( |
drh | 51dc84e | 2014-12-30 13:04:25 +0000 | [diff] [blame] | 325 | szPage, pCache->szExtra + ROUND8(sizeof(PgHdr)), |
| 326 | pCache->bPurgeable |
drh | c3031c6 | 2014-08-26 15:06:49 +0000 | [diff] [blame] | 327 | ); |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 328 | if( pNew==0 ) return SQLITE_NOMEM_BKPT; |
drh | c3031c6 | 2014-08-26 15:06:49 +0000 | [diff] [blame] | 329 | sqlite3GlobalConfig.pcache2.xCachesize(pNew, numberOfCachePages(pCache)); |
| 330 | if( pCache->pCache ){ |
| 331 | sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache); |
| 332 | } |
| 333 | pCache->pCache = pNew; |
drh | c3031c6 | 2014-08-26 15:06:49 +0000 | [diff] [blame] | 334 | pCache->szPage = szPage; |
drh | 5c8e092 | 2016-05-11 10:57:04 +0000 | [diff] [blame] | 335 | pcacheTrace(("%p.PAGESIZE %d\n",pCache,szPage)); |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 336 | } |
drh | c3031c6 | 2014-08-26 15:06:49 +0000 | [diff] [blame] | 337 | return SQLITE_OK; |
drh | 3b42abb | 2011-11-09 14:23:04 +0000 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | /* |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 341 | ** Try to obtain a page from the cache. |
drh | bc59ac0 | 2014-08-27 23:18:01 +0000 | [diff] [blame] | 342 | ** |
| 343 | ** This routine returns a pointer to an sqlite3_pcache_page object if |
| 344 | ** such an object is already in cache, or if a new one is created. |
| 345 | ** This routine returns a NULL pointer if the object was not in cache |
| 346 | ** and could not be created. |
| 347 | ** |
| 348 | ** The createFlags should be 0 to check for existing pages and should |
| 349 | ** be 3 (not 1, but 3) to try to create a new page. |
| 350 | ** |
| 351 | ** If the createFlag is 0, then NULL is always returned if the page |
| 352 | ** is not already in the cache. If createFlag is 1, then a new page |
| 353 | ** is created only if that can be done without spilling dirty pages |
| 354 | ** and without exceeding the cache size limit. |
| 355 | ** |
| 356 | ** The caller needs to invoke sqlite3PcacheFetchFinish() to properly |
| 357 | ** initialize the sqlite3_pcache_page object and convert it into a |
| 358 | ** PgHdr object. The sqlite3PcacheFetch() and sqlite3PcacheFetchFinish() |
| 359 | ** routines are split this way for performance reasons. When separated |
| 360 | ** they can both (usually) operate without having to push values to |
| 361 | ** the stack on entry and pop them back off on exit, which saves a |
| 362 | ** lot of pushing and popping. |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 363 | */ |
drh | bc59ac0 | 2014-08-27 23:18:01 +0000 | [diff] [blame] | 364 | sqlite3_pcache_page *sqlite3PcacheFetch( |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 365 | PCache *pCache, /* Obtain the page from this cache */ |
| 366 | Pgno pgno, /* Page number to obtain */ |
drh | bc59ac0 | 2014-08-27 23:18:01 +0000 | [diff] [blame] | 367 | int createFlag /* If true, create page if it does not exist already */ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 368 | ){ |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 369 | int eCreate; |
drh | 5c8e092 | 2016-05-11 10:57:04 +0000 | [diff] [blame] | 370 | sqlite3_pcache_page *pRes; |
danielk1977 | f599a19 | 2008-08-28 10:21:16 +0000 | [diff] [blame] | 371 | |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 372 | assert( pCache!=0 ); |
drh | c3031c6 | 2014-08-26 15:06:49 +0000 | [diff] [blame] | 373 | assert( pCache->pCache!=0 ); |
| 374 | assert( createFlag==3 || createFlag==0 ); |
dan | 401907e | 2016-05-11 20:03:23 +0000 | [diff] [blame] | 375 | assert( pCache->eCreate==((pCache->bPurgeable && pCache->pDirty) ? 1 : 2) ); |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 376 | |
drh | fe21a79 | 2014-02-03 17:04:29 +0000 | [diff] [blame] | 377 | /* eCreate defines what to do if the page does not exist. |
| 378 | ** 0 Do not allocate a new page. (createFlag==0) |
| 379 | ** 1 Allocate a new page if doing so is inexpensive. |
| 380 | ** (createFlag==1 AND bPurgeable AND pDirty) |
| 381 | ** 2 Allocate a new page even it doing so is difficult. |
| 382 | ** (createFlag==1 AND !(bPurgeable AND pDirty) |
| 383 | */ |
drh | c3031c6 | 2014-08-26 15:06:49 +0000 | [diff] [blame] | 384 | eCreate = createFlag & pCache->eCreate; |
| 385 | assert( eCreate==0 || eCreate==1 || eCreate==2 ); |
| 386 | assert( createFlag==0 || pCache->eCreate==eCreate ); |
| 387 | assert( createFlag==0 || eCreate==1+(!pCache->bPurgeable||!pCache->pDirty) ); |
drh | 5c8e092 | 2016-05-11 10:57:04 +0000 | [diff] [blame] | 388 | pRes = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, eCreate); |
| 389 | pcacheTrace(("%p.FETCH %d%s (result: %p)\n",pCache,pgno, |
| 390 | createFlag?" create":"",pRes)); |
| 391 | return pRes; |
drh | bc59ac0 | 2014-08-27 23:18:01 +0000 | [diff] [blame] | 392 | } |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 393 | |
drh | bc59ac0 | 2014-08-27 23:18:01 +0000 | [diff] [blame] | 394 | /* |
| 395 | ** If the sqlite3PcacheFetch() routine is unable to allocate a new |
dan | 41113b6 | 2016-04-05 21:07:58 +0000 | [diff] [blame] | 396 | ** page because no clean pages are available for reuse and the cache |
drh | bc59ac0 | 2014-08-27 23:18:01 +0000 | [diff] [blame] | 397 | ** size limit has been reached, then this routine can be invoked to |
| 398 | ** try harder to allocate a page. This routine might invoke the stress |
| 399 | ** callback to spill dirty pages to the journal. It will then try to |
| 400 | ** allocate the new page and will only fail to allocate a new page on |
| 401 | ** an OOM error. |
| 402 | ** |
| 403 | ** This routine should be invoked only after sqlite3PcacheFetch() fails. |
| 404 | */ |
| 405 | int sqlite3PcacheFetchStress( |
| 406 | PCache *pCache, /* Obtain the page from this cache */ |
| 407 | Pgno pgno, /* Page number to obtain */ |
| 408 | sqlite3_pcache_page **ppPage /* Write result here */ |
| 409 | ){ |
| 410 | PgHdr *pPg; |
| 411 | if( pCache->eCreate==2 ) return 0; |
| 412 | |
drh | 9b0cf34 | 2015-11-12 14:57:19 +0000 | [diff] [blame] | 413 | if( sqlite3PcachePagecount(pCache)>pCache->szSpill ){ |
| 414 | /* Find a dirty page to write-out and recycle. First try to find a |
| 415 | ** page that does not require a journal-sync (one with PGHDR_NEED_SYNC |
| 416 | ** cleared), but if that is not possible settle for any other |
| 417 | ** unreferenced dirty page. |
dan | b2ef900 | 2016-05-11 15:41:15 +0000 | [diff] [blame] | 418 | ** |
| 419 | ** If the LRU page in the dirty list that has a clear PGHDR_NEED_SYNC |
| 420 | ** flag is currently referenced, then the following may leave pSynced |
| 421 | ** set incorrectly (pointing to other than the LRU page with NEED_SYNC |
| 422 | ** cleared). This is Ok, as pSynced is just an optimization. */ |
drh | 9b0cf34 | 2015-11-12 14:57:19 +0000 | [diff] [blame] | 423 | for(pPg=pCache->pSynced; |
| 424 | pPg && (pPg->nRef || (pPg->flags&PGHDR_NEED_SYNC)); |
| 425 | pPg=pPg->pDirtyPrev |
| 426 | ); |
| 427 | pCache->pSynced = pPg; |
| 428 | if( !pPg ){ |
| 429 | for(pPg=pCache->pDirtyTail; pPg && pPg->nRef; pPg=pPg->pDirtyPrev); |
| 430 | } |
| 431 | if( pPg ){ |
| 432 | int rc; |
drh | c97125e | 2011-05-28 15:53:07 +0000 | [diff] [blame] | 433 | #ifdef SQLITE_LOG_CACHE_SPILL |
drh | 9b0cf34 | 2015-11-12 14:57:19 +0000 | [diff] [blame] | 434 | sqlite3_log(SQLITE_FULL, |
| 435 | "spill page %d making room for %d - cache used: %d/%d", |
| 436 | pPg->pgno, pgno, |
| 437 | sqlite3GlobalConfig.pcache.xPagecount(pCache->pCache), |
drh | bc59ac0 | 2014-08-27 23:18:01 +0000 | [diff] [blame] | 438 | numberOfCachePages(pCache)); |
drh | c97125e | 2011-05-28 15:53:07 +0000 | [diff] [blame] | 439 | #endif |
drh | 5c8e092 | 2016-05-11 10:57:04 +0000 | [diff] [blame] | 440 | pcacheTrace(("%p.SPILL %d\n",pCache,pPg->pgno)); |
drh | 9b0cf34 | 2015-11-12 14:57:19 +0000 | [diff] [blame] | 441 | rc = pCache->xStress(pCache->pStress, pPg); |
drh | 7aeb216 | 2016-05-13 04:24:25 +0000 | [diff] [blame] | 442 | pcacheDump(pCache); |
drh | 9b0cf34 | 2015-11-12 14:57:19 +0000 | [diff] [blame] | 443 | if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){ |
| 444 | return rc; |
| 445 | } |
danielk1977 | f599a19 | 2008-08-28 10:21:16 +0000 | [diff] [blame] | 446 | } |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 447 | } |
drh | bc59ac0 | 2014-08-27 23:18:01 +0000 | [diff] [blame] | 448 | *ppPage = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, 2); |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 449 | return *ppPage==0 ? SQLITE_NOMEM_BKPT : SQLITE_OK; |
drh | bc59ac0 | 2014-08-27 23:18:01 +0000 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | /* |
| 453 | ** This is a helper routine for sqlite3PcacheFetchFinish() |
| 454 | ** |
| 455 | ** In the uncommon case where the page being fetched has not been |
| 456 | ** initialized, this routine is invoked to do the initialization. |
| 457 | ** This routine is broken out into a separate function since it |
| 458 | ** requires extra stack manipulation that can be avoided in the common |
| 459 | ** case. |
| 460 | */ |
| 461 | static SQLITE_NOINLINE PgHdr *pcacheFetchFinishWithInit( |
| 462 | PCache *pCache, /* Obtain the page from this cache */ |
| 463 | Pgno pgno, /* Page number obtained */ |
| 464 | sqlite3_pcache_page *pPage /* Page obtained by prior PcacheFetch() call */ |
| 465 | ){ |
| 466 | PgHdr *pPgHdr; |
| 467 | assert( pPage!=0 ); |
| 468 | pPgHdr = (PgHdr*)pPage->pExtra; |
| 469 | assert( pPgHdr->pPage==0 ); |
drh | 216b70f | 2016-10-01 20:43:41 +0000 | [diff] [blame] | 470 | memset(&pPgHdr->pDirty, 0, sizeof(PgHdr) - offsetof(PgHdr,pDirty)); |
drh | bc59ac0 | 2014-08-27 23:18:01 +0000 | [diff] [blame] | 471 | pPgHdr->pPage = pPage; |
| 472 | pPgHdr->pData = pPage->pBuf; |
| 473 | pPgHdr->pExtra = (void *)&pPgHdr[1]; |
drh | a2ee589 | 2016-12-09 16:02:00 +0000 | [diff] [blame] | 474 | memset(pPgHdr->pExtra, 0, 8); |
drh | bc59ac0 | 2014-08-27 23:18:01 +0000 | [diff] [blame] | 475 | pPgHdr->pCache = pCache; |
| 476 | pPgHdr->pgno = pgno; |
drh | c78ae91 | 2015-06-29 04:21:15 +0000 | [diff] [blame] | 477 | pPgHdr->flags = PGHDR_CLEAN; |
drh | bc59ac0 | 2014-08-27 23:18:01 +0000 | [diff] [blame] | 478 | return sqlite3PcacheFetchFinish(pCache,pgno,pPage); |
| 479 | } |
| 480 | |
| 481 | /* |
| 482 | ** This routine converts the sqlite3_pcache_page object returned by |
| 483 | ** sqlite3PcacheFetch() into an initialized PgHdr object. This routine |
| 484 | ** must be called after sqlite3PcacheFetch() in order to get a usable |
| 485 | ** result. |
| 486 | */ |
| 487 | PgHdr *sqlite3PcacheFetchFinish( |
| 488 | PCache *pCache, /* Obtain the page from this cache */ |
| 489 | Pgno pgno, /* Page number obtained */ |
| 490 | sqlite3_pcache_page *pPage /* Page obtained by prior PcacheFetch() call */ |
| 491 | ){ |
| 492 | PgHdr *pPgHdr; |
| 493 | |
drh | d8c0ba3 | 2015-06-30 03:57:59 +0000 | [diff] [blame] | 494 | assert( pPage!=0 ); |
drh | bc59ac0 | 2014-08-27 23:18:01 +0000 | [diff] [blame] | 495 | pPgHdr = (PgHdr *)pPage->pExtra; |
| 496 | |
| 497 | if( !pPgHdr->pPage ){ |
| 498 | return pcacheFetchFinishWithInit(pCache, pgno, pPage); |
| 499 | } |
drh | 95a0b37 | 2015-09-03 20:43:55 +0000 | [diff] [blame] | 500 | pCache->nRefSum++; |
drh | bc59ac0 | 2014-08-27 23:18:01 +0000 | [diff] [blame] | 501 | pPgHdr->nRef++; |
drh | a0f6b12 | 2016-05-13 15:22:06 +0000 | [diff] [blame] | 502 | assert( sqlite3PcachePageSanity(pPgHdr) ); |
drh | bc59ac0 | 2014-08-27 23:18:01 +0000 | [diff] [blame] | 503 | return pPgHdr; |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | /* |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 507 | ** Decrement the reference count on a page. If the page is clean and the |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 508 | ** reference count drops to 0, then it is made eligible for recycling. |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 509 | */ |
drh | a8dcba9 | 2014-08-22 20:35:29 +0000 | [diff] [blame] | 510 | void SQLITE_NOINLINE sqlite3PcacheRelease(PgHdr *p){ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 511 | assert( p->nRef>0 ); |
drh | 95a0b37 | 2015-09-03 20:43:55 +0000 | [diff] [blame] | 512 | p->pCache->nRefSum--; |
| 513 | if( (--p->nRef)==0 ){ |
drh | c78ae91 | 2015-06-29 04:21:15 +0000 | [diff] [blame] | 514 | if( p->flags&PGHDR_CLEAN ){ |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 515 | pcacheUnpin(p); |
dan | 82c0447 | 2016-05-12 17:06:04 +0000 | [diff] [blame] | 516 | }else if( p->pDirtyPrev!=0 ){ /*OPTIMIZATION-IF-FALSE*/ |
| 517 | /* Move the page to the head of the dirty list. If p->pDirtyPrev==0, |
| 518 | ** then page p is already at the head of the dirty list and the |
| 519 | ** following call would be a no-op. Hence the OPTIMIZATION-IF-FALSE |
| 520 | ** tag above. */ |
drh | a8dcba9 | 2014-08-22 20:35:29 +0000 | [diff] [blame] | 521 | pcacheManageDirtyList(p, PCACHE_DIRTYLIST_FRONT); |
danielk1977 | d491e1b | 2008-08-26 18:05:48 +0000 | [diff] [blame] | 522 | } |
danielk1977 | 502b743 | 2008-08-25 14:49:42 +0000 | [diff] [blame] | 523 | } |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 524 | } |
| 525 | |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 526 | /* |
| 527 | ** Increase the reference count of a supplied page by 1. |
| 528 | */ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 529 | void sqlite3PcacheRef(PgHdr *p){ |
danielk1977 | 502b743 | 2008-08-25 14:49:42 +0000 | [diff] [blame] | 530 | assert(p->nRef>0); |
drh | a0f6b12 | 2016-05-13 15:22:06 +0000 | [diff] [blame] | 531 | assert( sqlite3PcachePageSanity(p) ); |
danielk1977 | 502b743 | 2008-08-25 14:49:42 +0000 | [diff] [blame] | 532 | p->nRef++; |
drh | 95a0b37 | 2015-09-03 20:43:55 +0000 | [diff] [blame] | 533 | p->pCache->nRefSum++; |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | /* |
danielk1977 | 4abdfa4 | 2008-08-27 09:44:39 +0000 | [diff] [blame] | 537 | ** Drop a page from the cache. There must be exactly one reference to the |
| 538 | ** page. This function deletes that reference, so after it returns the |
| 539 | ** page pointed to by p is invalid. |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 540 | */ |
| 541 | void sqlite3PcacheDrop(PgHdr *p){ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 542 | assert( p->nRef==1 ); |
drh | a0f6b12 | 2016-05-13 15:22:06 +0000 | [diff] [blame] | 543 | assert( sqlite3PcachePageSanity(p) ); |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 544 | if( p->flags&PGHDR_DIRTY ){ |
drh | a8dcba9 | 2014-08-22 20:35:29 +0000 | [diff] [blame] | 545 | pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE); |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 546 | } |
drh | 95a0b37 | 2015-09-03 20:43:55 +0000 | [diff] [blame] | 547 | p->pCache->nRefSum--; |
drh | a8dcba9 | 2014-08-22 20:35:29 +0000 | [diff] [blame] | 548 | sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 1); |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | /* |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 552 | ** Make sure the page is marked as dirty. If it isn't dirty already, |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 553 | ** make it so. |
| 554 | */ |
| 555 | void sqlite3PcacheMakeDirty(PgHdr *p){ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 556 | assert( p->nRef>0 ); |
drh | a0f6b12 | 2016-05-13 15:22:06 +0000 | [diff] [blame] | 557 | assert( sqlite3PcachePageSanity(p) ); |
dan | 82c0447 | 2016-05-12 17:06:04 +0000 | [diff] [blame] | 558 | if( p->flags & (PGHDR_CLEAN|PGHDR_DONT_WRITE) ){ /*OPTIMIZATION-IF-FALSE*/ |
drh | c78ae91 | 2015-06-29 04:21:15 +0000 | [diff] [blame] | 559 | p->flags &= ~PGHDR_DONT_WRITE; |
| 560 | if( p->flags & PGHDR_CLEAN ){ |
| 561 | p->flags ^= (PGHDR_DIRTY|PGHDR_CLEAN); |
drh | 5c8e092 | 2016-05-11 10:57:04 +0000 | [diff] [blame] | 562 | pcacheTrace(("%p.DIRTY %d\n",p->pCache,p->pgno)); |
drh | c78ae91 | 2015-06-29 04:21:15 +0000 | [diff] [blame] | 563 | assert( (p->flags & (PGHDR_DIRTY|PGHDR_CLEAN))==PGHDR_DIRTY ); |
| 564 | pcacheManageDirtyList(p, PCACHE_DIRTYLIST_ADD); |
| 565 | } |
drh | a0f6b12 | 2016-05-13 15:22:06 +0000 | [diff] [blame] | 566 | assert( sqlite3PcachePageSanity(p) ); |
danielk1977 | d491e1b | 2008-08-26 18:05:48 +0000 | [diff] [blame] | 567 | } |
danielk1977 | f599a19 | 2008-08-28 10:21:16 +0000 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | /* |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 571 | ** Make sure the page is marked as clean. If it isn't clean already, |
danielk1977 | f599a19 | 2008-08-28 10:21:16 +0000 | [diff] [blame] | 572 | ** make it so. |
| 573 | */ |
| 574 | void sqlite3PcacheMakeClean(PgHdr *p){ |
drh | a0f6b12 | 2016-05-13 15:22:06 +0000 | [diff] [blame] | 575 | assert( sqlite3PcachePageSanity(p) ); |
drh | 42bee5f | 2016-05-12 12:01:20 +0000 | [diff] [blame] | 576 | if( ALWAYS((p->flags & PGHDR_DIRTY)!=0) ){ |
drh | c78ae91 | 2015-06-29 04:21:15 +0000 | [diff] [blame] | 577 | assert( (p->flags & PGHDR_CLEAN)==0 ); |
drh | a8dcba9 | 2014-08-22 20:35:29 +0000 | [diff] [blame] | 578 | pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE); |
drh | 1aacbdb | 2015-06-29 18:29:10 +0000 | [diff] [blame] | 579 | p->flags &= ~(PGHDR_DIRTY|PGHDR_NEED_SYNC|PGHDR_WRITEABLE); |
drh | c78ae91 | 2015-06-29 04:21:15 +0000 | [diff] [blame] | 580 | p->flags |= PGHDR_CLEAN; |
drh | 5c8e092 | 2016-05-11 10:57:04 +0000 | [diff] [blame] | 581 | pcacheTrace(("%p.CLEAN %d\n",p->pCache,p->pgno)); |
drh | a0f6b12 | 2016-05-13 15:22:06 +0000 | [diff] [blame] | 582 | assert( sqlite3PcachePageSanity(p) ); |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 583 | if( p->nRef==0 ){ |
| 584 | pcacheUnpin(p); |
| 585 | } |
danielk1977 | f599a19 | 2008-08-28 10:21:16 +0000 | [diff] [blame] | 586 | } |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | /* |
| 590 | ** Make every page in the cache clean. |
| 591 | */ |
| 592 | void sqlite3PcacheCleanAll(PCache *pCache){ |
| 593 | PgHdr *p; |
drh | 7aeb216 | 2016-05-13 04:24:25 +0000 | [diff] [blame] | 594 | pcacheTrace(("%p.CLEAN-ALL\n",pCache)); |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 595 | while( (p = pCache->pDirty)!=0 ){ |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 596 | sqlite3PcacheMakeClean(p); |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 597 | } |
| 598 | } |
| 599 | |
| 600 | /* |
dan | 41113b6 | 2016-04-05 21:07:58 +0000 | [diff] [blame] | 601 | ** Clear the PGHDR_NEED_SYNC and PGHDR_WRITEABLE flag from all dirty pages. |
| 602 | */ |
| 603 | void sqlite3PcacheClearWritable(PCache *pCache){ |
| 604 | PgHdr *p; |
drh | 7aeb216 | 2016-05-13 04:24:25 +0000 | [diff] [blame] | 605 | pcacheTrace(("%p.CLEAR-WRITEABLE\n",pCache)); |
dan | 41113b6 | 2016-04-05 21:07:58 +0000 | [diff] [blame] | 606 | for(p=pCache->pDirty; p; p=p->pDirtyNext){ |
| 607 | p->flags &= ~(PGHDR_NEED_SYNC|PGHDR_WRITEABLE); |
| 608 | } |
| 609 | pCache->pSynced = pCache->pDirtyTail; |
| 610 | } |
| 611 | |
| 612 | /* |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 613 | ** Clear the PGHDR_NEED_SYNC flag from all dirty pages. |
| 614 | */ |
| 615 | void sqlite3PcacheClearSyncFlags(PCache *pCache){ |
| 616 | PgHdr *p; |
| 617 | for(p=pCache->pDirty; p; p=p->pDirtyNext){ |
| 618 | p->flags &= ~PGHDR_NEED_SYNC; |
| 619 | } |
| 620 | pCache->pSynced = pCache->pDirtyTail; |
| 621 | } |
| 622 | |
| 623 | /* |
| 624 | ** Change the page number of page p to newPgno. |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 625 | */ |
| 626 | void sqlite3PcacheMove(PgHdr *p, Pgno newPgno){ |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 627 | PCache *pCache = p->pCache; |
danielk1977 | d491e1b | 2008-08-26 18:05:48 +0000 | [diff] [blame] | 628 | assert( p->nRef>0 ); |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 629 | assert( newPgno>0 ); |
drh | a0f6b12 | 2016-05-13 15:22:06 +0000 | [diff] [blame] | 630 | assert( sqlite3PcachePageSanity(p) ); |
drh | 5c8e092 | 2016-05-11 10:57:04 +0000 | [diff] [blame] | 631 | pcacheTrace(("%p.MOVE %d -> %d\n",pCache,p->pgno,newPgno)); |
dan | 22e21ff | 2011-11-08 20:08:44 +0000 | [diff] [blame] | 632 | sqlite3GlobalConfig.pcache2.xRekey(pCache->pCache, p->pPage, p->pgno,newPgno); |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 633 | p->pgno = newPgno; |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 634 | if( (p->flags&PGHDR_DIRTY) && (p->flags&PGHDR_NEED_SYNC) ){ |
drh | a8dcba9 | 2014-08-22 20:35:29 +0000 | [diff] [blame] | 635 | pcacheManageDirtyList(p, PCACHE_DIRTYLIST_FRONT); |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 636 | } |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | /* |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 640 | ** Drop every cache entry whose page number is greater than "pgno". The |
| 641 | ** caller must ensure that there are no outstanding references to any pages |
| 642 | ** other than page 1 with a page number greater than pgno. |
| 643 | ** |
| 644 | ** If there is a reference to page 1 and the pgno parameter passed to this |
| 645 | ** function is 0, then the data area associated with page 1 is zeroed, but |
| 646 | ** the page object is not dropped. |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 647 | */ |
| 648 | void sqlite3PcacheTruncate(PCache *pCache, Pgno pgno){ |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 649 | if( pCache->pCache ){ |
| 650 | PgHdr *p; |
| 651 | PgHdr *pNext; |
drh | 5c8e092 | 2016-05-11 10:57:04 +0000 | [diff] [blame] | 652 | pcacheTrace(("%p.TRUNCATE %d\n",pCache,pgno)); |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 653 | for(p=pCache->pDirty; p; p=pNext){ |
| 654 | pNext = p->pDirtyNext; |
drh | f3609ee | 2010-03-19 19:23:51 +0000 | [diff] [blame] | 655 | /* This routine never gets call with a positive pgno except right |
| 656 | ** after sqlite3PcacheCleanAll(). So if there are dirty pages, |
| 657 | ** it must be that pgno==0. |
| 658 | */ |
| 659 | assert( p->pgno>0 ); |
dan | 41113b6 | 2016-04-05 21:07:58 +0000 | [diff] [blame] | 660 | if( p->pgno>pgno ){ |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 661 | assert( p->flags&PGHDR_DIRTY ); |
| 662 | sqlite3PcacheMakeClean(p); |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 663 | } |
| 664 | } |
drh | 95a0b37 | 2015-09-03 20:43:55 +0000 | [diff] [blame] | 665 | if( pgno==0 && pCache->nRefSum ){ |
drh | 39065c6 | 2015-06-26 02:41:31 +0000 | [diff] [blame] | 666 | sqlite3_pcache_page *pPage1; |
| 667 | pPage1 = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache,1,0); |
| 668 | if( ALWAYS(pPage1) ){ /* Page 1 is always available in cache, because |
drh | 95a0b37 | 2015-09-03 20:43:55 +0000 | [diff] [blame] | 669 | ** pCache->nRefSum>0 */ |
drh | 39065c6 | 2015-06-26 02:41:31 +0000 | [diff] [blame] | 670 | memset(pPage1->pBuf, 0, pCache->szPage); |
| 671 | pgno = 1; |
| 672 | } |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 673 | } |
dan | 22e21ff | 2011-11-08 20:08:44 +0000 | [diff] [blame] | 674 | sqlite3GlobalConfig.pcache2.xTruncate(pCache->pCache, pgno+1); |
danielk1977 | 062d4cb | 2008-08-29 09:10:02 +0000 | [diff] [blame] | 675 | } |
| 676 | } |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 677 | |
| 678 | /* |
| 679 | ** Close a cache. |
| 680 | */ |
| 681 | void sqlite3PcacheClose(PCache *pCache){ |
drh | c3031c6 | 2014-08-26 15:06:49 +0000 | [diff] [blame] | 682 | assert( pCache->pCache!=0 ); |
drh | 5c8e092 | 2016-05-11 10:57:04 +0000 | [diff] [blame] | 683 | pcacheTrace(("%p.CLOSE\n",pCache)); |
drh | c3031c6 | 2014-08-26 15:06:49 +0000 | [diff] [blame] | 684 | sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache); |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | /* |
| 688 | ** Discard the contents of the cache. |
| 689 | */ |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 690 | void sqlite3PcacheClear(PCache *pCache){ |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 691 | sqlite3PcacheTruncate(pCache, 0); |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | /* |
| 695 | ** Merge two lists of pages connected by pDirty and in pgno order. |
mistachkin | 2380f3f | 2016-05-20 20:58:30 +0000 | [diff] [blame] | 696 | ** Do not bother fixing the pDirtyPrev pointers. |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 697 | */ |
| 698 | static PgHdr *pcacheMergeDirtyList(PgHdr *pA, PgHdr *pB){ |
| 699 | PgHdr result, *pTail; |
| 700 | pTail = &result; |
drh | b982bfe | 2016-05-20 14:54:54 +0000 | [diff] [blame] | 701 | assert( pA!=0 && pB!=0 ); |
| 702 | for(;;){ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 703 | if( pA->pgno<pB->pgno ){ |
| 704 | pTail->pDirty = pA; |
| 705 | pTail = pA; |
| 706 | pA = pA->pDirty; |
drh | b982bfe | 2016-05-20 14:54:54 +0000 | [diff] [blame] | 707 | if( pA==0 ){ |
| 708 | pTail->pDirty = pB; |
| 709 | break; |
| 710 | } |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 711 | }else{ |
| 712 | pTail->pDirty = pB; |
| 713 | pTail = pB; |
| 714 | pB = pB->pDirty; |
drh | b982bfe | 2016-05-20 14:54:54 +0000 | [diff] [blame] | 715 | if( pB==0 ){ |
| 716 | pTail->pDirty = pA; |
| 717 | break; |
| 718 | } |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 719 | } |
| 720 | } |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 721 | return result.pDirty; |
| 722 | } |
| 723 | |
| 724 | /* |
| 725 | ** Sort the list of pages in accending order by pgno. Pages are |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 726 | ** connected by pDirty pointers. The pDirtyPrev pointers are |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 727 | ** corrupted by this sort. |
drh | e64ca7b | 2009-07-16 18:21:17 +0000 | [diff] [blame] | 728 | ** |
| 729 | ** Since there cannot be more than 2^31 distinct pages in a database, |
| 730 | ** there cannot be more than 31 buckets required by the merge sorter. |
| 731 | ** One extra bucket is added to catch overflow in case something |
| 732 | ** ever changes to make the previous sentence incorrect. |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 733 | */ |
drh | e64ca7b | 2009-07-16 18:21:17 +0000 | [diff] [blame] | 734 | #define N_SORT_BUCKET 32 |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 735 | static PgHdr *pcacheSortDirtyList(PgHdr *pIn){ |
drh | e64ca7b | 2009-07-16 18:21:17 +0000 | [diff] [blame] | 736 | PgHdr *a[N_SORT_BUCKET], *p; |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 737 | int i; |
| 738 | memset(a, 0, sizeof(a)); |
| 739 | while( pIn ){ |
| 740 | p = pIn; |
| 741 | pIn = p->pDirty; |
| 742 | p->pDirty = 0; |
drh | e64ca7b | 2009-07-16 18:21:17 +0000 | [diff] [blame] | 743 | for(i=0; ALWAYS(i<N_SORT_BUCKET-1); i++){ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 744 | if( a[i]==0 ){ |
| 745 | a[i] = p; |
| 746 | break; |
| 747 | }else{ |
| 748 | p = pcacheMergeDirtyList(a[i], p); |
| 749 | a[i] = 0; |
| 750 | } |
| 751 | } |
drh | e64ca7b | 2009-07-16 18:21:17 +0000 | [diff] [blame] | 752 | if( NEVER(i==N_SORT_BUCKET-1) ){ |
| 753 | /* To get here, there need to be 2^(N_SORT_BUCKET) elements in |
| 754 | ** the input list. But that is impossible. |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 755 | */ |
| 756 | a[i] = pcacheMergeDirtyList(a[i], p); |
| 757 | } |
| 758 | } |
| 759 | p = a[0]; |
| 760 | for(i=1; i<N_SORT_BUCKET; i++){ |
drh | b982bfe | 2016-05-20 14:54:54 +0000 | [diff] [blame] | 761 | if( a[i]==0 ) continue; |
| 762 | p = p ? pcacheMergeDirtyList(p, a[i]) : a[i]; |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 763 | } |
| 764 | return p; |
| 765 | } |
| 766 | |
| 767 | /* |
| 768 | ** Return a list of all dirty pages in the cache, sorted by page number. |
| 769 | */ |
| 770 | PgHdr *sqlite3PcacheDirtyList(PCache *pCache){ |
| 771 | PgHdr *p; |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 772 | for(p=pCache->pDirty; p; p=p->pDirtyNext){ |
| 773 | p->pDirty = p->pDirtyNext; |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 774 | } |
| 775 | return pcacheSortDirtyList(pCache->pDirty); |
| 776 | } |
| 777 | |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 778 | /* |
drh | 95a0b37 | 2015-09-03 20:43:55 +0000 | [diff] [blame] | 779 | ** Return the total number of references to all pages held by the cache. |
| 780 | ** |
| 781 | ** This is not the total number of pages referenced, but the sum of the |
| 782 | ** reference count for all pages. |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 783 | */ |
| 784 | int sqlite3PcacheRefCount(PCache *pCache){ |
drh | 95a0b37 | 2015-09-03 20:43:55 +0000 | [diff] [blame] | 785 | return pCache->nRefSum; |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 786 | } |
| 787 | |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 788 | /* |
| 789 | ** Return the number of references to the page supplied as an argument. |
| 790 | */ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 791 | int sqlite3PcachePageRefcount(PgHdr *p){ |
| 792 | return p->nRef; |
| 793 | } |
| 794 | |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 795 | /* |
| 796 | ** Return the total number of pages in the cache. |
| 797 | */ |
| 798 | int sqlite3PcachePagecount(PCache *pCache){ |
drh | c3031c6 | 2014-08-26 15:06:49 +0000 | [diff] [blame] | 799 | assert( pCache->pCache!=0 ); |
| 800 | return sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache); |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 801 | } |
| 802 | |
danielk1977 | f3d3c27 | 2008-11-19 16:52:44 +0000 | [diff] [blame] | 803 | #ifdef SQLITE_TEST |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 804 | /* |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 805 | ** Get the suggested cache-size value. |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 806 | */ |
| 807 | int sqlite3PcacheGetCachesize(PCache *pCache){ |
drh | 3b42abb | 2011-11-09 14:23:04 +0000 | [diff] [blame] | 808 | return numberOfCachePages(pCache); |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 809 | } |
danielk1977 | f3d3c27 | 2008-11-19 16:52:44 +0000 | [diff] [blame] | 810 | #endif |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 811 | |
| 812 | /* |
| 813 | ** Set the suggested cache-size value. |
| 814 | */ |
| 815 | void sqlite3PcacheSetCachesize(PCache *pCache, int mxPage){ |
drh | c3031c6 | 2014-08-26 15:06:49 +0000 | [diff] [blame] | 816 | assert( pCache->pCache!=0 ); |
drh | 3b42abb | 2011-11-09 14:23:04 +0000 | [diff] [blame] | 817 | pCache->szCache = mxPage; |
drh | c3031c6 | 2014-08-26 15:06:49 +0000 | [diff] [blame] | 818 | sqlite3GlobalConfig.pcache2.xCachesize(pCache->pCache, |
| 819 | numberOfCachePages(pCache)); |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 820 | } |
| 821 | |
drh | 09419b4 | 2011-11-16 19:29:17 +0000 | [diff] [blame] | 822 | /* |
drh | 9b0cf34 | 2015-11-12 14:57:19 +0000 | [diff] [blame] | 823 | ** Set the suggested cache-spill value. Make no changes if if the |
| 824 | ** argument is zero. Return the effective cache-spill size, which will |
| 825 | ** be the larger of the szSpill and szCache. |
| 826 | */ |
| 827 | int sqlite3PcacheSetSpillsize(PCache *p, int mxPage){ |
| 828 | int res; |
| 829 | assert( p->pCache!=0 ); |
| 830 | if( mxPage ){ |
| 831 | if( mxPage<0 ){ |
drh | 4f9c8ec | 2015-11-12 15:47:48 +0000 | [diff] [blame] | 832 | mxPage = (int)((-1024*(i64)mxPage)/(p->szPage+p->szExtra)); |
drh | 9b0cf34 | 2015-11-12 14:57:19 +0000 | [diff] [blame] | 833 | } |
| 834 | p->szSpill = mxPage; |
| 835 | } |
| 836 | res = numberOfCachePages(p); |
| 837 | if( res<p->szSpill ) res = p->szSpill; |
| 838 | return res; |
| 839 | } |
| 840 | |
| 841 | /* |
drh | 09419b4 | 2011-11-16 19:29:17 +0000 | [diff] [blame] | 842 | ** Free up as much memory as possible from the page cache. |
| 843 | */ |
| 844 | void sqlite3PcacheShrink(PCache *pCache){ |
drh | c3031c6 | 2014-08-26 15:06:49 +0000 | [diff] [blame] | 845 | assert( pCache->pCache!=0 ); |
| 846 | sqlite3GlobalConfig.pcache2.xShrink(pCache->pCache); |
drh | 09419b4 | 2011-11-16 19:29:17 +0000 | [diff] [blame] | 847 | } |
| 848 | |
drh | def6889 | 2014-11-04 12:11:23 +0000 | [diff] [blame] | 849 | /* |
| 850 | ** Return the size of the header added by this middleware layer |
| 851 | ** in the page-cache hierarchy. |
| 852 | */ |
drh | 37c057b | 2014-12-30 00:57:29 +0000 | [diff] [blame] | 853 | int sqlite3HeaderSizePcache(void){ return ROUND8(sizeof(PgHdr)); } |
drh | def6889 | 2014-11-04 12:11:23 +0000 | [diff] [blame] | 854 | |
dan | 0f52455 | 2016-04-13 16:52:11 +0000 | [diff] [blame] | 855 | /* |
| 856 | ** Return the number of dirty pages currently in the cache, as a percentage |
| 857 | ** of the configured cache size. |
| 858 | */ |
| 859 | int sqlite3PCachePercentDirty(PCache *pCache){ |
| 860 | PgHdr *pDirty; |
| 861 | int nDirty = 0; |
| 862 | int nCache = numberOfCachePages(pCache); |
| 863 | for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext) nDirty++; |
drh | b5895e5 | 2016-04-18 13:30:50 +0000 | [diff] [blame] | 864 | return nCache ? (int)(((i64)nDirty * 100) / nCache) : 0; |
dan | 0f52455 | 2016-04-13 16:52:11 +0000 | [diff] [blame] | 865 | } |
drh | def6889 | 2014-11-04 12:11:23 +0000 | [diff] [blame] | 866 | |
danielk1977 | 750e87d | 2009-07-25 11:46:48 +0000 | [diff] [blame] | 867 | #if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG) |
danielk1977 | 67e3da7 | 2008-08-21 12:19:44 +0000 | [diff] [blame] | 868 | /* |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 869 | ** For all dirty pages currently in the cache, invoke the specified |
| 870 | ** callback. This is only used if the SQLITE_CHECK_PAGES macro is |
| 871 | ** defined. |
danielk1977 | 67e3da7 | 2008-08-21 12:19:44 +0000 | [diff] [blame] | 872 | */ |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 873 | void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *)){ |
| 874 | PgHdr *pDirty; |
| 875 | for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext){ |
| 876 | xIter(pDirty); |
danielk1977 | 67e3da7 | 2008-08-21 12:19:44 +0000 | [diff] [blame] | 877 | } |
danielk1977 | 062d4cb | 2008-08-29 09:10:02 +0000 | [diff] [blame] | 878 | } |
| 879 | #endif |