Debian Codegraph @

storage/innobase/include/buf0buf.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/buf0buf.h
29 The database buffer pool high-level routines
30
31 Created 11/5/1995 Heikki Tuuri
32 *******************************************************/
33
34 #ifndef buf0buf_h
35 #define buf0buf_h
36
37 #include "buf0types.h"
38 #include "fil0fil.h"
39 #include "hash0hash.h"
40 #include "mtr0types.h"
41 #include "os0proc.h"
42 #include "page0types.h"
43 #include "srv0shutdown.h"
44 #include "srv0srv.h"
45 #include "univ.i"
46 #include "ut0byte.h"
47 #include "ut0rbt.h"
48
49 #include "buf/buf.h"
50
51 #include <ostream>
52
53 // Forward declaration
54 struct fil_addr_t;
55
56 /** @name Modes for buf_page_get_gen */
57 /** @{ */
58 enum class Page_fetch {
59 /** Get always */
60 NORMAL,
61
62 /** Same as NORMAL, but hint that the fetch is part of a large scan.
63 Try not to flood the buffer pool with pages that may not be accessed again
64 any time soon. */
65 SCAN,
66
67 /** get if in pool */
68 IF_IN_POOL,
69
70 /** get if in pool, do not make the block young in the LRU list */
71 PEEK_IF_IN_POOL,
72
73 /** get and bufferfix, but set no latch; we have separated this case, because
74 it is error-prone programming not to set a latch, and it should be used with
75 care */
76 NO_LATCH,
77
78 /** Get the page only if it's in the buffer pool, if not then set a watch on
79 the page. */
80 IF_IN_POOL_OR_WATCH,
81
82 /** Like Page_fetch::NORMAL, but do not mind if the file page has been
83 freed. */
84 POSSIBLY_FREED
85 };
86 /** @} */
87
88 /** @name Modes for buf_page_get_known_nowait */
89
90 /** @{ */
91 enum class Cache_hint {
92 /** Move the block to the start of the LRU list if there is a danger that the
93 block would drift out of the buffer pool*/
94 MAKE_YOUNG = 51,
95
96 /** Preserve the current LRU position of the block. */
97 KEEP_OLD = 52
98 };
99
100 /** @} */
101
102 /** Number of bits to representing a buffer pool ID */
103 constexpr ulint MAX_BUFFER_POOLS_BITS = 6;
104
105 /** The maximum number of buffer pools that can be defined */
106 constexpr ulint MAX_BUFFER_POOLS = (1 << MAX_BUFFER_POOLS_BITS);
107
108 /** Maximum number of concurrent buffer pool watches */
109 #define BUF_POOL_WATCH_SIZE (srv_n_purge_threads + 1)
110
111 /** The maximum number of page_hash locks */
112 constexpr ulint MAX_PAGE_HASH_LOCKS = 1024;
113
114 /** The buffer pools of the database */
115 extern buf_pool_t *buf_pool_ptr;
116
117 #ifdef UNIV_HOTBACKUP
118 /** first block, for --apply-log */
119 extern buf_block_t *back_block1;
120 /** second block, for page reorganize */
121 extern buf_block_t *back_block2;
122 #endif /* UNIV_HOTBACKUP */
123
124 /** @brief States of a control block
125 @see buf_page_t
126
127 The enumeration values must be 0..7. */
128 enum buf_page_state : uint8_t {
129 /** A sentinel for the buffer pool watch, element of buf_pool->watch[] */
130 BUF_BLOCK_POOL_WATCH,
131 /** Contains a clean compressed page */
132 BUF_BLOCK_ZIP_PAGE,
133 /** Contains a compressed page that is in the buf_pool->flush_list */
134 BUF_BLOCK_ZIP_DIRTY,
135
136 /** Is in the free list; must be after the BUF_BLOCK_ZIP_ constants for
137 compressed-only pages @see buf_block_state_valid() */
138 BUF_BLOCK_NOT_USED,
139
140 /** When buf_LRU_get_free_block returns a block, it is in this state */
141 BUF_BLOCK_READY_FOR_USE,
142
143 /** Contains a buffered file page */
144 BUF_BLOCK_FILE_PAGE,
145
146 /** Contains some main memory object */
147 BUF_BLOCK_MEMORY,
148
149 /** Hash index should be removed before putting to the free list */
150 BUF_BLOCK_REMOVE_HASH
151 };
152
153 const std::unordered_map<buf_page_state, std::string_view> buf_page_state_str{
154 /* First three states are for compression pages and are not states we would
155 get as we scan pages through buffer blocks */
156 {BUF_BLOCK_POOL_WATCH, ""},
157 {BUF_BLOCK_ZIP_PAGE, ""},
158 {BUF_BLOCK_ZIP_DIRTY, ""},
159 {BUF_BLOCK_NOT_USED, "NOT_USED"},
160 {BUF_BLOCK_READY_FOR_USE, "READY_FOR_USE"},
161 {BUF_BLOCK_FILE_PAGE, "FILE_PAGE"},
162 {BUF_BLOCK_MEMORY, "MEMORY"},
163 {BUF_BLOCK_REMOVE_HASH, "REMOVE_HASH"}};
164
165 /** This structure defines information we will fetch from each buffer pool. It
166 will be used to print table IO stats */
167 struct buf_pool_info_t {
168 /* General buffer pool info */
169 ulint pool_unique_id; /*!< Buffer Pool ID */
170 ulint pool_size; /*!< Buffer Pool size in pages */
171 ulint lru_len; /*!< Length of buf_pool->LRU */
172 ulint old_lru_len; /*!< buf_pool->LRU_old_len */
173 ulint free_list_len; /*!< Length of buf_pool->free list */
174 ulint flush_list_len; /*!< Length of buf_pool->flush_list */
175 ulint n_pend_unzip; /*!< buf_pool->n_pend_unzip, pages
176 pending decompress */
177 ulint n_pend_reads; /*!< buf_pool->n_pend_reads, pages
178 pending read */
179 ulint n_pending_flush_lru; /*!< Pages pending flush in LRU */
180 ulint n_pending_flush_single_page; /*!< Pages pending to be
181 flushed as part of single page
182 flushes issued by various user
183 threads */
184 ulint n_pending_flush_list; /*!< Pages pending flush in FLUSH
185 LIST */
186 ulint n_pages_made_young; /*!< number of pages made young */
187 ulint n_pages_not_made_young; /*!< number of pages not made young */
188 ulint n_pages_read; /*!< buf_pool->n_pages_read */
189 ulint n_pages_created; /*!< buf_pool->n_pages_created */
190 ulint n_pages_written; /*!< buf_pool->n_pages_written */
191 ulint n_page_gets; /*!< buf_pool->n_page_gets */
192 ulint n_ra_pages_read_rnd; /*!< buf_pool->n_ra_pages_read_rnd,
193 number of pages readahead */
194 ulint n_ra_pages_read; /*!< buf_pool->n_ra_pages_read, number
195 of pages readahead */
196 ulint n_ra_pages_evicted; /*!< buf_pool->n_ra_pages_evicted,
197 number of readahead pages evicted
198 without access */
199 ulint n_page_get_delta; /*!< num of buffer pool page gets since
200 last printout */
201
202 /* Buffer pool access stats */
203 double page_made_young_rate; /*!< page made young rate in pages
204 per second */
205 double page_not_made_young_rate; /*!< page not made young rate
206 in pages per second */
207 double pages_read_rate; /*!< num of pages read per second */
208 double pages_created_rate; /*!< num of pages create per second */
209 double pages_written_rate; /*!< num of pages written per second */
210 ulint page_read_delta; /*!< num of pages read since last
211 printout */
212 ulint young_making_delta; /*!< num of pages made young since
213 last printout */
214 ulint not_young_making_delta; /*!< num of pages not make young since
215 last printout */
216
217 /* Statistics about read ahead algorithm. */
218 double pages_readahead_rnd_rate; /*!< random readahead rate in pages per
219 second */
220 double pages_readahead_rate; /*!< readahead rate in pages per
221 second */
222 double pages_evicted_rate; /*!< rate of readahead page evicted
223 without access, in pages per second */
224
225 /* Stats about LRU eviction */
226 ulint unzip_lru_len; /*!< length of buf_pool->unzip_LRU
227 list */
228 /* Counters for LRU policy */
229 ulint io_sum; /*!< buf_LRU_stat_sum.io */
230 ulint io_cur; /*!< buf_LRU_stat_cur.io, num of IO
231 for current interval */
232 ulint unzip_sum; /*!< buf_LRU_stat_sum.unzip */
233 ulint unzip_cur; /*!< buf_LRU_stat_cur.unzip, num
234 pages decompressed in current
235 interval */
236 };
237
238 /** The occupied bytes of lists in all buffer pools */
239 struct buf_pools_list_size_t {
240 ulint LRU_bytes; /*!< LRU size in bytes */
241 ulint unzip_LRU_bytes; /*!< unzip_LRU size in bytes */
242 ulint flush_list_bytes; /*!< flush_list size in bytes */
243 };
244
245 #ifndef UNIV_HOTBACKUP
246 /** Creates the buffer pool.
247 @param[in] total_size Size of the total pool in bytes.
248 @param[in] n_instances Number of buffer pool instances to create.
249 @return DB_SUCCESS if success, DB_ERROR if not enough memory or error */
250 dberr_t buf_pool_init(ulint total_size, ulint n_instances);
251
252 /** Frees the buffer pool at shutdown. This must not be invoked before
253 freeing all mutexes. */
254 void buf_pool_free_all();
255
256 /** Determines if a block is intended to be withdrawn. The caller must ensure
257 that there was a sufficient memory barrier to read curr_size and old_size.
258 @param[in] buf_pool buffer pool instance
259 @param[in] block pointer to control block
260 @retval true if will be withdrawn */
261 bool buf_block_will_withdrawn(buf_pool_t *buf_pool, const buf_block_t *block);
262
263 /** Determines if a frame is intended to be withdrawn. The caller must ensure
264 that there was a sufficient memory barrier to read curr_size and old_size.
265 @param[in] buf_pool buffer pool instance
266 @param[in] ptr pointer to a frame
267 @retval true if will be withdrawn */
268 bool buf_frame_will_withdrawn(buf_pool_t *buf_pool, const byte *ptr);
269
270 /** This is the thread for resizing buffer pool. It waits for an event and
271 when waked up either performs a resizing and sleeps again. */
272 void buf_resize_thread();
273
274 /** Checks if innobase_should_madvise_buf_pool() value has changed since we've
275 last check and if so, then updates buf_pool_should_madvise and calls madvise
276 for all chunks in all srv_buf_pool_instances.
277 @see buf_pool_should_madvise comment for a longer explanation. */
278 void buf_pool_update_madvise();
279
280 /** Clears the adaptive hash index on all pages in the buffer pool. */
281 void buf_pool_clear_hash_index(void);
282
283 /** Gets the current size of buffer buf_pool in bytes.
284 @return size in bytes */
285 static inline ulint buf_pool_get_curr_size(void);
286 /** Gets the current size of buffer buf_pool in frames.
287 @return size in pages */
288 static inline ulint buf_pool_get_n_pages(void);
289 #endif /* !UNIV_HOTBACKUP */
290
291 /** Gets the smallest oldest_modification lsn among all of the earliest
292 added pages in flush lists. In other words - takes the last dirty page
293 from each flush list, and calculates minimum oldest_modification among
294 all of them. Does not acquire global lock for the whole process, so the
295 result might come from inconsistent view on flush lists.
296
297 @note Note that because of the relaxed order in each flush list, this
298 functions no longer returns the smallest oldest_modification among all
299 of the dirty pages. If you wanted to have a safe lsn, which is smaller
300 than every oldest_modification, you would need to use another function:
301 buf_pool_get_oldest_modification_lwm().
302
303 Returns zero if there were no dirty pages (flush lists were empty).
304
305 @return minimum oldest_modification of last pages from flush lists,
306 zero if flush lists were empty */
307 lsn_t buf_pool_get_oldest_modification_approx(void);
308
309 /** Gets a safe low watermark for oldest_modification. It's guaranteed
310 that there were no dirty pages with smaller oldest_modification in the
311 whole flush lists.
312
313 Returns zero if flush lists were empty, be careful in such case, because
314 taking the newest lsn is probably not a good idea. If you wanted to rely
315 on some lsn in such case, you would need to follow pattern:
316
317 dpa_lsn = log_buffer_dirty_pages_added_up_to_lsn(*log_sys);
318
319 lwm_lsn = buf_pool_get_oldest_modification_lwm();
320
321 if (lwm_lsn == 0) lwm_lsn = dpa_lsn;
322
323 The order is important to avoid race conditions.
324
325 @remarks
326 It's guaranteed that the returned value will not be smaller than the
327 last checkpoint lsn. It's not guaranteed that the returned value is
328 the maximum possible. It's just the best effort for the low cost.
329 It basically takes result of buf_pool_get_oldest_modification_approx()
330 and subtracts maximum possible lag introduced by relaxed order in
331 flush lists (srv_log_recent_closed_size).
332
333 @return safe low watermark for oldest_modification of dirty pages,
334 or zero if flush lists were empty; if non-zero, it is then
335 guaranteed not to be at block boundary (and it points to lsn
336 inside data fragment of block) */
337 lsn_t buf_pool_get_oldest_modification_lwm(void);
338
339 #ifndef UNIV_HOTBACKUP
340
341 /** Allocates a buf_page_t descriptor. This function must succeed. In case
342 of failure we assert in this function. */
343 static inline buf_page_t *buf_page_alloc_descriptor(void)
344 MY_ATTRIBUTE((malloc));
345
346 /** Free a buf_page_t descriptor.
347 @param[in] bpage bpage descriptor to free */
348 void buf_page_free_descriptor(buf_page_t *bpage);
349
350 /** Allocates a buffer block.
351 @return own: the allocated block, in state BUF_BLOCK_MEMORY */
352 buf_block_t *buf_block_alloc(
353 buf_pool_t *buf_pool); /*!< in: buffer pool instance,
354 or NULL for round-robin selection
355 of the buffer pool */
356 /** Frees a buffer block which does not contain a file page. */
357 static inline void buf_block_free(
358 buf_block_t *block); /*!< in, own: block to be freed */
359 #endif /* !UNIV_HOTBACKUP */
360
361 /** Copies contents of a buffer frame to a given buffer.
362 @param[in] buf buffer to copy to
363 @param[in] frame buffer frame
364 @return buf */
365 static inline byte *buf_frame_copy(byte *buf, const buf_frame_t *frame);
366
367 #ifndef UNIV_HOTBACKUP
368 /** This is the general function used to get optimistic access to a database
369 page.
370 @param[in] rw_latch RW_S_LATCH, RW_X_LATCH
371 @param[in,out] block Guessed block
372 @param[in] modify_clock Modify clock value
373 @param[in] fetch_mode Fetch mode
374 @param[in] file File name
375 @param[in] line Line where called
376 @param[in,out] mtr Mini-transaction
377 @return true if success */
378 bool buf_page_optimistic_get(ulint rw_latch, buf_block_t *block,
379 uint64_t modify_clock, Page_fetch fetch_mode,
380 const char *file, ulint line, mtr_t *mtr);
381
382 /** This is used to get access to a known database page, when no waiting can be
383 done.
384 @param[in] rw_latch RW_S_LATCH or RW_X_LATCH.
385 @param[in] block The known page.
386 @param[in] hint Cache_hint::MAKE_YOUNG or Cache_hint::KEEP_OLD
387 @param[in] file File name from where it was called.
388 @param[in] line Line from where it was called.
389 @param[in,out] mtr Mini-transaction covering the fetch
390 @return true if success */
391 bool buf_page_get_known_nowait(ulint rw_latch, buf_block_t *block,
392 Cache_hint hint, const char *file, ulint line,
393 mtr_t *mtr);
394
395 /** Given a tablespace id and page number tries to get that page. If the
396 page is not in the buffer pool it is not loaded and NULL is returned.
397 Suitable for using when holding the lock_sys latches (as it avoids deadlock).
398 @param[in] page_id page Id
399 @param[in] location Location where called
400 @param[in] mtr Mini-transaction
401 @return pointer to a page or NULL */
402 const buf_block_t *buf_page_try_get(const page_id_t &page_id,
403 ut::Location location, mtr_t *mtr);
404
405 /** Get read access to a compressed page (usually of type
406 FIL_PAGE_TYPE_ZBLOB or FIL_PAGE_TYPE_ZBLOB2).
407 The page must be released with buf_page_release_zip().
408 NOTE: the page is not protected by any latch. Mutual exclusion has to
409 be implemented at a higher level. In other words, all possible
410 accesses to a given page through this function must be protected by
411 the same set of mutexes or latches.
412 @param[in] page_id page id
413 @param[in] page_size page size
414 @return pointer to the block */
415 buf_page_t *buf_page_get_zip(const page_id_t &page_id,
416 const page_size_t &page_size);
417
418 /** This is the general function used to get access to a database page.
419 @param[in] page_id Page id
420 @param[in] page_size Page size
421 @param[in] rw_latch RW_S_LATCH, RW_X_LATCH, RW_NO_LATCH
422 @param[in] guess Guessed block or NULL
423 @param[in] mode Fetch mode.
424 @param[in] location Location from where this method was called.
425 @param[in] mtr Mini-transaction
426 @param[in] dirty_with_no_latch Mark page as dirty even if page is being
427 pinned without any latch
428 @return pointer to the block or NULL */
429 buf_block_t *buf_page_get_gen(const page_id_t &page_id,
430 const page_size_t &page_size, ulint rw_latch,
431 buf_block_t *guess, Page_fetch mode,
432 ut::Location location, mtr_t *mtr,
433 bool dirty_with_no_latch = false);
434
435 /** NOTE! The following macros should be used instead of buf_page_get_gen,
436 to improve debugging. Only values RW_S_LATCH and RW_X_LATCH are allowed
437 in LA! */
438 inline buf_block_t *buf_page_get(const page_id_t &id, const page_size_t &size,
439 ulint latch, ut::Location location,
440 mtr_t *mtr) {
441 return buf_page_get_gen(id, size, latch, nullptr, Page_fetch::NORMAL,
442 location, mtr);
443 }
444 /** Use these macros to bufferfix a page with no latching. Remember not to
445 read the contents of the page unless you know it is safe. Do not modify
446 the contents of the page! We have separated this case, because it is
447 error-prone programming not to set a latch, and it should be used
448 with care. */
449 inline buf_block_t *buf_page_get_with_no_latch(const page_id_t &id,
450 const page_size_t &size,
451 ut::Location location,
452 mtr_t *mtr) {
453 return buf_page_get_gen(id, size, RW_NO_LATCH, nullptr, Page_fetch::NO_LATCH,
454 location, mtr);
455 }
456
457 /** Initializes a page to the buffer buf_pool. The page is usually not read
458 from a file even if it cannot be found in the buffer buf_pool. This is one
459 of the functions which perform to a block a state transition NOT_USED =>
460 FILE_PAGE (the other is buf_page_get_gen). The page is latched by passed mtr.
461 @param[in] page_id Page id
462 @param[in] page_size Page size
463 @param[in] rw_latch RW_SX_LATCH, RW_X_LATCH
464 @param[in] mtr Mini-transaction
465 @return pointer to the block, page bufferfixed */
466 buf_block_t *buf_page_create(const page_id_t &page_id,
467 const page_size_t &page_size,
468 rw_lock_type_t rw_latch, mtr_t *mtr);
469
470 #else /* !UNIV_HOTBACKUP */
471
472 /** Inits a page to the buffer buf_pool, for use in mysqlbackup --restore.
473 @param[in] page_id page id
474 @param[in] page_size page size
475 @param[in,out] block block to init */
476 void meb_page_init(const page_id_t &page_id, const page_size_t &page_size,
477 buf_block_t *block);
478 #endif /* !UNIV_HOTBACKUP */
479
480 #ifndef UNIV_HOTBACKUP
481 /** Releases a compressed-only page acquired with buf_page_get_zip(). */
482 static inline void buf_page_release_zip(
483 buf_page_t *bpage); /*!< in: buffer block */
484
485 /** Releases a latch, if specified.
486 @param[in] block buffer block
487 @param[in] rw_latch RW_S_LATCH, RW_X_LATCH, RW_NO_LATCH */
488 static inline void buf_page_release_latch(buf_block_t *block, ulint rw_latch);
489
490 /** Moves a page to the start of the buffer pool LRU list. This high-level
491 function can be used to prevent an important page from slipping out of
492 the buffer pool.
493 @param[in,out] bpage buffer block of a file page */
494 void buf_page_make_young(buf_page_t *bpage);
495
496 /** Moved a page to the end of the buffer pool LRU list so that it can be
497 flushed out at the earliest.
498 @param[in] bpage buffer block of a file page */
499 void buf_page_make_old(buf_page_t *bpage);
500
501 /** Returns true if the page can be found in the buffer pool hash table.
502 NOTE that it is possible that the page is not yet read from disk,
503 though.
504 @param[in] page_id page id
505 @return true if found in the page hash table */
506 static inline bool buf_page_peek(const page_id_t &page_id);
507
508 #ifdef UNIV_DEBUG
509
510 /** Sets file_page_was_freed true if the page is found in the buffer pool.
511 This function should be called when we free a file page and want the
512 debug version to check that it is not accessed any more unless
513 reallocated.
514 @param[in] page_id page id
515 @return control block if found in page hash table, otherwise NULL */
516 buf_page_t *buf_page_set_file_page_was_freed(const page_id_t &page_id);
517
518 /** Sets file_page_was_freed false if the page is found in the buffer pool.
519 This function should be called when we free a file page and want the
520 debug version to check that it is not accessed any more unless
521 reallocated.
522 @param[in] page_id page id
523 @return control block if found in page hash table, otherwise NULL */
524 buf_page_t *buf_page_reset_file_page_was_freed(const page_id_t &page_id);
525
526 #endif /* UNIV_DEBUG */
527 /** Reads the freed_page_clock of a buffer block.
528 @return freed_page_clock */
529 [[nodiscard]] static inline ulint buf_page_get_freed_page_clock(
530 const buf_page_t *bpage); /*!< in: block */
531 /** Reads the freed_page_clock of a buffer block.
532 @return freed_page_clock */
533 [[nodiscard]] static inline ulint buf_block_get_freed_page_clock(
534 const buf_block_t *block); /*!< in: block */
535
536 /** Tells, for heuristics, if a block is still close enough to the MRU end of
537 the LRU list meaning that it is not in danger of getting evicted and also
538 implying that it has been accessed recently.
539 The page must be either buffer-fixed, either its page hash must be locked.
540 @param[in] bpage block
541 @return true if block is close to MRU end of LRU */
542 static inline bool buf_page_peek_if_young(const buf_page_t *bpage);
543
544 /** Recommends a move of a block to the start of the LRU list if there is
545 danger of dropping from the buffer pool.
546 NOTE: does not reserve the LRU list mutex.
547 @param[in] bpage block to make younger
548 @return true if should be made younger */
549 static inline bool buf_page_peek_if_too_old(const buf_page_t *bpage);
550
551 /** Gets the youngest modification log sequence number for a frame.
552 Returns zero if not file page or no modification occurred yet.
553 @return newest modification to page */
554 static inline lsn_t buf_page_get_newest_modification(
555 const buf_page_t *bpage); /*!< in: block containing the
556 page frame */
557
558 /** Increment the modify clock.
559 The caller must
560 (1) own the buf_pool->mutex and block bufferfix count has to be zero,
561 (2) own X or SX latch on the block->lock, or
562 (3) operate on a thread-private temporary table
563 @param[in,out] block buffer block */
564 static inline void buf_block_modify_clock_inc(buf_block_t *block);
565
566 /** Increments the bufferfix count.
567 @param[in] location location
568 @param[in,out] block block to bufferfix */
569 static inline void buf_block_buf_fix_inc_func(IF_DEBUG(ut::Location location, )
570 buf_block_t *block);
571
572 /** Increments the bufferfix count.
573 @param[in,out] bpage block to bufferfix
574 @return the count */
575 static inline ulint buf_block_fix(buf_page_t *bpage);
576
577 /** Increments the bufferfix count.
578 @param[in,out] block block to bufferfix
579 @return the count */
580 static inline ulint buf_block_fix(buf_block_t *block);
581
582 /** Decrements the bufferfix count.
583 @param[in,out] bpage block to bufferunfix
584 @return the remaining buffer-fix count */
585 static inline ulint buf_block_unfix(buf_page_t *bpage);
586
587 /** Decrements the bufferfix count.
588 @param[in,out] block block to bufferunfix
589 @return the remaining buffer-fix count */
590 static inline ulint buf_block_unfix(buf_block_t *block);
591
592 /** Unfixes the page, unlatches the page,
593 removes it from page_hash and removes it from LRU.
594 @param[in,out] bpage pointer to the block */
595 void buf_read_page_handle_error(buf_page_t *bpage);
596
597 /** Increments the bufferfix count.
598 @param[in,out] b block to bufferfix
599 @param[in] l location where requested */
600 inline void buf_block_buf_fix_inc(buf_block_t *b,
601 ut::Location l [[maybe_unused]]) {
602 buf_block_buf_fix_inc_func(IF_DEBUG(l, ) b);
603 }
604 #else /* !UNIV_HOTBACKUP */
605 static inline void buf_block_modify_clock_inc(buf_block_t *block) {}
606 #endif /* !UNIV_HOTBACKUP */
607
608 #ifndef UNIV_HOTBACKUP
609
610 /** Gets the space id, page offset, and byte offset within page of a pointer
611 pointing to a buffer frame containing a file page.
612 @param[in] ptr pointer to a buffer frame
613 @param[out] space space id
614 @param[out] addr page offset and byte offset */
615 static inline void buf_ptr_get_fsp_addr(const void *ptr, space_id_t *space,
616 fil_addr_t *addr);
617
618 #ifdef UNIV_DEBUG
619 /** Finds a block in the buffer pool that points to a
620 given compressed page. Used only to confirm that buffer pool does not contain a
621 given pointer, thus protected by zip_free_mutex.
622 @param[in] buf_pool buffer pool instance
623 @param[in] data pointer to compressed page
624 @return buffer block pointing to the compressed page, or NULL */
625 buf_block_t *buf_pool_contains_zip(buf_pool_t *buf_pool, const void *data);
626 #endif /* UNIV_DEBUG */
627
628 /***********************************************************************
629 FIXME_FTS: Gets the frame the pointer is pointing to. */
630 static inline buf_frame_t *buf_frame_align(
631 /* out: pointer to frame */
632 byte *ptr); /* in: pointer to a frame */
633
634 #if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
635 /** Validates the buffer pool data structure.
636 @return true */
637 bool buf_validate(void);
638 #endif /* UNIV_DEBUG || UNIV_BUF_DEBUG */
639 #if defined UNIV_DEBUG_PRINT || defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
640 /** Prints info of the buffer pool data structure. */
641 void buf_print(void);
642 #endif /* UNIV_DEBUG_PRINT || UNIV_DEBUG || UNIV_BUF_DEBUG */
643 #endif /* !UNIV_HOTBACKUP */
644 enum buf_page_print_flags {
645 /** Do not crash at the end of buf_page_print(). */
646 BUF_PAGE_PRINT_NO_CRASH = 1,
647 /** Do not print the full page dump. */
648 BUF_PAGE_PRINT_NO_FULL = 2
649 };
650
651 /** Prints a page to stderr.
652 @param[in] read_buf a database page
653 @param[in] page_size page size
654 @param[in] flags 0 or BUF_PAGE_PRINT_NO_CRASH or
655 BUF_PAGE_PRINT_NO_FULL */
656 void buf_page_print(const byte *read_buf, const page_size_t &page_size,
657 ulint flags);
658
659 /** Decompress a block.
660 @return true if successful */
661 bool buf_zip_decompress(buf_block_t *block, /*!< in/out: block */
662 bool check); /*!< in: true=verify the page checksum */
663 #ifndef UNIV_HOTBACKUP
664 #ifdef UNIV_DEBUG
665 /** Returns the number of latched pages in the buffer pool.
666 @return number of latched pages */
667 ulint buf_get_latched_pages_number(void);
668 #endif /* UNIV_DEBUG */
669 /** Returns the number of pending buf pool read ios.
670 @return number of pending read I/O operations */
671 ulint buf_get_n_pending_read_ios(void);
672 /** Prints info of the buffer i/o. */
673 void buf_print_io(FILE *file); /*!< in: file where to print */
674 /** Collect buffer pool stats information for a buffer pool. Also
675 record aggregated stats if there are more than one buffer pool
676 in the server */
677 void buf_stats_get_pool_info(
678 buf_pool_t *buf_pool, /*!< in: buffer pool */
679 ulint pool_id, /*!< in: buffer pool ID */
680 buf_pool_info_t *all_pool_info); /*!< in/out: buffer pool info
681 to fill */
682 /** Return the ratio in percents of modified pages in the buffer pool /
683 database pages in the buffer pool.
684 @return modified page percentage ratio */
685 double buf_get_modified_ratio_pct(void);
686 /** Refresh the statistics used to print per-second averages. */
687 void buf_refresh_io_stats_all();
688
689 /** Assert that all file pages in the buffer are in a replaceable state. */
690 void buf_assert_all_are_replaceable();
691
692 /** Computes number of pending I/O read operations for the buffer pool.
693 @return number of pending i/o reads */
694 size_t buf_pool_pending_io_reads_count();
695
696 /** Computes number of pending I/O write operations for the buffer pool.
697 @return number of pending i/o writes */
698 size_t buf_pool_pending_io_writes_count();
699
700 /** Waits until there are no pending I/O operations for the buffer pool.
701 Keep waiting in loop with sleeps, emitting information every minute.
702 This is used to avoid risk of some pending async IO (e.g. enqueued by
703 the linear read-ahead), which would involve ibuf merge and create new
704 redo records. */
705 void buf_pool_wait_for_no_pending_io();
706
707 /** Invalidates the file pages in the buffer pool when an archive recovery is
708 completed. All the file pages buffered must be in a replaceable state when
709 this function is called: not latched and not modified. */
710 void buf_pool_invalidate(void);
711
712 /*========================================================================
713 --------------------------- LOWER LEVEL ROUTINES -------------------------
714 =========================================================================*/
715
716 #ifdef UNIV_DEBUG
717 /** Adds latch level info for the rw-lock protecting the buffer frame. This
718 should be called in the debug version after a successful latching of a page if
719 we know the latching order level of the acquired latch.
720 @param[in] block buffer page where we have acquired latch
721 @param[in] level latching order level */
722 static inline void buf_block_dbg_add_level(buf_block_t *block,
723 latch_level_t level);
724 #else /* UNIV_DEBUG */
725 #define buf_block_dbg_add_level(block, level) /* nothing */
726 #endif /* UNIV_DEBUG */
727
728 #endif /* !UNIV_HOTBACKUP */
729
730 /** Gets the state of a block.
731 @return state */
732 static inline enum buf_page_state buf_page_get_state(
733 const buf_page_t *bpage); /*!< in: pointer to the control block */
734 /** Gets the state of a block.
735 @return state */
736 [[nodiscard]] static inline enum buf_page_state buf_block_get_state(
737 const buf_block_t *block); /*!< in: pointer to the control block */
738
739 /** Sets the state of a block.
740 @param[in,out] bpage pointer to control block
741 @param[in] state state */
742 static inline void buf_page_set_state(buf_page_t *bpage,
743 enum buf_page_state state);
744
745 /** Sets the state of a block.
746 @param[in,out] block pointer to control block
747 @param[in] state state */
748 static inline void buf_block_set_state(buf_block_t *block,
749 enum buf_page_state state);
750
751 /** Determines if a block is mapped to a tablespace.
752 @return true if mapped */
753 [[nodiscard]] static inline bool buf_page_in_file(
754 const buf_page_t *bpage); /*!< in: pointer to control block */
755 #ifndef UNIV_HOTBACKUP
756 /** Determines if a block should be on unzip_LRU list.
757 @return true if block belongs to unzip_LRU */
758 [[nodiscard]] static inline bool buf_page_belongs_to_unzip_LRU(
759 const buf_page_t *bpage); /*!< in: pointer to control block */
760
761 /** Gets the mutex of a block.
762 @return pointer to mutex protecting bpage */
763 [[nodiscard]] static inline BPageMutex *buf_page_get_mutex(
764 const buf_page_t *bpage); /*!< in: pointer to control block */
765
766 /** Get the flush type of a page.
767 @return flush type */
768 [[nodiscard]] static inline buf_flush_t buf_page_get_flush_type(
769 const buf_page_t *bpage); /*!< in: buffer page */
770
771 /** Set the flush type of a page.
772 @param[in] bpage buffer page
773 @param[in] flush_type flush type */
774 static inline void buf_page_set_flush_type(buf_page_t *bpage,
775 buf_flush_t flush_type);
776
777 /** Map a block to a file page.
778 @param[in,out] block pointer to control block
779 @param[in] page_id page id */
780 static inline void buf_block_set_file_page(buf_block_t *block,
781 const page_id_t &page_id);
782
783 /** Gets the io_fix state of a block.
784 @return io_fix state */
785 [[nodiscard]] static inline enum buf_io_fix buf_page_get_io_fix(
786 const buf_page_t *bpage); /*!< in: pointer to the control block */
787 /** Gets the io_fix state of a block.
788 @return io_fix state */
789 [[nodiscard]] static inline enum buf_io_fix buf_block_get_io_fix(
790 const buf_block_t *block); /*!< in: pointer to the control block */
791
792 /** Sets the io_fix state of a block.
793 @param[in,out] bpage control block
794 @param[in] io_fix io_fix state */
795 static inline void buf_page_set_io_fix(buf_page_t *bpage,
796 enum buf_io_fix io_fix);
797
798 /** Sets the io_fix state of a block.
799 @param[in,out] block control block
800 @param[in] io_fix io_fix state */
801 static inline void buf_block_set_io_fix(buf_block_t *block,
802 enum buf_io_fix io_fix);
803
804 /** Makes a block sticky. A sticky block implies that even after we release
805 the buf_pool->LRU_list_mutex and the block->mutex:
806 * it cannot be removed from the flush_list
807 * the block descriptor cannot be relocated
808 * it cannot be removed from the LRU list
809 Note that:
810 * the block can still change its position in the LRU list
811 * the next and previous pointers can change.
812 @param[in,out] bpage control block */
813 static inline void buf_page_set_sticky(buf_page_t *bpage);
814
815 /** Removes stickiness of a block. */
816 static inline void buf_page_unset_sticky(
817 buf_page_t *bpage); /*!< in/out: control block */
818 /** Determine if a buffer block can be relocated in memory. The block
819 can be dirty, but it must not be I/O-fixed or bufferfixed. */
820 [[nodiscard]] static inline bool buf_page_can_relocate(
821 const buf_page_t *bpage); /*!< control block being relocated */
822
823 /** Determine if a block has been flagged old.
824 @param[in] bpage control block
825 @return true if old */
826 [[nodiscard]] static inline bool buf_page_is_old(const buf_page_t *bpage);
827
828 /** Flag a block old.
829 @param[in,out] bpage control block
830 @param[in] old old */
831 static inline void buf_page_set_old(buf_page_t *bpage, bool old);
832
833 /** Determine the time of first access of a block in the buffer pool.
834 @return Time of first access, zero if not accessed
835 */
836 [[nodiscard]] static inline std::chrono::steady_clock::time_point
837 buf_page_is_accessed(const buf_page_t *bpage); /*!< in: control block */
838 /** Flag a block accessed. */
839 static inline void buf_page_set_accessed(
840 buf_page_t *bpage); /*!< in/out: control block */
841
842 /** Gets the buf_block_t handle of a buffered file block if an uncompressed
843 page frame exists, or NULL. page frame exists, or NULL. The caller must hold
844 either the appropriate hash lock in any mode, either the LRU list mutex. Note:
845 even though bpage is not declared a const we don't update its value. It is safe
846 to make this pure.
847 @param[in] bpage control block, or NULL
848 @return control block, or NULL */
849 [[nodiscard]] static inline buf_block_t *buf_page_get_block(buf_page_t *bpage);
850 #ifdef UNIV_DEBUG
851 /** Gets a pointer to the memory frame of a block.
852 @return pointer to the frame */
853 [[nodiscard]] static inline buf_frame_t *buf_block_get_frame(
854 const buf_block_t *block); /*!< in: pointer to the control block */
855 #else /* UNIV_DEBUG */
856 #define buf_block_get_frame(block) (block)->frame
857 #endif /* UNIV_DEBUG */
858 #else /* !UNIV_HOTBACKUP */
859 #define buf_block_get_frame(block) (block)->frame
860 #endif /* !UNIV_HOTBACKUP */
861
862 /** Get a buffer block from an adaptive hash index pointer.
863 This function does not return if the block is not identified.
864 @param[in] ptr pointer to within a page frame
865 @return pointer to block, never NULL */
866 buf_block_t *buf_block_from_ahi(const byte *ptr);
867
868 /** Find out if a block pointer points into one of currently used chunks of
869 the buffer pool. This is useful if you stored the pointer some time ago, and
870 want to dereference it now, and are afraid that buffer pool resize could free
871 the memory pointed by it. Thus calling this function requires holding at least
872 one of the latches which prevent freeing memory from buffer pool for the
873 duration of the call and until you pin the block in some other way, as otherwise
874 the result of this function might be obsolete by the time you dereference the
875 block (an s-latch on buf_page_hash_lock_get for any hash cell is enough).
876 @param buf_pool The buffer pool instance to search in.
877 @param ptr A pointer which you want to check. This function will not
878 dereference it.
879 @return true iff `block` points inside one of the chunks of the `buf_pool`
880 */
881 bool buf_is_block_in_instance(const buf_pool_t *buf_pool,
882 const buf_block_t *ptr);
883
884 #ifndef UNIV_HOTBACKUP
885
886 /** Inits a page for read to the buffer buf_pool. If the page is
887 (1) already in buf_pool, or
888 (2) if we specify to read only ibuf pages and the page is not an ibuf page, or
889 (3) if the space is deleted or being deleted,
890 then this function does nothing.
891 Sets the io_fix flag to BUF_IO_READ and sets a non-recursive exclusive lock
892 on the buffer frame. The io-handler must take care that the flag is cleared
893 and the lock released later.
894 @param[in] mode BUF_READ_IBUF_PAGES_ONLY, ...
895 @param[in] page_id page id
896 @param[in] page_size page size
897 @param[in] unzip true=request uncompressed page
898 @return pointer to the block or NULL */
899 buf_page_t *buf_page_init_for_read(ulint mode, const page_id_t &page_id,
900 const page_size_t &page_size, bool unzip);
901
902 /** Completes an asynchronous read or write request of a file page to or from
903 the buffer pool.
904 @param[in] bpage pointer to the block in question
905 @param[in] evict whether or not to evict the page from LRU list
906 @return true if successful */
907 bool buf_page_io_complete(buf_page_t *bpage, bool evict);
908
909 /** Free a stale page. Caller must hold the LRU mutex. Upon successful page
910 free the LRU mutex will be released.
911 @param[in,out] buf_pool Buffer pool the page belongs to.
912 @param[in,out] bpage Page to free.
913 @return true if page was freed. */
914 bool buf_page_free_stale(buf_pool_t *buf_pool, buf_page_t *bpage) noexcept;
915
916 /** Free a stale page. Caller must be holding the hash_lock in S mode if
917 hash_lock parameter is not nullptr. The hash lock will be released upon return
918 always. Caller must hold the LRU mutex if and only if the hash_lock parameter
919 is nullptr. Upon unsuccessful page free the LRU mutex will not be released if
920 hash_lock is nullptr.
921 @param[in,out] buf_pool Buffer pool the page belongs to.
922 @param[in,out] bpage Page to free.
923 @param[in,out] hash_lock Hash lock covering the fetch from the hash table if
924 latched in S mode. nullptr otherwise.
925 @return true if page was freed. */
926 bool buf_page_free_stale(buf_pool_t *buf_pool, buf_page_t *bpage,
927 rw_lock_t *hash_lock) noexcept;
928
929 /** Free a stale page that is being written. The caller must be within the
930 page's write code path.
931 @param[in,out] bpage Page to free.
932 @param[in] owns_sx_lock SX lock on block->lock is set. */
933 void buf_page_free_stale_during_write(buf_page_t *bpage,
934 bool owns_sx_lock = false) noexcept;
935
936 /** Calculates the index of a buffer pool to the buf_pool[] array.
937 @return the position of the buffer pool in buf_pool[] */
938 [[nodiscard]] static inline ulint buf_pool_index(
939 const buf_pool_t *buf_pool); /*!< in: buffer pool */
940 /** Returns the buffer pool instance given a page instance
941 @return buf_pool */
942 static inline buf_pool_t *buf_pool_from_bpage(
943 const buf_page_t *bpage); /*!< in: buffer pool page */
944 /** Returns the buffer pool instance given a block instance
945 @return buf_pool */
946 static inline buf_pool_t *buf_pool_from_block(
947 const buf_block_t *block); /*!< in: block */
948
949 /** Returns the buffer pool instance given a page id.
950 @param[in] page_id page id
951 @return buffer pool */
952 static inline buf_pool_t *buf_pool_get(const page_id_t &page_id);
953
954 /** Returns the buffer pool instance given its array index
955 @return buffer pool */
956 static inline buf_pool_t *buf_pool_from_array(
957 ulint index); /*!< in: array index to get
958 buffer pool instance from */
959
960 /** Returns the control block of a file page, NULL if not found.
961 @param[in] buf_pool buffer pool instance
962 @param[in] page_id page id
963 @return block, NULL if not found */
964 static inline buf_page_t *buf_page_hash_get_low(buf_pool_t *buf_pool,
965 const page_id_t &page_id);
966
967 /** Returns the control block of a file page, NULL if not found.
968 If the block is found and lock is not NULL then the appropriate
969 page_hash lock is acquired in the specified lock mode. Otherwise,
970 mode value is ignored. It is up to the caller to release the
971 lock. If the block is found and the lock is NULL then the page_hash
972 lock is released by this function.
973 @param[in] buf_pool buffer pool instance
974 @param[in] page_id page id
975 @param[in,out] lock lock of the page hash acquired if bpage is
976 found, NULL otherwise. If NULL is passed then the hash_lock is released by
977 this function.
978 @param[in] lock_mode RW_LOCK_X or RW_LOCK_S. Ignored if
979 lock == NULL
980 @param[in] watch if true, return watch sentinel also.
981 @return pointer to the bpage or NULL; if NULL, lock is also NULL or
982 a watch sentinel. */
983 static inline buf_page_t *buf_page_hash_get_locked(buf_pool_t *buf_pool,
984 const page_id_t &page_id,
985 rw_lock_t **lock,
986 ulint lock_mode,
987 bool watch = false);
988
989 /** Returns the control block of a file page, NULL if not found.
990 If the block is found and lock is not NULL then the appropriate
991 page_hash lock is acquired in the specified lock mode. Otherwise,
992 mode value is ignored. It is up to the caller to release the
993 lock. If the block is found and the lock is NULL then the page_hash
994 lock is released by this function.
995 @param[in] buf_pool buffer pool instance
996 @param[in] page_id page id
997 @param[in,out] lock lock of the page hash acquired if bpage is
998 found, NULL otherwise. If NULL is passed then the hash_lock is released by
999 this function.
1000 @param[in] lock_mode RW_LOCK_X or RW_LOCK_S. Ignored if
1001 lock == NULL
1002 @return pointer to the block or NULL; if NULL, lock is also NULL. */
1003 static inline buf_block_t *buf_block_hash_get_locked(buf_pool_t *buf_pool,
1004 const page_id_t &page_id,
1005 rw_lock_t **lock,
1006 ulint lock_mode);
1007
1008 /* There are four different ways we can try to get a bpage or block
1009 from the page hash:
1010 1) Caller already holds the appropriate page hash lock: in the case call
1011 buf_page_hash_get_low() function.
1012 2) Caller wants to hold page hash lock in x-mode
1013 3) Caller wants to hold page hash lock in s-mode
1014 4) Caller doesn't want to hold page hash lock */
1015 inline buf_page_t *buf_page_hash_get_s_locked(buf_pool_t *b,
1016 const page_id_t &page_id,
1017 rw_lock_t **l) {
1018 return buf_page_hash_get_locked(b, page_id, l, RW_LOCK_S);
1019 }
1020 inline buf_page_t *buf_page_hash_get_x_locked(buf_pool_t *b,
1021 const page_id_t &page_id,
1022 rw_lock_t **l) {
1023 return buf_page_hash_get_locked(b, page_id, l, RW_LOCK_X);
1024 }
1025 inline buf_page_t *buf_page_hash_get(buf_pool_t *b, const page_id_t &page_id) {
1026 return buf_page_hash_get_locked(b, page_id, nullptr, 0);
1027 }
1028 inline buf_page_t *buf_page_get_also_watch(buf_pool_t *b,
1029 const page_id_t &page_id) {
1030 return buf_page_hash_get_locked(b, page_id, nullptr, 0, true);
1031 }
1032
1033 inline buf_block_t *buf_block_hash_get_s_locked(buf_pool_t *b,
1034 const page_id_t &page_id,
1035 rw_lock_t **l) {
1036 return buf_block_hash_get_locked(b, page_id, l, RW_LOCK_S);
1037 }
1038 inline buf_block_t *buf_block_hash_get_x_locked(buf_pool_t *b,
1039 const page_id_t &page_id,
1040 rw_lock_t **l) {
1041 return buf_block_hash_get_locked(b, page_id, l, RW_LOCK_X);
1042 }
1043 inline buf_block_t *buf_block_hash_get(buf_pool_t *b,
1044 const page_id_t &page_id) {
1045 return buf_block_hash_get_locked(b, page_id, nullptr, 0);
1046 }
1047
1048 /** Gets the current length of the free list of buffer blocks.
1049 @return length of the free list */
1050 ulint buf_get_free_list_len(void);
1051
1052 /** Determine if a block is a sentinel for a buffer pool watch.
1053 @param[in] buf_pool buffer pool instance
1054 @param[in] bpage block
1055 @return true if a sentinel for a buffer pool watch, false if not */
1056 [[nodiscard]] bool buf_pool_watch_is_sentinel(const buf_pool_t *buf_pool,
1057 const buf_page_t *bpage);
1058
1059 /** Stop watching if the page has been read in.
1060 buf_pool_watch_set(same_page_id) must have returned NULL before.
1061 @param[in] page_id page id */
1062 void buf_pool_watch_unset(const page_id_t &page_id);
1063
1064 /** Check if the page has been read in.
1065 This may only be called after buf_pool_watch_set(same_page_id)
1066 has returned NULL and before invoking buf_pool_watch_unset(same_page_id).
1067 @param[in] page_id page id
1068 @return false if the given page was not read in, true if it was */
1069 [[nodiscard]] bool buf_pool_watch_occurred(const page_id_t &page_id);
1070
1071 /** Get total buffer pool statistics.
1072 @param[out] LRU_len Length of all lru lists
1073 @param[out] free_len Length of all free lists
1074 @param[out] flush_list_len Length of all flush lists */
1075 void buf_get_total_list_len(ulint *LRU_len, ulint *free_len,
1076 ulint *flush_list_len);
1077
1078 /** Get total list size in bytes from all buffer pools. */
1079 void buf_get_total_list_size_in_bytes(
1080 buf_pools_list_size_t *buf_pools_list_size); /*!< out: list sizes
1081 in all buffer pools */
1082 /** Get total buffer pool statistics. */
1083 void buf_get_total_stat(
1084 buf_pool_stat_t *tot_stat); /*!< out: buffer pool stats */
1085
1086 /** Get the nth chunk's buffer block in the specified buffer pool.
1087 @param[in] buf_pool buffer pool instance
1088 @param[in] n nth chunk in the buffer pool
1089 @param[in] chunk_size chunk_size
1090 @return the nth chunk's buffer block. */
1091 static inline buf_block_t *buf_get_nth_chunk_block(const buf_pool_t *buf_pool,
1092 ulint n, ulint *chunk_size);
1093
1094 /** Calculate aligned buffer pool size based on srv_buf_pool_chunk_unit,
1095 if needed.
1096 @param[in] size size in bytes
1097 @return aligned size */
1098 static inline ulint buf_pool_size_align(ulint size);
1099
1100 /** Adjust the proposed chunk unit size so that it satisfies all invariants
1101 @param[in] size proposed size of buffer pool chunk unit in bytes
1102 @return adjusted size which meets invariants */
1103 ulonglong buf_pool_adjust_chunk_unit(ulonglong size);
1104
1105 /** Calculate the checksum of a page from compressed table and update the
1106 page.
1107 @param[in,out] page page to update
1108 @param[in] size compressed page size
1109 @param[in] lsn LSN to stamp on the page
1110 @param[in] skip_lsn_check true to skip check for lsn (in DEBUG) */
1111 void buf_flush_update_zip_checksum(buf_frame_t *page, ulint size, lsn_t lsn,
1112 bool skip_lsn_check);
1113
1114 /** Return how many more pages must be added to the withdraw list to reach the
1115 withdraw target of the currently ongoing buffer pool resize.
1116 @param[in] buf_pool buffer pool instance
1117 @return page count to be withdrawn or zero if the target is already achieved or
1118 if the buffer pool is not currently being resized. */
1119 static inline ulint buf_get_withdraw_depth(buf_pool_t *buf_pool);
1120
1121 #endif /* !UNIV_HOTBACKUP */
1122
1123 /** The common buffer control block structure
1124 for compressed and uncompressed frames */
1125
1126 /** Number of bits used for buffer page states. */
1127 constexpr uint32_t BUF_PAGE_STATE_BITS = 3;
1128
1129 template <typename T>
1130 class copyable_atomic_t : public std::atomic<T> {
1131 public:
1132 copyable_atomic_t(const copyable_atomic_t<T> &other)
1133 : std::atomic<T>(other.load(std::memory_order_relaxed)) {}
1134 };
1135
1136 using buf_fix_count_atomic_t = copyable_atomic_t<uint32_t>;
1137 class buf_page_t {
1138 public:
1139 /** Copy constructor.
1140 @param[in] other Instance to copy from. */
1141 buf_page_t(const buf_page_t &other)
1142 : id(other.id),
1143 size(other.size),
1144 buf_fix_count(other.buf_fix_count),
1145 io_fix(other.io_fix),
1146 state(other.state),
1147 flush_type(other.flush_type),
1148 buf_pool_index(other.buf_pool_index),
1149 #ifndef UNIV_HOTBACKUP
1150 hash(other.hash),
1151 #endif /* !UNIV_HOTBACKUP */
1152 list(other.list),
1153 newest_modification(other.newest_modification),
1154 oldest_modification(other.oldest_modification),
1155 LRU(other.LRU),
1156 zip(other.zip)
1157 #ifndef UNIV_HOTBACKUP
1158 ,
1159 m_flush_observer(other.m_flush_observer),
1160 m_space(other.m_space),
1161 freed_page_clock(other.freed_page_clock),
1162 m_version(other.m_version),
1163 access_time(other.access_time),
1164 m_dblwr_id(other.m_dblwr_id),
1165 old(other.old)
1166 #ifdef UNIV_DEBUG
1167 ,
1168 file_page_was_freed(other.file_page_was_freed),
1169 in_flush_list(other.in_flush_list),
1170 in_free_list(other.in_free_list),
1171 in_LRU_list(other.in_LRU_list),
1172 in_page_hash(other.in_page_hash),
1173 in_zip_hash(other.in_zip_hash)
1174 #endif /* UNIV_DEBUG */
1175 #endif /* !UNIV_HOTBACKUP */
1176 {
1177 #ifndef UNIV_HOTBACKUP
1178 m_space->inc_ref();
1179 #endif /* !UNIV_HOTBACKUP */
1180 }
1181
1182 #ifndef UNIV_HOTBACKUP
1183 /** Set the doublewrite buffer ID.
1184 @param[in] batch_id Double write batch ID that flushed the page. */
1185 void set_dblwr_batch_id(uint16_t batch_id) { m_dblwr_id = batch_id; }
1186
1187 /** @return the double write batch id, or uint16_t max if undefined. */
1188 [[nodiscard]] uint16_t get_dblwr_batch_id() const { return (m_dblwr_id); }
1189
1190 /** Retrieve the tablespace id.
1191 @return tablespace id */
1192 [[nodiscard]] space_id_t space() const noexcept { return id.space(); }
1193
1194 /** Retrieve the page number.
1195 @return page number */
1196 [[nodiscard]] page_no_t page_no() const noexcept { return id.page_no(); }
1197
1198 /** Checks if this space reference saved during last page ID initialization
1199 was deleted or truncated since.
1200 @return true when space reference stored leads was deleted or truncated and
1201 this page should be discarded. Result is up to date until the fil_shard mutex
1202 is released. */
1203 inline bool is_stale() const {
1204 ut_a(m_space != nullptr);
1205 ut_a(id.space() == m_space->id);
1206 ut_a(m_version <= m_space->get_current_version());
1207 if (m_version == m_space->get_current_version()) {
1208 ut_a(!m_space->is_deleted());
1209 return false;
1210 } else {
1211 return true;
1212 }
1213 }
1214
1215 /** Checks if this space reference saved during last page ID initialization
1216 was deleted or truncated since.
1217 @return true when space reference stored leads was deleted or truncated and
1218 this page should be discarded. When false is returned, the status of stale is
1219 checked to be guaranteed. */
1220 inline bool was_stale() const {
1221 ut_a(m_space != nullptr);
1222 ut_a(id.space() == m_space->id);
1223 /* If the the version is OK, then the space must not be deleted.
1224 However, version is modified before the deletion flag is set, so reading
1225 these values need to be executed in reversed order. The atomic reads
1226 cannot be relaxed for it to work. */
1227 bool was_not_deleted = m_space->was_not_deleted();
1228 if (m_version == m_space->get_recent_version()) {
1229 ut_a(was_not_deleted);
1230 return false;
1231 } else {
1232 return true;
1233 }
1234 }
1235
1236 /** Retrieve the tablespace object if one was available during page ID
1237 initialization. The returned object is safe to use as long as this buf_page_t
1238 object is not changed. Caller should have a IO fix, buffer fix, mutex or any
1239 other mean to assure the page will not be freed. After that is released the
1240 space object may be freed.
1241 @return tablespace object */
1242 inline fil_space_t *get_space() const { return m_space; }
1243
1244 /** Sets stored page ID to the new value. Handles space object reference
1245 count.
1246 @param[in] new_page_id new page ID to be set. */
1247 inline void reset_page_id(page_id_t new_page_id) {
1248 if (m_space != nullptr) {
1249 /* If we reach this line through a call chain:
1250 srv_shutdown -> buf_pool_free_all -> buf_pool_free_instance ->
1251 buf_page_free_descriptor, then we are already past the fil system
1252 shutdown, and all fil_space_t objects were already freed. */
1253 if (srv_shutdown_state.load() != SRV_SHUTDOWN_EXIT_THREADS) {
1254 m_space->dec_ref();
1255 }
1256 }
1257 id = new_page_id;
1258 space_id_changed();
1259 }
1260
1261 /** Sets stored value to invalid/empty value. Handles space object reference
1262 count. */
1263 inline void reset_page_id() {
1264 reset_page_id(page_id_t(UINT32_UNDEFINED, UINT32_UNDEFINED));
1265 }
1266
1267 private:
1268 /** Updates new space reference and acquires "reference count latch" and the
1269 current version of the space object. */
1270 inline void space_id_changed() {
1271 m_space = nullptr;
1272 m_version = 0;
1273 if (id.space() != UINT32_UNDEFINED) {
1274 m_space = fil_space_get(id.space());
1275 /* There could be non-existent tablespace while importing it */
1276 if (m_space) {
1277 m_space->inc_ref();
1278 /* We don't have a way to check the MDL locks, which are guarding the
1279 version number, so we don't use get_current_version(). */
1280 m_version = m_space->get_recent_version();
1281 }
1282 }
1283 }
1284
1285 public:
1286 /** @return the flush observer instance. */
1287 Flush_observer *get_flush_observer() noexcept { return m_flush_observer; }
1288
1289 /** Set the flush observer for the page.
1290 @param[in] flush_observer The flush observer to set. */
1291 void set_flush_observer(Flush_observer *flush_observer) noexcept {
1292 /* Don't allow to set flush observer from non-null to null, or from one
1293 observer to another. */
1294 ut_a(m_flush_observer == nullptr || m_flush_observer == flush_observer);
1295 m_flush_observer = flush_observer;
1296 }
1297
1298 /** Remove the flush observer. */
1299 void reset_flush_observer() noexcept { m_flush_observer = nullptr; }
1300 #endif /* !UNIV_HOTBACKUP */
1301
1302 /** @return the LSN of the latest modification. */
1303 lsn_t get_newest_lsn() const noexcept { return newest_modification; }
1304
1305 /** @return the LSN of the first modification since the last time
1306 it was clean. */
1307 lsn_t get_oldest_lsn() const noexcept { return oldest_modification; }
1308
1309 /** @return true if the page is dirty. */
1310 bool is_dirty() const noexcept { return get_oldest_lsn() > 0; }
1311
1312 /** Set the latest modification LSN.
1313 @param[in] lsn Latest modification lSN. */
1314 void set_newest_lsn(lsn_t lsn) noexcept { newest_modification = lsn; }
1315
1316 /** Set the LSN when the page is modified for the first time.
1317 @param[in] lsn First modification LSN. */
1318 void set_oldest_lsn(lsn_t lsn) noexcept;
1319
1320 /** Set page to clean state. */
1321 void set_clean() noexcept { set_oldest_lsn(0); }
1322
1323 /** @name General fields
1324 None of these bit-fields must be modified without holding
1325 buf_page_get_mutex() [buf_block_t::mutex or
1326 buf_pool->zip_mutex], since they can be stored in the same
1327 machine word. */
1328 /** @{ */
1329
1330 /** Page id. */
1331 page_id_t id;
1332
1333 /** Page size. */
1334 page_size_t size;
1335
1336 /** Count of how many fold this block is currently bufferfixed. */
1337 buf_fix_count_atomic_t buf_fix_count;
1338
1339 private:
1340 /** Type of pending I/O operation.
1341 Modified under protection of buf_page_get_mutex(this).
1342 Read under protection of rules described in @see Buf_io_fix_latching_rules */
1343 copyable_atomic_t<buf_io_fix> io_fix;
1344
1345 public:
1346 /** Checks if io_fix has any of the known enum values.
1347 @param[in] io_fix the value to test
1348 @return true iff io_fix has any of the known enum values
1349 */
1350 static bool is_correct_io_fix_value(buf_io_fix io_fix) {
1351 switch (io_fix) {
1352 case BUF_IO_NONE:
1353 case BUF_IO_READ:
1354 case BUF_IO_WRITE:
1355 case BUF_IO_PIN:
1356 return true;
1357 }
1358 return false;
1359 }
1360
1361 #ifdef UNIV_DEBUG
1362 private:
1363 /** Checks if io_fix has any of the known enum values.
1364 @return true iff io_fix has any of the known enum values
1365 */
1366 bool has_correct_io_fix_value() const {
1367 return is_correct_io_fix_value(io_fix);
1368 }
1369 /* Helper debug-only functions related latching rules are moved to a separate
1370 class so that this header doesn't have to pull in Stateful_latching_rules.*/
1371 class Latching_rules_helpers;
1372 friend class Latching_rules_helpers;
1373
1374 /* Helper debug-only class used to track which thread is currently responsible
1375 for performing I/O operation on this page. There's at most one such thread and
1376 the responsibility might be passed from one to another during async I/O. This
1377 is used to prove correctness of io_fix state transitions and checking it
1378 without a latch in the io_completion threads. */
1379 class io_responsibility_t {
1380 /** The thread responsible for I/O on this page, or an impossible value if
1381 no thread is currently responsible for I/O*/
1382 std::thread::id responsible_thread{std::thread().get_id()};
1383
1384 public:
1385 /** Checks if there is any thread responsible for I/O on this page now.
1386 @return true iff there is a thread responsible for I/O on this page.*/
1387 bool someone_is_responsible() const {
1388 return responsible_thread != std::thread().get_id();
1389 }
1390
1391 /** Checks if the current thread is responsible for I/O on this page now.
1392 @return true iff the current thread is responsible for I/O on this page.*/
1393 bool current_thread_is_responsible() const {
1394 return responsible_thread == std::this_thread::get_id();
1395 }
1396
1397 /** Called by the thread responsible for I/O on this page to release its
1398 responsibility. */
1399 void release() {
1400 ut_a(current_thread_is_responsible());
1401 responsible_thread = std::thread().get_id();
1402 }
1403
1404 /** Called by the thread which becomes responsible for I/O on this page to
1405 indicate that it takes the responsibility. */
1406 void take() {
1407 ut_a(!someone_is_responsible());
1408 responsible_thread = std::this_thread::get_id();
1409 }
1410 };
1411 /** Tracks which thread is responsible for I/O on this page. */
1412 io_responsibility_t io_responsibility;
1413
1414 public:
1415 /** Checks if there is any thread responsible for I/O on this page now.
1416 @return true iff there is a thread responsible for I/O on this page.*/
1417 bool someone_has_io_responsibility() const {
1418 return io_responsibility.someone_is_responsible();
1419 }
1420
1421 /** Checks if the current thread is responsible for I/O on this page now.
1422 @return true iff the current thread is responsible for I/O on this page.*/
1423 bool current_thread_has_io_responsibility() const {
1424 return io_responsibility.current_thread_is_responsible();
1425 }
1426
1427 /** Called by the thread responsible for I/O on this page to release its
1428 responsibility. */
1429 void release_io_responsibility() { io_responsibility.release(); }
1430
1431 /** Called by the thread which becomes responsible for I/O on this page to
1432 indicate that it takes the responsibility. */
1433 void take_io_responsibility() {
1434 ut_ad(mutex_own(buf_page_get_mutex(this)) ||
1435 io_fix.load(std::memory_order_relaxed) == BUF_IO_WRITE ||
1436 io_fix.load(std::memory_order_relaxed) == BUF_IO_READ);
1437 io_responsibility.take();
1438 }
1439 #endif /* UNIV_DEBUG */
1440 private:
1441 /** Retrieves a value of io_fix without requiring or acquiring any latches.
1442 Note that this implies that the value might be stale unless caller establishes
1443 happens-before relation in some other way.
1444 This is a low-level function which shouldn't be used directly, but
1445 rather via wrapper methods which check if proper latches are taken or via one
1446 of the many `was_io_fix_something()` methods with name explicitly warning the
1447 developer about the uncertainty involved.
1448 @return the value of io_fix at some moment "during" the call */
1449 buf_io_fix get_io_fix_snapshot() const {
1450 ut_ad(has_correct_io_fix_value());
1451 return io_fix.load(std::memory_order_relaxed);
1452 }
1453
1454 public:
1455 /** This is called only when having full ownership of the page object and no
1456 other thread can reach it. This currently happens during buf_pool_create(),
1457 buf_pool_resize() (which latch quite a lot) or from fil_tablespace_iterate()
1458 which creates a fake, private block which is not really a part of the buffer
1459 pool.
1460 Therefore we allow this function to set io_fix without checking for any
1461 latches.
1462 Please use set_io_fix(BUF_IO_NONE) to change state in a regular situation. */
1463 void init_io_fix() {
1464 io_fix.store(BUF_IO_NONE, std::memory_order_relaxed);
1465 /* This is only needed because places which call init_io_fix() do not call
1466 buf_page_t's constructor */
1467 ut_d(new (&io_responsibility) io_responsibility_t{});
1468 }
1469
1470 /** This is called only when having full ownership of the page object and no
1471 other thread can reach it. This currently happens during buf_page_init_low()
1472 under buf_page_get_mutex(this), on a previously initialized page for reuse,
1473 yet should be treated as initialization of the field, not a state transition.
1474 Please use set_io_fix(BUF_IO_NONE) to change state in a regular situation. */
1475 void reinit_io_fix() {
1476 ut_ad(io_fix.load(std::memory_order_relaxed) == BUF_IO_NONE);
1477 ut_ad(!someone_has_io_responsibility());
1478 io_fix.store(BUF_IO_NONE, std::memory_order_relaxed);
1479 }
1480
1481 /** Sets io_fix to specified value.
1482 Assumes the caller holds buf_page_get_mutex(this).
1483 Might require additional latches depending on particular state transition.
1484 Calls take_io_responsibility() or release_io_responsibility() as needed.
1485 @see Buf_io_fix_latching_rules for specific rules. */
1486 void set_io_fix(buf_io_fix io_fix);
1487
1488 /** Retrieves the current value of io_fix.
1489 Assumes the caller holds buf_page_get_mutex(this).
1490 @return the current value of io_fix */
1491 buf_io_fix get_io_fix() const {
1492 ut_ad(mutex_own(buf_page_get_mutex(this)));
1493 return get_io_fix_snapshot();
1494 }
1495
1496 /** Checks if the current value of io_fix is BUF_IO_WRITE.
1497 Assumes the caller holds buf_page_get_mutex(this) or some other latches which
1498 prevent state transition from/to BUF_IO_WRITE.
1499 @see Buf_io_fix_latching_rules for specific rules.
1500 @return true iff the current value of io_fix == BUF_IO_WRITE */
1501 bool is_io_fix_write() const;
1502
1503 /** Checks if the current value of io_fix is BUF_IO_READ.
1504 Assumes the caller holds buf_page_get_mutex(this) or some other latches which
1505 prevent state transition from/to BUF_IO_READ.
1506 @see Buf_io_fix_latching_rules for specific rules.
1507 @return true iff the current value of io_fix == BUF_IO_READ */
1508 bool is_io_fix_read() const;
1509
1510 /** Assuming that io_fix is either BUF_IO_READ or BUF_IO_WRITE determines
1511 which of the two it is. Additionally it assumes the caller holds
1512 buf_page_get_mutex(this) or some other latches which prevent state transition
1513 from BUF_IO_READ or from BUF_IO_WRITE to another state.
1514 @see Buf_io_fix_latching_rules for specific rules.
1515 @return true iff the current value of io_fix == BUF_IO_READ */
1516 bool is_io_fix_read_as_opposed_to_write() const;
1517
1518 /** Checks if io_fix is BUF_IO_READ without requiring or acquiring any
1519 latches.
1520 Note that this implies calling this function twice in a row could produce
1521 different results.
1522 @return true iff io_fix equal to BUF_IO_READ was noticed*/
1523 bool was_io_fix_read() const { return get_io_fix_snapshot() == BUF_IO_READ; }
1524
1525 /** Checks if io_fix is BUF_IO_FIX or BUF_IO_READ or BUF_IO_WRITE without
1526 requiring or acquiring any latches.
1527 Note that this implies calling this function twice in a row could produce
1528 different results.
1529 @return true iff io_fix not equal to BUF_IO_NONE was noticed */
1530 bool was_io_fixed() const { return get_io_fix_snapshot() != BUF_IO_NONE; }
1531
1532 /** Checks if io_fix is BUF_IO_NONE without requiring or acquiring any
1533 latches.
1534 Note that this implies calling this function twice in a row could produce
1535 different results.
1536 Please, prefer this function over !was_io_fixed() to avoid the misleading
1537 interpretation as "not(Exists time such that io_fix(time))", while in fact we
1538 want and get "Exists time such that !io_fix(time)".
1539 @return true iff io_fix equal to BUF_IO_NONE was noticed */
1540 bool was_io_fix_none() const { return get_io_fix_snapshot() == BUF_IO_NONE; }
1541
1542 /** Block state. @see buf_page_in_file */
1543 buf_page_state state;
1544
1545 /** If this block is currently being flushed to disk, this tells
1546 the flush_type. @see buf_flush_t */
1547 buf_flush_t flush_type;
1548
1549 /** Index number of the buffer pool that this block belongs to */
1550 uint8_t buf_pool_index;
1551
1552 static_assert(MAX_BUFFER_POOLS <= 64,
1553 "MAX_BUFFER_POOLS > 64; redefine buf_pool_index");
1554
1555 /** @} */
1556 #ifndef UNIV_HOTBACKUP
1557 /** Node used in chaining to buf_pool->page_hash or buf_pool->zip_hash */
1558 buf_page_t *hash;
1559 #endif /* !UNIV_HOTBACKUP */
1560
1561 /** @name Page flushing fields
1562 All these are protected by buf_pool->mutex. */
1563 /** @{ */
1564
1565 /** Based on state, this is a list node, protected by the corresponding list
1566 mutex, in one of the following lists in buf_pool:
1567
1568 - BUF_BLOCK_NOT_USED: free, withdraw
1569 - BUF_BLOCK_FILE_PAGE: flush_list
1570 - BUF_BLOCK_ZIP_DIRTY: flush_list
1571 - BUF_BLOCK_ZIP_PAGE: zip_clean
1572
1573 The node pointers are protected by the corresponding list mutex.
1574
1575 The contents of the list node is undefined if !in_flush_list &&
1576 state == BUF_BLOCK_FILE_PAGE, or if state is one of
1577 BUF_BLOCK_MEMORY,
1578 BUF_BLOCK_REMOVE_HASH or
1579 BUF_BLOCK_READY_IN_USE. */
1580
1581 UT_LIST_NODE_T(buf_page_t) list;
1582
1583 private:
1584 /** The flush LSN, LSN when this page was written to the redo log. For
1585 non redo logged pages this is set using: buf_flush_borrow_lsn() */
1586 lsn_t newest_modification;
1587
1588 /** log sequence number of the youngest modification to this block, zero
1589 if not modified. Protected by block mutex */
1590 lsn_t oldest_modification;
1591
1592 public:
1593 /** log sequence number of the START of the log entry written of the oldest
1594 modification to this block which has not yet been flushed on disk; zero if
1595 all modifications are on disk. Writes to this field must be covered by both
1596 block->mutex and buf_pool->flush_list_mutex. Hence reads can happen while
1597 holding any one of the two mutexes */
1598 /** @} */
1599
1600 /** @name LRU replacement algorithm fields
1601 These fields are protected by both buf_pool->LRU_list_mutex and the
1602 block mutex. */
1603 /** @{ */
1604
1605 /** node of the LRU list */
1606 UT_LIST_NODE_T(buf_page_t) LRU;
1607
1608 /** compressed page; zip.data (but not the data it points to) is
1609 protected by buf_pool->zip_mutex; state == BUF_BLOCK_ZIP_PAGE and
1610 zip.data == NULL means an active buf_pool->watch */
1611 page_zip_des_t zip;
1612
1613 #ifndef UNIV_HOTBACKUP
1614 /** Flush observer instance. */
1615 Flush_observer *m_flush_observer{};
1616
1617 /** Tablespace instance that this page belongs to. */
1618 fil_space_t *m_space{};
1619
1620 /** The value of buf_pool->freed_page_clock when this block was the last
1621 time put to the head of the LRU list; a thread is allowed to read this
1622 for heuristic purposes without holding any mutex or latch */
1623 uint32_t freed_page_clock;
1624
1625 /** @} */
1626 /** Version of fil_space_t when the page was updated. It can also be viewed as
1627 the truncation number. */
1628 uint32_t m_version{};
1629
1630 /** Time of first access, or 0 if the block was never accessed in the
1631 buffer pool. Protected by block mutex */
1632 std::chrono::steady_clock::time_point access_time;
1633
1634 private:
1635 /** Double write instance ordinal value during writes. This is used
1636 by IO completion (writes) to select the double write instance.*/
1637 uint16_t m_dblwr_id{};
1638
1639 public:
1640 /** true if the block is in the old blocks in buf_pool->LRU_old */
1641 bool old;
1642
1643 #ifdef UNIV_DEBUG
1644 /** This is set to true when fsp frees a page in buffer pool;
1645 protected by buf_pool->zip_mutex or buf_block_t::mutex. */
1646 bool file_page_was_freed;
1647
1648 /** true if in buf_pool->flush_list; when buf_pool->flush_list_mutex
1649 is free, the following should hold:
1650 in_flush_list == (state == BUF_BLOCK_FILE_PAGE ||
1651 state == BUF_BLOCK_ZIP_DIRTY)
1652 Writes to this field must be covered by both buf_pool->flush_list_mutex
1653 and block->mutex. Hence reads can happen while holding any one of the
1654 two mutexes */
1655 bool in_flush_list;
1656
1657 /** true if in buf_pool->free; when buf_pool->free_list_mutex is free, the
1658 following should hold: in_free_list == (state == BUF_BLOCK_NOT_USED) */
1659 bool in_free_list;
1660
1661 /** true if the page is in the LRU list; used in debugging */
1662 bool in_LRU_list;
1663
1664 /** true if in buf_pool->page_hash */
1665 bool in_page_hash;
1666
1667 /** true if in buf_pool->zip_hash */
1668 bool in_zip_hash;
1669 #endif /* UNIV_DEBUG */
1670
1671 /** Print page metadata in JSON format {"key":"value"}. Asserts that caller
1672 holds page mutex and page if file page
1673 @param[in,out] outs the output stream
1674 @param[in] page the page whose metadata needs to be printed
1675 @return same output stream */
1676 friend std::ostream &operator<<(std::ostream &outs, const buf_page_t &page);
1677 #endif /* !UNIV_HOTBACKUP */
1678 };
1679
1680 /** Structure used by AHI to contain information on record prefixes to be
1681 considered in hash index subsystem. It is meant for using as a single 64bit
1682 atomic value, thus it needs to be aligned properly. */
1683 struct alignas(alignof(uint64_t)) btr_search_prefix_info_t {
1684 /** recommended prefix: number of bytes in an incomplete field
1685 @see BTR_PAGE_MAX_REC_SIZE */
1686 uint32_t n_bytes;
1687 /** recommended prefix length for hash search: number of full fields */
1688 uint16_t n_fields;
1689 /** true or false, depending on whether the leftmost record of several records
1690 with the same prefix should be indexed in the hash index */
1691 bool left_side;
1692
1693 bool equals_without_left_side(const btr_search_prefix_info_t &other) const {
1694 return n_bytes == other.n_bytes && n_fields == other.n_fields;
1695 }
1696
1697 bool operator==(const btr_search_prefix_info_t &other) const {
1698 return n_bytes == other.n_bytes && n_fields == other.n_fields &&
1699 left_side == other.left_side;
1700 }
1701
1702 bool operator!=(const btr_search_prefix_info_t &other) const {
1703 return !(*this == other);
1704 }
1705 };
1706
1707 /** The buffer control block structure */
1708 struct buf_block_t {
1709 /** @name General fields */
1710 /** @{ */
1711
1712 /** page information; this must be the first field, so
1713 that buf_pool->page_hash can point to buf_page_t or buf_block_t */
1714 buf_page_t page;
1715
1716 #ifndef UNIV_HOTBACKUP
1717 /** read-write lock of the buffer frame */
1718 BPageLock lock;
1719
1720 #endif /* UNIV_HOTBACKUP */
1721
1722 /** pointer to buffer frame which is of size UNIV_PAGE_SIZE, and aligned
1723 to an address divisible by UNIV_PAGE_SIZE */
1724 byte *frame;
1725
1726 /** node of the decompressed LRU list; a block is in the unzip_LRU list if
1727 page.state == BUF_BLOCK_FILE_PAGE and page.zip.data != NULL. Protected by
1728 both LRU_list_mutex and the block mutex. */
1729 UT_LIST_NODE_T(buf_block_t) unzip_LRU;
1730 #ifdef UNIV_DEBUG
1731
1732 /** true if the page is in the decompressed LRU list; used in debugging */
1733 bool in_unzip_LRU_list;
1734
1735 bool in_withdraw_list;
1736 #endif /* UNIV_DEBUG */
1737
1738 /** @} */
1739
1740 /** Structure that holds most AHI-related fields. */
1741 struct ahi_t {
1742 public:
1743 /** Recommended prefix info for hash search. It is atomically copied
1744 from the index's current recommendation for the prefix info and should
1745 eventually get to the block's actual prefix info used. It is used to decide
1746 when the n_hash_helps should be reset. It is modified only while having S-
1747 or X- latch on block's lock. */
1748 std::atomic<btr_search_prefix_info_t> recommended_prefix_info;
1749 /** Prefix info that was used for building hash index. It cannot be modified
1750 while there are any record entries added in the AHI. It's invariant that all
1751 records added to AHI from this block were folded using this prefix info. It
1752 may only be modified when we are holding the appropriate X-latch in
1753 btr_search_sys->parts[]->latch. Also, it happens that it is modified
1754 to not-empty value only when the block is held in private or the block's
1755 lock is S- or X-latched. This implies that the field's non-empty value may
1756 be read and use reliably when the appropriate
1757 btr_search_sys->parts[]->latch S-latch or X-latch is being held, or
1758 the block's lock is X-latched. */
1759 std::atomic<btr_search_prefix_info_t> prefix_info;
1760 static_assert(decltype(prefix_info)::is_always_lock_free);
1761
1762 /** Index for which the adaptive hash index has been created, or nullptr if
1763 the page does not exist in the index. Note that it does not guarantee that
1764 the AHI index is complete, though: there may have been hash collisions etc.
1765 It may be modified:
1766 - to nullptr if btr_search_enabled is false and block's mutex is held and
1767 block's state is BUF_BLOCK_FILE_PAGE and btr_search_enabled_mutex is
1768 owned, protecting the btr_search_enabled from being changed,
1769 - to nullptr if btr_search_enabled is false and block is held in private in
1770 BUF_BLOCK_REMOVE_HASH state in buf_LRU_free_page().
1771 - to any value under appropriate X-latch in btr_search_sys->parts[]->latch
1772 if btr_search_enabled is true (and setting btr_search_enabled to false in
1773 turn is protected by having all btr_search_sys->parts[]->latch X-latched).
1774 */
1775 std::atomic<dict_index_t *> index;
1776
1777 #if defined UNIV_AHI_DEBUG || defined UNIV_DEBUG
1778 /** Used in debugging. The number of pointers in the adaptive hash index
1779 pointing to this frame; Modified under appropriate X-latch in
1780 btr_search_sys->parts[]->latch. */
1781 std::atomic<uint16_t> n_pointers;
1782
1783 inline void validate() const {
1784 /* These fields are read without holding any AHI latches. Adding or
1785 removing a block from AHI requires having only an appropriate AHI part
1786 X-latched. If we have at least S-latched the correct AHI part (for which
1787 we would need at least S-latch on block for the block's index to not be
1788 changed in meantime) this check is certain. If we don't have necessary AHI
1789 latches, then:
1790 - it can't happen that the check fails while the block is removed from
1791 AHI. Both btr_search_drop_page_hash_index() and
1792 buf_pool_clear_hash_index() will first make the n_pointers be 0 and then
1793 set index to nullptr. As the index is an atomic variable, so if we
1794 synchronize with a reset to nullptr which is sequenced after the reset of
1795 n_pointers, we should see the n_pointers set to 0 here.
1796 - it can happen that the check fails while the block is added to the AHI
1797 right after we read the index is nullptr. In such case, if the n_pointers
1798 is not 0, we double check the index member. It can still be nullptr, if
1799 the block is removed after reading the n_pointers, but that should be near
1800 impossible. */
1801 ut_a(this->index.load() != nullptr || this->n_pointers.load() == 0 ||
1802 this->index.load() != nullptr);
1803 }
1804
1805 inline void assert_empty() const { ut_a(this->n_pointers.load() == 0); }
1806
1807 inline void assert_empty_on_init() const {
1808 UNIV_MEM_VALID(&this->n_pointers, sizeof(this->n_pointers));
1809 assert_empty();
1810 }
1811 #else
1812 inline void validate() const {}
1813
1814 inline void assert_empty() const {}
1815
1816 inline void assert_empty_on_init() const {}
1817 #endif /* UNIV_AHI_DEBUG || UNIV_DEBUG */
1818 } ahi;
1819
1820 /** Counter which controls how many times the current prefix recommendation
1821 would help in searches. If it is helpful enough, it will be used as the
1822 actual prefix to build hash for this block. It is modified similarly as
1823 recommended_prefix_info, that is only while having S- or X- latch on block's
1824 lock. Because it is modified concurrently, it may not have fully reliable
1825 count, but it is enough for this use case.
1826 Mind the n_hash_helps is AHI-related, and should be in the ahi_t struct above,
1827 but having it outside causes the made_dirty_with_no_latch to occupy the common
1828 8byte aligned 8byte long space, so basically it saves us 8bytes of the object
1829 that is used in high volumes. */
1830 std::atomic<uint32_t> n_hash_helps;
1831 /** true if block has been made dirty without acquiring X/SX latch as the
1832 block belongs to temporary tablespace and block is always accessed by a
1833 single thread. */
1834 bool made_dirty_with_no_latch;
1835
1836 #ifndef UNIV_HOTBACKUP
1837 #ifdef UNIV_DEBUG
1838 /** @name Debug fields */
1839 /** @{ */
1840 /** In the debug version, each thread which bufferfixes the block acquires
1841 an s-latch here; so we can use the debug utilities in sync0rw */
1842 rw_lock_t debug_latch;
1843 /** @} */
1844 #endif /* UNIV_DEBUG */
1845 #endif /* !UNIV_HOTBACKUP */
1846
1847 /** @name Optimistic search field */
1848 /** @{ */
1849
1850 /** This clock is incremented every time a pointer to a record on the page
1851 may become obsolete; this is used in the optimistic cursor positioning: if
1852 the modify clock has not changed, we know that the pointer is still valid;
1853 this field may be changed if the thread (1) owns the LRU list mutex and the
1854 page is not bufferfixed, or (2) the thread has an x-latch on the block,
1855 or (3) the block must belong to an intrinsic table */
1856 uint64_t modify_clock;
1857
1858 /** @} */
1859
1860 /** mutex protecting this block: state (also protected by the buffer
1861 pool mutex), io_fix, buf_fix_count, and accessed; we introduce this
1862 new mutex in InnoDB-5.1 to relieve contention on the buffer pool mutex */
1863 BPageMutex mutex;
1864
1865 /** Get the modified clock (version) value.
1866 @param[in] single_threaded Thread can only be written to or read by a
1867 single thread
1868 @return the modified clock vlue. */
1869 uint64_t get_modify_clock(IF_DEBUG(bool single_threaded)) const noexcept {
1870 #if defined(UNIV_DEBUG) && !defined(UNIV_LIBRARY) && !defined(UNIV_HOTBACKUP)
1871 /* No block latch is acquired when blocks access is guaranteed to be
1872 in single threaded mode. */
1873 constexpr auto mode = RW_LOCK_FLAG_X | RW_LOCK_FLAG_SX | RW_LOCK_FLAG_S;
1874 ut_ad(single_threaded || rw_lock_own_flagged(&lock, mode));
1875 #endif /* UNIV_DEBUG && !UNIV_LIBRARY */
1876
1877 return modify_clock;
1878 }
1879
1880 /** Get the page number and space id of the current buffer block.
1881 @return page number of the current buffer block. */
1882 const page_id_t &get_page_id() const { return page.id; }
1883
1884 /** Get the page number of the current buffer block.
1885 @return page number of the current buffer block. */
1886 page_no_t get_page_no() const { return (page.id.page_no()); }
1887
1888 /** Get the next page number of the current buffer block.
1889 @return next page number of the current buffer block. */
1890 page_no_t get_next_page_no() const {
1891 return (mach_read_from_4(frame + FIL_PAGE_NEXT));
1892 }
1893
1894 /** Get the prev page number of the current buffer block.
1895 @return prev page number of the current buffer block. */
1896 page_no_t get_prev_page_no() const {
1897 return (mach_read_from_4(frame + FIL_PAGE_PREV));
1898 }
1899
1900 /** Get the page type of the current buffer block.
1901 @return page type of the current buffer block. */
1902 page_type_t get_page_type() const {
1903 return (mach_read_from_2(frame + FIL_PAGE_TYPE));
1904 }
1905
1906 #ifndef UNIV_HOTBACKUP
1907 /** Print control block information in JSON format: {"key":"value"}
1908 @param[in,out] outs the output stream
1909 @param[in] block the control block whose information needs to be printed
1910 @return same output stream */
1911 friend std::ostream &operator<<(std::ostream &outs, const buf_block_t &block);
1912 #endif /* UNIV_HOTBACKUP */
1913
1914 uint16_t get_page_level() const;
1915 bool is_leaf() const;
1916 bool is_root() const;
1917 bool is_index_page() const;
1918
1919 /** Check if this index page is empty. An index page is considered empty
1920 if the next record of an infimum record is supremum record. Presence of
1921 del-marked records will make the page non-empty.
1922 @return true if this index page is empty. */
1923 bool is_empty() const;
1924
1925 /** Get the page type of the current buffer block as string.
1926 @return page type of the current buffer block as string. */
1927 [[nodiscard]] const char *get_page_type_str() const noexcept;
1928
1929 /** Gets the compressed page descriptor corresponding to an uncompressed page
1930 if applicable.
1931 @return page descriptor or nullptr. */
1932 page_zip_des_t *get_page_zip() noexcept {
1933 return page.zip.data != nullptr ? &page.zip : nullptr;
1934 }
1935
1936 /** Const version.
1937 @return page descriptor or nullptr. */
1938 page_zip_des_t const *get_page_zip() const noexcept {
1939 return page.zip.data != nullptr ? &page.zip : nullptr;
1940 }
1941 };
1942
1943 inline bool buf_block_t::is_root() const {
1944 return ((get_next_page_no() == FIL_NULL) && (get_prev_page_no() == FIL_NULL));
1945 }
1946
1947 inline bool buf_block_t::is_leaf() const { return get_page_level() == 0; }
1948
1949 inline bool buf_block_t::is_index_page() const {
1950 return get_page_type() == FIL_PAGE_INDEX;
1951 }
1952
1953 /** Check if a buf_block_t object is in a valid state
1954 @param block buffer block
1955 @return true if valid */
1956 inline bool buf_block_state_valid(buf_block_t *block) {
1957 return buf_block_get_state(block) >= BUF_BLOCK_NOT_USED &&
1958 buf_block_get_state(block) <= BUF_BLOCK_REMOVE_HASH;
1959 }
1960
1961 /** Compute the hash value for blocks in buf_pool->zip_hash. */
1962 /** @{ */
1963 static inline uint64_t buf_pool_hash_zip_frame(void *ptr) {
1964 return ut::hash_uint64(reinterpret_cast<uintptr_t>(ptr) >>
1965 UNIV_PAGE_SIZE_SHIFT);
1966 }
1967 static inline uint64_t buf_pool_hash_zip(buf_block_t *b) {
1968 return buf_pool_hash_zip_frame(b->frame);
1969 }
1970 /** @} */
1971
1972 /** A "Hazard Pointer" class used to iterate over page lists
1973 inside the buffer pool. A hazard pointer is a buf_page_t pointer
1974 which we intend to iterate over next and we want it remain valid
1975 even after we release the buffer pool mutex. */
1976 class HazardPointer {
1977 public:
1978 /** Constructor
1979 @param buf_pool buffer pool instance
1980 @param mutex mutex that is protecting the hp. */
1981 HazardPointer(const buf_pool_t *buf_pool, const ib_mutex_t *mutex)
1982 : m_buf_pool(buf_pool) IF_DEBUG(, m_mutex(mutex)), m_hp() {}
1983
1984 /** Destructor */
1985 virtual ~HazardPointer() = default;
1986
1987 /** Get current value */
1988 buf_page_t *get() const {
1989 ut_ad(mutex_own(m_mutex));
1990 return (m_hp);
1991 }
1992
1993 /** Set current value
1994 @param bpage buffer block to be set as hp */
1995 void set(buf_page_t *bpage);
1996
1997 /** Checks if a bpage is the hp
1998 @param bpage buffer block to be compared
1999 @return true if it is hp */
2000 bool is_hp(const buf_page_t *bpage);
2001
2002 /** Adjust the value of hp. This happens when some
2003 other thread working on the same list attempts to
2004 remove the hp from the list. Must be implemented
2005 by the derived classes.
2006 @param bpage buffer block to be compared */
2007 virtual void adjust(const buf_page_t *bpage) = 0;
2008
2009 /** Adjust the value of hp for moving. This happens
2010 when some other thread working on the same list
2011 attempts to relocate the hp of the page.
2012 @param bpage buffer block to be compared
2013 @param dpage buffer block to be moved to */
2014 void move(const buf_page_t *bpage, buf_page_t *dpage);
2015
2016 protected:
2017 /** Disable copying */
2018 HazardPointer(const HazardPointer &);
2019 HazardPointer &operator=(const HazardPointer &);
2020
2021 /** Buffer pool instance */
2022 const buf_pool_t *m_buf_pool;
2023
2024 #ifdef UNIV_DEBUG
2025 /** mutex that protects access to the m_hp. */
2026 const ib_mutex_t *m_mutex;
2027 #endif /* UNIV_DEBUG */
2028
2029 /** hazard pointer. */
2030 buf_page_t *m_hp;
2031 };
2032
2033 /** Class implementing buf_pool->flush_list hazard pointer */
2034 class FlushHp : public HazardPointer {
2035 public:
2036 /** Constructor
2037 @param buf_pool buffer pool instance
2038 @param mutex mutex that is protecting the hp. */
2039 FlushHp(const buf_pool_t *buf_pool, const ib_mutex_t *mutex)
2040 : HazardPointer(buf_pool, mutex) {}
2041
2042 /** Destructor */
2043 ~FlushHp() override = default;
2044
2045 /** Adjust the value of hp. This happens when some
2046 other thread working on the same list attempts to
2047 remove the hp from the list.
2048 @param bpage buffer block to be compared */
2049 void adjust(const buf_page_t *bpage) override;
2050 };
2051
2052 /** Class implementing buf_pool->LRU hazard pointer */
2053 class LRUHp : public HazardPointer {
2054 public:
2055 /** Constructor
2056 @param buf_pool buffer pool instance
2057 @param mutex mutex that is protecting the hp. */
2058 LRUHp(const buf_pool_t *buf_pool, const ib_mutex_t *mutex)
2059 : HazardPointer(buf_pool, mutex) {}
2060
2061 /** Destructor */
2062 ~LRUHp() override = default;
2063
2064 /** Adjust the value of hp. This happens when some
2065 other thread working on the same list attempts to
2066 remove the hp from the list.
2067 @param bpage buffer block to be compared */
2068 void adjust(const buf_page_t *bpage) override;
2069 };
2070
2071 /** Special purpose iterators to be used when scanning the LRU list.
2072 The idea is that when one thread finishes the scan it leaves the
2073 itr in that position and the other thread can start scan from
2074 there */
2075 class LRUItr : public LRUHp {
2076 public:
2077 /** Constructor
2078 @param buf_pool buffer pool instance
2079 @param mutex mutex that is protecting the hp. */
2080 LRUItr(const buf_pool_t *buf_pool, const ib_mutex_t *mutex)
2081 : LRUHp(buf_pool, mutex) {}
2082
2083 /** Destructor */
2084 ~LRUItr() override = default;
2085
2086 /** Selects from where to start a scan. If we have scanned
2087 too deep into the LRU list it resets the value to the tail
2088 of the LRU list.
2089 @return buf_page_t from where to start scan. */
2090 buf_page_t *start();
2091 };
2092
2093 /** Struct that is embedded in the free zip blocks */
2094 struct buf_buddy_free_t {
2095 union {
2096 ulint size; /*!< size of the block */
2097 byte bytes[FIL_PAGE_DATA];
2098 /*!< stamp[FIL_PAGE_ARCH_LOG_NO_OR_SPACE_ID]
2099 == BUF_BUDDY_FREE_STAMP denotes a free
2100 block. If the space_id field of buddy
2101 block != BUF_BUDDY_FREE_STAMP, the block
2102 is not in any zip_free list. If the
2103 space_id is BUF_BUDDY_FREE_STAMP then
2104 stamp[0] will contain the
2105 buddy block size. */
2106 } stamp;
2107
2108 buf_page_t bpage; /*!< Embedded bpage descriptor */
2109 UT_LIST_NODE_T(buf_buddy_free_t) list;
2110 /*!< Node of zip_free list */
2111 };
2112
2113 /** @brief The buffer pool statistics structure. */
2114 struct buf_pool_stat_t {
2115 using Shards = Counter::Shards<64>;
2116
2117 /** Number of page gets performed; also successful searches through the
2118 adaptive hash index are counted as page gets; this field is NOT protected
2119 by the buffer pool mutex */
2120 Shards m_n_page_gets;
2121
2122 /** Number of read operations. */
2123 std::atomic<uint64_t> n_pages_read;
2124
2125 /** Number of write operations. */
2126 std::atomic<uint64_t> n_pages_written;
2127
2128 /** number of pages created in the pool with no read. */
2129 std::atomic<uint64_t> n_pages_created;
2130
2131 /** Number of pages read in as part of random read ahead. */
2132 std::atomic<uint64_t> n_ra_pages_read_rnd;
2133
2134 /** Number of pages read in as part of read ahead. */
2135 std::atomic<uint64_t> n_ra_pages_read;
2136
2137 /** Number of read ahead pages that are evicted without being accessed.
2138 Protected by LRU_list_mutex. */
2139 uint64_t n_ra_pages_evicted;
2140
2141 /** Number of pages made young, in calls to buf_LRU_make_block_young().
2142 Protected by LRU_list_mutex. */
2143 uint64_t n_pages_made_young;
2144
2145 /** Number of pages not made young because the first access was not long
2146 enough ago, in buf_page_peek_if_too_old(). Not protected. */
2147 uint64_t n_pages_not_made_young;
2148
2149 /** LRU size in bytes. Protected by LRU_list_mutex. */
2150 uint64_t LRU_bytes;
2151
2152 /** Flush_list size in bytes. Protected by flush_list_mutex */
2153 uint64_t flush_list_bytes;
2154
2155 static void copy(buf_pool_stat_t &dst, const buf_pool_stat_t &src) noexcept {
2156 Counter::copy(dst.m_n_page_gets, src.m_n_page_gets);
2157
2158 dst.n_pages_read.store(src.n_pages_read.load());
2159
2160 dst.n_pages_written.store(src.n_pages_written.load());
2161
2162 dst.n_pages_created.store(src.n_pages_created.load());
2163
2164 dst.n_ra_pages_read_rnd.store(src.n_ra_pages_read_rnd.load());
2165
2166 dst.n_ra_pages_read.store(src.n_ra_pages_read.load());
2167
2168 dst.n_ra_pages_evicted = src.n_ra_pages_evicted;
2169
2170 dst.n_pages_made_young = src.n_pages_made_young;
2171
2172 dst.n_pages_not_made_young = src.n_pages_not_made_young;
2173
2174 dst.LRU_bytes = src.LRU_bytes;
2175
2176 dst.flush_list_bytes = src.flush_list_bytes;
2177 }
2178
2179 void reset() {
2180 Counter::clear(m_n_page_gets);
2181
2182 n_pages_read = 0;
2183 n_pages_written = 0;
2184 n_pages_created = 0;
2185 n_ra_pages_read_rnd = 0;
2186 n_ra_pages_read = 0;
2187 n_ra_pages_evicted = 0;
2188 n_pages_made_young = 0;
2189 n_pages_not_made_young = 0;
2190 LRU_bytes = 0;
2191 flush_list_bytes = 0;
2192 }
2193 };
2194
2195 /** Statistics of buddy blocks of a given size. */
2196 struct buf_buddy_stat_t {
2197 /** Number of blocks allocated from the buddy system. */
2198 std::atomic<ulint> used;
2199 /** Number of blocks relocated by the buddy system.
2200 Protected by buf_pool zip_free_mutex. */
2201 uint64_t relocated;
2202 /** Total duration of block relocations.
2203 Protected by buf_pool zip_free_mutex. */
2204 std::chrono::steady_clock::duration relocated_duration;
2205
2206 struct snapshot_t {
2207 ulint used;
2208 uint64_t relocated;
2209 std::chrono::steady_clock::duration relocated_duration;
2210 };
2211
2212 snapshot_t take_snapshot() {
2213 return {used.load(), relocated, relocated_duration};
2214 }
2215 };
2216
2217 /** @brief The buffer pool structure.
2218
2219 NOTE! The definition appears here only for other modules of this
2220 directory (buf) to see it. Do not use from outside! */
2221
2222 struct buf_pool_t {
2223 /** @name General fields */
2224 /** @{ */
2225 /** protects (de)allocation of chunks:
2226 - changes to chunks, n_chunks are performed while holding this latch,
2227 - reading buf_pool_should_madvise requires holding this latch for any
2228 buf_pool_t
2229 - writing to buf_pool_should_madvise requires holding these latches
2230 for all buf_pool_t-s */
2231 BufListMutex chunks_mutex;
2232
2233 /** LRU list mutex */
2234 BufListMutex LRU_list_mutex;
2235
2236 /** free and withdraw list mutex */
2237 BufListMutex free_list_mutex;
2238
2239 /** buddy allocator mutex */
2240 BufListMutex zip_free_mutex;
2241
2242 /** zip_hash mutex */
2243 BufListMutex zip_hash_mutex;
2244
2245 /** Flush state protection mutex */
2246 ib_mutex_t flush_state_mutex;
2247
2248 /** Zip mutex of this buffer pool instance, protects compressed only pages (of
2249 type buf_page_t, not buf_block_t */
2250 BufPoolZipMutex zip_mutex;
2251
2252 /** Array index of this buffer pool instance */
2253 ulint instance_no;
2254
2255 /** Current pool size in bytes */
2256 ulint curr_pool_size;
2257
2258 /** Reserve this much of the buffer pool for "old" blocks */
2259 ulint LRU_old_ratio;
2260 #ifdef UNIV_DEBUG
2261 /** Number of frames allocated from the buffer pool to the buddy system.
2262 Protected by zip_hash_mutex. */
2263 ulint buddy_n_frames;
2264 #endif
2265
2266 /** Number of buffer pool chunks */
2267 volatile ulint n_chunks;
2268
2269 /** New number of buffer pool chunks */
2270 volatile ulint n_chunks_new;
2271
2272 /** buffer pool chunks */
2273 buf_chunk_t *chunks;
2274
2275 /** old buffer pool chunks to be freed after resizing buffer pool */
2276 buf_chunk_t *chunks_old;
2277
2278 /** Current pool size in pages */
2279 ulint curr_size;
2280
2281 /** Previous pool size in pages */
2282 ulint old_size;
2283
2284 /** Size in pages of the area which the read-ahead algorithms read
2285 if invoked */
2286 page_no_t read_ahead_area;
2287
2288 /** Hash table of buf_page_t or buf_block_t file pages, buf_page_in_file() ==
2289 true, indexed by (space_id, offset). page_hash is protected by an array of
2290 mutexes. */
2291 hash_table_t *page_hash;
2292
2293 /** Hash table of buf_block_t blocks whose frames are allocated to the zip
2294 buddy system, indexed by block->frame */
2295 hash_table_t *zip_hash;
2296
2297 /** Number of pending read operations. Accessed atomically */
2298 std::atomic<ulint> n_pend_reads;
2299
2300 /** number of pending decompressions. Accessed atomically. */
2301 std::atomic<ulint> n_pend_unzip;
2302
2303 /** when buf_print_io was last time called. Accesses not protected. */
2304 std::chrono::steady_clock::time_point last_printout_time;
2305
2306 /** Statistics of buddy system, indexed by block size. Protected by zip_free
2307 mutex, except for the used field, which is also accessed atomically */
2308 buf_buddy_stat_t buddy_stat[BUF_BUDDY_SIZES_MAX + 1];
2309
2310 /** Current statistics */
2311 buf_pool_stat_t stat;
2312
2313 /** Old statistics */
2314 buf_pool_stat_t old_stat;
2315
2316 /** @} */
2317
2318 /** @name Page flushing algorithm fields */
2319
2320 /** @{ */
2321
2322 /** Mutex protecting the flush list access. This mutex protects flush_list,
2323 flush_rbt and bpage::list pointers when the bpage is on flush_list. It also
2324 protects writes to bpage::oldest_modification and flush_list_hp */
2325 BufListMutex flush_list_mutex;
2326
2327 /** "Hazard pointer" used during scan of flush_list while doing flush list
2328 batch. Protected by flush_list_mutex */
2329 FlushHp flush_hp;
2330
2331 /** Entry pointer to scan the oldest page except for system temporary */
2332 FlushHp oldest_hp;
2333
2334 /** Base node of the modified block list */
2335 UT_LIST_BASE_NODE_T(buf_page_t, list) flush_list;
2336
2337 /** This is true when a flush of the given type is being initialized.
2338 Protected by flush_state_mutex. */
2339 bool init_flush[BUF_FLUSH_N_TYPES];
2340
2341 /** This is the number of pending writes in the given flush type. Protected
2342 by flush_state_mutex. */
2343 ulint n_flush[BUF_FLUSH_N_TYPES];
2344
2345 /** This is in the set state when there is no flush batch of the given type
2346 running. Protected by flush_state_mutex. */
2347 os_event_t no_flush[BUF_FLUSH_N_TYPES];
2348
2349 /** A red-black tree is used exclusively during recovery to speed up
2350 insertions in the flush_list. This tree contains blocks in order of
2351 oldest_modification LSN and is kept in sync with the flush_list. Each
2352 member of the tree MUST also be on the flush_list. This tree is relevant
2353 only in recovery and is set to NULL once the recovery is over. Protected
2354 by flush_list_mutex */
2355 ib_rbt_t *flush_rbt;
2356
2357 /** A sequence number used to count the number of buffer blocks removed from
2358 the end of the LRU list; NOTE that this counter may wrap around at 4
2359 billion! A thread is allowed to read this for heuristic purposes without
2360 holding any mutex or latch. For non-heuristic purposes protected by
2361 LRU_list_mutex */
2362 ulint freed_page_clock;
2363
2364 /** Set to false when an LRU scan for free block fails. This flag is used to
2365 avoid repeated scans of LRU list when we know that there is no free block
2366 available in the scan depth for eviction. Set to true whenever we flush a
2367 batch from the buffer pool. Accessed protected by memory barriers. */
2368 bool try_LRU_scan;
2369
2370 /** Page Tracking start LSN. */
2371 lsn_t track_page_lsn;
2372
2373 /** Maximum LSN for which write io has already started. */
2374 lsn_t max_lsn_io;
2375
2376 /** @} */
2377
2378 /** @name LRU replacement algorithm fields */
2379 /** @{ */
2380
2381 /** Base node of the free block list */
2382 UT_LIST_BASE_NODE_T(buf_page_t, list) free;
2383
2384 /** base node of the withdraw block list. It is only used during shrinking
2385 buffer pool size, not to reuse the blocks will be removed. Protected by
2386 free_list_mutex */
2387 UT_LIST_BASE_NODE_T(buf_page_t, list) withdraw;
2388
2389 /** Target length of withdraw block list, when withdrawing */
2390 ulint withdraw_target;
2391
2392 /** "hazard pointer" used during scan of LRU while doing
2393 LRU list batch. Protected by buf_pool::LRU_list_mutex */
2394 LRUHp lru_hp;
2395
2396 /** Iterator used to scan the LRU list when searching for
2397 replaceable victim. Protected by buf_pool::LRU_list_mutex. */
2398 LRUItr lru_scan_itr;
2399
2400 /** Iterator used to scan the LRU list when searching for
2401 single page flushing victim. Protected by buf_pool::LRU_list_mutex. */
2402 LRUItr single_scan_itr;
2403
2404 /** Base node of the LRU list */
2405 UT_LIST_BASE_NODE_T(buf_page_t, LRU) LRU;
2406
2407 /** Pointer to the about LRU_old_ratio/BUF_LRU_OLD_RATIO_DIV oldest blocks in
2408 the LRU list; NULL if LRU length less than BUF_LRU_OLD_MIN_LEN; NOTE: when
2409 LRU_old != NULL, its length should always equal LRU_old_len */
2410 buf_page_t *LRU_old;
2411
2412 /** Length of the LRU list from the block to which LRU_old points onward,
2413 including that block; see buf0lru.cc for the restrictions on this value; 0
2414 if LRU_old == NULL; NOTE: LRU_old_len must be adjusted whenever LRU_old
2415 shrinks or grows! */
2416 ulint LRU_old_len;
2417
2418 /** Base node of the unzip_LRU list. The list is protected by the
2419 LRU_list_mutex. */
2420 UT_LIST_BASE_NODE_T(buf_block_t, unzip_LRU) unzip_LRU;
2421
2422 /** @} */
2423 /** @name Buddy allocator fields
2424 The buddy allocator is used for allocating compressed page
2425 frames and buf_page_t descriptors of blocks that exist
2426 in the buffer pool only in compressed form. */
2427 /** @{ */
2428 #if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
2429 /** Unmodified compressed pages */
2430 UT_LIST_BASE_NODE_T(buf_page_t, list) zip_clean;
2431 #endif /* UNIV_DEBUG || UNIV_BUF_DEBUG */
2432
2433 /** Buddy free lists */
2434 UT_LIST_BASE_NODE_T(buf_buddy_free_t, list) zip_free[BUF_BUDDY_SIZES_MAX];
2435
2436 /** Sentinel records for buffer pool watches. Scanning the array is protected
2437 by taking all page_hash latches in X. Updating or reading an individual
2438 watch page is protected by a corresponding individual page_hash latch. */
2439 buf_page_t *watch;
2440
2441 /** A wrapper for buf_pool_t::allocator.alocate_large which also advices the
2442 OS that this chunk should not be dumped to a core file if that was requested.
2443 Emits a warning to the log and disables @@global.core_file if advising was
2444 requested but could not be performed, but still return true as the allocation
2445 itself succeeded.
2446 @param[in] mem_size number of bytes to allocate
2447 @param[in,out] chunk mem and mem_pfx fields of this chunk will be updated
2448 to contain information about allocated memory region
2449 @return true iff allocated successfully */
2450 bool allocate_chunk(ulonglong mem_size, buf_chunk_t *chunk);
2451
2452 /** A wrapper for buf_pool_t::allocator.deallocate_large which also advices
2453 the OS that this chunk can be dumped to a core file.
2454 Emits a warning to the log and disables @@global.core_file if advising was
2455 requested but could not be performed.
2456 @param[in] chunk mem and mem_pfx fields of this chunk will be used to
2457 locate the memory region to free */
2458 void deallocate_chunk(buf_chunk_t *chunk);
2459
2460 /** Advices the OS that all chunks in this buffer pool instance can be dumped
2461 to a core file.
2462 Emits a warning to the log if could not succeed.
2463 @return true iff succeeded, false if no OS support or failed */
2464 bool madvise_dump();
2465
2466 /** Advices the OS that all chunks in this buffer pool instance should not
2467 be dumped to a core file.
2468 Emits a warning to the log if could not succeed.
2469 @return true iff succeeded, false if no OS support or failed */
2470 bool madvise_dont_dump();
2471
2472 static_assert(BUF_BUDDY_LOW <= UNIV_ZIP_SIZE_MIN,
2473 "BUF_BUDDY_LOW > UNIV_ZIP_SIZE_MIN");
2474 /** @} */
2475 };
2476
2477 /** Print the given buf_pool_t object.
2478 @param[in,out] out the output stream
2479 @param[in] buf_pool the buf_pool_t object to be printed
2480 @return the output stream */
2481 std::ostream &operator<<(std::ostream &out, const buf_pool_t &buf_pool);
2482
2483 /** @name Accessors for buffer pool mutexes
2484 Use these instead of accessing buffer pool mutexes directly. */
2485 /** @{ */
2486
2487 #ifndef UNIV_HOTBACKUP
2488 /** Test if flush list mutex is owned. */
2489 #define buf_flush_list_mutex_own(b) mutex_own(&(b)->flush_list_mutex)
2490
2491 /** Acquire the flush list mutex. */
2492 #define buf_flush_list_mutex_enter(b) \
2493 do { \
2494 mutex_enter(&(b)->flush_list_mutex); \
2495 } while (0)
2496 /** Release the flush list mutex. */
2497 #define buf_flush_list_mutex_exit(b) \
2498 do { \
2499 mutex_exit(&(b)->flush_list_mutex); \
2500 } while (0)
2501 /** Acquire the block->mutex. */
2502 #define buf_page_mutex_enter(b) \
2503 do { \
2504 mutex_enter(&(b)->mutex); \
2505 } while (0)
2506
2507 /** Release the block->mutex. */
2508 #define buf_page_mutex_exit(b) \
2509 do { \
2510 (b)->mutex.exit(); \
2511 } while (0)
2512
2513 /** Get appropriate page_hash_lock. */
2514 inline rw_lock_t *buf_page_hash_lock_get(const buf_pool_t *buf_pool,
2515 const page_id_t page_id) {
2516 return hash_get_lock(buf_pool->page_hash, page_id.hash());
2517 }
2518
2519 /** If not appropriate page_hash_lock, relock until appropriate. */
2520 inline rw_lock_t *buf_page_hash_lock_s_confirm(rw_lock_t *hash_lock,
2521 const buf_pool_t *buf_pool,
2522 const page_id_t page_id) {
2523 return hash_lock_s_confirm(hash_lock, buf_pool->page_hash, page_id.hash());
2524 }
2525
2526 inline rw_lock_t *buf_page_hash_lock_x_confirm(rw_lock_t *hash_lock,
2527 buf_pool_t *buf_pool,
2528 const page_id_t &page_id) {
2529 return hash_lock_x_confirm(hash_lock, buf_pool->page_hash, page_id.hash());
2530 }
2531 #endif /* !UNIV_HOTBACKUP */
2532
2533 #if defined(UNIV_DEBUG) && !defined(UNIV_HOTBACKUP)
2534 /** Test if page_hash lock is held in s-mode. */
2535 inline ulint buf_page_hash_lock_held_s(const buf_pool_t *buf_pool,
2536 const buf_page_t *bpage) {
2537 return rw_lock_own(buf_page_hash_lock_get(buf_pool, bpage->id), RW_LOCK_S);
2538 }
2539
2540 /** Test if page_hash lock is held in x-mode. */
2541 inline ulint buf_page_hash_lock_held_x(const buf_pool_t *buf_pool,
2542 const buf_page_t *bpage) {
2543 return rw_lock_own(buf_page_hash_lock_get((buf_pool), (bpage)->id),
2544 RW_LOCK_X);
2545 }
2546
2547 /** Test if page_hash lock is held in x or s-mode. */
2548 inline bool buf_page_hash_lock_held_s_or_x(const buf_pool_t *buf_pool,
2549 const buf_page_t *bpage) {
2550 return buf_page_hash_lock_held_s(buf_pool, bpage) ||
2551 buf_page_hash_lock_held_x(buf_pool, bpage);
2552 }
2553
2554 inline bool buf_block_hash_lock_held_s(const buf_pool_t *buf_pool,
2555 const buf_block_t *block) {
2556 return buf_page_hash_lock_held_s(buf_pool, &block->page);
2557 }
2558
2559 inline bool buf_block_hash_lock_held_x(const buf_pool_t *buf_pool,
2560 const buf_block_t *block) {
2561 return buf_page_hash_lock_held_x(buf_pool, &block->page);
2562 }
2563
2564 inline bool buf_block_hash_lock_held_s_or_x(const buf_pool_t *buf_pool,
2565 const buf_block_t *block) {
2566 return buf_page_hash_lock_held_s_or_x(buf_pool, &block->page);
2567 }
2568 #else /* UNIV_DEBUG && !UNIV_HOTBACKUP */
2569 #define buf_page_hash_lock_held_s(b, p) (true)
2570 #define buf_page_hash_lock_held_x(b, p) (true)
2571 #define buf_page_hash_lock_held_s_or_x(b, p) (true)
2572 #define buf_block_hash_lock_held_s(b, p) (true)
2573 #define buf_block_hash_lock_held_x(b, p) (true)
2574 #define buf_block_hash_lock_held_s_or_x(b, p) (true)
2575 #endif /* UNIV_DEBUG && !UNIV_HOTBACKUP */
2576
2577 /** @} */
2578
2579 /**********************************************************************
2580 Let us list the consistency conditions for different control block states.
2581
2582 NOT_USED: is in free list, not in LRU list, not in flush list, nor
2583 page hash table
2584 READY_FOR_USE: is not in free list, LRU list, or flush list, nor page
2585 hash table
2586 MEMORY: is not in free list, LRU list, or flush list, nor page
2587 hash table
2588 FILE_PAGE: space and offset are defined, is in page hash table
2589 if io_fix == BUF_IO_WRITE,
2590 pool: no_flush[flush_type] is in reset state,
2591 pool: n_flush[flush_type] > 0
2592
2593 (1) if buf_fix_count == 0, then
2594 is in LRU list, not in free list
2595 is in flush list,
2596 if and only if oldest_modification > 0
2597 is x-locked,
2598 if and only if io_fix == BUF_IO_READ
2599 is s-locked,
2600 if and only if io_fix == BUF_IO_WRITE
2601
2602 (2) if buf_fix_count > 0, then
2603 is not in LRU list, not in free list
2604 is in flush list,
2605 if and only if oldest_modification > 0
2606 if io_fix == BUF_IO_READ,
2607 is x-locked
2608 if io_fix == BUF_IO_WRITE,
2609 is s-locked
2610
2611 State transitions:
2612
2613 NOT_USED => READY_FOR_USE
2614 READY_FOR_USE => MEMORY
2615 READY_FOR_USE => FILE_PAGE
2616 MEMORY => NOT_USED
2617 FILE_PAGE => NOT_USED NOTE: This transition is allowed if and only if
2618 (1) buf_fix_count == 0,
2619 (2) oldest_modification == 0, and
2620 (3) io_fix == 0.
2621 */
2622
2623 #if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
2624 #ifndef UNIV_HOTBACKUP
2625 /** Functor to validate the LRU list. */
2626 struct CheckInLRUList {
2627 void operator()(const buf_page_t *elem) const { ut_a(elem->in_LRU_list); }
2628
2629 static void validate(const buf_pool_t *buf_pool) {
2630 CheckInLRUList check;
2631 ut_list_validate(buf_pool->LRU, check);
2632 }
2633 };
2634
2635 /** Functor to validate the LRU list. */
2636 struct CheckInFreeList {
2637 void operator()(const buf_page_t *elem) const { ut_a(elem->in_free_list); }
2638
2639 static void validate(const buf_pool_t *buf_pool) {
2640 CheckInFreeList check;
2641 ut_list_validate(buf_pool->free, check);
2642 }
2643 };
2644
2645 struct CheckUnzipLRUAndLRUList {
2646 void operator()(const buf_block_t *elem) const {
2647 ut_a(elem->page.in_LRU_list);
2648 ut_a(elem->in_unzip_LRU_list);
2649 }
2650
2651 static void validate(const buf_pool_t *buf_pool) {
2652 CheckUnzipLRUAndLRUList check;
2653 ut_list_validate(buf_pool->unzip_LRU, check);
2654 }
2655 };
2656 #endif /* !UNIV_HOTBACKUP */
2657 #endif /* UNIV_DEBUG || defined UNIV_BUF_DEBUG */
2658
2659 #ifndef UNIV_HOTBACKUP
2660 /** Prepare a page before adding to the free list.
2661 @param[in,out] bpage Buffer page to prepare for freeing. */
2662 inline void buf_page_prepare_for_free(buf_page_t *bpage) noexcept {
2663 bpage->reset_page_id();
2664 }
2665 #endif /* !UNIV_HOTBACKUP */
2666
2667 /** Gets the compressed page descriptor corresponding to an uncompressed
2668 page if applicable.
2669 @param[in] block Get the zip descriptor for this block. */
2670 inline page_zip_des_t *buf_block_get_page_zip(buf_block_t *block) noexcept {
2671 return block->get_page_zip();
2672 }
2673
2674 /** Gets the compressed page descriptor corresponding to an uncompressed
2675 page if applicable. Const version.
2676 @param[in] block Get the zip descriptor for this block.
2677 @return page descriptor or nullptr. */
2678 inline const page_zip_des_t *buf_block_get_page_zip(
2679 const buf_block_t *block) noexcept {
2680 return block->get_page_zip();
2681 }
2682
2683 /** Verify the page contained by the block. If there is page type
2684 mismatch then reset it to expected page type. Data files created
2685 before MySQL 5.7 GA may contain garbage in the FIL_PAGE_TYPE field.
2686 @param[in,out] block block that may possibly have invalid
2687 FIL_PAGE_TYPE
2688 @param[in] type Expected page type
2689 @param[in,out] mtr Mini-transaction */
2690 inline void buf_block_reset_page_type_on_mismatch(buf_block_t &block,
2691 page_type_t type,
2692 mtr_t &mtr) {
2693 byte *page = block.frame;
2694 page_type_t page_type = fil_page_get_type(page);
2695 if (page_type != type) {
2696 const page_id_t &page_id = block.page.id;
2697 fil_page_reset_type(page_id, page, type, &mtr);
2698 }
2699 }
2700 #include "buf0buf.ic"
2701
2702 #endif /* !buf0buf_h */