1 /*****************************************************************************
3 Copyright (c) 1995, 2011, Oracle and/or its affiliates. All Rights Reserved.
5 This program is free software; you can redistribute it and/or modify it under
6 the terms of the GNU General Public License as published by the Free Software
7 Foundation; version 2 of the License.
9 This program is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License along with
14 this program; if not, write to the Free Software Foundation, Inc.,
15 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 *****************************************************************************/
19 /**************************************************//**
20 @file include/buf0lru.h
21 The database buffer pool LRU replacement algorithm
23 Created 11/5/1995 Heikki Tuuri
24 *******************************************************/
31 #include "buf0types.h"
33 /******************************************************************//**
34 Tries to remove LRU flushed blocks from the end of the LRU list and put them
35 to the free list. This is beneficial for the efficiency of the insert buffer
36 operation, as flushed pages from non-unique non-clustered indexes are here
37 taken out of the buffer pool, and their inserts redirected to the insert
38 buffer. Otherwise, the flushed blocks could get modified again before read
39 operations need new buffer blocks, and the i/o work done in flushing would be
43 buf_LRU_try_free_flushed_blocks(void);
44 /*==================================*/
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
49 @return TRUE if less than 25 % of buffer pool left */
52 buf_LRU_buf_pool_running_out(void);
53 /*==============================*/
55 /*#######################################################################
56 These are low-level functions
57 #########################################################################*/
59 /** Minimum LRU list length for which the LRU_old pointer is defined */
60 #define BUF_LRU_OLD_MIN_LEN 512 /* 8 megabytes of 16k pages */
62 /** Maximum LRU list search length in buf_flush_LRU_recommendation() */
63 #define BUF_LRU_FREE_SEARCH_LEN (5 + 2 * BUF_READ_AHEAD_AREA)
65 /******************************************************************//**
66 Invalidates all pages belonging to a given tablespace when we are deleting
67 the data file(s) of that tablespace. A PROBLEM: if readahead is being started,
68 what guarantees that it will not try to read in pages after this operation has
72 buf_LRU_invalidate_tablespace(
73 /*==========================*/
74 ulint id
); /*!< in: space id */
75 #if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
76 /********************************************************************//**
77 Insert a compressed block into buf_pool->zip_clean in the LRU order. */
80 buf_LRU_insert_zip_clean(
81 /*=====================*/
82 buf_page_t
* bpage
); /*!< in: pointer to the block in question */
83 #endif /* UNIV_DEBUG || UNIV_BUF_DEBUG */
85 /******************************************************************//**
86 Try to free a block. If bpage is a descriptor of a compressed-only
87 page, the descriptor object will be freed as well.
89 NOTE: If this function returns TRUE, it will temporarily
90 release buf_pool_mutex. Furthermore, the page frame will no longer be
93 The caller must hold buf_pool_mutex and buf_page_get_mutex(bpage) and
94 release these two mutexes after the call. No other
95 buf_page_get_mutex() may be held when calling this function.
96 @return TRUE if freed, FALSE otherwise. */
101 buf_page_t
* bpage
, /*!< in: block to be freed */
102 ibool zip
) /*!< in: TRUE if should remove also the
103 compressed page of an uncompressed page */
104 __attribute__((nonnull
));
105 /******************************************************************//**
106 Try to free a replaceable block.
107 @return TRUE if found and freed */
110 buf_LRU_search_and_free_block(
111 /*==========================*/
112 ulint n_iterations
); /*!< in: how many times this has been called
113 repeatedly without result: a high value means
114 that we should search farther; if
115 n_iterations < 10, then we search
116 n_iterations / 10 * buf_pool->curr_size
117 pages from the end of the LRU list; if
118 n_iterations < 5, then we will also search
119 n_iterations / 5 of the unzip_LRU list. */
120 /******************************************************************//**
121 Returns a free block from the buf_pool. The block is taken off the
122 free list. If it is empty, returns NULL.
123 @return a free control block, or NULL if the buf_block->free list is empty */
126 buf_LRU_get_free_only(void);
127 /*=======================*/
128 /******************************************************************//**
129 Returns a free block from the buf_pool. The block is taken off the
130 free list. If it is empty, blocks are moved from the end of the
131 LRU list to the free list.
132 @return the free control block, in state BUF_BLOCK_READY_FOR_USE */
135 buf_LRU_get_free_block(void)
136 /*========================*/
137 __attribute__((warn_unused_result
));
139 /******************************************************************//**
140 Puts a block back to the free list. */
143 buf_LRU_block_free_non_file_page(
144 /*=============================*/
145 buf_block_t
* block
); /*!< in: block, must not contain a file page */
146 /******************************************************************//**
147 Adds a block to the LRU list. */
152 buf_page_t
* bpage
, /*!< in: control block */
153 ibool old
); /*!< in: TRUE if should be put to the old
154 blocks in the LRU list, else put to the
155 start; if the LRU list is very short, added to
156 the start regardless of this parameter */
157 /******************************************************************//**
158 Adds a block to the LRU list of decompressed zip pages. */
161 buf_unzip_LRU_add_block(
162 /*====================*/
163 buf_block_t
* block
, /*!< in: control block */
164 ibool old
); /*!< in: TRUE if should be put to the end
165 of the list, else put to the start */
166 /******************************************************************//**
167 Moves a block to the start of the LRU list. */
170 buf_LRU_make_block_young(
171 /*=====================*/
172 buf_page_t
* bpage
); /*!< in: control block */
173 /******************************************************************//**
174 Moves a block to the end of the LRU list. */
177 buf_LRU_make_block_old(
178 /*===================*/
179 buf_page_t
* bpage
); /*!< in: control block */
180 /**********************************************************************//**
181 Updates buf_LRU_old_ratio.
182 @return updated old_pct */
185 buf_LRU_old_ratio_update(
186 /*=====================*/
187 uint old_pct
,/*!< in: Reserve this percentage of
188 the buffer pool for "old" blocks. */
189 ibool adjust
);/*!< in: TRUE=adjust the LRU list;
190 FALSE=just assign buf_LRU_old_ratio
191 during the initialization of InnoDB */
192 /********************************************************************//**
193 Update the historical stats that we are collecting for LRU eviction
194 policy at the end of each interval. */
197 buf_LRU_stat_update(void);
198 /*=====================*/
200 #if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
201 /**********************************************************************//**
202 Validates the LRU list.
206 buf_LRU_validate(void);
207 /*==================*/
208 #endif /* UNIV_DEBUG || UNIV_BUF_DEBUG */
209 #if defined UNIV_DEBUG_PRINT || defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
210 /**********************************************************************//**
211 Prints the LRU list. */
216 #endif /* UNIV_DEBUG_PRINT || UNIV_DEBUG || UNIV_BUF_DEBUG */
218 /** @name Heuristics for detecting index scan @{ */
219 /** Reserve this much/BUF_LRU_OLD_RATIO_DIV of the buffer pool for
220 "old" blocks. Protected by buf_pool_mutex. */
221 extern uint buf_LRU_old_ratio
;
222 /** The denominator of buf_LRU_old_ratio. */
223 #define BUF_LRU_OLD_RATIO_DIV 1024
224 /** Maximum value of buf_LRU_old_ratio.
225 @see buf_LRU_old_adjust_len
226 @see buf_LRU_old_ratio_update */
227 #define BUF_LRU_OLD_RATIO_MAX BUF_LRU_OLD_RATIO_DIV
228 /** Minimum value of buf_LRU_old_ratio.
229 @see buf_LRU_old_adjust_len
230 @see buf_LRU_old_ratio_update
231 The minimum must exceed
232 (BUF_LRU_OLD_TOLERANCE + 5) * BUF_LRU_OLD_RATIO_DIV / BUF_LRU_OLD_MIN_LEN. */
233 #define BUF_LRU_OLD_RATIO_MIN 51
235 #if BUF_LRU_OLD_RATIO_MIN >= BUF_LRU_OLD_RATIO_MAX
236 # error "BUF_LRU_OLD_RATIO_MIN >= BUF_LRU_OLD_RATIO_MAX"
238 #if BUF_LRU_OLD_RATIO_MAX > BUF_LRU_OLD_RATIO_DIV
239 # error "BUF_LRU_OLD_RATIO_MAX > BUF_LRU_OLD_RATIO_DIV"
242 /** Move blocks to "new" LRU list only if the first access was at
243 least this many milliseconds ago. Not protected by any mutex or latch. */
244 extern uint buf_LRU_old_threshold_ms
;
247 /** @brief Statistics for selecting the LRU list for eviction.
249 These statistics are not 'of' LRU but 'for' LRU. We keep count of I/O
250 and page_zip_decompress() operations. Based on the statistics we decide
251 if we want to evict from buf_pool->unzip_LRU or buf_pool->LRU. */
252 struct buf_LRU_stat_struct
254 ulint io
; /**< Counter of buffer pool I/O operations. */
255 ulint unzip
; /**< Counter of page_zip_decompress operations. */
258 /** Statistics for selecting the LRU list for eviction. */
259 typedef struct buf_LRU_stat_struct buf_LRU_stat_t
;
261 /** Current operation counters. Not protected by any mutex.
262 Cleared by buf_LRU_stat_update(). */
263 extern buf_LRU_stat_t buf_LRU_stat_cur
;
265 /** Running sum of past values of buf_LRU_stat_cur.
266 Updated by buf_LRU_stat_update(). Protected by buf_pool_mutex. */
267 extern buf_LRU_stat_t buf_LRU_stat_sum
;
269 /********************************************************************//**
270 Increments the I/O counter in buf_LRU_stat_cur. */
271 #define buf_LRU_stat_inc_io() buf_LRU_stat_cur.io++
272 /********************************************************************//**
273 Increments the page_zip_decompress() counter in buf_LRU_stat_cur. */
274 #define buf_LRU_stat_inc_unzip() buf_LRU_stat_cur.unzip++
277 #include "buf0lru.ic"