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