xref: /sqlite-3.40.0/src/pcache.h (revision 6bcfe8b6)
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 header file defines the interface that the sqlite page cache
13 ** subsystem.
14 */
15 
16 #ifndef _PCACHE_H_
17 
18 typedef struct PgHdr PgHdr;
19 typedef struct PCache PCache;
20 
21 /*
22 ** Every page in the cache is controlled by an instance of the following
23 ** structure.
24 */
25 struct PgHdr {
26   sqlite3_pcache_page *pPage;    /* Pcache object page handle */
27   void *pData;                   /* Page data */
28   void *pExtra;                  /* Extra content */
29   PgHdr *pDirty;                 /* Transient list of dirty pages */
30   Pager *pPager;                 /* The pager this page is part of */
31   Pgno pgno;                     /* Page number for this page */
32 #ifdef SQLITE_CHECK_PAGES
33   u32 pageHash;                  /* Hash of page content */
34 #endif
35   u16 flags;                     /* PGHDR flags defined below */
36 
37   /**********************************************************************
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 */
42   PCache *pCache;                /* Cache that owns this page */
43 
44   PgHdr *pDirtyNext;             /* Next element in list of dirty pages */
45   PgHdr *pDirtyPrev;             /* Previous element in list of dirty pages */
46 };
47 
48 /* Bit values for PgHdr.flags */
49 #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 */
57 
58 #define PGHDR_WAL_APPEND      0x080  /* Appended to wal file */
59 
60 /* Initialize and shutdown the page cache subsystem */
61 int sqlite3PcacheInitialize(void);
62 void sqlite3PcacheShutdown(void);
63 
64 /* Page cache buffer management:
65 ** These routines implement SQLITE_CONFIG_PAGECACHE.
66 */
67 void sqlite3PCacheBufferSetup(void *, int sz, int n);
68 
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 */
73 int sqlite3PcacheOpen(
74   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 */
77   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 */
80 );
81 
82 /* Modify the page-size after the cache has been created. */
83 int sqlite3PcacheSetPageSize(PCache *, int);
84 
85 /* Return the size in bytes of a PCache object.  Used to preallocate
86 ** storage space.
87 */
88 int sqlite3PcacheSize(void);
89 
90 /* One release per successful fetch.  Page is pinned until released.
91 ** Reference counted.
92 */
93 sqlite3_pcache_page *sqlite3PcacheFetch(PCache*, Pgno, int createFlag);
94 int sqlite3PcacheFetchStress(PCache*, Pgno, sqlite3_pcache_page**);
95 PgHdr *sqlite3PcacheFetchFinish(PCache*, Pgno, sqlite3_pcache_page *pPage);
96 void sqlite3PcacheRelease(PgHdr*);
97 
98 void sqlite3PcacheDrop(PgHdr*);         /* Remove page from cache */
99 void sqlite3PcacheMakeDirty(PgHdr*);    /* Make sure page is marked dirty */
100 void sqlite3PcacheMakeClean(PgHdr*);    /* Mark a single page as clean */
101 void sqlite3PcacheCleanAll(PCache*);    /* Mark all dirty list pages as clean */
102 void sqlite3PcacheClearWritable(PCache*);
103 
104 /* Change a page number.  Used by incr-vacuum. */
105 void sqlite3PcacheMove(PgHdr*, Pgno);
106 
107 /* Remove all pages with pgno>x.  Reset the cache if x==0 */
108 void sqlite3PcacheTruncate(PCache*, Pgno x);
109 
110 /* Get a list of all dirty pages in the cache, sorted by page number */
111 PgHdr *sqlite3PcacheDirtyList(PCache*);
112 
113 /* Reset and close the cache object */
114 void sqlite3PcacheClose(PCache*);
115 
116 /* Clear flags from pages of the page cache */
117 void sqlite3PcacheClearSyncFlags(PCache *);
118 
119 /* Discard the contents of the cache */
120 void sqlite3PcacheClear(PCache*);
121 
122 /* Return the total number of outstanding page references */
123 int sqlite3PcacheRefCount(PCache*);
124 
125 /* Increment the reference count of an existing page */
126 void sqlite3PcacheRef(PgHdr*);
127 
128 int sqlite3PcachePageRefcount(PgHdr*);
129 
130 /* Return the total number of pages stored in the cache */
131 int sqlite3PcachePagecount(PCache*);
132 
133 #if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)
134 /* Iterate through all dirty pages currently stored in the cache. This
135 ** interface is only available if SQLITE_CHECK_PAGES is defined when the
136 ** library is built.
137 */
138 void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *));
139 #endif
140 
141 /* Set and get the suggested cache-size for the specified pager-cache.
142 **
143 ** If no global maximum is configured, then the system attempts to limit
144 ** the total number of pages cached by purgeable pager-caches to the sum
145 ** of the suggested cache-sizes.
146 */
147 void sqlite3PcacheSetCachesize(PCache *, int);
148 #ifdef SQLITE_TEST
149 int sqlite3PcacheGetCachesize(PCache *);
150 #endif
151 
152 /* Set or get the suggested spill-size for the specified pager-cache.
153 **
154 ** The spill-size is the minimum number of pages in cache before the cache
155 ** will attempt to spill dirty pages by calling xStress.
156 */
157 int sqlite3PcacheSetSpillsize(PCache *, int);
158 
159 /* Free up as much memory as possible from the page cache */
160 void sqlite3PcacheShrink(PCache*);
161 
162 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
163 /* Try to return memory used by the pcache module to the main memory heap */
164 int sqlite3PcacheReleaseMemory(int);
165 #endif
166 
167 #ifdef SQLITE_TEST
168 void sqlite3PcacheStats(int*,int*,int*,int*);
169 #endif
170 
171 void sqlite3PCacheSetDefault(void);
172 
173 /* Return the header size */
174 int sqlite3HeaderSizePcache(void);
175 int sqlite3HeaderSizePcache1(void);
176 
177 /* Number of dirty pages as a percentage of the configured cache size */
178 int sqlite3PCachePercentDirty(PCache*);
179 
180 #endif /* _PCACHE_H_ */
181