MySQL 8.1.0
Source Code Documentation
buf0flu.h
Go to the documentation of this file.
1/*****************************************************************************
2
3Copyright (c) 1995, 2023, Oracle and/or its affiliates.
4
5This program is free software; you can redistribute it and/or modify it under
6the terms of the GNU General Public License, version 2.0, as published by the
7Free Software Foundation.
8
9This program is also distributed with certain software (including but not
10limited to OpenSSL) that is licensed under separate terms, as designated in a
11particular file or component or in included license documentation. The authors
12of MySQL hereby grant you an additional permission to link the program and
13your derivative works with the separately licensed software that they have
14included with MySQL.
15
16This program is distributed in the hope that it will be useful, but WITHOUT
17ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
19for more details.
20
21You should have received a copy of the GNU General Public License along with
22this program; if not, write to the Free Software Foundation, Inc.,
2351 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24
25*****************************************************************************/
26
27/** @file include/buf0flu.h
28 The database buffer pool flush algorithm
29
30 Created 11/5/1995 Heikki Tuuri
31 *******************************************************/
32
33#ifndef buf0flu_h
34#define buf0flu_h
35
36#include "buf0types.h"
37#include "univ.i"
38#include "ut0byte.h"
39
40#ifndef UNIV_HOTBACKUP
41/** Checks if the page_cleaner is in active state. */
43
44#ifdef UNIV_DEBUG
45
46/** Value of MySQL global variable used to disable page cleaner. */
48
49#endif /* UNIV_DEBUG */
50
51/** Event to synchronise with the flushing. */
53
54/** Event to wait for one flushing step */
56
57class Alter_stage;
58
59/** Remove a block from the flush list of modified blocks.
60@param[in] bpage pointer to the block in question */
61void buf_flush_remove(buf_page_t *bpage);
62
63/** Relocates a buffer control block on the flush_list.
64 Note that it is assumed that the contents of bpage has already been
65 copied to dpage. */
67 buf_page_t *bpage, /*!< in/out: control block being moved */
68 buf_page_t *dpage); /*!< in/out: destination block */
69
70/** Updates the flush system data structures when a write is completed.
71@param[in] bpage pointer to the block in question */
73
74#endif /* !UNIV_HOTBACKUP */
75
76/** Check if page type is uncompressed.
77@param[in] page page frame
78@return true if uncompressed page type. */
79bool page_is_uncompressed_type(const byte *page);
80
81/** Initialize a page for writing to the tablespace.
82@param[in] block buffer block; NULL if bypassing the buffer pool
83@param[in,out] page page frame
84@param[in,out] page_zip_ compressed page, or NULL if uncompressed
85@param[in] newest_lsn newest modification LSN to the page
86@param[in] skip_checksum whether to disable the page checksum
87@param[in] skip_lsn_check true to skip check for LSN (in DEBUG) */
88void buf_flush_init_for_writing(const buf_block_t *block, byte *page,
89 void *page_zip_, lsn_t newest_lsn,
90 bool skip_checksum, bool skip_lsn_check);
91
92#ifndef UNIV_HOTBACKUP
93#if defined UNIV_DEBUG || defined UNIV_IBUF_DEBUG
94/** Writes a flushable page asynchronously from the buffer pool to a file.
95NOTE: block and LRU list mutexes must be held upon entering this function, and
96they will be released by this function after flushing. This is loosely based on
97buf_flush_batch() and buf_flush_page().
98@param[in,out] buf_pool buffer pool instance
99@param[in,out] block buffer control block
100@return true if the page was flushed and the mutex released */
101[[nodiscard]] bool buf_flush_page_try(buf_pool_t *buf_pool, buf_block_t *block);
102#endif /* UNIV_DEBUG || UNIV_IBUF_DEBUG */
103
104/** Do flushing batch of a given type.
105NOTE: The calling thread is not allowed to own any latches on pages!
106@param[in,out] buf_pool buffer pool instance
107@param[in] type flush type
108@param[in] min_n wished minimum number of blocks flushed
109(it is not guaranteed that the actual number is that big, though)
110@param[in] lsn_limit in the case BUF_FLUSH_LIST all blocks whose
111oldest_modification is smaller than this should be flushed (if their number
112does not exceed min_n), otherwise ignored
113@param[out] n_processed the number of pages which were processed is
114passed back to caller. Ignored if NULL
115@retval true if a batch was queued successfully.
116@retval false if another batch of same type was already running. */
117bool buf_flush_do_batch(buf_pool_t *buf_pool, buf_flush_t type, ulint min_n,
118 lsn_t lsn_limit, ulint *n_processed);
119
120/** This utility flushes dirty blocks from the end of the flush list of all
121buffer pool instances.
122NOTE: The calling thread is not allowed to own any latches on pages!
123@param[in] min_n wished minimum number of blocks flushed (it is
124not guaranteed that the actual number is that big, though)
125@param[in] lsn_limit in the case BUF_FLUSH_LIST all blocks whose
126oldest_modification is smaller than this should be flushed (if their number
127does not exceed min_n), otherwise ignored
128@param[out] n_processed the number of pages which were processed is
129passed back to caller. Ignored if NULL.
130@return true if a batch was queued successfully for each buffer pool
131instance. false if another batch of same type was already running in
132at least one of the buffer pool instance */
133bool buf_flush_lists(ulint min_n, lsn_t lsn_limit, ulint *n_processed);
134
135/** This function picks up a single page from the tail of the LRU
136list, flushes it (if it is dirty), removes it from page_hash and LRU
137list and puts it on the free list. It is called from user threads when
138they are unable to find a replaceable page at the tail of the LRU
139list i.e.: when the background LRU flushing in the page_cleaner thread
140is not fast enough to keep pace with the workload.
141@param[in,out] buf_pool buffer pool instance
142@return true if success. */
144
145/** Waits until a flush batch of the given type ends.
146@param[in] buf_pool Buffer pool instance.
147@param[in] flush_type Flush type. */
149
150/** Waits until a flush batch of the given type ends. This is called by a
151thread that only wants to wait for a flush to end but doesn't do any flushing
152itself.
153@param[in] buf_pool buffer pool instance
154@param[in] type BUF_FLUSH_LRU or BUF_FLUSH_LIST */
156
157/** This function should be called at a mini-transaction commit, if a page was
158modified in it. Puts the block to the list of modified blocks, if it not
159already in it.
160@param[in] block block which is modified
161@param[in] start_lsn start lsn of the first mtr in a set of mtr's
162@param[in] end_lsn end lsn of the last mtr in the set of mtr's
163@param[in] observer flush observer */
164static inline void buf_flush_note_modification(buf_block_t *block,
165 lsn_t start_lsn, lsn_t end_lsn,
166 Flush_observer *observer);
167
168/** This function should be called when recovery has modified a buffer page.
169@param[in] block block which is modified
170@param[in] start_lsn start lsn of the first mtr in a set of mtr's
171@param[in] end_lsn end lsn of the last mtr in the set of mtr's */
173 lsn_t start_lsn,
174 lsn_t end_lsn);
175
176/** Returns true if the file page block is immediately suitable for replacement,
177i.e., the transition FILE_PAGE => NOT_USED allowed. The caller must hold the
178LRU list and block mutexes.
179@param[in] bpage buffer control block, must be buf_page_in_file() and
180 in the LRU list
181@return true if can replace immediately */
183
184#ifdef UNIV_DEBUG
185struct SYS_VAR;
186
187/** Disables page cleaner threads (coordinator and workers).
188It's used by: SET GLOBAL innodb_page_cleaner_disabled_debug = 1 (0).
189@param[in] thd thread handle
190@param[in] var pointer to system variable
191@param[out] var_ptr where the formal string goes
192@param[in] save immediate result from check function */
194 void *var_ptr,
195 const void *save);
196#endif /* UNIV_DEBUG */
197
198/** Initialize page_cleaner. */
200
201/** Wait for any possible LRU flushes that are in progress to end. */
203
204#if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
205/** Validates the flush list.
206 @return true if ok */
207bool buf_flush_validate(buf_pool_t *buf_pool);
208#endif /* UNIV_DEBUG || UNIV_BUF_DEBUG */
209
210/** Initialize the red-black tree to speed up insertions into the flush_list
211 during recovery process. Should be called at the start of recovery
212 process before any page has been read/written. */
213void buf_flush_init_flush_rbt(void);
214
215/** Frees up the red-black tree. */
216void buf_flush_free_flush_rbt(void);
217
218/** Writes a flushable page asynchronously from the buffer pool to a file.
219NOTE: 1. in simulated aio we must call os_aio_simulated_wake_handler_threads
220after we have posted a batch of writes! 2. buf_page_get_mutex(bpage) must be
221held upon entering this function. The LRU list mutex must be held if flush_type
222== BUF_FLUSH_SINGLE_PAGE. Both mutexes will be released by this function if it
223returns true.
224@param[in] buf_pool buffer pool instance
225@param[in] bpage buffer control block
226@param[in] flush_type type of flush
227@param[in] sync true if sync IO request
228@return true if page was flushed */
229bool buf_flush_page(buf_pool_t *buf_pool, buf_page_t *bpage,
230 buf_flush_t flush_type, bool sync);
231
232/** Check if the block is modified and ready for flushing.
233Requires buf_page_get_mutex(bpage).
234@param[in] bpage buffer control block, must be buf_page_in_file()
235@param[in] flush_type type of flush
236@return true if can flush immediately */
237[[nodiscard]] bool buf_flush_ready_for_flush(buf_page_t *bpage,
239
240/** Check if there are any dirty pages that belong to a space id in the flush
241 list in a particular buffer pool.
242 @return number of dirty pages present in a single buffer pool */
244 buf_pool_t *buf_pool, /*!< in: buffer pool */
245 space_id_t id, /*!< in: space id to check */
246 Flush_observer *observer); /*!< in: flush observer to check */
247
248/** Executes fsync for all tablespaces, to fsync all pages written to disk. */
249void buf_flush_fsync();
250
251/** Synchronously flush dirty blocks from the end of the flush list of all
252 buffer pool instances. NOTE: The calling thread is not allowed to own any
253 latches on pages! */
255
256/** Checks if all flush lists are empty. It is supposed to be used in
257single thread, during startup or shutdown. Hence it does not acquire
258lock and it is caller's responsibility to guarantee that flush lists
259are not changed in background.
260@return true if all flush lists were empty. */
262
263/** We use Flush_observer to track flushing of non-redo logged pages in bulk
264create index(btr0load.cc).Since we disable redo logging during a index build,
265we need to make sure that all dirty pages modified by the index build are
266flushed to disk before any redo logged operations go to the index. */
267
269 public:
270 /** Constructor
271 @param[in] space_id table space id
272 @param[in] trx trx instance
273 @param[in,out] stage PFS progress monitoring instance, it's used by
274 ALTER TABLE. It is passed to log_preflush_pool_modified_pages() for
275 accounting. */
276 Flush_observer(space_id_t space_id, trx_t *trx, Alter_stage *stage) noexcept;
277
278 /** Destructor */
279 ~Flush_observer() noexcept;
280
281 /** Check pages have been flushed and removed from the flush list
282 in a buffer pool instance.
283 @param[in] instance_no buffer pool instance no
284 @return true if the pages were removed from the flush list */
285 bool is_complete(size_t instance_no) {
286 return m_flushed[instance_no].fetch_add(0, std::memory_order_relaxed) ==
287 m_removed[instance_no].fetch_add(0, std::memory_order_relaxed) ||
289 }
290
291 /** Interrupt observer not to wait. */
292 void interrupted() { m_interrupted = true; }
293
294 /** Check whether trx is interrupted
295 @return true if trx is interrupted */
296 bool check_interrupted();
297
298 /** Flush dirty pages. */
299 void flush();
300
301 /** Notify observer of flushing a page
302 @param[in] buf_pool buffer pool instance
303 @param[in] bpage buffer page to flush */
304 void notify_flush(buf_pool_t *buf_pool, buf_page_t *bpage);
305
306 /** Notify observer of removing a page from flush list
307 @param[in] buf_pool buffer pool instance
308 @param[in] bpage buffer page flushed */
309 void notify_remove(buf_pool_t *buf_pool, buf_page_t *bpage);
310
311 private:
312 using Counter = std::atomic_int;
313 using Counters = std::vector<Counter, ut::allocator<Counter>>;
314
315 /** Tablespace ID. */
317
318 /** Trx instance */
320
321 /** Performance schema accounting object, used by ALTER TABLE.
322 If not nullptr, then stage->begin_phase_flush() will be called initially,
323 specifying the number of pages to be attempted to be flushed and
324 subsequently, stage->inc() will be called for each page we attempt to
325 flush. */
327
328 /** Flush request sent, per buffer pool. */
330
331 /** Flush request finished, per buffer pool. */
333
334 /** Number of pages using this instance. */
336
337 /** True if the operation was interrupted. */
339};
340
341lsn_t get_flush_sync_lsn() noexcept;
342#endif /* !UNIV_HOTBACKUP */
343
344#include "buf0flu.ic"
345
346#endif
uint32_t space_id_t
Tablespace identifier.
Definition: api0api.h:50
void buf_flush_fsync()
Executes fsync for all tablespaces, to fsync all pages written to disk.
Definition: buf0flu.cc:3625
void buf_flush_wait_batch_end(buf_pool_t *buf_pool, buf_flush_t flush_type)
Waits until a flush batch of the given type ends.
Definition: buf0flu.cc:2051
bool buf_flush_ready_for_replace(buf_page_t *bpage)
Returns true if the file page block is immediately suitable for replacement, i.e.,...
Definition: buf0flu.cc:683
ulint buf_pool_get_dirty_pages_count(buf_pool_t *buf_pool, space_id_t id, Flush_observer *observer)
Check if there are any dirty pages that belong to a space id in the flush list in a particular buffer...
Definition: buf0flu.cc:3757
void buf_flush_write_complete(buf_page_t *bpage)
Updates the flush system data structures when a write is completed.
Definition: buf0flu.cc:916
bool buf_flush_do_batch(buf_pool_t *buf_pool, buf_flush_t type, ulint min_n, lsn_t lsn_limit, ulint *n_processed)
Do flushing batch of a given type.
Definition: buf0flu.cc:2071
void buf_flush_sync_all_buf_pools()
Synchronously flush dirty blocks from the end of the flush list of all buffer pool instances.
Definition: buf0flu.cc:3653
bool buf_flush_single_page_from_LRU(buf_pool_t *buf_pool)
This function picks up a single page from the tail of the LRU list, flushes it (if it is dirty),...
Definition: buf0flu.cc:2156
void buf_flush_wait_batch_end_wait_only(buf_pool_t *buf_pool, buf_flush_t type)
Waits until a flush batch of the given type ends.
bool buf_flush_validate(buf_pool_t *buf_pool)
Validates the flush list.
Definition: buf0flu.cc:3743
void buf_flush_init_flush_rbt(void)
Initialize the red-black tree to speed up insertions into the flush_list during recovery process.
Definition: buf0flu.cc:369
bool buf_flush_lists(ulint min_n, lsn_t lsn_limit, ulint *n_processed)
This utility flushes dirty blocks from the end of the flush list of all buffer pool instances.
Definition: buf0flu.cc:2094
static void buf_flush_recv_note_modification(buf_block_t *block, lsn_t start_lsn, lsn_t end_lsn)
This function should be called when recovery has modified a buffer page.
void buf_flush_page_cleaner_disabled_debug_update(THD *thd, SYS_VAR *var, void *var_ptr, const void *save)
Disables page cleaner threads (coordinator and workers).
Definition: buf0flu.cc:3116
bool buf_flush_page(buf_pool_t *buf_pool, buf_page_t *bpage, buf_flush_t flush_type, bool sync)
Writes a flushable page asynchronously from the buffer pool to a file.
Definition: buf0flu.cc:1274
void buf_flush_remove(buf_page_t *bpage)
Remove a block from the flush list of modified blocks.
Definition: buf0flu.cc:782
os_event_t buf_flush_tick_event
Event to wait for one flushing step.
Definition: buf0flu.cc:104
bool buf_flush_page_try(buf_pool_t *buf_pool, buf_block_t *block)
Writes a flushable page asynchronously from the buffer pool to a file.
Definition: buf0flu.cc:1406
os_event_t buf_flush_event
Event to synchronise with the flushing.
Definition: buf0flu.cc:101
static void buf_flush_note_modification(buf_block_t *block, lsn_t start_lsn, lsn_t end_lsn, Flush_observer *observer)
This function should be called at a mini-transaction commit, if a page was modified in it.
void buf_flush_wait_LRU_batch_end()
Wait for any possible LRU flushes that are in progress to end.
Definition: buf0flu.cc:2268
void buf_flush_free_flush_rbt(void)
Frees up the red-black tree.
Definition: buf0flu.cc:389
void buf_flush_relocate_on_flush_list(buf_page_t *bpage, buf_page_t *dpage)
Relocates a buffer control block on the flush_list.
Definition: buf0flu.cc:859
void buf_flush_page_cleaner_init()
Initialize page_cleaner.
Definition: buf0flu.cc:2826
lsn_t get_flush_sync_lsn() noexcept
Get the lsn up to which data pages are to be synchronously flushed.
Definition: buf0flu.cc:92
bool buf_are_flush_lists_empty_validate()
Checks if all flush lists are empty.
Definition: buf0flu.cc:410
bool innodb_page_cleaner_disabled_debug
Value of MySQL global variable used to disable page cleaner.
Definition: buf0flu.cc:202
bool buf_flush_ready_for_flush(buf_page_t *bpage, buf_flush_t flush_type)
Check if the block is modified and ready for flushing.
Definition: buf0flu.cc:776
void buf_flush_init_for_writing(const buf_block_t *block, byte *page, void *page_zip_, lsn_t newest_lsn, bool skip_checksum, bool skip_lsn_check)
Initialize a page for writing to the tablespace.
Definition: buf0flu.cc:995
bool buf_flush_page_cleaner_is_active()
Checks if the page_cleaner is in active state.
Definition: buf0flu.cc:2822
bool page_is_uncompressed_type(const byte *page)
Check if page type is uncompressed.
Definition: buf0flu.cc:971
The database buffer pool flush algorithm.
The database buffer pool global types for the directory.
buf_flush_t
Flags for flush types.
Definition: buf0types.h:67
Class used to report ALTER TABLE progress via performance_schema.
Definition: ut0stage.h:80
We use Flush_observer to track flushing of non-redo logged pages in bulk create index(btr0load....
Definition: buf0flu.h:268
trx_t * m_trx
Trx instance.
Definition: buf0flu.h:319
Counters m_flushed
Flush request sent, per buffer pool.
Definition: buf0flu.h:329
space_id_t m_space_id
Tablespace ID.
Definition: buf0flu.h:316
Flush_observer(space_id_t space_id, trx_t *trx, Alter_stage *stage) noexcept
Constructor.
Definition: buf0flu.cc:3804
void flush()
Flush dirty pages.
Definition: buf0flu.cc:3851
Counters m_removed
Flush request finished, per buffer pool.
Definition: buf0flu.h:332
void interrupted()
Interrupt observer not to wait.
Definition: buf0flu.h:292
bool check_interrupted()
Check whether trx is interrupted.
Definition: buf0flu.cc:3829
~Flush_observer() noexcept
Destructor.
Definition: buf0flu.cc:3818
void notify_remove(buf_pool_t *buf_pool, buf_page_t *bpage)
Notify observer of removing a page from flush list.
Definition: buf0flu.cc:3847
bool m_interrupted
True if the operation was interrupted.
Definition: buf0flu.h:338
void notify_flush(buf_pool_t *buf_pool, buf_page_t *bpage)
Notify observer of flushing a page.
Definition: buf0flu.cc:3839
std::vector< Counter, ut::allocator< Counter > > Counters
Definition: buf0flu.h:313
Counter m_n_ref_count
Number of pages using this instance.
Definition: buf0flu.h:335
Alter_stage * m_stage
Performance schema accounting object, used by ALTER TABLE.
Definition: buf0flu.h:326
bool is_complete(size_t instance_no)
Check pages have been flushed and removed from the flush list in a buffer pool instance.
Definition: buf0flu.h:285
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:33
Definition: buf0buf.h:1124
int page
Definition: ctype-mb.cc:1233
flush_type
Definition: my_sys.h:294
uint64_t lsn_t
Type used for all log sequence number storage and arithmetic.
Definition: log0types.h:62
Sharded atomic counter.
Definition: ut0counter.h:220
required string type
Definition: replication_group_member_actions.proto:33
Definition: plugin.h:67
The buffer control block structure.
Definition: buf0buf.h:1689
The buffer pool structure.
Definition: buf0buf.h:2174
InnoDB condition variable.
Definition: os0event.cc:62
Definition: trx0trx.h:685
Version control for database, common definitions, and include files.
unsigned long int ulint
Definition: univ.i:405
Utilities for byte operations.