Debian Codegraph @

storage/innobase/include/buf0types.h

raw
1 /*****************************************************************************
2
3 Copyright (c) 1995, 2026, Oracle and/or its affiliates.
4
5 This program is free software; you can redistribute it and/or modify it under
6 the terms of the GNU General Public License, version 2.0, as published by the
7 Free Software Foundation.
8
9 This program is designed to work with certain software (including
10 but not limited to OpenSSL) that is licensed under separate terms,
11 as designated in a particular file or component or in included license
12 documentation. The authors of MySQL hereby grant you an additional
13 permission to link the program and your derivative works with the
14 separately licensed software that they have either included with
15 the program or referenced in the documentation.
16
17 This program is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
20 for more details.
21
22 You should have received a copy of the GNU General Public License along with
23 this program; if not, write to the Free Software Foundation, Inc.,
24 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
26 *****************************************************************************/
27
28 /** @file include/buf0types.h
29 The database buffer pool global types for the directory
30
31 Created 11/17/1995 Heikki Tuuri
32 *******************************************************/
33
34 #ifndef buf0types_h
35 #define buf0types_h
36
37 #include "os0event.h"
38 #include "sync0rw.h"
39 #include "ut0byte.h"
40 #include "ut0mutex.h"
41 #include "ut0rnd.h"
42 #include "ut0ut.h"
43
44 /** Magic value to use instead of checksums when they are disabled */
45 constexpr uint32_t BUF_NO_CHECKSUM_MAGIC = 0xDEADBEEFUL;
46
47 /** Buffer page (uncompressed or compressed) */
48 class buf_page_t;
49 /** Buffer block for which an uncompressed page exists */
50 struct buf_block_t;
51 /** Buffer pool chunk comprising buf_block_t */
52 struct buf_chunk_t;
53 /** Buffer pool comprising buf_chunk_t */
54 struct buf_pool_t;
55 /** Buffer pool statistics struct */
56 struct buf_pool_stat_t;
57 /** Buffer pool buddy statistics struct */
58 struct buf_buddy_stat_t;
59 /** Doublewrite memory struct */
60 struct buf_dblwr_t;
61 /** Flush observer for bulk create index */
62 class Flush_observer;
63
64 /** A buffer frame. @see page_t */
65 typedef byte buf_frame_t;
66
67 /** Flags for flush types */
68 enum buf_flush_t : uint8_t {
69 /** Flush via the LRU list */
70 BUF_FLUSH_LRU = 0,
71
72 /** Flush via the flush list of dirty blocks */
73 BUF_FLUSH_LIST,
74
75 /** Flush via the LRU list but only a single page */
76 BUF_FLUSH_SINGLE_PAGE,
77
78 /** Index of last element + 1 */
79 BUF_FLUSH_N_TYPES
80 };
81
82 /** Algorithm to remove the pages for a tablespace from the buffer pool.
83 See buf_LRU_flush_or_remove_pages(). */
84 enum buf_remove_t {
85 /** Don't remove any pages. */
86 BUF_REMOVE_NONE,
87
88 /** Remove all pages from the buffer pool, don't write or sync to disk */
89 BUF_REMOVE_ALL_NO_WRITE,
90
91 /** Remove only from the flush list, don't write or sync to disk */
92 BUF_REMOVE_FLUSH_NO_WRITE,
93
94 /** Flush dirty pages to disk only don't remove from the buffer pool */
95 BUF_REMOVE_FLUSH_WRITE
96 };
97
98 /** Flags for io_fix types */
99 enum buf_io_fix : uint8_t {
100 /** no pending I/O */
101 BUF_IO_NONE = 0,
102
103 /** read pending */
104 BUF_IO_READ,
105
106 /** write pending */
107 BUF_IO_WRITE,
108
109 /** disallow relocation of block and its removal from the flush_list */
110 BUF_IO_PIN
111 };
112
113 /** Alternatives for srv_checksum_algorithm, which can be changed by
114 setting innodb_checksum_algorithm */
115 enum srv_checksum_algorithm_t {
116 SRV_CHECKSUM_ALGORITHM_CRC32, /*!< Write crc32, allow crc32,
117 innodb or none when reading */
118 SRV_CHECKSUM_ALGORITHM_STRICT_CRC32, /*!< Write crc32, allow crc32
119 when reading */
120 SRV_CHECKSUM_ALGORITHM_INNODB, /*!< Write innodb, allow crc32,
121 innodb or none when reading */
122 SRV_CHECKSUM_ALGORITHM_STRICT_INNODB, /*!< Write innodb, allow
123 innodb when reading */
124 SRV_CHECKSUM_ALGORITHM_NONE, /*!< Write none, allow crc32,
125 innodb or none when reading */
126 SRV_CHECKSUM_ALGORITHM_STRICT_NONE /*!< Write none, allow none
127 when reading */
128 };
129
130 /** Buffer pool resize status code and progress are tracked using these
131 atomic variables to ensure thread synchronization between
132 innodb_buffer_pool_size_update (raising srv_buf_resize_event) and
133 buf_resize_thread (handling srv_buf_resize_event) */
134 extern std::atomic_uint32_t buf_pool_resize_status_code;
135 extern std::atomic_uint32_t buf_pool_resize_status_progress;
136
137 /** Enumerate possible status codes during buffer pool resize. This is used
138 to identify the resize status using the corresponding code. */
139 enum buf_pool_resize_status_code_t {
140 /** Resize completed or Resize not in progress*/
141 BUF_POOL_RESIZE_COMPLETE = 0,
142
143 /** Resize started */
144 BUF_POOL_RESIZE_START = 1,
145
146 /** Disabling Adaptive Hash Index */
147 BUF_POOL_RESIZE_DISABLE_AHI = 2,
148
149 /** Withdrawing blocks */
150 BUF_POOL_RESIZE_WITHDRAW_BLOCKS = 3,
151
152 /** Acquiring global lock */
153 BUF_POOL_RESIZE_GLOBAL_LOCK = 4,
154
155 /** Resizing pool */
156 BUF_POOL_RESIZE_IN_PROGRESS = 5,
157
158 /** Resizing hash */
159 BUF_POOL_RESIZE_HASH = 6,
160
161 /** Resizing failed */
162 BUF_POOL_RESIZE_FAILED = 7
163 };
164
165 inline bool is_checksum_strict(srv_checksum_algorithm_t algo) {
166 return (algo == SRV_CHECKSUM_ALGORITHM_STRICT_CRC32 ||
167 algo == SRV_CHECKSUM_ALGORITHM_STRICT_INNODB ||
168 algo == SRV_CHECKSUM_ALGORITHM_STRICT_NONE);
169 }
170
171 inline bool is_checksum_strict(ulint algo) {
172 return (algo == SRV_CHECKSUM_ALGORITHM_STRICT_CRC32 ||
173 algo == SRV_CHECKSUM_ALGORITHM_STRICT_INNODB ||
174 algo == SRV_CHECKSUM_ALGORITHM_STRICT_NONE);
175 }
176
177 /** Parameters of binary buddy system for compressed pages (buf0buddy.h) */
178 /** @{ */
179 /** Zip shift value for the smallest page size */
180 constexpr uint32_t BUF_BUDDY_LOW_SHIFT = UNIV_ZIP_SIZE_SHIFT_MIN;
181
182 /** Smallest buddy page size */
183 constexpr uint32_t BUF_BUDDY_LOW = (1U << BUF_BUDDY_LOW_SHIFT);
184
185 /** Actual number of buddy sizes based on current page size */
186 #define BUF_BUDDY_SIZES (UNIV_PAGE_SIZE_SHIFT - BUF_BUDDY_LOW_SHIFT)
187
188 /** Maximum number of buddy sizes based on the max page size */
189 constexpr uint32_t BUF_BUDDY_SIZES_MAX =
190 UNIV_PAGE_SIZE_SHIFT_MAX - BUF_BUDDY_LOW_SHIFT;
191
192 /** twice the maximum block size of the buddy system;
193 the underlying memory is aligned by this amount:
194 this must be equal to UNIV_PAGE_SIZE */
195 #define BUF_BUDDY_HIGH (BUF_BUDDY_LOW << BUF_BUDDY_SIZES)
196 /** @} */
197
198 typedef ib_bpmutex_t BPageMutex;
199 typedef ib_mutex_t BufListMutex;
200 typedef ib_mutex_t FlushListMutex;
201 typedef BPageMutex BufPoolZipMutex;
202 #ifndef UNIV_HOTBACKUP
203 typedef rw_lock_t BPageLock;
204 #endif /* !UNIV_HOTBACKUP */
205
206 /** Page identifier. */
207 class page_id_t {
208 public:
209 /**
210 This class does not have a default constructor, because there is no natural
211 choice for default values of m_space and m_page_no.
212
213 If 0,0 were used, then it's not good as it doesn't match UINT32_UNDEFINED
214 used to denote impossible page_no_t in several places, and 0 is a legal
215 value for both space_id_t and page_id_t of a real page!
216
217 If UINT32_UNDEFINED,UINT32_UNDEFINED were used, then it doesn't match the
218 most common usage where use use memset(parent,0,sizeof(parent_t)); on a
219 parent struct where one of the members has page_id_t type - which is ok
220 given that page_id_t is TriviallyCopyable, and that the field is not
221 used until it is assigned some real value. Such constructor would be
222 misleading to people reading the code, as they might expect UINT32_UNDEFINED
223 value, if they didn't notice the memset code buried somewhere in parent's
224 initialization routine.
225
226 Therefore, please either be explicit by using (space,page_no) overload,
227 or continue to use memset at your own risk.
228 */
229 page_id_t() = delete;
230
231 /** Constructor from (space, page_no).
232 @param[in] space tablespace id
233 @param[in] page_no page number */
234 page_id_t(space_id_t space, page_no_t page_no)
235 : m_space(space), m_page_no(page_no) {}
236
237 /** Retrieve the tablespace id.
238 @return tablespace id */
239 inline space_id_t space() const { return (m_space); }
240
241 /** Retrieve the page number.
242 @return page number */
243 inline page_no_t page_no() const { return (m_page_no); }
244
245 /** Retrieve the hash value.
246 @return hashed value */
247 inline uint64_t hash() const {
248 constexpr uint64_t HASH_MASK = 1653893711;
249 return (((uint64_t)m_space << 20) + m_space + m_page_no) ^ HASH_MASK;
250 }
251
252 /** Reset the values from a (space, page_no).
253 @param[in] space tablespace id
254 @param[in] page_no page number */
255 inline void reset(space_id_t space, page_no_t page_no) {
256 m_space = space;
257 m_page_no = page_no;
258 }
259
260 /** Reset the page number only.
261 @param[in] page_no page number */
262 inline void set_page_no(page_no_t page_no) { m_page_no = page_no; }
263
264 /** Check if a given page_id_t object is equal to the current one.
265 @param[in] a page_id_t object to compare
266 @return true if equal */
267 inline bool operator==(const page_id_t &a) const {
268 return (a.space() == m_space && a.page_no() == m_page_no);
269 }
270
271 /** Check if a given page_id_t object is not equal to the current one.
272 @param[in] a page_id_t object to compare
273 @return true if not equal */
274 inline bool operator!=(const page_id_t &a) const { return !(*this == a); }
275
276 /** Provides a lexicographic ordering on <space_id,page_no> pairs
277 @param[in] other page_id_t object to compare
278 @return true if this is strictly smaller than other */
279 inline bool operator<(const page_id_t &other) const {
280 return m_space < other.space() ||
281 (m_space == other.space() && m_page_no < other.page_no());
282 }
283
284 private:
285 /** Tablespace id. */
286 space_id_t m_space;
287
288 /** Page number. */
289 page_no_t m_page_no;
290
291 friend std::ostream &operator<<(std::ostream &out, const page_id_t &page_id);
292 };
293
294 /** Print the given page_id_t object.
295 @param[in,out] out the output stream
296 @param[in] page_id the page_id_t object to be printed
297 @return the output stream */
298 std::ostream &operator<<(std::ostream &out, const page_id_t &page_id);
299
300 #endif /* buf0types.h */