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