MySQL 8.2.0
Source Code Documentation
buf0lru.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#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
44struct 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/*#######################################################################
53These are low-level functions
54#########################################################################*/
55
56/** Minimum LRU list length for which the LRU_old pointer is defined
578 megabytes of 16k pages */
58constexpr uint32_t BUF_LRU_OLD_MIN_LEN = 8 * 1024 / 16;
59#endif /* !UNIV_HOTBACKUP */
60
61/** Flushes all dirty pages or removes all pages belonging to a given
62tablespace. A PROBLEM: if readahead is being started, what guarantees
63that it will not try to read in pages after this operation has completed?
64@param[in] id tablespace ID
65@param[in] buf_remove remove or flush strategy
66@param[in] trx to check if the operation must be interrupted
67@param[in] strict true, if no page from tablespace can be in
68 buffer pool just after flush */
70 const trx_t *trx, bool strict = true);
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
80page, the descriptor object will be freed as well.
81NOTE: this function may temporarily release and relock the
82buf_page_get_mutex(). Furthermore, the page frame will no longer be
83accessible via bpage. If this function returns true, it will also release
84the LRU list mutex.
85The 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[[nodiscard]] bool buf_LRU_free_page(buf_page_t *bpage, bool zip);
91
92/** Try to free a replaceable block.
93@param[in,out] buf_pool buffer pool instance
94@param[in] scan_all scan whole LRU list if true, otherwise scan
95 only BUF_LRU_SEARCH_SCAN_THRESHOLD blocks
96@return true if found and freed */
97[[nodiscard]] bool buf_LRU_scan_and_free_block(buf_pool_t *buf_pool,
98 bool scan_all);
99
100/** Returns a free block from the buf_pool. The block is taken off the
101free list. If it is empty, returns NULL.
102@param[in] buf_pool buffer pool instance
103@return a free control block, or NULL if the buf_block->free list is empty */
105
106/** Returns a free block from the buf_pool. The block is taken off the
107free list. If free list is empty, blocks are moved from the end of the
108LRU list to the free list.
109This function is called from a user thread when it needs a clean
110block to read in a page. Note that we only ever get a block from
111the free list. Even when we flush a page or find a page in LRU scan
112we put it to free list to be used.
113* iteration 0:
114 * get a block from free list, success:done
115 * if buf_pool->try_LRU_scan is set
116 * scan LRU up to srv_LRU_scan_depth to find a clean block
117 * the above will put the block on free list
118 * success:retry the free list
119 * flush one dirty page from tail of LRU to disk
120 * the above will put the block on free list
121 * success: retry the free list
122* iteration 1:
123 * same as iteration 0 except:
124 * scan whole LRU list
125 * scan LRU list even if buf_pool->try_LRU_scan is not set
126* iteration > 1:
127 * same as iteration 1 but sleep 10ms
128@param[in,out] buf_pool buffer pool instance
129@return the free control block, in state BUF_BLOCK_READY_FOR_USE */
130[[nodiscard]] buf_block_t *buf_LRU_get_free_block(buf_pool_t *buf_pool);
131
132/** Determines if the unzip_LRU list should be used for evicting a victim
133instead of the general LRU list.
134@param[in,out] buf_pool buffer pool instance
135@return true if should use unzip_LRU */
137
138/** Puts a block back to the free list.
139@param[in] block block must not contain a file page */
141
142/** Adds a block to the LRU list. Please make sure that the page_size is
143 already set when invoking the function, so that we can get correct
144 page_size from the buffer page when adding a block into LRU */
145void buf_LRU_add_block(buf_page_t *bpage, /*!< in: control block */
146 bool old); /*!< in: true if should be put to the old
147 blocks in the LRU list, else put to the
148 start; if the LRU list is very short, added
149 to the start regardless of this parameter */
150
151/** Adds a block to the LRU list of decompressed zip pages.
152@param[in] block control block
153@param[in] old true if should be put to the end of the list,
154 else put to the start */
155void buf_unzip_LRU_add_block(buf_block_t *block, bool old);
156
157/** Moves a block to the start of the LRU list.
158@param[in] bpage control block */
160
161/** Moves a block to the end of the LRU list.
162@param[in] bpage control block */
164
165/** Updates buf_pool->LRU_old_ratio.
166 @return updated old_pct */
168 uint old_pct, /*!< in: Reserve this percentage of
169 the buffer pool for "old" blocks. */
170 bool adjust); /*!< in: true=adjust the LRU list;
171 false=just assign buf_pool->LRU_old_ratio
172 during the initialization of InnoDB */
173/** Update the historical stats that we are collecting for LRU eviction
174 policy at the end of each interval. */
175void buf_LRU_stat_update(void);
176
177/** Remove one page from LRU list and put it to free list. The caller must hold
178the LRU list and block mutexes and have page hash latched in X. The latch and
179the block mutexes will be released.
180@param[in,out] bpage block, must contain a file page and
181 be in a state where it can be freed; there
182 may or may not be a hash index to the page
183@param[in] ignore_content true if should ignore page content, since it
184 could be not initialized */
185void buf_LRU_free_one_page(buf_page_t *bpage, bool ignore_content);
186
187/** Adjust LRU hazard pointers if needed.
188@param[in] buf_pool Buffer pool instance
189@param[in] bpage Control block */
190void buf_LRU_adjust_hp(buf_pool_t *buf_pool, const buf_page_t *bpage);
191
192#if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
193/** Validates the LRU list. */
194void buf_LRU_validate(void);
195
196/** Validates the LRU list for one buffer pool instance.
197@param[in] buf_pool buffer pool instance */
199
200using Space_References = std::map<struct fil_space_t *, size_t>;
201
202/** Counts number of pages that are still in the LRU for each space instance
203encountered.
204@returns map of space instances into count of pages in LRU. */
206
207#endif /* UNIV_DEBUG || UNIV_BUF_DEBUG */
208#if defined UNIV_DEBUG_PRINT || defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
209/** Prints the LRU list. */
210void buf_LRU_print(void);
211#endif /* UNIV_DEBUG_PRINT || UNIV_DEBUG || UNIV_BUF_DEBUG */
212
213/** @name Heuristics for detecting index scan
214@{ */
215/** The denominator of buf_pool->LRU_old_ratio. */
216constexpr uint32_t BUF_LRU_OLD_RATIO_DIV = 1024;
217/** Maximum value of buf_pool->LRU_old_ratio.
218@see buf_LRU_old_adjust_len
219@see buf_pool->LRU_old_ratio_update */
221/** Minimum value of buf_pool->LRU_old_ratio.
222@see buf_LRU_old_adjust_len
223@see buf_pool->LRU_old_ratio_update
224The minimum must exceed
225(BUF_LRU_OLD_TOLERANCE + 5) * BUF_LRU_OLD_RATIO_DIV / BUF_LRU_OLD_MIN_LEN. */
226constexpr uint32_t BUF_LRU_OLD_RATIO_MIN = 51;
227
229 "BUF_LRU_OLD_RATIO_MIN >= BUF_LRU_OLD_RATIO_MAX");
231 "BUF_LRU_OLD_RATIO_MAX > BUF_LRU_OLD_RATIO_DIV");
232
233/** Move blocks to "new" LRU list only if the first access was at
234least this many milliseconds ago. Not protected by any mutex or latch. */
235std::chrono::milliseconds get_buf_LRU_old_threshold();
236/** @} */
237
238/** @brief Statistics for selecting the LRU list for eviction.
239
240These statistics are not 'of' LRU but 'for' LRU. We keep count of I/O
241and page_zip_decompress() operations. Based on the statistics we decide
242if we want to evict from buf_pool->unzip_LRU or buf_pool->LRU. */
244 ulint io; /**< Counter of buffer pool I/O operations. */
245 ulint unzip; /**< Counter of page_zip_decompress operations. */
246};
247
248/** Current operation counters. Not protected by any mutex.
249Cleared by buf_LRU_stat_update(). */
251
252/** Running sum of past values of buf_LRU_stat_cur.
253Updated by buf_LRU_stat_update(). Accesses protected by memory barriers. */
255
256/** Increments the I/O counter in buf_LRU_stat_cur. */
258/** Increments the page_zip_decompress() counter in buf_LRU_stat_cur. */
260
261#endif /* !UNIV_HOTBACKUP */
262
263#endif
uint32_t space_id_t
Tablespace identifier.
Definition: api0api.h:50
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:973
void buf_unzip_LRU_add_block(buf_block_t *block, bool old)
Adds a block to the LRU list of decompressed zip pages.
Definition: buf0lru.cc:1621
void buf_LRU_stat_inc_unzip()
Increments the page_zip_decompress() counter in buf_LRU_stat_cur.
Definition: buf0lru.h:259
bool 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:184
void buf_LRU_make_block_old(buf_page_t *bpage)
Moves a block to the end of the LRU list.
Definition: buf0lru.cc:1731
void buf_LRU_add_block(buf_page_t *bpage, bool old)
Adds a block to the LRU list.
Definition: buf0lru.cc:1704
void buf_LRU_make_block_young(buf_page_t *bpage)
Moves a block to the start of the LRU list.
Definition: buf0lru.cc:1716
uint buf_LRU_old_ratio_update(uint old_pct, bool adjust)
Updates buf_pool->LRU_old_ratio.
Definition: buf0lru.cc:2372
constexpr uint32_t BUF_LRU_OLD_RATIO_MIN
Minimum value of buf_pool->LRU_old_ratio.
Definition: buf0lru.h:226
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:1011
void buf_LRU_adjust_hp(buf_pool_t *buf_pool, const buf_page_t *bpage)
Adjust LRU hazard pointers if needed.
Definition: buf0lru.cc:1541
void buf_LRU_validate_instance(buf_pool_t *buf_pool)
Validates the LRU list for one buffer pool instance.
Definition: buf0lru.cc:2443
void buf_LRU_block_free_non_file_page(buf_block_t *block)
Puts a block back to the free list.
Definition: buf0lru.cc:1991
buf_LRU_stat_t buf_LRU_stat_sum
Running sum of past values of buf_LRU_stat_cur.
Definition: buf0lru.cc:123
bool buf_LRU_buf_pool_running_out(void)
Returns true if less than 25 % of the buffer pool is available.
Definition: buf0lru.cc:1182
void buf_LRU_validate(void)
Validates the LRU list.
Definition: buf0lru.cc:2518
constexpr uint32_t BUF_LRU_OLD_MIN_LEN
Minimum LRU list length for which the LRU_old pointer is defined 8 megabytes of 16k pages.
Definition: buf0lru.h:58
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:2394
std::chrono::milliseconds get_buf_LRU_old_threshold()
Move blocks to "new" LRU list only if the first access was at least this many milliseconds ago.
Definition: buf0lru.cc:132
buf_block_t * buf_LRU_get_free_block(buf_pool_t *buf_pool)
Returns a free block from the buf_pool.
Definition: buf0lru.cc:1310
buf_LRU_stat_t buf_LRU_stat_cur
Current operation counters.
Definition: buf0lru.cc:119
bool buf_LRU_scan_and_free_block(buf_pool_t *buf_pool, bool scan_all)
Try to free a replaceable block.
Definition: buf0lru.cc:1155
buf_block_t * buf_LRU_get_free_only(buf_pool_t *buf_pool)
Returns a free block from the buf_pool.
Definition: buf0lru.cc:1204
constexpr uint32_t BUF_LRU_OLD_RATIO_DIV
The denominator of buf_pool->LRU_old_ratio.
Definition: buf0lru.h:216
bool buf_LRU_free_page(buf_page_t *bpage, bool zip)
Try to free a block.
Definition: buf0lru.cc:1740
constexpr uint32_t BUF_LRU_OLD_RATIO_MAX
Maximum value of buf_pool->LRU_old_ratio.
Definition: buf0lru.h:220
void buf_LRU_print(void)
Prints the LRU list.
Definition: buf0lru.cc:2615
Space_References buf_LRU_count_space_references()
Counts number of pages that are still in the LRU for each space instance encountered.
Definition: buf0lru.cc:2525
void buf_LRU_stat_inc_io()
Increments the I/O counter in buf_LRU_stat_cur.
Definition: buf0lru.h:257
void buf_LRU_free_one_page(buf_page_t *bpage, bool ignore_content)
Remove one page from LRU list and put it to free list.
Definition: buf0lru.cc:2309
std::map< struct fil_space_t *, size_t > Space_References
Definition: buf0lru.h:200
The database buffer pool global types for the directory.
buf_remove_t
Algorithm to remove the pages for a tablespace from the buffer pool.
Definition: buf0types.h:83
Definition: buf0buf.h:1152
Statistics for selecting the LRU list for eviction.
Definition: buf0lru.h:243
ulint unzip
Counter of page_zip_decompress operations.
Definition: buf0lru.h:245
ulint io
Counter of buffer pool I/O operations.
Definition: buf0lru.h:244
The buffer control block structure.
Definition: buf0buf.h:1750
The buffer pool structure.
Definition: buf0buf.h:2278
Definition: trx0trx.h:683
Version control for database, common definitions, and include files.
unsigned long int ulint
Definition: univ.i:405
Utilities for byte operations.