| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 1995, 2026, Oracle and/or its affiliates. |
| 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 designed to work with certain software (including |
| 10 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 11 |
as designated in a particular file or component or in included license |
| 12 |
documentation. The authors of MySQL hereby grant you an additional |
| 13 |
permission to link the program and your derivative works with the |
| 14 |
separately licensed software that they have either included with |
| 15 |
the program or referenced in the documentation. |
| 16 |
|
| 17 |
This program is distributed in the hope that it will be useful, but WITHOUT |
| 18 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 19 |
FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0, |
| 20 |
for more details. |
| 21 |
|
| 22 |
You should have received a copy of the GNU General Public License along with |
| 23 |
this program; if not, write to the Free Software Foundation, Inc., |
| 24 |
51 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 |
| 45 |
struct 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 */ |
| 51 |
bool buf_LRU_buf_pool_running_out(void); |
| 52 |
|
| 53 |
/*####################################################################### |
| 54 |
These are low-level functions |
| 55 |
#########################################################################*/ |
| 56 |
|
| 57 |
/** Minimum LRU list length for which the LRU_old pointer is defined |
| 58 |
8 megabytes of 16k pages */ |
| 59 |
constexpr uint32_t BUF_LRU_OLD_MIN_LEN = 8 * 1024 / 16; |
| 60 |
#endif /* !UNIV_HOTBACKUP */ |
| 61 |
|
| 62 |
/** Flushes all dirty pages or removes all pages belonging to a given |
| 63 |
tablespace. A PROBLEM: if readahead is being started, what guarantees |
| 64 |
that it will not try to read in pages after this operation has completed? |
| 65 |
@param[in] id tablespace ID |
| 66 |
@param[in] buf_remove remove or flush strategy |
| 67 |
@param[in] trx to check if the operation must be interrupted |
| 68 |
@param[in] strict true, if no page from tablespace can be in |
| 69 |
buffer pool just after flush */ |
| 70 |
void buf_LRU_flush_or_remove_pages(space_id_t id, buf_remove_t buf_remove, |
| 71 |
const trx_t *trx, bool strict = true); |
| 72 |
|
| 73 |
#ifndef UNIV_HOTBACKUP |
| 74 |
#if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG |
| 75 |
/** Insert a compressed block into buf_pool->zip_clean in the LRU order. |
| 76 |
@param[in] bpage pointer to the block in question */ |
| 77 |
void buf_LRU_insert_zip_clean(buf_page_t *bpage); |
| 78 |
#endif /* UNIV_DEBUG || UNIV_BUF_DEBUG */ |
| 79 |
|
| 80 |
/** Try to free a block. If bpage is a descriptor of a compressed-only |
| 81 |
page, the descriptor object will be freed as well. |
| 82 |
NOTE: this function may temporarily release and relock the |
| 83 |
buf_page_get_mutex(). Furthermore, the page frame will no longer be |
| 84 |
accessible via bpage. If this function returns true, it will also release |
| 85 |
the LRU list mutex. |
| 86 |
The caller must hold the LRU list and buf_page_get_mutex() mutexes. |
| 87 |
@param[in] bpage block to be freed |
| 88 |
@param[in] zip true if should remove also the compressed page of |
| 89 |
an uncompressed page |
| 90 |
@return true if freed, false otherwise. */ |
| 91 |
[[nodiscard]] bool buf_LRU_free_page(buf_page_t *bpage, bool zip); |
| 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 true, otherwise scan |
| 96 |
only BUF_LRU_SEARCH_SCAN_THRESHOLD blocks |
| 97 |
@return true if found and freed */ |
| 98 |
[[nodiscard]] bool buf_LRU_scan_and_free_block(buf_pool_t *buf_pool, |
| 99 |
bool scan_all); |
| 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 */ |
| 105 |
buf_block_t *buf_LRU_get_free_only(buf_pool_t *buf_pool); |
| 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 */ |
| 131 |
[[nodiscard]] buf_block_t *buf_LRU_get_free_block(buf_pool_t *buf_pool); |
| 132 |
|
| 133 |
/** Determines if the unzip_LRU list should be used for evicting a victim |
| 134 |
instead of the general LRU list. |
| 135 |
@param[in,out] buf_pool buffer pool instance |
| 136 |
@return true if should use unzip_LRU */ |
| 137 |
bool buf_LRU_evict_from_unzip_LRU(buf_pool_t *buf_pool); |
| 138 |
|
| 139 |
/** Puts a block back to the free list. |
| 140 |
@param[in] block block must not contain a file page */ |
| 141 |
void buf_LRU_block_free_non_file_page(buf_block_t *block); |
| 142 |
|
| 143 |
/** Adds a block to the LRU list. Please make sure that the page_size is |
| 144 |
already set when invoking the function, so that we can get correct |
| 145 |
page_size from the buffer page when adding a block into LRU */ |
| 146 |
void buf_LRU_add_block(buf_page_t *bpage, /*!< in: control block */ |
| 147 |
bool old); /*!< in: true if should be put to the old |
| 148 |
blocks in the LRU list, else put to the |
| 149 |
start; if the LRU list is very short, added |
| 150 |
to the start regardless of this parameter */ |
| 151 |
|
| 152 |
/** Adds a block to the LRU list of decompressed zip pages. |
| 153 |
@param[in] block control block |
| 154 |
@param[in] old true if should be put to the end of the list, |
| 155 |
else put to the start */ |
| 156 |
void buf_unzip_LRU_add_block(buf_block_t *block, bool old); |
| 157 |
|
| 158 |
/** Moves a block to the start of the LRU list. |
| 159 |
@param[in] bpage control block */ |
| 160 |
void buf_LRU_make_block_young(buf_page_t *bpage); |
| 161 |
|
| 162 |
/** Moves a block to the end of the LRU list. |
| 163 |
@param[in] bpage control block */ |
| 164 |
void buf_LRU_make_block_old(buf_page_t *bpage); |
| 165 |
|
| 166 |
/** Updates buf_pool->LRU_old_ratio. |
| 167 |
@return updated old_pct */ |
| 168 |
uint buf_LRU_old_ratio_update( |
| 169 |
uint old_pct, /*!< in: Reserve this percentage of |
| 170 |
the buffer pool for "old" blocks. */ |
| 171 |
bool adjust); /*!< in: true=adjust the LRU list; |
| 172 |
false=just assign buf_pool->LRU_old_ratio |
| 173 |
during the initialization of InnoDB */ |
| 174 |
/** Update the historical stats that we are collecting for LRU eviction |
| 175 |
policy at the end of each interval. */ |
| 176 |
void buf_LRU_stat_update(void); |
| 177 |
|
| 178 |
/** Remove one page from LRU list and put it to free list. The caller must hold |
| 179 |
the LRU list and block mutexes and have page hash latched in X. The latch and |
| 180 |
the block mutexes will be released. |
| 181 |
@param[in,out] bpage block, must contain a file page and |
| 182 |
be in a state where it can be freed; there |
| 183 |
may or may not be a hash index to the page |
| 184 |
@param[in] ignore_content true if should ignore page content, since it |
| 185 |
could be not initialized */ |
| 186 |
void buf_LRU_free_one_page(buf_page_t *bpage, bool ignore_content); |
| 187 |
|
| 188 |
/** Adjust LRU hazard pointers if needed. |
| 189 |
@param[in] buf_pool Buffer pool instance |
| 190 |
@param[in] bpage Control block */ |
| 191 |
void buf_LRU_adjust_hp(buf_pool_t *buf_pool, const buf_page_t *bpage); |
| 192 |
|
| 193 |
#if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG |
| 194 |
/** Validates the LRU list. */ |
| 195 |
void buf_LRU_validate(void); |
| 196 |
|
| 197 |
/** Validates the LRU list for one buffer pool instance. |
| 198 |
@param[in] buf_pool buffer pool instance */ |
| 199 |
void buf_LRU_validate_instance(buf_pool_t *buf_pool); |
| 200 |
|
| 201 |
using Space_References = std::map<struct fil_space_t *, size_t>; |
| 202 |
|
| 203 |
/** Counts number of pages that are still in the LRU for each space instance |
| 204 |
encountered. |
| 205 |
@returns map of space instances into count of pages in LRU. */ |
| 206 |
Space_References buf_LRU_count_space_references(); |
| 207 |
|
| 208 |
#endif /* UNIV_DEBUG || UNIV_BUF_DEBUG */ |
| 209 |
#if defined UNIV_DEBUG_PRINT || defined UNIV_DEBUG || defined UNIV_BUF_DEBUG |
| 210 |
/** Prints the LRU list. */ |
| 211 |
void buf_LRU_print(void); |
| 212 |
#endif /* UNIV_DEBUG_PRINT || UNIV_DEBUG || UNIV_BUF_DEBUG */ |
| 213 |
|
| 214 |
/** @name Heuristics for detecting index scan |
| 215 |
@{ */ |
| 216 |
/** The denominator of buf_pool->LRU_old_ratio. */ |
| 217 |
constexpr uint32_t BUF_LRU_OLD_RATIO_DIV = 1024; |
| 218 |
/** Maximum value of buf_pool->LRU_old_ratio. |
| 219 |
@see buf_LRU_old_adjust_len |
| 220 |
@see buf_pool->LRU_old_ratio_update */ |
| 221 |
constexpr uint32_t BUF_LRU_OLD_RATIO_MAX = BUF_LRU_OLD_RATIO_DIV; |
| 222 |
/** Minimum value of buf_pool->LRU_old_ratio. |
| 223 |
@see buf_LRU_old_adjust_len |
| 224 |
@see buf_pool->LRU_old_ratio_update |
| 225 |
The minimum must exceed |
| 226 |
(BUF_LRU_OLD_TOLERANCE + 5) * BUF_LRU_OLD_RATIO_DIV / BUF_LRU_OLD_MIN_LEN. */ |
| 227 |
constexpr uint32_t BUF_LRU_OLD_RATIO_MIN = 51; |
| 228 |
|
| 229 |
static_assert(BUF_LRU_OLD_RATIO_MIN < BUF_LRU_OLD_RATIO_MAX, |
| 230 |
"BUF_LRU_OLD_RATIO_MIN >= BUF_LRU_OLD_RATIO_MAX"); |
| 231 |
static_assert(BUF_LRU_OLD_RATIO_MAX <= BUF_LRU_OLD_RATIO_DIV, |
| 232 |
"BUF_LRU_OLD_RATIO_MAX > BUF_LRU_OLD_RATIO_DIV"); |
| 233 |
|
| 234 |
/** Move blocks to "new" LRU list only if the first access was at |
| 235 |
least this many milliseconds ago. Not protected by any mutex or latch. */ |
| 236 |
std::chrono::milliseconds get_buf_LRU_old_threshold(); |
| 237 |
/** @} */ |
| 238 |
|
| 239 |
/** @brief Statistics for selecting the LRU list for eviction. |
| 240 |
|
| 241 |
These statistics are not 'of' LRU but 'for' LRU. We keep count of I/O |
| 242 |
and page_zip_decompress() operations. Based on the statistics we decide |
| 243 |
if we want to evict from buf_pool->unzip_LRU or buf_pool->LRU. */ |
| 244 |
struct buf_LRU_stat_t { |
| 245 |
ulint io; /**< Counter of buffer pool I/O operations. */ |
| 246 |
ulint unzip; /**< Counter of page_zip_decompress operations. */ |
| 247 |
}; |
| 248 |
|
| 249 |
/** Current operation counters. Not protected by any mutex. |
| 250 |
Cleared by buf_LRU_stat_update(). */ |
| 251 |
extern buf_LRU_stat_t buf_LRU_stat_cur; |
| 252 |
|
| 253 |
/** Running sum of past values of buf_LRU_stat_cur. |
| 254 |
Updated by buf_LRU_stat_update(). Accesses protected by memory barriers. */ |
| 255 |
extern buf_LRU_stat_t buf_LRU_stat_sum; |
| 256 |
|
| 257 |
/** Increments the I/O counter in buf_LRU_stat_cur. */ |
| 258 |
inline void buf_LRU_stat_inc_io() { buf_LRU_stat_cur.io++; } |
| 259 |
/** Increments the page_zip_decompress() counter in buf_LRU_stat_cur. */ |
| 260 |
inline void buf_LRU_stat_inc_unzip() { buf_LRU_stat_cur.unzip++; } |
| 261 |
|
| 262 |
#endif /* !UNIV_HOTBACKUP */ |
| 263 |
|
| 264 |
#endif |