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.

1152 lines
30 KiB

12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
  1. /*****************************************************************************
  2. Copyright (c) 1995, 2013, Oracle and/or its affiliates. All Rights Reserved.
  3. Copyright (c) 2008, Google Inc.
  4. Portions of this file contain modifications contributed and copyrighted by
  5. Google, Inc. Those modifications are gratefully acknowledged and are described
  6. briefly in the InnoDB documentation. The contributions by Google are
  7. incorporated with their permission, and subject to the conditions contained in
  8. the file COPYING.Google.
  9. This program is free software; you can redistribute it and/or modify it under
  10. the terms of the GNU General Public License as published by the Free Software
  11. Foundation; version 2 of the License.
  12. This program is distributed in the hope that it will be useful, but WITHOUT
  13. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  14. FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License along with
  16. this program; if not, write to the Free Software Foundation, Inc.,
  17. 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
  18. *****************************************************************************/
  19. /**************************************************//**
  20. @file sync/sync0arr.cc
  21. The wait array used in synchronization primitives
  22. Created 9/5/1995 Heikki Tuuri
  23. *******************************************************/
  24. #include "sync0arr.h"
  25. #ifdef UNIV_NONINL
  26. #include "sync0arr.ic"
  27. #endif
  28. #include "sync0sync.h"
  29. #include "sync0rw.h"
  30. #include "os0sync.h"
  31. #include "os0file.h"
  32. #include "lock0lock.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_t {
  69. void* wait_object; /*!< pointer to the object the
  70. thread is waiting for; if NULL
  71. the cell is free for use */
  72. ib_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_t {
  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. ib_mutex_t mutex; /*!< possible database mutex
  111. protecting this data structure */
  112. os_ib_mutex_t os_mutex; /*!< Possible operating system mutex
  113. protecting the data structure.
  114. As this data structure is used in
  115. constructing the database mutex,
  116. to prevent infinite recursion
  117. in implementation, we fall back to
  118. an OS mutex. */
  119. ulint res_count; /*!< count of cell reservations
  120. since creation of the array */
  121. };
  122. /** User configured sync array size */
  123. UNIV_INTERN ulong srv_sync_array_size = 32;
  124. /** Locally stored copy of srv_sync_array_size */
  125. static ulint sync_array_size;
  126. /** The global array of wait cells for implementation of the database's own
  127. mutexes and read-write locks */
  128. static sync_array_t** sync_wait_array;
  129. /** count of how many times an object has been signalled */
  130. static ulint sg_count;
  131. #ifdef UNIV_SYNC_DEBUG
  132. /******************************************************************//**
  133. This function is called only in the debug version. Detects a deadlock
  134. of one or more threads because of waits of semaphores.
  135. @return TRUE if deadlock detected */
  136. static
  137. ibool
  138. sync_array_detect_deadlock(
  139. /*=======================*/
  140. sync_array_t* arr, /*!< in: wait array; NOTE! the caller must
  141. own the mutex to array */
  142. sync_cell_t* start, /*!< in: cell where recursive search started */
  143. sync_cell_t* cell, /*!< in: cell to search */
  144. ulint depth); /*!< in: recursion depth */
  145. #endif /* UNIV_SYNC_DEBUG */
  146. /*****************************************************************//**
  147. Gets the nth cell in array.
  148. @return cell */
  149. static
  150. sync_cell_t*
  151. sync_array_get_nth_cell(
  152. /*====================*/
  153. sync_array_t* arr, /*!< in: sync array */
  154. ulint n) /*!< in: index */
  155. {
  156. ut_a(arr);
  157. ut_a(n < arr->n_cells);
  158. return(arr->array + n);
  159. }
  160. /******************************************************************//**
  161. Reserves the mutex semaphore protecting a sync array. */
  162. static
  163. void
  164. sync_array_enter(
  165. /*=============*/
  166. sync_array_t* arr) /*!< in: sync wait array */
  167. {
  168. os_mutex_enter(arr->os_mutex);
  169. }
  170. /******************************************************************//**
  171. Releases the mutex semaphore protecting a sync array. */
  172. static
  173. void
  174. sync_array_exit(
  175. /*============*/
  176. sync_array_t* arr) /*!< in: sync wait array */
  177. {
  178. os_mutex_exit(arr->os_mutex);
  179. }
  180. /*******************************************************************//**
  181. Creates a synchronization wait array. It is protected by a mutex
  182. which is automatically reserved when the functions operating on it
  183. are called.
  184. @return own: created wait array */
  185. static
  186. sync_array_t*
  187. sync_array_create(
  188. /*==============*/
  189. ulint n_cells) /*!< in: number of cells in the array
  190. to create */
  191. {
  192. ulint sz;
  193. sync_array_t* arr;
  194. ut_a(n_cells > 0);
  195. /* Allocate memory for the data structures */
  196. arr = static_cast<sync_array_t*>(ut_malloc(sizeof(*arr)));
  197. memset(arr, 0x0, sizeof(*arr));
  198. sz = sizeof(sync_cell_t) * n_cells;
  199. arr->array = static_cast<sync_cell_t*>(ut_malloc(sz));
  200. memset(arr->array, 0x0, sz);
  201. arr->n_cells = n_cells;
  202. /* Then create the mutex to protect the wait array complex */
  203. arr->os_mutex = os_mutex_create();
  204. return(arr);
  205. }
  206. /******************************************************************//**
  207. Frees the resources in a wait array. */
  208. static
  209. void
  210. sync_array_free(
  211. /*============*/
  212. sync_array_t* arr) /*!< in, own: sync wait array */
  213. {
  214. ut_a(arr->n_reserved == 0);
  215. sync_array_validate(arr);
  216. /* Release the mutex protecting the wait array complex */
  217. os_mutex_free(arr->os_mutex);
  218. ut_free(arr->array);
  219. ut_free(arr);
  220. }
  221. /********************************************************************//**
  222. Validates the integrity of the wait array. Checks
  223. that the number of reserved cells equals the count variable. */
  224. UNIV_INTERN
  225. void
  226. sync_array_validate(
  227. /*================*/
  228. sync_array_t* arr) /*!< in: sync wait array */
  229. {
  230. ulint i;
  231. sync_cell_t* cell;
  232. ulint count = 0;
  233. sync_array_enter(arr);
  234. for (i = 0; i < arr->n_cells; i++) {
  235. cell = sync_array_get_nth_cell(arr, i);
  236. if (cell->wait_object != NULL) {
  237. count++;
  238. }
  239. }
  240. ut_a(count == arr->n_reserved);
  241. sync_array_exit(arr);
  242. }
  243. /*******************************************************************//**
  244. Returns the event that the thread owning the cell waits for. */
  245. static
  246. os_event_t
  247. sync_cell_get_event(
  248. /*================*/
  249. sync_cell_t* cell) /*!< in: non-empty sync array cell */
  250. {
  251. ulint type = cell->request_type;
  252. if (type == SYNC_MUTEX) {
  253. return(((ib_mutex_t*) cell->wait_object)->event);
  254. } else if (type == RW_LOCK_WAIT_EX) {
  255. return(((rw_lock_t*) cell->wait_object)->wait_ex_event);
  256. } else { /* RW_LOCK_SHARED and RW_LOCK_EX wait on the same event */
  257. return(((rw_lock_t*) cell->wait_object)->event);
  258. }
  259. }
  260. /******************************************************************//**
  261. Reserves a wait array cell for waiting for an object.
  262. The event of the cell is reset to nonsignalled state.
  263. @return true if free cell is found, otherwise false */
  264. UNIV_INTERN
  265. bool
  266. sync_array_reserve_cell(
  267. /*====================*/
  268. sync_array_t* arr, /*!< in: wait array */
  269. void* object, /*!< in: pointer to the object to wait for */
  270. ulint type, /*!< in: lock request type */
  271. const char* file, /*!< in: file where requested */
  272. ulint line, /*!< in: line where requested */
  273. ulint* index) /*!< out: index of the reserved cell */
  274. {
  275. sync_cell_t* cell;
  276. os_event_t event;
  277. ulint i;
  278. ut_a(object);
  279. ut_a(index);
  280. sync_array_enter(arr);
  281. arr->res_count++;
  282. /* Reserve a new cell. */
  283. for (i = 0; i < arr->n_cells; i++) {
  284. cell = sync_array_get_nth_cell(arr, i);
  285. if (cell->wait_object == NULL) {
  286. cell->waiting = FALSE;
  287. cell->wait_object = object;
  288. if (type == SYNC_MUTEX) {
  289. cell->old_wait_mutex =
  290. static_cast<ib_mutex_t*>(object);
  291. } else {
  292. cell->old_wait_rw_lock =
  293. static_cast<rw_lock_t*>(object);
  294. }
  295. cell->request_type = type;
  296. cell->file = file;
  297. cell->line = line;
  298. arr->n_reserved++;
  299. *index = i;
  300. sync_array_exit(arr);
  301. /* Make sure the event is reset and also store
  302. the value of signal_count at which the event
  303. was reset. */
  304. event = sync_cell_get_event(cell);
  305. cell->signal_count = os_event_reset(event);
  306. cell->reservation_time = ut_time();
  307. cell->thread = os_thread_get_curr_id();
  308. return(true);
  309. }
  310. }
  311. /* No free cell found */
  312. return false;
  313. }
  314. /******************************************************************//**
  315. This function should be called when a thread starts to wait on
  316. a wait array cell. In the debug version this function checks
  317. if the wait for a semaphore will result in a deadlock, in which
  318. case prints info and asserts. */
  319. UNIV_INTERN
  320. void
  321. sync_array_wait_event(
  322. /*==================*/
  323. sync_array_t* arr, /*!< in: wait array */
  324. ulint index) /*!< in: index of the reserved cell */
  325. {
  326. sync_cell_t* cell;
  327. os_event_t event;
  328. ut_a(arr);
  329. sync_array_enter(arr);
  330. cell = sync_array_get_nth_cell(arr, index);
  331. ut_a(cell->wait_object);
  332. ut_a(!cell->waiting);
  333. ut_ad(os_thread_get_curr_id() == cell->thread);
  334. event = sync_cell_get_event(cell);
  335. cell->waiting = TRUE;
  336. #ifdef UNIV_SYNC_DEBUG
  337. /* We use simple enter to the mutex below, because if
  338. we cannot acquire it at once, mutex_enter would call
  339. recursively sync_array routines, leading to trouble.
  340. rw_lock_debug_mutex freezes the debug lists. */
  341. rw_lock_debug_mutex_enter();
  342. if (TRUE == sync_array_detect_deadlock(arr, cell, cell, 0)) {
  343. fputs("########################################\n", stderr);
  344. ut_error;
  345. }
  346. rw_lock_debug_mutex_exit();
  347. #endif
  348. sync_array_exit(arr);
  349. os_event_wait_low(event, cell->signal_count);
  350. sync_array_free_cell(arr, index);
  351. }
  352. /******************************************************************//**
  353. Reports info of a wait array cell. */
  354. static
  355. void
  356. sync_array_cell_print(
  357. /*==================*/
  358. FILE* file, /*!< in: file where to print */
  359. sync_cell_t* cell) /*!< in: sync cell */
  360. {
  361. ib_mutex_t* mutex;
  362. rw_lock_t* rwlock;
  363. ulint type;
  364. ulint writer;
  365. type = cell->request_type;
  366. fprintf(file,
  367. "--Thread %lu has waited at %s line %lu"
  368. " for %.2f seconds the semaphore:\n",
  369. (ulong) os_thread_pf(cell->thread),
  370. innobase_basename(cell->file), (ulong) cell->line,
  371. difftime(time(NULL), cell->reservation_time));
  372. if (type == SYNC_MUTEX) {
  373. /* We use old_wait_mutex in case the cell has already
  374. been freed meanwhile */
  375. mutex = cell->old_wait_mutex;
  376. fprintf(file,
  377. "Mutex at %p created file %s line %lu, lock var %lu\n"
  378. #ifdef UNIV_SYNC_DEBUG
  379. "Last time reserved in file %s line %lu, "
  380. #endif /* UNIV_SYNC_DEBUG */
  381. "waiters flag %lu\n",
  382. (void*) mutex, innobase_basename(mutex->cfile_name),
  383. (ulong) mutex->cline,
  384. (ulong) mutex->lock_word,
  385. #ifdef UNIV_SYNC_DEBUG
  386. mutex->file_name, (ulong) mutex->line,
  387. #endif /* UNIV_SYNC_DEBUG */
  388. (ulong) mutex->waiters);
  389. } else if (type == RW_LOCK_EX
  390. || type == RW_LOCK_WAIT_EX
  391. || type == RW_LOCK_SHARED) {
  392. fputs(type == RW_LOCK_EX ? "X-lock on"
  393. : type == RW_LOCK_WAIT_EX ? "X-lock (wait_ex) on"
  394. : "S-lock on", file);
  395. rwlock = cell->old_wait_rw_lock;
  396. fprintf(file,
  397. " RW-latch at %p created in file %s line %lu\n",
  398. (void*) rwlock, innobase_basename(rwlock->cfile_name),
  399. (ulong) rwlock->cline);
  400. writer = rw_lock_get_writer(rwlock);
  401. if (writer != RW_LOCK_NOT_LOCKED) {
  402. fprintf(file,
  403. "a writer (thread id %lu) has"
  404. " reserved it in mode %s",
  405. (ulong) os_thread_pf(rwlock->writer_thread),
  406. writer == RW_LOCK_EX
  407. ? " exclusive\n"
  408. : " wait exclusive\n");
  409. }
  410. fprintf(file,
  411. "number of readers %lu, waiters flag %lu, "
  412. "lock_word: %lx\n"
  413. "Last time read locked in file %s line %lu\n"
  414. "Last time write locked in file %s line %lu\n",
  415. (ulong) rw_lock_get_reader_count(rwlock),
  416. (ulong) rwlock->waiters,
  417. rwlock->lock_word,
  418. innobase_basename(rwlock->last_s_file_name),
  419. (ulong) rwlock->last_s_line,
  420. rwlock->last_x_file_name,
  421. (ulong) rwlock->last_x_line);
  422. } else {
  423. ut_error;
  424. }
  425. if (!cell->waiting) {
  426. fputs("wait has ended\n", file);
  427. }
  428. }
  429. #ifdef UNIV_SYNC_DEBUG
  430. /******************************************************************//**
  431. Looks for a cell with the given thread id.
  432. @return pointer to cell or NULL if not found */
  433. static
  434. sync_cell_t*
  435. sync_array_find_thread(
  436. /*===================*/
  437. sync_array_t* arr, /*!< in: wait array */
  438. os_thread_id_t thread) /*!< in: thread id */
  439. {
  440. ulint i;
  441. sync_cell_t* cell;
  442. for (i = 0; i < arr->n_cells; i++) {
  443. cell = sync_array_get_nth_cell(arr, i);
  444. if (cell->wait_object != NULL
  445. && os_thread_eq(cell->thread, thread)) {
  446. return(cell); /* Found */
  447. }
  448. }
  449. return(NULL); /* Not found */
  450. }
  451. /******************************************************************//**
  452. Recursion step for deadlock detection.
  453. @return TRUE if deadlock detected */
  454. static
  455. ibool
  456. sync_array_deadlock_step(
  457. /*=====================*/
  458. sync_array_t* arr, /*!< in: wait array; NOTE! the caller must
  459. own the mutex to array */
  460. sync_cell_t* start, /*!< in: cell where recursive search
  461. started */
  462. os_thread_id_t thread, /*!< in: thread to look at */
  463. ulint pass, /*!< in: pass value */
  464. ulint depth) /*!< in: recursion depth */
  465. {
  466. sync_cell_t* new_cell;
  467. if (pass != 0) {
  468. /* If pass != 0, then we do not know which threads are
  469. responsible of releasing the lock, and no deadlock can
  470. be detected. */
  471. return(FALSE);
  472. }
  473. new_cell = sync_array_find_thread(arr, thread);
  474. if (new_cell == start) {
  475. /* Deadlock */
  476. fputs("########################################\n"
  477. "DEADLOCK of threads detected!\n", stderr);
  478. return(TRUE);
  479. } else if (new_cell) {
  480. return(sync_array_detect_deadlock(
  481. arr, start, new_cell, depth + 1));
  482. }
  483. return(FALSE);
  484. }
  485. /******************************************************************//**
  486. This function is called only in the debug version. Detects a deadlock
  487. of one or more threads because of waits of semaphores.
  488. @return TRUE if deadlock detected */
  489. static
  490. ibool
  491. sync_array_detect_deadlock(
  492. /*=======================*/
  493. sync_array_t* arr, /*!< in: wait array; NOTE! the caller must
  494. own the mutex to array */
  495. sync_cell_t* start, /*!< in: cell where recursive search started */
  496. sync_cell_t* cell, /*!< in: cell to search */
  497. ulint depth) /*!< in: recursion depth */
  498. {
  499. ib_mutex_t* mutex;
  500. rw_lock_t* lock;
  501. os_thread_id_t thread;
  502. ibool ret;
  503. rw_lock_debug_t*debug;
  504. ut_a(arr);
  505. ut_a(start);
  506. ut_a(cell);
  507. ut_ad(cell->wait_object);
  508. ut_ad(os_thread_get_curr_id() == start->thread);
  509. ut_ad(depth < 100);
  510. depth++;
  511. if (!cell->waiting) {
  512. return(FALSE); /* No deadlock here */
  513. }
  514. if (cell->request_type == SYNC_MUTEX) {
  515. mutex = static_cast<ib_mutex_t*>(cell->wait_object);
  516. if (mutex_get_lock_word(mutex) != 0) {
  517. thread = mutex->thread_id;
  518. /* Note that mutex->thread_id above may be
  519. also OS_THREAD_ID_UNDEFINED, because the
  520. thread which held the mutex maybe has not
  521. yet updated the value, or it has already
  522. released the mutex: in this case no deadlock
  523. can occur, as the wait array cannot contain
  524. a thread with ID_UNDEFINED value. */
  525. ret = sync_array_deadlock_step(arr, start, thread, 0,
  526. depth);
  527. if (ret) {
  528. fprintf(stderr,
  529. "Mutex %p owned by thread %lu file %s line %lu\n",
  530. mutex, (ulong) os_thread_pf(mutex->thread_id),
  531. mutex->file_name, (ulong) mutex->line);
  532. sync_array_cell_print(stderr, cell);
  533. return(TRUE);
  534. }
  535. }
  536. return(FALSE); /* No deadlock */
  537. } else if (cell->request_type == RW_LOCK_EX
  538. || cell->request_type == RW_LOCK_WAIT_EX) {
  539. lock = static_cast<rw_lock_t*>(cell->wait_object);
  540. for (debug = UT_LIST_GET_FIRST(lock->debug_list);
  541. debug != 0;
  542. debug = UT_LIST_GET_NEXT(list, debug)) {
  543. thread = debug->thread_id;
  544. if (((debug->lock_type == RW_LOCK_EX)
  545. && !os_thread_eq(thread, cell->thread))
  546. || ((debug->lock_type == RW_LOCK_WAIT_EX)
  547. && !os_thread_eq(thread, cell->thread))
  548. || (debug->lock_type == RW_LOCK_SHARED)) {
  549. /* The (wait) x-lock request can block
  550. infinitely only if someone (can be also cell
  551. thread) is holding s-lock, or someone
  552. (cannot be cell thread) (wait) x-lock, and
  553. he is blocked by start thread */
  554. ret = sync_array_deadlock_step(
  555. arr, start, thread, debug->pass,
  556. depth);
  557. if (ret) {
  558. print:
  559. fprintf(stderr, "rw-lock %p ",
  560. (void*) lock);
  561. sync_array_cell_print(stderr, cell);
  562. rw_lock_debug_print(stderr, debug);
  563. return(TRUE);
  564. }
  565. }
  566. }
  567. return(FALSE);
  568. } else if (cell->request_type == RW_LOCK_SHARED) {
  569. lock = static_cast<rw_lock_t*>(cell->wait_object);
  570. for (debug = UT_LIST_GET_FIRST(lock->debug_list);
  571. debug != 0;
  572. debug = UT_LIST_GET_NEXT(list, debug)) {
  573. thread = debug->thread_id;
  574. if ((debug->lock_type == RW_LOCK_EX)
  575. || (debug->lock_type == RW_LOCK_WAIT_EX)) {
  576. /* The s-lock request can block infinitely
  577. only if someone (can also be cell thread) is
  578. holding (wait) x-lock, and he is blocked by
  579. start thread */
  580. ret = sync_array_deadlock_step(
  581. arr, start, thread, debug->pass,
  582. depth);
  583. if (ret) {
  584. goto print;
  585. }
  586. }
  587. }
  588. return(FALSE);
  589. } else {
  590. ut_error;
  591. }
  592. return(TRUE); /* Execution never reaches this line: for compiler
  593. fooling only */
  594. }
  595. #endif /* UNIV_SYNC_DEBUG */
  596. /******************************************************************//**
  597. Determines if we can wake up the thread waiting for a sempahore. */
  598. static
  599. ibool
  600. sync_arr_cell_can_wake_up(
  601. /*======================*/
  602. sync_cell_t* cell) /*!< in: cell to search */
  603. {
  604. ib_mutex_t* mutex;
  605. rw_lock_t* lock;
  606. if (cell->request_type == SYNC_MUTEX) {
  607. mutex = static_cast<ib_mutex_t*>(cell->wait_object);
  608. if (mutex_get_lock_word(mutex) == 0) {
  609. return(TRUE);
  610. }
  611. } else if (cell->request_type == RW_LOCK_EX) {
  612. lock = static_cast<rw_lock_t*>(cell->wait_object);
  613. if (lock->lock_word > 0) {
  614. /* Either unlocked or only read locked. */
  615. return(TRUE);
  616. }
  617. } else if (cell->request_type == RW_LOCK_WAIT_EX) {
  618. lock = static_cast<rw_lock_t*>(cell->wait_object);
  619. /* lock_word == 0 means all readers have left */
  620. if (lock->lock_word == 0) {
  621. return(TRUE);
  622. }
  623. } else if (cell->request_type == RW_LOCK_SHARED) {
  624. lock = static_cast<rw_lock_t*>(cell->wait_object);
  625. /* lock_word > 0 means no writer or reserved writer */
  626. if (lock->lock_word > 0) {
  627. return(TRUE);
  628. }
  629. }
  630. return(FALSE);
  631. }
  632. /******************************************************************//**
  633. Frees the cell. NOTE! sync_array_wait_event frees the cell
  634. automatically! */
  635. UNIV_INTERN
  636. void
  637. sync_array_free_cell(
  638. /*=================*/
  639. sync_array_t* arr, /*!< in: wait array */
  640. ulint index) /*!< in: index of the cell in array */
  641. {
  642. sync_cell_t* cell;
  643. sync_array_enter(arr);
  644. cell = sync_array_get_nth_cell(arr, index);
  645. ut_a(cell->wait_object != NULL);
  646. cell->waiting = FALSE;
  647. cell->wait_object = NULL;
  648. cell->signal_count = 0;
  649. ut_a(arr->n_reserved > 0);
  650. arr->n_reserved--;
  651. sync_array_exit(arr);
  652. }
  653. /**********************************************************************//**
  654. Increments the signalled count. */
  655. UNIV_INTERN
  656. void
  657. sync_array_object_signalled(void)
  658. /*=============================*/
  659. {
  660. #ifdef HAVE_ATOMIC_BUILTINS
  661. (void) os_atomic_increment_ulint(&sg_count, 1);
  662. #else
  663. ++sg_count;
  664. #endif /* HAVE_ATOMIC_BUILTINS */
  665. }
  666. /**********************************************************************//**
  667. If the wakeup algorithm does not work perfectly at semaphore relases,
  668. this function will do the waking (see the comment in mutex_exit). This
  669. function should be called about every 1 second in the server.
  670. Note that there's a race condition between this thread and mutex_exit
  671. changing the lock_word and calling signal_object, so sometimes this finds
  672. threads to wake up even when nothing has gone wrong. */
  673. static
  674. void
  675. sync_array_wake_threads_if_sema_free_low(
  676. /*=====================================*/
  677. sync_array_t* arr) /* in/out: wait array */
  678. {
  679. ulint i = 0;
  680. ulint count;
  681. sync_array_enter(arr);
  682. for (count = 0; count < arr->n_reserved; ++i) {
  683. sync_cell_t* cell;
  684. cell = sync_array_get_nth_cell(arr, i);
  685. if (cell->wait_object != NULL) {
  686. count++;
  687. if (sync_arr_cell_can_wake_up(cell)) {
  688. os_event_t event;
  689. event = sync_cell_get_event(cell);
  690. os_event_set(event);
  691. }
  692. }
  693. }
  694. sync_array_exit(arr);
  695. }
  696. /**********************************************************************//**
  697. If the wakeup algorithm does not work perfectly at semaphore relases,
  698. this function will do the waking (see the comment in mutex_exit). This
  699. function should be called about every 1 second in the server.
  700. Note that there's a race condition between this thread and mutex_exit
  701. changing the lock_word and calling signal_object, so sometimes this finds
  702. threads to wake up even when nothing has gone wrong. */
  703. UNIV_INTERN
  704. void
  705. sync_arr_wake_threads_if_sema_free(void)
  706. /*====================================*/
  707. {
  708. ulint i;
  709. for (i = 0; i < sync_array_size; ++i) {
  710. sync_array_wake_threads_if_sema_free_low(
  711. sync_wait_array[i]);
  712. }
  713. }
  714. /**********************************************************************//**
  715. Prints warnings of long semaphore waits to stderr.
  716. @return TRUE if fatal semaphore wait threshold was exceeded */
  717. static
  718. ibool
  719. sync_array_print_long_waits_low(
  720. /*============================*/
  721. sync_array_t* arr, /*!< in: sync array instance */
  722. os_thread_id_t* waiter, /*!< out: longest waiting thread */
  723. const void** sema, /*!< out: longest-waited-for semaphore */
  724. ibool* noticed)/*!< out: TRUE if long wait noticed */
  725. {
  726. ulint i;
  727. ulint fatal_timeout = srv_fatal_semaphore_wait_threshold;
  728. ibool fatal = FALSE;
  729. double longest_diff = 0;
  730. /* For huge tables, skip the check during CHECK TABLE etc... */
  731. if (fatal_timeout > SRV_SEMAPHORE_WAIT_EXTENSION) {
  732. return(FALSE);
  733. }
  734. #ifdef UNIV_DEBUG_VALGRIND
  735. /* Increase the timeouts if running under valgrind because it executes
  736. extremely slowly. UNIV_DEBUG_VALGRIND does not necessary mean that
  737. we are running under valgrind but we have no better way to tell.
  738. See Bug#58432 innodb.innodb_bug56143 fails under valgrind
  739. for an example */
  740. # define SYNC_ARRAY_TIMEOUT 2400
  741. fatal_timeout *= 10;
  742. #else
  743. # define SYNC_ARRAY_TIMEOUT 240
  744. #endif
  745. for (i = 0; i < arr->n_cells; i++) {
  746. double diff;
  747. sync_cell_t* cell;
  748. void* wait_object;
  749. cell = sync_array_get_nth_cell(arr, i);
  750. wait_object = cell->wait_object;
  751. if (wait_object == NULL || !cell->waiting) {
  752. continue;
  753. }
  754. diff = difftime(time(NULL), cell->reservation_time);
  755. if (diff > SYNC_ARRAY_TIMEOUT) {
  756. fputs("InnoDB: Warning: a long semaphore wait:\n",
  757. stderr);
  758. sync_array_cell_print(stderr, cell);
  759. *noticed = TRUE;
  760. }
  761. if (diff > fatal_timeout) {
  762. fatal = TRUE;
  763. }
  764. if (diff > longest_diff) {
  765. longest_diff = diff;
  766. *sema = wait_object;
  767. *waiter = cell->thread;
  768. }
  769. }
  770. #undef SYNC_ARRAY_TIMEOUT
  771. return(fatal);
  772. }
  773. /**********************************************************************//**
  774. Prints warnings of long semaphore waits to stderr.
  775. @return TRUE if fatal semaphore wait threshold was exceeded */
  776. UNIV_INTERN
  777. ibool
  778. sync_array_print_long_waits(
  779. /*========================*/
  780. os_thread_id_t* waiter, /*!< out: longest waiting thread */
  781. const void** sema) /*!< out: longest-waited-for semaphore */
  782. {
  783. ulint i;
  784. ibool fatal = FALSE;
  785. ibool noticed = FALSE;
  786. for (i = 0; i < sync_array_size; ++i) {
  787. sync_array_t* arr = sync_wait_array[i];
  788. sync_array_enter(arr);
  789. if (sync_array_print_long_waits_low(
  790. arr, waiter, sema, &noticed)) {
  791. fatal = TRUE;
  792. }
  793. sync_array_exit(arr);
  794. }
  795. if (noticed) {
  796. ibool old_val;
  797. fprintf(stderr,
  798. "InnoDB: ###### Starts InnoDB Monitor"
  799. " for 30 secs to print diagnostic info:\n");
  800. old_val = srv_print_innodb_monitor;
  801. /* If some crucial semaphore is reserved, then also the InnoDB
  802. Monitor can hang, and we do not get diagnostics. Since in
  803. many cases an InnoDB hang is caused by a pwrite() or a pread()
  804. call hanging inside the operating system, let us print right
  805. now the values of pending calls of these. */
  806. fprintf(stderr,
  807. "InnoDB: Pending preads %lu, pwrites %lu\n",
  808. (ulong) os_file_n_pending_preads,
  809. (ulong) os_file_n_pending_pwrites);
  810. srv_print_innodb_monitor = TRUE;
  811. os_event_set(lock_sys->timeout_event);
  812. os_thread_sleep(30000000);
  813. srv_print_innodb_monitor = static_cast<my_bool>(old_val);
  814. fprintf(stderr,
  815. "InnoDB: ###### Diagnostic info printed"
  816. " to the standard error stream\n");
  817. }
  818. return(fatal);
  819. }
  820. /**********************************************************************//**
  821. Prints info of the wait array. */
  822. static
  823. void
  824. sync_array_print_info_low(
  825. /*======================*/
  826. FILE* file, /*!< in: file where to print */
  827. sync_array_t* arr) /*!< in: wait array */
  828. {
  829. ulint i;
  830. ulint count = 0;
  831. fprintf(file,
  832. "OS WAIT ARRAY INFO: reservation count %ld\n",
  833. (long) arr->res_count);
  834. for (i = 0; count < arr->n_reserved; ++i) {
  835. sync_cell_t* cell;
  836. cell = sync_array_get_nth_cell(arr, i);
  837. if (cell->wait_object != NULL) {
  838. count++;
  839. sync_array_cell_print(file, cell);
  840. }
  841. }
  842. }
  843. /**********************************************************************//**
  844. Prints info of the wait array. */
  845. static
  846. void
  847. sync_array_print_info(
  848. /*==================*/
  849. FILE* file, /*!< in: file where to print */
  850. sync_array_t* arr) /*!< in: wait array */
  851. {
  852. sync_array_enter(arr);
  853. sync_array_print_info_low(file, arr);
  854. sync_array_exit(arr);
  855. }
  856. /**********************************************************************//**
  857. Create the primary system wait array(s), they are protected by an OS mutex */
  858. UNIV_INTERN
  859. void
  860. sync_array_init(
  861. /*============*/
  862. ulint n_threads) /*!< in: Number of slots to
  863. create in all arrays */
  864. {
  865. ulint i;
  866. ulint n_slots;
  867. ut_a(sync_wait_array == NULL);
  868. ut_a(srv_sync_array_size > 0);
  869. ut_a(n_threads > 0);
  870. sync_array_size = srv_sync_array_size;
  871. /* We have to use ut_malloc() because the mutex infrastructure
  872. hasn't been initialised yet. It is required by mem_alloc() and
  873. the heap functions. */
  874. sync_wait_array = static_cast<sync_array_t**>(
  875. ut_malloc(sizeof(*sync_wait_array) * sync_array_size));
  876. n_slots = 1 + (n_threads - 1) / sync_array_size;
  877. for (i = 0; i < sync_array_size; ++i) {
  878. sync_wait_array[i] = sync_array_create(n_slots);
  879. }
  880. }
  881. /**********************************************************************//**
  882. Close sync array wait sub-system. */
  883. UNIV_INTERN
  884. void
  885. sync_array_close(void)
  886. /*==================*/
  887. {
  888. ulint i;
  889. for (i = 0; i < sync_array_size; ++i) {
  890. sync_array_free(sync_wait_array[i]);
  891. }
  892. ut_free(sync_wait_array);
  893. sync_wait_array = NULL;
  894. }
  895. /**********************************************************************//**
  896. Print info about the sync array(s). */
  897. UNIV_INTERN
  898. void
  899. sync_array_print(
  900. /*=============*/
  901. FILE* file) /*!< in/out: Print to this stream */
  902. {
  903. ulint i;
  904. for (i = 0; i < sync_array_size; ++i) {
  905. sync_array_print_info(file, sync_wait_array[i]);
  906. }
  907. fprintf(file,
  908. "OS WAIT ARRAY INFO: signal count %ld\n", (long) sg_count);
  909. }
  910. /**********************************************************************//**
  911. Get an instance of the sync wait array. */
  912. UNIV_INTERN
  913. sync_array_t*
  914. sync_array_get(void)
  915. /*================*/
  916. {
  917. ulint i;
  918. static ulint count;
  919. #ifdef HAVE_ATOMIC_BUILTINS
  920. i = os_atomic_increment_ulint(&count, 1);
  921. #else
  922. i = count++;
  923. #endif /* HAVE_ATOMIC_BUILTINS */
  924. return(sync_wait_array[i % sync_array_size]);
  925. }