Debian Codegraph @

storage/innobase/sync/sync0debug.cc

raw
1 /*****************************************************************************
2
3 Copyright (c) 2012, 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 This program is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License, version 2.0, as published by the
13 Free Software Foundation.
14
15 This program is designed to work with certain software (including
16 but not limited to OpenSSL) that is licensed under separate terms,
17 as designated in a particular file or component or in included license
18 documentation. The authors of MySQL hereby grant you an additional
19 permission to link the program and your derivative works with the
20 separately licensed software that they have either included with
21 the program or referenced in the documentation.
22
23 This program is distributed in the hope that it will be useful, but WITHOUT
24 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
25 FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
26 for more details.
27
28 You should have received a copy of the GNU General Public License along with
29 this program; if not, write to the Free Software Foundation, Inc.,
30 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
31
32 *****************************************************************************/
33
34 /** @file sync/sync0debug.cc
35 Debug checks for latches.
36
37 Created 2012-08-21 Sunny Bains
38 *******************************************************/
39
40 #include "sync0debug.h"
41
42 #include <stddef.h>
43 #include <algorithm>
44 #include <atomic>
45 #include <iostream>
46 #include <map>
47 #include <string>
48 #include <vector>
49
50 #include "sync0rw.h"
51 #include "ut0mutex.h"
52
53 #include <scope_guard.h>
54
55 #ifndef UNIV_NO_ERR_MSGS
56 #include "srv0start.h"
57 #endif /* !UNIV_NO_ERR_MSGS */
58
59 #include "ut0new.h"
60
61 #ifdef UNIV_DEBUG
62 #ifndef UNIV_NO_ERR_MSGS
63 #include <current_thd.h>
64 #endif /* !UNIV_NO_ERR_MSGS */
65
66 bool srv_sync_debug;
67
68 /** The global mutex which protects debug info lists of all rw-locks.
69 To modify the debug info list of an rw-lock, this mutex has to be
70 acquired in addition to the mutex protecting the lock. */
71 static ib_mutex_t rw_lock_debug_mutex;
72
73 /** If deadlock detection does not get immediately the mutex,
74 it may wait for this event */
75 static os_event_t rw_lock_debug_event;
76
77 /** This is set to true, if there may be waiters for the event */
78 static std::atomic<bool> rw_lock_debug_waiters{false};
79
80 /** The latch held by a thread */
81 struct Latched {
82 /** Constructor */
83 Latched() : m_latch(), m_level(SYNC_UNKNOWN) {}
84
85 /** Constructor
86 @param[in] latch Latch instance
87 @param[in] level Level of latch held */
88 Latched(const latch_t *latch, latch_level_t level)
89 : m_latch(latch), m_level(level) {
90 /* No op */
91 }
92
93 /** @return the latch level */
94 latch_level_t get_level() const { return (m_level); }
95
96 /** Check if the rhs latch and level match
97 @param[in] rhs instance to compare with
98 @return true on match */
99 bool operator==(const Latched &rhs) const {
100 return (m_latch == rhs.m_latch && m_level == rhs.m_level);
101 }
102
103 /** The latch instance */
104 const latch_t *m_latch;
105
106 /** The latch level. For buffer blocks we can pass a separate latch
107 level to check against, see buf_block_dbg_add_level() */
108 latch_level_t m_level;
109 };
110
111 /** Thread specific latches. This is ordered on level in descending order. */
112 typedef std::vector<Latched, ut::allocator<Latched>> Latches;
113
114 /** The deadlock detector. */
115 struct LatchDebug {
116 /** Debug mutex for control structures, should not be tracked
117 by this module. */
118 typedef OSMutex Mutex;
119
120 /** For tracking a thread's latches. */
121 typedef std::map<std::thread::id, Latches *, std::less<std::thread::id>,
122 ut::allocator<std::pair<const std::thread::id, Latches *>>>
123 ThreadMap;
124
125 /** Constructor */
126 LatchDebug() UNIV_NOTHROW;
127
128 /** Destructor */
129 ~LatchDebug() UNIV_NOTHROW { m_mutex.destroy(); }
130
131 /** Create a new instance if one doesn't exist else return
132 the existing one.
133 @param[in] add add an empty entry if one is not
134 found (default no)
135 @return pointer to a thread's acquired latches. */
136 Latches *thread_latches(bool add = false) UNIV_NOTHROW;
137
138 /** Check that all the latches already owned by a thread have a higher
139 level than limit and returns the latch which violates this expectation if any.
140 @param[in] latches the thread's existing (acquired) latches
141 @param[in] limit to check against
142 @return latched if there is one with a level <= limit . */
143 const Latched *find_lower_or_equal(const Latches *latches,
144 latch_level_t limit) const UNIV_NOTHROW;
145
146 /** Checks if the level value exists in the thread's acquired latches.
147 @param[in] latches the thread's existing (acquired) latches
148 @param[in] level to lookup
149 @return latch if found or 0 */
150 const latch_t *find(const Latches *latches,
151 latch_level_t level) const UNIV_NOTHROW;
152
153 /** Checks if the level value exists in the thread's acquired latches.
154 @param[in] level The level to lookup
155 @return latch if found or NULL */
156 const latch_t *find(latch_level_t level) UNIV_NOTHROW;
157
158 /** Report error and abort.
159 @param[in] latches thread's existing latches
160 @param[in] latched The existing latch causing the
161 invariant to fail
162 @param[in] level The new level request that breaks
163 the order */
164 [[noreturn]] void crash(const Latches *latches, const Latched *latched,
165 latch_level_t level) const UNIV_NOTHROW;
166
167 /** Do a basic ordering check. Asserts that all the existing latches have a
168 level higher than the forbidden_level.
169 @param[in] latches thread's existing latches
170 @param[in] requested_level
171 the level of the requested latch, used in error message only, to provide the
172 context of the check to the user. For actual comparisons in asserts the value
173 of forbidden_level will be used.
174 @param[in] forbidden_level
175 Exclusive lower bound on levels of currently held latches - i.e. each latch in
176 @p latches must have level strictly grater than @p forbidden_level. Usually,
177 @p forbidden_level equals @p requested_level, but if we allow a thread to hold
178 more than one latch at @p requested_level (avoiding deadlock between them in
179 some other way), then it will be @p requested_level - 1. It can have a
180 value other than these two, in rare cases, where deadlock avoidance rules are
181 even more complicated, but this should be avoided.
182 */
183 void assert_all_held_are_above(const Latches *latches,
184 latch_level_t requested_level,
185 ulint forbidden_level) const UNIV_NOTHROW;
186
187 /** Asserts that all the latches already acquired by the thread have a
188 level higher than the newly requested latch. This is the most typical latching
189 order rule ensuring no deadlock cycle: strictly descending sequence can not
190 have a loop.
191 @param[in] latches thread's existing latches
192 @param[in] requested_level the level of the requested latch
193 */
194 void assert_requested_is_lower_than_held(latch_level_t requested_level,
195 const Latches *latches) const
196 UNIV_NOTHROW {
197 assert_all_held_are_above(latches, requested_level, requested_level);
198 }
199
200 /** Asserts that all the latches already acquired by the thread have a
201 level higher or equal to the newly requested latch. This is a rule used for
202 latches which can have multiple instances and a thread is allowed to latch
203 more than one such instance, when we can somehow prove there's no deadlock due
204 to threads requesting more than one. It's the responsibility of the developer
205 to document and prove this additional property, ideally encoding it in
206 LatchDebug::check_order. One example of such case would be when we know that
207 to latch more than one instance the thread must first acquire exclusive right
208 to another singleton latch. Another example would be if we always follow some
209 natural ordering on the latches of this kind, like by increasing address in an
210 array.
211 @param[in] latches thread's existing latches
212 @param[in] requested_level the level of the requested latch
213 */
214 void assert_requested_is_lower_or_equal_to_held(latch_level_t requested_level,
215 const Latches *latches) const
216 UNIV_NOTHROW {
217 assert_all_held_are_above(latches, requested_level, requested_level - 1);
218 }
219
220 /** Adds a latch and its level in the thread level array. Allocates
221 the memory for the array if called for the first time for this
222 OS thread. Makes the checks against other latch levels stored
223 in the array for this thread.
224
225 @param[in] latch latch that the thread wants to acqire.
226 @param[in] level latch level to check against */
227 void lock_validate(const latch_t *latch, latch_level_t level) UNIV_NOTHROW {
228 /* Ignore diagnostic latches, starting with '.' */
229
230 if (*latch->get_name() != '.' && latch->get_level() != SYNC_LEVEL_VARYING) {
231 ut_ad(level != SYNC_LEVEL_VARYING);
232
233 Latches *latches = check_order(latch, level);
234
235 if (!(latches->empty() || level == SYNC_LEVEL_VARYING ||
236 level == SYNC_NO_ORDER_CHECK ||
237 latches->back().get_level() == SYNC_NO_ORDER_CHECK ||
238 latches->back().m_latch->get_level() == SYNC_LEVEL_VARYING ||
239 latches->back().get_level() >= level)) {
240 const auto latest_latch_level = latches->back().m_latch->get_level();
241 const auto latest_level = latches->back().get_level();
242
243 #ifdef UNIV_NO_ERR_MSGS
244 ib::error()
245 #else
246 ib::error(ER_IB_LOCK_VALIDATE_LATCH_ORDER_VIOLATION)
247 #endif
248 << "LatchDebug::lock_validate() latch order violation. level="
249 << level << ", latest_latch_level=" << latest_latch_level
250 << ", latest_level=" << latest_level << ".";
251 ut_error;
252 }
253 }
254 }
255
256 /** Adds a latch and its level in the thread level array. Allocates
257 the memory for the array if called for the first time for this
258 OS thread. Makes the checks against other latch levels stored
259 in the array for this thread.
260
261 @param[in] latch latch that the thread wants to acquire.
262 @param[in] level latch level to check against */
263 void lock_granted(const latch_t *latch, latch_level_t level) UNIV_NOTHROW {
264 /* Ignore diagnostic latches, starting with '.' */
265
266 if (*latch->get_name() != '.' && latch->get_level() != SYNC_LEVEL_VARYING) {
267 Latches *latches = thread_latches(true);
268
269 latches->push_back(Latched(latch, level));
270 }
271 }
272
273 /** For recursive X rw-locks.
274 @param[in] latch The RW-Lock to relock */
275 void relock(const latch_t *latch) UNIV_NOTHROW {
276 ut_a(latch->m_rw_lock);
277
278 latch_level_t level = latch->get_level();
279
280 /* Ignore diagnostic latches, starting with '.' */
281
282 if (*latch->get_name() != '.' && latch->get_level() != SYNC_LEVEL_VARYING) {
283 Latches *latches = thread_latches(true);
284
285 Latches::iterator it =
286 std::find(latches->begin(), latches->end(), Latched(latch, level));
287
288 if (!(latches->empty() || level == SYNC_LEVEL_VARYING ||
289 level == SYNC_NO_ORDER_CHECK ||
290 latches->back().m_latch->get_level() == SYNC_LEVEL_VARYING ||
291 latches->back().m_latch->get_level() == SYNC_NO_ORDER_CHECK ||
292 latches->back().get_level() >= level || it != latches->end())) {
293 const auto latest_latch_level = latches->back().m_latch->get_level();
294 const auto latest_level = latches->back().get_level();
295
296 #ifdef UNIV_NO_ERR_MSGS
297 ib::error()
298 #else
299 ib::error(ER_IB_RELOCK_LATCH_ORDER_VIOLATION)
300 #endif
301 << "LatchDebug::relock() latch order violation. level=" << level
302 << ", latest_latch_level=" << latest_latch_level
303 << ", latest_level=" << latest_level << ".";
304 ut_error;
305 }
306
307 if (it == latches->end()) {
308 latches->push_back(Latched(latch, level));
309 } else {
310 latches->insert(it, Latched(latch, level));
311 }
312 }
313 }
314
315 /** Iterate over a thread's latches.
316 @param[in,out] functor The callback
317 @return true if the functor returns true. */
318 bool for_each(sync_check_functor_t &functor) UNIV_NOTHROW {
319 const Latches *latches = thread_latches();
320
321 if (latches == nullptr) {
322 return (functor.result());
323 }
324
325 Latches::const_iterator end = latches->end();
326
327 for (Latches::const_iterator it = latches->begin(); it != end; ++it) {
328 if (functor(it->m_level)) {
329 break;
330 }
331 }
332
333 return (functor.result());
334 }
335
336 /** Removes a latch from the thread level array if it is found there.
337 @param[in] latch The latch that was released */
338 void unlock(const latch_t *latch) UNIV_NOTHROW;
339
340 /** Get the level name
341 @param[in] level The level ID to lookup
342 @return level name */
343 const std::string &get_level_name(latch_level_t level) const UNIV_NOTHROW {
344 Levels::const_iterator it = m_levels.find(level);
345
346 ut_ad(it != m_levels.end());
347
348 return (it->second);
349 }
350
351 /** Initialise the debug data structures */
352 static void init() UNIV_NOTHROW;
353
354 /** Shutdown the latch debug checking */
355 static void shutdown() UNIV_NOTHROW;
356
357 /** @return the singleton instance */
358 static LatchDebug *instance() UNIV_NOTHROW { return (s_instance); }
359
360 /** Create the singleton instance */
361 static void create_instance() UNIV_NOTHROW {
362 ut_ad(s_instance == nullptr);
363
364 s_instance = ut::new_withkey<LatchDebug>(UT_NEW_THIS_FILE_PSI_KEY);
365 }
366
367 private:
368 /** Disable copying */
369 LatchDebug(const LatchDebug &);
370 LatchDebug &operator=(const LatchDebug &);
371
372 /** Adds a latch and its level in the thread level array. Allocates
373 the memory for the array if called first time for this OS thread.
374 Makes the checks against other latch levels stored in the array
375 for this thread.
376
377 @param[in] latch pointer to a mutex or an rw-lock
378 @param[in] level level in the latching order
379 @return the thread's latches */
380 Latches *check_order(const latch_t *latch, latch_level_t level) UNIV_NOTHROW;
381
382 /** Print the latches acquired by a thread
383 @param[in] latches Latches acquired by a thread */
384 void print_latches(const Latches *latches) const UNIV_NOTHROW;
385
386 /** Special handling for the RTR mutexes. We need to add proper
387 levels for them if possible.
388 @param[in] latch Latch to check
389 @return true if it is a an _RTR_ mutex */
390 bool is_rtr_mutex(const latch_t *latch) const UNIV_NOTHROW {
391 return (latch->get_id() == LATCH_ID_RTR_ACTIVE_MUTEX ||
392 latch->get_id() == LATCH_ID_RTR_PATH_MUTEX ||
393 latch->get_id() == LATCH_ID_RTR_MATCH_MUTEX ||
394 latch->get_id() == LATCH_ID_RTR_SSN_MUTEX);
395 }
396
397 private:
398 /** Comparator for the Levels . */
399 struct latch_level_less {
400 /** @return true if lhs < rhs */
401 bool operator()(const latch_level_t &lhs,
402 const latch_level_t &rhs) const UNIV_NOTHROW {
403 return (lhs < rhs);
404 }
405 };
406
407 typedef std::map<latch_level_t, std::string, latch_level_less,
408 ut::allocator<std::pair<const latch_level_t, std::string>>>
409 Levels;
410
411 /** Mutex protecting the deadlock detector data structures. */
412 Mutex m_mutex;
413
414 /** Thread specific data. Protected by m_mutex. */
415 ThreadMap m_threads;
416
417 /** Mapping from latche level to its string representation. */
418 Levels m_levels;
419
420 /** The singleton instance. Must be created in single threaded mode. */
421 static LatchDebug *s_instance;
422
423 public:
424 /** For checking whether this module has been initialised or not. */
425 static bool s_initialized;
426 };
427
428 /** The latch order checking infra-structure */
429 LatchDebug *LatchDebug::s_instance = nullptr;
430 bool LatchDebug::s_initialized = false;
431
432 #define LEVEL_MAP_INSERT(T) \
433 do { \
434 std::pair<Levels::iterator, bool> result = \
435 m_levels.insert(Levels::value_type(T, #T)); \
436 ut_ad(result.second); \
437 } while (0)
438
439 /** Setup the mapping from level ID to level name mapping */
440 LatchDebug::LatchDebug() {
441 m_mutex.init();
442
443 LEVEL_MAP_INSERT(SYNC_UNKNOWN);
444 LEVEL_MAP_INSERT(SYNC_MUTEX);
445 LEVEL_MAP_INSERT(RW_LOCK_SX);
446 LEVEL_MAP_INSERT(RW_LOCK_X_WAIT);
447 LEVEL_MAP_INSERT(RW_LOCK_S);
448 LEVEL_MAP_INSERT(RW_LOCK_X);
449 LEVEL_MAP_INSERT(RW_LOCK_NOT_LOCKED);
450 LEVEL_MAP_INSERT(SYNC_LOCK_FREE_HASH);
451 LEVEL_MAP_INSERT(SYNC_MONITOR_MUTEX);
452 LEVEL_MAP_INSERT(SYNC_ANY_LATCH);
453 LEVEL_MAP_INSERT(SYNC_ALTER_STAGE);
454 LEVEL_MAP_INSERT(SYNC_FIL_SHARD);
455 LEVEL_MAP_INSERT(SYNC_DBLWR);
456 LEVEL_MAP_INSERT(SYNC_BUF_CHUNKS);
457 LEVEL_MAP_INSERT(SYNC_BUF_FLUSH_LIST);
458 LEVEL_MAP_INSERT(SYNC_BUF_FLUSH_STATE);
459 LEVEL_MAP_INSERT(SYNC_BUF_ZIP_HASH);
460 LEVEL_MAP_INSERT(SYNC_BUF_FREE_LIST);
461 LEVEL_MAP_INSERT(SYNC_BUF_ZIP_FREE);
462 LEVEL_MAP_INSERT(SYNC_BUF_BLOCK);
463 LEVEL_MAP_INSERT(SYNC_BUF_PAGE_HASH);
464 LEVEL_MAP_INSERT(SYNC_BUF_LRU_LIST);
465 LEVEL_MAP_INSERT(SYNC_POOL);
466 LEVEL_MAP_INSERT(SYNC_POOL_MANAGER);
467 LEVEL_MAP_INSERT(SYNC_TEMP_POOL_MANAGER);
468 LEVEL_MAP_INSERT(SYNC_SEARCH_SYS);
469 LEVEL_MAP_INSERT(SYNC_WORK_QUEUE);
470 LEVEL_MAP_INSERT(SYNC_FTS_TOKENIZE);
471 LEVEL_MAP_INSERT(SYNC_FTS_OPTIMIZE);
472 LEVEL_MAP_INSERT(SYNC_FTS_BG_THREADS);
473 LEVEL_MAP_INSERT(SYNC_FTS_CACHE_INIT);
474 LEVEL_MAP_INSERT(SYNC_RECV);
475 LEVEL_MAP_INSERT(SYNC_RECV_WRITER);
476 LEVEL_MAP_INSERT(SYNC_LOG_SN);
477 LEVEL_MAP_INSERT(SYNC_LOG_SN_MUTEX);
478 LEVEL_MAP_INSERT(SYNC_LOG_LIMITS);
479 LEVEL_MAP_INSERT(SYNC_LOG_FLUSHER);
480 LEVEL_MAP_INSERT(SYNC_LOG_FILES);
481 LEVEL_MAP_INSERT(SYNC_LOG_WRITER);
482 LEVEL_MAP_INSERT(SYNC_LOG_WRITE_NOTIFIER);
483 LEVEL_MAP_INSERT(SYNC_LOG_FLUSH_NOTIFIER);
484 LEVEL_MAP_INSERT(SYNC_LOG_CLOSER);
485 LEVEL_MAP_INSERT(SYNC_LOG_CHECKPOINTER);
486 LEVEL_MAP_INSERT(SYNC_LOG_ARCH);
487 LEVEL_MAP_INSERT(SYNC_PAGE_ARCH);
488 LEVEL_MAP_INSERT(SYNC_PAGE_ARCH_OPER);
489 LEVEL_MAP_INSERT(SYNC_PAGE_ARCH_CLIENT);
490 LEVEL_MAP_INSERT(SYNC_PAGE_CLEANER);
491 LEVEL_MAP_INSERT(SYNC_PURGE_QUEUE);
492 LEVEL_MAP_INSERT(SYNC_TRX_SYS_HEADER);
493 LEVEL_MAP_INSERT(SYNC_THREADS);
494 LEVEL_MAP_INSERT(SYNC_TRX);
495 LEVEL_MAP_INSERT(SYNC_TRX_SYS);
496 LEVEL_MAP_INSERT(SYNC_TRX_SYS_SHARD);
497 LEVEL_MAP_INSERT(SYNC_TRX_SYS_SERIALISATION);
498 LEVEL_MAP_INSERT(SYNC_LOCK_SYS_GLOBAL);
499 LEVEL_MAP_INSERT(SYNC_LOCK_SYS_SHARDED);
500 LEVEL_MAP_INSERT(SYNC_LOCK_WAIT_SYS);
501 LEVEL_MAP_INSERT(SYNC_INDEX_ONLINE_LOG);
502 LEVEL_MAP_INSERT(SYNC_IBUF_BITMAP);
503 LEVEL_MAP_INSERT(SYNC_IBUF_BITMAP_MUTEX);
504 LEVEL_MAP_INSERT(SYNC_IBUF_TREE_NODE);
505 LEVEL_MAP_INSERT(SYNC_IBUF_TREE_NODE_NEW);
506 LEVEL_MAP_INSERT(SYNC_IBUF_INDEX_TREE);
507 LEVEL_MAP_INSERT(SYNC_IBUF_MUTEX);
508 LEVEL_MAP_INSERT(SYNC_FSP_PAGE);
509 LEVEL_MAP_INSERT(SYNC_FSP);
510 LEVEL_MAP_INSERT(SYNC_EXTERN_STORAGE);
511 LEVEL_MAP_INSERT(SYNC_RSEG_ARRAY_HEADER);
512 LEVEL_MAP_INSERT(SYNC_TRX_UNDO_PAGE);
513 LEVEL_MAP_INSERT(SYNC_RSEG_HEADER);
514 LEVEL_MAP_INSERT(SYNC_RSEG_HEADER_NEW);
515 LEVEL_MAP_INSERT(SYNC_TEMP_SPACE_RSEG);
516 LEVEL_MAP_INSERT(SYNC_UNDO_SPACE_RSEG);
517 LEVEL_MAP_INSERT(SYNC_TRX_SYS_RSEG);
518 LEVEL_MAP_INSERT(SYNC_RSEGS);
519 LEVEL_MAP_INSERT(SYNC_UNDO_SPACES);
520 LEVEL_MAP_INSERT(SYNC_UNDO_DDL);
521 LEVEL_MAP_INSERT(SYNC_TRX_UNDO);
522 LEVEL_MAP_INSERT(SYNC_PURGE_LATCH);
523 LEVEL_MAP_INSERT(SYNC_TREE_NODE);
524 LEVEL_MAP_INSERT(SYNC_TREE_NODE_FROM_HASH);
525 LEVEL_MAP_INSERT(SYNC_TREE_NODE_NEW);
526 LEVEL_MAP_INSERT(SYNC_INDEX_TREE);
527 LEVEL_MAP_INSERT(SYNC_PERSIST_DIRTY_TABLES);
528 LEVEL_MAP_INSERT(SYNC_PERSIST_AUTOINC);
529 LEVEL_MAP_INSERT(SYNC_IBUF_PESS_INSERT_MUTEX);
530 LEVEL_MAP_INSERT(SYNC_IBUF_HEADER);
531 LEVEL_MAP_INSERT(SYNC_DICT_HEADER);
532 LEVEL_MAP_INSERT(SYNC_TABLE);
533 LEVEL_MAP_INSERT(SYNC_STATS_AUTO_RECALC);
534 LEVEL_MAP_INSERT(SYNC_DICT_AUTOINC_MUTEX);
535 LEVEL_MAP_INSERT(SYNC_DICT);
536 LEVEL_MAP_INSERT(SYNC_AHI_ENABLED);
537 LEVEL_MAP_INSERT(SYNC_PARSER);
538 LEVEL_MAP_INSERT(SYNC_FTS_CACHE);
539 LEVEL_MAP_INSERT(SYNC_DICT_OPERATION);
540 LEVEL_MAP_INSERT(SYNC_TRX_I_S_LAST_READ);
541 LEVEL_MAP_INSERT(SYNC_TRX_I_S_RWLOCK);
542 LEVEL_MAP_INSERT(SYNC_LEVEL_VARYING);
543 LEVEL_MAP_INSERT(SYNC_NO_ORDER_CHECK);
544
545 /* Enum count starts from 0 */
546 ut_ad(m_levels.size() == SYNC_LEVEL_MAX + 1);
547 }
548
549 /** Print the latches acquired by a thread
550 @param[in] latches Latches acquired by a thread */
551 void LatchDebug::print_latches(const Latches *latches) const UNIV_NOTHROW {
552 #ifdef UNIV_NO_ERR_MSGS
553 ib::error()
554 #else
555 ib::error(ER_IB_MSG_1161)
556 #endif /* UNIV_NO_ERR_MSGS */
557 << "Latches already owned by this thread: ";
558
559 Latches::const_iterator end = latches->end();
560
561 for (Latches::const_iterator it = latches->begin(); it != end; ++it) {
562 #ifdef UNIV_NO_ERR_MSGS
563 ib::error()
564 #else
565 ib::error(ER_IB_MSG_1162)
566 #endif /* UNIV_NO_ERR_MSGS */
567 << sync_latch_get_name(it->m_latch->get_id()) << " -> " << it->m_level
568 << " "
569 << "(" << get_level_name(it->m_level) << ")";
570 }
571 }
572
573 /** Report error and abort
574 @param[in] latches thread's existing latches
575 @param[in] latched The existing latch causing the invariant to fail
576 @param[in] level The new level request that breaks the order */
577 void LatchDebug::crash(const Latches *latches, const Latched *latched,
578 latch_level_t level) const UNIV_NOTHROW {
579 const latch_t *latch = latched->m_latch;
580 const std::string &in_level_name = get_level_name(level);
581
582 const std::string &latch_level_name = get_level_name(latched->m_level);
583
584 #ifdef UNIV_NO_ERR_MSGS
585 ib::error()
586 #else
587 ib::error(ER_IB_MSG_1163)
588 #endif /* UNIV_NO_ERR_MSGS */
589 << "Thread " << to_string(std::this_thread::get_id())
590 << " already owns a latch " << sync_latch_get_name(latch->m_id)
591 << " at level"
592 << " " << latched->m_level << " (" << latch_level_name
593 << " ), which is at a lower/same level than the"
594 << " requested latch: " << level << " (" << in_level_name << "). "
595 << latch->to_string();
596
597 print_latches(latches);
598
599 ut_error;
600 }
601
602 const Latched *LatchDebug::find_lower_or_equal(
603 const Latches *latches, latch_level_t limit) const UNIV_NOTHROW {
604 Latches::const_iterator end = latches->end();
605
606 for (Latches::const_iterator it = latches->begin(); it != end; ++it) {
607 if (it->m_level <= limit) {
608 return (&(*it));
609 }
610 }
611
612 return (nullptr);
613 }
614
615 void LatchDebug::assert_all_held_are_above(
616 const Latches *latches, latch_level_t requested_level,
617 ulint forbidden_level) const UNIV_NOTHROW {
618 latch_level_t level = latch_level_t(forbidden_level);
619
620 ut_ad(level < SYNC_LEVEL_MAX);
621
622 const Latched *latched = find_lower_or_equal(latches, level);
623
624 if (latched != nullptr) {
625 crash(latches, latched, requested_level);
626 }
627 }
628
629 /** Create a new instance if one doesn't exist else return the existing one.
630 @param[in] add add an empty entry if one is not found
631 (default no)
632 @return pointer to a thread's acquired latches. */
633 Latches *LatchDebug::thread_latches(bool add) UNIV_NOTHROW {
634 m_mutex.enter();
635 auto mutex_guard = create_scope_guard([this]() { m_mutex.exit(); });
636
637 auto thread_id = std::this_thread::get_id();
638 ThreadMap::iterator lb = m_threads.lower_bound(thread_id);
639
640 if (lb != m_threads.end() && !(m_threads.key_comp()(thread_id, lb->first))) {
641 Latches *latches = lb->second;
642
643 return (latches);
644
645 } else if (!add) {
646 return (nullptr);
647
648 } else {
649 typedef ThreadMap::value_type value_type;
650
651 Latches *latches = ut::new_withkey<Latches>(UT_NEW_THIS_FILE_PSI_KEY);
652
653 ut_a(latches != nullptr);
654
655 latches->reserve(32);
656
657 m_threads.insert(lb, value_type(thread_id, latches));
658
659 return (latches);
660 }
661 }
662
663 /** Checks if the level value exists in the thread's acquired latches.
664 @param[in] latches the thread's existing (acquired) latches
665 @param[in] level to lookup
666 @return latch if found or 0 */
667 const latch_t *LatchDebug::find(const Latches *latches,
668 latch_level_t level) const UNIV_NOTHROW {
669 Latches::const_iterator end = latches->end();
670
671 for (Latches::const_iterator it = latches->begin(); it != end; ++it) {
672 if (it->m_level == level) {
673 return (it->m_latch);
674 }
675 }
676
677 return (nullptr);
678 }
679
680 /** Checks if the level value exists in the thread's acquired latches.
681 @param[in] level The level to lookup
682 @return latch if found or NULL */
683 const latch_t *LatchDebug::find(latch_level_t level) UNIV_NOTHROW {
684 return (find(thread_latches(), level));
685 }
686
687 /**
688 Adds a latch and its level in the thread level array. Allocates the memory
689 for the array if called first time for this OS thread. Makes the checks
690 against other latch levels stored in the array for this thread.
691 @param[in] latch pointer to a mutex or an rw-lock
692 @param[in] level level in the latching order
693 @return the thread's latches */
694 Latches *LatchDebug::check_order(const latch_t *latch,
695 latch_level_t level) UNIV_NOTHROW {
696 ut_ad(latch->get_level() != SYNC_LEVEL_VARYING);
697
698 Latches *latches = thread_latches(true);
699
700 /* NOTE that there is a problem with _NODE and _LEAF levels: if the
701 B-tree height changes, then a leaf can change to an internal node
702 or the other way around. We do not know at present if this can cause
703 unnecessary assertion failures below. */
704
705 switch (level) {
706 case SYNC_NO_ORDER_CHECK:
707 case SYNC_EXTERN_STORAGE:
708 case SYNC_TREE_NODE_FROM_HASH:
709 /* Do no order checking */
710 break;
711
712 case SYNC_LOG_SN:
713 case SYNC_LOG_SN_MUTEX:
714 case SYNC_TRX_SYS_HEADER:
715 case SYNC_LOCK_FREE_HASH:
716 case SYNC_MONITOR_MUTEX:
717 case SYNC_RECV:
718 case SYNC_RECV_WRITER:
719 case SYNC_FTS_BG_THREADS:
720 case SYNC_WORK_QUEUE:
721 case SYNC_FTS_TOKENIZE:
722 case SYNC_FTS_OPTIMIZE:
723 case SYNC_FTS_CACHE:
724 case SYNC_FTS_CACHE_INIT:
725 case SYNC_PAGE_CLEANER:
726 case SYNC_LOG_CHECKPOINTER:
727 case SYNC_LOG_CLOSER:
728 case SYNC_LOG_WRITER:
729 case SYNC_LOG_FLUSHER:
730 case SYNC_LOG_WRITE_NOTIFIER:
731 case SYNC_LOG_FLUSH_NOTIFIER:
732 case SYNC_LOG_LIMITS:
733 case SYNC_LOG_FILES:
734 case SYNC_LOG_ARCH:
735 case SYNC_PAGE_ARCH:
736 case SYNC_PAGE_ARCH_OPER:
737 case SYNC_PAGE_ARCH_CLIENT:
738 case SYNC_THREADS:
739 case SYNC_LOCK_WAIT_SYS:
740 case SYNC_TRX_SYS:
741 case SYNC_TRX_SYS_SHARD:
742 case SYNC_TRX_SYS_SERIALISATION:
743 case SYNC_IBUF_BITMAP_MUTEX:
744 case SYNC_TEMP_SPACE_RSEG:
745 case SYNC_UNDO_SPACE_RSEG:
746 case SYNC_TRX_SYS_RSEG:
747 case SYNC_RSEGS:
748 case SYNC_UNDO_SPACES:
749 case SYNC_UNDO_DDL:
750 case SYNC_TRX_UNDO:
751 case SYNC_PURGE_LATCH:
752 case SYNC_PURGE_QUEUE:
753 case SYNC_DICT_AUTOINC_MUTEX:
754 case SYNC_DICT_OPERATION:
755 case SYNC_DICT_HEADER:
756 case SYNC_TABLE:
757 case SYNC_TRX_I_S_RWLOCK:
758 case SYNC_TRX_I_S_LAST_READ:
759 case SYNC_IBUF_MUTEX:
760 case SYNC_INDEX_ONLINE_LOG:
761 case SYNC_STATS_AUTO_RECALC:
762 case SYNC_POOL:
763 case SYNC_POOL_MANAGER:
764 case SYNC_TEMP_POOL_MANAGER:
765 case SYNC_PARSER:
766 case SYNC_DICT:
767 case SYNC_AHI_ENABLED:
768 case SYNC_ALTER_STAGE:
769
770 /* This is the most typical case, in which we expect requested<held. */
771 assert_requested_is_lower_than_held(level, latches);
772 break;
773
774 case SYNC_ANY_LATCH:
775
776 /* Temporary workaround for LATCH_ID_RTR_*_MUTEX */
777 if (is_rtr_mutex(latch)) {
778 const Latched *latched = find_lower_or_equal(latches, level);
779
780 if (latched != nullptr && !is_rtr_mutex(latched->m_latch)) {
781 crash(latches, latched, level);
782 }
783
784 } else {
785 assert_requested_is_lower_than_held(level, latches);
786 }
787
788 break;
789
790 case SYNC_TRX:
791
792 /* Either the thread must own the lock_sys global latch, or
793 it is allowed to own only ONE trx_t::mutex. There are additional rules
794 for holding more than one trx_t::mutex @see trx_before_mutex_enter(). */
795
796 if (find_lower_or_equal(latches, level) != nullptr) {
797 assert_requested_is_lower_or_equal_to_held(level, latches);
798 ut_a(find(latches, SYNC_LOCK_SYS_GLOBAL) != nullptr);
799 }
800 break;
801
802 case SYNC_FIL_SHARD:
803 case SYNC_DBLWR:
804 case SYNC_BUF_CHUNKS:
805 case SYNC_BUF_FLUSH_LIST:
806 case SYNC_BUF_LRU_LIST:
807 case SYNC_BUF_FREE_LIST:
808 case SYNC_BUF_ZIP_FREE:
809 case SYNC_BUF_ZIP_HASH:
810 case SYNC_BUF_FLUSH_STATE:
811 case SYNC_RSEG_ARRAY_HEADER:
812 case SYNC_LOCK_SYS_GLOBAL:
813 case SYNC_LOCK_SYS_SHARDED:
814 case SYNC_BUF_PAGE_HASH:
815 case SYNC_BUF_BLOCK:
816 case SYNC_FSP:
817 case SYNC_SEARCH_SYS:
818
819 /* We can have multiple latches of this type therefore we
820 can only check whether the requested<=held condition holds. */
821 assert_requested_is_lower_or_equal_to_held(level, latches);
822 break;
823
824 case SYNC_IBUF_BITMAP:
825
826 /* Either the thread must own the master mutex to all
827 the bitmap pages, or it is allowed to latch only ONE
828 bitmap page. */
829
830 if (find(latches, SYNC_IBUF_BITMAP_MUTEX) != nullptr) {
831 assert_requested_is_lower_or_equal_to_held(level, latches);
832 } else {
833 assert_requested_is_lower_than_held(level, latches);
834 }
835 break;
836
837 case SYNC_FSP_PAGE:
838 ut_a(find(latches, SYNC_FSP) != nullptr);
839 break;
840
841 case SYNC_TRX_UNDO_PAGE:
842
843 /* Purge is allowed to read in as many UNDO pages as it likes.
844 The purge thread can read the UNDO pages without any covering
845 mutex. */
846
847 if (find(latches, SYNC_TRX_UNDO) == nullptr &&
848 find(latches, SYNC_TEMP_SPACE_RSEG) == nullptr &&
849 find(latches, SYNC_UNDO_SPACE_RSEG) == nullptr &&
850 find(latches, SYNC_TRX_SYS_RSEG) == nullptr) {
851 assert_requested_is_lower_or_equal_to_held(level, latches);
852 }
853 break;
854
855 case SYNC_RSEG_HEADER:
856
857 ut_a(find(latches, SYNC_TEMP_SPACE_RSEG) != nullptr ||
858 find(latches, SYNC_UNDO_SPACE_RSEG) != nullptr ||
859 find(latches, SYNC_TRX_SYS_RSEG) != nullptr);
860 break;
861
862 case SYNC_RSEG_HEADER_NEW:
863
864 ut_a(find(latches, SYNC_FSP_PAGE) != nullptr);
865 break;
866
867 case SYNC_TREE_NODE:
868
869 {
870 const latch_t *fsp_latch;
871
872 fsp_latch = find(latches, SYNC_FSP);
873
874 if ((fsp_latch == nullptr || !fsp_latch->is_temp_fsp()) &&
875 find(latches, SYNC_INDEX_TREE) == nullptr &&
876 find(latches, SYNC_DICT_OPERATION) == nullptr) {
877 assert_requested_is_lower_or_equal_to_held(level, latches);
878 }
879 }
880
881 break;
882
883 case SYNC_TREE_NODE_NEW:
884
885 ut_a(find(latches, SYNC_FSP_PAGE) != nullptr);
886 break;
887
888 case SYNC_INDEX_TREE:
889
890 assert_all_held_are_above(latches, level, SYNC_TREE_NODE - 1);
891 break;
892
893 case SYNC_IBUF_TREE_NODE:
894
895 if (find(latches, SYNC_IBUF_INDEX_TREE) == nullptr) {
896 assert_requested_is_lower_or_equal_to_held(level, latches);
897 }
898 break;
899
900 case SYNC_IBUF_TREE_NODE_NEW:
901
902 /* ibuf_add_free_page() allocates new pages for the change
903 buffer while only holding the tablespace x-latch. These
904 pre-allocated new pages may only be used while holding
905 ibuf_mutex, in btr_page_alloc_for_ibuf(). */
906
907 ut_a(find(latches, SYNC_IBUF_MUTEX) != nullptr ||
908 find(latches, SYNC_FSP) != nullptr);
909 break;
910
911 case SYNC_IBUF_INDEX_TREE:
912
913 if (find(latches, SYNC_FSP) != nullptr) {
914 assert_requested_is_lower_or_equal_to_held(level, latches);
915 } else {
916 assert_all_held_are_above(latches, level, SYNC_IBUF_TREE_NODE - 1);
917 }
918 break;
919
920 case SYNC_IBUF_PESS_INSERT_MUTEX:
921
922 assert_all_held_are_above(latches, level, SYNC_FSP - 1);
923 ut_a(find(latches, SYNC_IBUF_MUTEX) == nullptr);
924 break;
925
926 case SYNC_IBUF_HEADER:
927
928 assert_all_held_are_above(latches, level, SYNC_FSP - 1);
929 ut_a(find(latches, SYNC_IBUF_MUTEX) == nullptr);
930 ut_a(find(latches, SYNC_IBUF_PESS_INSERT_MUTEX) == nullptr);
931 break;
932
933 case SYNC_PERSIST_DIRTY_TABLES:
934
935 assert_all_held_are_above(latches, level, SYNC_IBUF_MUTEX);
936 break;
937
938 case SYNC_PERSIST_AUTOINC:
939
940 assert_all_held_are_above(latches, level, SYNC_IBUF_MUTEX);
941 ut_a(find(latches, SYNC_PERSIST_DIRTY_TABLES) == nullptr);
942 break;
943
944 case SYNC_MUTEX:
945 case SYNC_UNKNOWN:
946 case SYNC_LEVEL_VARYING:
947 case RW_LOCK_X:
948 case RW_LOCK_X_WAIT:
949 case RW_LOCK_S:
950 case RW_LOCK_SX:
951 case RW_LOCK_NOT_LOCKED:
952 /* These levels should never be set for a latch. */
953 ut_error;
954 break;
955 }
956
957 return (latches);
958 }
959
960 /** Removes a latch from the thread level array if it is found there.
961 @param[in] latch The latch that was released */
962 void LatchDebug::unlock(const latch_t *latch) UNIV_NOTHROW {
963 if (latch->get_level() == SYNC_LEVEL_VARYING) {
964 // We don't have varying level mutexes
965 ut_ad(latch->m_rw_lock);
966 }
967
968 Latches *latches;
969
970 if (*latch->get_name() == '.') {
971 /* Ignore diagnostic latches, starting with '.' */
972
973 } else if ((latches = thread_latches()) != nullptr) {
974 Latches::reverse_iterator rend = latches->rend();
975
976 for (Latches::reverse_iterator it = latches->rbegin(); it != rend; ++it) {
977 if (it->m_latch != latch) {
978 continue;
979 }
980
981 Latches::iterator i = it.base();
982
983 latches->erase(--i);
984
985 /* If this thread doesn't own any more
986 latches remove from the map.
987
988 FIXME: Perhaps use the master thread
989 to do purge. Or, do it from close connection.
990 This could be expensive. */
991
992 if (latches->empty()) {
993 m_mutex.enter();
994
995 m_threads.erase(std::this_thread::get_id());
996
997 m_mutex.exit();
998
999 ut::delete_(latches);
1000 }
1001
1002 return;
1003 }
1004
1005 if (latch->get_level() != SYNC_LEVEL_VARYING) {
1006 #ifdef UNIV_NO_ERR_MSGS
1007 ib::error()
1008 #else
1009 ib::error(ER_IB_MSG_1164)
1010 #endif /* UNIV_NO_ERR_MSGS */
1011 << "Couldn't find latch " << sync_latch_get_name(latch->get_id());
1012
1013 print_latches(latches);
1014
1015 /** Must find the latch. */
1016 ut_error;
1017 }
1018 }
1019 }
1020
1021 /** Check if it is OK to acquire the latch.
1022 @param[in] latch latch type */
1023 void sync_check_lock_validate(const latch_t *latch) {
1024 if (LatchDebug::instance() != nullptr) {
1025 LatchDebug::instance()->lock_validate(latch, latch->get_level());
1026 }
1027 }
1028
1029 /** Note that the lock has been granted
1030 @param[in] latch latch type */
1031 void sync_check_lock_granted(const latch_t *latch) {
1032 if (LatchDebug::instance() != nullptr) {
1033 LatchDebug::instance()->lock_granted(latch, latch->get_level());
1034 }
1035 }
1036
1037 /** Check if it is OK to acquire the latch.
1038 @param[in] latch latch type
1039 @param[in] level the level of the mutex */
1040 void sync_check_lock(const latch_t *latch, latch_level_t level) {
1041 if (LatchDebug::instance() != nullptr) {
1042 ut_ad(latch->get_level() == SYNC_LEVEL_VARYING);
1043 ut_ad(latch->get_id() == LATCH_ID_BUF_BLOCK_LOCK);
1044
1045 LatchDebug::instance()->lock_validate(latch, level);
1046 LatchDebug::instance()->lock_granted(latch, level);
1047 }
1048 }
1049
1050 /** Check if it is OK to re-acquire the lock.
1051 @param[in] latch RW-LOCK to relock (recursive X locks) */
1052 void sync_check_relock(const latch_t *latch) {
1053 if (LatchDebug::instance() != nullptr) {
1054 LatchDebug::instance()->relock(latch);
1055 }
1056 }
1057
1058 /** Removes a latch from the thread level array if it is found there.
1059 @param[in] latch The latch to unlock */
1060 void sync_check_unlock(const latch_t *latch) {
1061 if (LatchDebug::instance() != nullptr) {
1062 LatchDebug::instance()->unlock(latch);
1063 }
1064 }
1065
1066 /** Checks if the level array for the current thread contains a
1067 mutex or rw-latch at the specified level.
1068 @param[in] level to find
1069 @return a matching latch, or NULL if not found */
1070 const latch_t *sync_check_find(latch_level_t level) {
1071 if (LatchDebug::instance() != nullptr) {
1072 return (LatchDebug::instance()->find(level));
1073 }
1074
1075 return (nullptr);
1076 }
1077
1078 /** Checks that the level array for the current thread is empty.
1079 Terminate iteration if the functor returns true.
1080 @param[in,out] functor called for each element.
1081 @return true if the functor returns true */
1082 bool sync_check_iterate(sync_check_functor_t &functor) {
1083 if (LatchDebug::instance() != nullptr) {
1084 return (LatchDebug::instance()->for_each(functor));
1085 }
1086
1087 return (false);
1088 }
1089
1090 /** Enable sync order checking.
1091
1092 Note: We don't enforce any synchronisation checks. The caller must ensure
1093 that no races can occur */
1094 void sync_check_enable() {
1095 if (!srv_sync_debug) {
1096 return;
1097 }
1098
1099 /* We should always call this before we create threads. */
1100
1101 LatchDebug::create_instance();
1102 }
1103
1104 /** Initialise the debug data structures */
1105 void LatchDebug::init() UNIV_NOTHROW {
1106 ut_a(rw_lock_debug_event == nullptr);
1107
1108 mutex_create(LATCH_ID_RW_LOCK_DEBUG, &rw_lock_debug_mutex);
1109
1110 rw_lock_debug_event = os_event_create();
1111
1112 rw_lock_debug_waiters.store(false, std::memory_order_relaxed);
1113 }
1114
1115 /** Shutdown the latch debug checking
1116
1117 Note: We don't enforce any synchronisation checks. The caller must ensure
1118 that no races can occur */
1119 void LatchDebug::shutdown() UNIV_NOTHROW {
1120 ut_a(rw_lock_debug_event != nullptr);
1121
1122 os_event_destroy(rw_lock_debug_event);
1123
1124 rw_lock_debug_event = nullptr;
1125
1126 mutex_free(&rw_lock_debug_mutex);
1127
1128 ut_a(s_initialized);
1129
1130 s_initialized = false;
1131
1132 if (instance() == nullptr) {
1133 return;
1134 }
1135
1136 ut::delete_(s_instance);
1137
1138 LatchDebug::s_instance = nullptr;
1139 }
1140
1141 /** Acquires the debug mutex. We cannot use the mutex defined in sync0sync,
1142 because the debug mutex is also acquired in sync0arr while holding the OS
1143 mutex protecting the sync array, and the ordinary mutex_enter might
1144 recursively call routines in sync0arr, leading to a deadlock on the OS
1145 mutex. */
1146 void rw_lock_debug_mutex_enter() {
1147 for (;;) {
1148 if (0 == mutex_enter_nowait(&rw_lock_debug_mutex)) {
1149 return;
1150 }
1151
1152 const auto sig_count = os_event_reset(rw_lock_debug_event);
1153 /* We need to set rw_lock_debug_waiters to true AFTER we have reset the
1154 event, and got the sig_count, as doing it in opposite order might mean that
1155 we will miss the wakeup occurring in between, and will wait forever as our
1156 latest sig_count value will indicate we are waiting for a next wakeup. */
1157 rw_lock_debug_waiters.exchange(true, std::memory_order_acq_rel);
1158 /* We need to make sure we read the state of the rw_lock_debug_mutex AFTER
1159 we have set rw_lock_debug_waiters to true. Otherwise, if we might first
1160 observe a latched mutex, then the other thread releases it without waking up
1161 anyone, because we haven't yet set rw_lock_debug_waiters to true, and then
1162 we go to os_event_wait_low() forever as there is no one to wake us up.*/
1163 if (0 == mutex_enter_nowait(&rw_lock_debug_mutex)) {
1164 return;
1165 }
1166
1167 os_event_wait_low(rw_lock_debug_event, sig_count);
1168 }
1169 }
1170
1171 /** Releases the debug mutex. */
1172 void rw_lock_debug_mutex_exit() {
1173 mutex_exit(&rw_lock_debug_mutex);
1174 /* It is crucial that we read rw_lock_debug_waiters AFTER we have released
1175 the rw_lock_debug_mutex. If we check it too soon, we might miss a thread
1176 which decided to wait on the mutex we hold just after we have checked and
1177 never wake it up.
1178 Also, we want to establish a causal relation: if this thread sees
1179 rw_lock_debug_waiters set to true, then the os_event_set() from this thread
1180 happens after the thread setting rw_lock_debug_mutex to true has obtained the
1181 sig_count from os_event_reset(). */
1182 if (rw_lock_debug_waiters.exchange(false, std::memory_order_acq_rel)) {
1183 /* We want the rw_lock_debug_waiter to be set to false BEFORE the call to
1184 os_event_set() below. Otherwise we could overwrite true set by a new waiter
1185 waiting for a new lock owner (note we have already released the mutex!) */
1186 os_event_set(rw_lock_debug_event);
1187 }
1188 }
1189 #endif /* UNIV_DEBUG */
1190
1191 /* Meta data for all the InnoDB latches. If the latch is not in recorded
1192 here then it will be be considered for deadlock checks. */
1193 LatchMetaData latch_meta;
1194
1195 /** Load the latch meta data. */
1196 static void sync_latch_meta_init() UNIV_NOTHROW {
1197 latch_meta.resize(LATCH_ID_MAX + 1);
1198
1199 /* The latches should be ordered on latch_id_t. So that we can
1200 index directly into the vector to update and fetch meta-data. */
1201
1202 LATCH_ADD_MUTEX(LOCK_FREE_HASH, SYNC_LOCK_FREE_HASH,
1203 lock_free_hash_mutex_key);
1204
1205 LATCH_ADD_MUTEX(AHI_ENABLED, SYNC_AHI_ENABLED, ahi_enabled_mutex_key);
1206
1207 LATCH_ADD_MUTEX(ALTER_STAGE, SYNC_ALTER_STAGE, alter_stage_mutex_key);
1208
1209 LATCH_ADD_MUTEX(AUTOINC, SYNC_DICT_AUTOINC_MUTEX, autoinc_mutex_key);
1210
1211 LATCH_ADD_MUTEX(DDL_AUTOINC, SYNC_NO_ORDER_CHECK, ddl_autoinc_mutex_key);
1212
1213 #ifdef PFS_SKIP_BUFFER_MUTEX_RWLOCK
1214 LATCH_ADD_MUTEX(BUF_BLOCK_MUTEX, SYNC_BUF_BLOCK, PFS_NOT_INSTRUMENTED);
1215 #else
1216 LATCH_ADD_MUTEX(BUF_BLOCK_MUTEX, SYNC_BUF_BLOCK, buffer_block_mutex_key);
1217 #endif /* PFS_SKIP_BUFFER_MUTEX_RWLOCK */
1218 LATCH_ADD_MUTEX(BUF_POOL_CHUNKS, SYNC_BUF_CHUNKS, buf_pool_chunks_mutex_key);
1219
1220 LATCH_ADD_MUTEX(BUF_POOL_LRU_LIST, SYNC_BUF_LRU_LIST,
1221 buf_pool_LRU_list_mutex_key);
1222
1223 LATCH_ADD_MUTEX(BUF_POOL_FREE_LIST, SYNC_BUF_FREE_LIST,
1224 buf_pool_free_list_mutex_key);
1225
1226 LATCH_ADD_MUTEX(BUF_POOL_ZIP_FREE, SYNC_BUF_ZIP_FREE,
1227 buf_pool_zip_free_mutex_key);
1228
1229 LATCH_ADD_MUTEX(BUF_POOL_ZIP_HASH, SYNC_BUF_ZIP_HASH,
1230 buf_pool_zip_hash_mutex_key);
1231
1232 LATCH_ADD_MUTEX(BUF_POOL_FLUSH_STATE, SYNC_BUF_FLUSH_STATE,
1233 buf_pool_flush_state_mutex_key);
1234
1235 LATCH_ADD_MUTEX(BUF_POOL_ZIP, SYNC_BUF_BLOCK, buf_pool_zip_mutex_key);
1236
1237 LATCH_ADD_MUTEX(DICT_FOREIGN_ERR, SYNC_NO_ORDER_CHECK,
1238 dict_foreign_err_mutex_key);
1239
1240 LATCH_ADD_MUTEX(DICT_PERSIST_DIRTY_TABLES, SYNC_PERSIST_DIRTY_TABLES,
1241 dict_persist_dirty_tables_mutex_key);
1242
1243 LATCH_ADD_MUTEX(PERSIST_AUTOINC, SYNC_PERSIST_AUTOINC,
1244 autoinc_persisted_mutex_key);
1245
1246 LATCH_ADD_MUTEX(DICT_SYS, SYNC_DICT, dict_sys_mutex_key);
1247
1248 LATCH_ADD_MUTEX(DICT_TABLE, SYNC_TABLE, dict_table_mutex_key);
1249
1250 LATCH_ADD_MUTEX(PARSER, SYNC_PARSER, parser_mutex_key);
1251
1252 LATCH_ADD_MUTEX(FIL_SHARD, SYNC_FIL_SHARD, fil_system_mutex_key);
1253
1254 LATCH_ADD_MUTEX(FLUSH_LIST, SYNC_BUF_FLUSH_LIST, flush_list_mutex_key);
1255
1256 LATCH_ADD_MUTEX(FTS_BG_THREADS, SYNC_FTS_BG_THREADS,
1257 fts_bg_threads_mutex_key);
1258
1259 LATCH_ADD_MUTEX(FTS_DELETE, SYNC_FTS_OPTIMIZE, fts_delete_mutex_key);
1260
1261 LATCH_ADD_MUTEX(FTS_OPTIMIZE, SYNC_FTS_OPTIMIZE, fts_optimize_mutex_key);
1262
1263 LATCH_ADD_MUTEX(FTS_DOC_ID, SYNC_FTS_OPTIMIZE, fts_doc_id_mutex_key);
1264
1265 LATCH_ADD_MUTEX(FTS_PLL_TOKENIZE, SYNC_FTS_TOKENIZE,
1266 fts_pll_tokenize_mutex_key);
1267
1268 LATCH_ADD_MUTEX(HASH_TABLE_MUTEX, SYNC_BUF_PAGE_HASH, hash_table_mutex_key);
1269
1270 LATCH_ADD_MUTEX(IBUF_BITMAP, SYNC_IBUF_BITMAP_MUTEX, ibuf_bitmap_mutex_key);
1271
1272 LATCH_ADD_MUTEX(IBUF, SYNC_IBUF_MUTEX, ibuf_mutex_key);
1273
1274 LATCH_ADD_MUTEX(IBUF_PESSIMISTIC_INSERT, SYNC_IBUF_PESS_INSERT_MUTEX,
1275 ibuf_pessimistic_insert_mutex_key);
1276
1277 LATCH_ADD_MUTEX(LOG_CHECKPOINTER, SYNC_LOG_CHECKPOINTER,
1278 log_checkpointer_mutex_key);
1279
1280 LATCH_ADD_MUTEX(LOG_CLOSER, SYNC_LOG_CLOSER, log_closer_mutex_key);
1281
1282 LATCH_ADD_MUTEX(LOG_WRITER, SYNC_LOG_WRITER, log_writer_mutex_key);
1283
1284 LATCH_ADD_MUTEX(LOG_FLUSHER, SYNC_LOG_FLUSHER, log_flusher_mutex_key);
1285
1286 LATCH_ADD_MUTEX(LOG_WRITE_NOTIFIER, SYNC_LOG_WRITE_NOTIFIER,
1287 log_write_notifier_mutex_key);
1288
1289 LATCH_ADD_MUTEX(LOG_FLUSH_NOTIFIER, SYNC_LOG_FLUSH_NOTIFIER,
1290 log_flush_notifier_mutex_key);
1291
1292 LATCH_ADD_MUTEX(LOG_LIMITS, SYNC_LOG_LIMITS, log_limits_mutex_key);
1293
1294 LATCH_ADD_MUTEX(LOG_FILES, SYNC_LOG_FILES, log_files_mutex_key);
1295
1296 LATCH_ADD_RWLOCK(LOG_SN, SYNC_LOG_SN, log_sn_lock_key);
1297
1298 LATCH_ADD_MUTEX(LOG_SN_MUTEX, SYNC_LOG_SN_MUTEX, log_sn_mutex_key);
1299
1300 LATCH_ADD_MUTEX(LOG_ARCH, SYNC_LOG_ARCH, log_sys_arch_mutex_key);
1301
1302 LATCH_ADD_MUTEX(PAGE_ARCH, SYNC_PAGE_ARCH, page_sys_arch_mutex_key);
1303
1304 LATCH_ADD_MUTEX(PAGE_ARCH_OPER, SYNC_PAGE_ARCH_OPER,
1305 page_sys_arch_oper_mutex_key);
1306
1307 LATCH_ADD_MUTEX(PAGE_ARCH_CLIENT, SYNC_PAGE_ARCH_CLIENT,
1308 page_sys_arch_client_mutex_key);
1309
1310 LATCH_ADD_MUTEX(PAGE_CLEANER, SYNC_PAGE_CLEANER, page_cleaner_mutex_key);
1311
1312 LATCH_ADD_MUTEX(PURGE_SYS_PQ, SYNC_PURGE_QUEUE, purge_sys_pq_mutex_key);
1313
1314 LATCH_ADD_MUTEX(RECALC_POOL, SYNC_STATS_AUTO_RECALC, recalc_pool_mutex_key);
1315
1316 LATCH_ADD_MUTEX(RECV_SYS, SYNC_RECV, recv_sys_mutex_key);
1317
1318 LATCH_ADD_MUTEX(RECV_WRITER, SYNC_RECV_WRITER, recv_writer_mutex_key);
1319
1320 LATCH_ADD_MUTEX(TEMP_SPACE_RSEG, SYNC_TEMP_SPACE_RSEG,
1321 temp_space_rseg_mutex_key);
1322
1323 LATCH_ADD_MUTEX(UNDO_SPACE_RSEG, SYNC_UNDO_SPACE_RSEG,
1324 undo_space_rseg_mutex_key);
1325
1326 LATCH_ADD_MUTEX(TRX_SYS_RSEG, SYNC_TRX_SYS_RSEG, trx_sys_rseg_mutex_key);
1327
1328 #ifdef UNIV_DEBUG
1329 /* Mutex names starting with '.' are not tracked. They are assumed
1330 to be diagnostic mutexes used in debugging. */
1331 latch_meta[LATCH_ID_RW_LOCK_DEBUG] = LATCH_ADD_MUTEX(
1332 RW_LOCK_DEBUG, SYNC_NO_ORDER_CHECK, rw_lock_debug_mutex_key);
1333 #endif /* UNIV_DEBUG */
1334
1335 LATCH_ADD_MUTEX(RTR_SSN_MUTEX, SYNC_ANY_LATCH, rtr_ssn_mutex_key);
1336
1337 LATCH_ADD_MUTEX(RTR_ACTIVE_MUTEX, SYNC_ANY_LATCH, rtr_active_mutex_key);
1338
1339 LATCH_ADD_MUTEX(RTR_MATCH_MUTEX, SYNC_ANY_LATCH, rtr_match_mutex_key);
1340
1341 LATCH_ADD_MUTEX(RTR_PATH_MUTEX, SYNC_ANY_LATCH, rtr_path_mutex_key);
1342
1343 LATCH_ADD_MUTEX(RW_LOCK_LIST, SYNC_NO_ORDER_CHECK, rw_lock_list_mutex_key);
1344
1345 LATCH_ADD_MUTEX(SRV_INNODB_MONITOR, SYNC_NO_ORDER_CHECK,
1346 srv_innodb_monitor_mutex_key);
1347
1348 LATCH_ADD_MUTEX(SRV_MISC_TMPFILE, SYNC_ANY_LATCH, srv_misc_tmpfile_mutex_key);
1349
1350 LATCH_ADD_MUTEX(SRV_MONITOR_FILE, SYNC_NO_ORDER_CHECK,
1351 srv_monitor_file_mutex_key);
1352
1353 #ifdef UNIV_DEBUG
1354 LATCH_ADD_MUTEX(SYNC_THREAD, SYNC_NO_ORDER_CHECK, sync_thread_mutex_key);
1355 #else
1356 LATCH_ADD_MUTEX(SYNC_THREAD, SYNC_NO_ORDER_CHECK, PFS_NOT_INSTRUMENTED);
1357 #endif /* UNIV_DEBUG */
1358
1359 LATCH_ADD_MUTEX(TRX_UNDO, SYNC_TRX_UNDO, trx_undo_mutex_key);
1360
1361 LATCH_ADD_MUTEX(TRX_POOL, SYNC_POOL, trx_pool_mutex_key);
1362
1363 LATCH_ADD_MUTEX(TRX_POOL_MANAGER, SYNC_POOL_MANAGER,
1364 trx_pool_manager_mutex_key);
1365
1366 LATCH_ADD_MUTEX(TEMP_POOL_MANAGER, SYNC_TEMP_POOL_MANAGER,
1367 temp_pool_manager_mutex_key);
1368
1369 LATCH_ADD_MUTEX(TRX, SYNC_TRX, trx_mutex_key);
1370
1371 LATCH_ADD_MUTEX(LOCK_SYS_PAGE, SYNC_LOCK_SYS_SHARDED,
1372 lock_sys_page_mutex_key);
1373
1374 LATCH_ADD_MUTEX(LOCK_SYS_TABLE, SYNC_LOCK_SYS_SHARDED,
1375 lock_sys_table_mutex_key);
1376
1377 LATCH_ADD_MUTEX(LOCK_SYS_WAIT, SYNC_LOCK_WAIT_SYS, lock_wait_mutex_key);
1378
1379 LATCH_ADD_MUTEX(TRX_SYS, SYNC_TRX_SYS, trx_sys_mutex_key);
1380
1381 LATCH_ADD_MUTEX(TRX_SYS_SHARD, SYNC_TRX_SYS_SHARD, trx_sys_shard_mutex_key);
1382
1383 LATCH_ADD_MUTEX(TRX_SYS_SERIALISATION, SYNC_TRX_SYS_SERIALISATION,
1384 trx_sys_serialisation_mutex_key);
1385
1386 LATCH_ADD_MUTEX(SRV_SYS, SYNC_THREADS, srv_sys_mutex_key);
1387
1388 LATCH_ADD_MUTEX(SRV_SYS_TASKS, SYNC_ANY_LATCH, srv_threads_mutex_key);
1389
1390 LATCH_ADD_MUTEX(PAGE_ZIP_STAT_PER_INDEX, SYNC_ANY_LATCH,
1391 page_zip_stat_per_index_mutex_key);
1392
1393 #ifndef PFS_SKIP_EVENT_MUTEX
1394 LATCH_ADD_MUTEX(EVENT_MANAGER, SYNC_NO_ORDER_CHECK, event_manager_mutex_key);
1395 LATCH_ADD_MUTEX(EVENT_MUTEX, SYNC_NO_ORDER_CHECK, event_mutex_key);
1396 #else
1397 LATCH_ADD_MUTEX(EVENT_MANAGER, SYNC_NO_ORDER_CHECK, PFS_NOT_INSTRUMENTED);
1398 LATCH_ADD_MUTEX(EVENT_MUTEX, SYNC_NO_ORDER_CHECK, PFS_NOT_INSTRUMENTED);
1399 #endif /* !PFS_SKIP_EVENT_MUTEX */
1400
1401 LATCH_ADD_MUTEX(SYNC_ARRAY_MUTEX, SYNC_NO_ORDER_CHECK, sync_array_mutex_key);
1402
1403 LATCH_ADD_MUTEX(ZIP_PAD_MUTEX, SYNC_NO_ORDER_CHECK, zip_pad_mutex_key);
1404
1405 LATCH_ADD_MUTEX(OS_AIO_READ_MUTEX, SYNC_NO_ORDER_CHECK, PFS_NOT_INSTRUMENTED);
1406
1407 LATCH_ADD_MUTEX(OS_AIO_WRITE_MUTEX, SYNC_NO_ORDER_CHECK,
1408 PFS_NOT_INSTRUMENTED);
1409
1410 LATCH_ADD_MUTEX(OS_AIO_IBUF_MUTEX, SYNC_NO_ORDER_CHECK, PFS_NOT_INSTRUMENTED);
1411
1412 LATCH_ADD_MUTEX(ROW_DROP_LIST, SYNC_NO_ORDER_CHECK, row_drop_list_mutex_key);
1413
1414 LATCH_ADD_MUTEX(INDEX_ONLINE_LOG, SYNC_INDEX_ONLINE_LOG,
1415 index_online_log_key);
1416
1417 LATCH_ADD_MUTEX(WORK_QUEUE, SYNC_WORK_QUEUE, PFS_NOT_INSTRUMENTED);
1418
1419 // Add the RW locks
1420 LATCH_ADD_RWLOCK(BTR_SEARCH, SYNC_SEARCH_SYS, btr_search_latch_key);
1421
1422 #ifndef PFS_SKIP_BUFFER_MUTEX_RWLOCK
1423 LATCH_ADD_RWLOCK(BUF_BLOCK_LOCK, SYNC_LEVEL_VARYING, buf_block_lock_key);
1424 #else
1425 LATCH_ADD_RWLOCK(BUF_BLOCK_LOCK, SYNC_LEVEL_VARYING, PFS_NOT_INSTRUMENTED);
1426 #endif /* !PFS_SKIP_BUFFER_MUTEX_RWLOCK */
1427
1428 #ifdef UNIV_DEBUG
1429 LATCH_ADD_RWLOCK(BUF_BLOCK_DEBUG, SYNC_NO_ORDER_CHECK,
1430 buf_block_debug_latch_key);
1431 #else
1432 LATCH_ADD_RWLOCK(BUF_BLOCK_DEBUG, SYNC_NO_ORDER_CHECK, PFS_NOT_INSTRUMENTED);
1433 #endif /* UNIV_DEBUG */
1434
1435 LATCH_ADD_RWLOCK(DICT_OPERATION, SYNC_DICT_OPERATION,
1436 dict_operation_lock_key);
1437
1438 LATCH_ADD_RWLOCK(RSEGS, SYNC_RSEGS, rsegs_lock_key);
1439
1440 LATCH_ADD_RWLOCK(LOCK_SYS_GLOBAL, SYNC_LOCK_SYS_GLOBAL,
1441 lock_sys_global_rw_lock_key);
1442
1443 LATCH_ADD_RWLOCK(UNDO_SPACES, SYNC_UNDO_SPACES, undo_spaces_lock_key);
1444
1445 LATCH_ADD_MUTEX(UNDO_DDL, SYNC_UNDO_DDL, PFS_NOT_INSTRUMENTED);
1446
1447 LATCH_ADD_RWLOCK(FIL_SPACE, SYNC_FSP, fil_space_latch_key);
1448
1449 LATCH_ADD_RWLOCK(FTS_CACHE, SYNC_FTS_CACHE, fts_cache_rw_lock_key);
1450
1451 LATCH_ADD_RWLOCK(FTS_CACHE_INIT, SYNC_FTS_CACHE_INIT,
1452 fts_cache_init_rw_lock_key);
1453
1454 LATCH_ADD_RWLOCK(TRX_I_S_CACHE, SYNC_TRX_I_S_RWLOCK, trx_i_s_cache_lock_key);
1455
1456 LATCH_ADD_RWLOCK(TRX_PURGE, SYNC_PURGE_LATCH, trx_purge_latch_key);
1457
1458 LATCH_ADD_RWLOCK(IBUF_INDEX_TREE, SYNC_IBUF_INDEX_TREE,
1459 index_tree_rw_lock_key);
1460
1461 LATCH_ADD_RWLOCK(INDEX_TREE, SYNC_INDEX_TREE, index_tree_rw_lock_key);
1462
1463 LATCH_ADD_RWLOCK(DICT_TABLE_STATS, SYNC_INDEX_TREE, dict_table_stats_key);
1464
1465 LATCH_ADD_RWLOCK(HASH_TABLE_RW_LOCK, SYNC_BUF_PAGE_HASH,
1466 hash_table_locks_key);
1467
1468 LATCH_ADD_RWLOCK(SYNC_DEBUG_MUTEX, SYNC_NO_ORDER_CHECK, PFS_NOT_INSTRUMENTED);
1469
1470 LATCH_ADD_MUTEX(FILE_OPEN, SYNC_NO_ORDER_CHECK, file_open_mutex_key);
1471
1472 LATCH_ADD_MUTEX(MASTER_KEY_ID_MUTEX, SYNC_NO_ORDER_CHECK,
1473 master_key_id_mutex_key);
1474
1475 LATCH_ADD_MUTEX(CLONE_SYS, SYNC_NO_ORDER_CHECK, clone_sys_mutex_key);
1476
1477 LATCH_ADD_MUTEX(CLONE_TASK, SYNC_NO_ORDER_CHECK, clone_task_mutex_key);
1478
1479 LATCH_ADD_MUTEX(CLONE_SNAPSHOT, SYNC_NO_ORDER_CHECK,
1480 clone_snapshot_mutex_key);
1481
1482 LATCH_ADD_MUTEX(PARALLEL_READ, SYNC_NO_ORDER_CHECK, parallel_read_mutex_key);
1483
1484 LATCH_ADD_MUTEX(REDO_LOG_ARCHIVE_ADMIN_MUTEX, SYNC_NO_ORDER_CHECK,
1485 PFS_NOT_INSTRUMENTED);
1486
1487 LATCH_ADD_MUTEX(REDO_LOG_ARCHIVE_QUEUE_MUTEX, SYNC_NO_ORDER_CHECK,
1488 PFS_NOT_INSTRUMENTED);
1489
1490 LATCH_ADD_MUTEX(DBLWR, SYNC_DBLWR, dblwr_mutex_key);
1491
1492 LATCH_ADD_MUTEX(TEST_MUTEX, SYNC_NO_ORDER_CHECK, PFS_NOT_INSTRUMENTED);
1493
1494 latch_id_t id = LATCH_ID_NONE;
1495
1496 /* The array should be ordered on latch ID.We need to
1497 index directly into it from the mutex policy to update
1498 the counters and access the meta-data. */
1499
1500 LatchMetaData::iterator it = latch_meta.begin();
1501
1502 /* Skip the first entry, it is always NULL (LATCH_ID_NONE) */
1503
1504 for (++it; it != latch_meta.end(); ++it) {
1505 const latch_meta_t *meta = *it;
1506
1507 /* Debug latches will be missing */
1508
1509 if (meta == nullptr) {
1510 continue;
1511 }
1512
1513 ut_a(meta->get_id() != LATCH_ID_NONE);
1514 ut_a(id < meta->get_id());
1515
1516 id = meta->get_id();
1517 }
1518 }
1519
1520 /** Destroy the latch meta data */
1521 static void sync_latch_meta_destroy() {
1522 for (LatchMetaData::iterator it = latch_meta.begin(); it != latch_meta.end();
1523 ++it) {
1524 ut::delete_(*it);
1525 }
1526
1527 latch_meta.clear();
1528 }
1529
1530 /** Track mutex file creation name and line number. This is to avoid storing
1531 { const char* name; uint16_t line; } in every instance. This results in the
1532 sizeof(Mutex) > 64. We use a lookup table to store it separately. Fetching
1533 the values is very rare, only required for diagnostic purposes. And, we
1534 don't create/destroy mutexes that frequently. */
1535 struct CreateTracker {
1536 /** Constructor */
1537 CreateTracker() UNIV_NOTHROW { m_mutex.init(); }
1538
1539 /** Destructor */
1540 ~CreateTracker() UNIV_NOTHROW {
1541 ut_ad(m_files.empty());
1542
1543 m_mutex.destroy();
1544 }
1545
1546 /** Register where the latch was created
1547 @param[in] ptr Latch instance
1548 @param[in] filename Where created
1549 @param[in] line Line number in filename */
1550 void register_latch(const void *ptr, const char *filename,
1551 uint16_t line) UNIV_NOTHROW {
1552 m_mutex.enter();
1553
1554 Files::iterator lb = m_files.lower_bound(ptr);
1555
1556 ut_ad(lb == m_files.end() || m_files.key_comp()(ptr, lb->first));
1557
1558 typedef Files::value_type value_type;
1559
1560 m_files.insert(lb, value_type(ptr, File(filename, line)));
1561
1562 m_mutex.exit();
1563 }
1564
1565 /** Deregister a latch - when it is destroyed
1566 @param[in] ptr Latch instance being destroyed */
1567 void deregister_latch(const void *ptr) UNIV_NOTHROW {
1568 m_mutex.enter();
1569
1570 Files::iterator lb = m_files.lower_bound(ptr);
1571
1572 ut_ad(lb != m_files.end() && !(m_files.key_comp()(ptr, lb->first)));
1573
1574 m_files.erase(lb);
1575
1576 m_mutex.exit();
1577 }
1578
1579 /** Get the create string, format is "name:line"
1580 @param[in] ptr Latch instance
1581 @return the create string or "" if not found */
1582 std::string get(const void *ptr) UNIV_NOTHROW {
1583 m_mutex.enter();
1584
1585 std::string created;
1586
1587 Files::iterator lb = m_files.lower_bound(ptr);
1588
1589 if (lb != m_files.end() && !(m_files.key_comp()(ptr, lb->first))) {
1590 std::ostringstream msg;
1591
1592 msg << lb->second.m_name << ":" << lb->second.m_line;
1593
1594 created = msg.str();
1595 }
1596
1597 m_mutex.exit();
1598
1599 return (created);
1600 }
1601
1602 private:
1603 /** For tracking the filename and line number */
1604 struct File {
1605 /** Constructor */
1606 File() UNIV_NOTHROW : m_name(), m_line() {}
1607
1608 /** Constructor
1609 @param[in] name Filename where created
1610 @param[in] line Line number where created */
1611 File(const char *name, uint16_t line) UNIV_NOTHROW
1612 : m_name(sync_basename(name)),
1613 m_line(line) {
1614 /* No op */
1615 }
1616
1617 /** Filename where created */
1618 std::string m_name;
1619
1620 /** Line number where created */
1621 uint16_t m_line;
1622 };
1623
1624 /** Map the mutex instance to where it was created */
1625 typedef std::map<const void *, File, std::less<const void *>,
1626 ut::allocator<std::pair<const void *const, File>>>
1627 Files;
1628
1629 typedef OSMutex Mutex;
1630
1631 /** Mutex protecting m_files */
1632 Mutex m_mutex;
1633
1634 /** Track the latch creation */
1635 Files m_files;
1636 };
1637
1638 /** Track latch creation location. For reducing the size of the latches */
1639 static CreateTracker *create_tracker;
1640
1641 /** Register a latch, called when it is created
1642 @param[in] ptr Latch instance that was created
1643 @param[in] filename Filename where it was created
1644 @param[in] line Line number in filename */
1645 void sync_file_created_register(const void *ptr, const char *filename,
1646 uint16_t line) {
1647 create_tracker->register_latch(ptr, filename, line);
1648 }
1649
1650 /** Deregister a latch, called when it is destroyed
1651 @param[in] ptr Latch to be destroyed */
1652 void sync_file_created_deregister(const void *ptr) {
1653 create_tracker->deregister_latch(ptr);
1654 }
1655
1656 /** Get the string where the file was created. Its format is "name:line"
1657 @param[in] ptr Latch instance
1658 @return created information or "" if can't be found */
1659 std::string sync_file_created_get(const void *ptr) {
1660 return (create_tracker->get(ptr));
1661 }
1662
1663 /** Initializes the synchronization data structures.
1664 @param[in] max_threads Maximum threads that can be created. */
1665 void sync_check_init(size_t max_threads) {
1666 ut_ad(!LatchDebug::s_initialized);
1667 ut_d(LatchDebug::s_initialized = true);
1668
1669 /** For collecting latch statistic - SHOW ... MUTEX */
1670 mutex_monitor = ut::new_withkey<MutexMonitor>(UT_NEW_THIS_FILE_PSI_KEY);
1671
1672 /** For trcking mutex creation location */
1673 create_tracker = ut::new_withkey<CreateTracker>(UT_NEW_THIS_FILE_PSI_KEY);
1674
1675 sync_latch_meta_init();
1676
1677 /* Init the mutex list and create the mutex to protect it. */
1678
1679 mutex_create(LATCH_ID_RW_LOCK_LIST, &rw_lock_list_mutex);
1680
1681 ut_d(LatchDebug::init());
1682
1683 sync_array_init(max_threads);
1684 }
1685
1686 /** Frees the resources in InnoDB's own synchronization data structures. Use
1687 os_sync_free() after calling this. */
1688 void sync_check_close() {
1689 ut_d(LatchDebug::shutdown());
1690
1691 mutex_free(&rw_lock_list_mutex);
1692
1693 sync_array_close();
1694
1695 ut::delete_(mutex_monitor);
1696
1697 mutex_monitor = nullptr;
1698
1699 ut::delete_(create_tracker);
1700
1701 create_tracker = nullptr;
1702
1703 sync_latch_meta_destroy();
1704 }
1705
1706 #ifdef UNIV_DEBUG
1707 std::mutex Sync_point::s_mutex{};
1708 Sync_point::Sync_points Sync_point::s_sync_points{};
1709
1710 void Sync_point::add(const THD *thd, const std::string &target) noexcept {
1711 const std::lock_guard<std::mutex> lock(s_mutex);
1712
1713 auto r1 = std::find_if(
1714 std::begin(s_sync_points), std::end(s_sync_points),
1715 [=](const Sync_point &sync_point) { return thd == sync_point.m_thd; });
1716
1717 if (r1 != s_sync_points.end()) {
1718 const auto &b = std::begin(r1->m_targets);
1719 const auto &e = std::end(r1->m_targets);
1720 const auto r2 = std::find(b, e, target);
1721
1722 if (r2 == e) {
1723 r1->m_targets.push_back(target);
1724 }
1725 } else {
1726 s_sync_points.push_back(Sync_point{thd});
1727 s_sync_points.back().m_targets.push_back(target);
1728 }
1729 }
1730
1731 bool Sync_point::enabled(const THD *thd, const std::string &target) noexcept {
1732 const std::lock_guard<std::mutex> lock(s_mutex);
1733
1734 auto r1 = std::find_if(
1735 std::begin(s_sync_points), std::end(s_sync_points),
1736 [=](const Sync_point &sync_point) { return thd == sync_point.m_thd; });
1737
1738 if (r1 == s_sync_points.end()) {
1739 return false;
1740 }
1741
1742 const auto &b = std::begin(r1->m_targets);
1743 const auto &e = std::end(r1->m_targets);
1744 const auto r2 = std::find(b, e, target);
1745
1746 return r2 != e;
1747 }
1748
1749 bool Sync_point::enabled(const std::string &target) noexcept {
1750 #ifndef UNIV_NO_ERR_MSGS
1751 return enabled(current_thd, target);
1752 #else
1753 return false;
1754 #endif /* !UNIV_NO_ERR_MSGS */
1755 }
1756
1757 void Sync_point::erase(const THD *thd, const std::string &target) noexcept {
1758 const std::lock_guard<std::mutex> lock(s_mutex);
1759
1760 auto r1 = std::find_if(
1761 std::begin(s_sync_points), std::end(s_sync_points),
1762 [=](const Sync_point &sync_point) { return thd == sync_point.m_thd; });
1763
1764 if (r1 != s_sync_points.end()) {
1765 const auto &b = std::begin(r1->m_targets);
1766 const auto &e = std::end(r1->m_targets);
1767 const auto r2 = std::find(b, e, target);
1768
1769 if (r2 != e) {
1770 r1->m_targets.erase(r2);
1771 if (r1->m_targets.empty()) {
1772 s_sync_points.erase(r1);
1773 }
1774 }
1775 }
1776 }
1777 #endif /* UNIV_DEBUG */