136
137
on duplicate key checking and foreign key checking */
137
138
UNIV_INTERN ibool srv_locks_unsafe_for_binlog = FALSE;
139
/* If this flag is TRUE, then we will use the native aio of the
140
OS (provided we compiled Innobase with it in), otherwise we will
141
use simulated aio we build below with threads.
142
Currently we support native aio on windows and linux */
143
UNIV_INTERN my_bool srv_use_native_aio = TRUE;
146
/* Windows native condition variables. We use runtime loading / function
147
pointers, because they are not available on Windows Server 2003 and
150
We use condition for events on Windows if possible, even if os_event
151
resembles Windows kernel event object well API-wise. The reason is
152
performance, kernel objects are heavyweights and WaitForSingleObject() is a
153
performance killer causing calling thread to context switch. Besides, Innodb
154
is preallocating large number (often millions) of os_events. With kernel event
155
objects it takes a big chunk out of non-paged pool, which is better suited
156
for tasks like IO than for storing idle event objects. */
157
UNIV_INTERN ibool srv_use_native_conditions = FALSE;
160
140
UNIV_INTERN ulint srv_n_data_files = 0;
161
141
UNIV_INTERN char** srv_data_file_names = NULL;
162
142
/* size in database pages */
488
459
/* Mutex for locking srv_monitor_file */
489
460
UNIV_INTERN mutex_t srv_monitor_file_mutex;
491
#ifdef UNIV_PFS_MUTEX
492
/* Key to register kernel_mutex with performance schema */
493
UNIV_INTERN mysql_pfs_key_t kernel_mutex_key;
494
/* Key to register srv_innodb_monitor_mutex with performance schema */
495
UNIV_INTERN mysql_pfs_key_t srv_innodb_monitor_mutex_key;
496
/* Key to register srv_monitor_file_mutex with performance schema */
497
UNIV_INTERN mysql_pfs_key_t srv_monitor_file_mutex_key;
498
/* Key to register srv_dict_tmpfile_mutex with performance schema */
499
UNIV_INTERN mysql_pfs_key_t srv_dict_tmpfile_mutex_key;
500
/* Key to register the mutex with performance schema */
501
UNIV_INTERN mysql_pfs_key_t srv_misc_tmpfile_mutex_key;
502
#endif /* UNIV_PFS_MUTEX */
504
461
/* Temporary file for innodb monitor output */
505
462
UNIV_INTERN FILE* srv_monitor_file;
506
463
/* Mutex for locking srv_dict_tmpfile.
1016
973
srv_sys = mem_alloc(sizeof(srv_sys_t));
1018
975
kernel_mutex_temp = mem_alloc(sizeof(mutex_t));
1019
mutex_create(kernel_mutex_key, &kernel_mutex, SYNC_KERNEL);
976
mutex_create(&kernel_mutex, SYNC_KERNEL);
1021
mutex_create(srv_innodb_monitor_mutex_key,
1022
&srv_innodb_monitor_mutex, SYNC_NO_ORDER_CHECK);
978
mutex_create(&srv_innodb_monitor_mutex, SYNC_NO_ORDER_CHECK);
1024
980
srv_sys->threads = mem_alloc(OS_THREAD_MAX_N * sizeof(srv_slot_t));
1620
1576
row_mysql_unfreeze_data_dictionary(trx);
1622
1578
case RW_X_LATCH:
1623
/* There should never be a lock wait when the
1624
dictionary latch is reserved in X mode. Dictionary
1625
transactions should only acquire locks on dictionary
1626
tables, not other tables. All access to dictionary
1627
tables should be covered by dictionary
1629
ut_print_timestamp(stderr);
1630
fputs(" InnoDB: Error: dict X latch held in "
1631
"srv_suspend_mysql_thread\n", stderr);
1632
/* This should never occur. This incorrect handling
1633
was added in the early development of
1634
ha_innobase::add_index() in InnoDB Plugin 1.0. */
1635
1579
/* Release fast index creation latch */
1636
1580
row_mysql_unlock_data_dictionary(trx);
2003
1933
export_vars.innodb_data_reads = os_n_file_reads;
2004
1934
export_vars.innodb_data_writes = os_n_file_writes;
2005
1935
export_vars.innodb_data_written = srv_data_written;
2006
export_vars.innodb_buffer_pool_read_requests = stat.n_page_gets;
1936
export_vars.innodb_buffer_pool_read_requests = buf_pool->stat.n_page_gets;
2007
1937
export_vars.innodb_buffer_pool_write_requests
2008
1938
= srv_buf_pool_write_requests;
2009
1939
export_vars.innodb_buffer_pool_wait_free = srv_buf_pool_wait_free;
2010
1940
export_vars.innodb_buffer_pool_pages_flushed = srv_buf_pool_flushed;
2011
1941
export_vars.innodb_buffer_pool_reads = srv_buf_pool_reads;
2012
1942
export_vars.innodb_buffer_pool_read_ahead
2013
= stat.n_ra_pages_read;
1943
= buf_pool->stat.n_ra_pages_read;
2014
1944
export_vars.innodb_buffer_pool_read_ahead_evicted
2015
= stat.n_ra_pages_evicted;
2016
export_vars.innodb_buffer_pool_pages_data = LRU_len;
2017
export_vars.innodb_buffer_pool_pages_dirty = flush_list_len;
2018
export_vars.innodb_buffer_pool_pages_free = free_len;
1945
= buf_pool->stat.n_ra_pages_evicted;
1946
export_vars.innodb_buffer_pool_pages_data
1947
= UT_LIST_GET_LEN(buf_pool->LRU);
1948
export_vars.innodb_buffer_pool_pages_dirty
1949
= UT_LIST_GET_LEN(buf_pool->flush_list);
1950
export_vars.innodb_buffer_pool_pages_free
1951
= UT_LIST_GET_LEN(buf_pool->free);
2019
1952
#ifdef UNIV_DEBUG
2020
1953
export_vars.innodb_buffer_pool_pages_latched
2021
1954
= buf_get_latched_pages_number();
2022
1955
#endif /* UNIV_DEBUG */
2023
export_vars.innodb_buffer_pool_pages_total = buf_pool_get_n_pages();
1956
export_vars.innodb_buffer_pool_pages_total = buf_pool->curr_size;
2025
export_vars.innodb_buffer_pool_pages_misc
2026
= buf_pool_get_n_pages() - LRU_len - free_len;
1958
export_vars.innodb_buffer_pool_pages_misc = buf_pool->curr_size
1959
- UT_LIST_GET_LEN(buf_pool->LRU)
1960
- UT_LIST_GET_LEN(buf_pool->free);
2027
1961
#ifdef HAVE_ATOMIC_BUILTINS
2028
1962
export_vars.innodb_have_atomic_builtins = 1;
2039
1973
export_vars.innodb_log_writes = srv_log_writes;
2040
1974
export_vars.innodb_dblwr_pages_written = srv_dblwr_pages_written;
2041
1975
export_vars.innodb_dblwr_writes = srv_dblwr_writes;
2042
export_vars.innodb_pages_created = stat.n_pages_created;
2043
export_vars.innodb_pages_read = stat.n_pages_read;
2044
export_vars.innodb_pages_written = stat.n_pages_written;
1976
export_vars.innodb_pages_created = buf_pool->stat.n_pages_created;
1977
export_vars.innodb_pages_read = buf_pool->stat.n_pages_read;
1978
export_vars.innodb_pages_written = buf_pool->stat.n_pages_written;
2045
1979
export_vars.innodb_row_lock_waits = srv_n_lock_wait_count;
2046
1980
export_vars.innodb_row_lock_current_waits
2047
1981
= srv_n_lock_wait_current_count;
2437
2356
OS_THREAD_DUMMY_RETURN;
2440
/**********************************************************************//**
2441
Check whether any background thread is active.
2442
@return FALSE if all are are suspended or have exited. */
2445
srv_is_any_background_thread_active(void)
2446
/*=====================================*/
2451
mutex_enter(&kernel_mutex);
2453
for (i = SRV_COM; i <= SRV_MASTER; ++i) {
2454
if (srv_n_threads_active[i] != 0) {
2460
mutex_exit(&kernel_mutex);
2465
2359
/*******************************************************************//**
2466
2360
Tells the InnoDB server that there has been activity in the database
2467
2361
and wakes up the master thread if it is suspended (not sleeping). Used
2468
2362
in the MySQL interface. Note that there is a small chance that the master
2469
thread stays suspended (we do not protect our operation with the
2470
srv_sys_t->mutex, for performance reasons). */
2363
thread stays suspended (we do not protect our operation with the kernel
2364
mutex, for performace reasons). */
2473
2367
srv_active_wake_master_thread(void)
2488
2382
/*******************************************************************//**
2489
Tells the purge thread that there has been activity in the database
2490
and wakes up the purge thread if it is suspended (not sleeping). Note
2491
that there is a small chance that the purge thread stays suspended
2492
(we do not protect our operation with the kernel mutex, for
2493
performace reasons). */
2496
srv_wake_purge_thread_if_not_active(void)
2497
/*=====================================*/
2499
ut_ad(!mutex_own(&kernel_mutex));
2501
if (srv_n_purge_threads > 0
2502
&& srv_n_threads_active[SRV_WORKER] == 0) {
2504
mutex_enter(&kernel_mutex);
2506
srv_release_threads(SRV_WORKER, 1);
2508
mutex_exit(&kernel_mutex);
2512
/*******************************************************************//**
2513
2383
Wakes up the master thread if it is suspended or being suspended. */
2525
2395
mutex_exit(&kernel_mutex);
2528
/*******************************************************************//**
2529
Wakes up the purge thread if it's not already awake. */
2532
srv_wake_purge_thread(void)
2533
/*=======================*/
2535
ut_ad(!mutex_own(&kernel_mutex));
2537
if (srv_n_purge_threads > 0) {
2539
mutex_enter(&kernel_mutex);
2541
srv_release_threads(SRV_WORKER, 1);
2543
mutex_exit(&kernel_mutex);
2547
2398
/**********************************************************************
2548
2399
The master thread is tasked to ensure that flush of log file happens
2549
2400
once every second in the background. This is to ensure that not more
2567
/********************************************************************//**
2568
Do a full purge, reconfigure the purge sub-system if a dynamic
2569
change is detected. */
2572
srv_master_do_purge(void)
2573
/*=====================*/
2575
ulint n_pages_purged;
2577
ut_ad(!mutex_own(&kernel_mutex));
2579
ut_a(srv_n_purge_threads == 0);
2582
/* Check for shutdown and change in purge config. */
2583
if (srv_fast_shutdown && srv_shutdown_state > 0) {
2584
/* Nothing to purge. */
2587
n_pages_purged = trx_purge(srv_purge_batch_size);
2590
srv_sync_log_buffer_in_background();
2592
} while (n_pages_purged > 0);
2595
2418
/*********************************************************************//**
2596
2419
The master thread controlling the server.
2597
2420
@return a dummy parameter */
2664
2480
when there is database activity */
2666
2482
srv_last_log_flush_time = time(NULL);
2668
/* Sleep for 1 second on entrying the for loop below the first time. */
2669
next_itr_time = ut_time_ms() + 1000;
2671
2485
for (i = 0; i < 10; i++) {
2672
ulint cur_time = ut_time_ms();
2674
buf_get_total_stat(&buf_stat);
2676
n_ios_old = log_sys->n_log_ios + buf_stat.n_pages_read
2677
+ buf_stat.n_pages_written;
2486
n_ios_old = log_sys->n_log_ios + buf_pool->stat.n_pages_read
2487
+ buf_pool->stat.n_pages_written;
2679
2488
srv_main_thread_op_info = "sleeping";
2680
2489
srv_main_1_second_loops++;
2682
if (next_itr_time > cur_time) {
2684
/* Get sleep interval in micro seconds. We use
2685
ut_min() to avoid long sleep in case of
2687
os_thread_sleep(ut_min(1000000,
2688
(next_itr_time - cur_time)
2493
os_thread_sleep(1000000);
2690
2494
srv_main_sleeps++;
2693
/* Each iteration should happen at 1 second interval. */
2694
next_itr_time = ut_time_ms() + 1000;
2696
2499
/* ALTER TABLE in MySQL requires on Unix that the table handler
2697
2500
can drop tables lazily after there no longer are SELECT
2715
2518
log_free_check();
2717
2520
/* If i/os during one second sleep were less than 5% of
2718
capacity, we assume that there is free disk i/o capacity
2719
available, and it makes sense to do an insert buffer merge. */
2521
capacity, we assume that there is free disk i/o capacity
2522
available, and it makes sense to do an insert buffer merge. */
2721
buf_get_total_stat(&buf_stat);
2722
2524
n_pend_ios = buf_get_n_pending_ios()
2723
2525
+ log_sys->n_pending_writes;
2724
n_ios = log_sys->n_log_ios + buf_stat.n_pages_read
2725
+ buf_stat.n_pages_written;
2526
n_ios = log_sys->n_log_ios + buf_pool->stat.n_pages_read
2527
+ buf_pool->stat.n_pages_written;
2726
2528
if (n_pend_ios < SRV_PEND_IO_THRESHOLD
2727
2529
&& (n_ios - n_ios_old < SRV_RECENT_IO_ACTIVITY)) {
2728
2530
srv_main_thread_op_info = "doing insert buffer merge";
2741
2543
srv_main_thread_op_info =
2742
2544
"flushing buffer pool pages";
2743
n_pages_flushed = buf_flush_list(
2744
PCT_IO(100), IB_ULONGLONG_MAX);
2545
n_pages_flushed = buf_flush_batch(BUF_FLUSH_LIST,
2549
/* If we had to do the flush, it may have taken
2550
even more than 1 second, and also, there may be more
2551
to flush. Do not sleep 1 second during the next
2552
iteration of this loop. */
2746
2555
} else if (srv_adaptive_flushing) {
2748
2557
/* Try to keep the rate of flushing of dirty
2787
2601
loop above requests writes for that case. The writes done here
2788
2602
are not required, and may be disabled. */
2790
buf_get_total_stat(&buf_stat);
2791
2604
n_pend_ios = buf_get_n_pending_ios() + log_sys->n_pending_writes;
2792
n_ios = log_sys->n_log_ios + buf_stat.n_pages_read
2793
+ buf_stat.n_pages_written;
2605
n_ios = log_sys->n_log_ios + buf_pool->stat.n_pages_read
2606
+ buf_pool->stat.n_pages_written;
2795
2608
srv_main_10_second_loops++;
2796
2609
if (n_pend_ios < SRV_PEND_IO_THRESHOLD
2797
2610
&& (n_ios - n_ios_very_old < SRV_PAST_IO_ACTIVITY)) {
2799
2612
srv_main_thread_op_info = "flushing buffer pool pages";
2800
buf_flush_list(PCT_IO(100), IB_ULONGLONG_MAX);
2613
buf_flush_batch(BUF_FLUSH_LIST, PCT_IO(100),
2802
2616
/* Flush logs if needed */
2803
2617
srv_sync_log_buffer_in_background();
2812
2626
/* Flush logs if needed */
2813
2627
srv_sync_log_buffer_in_background();
2815
if (srv_n_purge_threads == 0) {
2816
srv_main_thread_op_info = "master purging";
2818
srv_master_do_purge();
2629
/* We run a full purge every 10 seconds, even if the server
2820
2633
if (srv_fast_shutdown && srv_shutdown_state > 0) {
2822
2635
goto background_loop;
2638
srv_main_thread_op_info = "purging";
2639
n_pages_purged = trx_purge();
2641
/* Flush logs if needed */
2642
srv_sync_log_buffer_in_background();
2644
} while (n_pages_purged);
2826
2646
srv_main_thread_op_info = "flushing buffer pool pages";
2833
2653
(> 70 %), we assume we can afford reserving the disk(s) for
2834
2654
the time it requires to flush 100 pages */
2836
n_pages_flushed = buf_flush_list(
2837
PCT_IO(100), IB_ULONGLONG_MAX);
2656
n_pages_flushed = buf_flush_batch(BUF_FLUSH_LIST,
2839
2660
/* Otherwise, we only flush a small number of pages so that
2840
2661
we do not unnecessarily use much disk i/o capacity from
2843
n_pages_flushed = buf_flush_list(
2844
PCT_IO(10), IB_ULONGLONG_MAX);
2664
n_pages_flushed = buf_flush_batch(BUF_FLUSH_LIST,
2847
2669
srv_main_thread_op_info = "making checkpoint";
2888
2710
os_thread_sleep(100000);
2891
if (srv_n_purge_threads == 0) {
2892
srv_main_thread_op_info = "master purging";
2894
srv_master_do_purge();
2713
srv_main_thread_op_info = "purging";
2715
/* Run a full purge */
2717
if (srv_fast_shutdown && srv_shutdown_state > 0) {
2722
srv_main_thread_op_info = "purging";
2723
n_pages_purged = trx_purge();
2725
/* Flush logs if needed */
2726
srv_sync_log_buffer_in_background();
2728
} while (n_pages_purged);
2897
2730
srv_main_thread_op_info = "reserving kernel mutex";
3048
2881
OS_THREAD_DUMMY_RETURN; /* Not reached, avoid compiler warning */
3052
/*********************************************************************//**
3053
Asynchronous purge thread.
3054
@return a dummy parameter */
3059
void* arg __attribute__((unused))) /*!< in: a dummy parameter
3060
required by os_thread_create */
3063
ulint slot_no = ULINT_UNDEFINED;
3064
ulint n_total_purged = ULINT_UNDEFINED;
3066
ut_a(srv_n_purge_threads == 1);
3068
#ifdef UNIV_DEBUG_THREAD_CREATION
3069
fprintf(stderr, "InnoDB: Purge thread running, id %lu\n",
3070
os_thread_pf(os_thread_get_curr_id()));
3071
#endif /* UNIV_DEBUG_THREAD_CREATION */
3073
mutex_enter(&kernel_mutex);
3075
slot_no = srv_table_reserve_slot(SRV_WORKER);
3077
slot = srv_table_get_nth_slot(slot_no);
3079
++srv_n_threads_active[SRV_WORKER];
3081
mutex_exit(&kernel_mutex);
3083
while (srv_shutdown_state != SRV_SHUTDOWN_EXIT_THREADS) {
3085
ulint n_pages_purged;
3087
/* If there are very few records to purge or the last
3088
purge didn't purge any records then wait for activity.
3089
We peek at the history len without holding any mutex
3090
because in the worst case we will end up waiting for
3091
the next purge event. */
3092
if (trx_sys->rseg_history_len < srv_purge_batch_size
3093
|| n_total_purged == 0) {
3097
mutex_enter(&kernel_mutex);
3099
event = srv_suspend_thread();
3101
mutex_exit(&kernel_mutex);
3103
os_event_wait(event);
3106
/* Check for shutdown and whether we should do purge at all. */
3107
if (srv_force_recovery >= SRV_FORCE_NO_BACKGROUND
3108
|| srv_shutdown_state != 0
3109
|| srv_fast_shutdown) {
3116
/* Purge until there are no more records to purge and there is
3117
no change in configuration or server state. */
3119
n_pages_purged = trx_purge(srv_purge_batch_size);
3121
n_total_purged += n_pages_purged;
3123
} while (n_pages_purged > 0 && !srv_fast_shutdown);
3125
srv_sync_log_buffer_in_background();
3128
mutex_enter(&kernel_mutex);
3130
ut_ad(srv_table_get_nth_slot(slot_no) == slot);
3132
/* Decrement the active count. */
3133
srv_suspend_thread();
3135
slot->in_use = FALSE;
3137
/* Free the thread local memory. */
3138
thr_local_free(os_thread_get_curr_id());
3140
mutex_exit(&kernel_mutex);
3142
#ifdef UNIV_DEBUG_THREAD_CREATION
3143
fprintf(stderr, "InnoDB: Purge thread exiting, id %lu\n",
3144
os_thread_pf(os_thread_get_curr_id()));
3145
#endif /* UNIV_DEBUG_THREAD_CREATION */
3147
/* We count the number of threads in os_thread_exit(). A created
3148
thread should always use that to exit and not use return() to exit. */
3149
os_thread_exit(NULL);
3151
OS_THREAD_DUMMY_RETURN; /* Not reached, avoid compiler warning */
3154
/**********************************************************************//**
3155
Enqueues a task to server task queue and releases a worker thread, if there
3156
is a suspended one. */
3159
srv_que_task_enqueue_low(
3160
/*=====================*/
3161
que_thr_t* thr) /*!< in: query thread */
3165
mutex_enter(&kernel_mutex);
3167
UT_LIST_ADD_LAST(queue, srv_sys->tasks, thr);
3169
srv_release_threads(SRV_WORKER, 1);
3171
mutex_exit(&kernel_mutex);