日期:2014-05-16  浏览次数:20487 次

mysql5.5.25 源码阅读---innodb 主线程工作流程
srv0src.cc

srv_master_thread(void* arg __attribute__((unused)))
{
      ## 省略各种变量声明、赋值
loop:
/*****************************************************************/
/* ---- When there is database activity by users, we cycle in this
loop */

srv_main_thread_op_info = "reserving kernel mutex";

        ## 获取bp的统计信息
buf_get_total_stat(&buf_stat);

n_ios_very_old = log_sys->n_log_ios + buf_stat.n_pages_read
+ buf_stat.n_pages_written;
mutex_enter(&kernel_mutex);

/* Store the user activity counter at the start of this loop */
old_activity_count = srv_activity_count;

mutex_exit(&kernel_mutex);

if (srv_force_recovery >= SRV_FORCE_NO_BACKGROUND) {

goto suspend_thread;
}

/* ---- We run the following loop approximately once per second
when there is database activity */

srv_last_log_flush_time = time(NULL);

/* Sleep for 1 second on entrying the for loop below the first time. */
next_itr_time = ut_time_ms() + 1000;

每1秒进行的工作
        for (i = 0; i < 10; i++) {
ulint cur_time = ut_time_ms();

/* ALTER TABLE in MySQL requires on Unix that the table handler
can drop tables lazily after there no longer are SELECT
queries to them. */
srv_main_thread_op_info = "doing background drop tables";

                ## ALTER TABLE产生的drop tables lazily信息
row_drop_tables_for_mysql_in_background();

srv_main_thread_op_info = "";

if (srv_fast_shutdown && srv_shutdown_state > 0) {
goto background_loop;
}

                ## 获取bp的统计信息
buf_get_total_stat(&buf_stat);

n_ios_old = log_sys->n_log_ios + buf_stat.n_pages_read
+ buf_stat.n_pages_written;

srv_main_thread_op_info = "sleeping";
srv_main_1_second_loops++;

                ## 可能sleep 1秒
if (next_itr_time > cur_time
    && srv_shutdown_state == SRV_SHUTDOWN_NONE) {

/* Get sleep interval in micro seconds. We use
ut_min() to avoid long sleep in case of
wrap around. */
os_thread_sleep(ut_min(1000000,
(next_itr_time - cur_time)
* 1000));
srv_main_sleeps++;
}

/* Each iteration should happen at 1 second interval. */
next_itr_time = ut_time_ms() + 1000;

                ## flush log buffer
srv_sync_log_buffer_in_background();

                ## 检查是否要进行flush log buffer 或者产生一个新的checkpoint
srv_main_thread_op_info = "making checkpoint";
                log_free_check();

/* If i/os during one second sleep were less than 5% of
capacity, we assume that there is free disk i/o capacity
available, and it makes sense to do an insert buffer merge. */

                ## 获取bp的统计信息
buf_get_total_stat(&buf_s