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