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