blob: 475c04c0618883f1f8486181980be775dc80cf1b [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 */
29 PgHdr *pDirty; /* Transient list of dirty pages */
drha85f7e32008-08-28 02:26:07 +000030 Pager *pPager; /* The pager this page is part of */
drha4510172012-02-02 15:50:17 +000031 Pgno pgno; /* Page number for this page */
danielk19778c0a7912008-08-20 14:49:23 +000032#ifdef SQLITE_CHECK_PAGES
drha85f7e32008-08-28 02:26:07 +000033 u32 pageHash; /* Hash of page content */
danielk19778c0a7912008-08-20 14:49:23 +000034#endif
drha85f7e32008-08-28 02:26:07 +000035 u16 flags; /* PGHDR flags defined below */
danielk1977bc2ca9e2008-11-13 14:28:28 +000036
drha85f7e32008-08-28 02:26:07 +000037 /**********************************************************************
38 ** Elements above are public. All that follows is private to pcache.c
39 ** and should not be accessed by other modules.
40 */
41 i16 nRef; /* Number of users of this page */
danielk19778c0a7912008-08-20 14:49:23 +000042 PCache *pCache; /* Cache that owns this page */
danielk1977b3175382008-10-17 18:51:52 +000043
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 */
danielk19778c0a7912008-08-20 14:49:23 +000046};
47
48/* Bit values for PgHdr.flags */
drh1aacbdb2015-06-29 18:29:10 +000049#define PGHDR_CLEAN 0x001 /* Page not on the PCache.pDirty list */
50#define PGHDR_DIRTY 0x002 /* Page is on the PCache.pDirty list */
51#define PGHDR_WRITEABLE 0x004 /* Journaled and ready to modify */
52#define PGHDR_NEED_SYNC 0x008 /* Fsync the rollback journal before
53 ** writing this page to the database */
54#define PGHDR_NEED_READ 0x010 /* Content is unread */
55#define PGHDR_DONT_WRITE 0x020 /* Do not write content to disk */
56#define PGHDR_MMAP 0x040 /* This is an mmap page object */
danb2d3de32013-03-14 18:34:37 +000057
dand6f7c972016-01-09 16:39:29 +000058#define PGHDR_WAL_APPEND 0x080 /* Appended to wal file */
59
danielk19778c0a7912008-08-20 14:49:23 +000060/* Initialize and shutdown the page cache subsystem */
61int sqlite3PcacheInitialize(void);
62void sqlite3PcacheShutdown(void);
63
64/* Page cache buffer management:
65** These routines implement SQLITE_CONFIG_PAGECACHE.
66*/
67void sqlite3PCacheBufferSetup(void *, int sz, int n);
danielk19778c0a7912008-08-20 14:49:23 +000068
69/* Create a new pager cache.
70** Under memory stress, invoke xStress to try to make pages clean.
71** Only clean and unpinned pages can be reclaimed.
72*/
drhc3031c62014-08-26 15:06:49 +000073int sqlite3PcacheOpen(
danielk1977a858aa22008-08-22 16:22:17 +000074 int szPage, /* Size of every page */
75 int szExtra, /* Extra space associated with each page */
76 int bPurgeable, /* True if pages are on backing store */
danielk1977a858aa22008-08-22 16:22:17 +000077 int (*xStress)(void*, PgHdr*), /* Call to try to make pages clean */
78 void *pStress, /* Argument to xStress */
79 PCache *pToInit /* Preallocated space for the PCache */
danielk19778c0a7912008-08-20 14:49:23 +000080);
81
82/* Modify the page-size after the cache has been created. */
drhc3031c62014-08-26 15:06:49 +000083int sqlite3PcacheSetPageSize(PCache *, int);
danielk19778c0a7912008-08-20 14:49:23 +000084
85/* Return the size in bytes of a PCache object. Used to preallocate
86** storage space.
87*/
88int sqlite3PcacheSize(void);
89
90/* One release per successful fetch. Page is pinned until released.
91** Reference counted.
92*/
drhbc59ac02014-08-27 23:18:01 +000093sqlite3_pcache_page *sqlite3PcacheFetch(PCache*, Pgno, int createFlag);
94int sqlite3PcacheFetchStress(PCache*, Pgno, sqlite3_pcache_page**);
95PgHdr *sqlite3PcacheFetchFinish(PCache*, Pgno, sqlite3_pcache_page *pPage);
danielk19778c0a7912008-08-20 14:49:23 +000096void sqlite3PcacheRelease(PgHdr*);
97
98void sqlite3PcacheDrop(PgHdr*); /* Remove page from cache */
99void sqlite3PcacheMakeDirty(PgHdr*); /* Make sure page is marked dirty */
100void sqlite3PcacheMakeClean(PgHdr*); /* Mark a single page as clean */
101void sqlite3PcacheCleanAll(PCache*); /* Mark all dirty list pages as clean */
102
103/* Change a page number. Used by incr-vacuum. */
104void sqlite3PcacheMove(PgHdr*, Pgno);
105
danielk19778c0a7912008-08-20 14:49:23 +0000106/* Remove all pages with pgno>x. Reset the cache if x==0 */
107void sqlite3PcacheTruncate(PCache*, Pgno x);
108
danielk19778c0a7912008-08-20 14:49:23 +0000109/* Get a list of all dirty pages in the cache, sorted by page number */
110PgHdr *sqlite3PcacheDirtyList(PCache*);
111
danielk19778c0a7912008-08-20 14:49:23 +0000112/* Reset and close the cache object */
113void sqlite3PcacheClose(PCache*);
114
drhb3df2e12008-09-17 20:06:26 +0000115/* Clear flags from pages of the page cache */
danielk1977bc2ca9e2008-11-13 14:28:28 +0000116void sqlite3PcacheClearSyncFlags(PCache *);
danielk19778c0a7912008-08-20 14:49:23 +0000117
danielk19778c0a7912008-08-20 14:49:23 +0000118/* Discard the contents of the cache */
danielk1977bea2a942009-01-20 17:06:27 +0000119void sqlite3PcacheClear(PCache*);
danielk19778c0a7912008-08-20 14:49:23 +0000120
121/* Return the total number of outstanding page references */
122int sqlite3PcacheRefCount(PCache*);
123
124/* Increment the reference count of an existing page */
125void sqlite3PcacheRef(PgHdr*);
126
danielk197771d5d2c2008-09-29 11:49:47 +0000127int sqlite3PcachePageRefcount(PgHdr*);
128
danielk19778c0a7912008-08-20 14:49:23 +0000129/* Return the total number of pages stored in the cache */
130int sqlite3PcachePagecount(PCache*);
131
danielk1977750e87d2009-07-25 11:46:48 +0000132#if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)
danielk1977bc2ca9e2008-11-13 14:28:28 +0000133/* Iterate through all dirty pages currently stored in the cache. This
134** interface is only available if SQLITE_CHECK_PAGES is defined when the
135** library is built.
danielk19778c0a7912008-08-20 14:49:23 +0000136*/
danielk1977bc2ca9e2008-11-13 14:28:28 +0000137void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *));
drh419fcf62008-10-11 17:42:28 +0000138#endif
danielk19778c0a7912008-08-20 14:49:23 +0000139
danielk1977d491e1b2008-08-26 18:05:48 +0000140/* Set and get the suggested cache-size for the specified pager-cache.
danielk19778c0a7912008-08-20 14:49:23 +0000141**
142** If no global maximum is configured, then the system attempts to limit
143** the total number of pages cached by purgeable pager-caches to the sum
144** of the suggested cache-sizes.
145*/
danielk19778c0a7912008-08-20 14:49:23 +0000146void sqlite3PcacheSetCachesize(PCache *, int);
danielk1977f3d3c272008-11-19 16:52:44 +0000147#ifdef SQLITE_TEST
148int sqlite3PcacheGetCachesize(PCache *);
149#endif
danielk19778c0a7912008-08-20 14:49:23 +0000150
drh9b0cf342015-11-12 14:57:19 +0000151/* Set or get the suggested spill-size for the specified pager-cache.
152**
153** The spill-size is the minimum number of pages in cache before the cache
154** will attempt to spill dirty pages by calling xStress.
155*/
156int sqlite3PcacheSetSpillsize(PCache *, int);
157
drh09419b42011-11-16 19:29:17 +0000158/* Free up as much memory as possible from the page cache */
159void sqlite3PcacheShrink(PCache*);
160
drh419fcf62008-10-11 17:42:28 +0000161#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
danielk1977d491e1b2008-08-26 18:05:48 +0000162/* Try to return memory used by the pcache module to the main memory heap */
danielk197767e3da72008-08-21 12:19:44 +0000163int sqlite3PcacheReleaseMemory(int);
drh419fcf62008-10-11 17:42:28 +0000164#endif
danielk197767e3da72008-08-21 12:19:44 +0000165
drh419fcf62008-10-11 17:42:28 +0000166#ifdef SQLITE_TEST
danielk1977062d4cb2008-08-29 09:10:02 +0000167void sqlite3PcacheStats(int*,int*,int*,int*);
drh419fcf62008-10-11 17:42:28 +0000168#endif
danielk1977062d4cb2008-08-29 09:10:02 +0000169
danielk1977bc2ca9e2008-11-13 14:28:28 +0000170void sqlite3PCacheSetDefault(void);
171
drhdef68892014-11-04 12:11:23 +0000172/* Return the header size */
173int sqlite3HeaderSizePcache(void);
174int sqlite3HeaderSizePcache1(void);
175
danielk19778c0a7912008-08-20 14:49:23 +0000176#endif /* _PCACHE_H_ */