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