Debian Codegraph @

storage/innobase/buf/buf0lru.cc

raw
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 /** @file buf/buf0lru.cc
29 The database buffer replacement algorithm
30
31 Created 11/5/1995 Heikki Tuuri
32 *******************************************************/
33
34 #include "buf0lru.h"
35
36 #include "btr0btr.h"
37 #include "btr0sea.h"
38 #include "buf0buddy.h"
39 #include "buf0buf.h"
40 #include "buf0dblwr.h"
41 #include "buf0flu.h"
42 #include "buf0rea.h"
43 #include "buf0stats.h"
44 #include "fil0fil.h"
45 #include "hash0hash.h"
46 #include "ibuf0ibuf.h"
47 #include "log0recv.h"
48 #include "log0write.h"
49 #include "my_dbug.h"
50 #include "os0event.h"
51 #include "os0file.h"
52 #include "page0zip.h"
53 #include "srv0mon.h"
54 #include "srv0srv.h"
55 #include "sync0rw.h"
56 #include "trx0trx.h"
57 #include "ut0byte.h"
58 #include "ut0rnd.h"
59
60 /** The number of blocks from the LRU_old pointer onward, including
61 the block pointed to, must be buf_pool->LRU_old_ratio/BUF_LRU_OLD_RATIO_DIV
62 of the whole LRU list length, except that the tolerance defined below
63 is allowed. Note that the tolerance must be small enough such that for
64 even the BUF_LRU_OLD_MIN_LEN long LRU list, the LRU_old pointer is not
65 allowed to point to either end of the LRU list. */
66
67 constexpr uint32_t BUF_LRU_OLD_TOLERANCE = 20;
68
69 /** The minimum amount of non-old blocks when the LRU_old list exists
70 (that is, when there are more than BUF_LRU_OLD_MIN_LEN blocks).
71 @see buf_LRU_old_adjust_len */
72 constexpr uint32_t BUF_LRU_NON_OLD_MIN_LEN = 5;
73 static_assert(BUF_LRU_NON_OLD_MIN_LEN < BUF_LRU_OLD_MIN_LEN,
74 "BUF_LRU_NON_OLD_MIN_LEN >= BUF_LRU_OLD_MIN_LEN");
75 /** When dropping the search hash index entries before deleting an ibd
76 file, we build a local array of pages belonging to that tablespace
77 in the buffer pool. Following is the size of that array.
78 We also release buf_pool->LRU_list_mutex after scanning this many pages of the
79 flush_list when dropping a table. This is to ensure that other threads
80 are not blocked for extended period of time when using very large
81 buffer pools. */
82 static const ulint BUF_LRU_DROP_SEARCH_SIZE = 1024;
83
84 /** We scan these many blocks when looking for a clean page to evict
85 during LRU eviction. */
86 static const ulint BUF_LRU_SEARCH_SCAN_THRESHOLD = 100;
87
88 /** If we switch on the InnoDB monitor because there are too few available
89 frames in the buffer pool, we set this to true */
90 static std::atomic_bool buf_lru_switched_on_innodb_mon = false;
91
92 /** These statistics are not 'of' LRU but 'for' LRU. We keep count of I/O
93 and page_zip_decompress() operations. Based on the statistics,
94 buf_LRU_evict_from_unzip_LRU() decides if we want to evict from
95 unzip_LRU or the regular LRU. From unzip_LRU, we will only evict the
96 uncompressed frame (meaning we can evict dirty blocks as well). From
97 the regular LRU, we will evict the entire block (i.e.: both the
98 uncompressed and compressed data), which must be clean. */
99
100 /** @{ */
101
102 /** Number of intervals for which we keep the history of these stats.
103 Each interval is 1 second, defined by the rate at which
104 srv_error_monitor_thread() calls buf_LRU_stat_update(). */
105 static const ulint BUF_LRU_STAT_N_INTERVAL = 50;
106
107 /** Co-efficient with which we multiply I/O operations to equate them
108 with page_zip_decompress() operations. */
109 static const ulint BUF_LRU_IO_TO_UNZIP_FACTOR = 50;
110
111 /** Sampled values buf_LRU_stat_cur.
112 Not protected by any mutex. Updated by buf_LRU_stat_update(). */
113 static buf_LRU_stat_t buf_LRU_stat_arr[BUF_LRU_STAT_N_INTERVAL];
114
115 /** Cursor to buf_LRU_stat_arr[] that is updated in a round-robin fashion. */
116 static ulint buf_LRU_stat_arr_ind;
117
118 /** Current operation counters. Not protected by any mutex. Cleared
119 by buf_LRU_stat_update(). */
120 buf_LRU_stat_t buf_LRU_stat_cur;
121
122 /** Running sum of past values of buf_LRU_stat_cur.
123 Updated by buf_LRU_stat_update(). Not Protected by any mutex. */
124 buf_LRU_stat_t buf_LRU_stat_sum;
125
126 /** @} */
127
128 /** @name Heuristics for detecting index scan
129 @{ */
130 /** Move blocks to "new" LRU list only if the first access was at
131 least this many milliseconds ago. Not protected by any mutex or latch. */
132 uint buf_LRU_old_threshold;
133 std::chrono::milliseconds get_buf_LRU_old_threshold() {
134 return std::chrono::milliseconds{buf_LRU_old_threshold};
135 }
136
137 /** @} */
138
139 /** Takes a block out of the LRU list and page hash table.
140 If the block is compressed-only (BUF_BLOCK_ZIP_PAGE),
141 the object will be freed.
142
143 The caller must hold buf_pool->LRU_list_mutex, the buf_page_get_mutex() mutex
144 and the appropriate hash_lock. This function will release the
145 buf_page_get_mutex() and the hash_lock.
146
147 If a compressed page is freed other compressed pages may be relocated.
148
149 @param[in] bpage block, must contain a file page and
150 be in a state where it can be freed; there
151 may or may not be a hash index to the page
152 @param[in] zip true if should remove also the
153 compressed page of an uncompressed page
154 @param[in] ignore_content true if should ignore page content, since it
155 could be not initialized
156 @retval true if BUF_BLOCK_FILE_PAGE was removed from page_hash. The
157 caller needs to free the page to the free list
158 @retval false if BUF_BLOCK_ZIP_PAGE was removed from page_hash. In
159 this case the block is already returned to the buddy allocator. */
160 [[nodiscard]] static bool buf_LRU_block_remove_hashed(buf_page_t *bpage,
161 bool zip,
162 bool ignore_content);
163
164 /** Puts a file page whose has no hash index to the free list.
165 @param[in,out] block Must contain a file page and be in a state
166 where it can be freed. */
167 static void buf_LRU_block_free_hashed_page(buf_block_t *block) noexcept;
168
169 /** Increases LRU size in bytes with page size inline function
170 @param[in] bpage control block
171 @param[in] buf_pool buffer pool instance */
172 static inline void incr_LRU_size_in_bytes(buf_page_t *bpage,
173 buf_pool_t *buf_pool) {
174 ut_ad(mutex_own(&buf_pool->LRU_list_mutex));
175
176 buf_pool->stat.LRU_bytes += bpage->size.physical();
177
178 ut_ad(buf_pool->stat.LRU_bytes <= buf_pool->curr_pool_size);
179 }
180
181 /** Determines if the unzip_LRU list should be used for evicting a victim
182 instead of the general LRU list.
183 @param[in,out] buf_pool buffer pool instance
184 @return true if should use unzip_LRU */
185 bool buf_LRU_evict_from_unzip_LRU(buf_pool_t *buf_pool) {
186 ut_ad(mutex_own(&buf_pool->LRU_list_mutex));
187
188 /* If the unzip_LRU list is empty, we can only use the LRU. */
189 if (UT_LIST_GET_LEN(buf_pool->unzip_LRU) == 0) {
190 return false;
191 }
192
193 /* If unzip_LRU is at most 10% of the size of the LRU list,
194 then use the LRU. This slack allows us to keep hot
195 decompressed pages in the buffer pool. */
196 if (UT_LIST_GET_LEN(buf_pool->unzip_LRU) <=
197 UT_LIST_GET_LEN(buf_pool->LRU) / 10) {
198 return false;
199 }
200
201 /* If eviction hasn't started yet, we assume by default
202 that a workload is disk bound. */
203 if (buf_pool->freed_page_clock == 0) {
204 return true;
205 }
206
207 /* Calculate the average over past intervals, and add the values
208 of the current interval. */
209 ulint io_avg =
210 buf_LRU_stat_sum.io / BUF_LRU_STAT_N_INTERVAL + buf_LRU_stat_cur.io;
211
212 ulint unzip_avg =
213 buf_LRU_stat_sum.unzip / BUF_LRU_STAT_N_INTERVAL + buf_LRU_stat_cur.unzip;
214
215 /* Decide based on our formula. If the load is I/O bound
216 (unzip_avg is smaller than the weighted io_avg), evict an
217 uncompressed frame from unzip_LRU. Otherwise we assume that
218 the load is CPU bound and evict from the regular LRU. */
219 return (unzip_avg <= io_avg * BUF_LRU_IO_TO_UNZIP_FACTOR);
220 }
221
222 /** Attempts to drop page hash index on a batch of pages belonging to a
223 particular space id.
224 @param[in] space_id space id
225 @param[in] page_size page size
226 @param[in] arr array of page_no
227 @param[in] count number of entries in array */
228 static void buf_LRU_drop_page_hash_batch(space_id_t space_id,
229 const page_size_t &page_size,
230 const page_no_t *arr, ulint count) {
231 ut_ad(count <= BUF_LRU_DROP_SEARCH_SIZE);
232
233 for (ulint i = 0; i < count; ++i, ++arr) {
234 /* While our only caller
235 buf_LRU_drop_page_hash_for_tablespace()
236 is being executed for DROP TABLE or similar,
237 the table cannot be evicted from the buffer pool.
238 Note: this should not be executed for DROP TABLESPACE,
239 because DROP TABLESPACE would be refused if tables existed
240 in the tablespace, and a previous DROP TABLE would have
241 already removed the AHI entries. */
242 btr_search_drop_page_hash_when_freed(page_id_t(space_id, *arr), page_size);
243 }
244 }
245
246 /** When doing a DROP TABLE/DISCARD TABLESPACE we have to drop all page
247 hash index entries belonging to that table. This function tries to
248 do that in batch. Note that this is a 'best effort' attempt and does
249 not guarantee that ALL hash entries will be removed.
250 @param[in] buf_pool buffer pool instance
251 @param[in] space_id space id */
252 static void buf_LRU_drop_page_hash_for_tablespace(buf_pool_t *buf_pool,
253 space_id_t space_id) {
254 bool found;
255 const page_size_t page_size(fil_space_get_page_size(space_id, &found));
256
257 if (!found) {
258 /* Somehow, the tablespace does not exist. Nothing to drop. */
259 ut_d(ut_error);
260 ut_o(return);
261 }
262
263 page_no_t *page_arr = static_cast<page_no_t *>(ut::malloc_withkey(
264 UT_NEW_THIS_FILE_PSI_KEY, sizeof(page_no_t) * BUF_LRU_DROP_SEARCH_SIZE));
265
266 ulint num_entries = 0;
267
268 mutex_enter(&buf_pool->LRU_list_mutex);
269
270 scan_again:
271 for (buf_page_t *bpage = UT_LIST_GET_LAST(buf_pool->LRU); bpage != nullptr;
272 /* No op */) {
273 buf_page_t *prev_bpage = UT_LIST_GET_PREV(LRU, bpage);
274
275 ut_a(buf_page_in_file(bpage));
276
277 if (buf_page_get_state(bpage) != BUF_BLOCK_FILE_PAGE ||
278 bpage->id.space() != space_id || bpage->was_io_fixed()) {
279 /* Compressed pages are never hashed.
280 Skip blocks of other tablespaces.
281 Skip I/O-fixed blocks (to be dealt with later). */
282 next_page:
283 bpage = prev_bpage;
284 continue;
285 }
286
287 buf_block_t *block = reinterpret_cast<buf_block_t *>(bpage);
288
289 mutex_enter(&block->mutex);
290
291 block->ahi.validate();
292
293 bool skip = bpage->buf_fix_count > 0 || !block->ahi.index;
294
295 mutex_exit(&block->mutex);
296
297 if (skip) {
298 /* Skip this block, because there are
299 no adaptive hash index entries
300 pointing to it, or because we cannot
301 drop them due to the buffer-fix. */
302 goto next_page;
303 }
304
305 /* Store the page number so that we can drop the hash
306 index in a batch later. */
307 page_arr[num_entries] = bpage->id.page_no();
308 ut_a(num_entries < BUF_LRU_DROP_SEARCH_SIZE);
309 ++num_entries;
310
311 if (num_entries < BUF_LRU_DROP_SEARCH_SIZE) {
312 goto next_page;
313 }
314
315 /* Array full. We release the LRU list mutex to obey
316 the latching order. */
317 mutex_exit(&buf_pool->LRU_list_mutex);
318
319 buf_LRU_drop_page_hash_batch(space_id, page_size, page_arr, num_entries);
320
321 num_entries = 0;
322
323 mutex_enter(&buf_pool->LRU_list_mutex);
324
325 /* Note that we released the buf_pool->LRU_list_mutex above
326 after reading the prev_bpage during processing of a
327 page_hash_batch (i.e.: when the array was full).
328 Because prev_bpage could belong to a compressed-only
329 block, it may have been relocated, and thus the
330 pointer cannot be trusted. Because bpage is of type
331 buf_block_t, it is safe to dereference.
332
333 bpage can change in the LRU list. This is OK because
334 this function is a 'best effort' to drop as many
335 search hash entries as possible and it does not
336 guarantee that ALL such entries will be dropped. */
337
338 /* If, however, bpage has been removed from LRU list
339 to the free list then we should restart the scan. */
340 if (bpage != nullptr && buf_page_get_state(bpage) != BUF_BLOCK_FILE_PAGE) {
341 goto scan_again;
342 }
343 }
344
345 mutex_exit(&buf_pool->LRU_list_mutex);
346
347 /* Drop any remaining batch of search hashed pages. */
348 buf_LRU_drop_page_hash_batch(space_id, page_size, page_arr, num_entries);
349 ut::free(page_arr);
350 }
351
352 /** Try to pin the block in buffer pool. Once pinned, the block cannot be moved
353 within flush list or removed. The dirty page can be flushed when we release the
354 flush list mutex. We return without pinning in that case.
355 @param[in,out] buf_pool buffer pool instance
356 @param[in,out] bpage page to remove
357 @return true if page could be pinned successfully. */
358 static bool buf_page_try_pin(buf_pool_t *buf_pool, buf_page_t *bpage) {
359 /* Allow pin/unpin with NULL. */
360 if (bpage == nullptr) {
361 return true;
362 }
363
364 ut_ad(mutex_own(&buf_pool->LRU_list_mutex));
365 ut_ad(buf_flush_list_mutex_own(buf_pool));
366 ut_ad(bpage->in_flush_list);
367
368 /* To take care of the ABA problem that the block get flushed and
369 re-inserted into flush list, we can check the oldest LSN. It is
370 safe to access oldest LSN with flush list mutex protection as it
371 is set and reset while adding and removing from flush list. */
372 auto saved_oldest_lsn = bpage->get_oldest_lsn();
373
374 buf_flush_list_mutex_exit(buf_pool);
375
376 /* The LRU list mutex ensures that the page descriptor cannot be freed
377 for both compressed and uncompressed page. */
378 BPageMutex *block_mutex = buf_page_get_mutex(bpage);
379 mutex_enter(block_mutex);
380
381 bool pinned = false;
382
383 /* Recheck the I/O fix and the flush list presence now that we
384 hold the right mutex */
385 if (buf_page_get_io_fix(bpage) == BUF_IO_NONE && bpage->is_dirty() &&
386 saved_oldest_lsn == bpage->get_oldest_lsn()) {
387 /* "Fix" the block so that the position cannot be
388 changed after we release the buffer pool and
389 block mutexes. */
390 buf_page_set_sticky(bpage);
391 pinned = true;
392 ut_ad(bpage->in_flush_list);
393 }
394
395 mutex_exit(block_mutex);
396 buf_flush_list_mutex_enter(buf_pool);
397
398 return pinned;
399 }
400
401 /** Unpin the block in buffer pool. Ensure that the dirty page cannot be
402 flushed even though we need to release the flush list mutex momentarily.
403 @param[in,out] buf_pool buffer pool instance
404 @param[in,out] bpage page to remove */
405 static void buf_page_unpin(buf_pool_t *buf_pool, buf_page_t *bpage) {
406 /* Allow pin/unpin with NULL. */
407 if (bpage == nullptr) {
408 return;
409 }
410
411 ut_ad(mutex_own(&buf_pool->LRU_list_mutex));
412 ut_ad(buf_flush_list_mutex_own(buf_pool));
413
414 buf_flush_list_mutex_exit(buf_pool);
415
416 BPageMutex *block_mutex = buf_page_get_mutex(bpage);
417 mutex_enter(block_mutex);
418
419 /* "Unfix" the block now that we have both the LRU list and block mutexes . */
420 buf_page_unset_sticky(bpage);
421
422 buf_flush_list_mutex_enter(buf_pool);
423
424 /* Release block mutex only after re-acquiring the flush list mutex to
425 avoid any window where the page could have been flushed concurrently. The
426 block mutex must be acquired before flushing a page. */
427 mutex_exit(block_mutex);
428 }
429
430 /** If we have hogged the resources for too long then release the LRU list and
431 flush list mutexes and do a thread yield. Set the current page to "sticky" so
432 that it is not relocated during the yield. If I/O is started before sticky BIT
433 could be set, we skip yielding. The caller should restart the scan.
434 @param[in,out] buf_pool buffer pool instance
435 @param[in,out] bpage page to remove
436 @param[in] processed number of pages processed
437 @param[out] restart if caller needs to restart scan
438 @return true if yielded. */
439 [[nodiscard]] static bool buf_flush_try_yield(buf_pool_t *buf_pool,
440 buf_page_t *bpage,
441 size_t processed, bool &restart) {
442 ut_ad(mutex_own(&buf_pool->LRU_list_mutex));
443 ut_ad(buf_flush_list_mutex_own(buf_pool));
444
445 restart = false;
446
447 /* Every BUF_LRU_DROP_SEARCH_SIZE iterations in the loop we release
448 buf_pool->LRU_list_mutex to let other threads do their job but only if the
449 block is not IO fixed. This ensures that the block stays in its position in
450 the flush_list. We read io_fix without block_mutex, because we will recheck
451 with block_mutex.*/
452 if (bpage != nullptr && processed >= BUF_LRU_DROP_SEARCH_SIZE &&
453 bpage->was_io_fix_none()) {
454 if (!buf_page_try_pin(buf_pool, bpage)) {
455 restart = true;
456 return false;
457 }
458
459 ut_ad(bpage->in_flush_list);
460 ut_d(auto oldest_lsn = bpage->get_oldest_lsn());
461
462 /* Now it is safe to release the LRU list mutex. */
463 buf_flush_list_mutex_exit(buf_pool);
464 mutex_exit(&buf_pool->LRU_list_mutex);
465
466 /* Try and force a context switch. */
467 std::this_thread::yield();
468
469 mutex_enter(&buf_pool->LRU_list_mutex);
470 buf_flush_list_mutex_enter(buf_pool);
471
472 buf_page_unpin(buf_pool, bpage);
473
474 /* Should not have been removed from the flush list during the yield. */
475 ut_ad(bpage->in_flush_list);
476
477 /* The oldest LSN change would mean the page is removed and inserted back.
478 This ABA issue is handled during pinning and should not be the case. */
479 ut_ad(oldest_lsn == bpage->get_oldest_lsn());
480
481 return true;
482 }
483 return false;
484 }
485
486 /** Check if a dirty page should be flushed or removed based on space ID and
487 flush observer.
488 @param[in] page dirty page in flush list
489 @param[in] observer Flush observer
490 @param[in] space Space ID
491 @return true, if page should considered for flush or removal. */
492 static inline bool check_page_flush_observer(buf_page_t *page,
493 const Flush_observer *observer,
494 space_id_t space) {
495 /* If no flush observer then compare space ID. */
496 if (observer == nullptr) {
497 return (space == page->id.space());
498 }
499 /* Otherwise, match the flush observer pointer. */
500 return (observer == page->get_flush_observer());
501 }
502
503 /** Attempts to remove a single page from flush list. It is fine to
504 skip flush if the page flush is already in progress.
505 @param[in,out] buf_pool buffer pool instance
506 @param[in,out] bpage page to remove
507 @return true if page could be removed successfully. */
508 static bool remove_page_flush_list(buf_pool_t *buf_pool, buf_page_t *bpage) {
509 ut_ad(mutex_own(&buf_pool->LRU_list_mutex));
510 ut_ad(buf_flush_list_mutex_own(buf_pool));
511
512 /* It is safe to check bpage->space and bpage->io_fix while holding
513 buf_pool->LRU_list_mutex only. We will repeat the check of io_fix
514 under block_mutex later, this is just an optimization to avoid the
515 mutex acquisition if its likely io_fix is not NONE. */
516 if (bpage->was_io_fixed()) {
517 /* We cannot remove this page during this scan. */
518 return false;
519 }
520 BPageMutex *block_mutex = buf_page_get_mutex(bpage);
521
522 /* We don't have to worry about bpage becoming a dangling pointer by a
523 compressed page flush list relocation because we hold LRU mutex. */
524 buf_flush_list_mutex_exit(buf_pool);
525 mutex_enter(block_mutex);
526
527 bool removed = false;
528 /* Recheck the page I/O fix and the flush list presence now that we hold
529 the right mutex. */
530 if (buf_page_get_io_fix(bpage) == BUF_IO_NONE && bpage->is_dirty()) {
531 buf_flush_remove(bpage);
532 removed = true;
533 }
534
535 mutex_exit(block_mutex);
536 buf_flush_list_mutex_enter(buf_pool);
537
538 ut_ad(mutex_own(&buf_pool->LRU_list_mutex));
539 return removed;
540 }
541
542 /** Remove all dirty pages belonging to a given tablespace inside a specific
543 buffer pool instance. The pages still remain a part of LRU and are evicted from
544 the list as they age towards the tail of the LRU. We don't check for interrupt
545 as we must finish the operation. Usually this function is called as a cleanup
546 work after an interrupt is received.
547 @param[in,out] buf_pool buffer pool instance
548 @param[in] id space id for which to remove or flush pages
549 @param[in] observer flush observer to identify specific pages
550 @retval DB_SUCCESS if all freed
551 @retval DB_FAIL if not all freed and caller should call function again. */
552 [[nodiscard]] static dberr_t remove_pages_flush_list(buf_pool_t *buf_pool,
553 space_id_t id,
554 Flush_observer *observer) {
555 ut_ad(mutex_own(&buf_pool->LRU_list_mutex));
556
557 buf_flush_list_mutex_enter(buf_pool);
558
559 buf_page_t *prev = nullptr;
560 size_t processed = 0;
561 dberr_t error = DB_SUCCESS;
562
563 for (buf_page_t *bpage = UT_LIST_GET_LAST(buf_pool->flush_list);
564 bpage != nullptr; bpage = prev) {
565 ut_a(buf_page_in_file(bpage));
566
567 /* Save the previous page before freeing the current one. */
568 prev = UT_LIST_GET_PREV(list, bpage);
569
570 if (check_page_flush_observer(bpage, observer, id)) {
571 /* Try to PIN the previous page before removing. This would let us
572 continue the current iteration after removal. */
573 bool pinned = buf_page_try_pin(buf_pool, prev);
574
575 /* Try to remove the current page even if pin failed. */
576 bool removed = remove_page_flush_list(buf_pool, bpage);
577
578 if (!pinned) {
579 /* We should not trust prev pointer as PIN was unsuccessful. */
580 error = DB_FAIL;
581 break;
582 }
583
584 if (!removed) {
585 /* Currently we come back and re-check. The iteration can continue.
586 In future, it is also possible to wait for the concurrent IO to complete
587 to avoid re-scanning. */
588 error = DB_FAIL;
589 }
590
591 buf_page_unpin(buf_pool, prev);
592 }
593
594 ++processed;
595
596 /* Yield if we have hogged the CPU and mutexes for too long. */
597 bool restart = false;
598 if (buf_flush_try_yield(buf_pool, prev, processed, restart)) {
599 ut_ad(!restart);
600 /* Reset the batch size counter if we had to yield. */
601 processed = 0;
602 }
603
604 if (restart) {
605 /* The previous page is already flushed or being flushed. We need at least
606 another iteration. Current iteration can continue. */
607 error = DB_FAIL;
608 }
609 }
610
611 buf_flush_list_mutex_exit(buf_pool);
612 return error;
613 }
614
615 /** Flushes a single page inside a buffer pool instance.
616 @param[in,out] buf_pool buffer pool instance
617 @param[in,out] bpage page to flush
618 @return true if page was flushed. */
619 static bool flush_page_flush_list(buf_pool_t *buf_pool, buf_page_t *bpage) {
620 ut_ad(mutex_own(&buf_pool->LRU_list_mutex));
621 ut_ad(buf_flush_list_mutex_own(buf_pool));
622
623 if (bpage->was_io_fixed()) {
624 return false;
625 }
626
627 BPageMutex *block_mutex = buf_page_get_mutex(bpage);
628
629 /* We don't have to worry about bpage becoming a dangling pointer by a
630 compressed page flush list relocation because we hold LRU mutex. */
631 buf_flush_list_mutex_exit(buf_pool);
632
633 mutex_enter(block_mutex);
634 bool flushed = false;
635
636 if (buf_flush_ready_for_flush(bpage, BUF_FLUSH_SINGLE_PAGE)) {
637 /* We trigger single page flush and async IO. However, if double write is
638 used, dblwr::write() forces all single page flush to sync IO.
639 1. It makes the function behaviour change from sync to async for temp
640 tablespaces and if redo is disabled. The caller must not assume
641 the page is flushed when we return flushed = T.
642 2. For bulk flush async trigger could be better for performance and seems
643 to be the case in 5.7. Need to validate if 8.0 forcing sync flush
644 is intentional - No functional impact. */
645 flushed = buf_flush_page(buf_pool, bpage, BUF_FLUSH_SINGLE_PAGE, false);
646 }
647
648 if (flushed) {
649 /* During flush, we have already released the LRU list and block mutexes.
650 Wake up possible simulated aio thread to actually post the writes to the
651 operating system */
652 os_aio_simulated_wake_handler_threads();
653 mutex_enter(&buf_pool->LRU_list_mutex);
654 } else {
655 mutex_exit(block_mutex);
656 }
657
658 buf_flush_list_mutex_enter(buf_pool);
659
660 ut_ad(!mutex_own(block_mutex));
661 ut_ad(mutex_own(&buf_pool->LRU_list_mutex));
662
663 return flushed;
664 }
665
666 /** Remove all dirty pages belonging to a given tablespace inside a specific
667 buffer pool instance when we are deleting the data file(s) of that
668 tablespace. The pages still remain a part of LRU and are evicted from
669 the list as they age towards the tail of the LRU.
670 @param[in,out] buf_pool buffer pool instance
671 @param[in] id space id for which to remove or flush pages
672 @param[in] observer flush observer
673 @param[in] trx transaction to check if the operation must be
674 interrupted, can be NULL
675 @retval DB_SUCCESS if all freed
676 @retval DB_FAIL if not all freed
677 @retval DB_INTERRUPTED if the transaction was interrupted */
678 [[nodiscard]] static dberr_t flush_pages_flush_list(buf_pool_t *buf_pool,
679 space_id_t id,
680 Flush_observer *observer,
681 const trx_t *trx) {
682 ut_ad(mutex_own(&buf_pool->LRU_list_mutex));
683
684 buf_flush_list_mutex_enter(buf_pool);
685
686 buf_page_t *prev = nullptr;
687 size_t processed = 0;
688
689 dberr_t error = DB_SUCCESS;
690
691 for (buf_page_t *bpage = UT_LIST_GET_LAST(buf_pool->flush_list);
692 bpage != nullptr; bpage = prev) {
693 ut_a(buf_page_in_file(bpage));
694
695 /* Save the previous page before flushing the current one. */
696 prev = UT_LIST_GET_PREV(list, bpage);
697
698 if (check_page_flush_observer(bpage, observer, id)) {
699 /* Try to PIN the previous page before flushing. This would let us
700 continue the current iteration after flush. */
701 bool pinned = buf_page_try_pin(buf_pool, prev);
702
703 /* Try to flush the current page even if pin failed. */
704 flush_page_flush_list(buf_pool, bpage);
705
706 /* Currently we come back and re-check once flush is triggered. If the
707 flush is unsuccessful we need to rescan too. So, we set the error for
708 rescan unconditionally here. The iteration can continue if PIN was
709 successful. */
710 error = DB_FAIL;
711
712 if (!pinned) {
713 /* We should not trust prev pointer as PIN was unsuccessful. */
714 break;
715 }
716
717 buf_page_unpin(buf_pool, prev);
718 }
719
720 ++processed;
721
722 /* Yield if we have hogged the CPU and mutexes for too long. */
723 bool restart = false;
724 if (buf_flush_try_yield(buf_pool, prev, processed, restart)) {
725 ut_ad(!restart);
726 /* Reset the batch size counter if we had to yield. */
727 processed = 0;
728 }
729
730 if (restart) {
731 /* The previous page is already flushed or being flushed. We need at least
732 another iteration. Current iteration can continue. */
733 error = DB_FAIL;
734 }
735
736 /* The check for trx is interrupted is expensive, we want to check every
737 N iterations. */
738 if (processed == 0 && trx && trx_is_interrupted(trx)) {
739 if (trx->flush_observer != nullptr) {
740 trx->flush_observer->interrupted();
741 }
742 error = DB_INTERRUPTED;
743 break;
744 }
745 }
746
747 buf_flush_list_mutex_exit(buf_pool);
748 return error;
749 }
750
751 /** Remove or flush all the dirty pages that belong to a given tablespace
752 inside a specific buffer pool instance. The pages will remain in the LRU
753 list and will be evicted from the LRU list as they age and move towards
754 the tail of the LRU list.
755 @param[in,out] buf_pool buffer pool instance
756 @param[in] id space id
757 @param[in] observer flush observer
758 @param[in] flush flush to disk if true, otherwise remove
759 the pages without flushing
760 @param[in] trx transaction to check if the operation
761 must be interrupted
762 @param[in] strict true, if no page from tablespace
763 can be in buffer pool just after flush */
764 static void buf_flush_dirty_pages(buf_pool_t *buf_pool, space_id_t id,
765 Flush_observer *observer, bool flush,
766 const trx_t *trx, bool strict) {
767 dberr_t err;
768
769 do {
770 /* TODO: it should be possible to avoid locking the LRU list
771 mutex here. */
772 mutex_enter(&buf_pool->LRU_list_mutex);
773
774 if (flush) {
775 err = flush_pages_flush_list(buf_pool, id, observer, trx);
776
777 } else {
778 err = remove_pages_flush_list(buf_pool, id, observer);
779 }
780
781 mutex_exit(&buf_pool->LRU_list_mutex);
782
783 ut_ad(buf_flush_validate(buf_pool));
784
785 if (err == DB_FAIL) {
786 std::this_thread::sleep_for(std::chrono::milliseconds(2));
787 }
788
789 if (err == DB_INTERRUPTED && observer != nullptr) {
790 ut_a(flush);
791
792 flush = false;
793 err = DB_FAIL;
794 }
795
796 /* DB_FAIL is a soft error, it means that the task wasn't
797 completed, needs to be retried. */
798
799 ut_ad(buf_flush_validate(buf_pool));
800
801 } while (err == DB_FAIL);
802
803 ut_ad(observer != nullptr || err == DB_INTERRUPTED || !strict ||
804 buf_pool_get_dirty_pages_count(buf_pool, id, observer) == 0);
805 }
806
807 /** Remove all pages that belong to a given tablespace inside a specific
808 buffer pool instance when we are DISCARDing the tablespace.
809 @param[in,out] buf_pool buffer pool instance
810 @param[in] id space id */
811 static void buf_LRU_remove_all_pages(buf_pool_t *buf_pool, ulint id) {
812 buf_page_t *bpage;
813
814 scan_again:
815 mutex_enter(&buf_pool->LRU_list_mutex);
816
817 auto all_freed = true;
818
819 for (bpage = UT_LIST_GET_LAST(buf_pool->LRU); bpage != nullptr;
820 /* No op */) {
821 rw_lock_t *hash_lock;
822 buf_page_t *prev_bpage;
823 BPageMutex *block_mutex;
824
825 ut_a(buf_page_in_file(bpage));
826 ut_ad(bpage->in_LRU_list);
827
828 prev_bpage = UT_LIST_GET_PREV(LRU, bpage);
829
830 /* It is safe to check bpage->id.space() and bpage->io_fix
831 while holding buf_pool->LRU_list_mutex only and later recheck
832 while holding the buf_page_get_mutex() mutex. */
833
834 if (bpage->id.space() != id) {
835 /* Skip this block, as it does not belong to
836 the space that is being invalidated. */
837 goto next_page;
838 } else if (bpage->was_io_fixed()) {
839 /* We cannot remove this page during this scan
840 yet; maybe the system is currently reading it
841 in, or flushing the modifications to the file */
842
843 all_freed = false;
844 goto next_page;
845 } else {
846 hash_lock = buf_page_hash_lock_get(buf_pool, bpage->id);
847
848 rw_lock_x_lock(hash_lock, UT_LOCATION_HERE);
849
850 block_mutex = buf_page_get_mutex(bpage);
851
852 mutex_enter(block_mutex);
853
854 if (bpage->id.space() != id || bpage->buf_fix_count > 0 ||
855 (buf_page_get_io_fix(bpage) != BUF_IO_NONE)) {
856 mutex_exit(block_mutex);
857
858 rw_lock_x_unlock(hash_lock);
859
860 /* We cannot remove this page during
861 this scan yet; maybe the system is
862 currently reading it in, or flushing
863 the modifications to the file */
864
865 all_freed = false;
866
867 goto next_page;
868 }
869 }
870
871 ut_ad(mutex_own(block_mutex));
872
873 DBUG_PRINT("ib_buf", ("evict page " UINT32PF ":" UINT32PF " state %u",
874 bpage->id.space(), bpage->id.page_no(),
875 static_cast<unsigned>(bpage->state)));
876
877 if (buf_page_get_state(bpage) != BUF_BLOCK_FILE_PAGE) {
878 /* Do nothing, because the adaptive hash index
879 covers uncompressed pages only. */
880 } else if (((buf_block_t *)bpage)->ahi.index) {
881 mutex_exit(&buf_pool->LRU_list_mutex);
882
883 rw_lock_x_unlock(hash_lock);
884
885 mutex_exit(block_mutex);
886
887 /* Note that the following call will acquire
888 and release block->lock X-latch.
889 Note that the table cannot be evicted during
890 the execution of ALTER TABLE...DISCARD TABLESPACE
891 because MySQL is keeping the table handle open. */
892
893 btr_search_drop_page_hash_when_freed(bpage->id, bpage->size);
894
895 goto scan_again;
896 } else {
897 reinterpret_cast<buf_block_t *>(bpage)->ahi.assert_empty();
898 }
899
900 if (bpage->is_dirty()) {
901 buf_flush_remove(bpage);
902 }
903
904 ut_ad(!bpage->in_flush_list);
905
906 /* Remove from the LRU list. */
907
908 if (buf_LRU_block_remove_hashed(bpage, true, false)) {
909 buf_LRU_block_free_hashed_page((buf_block_t *)bpage);
910 } else {
911 ut_ad(block_mutex == &buf_pool->zip_mutex);
912 }
913
914 ut_ad(!mutex_own(block_mutex));
915
916 /* buf_LRU_block_remove_hashed() releases the hash_lock */
917 ut_ad(!rw_lock_own(hash_lock, RW_LOCK_X));
918 ut_ad(!rw_lock_own(hash_lock, RW_LOCK_S));
919
920 next_page:
921 bpage = prev_bpage;
922 }
923
924 mutex_exit(&buf_pool->LRU_list_mutex);
925
926 if (!all_freed) {
927 std::this_thread::sleep_for(std::chrono::milliseconds(20));
928
929 goto scan_again;
930 }
931 }
932
933 /** Remove pages belonging to a given tablespace inside a specific
934 buffer pool instance when we are deleting the data file(s) of that
935 tablespace. The pages still remain a part of LRU and are evicted from
936 the list as they age towards the tail of the LRU only if buf_remove
937 is BUF_REMOVE_FLUSH_NO_WRITE. */
938 static void buf_LRU_remove_pages(
939 buf_pool_t *buf_pool, /*!< buffer pool instance */
940 space_id_t id, /*!< in: space id */
941 buf_remove_t buf_remove, /*!< in: remove or flush strategy */
942 const trx_t *trx, /*!< to check if the operation must
943 be interrupted */
944 bool strict) /*!< in: true if no page from tablespace
945 can be in buffer pool just after flush */
946 {
947 Flush_observer *observer = (trx == nullptr) ? nullptr : trx->flush_observer;
948
949 switch (buf_remove) {
950 case BUF_REMOVE_ALL_NO_WRITE:
951 buf_LRU_remove_all_pages(buf_pool, id);
952 break;
953
954 case BUF_REMOVE_FLUSH_NO_WRITE:
955 /* Pass trx as NULL to avoid interruption check. */
956 buf_flush_dirty_pages(buf_pool, id, observer, false, nullptr, strict);
957 break;
958
959 case BUF_REMOVE_FLUSH_WRITE:
960 buf_flush_dirty_pages(buf_pool, id, observer, true, trx, strict);
961
962 if (observer == nullptr) {
963 /* Ensure that all asynchronous IO is completed. */
964 os_aio_wait_until_no_pending_writes();
965 fil_flush(id);
966 }
967 break;
968
969 case BUF_REMOVE_NONE:
970 ut_error;
971 break;
972 }
973 }
974
975 void buf_LRU_flush_or_remove_pages(space_id_t id, buf_remove_t buf_remove,
976 const trx_t *trx, bool strict) {
977 /* Before we attempt to drop pages one by one we first
978 attempt to drop page hash index entries in batches to make
979 it more efficient. The batching attempt is a best effort
980 attempt and does not guarantee that all pages hash entries
981 will be dropped. We get rid of remaining page hash entries
982 one by one below. */
983 for (ulint i = 0; i < srv_buf_pool_instances; i++) {
984 auto buf_pool = buf_pool_from_array(i);
985
986 switch (buf_remove) {
987 case BUF_REMOVE_ALL_NO_WRITE:
988 buf_LRU_drop_page_hash_for_tablespace(buf_pool, id);
989 break;
990
991 case BUF_REMOVE_FLUSH_NO_WRITE:
992 /* It is a DROP TABLE for a single table
993 tablespace. No AHI entries exist because
994 we already dealt with them when freeing up
995 extents. */
996 case BUF_REMOVE_FLUSH_WRITE:
997 /* We allow read-only queries against the
998 table, there is no need to drop the AHI entries. */
999 break;
1000
1001 case BUF_REMOVE_NONE:
1002 ut_error;
1003 break;
1004 }
1005
1006 buf_LRU_remove_pages(buf_pool, id, buf_remove, trx, strict);
1007 }
1008 }
1009
1010 #if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
1011 /** Insert a compressed block into buf_pool->zip_clean in the LRU order.
1012 @param[in] bpage pointer to the block in question */
1013 void buf_LRU_insert_zip_clean(buf_page_t *bpage) {
1014 buf_pool_t *buf_pool = buf_pool_from_bpage(bpage);
1015
1016 ut_ad(mutex_own(&buf_pool->LRU_list_mutex));
1017 ut_ad(mutex_own(&buf_pool->zip_mutex));
1018 ut_ad(buf_page_get_state(bpage) == BUF_BLOCK_ZIP_PAGE);
1019
1020 /* Find the first successor of bpage in the LRU list
1021 that is in the zip_clean list. */
1022 buf_page_t *b = bpage;
1023
1024 do {
1025 b = UT_LIST_GET_NEXT(LRU, b);
1026 } while (b && buf_page_get_state(b) != BUF_BLOCK_ZIP_PAGE);
1027
1028 /* Insert bpage before b, i.e., after the predecessor of b. */
1029 if (b != nullptr) {
1030 b = UT_LIST_GET_PREV(list, b);
1031 }
1032
1033 if (b != nullptr) {
1034 UT_LIST_INSERT_AFTER(buf_pool->zip_clean, b, bpage);
1035 } else {
1036 UT_LIST_ADD_FIRST(buf_pool->zip_clean, bpage);
1037 }
1038 }
1039 #endif /* UNIV_DEBUG || UNIV_BUF_DEBUG */
1040
1041 /** Try to free an uncompressed page of a compressed block from the unzip
1042 LRU list. The compressed page is preserved, and it need not be clean.
1043 @param[in] buf_pool buffer pool instance
1044 @param[in] scan_all scan whole LRU list if true, otherwise
1045 scan only srv_LRU_scan_depth / 2 blocks
1046 @return true if freed */
1047 static bool buf_LRU_free_from_unzip_LRU_list(buf_pool_t *buf_pool,
1048 bool scan_all) {
1049 ut_ad(mutex_own(&buf_pool->LRU_list_mutex));
1050
1051 if (!buf_LRU_evict_from_unzip_LRU(buf_pool)) {
1052 return (false);
1053 }
1054
1055 ulint scanned = 0;
1056 bool freed = false;
1057
1058 for (buf_block_t *block = UT_LIST_GET_LAST(buf_pool->unzip_LRU);
1059 block != nullptr && !freed && (scan_all || scanned < srv_LRU_scan_depth);
1060 ++scanned) {
1061 buf_block_t *prev_block;
1062
1063 prev_block = UT_LIST_GET_PREV(unzip_LRU, block);
1064
1065 mutex_enter(&block->mutex);
1066
1067 ut_ad(buf_block_get_state(block) == BUF_BLOCK_FILE_PAGE);
1068 ut_ad(block->in_unzip_LRU_list);
1069 ut_ad(block->page.in_LRU_list);
1070
1071 freed = buf_LRU_free_page(&block->page, false);
1072
1073 if (!freed) {
1074 mutex_exit(&block->mutex);
1075 }
1076
1077 block = prev_block;
1078 }
1079
1080 if (scanned) {
1081 MONITOR_INC_VALUE_CUMULATIVE(MONITOR_LRU_UNZIP_SEARCH_SCANNED,
1082 MONITOR_LRU_UNZIP_SEARCH_SCANNED_NUM_CALL,
1083 MONITOR_LRU_UNZIP_SEARCH_SCANNED_PER_CALL,
1084 scanned);
1085 }
1086
1087 return (freed);
1088 }
1089
1090 /** Try to free a clean page from the common LRU list.
1091 @param[in,out] buf_pool buffer pool instance
1092 @param[in] scan_all scan whole LRU list if true, otherwise scan
1093 only up to BUF_LRU_SEARCH_SCAN_THRESHOLD
1094 @return true if freed */
1095 static bool buf_LRU_free_from_common_LRU_list(buf_pool_t *buf_pool,
1096 bool scan_all) {
1097 ut_ad(mutex_own(&buf_pool->LRU_list_mutex));
1098
1099 bool freed{};
1100 ulint scanned{};
1101
1102 for (buf_page_t *bpage = buf_pool->lru_scan_itr.start();
1103 bpage != nullptr && !freed &&
1104 (scan_all || scanned < BUF_LRU_SEARCH_SCAN_THRESHOLD);
1105 ++scanned, bpage = buf_pool->lru_scan_itr.get()) {
1106 ut_ad(mutex_own(&buf_pool->LRU_list_mutex));
1107 auto prev = UT_LIST_GET_PREV(LRU, bpage);
1108 auto block_mutex = buf_page_get_mutex(bpage);
1109
1110 buf_pool->lru_scan_itr.set(prev);
1111
1112 ut_ad(bpage->in_LRU_list);
1113 ut_ad(buf_page_in_file(bpage));
1114
1115 const auto accessed = buf_page_is_accessed(bpage);
1116
1117 if (bpage->was_stale()) {
1118 freed = buf_page_free_stale(buf_pool, bpage);
1119 } else {
1120 mutex_enter(block_mutex);
1121
1122 if (buf_flush_ready_for_replace(bpage)) {
1123 freed = buf_LRU_free_page(bpage, true);
1124 }
1125
1126 if (!freed) {
1127 mutex_exit(block_mutex);
1128 }
1129 }
1130
1131 if (freed && accessed == std::chrono::steady_clock::time_point{}) {
1132 /* Keep track of pages that are evicted without
1133 ever being accessed. This gives us a measure of
1134 the effectiveness of readahead */
1135 ++buf_pool->stat.n_ra_pages_evicted;
1136 }
1137
1138 ut_ad(!mutex_own(block_mutex));
1139
1140 if (freed) {
1141 break;
1142 }
1143 }
1144
1145 if (scanned) {
1146 MONITOR_INC_VALUE_CUMULATIVE(MONITOR_LRU_SEARCH_SCANNED,
1147 MONITOR_LRU_SEARCH_SCANNED_NUM_CALL,
1148 MONITOR_LRU_SEARCH_SCANNED_PER_CALL, scanned);
1149 }
1150
1151 ut_ad(freed ? !mutex_own(&buf_pool->LRU_list_mutex)
1152 : mutex_own(&buf_pool->LRU_list_mutex));
1153
1154 return (freed);
1155 }
1156
1157 bool buf_LRU_scan_and_free_block(buf_pool_t *buf_pool, bool scan_all) {
1158 bool freed = false;
1159 bool use_unzip_list = UT_LIST_GET_LEN(buf_pool->unzip_LRU) > 0;
1160
1161 mutex_enter(&buf_pool->LRU_list_mutex);
1162
1163 if (use_unzip_list) {
1164 freed = buf_LRU_free_from_unzip_LRU_list(buf_pool, scan_all);
1165 }
1166
1167 if (!freed) {
1168 freed = buf_LRU_free_from_common_LRU_list(buf_pool, scan_all);
1169 }
1170
1171 if (!freed) {
1172 mutex_exit(&buf_pool->LRU_list_mutex);
1173 }
1174
1175 ut_ad(!mutex_own(&buf_pool->LRU_list_mutex));
1176
1177 return (freed);
1178 }
1179
1180 /** Returns true if less than 25 % of the buffer pool in any instance is
1181 available. This can be used in heuristics to prevent huge transactions
1182 eating up the whole buffer pool for their locks.
1183 @return true if less than 25 % of buffer pool left */
1184 bool buf_LRU_buf_pool_running_out(void) {
1185 bool ret = false;
1186
1187 for (ulint i = 0; i < srv_buf_pool_instances && !ret; i++) {
1188 buf_pool_t *buf_pool;
1189
1190 buf_pool = buf_pool_from_array(i);
1191
1192 if (!recv_recovery_is_on() &&
1193 UT_LIST_GET_LEN(buf_pool->free) + UT_LIST_GET_LEN(buf_pool->LRU) <
1194 std::min(buf_pool->curr_size, buf_pool->old_size) / 4) {
1195 ret = true;
1196 }
1197 }
1198
1199 return ret;
1200 }
1201
1202 /** Returns a free block from the buf_pool.
1203 The block is taken off the free list. If it is empty, returns NULL.
1204 @param[in] buf_pool buffer pool instance
1205 @return a free control block, or NULL if the buf_block->free list is empty */
1206 buf_block_t *buf_LRU_get_free_only(buf_pool_t *buf_pool) {
1207 buf_block_t *block;
1208
1209 mutex_enter(&buf_pool->free_list_mutex);
1210
1211 block = reinterpret_cast<buf_block_t *>(UT_LIST_GET_FIRST(buf_pool->free));
1212
1213 while (block != nullptr) {
1214 ut_ad(block->page.in_free_list);
1215 ut_d(block->page.in_free_list = false);
1216 ut_ad(!block->page.in_flush_list);
1217 ut_ad(!block->page.in_LRU_list);
1218 ut_a(!buf_page_in_file(&block->page));
1219 UT_LIST_REMOVE(buf_pool->free, &block->page);
1220 mutex_exit(&buf_pool->free_list_mutex);
1221
1222 if (!buf_get_withdraw_depth(buf_pool) ||
1223 !buf_block_will_withdrawn(buf_pool, block)) {
1224 /* found valid free block */
1225 /* No adaptive hash index entries may point to
1226 a free block. */
1227 block->ahi.assert_empty();
1228
1229 buf_block_set_state(block, BUF_BLOCK_READY_FOR_USE);
1230
1231 UNIV_MEM_ALLOC(block->frame, UNIV_PAGE_SIZE);
1232
1233 ut_ad(buf_pool_from_block(block) == buf_pool);
1234
1235 return (block);
1236 }
1237
1238 /* This should be withdrawn */
1239 mutex_enter(&buf_pool->free_list_mutex);
1240 UT_LIST_ADD_LAST(buf_pool->withdraw, &block->page);
1241 ut_d(block->in_withdraw_list = true);
1242
1243 block = reinterpret_cast<buf_block_t *>(UT_LIST_GET_FIRST(buf_pool->free));
1244 }
1245
1246 mutex_exit(&buf_pool->free_list_mutex);
1247
1248 return (block);
1249 }
1250
1251 /** Checks how much of buf_pool is occupied by non-data objects like
1252 AHI, lock heaps etc. Depending on the size of non-data objects this
1253 function will either assert or issue a warning and switch on the
1254 status monitor. */
1255 static void buf_LRU_check_size_of_non_data_objects(
1256 const buf_pool_t *buf_pool) /*!< in: buffer pool instance */
1257 {
1258 if (!recv_recovery_is_on() && buf_pool->curr_size == buf_pool->old_size &&
1259 UT_LIST_GET_LEN(buf_pool->free) + UT_LIST_GET_LEN(buf_pool->LRU) <
1260 buf_pool->curr_size / 20) {
1261 ib::fatal(UT_LOCATION_HERE, ER_IB_MSG_132)
1262 << "Over 95 percent of the buffer pool is"
1263 " occupied by lock heaps or the adaptive hash index!"
1264 " Check that your transactions do not set too many"
1265 " row locks. Your buffer pool size is "
1266 << (buf_pool->curr_size / (1024 * 1024 / UNIV_PAGE_SIZE))
1267 << " MB."
1268 " Maybe you should make the buffer pool bigger?"
1269 " We intentionally generate a seg fault to print"
1270 " a stack trace on Linux!";
1271
1272 } else if (!recv_recovery_is_on() &&
1273 buf_pool->curr_size == buf_pool->old_size &&
1274 (UT_LIST_GET_LEN(buf_pool->free) +
1275 UT_LIST_GET_LEN(buf_pool->LRU)) < buf_pool->curr_size / 3) {
1276 if (!buf_lru_switched_on_innodb_mon.exchange(true)) {
1277 /* Over 67 % of the buffer pool is occupied by lock
1278 heaps or the adaptive hash index. This may be a memory
1279 leak! */
1280
1281 ib::warn(ER_IB_MSG_133)
1282 << "Over 67 percent of the buffer pool is"
1283 " occupied by lock heaps or the adaptive hash"
1284 " index! Check that your transactions do not"
1285 " set too many row locks. Your buffer pool"
1286 " size is "
1287 << (buf_pool->curr_size / (1024 * 1024 / UNIV_PAGE_SIZE))
1288 << " MB. Maybe you should make the buffer pool"
1289 " bigger?. Starting the InnoDB Monitor to print"
1290 " diagnostics, including lock heap and hash"
1291 " index sizes.";
1292
1293 srv_innodb_needs_monitoring++;
1294 }
1295
1296 } else if (buf_lru_switched_on_innodb_mon.load()) {
1297 if (buf_lru_switched_on_innodb_mon.exchange(false)) {
1298 srv_innodb_needs_monitoring--;
1299 }
1300 }
1301 }
1302
1303 /** Returns a free block from the buf_pool. The block is taken off the
1304 free list. If free list is empty, blocks are moved from the end of the
1305 LRU list to the free list.
1306 This function is called from a user thread when it needs a clean
1307 block to read in a page. Note that we only ever get a block from
1308 the free list. Even when we flush a page or find a page in LRU scan
1309 we put it to free list to be used.
1310 * iteration 0:
1311 * get a block from free list, success:done
1312 * if buf_pool->try_LRU_scan is set
1313 * scan LRU up to srv_LRU_scan_depth to find a clean block
1314 * the above will put the block on free list
1315 * success:retry the free list
1316 * flush one dirty page from tail of LRU to disk
1317 * the above will put the block on free list
1318 * success: retry the free list
1319 * iteration 1:
1320 * same as iteration 0 except:
1321 * scan whole LRU list
1322 * scan LRU list even if buf_pool->try_LRU_scan is not set
1323 * iteration > 1:
1324 * same as iteration 1 but sleep 10ms
1325 @param[in,out] buf_pool buffer pool instance
1326 @return the free control block, in state BUF_BLOCK_READY_FOR_USE */
1327 buf_block_t *buf_LRU_get_free_block(buf_pool_t *buf_pool) {
1328 buf_block_t *block = nullptr;
1329 bool freed = false;
1330 ulint n_iterations = 0;
1331 ulint flush_failures = 0;
1332 bool started_monitor = false;
1333
1334 ut_ad(!mutex_own(&buf_pool->LRU_list_mutex));
1335
1336 MONITOR_INC(MONITOR_LRU_GET_FREE_SEARCH);
1337 loop:
1338 buf_LRU_check_size_of_non_data_objects(buf_pool);
1339
1340 /* If there is a block in the free list, take it */
1341 block = buf_LRU_get_free_only(buf_pool);
1342
1343 if (block != nullptr) {
1344 ut_ad(!block->page.someone_has_io_responsibility());
1345 ut_ad(buf_pool_from_block(block) == buf_pool);
1346 memset(&block->page.zip, 0, sizeof block->page.zip);
1347
1348 if (started_monitor) {
1349 srv_innodb_needs_monitoring--;
1350 }
1351
1352 block->page.reset_flush_observer();
1353 return block;
1354 }
1355
1356 MONITOR_INC(MONITOR_LRU_GET_FREE_LOOPS);
1357
1358 freed = false;
1359 os_rmb;
1360 if (buf_pool->try_LRU_scan || n_iterations > 0) {
1361 /* If no block was in the free list, search from the
1362 end of the LRU list and try to free a block there.
1363 If we are doing for the first time we'll scan only
1364 tail of the LRU list otherwise we scan the whole LRU
1365 list. */
1366 freed = buf_LRU_scan_and_free_block(buf_pool, n_iterations > 0);
1367
1368 if (!freed && n_iterations == 0) {
1369 /* Tell other threads that there is no point
1370 in scanning the LRU list. This flag is set to
1371 true again when we flush a batch from this
1372 buffer pool. */
1373 buf_pool->try_LRU_scan = false;
1374 os_wmb;
1375 }
1376 }
1377
1378 if (freed) {
1379 goto loop;
1380 }
1381
1382 if (n_iterations > 20 && srv_buf_pool_old_size == srv_buf_pool_size) {
1383 ib::warn(ER_IB_MSG_134)
1384 << "Difficult to find free blocks in the buffer pool"
1385 " ("
1386 << n_iterations << " search iterations)! " << flush_failures
1387 << " failed attempts to"
1388 " flush a page! Consider increasing the buffer pool"
1389 " size. It is also possible that in your Unix version"
1390 " fsync is very slow, or completely frozen inside"
1391 " the OS kernel. Then upgrading to a newer version"
1392 " of your operating system may help. Look at the"
1393 " number of fsyncs in diagnostic info below."
1394 " Pending flushes (fsync) log: "
1395 << log_pending_flushes()
1396 << "; buffer pool: " << fil_n_pending_tablespace_flushes << ". "
1397 << os_n_file_reads << " OS file reads, " << os_n_file_writes
1398 << " OS file writes, " << os_n_fsyncs
1399 << " OS fsyncs. Starting InnoDB Monitor to print"
1400 " further diagnostics to the standard output.";
1401 if (!started_monitor) {
1402 started_monitor = true;
1403 srv_innodb_needs_monitoring++;
1404 }
1405 }
1406
1407 /* If we have scanned the whole LRU and still are unable to
1408 find a free block then we should sleep here to let the
1409 page_cleaner do an LRU batch for us. */
1410
1411 if (!srv_read_only_mode) {
1412 os_event_set(buf_flush_event);
1413 }
1414
1415 if (n_iterations > 1) {
1416 MONITOR_INC(MONITOR_LRU_GET_FREE_WAITS);
1417 std::this_thread::sleep_for(std::chrono::milliseconds(10));
1418 }
1419
1420 /* No free block was found: try to flush the LRU list.
1421 This call will flush one page from the LRU and put it on the
1422 free list. That means that the free block is up for grabs for
1423 all user threads.
1424
1425 TODO: A more elegant way would have been to return the freed
1426 up block to the caller here but the code that deals with
1427 removing the block from page_hash and LRU_list is fairly
1428 involved (particularly in case of compressed pages). We
1429 can do that in a separate patch sometime in future. */
1430
1431 if (!buf_flush_single_page_from_LRU(buf_pool)) {
1432 MONITOR_INC(MONITOR_LRU_SINGLE_FLUSH_FAILURE_COUNT);
1433 ++flush_failures;
1434 }
1435
1436 srv_stats.buf_pool_wait_free.add(n_iterations, 1);
1437
1438 n_iterations++;
1439
1440 goto loop;
1441 }
1442
1443 /** Calculates the desired number for the old blocks list.
1444 @param[in] buf_pool buffer pool instance */
1445 static size_t calculate_desired_LRU_old_size(const buf_pool_t *buf_pool) {
1446 return std::min(UT_LIST_GET_LEN(buf_pool->LRU) *
1447 static_cast<size_t>(buf_pool->LRU_old_ratio) /
1448 BUF_LRU_OLD_RATIO_DIV,
1449 UT_LIST_GET_LEN(buf_pool->LRU) -
1450 (BUF_LRU_OLD_TOLERANCE + BUF_LRU_NON_OLD_MIN_LEN));
1451 }
1452
1453 /** Moves the LRU_old pointer so that the length of the old blocks list
1454 is inside the allowed limits.
1455 @param[in] buf_pool buffer pool instance */
1456 static inline void buf_LRU_old_adjust_len(buf_pool_t *buf_pool) {
1457 ulint old_len;
1458 ulint new_len;
1459
1460 ut_a(buf_pool->LRU_old);
1461 ut_ad(mutex_own(&buf_pool->LRU_list_mutex));
1462 ut_ad(buf_pool->LRU_old_ratio >= BUF_LRU_OLD_RATIO_MIN);
1463 ut_ad(buf_pool->LRU_old_ratio <= BUF_LRU_OLD_RATIO_MAX);
1464 static_assert(BUF_LRU_OLD_RATIO_MIN * BUF_LRU_OLD_MIN_LEN >
1465 BUF_LRU_OLD_RATIO_DIV * (BUF_LRU_OLD_TOLERANCE + 5),
1466 "BUF_LRU_OLD_RATIO_MIN * BUF_LRU_OLD_MIN_LEN <= "
1467 "BUF_LRU_OLD_RATIO_DIV * (BUF_LRU_OLD_TOLERANCE + 5)");
1468 #ifdef UNIV_LRU_DEBUG
1469 /* buf_pool->LRU_old must be the first item in the LRU list
1470 whose "old" flag is set. */
1471 ut_a(buf_pool->LRU_old->old);
1472 ut_a(!UT_LIST_GET_PREV(LRU, buf_pool->LRU_old) ||
1473 !UT_LIST_GET_PREV(LRU, buf_pool->LRU_old)->old);
1474 ut_a(!UT_LIST_GET_NEXT(LRU, buf_pool->LRU_old) ||
1475 UT_LIST_GET_NEXT(LRU, buf_pool->LRU_old)->old);
1476 #endif /* UNIV_LRU_DEBUG */
1477
1478 old_len = buf_pool->LRU_old_len;
1479 new_len = calculate_desired_LRU_old_size(buf_pool);
1480
1481 for (;;) {
1482 buf_page_t *LRU_old = buf_pool->LRU_old;
1483
1484 ut_a(LRU_old);
1485 ut_ad(LRU_old->in_LRU_list);
1486 #ifdef UNIV_LRU_DEBUG
1487 ut_a(LRU_old->old);
1488 #endif /* UNIV_LRU_DEBUG */
1489
1490 /* Update the LRU_old pointer if necessary */
1491
1492 if (old_len + BUF_LRU_OLD_TOLERANCE < new_len) {
1493 buf_pool->LRU_old = LRU_old = UT_LIST_GET_PREV(LRU, LRU_old);
1494 #ifdef UNIV_LRU_DEBUG
1495 ut_a(!LRU_old->old);
1496 #endif /* UNIV_LRU_DEBUG */
1497 old_len = ++buf_pool->LRU_old_len;
1498 buf_page_set_old(LRU_old, true);
1499
1500 } else if (old_len > new_len + BUF_LRU_OLD_TOLERANCE) {
1501 buf_pool->LRU_old = UT_LIST_GET_NEXT(LRU, LRU_old);
1502 old_len = --buf_pool->LRU_old_len;
1503 buf_page_set_old(LRU_old, false);
1504 } else {
1505 return;
1506 }
1507 }
1508 }
1509
1510 /** Initializes the old blocks pointer in the LRU list. This function should be
1511 called when the LRU list grows to BUF_LRU_OLD_MIN_LEN length.
1512 @param[in,out] buf_pool buffer pool instance */
1513 static void buf_LRU_old_init(buf_pool_t *buf_pool) {
1514 ut_ad(mutex_own(&buf_pool->LRU_list_mutex));
1515 ut_a(UT_LIST_GET_LEN(buf_pool->LRU) == BUF_LRU_OLD_MIN_LEN);
1516
1517 /* We first initialize all blocks in the LRU list as old and then use
1518 the adjust function to move the LRU_old pointer to the right
1519 position */
1520
1521 for (buf_page_t *bpage = UT_LIST_GET_LAST(buf_pool->LRU); bpage != nullptr;
1522 bpage = UT_LIST_GET_PREV(LRU, bpage)) {
1523 ut_ad(bpage->in_LRU_list);
1524 ut_ad(buf_page_in_file(bpage));
1525
1526 /* This loop temporarily violates the
1527 assertions of buf_page_set_old(). */
1528 bpage->old = true;
1529 }
1530
1531 buf_pool->LRU_old = UT_LIST_GET_FIRST(buf_pool->LRU);
1532 buf_pool->LRU_old_len = UT_LIST_GET_LEN(buf_pool->LRU);
1533
1534 buf_LRU_old_adjust_len(buf_pool);
1535 }
1536
1537 /** Remove a block from the unzip_LRU list if it belonged to the list.
1538 @param[in] bpage control block */
1539 static void buf_unzip_LRU_remove_block_if_needed(buf_page_t *bpage) {
1540 buf_pool_t *buf_pool = buf_pool_from_bpage(bpage);
1541
1542 ut_ad(buf_page_in_file(bpage));
1543 ut_ad(mutex_own(&buf_pool->LRU_list_mutex));
1544
1545 if (buf_page_belongs_to_unzip_LRU(bpage)) {
1546 buf_block_t *block = reinterpret_cast<buf_block_t *>(bpage);
1547
1548 ut_ad(block->in_unzip_LRU_list);
1549 ut_d(block->in_unzip_LRU_list = false);
1550
1551 UT_LIST_REMOVE(buf_pool->unzip_LRU, block);
1552 }
1553 }
1554
1555 /** Adjust LRU hazard pointers if needed.
1556 @param[in] buf_pool Buffer pool instance
1557 @param[in] bpage Control block */
1558 void buf_LRU_adjust_hp(buf_pool_t *buf_pool, const buf_page_t *bpage) {
1559 buf_pool->lru_hp.adjust(bpage);
1560 buf_pool->lru_scan_itr.adjust(bpage);
1561 buf_pool->single_scan_itr.adjust(bpage);
1562 }
1563
1564 /** Removes a block from the LRU list.
1565 @param[in] bpage control block */
1566 static inline void buf_LRU_remove_block(buf_page_t *bpage) {
1567 buf_pool_t *buf_pool = buf_pool_from_bpage(bpage);
1568
1569 ut_ad(mutex_own(&buf_pool->LRU_list_mutex));
1570
1571 ut_a(buf_page_in_file(bpage));
1572
1573 ut_ad(bpage->in_LRU_list);
1574
1575 /* Important that we adjust the hazard pointers before removing
1576 bpage from the LRU list. */
1577 buf_LRU_adjust_hp(buf_pool, bpage);
1578
1579 /* If the LRU_old pointer is defined and points to just this block,
1580 move it backward one step */
1581
1582 if (bpage == buf_pool->LRU_old) {
1583 /* Below: the previous block is guaranteed to exist,
1584 because the LRU_old pointer is only allowed to differ
1585 by BUF_LRU_OLD_TOLERANCE from strict
1586 buf_pool->LRU_old_ratio/BUF_LRU_OLD_RATIO_DIV of the LRU
1587 list length. */
1588 buf_page_t *prev_bpage = UT_LIST_GET_PREV(LRU, bpage);
1589
1590 ut_a(prev_bpage);
1591 #ifdef UNIV_LRU_DEBUG
1592 ut_a(!prev_bpage->old);
1593 #endif /* UNIV_LRU_DEBUG */
1594 buf_pool->LRU_old = prev_bpage;
1595 buf_page_set_old(prev_bpage, true);
1596
1597 buf_pool->LRU_old_len++;
1598 }
1599
1600 /* Remove the block from the LRU list */
1601 UT_LIST_REMOVE(buf_pool->LRU, bpage);
1602 ut_d(bpage->in_LRU_list = false);
1603
1604 buf_pool->stat.LRU_bytes -= bpage->size.physical();
1605
1606 buf_unzip_LRU_remove_block_if_needed(bpage);
1607
1608 /* If the LRU list is so short that LRU_old is not defined,
1609 clear the "old" flags and return */
1610 if (UT_LIST_GET_LEN(buf_pool->LRU) < BUF_LRU_OLD_MIN_LEN) {
1611 for (auto bpage : buf_pool->LRU) {
1612 /* This loop temporarily violates the
1613 assertions of buf_page_set_old(). */
1614 bpage->old = false;
1615 }
1616
1617 buf_pool->LRU_old = nullptr;
1618 buf_pool->LRU_old_len = 0;
1619
1620 return;
1621 }
1622
1623 ut_ad(buf_pool->LRU_old);
1624
1625 /* Update the LRU_old_len field if necessary */
1626 if (buf_page_is_old(bpage)) {
1627 buf_pool->LRU_old_len--;
1628 }
1629
1630 /* Adjust the length of the old block list if necessary */
1631 buf_LRU_old_adjust_len(buf_pool);
1632 }
1633
1634 /** Adds a block to the LRU list of decompressed zip pages.
1635 @param[in] block control block
1636 @param[in] old true if should be put to the end of the list,
1637 else put to the start */
1638 void buf_unzip_LRU_add_block(buf_block_t *block, bool old) {
1639 buf_pool_t *buf_pool = buf_pool_from_block(block);
1640
1641 ut_ad(mutex_own(&buf_pool->LRU_list_mutex));
1642
1643 ut_a(buf_page_belongs_to_unzip_LRU(&block->page));
1644
1645 ut_ad(!block->in_unzip_LRU_list);
1646 ut_d(block->in_unzip_LRU_list = true);
1647
1648 if (old) {
1649 UT_LIST_ADD_LAST(buf_pool->unzip_LRU, block);
1650 } else {
1651 UT_LIST_ADD_FIRST(buf_pool->unzip_LRU, block);
1652 }
1653 }
1654
1655 /** Adds a block to the LRU list. Please make sure that the page_size is
1656 already set when invoking the function, so that we can get correct
1657 page_size from the buffer page when adding a block into LRU
1658 @param[in] bpage control block
1659 @param[in] old true if should be put to the old blocks in the LRU list,
1660 else put to the start; if the LRU list is very short,
1661 the block is added to the start, regardless of this
1662 parameter */
1663 static inline void buf_LRU_add_block_low(buf_page_t *bpage, bool old) {
1664 buf_pool_t *buf_pool = buf_pool_from_bpage(bpage);
1665
1666 ut_ad(mutex_own(&buf_pool->LRU_list_mutex));
1667
1668 ut_a(buf_page_in_file(bpage));
1669 ut_ad(!bpage->in_LRU_list);
1670
1671 if (!old || (UT_LIST_GET_LEN(buf_pool->LRU) < BUF_LRU_OLD_MIN_LEN)) {
1672 UT_LIST_ADD_FIRST(buf_pool->LRU, bpage);
1673
1674 bpage->freed_page_clock = buf_pool->freed_page_clock;
1675 } else {
1676 #ifdef UNIV_LRU_DEBUG
1677 /* buf_pool->LRU_old must be the first item in the LRU list
1678 whose "old" flag is set. */
1679 ut_a(buf_pool->LRU_old->old);
1680 ut_a(!UT_LIST_GET_PREV(LRU, buf_pool->LRU_old) ||
1681 !UT_LIST_GET_PREV(LRU, buf_pool->LRU_old)->old);
1682 ut_a(!UT_LIST_GET_NEXT(LRU, buf_pool->LRU_old) ||
1683 UT_LIST_GET_NEXT(LRU, buf_pool->LRU_old)->old);
1684 #endif /* UNIV_LRU_DEBUG */
1685 UT_LIST_INSERT_AFTER(buf_pool->LRU, buf_pool->LRU_old, bpage);
1686
1687 buf_pool->LRU_old_len++;
1688 }
1689
1690 ut_d(bpage->in_LRU_list = true);
1691
1692 incr_LRU_size_in_bytes(bpage, buf_pool);
1693
1694 if (UT_LIST_GET_LEN(buf_pool->LRU) > BUF_LRU_OLD_MIN_LEN) {
1695 ut_ad(buf_pool->LRU_old);
1696
1697 /* Adjust the length of the old block list if necessary */
1698
1699 buf_page_set_old(bpage, old);
1700 buf_LRU_old_adjust_len(buf_pool);
1701
1702 } else if (UT_LIST_GET_LEN(buf_pool->LRU) == BUF_LRU_OLD_MIN_LEN) {
1703 /* The LRU list is now long enough for LRU_old to become
1704 defined: init it */
1705
1706 buf_LRU_old_init(buf_pool);
1707 } else {
1708 buf_page_set_old(bpage, buf_pool->LRU_old != nullptr);
1709 }
1710
1711 /* If this is a zipped block with decompressed frame as well
1712 then put it on the unzip_LRU list */
1713 if (buf_page_belongs_to_unzip_LRU(bpage)) {
1714 buf_unzip_LRU_add_block((buf_block_t *)bpage, old);
1715 }
1716 }
1717
1718 /** Adds a block to the LRU list. Please make sure that the page_size is
1719 already set when invoking the function, so that we can get correct
1720 page_size from the buffer page when adding a block into LRU */
1721 void buf_LRU_add_block(buf_page_t *bpage, /*!< in: control block */
1722 bool old) /*!< in: true if should be put to the old
1723 blocks in the LRU list, else put to the start;
1724 if the LRU list is very short, the block is
1725 added to the start, regardless of this
1726 parameter */
1727 {
1728 buf_LRU_add_block_low(bpage, old);
1729 }
1730
1731 /** Moves a block to the start of the LRU list.
1732 @param[in] bpage control block */
1733 void buf_LRU_make_block_young(buf_page_t *bpage) {
1734 buf_pool_t *buf_pool = buf_pool_from_bpage(bpage);
1735
1736 ut_ad(mutex_own(&buf_pool->LRU_list_mutex));
1737
1738 if (bpage->old) {
1739 buf_pool->stat.n_pages_made_young++;
1740 }
1741
1742 buf_LRU_remove_block(bpage);
1743 buf_LRU_add_block_low(bpage, false);
1744 }
1745
1746 /** Moves a block to the end of the LRU list.
1747 @param[in] bpage control block */
1748 void buf_LRU_make_block_old(buf_page_t *bpage) {
1749 ut_d(buf_pool_t *buf_pool =) buf_pool_from_bpage(bpage);
1750
1751 ut_ad(mutex_own(&buf_pool->LRU_list_mutex));
1752
1753 buf_LRU_remove_block(bpage);
1754 buf_LRU_add_block_low(bpage, true);
1755 }
1756
1757 bool buf_LRU_free_page(buf_page_t *bpage, bool zip) {
1758 auto buf_pool = buf_pool_from_bpage(bpage);
1759 auto block_mutex = buf_page_get_mutex(bpage);
1760 auto hash_lock = buf_page_hash_lock_get(buf_pool, bpage->id);
1761
1762 ut_ad(bpage->in_LRU_list);
1763 ut_ad(mutex_own(&buf_pool->LRU_list_mutex));
1764 ut_ad(mutex_own(block_mutex));
1765 ut_ad(buf_page_in_file(bpage));
1766
1767 if (!buf_page_can_relocate(bpage)) {
1768 /* Do not free buffer fixed and I/O-fixed blocks. */
1769 return (false);
1770 }
1771
1772 #ifdef UNIV_IBUF_COUNT_DEBUG
1773 ut_a(ibuf_count_get(bpage->id) == 0);
1774 #endif /* UNIV_IBUF_COUNT_DEBUG */
1775
1776 buf_page_t *b{};
1777 auto is_dirty = bpage->is_dirty();
1778
1779 if (zip || bpage->zip.data == nullptr) {
1780 /* This would completely free the block. */
1781 /* Do not completely free dirty blocks. */
1782
1783 if (is_dirty) {
1784 return (false);
1785 }
1786 } else if (is_dirty && buf_page_get_state(bpage) != BUF_BLOCK_FILE_PAGE) {
1787 ut_ad(buf_page_get_state(bpage) == BUF_BLOCK_ZIP_DIRTY);
1788
1789 return (false);
1790
1791 } else if (buf_page_get_state(bpage) == BUF_BLOCK_FILE_PAGE) {
1792 b = buf_page_alloc_descriptor();
1793 ut_a(b);
1794 }
1795
1796 ut_ad(buf_page_in_file(bpage));
1797 ut_ad(bpage->in_LRU_list);
1798 ut_ad(bpage->in_flush_list == is_dirty);
1799
1800 DBUG_PRINT("ib_buf", ("free page " UINT32PF ":" UINT32PF, bpage->id.space(),
1801 bpage->id.page_no()));
1802
1803 mutex_exit(block_mutex);
1804 DBUG_EXECUTE_IF("buf_lru_free_page_delay_block_mutex_reacquisition",
1805 std::this_thread::sleep_for(std::chrono::microseconds(100)););
1806
1807 rw_lock_x_lock(hash_lock, UT_LOCATION_HERE);
1808 mutex_enter(block_mutex);
1809 is_dirty = bpage->is_dirty();
1810
1811 if (!buf_page_can_relocate(bpage) ||
1812 ((zip || bpage->zip.data == nullptr) && is_dirty)) {
1813 rw_lock_x_unlock(hash_lock);
1814
1815 if (b != nullptr) {
1816 buf_page_free_descriptor(b);
1817 }
1818
1819 return (false);
1820 }
1821
1822 if (is_dirty && buf_page_get_state(bpage) != BUF_BLOCK_FILE_PAGE) {
1823 ut_ad(buf_page_get_state(bpage) == BUF_BLOCK_ZIP_DIRTY);
1824
1825 rw_lock_x_unlock(hash_lock);
1826
1827 if (b != nullptr) {
1828 buf_page_free_descriptor(b);
1829 }
1830
1831 return (false);
1832 }
1833
1834 if (b != nullptr) {
1835 new (b) buf_page_t(*bpage);
1836 }
1837
1838 ut_ad(rw_lock_own(hash_lock, RW_LOCK_X));
1839 ut_ad(buf_page_can_relocate(bpage));
1840
1841 if (!buf_LRU_block_remove_hashed(bpage, zip, false)) {
1842 mutex_exit(&buf_pool->LRU_list_mutex);
1843
1844 if (b != nullptr) {
1845 buf_page_free_descriptor(b);
1846 }
1847 return true;
1848 }
1849 ut_ad(!mutex_own(block_mutex));
1850
1851 /* buf_LRU_block_remove_hashed() releases the hash_lock */
1852 ut_ad(!rw_lock_own(hash_lock, RW_LOCK_X) &&
1853 !rw_lock_own(hash_lock, RW_LOCK_S));
1854
1855 /* We have just freed a BUF_BLOCK_FILE_PAGE. If b != nullptr
1856 then it was a compressed page with an uncompressed frame and
1857 we are interested in freeing only the uncompressed frame.
1858 Therefore we have to reinsert the compressed page descriptor
1859 into the LRU and page_hash (and possibly flush_list).
1860 if b == nullptr then it was a regular page that has been freed */
1861
1862 if (b != nullptr) {
1863 auto prev_b = UT_LIST_GET_PREV(LRU, b);
1864
1865 rw_lock_x_lock(hash_lock, UT_LOCATION_HERE);
1866
1867 mutex_enter(block_mutex);
1868
1869 ut_a(!buf_page_hash_get_low(buf_pool, b->id));
1870
1871 b->state = b->is_dirty() ? BUF_BLOCK_ZIP_DIRTY : BUF_BLOCK_ZIP_PAGE;
1872
1873 ut_ad(b->size.is_compressed());
1874
1875 UNIV_MEM_DESC(b->zip.data, b->size.physical());
1876
1877 /* The fields in_page_hash and in_LRU_list of
1878 the to-be-freed block descriptor should have
1879 been cleared in
1880 buf_LRU_block_remove_hashed(), which
1881 invokes buf_LRU_remove_block(). */
1882 ut_ad(!bpage->in_page_hash);
1883 ut_ad(!bpage->in_LRU_list);
1884
1885 /* bpage->state was BUF_BLOCK_FILE_PAGE because
1886 b != NULL. The type cast below is thus valid. */
1887 ut_ad(!((buf_block_t *)bpage)->in_unzip_LRU_list);
1888
1889 /* The fields of bpage were copied to b before
1890 buf_LRU_block_remove_hashed() was invoked. */
1891 ut_ad(!b->in_zip_hash);
1892 ut_ad(b->in_page_hash);
1893 ut_ad(b->in_LRU_list);
1894
1895 HASH_INSERT(buf_page_t, hash, buf_pool->page_hash, b->id.hash(), b);
1896
1897 /* Insert b where bpage was in the LRU list. */
1898 if (prev_b != nullptr) {
1899 ut_ad(prev_b->in_LRU_list);
1900 ut_ad(buf_page_in_file(prev_b));
1901
1902 UT_LIST_INSERT_AFTER(buf_pool->LRU, prev_b, b);
1903
1904 incr_LRU_size_in_bytes(b, buf_pool);
1905
1906 if (buf_page_is_old(b)) {
1907 buf_pool->LRU_old_len++;
1908 if (buf_pool->LRU_old == UT_LIST_GET_NEXT(LRU, b)) {
1909 buf_pool->LRU_old = b;
1910 }
1911 }
1912
1913 auto lru_len = UT_LIST_GET_LEN(buf_pool->LRU);
1914
1915 if (lru_len > BUF_LRU_OLD_MIN_LEN) {
1916 ut_ad(buf_pool->LRU_old);
1917 /* Adjust the length of the
1918 old block list if necessary */
1919 buf_LRU_old_adjust_len(buf_pool);
1920 } else if (lru_len == BUF_LRU_OLD_MIN_LEN) {
1921 /* The LRU list is now long
1922 enough for LRU_old to become
1923 defined: init it */
1924 buf_LRU_old_init(buf_pool);
1925 }
1926 #ifdef UNIV_LRU_DEBUG
1927 /* Check that the "old" flag is consistent
1928 in the block and its neighbours. */
1929 buf_page_set_old(b, buf_page_is_old(b));
1930 #endif /* UNIV_LRU_DEBUG */
1931 } else {
1932 ut_d(b->in_LRU_list = false);
1933 buf_LRU_add_block_low(b, buf_page_is_old(b));
1934 }
1935
1936 mutex_enter(&buf_pool->zip_mutex);
1937 rw_lock_x_unlock(hash_lock);
1938 if (b->state == BUF_BLOCK_ZIP_PAGE) {
1939 #if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
1940 buf_LRU_insert_zip_clean(b);
1941 #endif /* UNIV_DEBUG || UNIV_BUF_DEBUG */
1942 } else {
1943 /* Relocate on buf_pool->flush_list. */
1944 buf_flush_relocate_on_flush_list(bpage, b);
1945 }
1946
1947 bpage->zip.data = nullptr;
1948
1949 page_zip_set_size(&bpage->zip, 0);
1950
1951 bpage->size.copy_from(
1952 page_size_t(bpage->size.logical(), bpage->size.logical(), false));
1953
1954 /* Prevent buf_page_get_gen() from
1955 decompressing the block while we release block_mutex. */
1956
1957 buf_page_set_sticky(b);
1958
1959 mutex_exit(&buf_pool->zip_mutex);
1960
1961 mutex_exit(block_mutex);
1962 }
1963
1964 mutex_exit(&buf_pool->LRU_list_mutex);
1965
1966 /* Remove possible adaptive hash index on the page.
1967 The page was declared uninitialized by
1968 buf_LRU_block_remove_hashed(). We need to flag
1969 the contents of the page valid (which it still is) in
1970 order to avoid bogus Valgrind warnings.*/
1971 UNIV_MEM_VALID(((buf_block_t *)bpage)->frame, UNIV_PAGE_SIZE);
1972 btr_search_drop_page_hash_index((buf_block_t *)bpage, true);
1973 UNIV_MEM_INVALID(((buf_block_t *)bpage)->frame, UNIV_PAGE_SIZE);
1974
1975 if (b != nullptr) {
1976 /* Compute and stamp the compressed page
1977 checksum while not holding any mutex. The
1978 block is already half-freed
1979 (BUF_BLOCK_REMOVE_HASH) and removed from
1980 buf_pool->page_hash, thus inaccessible by any
1981 other thread. */
1982
1983 ut_ad(b->size.is_compressed());
1984
1985 BlockReporter reporter = BlockReporter(false, b->zip.data, b->size, false);
1986
1987 const uint32_t checksum = reporter.calc_zip_checksum(
1988 static_cast<srv_checksum_algorithm_t>(srv_checksum_algorithm));
1989
1990 mach_write_to_4(b->zip.data + FIL_PAGE_SPACE_OR_CHKSUM, checksum);
1991 }
1992
1993 if (b != nullptr) {
1994 mutex_enter(&buf_pool->zip_mutex);
1995
1996 buf_page_unset_sticky(b);
1997
1998 mutex_exit(&buf_pool->zip_mutex);
1999 }
2000
2001 buf_LRU_block_free_hashed_page((buf_block_t *)bpage);
2002
2003 return (true);
2004 }
2005
2006 /** Puts a block back to the free list.
2007 @param[in] block block must not contain a file page */
2008 void buf_LRU_block_free_non_file_page(buf_block_t *block) {
2009 void *data;
2010 buf_pool_t *buf_pool = buf_pool_from_block(block);
2011
2012 switch (buf_block_get_state(block)) {
2013 case BUF_BLOCK_MEMORY:
2014 case BUF_BLOCK_READY_FOR_USE:
2015 break;
2016 default:
2017 ut_error;
2018 }
2019
2020 block->ahi.assert_empty();
2021 ut_ad(!block->page.in_free_list);
2022 ut_ad(!block->page.in_flush_list);
2023 ut_ad(!block->page.in_LRU_list);
2024
2025 #ifdef UNIV_DEBUG
2026 /* Wipe contents of page to reveal possible stale pointers to it */
2027 memset(block->frame, '\0', UNIV_PAGE_SIZE);
2028 #else
2029 /* Wipe page_no and space_id */
2030 memset(block->frame + FIL_PAGE_OFFSET, 0xfe, 4);
2031 memset(block->frame + FIL_PAGE_ARCH_LOG_NO_OR_SPACE_ID, 0xfe, 4);
2032 #endif /* UNIV_DEBUG */
2033 UNIV_MEM_ASSERT_AND_FREE(block->frame, UNIV_PAGE_SIZE);
2034 data = block->page.zip.data;
2035
2036 if (data != nullptr) {
2037 block->page.zip.data = nullptr;
2038
2039 ut_ad(block->page.size.is_compressed());
2040
2041 buf_buddy_free(buf_pool, data, block->page.size.physical());
2042
2043 page_zip_set_size(&block->page.zip, 0);
2044
2045 block->page.size.copy_from(page_size_t(block->page.size.logical(),
2046 block->page.size.logical(), false));
2047 }
2048
2049 #ifndef UNIV_HOTBACKUP
2050 buf_page_prepare_for_free(&block->page);
2051 ut_ad(block->page.get_space() == nullptr);
2052 #endif /* !UNIV_HOTBACKUP */
2053
2054 if (buf_get_withdraw_depth(buf_pool) &&
2055 buf_block_will_withdrawn(buf_pool, block)) {
2056 /* This should be withdrawn */
2057 buf_block_set_state(block, BUF_BLOCK_NOT_USED);
2058 mutex_enter(&buf_pool->free_list_mutex);
2059 UT_LIST_ADD_LAST(buf_pool->withdraw, &block->page);
2060 ut_d(block->in_withdraw_list = true);
2061 mutex_exit(&buf_pool->free_list_mutex);
2062 } else {
2063 buf_block_set_state(block, BUF_BLOCK_NOT_USED);
2064 mutex_enter(&buf_pool->free_list_mutex);
2065 UT_LIST_ADD_FIRST(buf_pool->free, &block->page);
2066 ut_d(block->page.in_free_list = true);
2067 ut_ad(!block->page.someone_has_io_responsibility());
2068 mutex_exit(&buf_pool->free_list_mutex);
2069 }
2070 }
2071
2072 /** Takes a block out of the LRU list and page hash table.
2073 If the block is compressed-only (BUF_BLOCK_ZIP_PAGE),
2074 the object will be freed.
2075
2076 The caller must hold buf_pool->LRU_list_mutex, the buf_page_get_mutex() mutex
2077 and the appropriate hash_lock. This function will release the
2078 buf_page_get_mutex() and the hash_lock.
2079
2080 If a compressed page is freed other compressed pages may be relocated.
2081
2082 @param[in] bpage block, must contain a file page and
2083 be in a state where it can be freed; there
2084 may or may not be a hash index to the page
2085 @param[in] zip true if should remove also the
2086 compressed page of an uncompressed page
2087 @param[in] ignore_content true if should ignore page content, since it
2088 could be not initialized
2089 @retval true if BUF_BLOCK_FILE_PAGE was removed from page_hash. The
2090 caller needs to free the page to the free list
2091 @retval false if BUF_BLOCK_ZIP_PAGE was removed from page_hash. In
2092 this case the block is already returned to the buddy allocator. */
2093 static bool buf_LRU_block_remove_hashed(buf_page_t *bpage, bool zip,
2094 bool ignore_content) {
2095 const buf_page_t *hashed_bpage;
2096 buf_pool_t *buf_pool = buf_pool_from_bpage(bpage);
2097 rw_lock_t *hash_lock;
2098
2099 ut_ad(mutex_own(&buf_pool->LRU_list_mutex));
2100 ut_ad(mutex_own(buf_page_get_mutex(bpage)));
2101
2102 hash_lock = buf_page_hash_lock_get(buf_pool, bpage->id);
2103
2104 ut_ad(rw_lock_own(hash_lock, RW_LOCK_X));
2105
2106 ut_a(buf_page_get_io_fix(bpage) == BUF_IO_NONE);
2107 ut_a(bpage->buf_fix_count == 0);
2108
2109 buf_LRU_remove_block(bpage);
2110
2111 buf_pool->freed_page_clock += 1;
2112
2113 switch (buf_page_get_state(bpage)) {
2114 case BUF_BLOCK_FILE_PAGE: {
2115 UNIV_MEM_ASSERT_W(bpage, sizeof(buf_block_t));
2116 UNIV_MEM_ASSERT_W(((buf_block_t *)bpage)->frame, UNIV_PAGE_SIZE);
2117
2118 buf_block_modify_clock_inc((buf_block_t *)bpage);
2119
2120 if (bpage->zip.data != nullptr) {
2121 const page_t *page = ((buf_block_t *)bpage)->frame;
2122
2123 ut_a(!zip || !bpage->is_dirty());
2124 ut_ad(bpage->size.is_compressed());
2125
2126 switch (fil_page_get_type(page)) {
2127 case FIL_PAGE_TYPE_ALLOCATED:
2128 case FIL_PAGE_INODE:
2129 case FIL_PAGE_IBUF_BITMAP:
2130 case FIL_PAGE_TYPE_FSP_HDR:
2131 case FIL_PAGE_TYPE_XDES:
2132 case FIL_PAGE_TYPE_ZLOB_FIRST:
2133 case FIL_PAGE_TYPE_ZLOB_DATA:
2134 case FIL_PAGE_TYPE_ZLOB_INDEX:
2135 case FIL_PAGE_TYPE_ZLOB_FRAG:
2136 case FIL_PAGE_TYPE_ZLOB_FRAG_ENTRY:
2137 /* These are essentially uncompressed pages. */
2138 if (!zip) {
2139 /* InnoDB writes the data to the
2140 uncompressed page frame. Copy it
2141 to the compressed page, which will
2142 be preserved. */
2143 memcpy(bpage->zip.data, page, bpage->size.physical());
2144 }
2145 break;
2146 case FIL_PAGE_TYPE_ZBLOB:
2147 case FIL_PAGE_TYPE_ZBLOB2:
2148 case FIL_PAGE_SDI_ZBLOB:
2149 break;
2150 case FIL_PAGE_INDEX:
2151 case FIL_PAGE_SDI:
2152 case FIL_PAGE_RTREE:
2153 #ifdef UNIV_ZIP_DEBUG
2154 ut_a(page_zip_validate(&bpage->zip, page,
2155 ((buf_block_t *)bpage)->index));
2156 #endif /* UNIV_ZIP_DEBUG */
2157 break;
2158 default:
2159 ib::error(ER_IB_MSG_135) << "The compressed page to be"
2160 " evicted seems corrupt:";
2161 ut_print_buf(stderr, page, bpage->size.logical());
2162
2163 ib::error(ER_IB_MSG_136) << "Possibly older version of"
2164 " the page:";
2165
2166 ut_print_buf(stderr, bpage->zip.data, bpage->size.physical());
2167 putc('\n', stderr);
2168 ut_error;
2169 }
2170
2171 break;
2172 }
2173
2174 if (!ignore_content) {
2175 /* Account the eviction of index leaf pages from
2176 the buffer pool(s). */
2177
2178 const byte *frame = bpage->zip.data != nullptr
2179 ? bpage->zip.data
2180 : reinterpret_cast<buf_block_t *>(bpage)->frame;
2181
2182 const ulint type = fil_page_get_type(frame);
2183
2184 if ((type == FIL_PAGE_INDEX || type == FIL_PAGE_RTREE) &&
2185 page_is_leaf(frame)) {
2186 uint32_t space_id = bpage->id.space();
2187
2188 space_index_t idx_id = btr_page_get_index_id(frame);
2189
2190 buf_stat_per_index->dec(index_id_t(space_id, idx_id));
2191 }
2192 }
2193 }
2194 [[fallthrough]];
2195 case BUF_BLOCK_ZIP_PAGE:
2196 ut_a(!bpage->is_dirty());
2197 if (bpage->size.is_compressed()) {
2198 UNIV_MEM_ASSERT_W(bpage->zip.data, bpage->size.physical());
2199 }
2200 break;
2201 case BUF_BLOCK_POOL_WATCH:
2202 case BUF_BLOCK_ZIP_DIRTY:
2203 case BUF_BLOCK_NOT_USED:
2204 case BUF_BLOCK_READY_FOR_USE:
2205 case BUF_BLOCK_MEMORY:
2206 case BUF_BLOCK_REMOVE_HASH:
2207 ut_error;
2208 break;
2209 }
2210
2211 hashed_bpage = buf_page_hash_get_low(buf_pool, bpage->id);
2212
2213 if (bpage != hashed_bpage) {
2214 ib::error(ER_IB_MSG_137)
2215 << "Page " << bpage->id << " not found in the hash table";
2216
2217 if (hashed_bpage) {
2218 ib::error(ER_IB_MSG_138)
2219 << "In hash table we find block " << hashed_bpage << " of "
2220 << hashed_bpage->id << " which is not " << bpage;
2221 }
2222
2223 ut_d(mutex_exit(buf_page_get_mutex(bpage)));
2224 ut_d(rw_lock_x_unlock(hash_lock));
2225 ut_d(mutex_exit(&buf_pool->LRU_list_mutex));
2226 ut_d(buf_print());
2227 ut_d(buf_LRU_print());
2228 ut_d(buf_validate());
2229 ut_d(buf_LRU_validate());
2230 ut_d(ut_error);
2231 }
2232
2233 ut_ad(!bpage->in_zip_hash);
2234 ut_ad(bpage->in_page_hash);
2235 ut_d(bpage->in_page_hash = false);
2236
2237 HASH_DELETE(buf_page_t, hash, buf_pool->page_hash, bpage->id.hash(), bpage);
2238
2239 switch (buf_page_get_state(bpage)) {
2240 case BUF_BLOCK_ZIP_PAGE:
2241 ut_ad(!bpage->in_free_list);
2242 ut_ad(!bpage->in_flush_list);
2243 ut_ad(!bpage->in_LRU_list);
2244 ut_a(bpage->zip.data);
2245 ut_a(bpage->size.is_compressed());
2246
2247 #if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
2248 UT_LIST_REMOVE(buf_pool->zip_clean, bpage);
2249 #endif /* UNIV_DEBUG || UNIV_BUF_DEBUG */
2250
2251 mutex_exit(&buf_pool->zip_mutex);
2252 rw_lock_x_unlock(hash_lock);
2253
2254 buf_buddy_free(buf_pool, bpage->zip.data, bpage->size.physical());
2255
2256 buf_page_free_descriptor(bpage);
2257 return (false);
2258
2259 case BUF_BLOCK_FILE_PAGE:
2260 memset(((buf_block_t *)bpage)->frame + FIL_PAGE_OFFSET, 0xff, 4);
2261 memset(((buf_block_t *)bpage)->frame + FIL_PAGE_ARCH_LOG_NO_OR_SPACE_ID,
2262 0xff, 4);
2263 UNIV_MEM_INVALID(((buf_block_t *)bpage)->frame, UNIV_PAGE_SIZE);
2264 buf_page_set_state(bpage, BUF_BLOCK_REMOVE_HASH);
2265
2266 /* Question: If we release bpage and hash mutex here
2267 then what protects us against:
2268 1) Some other thread buffer fixing this page
2269 2) Some other thread trying to read this page and
2270 not finding it in buffer pool attempting to read it
2271 from the disk.
2272 Answer:
2273 1) Cannot happen because the page is no longer in the
2274 page_hash. Only possibility is when while invalidating
2275 a tablespace we buffer fix the prev_page in LRU to
2276 avoid relocation during the scan. But that is not
2277 possible because we are holding LRU list mutex.
2278
2279 2) Not possible because in buf_page_init_for_read()
2280 we do a look up of page_hash while holding LRU list
2281 mutex and since we are holding LRU list mutex here
2282 and by the time we'll release it in the caller we'd
2283 have inserted the compressed only descriptor in the
2284 page_hash. */
2285 ut_ad(mutex_own(&buf_pool->LRU_list_mutex));
2286 rw_lock_x_unlock(hash_lock);
2287 mutex_exit(&((buf_block_t *)bpage)->mutex);
2288
2289 if (zip && bpage->zip.data) {
2290 /* Free the compressed page. */
2291 void *data = bpage->zip.data;
2292 bpage->zip.data = nullptr;
2293
2294 ut_ad(!bpage->in_free_list);
2295 ut_ad(!bpage->in_flush_list);
2296 ut_ad(!bpage->in_LRU_list);
2297
2298 buf_buddy_free(buf_pool, data, bpage->size.physical());
2299
2300 page_zip_set_size(&bpage->zip, 0);
2301
2302 bpage->size.copy_from(
2303 page_size_t(bpage->size.logical(), bpage->size.logical(), false));
2304 }
2305
2306 return (true);
2307
2308 case BUF_BLOCK_POOL_WATCH:
2309 case BUF_BLOCK_ZIP_DIRTY:
2310 case BUF_BLOCK_NOT_USED:
2311 case BUF_BLOCK_READY_FOR_USE:
2312 case BUF_BLOCK_MEMORY:
2313 case BUF_BLOCK_REMOVE_HASH:
2314 break;
2315 }
2316
2317 ut_error;
2318 }
2319
2320 static void buf_LRU_block_free_hashed_page(buf_block_t *block) noexcept {
2321 buf_block_set_state(block, BUF_BLOCK_MEMORY);
2322
2323 buf_LRU_block_free_non_file_page(block);
2324 }
2325
2326 void buf_LRU_free_one_page(buf_page_t *bpage, bool ignore_content) {
2327 #ifdef UNIV_DEBUG
2328 buf_pool_t *buf_pool = buf_pool_from_bpage(bpage);
2329 BPageMutex *block_mutex = buf_page_get_mutex(bpage);
2330 rw_lock_t *hash_lock = buf_page_hash_lock_get(buf_pool, bpage->id);
2331
2332 ut_ad(mutex_own(&buf_pool->LRU_list_mutex));
2333 ut_ad(mutex_own(block_mutex));
2334 ut_ad(rw_lock_own(hash_lock, RW_LOCK_X));
2335 #endif /* UNIV_DEBUG */
2336
2337 if (buf_LRU_block_remove_hashed(bpage, true, ignore_content)) {
2338 buf_LRU_block_free_hashed_page((buf_block_t *)bpage);
2339 }
2340
2341 /* buf_LRU_block_remove_hashed() releases hash_lock and block_mutex */
2342 ut_ad(!rw_lock_own(hash_lock, RW_LOCK_X) &&
2343 !rw_lock_own(hash_lock, RW_LOCK_S));
2344
2345 ut_ad(!mutex_own(block_mutex));
2346 }
2347
2348 /** Updates buf_pool->LRU_old_ratio for one buffer pool instance.
2349 @param[in] buf_pool buffer pool instance
2350 @param[in] old_pct Reserve this percentage of
2351 the buffer pool for "old" blocks
2352 @param[in] adjust true=adjust the LRU list;
2353 false=just assign buf_pool->LRU_old_ratio
2354 during the initialization of InnoDB
2355 @return updated old_pct */
2356 static uint buf_LRU_old_ratio_update_instance(buf_pool_t *buf_pool,
2357 uint old_pct, bool adjust) {
2358 uint ratio;
2359
2360 ratio = old_pct * BUF_LRU_OLD_RATIO_DIV / 100;
2361 if (ratio < BUF_LRU_OLD_RATIO_MIN) {
2362 ratio = BUF_LRU_OLD_RATIO_MIN;
2363 } else if (ratio > BUF_LRU_OLD_RATIO_MAX) {
2364 ratio = BUF_LRU_OLD_RATIO_MAX;
2365 }
2366
2367 if (adjust) {
2368 mutex_enter(&buf_pool->LRU_list_mutex);
2369
2370 if (ratio != buf_pool->LRU_old_ratio) {
2371 buf_pool->LRU_old_ratio = ratio;
2372
2373 if (UT_LIST_GET_LEN(buf_pool->LRU) >= BUF_LRU_OLD_MIN_LEN) {
2374 buf_LRU_old_adjust_len(buf_pool);
2375 }
2376 }
2377
2378 mutex_exit(&buf_pool->LRU_list_mutex);
2379 } else {
2380 buf_pool->LRU_old_ratio = ratio;
2381 }
2382 /* the reverse of
2383 ratio = old_pct * BUF_LRU_OLD_RATIO_DIV / 100 */
2384 return ((uint)(ratio * 100 / (double)BUF_LRU_OLD_RATIO_DIV + 0.5));
2385 }
2386
2387 /** Updates buf_pool->LRU_old_ratio.
2388 @return updated old_pct */
2389 uint buf_LRU_old_ratio_update(
2390 uint old_pct, /*!< in: Reserve this percentage of
2391 the buffer pool for "old" blocks. */
2392 bool adjust) /*!< in: true=adjust the LRU list;
2393 false=just assign buf_pool->LRU_old_ratio
2394 during the initialization of InnoDB */
2395 {
2396 uint new_ratio = 0;
2397
2398 for (ulint i = 0; i < srv_buf_pool_instances; i++) {
2399 buf_pool_t *buf_pool;
2400
2401 buf_pool = buf_pool_from_array(i);
2402
2403 new_ratio = buf_LRU_old_ratio_update_instance(buf_pool, old_pct, adjust);
2404 }
2405
2406 return (new_ratio);
2407 }
2408
2409 /** Update the historical stats that we are collecting for LRU eviction
2410 policy at the end of each interval. */
2411 void buf_LRU_stat_update(void) {
2412 buf_LRU_stat_t *item;
2413 buf_pool_t *buf_pool;
2414 bool evict_started = false;
2415 buf_LRU_stat_t cur_stat;
2416
2417 /* If we haven't started eviction yet then don't update stats. */
2418 os_rmb;
2419 for (ulint i = 0; i < srv_buf_pool_instances; i++) {
2420 buf_pool = buf_pool_from_array(i);
2421
2422 if (buf_pool->freed_page_clock != 0) {
2423 evict_started = true;
2424 break;
2425 }
2426 }
2427
2428 if (!evict_started) {
2429 goto func_exit;
2430 }
2431
2432 /* Update the index. */
2433 item = &buf_LRU_stat_arr[buf_LRU_stat_arr_ind];
2434 buf_LRU_stat_arr_ind++;
2435 buf_LRU_stat_arr_ind %= BUF_LRU_STAT_N_INTERVAL;
2436
2437 /* Add the current value and subtract the obsolete entry.
2438 Since buf_LRU_stat_cur is not protected by any mutex,
2439 it can be changing between adding to buf_LRU_stat_sum
2440 and copying to item. Assign it to local variables to make
2441 sure the same value assign to the buf_LRU_stat_sum
2442 and item */
2443 cur_stat = buf_LRU_stat_cur;
2444
2445 buf_LRU_stat_sum.io += cur_stat.io - item->io;
2446 buf_LRU_stat_sum.unzip += cur_stat.unzip - item->unzip;
2447
2448 /* Put current entry in the array. */
2449 memcpy(item, &cur_stat, sizeof *item);
2450
2451 func_exit:
2452 /* Clear the current entry. */
2453 memset(&buf_LRU_stat_cur, 0, sizeof buf_LRU_stat_cur);
2454 os_wmb;
2455 }
2456
2457 #if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
2458 /** Validates the LRU list for one buffer pool instance.
2459 @param[in] buf_pool buffer pool instance */
2460 void buf_LRU_validate_instance(buf_pool_t *buf_pool) {
2461 mutex_enter(&buf_pool->LRU_list_mutex);
2462
2463 if (UT_LIST_GET_LEN(buf_pool->LRU) >= BUF_LRU_OLD_MIN_LEN) {
2464 ut_a(buf_pool->LRU_old);
2465
2466 const size_t new_len = calculate_desired_LRU_old_size(buf_pool);
2467
2468 ut_a(buf_pool->LRU_old_len >= new_len - BUF_LRU_OLD_TOLERANCE);
2469 ut_a(buf_pool->LRU_old_len <= new_len + BUF_LRU_OLD_TOLERANCE);
2470 }
2471
2472 CheckInLRUList::validate(buf_pool);
2473
2474 ulint old_len = 0;
2475
2476 for (auto bpage : buf_pool->LRU) {
2477 switch (buf_page_get_state(bpage)) {
2478 case BUF_BLOCK_POOL_WATCH:
2479 case BUF_BLOCK_NOT_USED:
2480 case BUF_BLOCK_READY_FOR_USE:
2481 case BUF_BLOCK_MEMORY:
2482 case BUF_BLOCK_REMOVE_HASH:
2483 ut_error;
2484 break;
2485 case BUF_BLOCK_FILE_PAGE:
2486 ut_ad(((buf_block_t *)bpage)->in_unzip_LRU_list ==
2487 buf_page_belongs_to_unzip_LRU(bpage));
2488 case BUF_BLOCK_ZIP_PAGE:
2489 case BUF_BLOCK_ZIP_DIRTY:
2490 break;
2491 }
2492
2493 if (buf_page_is_old(bpage)) {
2494 const buf_page_t *prev = UT_LIST_GET_PREV(LRU, bpage);
2495 const buf_page_t *next = UT_LIST_GET_NEXT(LRU, bpage);
2496
2497 if (!old_len++) {
2498 ut_a(buf_pool->LRU_old == bpage);
2499 } else {
2500 ut_a(!prev || buf_page_is_old(prev));
2501 }
2502
2503 ut_a(!next || buf_page_is_old(next));
2504 }
2505 }
2506
2507 ut_a(buf_pool->LRU_old_len == old_len);
2508
2509 mutex_exit(&buf_pool->LRU_list_mutex);
2510
2511 mutex_enter(&buf_pool->free_list_mutex);
2512
2513 CheckInFreeList::validate(buf_pool);
2514
2515 for (auto bpage : buf_pool->free) {
2516 ut_a(buf_page_get_state(bpage) == BUF_BLOCK_NOT_USED);
2517 }
2518
2519 mutex_exit(&buf_pool->free_list_mutex);
2520
2521 mutex_enter(&buf_pool->LRU_list_mutex);
2522
2523 CheckUnzipLRUAndLRUList::validate(buf_pool);
2524
2525 for (auto block : buf_pool->unzip_LRU) {
2526 ut_ad(block->in_unzip_LRU_list);
2527 ut_ad(block->page.in_LRU_list);
2528 ut_a(buf_page_belongs_to_unzip_LRU(&block->page));
2529 }
2530
2531 mutex_exit(&buf_pool->LRU_list_mutex);
2532 }
2533
2534 /** Validates the LRU list. */
2535 void buf_LRU_validate(void) {
2536 for (size_t i = 0; i < srv_buf_pool_instances; i++) {
2537 buf_pool_t *buf_pool = buf_pool_from_array(i);
2538 buf_LRU_validate_instance(buf_pool);
2539 }
2540 }
2541
2542 Space_References buf_LRU_count_space_references() {
2543 Space_References result;
2544 for (size_t i = 0; i < srv_buf_pool_instances; i++) {
2545 buf_pool_t *buf_pool = buf_pool_from_array(i);
2546
2547 mutex_enter(&buf_pool->LRU_list_mutex);
2548
2549 for (auto bpage : buf_pool->LRU) {
2550 /* We have the LRU mutex, it is safe to assume the space ID will not be
2551 changed, as it would require removal from the LRU first. */
2552 result[bpage->get_space()]++;
2553 }
2554
2555 for (size_t j = 0; j < BUF_POOL_WATCH_SIZE; j++) {
2556 const auto &bpage = &buf_pool->watch[j];
2557
2558 switch (bpage->state) {
2559 case BUF_BLOCK_ZIP_PAGE:
2560 result[bpage->get_space()]++;
2561 break;
2562 default:
2563 break;
2564 }
2565 }
2566
2567 mutex_exit(&buf_pool->LRU_list_mutex);
2568 }
2569
2570 return result;
2571 }
2572 #endif /* UNIV_DEBUG || UNIV_BUF_DEBUG */
2573
2574 #if defined UNIV_DEBUG_PRINT || defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
2575 /** Prints the LRU list for one buffer pool instance.
2576 @param[in] buf_pool buffer pool instance */
2577 static void buf_LRU_print_instance(buf_pool_t *buf_pool) {
2578 mutex_enter(&buf_pool->LRU_list_mutex);
2579
2580 for (auto bpage : buf_pool->LRU) {
2581 mutex_enter(buf_page_get_mutex(bpage));
2582
2583 fprintf(stderr, "BLOCK space " UINT32PF " page " UINT32PF " ",
2584 bpage->id.space(), bpage->id.page_no());
2585
2586 if (buf_page_is_old(bpage)) {
2587 fputs("old ", stderr);
2588 }
2589
2590 if (bpage->buf_fix_count) {
2591 fprintf(stderr, "buffix count %lu ", (ulong)bpage->buf_fix_count);
2592 }
2593
2594 if (buf_page_get_io_fix(bpage)) {
2595 fprintf(stderr, "io_fix %lu ", (ulong)buf_page_get_io_fix(bpage));
2596 }
2597
2598 if (bpage->is_dirty()) {
2599 fputs("modif. ", stderr);
2600 }
2601
2602 switch (buf_page_get_state(bpage)) {
2603 const byte *frame;
2604 case BUF_BLOCK_FILE_PAGE:
2605 frame = buf_block_get_frame((buf_block_t *)bpage);
2606 fprintf(stderr,
2607 "\ntype %lu"
2608 " index id " IB_ID_FMT "\n",
2609 (ulong)fil_page_get_type(frame), btr_page_get_index_id(frame));
2610 break;
2611 case BUF_BLOCK_ZIP_PAGE:
2612 frame = bpage->zip.data;
2613 fprintf(stderr,
2614 "\ntype %lu size %lu"
2615 " index id " IB_ID_FMT "\n",
2616 (ulong)fil_page_get_type(frame), (ulong)bpage->size.physical(),
2617 btr_page_get_index_id(frame));
2618 break;
2619
2620 default:
2621 fprintf(stderr, "\n!state %lu!\n", (ulong)buf_page_get_state(bpage));
2622 break;
2623 }
2624
2625 mutex_exit(buf_page_get_mutex(bpage));
2626 }
2627
2628 mutex_exit(&buf_pool->LRU_list_mutex);
2629 }
2630
2631 /** Prints the LRU list. */
2632 void buf_LRU_print(void) {
2633 for (size_t i = 0; i < srv_buf_pool_instances; i++) {
2634 buf_pool_t *buf_pool;
2635
2636 buf_pool = buf_pool_from_array(i);
2637 buf_LRU_print_instance(buf_pool);
2638 }
2639 }
2640 #endif /* UNIV_DEBUG_PRINT || UNIV_DEBUG || UNIV_BUF_DEBUG */