blob: bbc2cb453984e1e7c732b8a7672dc026998a295c [file] [log] [blame]
danielk19778c0a7912008-08-20 14:49:23 +00001/*
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 header file defines the interface that the sqlite page cache
13** subsystem.
danielk19778c0a7912008-08-20 14:49:23 +000014*/
15
16#ifndef _PCACHE_H_
17
18typedef struct PgHdr PgHdr;
19typedef struct PCache PCache;
20
21/*
22** Every page in the cache is controlled by an instance of the following
23** structure.
24*/
25struct PgHdr {
dan22e21ff2011-11-08 20:08:44 +000026 sqlite3_pcache_page *pPage; /* Pcache object page handle */
27 void *pData; /* Page data */
danielk19778c0a7912008-08-20 14:49:23 +000028 void *pExtra; /* Extra content */
drh02f18cc2017-05-31 03:20:39 +000029 PCache *pCache; /* PRIVATE: Cache that owns this page */
drh72e6a392016-05-11 23:54:14 +000030 PgHdr *pDirty; /* Transient list of dirty sorted by pgno */
drha85f7e32008-08-28 02:26:07 +000031 Pager *pPager; /* The pager this page is part of */
drha4510172012-02-02 15:50:17 +000032 Pgno pgno; /* Page number for this page */
danielk19778c0a7912008-08-20 14:49:23 +000033#ifdef SQLITE_CHECK_PAGES
drha85f7e32008-08-28 02:26:07 +000034 u32 pageHash; /* Hash of page content */
danielk19778c0a7912008-08-20 14:49:23 +000035#endif
drha85f7e32008-08-28 02:26:07 +000036 u16 flags; /* PGHDR flags defined below */
danielk1977bc2ca9e2008-11-13 14:28:28 +000037
drha85f7e32008-08-28 02:26:07 +000038 /**********************************************************************
drh02f18cc2017-05-31 03:20:39 +000039 ** Elements above, except pCache, are public. All that follow are
40 ** private to pcache.c and should not be accessed by other modules.
41 ** pCache is grouped with the public elements for efficiency.
drha85f7e32008-08-28 02:26:07 +000042 */
43 i16 nRef; /* Number of users of this page */
danielk1977bc2ca9e2008-11-13 14:28:28 +000044 PgHdr *pDirtyNext; /* Next element in list of dirty pages */
45 PgHdr *pDirtyPrev; /* Previous element in list of dirty pages */
drhf0dae6d2017-09-01 12:18:41 +000046 /* NB: pDirtyNext and pDirtyPrev are undefined if the
47 ** PgHdr object is not dirty */
danielk19778c0a7912008-08-20 14:49:23 +000048};
49
50/* Bit values for PgHdr.flags */
drh1aacbdb2015-06-29 18:29:10 +000051#define PGHDR_CLEAN 0x001 /* Page not on the PCache.pDirty list */
52#define PGHDR_DIRTY 0x002 /* Page is on the PCache.pDirty list */
53#define PGHDR_WRITEABLE 0x004 /* Journaled and ready to modify */
54#define PGHDR_NEED_SYNC 0x008 /* Fsync the rollback journal before
55 ** writing this page to the database */
drha0f6b122016-05-13 15:22:06 +000056#define PGHDR_DONT_WRITE 0x010 /* Do not write content to disk */
57#define PGHDR_MMAP 0x020 /* This is an mmap page object */
danb2d3de32013-03-14 18:34:37 +000058
drha0f6b122016-05-13 15:22:06 +000059#define PGHDR_WAL_APPEND 0x040 /* Appended to wal file */
dand6f7c972016-01-09 16:39:29 +000060
danielk19778c0a7912008-08-20 14:49:23 +000061/* Initialize and shutdown the page cache subsystem */
62int sqlite3PcacheInitialize(void);
63void sqlite3PcacheShutdown(void);
64
65/* Page cache buffer management:
66** These routines implement SQLITE_CONFIG_PAGECACHE.
67*/
68void sqlite3PCacheBufferSetup(void *, int sz, int n);
danielk19778c0a7912008-08-20 14:49:23 +000069
70/* Create a new pager cache.
71** Under memory stress, invoke xStress to try to make pages clean.
72** Only clean and unpinned pages can be reclaimed.
73*/
drhc3031c62014-08-26 15:06:49 +000074int sqlite3PcacheOpen(
danielk1977a858aa22008-08-22 16:22:17 +000075 int szPage, /* Size of every page */
76 int szExtra, /* Extra space associated with each page */
77 int bPurgeable, /* True if pages are on backing store */
danielk1977a858aa22008-08-22 16:22:17 +000078 int (*xStress)(void*, PgHdr*), /* Call to try to make pages clean */
79 void *pStress, /* Argument to xStress */
80 PCache *pToInit /* Preallocated space for the PCache */
danielk19778c0a7912008-08-20 14:49:23 +000081);
82
83/* Modify the page-size after the cache has been created. */
drhc3031c62014-08-26 15:06:49 +000084int sqlite3PcacheSetPageSize(PCache *, int);
danielk19778c0a7912008-08-20 14:49:23 +000085
86/* Return the size in bytes of a PCache object. Used to preallocate
87** storage space.
88*/
89int sqlite3PcacheSize(void);
90
91/* One release per successful fetch. Page is pinned until released.
92** Reference counted.
93*/
drhbc59ac02014-08-27 23:18:01 +000094sqlite3_pcache_page *sqlite3PcacheFetch(PCache*, Pgno, int createFlag);
95int sqlite3PcacheFetchStress(PCache*, Pgno, sqlite3_pcache_page**);
96PgHdr *sqlite3PcacheFetchFinish(PCache*, Pgno, sqlite3_pcache_page *pPage);
danielk19778c0a7912008-08-20 14:49:23 +000097void sqlite3PcacheRelease(PgHdr*);
98
99void sqlite3PcacheDrop(PgHdr*); /* Remove page from cache */
100void sqlite3PcacheMakeDirty(PgHdr*); /* Make sure page is marked dirty */
101void sqlite3PcacheMakeClean(PgHdr*); /* Mark a single page as clean */
102void sqlite3PcacheCleanAll(PCache*); /* Mark all dirty list pages as clean */
drh6bcfe8b2016-04-21 15:24:46 +0000103void sqlite3PcacheClearWritable(PCache*);
danielk19778c0a7912008-08-20 14:49:23 +0000104
105/* Change a page number. Used by incr-vacuum. */
106void sqlite3PcacheMove(PgHdr*, Pgno);
107
danielk19778c0a7912008-08-20 14:49:23 +0000108/* Remove all pages with pgno>x. Reset the cache if x==0 */
109void sqlite3PcacheTruncate(PCache*, Pgno x);
110
danielk19778c0a7912008-08-20 14:49:23 +0000111/* Get a list of all dirty pages in the cache, sorted by page number */
112PgHdr *sqlite3PcacheDirtyList(PCache*);
113
danielk19778c0a7912008-08-20 14:49:23 +0000114/* Reset and close the cache object */
115void sqlite3PcacheClose(PCache*);
116
drhb3df2e12008-09-17 20:06:26 +0000117/* Clear flags from pages of the page cache */
danielk1977bc2ca9e2008-11-13 14:28:28 +0000118void sqlite3PcacheClearSyncFlags(PCache *);
danielk19778c0a7912008-08-20 14:49:23 +0000119
danielk19778c0a7912008-08-20 14:49:23 +0000120/* Discard the contents of the cache */
danielk1977bea2a942009-01-20 17:06:27 +0000121void sqlite3PcacheClear(PCache*);
danielk19778c0a7912008-08-20 14:49:23 +0000122
123/* Return the total number of outstanding page references */
124int sqlite3PcacheRefCount(PCache*);
125
126/* Increment the reference count of an existing page */
127void sqlite3PcacheRef(PgHdr*);
128
danielk197771d5d2c2008-09-29 11:49:47 +0000129int sqlite3PcachePageRefcount(PgHdr*);
130
danielk19778c0a7912008-08-20 14:49:23 +0000131/* Return the total number of pages stored in the cache */
132int sqlite3PcachePagecount(PCache*);
133
danielk1977750e87d2009-07-25 11:46:48 +0000134#if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)
danielk1977bc2ca9e2008-11-13 14:28:28 +0000135/* Iterate through all dirty pages currently stored in the cache. This
136** interface is only available if SQLITE_CHECK_PAGES is defined when the
137** library is built.
danielk19778c0a7912008-08-20 14:49:23 +0000138*/
danielk1977bc2ca9e2008-11-13 14:28:28 +0000139void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *));
drh419fcf62008-10-11 17:42:28 +0000140#endif
danielk19778c0a7912008-08-20 14:49:23 +0000141
drha0f6b122016-05-13 15:22:06 +0000142#if defined(SQLITE_DEBUG)
143/* Check invariants on a PgHdr object */
144int sqlite3PcachePageSanity(PgHdr*);
145#endif
146
danielk1977d491e1b2008-08-26 18:05:48 +0000147/* Set and get the suggested cache-size for the specified pager-cache.
danielk19778c0a7912008-08-20 14:49:23 +0000148**
149** If no global maximum is configured, then the system attempts to limit
150** the total number of pages cached by purgeable pager-caches to the sum
151** of the suggested cache-sizes.
152*/
danielk19778c0a7912008-08-20 14:49:23 +0000153void sqlite3PcacheSetCachesize(PCache *, int);
danielk1977f3d3c272008-11-19 16:52:44 +0000154#ifdef SQLITE_TEST
155int sqlite3PcacheGetCachesize(PCache *);
156#endif
danielk19778c0a7912008-08-20 14:49:23 +0000157
drh9b0cf342015-11-12 14:57:19 +0000158/* Set or get the suggested spill-size for the specified pager-cache.
159**
160** The spill-size is the minimum number of pages in cache before the cache
161** will attempt to spill dirty pages by calling xStress.
162*/
163int sqlite3PcacheSetSpillsize(PCache *, int);
164
drh09419b42011-11-16 19:29:17 +0000165/* Free up as much memory as possible from the page cache */
166void sqlite3PcacheShrink(PCache*);
167
drh419fcf62008-10-11 17:42:28 +0000168#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
danielk1977d491e1b2008-08-26 18:05:48 +0000169/* Try to return memory used by the pcache module to the main memory heap */
danielk197767e3da72008-08-21 12:19:44 +0000170int sqlite3PcacheReleaseMemory(int);
drh419fcf62008-10-11 17:42:28 +0000171#endif
danielk197767e3da72008-08-21 12:19:44 +0000172
drh419fcf62008-10-11 17:42:28 +0000173#ifdef SQLITE_TEST
danielk1977062d4cb2008-08-29 09:10:02 +0000174void sqlite3PcacheStats(int*,int*,int*,int*);
drh419fcf62008-10-11 17:42:28 +0000175#endif
danielk1977062d4cb2008-08-29 09:10:02 +0000176
danielk1977bc2ca9e2008-11-13 14:28:28 +0000177void sqlite3PCacheSetDefault(void);
178
drhdef68892014-11-04 12:11:23 +0000179/* Return the header size */
180int sqlite3HeaderSizePcache(void);
181int sqlite3HeaderSizePcache1(void);
182
dan0f524552016-04-13 16:52:11 +0000183/* Number of dirty pages as a percentage of the configured cache size */
184int sqlite3PCachePercentDirty(PCache*);
185
danielk19778c0a7912008-08-20 14:49:23 +0000186#endif /* _PCACHE_H_ */