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