Debian Codegraph @

storage/innobase/srv/srv0conc.cc

raw
1 /*****************************************************************************
2
3 Copyright (c) 2011, 2026, Oracle and/or its affiliates.
4
5 Portions of this file contain modifications contributed and copyrighted by
6 Google, Inc. Those modifications are gratefully acknowledged and are described
7 briefly in the InnoDB documentation. The contributions by Google are
8 incorporated with their permission, and subject to the conditions contained in
9 the file COPYING.Google.
10
11 Portions of this file contain modifications contributed and copyrighted
12 by Percona Inc.. Those modifications are
13 gratefully acknowledged and are described briefly in the InnoDB
14 documentation. The contributions by Percona Inc. are incorporated with
15 their permission, and subject to the conditions contained in the file
16 COPYING.Percona.
17
18 This program is free software; you can redistribute it and/or modify it under
19 the terms of the GNU General Public License, version 2.0, as published by the
20 Free Software Foundation.
21
22 This program is designed to work with certain software (including
23 but not limited to OpenSSL) that is licensed under separate terms,
24 as designated in a particular file or component or in included license
25 documentation. The authors of MySQL hereby grant you an additional
26 permission to link the program and your derivative works with the
27 separately licensed software that they have either included with
28 the program or referenced in the documentation.
29
30 This program is distributed in the hope that it will be useful, but WITHOUT
31 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
32 FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
33 for more details.
34
35 You should have received a copy of the GNU General Public License along with
36 this program; if not, write to the Free Software Foundation, Inc.,
37 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
38
39 *****************************************************************************/
40
41 /** @file srv/srv0conc.cc
42
43 InnoDB concurrency manager
44
45 Created 2011/04/18 Sunny Bains
46 *******************************************************/
47
48 #include <mysql/service_thd_wait.h>
49 #include <stddef.h>
50 #include <sys/types.h>
51
52 #include "dict0dict.h"
53 #include "ha_prototypes.h"
54 #include "row0mysql.h"
55 #include "srv0srv.h"
56 #include "trx0trx.h"
57
58 /** Number of times a thread is allowed to enter InnoDB within the same
59 SQL query after it has once got the ticket. */
60 ulong srv_n_free_tickets_to_enter = 500;
61
62 /** Maximum sleep delay (in micro-seconds), value of 0 disables it. */
63 ulong srv_adaptive_max_sleep_delay = 150000;
64
65 ulong srv_thread_sleep_delay = 10000;
66
67 /** The following controls how many threads we let inside InnoDB concurrently:
68 threads waiting for locks are not counted into the number because otherwise
69 we could get a deadlock. Value of 0 will disable the concurrency check. */
70
71 ulong srv_thread_concurrency = 0;
72
73 /** Variables tracking the active and waiting threads. */
74 struct srv_conc_t {
75 char pad[ut::INNODB_CACHE_LINE_SIZE];
76
77 /** Number of transactions that have declared_to_be_inside_innodb set.
78 It used to be a non-error for this value to drop below zero temporarily.
79 This is no longer true. We'll, however, keep the signed datatype to add
80 assertions to catch any corner cases that we may have missed. */
81 std::atomic<int32_t> n_active;
82
83 /** Number of OS threads waiting in the FIFO for permission to
84 enter InnoDB */
85 std::atomic<int32_t> n_waiting;
86 };
87
88 /* Control variables for tracking concurrency. */
89 static srv_conc_t srv_conc;
90
91 /** Note that a user thread is entering InnoDB. */
92 static void srv_enter_innodb_with_tickets(
93 trx_t *trx) /*!< in/out: transaction that wants
94 to enter InnoDB */
95 {
96 trx->declared_to_be_inside_innodb = true;
97 trx->n_tickets_to_enter_innodb = srv_n_free_tickets_to_enter;
98 }
99
100 /** Handle the scheduling of a user thread that wants to enter InnoDB. Setting
101 srv_adaptive_max_sleep_delay > 0 switches the adaptive sleep calibration to
102 ON. When set, we want to wait in the queue for as little time as possible.
103 However, very short waits will result in a lot of context switches and that
104 is also not desirable. When threads need to sleep multiple times we increment
105 srv_thread_sleep_delay by one. When we see threads getting a slot without
106 waiting and there are no other threads waiting in the queue, we try and reduce
107 the wait as much as we can. Currently we reduce it by half each time. If the
108 thread only had to wait for one turn before it was able to enter InnoDB we
109 decrement it by one. This is to try and keep the sleep time stable around the
110 "optimum" sleep time.
111 @return InnoDB error code. */
112 static dberr_t srv_conc_enter_innodb_with_atomics(
113 trx_t *trx) /*!< in/out: transaction that wants
114 to enter InnoDB */
115 {
116 ulint n_sleeps = 0;
117 bool notified_mysql = false;
118
119 ut_a(!trx->declared_to_be_inside_innodb);
120
121 for (;;) {
122 ulint sleep_in_us;
123
124 if (srv_thread_concurrency == 0) {
125 if (notified_mysql) {
126 srv_conc.n_waiting.fetch_sub(1, std::memory_order_relaxed);
127
128 thd_wait_end(trx->mysql_thd);
129 }
130
131 return DB_SUCCESS;
132 }
133
134 if (srv_conc.n_active.load(std::memory_order_relaxed) <
135 (int32_t)srv_thread_concurrency) {
136 /* Check if there are any free tickets. */
137 const auto n_active =
138 srv_conc.n_active.fetch_add(1, std::memory_order_acquire) + 1;
139
140 if (n_active <= (int32_t)srv_thread_concurrency) {
141 srv_enter_innodb_with_tickets(trx);
142
143 if (notified_mysql) {
144 srv_conc.n_waiting.fetch_sub(1, std::memory_order_relaxed);
145
146 thd_wait_end(trx->mysql_thd);
147 }
148
149 if (srv_adaptive_max_sleep_delay > 0) {
150 if (srv_thread_sleep_delay > 20 && n_sleeps == 1) {
151 --srv_thread_sleep_delay;
152 }
153
154 if (srv_conc.n_waiting.load(std::memory_order_relaxed) == 0) {
155 srv_thread_sleep_delay >>= 1;
156 }
157 }
158
159 return DB_SUCCESS;
160 }
161
162 /* Since there were no free seats, we relinquish
163 the overbooked ticket. */
164 srv_conc.n_active.fetch_sub(1, std::memory_order_release);
165 }
166
167 if (!notified_mysql) {
168 srv_conc.n_waiting.fetch_add(1, std::memory_order_relaxed);
169
170 thd_wait_begin(trx->mysql_thd, THD_WAIT_USER_LOCK);
171
172 notified_mysql = true;
173 }
174
175 DEBUG_SYNC_C("user_thread_waiting");
176 trx->op_info = "sleeping before entering InnoDB";
177
178 sleep_in_us = srv_thread_sleep_delay;
179
180 /* Guard against overflow when adaptive sleep delay is on. */
181
182 if (srv_adaptive_max_sleep_delay > 0 &&
183 sleep_in_us > srv_adaptive_max_sleep_delay) {
184 sleep_in_us = srv_adaptive_max_sleep_delay;
185 srv_thread_sleep_delay = static_cast<ulong>(sleep_in_us);
186 }
187
188 std::this_thread::sleep_for(std::chrono::microseconds(sleep_in_us));
189
190 trx->op_info = "";
191
192 ++n_sleeps;
193
194 if (srv_adaptive_max_sleep_delay > 0 && n_sleeps > 1) {
195 ++srv_thread_sleep_delay;
196 }
197
198 if (trx_is_interrupted(trx)) {
199 if (notified_mysql) {
200 srv_conc.n_waiting.fetch_sub(1, std::memory_order_relaxed);
201
202 thd_wait_end(trx->mysql_thd);
203 }
204 return DB_INTERRUPTED;
205 }
206 }
207 }
208
209 /** Note that a user thread is leaving InnoDB code. */
210 static void srv_conc_exit_innodb_with_atomics(
211 trx_t *trx) /*!< in/out: transaction */
212 {
213 trx->n_tickets_to_enter_innodb = 0;
214 trx->declared_to_be_inside_innodb = false;
215 srv_conc.n_active.fetch_sub(1, std::memory_order_release);
216 }
217
218 dberr_t srv_conc_enter_innodb(row_prebuilt_t *prebuilt) {
219 trx_t *trx = prebuilt->trx;
220
221 #ifdef UNIV_DEBUG
222 {
223 btrsea_sync_check check(trx->has_search_latch);
224
225 ut_ad(!sync_check_iterate(check));
226 }
227 #endif /* UNIV_DEBUG */
228
229 return srv_conc_enter_innodb_with_atomics(trx);
230 }
231
232 /** This lets a thread enter InnoDB regardless of the number of threads inside
233 InnoDB. This must be called when a thread ends a lock wait. */
234 void srv_conc_force_enter_innodb(trx_t *trx) /*!< in: transaction object
235 associated with the thread */
236 {
237 #ifdef UNIV_DEBUG
238 {
239 btrsea_sync_check check(trx->has_search_latch);
240
241 ut_ad(!sync_check_iterate(check));
242 }
243 #endif /* UNIV_DEBUG */
244
245 if (!srv_thread_concurrency) {
246 return;
247 }
248
249 ut_ad(srv_conc.n_active.load(std::memory_order_relaxed) >= 0);
250
251 srv_conc.n_active.fetch_add(1, std::memory_order_acquire);
252
253 trx->n_tickets_to_enter_innodb = 1;
254 trx->declared_to_be_inside_innodb = true;
255 }
256
257 /** This must be called when a thread exits InnoDB in a lock wait or at the
258 end of an SQL statement. */
259 void srv_conc_force_exit_innodb(trx_t *trx) /*!< in: transaction object
260 associated with the thread */
261 {
262 if ((trx->mysql_thd != nullptr &&
263 thd_is_replication_slave_thread(trx->mysql_thd)) ||
264 trx->declared_to_be_inside_innodb == false) {
265 return;
266 }
267
268 srv_conc_exit_innodb_with_atomics(trx);
269
270 #ifdef UNIV_DEBUG
271 {
272 btrsea_sync_check check(trx->has_search_latch);
273
274 ut_ad(!sync_check_iterate(check));
275 }
276 #endif /* UNIV_DEBUG */
277 }
278
279 /** Get the count of threads waiting inside InnoDB. */
280 int32_t srv_conc_get_waiting_threads(void) {
281 return srv_conc.n_waiting.load(std::memory_order_relaxed);
282 }
283
284 /** Get the count of threads active inside InnoDB. */
285 int32_t srv_conc_get_active_threads(void) {
286 return srv_conc.n_active.load(std::memory_order_relaxed);
287 }