danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 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. |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 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 { |
dan | 22e21ff | 2011-11-08 20:08:44 +0000 | [diff] [blame] | 26 | sqlite3_pcache_page *pPage; /* Pcache object page handle */ |
| 27 | void *pData; /* Page data */ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 28 | void *pExtra; /* Extra content */ |
drh | 72e6a39 | 2016-05-11 23:54:14 +0000 | [diff] [blame] | 29 | PgHdr *pDirty; /* Transient list of dirty sorted by pgno */ |
drh | a85f7e3 | 2008-08-28 02:26:07 +0000 | [diff] [blame] | 30 | Pager *pPager; /* The pager this page is part of */ |
drh | a451017 | 2012-02-02 15:50:17 +0000 | [diff] [blame] | 31 | Pgno pgno; /* Page number for this page */ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 32 | #ifdef SQLITE_CHECK_PAGES |
drh | a85f7e3 | 2008-08-28 02:26:07 +0000 | [diff] [blame] | 33 | u32 pageHash; /* Hash of page content */ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 34 | #endif |
drh | a85f7e3 | 2008-08-28 02:26:07 +0000 | [diff] [blame] | 35 | u16 flags; /* PGHDR flags defined below */ |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 36 | |
drh | a85f7e3 | 2008-08-28 02:26:07 +0000 | [diff] [blame] | 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 */ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 42 | PCache *pCache; /* Cache that owns this page */ |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 43 | |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 44 | PgHdr *pDirtyNext; /* Next element in list of dirty pages */ |
| 45 | PgHdr *pDirtyPrev; /* Previous element in list of dirty pages */ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 46 | }; |
| 47 | |
| 48 | /* Bit values for PgHdr.flags */ |
drh | 1aacbdb | 2015-06-29 18:29:10 +0000 | [diff] [blame] | 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 */ |
drh | a0f6b12 | 2016-05-13 15:22:06 +0000 | [diff] [blame] | 54 | #define PGHDR_DONT_WRITE 0x010 /* Do not write content to disk */ |
| 55 | #define PGHDR_MMAP 0x020 /* This is an mmap page object */ |
dan | b2d3de3 | 2013-03-14 18:34:37 +0000 | [diff] [blame] | 56 | |
drh | a0f6b12 | 2016-05-13 15:22:06 +0000 | [diff] [blame] | 57 | #define PGHDR_WAL_APPEND 0x040 /* Appended to wal file */ |
dan | d6f7c97 | 2016-01-09 16:39:29 +0000 | [diff] [blame] | 58 | |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 59 | /* Initialize and shutdown the page cache subsystem */ |
| 60 | int sqlite3PcacheInitialize(void); |
| 61 | void sqlite3PcacheShutdown(void); |
| 62 | |
| 63 | /* Page cache buffer management: |
| 64 | ** These routines implement SQLITE_CONFIG_PAGECACHE. |
| 65 | */ |
| 66 | void sqlite3PCacheBufferSetup(void *, int sz, int n); |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 67 | |
| 68 | /* Create a new pager cache. |
| 69 | ** Under memory stress, invoke xStress to try to make pages clean. |
| 70 | ** Only clean and unpinned pages can be reclaimed. |
| 71 | */ |
drh | c3031c6 | 2014-08-26 15:06:49 +0000 | [diff] [blame] | 72 | int sqlite3PcacheOpen( |
danielk1977 | a858aa2 | 2008-08-22 16:22:17 +0000 | [diff] [blame] | 73 | int szPage, /* Size of every page */ |
| 74 | int szExtra, /* Extra space associated with each page */ |
| 75 | int bPurgeable, /* True if pages are on backing store */ |
danielk1977 | a858aa2 | 2008-08-22 16:22:17 +0000 | [diff] [blame] | 76 | int (*xStress)(void*, PgHdr*), /* Call to try to make pages clean */ |
| 77 | void *pStress, /* Argument to xStress */ |
| 78 | PCache *pToInit /* Preallocated space for the PCache */ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 79 | ); |
| 80 | |
| 81 | /* Modify the page-size after the cache has been created. */ |
drh | c3031c6 | 2014-08-26 15:06:49 +0000 | [diff] [blame] | 82 | int sqlite3PcacheSetPageSize(PCache *, int); |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 83 | |
| 84 | /* Return the size in bytes of a PCache object. Used to preallocate |
| 85 | ** storage space. |
| 86 | */ |
| 87 | int sqlite3PcacheSize(void); |
| 88 | |
| 89 | /* One release per successful fetch. Page is pinned until released. |
| 90 | ** Reference counted. |
| 91 | */ |
drh | bc59ac0 | 2014-08-27 23:18:01 +0000 | [diff] [blame] | 92 | sqlite3_pcache_page *sqlite3PcacheFetch(PCache*, Pgno, int createFlag); |
| 93 | int sqlite3PcacheFetchStress(PCache*, Pgno, sqlite3_pcache_page**); |
| 94 | PgHdr *sqlite3PcacheFetchFinish(PCache*, Pgno, sqlite3_pcache_page *pPage); |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 95 | void sqlite3PcacheRelease(PgHdr*); |
| 96 | |
| 97 | void sqlite3PcacheDrop(PgHdr*); /* Remove page from cache */ |
| 98 | void sqlite3PcacheMakeDirty(PgHdr*); /* Make sure page is marked dirty */ |
| 99 | void sqlite3PcacheMakeClean(PgHdr*); /* Mark a single page as clean */ |
| 100 | void sqlite3PcacheCleanAll(PCache*); /* Mark all dirty list pages as clean */ |
drh | 6bcfe8b | 2016-04-21 15:24:46 +0000 | [diff] [blame] | 101 | void sqlite3PcacheClearWritable(PCache*); |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 102 | |
| 103 | /* Change a page number. Used by incr-vacuum. */ |
| 104 | void sqlite3PcacheMove(PgHdr*, Pgno); |
| 105 | |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 106 | /* Remove all pages with pgno>x. Reset the cache if x==0 */ |
| 107 | void sqlite3PcacheTruncate(PCache*, Pgno x); |
| 108 | |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 109 | /* Get a list of all dirty pages in the cache, sorted by page number */ |
| 110 | PgHdr *sqlite3PcacheDirtyList(PCache*); |
| 111 | |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 112 | /* Reset and close the cache object */ |
| 113 | void sqlite3PcacheClose(PCache*); |
| 114 | |
drh | b3df2e1 | 2008-09-17 20:06:26 +0000 | [diff] [blame] | 115 | /* Clear flags from pages of the page cache */ |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 116 | void sqlite3PcacheClearSyncFlags(PCache *); |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 117 | |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 118 | /* Discard the contents of the cache */ |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 119 | void sqlite3PcacheClear(PCache*); |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 120 | |
| 121 | /* Return the total number of outstanding page references */ |
| 122 | int sqlite3PcacheRefCount(PCache*); |
| 123 | |
| 124 | /* Increment the reference count of an existing page */ |
| 125 | void sqlite3PcacheRef(PgHdr*); |
| 126 | |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 127 | int sqlite3PcachePageRefcount(PgHdr*); |
| 128 | |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 129 | /* Return the total number of pages stored in the cache */ |
| 130 | int sqlite3PcachePagecount(PCache*); |
| 131 | |
danielk1977 | 750e87d | 2009-07-25 11:46:48 +0000 | [diff] [blame] | 132 | #if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG) |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 133 | /* 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. |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 136 | */ |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 137 | void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *)); |
drh | 419fcf6 | 2008-10-11 17:42:28 +0000 | [diff] [blame] | 138 | #endif |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 139 | |
drh | a0f6b12 | 2016-05-13 15:22:06 +0000 | [diff] [blame] | 140 | #if defined(SQLITE_DEBUG) |
| 141 | /* Check invariants on a PgHdr object */ |
| 142 | int sqlite3PcachePageSanity(PgHdr*); |
| 143 | #endif |
| 144 | |
danielk1977 | d491e1b | 2008-08-26 18:05:48 +0000 | [diff] [blame] | 145 | /* Set and get the suggested cache-size for the specified pager-cache. |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 146 | ** |
| 147 | ** If no global maximum is configured, then the system attempts to limit |
| 148 | ** the total number of pages cached by purgeable pager-caches to the sum |
| 149 | ** of the suggested cache-sizes. |
| 150 | */ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 151 | void sqlite3PcacheSetCachesize(PCache *, int); |
danielk1977 | f3d3c27 | 2008-11-19 16:52:44 +0000 | [diff] [blame] | 152 | #ifdef SQLITE_TEST |
| 153 | int sqlite3PcacheGetCachesize(PCache *); |
| 154 | #endif |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 155 | |
drh | 9b0cf34 | 2015-11-12 14:57:19 +0000 | [diff] [blame] | 156 | /* Set or get the suggested spill-size for the specified pager-cache. |
| 157 | ** |
| 158 | ** The spill-size is the minimum number of pages in cache before the cache |
| 159 | ** will attempt to spill dirty pages by calling xStress. |
| 160 | */ |
| 161 | int sqlite3PcacheSetSpillsize(PCache *, int); |
| 162 | |
drh | 09419b4 | 2011-11-16 19:29:17 +0000 | [diff] [blame] | 163 | /* Free up as much memory as possible from the page cache */ |
| 164 | void sqlite3PcacheShrink(PCache*); |
| 165 | |
drh | 419fcf6 | 2008-10-11 17:42:28 +0000 | [diff] [blame] | 166 | #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT |
danielk1977 | d491e1b | 2008-08-26 18:05:48 +0000 | [diff] [blame] | 167 | /* Try to return memory used by the pcache module to the main memory heap */ |
danielk1977 | 67e3da7 | 2008-08-21 12:19:44 +0000 | [diff] [blame] | 168 | int sqlite3PcacheReleaseMemory(int); |
drh | 419fcf6 | 2008-10-11 17:42:28 +0000 | [diff] [blame] | 169 | #endif |
danielk1977 | 67e3da7 | 2008-08-21 12:19:44 +0000 | [diff] [blame] | 170 | |
drh | 419fcf6 | 2008-10-11 17:42:28 +0000 | [diff] [blame] | 171 | #ifdef SQLITE_TEST |
danielk1977 | 062d4cb | 2008-08-29 09:10:02 +0000 | [diff] [blame] | 172 | void sqlite3PcacheStats(int*,int*,int*,int*); |
drh | 419fcf6 | 2008-10-11 17:42:28 +0000 | [diff] [blame] | 173 | #endif |
danielk1977 | 062d4cb | 2008-08-29 09:10:02 +0000 | [diff] [blame] | 174 | |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 175 | void sqlite3PCacheSetDefault(void); |
| 176 | |
drh | def6889 | 2014-11-04 12:11:23 +0000 | [diff] [blame] | 177 | /* Return the header size */ |
| 178 | int sqlite3HeaderSizePcache(void); |
| 179 | int sqlite3HeaderSizePcache1(void); |
| 180 | |
dan | 0f52455 | 2016-04-13 16:52:11 +0000 | [diff] [blame] | 181 | /* Number of dirty pages as a percentage of the configured cache size */ |
| 182 | int sqlite3PCachePercentDirty(PCache*); |
| 183 | |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 184 | #endif /* _PCACHE_H_ */ |