Debian Codegraph @

storage/innobase/include/sync0types.h

raw
1 /*****************************************************************************
2
3 Copyright (c) 1995, 2026, Oracle and/or its affiliates.
4
5 This program is free software; you can redistribute it and/or modify it under
6 the terms of the GNU General Public License, version 2.0, as published by the
7 Free Software Foundation.
8
9 This program is designed to work with certain software (including
10 but not limited to OpenSSL) that is licensed under separate terms,
11 as designated in a particular file or component or in included license
12 documentation. The authors of MySQL hereby grant you an additional
13 permission to link the program and your derivative works with the
14 separately licensed software that they have either included with
15 the program or referenced in the documentation.
16
17 This program is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
20 for more details.
21
22 You should have received a copy of the GNU General Public License along with
23 this program; if not, write to the Free Software Foundation, Inc.,
24 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
26 *****************************************************************************/
27
28 /** @file include/sync0types.h
29 Global types for sync
30
31 Created 9/5/1995 Heikki Tuuri
32 *******************************************************/
33
34 #ifndef sync0types_h
35 #define sync0types_h
36
37 #include <iostream>
38 #include <vector>
39
40 #include "sync0sync.h"
41 #include "univ.i"
42 #include "ut0counter.h"
43 #include "ut0log.h"
44 #include "ut0new.h"
45
46 #ifdef UNIV_DEBUG
47 /** Set when InnoDB has invoked exit(). */
48 extern bool innodb_calling_exit;
49 #endif /* UNIV_DEBUG */
50
51 #ifdef _WIN32
52 /** Native mutex */
53 typedef CRITICAL_SECTION sys_mutex_t;
54 #else
55 /** Native mutex */
56 typedef pthread_mutex_t sys_mutex_t;
57 #endif /* _WIN32 */
58
59 /*
60 LATCHING ORDER WITHIN THE DATABASE
61 ==================================
62
63 The mutex or latch in the central memory object, for instance, a rollback
64 segment object, must be acquired before acquiring the latch or latches to
65 the corresponding file data structure. In the latching order below, these
66 file page object latches are placed immediately below the corresponding
67 central memory object latch or mutex.
68
69 Synchronization object Notes
70 ---------------------- -----
71
72 Dictionary mutex If we have a pointer to a dictionary
73 | object, e.g., a table, it can be
74 | accessed without reserving the
75 | dictionary mutex. We must have a
76 | reservation, a memoryfix, to the
77 | appropriate table object in this case,
78 | and the table must be explicitly
79 | released later.
80 V
81 Dictionary header
82 |
83 V
84 Secondary index tree latch The tree latch protects also all
85 | the B-tree non-leaf pages. These
86 V can be read with the page only
87 Secondary index non-leaf bufferfixed to save CPU time,
88 | no s-latch is needed on the page.
89 | Modification of a page requires an
90 | x-latch on the page, however. If a
91 | thread owns an x-latch to the tree,
92 | it is allowed to latch non-leaf pages
93 | even after it has acquired the fsp
94 | latch.
95 V
96 Secondary index leaf The latch on the secondary index leaf
97 | can be kept while accessing the
98 | clustered index, to save CPU time.
99 V
100 Clustered index tree latch To increase concurrency, the tree
101 | latch is usually released when the
102 | leaf page latch has been acquired.
103 V
104 Clustered index non-leaf
105 |
106 V
107 Clustered index leaf
108 |
109 V
110 Transaction system header
111 |
112 V
113 Transaction undo mutex The undo log entry must be written
114 | before any index page is modified.
115 | Transaction undo mutex is for the undo
116 | logs the analogue of the tree latch
117 | for a B-tree. If a thread has the
118 | trx undo mutex reserved, it is allowed
119 | to latch the undo log pages in any
120 | order, and also after it has acquired
121 | the fsp latch.
122 V
123 Rollback segment mutex The rollback segment mutex must be
124 | reserved, if, e.g., a new page must
125 | be added to an undo log. The rollback
126 | segment and the undo logs in its
127 | history list can be seen as an
128 | analogue of a B-tree, and the latches
129 | reserved similarly, using a version of
130 | lock-coupling. If an undo log must be
131 | extended by a page when inserting an
132 | undo log record, this corresponds to
133 | a pessimistic insert in a B-tree.
134 V
135 Rollback segment header
136 |
137 V
138 Purge system latch
139 |
140 V
141 Undo log pages If a thread owns the trx undo mutex,
142 | or for a log in the history list, the
143 | rseg mutex, it is allowed to latch
144 | undo log pages in any order, and even
145 | after it has acquired the fsp latch.
146 | If a thread does not have the
147 | appropriate mutex, it is allowed to
148 | latch only a single undo log page in
149 | a mini-transaction.
150 V
151 File space management latch If a mini-transaction must allocate
152 | several file pages, it can do that,
153 | because it keeps the x-latch to the
154 | file space management in its memo.
155 V
156 File system pages
157 |
158 V
159 lock_sys_wait_mutex Mutex protecting lock timeout data
160 |
161 V
162 lock_sys->global_sharded_latch Sharded rw-latch protecting lock_sys_t
163 |
164 V
165 lock_sys->table_mutexes Mutexes protecting lock_sys_t table
166 | lock queues
167 |
168 V
169 lock_sys->page_mutexes Mutexes protecting lock_sys_t page
170 | lock queues
171 |
172 V
173 trx_sys->mutex Mutex protecting trx_sys_t
174 |
175 V
176 Threads mutex Background thread scheduling mutex
177 |
178 V
179 query_thr_mutex Mutex protecting query threads
180 |
181 V
182 trx_mutex Mutex protecting trx_t fields
183 |
184 V
185 Search system mutex
186 |
187 V
188 Buffer pool mutexes
189 |
190 V
191 Log mutex
192 |
193 Any other latch
194 |
195 V
196 Memory pool mutex */
197
198 /** Latching order levels. If you modify these, you have to also update
199 LatchDebug internals in sync0debug.cc */
200
201 enum latch_level_t {
202 SYNC_UNKNOWN = 0,
203
204 SYNC_MUTEX = 1,
205
206 RW_LOCK_SX,
207 RW_LOCK_X_WAIT,
208 RW_LOCK_S,
209 RW_LOCK_X,
210 RW_LOCK_NOT_LOCKED,
211
212 SYNC_LOCK_FREE_HASH,
213
214 SYNC_MONITOR_MUTEX,
215
216 SYNC_ANY_LATCH,
217
218 SYNC_ALTER_STAGE,
219
220 SYNC_FIL_SHARD,
221
222 SYNC_PAGE_ARCH_OPER,
223
224 SYNC_BUF_FLUSH_LIST,
225 SYNC_BUF_FLUSH_STATE,
226 SYNC_BUF_ZIP_HASH,
227 SYNC_BUF_FREE_LIST,
228 SYNC_BUF_ZIP_FREE,
229 SYNC_BUF_BLOCK,
230 SYNC_BUF_PAGE_HASH,
231 SYNC_BUF_LRU_LIST,
232 SYNC_BUF_CHUNKS,
233
234 SYNC_DBLWR,
235
236 SYNC_SEARCH_SYS,
237
238 SYNC_WORK_QUEUE,
239
240 SYNC_FTS_TOKENIZE,
241 SYNC_FTS_OPTIMIZE,
242 SYNC_FTS_BG_THREADS,
243 SYNC_FTS_CACHE_INIT,
244 SYNC_RECV,
245
246 SYNC_LOG_LIMITS,
247 SYNC_LOG_FLUSHER,
248 SYNC_LOG_FILES,
249 SYNC_LOG_WRITER,
250 SYNC_LOG_WRITE_NOTIFIER,
251 SYNC_LOG_FLUSH_NOTIFIER,
252 SYNC_LOG_CLOSER,
253 SYNC_LOG_CHECKPOINTER,
254 SYNC_LOG_SN,
255 SYNC_LOG_SN_MUTEX,
256 SYNC_PAGE_ARCH,
257 SYNC_PAGE_ARCH_CLIENT,
258 SYNC_LOG_ARCH,
259
260 SYNC_PAGE_CLEANER,
261 SYNC_TRX_SYS_HEADER,
262 SYNC_TRX_SYS_SERIALISATION,
263 SYNC_PURGE_QUEUE,
264 SYNC_THREADS,
265 SYNC_TRX,
266 SYNC_POOL,
267 SYNC_POOL_MANAGER,
268 SYNC_TRX_SYS_SHARD,
269 SYNC_TRX_SYS,
270 SYNC_LOCK_SYS_SHARDED,
271 SYNC_LOCK_SYS_GLOBAL,
272 SYNC_LOCK_WAIT_SYS,
273
274 SYNC_INDEX_ONLINE_LOG,
275
276 SYNC_IBUF_BITMAP,
277 SYNC_IBUF_BITMAP_MUTEX,
278 SYNC_IBUF_TREE_NODE,
279 SYNC_IBUF_TREE_NODE_NEW,
280 SYNC_IBUF_INDEX_TREE,
281
282 SYNC_IBUF_MUTEX,
283
284 SYNC_FSP_PAGE,
285 SYNC_FSP,
286 SYNC_TEMP_POOL_MANAGER,
287 SYNC_EXTERN_STORAGE,
288 SYNC_RSEG_ARRAY_HEADER,
289 SYNC_TRX_UNDO_PAGE,
290 SYNC_RSEG_HEADER,
291 SYNC_RSEG_HEADER_NEW,
292 SYNC_TEMP_SPACE_RSEG,
293 SYNC_UNDO_SPACE_RSEG,
294 SYNC_TRX_SYS_RSEG,
295 SYNC_TRX_UNDO,
296 SYNC_PURGE_LATCH,
297 SYNC_TREE_NODE,
298 SYNC_TREE_NODE_FROM_HASH,
299 SYNC_TREE_NODE_NEW,
300 SYNC_INDEX_TREE,
301 SYNC_RSEGS,
302 SYNC_UNDO_SPACES,
303
304 SYNC_PERSIST_DIRTY_TABLES,
305 SYNC_PERSIST_AUTOINC,
306
307 SYNC_IBUF_PESS_INSERT_MUTEX,
308 SYNC_IBUF_HEADER,
309 SYNC_DICT_HEADER,
310 SYNC_TABLE,
311 SYNC_STATS_AUTO_RECALC,
312 SYNC_DICT_AUTOINC_MUTEX,
313 SYNC_DICT,
314 SYNC_PARSER,
315 SYNC_FTS_CACHE,
316 SYNC_UNDO_DDL,
317
318 SYNC_DICT_OPERATION,
319
320 SYNC_AHI_ENABLED,
321
322 SYNC_TRX_I_S_LAST_READ,
323
324 SYNC_TRX_I_S_RWLOCK,
325
326 SYNC_RECV_WRITER,
327
328 /** Level is varying. Only used with buffer pool page locks, which
329 do not have a fixed level, but instead have their level set after
330 the page is locked; see e.g. ibuf_bitmap_get_map_page(). */
331
332 SYNC_LEVEL_VARYING,
333
334 /** This can be used to suppress order checking. */
335 SYNC_NO_ORDER_CHECK,
336
337 /** Maximum level value */
338 SYNC_LEVEL_MAX = SYNC_NO_ORDER_CHECK
339 };
340
341 /** Each latch has an ID. This id is used for creating the latch and to look
342 up its meta-data. See sync0debug.c. The order does not matter here, but
343 alphabetical ordering seems useful */
344 enum latch_id_t {
345 LATCH_ID_NONE = 0,
346 LATCH_ID_AHI_ENABLED,
347 LATCH_ID_ALTER_STAGE,
348 LATCH_ID_AUTOINC,
349 LATCH_ID_BUF_BLOCK_MUTEX,
350 LATCH_ID_BUF_POOL_CHUNKS,
351 LATCH_ID_BUF_POOL_ZIP,
352 LATCH_ID_BUF_POOL_LRU_LIST,
353 LATCH_ID_BUF_POOL_FREE_LIST,
354 LATCH_ID_BUF_POOL_ZIP_FREE,
355 LATCH_ID_BUF_POOL_ZIP_HASH,
356 LATCH_ID_BUF_POOL_FLUSH_STATE,
357 LATCH_ID_DBLWR,
358 LATCH_ID_DBLWR_SPACE_CACHE,
359 LATCH_ID_DDL_AUTOINC,
360 LATCH_ID_CACHE_LAST_READ,
361 LATCH_ID_DICT_FOREIGN_ERR,
362 LATCH_ID_DICT_SYS,
363 LATCH_ID_DICT_TABLE,
364 LATCH_ID_FIL_SHARD,
365 LATCH_ID_FLUSH_LIST,
366 LATCH_ID_FTS_BG_THREADS,
367 LATCH_ID_FTS_DELETE,
368 LATCH_ID_FTS_OPTIMIZE,
369 LATCH_ID_FTS_DOC_ID,
370 LATCH_ID_FTS_PLL_TOKENIZE,
371 LATCH_ID_HASH_TABLE_MUTEX,
372 LATCH_ID_IBUF_BITMAP,
373 LATCH_ID_IBUF,
374 LATCH_ID_IBUF_PESSIMISTIC_INSERT,
375 LATCH_ID_LOCK_FREE_HASH,
376 LATCH_ID_LOCK_SYS_GLOBAL,
377 LATCH_ID_LOCK_SYS_PAGE,
378 LATCH_ID_LOCK_SYS_TABLE,
379 LATCH_ID_LOCK_SYS_WAIT,
380 LATCH_ID_LOG_SN,
381 LATCH_ID_LOG_SN_MUTEX,
382 LATCH_ID_LOG_CHECKPOINTER,
383 LATCH_ID_LOG_CLOSER,
384 LATCH_ID_LOG_WRITER,
385 LATCH_ID_LOG_FLUSHER,
386 LATCH_ID_LOG_WRITE_NOTIFIER,
387 LATCH_ID_LOG_FLUSH_NOTIFIER,
388 LATCH_ID_LOG_LIMITS,
389 LATCH_ID_LOG_FILES,
390 LATCH_ID_PARSER,
391 LATCH_ID_LOG_ARCH,
392 LATCH_ID_PAGE_ARCH,
393 LATCH_ID_PAGE_ARCH_OPER,
394 LATCH_ID_PAGE_ARCH_CLIENT,
395 LATCH_ID_PERSIST_METADATA_BUFFER,
396 LATCH_ID_DICT_PERSIST_DIRTY_TABLES,
397 LATCH_ID_PERSIST_AUTOINC,
398 LATCH_ID_DICT_PERSIST_CHECKPOINT,
399 LATCH_ID_PAGE_CLEANER,
400 LATCH_ID_PURGE_SYS_PQ,
401 LATCH_ID_RECALC_POOL,
402 LATCH_ID_RECV_SYS,
403 LATCH_ID_RECV_WRITER,
404 LATCH_ID_TEMP_SPACE_RSEG,
405 LATCH_ID_UNDO_SPACE_RSEG,
406 LATCH_ID_TRX_SYS_RSEG,
407 LATCH_ID_RW_LOCK_DEBUG,
408 LATCH_ID_RTR_SSN_MUTEX,
409 LATCH_ID_RTR_ACTIVE_MUTEX,
410 LATCH_ID_RTR_MATCH_MUTEX,
411 LATCH_ID_RTR_PATH_MUTEX,
412 LATCH_ID_RW_LOCK_LIST,
413 LATCH_ID_SRV_INNODB_MONITOR,
414 LATCH_ID_SRV_MISC_TMPFILE,
415 LATCH_ID_SRV_MONITOR_FILE,
416 LATCH_ID_SYNC_THREAD,
417 LATCH_ID_TRX_UNDO,
418 LATCH_ID_TRX_POOL,
419 LATCH_ID_TRX_POOL_MANAGER,
420 LATCH_ID_TEMP_POOL_MANAGER,
421 LATCH_ID_TRX,
422 LATCH_ID_TRX_SYS,
423 LATCH_ID_TRX_SYS_SHARD,
424 LATCH_ID_TRX_SYS_SERIALISATION,
425 LATCH_ID_SRV_SYS,
426 LATCH_ID_SRV_SYS_TASKS,
427 LATCH_ID_PAGE_ZIP_STAT_PER_INDEX,
428 LATCH_ID_EVENT_MANAGER,
429 LATCH_ID_EVENT_MUTEX,
430 LATCH_ID_SYNC_ARRAY_MUTEX,
431 LATCH_ID_ZIP_PAD_MUTEX,
432 LATCH_ID_OS_AIO_READ_MUTEX,
433 LATCH_ID_OS_AIO_WRITE_MUTEX,
434 LATCH_ID_OS_AIO_IBUF_MUTEX,
435 LATCH_ID_ROW_DROP_LIST,
436 LATCH_ID_INDEX_ONLINE_LOG,
437 LATCH_ID_WORK_QUEUE,
438 LATCH_ID_BTR_SEARCH,
439 LATCH_ID_BUF_BLOCK_LOCK,
440 LATCH_ID_BUF_BLOCK_DEBUG,
441 LATCH_ID_DICT_OPERATION,
442 LATCH_ID_CHECKPOINT,
443 LATCH_ID_RSEGS,
444 LATCH_ID_UNDO_SPACES,
445 LATCH_ID_UNDO_DDL,
446 LATCH_ID_FIL_SPACE,
447 LATCH_ID_FTS_CACHE,
448 LATCH_ID_FTS_CACHE_INIT,
449 LATCH_ID_TRX_I_S_CACHE,
450 LATCH_ID_TRX_PURGE,
451 LATCH_ID_IBUF_INDEX_TREE,
452 LATCH_ID_INDEX_TREE,
453 LATCH_ID_DICT_TABLE_STATS,
454 LATCH_ID_HASH_TABLE_RW_LOCK,
455 LATCH_ID_BUF_CHUNK_MAP_LATCH,
456 LATCH_ID_SYNC_DEBUG_MUTEX,
457 LATCH_ID_MASTER_KEY_ID_MUTEX,
458 LATCH_ID_FILE_OPEN,
459 LATCH_ID_CLONE_SYS,
460 LATCH_ID_CLONE_TASK,
461 LATCH_ID_CLONE_SNAPSHOT,
462 LATCH_ID_PARALLEL_READ,
463 LATCH_ID_DBLR,
464 LATCH_ID_REDO_LOG_ARCHIVE_ADMIN_MUTEX,
465 LATCH_ID_REDO_LOG_ARCHIVE_QUEUE_MUTEX,
466 LATCH_ID_TEST_MUTEX,
467 LATCH_ID_MAX = LATCH_ID_TEST_MUTEX
468 };
469
470 /** OS mutex, without any policy. It is a thin wrapper around the
471 system mutexes. The interface is different from the policy mutexes,
472 to ensure that it is called directly and not confused with the
473 policy mutexes. */
474 struct OSMutex {
475 /** Constructor */
476 OSMutex() UNIV_NOTHROW { ut_d(m_freed = true); }
477
478 /** Create the mutex by calling the system functions. */
479 void init() UNIV_NOTHROW {
480 ut_ad(m_freed);
481
482 #ifdef _WIN32
483 InitializeCriticalSection((LPCRITICAL_SECTION)&m_mutex);
484 #else
485 {
486 int ret = pthread_mutex_init(&m_mutex, nullptr);
487 ut_a(ret == 0);
488 }
489 #endif /* _WIN32 */
490
491 ut_d(m_freed = false);
492 }
493
494 /** Destructor */
495 ~OSMutex() = default;
496
497 /** Destroy the mutex */
498 void destroy() UNIV_NOTHROW {
499 ut_ad(innodb_calling_exit || !m_freed);
500 #ifdef _WIN32
501 DeleteCriticalSection((LPCRITICAL_SECTION)&m_mutex);
502 #else
503 int ret;
504
505 ret = pthread_mutex_destroy(&m_mutex);
506
507 if (ret != 0) {
508 #ifdef UNIV_NO_ERR_MSGS
509 ib::error()
510 #else
511 ib::error(ER_IB_MSG_1372)
512 #endif
513 << "Return value " << ret << " when calling pthread_mutex_destroy().";
514 }
515 #endif /* _WIN32 */
516 ut_d(m_freed = true);
517 }
518
519 /** Release the mutex. */
520 void exit() UNIV_NOTHROW {
521 ut_ad(innodb_calling_exit || !m_freed);
522 #ifdef _WIN32
523 LeaveCriticalSection(&m_mutex);
524 #else
525 int ret = pthread_mutex_unlock(&m_mutex);
526 ut_a(ret == 0);
527 #endif /* _WIN32 */
528 }
529
530 /** Acquire the mutex. */
531 void enter() UNIV_NOTHROW {
532 ut_ad(innodb_calling_exit || !m_freed);
533 #ifdef _WIN32
534 EnterCriticalSection((LPCRITICAL_SECTION)&m_mutex);
535 #else
536 int ret = pthread_mutex_lock(&m_mutex);
537 ut_a(ret == 0);
538 #endif /* _WIN32 */
539 }
540
541 void lock() { enter(); }
542 void unlock() { exit(); }
543
544 /** @return true if locking succeeded */
545 bool try_lock() UNIV_NOTHROW {
546 ut_ad(innodb_calling_exit || !m_freed);
547 #ifdef _WIN32
548 return (TryEnterCriticalSection(&m_mutex) != 0);
549 #else
550 return (pthread_mutex_trylock(&m_mutex) == 0);
551 #endif /* _WIN32 */
552 }
553
554 /** Required for os_event_t */
555 operator sys_mutex_t *() UNIV_NOTHROW { return (&m_mutex); }
556
557 #if defined(UNIV_LIBRARY) && defined(UNIV_DEBUG)
558 bool is_owned() {
559 /* This should never be reached. This is
560 added to fix is_owned() compilation errors
561 for library. We will never reach here because
562 mutexes are disabled in library. */
563 ut_error;
564 return (false);
565 }
566 #endif /* UNIV_LIBRARY && UNIV_DEBUG */
567
568 private:
569 #ifdef UNIV_DEBUG
570 /** true if the mutex has been freed/destroyed. */
571 bool m_freed;
572 #endif /* UNIV_DEBUG */
573
574 sys_mutex_t m_mutex;
575 };
576
577 #ifndef UNIV_LIBRARY
578 #ifdef UNIV_PFS_MUTEX
579 /** Latch element
580 Used for mutexes which have PFS keys defined under UNIV_PFS_MUTEX.
581 @param[in] id Latch id
582 @param[in] level Latch level
583 @param[in] key PFS key */
584 #define LATCH_ADD_MUTEX(id, level, key) \
585 latch_meta[LATCH_ID_##id] = ut::new_withkey<latch_meta_t>( \
586 UT_NEW_THIS_FILE_PSI_KEY, LATCH_ID_##id, #id, level, #level, key)
587
588 #ifdef UNIV_PFS_RWLOCK
589 /** Latch element.
590 Used for rwlocks which have PFS keys defined under UNIV_PFS_RWLOCK.
591 @param[in] id Latch id
592 @param[in] level Latch level
593 @param[in] key PFS key */
594 #define LATCH_ADD_RWLOCK(id, level, key) \
595 latch_meta[LATCH_ID_##id] = ut::new_withkey<latch_meta_t>( \
596 UT_NEW_THIS_FILE_PSI_KEY, LATCH_ID_##id, #id, level, #level, key)
597 #else
598 #define LATCH_ADD_RWLOCK(id, level, key) \
599 latch_meta[LATCH_ID_##id] = \
600 ut::new_withkey<latch_meta_t>(UT_NEW_THIS_FILE_PSI_KEY, LATCH_ID_##id, \
601 #id, level, #level, PSI_NOT_INSTRUMENTED)
602 #endif /* UNIV_PFS_RWLOCK */
603
604 #else
605 #define LATCH_ADD_MUTEX(id, level, key) \
606 latch_meta[LATCH_ID_##id] = ut::new_withkey<latch_meta_t>( \
607 UT_NEW_THIS_FILE_PSI_KEY, LATCH_ID_##id, #id, level, #level)
608 #define LATCH_ADD_RWLOCK(id, level, key) \
609 latch_meta[LATCH_ID_##id] = ut::new_withkey<latch_meta_t>( \
610 UT_NEW_THIS_FILE_PSI_KEY, LATCH_ID_##id, #id, level, #level)
611 #endif /* UNIV_PFS_MUTEX */
612
613 /** Default latch counter */
614 class LatchCounter {
615 public:
616 /** The counts we collect for a mutex */
617 struct Count {
618 /** Constructor */
619 Count() UNIV_NOTHROW : m_spins(), m_waits(), m_calls(), m_enabled() {
620 /* No op */
621 }
622
623 /** Rest the values to zero */
624 void reset() UNIV_NOTHROW {
625 m_spins = 0;
626 m_waits = 0;
627 m_calls = 0;
628 }
629
630 /** Number of spins trying to acquire the latch. */
631 uint32_t m_spins;
632
633 /** Number of waits trying to acquire the latch */
634 uint32_t m_waits;
635
636 /** Number of times it was called */
637 uint32_t m_calls;
638
639 /** true if enabled */
640 bool m_enabled;
641 };
642
643 /** Constructor */
644 LatchCounter() UNIV_NOTHROW : m_active(false) { m_mutex.init(); }
645
646 /** Destructor */
647 ~LatchCounter() UNIV_NOTHROW {
648 m_mutex.destroy();
649
650 for (Count *count : m_counters) {
651 ut::delete_(count);
652 }
653 }
654
655 /** Reset all counters to zero. It is not protected by any
656 mutex and we don't care about atomicity. Unless it is a
657 demonstrated problem. The information collected is not
658 required for the correct functioning of the server. */
659 void reset() UNIV_NOTHROW {
660 m_mutex.enter();
661
662 for (Count *count : m_counters) {
663 count->reset();
664 }
665
666 m_mutex.exit();
667 }
668
669 /** @return the aggregate counter */
670 Count *sum_register() UNIV_NOTHROW {
671 m_mutex.enter();
672
673 Count *count;
674
675 if (m_counters.empty()) {
676 count = ut::new_withkey<Count>(UT_NEW_THIS_FILE_PSI_KEY);
677 m_counters.push_back(count);
678 } else {
679 ut_a(m_counters.size() == 1);
680 count = m_counters[0];
681 }
682
683 m_mutex.exit();
684
685 return (count);
686 }
687
688 /** Deregister the count. We don't do anything
689 @param[in] count The count instance to deregister */
690 void sum_deregister(Count *count
691 [[maybe_unused]]) const UNIV_NOTHROW { /* Do nothing */
692 }
693
694 /** Register a single instance counter */
695 void single_register(Count *count) UNIV_NOTHROW {
696 m_mutex.enter();
697
698 m_counters.push_back(count);
699
700 m_mutex.exit();
701 }
702
703 /** Deregister a single instance counter
704 @param[in] count The count instance to deregister */
705 void single_deregister(Count *count) UNIV_NOTHROW {
706 m_mutex.enter();
707
708 m_counters.erase(std::remove(m_counters.begin(), m_counters.end(), count),
709 m_counters.end());
710
711 m_mutex.exit();
712 }
713
714 /** Iterate over the counters */
715 template <typename Callback>
716 void iterate(Callback &&callback) const UNIV_NOTHROW {
717 m_mutex.enter();
718 for (const Count *count : m_counters) {
719 std::forward<Callback>(callback)(count);
720 }
721 m_mutex.exit();
722 }
723
724 /** Disable the monitoring */
725 void enable() UNIV_NOTHROW {
726 m_mutex.enter();
727
728 for (Count *count : m_counters) {
729 count->m_enabled = true;
730 }
731
732 m_active = true;
733
734 m_mutex.exit();
735 }
736
737 /** Disable the monitoring */
738 void disable() UNIV_NOTHROW {
739 m_mutex.enter();
740
741 for (Count *count : m_counters) {
742 count->m_enabled = false;
743 }
744
745 m_active = false;
746
747 m_mutex.exit();
748 }
749
750 /** @return if monitoring is active */
751 bool is_enabled() const UNIV_NOTHROW { return (m_active); }
752
753 private:
754 /* Disable copying */
755 LatchCounter(const LatchCounter &);
756 LatchCounter &operator=(const LatchCounter &);
757
758 private:
759 typedef OSMutex Mutex;
760 typedef std::vector<Count *> Counters;
761
762 /** Mutex protecting m_counters */
763 mutable Mutex m_mutex;
764
765 /** Counters for the latches */
766 Counters m_counters;
767
768 /** if true then we collect the data */
769 bool m_active;
770 };
771
772 /** Latch meta data */
773 template <typename Counter = LatchCounter>
774 class LatchMeta {
775 public:
776 typedef Counter CounterType;
777
778 #ifdef UNIV_PFS_MUTEX
779 typedef mysql_pfs_key_t pfs_key_t;
780 #endif /* UNIV_PFS_MUTEX */
781
782 /** Constructor */
783 LatchMeta()
784 : m_id(LATCH_ID_NONE),
785 m_name(),
786 m_level(SYNC_UNKNOWN),
787 m_level_name()
788 #ifdef UNIV_PFS_MUTEX
789 ,
790 m_pfs_key()
791 #endif /* UNIV_PFS_MUTEX */
792 {
793 }
794
795 /** Destructor */
796 ~LatchMeta() = default;
797
798 /** Constructor
799 @param[in] id Latch id
800 @param[in] name Latch name
801 @param[in] level Latch level
802 @param[in] level_name Latch level text representation
803 @param[in] key PFS key */
804 LatchMeta(latch_id_t id, const char *name, latch_level_t level,
805 const char *level_name
806 #ifdef UNIV_PFS_MUTEX
807 ,
808 pfs_key_t key
809 #endif /* UNIV_PFS_MUTEX */
810 )
811 : m_id(id),
812 m_name(name),
813 m_level(level),
814 m_level_name(level_name)
815 #ifdef UNIV_PFS_MUTEX
816 ,
817 m_pfs_key(key)
818 #endif /* UNIV_PFS_MUTEX */
819 {
820 /* No op */
821 }
822
823 /* Less than operator.
824 @param[in] rhs Instance to compare against
825 @return true if this.get_id() < rhs.get_id() */
826 bool operator<(const LatchMeta &rhs) const {
827 return (get_id() < rhs.get_id());
828 }
829
830 /** @return the latch id */
831 latch_id_t get_id() const { return (m_id); }
832
833 /** @return the latch name */
834 const char *get_name() const { return (m_name); }
835
836 /** @return the latch level */
837 latch_level_t get_level() const { return (m_level); }
838
839 /** @return the latch level name */
840 const char *get_level_name() const { return (m_level_name); }
841
842 #ifdef UNIV_PFS_MUTEX
843 /** @return the PFS key for the latch */
844 pfs_key_t get_pfs_key() const { return (m_pfs_key); }
845 #endif /* UNIV_PFS_MUTEX */
846
847 /** @return the counter instance */
848 Counter *get_counter() { return (&m_counter); }
849
850 private:
851 /** Latch id */
852 latch_id_t m_id;
853
854 /** Latch name */
855 const char *m_name;
856
857 /** Latch level in the ordering */
858 latch_level_t m_level;
859
860 /** Latch level text representation */
861 const char *m_level_name;
862
863 #ifdef UNIV_PFS_MUTEX
864 /** PFS key */
865 pfs_key_t m_pfs_key;
866 #endif /* UNIV_PFS_MUTEX */
867
868 /** For gathering latch statistics */
869 Counter m_counter;
870 };
871
872 typedef LatchMeta<LatchCounter> latch_meta_t;
873 typedef std::vector<latch_meta_t *, ut::allocator<latch_meta_t *>>
874 LatchMetaData;
875
876 /** Note: This is accessed without any mutex protection. It is initialised
877 at startup and elements should not be added to or removed from it after
878 that. See sync_latch_meta_init() */
879 extern LatchMetaData latch_meta;
880
881 /** Get the latch meta-data from the latch ID
882 @param[in] id Latch ID
883 @return the latch meta data */
884 inline latch_meta_t &sync_latch_get_meta(latch_id_t id) {
885 ut_ad(static_cast<size_t>(id) < latch_meta.size());
886 ut_ad(id == latch_meta[id]->get_id());
887
888 return (*latch_meta[id]);
889 }
890
891 /** Fetch the counter for the latch
892 @param[in] id Latch ID
893 @return the latch counter */
894 inline latch_meta_t::CounterType *sync_latch_get_counter(latch_id_t id) {
895 latch_meta_t &meta = sync_latch_get_meta(id);
896
897 return (meta.get_counter());
898 }
899
900 /** Get the latch name from the latch ID
901 @param[in] id Latch ID
902 @return the name, will assert if not found */
903 inline const char *sync_latch_get_name(latch_id_t id) {
904 const latch_meta_t &meta = sync_latch_get_meta(id);
905
906 return (meta.get_name());
907 }
908
909 /** Get the latch ordering level
910 @param[in] id Latch id to lookup
911 @return the latch level */
912 inline latch_level_t sync_latch_get_level(latch_id_t id) {
913 const latch_meta_t &meta = sync_latch_get_meta(id);
914
915 return (meta.get_level());
916 }
917
918 #ifdef UNIV_PFS_MUTEX
919 /** Get the latch PFS key from the latch ID
920 @param[in] id Latch ID
921 @return the PFS key */
922 inline mysql_pfs_key_t sync_latch_get_pfs_key(latch_id_t id) {
923 const latch_meta_t &meta = sync_latch_get_meta(id);
924
925 return (meta.get_pfs_key());
926 }
927 #endif /* UNIV_PFS_MUTEX */
928
929 #ifndef UNIV_HOTBACKUP
930 /** String representation of the filename and line number where the
931 latch was created
932 @param[in] id Latch ID
933 @param[in] created Filename and line number where it was created
934 @return the string representation */
935 std::string sync_mutex_to_string(latch_id_t id, const std::string &created);
936
937 /** Print the filename "basename"
938 @return the basename */
939 const char *sync_basename(const char *filename);
940 #endif /* !UNIV_HOTBACKUP */
941
942 /** Register a latch, called when it is created
943 @param[in] ptr Latch instance that was created
944 @param[in] filename Filename where it was created
945 @param[in] line Line number in filename */
946 void sync_file_created_register(const void *ptr, const char *filename,
947 uint16_t line);
948
949 /** Deregister a latch, called when it is destroyed
950 @param[in] ptr Latch to be destroyed */
951 void sync_file_created_deregister(const void *ptr);
952
953 /** Get the string where the file was created. Its format is "name:line"
954 @param[in] ptr Latch instance
955 @return created information or "" if can't be found */
956 std::string sync_file_created_get(const void *ptr);
957
958 #endif /* !UNIV_LIBRARY */
959
960 #ifdef UNIV_DEBUG
961
962 /** All (ordered) latches, used in debugging, must derive from this class. */
963 struct latch_t {
964 /** Constructor
965 @param[in] id The latch ID */
966 explicit latch_t(latch_id_t id = LATCH_ID_NONE) UNIV_NOTHROW : m_id(id),
967 m_rw_lock(),
968 m_temp_fsp() {}
969
970 latch_t &operator=(const latch_t &) = default;
971
972 /** Destructor */
973 virtual ~latch_t() UNIV_NOTHROW = default;
974
975 /** @return the latch ID */
976 latch_id_t get_id() const { return (m_id); }
977
978 /** @return true if it is a rw-lock */
979 bool is_rw_lock() const UNIV_NOTHROW { return (m_rw_lock); }
980
981 /** Print the latch context
982 @return the string representation */
983 virtual std::string to_string() const = 0;
984
985 /** @return "filename:line" from where the latch was last locked */
986 virtual std::string locked_from() const = 0;
987
988 /** @return the latch level */
989 latch_level_t get_level() const UNIV_NOTHROW {
990 ut_a(m_id != LATCH_ID_NONE);
991
992 #ifndef UNIV_LIBRARY
993 return (sync_latch_get_level(m_id));
994 #else
995 /* This should never be reached. This is
996 added to fix compilation errors
997 for library. We will never reach here because
998 mutexes are disabled in library. */
999 ut_error;
1000 return (SYNC_UNKNOWN);
1001 #endif /* !UNIV_LIBRARY */
1002 }
1003
1004 /** @return true if the latch is for a temporary file space*/
1005 bool is_temp_fsp() const UNIV_NOTHROW { return (m_temp_fsp); }
1006
1007 /** Set the temporary tablespace flag. The latch order constraints
1008 are different for intrinsic tables. We don't always acquire the
1009 index->lock. We need to figure out the context and add some special
1010 rules during the checks. */
1011 void set_temp_fsp() UNIV_NOTHROW {
1012 ut_ad(get_id() == LATCH_ID_FIL_SPACE);
1013 m_temp_fsp = true;
1014 }
1015
1016 /** @return the latch name, m_id must be set */
1017 const char *get_name() const UNIV_NOTHROW {
1018 ut_a(m_id != LATCH_ID_NONE);
1019
1020 #ifndef UNIV_LIBRARY
1021 return (sync_latch_get_name(m_id));
1022 #else
1023 /* This should never be reached. This is
1024 added to fix compilation errors
1025 for library. We will never reach here because
1026 mutexes are disabled in library. */
1027 ut_error;
1028 return (nullptr);
1029 #endif /* !UNIV_LIBRARY */
1030 }
1031
1032 /** Latch ID */
1033 latch_id_t m_id;
1034
1035 /** true if it is a rw-lock. In debug mode, rw_lock_t derives from
1036 this class and sets this variable. */
1037 bool m_rw_lock;
1038
1039 /** true if it is an temporary space latch */
1040 bool m_temp_fsp;
1041 };
1042
1043 /** Subclass this to iterate over a thread's acquired latch levels. */
1044 struct sync_check_functor_t {
1045 virtual ~sync_check_functor_t() = default;
1046 virtual bool operator()(const latch_level_t) = 0;
1047 virtual bool result() const = 0;
1048 };
1049
1050 /** Functor to check whether the calling thread owns the btr search mutex. */
1051 struct btrsea_sync_check : public sync_check_functor_t {
1052 /** Constructor
1053 @param[in] has_search_latch true if owns the latch */
1054 explicit btrsea_sync_check(bool has_search_latch)
1055 : m_result(), m_has_search_latch(has_search_latch) {}
1056
1057 /** Destructor */
1058 ~btrsea_sync_check() override = default;
1059
1060 /** Called for every latch owned by the calling thread.
1061 @param[in] level Level of the existing latch
1062 @return true if the predicate check fails */
1063 bool operator()(const latch_level_t level) override {
1064 /* If calling thread doesn't hold search latch then
1065 check if there are latch level exception provided.
1066
1067 Note: Optimizer has added InnoDB intrinsic table as an
1068 alternative to MyISAM intrinsic table. With this a new
1069 control flow comes into existence, it is:
1070
1071 Server -> Plugin -> SE
1072
1073 Plugin in this case is I_S which is sharing the latch vector
1074 of InnoDB and so there could be lock conflicts. Ideally
1075 the Plugin should use a difference namespace latch vector
1076 as it doesn't have any dependency with SE latching protocol.
1077
1078 Added check that will allow thread to hold I_S latches */
1079
1080 if (!m_has_search_latch &&
1081 (level != SYNC_SEARCH_SYS && level != SYNC_DICT &&
1082 level != SYNC_FTS_CACHE && level != SYNC_UNDO_DDL &&
1083 level != SYNC_DICT_OPERATION && level != SYNC_TRX_I_S_LAST_READ &&
1084 level != SYNC_TRX_I_S_RWLOCK)) {
1085 m_result = true;
1086 #ifdef UNIV_NO_ERR_MSGS
1087 ib::error()
1088 #else
1089 ib::error(ER_IB_MSG_1373)
1090 #endif
1091 << "Debug: Calling thread does not hold search "
1092 "latch but does hold latch level "
1093 << level << ".";
1094
1095 return (m_result);
1096 }
1097
1098 return (false);
1099 }
1100
1101 /** @return result from the check */
1102 bool result() const override { return (m_result); }
1103
1104 private:
1105 /** True if all OK */
1106 bool m_result;
1107
1108 /** If the caller owns the search latch */
1109 const bool m_has_search_latch;
1110 };
1111
1112 /** Functor to check for dictionary latching constraints. */
1113 struct dict_sync_check : public sync_check_functor_t {
1114 /** Constructor
1115 @param[in] dict_mutex_allowed true if the dict mutex
1116 is allowed */
1117 explicit dict_sync_check(bool dict_mutex_allowed)
1118 : m_result(), m_dict_mutex_allowed(dict_mutex_allowed) {}
1119
1120 /** Destructor */
1121 ~dict_sync_check() override = default;
1122
1123 /** Check the latching constraints
1124 @param[in] level The level held by the thread */
1125 bool operator()(const latch_level_t level) override {
1126 if (!m_dict_mutex_allowed ||
1127 (level != SYNC_DICT && level != SYNC_UNDO_SPACES &&
1128 level != SYNC_FTS_CACHE && level != SYNC_DICT_OPERATION &&
1129 /* This only happens in recv_apply_hashed_log_recs. */
1130 level != SYNC_RECV_WRITER && level != SYNC_NO_ORDER_CHECK)) {
1131 m_result = true;
1132 #ifdef UNIV_NO_ERR_MSGS
1133 ib::error()
1134 #else
1135 ib::error(ER_IB_MSG_1374)
1136 #endif
1137 << "Debug: Dictionary latch order violation for level " << level
1138 << ".";
1139
1140 return (true);
1141 }
1142
1143 return (false);
1144 }
1145
1146 /** @return the result of the check */
1147 virtual bool result() const override { return (m_result); }
1148
1149 private:
1150 /** True if all OK */
1151 bool m_result;
1152
1153 /** True if it is OK to hold the dict mutex */
1154 const bool m_dict_mutex_allowed;
1155 };
1156
1157 /** Functor to check for given latching constraints. */
1158 struct sync_allowed_latches : public sync_check_functor_t {
1159 /** Constructor
1160 @param[in] from first element in an array of latch_level_t
1161 @param[in] to last element in an array of latch_level_t */
1162 sync_allowed_latches(const latch_level_t *from, const latch_level_t *to)
1163 : m_result(), m_latches(from, to) {}
1164
1165 /** Default constructor. The list of allowed latches is empty. */
1166 sync_allowed_latches() : m_result(), m_latches() {}
1167
1168 /** Check whether the given latch_t violates the latch constraint.
1169 This object maintains a list of allowed latch levels, and if the given
1170 latch belongs to a latch level that is not there in the allowed list,
1171 then it is a violation.
1172
1173 @param[in] level The latch level to check
1174 @return true if there is a latch ordering violation */
1175 virtual bool operator()(const latch_level_t level) override {
1176 for (latch_level_t allowed_level : m_latches) {
1177 if (level == allowed_level) {
1178 m_result = false;
1179
1180 /* No violation */
1181 return (m_result);
1182 }
1183 }
1184
1185 #ifdef UNIV_NO_ERR_MSGS
1186 ib::error()
1187 #else
1188 ib::error(ER_IB_MSG_1375)
1189 #endif
1190 << "Debug: sync_allowed_latches violation for level=" << level;
1191 m_result = true;
1192 return (m_result);
1193 }
1194
1195 /** @return the result of the check */
1196 virtual bool result() const override { return (m_result); }
1197
1198 private:
1199 /** Save the result of validation check here
1200 True if all OK */
1201 bool m_result;
1202
1203 typedef std::vector<latch_level_t, ut::allocator<latch_level_t>> latches_t;
1204
1205 /** List of latch levels that are allowed to be held */
1206 latches_t m_latches;
1207 };
1208
1209 typedef ulint rw_lock_flags_t;
1210
1211 /* Flags to specify lock types for rw_lock_own_flagged() */
1212 enum rw_lock_flag_t {
1213 RW_LOCK_FLAG_S = 1 << 0,
1214 RW_LOCK_FLAG_X = 1 << 1,
1215 RW_LOCK_FLAG_SX = 1 << 2
1216 };
1217
1218 #endif /* UNIV_DBEUG */
1219
1220 #endif /* sync0types_h */