1 # name : innodb_thread_concurrency_timer_based.patch
2 # introduced : 11 or before
3 # maintainer : Yasufumi
6 # Any small change to this file in the main branch
7 # should be done or reviewed by the maintainer!
8 --- a/storage/innobase/handler/ha_innodb.cc
9 +++ b/storage/innobase/handler/ha_innodb.cc
11 static ulong innobase_write_io_threads;
12 static long innobase_buffer_pool_instances = 1;
14 +static my_bool innobase_thread_concurrency_timer_based;
15 static long long innobase_buffer_pool_size, innobase_log_file_size;
17 /** Percentage of the buffer pool to reserve for 'old' blocks.
19 srv_n_log_files = (ulint) innobase_log_files_in_group;
20 srv_log_file_size = (ulint) innobase_log_file_size;
22 + srv_thread_concurrency_timer_based =
23 + (ibool) innobase_thread_concurrency_timer_based;
25 #ifdef UNIV_LOG_ARCHIVE
26 srv_log_archive_on = (ulint) innobase_log_archive;
27 #endif /* UNIV_LOG_ARCHIVE */
28 @@ -11601,6 +11605,12 @@
29 "Maximum delay between polling for a spin lock (6 by default)",
30 NULL, NULL, 6L, 0L, ~0L, 0);
32 +static MYSQL_SYSVAR_BOOL(thread_concurrency_timer_based,
33 + innobase_thread_concurrency_timer_based,
34 + PLUGIN_VAR_NOCMDARG | PLUGIN_VAR_READONLY,
35 + "Use InnoDB timer based concurrency throttling. ",
38 static MYSQL_SYSVAR_ULONG(thread_concurrency, srv_thread_concurrency,
40 "Helps in performance tuning in heavily concurrent environments. Sets the maximum number of threads allowed inside InnoDB. Value 0 will disable the thread throttling.",
41 @@ -11859,6 +11869,7 @@
42 MYSQL_SYSVAR(spin_wait_delay),
43 MYSQL_SYSVAR(table_locks),
44 MYSQL_SYSVAR(thread_concurrency),
45 + MYSQL_SYSVAR(thread_concurrency_timer_based),
46 MYSQL_SYSVAR(thread_sleep_delay),
47 MYSQL_SYSVAR(autoinc_lock_mode),
48 MYSQL_SYSVAR(show_verbose_locks),
49 --- a/storage/innobase/include/srv0srv.h
50 +++ b/storage/innobase/include/srv0srv.h
52 extern ulint srv_mem_pool_size;
53 extern ulint srv_lock_table_size;
55 +extern ibool srv_thread_concurrency_timer_based;
57 extern ulint srv_n_file_io_threads;
58 extern my_bool srv_random_read_ahead;
59 extern ulong srv_read_ahead_threshold;
60 --- a/storage/innobase/srv/srv0srv.c
61 +++ b/storage/innobase/srv/srv0srv.c
63 computer. Bigger computers need bigger values. Value 0 will disable the
66 +UNIV_INTERN ibool srv_thread_concurrency_timer_based = FALSE;
67 UNIV_INTERN ulong srv_thread_concurrency = 0;
69 /* this mutex protects srv_conc data structures */
70 @@ -1148,6 +1149,75 @@
71 /*********************************************************************//**
72 Puts an OS thread to wait if there are too many concurrent threads
73 (>= srv_thread_concurrency) inside InnoDB. The threads wait in a FIFO queue. */
75 +#ifdef HAVE_ATOMIC_BUILTINS
77 +enter_innodb_with_tickets(trx_t* trx)
79 + trx->declared_to_be_inside_innodb = TRUE;
80 + trx->n_tickets_to_enter_innodb = SRV_FREE_TICKETS_TO_ENTER;
85 +srv_conc_enter_innodb_timer_based(trx_t* trx)
87 + lint conc_n_threads;
88 + ibool has_yielded = FALSE;
89 + ulint has_slept = 0;
91 + if (trx->declared_to_be_inside_innodb) {
92 + ut_print_timestamp(stderr);
94 +" InnoDB: Error: trying to declare trx to enter InnoDB, but\n"
95 +"InnoDB: it already is declared.\n", stderr);
96 + trx_print(stderr, trx, 0);
100 + if (srv_conc_n_threads < (lint) srv_thread_concurrency) {
101 + conc_n_threads = os_atomic_increment_lint(&srv_conc_n_threads, 1);
102 + if (conc_n_threads <= (lint) srv_thread_concurrency) {
103 + enter_innodb_with_tickets(trx);
106 + (void) os_atomic_increment_lint(&srv_conc_n_threads, -1);
110 + has_yielded = TRUE;
114 + if (trx->has_search_latch
115 + || NULL != UT_LIST_GET_FIRST(trx->trx_locks)) {
117 + conc_n_threads = os_atomic_increment_lint(&srv_conc_n_threads, 1);
118 + enter_innodb_with_tickets(trx);
123 + trx->op_info = "sleeping before entering InnoDB";
124 + os_thread_sleep(10000);
128 + conc_n_threads = os_atomic_increment_lint(&srv_conc_n_threads, 1);
129 + enter_innodb_with_tickets(trx);
134 +srv_conc_exit_innodb_timer_based(trx_t* trx)
136 + (void) os_atomic_increment_lint(&srv_conc_n_threads, -1);
137 + trx->declared_to_be_inside_innodb = FALSE;
138 + trx->n_tickets_to_enter_innodb = 0;
145 srv_conc_enter_innodb(
146 @@ -1182,6 +1252,13 @@
150 +#ifdef HAVE_ATOMIC_BUILTINS
151 + if (srv_thread_concurrency_timer_based) {
152 + srv_conc_enter_innodb_timer_based(trx);
157 os_fast_mutex_lock(&srv_conc_mutex);
159 if (trx->declared_to_be_inside_innodb) {
160 @@ -1335,6 +1412,14 @@
163 ut_ad(srv_conc_n_threads >= 0);
164 +#ifdef HAVE_ATOMIC_BUILTINS
165 + if (srv_thread_concurrency_timer_based) {
166 + (void) os_atomic_increment_lint(&srv_conc_n_threads, 1);
167 + trx->declared_to_be_inside_innodb = TRUE;
168 + trx->n_tickets_to_enter_innodb = 1;
173 os_fast_mutex_lock(&srv_conc_mutex);
175 @@ -1368,6 +1453,13 @@
179 +#ifdef HAVE_ATOMIC_BUILTINS
180 + if (srv_thread_concurrency_timer_based) {
181 + srv_conc_exit_innodb_timer_based(trx);
186 os_fast_mutex_lock(&srv_conc_mutex);
188 ut_ad(srv_conc_n_threads > 0);