MySQL 26.7.0
Source Code Documentation
buf0lru.h
Go to the documentation of this file.
1/*****************************************************************************
2
3Copyright (c) 1995, 2026, 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 designed to work with certain software (including
10but not limited to OpenSSL) that is licensed under separate terms,
11as designated in a particular file or component or in included license
12documentation. The authors of MySQL hereby grant you an additional
13permission to link the program and your derivative works with the
14separately licensed software that they have either included with
15the program or referenced in the documentation.
16
17This program is distributed in the hope that it will be useful, but WITHOUT
18ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
20for more details.
21
22You should have received a copy of the GNU General Public License along with
23this program; if not, write to the Free Software Foundation, Inc.,
2451 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
26*****************************************************************************/
27
28#include <sys/types.h>
29
30/** @file include/buf0lru.h
31 The database buffer pool LRU replacement algorithm
32
33 Created 11/5/1995 Heikki Tuuri
34 *******************************************************/
35
36#ifndef buf0lru_h
37#define buf0lru_h
38
39#include "buf0types.h"
40#include "univ.i"
41#ifndef UNIV_HOTBACKUP
42#include "ut0byte.h"
43
44// Forward declaration
45struct trx_t;
46
47/** Returns true if less than 25 % of the buffer pool is available. This can be
48 used in heuristics to prevent huge transactions eating up the whole buffer
49 pool for their locks.
50 @return true if less than 25 % of buffer pool left */
52
53/*#######################################################################
54These are low-level functions
55#########################################################################*/
56
57/** Minimum LRU list length for which the LRU_old pointer is defined
588 megabytes of 16k pages */
59constexpr uint32_t BUF_LRU_OLD_MIN_LEN = 8 * 1024 / 16;
60#endif /* !UNIV_HOTBACKUP */
61
62#ifndef UNIV_HOTBACKUP
63#if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
64/** Insert a compressed block into buf_pool->zip_clean in the LRU order.
65@param[in] bpage pointer to the block in question */
67#endif /* UNIV_DEBUG || UNIV_BUF_DEBUG */
68
69/** Try to free a block. If bpage is a descriptor of a compressed-only
70page, the descriptor object will be freed as well.
71NOTE: this function may temporarily release and relock the
72buf_page_get_mutex(). Furthermore, the page frame will no longer be
73accessible via bpage. If this function returns true, it will also release
74the LRU list mutex.
75The caller must hold the LRU list and buf_page_get_mutex() mutexes.
76@param[in] bpage block to be freed
77@param[in] zip true if should remove also the compressed page of
78 an uncompressed page
79@return true if freed, false otherwise. */
80[[nodiscard]] bool buf_LRU_free_page(buf_page_t *bpage, bool zip);
81
82/** Try to free a replaceable block.
83@param[in,out] buf_pool buffer pool instance
84@param[in] scan_all scan whole LRU list if true, otherwise scan
85 only BUF_LRU_SEARCH_SCAN_THRESHOLD blocks
86@return true if found and freed */
87[[nodiscard]] bool buf_LRU_scan_and_free_block(buf_pool_t *buf_pool,
88 bool scan_all);
89
90/** Returns a free block from the buf_pool. The block is taken off the
91free list. If it is empty, returns NULL.
92@param[in] buf_pool buffer pool instance
93@return a free control block, or NULL if the buf_block->free list is empty */
95
96/** Returns a free block from the buf_pool. The block is taken off the
97free list. If free list is empty, blocks are moved from the end of the
98LRU list to the free list.
99This function is called from a user thread when it needs a clean
100block to read in a page. Note that we only ever get a block from
101the free list. Even when we flush a page or find a page in LRU scan
102we put it to free list to be used.
103* iteration 0:
104 * get a block from free list, success:done
105 * if buf_pool->try_LRU_scan is set
106 * scan LRU up to srv_LRU_scan_depth to find a clean block
107 * the above will put the block on free list
108 * success:retry the free list
109 * flush one dirty page from tail of LRU to disk
110 * the above will put the block on free list
111 * success: retry the free list
112* iteration 1:
113 * same as iteration 0 except:
114 * scan whole LRU list
115 * scan LRU list even if buf_pool->try_LRU_scan is not set
116* iteration > 1:
117 * same as iteration 1 but sleep 10ms
118@param[in,out] buf_pool buffer pool instance
119@return the free control block, in state BUF_BLOCK_READY_FOR_USE */
120[[nodiscard]] buf_block_t *buf_LRU_get_free_block(buf_pool_t *buf_pool);
121
122/** Determines if the unzip_LRU list should be used for evicting a victim
123instead of the general LRU list.
124@param[in,out] buf_pool buffer pool instance
125@return true if should use unzip_LRU */
127
128/** Puts a block back to the free list.
129@param[in] block block must not contain a file page */
131
132/** Adds a block to the LRU list. Please make sure that the page_size is
133 already set when invoking the function, so that we can get correct
134 page_size from the buffer page when adding a block into LRU */
135void buf_LRU_add_block(buf_page_t *bpage, /*!< in: control block */
136 bool old); /*!< in: true if should be put to the old
137 blocks in the LRU list, else put to the
138 start; if the LRU list is very short, added
139 to the start regardless of this parameter */
140
141/** Adds a block to the LRU list of decompressed zip pages.
142@param[in] block control block
143@param[in] old true if should be put to the end of the list,
144 else put to the start */
145void buf_unzip_LRU_add_block(buf_block_t *block, bool old);
146
147/** Moves a block to the start of the LRU list.
148@param[in] bpage control block */
150
151/** Moves a block to the end of the LRU list.
152@param[in] bpage control block */
154
155/** Updates buf_pool->LRU_old_ratio.
156 @return updated old_pct */
158 uint old_pct, /*!< in: Reserve this percentage of
159 the buffer pool for "old" blocks. */
160 bool adjust); /*!< in: true=adjust the LRU list;
161 false=just assign buf_pool->LRU_old_ratio
162 during the initialization of InnoDB */
163/** Update the historical stats that we are collecting for LRU eviction
164 policy at the end of each interval. */
165void buf_LRU_stat_update(void);
166
167/** Remove one page from LRU list and put it to free list. The caller must hold
168the LRU list and block mutexes and have page hash latched in X. The latch and
169the block mutexes will be released.
170@param[in,out] bpage block, must contain a file page and
171 be in a state where it can be freed; there
172 may or may not be a hash index to the page
173@param[in] ignore_content true if should ignore page content, since it
174 could be not initialized */
175void buf_LRU_free_one_page(buf_page_t *bpage, bool ignore_content);
176
177/** Adjust LRU hazard pointers if needed.
178@param[in] buf_pool Buffer pool instance
179@param[in] bpage Control block */
180void buf_LRU_adjust_hp(buf_pool_t *buf_pool, const buf_page_t *bpage);
181
182#if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
183/** Validates the LRU list. */
184void buf_LRU_validate(void);
185
186/** Validates the LRU list for one buffer pool instance.
187@param[in] buf_pool buffer pool instance */
189
190using Space_References = std::map<class fil_space_t *, size_t>;
191
192/** Counts number of pages that are still in the LRU for each space instance
193encountered.
194@returns map of space instances into count of pages in LRU. */
196
197#endif /* UNIV_DEBUG || UNIV_BUF_DEBUG */
198#if defined UNIV_DEBUG_PRINT || defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
199/** Prints the LRU list. */
200void buf_LRU_print(void);
201#endif /* UNIV_DEBUG_PRINT || UNIV_DEBUG || UNIV_BUF_DEBUG */
202
203/** @name Heuristics for detecting index scan
204@{ */
205/** The denominator of buf_pool->LRU_old_ratio. */
206constexpr uint32_t BUF_LRU_OLD_RATIO_DIV = 1024;
207/** Maximum value of buf_pool->LRU_old_ratio.
208@see buf_LRU_old_adjust_len
209@see buf_pool->LRU_old_ratio_update */
211/** Minimum value of buf_pool->LRU_old_ratio.
212@see buf_LRU_old_adjust_len
213@see buf_pool->LRU_old_ratio_update
214The minimum must exceed
215(BUF_LRU_OLD_TOLERANCE + 5) * BUF_LRU_OLD_RATIO_DIV / BUF_LRU_OLD_MIN_LEN. */
216constexpr uint32_t BUF_LRU_OLD_RATIO_MIN = 51;
217
219 "BUF_LRU_OLD_RATIO_MIN >= BUF_LRU_OLD_RATIO_MAX");
221 "BUF_LRU_OLD_RATIO_MAX > BUF_LRU_OLD_RATIO_DIV");
222
223/** Move blocks to "new" LRU list only if the first access was at
224least this many milliseconds ago. Not protected by any mutex or latch. */
226/** @} */
227
228/** @brief Statistics for selecting the LRU list for eviction.
229
230These statistics are not 'of' LRU but 'for' LRU. We keep count of I/O
231and page_zip_decompress() operations. Based on the statistics we decide
232if we want to evict from buf_pool->unzip_LRU or buf_pool->LRU. */
234 ulint io; /**< Counter of buffer pool I/O operations. */
235 ulint unzip; /**< Counter of page_zip_decompress operations. */
236};
237
238/** Current operation counters. Not protected by any mutex.
239Cleared by buf_LRU_stat_update(). */
241
242/** Running sum of past values of buf_LRU_stat_cur.
243Updated by buf_LRU_stat_update(). Accesses protected by memory barriers. */
245
246/** Increments the I/O counter in buf_LRU_stat_cur. */
248/** Increments the page_zip_decompress() counter in buf_LRU_stat_cur. */
250
251#endif /* !UNIV_HOTBACKUP */
252
253#endif
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:837
void buf_LRU_stat_inc_unzip()
Increments the page_zip_decompress() counter in buf_LRU_stat_cur.
Definition: buf0lru.h:249
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:178
std::map< class fil_space_t *, size_t > Space_References
Definition: buf0lru.h:190
void buf_LRU_make_block_old(buf_page_t *bpage)
Moves a block to the end of the LRU list.
Definition: buf0lru.cc:947
void buf_LRU_add_block(buf_page_t *bpage, bool old)
Adds a block to the LRU list.
Definition: buf0lru.cc:920
void buf_LRU_make_block_young(buf_page_t *bpage)
Moves a block to the start of the LRU list.
Definition: buf0lru.cc:932
uint buf_LRU_old_ratio_update(uint old_pct, bool adjust)
Updates buf_pool->LRU_old_ratio.
Definition: buf0lru.cc:1595
constexpr uint32_t BUF_LRU_OLD_RATIO_MIN
Minimum value of buf_pool->LRU_old_ratio.
Definition: buf0lru.h:216
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:218
void buf_LRU_adjust_hp(buf_pool_t *buf_pool, const buf_page_t *bpage)
Adjust LRU hazard pointers if needed.
Definition: buf0lru.cc:757
void buf_LRU_validate_instance(buf_pool_t *buf_pool)
Validates the LRU list for one buffer pool instance.
Definition: buf0lru.cc:1666
void buf_LRU_block_free_non_file_page(buf_block_t *block)
Puts a block back to the free list.
Definition: buf0lru.cc:1207
buf_LRU_stat_t buf_LRU_stat_sum
Running sum of past values of buf_LRU_stat_cur.
Definition: buf0lru.cc:117
bool buf_LRU_buf_pool_running_out(void)
Returns true if less than 25 % of the buffer pool is available.
Definition: buf0lru.cc:389
void buf_LRU_validate(void)
Validates the LRU list.
Definition: buf0lru.cc:1741
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:59
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:1617
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:126
buf_block_t * buf_LRU_get_free_block(buf_pool_t *buf_pool)
Returns a free block from the buf_pool.
Definition: buf0lru.cc:517
buf_LRU_stat_t buf_LRU_stat_cur
Current operation counters.
Definition: buf0lru.cc:113
bool buf_LRU_scan_and_free_block(buf_pool_t *buf_pool, bool scan_all)
Try to free a replaceable block.
Definition: buf0lru.cc:362
buf_block_t * buf_LRU_get_free_only(buf_pool_t *buf_pool)
Returns a free block from the buf_pool.
Definition: buf0lru.cc:411
constexpr uint32_t BUF_LRU_OLD_RATIO_DIV
The denominator of buf_pool->LRU_old_ratio.
Definition: buf0lru.h:206
bool buf_LRU_free_page(buf_page_t *bpage, bool zip)
Try to free a block.
Definition: buf0lru.cc:956
constexpr uint32_t BUF_LRU_OLD_RATIO_MAX
Maximum value of buf_pool->LRU_old_ratio.
Definition: buf0lru.h:210
void buf_LRU_print(void)
Prints the LRU list.
Definition: buf0lru.cc:1838
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:1748
void buf_LRU_stat_inc_io()
Increments the I/O counter in buf_LRU_stat_cur.
Definition: buf0lru.h:247
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:1532
The database buffer pool global types for the directory.
Definition: buf0buf.h:1156
std::chrono::milliseconds milliseconds
Definition: authorize_manager.cc:69
Statistics for selecting the LRU list for eviction.
Definition: buf0lru.h:233
ulint unzip
Counter of page_zip_decompress operations.
Definition: buf0lru.h:235
ulint io
Counter of buffer pool I/O operations.
Definition: buf0lru.h:234
The buffer control block structure.
Definition: buf0buf.h:1756
The buffer pool structure.
Definition: buf0buf.h:2285
Definition: trx0trx.h:670
Version control for database, common definitions, and include files.
unsigned long int ulint
Definition: univ.i:403
Utilities for byte operations.