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 diff -ruN a/storage/innobase/handler/ha_innodb.cc b/storage/innobase/handler/ha_innodb.cc
9 --- a/storage/innobase/handler/ha_innodb.cc 2010-12-03 15:41:52.045404706 +0900
10 +++ b/storage/innobase/handler/ha_innodb.cc 2010-12-03 15:42:11.568959457 +0900
12 static ulong innobase_write_io_threads;
13 static long innobase_buffer_pool_instances = 1;
15 +static my_bool innobase_thread_concurrency_timer_based;
16 static long long innobase_buffer_pool_size, innobase_log_file_size;
18 /** Percentage of the buffer pool to reserve for 'old' blocks.
20 srv_n_log_files = (ulint) innobase_log_files_in_group;
21 srv_log_file_size = (ulint) innobase_log_file_size;
23 + srv_thread_concurrency_timer_based =
24 + (ibool) innobase_thread_concurrency_timer_based;
26 #ifdef UNIV_LOG_ARCHIVE
27 srv_log_archive_on = (ulint) innobase_log_archive;
28 #endif /* UNIV_LOG_ARCHIVE */
29 @@ -11371,6 +11375,12 @@
30 "Maximum delay between polling for a spin lock (6 by default)",
31 NULL, NULL, 6L, 0L, ~0L, 0);
33 +static MYSQL_SYSVAR_BOOL(thread_concurrency_timer_based,
34 + innobase_thread_concurrency_timer_based,
35 + PLUGIN_VAR_NOCMDARG | PLUGIN_VAR_READONLY,
36 + "Use InnoDB timer based concurrency throttling. ",
39 static MYSQL_SYSVAR_ULONG(thread_concurrency, srv_thread_concurrency,
41 "Helps in performance tuning in heavily concurrent environments. Sets the maximum number of threads allowed inside InnoDB. Value 0 will disable the thread throttling.",
42 @@ -11579,6 +11589,7 @@
43 MYSQL_SYSVAR(spin_wait_delay),
44 MYSQL_SYSVAR(table_locks),
45 MYSQL_SYSVAR(thread_concurrency),
46 + MYSQL_SYSVAR(thread_concurrency_timer_based),
47 MYSQL_SYSVAR(thread_sleep_delay),
48 MYSQL_SYSVAR(autoinc_lock_mode),
49 MYSQL_SYSVAR(show_verbose_locks),
50 diff -ruN a/storage/innobase/include/srv0srv.h b/storage/innobase/include/srv0srv.h
51 --- a/storage/innobase/include/srv0srv.h 2010-12-03 15:37:45.543027751 +0900
52 +++ b/storage/innobase/include/srv0srv.h 2010-12-03 15:42:11.571024631 +0900
54 extern ulint srv_mem_pool_size;
55 extern ulint srv_lock_table_size;
57 +extern ibool srv_thread_concurrency_timer_based;
59 extern ulint srv_n_file_io_threads;
60 extern ulong srv_read_ahead_threshold;
61 extern ulint srv_n_read_io_threads;
62 diff -ruN a/storage/innobase/srv/srv0srv.c b/storage/innobase/srv/srv0srv.c
63 --- a/storage/innobase/srv/srv0srv.c 2010-12-03 15:37:45.546023493 +0900
64 +++ b/storage/innobase/srv/srv0srv.c 2010-12-03 15:42:11.574955879 +0900
66 computer. Bigger computers need bigger values. Value 0 will disable the
69 +UNIV_INTERN ibool srv_thread_concurrency_timer_based = FALSE;
70 UNIV_INTERN ulong srv_thread_concurrency = 0;
72 /* this mutex protects srv_conc data structures */
73 @@ -1164,6 +1165,75 @@
74 /*********************************************************************//**
75 Puts an OS thread to wait if there are too many concurrent threads
76 (>= srv_thread_concurrency) inside InnoDB. The threads wait in a FIFO queue. */
78 +#ifdef HAVE_ATOMIC_BUILTINS
80 +enter_innodb_with_tickets(trx_t* trx)
82 + trx->declared_to_be_inside_innodb = TRUE;
83 + trx->n_tickets_to_enter_innodb = SRV_FREE_TICKETS_TO_ENTER;
88 +srv_conc_enter_innodb_timer_based(trx_t* trx)
90 + lint conc_n_threads;
91 + ibool has_yielded = FALSE;
92 + ulint has_slept = 0;
94 + if (trx->declared_to_be_inside_innodb) {
95 + ut_print_timestamp(stderr);
97 +" InnoDB: Error: trying to declare trx to enter InnoDB, but\n"
98 +"InnoDB: it already is declared.\n", stderr);
99 + trx_print(stderr, trx, 0);
100 + putc('\n', stderr);
103 + if (srv_conc_n_threads < (lint) srv_thread_concurrency) {
104 + conc_n_threads = os_atomic_increment_lint(&srv_conc_n_threads, 1);
105 + if (conc_n_threads <= (lint) srv_thread_concurrency) {
106 + enter_innodb_with_tickets(trx);
109 + os_atomic_increment_lint(&srv_conc_n_threads, -1);
113 + has_yielded = TRUE;
117 + if (trx->has_search_latch
118 + || NULL != UT_LIST_GET_FIRST(trx->trx_locks)) {
120 + conc_n_threads = os_atomic_increment_lint(&srv_conc_n_threads, 1);
121 + enter_innodb_with_tickets(trx);
126 + trx->op_info = "sleeping before entering InnoDB";
127 + os_thread_sleep(10000);
131 + conc_n_threads = os_atomic_increment_lint(&srv_conc_n_threads, 1);
132 + enter_innodb_with_tickets(trx);
137 +srv_conc_exit_innodb_timer_based(trx_t* trx)
139 + os_atomic_increment_lint(&srv_conc_n_threads, -1);
140 + trx->declared_to_be_inside_innodb = FALSE;
141 + trx->n_tickets_to_enter_innodb = 0;
148 srv_conc_enter_innodb(
149 @@ -1194,6 +1264,13 @@
153 +#ifdef HAVE_ATOMIC_BUILTINS
154 + if (srv_thread_concurrency_timer_based) {
155 + srv_conc_enter_innodb_timer_based(trx);
160 os_fast_mutex_lock(&srv_conc_mutex);
162 if (trx->declared_to_be_inside_innodb) {
163 @@ -1339,6 +1416,14 @@
166 ut_ad(srv_conc_n_threads >= 0);
167 +#ifdef HAVE_ATOMIC_BUILTINS
168 + if (srv_thread_concurrency_timer_based) {
169 + os_atomic_increment_lint(&srv_conc_n_threads, 1);
170 + trx->declared_to_be_inside_innodb = TRUE;
171 + trx->n_tickets_to_enter_innodb = 1;
176 os_fast_mutex_lock(&srv_conc_mutex);
178 @@ -1372,6 +1457,13 @@
182 +#ifdef HAVE_ATOMIC_BUILTINS
183 + if (srv_thread_concurrency_timer_based) {
184 + srv_conc_exit_innodb_timer_based(trx);
189 os_fast_mutex_lock(&srv_conc_mutex);
191 ut_ad(srv_conc_n_threads > 0);