| 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/buf0flu.h |
| 29 |
The database buffer pool flush algorithm |
| 30 |
|
| 31 |
Created 11/5/1995 Heikki Tuuri |
| 32 |
*******************************************************/ |
| 33 |
|
| 34 |
#ifndef buf0flu_h |
| 35 |
#define buf0flu_h |
| 36 |
|
| 37 |
#include "buf0types.h" |
| 38 |
#include "univ.i" |
| 39 |
#include "ut0byte.h" |
| 40 |
|
| 41 |
#ifndef UNIV_HOTBACKUP |
| 42 |
/** Checks if the page_cleaner is in active state. */ |
| 43 |
bool buf_flush_page_cleaner_is_active(); |
| 44 |
|
| 45 |
#ifdef UNIV_DEBUG |
| 46 |
|
| 47 |
/** Value of MySQL global variable used to disable page cleaner. */ |
| 48 |
extern bool innodb_page_cleaner_disabled_debug; |
| 49 |
|
| 50 |
#endif /* UNIV_DEBUG */ |
| 51 |
|
| 52 |
/** Event to synchronise with the flushing. */ |
| 53 |
extern os_event_t buf_flush_event; |
| 54 |
|
| 55 |
/** Event to wait for one flushing step */ |
| 56 |
extern os_event_t buf_flush_tick_event; |
| 57 |
|
| 58 |
class Alter_stage; |
| 59 |
|
| 60 |
/** Remove a block from the flush list of modified blocks. |
| 61 |
@param[in] bpage pointer to the block in question */ |
| 62 |
void buf_flush_remove(buf_page_t *bpage); |
| 63 |
|
| 64 |
/** Relocates a buffer control block on the flush_list. |
| 65 |
Note that it is assumed that the contents of bpage has already been |
| 66 |
copied to dpage. */ |
| 67 |
void buf_flush_relocate_on_flush_list( |
| 68 |
buf_page_t *bpage, /*!< in/out: control block being moved */ |
| 69 |
buf_page_t *dpage); /*!< in/out: destination block */ |
| 70 |
|
| 71 |
/** Updates the flush system data structures when a write is completed. |
| 72 |
@param[in] bpage pointer to the block in question */ |
| 73 |
void buf_flush_write_complete(buf_page_t *bpage); |
| 74 |
|
| 75 |
#endif /* !UNIV_HOTBACKUP */ |
| 76 |
|
| 77 |
/** Check if page type is uncompressed. |
| 78 |
@param[in] page page frame |
| 79 |
@return true if uncompressed page type. */ |
| 80 |
bool page_is_uncompressed_type(const byte *page); |
| 81 |
|
| 82 |
/** Initialize a page for writing to the tablespace. |
| 83 |
@param[in] block buffer block; NULL if bypassing the buffer pool |
| 84 |
@param[in,out] page page frame |
| 85 |
@param[in,out] page_zip_ compressed page, or NULL if uncompressed |
| 86 |
@param[in] newest_lsn newest modification LSN to the page |
| 87 |
@param[in] skip_checksum whether to disable the page checksum |
| 88 |
@param[in] skip_lsn_check true to skip check for LSN (in DEBUG) */ |
| 89 |
void buf_flush_init_for_writing(const buf_block_t *block, byte *page, |
| 90 |
void *page_zip_, lsn_t newest_lsn, |
| 91 |
bool skip_checksum, bool skip_lsn_check); |
| 92 |
|
| 93 |
#ifndef UNIV_HOTBACKUP |
| 94 |
#if defined UNIV_DEBUG || defined UNIV_IBUF_DEBUG |
| 95 |
/** Writes a flushable page asynchronously from the buffer pool to a file. |
| 96 |
NOTE: block and LRU list mutexes must be held upon entering this function, and |
| 97 |
they will be released by this function after flushing. This is loosely based on |
| 98 |
buf_flush_batch() and buf_flush_page(). |
| 99 |
@param[in,out] buf_pool buffer pool instance |
| 100 |
@param[in,out] block buffer control block |
| 101 |
@return true if the page was flushed and the mutex released */ |
| 102 |
[[nodiscard]] bool buf_flush_page_try(buf_pool_t *buf_pool, buf_block_t *block); |
| 103 |
#endif /* UNIV_DEBUG || UNIV_IBUF_DEBUG */ |
| 104 |
|
| 105 |
/** Do flushing batch of a given type. |
| 106 |
NOTE: The calling thread is not allowed to own any latches on pages! |
| 107 |
@param[in,out] buf_pool buffer pool instance |
| 108 |
@param[in] type flush type |
| 109 |
@param[in] min_n wished minimum number of blocks flushed |
| 110 |
(it is not guaranteed that the actual number is that big, though) |
| 111 |
@param[in] lsn_limit in the case BUF_FLUSH_LIST all blocks whose |
| 112 |
oldest_modification is smaller than this should be flushed (if their number |
| 113 |
does not exceed min_n), otherwise ignored |
| 114 |
@param[out] n_processed the number of pages which were processed is |
| 115 |
passed back to caller. Ignored if NULL |
| 116 |
@retval true if a batch was queued successfully. |
| 117 |
@retval false if another batch of same type was already running. */ |
| 118 |
bool buf_flush_do_batch(buf_pool_t *buf_pool, buf_flush_t type, ulint min_n, |
| 119 |
lsn_t lsn_limit, ulint *n_processed); |
| 120 |
|
| 121 |
/** This utility flushes dirty blocks from the end of the flush list of all |
| 122 |
buffer pool instances. |
| 123 |
NOTE: The calling thread is not allowed to own any latches on pages! |
| 124 |
@param[in] min_n wished minimum number of blocks flushed (it is |
| 125 |
not guaranteed that the actual number is that big, though) |
| 126 |
@param[in] lsn_limit in the case BUF_FLUSH_LIST all blocks whose |
| 127 |
oldest_modification is smaller than this should be flushed (if their number |
| 128 |
does not exceed min_n), otherwise ignored |
| 129 |
@param[out] n_processed the number of pages which were processed is |
| 130 |
passed back to caller. Ignored if NULL. |
| 131 |
@return true if a batch was queued successfully for each buffer pool |
| 132 |
instance. false if another batch of same type was already running in |
| 133 |
at least one of the buffer pool instance */ |
| 134 |
bool buf_flush_lists(ulint min_n, lsn_t lsn_limit, ulint *n_processed); |
| 135 |
|
| 136 |
/** This function picks up a single page from the tail of the LRU |
| 137 |
list, flushes it (if it is dirty), removes it from page_hash and LRU |
| 138 |
list and puts it on the free list. It is called from user threads when |
| 139 |
they are unable to find a replaceable page at the tail of the LRU |
| 140 |
list i.e.: when the background LRU flushing in the page_cleaner thread |
| 141 |
is not fast enough to keep pace with the workload. |
| 142 |
@param[in,out] buf_pool buffer pool instance |
| 143 |
@return true if success. */ |
| 144 |
bool buf_flush_single_page_from_LRU(buf_pool_t *buf_pool); |
| 145 |
|
| 146 |
/** Waits until a flush batch of the given type ends. |
| 147 |
@param[in] buf_pool Buffer pool instance. |
| 148 |
@param[in] flush_type Flush type. */ |
| 149 |
void buf_flush_wait_batch_end(buf_pool_t *buf_pool, buf_flush_t flush_type); |
| 150 |
|
| 151 |
/** Waits until a flush batch of the given type ends. This is called by a |
| 152 |
thread that only wants to wait for a flush to end but doesn't do any flushing |
| 153 |
itself. |
| 154 |
@param[in] buf_pool buffer pool instance |
| 155 |
@param[in] type BUF_FLUSH_LRU or BUF_FLUSH_LIST */ |
| 156 |
void buf_flush_wait_batch_end_wait_only(buf_pool_t *buf_pool, buf_flush_t type); |
| 157 |
|
| 158 |
/** This function should be called at a mini-transaction commit, if a page was |
| 159 |
modified in it. Puts the block to the list of modified blocks, if it not |
| 160 |
already in it. |
| 161 |
@param[in] block block which is modified |
| 162 |
@param[in] start_lsn start lsn of the first mtr in a set of mtr's |
| 163 |
@param[in] end_lsn end lsn of the last mtr in the set of mtr's |
| 164 |
@param[in] observer flush observer */ |
| 165 |
static inline void buf_flush_note_modification(buf_block_t *block, |
| 166 |
lsn_t start_lsn, lsn_t end_lsn, |
| 167 |
Flush_observer *observer); |
| 168 |
|
| 169 |
/** This function should be called when recovery has modified a buffer page. |
| 170 |
@param[in] block block which is modified |
| 171 |
@param[in] start_lsn start lsn of the first mtr in a set of mtr's |
| 172 |
@param[in] end_lsn end lsn of the last mtr in the set of mtr's */ |
| 173 |
static inline void buf_flush_recv_note_modification(buf_block_t *block, |
| 174 |
lsn_t start_lsn, |
| 175 |
lsn_t end_lsn); |
| 176 |
|
| 177 |
/** Returns true if the file page block is immediately suitable for replacement, |
| 178 |
i.e., the transition FILE_PAGE => NOT_USED allowed. The caller must hold the |
| 179 |
LRU list and block mutexes. |
| 180 |
@param[in] bpage buffer control block, must be buf_page_in_file() and |
| 181 |
in the LRU list |
| 182 |
@return true if can replace immediately */ |
| 183 |
bool buf_flush_ready_for_replace(const buf_page_t *bpage); |
| 184 |
|
| 185 |
#ifdef UNIV_DEBUG |
| 186 |
struct SYS_VAR; |
| 187 |
|
| 188 |
/** Disables page cleaner threads (coordinator and workers). |
| 189 |
It's used by: SET GLOBAL innodb_page_cleaner_disabled_debug = 1 (0). |
| 190 |
@param[in] thd thread handle |
| 191 |
@param[in] var pointer to system variable |
| 192 |
@param[out] var_ptr where the formal string goes |
| 193 |
@param[in] save immediate result from check function */ |
| 194 |
void buf_flush_page_cleaner_disabled_debug_update(THD *thd, SYS_VAR *var, |
| 195 |
void *var_ptr, |
| 196 |
const void *save); |
| 197 |
#endif /* UNIV_DEBUG */ |
| 198 |
|
| 199 |
/** Initialize page_cleaner. */ |
| 200 |
void buf_flush_page_cleaner_init(); |
| 201 |
|
| 202 |
/** Wait for any possible LRU flushes that are in progress to end. */ |
| 203 |
void buf_flush_wait_LRU_batch_end(); |
| 204 |
|
| 205 |
#if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG |
| 206 |
/** Validates the flush list. |
| 207 |
@return true if ok */ |
| 208 |
bool buf_flush_validate(buf_pool_t *buf_pool); |
| 209 |
#endif /* UNIV_DEBUG || UNIV_BUF_DEBUG */ |
| 210 |
|
| 211 |
/** Initialize the red-black tree to speed up insertions into the flush_list |
| 212 |
during recovery process. Should be called at the start of recovery |
| 213 |
process before any page has been read/written. */ |
| 214 |
void buf_flush_init_flush_rbt(void); |
| 215 |
|
| 216 |
/** Frees up the red-black tree. */ |
| 217 |
void buf_flush_free_flush_rbt(void); |
| 218 |
|
| 219 |
/** Writes a flushable page asynchronously from the buffer pool to a file. |
| 220 |
NOTE: 1. in simulated aio we must call os_aio_simulated_wake_handler_threads |
| 221 |
after we have posted a batch of writes! 2. buf_page_get_mutex(bpage) must be |
| 222 |
held upon entering this function. The LRU list mutex must be held if flush_type |
| 223 |
== BUF_FLUSH_SINGLE_PAGE. Both mutexes will be released by this function if it |
| 224 |
returns true. |
| 225 |
@param[in] buf_pool buffer pool instance |
| 226 |
@param[in] bpage buffer control block |
| 227 |
@param[in] flush_type type of flush |
| 228 |
@param[in] sync true if sync IO request |
| 229 |
@return true if page was flushed */ |
| 230 |
bool buf_flush_page(buf_pool_t *buf_pool, buf_page_t *bpage, |
| 231 |
buf_flush_t flush_type, bool sync); |
| 232 |
|
| 233 |
/** Check if the block is modified and ready for flushing. |
| 234 |
Requires buf_page_get_mutex(bpage). |
| 235 |
@param[in] bpage buffer control block, must be buf_page_in_file() |
| 236 |
@param[in] flush_type type of flush |
| 237 |
@return true if can flush immediately */ |
| 238 |
[[nodiscard]] bool buf_flush_ready_for_flush(buf_page_t *bpage, |
| 239 |
buf_flush_t flush_type); |
| 240 |
|
| 241 |
/** Check if there are any dirty pages that belong to a space id in the flush |
| 242 |
list in a particular buffer pool. |
| 243 |
@return number of dirty pages present in a single buffer pool */ |
| 244 |
ulint buf_pool_get_dirty_pages_count( |
| 245 |
buf_pool_t *buf_pool, /*!< in: buffer pool */ |
| 246 |
space_id_t id, /*!< in: space id to check */ |
| 247 |
Flush_observer *observer); /*!< in: flush observer to check */ |
| 248 |
|
| 249 |
/** Executes fsync for all tablespaces, to fsync all pages written to disk. */ |
| 250 |
void buf_flush_fsync(); |
| 251 |
|
| 252 |
/** Synchronously flush dirty blocks from the end of the flush list of all |
| 253 |
buffer pool instances. NOTE: The calling thread is not allowed to own any |
| 254 |
latches on pages! */ |
| 255 |
void buf_flush_sync_all_buf_pools(); |
| 256 |
|
| 257 |
/** Checks if all flush lists are empty. It is supposed to be used in |
| 258 |
single thread, during startup or shutdown. Hence it does not acquire |
| 259 |
lock and it is caller's responsibility to guarantee that flush lists |
| 260 |
are not changed in background. |
| 261 |
@return true if all flush lists were empty. */ |
| 262 |
bool buf_are_flush_lists_empty_validate(); |
| 263 |
|
| 264 |
/** We use Flush_observer to track flushing of non-redo logged pages in bulk |
| 265 |
create index(btr0load.cc).Since we disable redo logging during a index build, |
| 266 |
we need to make sure that all dirty pages modified by the index build are |
| 267 |
flushed to disk before any redo logged operations go to the index. */ |
| 268 |
|
| 269 |
class Flush_observer { |
| 270 |
public: |
| 271 |
/** Constructor |
| 272 |
@param[in] space_id table space id |
| 273 |
@param[in] trx trx instance |
| 274 |
@param[in,out] stage PFS progress monitoring instance, it's used by |
| 275 |
ALTER TABLE. It is passed to log_preflush_pool_modified_pages() for |
| 276 |
accounting. */ |
| 277 |
Flush_observer(space_id_t space_id, trx_t *trx, Alter_stage *stage) noexcept; |
| 278 |
|
| 279 |
/** Destructor */ |
| 280 |
~Flush_observer() noexcept; |
| 281 |
|
| 282 |
/** Check pages have been flushed and removed from the flush list |
| 283 |
in a buffer pool instance. |
| 284 |
@param[in] instance_no buffer pool instance no |
| 285 |
@return true if the pages were removed from the flush list */ |
| 286 |
bool is_complete(size_t instance_no) { |
| 287 |
return m_flushed[instance_no].fetch_add(0, std::memory_order_relaxed) == |
| 288 |
m_removed[instance_no].fetch_add(0, std::memory_order_relaxed) || |
| 289 |
m_interrupted; |
| 290 |
} |
| 291 |
|
| 292 |
/** Interrupt observer not to wait. */ |
| 293 |
void interrupted() { m_interrupted = true; } |
| 294 |
|
| 295 |
/** Check whether trx is interrupted |
| 296 |
@return true if trx is interrupted */ |
| 297 |
bool check_interrupted(); |
| 298 |
|
| 299 |
/** Flush dirty pages. */ |
| 300 |
void flush(); |
| 301 |
|
| 302 |
/** Notify observer of flushing a page |
| 303 |
@param[in] buf_pool buffer pool instance |
| 304 |
@param[in] bpage buffer page to flush */ |
| 305 |
void notify_flush(buf_pool_t *buf_pool, buf_page_t *bpage); |
| 306 |
|
| 307 |
/** Notify observer of removing a page from flush list |
| 308 |
@param[in] buf_pool buffer pool instance |
| 309 |
@param[in] bpage buffer page flushed */ |
| 310 |
void notify_remove(buf_pool_t *buf_pool, buf_page_t *bpage); |
| 311 |
|
| 312 |
private: |
| 313 |
using Counter = std::atomic_int; |
| 314 |
using Counters = std::vector<Counter, ut::allocator<Counter>>; |
| 315 |
|
| 316 |
/** Tablespace ID. */ |
| 317 |
space_id_t m_space_id{}; |
| 318 |
|
| 319 |
/** Trx instance */ |
| 320 |
trx_t *m_trx{}; |
| 321 |
|
| 322 |
/** Performance schema accounting object, used by ALTER TABLE. |
| 323 |
If not nullptr, then stage->begin_phase_flush() will be called initially, |
| 324 |
specifying the number of pages to be attempted to be flushed and |
| 325 |
subsequently, stage->inc() will be called for each page we attempt to |
| 326 |
flush. */ |
| 327 |
Alter_stage *m_stage{}; |
| 328 |
|
| 329 |
/** Flush request sent, per buffer pool. */ |
| 330 |
Counters m_flushed{}; |
| 331 |
|
| 332 |
/** Flush request finished, per buffer pool. */ |
| 333 |
Counters m_removed{}; |
| 334 |
|
| 335 |
/** Number of pages using this instance. */ |
| 336 |
Counter m_n_ref_count{}; |
| 337 |
|
| 338 |
/** True if the operation was interrupted. */ |
| 339 |
bool m_interrupted{}; |
| 340 |
}; |
| 341 |
|
| 342 |
lsn_t get_flush_sync_lsn() noexcept; |
| 343 |
#endif /* !UNIV_HOTBACKUP */ |
| 344 |
|
| 345 |
#include "buf0flu.ic" |
| 346 |
|
| 347 |
#endif |