=== modified file 'mysql-test/suite/innodb/r/innodb_monitor.result' --- mysql-test/suite/innodb/r/innodb_monitor.result 2013-09-13 02:08:28 +0000 +++ mysql-test/suite/innodb/r/innodb_monitor.result 2014-01-27 16:09:27 +0000 @@ -43,7 +43,6 @@ buffer_flush_batch_scanned disabled buffer_flush_batch_num_scan disabled buffer_flush_batch_scanned_per_call disabled -buffer_flush_batch_rescan disabled buffer_flush_batch_total_pages disabled buffer_flush_batches disabled buffer_flush_batch_pages disabled @@ -68,9 +67,12 @@ buffer_LRU_batch_scanned disabled buffer_LRU_batch_num_scan disabled buffer_LRU_batch_scanned_per_call disabled -buffer_LRU_batch_total_pages disabled -buffer_LRU_batches disabled -buffer_LRU_batch_pages disabled +buffer_LRU_batch_flush_total_pages disabled +buffer_LRU_batches_flush disabled +buffer_LRU_batch_flush_pages disabled +buffer_LRU_batch_evict_total_pages disabled +buffer_LRU_batches_evict disabled +buffer_LRU_batch_evict_pages disabled buffer_LRU_single_flush_scanned disabled buffer_LRU_single_flush_num_scan disabled buffer_LRU_single_flush_scanned_per_call disabled === modified file 'mysql-test/suite/sys_vars/r/innodb_monitor_disable_basic.result' --- mysql-test/suite/sys_vars/r/innodb_monitor_disable_basic.result 2013-09-13 02:08:28 +0000 +++ mysql-test/suite/sys_vars/r/innodb_monitor_disable_basic.result 2014-01-27 16:09:27 +0000 @@ -43,7 +43,6 @@ buffer_flush_batch_scanned disabled buffer_flush_batch_num_scan disabled buffer_flush_batch_scanned_per_call disabled -buffer_flush_batch_rescan disabled buffer_flush_batch_total_pages disabled buffer_flush_batches disabled buffer_flush_batch_pages disabled @@ -68,9 +67,12 @@ buffer_LRU_batch_scanned disabled buffer_LRU_batch_num_scan disabled buffer_LRU_batch_scanned_per_call disabled -buffer_LRU_batch_total_pages disabled -buffer_LRU_batches disabled -buffer_LRU_batch_pages disabled +buffer_LRU_batch_flush_total_pages disabled +buffer_LRU_batches_flush disabled +buffer_LRU_batch_flush_pages disabled +buffer_LRU_batch_evict_total_pages disabled +buffer_LRU_batches_evict disabled +buffer_LRU_batch_evict_pages disabled buffer_LRU_single_flush_scanned disabled buffer_LRU_single_flush_num_scan disabled buffer_LRU_single_flush_scanned_per_call disabled === modified file 'mysql-test/suite/sys_vars/r/innodb_monitor_enable_basic.result' --- mysql-test/suite/sys_vars/r/innodb_monitor_enable_basic.result 2013-09-13 02:08:28 +0000 +++ mysql-test/suite/sys_vars/r/innodb_monitor_enable_basic.result 2014-01-27 16:09:27 +0000 @@ -43,7 +43,6 @@ buffer_flush_batch_scanned disabled buffer_flush_batch_num_scan disabled buffer_flush_batch_scanned_per_call disabled -buffer_flush_batch_rescan disabled buffer_flush_batch_total_pages disabled buffer_flush_batches disabled buffer_flush_batch_pages disabled @@ -68,9 +67,12 @@ buffer_LRU_batch_scanned disabled buffer_LRU_batch_num_scan disabled buffer_LRU_batch_scanned_per_call disabled -buffer_LRU_batch_total_pages disabled -buffer_LRU_batches disabled -buffer_LRU_batch_pages disabled +buffer_LRU_batch_flush_total_pages disabled +buffer_LRU_batches_flush disabled +buffer_LRU_batch_flush_pages disabled +buffer_LRU_batch_evict_total_pages disabled +buffer_LRU_batches_evict disabled +buffer_LRU_batch_evict_pages disabled buffer_LRU_single_flush_scanned disabled buffer_LRU_single_flush_num_scan disabled buffer_LRU_single_flush_scanned_per_call disabled === modified file 'mysql-test/suite/sys_vars/r/innodb_monitor_reset_all_basic.result' --- mysql-test/suite/sys_vars/r/innodb_monitor_reset_all_basic.result 2013-09-13 02:08:28 +0000 +++ mysql-test/suite/sys_vars/r/innodb_monitor_reset_all_basic.result 2014-01-27 16:09:27 +0000 @@ -43,7 +43,6 @@ buffer_flush_batch_scanned disabled buffer_flush_batch_num_scan disabled buffer_flush_batch_scanned_per_call disabled -buffer_flush_batch_rescan disabled buffer_flush_batch_total_pages disabled buffer_flush_batches disabled buffer_flush_batch_pages disabled @@ -68,9 +67,12 @@ buffer_LRU_batch_scanned disabled buffer_LRU_batch_num_scan disabled buffer_LRU_batch_scanned_per_call disabled -buffer_LRU_batch_total_pages disabled -buffer_LRU_batches disabled -buffer_LRU_batch_pages disabled +buffer_LRU_batch_flush_total_pages disabled +buffer_LRU_batches_flush disabled +buffer_LRU_batch_flush_pages disabled +buffer_LRU_batch_evict_total_pages disabled +buffer_LRU_batches_evict disabled +buffer_LRU_batch_evict_pages disabled buffer_LRU_single_flush_scanned disabled buffer_LRU_single_flush_num_scan disabled buffer_LRU_single_flush_scanned_per_call disabled === modified file 'mysql-test/suite/sys_vars/r/innodb_monitor_reset_basic.result' --- mysql-test/suite/sys_vars/r/innodb_monitor_reset_basic.result 2013-09-13 02:08:28 +0000 +++ mysql-test/suite/sys_vars/r/innodb_monitor_reset_basic.result 2014-01-27 16:09:27 +0000 @@ -43,7 +43,6 @@ buffer_flush_batch_scanned disabled buffer_flush_batch_num_scan disabled buffer_flush_batch_scanned_per_call disabled -buffer_flush_batch_rescan disabled buffer_flush_batch_total_pages disabled buffer_flush_batches disabled buffer_flush_batch_pages disabled @@ -68,9 +67,12 @@ buffer_LRU_batch_scanned disabled buffer_LRU_batch_num_scan disabled buffer_LRU_batch_scanned_per_call disabled -buffer_LRU_batch_total_pages disabled -buffer_LRU_batches disabled -buffer_LRU_batch_pages disabled +buffer_LRU_batch_flush_total_pages disabled +buffer_LRU_batches_flush disabled +buffer_LRU_batch_flush_pages disabled +buffer_LRU_batch_evict_total_pages disabled +buffer_LRU_batches_evict disabled +buffer_LRU_batch_evict_pages disabled buffer_LRU_single_flush_scanned disabled buffer_LRU_single_flush_num_scan disabled buffer_LRU_single_flush_scanned_per_call disabled === modified file 'storage/innobase/buf/buf0buf.cc' --- storage/innobase/buf/buf0buf.cc 2013-08-21 06:30:54 +0000 +++ storage/innobase/buf/buf0buf.cc 2014-01-27 16:09:27 +0000 @@ -54,6 +54,8 @@ #include "srv0mon.h" #include "buf0checksum.h" +#include + /* IMPLEMENTATION OF THE BUFFER POOL ================================= @@ -1300,6 +1302,19 @@ buf_pool->try_LRU_scan = TRUE; + /* Initialize the hazard pointer for flush_list batches */ + new(&buf_pool->flush_hp) + FlushHp(buf_pool, &buf_pool->flush_list_mutex); + + /* Initialize the hazard pointer for LRU batches */ + new(&buf_pool->lru_hp) LRUHp(buf_pool, &buf_pool->mutex); + + /* Initialize the iterator for LRU scan search */ + new(&buf_pool->lru_scan_itr) LRUItr(buf_pool, &buf_pool->mutex); + + /* Initialize the iterator for single page scan search */ + new(&buf_pool->single_scan_itr) LRUItr(buf_pool, &buf_pool->mutex); + buf_pool_mutex_exit(buf_pool); return(DB_SUCCESS); @@ -1390,6 +1405,8 @@ btr_search_sys_create(buf_pool_get_curr_size() / sizeof(void*) / 64); + buf_flush_event = os_event_create(); + return(DB_SUCCESS); } @@ -1506,6 +1523,10 @@ memcpy(dpage, bpage, sizeof *dpage); + /* Important that we adjust the hazard pointer before + removing bpage from LRU list. */ + buf_LRU_adjust_hp(buf_pool, bpage); + ut_d(bpage->in_LRU_list = FALSE); ut_d(bpage->in_page_hash = FALSE); @@ -1513,7 +1534,7 @@ b = UT_LIST_GET_PREV(LRU, bpage); UT_LIST_REMOVE(LRU, buf_pool->LRU, bpage); - if (b) { + if (b != NULL) { UT_LIST_INSERT_AFTER(LRU, buf_pool->LRU, b, dpage); } else { UT_LIST_ADD_FIRST(LRU, buf_pool->LRU, dpage); @@ -1544,6 +1565,84 @@ HASH_INSERT(buf_page_t, hash, buf_pool->page_hash, fold, dpage); } +/** Hazard Pointer implementation. */ + +/** Set current value +@param bpage buffer block to be set as hp */ +void +HazardPointer::set(buf_page_t* bpage) +{ + ut_ad(mutex_own(m_mutex)); + ut_ad(!bpage || buf_pool_from_bpage(bpage) == m_buf_pool); + ut_ad(!bpage || buf_page_in_file(bpage)); + + m_hp = bpage; +} + +/** Checks if a bpage is the hp +@param bpage buffer block to be compared +@return true if it is hp */ + +bool +HazardPointer::is_hp(const buf_page_t* bpage) +{ + ut_ad(mutex_own(m_mutex)); + ut_ad(!m_hp || buf_pool_from_bpage(m_hp) == m_buf_pool); + ut_ad(!bpage || buf_pool_from_bpage(bpage) == m_buf_pool); + + return(bpage == m_hp); +} + +/** Adjust the value of hp. This happens when some other thread working +on the same list attempts to remove the hp from the list. +@param bpage buffer block to be compared */ + +void +FlushHp::adjust(const buf_page_t* bpage) +{ + ut_ad(bpage != NULL); + + /** We only support reverse traversal for now. */ + if (is_hp(bpage)) { + m_hp = UT_LIST_GET_PREV(list, m_hp); + } + + ut_ad(!m_hp || m_hp->in_flush_list); +} + +/** Adjust the value of hp. This happens when some other thread working +on the same list attempts to remove the hp from the list. +@param bpage buffer block to be compared */ + +void +LRUHp::adjust(const buf_page_t* bpage) +{ + ut_ad(bpage); + + /** We only support reverse traversal for now. */ + if (is_hp(bpage)) { + m_hp = UT_LIST_GET_PREV(LRU, m_hp); + } + + ut_ad(!m_hp || m_hp->in_LRU_list); +} + +/** Selects from where to start a scan. If we have scanned too deep into +the LRU list it resets the value to the tail of the LRU list. +@return buf_page_t from where to start scan. */ + +buf_page_t* +LRUItr::start() +{ + ut_ad(mutex_own(m_mutex)); + + if (!m_hp || m_hp->old) { + m_hp = UT_LIST_GET_LAST(m_buf_pool->LRU); + } + + return(m_hp); +} + /********************************************************************//** Determine if a block is a sentinel for a buffer pool watch. @return TRUE if a sentinel for a buffer pool watch, FALSE if not */ @@ -3909,7 +4008,10 @@ bool buf_page_io_complete( /*=================*/ - buf_page_t* bpage) /*!< in: pointer to the block in question */ + buf_page_t* bpage, /*!< in: pointer to the block in question */ + bool evict) /*!< in: whether or not to evict the page + from LRU list. */ + { enum buf_io_fix io_type; buf_pool_t* buf_pool = buf_pool_from_bpage(bpage); @@ -4090,6 +4192,7 @@ id. */ buf_page_set_io_fix(bpage, BUF_IO_NONE); + buf_page_monitor(bpage, io_type); switch (io_type) { case BUF_IO_READ: @@ -4106,6 +4209,8 @@ BUF_IO_READ); } + mutex_exit(buf_page_get_mutex(bpage)); + break; case BUF_IO_WRITE: @@ -4121,14 +4226,27 @@ buf_pool->stat.n_pages_written++; + /* We decide whether or not to evict the page from the + LRU list based on the flush_type. + * BUF_FLUSH_LIST: don't evict + * BUF_FLUSH_LRU: always evict + * BUF_FLUSH_SINGLE_PAGE: eviction preference is passed + by the caller explicitly. */ + if (buf_page_get_flush_type(bpage) == BUF_FLUSH_LRU) { + evict = true; + } + + mutex_exit(buf_page_get_mutex(bpage)); + if (evict) { + buf_LRU_free_page(bpage, true); + } + break; default: ut_error; } - buf_page_monitor(bpage, io_type); - #ifdef UNIV_DEBUG if (buf_debug_prints) { fprintf(stderr, "Has %s page space %lu page no %lu\n", @@ -4138,7 +4256,6 @@ } #endif /* UNIV_DEBUG */ - mutex_exit(buf_page_get_mutex(bpage)); buf_pool_mutex_exit(buf_pool); return(true); @@ -4220,7 +4337,7 @@ buf_pool_mutex_enter(buf_pool); - while (buf_LRU_scan_and_free_block(buf_pool, TRUE)) { + while (buf_LRU_scan_and_free_block(buf_pool, true)) { } ut_ad(UT_LIST_GET_LEN(buf_pool->LRU) == 0); @@ -4781,10 +4898,9 @@ buf_get_n_pending_read_ios(void) /*============================*/ { - ulint i; ulint pend_ios = 0; - for (i = 0; i < srv_buf_pool_instances; i++) { + for (ulint i = 0; i < srv_buf_pool_instances; i++) { pend_ios += buf_pool_from_array(i)->n_pend_reads; } === modified file 'storage/innobase/buf/buf0flu.cc' --- storage/innobase/buf/buf0flu.cc 2013-08-06 01:41:11 +0000 +++ storage/innobase/buf/buf0flu.cc 2014-01-27 16:10:45 +0000 @@ -59,14 +59,13 @@ doing the shutdown */ UNIV_INTERN ibool buf_page_cleaner_is_active = FALSE; -/** LRU flush batch is further divided into this chunk size to -reduce the wait time for the threads waiting for a clean block */ -#define PAGE_CLEANER_LRU_BATCH_CHUNK_SIZE 100 - #ifdef UNIV_PFS_THREAD UNIV_INTERN mysql_pfs_key_t buf_page_cleaner_thread_key; #endif /* UNIV_PFS_THREAD */ +/** Event to synchronise with the flushing. */ + os_event_t buf_flush_event; + /** If LRU list of a buf_pool is less than this size then LRU eviction should not happen. This is because when we do LRU flushing we also put the blocks on free list. If LRU list is very small then we can end up @@ -130,60 +129,6 @@ } #endif /* UNIV_DEBUG || UNIV_BUF_DEBUG */ -/*******************************************************************//** -Sets hazard pointer during flush_list iteration. */ -UNIV_INLINE -void -buf_flush_set_hp( -/*=============*/ - buf_pool_t* buf_pool,/*!< in/out: buffer pool instance */ - const buf_page_t* bpage) /*!< in: buffer control block */ -{ - ut_ad(buf_flush_list_mutex_own(buf_pool)); - ut_ad(buf_pool->flush_list_hp == NULL || bpage == NULL); - ut_ad(!bpage || buf_page_in_file(bpage)); - ut_ad(!bpage || bpage->in_flush_list); - ut_ad(!bpage || buf_pool_from_bpage(bpage) == buf_pool); - - buf_pool->flush_list_hp = bpage; -} - -/*******************************************************************//** -Checks if the given block is a hazard pointer -@return true if bpage is hazard pointer */ -UNIV_INLINE -bool -buf_flush_is_hp( -/*============*/ - buf_pool_t* buf_pool,/*!< in: buffer pool instance */ - const buf_page_t* bpage) /*!< in: buffer control block */ -{ - ut_ad(buf_flush_list_mutex_own(buf_pool)); - - return(buf_pool->flush_list_hp == bpage); -} - -/*******************************************************************//** -Whenever we move a block in flush_list (either to remove it or to -relocate it) we check the hazard pointer set by some other thread -doing the flush list scan. If the hazard pointer is the same as the -one we are about going to move then we set it to NULL to force a rescan -in the thread doing the batch. */ -UNIV_INLINE -void -buf_flush_update_hp( -/*================*/ - buf_pool_t* buf_pool, /*!< in: buffer pool instance */ - buf_page_t* bpage) /*!< in: buffer control block */ -{ - ut_ad(buf_flush_list_mutex_own(buf_pool)); - - if (buf_flush_is_hp(buf_pool, bpage)) { - buf_flush_set_hp(buf_pool, NULL); - MONITOR_INC(MONITOR_FLUSH_HP_RESCAN); - } -} - /******************************************************************//** Insert a block in the flush_rbt and returns a pointer to its predecessor or NULL if no predecessor. The ordering is maintained @@ -589,6 +534,10 @@ buf_flush_list_mutex_enter(buf_pool); + /* Important that we adjust the hazard pointer before removing + the bpage from flush list. */ + buf_pool->flush_hp.adjust(bpage); + switch (buf_page_get_state(bpage)) { case BUF_BLOCK_POOL_WATCH: case BUF_BLOCK_ZIP_PAGE: @@ -629,7 +578,6 @@ ut_a(buf_flush_validate_skip(buf_pool)); #endif /* UNIV_DEBUG || UNIV_BUF_DEBUG */ - buf_flush_update_hp(buf_pool, bpage); buf_flush_list_mutex_exit(buf_pool); } @@ -680,6 +628,10 @@ prev_b = buf_flush_insert_in_flush_rbt(dpage); } + /* Important that we adjust the hazard pointer before removing + the bpage from the flush list. */ + buf_pool->flush_hp.adjust(bpage); + /* Must be done after we have removed it from the flush_rbt because we assert on in_flush_list in comparison function. */ ut_d(bpage->in_flush_list = FALSE); @@ -708,7 +660,6 @@ ut_a(buf_flush_validate_low(buf_pool)); #endif /* UNIV_DEBUG || UNIV_BUF_DEBUG */ - buf_flush_update_hp(buf_pool, bpage); buf_flush_list_mutex_exit(buf_pool); } @@ -977,7 +928,10 @@ if (sync) { ut_ad(flush_type == BUF_FLUSH_SINGLE_PAGE); fil_flush(buf_page_get_space(bpage)); - buf_page_io_complete(bpage); + + /* true means we want to evict this page from the + LRU list as well. */ + buf_page_io_complete(bpage, true); } /* Increment the counter of I/O operations used @@ -1470,9 +1424,11 @@ It attempts to make 'max' blocks available in the free list. Note that it is a best effort attempt and it is not guaranteed that after a call to this function there will be 'max' blocks in the free list. -@return number of blocks for which the write request was queued. */ +@return pair of numbers where first number is the blocks for which +flush request is queued and second is the number of blocks that were +clean and simply evicted from the LRU. */ static -ulint +std::pair buf_flush_LRU_list_batch( /*=====================*/ buf_pool_t* buf_pool, /*!< in: buffer pool instance */ @@ -1481,64 +1437,60 @@ { buf_page_t* bpage; ulint scanned = 0; - ulint count = 0; + ulint evict_count = 0; + ulint flush_count = 0; ulint free_len = UT_LIST_GET_LEN(buf_pool->free); ulint lru_len = UT_LIST_GET_LEN(buf_pool->LRU); ut_ad(buf_pool_mutex_own(buf_pool)); - bpage = UT_LIST_GET_LAST(buf_pool->LRU); - while (bpage != NULL && count < max - && free_len < srv_LRU_scan_depth - && lru_len > BUF_LRU_MIN_LEN) { + for (bpage = UT_LIST_GET_LAST(buf_pool->LRU); + bpage != NULL && (flush_count + evict_count) < max + && free_len < srv_LRU_scan_depth + && lru_len > BUF_LRU_MIN_LEN; + ++scanned, + bpage = buf_pool->lru_hp.get()) { + + buf_page_t* prev = UT_LIST_GET_PREV(LRU, bpage); + buf_pool->lru_hp.set(prev); ib_mutex_t* block_mutex = buf_page_get_mutex(bpage); - ibool evict; - mutex_enter(block_mutex); - evict = buf_flush_ready_for_replace(bpage); - mutex_exit(block_mutex); - - ++scanned; - - /* If the block is ready to be replaced we try to - free it i.e.: put it on the free list. - Otherwise we try to flush the block and its - neighbors. In this case we'll put it on the - free list in the next pass. We do this extra work - of putting blocks to the free list instead of - just flushing them because after every flush - we have to restart the scan from the tail of - the LRU list and if we don't clear the tail - of the flushed pages then the scan becomes - O(n*n). */ - if (evict) { + + if (buf_flush_ready_for_replace(bpage)) { + /* block is ready for eviction i.e., it is + clean and is not IO-fixed or buffer fixed. */ + mutex_exit(block_mutex); if (buf_LRU_free_page(bpage, true)) { - /* buf_pool->mutex was potentially - released and reacquired. */ - bpage = UT_LIST_GET_LAST(buf_pool->LRU); - } else { - bpage = UT_LIST_GET_PREV(LRU, bpage); + ++evict_count; } - } else if (buf_flush_page_and_try_neighbors( - bpage, - BUF_FLUSH_LRU, max, &count)) { - - /* buf_pool->mutex was released. - Restart the scan. */ - bpage = UT_LIST_GET_LAST(buf_pool->LRU); + } else if (buf_flush_ready_for_flush(bpage, BUF_FLUSH_LRU)) { + /* Block is ready for flush. Dispatch an IO + request. The IO helper thread will put it on + free list in IO completion routine. */ + mutex_exit(block_mutex); + buf_flush_page_and_try_neighbors( + bpage, BUF_FLUSH_LRU, max, &flush_count); } else { - bpage = UT_LIST_GET_PREV(LRU, bpage); + /* Can't evict or dispatch this block. Go to + previous. */ + ut_ad(buf_pool->lru_hp.is_hp(prev)); + mutex_exit(block_mutex); } + ut_ad(!mutex_own(block_mutex)); + ut_ad(buf_pool_mutex_own(buf_pool)); + free_len = UT_LIST_GET_LEN(buf_pool->free); lru_len = UT_LIST_GET_LEN(buf_pool->LRU); } + buf_pool->lru_hp.set(NULL); + /* We keep track of all flushes happening as part of LRU flush. When estimating the desired rate at which flush_list should be flushed, we factor in this value. */ - buf_lru_flush_page_count += count; + buf_lru_flush_page_count += flush_count; ut_ad(buf_pool_mutex_own(buf_pool)); @@ -1550,34 +1502,40 @@ scanned); } - return(count); + return(std::make_pair(flush_count, evict_count)); } /*******************************************************************//** Flush and move pages from LRU or unzip_LRU list to the free list. Whether LRU or unzip_LRU is used depends on the state of the system. -@return number of blocks for which either the write request was queued -or in case of unzip_LRU the number of blocks actually moved to the -free list */ +@return pair of numbers where first number is the blocks for which +flush request is queued and second is the number of blocks that were +uncompressed frames in unzip_LRU and were simply evicted or blocks +that were part of LRU and were clean and simply evicted from the LRU. */ static -ulint +std::pair buf_do_LRU_batch( /*=============*/ buf_pool_t* buf_pool, /*!< in: buffer pool instance */ ulint max) /*!< in: desired number of blocks in the free_list */ { - ulint count = 0; + ulint count = 0; + std::pair res; if (buf_LRU_evict_from_unzip_LRU(buf_pool)) { - count += buf_free_from_unzip_LRU_list_batch(buf_pool, max); + count = buf_free_from_unzip_LRU_list_batch(buf_pool, max); } if (max > count) { - count += buf_flush_LRU_list_batch(buf_pool, max - count); + res = buf_flush_LRU_list_batch(buf_pool, max - count); } - return(count); + /* Add evicted pages from unzip_LRU to the evicted pages from + the simple LRU. */ + res.second += count; + + return(res); } /*******************************************************************//** @@ -1619,6 +1577,7 @@ for (buf_page_t* bpage = UT_LIST_GET_LAST(buf_pool->flush_list); count < min_n && bpage != NULL && len > 0 && bpage->oldest_modification < lsn_limit; + bpage = buf_pool->flush_hp.get(), ++scanned) { buf_page_t* prev; @@ -1627,8 +1586,7 @@ ut_ad(bpage->in_flush_list); prev = UT_LIST_GET_PREV(list, bpage); - buf_flush_set_hp(buf_pool, prev); - + buf_pool->flush_hp.set(prev); buf_flush_list_mutex_exit(buf_pool); #ifdef UNIV_DEBUG @@ -1639,23 +1597,12 @@ buf_flush_list_mutex_enter(buf_pool); - ut_ad(flushed || buf_flush_is_hp(buf_pool, prev)); - - if (!buf_flush_is_hp(buf_pool, prev)) { - /* The hazard pointer was reset by some other - thread. Restart the scan. */ - ut_ad(buf_flush_is_hp(buf_pool, NULL)); - bpage = UT_LIST_GET_LAST(buf_pool->flush_list); - len = UT_LIST_GET_LEN(buf_pool->flush_list); - } else { - bpage = prev; - --len; - buf_flush_set_hp(buf_pool, NULL); - } - - ut_ad(!bpage || bpage->in_flush_list); + ut_ad(flushed || buf_pool->flush_hp.is_hp(prev)); + + --len; } + buf_pool->flush_hp.set(NULL); buf_flush_list_mutex_exit(buf_pool); MONITOR_INC_VALUE_CUMULATIVE(MONITOR_FLUSH_BATCH_SCANNED, @@ -1674,9 +1621,15 @@ pages: to avoid deadlocks, this function must be written so that it cannot end up waiting for these latches! NOTE 2: in the case of a flush list flush, the calling thread is not allowed to own any latches on pages! -@return number of blocks for which the write request was queued */ +@return pair of numbers: + In case of LRU list: + First number = pages flushed + Second number = pages evicted + In case of flush list: + First number = pages flushed + Second number = 0 */ static -ulint +std::pair buf_flush_batch( /*============*/ buf_pool_t* buf_pool, /*!< in: buffer pool instance */ @@ -1693,7 +1646,7 @@ (if their number does not exceed min_n), otherwise ignored */ { - ulint count = 0; + std::pair res; ut_ad(flush_type == BUF_FLUSH_LRU || flush_type == BUF_FLUSH_LIST); #ifdef UNIV_SYNC_DEBUG @@ -1707,10 +1660,11 @@ the flush functions. */ switch (flush_type) { case BUF_FLUSH_LRU: - count = buf_do_LRU_batch(buf_pool, min_n); + res = buf_do_LRU_batch(buf_pool, min_n); break; case BUF_FLUSH_LIST: - count = buf_do_flush_list_batch(buf_pool, min_n, lsn_limit); + res.first = buf_do_flush_list_batch(buf_pool, min_n, lsn_limit); + res.second = 0; break; default: ut_error; @@ -1719,15 +1673,15 @@ buf_pool_mutex_exit(buf_pool); #ifdef UNIV_DEBUG - if (buf_debug_prints && count > 0) { + if (buf_debug_prints && res.first > 0) { fprintf(stderr, flush_type == BUF_FLUSH_LRU ? "Flushed %lu pages in LRU flush\n" : "Flushed %lu pages in flush list flush\n", - (ulong) count); + (ulong) res.first); } #endif /* UNIV_DEBUG */ - return(count); + return(res); } /******************************************************************//** @@ -1836,54 +1790,12 @@ } } else { thd_wait_begin(NULL, THD_WAIT_DISKIO); - os_event_wait(buf_pool->no_flush[type]); + os_event_wait(buf_pool->no_flush[type]); thd_wait_end(NULL); } } /*******************************************************************//** -This utility flushes dirty blocks from the end of the LRU list and also -puts replaceable clean pages from the end of the LRU list to the free -list. -NOTE: The calling thread is not allowed to own any latches on pages! -@return true if a batch was queued successfully. false if another batch -of same type was already running. */ -static -bool -buf_flush_LRU( -/*==========*/ - buf_pool_t* buf_pool, /*!< in/out: buffer pool instance */ - ulint min_n, /*!< in: wished minimum mumber of blocks - flushed (it is not guaranteed that the - actual number is that big, though) */ - ulint* n_processed) /*!< out: the number of pages - which were processed is passed - back to caller. Ignored if NULL */ -{ - ulint page_count; - - if (n_processed) { - *n_processed = 0; - } - - if (!buf_flush_start(buf_pool, BUF_FLUSH_LRU)) { - return(false); - } - - page_count = buf_flush_batch(buf_pool, BUF_FLUSH_LRU, min_n, 0); - - buf_flush_end(buf_pool, BUF_FLUSH_LRU); - - buf_flush_common(BUF_FLUSH_LRU, page_count); - - if (n_processed) { - *n_processed = page_count; - } - - return(true); -} - -/*******************************************************************//** This utility flushes dirty blocks from the end of the flush list of all buffer pool instances. NOTE: The calling thread is not allowed to own any latches on pages! @@ -1925,8 +1837,8 @@ /* Flush to lsn_limit in all buffer pool instances */ for (i = 0; i < srv_buf_pool_instances; i++) { - buf_pool_t* buf_pool; - ulint page_count = 0; + buf_pool_t* buf_pool; + std::pair res; buf_pool = buf_pool_from_array(i); @@ -1946,23 +1858,23 @@ continue; } - page_count = buf_flush_batch( + res = buf_flush_batch( buf_pool, BUF_FLUSH_LIST, min_n, lsn_limit); buf_flush_end(buf_pool, BUF_FLUSH_LIST); - buf_flush_common(BUF_FLUSH_LIST, page_count); + buf_flush_common(BUF_FLUSH_LIST, res.first); if (n_processed) { - *n_processed += page_count; + *n_processed += res.first; } - if (page_count) { + if (res.first) { MONITOR_INC_VALUE_CUMULATIVE( MONITOR_FLUSH_BATCH_TOTAL_PAGE, MONITOR_FLUSH_BATCH_COUNT, MONITOR_FLUSH_BATCH_PAGES, - page_count); + res.first); } } @@ -1970,94 +1882,79 @@ } /******************************************************************//** -This function picks up a single dirty page from the tail of the LRU -list, flushes it, removes it from page_hash and LRU list and puts -it on the free list. It is called from user threads when they are -unable to find a replaceable page at the tail of the LRU list i.e.: -when the background LRU flushing in the page_cleaner thread is not -fast enough to keep pace with the workload. -@return TRUE if success. */ +This function picks up a single page from the tail of the LRU +list, flushes it (if it is dirty), removes it from page_hash and LRU +list and puts it on the free list. It is called from user threads when +they are unable to find a replaceable page at the tail of the LRU +list i.e.: when the background LRU flushing in the page_cleaner thread +is not fast enough to keep pace with the workload. +@return true if success. */ UNIV_INTERN -ibool +bool buf_flush_single_page_from_LRU( /*===========================*/ buf_pool_t* buf_pool) /*!< in/out: buffer pool instance */ { ulint scanned; buf_page_t* bpage; - ib_mutex_t* block_mutex; - ibool freed; - bool evict_zip; + bool freed; buf_pool_mutex_enter(buf_pool); - for (bpage = UT_LIST_GET_LAST(buf_pool->LRU), scanned = 1; - bpage != NULL; - bpage = UT_LIST_GET_PREV(LRU, bpage), ++scanned) { - - block_mutex = buf_page_get_mutex(bpage); + for (bpage = buf_pool->single_scan_itr.start(), + scanned = 0, freed = false; bpage != NULL; ++scanned, + bpage = buf_pool->single_scan_itr.get()) { + + ut_ad(buf_pool_mutex_own(buf_pool)); + + buf_page_t* prev = UT_LIST_GET_PREV(LRU, bpage); + buf_pool->single_scan_itr.set(prev); + + ib_mutex_t* block_mutex = buf_page_get_mutex(bpage); mutex_enter(block_mutex); - if (buf_flush_ready_for_flush(bpage, - BUF_FLUSH_SINGLE_PAGE)) { - /* buf_flush_page() will release the block - mutex */ + + if (buf_flush_ready_for_replace(bpage)) { + /* block is ready for eviction i.e., it is + clean and is not IO-fixed or buffer fixed. */ + mutex_exit(block_mutex); + if (buf_LRU_free_page(bpage, true)) { + buf_pool_mutex_exit(buf_pool); + freed = true; + break; + } + + } else if (buf_flush_ready_for_flush( + bpage, BUF_FLUSH_SINGLE_PAGE)) { + /* Block is ready for flush. Dispatch an IO + request. We'll put it on free list in IO + completion routine. The following call will + release the buffer pool and block mutex. */ + buf_flush_page(buf_pool, bpage, + BUF_FLUSH_SINGLE_PAGE, true); + freed = true; break; + } else { + mutex_exit(block_mutex); } - mutex_exit(block_mutex); + + ut_ad(!mutex_own(block_mutex)); } - MONITOR_INC_VALUE_CUMULATIVE( - MONITOR_LRU_SINGLE_FLUSH_SCANNED, - MONITOR_LRU_SINGLE_FLUSH_SCANNED_NUM_CALL, - MONITOR_LRU_SINGLE_FLUSH_SCANNED_PER_CALL, - scanned); - - if (!bpage) { + if (!freed) { /* Can't find a single flushable page. */ - buf_pool_mutex_exit(buf_pool); - return(FALSE); - } - - /* The following call will release the buffer pool and - block mutex. */ - buf_flush_page(buf_pool, bpage, BUF_FLUSH_SINGLE_PAGE, true); - - /* At this point the page has been written to the disk. - As we are not holding buffer pool or block mutex therefore - we cannot use the bpage safely. It may have been plucked out - of the LRU list by some other thread or it may even have - relocated in case of a compressed page. We need to start - the scan of LRU list again to remove the block from the LRU - list and put it on the free list. */ - buf_pool_mutex_enter(buf_pool); - - for (bpage = UT_LIST_GET_LAST(buf_pool->LRU); - bpage != NULL; - bpage = UT_LIST_GET_PREV(LRU, bpage)) { - - ibool ready; - - block_mutex = buf_page_get_mutex(bpage); - mutex_enter(block_mutex); - ready = buf_flush_ready_for_replace(bpage); - mutex_exit(block_mutex); - if (ready) { - break; - } - - } - - if (!bpage) { - /* Can't find a single replaceable page. */ - buf_pool_mutex_exit(buf_pool); - return(FALSE); - } - - evict_zip = !buf_LRU_evict_from_unzip_LRU(buf_pool);; - - freed = buf_LRU_free_page(bpage, evict_zip); - buf_pool_mutex_exit(buf_pool); - + ut_ad(!bpage); + buf_pool_mutex_exit(buf_pool); + } + + if (scanned) { + MONITOR_INC_VALUE_CUMULATIVE( + MONITOR_LRU_SINGLE_FLUSH_SCANNED, + MONITOR_LRU_SINGLE_FLUSH_SCANNED_NUM_CALL, + MONITOR_LRU_SINGLE_FLUSH_SCANNED_PER_CALL, + scanned); + } + + ut_ad(!buf_pool_mutex_own(buf_pool)); return(freed); } @@ -2067,18 +1964,19 @@ * Flush dirty pages at the tail of LRU to the disk The depth to which we scan each buffer pool is controlled by dynamic config parameter innodb_LRU_scan_depth. -@return total pages flushed */ +@return total pages processed. */ UNIV_INTERN ulint buf_flush_LRU_tail(void) /*====================*/ { - ulint total_flushed = 0; + ulint total_processed = 0; for (ulint i = 0; i < srv_buf_pool_instances; i++) { - buf_pool_t* buf_pool = buf_pool_from_array(i); - ulint scan_depth; + buf_pool_t* buf_pool = buf_pool_from_array(i); + std::pair res; + ulint scan_depth; /* srv_LRU_scan_depth can be arbitrarily large value. We cap it with current LRU size. */ @@ -2088,47 +1986,40 @@ scan_depth = ut_min(srv_LRU_scan_depth, scan_depth); - /* We divide LRU flush into smaller chunks because - there may be user threads waiting for the flush to - end in buf_LRU_get_free_block(). */ - for (ulint j = 0; - j < scan_depth; - j += PAGE_CLEANER_LRU_BATCH_CHUNK_SIZE) { - - ulint n_flushed = 0; - - /* Currently page_cleaner is the only thread - that can trigger an LRU flush. It is possible - that a batch triggered during last iteration is - still running, */ - if (buf_flush_LRU(buf_pool, - PAGE_CLEANER_LRU_BATCH_CHUNK_SIZE, - &n_flushed)) { - - /* Allowed only one batch per - buffer pool instance. */ - buf_flush_wait_batch_end( - buf_pool, BUF_FLUSH_LRU); - } - - if (n_flushed) { - total_flushed += n_flushed; - } else { - /* Nothing to flush */ - break; - } - } - } - - if (total_flushed) { - MONITOR_INC_VALUE_CUMULATIVE( - MONITOR_LRU_BATCH_TOTAL_PAGE, - MONITOR_LRU_BATCH_COUNT, - MONITOR_LRU_BATCH_PAGES, - total_flushed); - } - - return(total_flushed); + /* Currently page_cleaner is the only thread + that can trigger an LRU flush. It is possible + that a batch triggered during last iteration is + still running, */ + if (!buf_flush_start(buf_pool, BUF_FLUSH_LRU)) { + continue; + } + + res = buf_flush_batch(buf_pool, BUF_FLUSH_LRU, scan_depth, 0); + + buf_flush_end(buf_pool, BUF_FLUSH_LRU); + + buf_flush_common(BUF_FLUSH_LRU, res.first); + + if (res.first) { + MONITOR_INC_VALUE_CUMULATIVE( + MONITOR_LRU_BATCH_FLUSH_TOTAL_PAGE, + MONITOR_LRU_BATCH_FLUSH_COUNT, + MONITOR_LRU_BATCH_FLUSH_PAGES, + res.first); + } + + if (res.second) { + MONITOR_INC_VALUE_CUMULATIVE( + MONITOR_LRU_BATCH_EVICT_TOTAL_PAGE, + MONITOR_LRU_BATCH_EVICT_COUNT, + MONITOR_LRU_BATCH_EVICT_PAGES, + res.second); + } + + total_processed += (res.first + res.second); + } + + return(total_processed); } /*********************************************************************//** @@ -2372,11 +2263,14 @@ if (next_loop_time > cur_time) { /* Get sleep interval in micro seconds. We use - ut_min() to avoid long sleep in case of - wrap around. */ - os_thread_sleep(ut_min(1000000, - (next_loop_time - cur_time) - * 1000)); + ut_min() to avoid long sleep in case of wrap around. */ + ulint sleep_us; + + sleep_us = ut_min(1000000, (next_loop_time - cur_time) * 1000); + + ib_int64_t sig_count = os_event_reset(buf_flush_event); + + os_event_wait_time_low(buf_flush_event, sleep_us, sig_count); } } @@ -2417,6 +2311,7 @@ if (srv_check_activity(last_activity) || buf_get_n_pending_read_ios() || n_flushed == 0) { + page_cleaner_sleep_if_needed(next_loop_time); } @@ -2432,8 +2327,7 @@ n_flushed += page_cleaner_flush_pages_if_needed(); } else { n_flushed = page_cleaner_do_flush_batch( - PCT_IO(100), - LSN_MAX); + PCT_IO(100), LSN_MAX); if (n_flushed) { MONITOR_INC_VALUE_CUMULATIVE( @@ -2500,6 +2394,7 @@ /* Some sanity checks */ ut_a(srv_get_active_thread_type() == SRV_NONE); ut_a(srv_shutdown_state == SRV_SHUTDOWN_FLUSH_PHASE); + for (ulint i = 0; i < srv_buf_pool_instances; i++) { buf_pool_t* buf_pool = buf_pool_from_array(i); ut_a(UT_LIST_GET_LEN(buf_pool->flush_list) == 0); @@ -2510,6 +2405,8 @@ thread_exit: buf_page_cleaner_is_active = FALSE; + os_event_free(buf_flush_event); + /* We count the number of threads in os_thread_exit(). A created thread should always use that to exit and not use return() to exit. */ os_thread_exit(NULL); === modified file 'storage/innobase/buf/buf0lru.cc' --- storage/innobase/buf/buf0lru.cc 2013-07-05 05:04:41 +0000 +++ storage/innobase/buf/buf0lru.cc 2014-01-27 16:09:27 +0000 @@ -81,6 +81,10 @@ buffer pools. */ #define BUF_LRU_DROP_SEARCH_SIZE 1024 +/** We scan these many blocks when looking for a clean page to evict +during LRU eviction. */ +#define BUF_LRU_SEARCH_SCAN_THRESHOLD 100 + /** If we switch on the InnoDB monitor because there are too few available frames in the buffer pool, we set this to TRUE */ static ibool buf_lru_switched_on_innodb_mon = FALSE; @@ -947,14 +951,14 @@ /******************************************************************//** Try to free an uncompressed page of a compressed block from the unzip LRU list. The compressed page is preserved, and it need not be clean. -@return TRUE if freed */ +@return true if freed */ UNIV_INLINE -ibool +bool buf_LRU_free_from_unzip_LRU_list( /*=============================*/ buf_pool_t* buf_pool, /*!< in: buffer pool instance */ - ibool scan_all) /*!< in: scan whole LRU list - if TRUE, otherwise scan only + bool scan_all) /*!< in: scan whole LRU list + if true, otherwise scan only srv_LRU_scan_depth / 2 blocks. */ { buf_block_t* block; @@ -968,7 +972,7 @@ } for (block = UT_LIST_GET_LAST(buf_pool->unzip_LRU), - scanned = 1, freed = FALSE; + scanned = 0, freed = FALSE; block != NULL && !freed && (scan_all || scanned < srv_LRU_scan_depth); ++scanned) { @@ -985,24 +989,26 @@ block = prev_block; } - MONITOR_INC_VALUE_CUMULATIVE( - MONITOR_LRU_UNZIP_SEARCH_SCANNED, - MONITOR_LRU_UNZIP_SEARCH_SCANNED_NUM_CALL, - MONITOR_LRU_UNZIP_SEARCH_SCANNED_PER_CALL, - scanned); + if (scanned) { + MONITOR_INC_VALUE_CUMULATIVE( + MONITOR_LRU_UNZIP_SEARCH_SCANNED, + MONITOR_LRU_UNZIP_SEARCH_SCANNED_NUM_CALL, + MONITOR_LRU_UNZIP_SEARCH_SCANNED_PER_CALL, + scanned); + } return(freed); } /******************************************************************//** Try to free a clean page from the common LRU list. -@return TRUE if freed */ +@return true if freed */ UNIV_INLINE -ibool +bool buf_LRU_free_from_common_LRU_list( /*==============================*/ buf_pool_t* buf_pool, /*!< in: buffer pool instance */ - ibool scan_all) /*!< in: scan whole LRU list - if TRUE, otherwise scan only + bool scan_all) /*!< in: scan whole LRU list + if true, otherwise scan only srv_LRU_scan_depth / 2 blocks. */ { buf_page_t* bpage; @@ -1011,21 +1017,29 @@ ut_ad(buf_pool_mutex_own(buf_pool)); - for (bpage = UT_LIST_GET_LAST(buf_pool->LRU), - scanned = 1, freed = FALSE; - bpage != NULL && !freed - && (scan_all || scanned < srv_LRU_scan_depth); - ++scanned) { - - unsigned accessed; - buf_page_t* prev_bpage = UT_LIST_GET_PREV(LRU, - bpage); - - ut_ad(buf_page_in_file(bpage)); - ut_ad(bpage->in_LRU_list); - - accessed = buf_page_is_accessed(bpage); - freed = buf_LRU_free_page(bpage, true); + for (bpage = buf_pool->lru_scan_itr.start(), + scanned = 0, freed = false; bpage != NULL && !freed + && (scan_all || scanned < BUF_LRU_SEARCH_SCAN_THRESHOLD); + ++scanned, bpage = buf_pool->lru_scan_itr.get()) { + + buf_page_t* prev = UT_LIST_GET_PREV(LRU, bpage); + buf_pool->lru_scan_itr.set(prev); + + ib_mutex_t* mutex = buf_page_get_mutex(bpage); + mutex_enter(mutex); + + ut_ad(buf_page_in_file(bpage)); + ut_ad(bpage->in_LRU_list); + + unsigned accessed = buf_page_is_accessed(bpage); + + if (buf_flush_ready_for_replace(bpage)) { + mutex_exit(mutex); + freed = buf_LRU_free_page(bpage, true); + } else { + mutex_exit(mutex); + } + if (freed && !accessed) { /* Keep track of pages that are evicted without ever being accessed. This gives us a measure of @@ -1033,28 +1047,31 @@ ++buf_pool->stat.n_ra_pages_evicted; } - bpage = prev_bpage; + ut_ad(buf_pool_mutex_own(buf_pool)); + ut_ad(!mutex_own(mutex)); } - MONITOR_INC_VALUE_CUMULATIVE( - MONITOR_LRU_SEARCH_SCANNED, - MONITOR_LRU_SEARCH_SCANNED_NUM_CALL, - MONITOR_LRU_SEARCH_SCANNED_PER_CALL, - scanned); + if (scanned) { + MONITOR_INC_VALUE_CUMULATIVE( + MONITOR_LRU_SEARCH_SCANNED, + MONITOR_LRU_SEARCH_SCANNED_NUM_CALL, + MONITOR_LRU_SEARCH_SCANNED_PER_CALL, + scanned); + } return(freed); } /******************************************************************//** Try to free a replaceable block. -@return TRUE if found and freed */ +@return true if found and freed */ UNIV_INTERN -ibool +bool buf_LRU_scan_and_free_block( /*========================*/ buf_pool_t* buf_pool, /*!< in: buffer pool instance */ - ibool scan_all) /*!< in: scan whole LRU list - if TRUE, otherwise scan only + bool scan_all) /*!< in: scan whole LRU list + if true, otherwise scan only 'old' blocks. */ { ut_ad(buf_pool_mutex_own(buf_pool)); @@ -1224,8 +1241,6 @@ we put it to free list to be used. * iteration 0: * get a block from free list, success:done - * if there is an LRU flush batch in progress: - * wait for batch to end: retry free list * if buf_pool->try_LRU_scan is set * scan LRU up to srv_LRU_scan_depth to find a clean block * the above will put the block on free list @@ -1238,7 +1253,7 @@ * scan whole LRU list * scan LRU list even if buf_pool->try_LRU_scan is not set * iteration > 1: - * same as iteration 1 but sleep 100ms + * same as iteration 1 but sleep 10ms @return the free control block, in state BUF_BLOCK_READY_FOR_USE */ UNIV_INTERN buf_block_t* @@ -1247,7 +1262,7 @@ buf_pool_t* buf_pool) /*!< in/out: buffer pool instance */ { buf_block_t* block = NULL; - ibool freed = FALSE; + bool freed = false; ulint n_iterations = 0; ulint flush_failures = 0; ibool mon_value_was = FALSE; @@ -1275,21 +1290,7 @@ return(block); } - if (buf_pool->init_flush[BUF_FLUSH_LRU] - && srv_use_doublewrite_buf - && buf_dblwr != NULL) { - - /* If there is an LRU flush happening in the background - then we wait for it to end instead of trying a single - page flush. If, however, we are not using doublewrite - buffer then it is better to do our own single page - flush instead of waiting for LRU flush to end. */ - buf_pool_mutex_exit(buf_pool); - buf_flush_wait_batch_end(buf_pool, BUF_FLUSH_LRU); - goto loop; - } - - freed = FALSE; + freed = false; if (buf_pool->try_LRU_scan || n_iterations > 0) { /* If no block was in the free list, search from the end of the LRU list and try to free a block there. @@ -1305,6 +1306,10 @@ TRUE again when we flush a batch from this buffer pool. */ buf_pool->try_LRU_scan = FALSE; + + /* Also tell the page_cleaner thread that + there is work for it to do. */ + os_event_set(buf_flush_event); } } @@ -1316,33 +1321,25 @@ } if (n_iterations > 20) { - ut_print_timestamp(stderr); - fprintf(stderr, - " InnoDB: Warning: difficult to find free blocks in\n" - "InnoDB: the buffer pool (%lu search iterations)!\n" - "InnoDB: %lu failed attempts to flush a page!" - " Consider\n" - "InnoDB: increasing the buffer pool size.\n" - "InnoDB: It is also possible that" - " in your Unix version\n" - "InnoDB: fsync is very slow, or" - " completely frozen inside\n" - "InnoDB: the OS kernel. Then upgrading to" - " a newer version\n" - "InnoDB: of your operating system may help." - " Look at the\n" - "InnoDB: number of fsyncs in diagnostic info below.\n" - "InnoDB: Pending flushes (fsync) log: %lu;" - " buffer pool: %lu\n" - "InnoDB: %lu OS file reads, %lu OS file writes," - " %lu OS fsyncs\n" - "InnoDB: Starting InnoDB Monitor to print further\n" - "InnoDB: diagnostics to the standard output.\n", + ib_logf(IB_LOG_LEVEL_WARN, + "Difficult to find free blocks in the buffer pool " + "(%lu search iterations)! %lu failed attempts to " + "flush a page! Consider increasing the buffer pool " + "size. It is also possible that in your Unix version " + "fsync is very slow, or completely frozen inside " + "the OS kernel. Then upgrading to a newer version " + "of your operating system may help. Look at the " + "number of fsyncs in diagnostic info below. " + "Pending flushes (fsync) log: %lu; buffer pool: " + "%lu. %lu OS file reads, %lu OS file writes, " + "%lu OS fsyncs. Starting InnoDB Monitor to print " + "further diagnostics to the standard output.", (ulong) n_iterations, (ulong) flush_failures, (ulong) fil_n_pending_log_flushes, (ulong) fil_n_pending_tablespace_flushes, - (ulong) os_n_file_reads, (ulong) os_n_file_writes, + (ulong) os_n_file_reads, + (ulong) os_n_file_writes, (ulong) os_n_fsyncs); mon_value_was = srv_print_innodb_monitor; @@ -1353,23 +1350,23 @@ /* If we have scanned the whole LRU and still are unable to find a free block then we should sleep here to let the - page_cleaner do an LRU batch for us. - TODO: It'd be better if we can signal the page_cleaner. Perhaps - we should use timed wait for page_cleaner. */ + page_cleaner do an LRU batch for us. */ + if (n_iterations > 1) { - - os_thread_sleep(100000); + os_thread_sleep(10000); } /* No free block was found: try to flush the LRU list. This call will flush one page from the LRU and put it on the free list. That means that the free block is up for grabs for all user threads. + TODO: A more elegant way would have been to return the freed up block to the caller here but the code that deals with removing the block from page_hash and LRU_list is fairly involved (particularly in case of compressed pages). We can do that in a separate patch sometime in future. */ + if (!buf_flush_single_page_from_LRU(buf_pool)) { MONITOR_INC(MONITOR_LRU_SINGLE_FLUSH_FAILURE_COUNT); ++flush_failures; @@ -1509,6 +1506,20 @@ } /******************************************************************//** +Adjust LRU hazard pointers if needed. */ + +void +buf_LRU_adjust_hp( +/*==============*/ + buf_pool_t* buf_pool,/*!< in: buffer pool instance */ + const buf_page_t* bpage) /*!< in: control block */ +{ + buf_pool->lru_hp.adjust(bpage); + buf_pool->lru_scan_itr.adjust(bpage); + buf_pool->single_scan_itr.adjust(bpage); +} + +/******************************************************************//** Removes a block from the LRU list. */ UNIV_INLINE void @@ -1527,6 +1538,10 @@ ut_ad(bpage->in_LRU_list); + /* Important that we adjust the hazard pointers before removing + bpage from the LRU list. */ + buf_LRU_adjust_hp(buf_pool, bpage); + /* If the LRU_old pointer is defined and points to just this block, move it backward one step */ === modified file 'storage/innobase/include/buf0buf.h' --- storage/innobase/include/buf0buf.h 2013-04-30 11:45:16 +0000 +++ storage/innobase/include/buf0buf.h 2014-01-27 16:09:27 +0000 @@ -1172,7 +1172,9 @@ bool buf_page_io_complete( /*=================*/ - buf_page_t* bpage); /*!< in: pointer to the block in question */ + buf_page_t* bpage, /*!< in: pointer to the block in question */ + bool evict = false);/*!< in: whether or not to evict + the page from LRU list. */ /********************************************************************//** Calculates a folded value of a file page address to use in the page hash table. @@ -1726,6 +1728,133 @@ #define BUF_POOL_ZIP_FOLD_BPAGE(b) BUF_POOL_ZIP_FOLD((buf_block_t*) (b)) /* @} */ +/** A "Hazard Pointer" class used to iterate over page lists +inside the buffer pool. A hazard pointer is a buf_page_t pointer +which we intend to iterate over next and we want it remain valid +even after we release the buffer pool mutex. */ +class HazardPointer { + +public: + /** Constructor + @param buf_pool buffer pool instance + @param mutex mutex that is protecting the hp. */ + HazardPointer(const buf_pool_t* buf_pool, const ib_mutex_t* mutex) + : + m_buf_pool(buf_pool) +#ifdef UNIV_DEBUG + , m_mutex(mutex) +#endif /* UNIV_DEBUG */ + , m_hp() {} + + /** Destructor */ + virtual ~HazardPointer() {} + + /** Get current value */ + buf_page_t* get() + { + ut_ad(mutex_own(m_mutex)); + return(m_hp); + } + + /** Set current value + @param bpage buffer block to be set as hp */ + void set(buf_page_t* bpage); + + /** Checks if a bpage is the hp + @param bpage buffer block to be compared + @return true if it is hp */ + bool is_hp(const buf_page_t* bpage); + + /** Adjust the value of hp. This happens when some + other thread working on the same list attempts to + remove the hp from the list. Must be implemented + by the derived classes. + @param bpage buffer block to be compared */ + virtual void adjust(const buf_page_t*) = 0; + +protected: + /** Disable copying */ + HazardPointer(const HazardPointer&); + HazardPointer& operator=(const HazardPointer&); + + /** Buffer pool instance */ + const buf_pool_t* m_buf_pool; + +#if UNIV_DEBUG + /** mutex that protects access to the m_hp. */ + const ib_mutex_t* m_mutex; +#endif /* UNIV_DEBUG */ + + /** hazard pointer. */ + buf_page_t* m_hp; +}; + +/** Class implementing buf_pool->flush_list hazard pointer */ +class FlushHp: public HazardPointer { + +public: + /** Constructor + @param buf_pool buffer pool instance + @param mutex mutex that is protecting the hp. */ + FlushHp(const buf_pool_t* buf_pool, const ib_mutex_t* mutex) + : + HazardPointer(buf_pool, mutex) {} + + /** Destructor */ + virtual ~FlushHp() {} + + /** Adjust the value of hp. This happens when some + other thread working on the same list attempts to + remove the hp from the list. + @param bpage buffer block to be compared */ + void adjust(const buf_page_t* bpage); +}; + +/** Class implementing buf_pool->LRU hazard pointer */ +class LRUHp: public HazardPointer { + +public: + /** Constructor + @param buf_pool buffer pool instance + @param mutex mutex that is protecting the hp. */ + LRUHp(const buf_pool_t* buf_pool, const ib_mutex_t* mutex) + : + HazardPointer(buf_pool, mutex) {} + + /** Destructor */ + virtual ~LRUHp() {} + + /** Adjust the value of hp. This happens when some + other thread working on the same list attempts to + remove the hp from the list. + @param bpage buffer block to be compared */ + void adjust(const buf_page_t* bpage); +}; + +/** Special purpose iterators to be used when scanning the LRU list. +The idea is that when one thread finishes the scan it leaves the +itr in that position and the other thread can start scan from +there */ +class LRUItr: public LRUHp { + +public: + /** Constructor + @param buf_pool buffer pool instance + @param mutex mutex that is protecting the hp. */ + LRUItr(const buf_pool_t* buf_pool, const ib_mutex_t* mutex) + : + LRUHp(buf_pool, mutex) {} + + /** Destructor */ + virtual ~LRUItr() {} + + /** Selects from where to start a scan. If we have scanned + too deep into the LRU list it resets the value to the tail + of the LRU list. + @return buf_page_t from where to start scan. */ + buf_page_t* start(); +}; + /** Struct that is embedded in the free zip blocks */ struct buf_buddy_free_t { union { @@ -1858,7 +1987,7 @@ also protects writes to bpage::oldest_modification and flush_list_hp */ - const buf_page_t* flush_list_hp;/*!< "hazard pointer" + FlushHp flush_hp;/*!< "hazard pointer" used during scan of flush_list while doing flush list batch. Protected by flush_list_mutex */ @@ -1916,8 +2045,22 @@ UT_LIST_BASE_NODE_T(buf_page_t) free; /*!< base node of the free block list */ + + /** "hazard pointer" used during scan of LRU while doing + LRU list batch. Protected by buf_pool::mutex */ + LRUHp lru_hp; + + /** Iterator used to scan the LRU list when searching for + replacable victim. Protected by buf_pool::mutex. */ + LRUItr lru_scan_itr; + + /** Iterator used to scan the LRU list when searching for + single page flushing victim. Protected by buf_pool::mutex. */ + LRUItr single_scan_itr; + UT_LIST_BASE_NODE_T(buf_page_t) LRU; /*!< base node of the LRU list */ + buf_page_t* LRU_old; /*!< pointer to the about LRU_old_ratio/BUF_LRU_OLD_RATIO_DIV oldest blocks in the LRU list; === modified file 'storage/innobase/include/buf0flu.h' --- storage/innobase/include/buf0flu.h 2013-06-10 20:44:22 +0000 +++ storage/innobase/include/buf0flu.h 2014-01-27 16:09:27 +0000 @@ -36,6 +36,9 @@ /** Flag indicating if the page_cleaner is in active state. */ extern ibool buf_page_cleaner_is_active; +/** Event to synchronise with the flushing. */ +extern os_event_t buf_flush_event; + /********************************************************************//** Remove a block from the flush list of modified blocks. */ UNIV_INTERN @@ -110,15 +113,15 @@ which were processed is passed back to caller. Ignored if NULL */ /******************************************************************//** -This function picks up a single dirty page from the tail of the LRU -list, flushes it, removes it from page_hash and LRU list and puts -it on the free list. It is called from user threads when they are -unable to find a replacable page at the tail of the LRU list i.e.: -when the background LRU flushing in the page_cleaner thread is not -fast enough to keep pace with the workload. -@return TRUE if success. */ +This function picks up a single page from the tail of the LRU +list, flushes it (if it is dirty), removes it from page_hash and LRU +list and puts it on the free list. It is called from user threads when +they are unable to find a replaceable page at the tail of the LRU +list i.e.: when the background LRU flushing in the page_cleaner thread +is not fast enough to keep pace with the workload. +@return true if success. */ UNIV_INTERN -ibool +bool buf_flush_single_page_from_LRU( /*===========================*/ buf_pool_t* buf_pool); /*!< in/out: buffer pool instance */ === modified file 'storage/innobase/include/buf0lru.h' --- storage/innobase/include/buf0lru.h 2013-06-10 20:44:22 +0000 +++ storage/innobase/include/buf0lru.h 2014-01-27 16:09:27 +0000 @@ -96,14 +96,14 @@ __attribute__((nonnull)); /******************************************************************//** Try to free a replaceable block. -@return TRUE if found and freed */ +@return true if found and freed */ UNIV_INTERN -ibool +bool buf_LRU_scan_and_free_block( /*========================*/ buf_pool_t* buf_pool, /*!< in: buffer pool instance */ - ibool scan_all) /*!< in: scan whole LRU list - if TRUE, otherwise scan only + bool scan_all) /*!< in: scan whole LRU list + if true, otherwise scan only 'old' blocks. */ __attribute__((nonnull,warn_unused_result)); /******************************************************************//** @@ -117,7 +117,7 @@ buf_pool_t* buf_pool); /*!< buffer pool instance */ /******************************************************************//** Returns a free block from the buf_pool. The block is taken off the -free list. If it is empty, blocks are moved from the end of the +free list. If free list is empty, blocks are moved from the end of the LRU list to the free list. This function is called from a user thread when it needs a clean block to read in a page. Note that we only ever get a block from @@ -125,8 +125,6 @@ we put it to free list to be used. * iteration 0: * get a block from free list, success:done - * if there is an LRU flush batch in progress: - * wait for batch to end: retry free list * if buf_pool->try_LRU_scan is set * scan LRU up to srv_LRU_scan_depth to find a clean block * the above will put the block on free list @@ -139,7 +137,7 @@ * scan whole LRU list * scan LRU list even if buf_pool->try_LRU_scan is not set * iteration > 1: - * same as iteration 1 but sleep 100ms + * same as iteration 1 but sleep 10ms @return the free control block, in state BUF_BLOCK_READY_FOR_USE */ UNIV_INTERN buf_block_t* @@ -231,6 +229,15 @@ may or may not be a hash index to the page */ __attribute__((nonnull)); +/******************************************************************//** +Adjust LRU hazard pointers if needed. */ + +void +buf_LRU_adjust_hp( +/*==============*/ + buf_pool_t* buf_pool,/*!< in: buffer pool instance */ + const buf_page_t* bpage); /*!< in: control block */ + #if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG /**********************************************************************//** Validates the LRU list. === modified file 'storage/innobase/include/srv0mon.h' --- storage/innobase/include/srv0mon.h 2013-09-13 02:08:28 +0000 +++ storage/innobase/include/srv0mon.h 2014-01-27 16:09:27 +0000 @@ -169,7 +169,6 @@ MONITOR_FLUSH_BATCH_SCANNED, MONITOR_FLUSH_BATCH_SCANNED_NUM_CALL, MONITOR_FLUSH_BATCH_SCANNED_PER_CALL, - MONITOR_FLUSH_HP_RESCAN, MONITOR_FLUSH_BATCH_TOTAL_PAGE, MONITOR_FLUSH_BATCH_COUNT, MONITOR_FLUSH_BATCH_PAGES, @@ -194,9 +193,12 @@ MONITOR_LRU_BATCH_SCANNED, MONITOR_LRU_BATCH_SCANNED_NUM_CALL, MONITOR_LRU_BATCH_SCANNED_PER_CALL, - MONITOR_LRU_BATCH_TOTAL_PAGE, - MONITOR_LRU_BATCH_COUNT, - MONITOR_LRU_BATCH_PAGES, + MONITOR_LRU_BATCH_FLUSH_TOTAL_PAGE, + MONITOR_LRU_BATCH_FLUSH_COUNT, + MONITOR_LRU_BATCH_FLUSH_PAGES, + MONITOR_LRU_BATCH_EVICT_TOTAL_PAGE, + MONITOR_LRU_BATCH_EVICT_COUNT, + MONITOR_LRU_BATCH_EVICT_PAGES, MONITOR_LRU_SINGLE_FLUSH_SCANNED, MONITOR_LRU_SINGLE_FLUSH_SCANNED_NUM_CALL, MONITOR_LRU_SINGLE_FLUSH_SCANNED_PER_CALL, === modified file 'storage/innobase/log/log0recv.cc' --- storage/innobase/log/log0recv.cc 2013-06-10 20:44:22 +0000 +++ storage/innobase/log/log0recv.cc 2014-01-27 16:09:27 +0000 @@ -341,7 +341,10 @@ while (srv_shutdown_state == SRV_SHUTDOWN_NONE) { - os_thread_sleep(100000); + /* Wait till we get a signal to clean the LRU list. + Bounded by max wait time of 100ms. */ + ib_int64_t sig_count = os_event_reset(buf_flush_event); + os_event_wait_time_low(buf_flush_event, 100000, sig_count); mutex_enter(&recv_sys->writer_mutex); === modified file 'storage/innobase/srv/srv0mon.cc' --- storage/innobase/srv/srv0mon.cc 2013-09-13 02:08:28 +0000 +++ storage/innobase/srv/srv0mon.cc 2014-01-27 16:09:27 +0000 @@ -325,11 +325,6 @@ MONITOR_SET_MEMBER, MONITOR_FLUSH_BATCH_SCANNED, MONITOR_FLUSH_BATCH_SCANNED_PER_CALL}, - {"buffer_flush_batch_rescan", "buffer", - "Number of times rescan of flush list forced", - MONITOR_NONE, - MONITOR_DEFAULT_START, MONITOR_FLUSH_HP_RESCAN}, - /* Cumulative counter for pages flushed in flush batches */ {"buffer_flush_batch_total_pages", "buffer", "Total pages flushed as part of flush batch", @@ -457,20 +452,36 @@ MONITOR_LRU_BATCH_SCANNED_PER_CALL}, /* Cumulative counter for LRU batch pages flushed */ - {"buffer_LRU_batch_total_pages", "buffer", + {"buffer_LRU_batch_flush_total_pages", "buffer", "Total pages flushed as part of LRU batches", - MONITOR_SET_OWNER, MONITOR_LRU_BATCH_COUNT, - MONITOR_LRU_BATCH_TOTAL_PAGE}, - - {"buffer_LRU_batches", "buffer", - "Number of LRU batches", - MONITOR_SET_MEMBER, MONITOR_LRU_BATCH_TOTAL_PAGE, - MONITOR_LRU_BATCH_COUNT}, - - {"buffer_LRU_batch_pages", "buffer", - "Pages queued as an LRU batch", - MONITOR_SET_MEMBER, MONITOR_LRU_BATCH_TOTAL_PAGE, - MONITOR_LRU_BATCH_PAGES}, + MONITOR_SET_OWNER, MONITOR_LRU_BATCH_FLUSH_COUNT, + MONITOR_LRU_BATCH_FLUSH_TOTAL_PAGE}, + + {"buffer_LRU_batches_flush", "buffer", + "Number of LRU batches", + MONITOR_SET_MEMBER, MONITOR_LRU_BATCH_FLUSH_TOTAL_PAGE, + MONITOR_LRU_BATCH_FLUSH_COUNT}, + + {"buffer_LRU_batch_flush_pages", "buffer", + "Pages queued as an LRU batch", + MONITOR_SET_MEMBER, MONITOR_LRU_BATCH_FLUSH_TOTAL_PAGE, + MONITOR_LRU_BATCH_FLUSH_PAGES}, + + /* Cumulative counter for LRU batch pages flushed */ + {"buffer_LRU_batch_evict_total_pages", "buffer", + "Total pages evicted as part of LRU batches", + MONITOR_SET_OWNER, MONITOR_LRU_BATCH_EVICT_COUNT, + MONITOR_LRU_BATCH_EVICT_TOTAL_PAGE}, + + {"buffer_LRU_batches_evict", "buffer", + "Number of LRU batches", + MONITOR_SET_MEMBER, MONITOR_LRU_BATCH_EVICT_TOTAL_PAGE, + MONITOR_LRU_BATCH_EVICT_COUNT}, + + {"buffer_LRU_batch_evict_pages", "buffer", + "Pages queued as an LRU batch", + MONITOR_SET_MEMBER, MONITOR_LRU_BATCH_EVICT_TOTAL_PAGE, + MONITOR_LRU_BATCH_EVICT_PAGES}, /* Cumulative counter for single page LRU scans */ {"buffer_LRU_single_flush_scanned", "buffer",