MySQL  8.0.20
Source Code Documentation
buf0lru.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 #include <sys/types.h>
28 
29 /** @file include/buf0lru.h
30  The database buffer pool LRU replacement algorithm
31 
32  Created 11/5/1995 Heikki Tuuri
33  *******************************************************/
34 
35 #ifndef buf0lru_h
36 #define buf0lru_h
37 
38 #include "buf0types.h"
39 #include "univ.i"
40 #ifndef UNIV_HOTBACKUP
41 #include "ut0byte.h"
42 
43 // Forward declaration
44 struct trx_t;
45 
46 /** Returns TRUE if less than 25 % of the buffer pool is available. This can be
47  used in heuristics to prevent huge transactions eating up the whole buffer
48  pool for their locks.
49  @return true if less than 25 % of buffer pool left */
51 
52 /*#######################################################################
53 These are low-level functions
54 #########################################################################*/
55 
56 /** Minimum LRU list length for which the LRU_old pointer is defined */
57 #define BUF_LRU_OLD_MIN_LEN 512 /* 8 megabytes of 16k pages */
58 #endif /* !UNIV_HOTBACKUP */
59 
60 /** Flushes all dirty pages or removes all pages belonging
61  to a given tablespace. A PROBLEM: if readahead is being started, what
62  guarantees that it will not try to read in pages after this operation
63  has completed? */
65  space_id_t id, /*!< in: space id */
66  buf_remove_t buf_remove, /*!< in: remove or flush strategy */
67  const trx_t *trx, /*!< to check if the operation must
68  be interrupted */
69  bool strict = true); /*!< in: true, if no page from tablespace
70  can be in buffer pool just after flush */
71 
72 #ifndef UNIV_HOTBACKUP
73 #if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
74 /** Insert a compressed block into buf_pool->zip_clean in the LRU order.
75 @param[in] bpage pointer to the block in question */
77 #endif /* UNIV_DEBUG || UNIV_BUF_DEBUG */
78 
79 /** Try to free a block. If bpage is a descriptor of a compressed-only
80 page, the descriptor object will be freed as well.
81 NOTE: this function may temporarily release and relock the
82 buf_page_get_get_mutex(). Furthermore, the page frame will no longer be
83 accessible via bpage. If this function returns true, it will also release
84 the LRU list mutex.
85 The caller must hold the LRU list and buf_page_get_mutex() mutexes.
86 @param[in] bpage block to be freed
87 @param[in] zip true if should remove also the compressed page of
88  an uncompressed page
89 @return true if freed, false otherwise. */
90 bool buf_LRU_free_page(buf_page_t *bpage, bool zip)
91  MY_ATTRIBUTE((warn_unused_result));
92 
93 /** Try to free a replaceable block.
94 @param[in,out] buf_pool buffer pool instance
95 @param[in] scan_all scan whole LRU list if ture, otherwise scan
96  only BUF_LRU_SEARCH_SCAN_THRESHOLD blocks
97 @return true if found and freed */
98 bool buf_LRU_scan_and_free_block(buf_pool_t *buf_pool, bool scan_all)
99  MY_ATTRIBUTE((warn_unused_result));
100 
101 /** Returns a free block from the buf_pool. The block is taken off the
102 free list. If it is empty, returns NULL.
103 @param[in] buf_pool buffer pool instance
104 @return a free control block, or NULL if the buf_block->free list is empty */
106 
107 /** Returns a free block from the buf_pool. The block is taken off the
108 free list. If free list is empty, blocks are moved from the end of the
109 LRU list to the free list.
110 This function is called from a user thread when it needs a clean
111 block to read in a page. Note that we only ever get a block from
112 the free list. Even when we flush a page or find a page in LRU scan
113 we put it to free list to be used.
114 * iteration 0:
115  * get a block from free list, success:done
116  * if buf_pool->try_LRU_scan is set
117  * scan LRU up to srv_LRU_scan_depth to find a clean block
118  * the above will put the block on free list
119  * success:retry the free list
120  * flush one dirty page from tail of LRU to disk
121  * the above will put the block on free list
122  * success: retry the free list
123 * iteration 1:
124  * same as iteration 0 except:
125  * scan whole LRU list
126  * scan LRU list even if buf_pool->try_LRU_scan is not set
127 * iteration > 1:
128  * same as iteration 1 but sleep 10ms
129 @param[in,out] buf_pool buffer pool instance
130 @return the free control block, in state BUF_BLOCK_READY_FOR_USE */
132  MY_ATTRIBUTE((warn_unused_result));
133 
134 /** Determines if the unzip_LRU list should be used for evicting a victim
135 instead of the general LRU list.
136 @param[in,out] buf_pool buffer pool instance
137 @return true if should use unzip_LRU */
139 
140 /** Puts a block back to the free list.
141 @param[in] block block must not contain a file page */
143 
144 /** Adds a block to the LRU list. Please make sure that the page_size is
145  already set when invoking the function, so that we can get correct
146  page_size from the buffer page when adding a block into LRU */
147 void buf_LRU_add_block(buf_page_t *bpage, /*!< in: control block */
148  ibool old); /*!< in: TRUE if should be put to the old
149  blocks in the LRU list, else put to the
150  start; if the LRU list is very short, added
151  to the start regardless of this parameter */
152 
153 /** Adds a block to the LRU list of decompressed zip pages.
154 @param[in] block control block
155 @param[in] old TRUE if should be put to the end of the list,
156  else put to the start */
157 void buf_unzip_LRU_add_block(buf_block_t *block, ibool old);
158 
159 /** Moves a block to the start of the LRU list.
160 @param[in] bpage control block */
162 
163 /** Updates buf_pool->LRU_old_ratio.
164  @return updated old_pct */
166  uint old_pct, /*!< in: Reserve this percentage of
167  the buffer pool for "old" blocks. */
168  ibool adjust); /*!< in: TRUE=adjust the LRU list;
169  FALSE=just assign buf_pool->LRU_old_ratio
170  during the initialization of InnoDB */
171 /** Update the historical stats that we are collecting for LRU eviction
172  policy at the end of each interval. */
173 void buf_LRU_stat_update(void);
174 
175 /** Remove one page from LRU list and put it to free list. The caller must hold
176 the LRU list and block mutexes and have page hash latched in X. The latch and
177 the block mutexes will be released.
178 @param[in,out] bpage block, must contain a file page and
179  be in a state where it can be freed; there
180  may or may not be a hash index to the page
181 @param[in] zip true if should remove also the compressed page
182  of an uncompressed page
183 @param[in] ignore_content true if should ignore page content, since it
184  could be not initialized */
185 void buf_LRU_free_one_page(buf_page_t *bpage, bool zip, bool ignore_content);
186 
187 /** Adjust LRU hazard pointers if needed. */
188 void buf_LRU_adjust_hp(buf_pool_t *buf_pool, /*!< in: buffer pool instance */
189  const buf_page_t *bpage); /*!< in: control block */
190 
191 #if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
192 /** Validates the LRU list.
193  @return true */
194 ibool buf_LRU_validate(void);
195 #endif /* UNIV_DEBUG || UNIV_BUF_DEBUG */
196 #if defined UNIV_DEBUG_PRINT || defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
197 /** Prints the LRU list. */
198 void buf_LRU_print(void);
199 #endif /* UNIV_DEBUG_PRINT || UNIV_DEBUG || UNIV_BUF_DEBUG */
200 
201 /** @name Heuristics for detecting index scan @{ */
202 /** The denominator of buf_pool->LRU_old_ratio. */
203 #define BUF_LRU_OLD_RATIO_DIV 1024
204 /** Maximum value of buf_pool->LRU_old_ratio.
205 @see buf_LRU_old_adjust_len
206 @see buf_pool->LRU_old_ratio_update */
207 #define BUF_LRU_OLD_RATIO_MAX BUF_LRU_OLD_RATIO_DIV
208 /** Minimum value of buf_pool->LRU_old_ratio.
209 @see buf_LRU_old_adjust_len
210 @see buf_pool->LRU_old_ratio_update
211 The minimum must exceed
212 (BUF_LRU_OLD_TOLERANCE + 5) * BUF_LRU_OLD_RATIO_DIV / BUF_LRU_OLD_MIN_LEN. */
213 #define BUF_LRU_OLD_RATIO_MIN 51
214 
215 #if BUF_LRU_OLD_RATIO_MIN >= BUF_LRU_OLD_RATIO_MAX
216 #error "BUF_LRU_OLD_RATIO_MIN >= BUF_LRU_OLD_RATIO_MAX"
217 #endif
218 #if BUF_LRU_OLD_RATIO_MAX > BUF_LRU_OLD_RATIO_DIV
219 #error "BUF_LRU_OLD_RATIO_MAX > BUF_LRU_OLD_RATIO_DIV"
220 #endif
221 
222 /** Move blocks to "new" LRU list only if the first access was at
223 least this many milliseconds ago. Not protected by any mutex or latch. */
225 /* @} */
226 
227 /** @brief Statistics for selecting the LRU list for eviction.
228 
229 These statistics are not 'of' LRU but 'for' LRU. We keep count of I/O
230 and page_zip_decompress() operations. Based on the statistics we decide
231 if we want to evict from buf_pool->unzip_LRU or buf_pool->LRU. */
233  ulint io; /**< Counter of buffer pool I/O operations. */
234  ulint unzip; /**< Counter of page_zip_decompress operations. */
235 };
236 
237 /** Current operation counters. Not protected by any mutex.
238 Cleared by buf_LRU_stat_update(). */
240 
241 /** Running sum of past values of buf_LRU_stat_cur.
242 Updated by buf_LRU_stat_update(). Accesses protected by memory barriers. */
244 
245 /** Increments the I/O counter in buf_LRU_stat_cur. */
246 #define buf_LRU_stat_inc_io() buf_LRU_stat_cur.io++
247 /** Increments the page_zip_decompress() counter in buf_LRU_stat_cur. */
248 #define buf_LRU_stat_inc_unzip() buf_LRU_stat_cur.unzip++
249 
250 #endif /* !UNIV_HOTBACKUP */
251 
252 #endif
void buf_LRU_stat_update(void)
Update the historical stats that we are collecting for LRU eviction policy at the end of each interva...
Definition: buf0lru.cc:2321
buf_remove_t
Algorithm to remove the pages for a tablespace from the buffer pool.
Definition: buf0types.h:76
void buf_LRU_print(void)
Prints the LRU list.
Definition: buf0lru.cc:2527
void buf_LRU_make_block_young(buf_page_t *bpage)
Moves a block to the start of the LRU list.
Definition: buf0lru.cc:1633
Definition: trx0trx.h:800
void buf_LRU_free_one_page(buf_page_t *bpage, bool zip, bool ignore_content)
Remove one page from LRU list and put it to free list.
Definition: buf0lru.cc:2236
The buffer control block structure.
Definition: buf0buf.h:1321
buf_block_t * buf_LRU_get_free_only(buf_pool_t *buf_pool)
Returns a free block from the buf_pool.
Definition: buf0lru.cc:1101
uint buf_LRU_old_threshold_ms
Move blocks to "new" LRU list only if the first access was at least this many milliseconds ago...
Definition: buf0lru.cc:131
void buf_LRU_adjust_hp(buf_pool_t *buf_pool, const buf_page_t *bpage)
Adjust LRU hazard pointers if needed.
Definition: buf0lru.cc:1453
The buffer pool structure.
Definition: buf0buf.h:1760
buf_block_t * buf_LRU_get_free_block(buf_pool_t *buf_pool)
Returns a free block from the buf_pool.
Definition: buf0lru.cc:1228
buf_LRU_stat_t buf_LRU_stat_sum
Running sum of past values of buf_LRU_stat_cur.
Definition: buf0lru.cc:124
uint buf_LRU_old_ratio_update(uint old_pct, ibool adjust)
Updates buf_pool->LRU_old_ratio.
Definition: buf0lru.cc:2299
ulint unzip
Counter of page_zip_decompress operations.
Definition: buf0lru.h:234
The database buffer pool global types for the directory.
ibool buf_LRU_evict_from_unzip_LRU(buf_pool_t *buf_pool)
Determines if the unzip_LRU list should be used for evicting a victim instead of the general LRU list...
Definition: buf0lru.cc:179
unsigned int uint
Definition: uca-dump.cc:29
void buf_LRU_block_free_non_file_page(buf_block_t *block)
Puts a block back to the free list.
Definition: buf0lru.cc:1904
Utilities for byte operations.
buf_LRU_stat_t buf_LRU_stat_cur
Current operation counters.
Definition: buf0lru.cc:120
void buf_LRU_flush_or_remove_pages(space_id_t id, buf_remove_t buf_remove, const trx_t *trx, bool strict=true)
Flushes all dirty pages or removes all pages belonging to a given tablespace.
Definition: buf0lru.cc:875
void buf_LRU_add_block(buf_page_t *bpage, ibool old)
Adds a block to the LRU list.
Definition: buf0lru.cc:1621
Definition: buf0buf.h:1161
uint32 space_id_t
Tablespace identifier.
Definition: api0api.h:59
bool buf_LRU_free_page(buf_page_t *bpage, bool zip)
Try to free a block.
Definition: buf0lru.cc:1657
ulint io
Counter of buffer pool I/O operations.
Definition: buf0lru.h:233
ibool buf_LRU_buf_pool_running_out(void)
Returns TRUE if less than 25 % of the buffer pool is available.
Definition: buf0lru.cc:1079
void buf_unzip_LRU_add_block(buf_block_t *block, ibool old)
Adds a block to the LRU list of decompressed zip pages.
Definition: buf0lru.cc:1537
ibool buf_LRU_validate(void)
Validates the LRU list.
Definition: buf0lru.cc:2456
bool buf_LRU_scan_and_free_block(buf_pool_t *buf_pool, bool scan_all)
Try to free a replaceable block.
Definition: buf0lru.cc:1054
Statistics for selecting the LRU list for eviction.
Definition: buf0lru.h:232
void buf_LRU_insert_zip_clean(buf_page_t *bpage)
Insert a compressed block into buf_pool->zip_clean in the LRU order.
Definition: buf0lru.cc:919