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.

1211 lines
31 KiB

17 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
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
15 years ago
15 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
17 years ago
17 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
15 years ago
15 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
17 years ago
17 years ago
17 years ago
17 years ago
17 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
15 years ago
15 years ago
16 years ago
16 years ago
16 years ago
16 years ago
  1. /*****************************************************************************
  2. Copyright (c) 1995, 2011, 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 St, Fifth Floor, Boston, MA 02110-1301 USA
  19. *****************************************************************************/
  20. /**************************************************//**
  21. @file sync/sync0arr.c
  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 "srv0srv.h"
  34. #include "ha_prototypes.h"
  35. /*
  36. WAIT ARRAY
  37. ==========
  38. The wait array consists of cells each of which has an
  39. an operating system event object created for it. The threads
  40. waiting for a mutex, for example, can reserve a cell
  41. in the array and suspend themselves to wait for the event
  42. to become signaled. When using the wait array, remember to make
  43. sure that some thread holding the synchronization object
  44. will eventually know that there is a waiter in the array and
  45. signal the object, to prevent infinite wait.
  46. Why we chose to implement a wait array? First, to make
  47. mutexes fast, we had to code our own implementation of them,
  48. which only in usually uncommon cases resorts to using
  49. slow operating system primitives. Then we had the choice of
  50. assigning a unique OS event for each mutex, which would
  51. be simpler, or using a global wait array. In some operating systems,
  52. the global wait array solution is more efficient and flexible,
  53. because we can do with a very small number of OS events,
  54. say 200. In NT 3.51, allocating events seems to be a quadratic
  55. algorithm, because 10 000 events are created fast, but
  56. 100 000 events takes a couple of minutes to create.
  57. As of 5.0.30 the above mentioned design is changed. Since now
  58. OS can handle millions of wait events efficiently, we no longer
  59. have this concept of each cell of wait array having one event.
  60. Instead, now the event that a thread wants to wait on is embedded
  61. in the wait object (mutex or rw_lock). We still keep the global
  62. wait array for the sake of diagnostics and also to avoid infinite
  63. wait The error_monitor thread scans the global wait array to signal
  64. any waiting threads who have missed the signal. */
  65. /** A cell where an individual thread may wait suspended
  66. until a resource is released. The suspending is implemented
  67. using an operating system event semaphore. */
  68. struct sync_cell_struct {
  69. void* wait_object; /*!< pointer to the object the
  70. thread is waiting for; if NULL
  71. the cell is free for use */
  72. mutex_t* old_wait_mutex; /*!< the latest wait mutex in cell */
  73. rw_lock_t* old_wait_rw_lock;
  74. /*!< the latest wait rw-lock
  75. in cell */
  76. ulint request_type; /*!< lock type requested on the
  77. object */
  78. const char* file; /*!< in debug version file where
  79. requested */
  80. ulint line; /*!< in debug version line where
  81. requested */
  82. os_thread_id_t thread; /*!< thread id of this waiting
  83. thread */
  84. ibool waiting; /*!< TRUE if the thread has already
  85. called sync_array_event_wait
  86. on this cell */
  87. ib_int64_t signal_count; /*!< We capture the signal_count
  88. of the wait_object when we
  89. reset the event. This value is
  90. then passed on to os_event_wait
  91. and we wait only if the event
  92. has not been signalled in the
  93. period between the reset and
  94. wait call. */
  95. time_t reservation_time;/*!< time when the thread reserved
  96. the wait cell */
  97. };
  98. /* NOTE: It is allowed for a thread to wait
  99. for an event allocated for the array without owning the
  100. protecting mutex (depending on the case: OS or database mutex), but
  101. all changes (set or reset) to the state of the event must be made
  102. while owning the mutex. */
  103. /** Synchronization array */
  104. struct sync_array_struct {
  105. ulint n_reserved; /*!< number of currently reserved
  106. cells in the wait array */
  107. ulint n_cells; /*!< number of cells in the
  108. wait array */
  109. sync_cell_t* array; /*!< pointer to wait array */
  110. ulint protection; /*!< this flag tells which
  111. mutex protects the data */
  112. mutex_t mutex; /*!< possible database mutex
  113. protecting this data structure */
  114. os_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 sg_count; /*!< count of how many times an
  122. object has been signalled */
  123. ulint res_count; /*!< count of cell reservations
  124. since creation of the array */
  125. };
  126. #ifdef UNIV_PFS_MUTEX
  127. /* Key to register the mutex with performance schema */
  128. UNIV_INTERN mysql_pfs_key_t syn_arr_mutex_key;
  129. #endif
  130. #ifdef UNIV_SYNC_DEBUG
  131. /******************************************************************//**
  132. This function is called only in the debug version. Detects a deadlock
  133. of one or more threads because of waits of semaphores.
  134. @return TRUE if deadlock detected */
  135. static
  136. ibool
  137. sync_array_detect_deadlock(
  138. /*=======================*/
  139. sync_array_t* arr, /*!< in: wait array; NOTE! the caller must
  140. own the mutex to array */
  141. sync_cell_t* start, /*!< in: cell where recursive search started */
  142. sync_cell_t* cell, /*!< in: cell to search */
  143. ulint depth); /*!< in: recursion depth */
  144. #endif /* UNIV_SYNC_DEBUG */
  145. /*****************************************************************//**
  146. Gets the nth cell in array.
  147. @return cell */
  148. static
  149. sync_cell_t*
  150. sync_array_get_nth_cell(
  151. /*====================*/
  152. sync_array_t* arr, /*!< in: sync array */
  153. ulint n) /*!< in: index */
  154. {
  155. ut_a(arr);
  156. ut_a(n < arr->n_cells);
  157. return(arr->array + n);
  158. }
  159. /******************************************************************//**
  160. Reserves the mutex semaphore protecting a sync array. */
  161. static
  162. void
  163. sync_array_enter(
  164. /*=============*/
  165. sync_array_t* arr) /*!< in: sync wait array */
  166. {
  167. ulint protection;
  168. protection = arr->protection;
  169. if (protection == SYNC_ARRAY_OS_MUTEX) {
  170. os_mutex_enter(arr->os_mutex);
  171. } else if (protection == SYNC_ARRAY_MUTEX) {
  172. mutex_enter(&(arr->mutex));
  173. } else {
  174. ut_error;
  175. }
  176. }
  177. /******************************************************************//**
  178. Releases the mutex semaphore protecting a sync array. */
  179. static
  180. void
  181. sync_array_exit(
  182. /*============*/
  183. sync_array_t* arr) /*!< in: sync wait array */
  184. {
  185. ulint protection;
  186. protection = arr->protection;
  187. if (protection == SYNC_ARRAY_OS_MUTEX) {
  188. os_mutex_exit(arr->os_mutex);
  189. } else if (protection == SYNC_ARRAY_MUTEX) {
  190. mutex_exit(&(arr->mutex));
  191. } else {
  192. ut_error;
  193. }
  194. }
  195. /*******************************************************************//**
  196. Creates a synchronization wait array. It is protected by a mutex
  197. which is automatically reserved when the functions operating on it
  198. are called.
  199. @return own: created wait array */
  200. UNIV_INTERN
  201. sync_array_t*
  202. sync_array_create(
  203. /*==============*/
  204. ulint n_cells, /*!< in: number of cells in the array
  205. to create */
  206. ulint protection) /*!< in: either SYNC_ARRAY_OS_MUTEX or
  207. SYNC_ARRAY_MUTEX: determines the type
  208. of mutex protecting the data structure */
  209. {
  210. ulint sz;
  211. sync_array_t* arr;
  212. ut_a(n_cells > 0);
  213. /* Allocate memory for the data structures */
  214. arr = ut_malloc(sizeof(sync_array_t));
  215. memset(arr, 0x0, sizeof(*arr));
  216. sz = sizeof(sync_cell_t) * n_cells;
  217. arr->array = ut_malloc(sz);
  218. memset(arr->array, 0x0, sz);
  219. arr->n_cells = n_cells;
  220. arr->protection = protection;
  221. /* Then create the mutex to protect the wait array complex */
  222. if (protection == SYNC_ARRAY_OS_MUTEX) {
  223. arr->os_mutex = os_mutex_create();
  224. } else if (protection == SYNC_ARRAY_MUTEX) {
  225. mutex_create(syn_arr_mutex_key,
  226. &arr->mutex, SYNC_NO_ORDER_CHECK);
  227. } else {
  228. ut_error;
  229. }
  230. return(arr);
  231. }
  232. /******************************************************************//**
  233. Frees the resources in a wait array. */
  234. UNIV_INTERN
  235. void
  236. sync_array_free(
  237. /*============*/
  238. sync_array_t* arr) /*!< in, own: sync wait array */
  239. {
  240. ulint protection;
  241. ut_a(arr->n_reserved == 0);
  242. sync_array_validate(arr);
  243. protection = arr->protection;
  244. /* Release the mutex protecting the wait array complex */
  245. if (protection == SYNC_ARRAY_OS_MUTEX) {
  246. os_mutex_free(arr->os_mutex);
  247. } else if (protection == SYNC_ARRAY_MUTEX) {
  248. mutex_free(&(arr->mutex));
  249. } else {
  250. ut_error;
  251. }
  252. ut_free(arr->array);
  253. ut_free(arr);
  254. }
  255. /********************************************************************//**
  256. Validates the integrity of the wait array. Checks
  257. that the number of reserved cells equals the count variable. */
  258. UNIV_INTERN
  259. void
  260. sync_array_validate(
  261. /*================*/
  262. sync_array_t* arr) /*!< in: sync wait array */
  263. {
  264. ulint i;
  265. sync_cell_t* cell;
  266. ulint count = 0;
  267. sync_array_enter(arr);
  268. for (i = 0; i < arr->n_cells; i++) {
  269. cell = sync_array_get_nth_cell(arr, i);
  270. if (cell->wait_object != NULL) {
  271. count++;
  272. }
  273. }
  274. ut_a(count == arr->n_reserved);
  275. sync_array_exit(arr);
  276. }
  277. /*******************************************************************//**
  278. Returns the event that the thread owning the cell waits for. */
  279. static
  280. os_event_t
  281. sync_cell_get_event(
  282. /*================*/
  283. sync_cell_t* cell) /*!< in: non-empty sync array cell */
  284. {
  285. ulint type = cell->request_type;
  286. if (type == SYNC_MUTEX) {
  287. return(((mutex_t *) cell->wait_object)->event);
  288. } else if (type == RW_LOCK_WAIT_EX) {
  289. return(((rw_lock_t *) cell->wait_object)->wait_ex_event);
  290. } else { /* RW_LOCK_SHARED and RW_LOCK_EX wait on the same event */
  291. return(((rw_lock_t *) cell->wait_object)->event);
  292. }
  293. }
  294. /******************************************************************//**
  295. Reserves a wait array cell for waiting for an object.
  296. The event of the cell is reset to nonsignalled state. */
  297. UNIV_INTERN
  298. void
  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) {
  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 = time(NULL);
  338. cell->thread = os_thread_get_curr_id();
  339. return;
  340. }
  341. }
  342. ut_error; /* No free cell found */
  343. return;
  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. mutex_t* mutex = NULL;
  395. rw_lock_t* rwlock = NULL;
  396. ulint type;
  397. ulint writer;
  398. type = cell->request_type;
  399. fprintf(file,
  400. "--Thread %lu has waited at %s line %lu"
  401. " for %#.5g seconds the semaphore:\n",
  402. (ulong) os_thread_pf(cell->thread),
  403. innobase_basename(cell->file), (ulong) cell->line,
  404. difftime(time(NULL), cell->reservation_time));
  405. if (type == SYNC_MUTEX) {
  406. /* We use old_wait_mutex in case the cell has already
  407. been freed meanwhile */
  408. mutex = cell->old_wait_mutex;
  409. if (mutex) {
  410. fprintf(file,
  411. "Mutex at %p '%s', lock var %lu\n"
  412. #ifdef UNIV_SYNC_DEBUG
  413. "Last time reserved in file %s line %lu, "
  414. #endif /* UNIV_SYNC_DEBUG */
  415. "waiters flag %lu\n",
  416. (void*) mutex, mutex->cmutex_name,
  417. (ulong) mutex->lock_word,
  418. #ifdef UNIV_SYNC_DEBUG
  419. mutex->file_name, (ulong) mutex->line,
  420. #endif /* UNIV_SYNC_DEBUG */
  421. (ulong) mutex->waiters);
  422. }
  423. /* If stacktrace feature is enabled we will send a SIGUSR2
  424. signal to thread waiting for the semaphore. Signal handler
  425. will then dump the current stack to error log. */
  426. if (srv_use_stacktrace && cell && cell->thread) {
  427. #ifdef __linux__
  428. pthread_kill(cell->thread, SIGUSR2);
  429. #endif
  430. }
  431. } else if (type == RW_LOCK_EX
  432. || type == RW_LOCK_WAIT_EX
  433. || type == RW_LOCK_SHARED) {
  434. fputs(type == RW_LOCK_EX ? "X-lock on"
  435. : type == RW_LOCK_WAIT_EX ? "X-lock (wait_ex) on"
  436. : "S-lock on", file);
  437. rwlock = cell->old_wait_rw_lock;
  438. if (rwlock) {
  439. fprintf(file,
  440. " RW-latch at %p '%s'\n",
  441. (void*) rwlock, rwlock->lock_name);
  442. writer = rw_lock_get_writer(rwlock);
  443. if (writer && writer != RW_LOCK_NOT_LOCKED) {
  444. fprintf(file,
  445. "a writer (thread id %lu) has"
  446. " reserved it in mode %s",
  447. (ulong) os_thread_pf(rwlock->writer_thread),
  448. writer == RW_LOCK_EX
  449. ? " exclusive\n"
  450. : " wait exclusive\n");
  451. *reserver = rwlock->writer_thread;
  452. }
  453. fprintf(file,
  454. "number of readers %lu, waiters flag %lu, "
  455. "lock_word: %lx\n"
  456. "Last time read locked in file %s line %lu\n"
  457. "Last time write locked in file %s line %lu\n",
  458. (ulong) rw_lock_get_reader_count(rwlock),
  459. (ulong) rwlock->waiters,
  460. rwlock->lock_word,
  461. innobase_basename(rwlock->last_s_file_name),
  462. (ulong) rwlock->last_s_line,
  463. rwlock->last_x_file_name,
  464. (ulong) rwlock->last_x_line);
  465. /* If stacktrace feature is enabled we will send a SIGUSR2
  466. signal to thread that has locked RW-latch with write mode.
  467. Signal handler will then dump the current stack to error log. */
  468. if (writer != RW_LOCK_NOT_LOCKED && srv_use_stacktrace &&
  469. rwlock && rwlock->writer_thread) {
  470. #ifdef __linux__
  471. pthread_kill(rwlock->writer_thread, SIGUSR2);
  472. #endif
  473. }
  474. }
  475. } else {
  476. ut_error;
  477. }
  478. if (!cell->waiting) {
  479. fputs("wait has ended\n", file);
  480. }
  481. }
  482. /******************************************************************//**
  483. Looks for a cell with the given thread id.
  484. @return pointer to cell or NULL if not found */
  485. static
  486. sync_cell_t*
  487. sync_array_find_thread(
  488. /*===================*/
  489. sync_array_t* arr, /*!< in: wait array */
  490. os_thread_id_t thread) /*!< in: thread id */
  491. {
  492. ulint i;
  493. sync_cell_t* cell;
  494. for (i = 0; i < arr->n_cells; i++) {
  495. cell = sync_array_get_nth_cell(arr, i);
  496. if (cell->wait_object != NULL
  497. && os_thread_eq(cell->thread, thread)) {
  498. return(cell); /* Found */
  499. }
  500. }
  501. return(NULL); /* Not found */
  502. }
  503. #ifdef UNIV_SYNC_DEBUG
  504. /******************************************************************//**
  505. Recursion step for deadlock detection.
  506. @return TRUE if deadlock detected */
  507. static
  508. ibool
  509. sync_array_deadlock_step(
  510. /*=====================*/
  511. sync_array_t* arr, /*!< in: wait array; NOTE! the caller must
  512. own the mutex to array */
  513. sync_cell_t* start, /*!< in: cell where recursive search
  514. started */
  515. os_thread_id_t thread, /*!< in: thread to look at */
  516. ulint pass, /*!< in: pass value */
  517. ulint depth) /*!< in: recursion depth */
  518. {
  519. sync_cell_t* new;
  520. if (pass != 0) {
  521. /* If pass != 0, then we do not know which threads are
  522. responsible of releasing the lock, and no deadlock can
  523. be detected. */
  524. return(FALSE);
  525. }
  526. new = sync_array_find_thread(arr, thread);
  527. if (UNIV_UNLIKELY(new == start)) {
  528. /* Deadlock */
  529. fputs("########################################\n"
  530. "DEADLOCK of threads detected!\n", stderr);
  531. return(TRUE);
  532. } else if (new) {
  533. return(sync_array_detect_deadlock(arr, start, new, depth + 1));
  534. }
  535. return(FALSE);
  536. }
  537. /******************************************************************//**
  538. This function is called only in the debug version. Detects a deadlock
  539. of one or more threads because of waits of semaphores.
  540. @return TRUE if deadlock detected */
  541. static
  542. ibool
  543. sync_array_detect_deadlock(
  544. /*=======================*/
  545. sync_array_t* arr, /*!< in: wait array; NOTE! the caller must
  546. own the mutex to array */
  547. sync_cell_t* start, /*!< in: cell where recursive search started */
  548. sync_cell_t* cell, /*!< in: cell to search */
  549. ulint depth) /*!< in: recursion depth */
  550. {
  551. mutex_t* mutex;
  552. rw_lock_t* lock;
  553. os_thread_id_t thread;
  554. ibool ret;
  555. rw_lock_debug_t*debug;
  556. ut_a(arr);
  557. ut_a(start);
  558. ut_a(cell);
  559. ut_ad(cell->wait_object);
  560. ut_ad(os_thread_get_curr_id() == start->thread);
  561. ut_ad(depth < 100);
  562. depth++;
  563. if (!cell->waiting) {
  564. return(FALSE); /* No deadlock here */
  565. }
  566. if (cell->request_type == SYNC_MUTEX) {
  567. mutex = cell->wait_object;
  568. if (mutex_get_lock_word(mutex) != 0) {
  569. thread = mutex->thread_id;
  570. /* Note that mutex->thread_id above may be
  571. also OS_THREAD_ID_UNDEFINED, because the
  572. thread which held the mutex maybe has not
  573. yet updated the value, or it has already
  574. released the mutex: in this case no deadlock
  575. can occur, as the wait array cannot contain
  576. a thread with ID_UNDEFINED value. */
  577. ret = sync_array_deadlock_step(arr, start, thread, 0,
  578. depth);
  579. if (ret) {
  580. fprintf(stderr,
  581. "Mutex %p owned by thread %lu file %s line %lu\n",
  582. mutex, (ulong) os_thread_pf(mutex->thread_id),
  583. mutex->file_name, (ulong) mutex->line);
  584. sync_array_cell_print(stderr, cell);
  585. return(TRUE);
  586. }
  587. }
  588. return(FALSE); /* No deadlock */
  589. } else if (cell->request_type == RW_LOCK_EX
  590. || cell->request_type == RW_LOCK_WAIT_EX) {
  591. lock = cell->wait_object;
  592. debug = UT_LIST_GET_FIRST(lock->debug_list);
  593. while (debug != NULL) {
  594. thread = debug->thread_id;
  595. if (((debug->lock_type == RW_LOCK_EX)
  596. && !os_thread_eq(thread, cell->thread))
  597. || ((debug->lock_type == RW_LOCK_WAIT_EX)
  598. && !os_thread_eq(thread, cell->thread))
  599. || (debug->lock_type == RW_LOCK_SHARED)) {
  600. /* The (wait) x-lock request can block
  601. infinitely only if someone (can be also cell
  602. thread) is holding s-lock, or someone
  603. (cannot be cell thread) (wait) x-lock, and
  604. he is blocked by start thread */
  605. ret = sync_array_deadlock_step(
  606. arr, start, thread, debug->pass,
  607. depth);
  608. if (ret) {
  609. print:
  610. fprintf(stderr, "rw-lock %p ",
  611. (void*) lock);
  612. sync_array_cell_print(stderr, cell);
  613. rw_lock_debug_print(stderr, debug);
  614. return(TRUE);
  615. }
  616. }
  617. debug = UT_LIST_GET_NEXT(list, debug);
  618. }
  619. return(FALSE);
  620. } else if (cell->request_type == RW_LOCK_SHARED) {
  621. lock = cell->wait_object;
  622. debug = UT_LIST_GET_FIRST(lock->debug_list);
  623. while (debug != NULL) {
  624. thread = debug->thread_id;
  625. if ((debug->lock_type == RW_LOCK_EX)
  626. || (debug->lock_type == RW_LOCK_WAIT_EX)) {
  627. /* The s-lock request can block infinitely
  628. only if someone (can also be cell thread) is
  629. holding (wait) x-lock, and he is blocked by
  630. start thread */
  631. ret = sync_array_deadlock_step(
  632. arr, start, thread, debug->pass,
  633. depth);
  634. if (ret) {
  635. goto print;
  636. }
  637. }
  638. debug = UT_LIST_GET_NEXT(list, debug);
  639. }
  640. return(FALSE);
  641. } else {
  642. ut_error;
  643. }
  644. return(TRUE); /* Execution never reaches this line: for compiler
  645. fooling only */
  646. }
  647. #endif /* UNIV_SYNC_DEBUG */
  648. /******************************************************************//**
  649. Determines if we can wake up the thread waiting for a sempahore. */
  650. static
  651. ibool
  652. sync_arr_cell_can_wake_up(
  653. /*======================*/
  654. sync_cell_t* cell) /*!< in: cell to search */
  655. {
  656. mutex_t* mutex;
  657. rw_lock_t* lock;
  658. if (cell->request_type == SYNC_MUTEX) {
  659. mutex = cell->wait_object;
  660. if (mutex_get_lock_word(mutex) == 0) {
  661. return(TRUE);
  662. }
  663. } else if (cell->request_type == RW_LOCK_EX) {
  664. lock = cell->wait_object;
  665. os_rmb;
  666. if (lock->lock_word > 0) {
  667. /* Either unlocked or only read locked. */
  668. return(TRUE);
  669. }
  670. } else if (cell->request_type == RW_LOCK_WAIT_EX) {
  671. lock = cell->wait_object;
  672. /* lock_word == 0 means all readers have left */
  673. os_rmb;
  674. if (lock->lock_word == 0) {
  675. return(TRUE);
  676. }
  677. } else if (cell->request_type == RW_LOCK_SHARED) {
  678. lock = cell->wait_object;
  679. /* lock_word > 0 means no writer or reserved writer */
  680. os_rmb;
  681. if (lock->lock_word > 0) {
  682. return(TRUE);
  683. }
  684. }
  685. return(FALSE);
  686. }
  687. /******************************************************************//**
  688. Frees the cell. NOTE! sync_array_wait_event frees the cell
  689. automatically! */
  690. UNIV_INTERN
  691. void
  692. sync_array_free_cell(
  693. /*=================*/
  694. sync_array_t* arr, /*!< in: wait array */
  695. ulint index) /*!< in: index of the cell in array */
  696. {
  697. sync_cell_t* cell;
  698. sync_array_enter(arr);
  699. cell = sync_array_get_nth_cell(arr, index);
  700. ut_a(cell->wait_object != NULL);
  701. cell->waiting = FALSE;
  702. cell->wait_object = NULL;
  703. cell->signal_count = 0;
  704. ut_a(arr->n_reserved > 0);
  705. arr->n_reserved--;
  706. sync_array_exit(arr);
  707. }
  708. /**********************************************************************//**
  709. Increments the signalled count. */
  710. UNIV_INTERN
  711. void
  712. sync_array_object_signalled(
  713. /*========================*/
  714. sync_array_t* arr) /*!< in: wait array */
  715. {
  716. #ifdef HAVE_ATOMIC_BUILTINS
  717. (void) os_atomic_increment_ulint(&arr->sg_count, 1);
  718. #else
  719. sync_array_enter(arr);
  720. arr->sg_count++;
  721. sync_array_exit(arr);
  722. #endif
  723. }
  724. /**********************************************************************//**
  725. If the wakeup algorithm does not work perfectly at semaphore relases,
  726. this function will do the waking (see the comment in mutex_exit). This
  727. function should be called about every 1 second in the server.
  728. Note that there's a race condition between this thread and mutex_exit
  729. changing the lock_word and calling signal_object, so sometimes this finds
  730. threads to wake up even when nothing has gone wrong. */
  731. UNIV_INTERN
  732. void
  733. sync_arr_wake_threads_if_sema_free(void)
  734. /*====================================*/
  735. {
  736. sync_array_t* arr = sync_primary_wait_array;
  737. sync_cell_t* cell;
  738. ulint count;
  739. ulint i;
  740. os_event_t event;
  741. sync_array_enter(arr);
  742. i = 0;
  743. count = 0;
  744. while (count < arr->n_reserved) {
  745. cell = sync_array_get_nth_cell(arr, i);
  746. i++;
  747. if (cell->wait_object == NULL) {
  748. continue;
  749. }
  750. count++;
  751. if (sync_arr_cell_can_wake_up(cell)) {
  752. event = sync_cell_get_event(cell);
  753. os_event_set(event);
  754. }
  755. }
  756. sync_array_exit(arr);
  757. }
  758. /**********************************************************************//**
  759. Prints warnings of long semaphore waits to stderr.
  760. @return TRUE if fatal semaphore wait threshold was exceeded */
  761. UNIV_INTERN
  762. ibool
  763. sync_array_print_long_waits(
  764. /*========================*/
  765. os_thread_id_t* waiter, /*!< out: longest waiting thread */
  766. const void** sema) /*!< out: longest-waited-for semaphore */
  767. {
  768. sync_cell_t* cell;
  769. ibool old_val;
  770. ibool noticed = FALSE;
  771. ulint i;
  772. ulint fatal_timeout = srv_fatal_semaphore_wait_threshold;
  773. ibool fatal = FALSE;
  774. double longest_diff = 0;
  775. /* For huge tables, skip the check during CHECK TABLE etc... */
  776. if (fatal_timeout > SRV_SEMAPHORE_WAIT_EXTENSION) {
  777. return(FALSE);
  778. }
  779. #ifdef UNIV_DEBUG_VALGRIND
  780. /* Increase the timeouts if running under valgrind because it executes
  781. extremely slowly. UNIV_DEBUG_VALGRIND does not necessary mean that
  782. we are running under valgrind but we have no better way to tell.
  783. See Bug#58432 innodb.innodb_bug56143 fails under valgrind
  784. for an example */
  785. # define SYNC_ARRAY_TIMEOUT 2400
  786. fatal_timeout *= 10;
  787. #else
  788. # define SYNC_ARRAY_TIMEOUT 240
  789. #endif
  790. sync_array_enter(sync_primary_wait_array);
  791. for (i = 0; i < sync_primary_wait_array->n_cells; i++) {
  792. double diff;
  793. void* wait_object;
  794. os_thread_id_t reserver=0;
  795. cell = sync_array_get_nth_cell(sync_primary_wait_array, i);
  796. wait_object = cell->wait_object;
  797. if (wait_object == NULL || !cell->waiting) {
  798. continue;
  799. }
  800. diff = difftime(time(NULL), cell->reservation_time);
  801. if (diff > SYNC_ARRAY_TIMEOUT) {
  802. fputs("InnoDB: Warning: a long semaphore wait:\n",
  803. stderr);
  804. sync_array_cell_print(stderr, cell, &reserver);
  805. noticed = TRUE;
  806. }
  807. if (diff > fatal_timeout) {
  808. fatal = TRUE;
  809. }
  810. if (diff > longest_diff) {
  811. longest_diff = diff;
  812. *sema = wait_object;
  813. *waiter = cell->thread;
  814. }
  815. }
  816. /* We found a long semaphore wait, wait all threads that are
  817. waiting for a semaphore. */
  818. if (noticed) {
  819. for (i = 0; i < sync_primary_wait_array->n_cells; i++) {
  820. void* wait_object;
  821. os_thread_id_t reserver=ULINT_UNDEFINED;
  822. ulint loop=0;
  823. cell = sync_array_get_nth_cell(sync_primary_wait_array, i);
  824. wait_object = cell->wait_object;
  825. if (wait_object == NULL || !cell->waiting) {
  826. continue;
  827. }
  828. fputs("InnoDB: Warning: semaphore wait:\n",
  829. stderr);
  830. sync_array_cell_print(stderr, cell, &reserver);
  831. noticed = TRUE;
  832. /* Try to output cell information for writer recursive way */
  833. while (reserver != ULINT_UNDEFINED) {
  834. sync_cell_t* reserver_wait;
  835. reserver_wait = sync_array_find_thread(sync_primary_wait_array, reserver);
  836. if (reserver_wait &&
  837. reserver_wait->wait_object != NULL &&
  838. reserver_wait->waiting) {
  839. fputs("InnoDB: Warning: Writer thread is waiting this semaphore:\n",
  840. stderr);
  841. reserver = ULINT_UNDEFINED;
  842. sync_array_cell_print(stderr, reserver_wait, &reserver);
  843. loop++;
  844. if (reserver_wait->thread == reserver) {
  845. reserver = ULINT_UNDEFINED;
  846. }
  847. } else {
  848. reserver = ULINT_UNDEFINED;
  849. }
  850. /* This is protection against loop */
  851. if (loop > 100) {
  852. fputs("InnoDB: Warning: Too many waiting threads.\n", stderr);
  853. break;
  854. }
  855. }
  856. }
  857. }
  858. sync_array_exit(sync_primary_wait_array);
  859. if (noticed) {
  860. fprintf(stderr,
  861. "InnoDB: ###### Starts InnoDB Monitor"
  862. " for 30 secs to print diagnostic info:\n");
  863. old_val = srv_print_innodb_monitor;
  864. /* If some crucial semaphore is reserved, then also the InnoDB
  865. Monitor can hang, and we do not get diagnostics. Since in
  866. many cases an InnoDB hang is caused by a pwrite() or a pread()
  867. call hanging inside the operating system, let us print right
  868. now the values of pending calls of these. */
  869. fprintf(stderr,
  870. "InnoDB: Pending preads %lu, pwrites %lu\n",
  871. (ulong)os_file_n_pending_preads,
  872. (ulong)os_file_n_pending_pwrites);
  873. srv_print_innodb_monitor = TRUE;
  874. os_event_set(srv_lock_timeout_thread_event);
  875. os_thread_sleep(30000000);
  876. srv_print_innodb_monitor = old_val;
  877. fprintf(stderr,
  878. "InnoDB: ###### Diagnostic info printed"
  879. " to the standard error stream\n");
  880. }
  881. #undef SYNC_ARRAY_TIMEOUT
  882. return(fatal);
  883. }
  884. /**********************************************************************//**
  885. Prints info of the wait array. */
  886. static
  887. void
  888. sync_array_output_info(
  889. /*===================*/
  890. FILE* file, /*!< in: file where to print */
  891. sync_array_t* arr) /*!< in: wait array; NOTE! caller must own the
  892. mutex */
  893. {
  894. sync_cell_t* cell;
  895. ulint count;
  896. ulint i;
  897. os_thread_id_t r;
  898. fprintf(file,
  899. "OS WAIT ARRAY INFO: reservation count %ld, signal count %ld\n",
  900. (long) arr->res_count, (long) arr->sg_count);
  901. i = 0;
  902. count = 0;
  903. while (count < arr->n_reserved) {
  904. cell = sync_array_get_nth_cell(arr, i);
  905. if (cell->wait_object != NULL) {
  906. count++;
  907. sync_array_cell_print(file, cell, &r);
  908. }
  909. i++;
  910. }
  911. }
  912. /**********************************************************************//**
  913. Prints info of the wait array. */
  914. UNIV_INTERN
  915. void
  916. sync_array_print_info(
  917. /*==================*/
  918. FILE* file, /*!< in: file where to print */
  919. sync_array_t* arr) /*!< in: wait array */
  920. {
  921. sync_array_enter(arr);
  922. sync_array_output_info(file, arr);
  923. sync_array_exit(arr);
  924. }
  925. /**********************************************************************//**
  926. Prints info of the wait array without using any mutexes/semaphores. */
  927. UNIV_INTERN
  928. void
  929. sync_array_print_xtradb(void)
  930. /*=========================*/
  931. {
  932. ulint i;
  933. sync_array_t* arr = sync_array_get();
  934. fputs("InnoDB: Semaphore wait debug output started for XtraDB:\n", stderr);
  935. for (i = 0; i < arr->n_cells; i++) {
  936. void* wait_object;
  937. sync_cell_t* cell;
  938. os_thread_id_t reserver=(os_thread_id_t)ULINT_UNDEFINED;
  939. ulint loop=0;
  940. cell = sync_array_get_nth_cell(arr, i);
  941. wait_object = cell->wait_object;
  942. if (wait_object == NULL || !cell->waiting) {
  943. continue;
  944. }
  945. fputs("InnoDB: Warning: semaphore wait:\n",
  946. stderr);
  947. sync_array_cell_print(stderr, cell, &reserver);
  948. /* Try to output cell information for writer recursive way */
  949. while (reserver != (os_thread_id_t)ULINT_UNDEFINED) {
  950. sync_cell_t* reserver_wait;
  951. reserver_wait = sync_array_find_thread(arr, reserver);
  952. if (reserver_wait &&
  953. reserver_wait->wait_object != NULL &&
  954. reserver_wait->waiting) {
  955. fputs("InnoDB: Warning: Writer thread is waiting this semaphore:\n",
  956. stderr);
  957. sync_array_cell_print(stderr, reserver_wait, &reserver);
  958. if (reserver_wait->thread == reserver) {
  959. reserver = (os_thread_id_t)ULINT_UNDEFINED;
  960. }
  961. } else {
  962. reserver = (os_thread_id_t)ULINT_UNDEFINED;
  963. }
  964. /* This is protection against loop */
  965. if (loop > 100) {
  966. fputs("InnoDB: Warning: Too many waiting threads.\n", stderr);
  967. break;
  968. }
  969. }
  970. }
  971. fputs("InnoDB: Semaphore wait debug output ended:\n", stderr);
  972. }