⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 buf0lru.c

📁 这是linux下运行的mysql软件包,可用于linux 下安装 php + mysql + apach 的网络配置
💻 C
📖 第 1 页 / 共 2 页
字号:
/******************************************************The database buffer replacement algorithm(c) 1995 Innobase OyCreated 11/5/1995 Heikki Tuuri*******************************************************/#include "buf0lru.h"#ifdef UNIV_NONINL#include "buf0lru.ic"#include "srv0srv.h"	/* Needed to getsrv_print_innodb_monitor */#endif#include "ut0byte.h"#include "ut0lst.h"#include "ut0rnd.h"#include "sync0sync.h"#include "sync0rw.h"#include "hash0hash.h"#include "os0sync.h"#include "fil0fil.h"#include "btr0btr.h"#include "buf0buf.h"#include "buf0flu.h"#include "buf0rea.h"#include "btr0sea.h"#include "os0file.h"#include "log0recv.h"/* The number of blocks from the LRU_old pointer onward, including the blockpointed to, must be 3/8 of the whole LRU list length, except that thetolerance defined below is allowed. Note that the tolerance must be smallenough such that for even the BUF_LRU_OLD_MIN_LEN long LRU list, theLRU_old pointer is not allowed to point to either end of the LRU list. */#define BUF_LRU_OLD_TOLERANCE	20/* The whole LRU list length is divided by this number to determine aninitial segment in buf_LRU_get_recent_limit */#define BUF_LRU_INITIAL_RATIO	8/* If we switch on the InnoDB monitor because there are too few availableframes in the buffer pool, we set this to TRUE */ibool	buf_lru_switched_on_innodb_mon	= FALSE;/**********************************************************************Takes a block out of the LRU list and page hash table and sets the blockstate to BUF_BLOCK_REMOVE_HASH. */staticvoidbuf_LRU_block_remove_hashed_page(/*=============================*/	buf_block_t*	block);	/* in: block, must contain a file page and				be in a state where it can be freed; there				may or may not be a hash index to the page *//**********************************************************************Puts a file page whose has no hash index to the free list. */staticvoidbuf_LRU_block_free_hashed_page(/*===========================*/	buf_block_t*	block);	/* in: block, must contain a file page and				be in a state where it can be freed *//**********************************************************************Invalidates all pages belonging to a given tablespace when we are deletingthe data file(s) of that tablespace. */voidbuf_LRU_invalidate_tablespace(/*==========================*/	ulint	id)	/* in: space id */{	buf_block_t*	block;	ulint		page_no;	ibool		all_freed;scan_again:	mutex_enter(&(buf_pool->mutex));		all_freed = TRUE;		block = UT_LIST_GET_LAST(buf_pool->LRU);	while (block != NULL) {	        ut_a(block->state == BUF_BLOCK_FILE_PAGE);		if (block->space == id		    && (block->buf_fix_count > 0 || block->io_fix != 0)) {			/* We cannot remove this page during this scan yet;			maybe the system is currently reading it in, or			flushing the modifications to the file */						all_freed = FALSE;			goto next_page;		}		if (block->space == id) {#ifdef UNIV_DEBUG			if (buf_debug_prints) {				printf(				"Dropping space %lu page %lu\n",					(ulong) block->space,				        (ulong) block->offset);			}#endif			if (block->is_hashed) {				page_no = block->offset;							mutex_exit(&(buf_pool->mutex));				/* Note that the following call will acquire				an S-latch on the page */				btr_search_drop_page_hash_when_freed(id,								page_no);				goto scan_again;			}			if (0 != ut_dulint_cmp(block->oldest_modification,							ut_dulint_zero)) {				/* Remove from the flush list of modified				blocks */				block->oldest_modification = ut_dulint_zero;				UT_LIST_REMOVE(flush_list, 						buf_pool->flush_list, block);			}			/* Remove from the LRU list */			buf_LRU_block_remove_hashed_page(block);			buf_LRU_block_free_hashed_page(block);		}next_page:		block = UT_LIST_GET_PREV(LRU, block);	}	mutex_exit(&(buf_pool->mutex));		if (!all_freed) {		os_thread_sleep(20000);	        goto scan_again;	}}/**********************************************************************Gets the minimum LRU_position field for the blocks in an initial segment(determined by BUF_LRU_INITIAL_RATIO) of the LRU list. The limit is notguaranteed to be precise, because the ulint_clock may wrap around. */ulintbuf_LRU_get_recent_limit(void)/*==========================*/			/* out: the limit; zero if could not determine it */{	buf_block_t*	block;	ulint		len;	ulint		limit;	mutex_enter(&(buf_pool->mutex));	len = UT_LIST_GET_LEN(buf_pool->LRU);	if (len < BUF_LRU_OLD_MIN_LEN) {		/* The LRU list is too short to do read-ahead */		mutex_exit(&(buf_pool->mutex));		return(0);	}	block = UT_LIST_GET_FIRST(buf_pool->LRU);	limit = block->LRU_position - len / BUF_LRU_INITIAL_RATIO;	mutex_exit(&(buf_pool->mutex));	return(limit);}/**********************************************************************Look for a replaceable block from the end of the LRU list and put it tothe free list if found. */iboolbuf_LRU_search_and_free_block(/*==========================*/				/* out: TRUE if freed */	ulint	n_iterations)   /* in: how many times this has been called				repeatedly without result: a high value means				that we should search farther; if value is				k < 10, then we only search k/10 * [number				of pages in the buffer pool] from the end				of the LRU list */{	buf_block_t*	block;	ulint		distance = 0;	ibool		freed;	mutex_enter(&(buf_pool->mutex));		freed = FALSE;	block = UT_LIST_GET_LAST(buf_pool->LRU);	while (block != NULL) {	        ut_a(block->in_LRU_list);		if (buf_flush_ready_for_replace(block)) {#ifdef UNIV_DEBUG			if (buf_debug_prints) {				fprintf(stderr,				"Putting space %lu page %lu to free list\n",					(ulong) block->space,				        (ulong) block->offset);			}#endif /* UNIV_DEBUG */			buf_LRU_block_remove_hashed_page(block);			mutex_exit(&(buf_pool->mutex));			/* Remove possible adaptive hash index built on the			page; in the case of AWE the block may not have a			frame at all */						if (block->frame) {				btr_search_drop_page_hash_index(block->frame);			}			mutex_enter(&(buf_pool->mutex));			ut_a(block->buf_fix_count == 0);			buf_LRU_block_free_hashed_page(block);			freed = TRUE;			break;		}		block = UT_LIST_GET_PREV(LRU, block);		distance++;		if (!freed && n_iterations <= 10		    && distance > 100 + (n_iterations * buf_pool->curr_size)					/ 10) {			buf_pool->LRU_flush_ended = 0;			mutex_exit(&(buf_pool->mutex));						return(FALSE);		}	}	if (buf_pool->LRU_flush_ended > 0) {		buf_pool->LRU_flush_ended--;	} 	if (!freed) {		buf_pool->LRU_flush_ended = 0;	}	mutex_exit(&(buf_pool->mutex));		return(freed);}	/**********************************************************************Tries to remove LRU flushed blocks from the end of the LRU list and put themto the free list. This is beneficial for the efficiency of the insert bufferoperation, as flushed pages from non-unique non-clustered indexes are heretaken out of the buffer pool, and their inserts redirected to the insertbuffer. Otherwise, the flushed blocks could get modified again before readoperations need new buffer blocks, and the i/o work done in flushing would bewasted. */voidbuf_LRU_try_free_flushed_blocks(void)/*=================================*/{	mutex_enter(&(buf_pool->mutex));	while (buf_pool->LRU_flush_ended > 0) {		mutex_exit(&(buf_pool->mutex));		buf_LRU_search_and_free_block(1);				mutex_enter(&(buf_pool->mutex));	}	mutex_exit(&(buf_pool->mutex));}	/**********************************************************************Returns TRUE if less than 25 % of the buffer pool is available. This can beused in heuristics to prevent huge transactions eating up the whole bufferpool for their locks. */iboolbuf_LRU_buf_pool_running_out(void)/*==============================*/				/* out: TRUE if less than 25 % of buffer pool				left */{	ibool	ret	= FALSE;	mutex_enter(&(buf_pool->mutex));	if (!recv_recovery_on && UT_LIST_GET_LEN(buf_pool->free)	   + UT_LIST_GET_LEN(buf_pool->LRU) < buf_pool->max_size / 4) {				ret = TRUE;	}	mutex_exit(&(buf_pool->mutex));	return(ret);}/**********************************************************************Returns a free block from buf_pool. The block is taken off the free list.If it is empty, blocks are moved from the end of the LRU list to the freelist. */buf_block_t*buf_LRU_get_free_block(void)/*========================*/				/* out: the free control block; also if AWE is				used, it is guaranteed that the block has its				page mapped to a frame when we return */{	buf_block_t*	block		= NULL;	ibool		freed;	ulint		n_iterations	= 1;	ibool		mon_value_was   = FALSE;	ibool		started_monitor	= FALSE;loop:	mutex_enter(&(buf_pool->mutex));	if (!recv_recovery_on && UT_LIST_GET_LEN(buf_pool->free)	   + UT_LIST_GET_LEN(buf_pool->LRU) < buf_pool->max_size / 20) {	   	ut_print_timestamp(stderr);	   	fprintf(stderr,"  InnoDB: ERROR: over 95 percent of the buffer pool is occupied by\n""InnoDB: lock heaps or the adaptive hash index! Check that your\n""InnoDB: transactions do not set too many row locks.\n""InnoDB: Your buffer pool size is %lu MB. Maybe you should make\n""InnoDB: the buffer pool bigger?\n""InnoDB: We intentionally generate a seg fault to print a stack trace\n""InnoDB: on Linux!\n",		(ulong)(buf_pool->curr_size / (1024 * 1024 / UNIV_PAGE_SIZE)));		ut_error;	   	} else if (!recv_recovery_on && UT_LIST_GET_LEN(buf_pool->free)	   + UT_LIST_GET_LEN(buf_pool->LRU) < buf_pool->max_size / 3) {		if (!buf_lru_switched_on_innodb_mon) {	   		/* Over 67 % of the buffer pool is occupied by lock			heaps or the adaptive hash index. This may be a memory			leak! */	   		ut_print_timestamp(stderr);	   		fprintf(stderr,"  InnoDB: WARNING: over 67 percent of the buffer pool is occupied by\n""InnoDB: lock heaps or the adaptive hash index! Check that your\n""InnoDB: transactions do not set too many row locks.\n""InnoDB: Your buffer pool size is %lu MB. Maybe you should make\n""InnoDB: the buffer pool bigger?\n""InnoDB: Starting the InnoDB Monitor to print diagnostics, including\n""InnoDB: lock heap and hash index sizes.\n",			(ulong) (buf_pool->curr_size / (1024 * 1024 / UNIV_PAGE_SIZE)));			buf_lru_switched_on_innodb_mon = TRUE;			srv_print_innodb_monitor = TRUE;			os_event_set(srv_lock_timeout_thread_event);		}	} else if (buf_lru_switched_on_innodb_mon) {		/* Switch off the InnoDB Monitor; this is a simple way		to stop the monitor if the situation becomes less urgent,		but may also surprise users if the user also switched on the		monitor! */		buf_lru_switched_on_innodb_mon = FALSE;		srv_print_innodb_monitor = FALSE;	}		/* If there is a block in the free list, take it */	if (UT_LIST_GET_LEN(buf_pool->free) > 0) {				block = UT_LIST_GET_FIRST(buf_pool->free);		ut_a(block->in_free_list);		UT_LIST_REMOVE(free, buf_pool->free, block);		block->in_free_list = FALSE;		ut_a(block->state != BUF_BLOCK_FILE_PAGE);	        ut_a(!block->in_LRU_list);		if (srv_use_awe) {			if (block->frame) {				/* Remove from the list of mapped pages */						UT_LIST_REMOVE(awe_LRU_free_mapped,					buf_pool->awe_LRU_free_mapped, block);			} else {				/* We map the page to a frame; second param				FALSE below because we do not want it to be				added to the awe_LRU_free_mapped list */				buf_awe_map_page_to_frame(block, FALSE);			}		}				block->state = BUF_BLOCK_READY_FOR_USE;		mutex_exit(&(buf_pool->mutex));		if (started_monitor) {			srv_print_innodb_monitor = mon_value_was;		}			return(block);	}		/* If no block was in the free list, search from the end of the LRU	list and try to free a block there */	mutex_exit(&(buf_pool->mutex));	freed = buf_LRU_search_and_free_block(n_iterations);	if (freed > 0) {		goto loop;	}	if (n_iterations > 30) {		ut_print_timestamp(stderr);		fprintf(stderr,		"InnoDB: Warning: difficult to find free blocks from\n"		"InnoDB: the buffer pool (%lu search iterations)! Consider\n"		"InnoDB: increasing the buffer pool size.\n"		"InnoDB: It is also possible that in your Unix version\n"		"InnoDB: fsync is very slow, or completely frozen inside\n"		"InnoDB: the OS kernel. Then upgrading to a newer version\n"		"InnoDB: of your operating system may help. Look at the\n"		"InnoDB: number of fsyncs in diagnostic info below.\n"		"InnoDB: Pending flushes (fsync) log: %lu; buffer pool: %lu\n"		"InnoDB: %lu OS file reads, %lu OS file writes, %lu OS fsyncs\n"		"InnoDB: Starting InnoDB Monitor to print further\n"		"InnoDB: diagnostics to the standard output.\n",			(ulong) n_iterations,			(ulong) fil_n_pending_log_flushes,			(ulong) fil_n_pending_tablespace_flushes,			(ulong) os_n_file_reads, (ulong) os_n_file_writes,                        (ulong) os_n_fsyncs);		mon_value_was = srv_print_innodb_monitor;		started_monitor = TRUE;		srv_print_innodb_monitor = TRUE;		os_event_set(srv_lock_timeout_thread_event);	}	/* No free block was found: try to flush the LRU list */	buf_flush_free_margin();        ++srv_buf_pool_wait_free;	os_aio_simulated_wake_handler_threads();	mutex_enter(&(buf_pool->mutex));	if (buf_pool->LRU_flush_ended > 0) {		/* We have written pages in an LRU flush. To make the insert		buffer more efficient, we try to move these pages to the free		list. */		mutex_exit(&(buf_pool->mutex));		buf_LRU_try_free_flushed_blocks();	} else {		mutex_exit(&(buf_pool->mutex));	}	if (n_iterations > 10) {		os_thread_sleep(500000);	}	n_iterations++;	goto loop;	}	/***********************************************************************Moves the LRU_old pointer so that the length of the old blocks listis inside the allowed limits. */UNIV_INLINEvoidbuf_LRU_old_adjust_len(void)/*========================*/{	ulint	old_len;	ulint	new_len;	ut_a(buf_pool->LRU_old);#ifdef UNIV_SYNC_DEBUG	ut_ad(mutex_own(&(buf_pool->mutex)));#endif /* UNIV_SYNC_DEBUG */	ut_ad(3 * (BUF_LRU_OLD_MIN_LEN / 8) > BUF_LRU_OLD_TOLERANCE + 5);	for (;;) {		old_len = buf_pool->LRU_old_len;		new_len = 3 * (UT_LIST_GET_LEN(buf_pool->LRU) / 8);		ut_a(buf_pool->LRU_old->in_LRU_list);		/* Update the LRU_old pointer if necessary */			if (old_len < new_len - BUF_LRU_OLD_TOLERANCE) {					buf_pool->LRU_old = UT_LIST_GET_PREV(LRU,							buf_pool->LRU_old);			(buf_pool->LRU_old)->old = TRUE;			buf_pool->LRU_old_len++;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -