You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1377 lines
36 KiB

17 years ago
10 years ago
17 years ago
17 years ago
17 years ago
16 years ago
15 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
15 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
17 years ago
17 years ago
16 years ago
17 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
17 years ago
10 years ago
16 years ago
16 years ago
17 years ago
17 years ago
16 years ago
16 years ago
17 years ago
16 years ago
15 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
16 years ago
16 years ago
10 years ago
16 years ago
16 years ago
10 years ago
11 years ago
11 years ago
  1. /*****************************************************************************
  2. Copyright (c) 1995, 2015, Oracle and/or its affiliates. All Rights Reserved.
  3. Copyright (c) 2008, Google Inc.
  4. Copyright (c) 2013, 2014, MariaDB Corporation. All Rights Reserved.
  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. This program is free software; you can redistribute it and/or modify it under
  11. the terms of the GNU General Public License as published by the Free Software
  12. Foundation; version 2 of the License.
  13. This program is distributed in the hope that it will be useful, but WITHOUT
  14. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  15. FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License along with
  17. this program; if not, write to the Free Software Foundation, Inc.,
  18. 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
  19. *****************************************************************************/
  20. /**************************************************//**
  21. @file sync/sync0arr.cc
  22. The wait array used in synchronization primitives
  23. Created 9/5/1995 Heikki Tuuri
  24. *******************************************************/
  25. #include "sync0arr.h"
  26. #ifdef UNIV_NONINL
  27. #include "sync0arr.ic"
  28. #endif
  29. #include "sync0sync.h"
  30. #include "sync0rw.h"
  31. #include "os0sync.h"
  32. #include "os0file.h"
  33. #include "lock0lock.h"
  34. #include "srv0srv.h"
  35. #include "ha_prototypes.h"
  36. /*
  37. WAIT ARRAY
  38. ==========
  39. The wait array consists of cells each of which has an
  40. an operating system event object created for it. The threads
  41. waiting for a mutex, for example, can reserve a cell
  42. in the array and suspend themselves to wait for the event
  43. to become signaled. When using the wait array, remember to make
  44. sure that some thread holding the synchronization object
  45. will eventually know that there is a waiter in the array and
  46. signal the object, to prevent infinite wait.
  47. Why we chose to implement a wait array? First, to make
  48. mutexes fast, we had to code our own implementation of them,
  49. which only in usually uncommon cases resorts to using
  50. slow operating system primitives. Then we had the choice of
  51. assigning a unique OS event for each mutex, which would
  52. be simpler, or using a global wait array. In some operating systems,
  53. the global wait array solution is more efficient and flexible,
  54. because we can do with a very small number of OS events,
  55. say 200. In NT 3.51, allocating events seems to be a quadratic
  56. algorithm, because 10 000 events are created fast, but
  57. 100 000 events takes a couple of minutes to create.
  58. As of 5.0.30 the above mentioned design is changed. Since now
  59. OS can handle millions of wait events efficiently, we no longer
  60. have this concept of each cell of wait array having one event.
  61. Instead, now the event that a thread wants to wait on is embedded
  62. in the wait object (mutex or rw_lock). We still keep the global
  63. wait array for the sake of diagnostics and also to avoid infinite
  64. wait The error_monitor thread scans the global wait array to signal
  65. any waiting threads who have missed the signal. */
  66. /** A cell where an individual thread may wait suspended
  67. until a resource is released. The suspending is implemented
  68. using an operating system event semaphore. */
  69. struct sync_cell_t {
  70. void* wait_object; /*!< pointer to the object the
  71. thread is waiting for; if NULL
  72. the cell is free for use */
  73. void* old_wait_mutex; /*!< the latest regular or priority
  74. wait mutex in cell */
  75. void* old_wait_rw_lock;
  76. /*!< the latest regular or priority
  77. wait rw-lock in cell */
  78. ulint request_type; /*!< lock type requested on the
  79. object */
  80. const char* file; /*!< in debug version file where
  81. requested */
  82. ulint line; /*!< in debug version line where
  83. requested */
  84. os_thread_id_t thread; /*!< thread id of this waiting
  85. thread */
  86. ibool waiting; /*!< TRUE if the thread has already
  87. called sync_array_event_wait
  88. on this cell */
  89. ib_int64_t signal_count; /*!< We capture the signal_count
  90. of the wait_object when we
  91. reset the event. This value is
  92. then passed on to os_event_wait
  93. and we wait only if the event
  94. has not been signalled in the
  95. period between the reset and
  96. wait call. */
  97. time_t reservation_time;/*!< time when the thread reserved
  98. the wait cell */
  99. };
  100. /* NOTE: It is allowed for a thread to wait
  101. for an event allocated for the array without owning the
  102. protecting mutex (depending on the case: OS or database mutex), but
  103. all changes (set or reset) to the state of the event must be made
  104. while owning the mutex. */
  105. /** Synchronization array */
  106. struct sync_array_t {
  107. ulint n_reserved; /*!< number of currently reserved
  108. cells in the wait array */
  109. ulint n_cells; /*!< number of cells in the
  110. wait array */
  111. sync_cell_t* array; /*!< pointer to wait array */
  112. ib_mutex_t mutex; /*!< possible database mutex
  113. protecting this data structure */
  114. os_ib_mutex_t os_mutex; /*!< Possible operating system mutex
  115. protecting the data structure.
  116. As this data structure is used in
  117. constructing the database mutex,
  118. to prevent infinite recursion
  119. in implementation, we fall back to
  120. an OS mutex. */
  121. ulint res_count; /*!< count of cell reservations
  122. since creation of the array */
  123. };
  124. /** User configured sync array size */
  125. UNIV_INTERN ulong srv_sync_array_size = 32;
  126. /** Locally stored copy of srv_sync_array_size */
  127. static ulint sync_array_size;
  128. /** The global array of wait cells for implementation of the database's own
  129. mutexes and read-write locks */
  130. static sync_array_t** sync_wait_array;
  131. /** count of how many times an object has been signalled */
  132. static ulint sg_count;
  133. #ifdef UNIV_SYNC_DEBUG
  134. /******************************************************************//**
  135. This function is called only in the debug version. Detects a deadlock
  136. of one or more threads because of waits of semaphores.
  137. @return TRUE if deadlock detected */
  138. static
  139. ibool
  140. sync_array_detect_deadlock(
  141. /*=======================*/
  142. sync_array_t* arr, /*!< in: wait array; NOTE! the caller must
  143. own the mutex to array */
  144. sync_cell_t* start, /*!< in: cell where recursive search started */
  145. sync_cell_t* cell, /*!< in: cell to search */
  146. ulint depth); /*!< in: recursion depth */
  147. #endif /* UNIV_SYNC_DEBUG */
  148. /*****************************************************************//**
  149. Gets the nth cell in array.
  150. @return cell */
  151. static
  152. sync_cell_t*
  153. sync_array_get_nth_cell(
  154. /*====================*/
  155. sync_array_t* arr, /*!< in: sync array */
  156. ulint n) /*!< in: index */
  157. {
  158. ut_a(arr);
  159. ut_a(n < arr->n_cells);
  160. return(arr->array + n);
  161. }
  162. /******************************************************************//**
  163. Looks for a cell with the given thread id.
  164. @return pointer to cell or NULL if not found */
  165. static
  166. sync_cell_t*
  167. sync_array_find_thread(
  168. /*===================*/
  169. sync_array_t* arr, /*!< in: wait array */
  170. os_thread_id_t thread) /*!< in: thread id */
  171. {
  172. ulint i;
  173. sync_cell_t* cell;
  174. for (i = 0; i < arr->n_cells; i++) {
  175. cell = sync_array_get_nth_cell(arr, i);
  176. if (cell->wait_object != NULL
  177. && os_thread_eq(cell->thread, thread)) {
  178. return(cell); /* Found */
  179. }
  180. }
  181. return(NULL); /* Not found */
  182. }
  183. /******************************************************************//**
  184. Reserves the mutex semaphore protecting a sync array. */
  185. static
  186. void
  187. sync_array_enter(
  188. /*=============*/
  189. sync_array_t* arr) /*!< in: sync wait array */
  190. {
  191. os_mutex_enter(arr->os_mutex);
  192. }
  193. /******************************************************************//**
  194. Releases the mutex semaphore protecting a sync array. */
  195. static
  196. void
  197. sync_array_exit(
  198. /*============*/
  199. sync_array_t* arr) /*!< in: sync wait array */
  200. {
  201. os_mutex_exit(arr->os_mutex);
  202. }
  203. /*******************************************************************//**
  204. Creates a synchronization wait array. It is protected by a mutex
  205. which is automatically reserved when the functions operating on it
  206. are called.
  207. @return own: created wait array */
  208. static
  209. sync_array_t*
  210. sync_array_create(
  211. /*==============*/
  212. ulint n_cells) /*!< in: number of cells in the array
  213. to create */
  214. {
  215. ulint sz;
  216. sync_array_t* arr;
  217. ut_a(n_cells > 0);
  218. /* Allocate memory for the data structures */
  219. arr = static_cast<sync_array_t*>(ut_malloc(sizeof(*arr)));
  220. memset(arr, 0x0, sizeof(*arr));
  221. sz = sizeof(sync_cell_t) * n_cells;
  222. arr->array = static_cast<sync_cell_t*>(ut_malloc(sz));
  223. memset(arr->array, 0x0, sz);
  224. arr->n_cells = n_cells;
  225. /* Then create the mutex to protect the wait array complex */
  226. arr->os_mutex = os_mutex_create();
  227. return(arr);
  228. }
  229. /******************************************************************//**
  230. Frees the resources in a wait array. */
  231. static
  232. void
  233. sync_array_free(
  234. /*============*/
  235. sync_array_t* arr) /*!< in, own: sync wait array */
  236. {
  237. ut_a(arr->n_reserved == 0);
  238. sync_array_validate(arr);
  239. /* Release the mutex protecting the wait array complex */
  240. os_mutex_free(arr->os_mutex);
  241. ut_free(arr->array);
  242. ut_free(arr);
  243. }
  244. /********************************************************************//**
  245. Validates the integrity of the wait array. Checks
  246. that the number of reserved cells equals the count variable. */
  247. UNIV_INTERN
  248. void
  249. sync_array_validate(
  250. /*================*/
  251. sync_array_t* arr) /*!< in: sync wait array */
  252. {
  253. ulint i;
  254. sync_cell_t* cell;
  255. ulint count = 0;
  256. sync_array_enter(arr);
  257. for (i = 0; i < arr->n_cells; i++) {
  258. cell = sync_array_get_nth_cell(arr, i);
  259. if (cell->wait_object != NULL) {
  260. count++;
  261. }
  262. }
  263. ut_a(count == arr->n_reserved);
  264. sync_array_exit(arr);
  265. }
  266. /*******************************************************************//**
  267. Returns the event that the thread owning the cell waits for. */
  268. static
  269. os_event_t
  270. sync_cell_get_event(
  271. /*================*/
  272. sync_cell_t* cell) /*!< in: non-empty sync array cell */
  273. {
  274. ulint type = cell->request_type;
  275. if (type == SYNC_MUTEX) {
  276. return(&((ib_mutex_t*) cell->wait_object)->event);
  277. } else if (type == SYNC_PRIO_MUTEX) {
  278. return(&((ib_prio_mutex_t*) cell->wait_object)
  279. ->high_priority_event);
  280. } else if (type == RW_LOCK_WAIT_EX) {
  281. return(&((rw_lock_t*) cell->wait_object)->wait_ex_event);
  282. } else if (type == PRIO_RW_LOCK_SHARED) {
  283. return(&((prio_rw_lock_t *) cell->wait_object)
  284. ->high_priority_s_event);
  285. } else if (type == PRIO_RW_LOCK_EX) {
  286. return(&((prio_rw_lock_t *) cell->wait_object)
  287. ->high_priority_x_event);
  288. } else { /* RW_LOCK_SHARED and RW_LOCK_EX wait on the same event */
  289. ut_ad(type == RW_LOCK_SHARED || type == RW_LOCK_EX);
  290. return(&((rw_lock_t*) cell->wait_object)->event);
  291. }
  292. }
  293. /******************************************************************//**
  294. Reserves a wait array cell for waiting for an object.
  295. The event of the cell is reset to nonsignalled state.
  296. @return true if free cell is found, otherwise false */
  297. UNIV_INTERN
  298. bool
  299. sync_array_reserve_cell(
  300. /*====================*/
  301. sync_array_t* arr, /*!< in: wait array */
  302. void* object, /*!< in: pointer to the object to wait for */
  303. ulint type, /*!< in: lock request type */
  304. const char* file, /*!< in: file where requested */
  305. ulint line, /*!< in: line where requested */
  306. ulint* index) /*!< out: index of the reserved cell */
  307. {
  308. sync_cell_t* cell;
  309. os_event_t event;
  310. ulint i;
  311. ut_a(object);
  312. ut_a(index);
  313. sync_array_enter(arr);
  314. arr->res_count++;
  315. /* Reserve a new cell. */
  316. for (i = 0; i < arr->n_cells; i++) {
  317. cell = sync_array_get_nth_cell(arr, i);
  318. if (cell->wait_object == NULL) {
  319. cell->waiting = FALSE;
  320. cell->wait_object = object;
  321. if (type == SYNC_MUTEX || type == SYNC_PRIO_MUTEX) {
  322. cell->old_wait_mutex = object;
  323. } else {
  324. cell->old_wait_rw_lock = object;
  325. }
  326. cell->request_type = type;
  327. cell->file = file;
  328. cell->line = line;
  329. arr->n_reserved++;
  330. *index = i;
  331. sync_array_exit(arr);
  332. /* Make sure the event is reset and also store
  333. the value of signal_count at which the event
  334. was reset. */
  335. event = sync_cell_get_event(cell);
  336. cell->signal_count = os_event_reset(event);
  337. cell->reservation_time = ut_time();
  338. cell->thread = os_thread_get_curr_id();
  339. return(true);
  340. }
  341. }
  342. /* No free cell found */
  343. return false;
  344. }
  345. /******************************************************************//**
  346. This function should be called when a thread starts to wait on
  347. a wait array cell. In the debug version this function checks
  348. if the wait for a semaphore will result in a deadlock, in which
  349. case prints info and asserts. */
  350. UNIV_INTERN
  351. void
  352. sync_array_wait_event(
  353. /*==================*/
  354. sync_array_t* arr, /*!< in: wait array */
  355. ulint index) /*!< in: index of the reserved cell */
  356. {
  357. sync_cell_t* cell;
  358. os_event_t event;
  359. ut_a(arr);
  360. sync_array_enter(arr);
  361. cell = sync_array_get_nth_cell(arr, index);
  362. ut_a(cell->wait_object);
  363. ut_a(!cell->waiting);
  364. ut_ad(os_thread_get_curr_id() == cell->thread);
  365. event = sync_cell_get_event(cell);
  366. cell->waiting = TRUE;
  367. #ifdef UNIV_SYNC_DEBUG
  368. /* We use simple enter to the mutex below, because if
  369. we cannot acquire it at once, mutex_enter would call
  370. recursively sync_array routines, leading to trouble.
  371. rw_lock_debug_mutex freezes the debug lists. */
  372. rw_lock_debug_mutex_enter();
  373. if (TRUE == sync_array_detect_deadlock(arr, cell, cell, 0)) {
  374. fputs("########################################\n", stderr);
  375. ut_error;
  376. }
  377. rw_lock_debug_mutex_exit();
  378. #endif
  379. sync_array_exit(arr);
  380. os_event_wait_low(event, cell->signal_count);
  381. sync_array_free_cell(arr, index);
  382. }
  383. /******************************************************************//**
  384. Reports info of a wait array cell. */
  385. static
  386. void
  387. sync_array_cell_print(
  388. /*==================*/
  389. FILE* file, /*!< in: file where to print */
  390. sync_cell_t* cell, /*!< in: sync cell */
  391. os_thread_id_t* reserver) /*!< out: write reserver or
  392. 0 */
  393. {
  394. ib_mutex_t* mutex;
  395. ib_prio_mutex_t* prio_mutex;
  396. rw_lock_t* rwlock;
  397. prio_rw_lock_t* prio_rwlock = NULL;
  398. ulint type;
  399. ulint writer;
  400. type = cell->request_type;
  401. fprintf(file,
  402. "--Thread %lu has waited at %s line %lu"
  403. " for %#.5g seconds the semaphore:\n",
  404. (ulong) os_thread_pf(cell->thread),
  405. innobase_basename(cell->file), (ulong) cell->line,
  406. difftime(time(NULL), cell->reservation_time));
  407. if (type == SYNC_MUTEX || type == SYNC_PRIO_MUTEX) {
  408. /* We use old_wait_mutex in case the cell has already
  409. been freed meanwhile */
  410. if (type == SYNC_MUTEX) {
  411. mutex = static_cast<ib_mutex_t*>(cell->old_wait_mutex);
  412. } else {
  413. prio_mutex = static_cast<ib_prio_mutex_t*>
  414. (cell->old_wait_mutex);
  415. mutex = &prio_mutex->base_mutex;
  416. }
  417. if (mutex) {
  418. fprintf(file,
  419. "Mutex at %p '%s', lock var %lu\n"
  420. #ifdef UNIV_SYNC_DEBUG
  421. "Last time reserved in file %s line %lu, "
  422. #endif /* UNIV_SYNC_DEBUG */
  423. "waiters flag %lu\n",
  424. (void*) mutex, mutex->cmutex_name,
  425. (ulong) mutex->lock_word,
  426. #ifdef UNIV_SYNC_DEBUG
  427. mutex->file_name, (ulong) mutex->line,
  428. #endif /* UNIV_SYNC_DEBUG */
  429. (ulong) mutex->waiters);
  430. }
  431. /* If stacktrace feature is enabled we will send a SIGUSR2
  432. signal to thread waiting for the semaphore. Signal handler
  433. will then dump the current stack to error log. */
  434. if (srv_use_stacktrace && cell && cell->thread) {
  435. #ifdef __linux__
  436. pthread_kill(cell->thread, SIGUSR2);
  437. #endif
  438. }
  439. if (type == SYNC_PRIO_MUTEX) {
  440. fprintf(file,
  441. "high-priority waiters count %lu\n",
  442. (ulong) prio_mutex->high_priority_waiters);
  443. }
  444. } else if (type == RW_LOCK_EX
  445. || type == RW_LOCK_WAIT_EX
  446. || type == RW_LOCK_SHARED
  447. || type == PRIO_RW_LOCK_SHARED
  448. || type == PRIO_RW_LOCK_EX) {
  449. fputs((type == RW_LOCK_EX || type == PRIO_RW_LOCK_EX)
  450. ? "X-lock on"
  451. : type == RW_LOCK_WAIT_EX ? "X-lock (wait_ex) on"
  452. : "S-lock on", file);
  453. /* Currently we are unable to tell high priority
  454. RW_LOCK_WAIT_EX waiter from a regular priority one. Assume
  455. it's a regular one. */
  456. if (type == RW_LOCK_EX || type == RW_LOCK_WAIT_EX
  457. || type == RW_LOCK_SHARED) {
  458. rwlock = static_cast<rw_lock_t *>
  459. (cell->old_wait_rw_lock);
  460. } else {
  461. prio_rwlock = static_cast<prio_rw_lock_t *>
  462. (cell->old_wait_rw_lock);
  463. rwlock = &prio_rwlock->base_lock;
  464. }
  465. if (rwlock) {
  466. fprintf(file,
  467. " RW-latch at %p '%s'\n",
  468. (void*) rwlock, rwlock->lock_name);
  469. writer = rw_lock_get_writer(rwlock);
  470. if (writer && writer != RW_LOCK_NOT_LOCKED) {
  471. fprintf(file,
  472. "a writer (thread id %lu) has"
  473. " reserved it in mode %s",
  474. (ulong) os_thread_pf(rwlock->writer_thread),
  475. writer == RW_LOCK_EX
  476. ? " exclusive\n"
  477. : " wait exclusive\n");
  478. *reserver = rwlock->writer_thread;
  479. }
  480. fprintf(file,
  481. "number of readers %lu, waiters flag %lu, "
  482. "lock_word: %lx\n"
  483. "Last time read locked in file %s line %lu\n"
  484. "Last time write locked in file %s line %lu\n",
  485. (ulong) rw_lock_get_reader_count(rwlock),
  486. (ulong) rwlock->waiters,
  487. rwlock->lock_word,
  488. innobase_basename(rwlock->last_s_file_name),
  489. (ulong) rwlock->last_s_line,
  490. rwlock->last_x_file_name,
  491. (ulong) rwlock->last_x_line);
  492. /* If stacktrace feature is enabled we will send a SIGUSR2
  493. signal to thread that has locked RW-latch with write mode.
  494. Signal handler will then dump the current stack to error log. */
  495. if (writer != RW_LOCK_NOT_LOCKED && srv_use_stacktrace &&
  496. rwlock && rwlock->writer_thread) {
  497. #ifdef __linux__
  498. pthread_kill(rwlock->writer_thread, SIGUSR2);
  499. #endif
  500. }
  501. }
  502. if (prio_rwlock) {
  503. fprintf(file, "high priority S waiters count %lu, "
  504. "high priority X waiters count %lu, "
  505. "wait-exclusive waiter is "
  506. "high priority if exists: %lu\n",
  507. prio_rwlock->high_priority_s_waiters,
  508. prio_rwlock->high_priority_x_waiters,
  509. prio_rwlock->high_priority_wait_ex_waiter);
  510. }
  511. } else {
  512. ut_error;
  513. }
  514. if (!cell->waiting) {
  515. fputs("wait has ended\n", file);
  516. }
  517. }
  518. #ifdef UNIV_SYNC_DEBUG
  519. /******************************************************************//**
  520. Recursion step for deadlock detection.
  521. @return TRUE if deadlock detected */
  522. static
  523. ibool
  524. sync_array_deadlock_step(
  525. /*=====================*/
  526. sync_array_t* arr, /*!< in: wait array; NOTE! the caller must
  527. own the mutex to array */
  528. sync_cell_t* start, /*!< in: cell where recursive search
  529. started */
  530. os_thread_id_t thread, /*!< in: thread to look at */
  531. ulint pass, /*!< in: pass value */
  532. ulint depth) /*!< in: recursion depth */
  533. {
  534. sync_cell_t* new_cell;
  535. if (pass != 0) {
  536. /* If pass != 0, then we do not know which threads are
  537. responsible of releasing the lock, and no deadlock can
  538. be detected. */
  539. return(FALSE);
  540. }
  541. new_cell = sync_array_find_thread(arr, thread);
  542. if (new_cell == start) {
  543. /* Deadlock */
  544. fputs("########################################\n"
  545. "DEADLOCK of threads detected!\n", stderr);
  546. return(TRUE);
  547. } else if (new_cell) {
  548. return(sync_array_detect_deadlock(
  549. arr, start, new_cell, depth + 1));
  550. }
  551. return(FALSE);
  552. }
  553. /******************************************************************//**
  554. This function is called only in the debug version. Detects a deadlock
  555. of one or more threads because of waits of semaphores.
  556. @return TRUE if deadlock detected */
  557. static
  558. ibool
  559. sync_array_detect_deadlock(
  560. /*=======================*/
  561. sync_array_t* arr, /*!< in: wait array; NOTE! the caller must
  562. own the mutex to array */
  563. sync_cell_t* start, /*!< in: cell where recursive search started */
  564. sync_cell_t* cell, /*!< in: cell to search */
  565. ulint depth) /*!< in: recursion depth */
  566. {
  567. ib_mutex_t* mutex;
  568. rw_lock_t* lock;
  569. os_thread_id_t thread;
  570. ibool ret;
  571. rw_lock_debug_t*debug;
  572. os_thread_id_t r = 0;
  573. ut_a(arr);
  574. ut_a(start);
  575. ut_a(cell);
  576. ut_ad(cell->wait_object);
  577. ut_ad(os_thread_get_curr_id() == start->thread);
  578. ut_ad(depth < 100);
  579. depth++;
  580. if (!cell->waiting) {
  581. return(FALSE); /* No deadlock here */
  582. }
  583. if (cell->request_type == SYNC_MUTEX
  584. || cell->request_type == SYNC_PRIO_MUTEX) {
  585. if (cell->request_type == SYNC_MUTEX) {
  586. mutex = static_cast<ib_mutex_t*>(cell->wait_object);
  587. } else {
  588. mutex = &(static_cast<ib_prio_mutex_t*>(
  589. cell->wait_object))->base_mutex;
  590. }
  591. if (mutex_get_lock_word(mutex) != 0) {
  592. thread = mutex->thread_id;
  593. /* Note that mutex->thread_id above may be
  594. also OS_THREAD_ID_UNDEFINED, because the
  595. thread which held the mutex maybe has not
  596. yet updated the value, or it has already
  597. released the mutex: in this case no deadlock
  598. can occur, as the wait array cannot contain
  599. a thread with ID_UNDEFINED value. */
  600. ret = sync_array_deadlock_step(arr, start, thread, 0,
  601. depth);
  602. if (ret) {
  603. fprintf(stderr,
  604. "Mutex %p owned by thread %lu file %s line %lu\n",
  605. mutex, (ulong) os_thread_pf(mutex->thread_id),
  606. mutex->file_name, (ulong) mutex->line);
  607. sync_array_cell_print(stderr, cell, &r);
  608. return(TRUE);
  609. }
  610. }
  611. return(FALSE); /* No deadlock */
  612. } else if (cell->request_type == RW_LOCK_EX
  613. || cell->request_type == PRIO_RW_LOCK_EX
  614. || cell->request_type == RW_LOCK_WAIT_EX) {
  615. lock = static_cast<rw_lock_t*>(cell->wait_object);
  616. for (debug = UT_LIST_GET_FIRST(lock->debug_list);
  617. debug != 0;
  618. debug = UT_LIST_GET_NEXT(list, debug)) {
  619. thread = debug->thread_id;
  620. if (((debug->lock_type == RW_LOCK_EX)
  621. && !os_thread_eq(thread, cell->thread))
  622. || ((debug->lock_type == RW_LOCK_WAIT_EX)
  623. && !os_thread_eq(thread, cell->thread))
  624. || (debug->lock_type == RW_LOCK_SHARED)) {
  625. /* The (wait) x-lock request can block
  626. infinitely only if someone (can be also cell
  627. thread) is holding s-lock, or someone
  628. (cannot be cell thread) (wait) x-lock, and
  629. he is blocked by start thread */
  630. ret = sync_array_deadlock_step(
  631. arr, start, thread, debug->pass,
  632. depth);
  633. if (ret) {
  634. print:
  635. fprintf(stderr, "rw-lock %p ",
  636. (void*) lock);
  637. sync_array_cell_print(stderr, cell, &r);
  638. rw_lock_debug_print(stderr, debug);
  639. return(TRUE);
  640. }
  641. }
  642. }
  643. return(FALSE);
  644. } else if (cell->request_type == RW_LOCK_SHARED
  645. || cell->request_type == PRIO_RW_LOCK_SHARED) {
  646. lock = static_cast<rw_lock_t*>(cell->wait_object);
  647. for (debug = UT_LIST_GET_FIRST(lock->debug_list);
  648. debug != 0;
  649. debug = UT_LIST_GET_NEXT(list, debug)) {
  650. thread = debug->thread_id;
  651. if ((debug->lock_type == RW_LOCK_EX)
  652. || (debug->lock_type == RW_LOCK_WAIT_EX)) {
  653. /* The s-lock request can block infinitely
  654. only if someone (can also be cell thread) is
  655. holding (wait) x-lock, and he is blocked by
  656. start thread */
  657. ret = sync_array_deadlock_step(
  658. arr, start, thread, debug->pass,
  659. depth);
  660. if (ret) {
  661. goto print;
  662. }
  663. }
  664. }
  665. return(FALSE);
  666. } else {
  667. ut_error;
  668. }
  669. return(TRUE); /* Execution never reaches this line: for compiler
  670. fooling only */
  671. }
  672. #endif /* UNIV_SYNC_DEBUG */
  673. /******************************************************************//**
  674. Determines if we can wake up the thread waiting for a sempahore. */
  675. static
  676. ibool
  677. sync_arr_cell_can_wake_up(
  678. /*======================*/
  679. sync_cell_t* cell) /*!< in: cell to search */
  680. {
  681. ib_mutex_t* mutex;
  682. rw_lock_t* lock;
  683. if (cell->request_type == SYNC_MUTEX
  684. || cell->request_type == SYNC_PRIO_MUTEX) {
  685. if (cell->request_type == SYNC_MUTEX) {
  686. mutex = static_cast<ib_mutex_t*>(cell->wait_object);
  687. } else {
  688. mutex = &(static_cast<ib_prio_mutex_t*>(
  689. cell->wait_object))->base_mutex;
  690. }
  691. os_rmb;
  692. if (mutex_get_lock_word(mutex) == 0) {
  693. return(TRUE);
  694. }
  695. } else if (cell->request_type == RW_LOCK_EX
  696. || cell->request_type == PRIO_RW_LOCK_EX) {
  697. lock = static_cast<rw_lock_t*>(cell->wait_object);
  698. os_rmb;
  699. if (lock->lock_word > 0) {
  700. /* Either unlocked or only read locked. */
  701. return(TRUE);
  702. }
  703. } else if (cell->request_type == RW_LOCK_WAIT_EX) {
  704. lock = static_cast<rw_lock_t*>(cell->wait_object);
  705. /* lock_word == 0 means all readers have left */
  706. os_rmb;
  707. if (lock->lock_word == 0) {
  708. return(TRUE);
  709. }
  710. } else if (cell->request_type == RW_LOCK_SHARED
  711. || cell->request_type == PRIO_RW_LOCK_SHARED) {
  712. lock = static_cast<rw_lock_t*>(cell->wait_object);
  713. /* lock_word > 0 means no writer or reserved writer */
  714. os_rmb;
  715. if (lock->lock_word > 0) {
  716. return(TRUE);
  717. }
  718. } else {
  719. ut_error;
  720. }
  721. return(FALSE);
  722. }
  723. /******************************************************************//**
  724. Frees the cell. NOTE! sync_array_wait_event frees the cell
  725. automatically! */
  726. UNIV_INTERN
  727. void
  728. sync_array_free_cell(
  729. /*=================*/
  730. sync_array_t* arr, /*!< in: wait array */
  731. ulint index) /*!< in: index of the cell in array */
  732. {
  733. sync_cell_t* cell;
  734. sync_array_enter(arr);
  735. cell = sync_array_get_nth_cell(arr, index);
  736. ut_a(cell->wait_object != NULL);
  737. cell->waiting = FALSE;
  738. cell->wait_object = NULL;
  739. cell->signal_count = 0;
  740. ut_a(arr->n_reserved > 0);
  741. arr->n_reserved--;
  742. sync_array_exit(arr);
  743. }
  744. /**********************************************************************//**
  745. Increments the signalled count. */
  746. UNIV_INTERN
  747. void
  748. sync_array_object_signalled(void)
  749. /*=============================*/
  750. {
  751. #ifdef HAVE_ATOMIC_BUILTINS
  752. (void) os_atomic_increment_ulint(&sg_count, 1);
  753. #else
  754. ++sg_count;
  755. #endif /* HAVE_ATOMIC_BUILTINS */
  756. }
  757. /**********************************************************************//**
  758. If the wakeup algorithm does not work perfectly at semaphore relases,
  759. this function will do the waking (see the comment in mutex_exit). This
  760. function should be called about every 1 second in the server.
  761. Note that there's a race condition between this thread and mutex_exit
  762. changing the lock_word and calling signal_object, so sometimes this finds
  763. threads to wake up even when nothing has gone wrong. */
  764. static
  765. void
  766. sync_array_wake_threads_if_sema_free_low(
  767. /*=====================================*/
  768. sync_array_t* arr) /* in/out: wait array */
  769. {
  770. ulint i = 0;
  771. ulint count;
  772. sync_array_enter(arr);
  773. for (count = 0; count < arr->n_reserved; ++i) {
  774. sync_cell_t* cell;
  775. cell = sync_array_get_nth_cell(arr, i);
  776. if (cell->wait_object != NULL) {
  777. count++;
  778. if (sync_arr_cell_can_wake_up(cell)) {
  779. os_event_t event;
  780. event = sync_cell_get_event(cell);
  781. os_event_set(event);
  782. }
  783. }
  784. }
  785. sync_array_exit(arr);
  786. }
  787. /**********************************************************************//**
  788. If the wakeup algorithm does not work perfectly at semaphore relases,
  789. this function will do the waking (see the comment in mutex_exit). This
  790. function should be called about every 1 second in the server.
  791. Note that there's a race condition between this thread and mutex_exit
  792. changing the lock_word and calling signal_object, so sometimes this finds
  793. threads to wake up even when nothing has gone wrong. */
  794. UNIV_INTERN
  795. void
  796. sync_arr_wake_threads_if_sema_free(void)
  797. /*====================================*/
  798. {
  799. ulint i;
  800. for (i = 0; i < sync_array_size; ++i) {
  801. sync_array_wake_threads_if_sema_free_low(
  802. sync_wait_array[i]);
  803. }
  804. }
  805. /**********************************************************************//**
  806. Prints warnings of long semaphore waits to stderr.
  807. @return TRUE if fatal semaphore wait threshold was exceeded */
  808. static
  809. ibool
  810. sync_array_print_long_waits_low(
  811. /*============================*/
  812. sync_array_t* arr, /*!< in: sync array instance */
  813. os_thread_id_t* waiter, /*!< out: longest waiting thread */
  814. const void** sema, /*!< out: longest-waited-for semaphore */
  815. ibool* noticed)/*!< out: TRUE if long wait noticed */
  816. {
  817. ulint i;
  818. ulint fatal_timeout = srv_fatal_semaphore_wait_threshold;
  819. ibool fatal = FALSE;
  820. double longest_diff = 0;
  821. /* For huge tables, skip the check during CHECK TABLE etc... */
  822. if (fatal_timeout > SRV_SEMAPHORE_WAIT_EXTENSION) {
  823. return(FALSE);
  824. }
  825. #ifdef UNIV_DEBUG_VALGRIND
  826. /* Increase the timeouts if running under valgrind because it executes
  827. extremely slowly. UNIV_DEBUG_VALGRIND does not necessary mean that
  828. we are running under valgrind but we have no better way to tell.
  829. See Bug#58432 innodb.innodb_bug56143 fails under valgrind
  830. for an example */
  831. # define SYNC_ARRAY_TIMEOUT 2400
  832. fatal_timeout *= 10;
  833. #else
  834. # define SYNC_ARRAY_TIMEOUT 240
  835. #endif
  836. for (i = 0; i < arr->n_cells; i++) {
  837. double diff;
  838. sync_cell_t* cell;
  839. void* wait_object;
  840. os_thread_id_t reserver=0;
  841. cell = sync_array_get_nth_cell(arr, i);
  842. wait_object = cell->wait_object;
  843. if (wait_object == NULL || !cell->waiting) {
  844. continue;
  845. }
  846. diff = difftime(time(NULL), cell->reservation_time);
  847. if (diff > SYNC_ARRAY_TIMEOUT) {
  848. fputs("InnoDB: Warning: a long semaphore wait:\n",
  849. stderr);
  850. sync_array_cell_print(stderr, cell, &reserver);
  851. *noticed = TRUE;
  852. }
  853. if (diff > fatal_timeout) {
  854. fatal = TRUE;
  855. }
  856. if (diff > longest_diff) {
  857. longest_diff = diff;
  858. *sema = wait_object;
  859. *waiter = cell->thread;
  860. }
  861. }
  862. /* We found a long semaphore wait, wait all threads that are
  863. waiting for a semaphore. */
  864. if (*noticed) {
  865. for (i = 0; i < arr->n_cells; i++) {
  866. void* wait_object;
  867. sync_cell_t* cell;
  868. os_thread_id_t reserver=(os_thread_id_t)ULINT_UNDEFINED;
  869. ulint loop=0;
  870. cell = sync_array_get_nth_cell(arr, i);
  871. wait_object = cell->wait_object;
  872. if (wait_object == NULL || !cell->waiting) {
  873. continue;
  874. }
  875. fputs("InnoDB: Warning: semaphore wait:\n",
  876. stderr);
  877. sync_array_cell_print(stderr, cell, &reserver);
  878. /* Try to output cell information for writer recursive way */
  879. while (reserver != (os_thread_id_t)ULINT_UNDEFINED) {
  880. sync_cell_t* reserver_wait;
  881. reserver_wait = sync_array_find_thread(arr, reserver);
  882. if (reserver_wait &&
  883. reserver_wait->wait_object != NULL &&
  884. reserver_wait->waiting) {
  885. fputs("InnoDB: Warning: Writer thread is waiting this semaphore:\n",
  886. stderr);
  887. sync_array_cell_print(stderr, reserver_wait, &reserver);
  888. if (reserver_wait->thread == reserver) {
  889. reserver = (os_thread_id_t)ULINT_UNDEFINED;
  890. }
  891. } else {
  892. reserver = (os_thread_id_t)ULINT_UNDEFINED;
  893. }
  894. /* This is protection against loop */
  895. if (loop > 100) {
  896. fputs("InnoDB: Warning: Too many waiting threads.\n", stderr);
  897. break;
  898. }
  899. }
  900. }
  901. }
  902. #undef SYNC_ARRAY_TIMEOUT
  903. return(fatal);
  904. }
  905. /**********************************************************************//**
  906. Prints warnings of long semaphore waits to stderr.
  907. @return TRUE if fatal semaphore wait threshold was exceeded */
  908. UNIV_INTERN
  909. ibool
  910. sync_array_print_long_waits(
  911. /*========================*/
  912. os_thread_id_t* waiter, /*!< out: longest waiting thread */
  913. const void** sema) /*!< out: longest-waited-for semaphore */
  914. {
  915. ulint i;
  916. ibool fatal = FALSE;
  917. ibool noticed = FALSE;
  918. for (i = 0; i < sync_array_size; ++i) {
  919. sync_array_t* arr = sync_wait_array[i];
  920. sync_array_enter(arr);
  921. if (sync_array_print_long_waits_low(
  922. arr, waiter, sema, &noticed)) {
  923. fatal = TRUE;
  924. }
  925. sync_array_exit(arr);
  926. }
  927. if (noticed) {
  928. ibool old_val;
  929. fprintf(stderr,
  930. "InnoDB: ###### Starts InnoDB Monitor"
  931. " for 30 secs to print diagnostic info:\n");
  932. old_val = srv_print_innodb_monitor;
  933. /* If some crucial semaphore is reserved, then also the InnoDB
  934. Monitor can hang, and we do not get diagnostics. Since in
  935. many cases an InnoDB hang is caused by a pwrite() or a pread()
  936. call hanging inside the operating system, let us print right
  937. now the values of pending calls of these. */
  938. fprintf(stderr,
  939. "InnoDB: Pending reads " UINT64PF
  940. ", writes " UINT64PF "\n",
  941. MONITOR_VALUE(MONITOR_OS_PENDING_READS),
  942. MONITOR_VALUE(MONITOR_OS_PENDING_WRITES));
  943. srv_print_innodb_monitor = TRUE;
  944. os_event_set(srv_monitor_event);
  945. os_thread_sleep(30000000);
  946. srv_print_innodb_monitor = static_cast<my_bool>(old_val);
  947. fprintf(stderr,
  948. "InnoDB: ###### Diagnostic info printed"
  949. " to the standard error stream\n");
  950. }
  951. return(fatal);
  952. }
  953. /**********************************************************************//**
  954. Prints info of the wait array. */
  955. static
  956. void
  957. sync_array_print_info_low(
  958. /*======================*/
  959. FILE* file, /*!< in: file where to print */
  960. sync_array_t* arr) /*!< in: wait array */
  961. {
  962. ulint i;
  963. ulint count = 0;
  964. fprintf(file,
  965. "OS WAIT ARRAY INFO: reservation count " ULINTPF "\n",
  966. arr->res_count);
  967. for (i = 0; count < arr->n_reserved; ++i) {
  968. sync_cell_t* cell;
  969. os_thread_id_t r = 0;
  970. cell = sync_array_get_nth_cell(arr, i);
  971. if (cell->wait_object != NULL) {
  972. count++;
  973. sync_array_cell_print(file, cell, &r);
  974. }
  975. }
  976. }
  977. /**********************************************************************//**
  978. Prints info of the wait array. */
  979. static
  980. void
  981. sync_array_print_info(
  982. /*==================*/
  983. FILE* file, /*!< in: file where to print */
  984. sync_array_t* arr) /*!< in: wait array */
  985. {
  986. sync_array_enter(arr);
  987. sync_array_print_info_low(file, arr);
  988. sync_array_exit(arr);
  989. }
  990. /**********************************************************************//**
  991. Create the primary system wait array(s), they are protected by an OS mutex */
  992. UNIV_INTERN
  993. void
  994. sync_array_init(
  995. /*============*/
  996. ulint n_threads) /*!< in: Number of slots to
  997. create in all arrays */
  998. {
  999. ulint i;
  1000. ulint n_slots;
  1001. ut_a(sync_wait_array == NULL);
  1002. ut_a(srv_sync_array_size > 0);
  1003. ut_a(n_threads > 0);
  1004. sync_array_size = srv_sync_array_size;
  1005. /* We have to use ut_malloc() because the mutex infrastructure
  1006. hasn't been initialised yet. It is required by mem_alloc() and
  1007. the heap functions. */
  1008. sync_wait_array = static_cast<sync_array_t**>(
  1009. ut_malloc(sizeof(*sync_wait_array) * sync_array_size));
  1010. n_slots = 1 + (n_threads - 1) / sync_array_size;
  1011. for (i = 0; i < sync_array_size; ++i) {
  1012. sync_wait_array[i] = sync_array_create(n_slots);
  1013. }
  1014. }
  1015. /**********************************************************************//**
  1016. Close sync array wait sub-system. */
  1017. UNIV_INTERN
  1018. void
  1019. sync_array_close(void)
  1020. /*==================*/
  1021. {
  1022. ulint i;
  1023. for (i = 0; i < sync_array_size; ++i) {
  1024. sync_array_free(sync_wait_array[i]);
  1025. }
  1026. ut_free(sync_wait_array);
  1027. sync_wait_array = NULL;
  1028. }
  1029. /**********************************************************************//**
  1030. Print info about the sync array(s). */
  1031. UNIV_INTERN
  1032. void
  1033. sync_array_print(
  1034. /*=============*/
  1035. FILE* file) /*!< in/out: Print to this stream */
  1036. {
  1037. ulint i;
  1038. for (i = 0; i < sync_array_size; ++i) {
  1039. sync_array_print_info(file, sync_wait_array[i]);
  1040. }
  1041. fprintf(file,
  1042. "OS WAIT ARRAY INFO: signal count " ULINTPF "\n", sg_count);
  1043. }
  1044. /**********************************************************************//**
  1045. Get an instance of the sync wait array. */
  1046. UNIV_INTERN
  1047. sync_array_t*
  1048. sync_array_get(void)
  1049. /*================*/
  1050. {
  1051. ulint i;
  1052. static ulint count;
  1053. #ifdef HAVE_ATOMIC_BUILTINS
  1054. i = os_atomic_increment_ulint(&count, 1);
  1055. #else
  1056. i = count++;
  1057. #endif /* HAVE_ATOMIC_BUILTINS */
  1058. return(sync_wait_array[i % sync_array_size]);
  1059. }
  1060. /**********************************************************************//**
  1061. Prints info of the wait array without using any mutexes/semaphores. */
  1062. UNIV_INTERN
  1063. void
  1064. sync_array_print_xtradb(void)
  1065. /*=========================*/
  1066. {
  1067. ulint i;
  1068. sync_array_t* arr = sync_array_get();
  1069. fputs("InnoDB: Semaphore wait debug output started for XtraDB:\n", stderr);
  1070. for (i = 0; i < arr->n_cells; i++) {
  1071. void* wait_object;
  1072. sync_cell_t* cell;
  1073. os_thread_id_t reserver=(os_thread_id_t)ULINT_UNDEFINED;
  1074. ulint loop=0;
  1075. cell = sync_array_get_nth_cell(arr, i);
  1076. wait_object = cell->wait_object;
  1077. if (wait_object == NULL || !cell->waiting) {
  1078. continue;
  1079. }
  1080. fputs("InnoDB: Warning: semaphore wait:\n",
  1081. stderr);
  1082. sync_array_cell_print(stderr, cell, &reserver);
  1083. /* Try to output cell information for writer recursive way */
  1084. while (reserver != (os_thread_id_t)ULINT_UNDEFINED) {
  1085. sync_cell_t* reserver_wait;
  1086. reserver_wait = sync_array_find_thread(arr, reserver);
  1087. if (reserver_wait &&
  1088. reserver_wait->wait_object != NULL &&
  1089. reserver_wait->waiting) {
  1090. fputs("InnoDB: Warning: Writer thread is waiting this semaphore:\n",
  1091. stderr);
  1092. sync_array_cell_print(stderr, reserver_wait, &reserver);
  1093. if (reserver_wait->thread == reserver) {
  1094. reserver = (os_thread_id_t)ULINT_UNDEFINED;
  1095. }
  1096. } else {
  1097. reserver = (os_thread_id_t)ULINT_UNDEFINED;
  1098. }
  1099. /* This is protection against loop */
  1100. if (loop > 100) {
  1101. fputs("InnoDB: Warning: Too many waiting threads.\n", stderr);
  1102. break;
  1103. }
  1104. }
  1105. }
  1106. fputs("InnoDB: Semaphore wait debug output ended:\n", stderr);
  1107. }