MySQL  8.0.18
Source Code Documentation
buf0flu.h
Go to the documentation of this file.
1 /*****************************************************************************
2 
3 Copyright (c) 1995, 2019, Oracle and/or its affiliates. All Rights Reserved.
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 also distributed with certain software (including but not
10 limited to OpenSSL) that is licensed under separate terms, as designated in a
11 particular file or component or in included license documentation. The authors
12 of MySQL hereby grant you an additional permission to link the program and
13 your derivative works with the separately licensed software that they have
14 included with MySQL.
15 
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
19 for more details.
20 
21 You should have received a copy of the GNU General Public License along with
22 this program; if not, write to the Free Software Foundation, Inc.,
23 51 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 "log0log.h"
38 #include "univ.i"
39 #include "ut0byte.h"
40 
41 #ifndef UNIV_HOTBACKUP
42 /** Checks if the page_cleaner is in active state. */
44 
45 #ifdef UNIV_DEBUG
46 
47 /** Value of MySQL global variable used to disable page cleaner. */
49 
50 #endif /* UNIV_DEBUG */
51 
52 /** Event to synchronise with the flushing. */
54 
55 class ut_stage_alter_t;
56 
57 /** Remove a block from the flush list of modified blocks.
58 @param[in] bpage pointer to the block in question */
59 void buf_flush_remove(buf_page_t *bpage);
60 
61 /** Relocates a buffer control block on the flush_list.
62  Note that it is assumed that the contents of bpage has already been
63  copied to dpage. */
65  buf_page_t *bpage, /*!< in/out: control block being moved */
66  buf_page_t *dpage); /*!< in/out: destination block */
67 
68 /** Updates the flush system data structures when a write is completed.
69 @param[in] bpage pointer to the block in question */
71 
72 #endif /* !UNIV_HOTBACKUP */
73 
74 /** Check if page type is uncompressed.
75 @param[in] page page frame
76 @return true if uncompressed page type. */
78 
79 /** Initialize a page for writing to the tablespace.
80 @param[in] block buffer block; NULL if bypassing the buffer pool
81 @param[in,out] page page frame
82 @param[in,out] page_zip_ compressed page, or NULL if uncompressed
83 @param[in] newest_lsn newest modification LSN to the page
84 @param[in] skip_checksum whether to disable the page checksum
85 @param[in] skip_lsn_check true to skip check for lsn (in DEBUG) */
87  void *page_zip_, lsn_t newest_lsn,
88  bool skip_checksum, bool skip_lsn_check);
89 
90 #ifndef UNIV_HOTBACKUP
91 #if defined UNIV_DEBUG || defined UNIV_IBUF_DEBUG
92 /** Writes a flushable page asynchronously from the buffer pool to a file.
93 NOTE: block and LRU list mutexes must be held upon entering this function, and
94 they will be released by this function after flushing. This is loosely based on
95 buf_flush_batch() and buf_flush_page().
96 @param[in,out] buf_pool buffer pool instance
97 @param[in,out] block buffer control block
98 @return true if the page was flushed and the mutex released */
99 ibool buf_flush_page_try(buf_pool_t *buf_pool, buf_block_t *block)
100  MY_ATTRIBUTE((warn_unused_result));
101 #endif /* UNIV_DEBUG || UNIV_IBUF_DEBUG */
102 /** Do flushing batch of a given type.
103 NOTE: The calling thread is not allowed to own any latches on pages!
104 @param[in,out] buf_pool buffer pool instance
105 @param[in] type flush type
106 @param[in] min_n wished minimum mumber of blocks flushed
107 (it is not guaranteed that the actual number is that big, though)
108 @param[in] lsn_limit in the case BUF_FLUSH_LIST all blocks whose
109 oldest_modification is smaller than this should be flushed (if their number
110 does not exceed min_n), otherwise ignored
111 @param[out] n_processed the number of pages which were processed is
112 passed back to caller. Ignored if NULL
113 @retval true if a batch was queued successfully.
114 @retval false if another batch of same type was already running. */
115 bool buf_flush_do_batch(buf_pool_t *buf_pool, buf_flush_t type, ulint min_n,
116  lsn_t lsn_limit, ulint *n_processed);
117 
118 /** This utility flushes dirty blocks from the end of the flush list of all
119 buffer pool instances.
120 NOTE: The calling thread is not allowed to own any latches on pages!
121 @param[in] min_n wished minimum mumber of blocks flushed (it is
122 not guaranteed that the actual number is that big, though)
123 @param[in] lsn_limit in the case BUF_FLUSH_LIST all blocks whose
124 oldest_modification is smaller than this should be flushed (if their number
125 does not exceed min_n), otherwise ignored
126 @param[out] n_processed the number of pages which were processed is
127 passed back to caller. Ignored if NULL.
128 @return true if a batch was queued successfully for each buffer pool
129 instance. false if another batch of same type was already running in
130 at least one of the buffer pool instance */
131 bool buf_flush_lists(ulint min_n, lsn_t lsn_limit, ulint *n_processed);
132 
133 /** This function picks up a single page from the tail of the LRU
134 list, flushes it (if it is dirty), removes it from page_hash and LRU
135 list and puts it on the free list. It is called from user threads when
136 they are unable to find a replaceable page at the tail of the LRU
137 list i.e.: when the background LRU flushing in the page_cleaner thread
138 is not fast enough to keep pace with the workload.
139 @param[in,out] buf_pool buffer pool instance
140 @return true if success. */
142 
143 /** Waits until a flush batch of the given type ends */
145  buf_pool_t *buf_pool, /*!< in: buffer pool instance */
146  buf_flush_t type); /*!< in: BUF_FLUSH_LRU
147  or BUF_FLUSH_LIST */
148 
149 /** Waits until a flush batch of the given type ends. This is called by a
150 thread that only wants to wait for a flush to end but doesn't do any flushing
151 itself.
152 @param[in] buf_pool buffer pool instance
153 @param[in] type BUF_FLUSH_LRU or BUF_FLUSH_LIST */
155 
156 /** This function should be called at a mini-transaction commit, if a page was
157 modified in it. Puts the block to the list of modified blocks, if it not
158 already in it.
159 @param[in] block block which is modified
160 @param[in] start_lsn start lsn of the first mtr in a set of mtr's
161 @param[in] end_lsn end lsn of the last mtr in the set of mtr's
162 @param[in] observer flush observer */
163 UNIV_INLINE
164 void buf_flush_note_modification(buf_block_t *block, lsn_t start_lsn,
165  lsn_t end_lsn, FlushObserver *observer);
166 
167 /** This function should be called when recovery has modified a buffer page.
168 @param[in] block block which is modified
169 @param[in] start_lsn start lsn of the first mtr in a set of mtr's
170 @param[in] end_lsn end lsn of the last mtr in the set of mtr's */
171 UNIV_INLINE
172 void buf_flush_recv_note_modification(buf_block_t *block, lsn_t start_lsn,
173  lsn_t end_lsn);
174 
175 /** Returns TRUE if the file page block is immediately suitable for replacement,
176 i.e., the transition FILE_PAGE => NOT_USED allowed. The caller must hold the
177 LRU list and block mutexes.
178 @param[in] bpage buffer control block, must be buf_page_in_file() and
179  in the LRU list
180 @return true if can replace immediately */
182 
183 #ifdef UNIV_DEBUG
184 struct SYS_VAR;
185 
186 /** Disables page cleaner threads (coordinator and workers).
187 It's used by: SET GLOBAL innodb_page_cleaner_disabled_debug = 1 (0).
188 @param[in] thd thread handle
189 @param[in] var pointer to system variable
190 @param[out] var_ptr where the formal string goes
191 @param[in] save immediate result from check function */
193  void *var_ptr,
194  const void *save);
195 #endif /* UNIV_DEBUG */
196 
197 /** Initialize page_cleaner.
198 @param[in] n_page_cleaners Number of page cleaner threads to create */
199 void buf_flush_page_cleaner_init(size_t n_page_cleaners);
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 */
207 ibool 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. */
213 void buf_flush_init_flush_rbt(void);
214 
215 /** Frees up the red-black tree. */
216 void buf_flush_free_flush_rbt(void);
217 
218 /** Writes a flushable page asynchronously from the buffer pool to a file.
219 NOTE: 1. in simulated aio we must call os_aio_simulated_wake_handler_threads
220 after we have posted a batch of writes! 2. buf_page_get_mutex(bpage) must be
221 held 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
223 returns 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 */
229 ibool 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.
233 @param[in] bpage buffer control block, must be buf_page_in_file()
234 @param[in] flush_type type of flush
235 @return true if can flush immediately */
237  MY_ATTRIBUTE((warn_unused_result));
238 
239 /** Check if there are any dirty pages that belong to a space id in the flush
240  list in a particular buffer pool.
241  @return number of dirty pages present in a single buffer pool */
243  buf_pool_t *buf_pool, /*!< in: buffer pool */
244  space_id_t id, /*!< in: space id to check */
245  FlushObserver *observer); /*!< in: flush observer to check */
246 
247 /** Synchronously flush dirty blocks from the end of the flush list of all
248  buffer pool instances. NOTE: The calling thread is not allowed to own any
249  latches on pages! */
251 
252 /** Request IO burst and wake page_cleaner up.
253 @param[in] lsn_limit upper limit of LSN to be flushed
254 @return true if we requested higher lsn than ever requested so far */
255 bool buf_flush_request_force(lsn_t lsn_limit);
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. */
263 
264 /** We use FlushObserver to track flushing of non-redo logged pages in bulk
265 create index(BtrBulk.cc).Since we disable redo logging during a index build,
266 we need to make sure that all dirty pages modifed by the index build are
267 flushed to disk before any redo logged operations go to the index. */
268 
270  public:
271  /** Constructor
272  @param[in] space_id table space id
273  @param[in] trx trx instance
274  @param[in] stage performance schema accounting object,
275  used by ALTER TABLE. It is passed to log_preflush_pool_modified_pages()
276  for accounting. */
277  FlushObserver(space_id_t space_id, trx_t *trx, ut_stage_alter_t *stage);
278 
279  /** Deconstructor */
280  ~FlushObserver();
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(ulint instance_no) {
287  os_rmb;
288  return (m_flushed->at(instance_no) == m_removed->at(instance_no) ||
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  /** Table space id */
315 
316  /** Trx instance */
318 
319  /** Performance schema accounting object, used by ALTER TABLE.
320  If not NULL, then stage->begin_phase_flush() will be called initially,
321  specifying the number of pages to be attempted to be flushed and
322  subsequently, stage->inc() will be called for each page we attempt to
323  flush. */
325 
326  /* Flush request sent */
327  std::vector<ulint> *m_flushed;
328 
329  /* Flush request finished */
330  std::vector<ulint> *m_removed;
331 
332  /* True if the operation was interrupted. */
334 };
335 
336 #endif /* !UNIV_HOTBACKUP */
337 
338 #include "buf0flu.ic"
339 
340 #endif
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.
uint64_t lsn_t
Type used for all log sequence number storage and arithmetics.
Definition: log0types.h:59
void buf_flush_free_flush_rbt(void)
Frees up the red-black tree.
Definition: buf0flu.cc:372
bool page_is_uncompressed_type(const byte *page)
Check if page type is uncompressed.
Definition: buf0flu.cc:906
bool buf_flush_page_cleaner_is_active()
Checks if the page_cleaner is in active state.
Definition: buf0flu.cc:2604
Definition: plugin.h:62
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:352
Definition: trx0trx.h:780
ulint buf_pool_get_dirty_pages_count(buf_pool_t *buf_pool, space_id_t id, FlushObserver *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:3496
The buffer control block structure.
Definition: buf0buf.h:1318
UNIV_INLINE void buf_flush_note_modification(buf_block_t *block, lsn_t start_lsn, lsn_t end_lsn, FlushObserver *observer)
This function should be called at a mini-transaction commit, if a page was modified in it...
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:930
void buf_flush_remove(buf_page_t *bpage)
Remove a block from the flush list of modified blocks.
Definition: buf0flu.cc:724
void flush()
Flush dirty pages.
Definition: buf0flu.cc:3615
~FlushObserver()
Deconstructor.
Definition: buf0flu.cc:3571
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:2912
ut_stage_alter_t * m_stage
Performance schema accounting object, used by ALTER TABLE.
Definition: buf0flu.h:324
void notify_remove(buf_pool_t *buf_pool, buf_page_t *bpage)
Notify observer of removing a page from flush list.
Definition: buf0flu.cc:3610
ibool buf_flush_validate(buf_pool_t *buf_pool)
Validates the flush list.
Definition: buf0flu.cc:3479
void buf_flush_wait_LRU_batch_end()
Wait for any possible LRU flushes that are in progress to end.
Definition: buf0flu.cc:2268
bool buf_are_flush_lists_empty_validate()
Checks if all flush lists are empty.
Definition: buf0flu.cc:393
trx_t * m_trx
Trx instance.
Definition: buf0flu.h:317
The buffer pool structure.
Definition: buf0buf.h:1708
Redo log constants and functions.
os_event_t buf_flush_event
Event to synchronise with the flushing.
Definition: buf0flu.cc:96
ibool 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:1250
int page
Definition: ctype-mb.cc:1231
FlushObserver(space_id_t space_id, trx_t *trx, ut_stage_alter_t *stage)
Constructor.
Definition: buf0flu.cc:3552
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:2160
bool innodb_page_cleaner_disabled_debug
Value of MySQL global variable used to disable page cleaner.
Definition: buf0flu.cc:194
void buf_flush_sync_all_buf_pools(void)
Synchronously flush dirty blocks from the end of the flush list of all buffer pool instances...
Definition: buf0flu.cc:3365
UNIV_INLINE 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.
The database buffer pool global types for the directory.
void buf_flush_write_complete(buf_page_t *bpage)
Updates the flush system data structures when a write is completed.
Definition: buf0flu.cc:856
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:2059
We use FlushObserver to track flushing of non-redo logged pages in bulk create index(BtrBulk.cc).Since we disable redo logging during a index build, we need to make sure that all dirty pages modifed by the index build are flushed to disk before any redo logged operations go to the index.
Definition: buf0flu.h:269
space_id_t m_space_id
Table space id.
Definition: buf0flu.h:314
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:686
Utilities for byte operations.
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:800
bool m_interrupted
Definition: buf0flu.h:333
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:2098
void buf_flush_wait_batch_end(buf_pool_t *buf_pool, buf_flush_t type)
Waits until a flush batch of the given type ends.
Definition: buf0flu.cc:2021
flush_type
Definition: my_sys.h:287
void notify_flush(buf_pool_t *buf_pool, buf_page_t *bpage)
Notify observer of flushing a page.
Definition: buf0flu.cc:3599
Definition: buf0buf.h:1161
uint32 space_id_t
Tablespace identifier.
Definition: api0api.h:59
void buf_flush_page_cleaner_init(size_t n_page_cleaners)
Initialize page_cleaner.
Definition: buf0flu.cc:2610
std::vector< ulint > * m_flushed
Definition: buf0flu.h:327
int type
Definition: http_common.h:411
Class used to report ALTER TABLE progress via performance_schema.
Definition: ut0stage.h:77
ibool 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:1388
InnoDB condition variable.
Definition: os0event.cc:66
void interrupted()
Interrupt observer not to wait.
Definition: buf0flu.h:293
buf_flush_t
Flags for flush types.
Definition: buf0types.h:65
bool buf_flush_request_force(lsn_t lsn_limit)
Request IO burst and wake page_cleaner up.
Definition: buf0flu.cc:3387
ibool buf_flush_ready_for_replace(buf_page_t *bpage)
Returns TRUE if the file page block is immediately suitable for replacement, i.e., the transition FILE_PAGE => NOT_USED allowed.
Definition: buf0flu.cc:663
bool check_interrupted()
Check whether trx is interrupted.
Definition: buf0flu.cc:3586
bool is_complete(ulint instance_no)
Check pages have been flushed and removed from the flush list in a buffer pool instance.
Definition: buf0flu.h:286
#define os_rmb
barrier definitions for memory ordering
Definition: os0atomic.h:340
unsigned char byte
Blob class.
Definition: common.h:159
std::vector< ulint > * m_removed
Definition: buf0flu.h:330
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_class.h:778