Commit a03dd94b authored by Jan Lindström's avatar Jan Lindström

MDEV-6936: Buffer pool list scan optimization

Merged Facebook commit 617aef9f911d825e9053f3d611d0389e02031225
authored by Inaam Rana to InnoDB storage engine (not XtraDB)
from https://github.com/facebook/mysql-5.6

WL#7047 - Optimize buffer pool list scans and related batch processing

Reduce excessive scanning of pages when doing flush list batches. The
fix is to introduce the concept of "Hazard Pointer", this reduces the
time complexity of the scan from O(n*n) to O.

The concept of hazard pointer is reversed in this work. Academically
hazard pointer is a pointer that the thread working on it will declar
such and as long as that thread is not done no other thread is allowe
do anything with it.

In this WL we declare the pointer as a hazard pointer and then if any
thread attempts to work on it, it is allowed to do so but it has to a
the hazard pointer to the next valid value. We use hazard pointer sol
reverse traversal of lists within a buffer pool instance.

Add an event to control the background flush thread. The background f
thread wait has been converted to an os event timed wait so that it c
signalled by threads that want to kick start a background flush when
buffer pool is running low on free/dirty pages.
parent 84de2770
...@@ -47,7 +47,6 @@ buffer_data_written disabled ...@@ -47,7 +47,6 @@ buffer_data_written disabled
buffer_flush_batch_scanned disabled buffer_flush_batch_scanned disabled
buffer_flush_batch_num_scan disabled buffer_flush_batch_num_scan disabled
buffer_flush_batch_scanned_per_call disabled buffer_flush_batch_scanned_per_call disabled
buffer_flush_batch_rescan disabled
buffer_flush_batch_total_pages disabled buffer_flush_batch_total_pages disabled
buffer_flush_batches disabled buffer_flush_batches disabled
buffer_flush_batch_pages disabled buffer_flush_batch_pages disabled
...@@ -72,9 +71,12 @@ buffer_flush_background_pages disabled ...@@ -72,9 +71,12 @@ buffer_flush_background_pages disabled
buffer_LRU_batch_scanned disabled buffer_LRU_batch_scanned disabled
buffer_LRU_batch_num_scan disabled buffer_LRU_batch_num_scan disabled
buffer_LRU_batch_scanned_per_call disabled buffer_LRU_batch_scanned_per_call disabled
buffer_LRU_batch_total_pages disabled buffer_LRU_batch_flush_total_pages disabled
buffer_LRU_batches disabled buffer_LRU_batches_flush disabled
buffer_LRU_batch_pages 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_scanned disabled
buffer_LRU_single_flush_num_scan disabled buffer_LRU_single_flush_num_scan disabled
buffer_LRU_single_flush_scanned_per_call disabled buffer_LRU_single_flush_scanned_per_call disabled
......
...@@ -47,7 +47,6 @@ buffer_data_written disabled ...@@ -47,7 +47,6 @@ buffer_data_written disabled
buffer_flush_batch_scanned disabled buffer_flush_batch_scanned disabled
buffer_flush_batch_num_scan disabled buffer_flush_batch_num_scan disabled
buffer_flush_batch_scanned_per_call disabled buffer_flush_batch_scanned_per_call disabled
buffer_flush_batch_rescan disabled
buffer_flush_batch_total_pages disabled buffer_flush_batch_total_pages disabled
buffer_flush_batches disabled buffer_flush_batches disabled
buffer_flush_batch_pages disabled buffer_flush_batch_pages disabled
...@@ -72,9 +71,12 @@ buffer_flush_background_pages disabled ...@@ -72,9 +71,12 @@ buffer_flush_background_pages disabled
buffer_LRU_batch_scanned disabled buffer_LRU_batch_scanned disabled
buffer_LRU_batch_num_scan disabled buffer_LRU_batch_num_scan disabled
buffer_LRU_batch_scanned_per_call disabled buffer_LRU_batch_scanned_per_call disabled
buffer_LRU_batch_total_pages disabled buffer_LRU_batch_flush_total_pages disabled
buffer_LRU_batches disabled buffer_LRU_batches_flush disabled
buffer_LRU_batch_pages 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_scanned disabled
buffer_LRU_single_flush_num_scan disabled buffer_LRU_single_flush_num_scan disabled
buffer_LRU_single_flush_scanned_per_call disabled buffer_LRU_single_flush_scanned_per_call disabled
......
...@@ -47,7 +47,6 @@ buffer_data_written disabled ...@@ -47,7 +47,6 @@ buffer_data_written disabled
buffer_flush_batch_scanned disabled buffer_flush_batch_scanned disabled
buffer_flush_batch_num_scan disabled buffer_flush_batch_num_scan disabled
buffer_flush_batch_scanned_per_call disabled buffer_flush_batch_scanned_per_call disabled
buffer_flush_batch_rescan disabled
buffer_flush_batch_total_pages disabled buffer_flush_batch_total_pages disabled
buffer_flush_batches disabled buffer_flush_batches disabled
buffer_flush_batch_pages disabled buffer_flush_batch_pages disabled
...@@ -72,9 +71,12 @@ buffer_flush_background_pages disabled ...@@ -72,9 +71,12 @@ buffer_flush_background_pages disabled
buffer_LRU_batch_scanned disabled buffer_LRU_batch_scanned disabled
buffer_LRU_batch_num_scan disabled buffer_LRU_batch_num_scan disabled
buffer_LRU_batch_scanned_per_call disabled buffer_LRU_batch_scanned_per_call disabled
buffer_LRU_batch_total_pages disabled buffer_LRU_batch_flush_total_pages disabled
buffer_LRU_batches disabled buffer_LRU_batches_flush disabled
buffer_LRU_batch_pages 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_scanned disabled
buffer_LRU_single_flush_num_scan disabled buffer_LRU_single_flush_num_scan disabled
buffer_LRU_single_flush_scanned_per_call disabled buffer_LRU_single_flush_scanned_per_call disabled
......
...@@ -47,7 +47,6 @@ buffer_data_written disabled ...@@ -47,7 +47,6 @@ buffer_data_written disabled
buffer_flush_batch_scanned disabled buffer_flush_batch_scanned disabled
buffer_flush_batch_num_scan disabled buffer_flush_batch_num_scan disabled
buffer_flush_batch_scanned_per_call disabled buffer_flush_batch_scanned_per_call disabled
buffer_flush_batch_rescan disabled
buffer_flush_batch_total_pages disabled buffer_flush_batch_total_pages disabled
buffer_flush_batches disabled buffer_flush_batches disabled
buffer_flush_batch_pages disabled buffer_flush_batch_pages disabled
...@@ -72,9 +71,12 @@ buffer_flush_background_pages disabled ...@@ -72,9 +71,12 @@ buffer_flush_background_pages disabled
buffer_LRU_batch_scanned disabled buffer_LRU_batch_scanned disabled
buffer_LRU_batch_num_scan disabled buffer_LRU_batch_num_scan disabled
buffer_LRU_batch_scanned_per_call disabled buffer_LRU_batch_scanned_per_call disabled
buffer_LRU_batch_total_pages disabled buffer_LRU_batch_flush_total_pages disabled
buffer_LRU_batches disabled buffer_LRU_batches_flush disabled
buffer_LRU_batch_pages 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_scanned disabled
buffer_LRU_single_flush_num_scan disabled buffer_LRU_single_flush_num_scan disabled
buffer_LRU_single_flush_scanned_per_call disabled buffer_LRU_single_flush_scanned_per_call disabled
......
...@@ -55,6 +55,8 @@ Created 11/5/1995 Heikki Tuuri ...@@ -55,6 +55,8 @@ Created 11/5/1995 Heikki Tuuri
#include "srv0mon.h" #include "srv0mon.h"
#include "buf0checksum.h" #include "buf0checksum.h"
#include <new>
/* /*
IMPLEMENTATION OF THE BUFFER POOL IMPLEMENTATION OF THE BUFFER POOL
================================= =================================
...@@ -1329,6 +1331,19 @@ buf_pool_init_instance( ...@@ -1329,6 +1331,19 @@ buf_pool_init_instance(
buf_pool->try_LRU_scan = TRUE; 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); buf_pool_mutex_exit(buf_pool);
return(DB_SUCCESS); return(DB_SUCCESS);
...@@ -1419,6 +1434,8 @@ buf_pool_init( ...@@ -1419,6 +1434,8 @@ buf_pool_init(
btr_search_sys_create(buf_pool_get_curr_size() / sizeof(void*) / 64); btr_search_sys_create(buf_pool_get_curr_size() / sizeof(void*) / 64);
buf_flush_event = os_event_create();
return(DB_SUCCESS); return(DB_SUCCESS);
} }
...@@ -1535,6 +1552,10 @@ buf_relocate( ...@@ -1535,6 +1552,10 @@ buf_relocate(
memcpy(dpage, bpage, sizeof *dpage); 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_LRU_list = FALSE);
ut_d(bpage->in_page_hash = FALSE); ut_d(bpage->in_page_hash = FALSE);
...@@ -1573,6 +1594,84 @@ buf_relocate( ...@@ -1573,6 +1594,84 @@ buf_relocate(
HASH_INSERT(buf_page_t, hash, buf_pool->page_hash, fold, dpage); 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. 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 */ @return TRUE if a sentinel for a buffer pool watch, FALSE if not */
...@@ -4057,7 +4156,10 @@ UNIV_INTERN ...@@ -4057,7 +4156,10 @@ UNIV_INTERN
bool bool
buf_page_io_complete( 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; enum buf_io_fix io_type;
buf_pool_t* buf_pool = buf_pool_from_bpage(bpage); buf_pool_t* buf_pool = buf_pool_from_bpage(bpage);
...@@ -4238,6 +4340,7 @@ corrupt: ...@@ -4238,6 +4340,7 @@ corrupt:
id. */ id. */
buf_page_set_io_fix(bpage, BUF_IO_NONE); buf_page_set_io_fix(bpage, BUF_IO_NONE);
buf_page_monitor(bpage, io_type);
switch (io_type) { switch (io_type) {
case BUF_IO_READ: case BUF_IO_READ:
...@@ -4254,6 +4357,8 @@ corrupt: ...@@ -4254,6 +4357,8 @@ corrupt:
BUF_IO_READ); BUF_IO_READ);
} }
mutex_exit(buf_page_get_mutex(bpage));
break; break;
case BUF_IO_WRITE: case BUF_IO_WRITE:
...@@ -4269,14 +4374,30 @@ corrupt: ...@@ -4269,14 +4374,30 @@ corrupt:
buf_pool->stat.n_pages_written++; buf_pool->stat.n_pages_written++;
/* In case of flush batches i.e.: BUF_FLUSH_LIST and
BUF_FLUSH_LRU this function is always called from IO
helper thread. In this case, we decide whether or not
to evict the page based on flush type. The value
passed as evict is the default value in function
definition which is false.
We always evict in case of LRU batch and never evict
in case of flush list batch. For single page flush
the caller sets the appropriate value. */
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; break;
default: default:
ut_error; ut_error;
} }
buf_page_monitor(bpage, io_type);
#ifdef UNIV_DEBUG #ifdef UNIV_DEBUG
if (buf_debug_prints) { if (buf_debug_prints) {
fprintf(stderr, "Has %s page space %lu page no %lu\n", fprintf(stderr, "Has %s page space %lu page no %lu\n",
...@@ -4286,7 +4407,6 @@ corrupt: ...@@ -4286,7 +4407,6 @@ corrupt:
} }
#endif /* UNIV_DEBUG */ #endif /* UNIV_DEBUG */
mutex_exit(buf_page_get_mutex(bpage));
buf_pool_mutex_exit(buf_pool); buf_pool_mutex_exit(buf_pool);
return(true); return(true);
......
...@@ -64,14 +64,13 @@ need to protect it by a mutex. It is only ever read by the thread ...@@ -64,14 +64,13 @@ need to protect it by a mutex. It is only ever read by the thread
doing the shutdown */ doing the shutdown */
UNIV_INTERN ibool buf_page_cleaner_is_active = FALSE; 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 #ifdef UNIV_PFS_THREAD
UNIV_INTERN mysql_pfs_key_t buf_page_cleaner_thread_key; UNIV_INTERN mysql_pfs_key_t buf_page_cleaner_thread_key;
#endif /* UNIV_PFS_THREAD */ #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 /** 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 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 the blocks on free list. If LRU list is very small then we can end up
...@@ -135,60 +134,6 @@ buf_flush_validate_skip( ...@@ -135,60 +134,6 @@ buf_flush_validate_skip(
} }
#endif /* UNIV_DEBUG || UNIV_BUF_DEBUG */ #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 Insert a block in the flush_rbt and returns a pointer to its
predecessor or NULL if no predecessor. The ordering is maintained predecessor or NULL if no predecessor. The ordering is maintained
...@@ -587,6 +532,10 @@ buf_flush_remove( ...@@ -587,6 +532,10 @@ buf_flush_remove(
buf_flush_list_mutex_enter(buf_pool); 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)) { switch (buf_page_get_state(bpage)) {
case BUF_BLOCK_POOL_WATCH: case BUF_BLOCK_POOL_WATCH:
case BUF_BLOCK_ZIP_PAGE: case BUF_BLOCK_ZIP_PAGE:
...@@ -627,7 +576,6 @@ buf_flush_remove( ...@@ -627,7 +576,6 @@ buf_flush_remove(
ut_a(buf_flush_validate_skip(buf_pool)); ut_a(buf_flush_validate_skip(buf_pool));
#endif /* UNIV_DEBUG || UNIV_BUF_DEBUG */ #endif /* UNIV_DEBUG || UNIV_BUF_DEBUG */
buf_flush_update_hp(buf_pool, bpage);
buf_flush_list_mutex_exit(buf_pool); buf_flush_list_mutex_exit(buf_pool);
} }
...@@ -678,6 +626,10 @@ buf_flush_relocate_on_flush_list( ...@@ -678,6 +626,10 @@ buf_flush_relocate_on_flush_list(
prev_b = buf_flush_insert_in_flush_rbt(dpage); 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 /* Must be done after we have removed it from the flush_rbt
because we assert on in_flush_list in comparison function. */ because we assert on in_flush_list in comparison function. */
ut_d(bpage->in_flush_list = FALSE); ut_d(bpage->in_flush_list = FALSE);
...@@ -706,7 +658,6 @@ buf_flush_relocate_on_flush_list( ...@@ -706,7 +658,6 @@ buf_flush_relocate_on_flush_list(
ut_a(buf_flush_validate_low(buf_pool)); ut_a(buf_flush_validate_low(buf_pool));
#endif /* UNIV_DEBUG || UNIV_BUF_DEBUG */ #endif /* UNIV_DEBUG || UNIV_BUF_DEBUG */
buf_flush_update_hp(buf_pool, bpage);
buf_flush_list_mutex_exit(buf_pool); buf_flush_list_mutex_exit(buf_pool);
} }
...@@ -997,7 +948,10 @@ buf_flush_write_block_low( ...@@ -997,7 +948,10 @@ buf_flush_write_block_low(
if (sync) { if (sync) {
ut_ad(flush_type == BUF_FLUSH_SINGLE_PAGE); ut_ad(flush_type == BUF_FLUSH_SINGLE_PAGE);
fil_flush(buf_page_get_space(bpage)); 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 /* Increment the counter of I/O operations used
...@@ -1091,10 +1045,10 @@ buf_flush_page( ...@@ -1091,10 +1045,10 @@ buf_flush_page(
rw_lock_s_lock_gen(rw_lock, BUF_IO_WRITE); rw_lock_s_lock_gen(rw_lock, BUF_IO_WRITE);
} }
/* Even though bpage is not protected by any mutex at this /* Even though bpage is not protected by any mutex at this
point, it is safe to access bpage, because it is io_fixed and point, it is safe to access bpage, because it is io_fixed and
oldest_modification != 0. Thus, it cannot be relocated in the oldest_modification != 0. Thus, it cannot be relocated in the
buffer pool or removed from flush_list or LRU_list. */ buffer pool or removed from flush_list or LRU_list. */
buf_flush_write_block_low(bpage, flush_type, sync); buf_flush_write_block_low(bpage, flush_type, sync);
} }
...@@ -1458,105 +1412,70 @@ This utility flushes dirty blocks from the end of the LRU list. ...@@ -1458,105 +1412,70 @@ This utility flushes dirty blocks from the end of the LRU list.
The calling thread is not allowed to own any latches on pages! The calling thread is not allowed to own any latches on pages!
It attempts to make 'max' blocks available in the free list. Note that 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 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. to this function there will be 'max' blocks in the free list.*/
@return number of blocks for which the write request was queued. */ __attribute__((nonnull))
static void
ulint
buf_flush_LRU_list_batch( buf_flush_LRU_list_batch(
/*=====================*/ /*=====================*/
buf_pool_t* buf_pool, /*!< in: buffer pool instance */ buf_pool_t* buf_pool, /*!< in: buffer pool instance */
ulint max) /*!< in: desired number of ulint max, /*!< in: desired number of
blocks in the free_list */ blocks in the free_list */
flush_counters_t* n) /*!< out: flushed/evicted page
counts */
{ {
buf_page_t* bpage; buf_page_t* bpage;
ulint count = 0;
ulint scanned = 0; ulint scanned = 0;
ulint free_len = UT_LIST_GET_LEN(buf_pool->free); ulint free_len = UT_LIST_GET_LEN(buf_pool->free);
ulint lru_len = UT_LIST_GET_LEN(buf_pool->LRU); ulint lru_len = UT_LIST_GET_LEN(buf_pool->LRU);
n->flushed = 0;
n->evicted = 0;
n->unzip_LRU_evicted = 0;
ut_ad(buf_pool_mutex_own(buf_pool)); ut_ad(buf_pool_mutex_own(buf_pool));
bpage = UT_LIST_GET_LAST(buf_pool->LRU); for (bpage = UT_LIST_GET_LAST(buf_pool->LRU);
while (bpage != NULL && count < max bpage != NULL && (n->evicted + n->flushed) < max
&& free_len < srv_LRU_scan_depth && free_len < srv_LRU_scan_depth
&& lru_len > BUF_LRU_MIN_LEN) { && lru_len > BUF_LRU_MIN_LEN;
++scanned,
bpage = buf_pool->lru_hp.get()) {
ib_mutex_t* block_mutex = buf_page_get_mutex(bpage); buf_page_t* prev = UT_LIST_GET_PREV(LRU, bpage);
ibool evict; buf_pool->lru_hp.set(prev);
ib_mutex_t* block_mutex = buf_page_get_mutex(bpage);
mutex_enter(block_mutex); mutex_enter(block_mutex);
evict = buf_flush_ready_for_replace(bpage); bool evict = buf_flush_ready_for_replace(bpage);
mutex_exit(block_mutex); 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 (evict) {
/* block is ready for eviction i.e., it is
clean and is not IO-fixed or buffer fixed. */
if (buf_LRU_free_page(bpage, true)) { if (buf_LRU_free_page(bpage, true)) {
/* buf_pool->mutex was potentially n->evicted++;
released and reacquired. */
bpage = UT_LIST_GET_LAST(buf_pool->LRU);
} else {
bpage = UT_LIST_GET_PREV(LRU, bpage);
} }
} else { } else {
ulint space; /* Block is ready for flush. Dispatch an IO
ulint offset; request. The IO helper thread will put it on
buf_page_t* prev_bpage; free list in IO completion routine. */
buf_flush_page_and_try_neighbors(
prev_bpage = UT_LIST_GET_PREV(LRU, bpage); bpage, BUF_FLUSH_LRU, max, &n->flushed);
/* Save the previous bpage */
if (prev_bpage != NULL) {
space = prev_bpage->space;
offset = prev_bpage->offset;
} else {
space = ULINT_UNDEFINED;
offset = ULINT_UNDEFINED;
}
if (!buf_flush_page_and_try_neighbors(
bpage, BUF_FLUSH_LRU, max, &count)) {
bpage = prev_bpage;
} else {
/* buf_pool->mutex was released.
reposition the iterator. Note: the
prev block could have been repositioned
too but that should be rare. */
if (prev_bpage != NULL) {
ut_ad(space != ULINT_UNDEFINED);
ut_ad(offset != ULINT_UNDEFINED);
prev_bpage = buf_page_hash_get(
buf_pool, space, offset);
}
bpage = prev_bpage;
}
} }
ut_ad(!mutex_own(block_mutex));
ut_ad(buf_pool_mutex_own(buf_pool));
free_len = UT_LIST_GET_LEN(buf_pool->free); free_len = UT_LIST_GET_LEN(buf_pool->free);
lru_len = UT_LIST_GET_LEN(buf_pool->LRU); 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 /* We keep track of all flushes happening as part of LRU
flush. When estimating the desired rate at which flush_list flush. When estimating the desired rate at which flush_list
should be flushed, we factor in this value. */ should be flushed, we factor in this value. */
buf_lru_flush_page_count += count; buf_lru_flush_page_count += n->flushed;
ut_ad(buf_pool_mutex_own(buf_pool)); ut_ad(buf_pool_mutex_own(buf_pool));
...@@ -1567,35 +1486,38 @@ buf_flush_LRU_list_batch( ...@@ -1567,35 +1486,38 @@ buf_flush_LRU_list_batch(
MONITOR_LRU_BATCH_SCANNED_PER_CALL, MONITOR_LRU_BATCH_SCANNED_PER_CALL,
scanned); scanned);
} }
return(count);
} }
/*******************************************************************//** /*******************************************************************//**
Flush and move pages from LRU or unzip_LRU list to the free list. 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. 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 __attribute__((nonnull))
or in case of unzip_LRU the number of blocks actually moved to the
free list */
static static
ulint void
buf_do_LRU_batch( buf_do_LRU_batch(
/*=============*/ /*=============*/
buf_pool_t* buf_pool, /*!< in: buffer pool instance */ buf_pool_t* buf_pool, /*!< in: buffer pool instance */
ulint max) /*!< in: desired number of ulint max, /*!< in: desired number of
blocks in the free_list */ blocks in the free_list */
flush_counters_t* n) /*!< out: flushed/evicted page
counts */
{ {
ulint count = 0;
if (buf_LRU_evict_from_unzip_LRU(buf_pool)) { if (buf_LRU_evict_from_unzip_LRU(buf_pool)) {
count += buf_free_from_unzip_LRU_list_batch(buf_pool, max); n->unzip_LRU_evicted = buf_free_from_unzip_LRU_list_batch(buf_pool, max);
} else {
n->unzip_LRU_evicted = 0;
} }
if (max > count) { if (max > n->unzip_LRU_evicted) {
count += buf_flush_LRU_list_batch(buf_pool, max - count); buf_flush_LRU_list_batch(buf_pool, max - n->unzip_LRU_evicted, n);
} else {
n->evicted = 0;
n->flushed = 0;
} }
return(count); /* Add evicted pages from unzip_LRU to the evicted pages from
the simple LRU. */
n->evicted += n->unzip_LRU_evicted;
} }
/*******************************************************************//** /*******************************************************************//**
...@@ -1637,6 +1559,7 @@ buf_do_flush_list_batch( ...@@ -1637,6 +1559,7 @@ buf_do_flush_list_batch(
for (buf_page_t* bpage = UT_LIST_GET_LAST(buf_pool->flush_list); for (buf_page_t* bpage = UT_LIST_GET_LAST(buf_pool->flush_list);
count < min_n && bpage != NULL && len > 0 count < min_n && bpage != NULL && len > 0
&& bpage->oldest_modification < lsn_limit; && bpage->oldest_modification < lsn_limit;
bpage = buf_pool->flush_hp.get(),
++scanned) { ++scanned) {
buf_page_t* prev; buf_page_t* prev;
...@@ -1645,8 +1568,7 @@ buf_do_flush_list_batch( ...@@ -1645,8 +1568,7 @@ buf_do_flush_list_batch(
ut_ad(bpage->in_flush_list); ut_ad(bpage->in_flush_list);
prev = UT_LIST_GET_PREV(list, bpage); 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); buf_flush_list_mutex_exit(buf_pool);
#ifdef UNIV_DEBUG #ifdef UNIV_DEBUG
...@@ -1657,23 +1579,12 @@ buf_do_flush_list_batch( ...@@ -1657,23 +1579,12 @@ buf_do_flush_list_batch(
buf_flush_list_mutex_enter(buf_pool); buf_flush_list_mutex_enter(buf_pool);
ut_ad(flushed || buf_flush_is_hp(buf_pool, prev)); ut_ad(flushed || buf_pool->flush_hp.is_hp(prev));
if (!buf_flush_is_hp(buf_pool, prev)) { --len;
/* 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);
} }
buf_pool->flush_hp.set(NULL);
buf_flush_list_mutex_exit(buf_pool); buf_flush_list_mutex_exit(buf_pool);
MONITOR_INC_VALUE_CUMULATIVE(MONITOR_FLUSH_BATCH_SCANNED, MONITOR_INC_VALUE_CUMULATIVE(MONITOR_FLUSH_BATCH_SCANNED,
...@@ -1691,9 +1602,9 @@ This utility flushes dirty blocks from the end of the LRU list or flush_list. ...@@ -1691,9 +1602,9 @@ This utility flushes dirty blocks from the end of the LRU list or flush_list.
NOTE 1: in the case of an LRU flush the calling thread may own latches to NOTE 1: in the case of an LRU flush the calling thread may own latches to
pages: to avoid deadlocks, this function must be written so that it cannot 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, 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! the calling thread is not allowed to own any latches on pages! */
@return number of blocks for which the write request was queued */ __attribute__((nonnull))
ulint void
buf_flush_batch( buf_flush_batch(
/*============*/ /*============*/
buf_pool_t* buf_pool, /*!< in: buffer pool instance */ buf_pool_t* buf_pool, /*!< in: buffer pool instance */
...@@ -1704,14 +1615,14 @@ buf_flush_batch( ...@@ -1704,14 +1615,14 @@ buf_flush_batch(
ulint min_n, /*!< in: wished minimum mumber of blocks ulint min_n, /*!< in: wished minimum mumber of blocks
flushed (it is not guaranteed that the flushed (it is not guaranteed that the
actual number is that big, though) */ actual number is that big, though) */
lsn_t lsn_limit) /*!< in: in the case of BUF_FLUSH_LIST lsn_t lsn_limit, /*!< in: in the case of BUF_FLUSH_LIST
all blocks whose oldest_modification is all blocks whose oldest_modification is
smaller than this should be flushed smaller than this should be flushed
(if their number does not exceed (if their number does not exceed
min_n), otherwise ignored */ min_n), otherwise ignored */
flush_counters_t* n) /*!< out: flushed/evicted page
counts */
{ {
ulint count = 0;
ut_ad(flush_type == BUF_FLUSH_LRU || flush_type == BUF_FLUSH_LIST); ut_ad(flush_type == BUF_FLUSH_LRU || flush_type == BUF_FLUSH_LIST);
#ifdef UNIV_SYNC_DEBUG #ifdef UNIV_SYNC_DEBUG
ut_ad((flush_type != BUF_FLUSH_LIST) ut_ad((flush_type != BUF_FLUSH_LIST)
...@@ -1724,10 +1635,11 @@ buf_flush_batch( ...@@ -1724,10 +1635,11 @@ buf_flush_batch(
the flush functions. */ the flush functions. */
switch (flush_type) { switch (flush_type) {
case BUF_FLUSH_LRU: case BUF_FLUSH_LRU:
count = buf_do_LRU_batch(buf_pool, min_n); buf_do_LRU_batch(buf_pool, min_n, n);
break; break;
case BUF_FLUSH_LIST: case BUF_FLUSH_LIST:
count = buf_do_flush_list_batch(buf_pool, min_n, lsn_limit); n->flushed = buf_do_flush_list_batch(buf_pool, min_n, lsn_limit);
n->evicted = 0;
break; break;
default: default:
ut_error; ut_error;
...@@ -1736,15 +1648,13 @@ buf_flush_batch( ...@@ -1736,15 +1648,13 @@ buf_flush_batch(
buf_pool_mutex_exit(buf_pool); buf_pool_mutex_exit(buf_pool);
#ifdef UNIV_DEBUG #ifdef UNIV_DEBUG
if (buf_debug_prints && count > 0) { if (buf_debug_prints && n->flushed > 0) {
fprintf(stderr, flush_type == BUF_FLUSH_LRU fprintf(stderr, flush_type == BUF_FLUSH_LRU
? "Flushed %lu pages in LRU flush\n" ? "Flushed %lu pages in LRU flush\n"
: "Flushed %lu pages in flush list flush\n", : "Flushed %lu pages in flush list flush\n",
(ulong) count); (ulong) n->flushed);
} }
#endif /* UNIV_DEBUG */ #endif /* UNIV_DEBUG */
return(count);
} }
/******************************************************************//** /******************************************************************//**
...@@ -1855,48 +1765,6 @@ buf_flush_wait_batch_end( ...@@ -1855,48 +1765,6 @@ buf_flush_wait_batch_end(
} }
} }
/*******************************************************************//**
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 This utility flushes dirty blocks from the end of the flush list of
all buffer pool instances. all buffer pool instances.
...@@ -1943,8 +1811,8 @@ buf_flush_list( ...@@ -1943,8 +1811,8 @@ buf_flush_list(
/* Flush to lsn_limit in all buffer pool instances */ /* Flush to lsn_limit in all buffer pool instances */
for (i = 0; i < srv_buf_pool_instances; i++) { for (i = 0; i < srv_buf_pool_instances; i++) {
buf_pool_t* buf_pool; buf_pool_t* buf_pool;
ulint page_count = 0; flush_counters_t n;
buf_pool = buf_pool_from_array(i); buf_pool = buf_pool_from_array(i);
...@@ -1964,23 +1832,23 @@ buf_flush_list( ...@@ -1964,23 +1832,23 @@ buf_flush_list(
continue; continue;
} }
page_count = buf_flush_batch( buf_flush_batch(
buf_pool, BUF_FLUSH_LIST, min_n, lsn_limit); buf_pool, BUF_FLUSH_LIST, min_n, lsn_limit, &n);
buf_flush_end(buf_pool, BUF_FLUSH_LIST); buf_flush_end(buf_pool, BUF_FLUSH_LIST);
buf_flush_common(BUF_FLUSH_LIST, page_count); buf_flush_common(BUF_FLUSH_LIST, n.flushed);
if (n_processed) { if (n_processed) {
*n_processed += page_count; *n_processed += n.flushed;
} }
if (page_count) { if (n.flushed) {
MONITOR_INC_VALUE_CUMULATIVE( MONITOR_INC_VALUE_CUMULATIVE(
MONITOR_FLUSH_BATCH_TOTAL_PAGE, MONITOR_FLUSH_BATCH_TOTAL_PAGE,
MONITOR_FLUSH_BATCH_COUNT, MONITOR_FLUSH_BATCH_COUNT,
MONITOR_FLUSH_BATCH_PAGES, MONITOR_FLUSH_BATCH_PAGES,
page_count); n.flushed);
} }
} }
...@@ -1988,12 +1856,12 @@ buf_flush_list( ...@@ -1988,12 +1856,12 @@ buf_flush_list(
} }
/******************************************************************//** /******************************************************************//**
This function picks up a single dirty page from the tail of the LRU This function picks up a single page from the tail of the LRU
list, flushes it, removes it from page_hash and LRU list and puts list, flushes it (if it is dirty), removes it from page_hash and LRU
it on the free list. It is called from user threads when they are list and puts it on the free list. It is called from user threads when
unable to find a replaceable page at the tail of the LRU list i.e.: they are unable to find a replaceable page at the tail of the LRU
when the background LRU flushing in the page_cleaner thread is not list i.e.: when the background LRU flushing in the page_cleaner thread
fast enough to keep pace with the workload. is not fast enough to keep pace with the workload.
@return TRUE if success. */ @return TRUE if success. */
UNIV_INTERN UNIV_INTERN
ibool ibool
...@@ -2003,84 +1871,67 @@ buf_flush_single_page_from_LRU( ...@@ -2003,84 +1871,67 @@ buf_flush_single_page_from_LRU(
{ {
ulint scanned; ulint scanned;
buf_page_t* bpage; buf_page_t* bpage;
ibool freed;
buf_pool_mutex_enter(buf_pool); buf_pool_mutex_enter(buf_pool);
for (bpage = UT_LIST_GET_LAST(buf_pool->LRU), scanned = 1; for (bpage = buf_pool->single_scan_itr.start(),
scanned = 0, freed = FALSE;
bpage != NULL; bpage != NULL;
bpage = UT_LIST_GET_PREV(LRU, bpage), ++scanned) { ++scanned, bpage = buf_pool->single_scan_itr.get()) {
ib_mutex_t* block_mutex = buf_page_get_mutex(bpage);
mutex_enter(block_mutex); ut_ad(buf_pool_mutex_own(buf_pool));
if (buf_flush_ready_for_flush(bpage, BUF_FLUSH_SINGLE_PAGE)) { buf_page_t* prev = UT_LIST_GET_PREV(LRU, bpage);
buf_pool->single_scan_itr.set(prev);
/* The following call will release the buffer pool ib_mutex_t* block_mutex = buf_page_get_mutex(bpage);
and block mutex. */ mutex_enter(block_mutex);
ibool flushed = buf_flush_page(
buf_pool, bpage, BUF_FLUSH_SINGLE_PAGE, true);
if (flushed) { if (buf_flush_ready_for_replace(bpage)) {
/* buf_flush_page() will release the /* block is ready for eviction i.e., it is
block mutex */ 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; 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, if
successful, will release the buffer pool and
block mutex. */
freed = buf_flush_page(buf_pool, bpage,
BUF_FLUSH_SINGLE_PAGE, true);
if (freed) {
/* block and buffer pool mutex have
already been reelased. */
break;
}
mutex_exit(block_mutex);
} else {
mutex_exit(block_mutex);
} }
mutex_exit(block_mutex);
} }
MONITOR_INC_VALUE_CUMULATIVE( if (!freed) {
MONITOR_LRU_SINGLE_FLUSH_SCANNED,
MONITOR_LRU_SINGLE_FLUSH_SCANNED_NUM_CALL,
MONITOR_LRU_SINGLE_FLUSH_SCANNED_PER_CALL,
scanned);
if (bpage == NULL) {
/* Can't find a single flushable page. */ /* Can't find a single flushable page. */
ut_ad(!bpage);
buf_pool_mutex_exit(buf_pool); buf_pool_mutex_exit(buf_pool);
return(FALSE);
} }
if (scanned) {
ibool freed = FALSE; MONITOR_INC_VALUE_CUMULATIVE(
MONITOR_LRU_SINGLE_FLUSH_SCANNED,
/* At this point the page has been written to the disk. MONITOR_LRU_SINGLE_FLUSH_SCANNED_NUM_CALL,
As we are not holding buffer pool or block mutex therefore MONITOR_LRU_SINGLE_FLUSH_SCANNED_PER_CALL,
we cannot use the bpage safely. It may have been plucked out scanned);
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)) {
ib_mutex_t* block_mutex = buf_page_get_mutex(bpage);
mutex_enter(block_mutex);
ibool ready = buf_flush_ready_for_replace(bpage);
mutex_exit(block_mutex);
if (ready) {
bool evict_zip;
evict_zip = !buf_LRU_evict_from_unzip_LRU(buf_pool);;
freed = buf_LRU_free_page(bpage, evict_zip);
break;
}
} }
buf_pool_mutex_exit(buf_pool); ut_ad(!buf_pool_mutex_own(buf_pool));
return(freed); return(freed);
} }
...@@ -2107,6 +1958,7 @@ buf_flush_LRU_tail(void) ...@@ -2107,6 +1958,7 @@ buf_flush_LRU_tail(void)
buf_pool_t* buf_pool = buf_pool_from_array(i); buf_pool_t* buf_pool = buf_pool_from_array(i);
ulint scan_depth; ulint scan_depth;
flush_counters_t n;
/* srv_LRU_scan_depth can be arbitrarily large value. /* srv_LRU_scan_depth can be arbitrarily large value.
We cap it with current LRU size. */ We cap it with current LRU size. */
...@@ -2116,44 +1968,37 @@ buf_flush_LRU_tail(void) ...@@ -2116,44 +1968,37 @@ buf_flush_LRU_tail(void)
scan_depth = ut_min(srv_LRU_scan_depth, scan_depth); scan_depth = ut_min(srv_LRU_scan_depth, scan_depth);
/* We divide LRU flush into smaller chunks because /* Currently page_cleaner is the only thread
there may be user threads waiting for the flush to that can trigger an LRU flush. It is possible
end in buf_LRU_get_free_block(). */ that a batch triggered during last iteration is
for (ulint j = 0; still running, */
j < scan_depth; if (!buf_flush_start(buf_pool, BUF_FLUSH_LRU)) {
j += PAGE_CLEANER_LRU_BATCH_CHUNK_SIZE) { continue;
}
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) { buf_flush_batch(buf_pool, BUF_FLUSH_LRU, scan_depth, 0, &n);
total_flushed += n_flushed;
} else { buf_flush_end(buf_pool, BUF_FLUSH_LRU);
/* Nothing to flush */
break; buf_flush_common(BUF_FLUSH_LRU, n.flushed);
}
if (n.flushed) {
MONITOR_INC_VALUE_CUMULATIVE(
MONITOR_LRU_BATCH_FLUSH_TOTAL_PAGE,
MONITOR_LRU_BATCH_FLUSH_COUNT,
MONITOR_LRU_BATCH_FLUSH_PAGES,
n.flushed);
} }
}
if (total_flushed) { if (n.evicted) {
MONITOR_INC_VALUE_CUMULATIVE( MONITOR_INC_VALUE_CUMULATIVE(
MONITOR_LRU_BATCH_TOTAL_PAGE, MONITOR_LRU_BATCH_EVICT_TOTAL_PAGE,
MONITOR_LRU_BATCH_COUNT, MONITOR_LRU_BATCH_EVICT_COUNT,
MONITOR_LRU_BATCH_PAGES, MONITOR_LRU_BATCH_EVICT_PAGES,
total_flushed); n.evicted);
}
total_flushed += (n.flushed + n.evicted);
} }
return(total_flushed); return(total_flushed);
...@@ -2404,11 +2249,14 @@ page_cleaner_sleep_if_needed( ...@@ -2404,11 +2249,14 @@ page_cleaner_sleep_if_needed(
if (next_loop_time > cur_time) { if (next_loop_time > cur_time) {
/* Get sleep interval in micro seconds. We use /* Get sleep interval in micro seconds. We use
ut_min() to avoid long sleep in case of ut_min() to avoid long sleep in case of wrap around. */
wrap around. */ ulint sleep_us;
os_thread_sleep(ut_min(1000000,
(next_loop_time - cur_time) sleep_us = ut_min(1000000, (next_loop_time - cur_time) * 1000);
* 1000));
ib_int64_t sig_count = os_event_reset(buf_flush_event);
os_event_wait_time_low(buf_flush_event, sleep_us, sig_count);
} }
} }
...@@ -2538,6 +2386,8 @@ DECLARE_THREAD(buf_flush_page_cleaner_thread)( ...@@ -2538,6 +2386,8 @@ DECLARE_THREAD(buf_flush_page_cleaner_thread)(
thread_exit: thread_exit:
buf_page_cleaner_is_active = FALSE; buf_page_cleaner_is_active = FALSE;
os_event_free(buf_flush_event);
/* We count the number of threads in os_thread_exit(). A created /* 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. */ thread should always use that to exit and not use return() to exit. */
os_thread_exit(NULL); os_thread_exit(NULL);
......
...@@ -81,6 +81,10 @@ are not blocked for extended period of time when using very large ...@@ -81,6 +81,10 @@ are not blocked for extended period of time when using very large
buffer pools. */ buffer pools. */
#define BUF_LRU_DROP_SEARCH_SIZE 1024 #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 /** If we switch on the InnoDB monitor because there are too few available
frames in the buffer pool, we set this to TRUE */ frames in the buffer pool, we set this to TRUE */
static ibool buf_lru_switched_on_innodb_mon = FALSE; static ibool buf_lru_switched_on_innodb_mon = FALSE;
...@@ -961,7 +965,7 @@ buf_LRU_free_from_unzip_LRU_list( ...@@ -961,7 +965,7 @@ buf_LRU_free_from_unzip_LRU_list(
} }
for (block = UT_LIST_GET_LAST(buf_pool->unzip_LRU), for (block = UT_LIST_GET_LAST(buf_pool->unzip_LRU),
scanned = 1, freed = FALSE; scanned = 0, freed = FALSE;
block != NULL && !freed block != NULL && !freed
&& (scan_all || scanned < srv_LRU_scan_depth); && (scan_all || scanned < srv_LRU_scan_depth);
++scanned) { ++scanned) {
...@@ -978,11 +982,13 @@ buf_LRU_free_from_unzip_LRU_list( ...@@ -978,11 +982,13 @@ buf_LRU_free_from_unzip_LRU_list(
block = prev_block; block = prev_block;
} }
MONITOR_INC_VALUE_CUMULATIVE( if (scanned) {
MONITOR_LRU_UNZIP_SEARCH_SCANNED, MONITOR_INC_VALUE_CUMULATIVE(
MONITOR_LRU_UNZIP_SEARCH_SCANNED_NUM_CALL, MONITOR_LRU_UNZIP_SEARCH_SCANNED,
MONITOR_LRU_UNZIP_SEARCH_SCANNED_PER_CALL, MONITOR_LRU_UNZIP_SEARCH_SCANNED_NUM_CALL,
scanned); MONITOR_LRU_UNZIP_SEARCH_SCANNED_PER_CALL,
scanned);
}
return(freed); return(freed);
} }
...@@ -1004,21 +1010,30 @@ buf_LRU_free_from_common_LRU_list( ...@@ -1004,21 +1010,30 @@ buf_LRU_free_from_common_LRU_list(
ut_ad(buf_pool_mutex_own(buf_pool)); ut_ad(buf_pool_mutex_own(buf_pool));
for (bpage = UT_LIST_GET_LAST(buf_pool->LRU), for (bpage = buf_pool->lru_scan_itr.start(),
scanned = 1, freed = FALSE; scanned = 0, freed = false;
bpage != NULL && !freed bpage != NULL && !freed
&& (scan_all || scanned < srv_LRU_scan_depth); && (scan_all || scanned < BUF_LRU_SEARCH_SCAN_THRESHOLD);
++scanned) { ++scanned, bpage = buf_pool->lru_scan_itr.get()) {
unsigned accessed; buf_page_t* prev = UT_LIST_GET_PREV(LRU, bpage);
buf_page_t* prev_bpage = UT_LIST_GET_PREV(LRU, buf_pool->lru_scan_itr.set(prev);
bpage);
ib_mutex_t* mutex = buf_page_get_mutex(bpage);
mutex_enter(mutex);
ut_ad(buf_page_in_file(bpage)); ut_ad(buf_page_in_file(bpage));
ut_ad(bpage->in_LRU_list); ut_ad(bpage->in_LRU_list);
accessed = buf_page_is_accessed(bpage); unsigned accessed = buf_page_is_accessed(bpage);
freed = buf_LRU_free_page(bpage, true);
if (buf_flush_ready_for_replace(bpage)) {
mutex_exit(mutex);
freed = buf_LRU_free_page(bpage, true);
} else {
mutex_exit(mutex);
}
if (freed && !accessed) { if (freed && !accessed) {
/* Keep track of pages that are evicted without /* Keep track of pages that are evicted without
ever being accessed. This gives us a measure of ever being accessed. This gives us a measure of
...@@ -1026,14 +1041,17 @@ buf_LRU_free_from_common_LRU_list( ...@@ -1026,14 +1041,17 @@ buf_LRU_free_from_common_LRU_list(
++buf_pool->stat.n_ra_pages_evicted; ++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( if (scanned) {
MONITOR_LRU_SEARCH_SCANNED, MONITOR_INC_VALUE_CUMULATIVE(
MONITOR_LRU_SEARCH_SCANNED_NUM_CALL, MONITOR_LRU_SEARCH_SCANNED,
MONITOR_LRU_SEARCH_SCANNED_PER_CALL, MONITOR_LRU_SEARCH_SCANNED_NUM_CALL,
scanned); MONITOR_LRU_SEARCH_SCANNED_PER_CALL,
scanned);
}
return(freed); return(freed);
} }
...@@ -1217,8 +1235,6 @@ the free list. Even when we flush a page or find a page in LRU scan ...@@ -1217,8 +1235,6 @@ the free list. Even when we flush a page or find a page in LRU scan
we put it to free list to be used. we put it to free list to be used.
* iteration 0: * iteration 0:
* get a block from free list, success:done * 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 * if buf_pool->try_LRU_scan is set
* scan LRU up to srv_LRU_scan_depth to find a clean block * scan LRU up to srv_LRU_scan_depth to find a clean block
* the above will put the block on free list * the above will put the block on free list
...@@ -1231,7 +1247,7 @@ we put it to free list to be used. ...@@ -1231,7 +1247,7 @@ we put it to free list to be used.
* scan whole LRU list * scan whole LRU list
* scan LRU list even if buf_pool->try_LRU_scan is not set * scan LRU list even if buf_pool->try_LRU_scan is not set
* iteration > 1: * 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 */ @return the free control block, in state BUF_BLOCK_READY_FOR_USE */
UNIV_INTERN UNIV_INTERN
buf_block_t* buf_block_t*
...@@ -1269,20 +1285,6 @@ loop: ...@@ -1269,20 +1285,6 @@ loop:
return(block); 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 (buf_pool->try_LRU_scan || n_iterations > 0) {
/* If no block was in the free list, search from the /* If no block was in the free list, search from the
...@@ -1299,6 +1301,10 @@ loop: ...@@ -1299,6 +1301,10 @@ loop:
TRUE again when we flush a batch from this TRUE again when we flush a batch from this
buffer pool. */ buffer pool. */
buf_pool->try_LRU_scan = FALSE; 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);
} }
} }
...@@ -1347,12 +1353,10 @@ loop: ...@@ -1347,12 +1353,10 @@ loop:
/* If we have scanned the whole LRU and still are unable to /* If we have scanned the whole LRU and still are unable to
find a free block then we should sleep here to let the find a free block then we should sleep here to let the
page_cleaner do an LRU batch for us. 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. */
if (n_iterations > 1) {
os_thread_sleep(100000); if (n_iterations > 1) {
os_thread_sleep(10000);
} }
/* No free block was found: try to flush the LRU list. /* No free block was found: try to flush the LRU list.
...@@ -1502,6 +1506,20 @@ buf_unzip_LRU_remove_block_if_needed( ...@@ -1502,6 +1506,20 @@ buf_unzip_LRU_remove_block_if_needed(
} }
} }
/******************************************************************//**
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. */ Removes a block from the LRU list. */
UNIV_INLINE UNIV_INLINE
...@@ -1521,6 +1539,10 @@ buf_LRU_remove_block( ...@@ -1521,6 +1539,10 @@ buf_LRU_remove_block(
ut_ad(bpage->in_LRU_list); 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, /* If the LRU_old pointer is defined and points to just this block,
move it backward one step */ move it backward one step */
......
...@@ -110,7 +110,8 @@ typedef struct wrk_itm ...@@ -110,7 +110,8 @@ typedef struct wrk_itm
will be used */ will be used */
wr_tsk_t wr; /*!< Flush page list */ wr_tsk_t wr; /*!< Flush page list */
rd_tsk_t rd; /*!< Decompress page list */ rd_tsk_t rd; /*!< Decompress page list */
ulint n_flushed; /*!< Flushed pages count */ ulint n_flushed; /*!< Number of flushed pages */
ulint n_evicted; /*!< Number of evicted pages */
os_thread_id_t id_usr; /*!< Thread-id currently working */ os_thread_id_t id_usr; /*!< Thread-id currently working */
wrk_status_t wi_status; /*!< Work item status */ wrk_status_t wi_status; /*!< Work item status */
mem_heap_t *wheap; /*!< Heap were to allocate memory mem_heap_t *wheap; /*!< Heap were to allocate memory
...@@ -180,6 +181,7 @@ buf_mtflu_flush_pool_instance( ...@@ -180,6 +181,7 @@ buf_mtflu_flush_pool_instance(
/*==========================*/ /*==========================*/
wrk_t *work_item) /*!< inout: work item to be flushed */ wrk_t *work_item) /*!< inout: work item to be flushed */
{ {
flush_counters_t n;
ut_a(work_item != NULL); ut_a(work_item != NULL);
ut_a(work_item->wr.buf_pool != NULL); ut_a(work_item->wr.buf_pool != NULL);
...@@ -211,14 +213,16 @@ buf_mtflu_flush_pool_instance( ...@@ -211,14 +213,16 @@ buf_mtflu_flush_pool_instance(
work_item->wr.min = ut_min(srv_LRU_scan_depth,work_item->wr.min); work_item->wr.min = ut_min(srv_LRU_scan_depth,work_item->wr.min);
} }
work_item->n_flushed = buf_flush_batch(work_item->wr.buf_pool, buf_flush_batch(work_item->wr.buf_pool,
work_item->wr.flush_type, work_item->wr.flush_type,
work_item->wr.min, work_item->wr.min,
work_item->wr.lsn_limit); work_item->wr.lsn_limit,
&n);
buf_flush_end(work_item->wr.buf_pool, work_item->wr.flush_type); buf_flush_end(work_item->wr.buf_pool, work_item->wr.flush_type);
buf_flush_common(work_item->wr.flush_type, work_item->n_flushed); buf_flush_common(work_item->wr.flush_type, n.flushed);
work_item->n_flushed = n.flushed;
work_item->n_evicted = n.evicted;
return work_item->n_flushed; return work_item->n_flushed;
} }
...@@ -359,7 +363,7 @@ void ...@@ -359,7 +363,7 @@ void
buf_mtflu_io_thread_exit(void) buf_mtflu_io_thread_exit(void)
/*==========================*/ /*==========================*/
{ {
long i; ulint i;
thread_sync_t* mtflush_io = mtflush_ctx; thread_sync_t* mtflush_io = mtflush_ctx;
wrk_t* work_item = NULL; wrk_t* work_item = NULL;
...@@ -393,7 +397,7 @@ buf_mtflu_io_thread_exit(void) ...@@ -393,7 +397,7 @@ buf_mtflu_io_thread_exit(void)
ut_a(ib_wqueue_is_empty(mtflush_io->wq)); ut_a(ib_wqueue_is_empty(mtflush_io->wq));
/* Send one exit work item/thread */ /* Send one exit work item/thread */
for (i=0; i < srv_mtflush_threads; i++) { for (i=0; i < (ulint)srv_mtflush_threads; i++) {
work_item[i].tsk = MT_WRK_NONE; work_item[i].tsk = MT_WRK_NONE;
work_item[i].wi_status = WRK_ITEM_EXIT; work_item[i].wi_status = WRK_ITEM_EXIT;
work_item[i].wheap = mtflush_io->wheap; work_item[i].wheap = mtflush_io->wheap;
...@@ -413,11 +417,8 @@ buf_mtflu_io_thread_exit(void) ...@@ -413,11 +417,8 @@ buf_mtflu_io_thread_exit(void)
ut_a(ib_wqueue_is_empty(mtflush_io->wq)); ut_a(ib_wqueue_is_empty(mtflush_io->wq));
/* Requests sent */
os_fast_mutex_unlock(&mtflush_mtx);
/* Collect all work done items */ /* Collect all work done items */
for (i=0; i < srv_mtflush_threads;) { for (i=0; i < (ulint)srv_mtflush_threads;) {
wrk_t* work_item = NULL; wrk_t* work_item = NULL;
work_item = (wrk_t *)ib_wqueue_timedwait(mtflush_io->wr_cq, MT_WAIT_IN_USECS); work_item = (wrk_t *)ib_wqueue_timedwait(mtflush_io->wr_cq, MT_WAIT_IN_USECS);
...@@ -432,6 +433,11 @@ buf_mtflu_io_thread_exit(void) ...@@ -432,6 +433,11 @@ buf_mtflu_io_thread_exit(void)
/* Wait about 1/2 sec to allow threads really exit */ /* Wait about 1/2 sec to allow threads really exit */
os_thread_sleep(MT_WAIT_IN_USECS); os_thread_sleep(MT_WAIT_IN_USECS);
while(!ib_wqueue_is_empty(mtflush_io->wq))
{
ib_wqueue_nowait(mtflush_io->wq);
}
ut_a(ib_wqueue_is_empty(mtflush_io->wq)); ut_a(ib_wqueue_is_empty(mtflush_io->wq));
ut_a(ib_wqueue_is_empty(mtflush_io->wr_cq)); ut_a(ib_wqueue_is_empty(mtflush_io->wr_cq));
ut_a(ib_wqueue_is_empty(mtflush_io->rd_cq)); ut_a(ib_wqueue_is_empty(mtflush_io->rd_cq));
...@@ -441,6 +447,8 @@ buf_mtflu_io_thread_exit(void) ...@@ -441,6 +447,8 @@ buf_mtflu_io_thread_exit(void)
ib_wqueue_free(mtflush_io->wr_cq); ib_wqueue_free(mtflush_io->wr_cq);
ib_wqueue_free(mtflush_io->rd_cq); ib_wqueue_free(mtflush_io->rd_cq);
/* Requests sent */
os_fast_mutex_unlock(&mtflush_mtx);
os_fast_mutex_free(&mtflush_mtx); os_fast_mutex_free(&mtflush_mtx);
os_fast_mutex_free(&mtflush_io->thread_global_mtx); os_fast_mutex_free(&mtflush_io->thread_global_mtx);
...@@ -518,8 +526,8 @@ ulint ...@@ -518,8 +526,8 @@ ulint
buf_mtflu_flush_work_items( buf_mtflu_flush_work_items(
/*=======================*/ /*=======================*/
ulint buf_pool_inst, /*!< in: Number of buffer pool instances */ ulint buf_pool_inst, /*!< in: Number of buffer pool instances */
ulint *per_pool_pages_flushed, /*!< out: Number of pages flush_counters_t *per_pool_cnt, /*!< out: Number of pages
flushed/instance */ flushed or evicted /instance */
buf_flush_t flush_type, /*!< in: Type of flush */ buf_flush_t flush_type, /*!< in: Type of flush */
ulint min_n, /*!< in: Wished minimum number of ulint min_n, /*!< in: Wished minimum number of
blocks to be flushed */ blocks to be flushed */
...@@ -549,6 +557,7 @@ buf_mtflu_flush_work_items( ...@@ -549,6 +557,7 @@ buf_mtflu_flush_work_items(
work_item[i].wheap = work_heap; work_item[i].wheap = work_heap;
work_item[i].rheap = reply_heap; work_item[i].rheap = reply_heap;
work_item[i].n_flushed = 0; work_item[i].n_flushed = 0;
work_item[i].n_evicted = 0;
work_item[i].id_usr = 0; work_item[i].id_usr = 0;
ib_wqueue_add(mtflush_ctx->wq, ib_wqueue_add(mtflush_ctx->wq,
...@@ -562,7 +571,8 @@ buf_mtflu_flush_work_items( ...@@ -562,7 +571,8 @@ buf_mtflu_flush_work_items(
done_wi = (wrk_t *)ib_wqueue_wait(mtflush_ctx->wr_cq); done_wi = (wrk_t *)ib_wqueue_wait(mtflush_ctx->wr_cq);
if (done_wi != NULL) { if (done_wi != NULL) {
per_pool_pages_flushed[i] = done_wi->n_flushed; per_pool_cnt[i].flushed = done_wi->n_flushed;
per_pool_cnt[i].evicted = done_wi->n_evicted;
#ifdef UNIV_MTFLUSH_DEBUG #ifdef UNIV_MTFLUSH_DEBUG
if((int)done_wi->id_usr == 0 && if((int)done_wi->id_usr == 0 &&
...@@ -576,7 +586,7 @@ buf_mtflu_flush_work_items( ...@@ -576,7 +586,7 @@ buf_mtflu_flush_work_items(
} }
#endif #endif
n_flushed+= done_wi->n_flushed; n_flushed+= done_wi->n_flushed+done_wi->n_evicted;
i++; i++;
} }
} }
...@@ -607,9 +617,9 @@ buf_mtflu_flush_list( ...@@ -607,9 +617,9 @@ buf_mtflu_flush_list(
back to caller. Ignored if NULL */ back to caller. Ignored if NULL */
{ {
ulint i; ulint i;
bool success = true; bool success = true;
ulint cnt_flush[MTFLUSH_MAX_WORKER]; flush_counters_t cnt[MTFLUSH_MAX_WORKER];
if (n_processed) { if (n_processed) {
*n_processed = 0; *n_processed = 0;
...@@ -627,20 +637,29 @@ buf_mtflu_flush_list( ...@@ -627,20 +637,29 @@ buf_mtflu_flush_list(
/* This lock is to safequard against re-entry if any. */ /* This lock is to safequard against re-entry if any. */
os_fast_mutex_lock(&mtflush_mtx); os_fast_mutex_lock(&mtflush_mtx);
buf_mtflu_flush_work_items(srv_buf_pool_instances, buf_mtflu_flush_work_items(srv_buf_pool_instances,
cnt_flush, BUF_FLUSH_LIST, cnt, BUF_FLUSH_LIST,
min_n, lsn_limit); min_n, lsn_limit);
os_fast_mutex_unlock(&mtflush_mtx); os_fast_mutex_unlock(&mtflush_mtx);
for (i = 0; i < srv_buf_pool_instances; i++) { for (i = 0; i < srv_buf_pool_instances; i++) {
if (n_processed) { if (n_processed) {
*n_processed += cnt_flush[i]; *n_processed += cnt[i].flushed+cnt[i].evicted;
} }
if (cnt_flush[i]) {
if (cnt[i].flushed) {
MONITOR_INC_VALUE_CUMULATIVE( MONITOR_INC_VALUE_CUMULATIVE(
MONITOR_FLUSH_BATCH_TOTAL_PAGE, MONITOR_FLUSH_BATCH_TOTAL_PAGE,
MONITOR_FLUSH_BATCH_COUNT, MONITOR_FLUSH_BATCH_COUNT,
MONITOR_FLUSH_BATCH_PAGES, MONITOR_FLUSH_BATCH_PAGES,
cnt_flush[i]); cnt[i].flushed);
}
if(cnt[i].evicted) {
MONITOR_INC_VALUE_CUMULATIVE(
MONITOR_LRU_BATCH_EVICT_TOTAL_PAGE,
MONITOR_LRU_BATCH_EVICT_COUNT,
MONITOR_LRU_BATCH_EVICT_PAGES,
cnt[i].evicted);
} }
} }
#ifdef UNIV_MTFLUSH_DEBUG #ifdef UNIV_MTFLUSH_DEBUG
...@@ -663,25 +682,38 @@ buf_mtflu_flush_LRU_tail(void) ...@@ -663,25 +682,38 @@ buf_mtflu_flush_LRU_tail(void)
/*==========================*/ /*==========================*/
{ {
ulint total_flushed=0, i; ulint total_flushed=0, i;
ulint cnt_flush[MTFLUSH_MAX_WORKER]; flush_counters_t cnt[MTFLUSH_MAX_WORKER];
ut_a(buf_mtflu_init_done()); ut_a(buf_mtflu_init_done());
/* At shutdown do not send requests anymore */
if (!mtflush_ctx || mtflush_ctx->gwt_status == WTHR_KILL_IT) {
return (total_flushed);
}
/* This lock is to safeguard against re-entry if any */ /* This lock is to safeguard against re-entry if any */
os_fast_mutex_lock(&mtflush_mtx); os_fast_mutex_lock(&mtflush_mtx);
buf_mtflu_flush_work_items(srv_buf_pool_instances, buf_mtflu_flush_work_items(srv_buf_pool_instances,
cnt_flush, BUF_FLUSH_LRU, srv_LRU_scan_depth, 0); cnt, BUF_FLUSH_LRU, srv_LRU_scan_depth, 0);
os_fast_mutex_unlock(&mtflush_mtx); os_fast_mutex_unlock(&mtflush_mtx);
for (i = 0; i < srv_buf_pool_instances; i++) { for (i = 0; i < srv_buf_pool_instances; i++) {
if (cnt_flush[i]) { total_flushed += cnt[i].flushed+cnt[i].evicted;
total_flushed += cnt_flush[i];
if (cnt[i].flushed) {
MONITOR_INC_VALUE_CUMULATIVE( MONITOR_INC_VALUE_CUMULATIVE(
MONITOR_LRU_BATCH_TOTAL_PAGE, MONITOR_LRU_BATCH_FLUSH_TOTAL_PAGE,
MONITOR_LRU_BATCH_COUNT, MONITOR_LRU_BATCH_FLUSH_COUNT,
MONITOR_LRU_BATCH_PAGES, MONITOR_LRU_BATCH_FLUSH_PAGES,
cnt_flush[i]); cnt[i].flushed);
}
if(cnt[i].evicted) {
MONITOR_INC_VALUE_CUMULATIVE(
MONITOR_LRU_BATCH_EVICT_TOTAL_PAGE,
MONITOR_LRU_BATCH_EVICT_COUNT,
MONITOR_LRU_BATCH_EVICT_PAGES,
cnt[i].evicted);
} }
} }
......
...@@ -1199,7 +1199,9 @@ UNIV_INTERN ...@@ -1199,7 +1199,9 @@ UNIV_INTERN
bool bool
buf_page_io_complete( 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 Calculates a folded value of a file page address to use in the page hash
table. table.
...@@ -1762,6 +1764,133 @@ Compute the hash fold value for blocks in buf_pool->zip_hash. */ ...@@ -1762,6 +1764,133 @@ Compute the hash fold value for blocks in buf_pool->zip_hash. */
#define BUF_POOL_ZIP_FOLD_BPAGE(b) BUF_POOL_ZIP_FOLD((buf_block_t*) (b)) #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 that is embedded in the free zip blocks */
struct buf_buddy_free_t { struct buf_buddy_free_t {
union { union {
...@@ -1894,7 +2023,7 @@ struct buf_pool_t{ ...@@ -1894,7 +2023,7 @@ struct buf_pool_t{
also protects writes to also protects writes to
bpage::oldest_modification and bpage::oldest_modification and
flush_list_hp */ flush_list_hp */
const buf_page_t* flush_list_hp;/*!< "hazard pointer" FlushHp flush_hp;/*!< "hazard pointer"
used during scan of flush_list used during scan of flush_list
while doing flush list batch. while doing flush list batch.
Protected by flush_list_mutex */ Protected by flush_list_mutex */
...@@ -1952,6 +2081,19 @@ struct buf_pool_t{ ...@@ -1952,6 +2081,19 @@ struct buf_pool_t{
UT_LIST_BASE_NODE_T(buf_page_t) free; UT_LIST_BASE_NODE_T(buf_page_t) free;
/*!< base node of the free /*!< base node of the free
block list */ 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; UT_LIST_BASE_NODE_T(buf_page_t) LRU;
/*!< base node of the LRU list */ /*!< base node of the LRU list */
buf_page_t* LRU_old; /*!< pointer to the about buf_page_t* LRU_old; /*!< pointer to the about
......
...@@ -37,6 +37,17 @@ Created 11/5/1995 Heikki Tuuri ...@@ -37,6 +37,17 @@ Created 11/5/1995 Heikki Tuuri
/** Flag indicating if the page_cleaner is in active state. */ /** Flag indicating if the page_cleaner is in active state. */
extern ibool buf_page_cleaner_is_active; extern ibool buf_page_cleaner_is_active;
/** Event to synchronise with the flushing. */
extern os_event_t buf_flush_event;
/** Handled page counters for a single flush */
struct flush_counters_t {
ulint flushed; /*!< number of dirty pages flushed */
ulint evicted; /*!< number of clean pages evicted */
ulint unzip_LRU_evicted;/*!< number of uncompressed page images
evicted */
};
/********************************************************************//** /********************************************************************//**
Remove a block from the flush list of modified blocks. */ Remove a block from the flush list of modified blocks. */
UNIV_INTERN UNIV_INTERN
...@@ -111,12 +122,12 @@ buf_flush_list( ...@@ -111,12 +122,12 @@ buf_flush_list(
which were processed is passed which were processed is passed
back to caller. Ignored if NULL */ back to caller. Ignored if NULL */
/******************************************************************//** /******************************************************************//**
This function picks up a single dirty page from the tail of the LRU This function picks up a single page from the tail of the LRU
list, flushes it, removes it from page_hash and LRU list and puts list, flushes it (if it is dirty), removes it from page_hash and LRU
it on the free list. It is called from user threads when they are list and puts it on the free list. It is called from user threads when
unable to find a replacable page at the tail of the LRU list i.e.: they are unable to find a replaceable page at the tail of the LRU
when the background LRU flushing in the page_cleaner thread is not list i.e.: when the background LRU flushing in the page_cleaner thread
fast enough to keep pace with the workload. is not fast enough to keep pace with the workload.
@return TRUE if success. */ @return TRUE if success. */
UNIV_INTERN UNIV_INTERN
ibool ibool
...@@ -309,9 +320,9 @@ This utility flushes dirty blocks from the end of the LRU list or flush_list. ...@@ -309,9 +320,9 @@ This utility flushes dirty blocks from the end of the LRU list or flush_list.
NOTE 1: in the case of an LRU flush the calling thread may own latches to NOTE 1: in the case of an LRU flush the calling thread may own latches to
pages: to avoid deadlocks, this function must be written so that it cannot 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, 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! the calling thread is not allowed to own any latches on pages! */
@return number of blocks for which the write request was queued */ __attribute__((nonnull))
ulint void
buf_flush_batch( buf_flush_batch(
/*============*/ /*============*/
buf_pool_t* buf_pool, /*!< in: buffer pool instance */ buf_pool_t* buf_pool, /*!< in: buffer pool instance */
...@@ -322,11 +333,13 @@ buf_flush_batch( ...@@ -322,11 +333,13 @@ buf_flush_batch(
ulint min_n, /*!< in: wished minimum mumber of blocks ulint min_n, /*!< in: wished minimum mumber of blocks
flushed (it is not guaranteed that the flushed (it is not guaranteed that the
actual number is that big, though) */ actual number is that big, though) */
lsn_t lsn_limit); /*!< in: in the case of BUF_FLUSH_LIST lsn_t lsn_limit, /*!< in: in the case of BUF_FLUSH_LIST
all blocks whose oldest_modification is all blocks whose oldest_modification is
smaller than this should be flushed smaller than this should be flushed
(if their number does not exceed (if their number does not exceed
min_n), otherwise ignored */ min_n), otherwise ignored */
flush_counters_t* n); /*!< out: flushed/evicted page
counts */
#ifndef UNIV_NONINL #ifndef UNIV_NONINL
......
...@@ -117,7 +117,7 @@ buf_LRU_get_free_only( ...@@ -117,7 +117,7 @@ buf_LRU_get_free_only(
buf_pool_t* buf_pool); /*!< buffer pool instance */ buf_pool_t* buf_pool); /*!< buffer pool instance */
/******************************************************************//** /******************************************************************//**
Returns a free block from the buf_pool. The block is taken off the 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. LRU list to the free list.
This function is called from a user thread when it needs a clean 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 block to read in a page. Note that we only ever get a block from
...@@ -125,8 +125,6 @@ the free list. Even when we flush a page or find a page in LRU scan ...@@ -125,8 +125,6 @@ the free list. Even when we flush a page or find a page in LRU scan
we put it to free list to be used. we put it to free list to be used.
* iteration 0: * iteration 0:
* get a block from free list, success:done * 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 * if buf_pool->try_LRU_scan is set
* scan LRU up to srv_LRU_scan_depth to find a clean block * scan LRU up to srv_LRU_scan_depth to find a clean block
* the above will put the block on free list * the above will put the block on free list
...@@ -139,7 +137,7 @@ we put it to free list to be used. ...@@ -139,7 +137,7 @@ we put it to free list to be used.
* scan whole LRU list * scan whole LRU list
* scan LRU list even if buf_pool->try_LRU_scan is not set * scan LRU list even if buf_pool->try_LRU_scan is not set
* iteration > 1: * 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 */ @return the free control block, in state BUF_BLOCK_READY_FOR_USE */
UNIV_INTERN UNIV_INTERN
buf_block_t* buf_block_t*
...@@ -231,6 +229,15 @@ buf_LRU_free_one_page( ...@@ -231,6 +229,15 @@ buf_LRU_free_one_page(
may or may not be a hash index to the page */ may or may not be a hash index to the page */
__attribute__((nonnull)); __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 #if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
/**********************************************************************//** /**********************************************************************//**
Validates the LRU list. Validates the LRU list.
......
...@@ -174,7 +174,6 @@ enum monitor_id_t { ...@@ -174,7 +174,6 @@ enum monitor_id_t {
MONITOR_FLUSH_BATCH_SCANNED, MONITOR_FLUSH_BATCH_SCANNED,
MONITOR_FLUSH_BATCH_SCANNED_NUM_CALL, MONITOR_FLUSH_BATCH_SCANNED_NUM_CALL,
MONITOR_FLUSH_BATCH_SCANNED_PER_CALL, MONITOR_FLUSH_BATCH_SCANNED_PER_CALL,
MONITOR_FLUSH_HP_RESCAN,
MONITOR_FLUSH_BATCH_TOTAL_PAGE, MONITOR_FLUSH_BATCH_TOTAL_PAGE,
MONITOR_FLUSH_BATCH_COUNT, MONITOR_FLUSH_BATCH_COUNT,
MONITOR_FLUSH_BATCH_PAGES, MONITOR_FLUSH_BATCH_PAGES,
...@@ -199,9 +198,12 @@ enum monitor_id_t { ...@@ -199,9 +198,12 @@ enum monitor_id_t {
MONITOR_LRU_BATCH_SCANNED, MONITOR_LRU_BATCH_SCANNED,
MONITOR_LRU_BATCH_SCANNED_NUM_CALL, MONITOR_LRU_BATCH_SCANNED_NUM_CALL,
MONITOR_LRU_BATCH_SCANNED_PER_CALL, MONITOR_LRU_BATCH_SCANNED_PER_CALL,
MONITOR_LRU_BATCH_TOTAL_PAGE, MONITOR_LRU_BATCH_FLUSH_TOTAL_PAGE,
MONITOR_LRU_BATCH_COUNT, MONITOR_LRU_BATCH_FLUSH_COUNT,
MONITOR_LRU_BATCH_PAGES, 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,
MONITOR_LRU_SINGLE_FLUSH_SCANNED_NUM_CALL, MONITOR_LRU_SINGLE_FLUSH_SCANNED_NUM_CALL,
MONITOR_LRU_SINGLE_FLUSH_SCANNED_PER_CALL, MONITOR_LRU_SINGLE_FLUSH_SCANNED_PER_CALL,
......
...@@ -348,7 +348,10 @@ DECLARE_THREAD(recv_writer_thread)( ...@@ -348,7 +348,10 @@ DECLARE_THREAD(recv_writer_thread)(
while (srv_shutdown_state == SRV_SHUTDOWN_NONE) { 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); mutex_enter(&recv_sys->writer_mutex);
......
...@@ -350,11 +350,6 @@ static monitor_info_t innodb_counter_info[] = ...@@ -350,11 +350,6 @@ static monitor_info_t innodb_counter_info[] =
MONITOR_SET_MEMBER, MONITOR_FLUSH_BATCH_SCANNED, MONITOR_SET_MEMBER, MONITOR_FLUSH_BATCH_SCANNED,
MONITOR_FLUSH_BATCH_SCANNED_PER_CALL}, 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 */ /* Cumulative counter for pages flushed in flush batches */
{"buffer_flush_batch_total_pages", "buffer", {"buffer_flush_batch_total_pages", "buffer",
"Total pages flushed as part of flush batch", "Total pages flushed as part of flush batch",
...@@ -482,20 +477,36 @@ static monitor_info_t innodb_counter_info[] = ...@@ -482,20 +477,36 @@ static monitor_info_t innodb_counter_info[] =
MONITOR_LRU_BATCH_SCANNED_PER_CALL}, MONITOR_LRU_BATCH_SCANNED_PER_CALL},
/* Cumulative counter for LRU batch pages flushed */ /* 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", "Total pages flushed as part of LRU batches",
MONITOR_SET_OWNER, MONITOR_LRU_BATCH_COUNT, MONITOR_SET_OWNER, MONITOR_LRU_BATCH_FLUSH_COUNT,
MONITOR_LRU_BATCH_TOTAL_PAGE}, 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", "buffer", {"buffer_LRU_batches_evict", "buffer",
"Number of LRU batches", "Number of LRU batches",
MONITOR_SET_MEMBER, MONITOR_LRU_BATCH_TOTAL_PAGE, MONITOR_SET_MEMBER, MONITOR_LRU_BATCH_EVICT_TOTAL_PAGE,
MONITOR_LRU_BATCH_COUNT}, MONITOR_LRU_BATCH_EVICT_COUNT},
{"buffer_LRU_batch_pages", "buffer", {"buffer_LRU_batch_evict_pages", "buffer",
"Pages queued as an LRU batch", "Pages queued as an LRU batch",
MONITOR_SET_MEMBER, MONITOR_LRU_BATCH_TOTAL_PAGE, MONITOR_SET_MEMBER, MONITOR_LRU_BATCH_EVICT_TOTAL_PAGE,
MONITOR_LRU_BATCH_PAGES}, MONITOR_LRU_BATCH_EVICT_PAGES},
/* Cumulative counter for single page LRU scans */ /* Cumulative counter for single page LRU scans */
{"buffer_LRU_single_flush_scanned", "buffer", {"buffer_LRU_single_flush_scanned", "buffer",
......
...@@ -2338,19 +2338,26 @@ buf_flush_LRU_tail(void) ...@@ -2338,19 +2338,26 @@ buf_flush_LRU_tail(void)
free_len = UT_LIST_GET_LEN( free_len = UT_LIST_GET_LEN(
buf_pool->free); buf_pool->free);
} }
if (n.flushed) {
MONITOR_INC_VALUE_CUMULATIVE(
MONITOR_LRU_BATCH_FLUSH_TOTAL_PAGE,
MONITOR_LRU_BATCH_FLUSH_COUNT,
MONITOR_LRU_BATCH_FLUSH_PAGES,
n.flushed);
}
if (n.evicted) {
MONITOR_INC_VALUE_CUMULATIVE(
MONITOR_LRU_BATCH_EVICT_TOTAL_PAGE,
MONITOR_LRU_BATCH_EVICT_COUNT,
MONITOR_LRU_BATCH_EVICT_PAGES,
n.evicted);
}
} while (active_instance[i] } while (active_instance[i]
&& free_len <= free_list_lwm); && free_len <= free_list_lwm);
} }
} }
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); return(total_flushed);
} }
......
...@@ -110,7 +110,8 @@ typedef struct wrk_itm ...@@ -110,7 +110,8 @@ typedef struct wrk_itm
will be used */ will be used */
wr_tsk_t wr; /*!< Flush page list */ wr_tsk_t wr; /*!< Flush page list */
rd_tsk_t rd; /*!< Decompress page list */ rd_tsk_t rd; /*!< Decompress page list */
ulint n_flushed; /*!< Flushed pages count */ ulint n_flushed; /*!< Number of flushed pages */
ulint n_evicted; /*!< Number of evicted pages */
os_thread_id_t id_usr; /*!< Thread-id currently working */ os_thread_id_t id_usr; /*!< Thread-id currently working */
wrk_status_t wi_status; /*!< Work item status */ wrk_status_t wi_status; /*!< Work item status */
mem_heap_t *wheap; /*!< Heap were to allocate memory mem_heap_t *wheap; /*!< Heap were to allocate memory
...@@ -366,7 +367,7 @@ void ...@@ -366,7 +367,7 @@ void
buf_mtflu_io_thread_exit(void) buf_mtflu_io_thread_exit(void)
/*==========================*/ /*==========================*/
{ {
long i; ulint i;
thread_sync_t* mtflush_io = mtflush_ctx; thread_sync_t* mtflush_io = mtflush_ctx;
wrk_t* work_item = NULL; wrk_t* work_item = NULL;
...@@ -397,7 +398,7 @@ buf_mtflu_io_thread_exit(void) ...@@ -397,7 +398,7 @@ buf_mtflu_io_thread_exit(void)
os_fast_mutex_lock(&mtflush_mtx); os_fast_mutex_lock(&mtflush_mtx);
/* Send one exit work item/thread */ /* Send one exit work item/thread */
for (i=0; i < srv_mtflush_threads; i++) { for (i=0; i < (ulint)srv_mtflush_threads; i++) {
work_item[i].tsk = MT_WRK_NONE; work_item[i].tsk = MT_WRK_NONE;
work_item[i].wi_status = WRK_ITEM_EXIT; work_item[i].wi_status = WRK_ITEM_EXIT;
work_item[i].wheap = mtflush_io->wheap; work_item[i].wheap = mtflush_io->wheap;
...@@ -417,11 +418,8 @@ buf_mtflu_io_thread_exit(void) ...@@ -417,11 +418,8 @@ buf_mtflu_io_thread_exit(void)
ut_a(ib_wqueue_is_empty(mtflush_io->wq)); ut_a(ib_wqueue_is_empty(mtflush_io->wq));
/* Requests sent */
os_fast_mutex_unlock(&mtflush_mtx);
/* Collect all work done items */ /* Collect all work done items */
for (i=0; i < srv_mtflush_threads;) { for (i=0; i < (ulint)srv_mtflush_threads;) {
wrk_t* work_item = NULL; wrk_t* work_item = NULL;
work_item = (wrk_t *)ib_wqueue_timedwait(mtflush_io->wr_cq, MT_WAIT_IN_USECS); work_item = (wrk_t *)ib_wqueue_timedwait(mtflush_io->wr_cq, MT_WAIT_IN_USECS);
...@@ -436,6 +434,16 @@ buf_mtflu_io_thread_exit(void) ...@@ -436,6 +434,16 @@ buf_mtflu_io_thread_exit(void)
/* Wait about 1/2 sec to allow threads really exit */ /* Wait about 1/2 sec to allow threads really exit */
os_thread_sleep(MT_WAIT_IN_USECS); os_thread_sleep(MT_WAIT_IN_USECS);
while(!ib_wqueue_is_empty(mtflush_io->wq))
{
ib_wqueue_nowait(mtflush_io->wq);
}
while(!ib_wqueue_is_empty(mtflush_io->wq))
{
ib_wqueue_nowait(mtflush_io->wq);
}
ut_a(ib_wqueue_is_empty(mtflush_io->wq)); ut_a(ib_wqueue_is_empty(mtflush_io->wq));
ut_a(ib_wqueue_is_empty(mtflush_io->wr_cq)); ut_a(ib_wqueue_is_empty(mtflush_io->wr_cq));
ut_a(ib_wqueue_is_empty(mtflush_io->rd_cq)); ut_a(ib_wqueue_is_empty(mtflush_io->rd_cq));
...@@ -445,6 +453,8 @@ buf_mtflu_io_thread_exit(void) ...@@ -445,6 +453,8 @@ buf_mtflu_io_thread_exit(void)
ib_wqueue_free(mtflush_io->wr_cq); ib_wqueue_free(mtflush_io->wr_cq);
ib_wqueue_free(mtflush_io->rd_cq); ib_wqueue_free(mtflush_io->rd_cq);
/* Requests sent */
os_fast_mutex_unlock(&mtflush_mtx);
os_fast_mutex_free(&mtflush_mtx); os_fast_mutex_free(&mtflush_mtx);
os_fast_mutex_free(&mtflush_io->thread_global_mtx); os_fast_mutex_free(&mtflush_io->thread_global_mtx);
...@@ -522,8 +532,8 @@ ulint ...@@ -522,8 +532,8 @@ ulint
buf_mtflu_flush_work_items( buf_mtflu_flush_work_items(
/*=======================*/ /*=======================*/
ulint buf_pool_inst, /*!< in: Number of buffer pool instances */ ulint buf_pool_inst, /*!< in: Number of buffer pool instances */
ulint *per_pool_pages_flushed, /*!< out: Number of pages flush_counters_t *per_pool_cnt, /*!< out: Number of pages
flushed/instance */ flushed or evicted /instance */
buf_flush_t flush_type, /*!< in: Type of flush */ buf_flush_t flush_type, /*!< in: Type of flush */
ulint min_n, /*!< in: Wished minimum number of ulint min_n, /*!< in: Wished minimum number of
blocks to be flushed */ blocks to be flushed */
...@@ -553,6 +563,7 @@ buf_mtflu_flush_work_items( ...@@ -553,6 +563,7 @@ buf_mtflu_flush_work_items(
work_item[i].wheap = work_heap; work_item[i].wheap = work_heap;
work_item[i].rheap = reply_heap; work_item[i].rheap = reply_heap;
work_item[i].n_flushed = 0; work_item[i].n_flushed = 0;
work_item[i].n_evicted = 0;
work_item[i].id_usr = 0; work_item[i].id_usr = 0;
ib_wqueue_add(mtflush_ctx->wq, ib_wqueue_add(mtflush_ctx->wq,
...@@ -566,7 +577,8 @@ buf_mtflu_flush_work_items( ...@@ -566,7 +577,8 @@ buf_mtflu_flush_work_items(
done_wi = (wrk_t *)ib_wqueue_wait(mtflush_ctx->wr_cq); done_wi = (wrk_t *)ib_wqueue_wait(mtflush_ctx->wr_cq);
if (done_wi != NULL) { if (done_wi != NULL) {
per_pool_pages_flushed[i] = done_wi->n_flushed; per_pool_cnt[i].flushed = done_wi->n_flushed;
per_pool_cnt[i].evicted = done_wi->n_evicted;
#ifdef UNIV_MTFLUSH_DEBUG #ifdef UNIV_MTFLUSH_DEBUG
if((int)done_wi->id_usr == 0 && if((int)done_wi->id_usr == 0 &&
...@@ -580,7 +592,7 @@ buf_mtflu_flush_work_items( ...@@ -580,7 +592,7 @@ buf_mtflu_flush_work_items(
} }
#endif #endif
n_flushed+= done_wi->n_flushed; n_flushed+= done_wi->n_flushed+done_wi->n_evicted;
i++; i++;
} }
} }
...@@ -614,9 +626,9 @@ buf_mtflu_flush_list( ...@@ -614,9 +626,9 @@ buf_mtflu_flush_list(
back to caller. Ignored if NULL */ back to caller. Ignored if NULL */
{ {
ulint i; ulint i;
bool success = true; bool success = true;
ulint cnt_flush[MTFLUSH_MAX_WORKER]; flush_counters_t cnt[MTFLUSH_MAX_WORKER];
if (n_processed) { if (n_processed) {
*n_processed = 0; *n_processed = 0;
...@@ -634,20 +646,29 @@ buf_mtflu_flush_list( ...@@ -634,20 +646,29 @@ buf_mtflu_flush_list(
/* This lock is to safequard against re-entry if any. */ /* This lock is to safequard against re-entry if any. */
os_fast_mutex_lock(&mtflush_mtx); os_fast_mutex_lock(&mtflush_mtx);
buf_mtflu_flush_work_items(srv_buf_pool_instances, buf_mtflu_flush_work_items(srv_buf_pool_instances,
cnt_flush, BUF_FLUSH_LIST, cnt, BUF_FLUSH_LIST,
min_n, lsn_limit); min_n, lsn_limit);
os_fast_mutex_unlock(&mtflush_mtx); os_fast_mutex_unlock(&mtflush_mtx);
for (i = 0; i < srv_buf_pool_instances; i++) { for (i = 0; i < srv_buf_pool_instances; i++) {
if (n_processed) { if (n_processed) {
*n_processed += cnt_flush[i]; *n_processed += cnt[i].flushed+cnt[i].evicted;
} }
if (cnt_flush[i]) {
if (cnt[i].flushed) {
MONITOR_INC_VALUE_CUMULATIVE( MONITOR_INC_VALUE_CUMULATIVE(
MONITOR_FLUSH_BATCH_TOTAL_PAGE, MONITOR_FLUSH_BATCH_TOTAL_PAGE,
MONITOR_FLUSH_BATCH_COUNT, MONITOR_FLUSH_BATCH_COUNT,
MONITOR_FLUSH_BATCH_PAGES, MONITOR_FLUSH_BATCH_PAGES,
cnt_flush[i]); cnt[i].flushed);
}
if(cnt[i].evicted) {
MONITOR_INC_VALUE_CUMULATIVE(
MONITOR_LRU_BATCH_EVICT_TOTAL_PAGE,
MONITOR_LRU_BATCH_EVICT_COUNT,
MONITOR_LRU_BATCH_EVICT_PAGES,
cnt[i].evicted);
} }
} }
#ifdef UNIV_MTFLUSH_DEBUG #ifdef UNIV_MTFLUSH_DEBUG
...@@ -670,25 +691,37 @@ buf_mtflu_flush_LRU_tail(void) ...@@ -670,25 +691,37 @@ buf_mtflu_flush_LRU_tail(void)
/*==========================*/ /*==========================*/
{ {
ulint total_flushed=0, i; ulint total_flushed=0, i;
ulint cnt_flush[MTFLUSH_MAX_WORKER]; flush_counters_t cnt[MTFLUSH_MAX_WORKER];
ut_a(buf_mtflu_init_done()); ut_a(buf_mtflu_init_done());
/* At shutdown do not send requests anymore */
if (!mtflush_ctx || mtflush_ctx->gwt_status == WTHR_KILL_IT) {
return (total_flushed);
}
/* This lock is to safeguard against re-entry if any */ /* This lock is to safeguard against re-entry if any */
os_fast_mutex_lock(&mtflush_mtx); os_fast_mutex_lock(&mtflush_mtx);
buf_mtflu_flush_work_items(srv_buf_pool_instances, buf_mtflu_flush_work_items(srv_buf_pool_instances,
cnt_flush, BUF_FLUSH_LRU, srv_LRU_scan_depth, 0); cnt, BUF_FLUSH_LRU, srv_LRU_scan_depth, 0);
os_fast_mutex_unlock(&mtflush_mtx); os_fast_mutex_unlock(&mtflush_mtx);
for (i = 0; i < srv_buf_pool_instances; i++) { for (i = 0; i < srv_buf_pool_instances; i++) {
if (cnt_flush[i]) { total_flushed += cnt[i].flushed+cnt[i].evicted;
total_flushed += cnt_flush[i];
if (cnt[i].flushed) {
MONITOR_INC_VALUE_CUMULATIVE( MONITOR_INC_VALUE_CUMULATIVE(
MONITOR_LRU_BATCH_TOTAL_PAGE, MONITOR_LRU_BATCH_FLUSH_TOTAL_PAGE,
MONITOR_LRU_BATCH_COUNT, MONITOR_LRU_BATCH_FLUSH_COUNT,
MONITOR_LRU_BATCH_PAGES, MONITOR_LRU_BATCH_FLUSH_PAGES,
cnt_flush[i]); cnt[i].flushed);
}
if(cnt[i].evicted) {
MONITOR_INC_VALUE_CUMULATIVE(
MONITOR_LRU_BATCH_EVICT_TOTAL_PAGE,
MONITOR_LRU_BATCH_EVICT_COUNT,
MONITOR_LRU_BATCH_EVICT_PAGES,
cnt[i].evicted);
} }
} }
......
...@@ -199,9 +199,12 @@ enum monitor_id_t { ...@@ -199,9 +199,12 @@ enum monitor_id_t {
MONITOR_LRU_BATCH_SCANNED, MONITOR_LRU_BATCH_SCANNED,
MONITOR_LRU_BATCH_SCANNED_NUM_CALL, MONITOR_LRU_BATCH_SCANNED_NUM_CALL,
MONITOR_LRU_BATCH_SCANNED_PER_CALL, MONITOR_LRU_BATCH_SCANNED_PER_CALL,
MONITOR_LRU_BATCH_TOTAL_PAGE, MONITOR_LRU_BATCH_FLUSH_TOTAL_PAGE,
MONITOR_LRU_BATCH_COUNT, MONITOR_LRU_BATCH_FLUSH_COUNT,
MONITOR_LRU_BATCH_PAGES, 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,
MONITOR_LRU_SINGLE_FLUSH_SCANNED_NUM_CALL, MONITOR_LRU_SINGLE_FLUSH_SCANNED_NUM_CALL,
MONITOR_LRU_SINGLE_FLUSH_SCANNED_PER_CALL, MONITOR_LRU_SINGLE_FLUSH_SCANNED_PER_CALL,
......
...@@ -350,11 +350,6 @@ static monitor_info_t innodb_counter_info[] = ...@@ -350,11 +350,6 @@ static monitor_info_t innodb_counter_info[] =
MONITOR_SET_MEMBER, MONITOR_FLUSH_BATCH_SCANNED, MONITOR_SET_MEMBER, MONITOR_FLUSH_BATCH_SCANNED,
MONITOR_FLUSH_BATCH_SCANNED_PER_CALL}, 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 */ /* Cumulative counter for pages flushed in flush batches */
{"buffer_flush_batch_total_pages", "buffer", {"buffer_flush_batch_total_pages", "buffer",
"Total pages flushed as part of flush batch", "Total pages flushed as part of flush batch",
...@@ -482,20 +477,36 @@ static monitor_info_t innodb_counter_info[] = ...@@ -482,20 +477,36 @@ static monitor_info_t innodb_counter_info[] =
MONITOR_LRU_BATCH_SCANNED_PER_CALL}, MONITOR_LRU_BATCH_SCANNED_PER_CALL},
/* Cumulative counter for LRU batch pages flushed */ /* 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", "Total pages flushed as part of LRU batches",
MONITOR_SET_OWNER, MONITOR_LRU_BATCH_COUNT, MONITOR_SET_OWNER, MONITOR_LRU_BATCH_FLUSH_COUNT,
MONITOR_LRU_BATCH_TOTAL_PAGE}, 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", "buffer", {"buffer_LRU_batches_evict", "buffer",
"Number of LRU batches", "Number of LRU batches",
MONITOR_SET_MEMBER, MONITOR_LRU_BATCH_TOTAL_PAGE, MONITOR_SET_MEMBER, MONITOR_LRU_BATCH_EVICT_TOTAL_PAGE,
MONITOR_LRU_BATCH_COUNT}, MONITOR_LRU_BATCH_EVICT_COUNT},
{"buffer_LRU_batch_pages", "buffer", {"buffer_LRU_batch_evict_pages", "buffer",
"Pages queued as an LRU batch", "Pages queued as an LRU batch",
MONITOR_SET_MEMBER, MONITOR_LRU_BATCH_TOTAL_PAGE, MONITOR_SET_MEMBER, MONITOR_LRU_BATCH_EVICT_TOTAL_PAGE,
MONITOR_LRU_BATCH_PAGES}, MONITOR_LRU_BATCH_EVICT_PAGES},
/* Cumulative counter for single page LRU scans */ /* Cumulative counter for single page LRU scans */
{"buffer_LRU_single_flush_scanned", "buffer", {"buffer_LRU_single_flush_scanned", "buffer",
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment