innodb中master线程的调度的算法改进(mysql 5.6.26)
生活随笔
收集整理的這篇文章主要介紹了
innodb中master线程的调度的算法改进(mysql 5.6.26)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
innodb中master線程的調度的算法改進(mysql 5.6.26)作者:周琳QQ:715169549源碼交流群:1963809051.master線程的調度:/*********************************************************************//**The master thread controlling the server.
os_thread_ret_t DECLARE_THREAD(srv_master_thread)
如下是核心代碼:ulint old_activity_count = srv_get_activity_count();if (srv_force_recovery >= SRV_FORCE_NO_BACKGROUND) {goto suspend_thread;}while (srv_shutdown_state == SRV_SHUTDOWN_NONE) {/*在linux下就是1000000/1000000,在針對這個整除的值%1000000;*這個就是os_thread_sleep( microseconds);的計算方式srv_master_sleep(); //MONITOR_INC(MONITOR_MASTER_THREAD_SLEEP);/*很顯然并沒有按照1秒和10秒的算法來,而是通過當前服務器活動的數量來決定。if (srv_check_activity(old_activity_count)) { //srv_check_activity(old_activity_count)返回:return(srv_sys->activity_count != old_activity_count);old_activity_count = srv_get_activity_count(); //srv_get_activity_count返回:return(srv_sys->activity_count);srv_master_do_active_tasks();} else {srv_master_do_idle_tasks();}}while (srv_master_do_shutdown_tasks(&last_print_time)) {/* Shouldn't loop here in case of very fast shutdown */ut_ad(srv_fast_shutdown < 2);}}srv_master_do_active_tasks做的事情://#define PCT_IO(p) ((ulong) (srv_io_capacity * ((double) (p) / 100.0)))//# define SRV_MASTER_CHECKPOINT_INTERVAL (7)1.log_free_check,檢查是否有空閑的log buffer。2.ibuf_contract_in_background(0, FALSE),合并PCT_IO(5)個插入緩沖3.srv_sync_log_buffer_in_background,后臺同步刷新log buffer日志到log file4.cur_time % SRV_MASTER_CHECKPOINT_INTERVAL == 0,就做log_checkpoint(TRUE, FALSE),而log_checkpoint僅僅是檢查bp中的lsn以及把lsn的值寫入到redo file中。真正做檢查點刷新臟頁到磁盤的是log_make_checkpoint_at函數。srv_master_do_idle_tasks做的事情:1.log_free_check,檢查是否有空閑的log buffer。2.ibuf_contract_in_background(0, TRUE),合并PCT_IO(100)個插入緩沖.3.srv_sync_log_buffer_in_background,刷新log buffer到磁盤。4.cur_time % SRV_MASTER_CHECKPOINT_INTERVAL == 0,就做log_checkpoint(TRUE, FALSE),而log_checkpoint僅僅是檢查bp中的lsn以及把lsn的值寫入到redo file中。真正做檢查點刷新臟頁到磁盤的是log_make_checkpoint_at函數。如下是srv_sys_t結構:元素包含一個線程表和一個task queue。/** The server system struct */struct srv_sys_t{ib_mutex_t tasks_mutex; /*!< variable protecting thetasks queue */UT_LIST_BASE_NODE_T(que_thr_t)tasks; /*!< task queue */ib_mutex_t mutex; /*!< variable protecting thefields below. */ulint n_sys_threads; /*!< size of the sys_threadsarray */srv_slot_t* sys_threads; /*!< server thread table */ulint n_threads_active[SRV_MASTER + 1];/*!< number of threads activein a thread class */srv_stats_t::ulint_ctr_1_tactivity_count; /*!< For tracking serveractivity */};接下來介紹下activity_count,這個函數是由srv_inc_activity_count(srv_sys->activity_count.inc();)進行自增的,這個函數會在哪些動作中調用呢?我們看下調用該函數的函數流程:1.row_undo_step:在表中回滾一行的操作->srv_inc_activity_count()2.srv_active_wake_master_thread:如果需要主線程處理,切主線程處于掛起狀態就喚醒主線程。->srv_inc_activity_count()3.srv_wake_master_thread:如果主線程掛起或者正在被掛起,喚醒主線程。->srv_inc_activity_count()上述函數還被很多函數調用,activity_count可以理解為mysql運行中操作的計數器,每個操作都會累加。所以master線程依據時間間隔的activity_count計數器來判斷執行srv_master_do_active_tasks還是srv_master_do_idle_tasks??梢钥闯鲞@個計數器如果在1秒以后這個值如果不發生變化就切換到srv_master_do_idle_tasks。2.page cleaner線程:(buf0flu.c中)核心代碼: ulint next_loop_time = ut_time_ms() + 1000;while (srv_shutdown_state == SRV_SHUTDOWN_NONE) {//服務器沒有關閉的狀態下/* The page_cleaner skips sleep if the server isidle and there are no pending IOs in the buffer pooland there is work to do. */if (srv_check_activity(last_activity) //檢查當前服務器的活動次數|| buf_get_n_pending_read_ios()|| n_flushed == 0) {page_cleaner_sleep_if_needed(next_loop_time); //休眠min(1000000,(next_loop_time-當前時間)*1000))
}next_loop_time = ut_time_ms() + 1000; //重新計算當前next_loop_time值if (srv_check_activity(last_activity)) { //檢查當前服務器活動數量last_activity = srv_get_activity_count();/* Flush pages from end of LRU if required */n_flushed = buf_flush_LRU_tail(); /* Flush pages from flush_list if required */n_flushed += page_cleaner_flush_pages_if_needed();} else {n_flushed = page_cleaner_do_flush_batch(PCT_IO(100), //刷新srv_io_capacity個臟頁到磁盤
LSN_MAX);}}if (srv_fast_shutdown == 2) {/* In very fast shutdown we simulate a crash ofbuffer pool. We are not required to do any flushing */goto thread_exit;}do {n_flushed = page_cleaner_do_flush_batch(PCT_IO(100), LSN_MAX); //刷新srv_io_capacity個臟頁到磁盤/* We sleep only if there are no pages to flush */if (n_flushed == 0) {os_thread_sleep(100000); //休眠1秒
}} while (srv_shutdown_state == SRV_SHUTDOWN_CLEANUP);do {success = buf_flush_list(PCT_IO(100), LSN_MAX, &n_flushed);buf_flush_wait_batch_end(NULL, BUF_FLUSH_LIST);} while (!success || n_flushed > 0);
?
轉載于:https://www.cnblogs.com/innobase/p/4726663.html
總結
以上是生活随笔為你收集整理的innodb中master线程的调度的算法改进(mysql 5.6.26)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HDU 2546(01背包)
- 下一篇: Java中的Scanner类和Strin