35#include "sync0sync.ic"
168#if defined(UNIV_DEBUG) && !defined(UNIV_HOTBACKUP)
176static ib_int64_t mutex_spin_round_count = 0;
179static ib_int64_t mutex_spin_wait_count = 0;
182static ib_int64_t mutex_os_wait_count = 0;
199#ifdef UNIV_SYNC_DEBUG
204UNIV_INTERN sync_thread_t* sync_thread_level_arrays;
207UNIV_INTERN
mutex_t sync_thread_mutex;
209# ifdef UNIV_PFS_MUTEX
210UNIV_INTERN mysql_pfs_key_t sync_thread_mutex_key;
221UNIV_INTERN mysql_pfs_key_t mutex_list_mutex_key;
224#ifdef UNIV_SYNC_DEBUG
226UNIV_INTERN ibool sync_order_checks_on = FALSE;
230static const ulint SYNC_THREAD_N_LEVELS = 10000;
276 const char* cmutex_name,
277# ifdef UNIV_SYNC_DEBUG
281 const char* cfile_name,
284#if defined(HAVE_ATOMIC_BUILTINS)
285 mutex_reset_lock_word(mutex);
291 mutex_set_waiters(mutex, 0);
293 mutex->magic_n = MUTEX_MAGIC_N;
295#ifdef UNIV_SYNC_DEBUG
297 mutex->file_name =
"not yet reserved";
298 mutex->level = level;
301 mutex->
cline = cline;
304 mutex->cmutex_name= cmutex_name;
305 mutex->count_using= 0;
306 mutex->mutex_type= 0;
307 mutex->lspent_time= 0;
308 mutex->lmax_spent_time= 0;
309 mutex->count_spin_loop= 0;
310 mutex->count_spin_rounds= 0;
311 mutex->count_os_yield= 0;
320#ifdef UNIV_SYNC_DEBUG
321 || (mutex == &sync_thread_mutex)
349 ut_ad(mutex_validate(mutex));
351 ut_a(mutex_get_waiters(mutex) == 0);
354 if (mutex == &mem_hash_mutex) {
363#ifdef UNIV_SYNC_DEBUG
364 && mutex != &sync_thread_mutex
386#if !defined(HAVE_ATOMIC_BUILTINS)
413 ut_ad(mutex_validate(mutex));
415 if (!mutex_test_and_set(mutex)) {
418#ifdef UNIV_SYNC_DEBUG
419 mutex_set_debug_info(mutex, file_name, line);
439 ut_a(mutex->magic_n == MUTEX_MAGIC_N);
454 ut_ad(mutex_validate(mutex));
489 const char* file_name,
496 ib_int64_t lstart_time = 0, lfinish_time;
500 uint timer_started = 0;
508 mutex_spin_wait_count++;
521 ut_d(mutex->count_spin_loop++);
524 if (srv_spin_wait_delay) {
533 mutex->count_os_yield++;
534#ifndef UNIV_HOTBACKUP
535 if (timed_mutexes && timer_started == 0) {
537 lstart_time= (ib_int64_t)sec * 1000000 + ms;
545#ifdef UNIV_SRV_PRINT_LATCH_WAITS
547 "Thread %lu spin wait mutex at %p"
548 " cfile %s cline %lu rnds %lu\n",
551 (ulong) mutex->cline, (ulong) i);
554 mutex_spin_round_count += i;
556 ut_d(mutex->count_spin_rounds += i);
558 if (mutex_test_and_set(mutex) == 0) {
562#ifdef UNIV_SYNC_DEBUG
563 mutex_set_debug_info(mutex, file_name, line);
583 SYNC_MUTEX, file_name, line, &index);
591 mutex_set_waiters(mutex, 1);
594 for (i = 0; i < 4; i++) {
595 if (mutex_test_and_set(mutex) == 0) {
601#ifdef UNIV_SYNC_DEBUG
602 mutex_set_debug_info(mutex, file_name, line);
605#ifdef UNIV_SRV_PRINT_LATCH_WAITS
606 fprintf(stderr,
"Thread %lu spin wait succeeds at 2:"
624#ifdef UNIV_SRV_PRINT_LATCH_WAITS
626 "Thread %lu OS wait mutex at %p cfile %s cline %lu rnds %lu\n",
629 (ulong) mutex->cline, (ulong) i);
632 mutex_os_wait_count++;
634 mutex->count_os_wait++;
637#ifndef UNIV_HOTBACKUP
638 if (timed_mutexes == 1 && timer_started == 0) {
640 lstart_time= (ib_int64_t)sec * 1000000 + ms;
651 if (timed_mutexes == 1 && timer_started==1) {
653 lfinish_time= (ib_int64_t)sec * 1000000 + ms;
655 ltime_diff= (ulint) (lfinish_time - lstart_time);
656 mutex->lspent_time += ltime_diff;
658 if (mutex->lmax_spent_time < ltime_diff) {
659 mutex->lmax_spent_time= ltime_diff;
674 mutex_set_waiters(mutex, 0);
682#ifdef UNIV_SYNC_DEBUG
690 const char* file_name,
696 sync_thread_add_level(mutex, mutex->level);
698 mutex->file_name = file_name;
709 const char** file_name,
716 *file_name = mutex->file_name;
718 *thread_id = mutex->thread_id;
725mutex_list_print_info(
730 const char* file_name;
737 "----------\n", file);
743 while (mutex != NULL) {
747 mutex_get_debug_info(mutex, &file_name, &line,
750 "Locked mutex: addr %p thread %ld"
751 " file %s line %ld\n",
759 fprintf(file,
"Total number of mutexes %ld\n", count);
769mutex_n_reserved(
void)
806 return(mutex_n_reserved() + rw_lock_n_locked() == 0);
814sync_thread_level_arrays_find_slot(
void)
823 for (i = 0; i < OS_THREAD_MAX_N; i++) {
826 slot = &sync_thread_level_arrays[i];
842sync_thread_level_arrays_find_free(
void)
848 for (i = 0; i < OS_THREAD_MAX_N; i++) {
851 slot = &sync_thread_level_arrays[i];
853 if (slot->
levels == NULL) {
869 const sync_level_t* slot)
876 if (mutex->magic_n == MUTEX_MAGIC_N) {
878 "Mutex created at %s %lu\n",
880 (ulong) mutex->
cline);
884 const char* file_name;
887 mutex_get_debug_info(
888 mutex, &file_name, &line, &thread_id);
891 "InnoDB: Locked mutex:"
892 " addr %p thread %ld file %s line %ld\n",
894 file_name, (ulong) line);
896 fputs(
"Not locked\n", stderr);
899 rw_lock_t* lock = slot->
latch;
920 for (i = 0; i < arr->
n_elems; i++) {
921 const sync_level_t* slot;
923 slot = &arr->
elems[i];
925 if (slot->
latch != NULL && slot->
level <= limit) {
928 "InnoDB: sync levels should be"
929 " > %lu but a level is %lu\n",
930 (ulong) limit, (ulong) slot->
level);
931 sync_print_warning(slot);
947sync_thread_levels_contain(
955 for (i = 0; i < arr->
n_elems; i++) {
956 const sync_level_t* slot;
958 slot = &arr->
elems[i];
960 if (slot->
latch != NULL && slot->
level == level) {
974sync_thread_levels_contains(
981 sync_thread_t* thread_slot;
983 if (!sync_order_checks_on) {
988 mutex_enter(&sync_thread_mutex);
990 thread_slot = sync_thread_level_arrays_find_slot();
992 if (thread_slot == NULL) {
994 mutex_exit(&sync_thread_mutex);
999 arr = thread_slot->
levels;
1001 for (i = 0; i < arr->
n_elems; i++) {
1004 slot = &arr->
elems[i];
1006 if (slot->
latch != NULL && slot->
level == level) {
1008 mutex_exit(&sync_thread_mutex);
1009 return(slot->
latch);
1013 mutex_exit(&sync_thread_mutex);
1023sync_thread_levels_nonempty_gen(
1025 ibool dict_mutex_allowed)
1032 sync_thread_t* thread_slot;
1034 if (!sync_order_checks_on) {
1039 mutex_enter(&sync_thread_mutex);
1041 thread_slot = sync_thread_level_arrays_find_slot();
1043 if (thread_slot == NULL) {
1045 mutex_exit(&sync_thread_mutex);
1050 arr = thread_slot->
levels;
1052 for (i = 0; i < arr->
n_elems; ++i) {
1053 const sync_level_t* slot;
1055 slot = &arr->
elems[i];
1057 if (slot->
latch != NULL
1058 && (!dict_mutex_allowed
1059 || (slot->
level != SYNC_DICT
1060 && slot->
level != SYNC_DICT_OPERATION))) {
1062 mutex_exit(&sync_thread_mutex);
1065 return(slot->
latch);
1069 mutex_exit(&sync_thread_mutex);
1079sync_thread_levels_empty(
void)
1082 return(sync_thread_levels_empty_gen(FALSE));
1091sync_thread_add_level(
1100 sync_thread_t* thread_slot;
1102 if (!sync_order_checks_on) {
1107 if ((latch == (
void*)&sync_thread_mutex)
1109 || (latch == (
void*)&rw_lock_debug_mutex)
1110 || (latch == (
void*)&rw_lock_list_mutex)) {
1115 if (level == SYNC_LEVEL_VARYING) {
1120 mutex_enter(&sync_thread_mutex);
1122 thread_slot = sync_thread_level_arrays_find_slot();
1124 if (thread_slot == NULL) {
1128 + (
sizeof(*array->
elems) * SYNC_THREAD_N_LEVELS);
1131 array = calloc(sz,
sizeof(
char));
1132 ut_a(array != NULL);
1134 array->next_free = ULINT_UNDEFINED;
1135 array->max_elems = SYNC_THREAD_N_LEVELS;
1136 array->elems = (sync_level_t*) &array[1];
1138 thread_slot = sync_thread_level_arrays_find_free();
1139 thread_slot->
levels = array;
1143 array = thread_slot->
levels;
1151 case SYNC_NO_ORDER_CHECK:
1152 case SYNC_EXTERN_STORAGE:
1153 case SYNC_TREE_NODE_FROM_HASH:
1156 case SYNC_TRX_SYS_HEADER:
1166 case SYNC_WORK_QUEUE:
1168 case SYNC_LOG_FLUSH_ORDER:
1169 case SYNC_THR_LOCAL:
1170 case SYNC_ANY_LATCH:
1171 case SYNC_FILE_FORMAT_TAG:
1172 case SYNC_DOUBLEWRITE:
1173 case SYNC_SEARCH_SYS:
1174 case SYNC_SEARCH_SYS_CONF:
1175 case SYNC_TRX_LOCK_HEAP:
1177 case SYNC_IBUF_BITMAP_MUTEX:
1180 case SYNC_PURGE_LATCH:
1181 case SYNC_PURGE_QUEUE:
1182 case SYNC_DICT_AUTOINC_MUTEX:
1183 case SYNC_DICT_OPERATION:
1184 case SYNC_DICT_HEADER:
1185 case SYNC_TRX_I_S_RWLOCK:
1186 case SYNC_TRX_I_S_LAST_READ:
1187 if (!sync_thread_levels_g(array, level, TRUE)) {
1189 "InnoDB: sync_thread_levels_g(array, %lu)"
1190 " does not hold!\n", level);
1194 case SYNC_BUF_FLUSH_LIST:
1198 if (!sync_thread_levels_g(array, level-1, TRUE)) {
1200 "InnoDB: sync_thread_levels_g(array, %lu)"
1201 " does not hold!\n", level-1);
1206 case SYNC_BUF_BLOCK:
1210 if (!sync_thread_levels_g(array, level, FALSE)) {
1211 ut_a(sync_thread_levels_g(array, level - 1, TRUE));
1212 ut_a(sync_thread_levels_contain(array, SYNC_BUF_POOL));
1216 if (sync_thread_levels_contain(array, SYNC_KERNEL)) {
1217 ut_a(sync_thread_levels_g(array, SYNC_REC_LOCK - 1,
1220 ut_a(sync_thread_levels_g(array, SYNC_REC_LOCK, TRUE));
1223 case SYNC_IBUF_BITMAP:
1227 if (sync_thread_levels_contain(array,
1228 SYNC_IBUF_BITMAP_MUTEX)) {
1229 ut_a(sync_thread_levels_g(array, SYNC_IBUF_BITMAP - 1,
1236 || sync_thread_levels_g(array, SYNC_IBUF_BITMAP,
1241 ut_a(sync_thread_levels_contain(array, SYNC_FSP));
1244 ut_a(sync_thread_levels_contain(array, SYNC_FSP)
1245 || sync_thread_levels_g(array, SYNC_FSP, TRUE));
1247 case SYNC_TRX_UNDO_PAGE:
1255 ut_a(sync_thread_levels_contain(array, SYNC_TRX_UNDO)
1256 || sync_thread_levels_contain(array, SYNC_RSEG)
1257 || sync_thread_levels_g(array, level - 1, TRUE));
1259 case SYNC_RSEG_HEADER:
1260 ut_a(sync_thread_levels_contain(array, SYNC_RSEG));
1262 case SYNC_RSEG_HEADER_NEW:
1263 ut_a(sync_thread_levels_contain(array, SYNC_KERNEL)
1264 && sync_thread_levels_contain(array, SYNC_FSP_PAGE));
1266 case SYNC_TREE_NODE:
1267 ut_a(sync_thread_levels_contain(array, SYNC_INDEX_TREE)
1268 || sync_thread_levels_contain(array, SYNC_DICT_OPERATION)
1269 || sync_thread_levels_g(array, SYNC_TREE_NODE - 1, TRUE));
1271 case SYNC_TREE_NODE_NEW:
1272 ut_a(sync_thread_levels_contain(array, SYNC_FSP_PAGE)
1273 || sync_thread_levels_contain(array, SYNC_IBUF_MUTEX));
1275 case SYNC_INDEX_TREE:
1276 if (sync_thread_levels_contain(array, SYNC_IBUF_MUTEX)
1277 && sync_thread_levels_contain(array, SYNC_FSP)) {
1278 ut_a(sync_thread_levels_g(array, SYNC_FSP_PAGE - 1,
1281 ut_a(sync_thread_levels_g(array, SYNC_TREE_NODE - 1,
1285 case SYNC_IBUF_MUTEX:
1286 ut_a(sync_thread_levels_g(array, SYNC_FSP_PAGE - 1, TRUE));
1288 case SYNC_IBUF_PESS_INSERT_MUTEX:
1289 ut_a(sync_thread_levels_g(array, SYNC_FSP - 1, TRUE));
1290 ut_a(!sync_thread_levels_contain(array, SYNC_IBUF_MUTEX));
1292 case SYNC_IBUF_HEADER:
1293 ut_a(sync_thread_levels_g(array, SYNC_FSP - 1, TRUE));
1294 ut_a(!sync_thread_levels_contain(array, SYNC_IBUF_MUTEX));
1295 ut_a(!sync_thread_levels_contain(array,
1296 SYNC_IBUF_PESS_INSERT_MUTEX));
1300 ut_a(buf_debug_prints
1301 || sync_thread_levels_g(array, SYNC_DICT, TRUE));
1303 ut_a(sync_thread_levels_g(array, SYNC_DICT, TRUE));
1310 if (array->next_free == ULINT_UNDEFINED) {
1311 ut_a(array->n_elems < array->max_elems);
1313 i = array->n_elems++;
1315 i = array->next_free;
1316 array->next_free = array->elems[i].level;
1319 ut_a(i < array->n_elems);
1320 ut_a(i != ULINT_UNDEFINED);
1324 slot = &array->elems[i];
1328 slot->
latch = latch;
1329 slot->
level = level;
1331 mutex_exit(&sync_thread_mutex);
1341sync_thread_reset_level(
1346 sync_thread_t* thread_slot;
1349 if (!sync_order_checks_on) {
1354 if ((latch == (
void*)&sync_thread_mutex)
1356 || (latch == (
void*)&rw_lock_debug_mutex)
1357 || (latch == (
void*)&rw_lock_list_mutex)) {
1362 mutex_enter(&sync_thread_mutex);
1364 thread_slot = sync_thread_level_arrays_find_slot();
1366 if (thread_slot == NULL) {
1370 mutex_exit(&sync_thread_mutex);
1374 array = thread_slot->
levels;
1376 for (i = 0; i < array->
n_elems; i++) {
1379 slot = &array->
elems[i];
1381 if (slot->
latch != latch) {
1399 if (array->
in_use == 0) {
1404 mutex_exit(&sync_thread_mutex);
1409 if (((
mutex_t*) latch)->magic_n != MUTEX_MAGIC_N) {
1412 rw_lock = (rw_lock_t*) latch;
1414 if (rw_lock->level == SYNC_LEVEL_VARYING) {
1415 mutex_exit(&sync_thread_mutex);
1423 mutex_exit(&sync_thread_mutex);
1445#ifdef UNIV_SYNC_DEBUG
1449 sync_thread_level_arrays = calloc(
1450 sizeof(sync_thread_t), OS_THREAD_MAX_N);
1451 ut_a(sync_thread_level_arrays != NULL);
1458 SYNC_NO_ORDER_CHECK);
1459#ifdef UNIV_SYNC_DEBUG
1460 mutex_create(sync_thread_mutex_key, &sync_thread_mutex,
1461 SYNC_NO_ORDER_CHECK);
1467 mutex_create(rw_lock_list_mutex_key, &rw_lock_list_mutex,
1468 SYNC_NO_ORDER_CHECK);
1470#ifdef UNIV_SYNC_DEBUG
1471 mutex_create(rw_lock_debug_mutex_key, &rw_lock_debug_mutex,
1472 SYNC_NO_ORDER_CHECK);
1475 rw_lock_debug_waiters = FALSE;
1479#ifdef UNIV_SYNC_DEBUG
1484sync_thread_level_arrays_free(
void)
1490 for (i = 0; i < OS_THREAD_MAX_N; i++) {
1491 sync_thread_t* slot;
1493 slot = &sync_thread_level_arrays[i];
1496 if (slot->
levels != NULL) {
1502 free(sync_thread_level_arrays);
1503 sync_thread_level_arrays = NULL;
1523#ifdef UNIV_MEM_DEBUG
1524 if (mutex == &mem_hash_mutex) {
1536#ifdef UNIV_SYNC_DEBUG
1537 mutex_free(&sync_thread_mutex);
1540 sync_order_checks_on = FALSE;
1542 sync_thread_level_arrays_free();
1556#ifdef UNIV_SYNC_DEBUG
1557 fprintf(file,
"Mutex exits %llu, rws exits %llu, rwx exits %llu\n",
1562 "Mutex spin waits %" PRId64
", rounds %" PRId64
", "
1563 "OS waits %" PRId64
"\n"
1564 "RW-shared spins %" PRId64
", rounds %" PRId64
", OS waits %" PRId64
";"
1565 " RW-excl spins %" PRId64
", rounds %" PRId64
", OS waits %" PRId64
"\n",
1566 mutex_spin_wait_count,
1567 mutex_spin_round_count,
1568 mutex_os_wait_count,
1577 "Spin rounds per wait: %.2f mutex, %.2f RW-shared, "
1579 (
double) mutex_spin_round_count /
1580 (mutex_spin_wait_count ? mutex_spin_wait_count : 1),
1595#ifdef UNIV_SYNC_DEBUG
1596 mutex_list_print_info(file);
1598 rw_lock_list_print_info(file);
const char * innobase_basename(const char *path_name)
UNIV_INTERN void os_fast_mutex_free(os_fast_mutex_t *fast_mutex)
UNIV_INTERN os_event_t os_event_create(const char *name)
UNIV_INTERN void os_event_set(os_event_t event)
UNIV_INTERN void os_fast_mutex_init(os_fast_mutex_t *fast_mutex)
UNIV_INTERN void os_event_free(os_event_t event)
UNIV_INTERN void os_thread_yield(void)
os_thread_t os_thread_id_t
UNIV_INTERN ibool os_thread_eq(os_thread_id_t a, os_thread_id_t b)
UNIV_INTERN os_thread_id_t os_thread_get_curr_id(void)
UNIV_INTERN ulint os_thread_pf(os_thread_id_t a)
ibool srv_is_being_started
volatile lock_word_t lock_word
UNIV_INTERN void sync_array_object_signalled(sync_array_t *arr)
UNIV_INTERN void sync_array_wait_event(sync_array_t *arr, ulint index)
UNIV_INTERN void sync_array_print_info(FILE *file, sync_array_t *arr)
UNIV_INTERN void sync_array_reserve_cell(sync_array_t *arr, void *object, ulint type, const char *file, ulint line, ulint *index)
#define SYNC_ARRAY_OS_MUTEX
UNIV_INTERN void sync_array_free_cell(sync_array_t *arr, ulint index)
UNIV_INTERN sync_array_t * sync_array_create(ulint n_cells, ulint protection)
UNIV_INTERN void sync_array_free(sync_array_t *arr)
struct sync_array_struct sync_array_t
ib_int64_t rw_x_spin_round_count
ib_int64_t rw_s_spin_wait_count
ib_int64_t rw_s_os_wait_count
ib_int64_t rw_s_spin_round_count
ib_int64_t rw_x_spin_wait_count
ib_int64_t rw_s_exit_count
ib_int64_t rw_x_exit_count
ib_int64_t rw_x_os_wait_count
UNIV_INTERN void sync_close(void)
UNIV_INTERN ulint mutex_enter_nowait_func(mutex_t *mutex, const char *file_name, ulint line)
UNIV_INTERN void sync_print(FILE *file)
UNIV_INTERN void mutex_create_func(mutex_t *mutex, const char *cfile_name, ulint cline)
UNIV_INTERN void sync_init(void)
ut_list_base_node_t mutex_list
ib_int64_t mutex_exit_count
UNIV_INLINE lock_word_t mutex_get_lock_word(const mutex_t *mutex)
sync_array_t * sync_primary_wait_array
UNIV_INTERN void mutex_free_func(mutex_t *mutex)
UNIV_INTERN void sync_print_wait_info(FILE *file)
#define UT_LIST_GET_LEN(BASE)
#define UT_LIST_REMOVE(NAME, BASE, N)
#define UT_LIST_GET_FIRST(BASE)
#define UT_LIST_GET_PREV(NAME, N)
#define UT_LIST_ADD_FIRST(NAME, BASE, N)
#define UT_LIST_GET_NEXT(NAME, N)
#define UT_LIST_INIT(BASE)
UNIV_INLINE ulint ut_rnd_interval(ulint low, ulint high)
UNIV_INTERN int ut_usectime(ulint *sec, ulint *ms)
UNIV_INTERN ulint ut_delay(ulint delay)