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