xref: /sqlite-3.40.0/src/pcache.h (revision 09236755)
18c0a791aSdanielk1977 /*
28c0a791aSdanielk1977 ** 2008 August 05
38c0a791aSdanielk1977 **
48c0a791aSdanielk1977 ** The author disclaims copyright to this source code.  In place of
58c0a791aSdanielk1977 ** a legal notice, here is a blessing:
68c0a791aSdanielk1977 **
78c0a791aSdanielk1977 **    May you do good and not evil.
88c0a791aSdanielk1977 **    May you find forgiveness for yourself and forgive others.
98c0a791aSdanielk1977 **    May you share freely, never taking more than you give.
108c0a791aSdanielk1977 **
118c0a791aSdanielk1977 *************************************************************************
128c0a791aSdanielk1977 ** This header file defines the interface that the sqlite page cache
138c0a791aSdanielk1977 ** subsystem.
148c0a791aSdanielk1977 */
158c0a791aSdanielk1977 
168c0a791aSdanielk1977 #ifndef _PCACHE_H_
178c0a791aSdanielk1977 
188c0a791aSdanielk1977 typedef struct PgHdr PgHdr;
198c0a791aSdanielk1977 typedef struct PCache PCache;
208c0a791aSdanielk1977 
218c0a791aSdanielk1977 /*
228c0a791aSdanielk1977 ** Every page in the cache is controlled by an instance of the following
238c0a791aSdanielk1977 ** structure.
248c0a791aSdanielk1977 */
258c0a791aSdanielk1977 struct PgHdr {
2622e21ff4Sdan   sqlite3_pcache_page *pPage;    /* Pcache object page handle */
2722e21ff4Sdan   void *pData;                   /* Page data */
288c0a791aSdanielk1977   void *pExtra;                  /* Extra content */
2902f18cc5Sdrh   PCache *pCache;                /* PRIVATE: Cache that owns this page */
3072e6a399Sdrh   PgHdr *pDirty;                 /* Transient list of dirty sorted by pgno */
31a85f7e36Sdrh   Pager *pPager;                 /* The pager this page is part of */
32a451017dSdrh   Pgno pgno;                     /* Page number for this page */
338c0a791aSdanielk1977 #ifdef SQLITE_CHECK_PAGES
34a85f7e36Sdrh   u32 pageHash;                  /* Hash of page content */
358c0a791aSdanielk1977 #endif
36a85f7e36Sdrh   u16 flags;                     /* PGHDR flags defined below */
37bc2ca9ebSdanielk1977 
38a85f7e36Sdrh   /**********************************************************************
3902f18cc5Sdrh   ** Elements above, except pCache, are public.  All that follow are
4002f18cc5Sdrh   ** private to pcache.c and should not be accessed by other modules.
4102f18cc5Sdrh   ** pCache is grouped with the public elements for efficiency.
42a85f7e36Sdrh   */
43a85f7e36Sdrh   i16 nRef;                      /* Number of users of this page */
44bc2ca9ebSdanielk1977   PgHdr *pDirtyNext;             /* Next element in list of dirty pages */
45bc2ca9ebSdanielk1977   PgHdr *pDirtyPrev;             /* Previous element in list of dirty pages */
46f0dae6d0Sdrh                           /* NB: pDirtyNext and pDirtyPrev are undefined if the
47f0dae6d0Sdrh                           ** PgHdr object is not dirty */
488c0a791aSdanielk1977 };
498c0a791aSdanielk1977 
508c0a791aSdanielk1977 /* Bit values for PgHdr.flags */
511aacbdb3Sdrh #define PGHDR_CLEAN           0x001  /* Page not on the PCache.pDirty list */
521aacbdb3Sdrh #define PGHDR_DIRTY           0x002  /* Page is on the PCache.pDirty list */
531aacbdb3Sdrh #define PGHDR_WRITEABLE       0x004  /* Journaled and ready to modify */
541aacbdb3Sdrh #define PGHDR_NEED_SYNC       0x008  /* Fsync the rollback journal before
55b3df2e1cSdrh                                      ** writing this page to the database */
56a0f6b124Sdrh #define PGHDR_DONT_WRITE      0x010  /* Do not write content to disk */
57a0f6b124Sdrh #define PGHDR_MMAP            0x020  /* This is an mmap page object */
58b2d3de3bSdan 
59a0f6b124Sdrh #define PGHDR_WAL_APPEND      0x040  /* Appended to wal file */
60d6f7c979Sdan 
618c0a791aSdanielk1977 /* Initialize and shutdown the page cache subsystem */
628c0a791aSdanielk1977 int sqlite3PcacheInitialize(void);
638c0a791aSdanielk1977 void sqlite3PcacheShutdown(void);
648c0a791aSdanielk1977 
658c0a791aSdanielk1977 /* Page cache buffer management:
668c0a791aSdanielk1977 ** These routines implement SQLITE_CONFIG_PAGECACHE.
678c0a791aSdanielk1977 */
688c0a791aSdanielk1977 void sqlite3PCacheBufferSetup(void *, int sz, int n);
698c0a791aSdanielk1977 
708c0a791aSdanielk1977 /* Create a new pager cache.
718c0a791aSdanielk1977 ** Under memory stress, invoke xStress to try to make pages clean.
728c0a791aSdanielk1977 ** Only clean and unpinned pages can be reclaimed.
738c0a791aSdanielk1977 */
74c3031c61Sdrh int sqlite3PcacheOpen(
758c0a791aSdanielk1977   int szPage,                    /* Size of every page */
768c0a791aSdanielk1977   int szExtra,                   /* Extra space associated with each page */
778c0a791aSdanielk1977   int bPurgeable,                /* True if pages are on backing store */
78a858aa2eSdanielk1977   int (*xStress)(void*, PgHdr*), /* Call to try to make pages clean */
798c0a791aSdanielk1977   void *pStress,                 /* Argument to xStress */
808c0a791aSdanielk1977   PCache *pToInit                /* Preallocated space for the PCache */
818c0a791aSdanielk1977 );
828c0a791aSdanielk1977 
838c0a791aSdanielk1977 /* Modify the page-size after the cache has been created. */
84c3031c61Sdrh int sqlite3PcacheSetPageSize(PCache *, int);
858c0a791aSdanielk1977 
868c0a791aSdanielk1977 /* Return the size in bytes of a PCache object.  Used to preallocate
878c0a791aSdanielk1977 ** storage space.
888c0a791aSdanielk1977 */
898c0a791aSdanielk1977 int sqlite3PcacheSize(void);
908c0a791aSdanielk1977 
918c0a791aSdanielk1977 /* One release per successful fetch.  Page is pinned until released.
928c0a791aSdanielk1977 ** Reference counted.
938c0a791aSdanielk1977 */
94bc59ac0eSdrh sqlite3_pcache_page *sqlite3PcacheFetch(PCache*, Pgno, int createFlag);
95bc59ac0eSdrh int sqlite3PcacheFetchStress(PCache*, Pgno, sqlite3_pcache_page**);
96bc59ac0eSdrh PgHdr *sqlite3PcacheFetchFinish(PCache*, Pgno, sqlite3_pcache_page *pPage);
978c0a791aSdanielk1977 void sqlite3PcacheRelease(PgHdr*);
988c0a791aSdanielk1977 
998c0a791aSdanielk1977 void sqlite3PcacheDrop(PgHdr*);         /* Remove page from cache */
1008c0a791aSdanielk1977 void sqlite3PcacheMakeDirty(PgHdr*);    /* Make sure page is marked dirty */
1018c0a791aSdanielk1977 void sqlite3PcacheMakeClean(PgHdr*);    /* Mark a single page as clean */
1028c0a791aSdanielk1977 void sqlite3PcacheCleanAll(PCache*);    /* Mark all dirty list pages as clean */
1036bcfe8b6Sdrh void sqlite3PcacheClearWritable(PCache*);
1048c0a791aSdanielk1977 
1058c0a791aSdanielk1977 /* Change a page number.  Used by incr-vacuum. */
1068c0a791aSdanielk1977 void sqlite3PcacheMove(PgHdr*, Pgno);
1078c0a791aSdanielk1977 
1088c0a791aSdanielk1977 /* Remove all pages with pgno>x.  Reset the cache if x==0 */
1098c0a791aSdanielk1977 void sqlite3PcacheTruncate(PCache*, Pgno x);
1108c0a791aSdanielk1977 
1118c0a791aSdanielk1977 /* Get a list of all dirty pages in the cache, sorted by page number */
1128c0a791aSdanielk1977 PgHdr *sqlite3PcacheDirtyList(PCache*);
1138c0a791aSdanielk1977 
1148c0a791aSdanielk1977 /* Reset and close the cache object */
1158c0a791aSdanielk1977 void sqlite3PcacheClose(PCache*);
1168c0a791aSdanielk1977 
117b3df2e1cSdrh /* Clear flags from pages of the page cache */
118bc2ca9ebSdanielk1977 void sqlite3PcacheClearSyncFlags(PCache *);
1198c0a791aSdanielk1977 
1208c0a791aSdanielk1977 /* Discard the contents of the cache */
121bea2a948Sdanielk1977 void sqlite3PcacheClear(PCache*);
1228c0a791aSdanielk1977 
1238c0a791aSdanielk1977 /* Return the total number of outstanding page references */
1248c0a791aSdanielk1977 int sqlite3PcacheRefCount(PCache*);
1258c0a791aSdanielk1977 
1268c0a791aSdanielk1977 /* Increment the reference count of an existing page */
1278c0a791aSdanielk1977 void sqlite3PcacheRef(PgHdr*);
1288c0a791aSdanielk1977 
12971d5d2cdSdanielk1977 int sqlite3PcachePageRefcount(PgHdr*);
13071d5d2cdSdanielk1977 
1318c0a791aSdanielk1977 /* Return the total number of pages stored in the cache */
1328c0a791aSdanielk1977 int sqlite3PcachePagecount(PCache*);
1338c0a791aSdanielk1977 
134750e87dfSdanielk1977 #if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)
135bc2ca9ebSdanielk1977 /* Iterate through all dirty pages currently stored in the cache. This
136bc2ca9ebSdanielk1977 ** interface is only available if SQLITE_CHECK_PAGES is defined when the
137bc2ca9ebSdanielk1977 ** library is built.
1388c0a791aSdanielk1977 */
139bc2ca9ebSdanielk1977 void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *));
140419fcf66Sdrh #endif
1418c0a791aSdanielk1977 
142a0f6b124Sdrh #if defined(SQLITE_DEBUG)
143a0f6b124Sdrh /* Check invariants on a PgHdr object */
144a0f6b124Sdrh int sqlite3PcachePageSanity(PgHdr*);
145a0f6b124Sdrh #endif
146a0f6b124Sdrh 
147d491e1bfSdanielk1977 /* Set and get the suggested cache-size for the specified pager-cache.
1488c0a791aSdanielk1977 **
1498c0a791aSdanielk1977 ** If no global maximum is configured, then the system attempts to limit
1508c0a791aSdanielk1977 ** the total number of pages cached by purgeable pager-caches to the sum
1518c0a791aSdanielk1977 ** of the suggested cache-sizes.
1528c0a791aSdanielk1977 */
1538c0a791aSdanielk1977 void sqlite3PcacheSetCachesize(PCache *, int);
154f3d3c27aSdanielk1977 #ifdef SQLITE_TEST
155f3d3c27aSdanielk1977 int sqlite3PcacheGetCachesize(PCache *);
156f3d3c27aSdanielk1977 #endif
1578c0a791aSdanielk1977 
1589b0cf34fSdrh /* Set or get the suggested spill-size for the specified pager-cache.
1599b0cf34fSdrh **
1609b0cf34fSdrh ** The spill-size is the minimum number of pages in cache before the cache
1619b0cf34fSdrh ** will attempt to spill dirty pages by calling xStress.
1629b0cf34fSdrh */
1639b0cf34fSdrh int sqlite3PcacheSetSpillsize(PCache *, int);
1649b0cf34fSdrh 
16509419b4bSdrh /* Free up as much memory as possible from the page cache */
16609419b4bSdrh void sqlite3PcacheShrink(PCache*);
16709419b4bSdrh 
168419fcf66Sdrh #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
169d491e1bfSdanielk1977 /* Try to return memory used by the pcache module to the main memory heap */
17067e3da7aSdanielk1977 int sqlite3PcacheReleaseMemory(int);
171419fcf66Sdrh #endif
17267e3da7aSdanielk1977 
173419fcf66Sdrh #ifdef SQLITE_TEST
174062d4cb0Sdanielk1977 void sqlite3PcacheStats(int*,int*,int*,int*);
175419fcf66Sdrh #endif
176062d4cb0Sdanielk1977 
177bc2ca9ebSdanielk1977 void sqlite3PCacheSetDefault(void);
178bc2ca9ebSdanielk1977 
179def6889dSdrh /* Return the header size */
180def6889dSdrh int sqlite3HeaderSizePcache(void);
181def6889dSdrh int sqlite3HeaderSizePcache1(void);
182def6889dSdrh 
1830f52455aSdan /* Number of dirty pages as a percentage of the configured cache size */
1840f52455aSdan int sqlite3PCachePercentDirty(PCache*);
1850f52455aSdan 
186*09236755Sdan #ifdef SQLITE_DIRECT_OVERFLOW_READ
187*09236755Sdan int sqlite3PCacheIsDirty(PCache *pCache);
188*09236755Sdan #endif
189*09236755Sdan 
1908c0a791aSdanielk1977 #endif /* _PCACHE_H_ */
191