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.

1285 lines
33 KiB

11 years ago
12 years ago
12 years ago
12 years ago
12 years ago
11 years ago
11 years ago
11 years ago
11 years ago
12 years ago
11 years ago
12 years ago
11 years ago
11 years ago
11 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. 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. Looks for a cell with the given thread id.
  162. @return pointer to cell or NULL if not found */
  163. static
  164. sync_cell_t*
  165. sync_array_find_thread(
  166. /*===================*/
  167. sync_array_t* arr, /*!< in: wait array */
  168. os_thread_id_t thread) /*!< in: thread id */
  169. {
  170. ulint i;
  171. sync_cell_t* cell;
  172. for (i = 0; i < arr->n_cells; i++) {
  173. cell = sync_array_get_nth_cell(arr, i);
  174. if (cell->wait_object != NULL
  175. && os_thread_eq(cell->thread, thread)) {
  176. return(cell); /* Found */
  177. }
  178. }
  179. return(NULL); /* Not found */
  180. }
  181. /******************************************************************//**
  182. Reserves the mutex semaphore protecting a sync array. */
  183. static
  184. void
  185. sync_array_enter(
  186. /*=============*/
  187. sync_array_t* arr) /*!< in: sync wait array */
  188. {
  189. os_mutex_enter(arr->os_mutex);
  190. }
  191. /******************************************************************//**
  192. Releases the mutex semaphore protecting a sync array. */
  193. static
  194. void
  195. sync_array_exit(
  196. /*============*/
  197. sync_array_t* arr) /*!< in: sync wait array */
  198. {
  199. os_mutex_exit(arr->os_mutex);
  200. }
  201. /*******************************************************************//**
  202. Creates a synchronization wait array. It is protected by a mutex
  203. which is automatically reserved when the functions operating on it
  204. are called.
  205. @return own: created wait array */
  206. static
  207. sync_array_t*
  208. sync_array_create(
  209. /*==============*/
  210. ulint n_cells) /*!< in: number of cells in the array
  211. to create */
  212. {
  213. ulint sz;
  214. sync_array_t* arr;
  215. ut_a(n_cells > 0);
  216. /* Allocate memory for the data structures */
  217. arr = static_cast<sync_array_t*>(ut_malloc(sizeof(*arr)));
  218. memset(arr, 0x0, sizeof(*arr));
  219. sz = sizeof(sync_cell_t) * n_cells;
  220. arr->array = static_cast<sync_cell_t*>(ut_malloc(sz));
  221. memset(arr->array, 0x0, sz);
  222. arr->n_cells = n_cells;
  223. /* Then create the mutex to protect the wait array complex */
  224. arr->os_mutex = os_mutex_create();
  225. return(arr);
  226. }
  227. /******************************************************************//**
  228. Frees the resources in a wait array. */
  229. static
  230. void
  231. sync_array_free(
  232. /*============*/
  233. sync_array_t* arr) /*!< in, own: sync wait array */
  234. {
  235. ut_a(arr->n_reserved == 0);
  236. sync_array_validate(arr);
  237. /* Release the mutex protecting the wait array complex */
  238. os_mutex_free(arr->os_mutex);
  239. ut_free(arr->array);
  240. ut_free(arr);
  241. }
  242. /********************************************************************//**
  243. Validates the integrity of the wait array. Checks
  244. that the number of reserved cells equals the count variable. */
  245. UNIV_INTERN
  246. void
  247. sync_array_validate(
  248. /*================*/
  249. sync_array_t* arr) /*!< in: sync wait array */
  250. {
  251. ulint i;
  252. sync_cell_t* cell;
  253. ulint count = 0;
  254. sync_array_enter(arr);
  255. for (i = 0; i < arr->n_cells; i++) {
  256. cell = sync_array_get_nth_cell(arr, i);
  257. if (cell->wait_object != NULL) {
  258. count++;
  259. }
  260. }
  261. ut_a(count == arr->n_reserved);
  262. sync_array_exit(arr);
  263. }
  264. /*******************************************************************//**
  265. Returns the event that the thread owning the cell waits for. */
  266. static
  267. os_event_t
  268. sync_cell_get_event(
  269. /*================*/
  270. sync_cell_t* cell) /*!< in: non-empty sync array cell */
  271. {
  272. ulint type = cell->request_type;
  273. if (type == SYNC_MUTEX) {
  274. return(((ib_mutex_t*) cell->wait_object)->event);
  275. } else if (type == RW_LOCK_WAIT_EX) {
  276. return(((rw_lock_t*) cell->wait_object)->wait_ex_event);
  277. } else { /* RW_LOCK_SHARED and RW_LOCK_EX wait on the same event */
  278. return(((rw_lock_t*) cell->wait_object)->event);
  279. }
  280. }
  281. /******************************************************************//**
  282. Reserves a wait array cell for waiting for an object.
  283. The event of the cell is reset to nonsignalled state.
  284. @return true if free cell is found, otherwise false */
  285. UNIV_INTERN
  286. bool
  287. sync_array_reserve_cell(
  288. /*====================*/
  289. sync_array_t* arr, /*!< in: wait array */
  290. void* object, /*!< in: pointer to the object to wait for */
  291. ulint type, /*!< in: lock request type */
  292. const char* file, /*!< in: file where requested */
  293. ulint line, /*!< in: line where requested */
  294. ulint* index) /*!< out: index of the reserved cell */
  295. {
  296. sync_cell_t* cell;
  297. os_event_t event;
  298. ulint i;
  299. ut_a(object);
  300. ut_a(index);
  301. sync_array_enter(arr);
  302. arr->res_count++;
  303. /* Reserve a new cell. */
  304. for (i = 0; i < arr->n_cells; i++) {
  305. cell = sync_array_get_nth_cell(arr, i);
  306. if (cell->wait_object == NULL) {
  307. cell->waiting = FALSE;
  308. cell->wait_object = object;
  309. if (type == SYNC_MUTEX) {
  310. cell->old_wait_mutex =
  311. static_cast<ib_mutex_t*>(object);
  312. } else {
  313. cell->old_wait_rw_lock =
  314. static_cast<rw_lock_t*>(object);
  315. }
  316. cell->request_type = type;
  317. cell->file = file;
  318. cell->line = line;
  319. arr->n_reserved++;
  320. *index = i;
  321. sync_array_exit(arr);
  322. /* Make sure the event is reset and also store
  323. the value of signal_count at which the event
  324. was reset. */
  325. event = sync_cell_get_event(cell);
  326. cell->signal_count = os_event_reset(event);
  327. cell->reservation_time = ut_time();
  328. cell->thread = os_thread_get_curr_id();
  329. return(true);
  330. }
  331. }
  332. /* No free cell found */
  333. return false;
  334. }
  335. /******************************************************************//**
  336. This function should be called when a thread starts to wait on
  337. a wait array cell. In the debug version this function checks
  338. if the wait for a semaphore will result in a deadlock, in which
  339. case prints info and asserts. */
  340. UNIV_INTERN
  341. void
  342. sync_array_wait_event(
  343. /*==================*/
  344. sync_array_t* arr, /*!< in: wait array */
  345. ulint index) /*!< in: index of the reserved cell */
  346. {
  347. sync_cell_t* cell;
  348. os_event_t event;
  349. ut_a(arr);
  350. sync_array_enter(arr);
  351. cell = sync_array_get_nth_cell(arr, index);
  352. ut_a(cell->wait_object);
  353. ut_a(!cell->waiting);
  354. ut_ad(os_thread_get_curr_id() == cell->thread);
  355. event = sync_cell_get_event(cell);
  356. cell->waiting = TRUE;
  357. #ifdef UNIV_SYNC_DEBUG
  358. /* We use simple enter to the mutex below, because if
  359. we cannot acquire it at once, mutex_enter would call
  360. recursively sync_array routines, leading to trouble.
  361. rw_lock_debug_mutex freezes the debug lists. */
  362. rw_lock_debug_mutex_enter();
  363. if (TRUE == sync_array_detect_deadlock(arr, cell, cell, 0)) {
  364. fputs("########################################\n", stderr);
  365. ut_error;
  366. }
  367. rw_lock_debug_mutex_exit();
  368. #endif
  369. sync_array_exit(arr);
  370. os_event_wait_low(event, cell->signal_count);
  371. sync_array_free_cell(arr, index);
  372. }
  373. /******************************************************************//**
  374. Reports info of a wait array cell. */
  375. static
  376. void
  377. sync_array_cell_print(
  378. /*==================*/
  379. FILE* file, /*!< in: file where to print */
  380. sync_cell_t* cell, /*!< in: sync cell */
  381. os_thread_id_t* reserver) /*!< out: write reserver or
  382. 0 */
  383. {
  384. ib_mutex_t* mutex;
  385. rw_lock_t* rwlock;
  386. ulint type;
  387. ulint writer;
  388. type = cell->request_type;
  389. fprintf(file,
  390. "--Thread %lu has waited at %s line %lu"
  391. " for %.2f seconds the semaphore:\n",
  392. (ulong) os_thread_pf(cell->thread),
  393. innobase_basename(cell->file), (ulong) cell->line,
  394. difftime(time(NULL), cell->reservation_time));
  395. if (type == SYNC_MUTEX) {
  396. /* We use old_wait_mutex in case the cell has already
  397. been freed meanwhile */
  398. mutex = cell->old_wait_mutex;
  399. if (mutex) {
  400. fprintf(file,
  401. "Mutex at %p created file %s line %lu, lock var %lu\n"
  402. #ifdef UNIV_SYNC_DEBUG
  403. "Last time reserved in file %s line %lu, "
  404. #endif /* UNIV_SYNC_DEBUG */
  405. "waiters flag %lu\n",
  406. (void*) mutex, innobase_basename(mutex->cfile_name),
  407. (ulong) mutex->cline,
  408. (ulong) mutex->lock_word,
  409. #ifdef UNIV_SYNC_DEBUG
  410. mutex->file_name, (ulong) mutex->line,
  411. #endif /* UNIV_SYNC_DEBUG */
  412. (ulong) mutex->waiters);
  413. }
  414. } else if (type == RW_LOCK_EX
  415. || type == RW_LOCK_WAIT_EX
  416. || type == RW_LOCK_SHARED) {
  417. fputs(type == RW_LOCK_EX ? "X-lock on"
  418. : type == RW_LOCK_WAIT_EX ? "X-lock (wait_ex) on"
  419. : "S-lock on", file);
  420. rwlock = cell->old_wait_rw_lock;
  421. if (rwlock) {
  422. fprintf(file,
  423. " RW-latch at %p created in file %s line %lu\n",
  424. (void*) rwlock, innobase_basename(rwlock->cfile_name),
  425. (ulong) rwlock->cline);
  426. writer = rw_lock_get_writer(rwlock);
  427. if (writer != RW_LOCK_NOT_LOCKED) {
  428. fprintf(file,
  429. "a writer (thread id %lu) has"
  430. " reserved it in mode %s",
  431. (ulong) os_thread_pf(rwlock->writer_thread),
  432. writer == RW_LOCK_EX
  433. ? " exclusive\n"
  434. : " wait exclusive\n");
  435. *reserver = rwlock->writer_thread;
  436. }
  437. fprintf(file,
  438. "number of readers %lu, waiters flag %lu, "
  439. "lock_word: %lx\n"
  440. "Last time read locked in file %s line %lu\n"
  441. "Last time write locked in file %s line %lu\n",
  442. (ulong) rw_lock_get_reader_count(rwlock),
  443. (ulong) rwlock->waiters,
  444. rwlock->lock_word,
  445. innobase_basename(rwlock->last_s_file_name),
  446. (ulong) rwlock->last_s_line,
  447. rwlock->last_x_file_name,
  448. (ulong) rwlock->last_x_line);
  449. }
  450. } else {
  451. ut_error;
  452. }
  453. if (!cell->waiting) {
  454. fputs("wait has ended\n", file);
  455. }
  456. }
  457. #ifdef UNIV_SYNC_DEBUG
  458. /******************************************************************//**
  459. Recursion step for deadlock detection.
  460. @return TRUE if deadlock detected */
  461. static
  462. ibool
  463. sync_array_deadlock_step(
  464. /*=====================*/
  465. sync_array_t* arr, /*!< in: wait array; NOTE! the caller must
  466. own the mutex to array */
  467. sync_cell_t* start, /*!< in: cell where recursive search
  468. started */
  469. os_thread_id_t thread, /*!< in: thread to look at */
  470. ulint pass, /*!< in: pass value */
  471. ulint depth) /*!< in: recursion depth */
  472. {
  473. sync_cell_t* new_cell;
  474. if (pass != 0) {
  475. /* If pass != 0, then we do not know which threads are
  476. responsible of releasing the lock, and no deadlock can
  477. be detected. */
  478. return(FALSE);
  479. }
  480. new_cell = sync_array_find_thread(arr, thread);
  481. if (new_cell == start) {
  482. /* Deadlock */
  483. fputs("########################################\n"
  484. "DEADLOCK of threads detected!\n", stderr);
  485. return(TRUE);
  486. } else if (new_cell) {
  487. return(sync_array_detect_deadlock(
  488. arr, start, new_cell, depth + 1));
  489. }
  490. return(FALSE);
  491. }
  492. /******************************************************************//**
  493. This function is called only in the debug version. Detects a deadlock
  494. of one or more threads because of waits of semaphores.
  495. @return TRUE if deadlock detected */
  496. static
  497. ibool
  498. sync_array_detect_deadlock(
  499. /*=======================*/
  500. sync_array_t* arr, /*!< in: wait array; NOTE! the caller must
  501. own the mutex to array */
  502. sync_cell_t* start, /*!< in: cell where recursive search started */
  503. sync_cell_t* cell, /*!< in: cell to search */
  504. ulint depth) /*!< in: recursion depth */
  505. {
  506. ib_mutex_t* mutex;
  507. rw_lock_t* lock;
  508. os_thread_id_t thread;
  509. ibool ret;
  510. rw_lock_debug_t*debug;
  511. os_thread_id_t reserver=0;
  512. ut_a(arr);
  513. ut_a(start);
  514. ut_a(cell);
  515. ut_ad(cell->wait_object);
  516. ut_ad(os_thread_get_curr_id() == start->thread);
  517. ut_ad(depth < 100);
  518. depth++;
  519. if (!cell->waiting) {
  520. return(FALSE); /* No deadlock here */
  521. }
  522. if (cell->request_type == SYNC_MUTEX) {
  523. mutex = static_cast<ib_mutex_t*>(cell->wait_object);
  524. if (mutex_get_lock_word(mutex) != 0) {
  525. thread = mutex->thread_id;
  526. /* Note that mutex->thread_id above may be
  527. also OS_THREAD_ID_UNDEFINED, because the
  528. thread which held the mutex maybe has not
  529. yet updated the value, or it has already
  530. released the mutex: in this case no deadlock
  531. can occur, as the wait array cannot contain
  532. a thread with ID_UNDEFINED value. */
  533. ret = sync_array_deadlock_step(arr, start, thread, 0,
  534. depth);
  535. if (ret) {
  536. fprintf(stderr,
  537. "Mutex %p owned by thread %lu file %s line %lu\n",
  538. mutex, (ulong) os_thread_pf(mutex->thread_id),
  539. mutex->file_name, (ulong) mutex->line);
  540. sync_array_cell_print(stderr, cell, &reserver);
  541. return(TRUE);
  542. }
  543. }
  544. return(FALSE); /* No deadlock */
  545. } else if (cell->request_type == RW_LOCK_EX
  546. || cell->request_type == RW_LOCK_WAIT_EX) {
  547. lock = static_cast<rw_lock_t*>(cell->wait_object);
  548. for (debug = UT_LIST_GET_FIRST(lock->debug_list);
  549. debug != 0;
  550. debug = UT_LIST_GET_NEXT(list, debug)) {
  551. thread = debug->thread_id;
  552. if (((debug->lock_type == RW_LOCK_EX)
  553. && !os_thread_eq(thread, cell->thread))
  554. || ((debug->lock_type == RW_LOCK_WAIT_EX)
  555. && !os_thread_eq(thread, cell->thread))
  556. || (debug->lock_type == RW_LOCK_SHARED)) {
  557. /* The (wait) x-lock request can block
  558. infinitely only if someone (can be also cell
  559. thread) is holding s-lock, or someone
  560. (cannot be cell thread) (wait) x-lock, and
  561. he is blocked by start thread */
  562. ret = sync_array_deadlock_step(
  563. arr, start, thread, debug->pass,
  564. depth);
  565. if (ret) {
  566. print:
  567. fprintf(stderr, "rw-lock %p ",
  568. (void*) lock);
  569. sync_array_cell_print(stderr, cell, &reserver);
  570. rw_lock_debug_print(stderr, debug);
  571. return(TRUE);
  572. }
  573. }
  574. }
  575. return(FALSE);
  576. } else if (cell->request_type == RW_LOCK_SHARED) {
  577. lock = static_cast<rw_lock_t*>(cell->wait_object);
  578. for (debug = UT_LIST_GET_FIRST(lock->debug_list);
  579. debug != 0;
  580. debug = UT_LIST_GET_NEXT(list, debug)) {
  581. thread = debug->thread_id;
  582. if ((debug->lock_type == RW_LOCK_EX)
  583. || (debug->lock_type == RW_LOCK_WAIT_EX)) {
  584. /* The s-lock request can block infinitely
  585. only if someone (can also be cell thread) is
  586. holding (wait) x-lock, and he is blocked by
  587. start thread */
  588. ret = sync_array_deadlock_step(
  589. arr, start, thread, debug->pass,
  590. depth);
  591. if (ret) {
  592. goto print;
  593. }
  594. }
  595. }
  596. return(FALSE);
  597. } else {
  598. ut_error;
  599. }
  600. return(TRUE); /* Execution never reaches this line: for compiler
  601. fooling only */
  602. }
  603. #endif /* UNIV_SYNC_DEBUG */
  604. /******************************************************************//**
  605. Determines if we can wake up the thread waiting for a sempahore. */
  606. static
  607. ibool
  608. sync_arr_cell_can_wake_up(
  609. /*======================*/
  610. sync_cell_t* cell) /*!< in: cell to search */
  611. {
  612. ib_mutex_t* mutex;
  613. rw_lock_t* lock;
  614. if (cell->request_type == SYNC_MUTEX) {
  615. mutex = static_cast<ib_mutex_t*>(cell->wait_object);
  616. os_rmb;
  617. if (mutex_get_lock_word(mutex) == 0) {
  618. return(TRUE);
  619. }
  620. } else if (cell->request_type == RW_LOCK_EX) {
  621. lock = static_cast<rw_lock_t*>(cell->wait_object);
  622. os_rmb;
  623. if (lock->lock_word > 0) {
  624. /* Either unlocked or only read locked. */
  625. return(TRUE);
  626. }
  627. } else if (cell->request_type == RW_LOCK_WAIT_EX) {
  628. lock = static_cast<rw_lock_t*>(cell->wait_object);
  629. /* lock_word == 0 means all readers have left */
  630. os_rmb;
  631. if (lock->lock_word == 0) {
  632. return(TRUE);
  633. }
  634. } else if (cell->request_type == RW_LOCK_SHARED) {
  635. lock = static_cast<rw_lock_t*>(cell->wait_object);
  636. /* lock_word > 0 means no writer or reserved writer */
  637. os_rmb;
  638. if (lock->lock_word > 0) {
  639. return(TRUE);
  640. }
  641. }
  642. return(FALSE);
  643. }
  644. /******************************************************************//**
  645. Frees the cell. NOTE! sync_array_wait_event frees the cell
  646. automatically! */
  647. UNIV_INTERN
  648. void
  649. sync_array_free_cell(
  650. /*=================*/
  651. sync_array_t* arr, /*!< in: wait array */
  652. ulint index) /*!< in: index of the cell in array */
  653. {
  654. sync_cell_t* cell;
  655. sync_array_enter(arr);
  656. cell = sync_array_get_nth_cell(arr, index);
  657. ut_a(cell->wait_object != NULL);
  658. cell->waiting = FALSE;
  659. cell->wait_object = NULL;
  660. cell->signal_count = 0;
  661. ut_a(arr->n_reserved > 0);
  662. arr->n_reserved--;
  663. sync_array_exit(arr);
  664. }
  665. /**********************************************************************//**
  666. Increments the signalled count. */
  667. UNIV_INTERN
  668. void
  669. sync_array_object_signalled(void)
  670. /*=============================*/
  671. {
  672. #ifdef HAVE_ATOMIC_BUILTINS
  673. (void) os_atomic_increment_ulint(&sg_count, 1);
  674. #else
  675. ++sg_count;
  676. #endif /* HAVE_ATOMIC_BUILTINS */
  677. }
  678. /**********************************************************************//**
  679. If the wakeup algorithm does not work perfectly at semaphore relases,
  680. this function will do the waking (see the comment in mutex_exit). This
  681. function should be called about every 1 second in the server.
  682. Note that there's a race condition between this thread and mutex_exit
  683. changing the lock_word and calling signal_object, so sometimes this finds
  684. threads to wake up even when nothing has gone wrong. */
  685. static
  686. void
  687. sync_array_wake_threads_if_sema_free_low(
  688. /*=====================================*/
  689. sync_array_t* arr) /* in/out: wait array */
  690. {
  691. ulint i = 0;
  692. ulint count;
  693. sync_array_enter(arr);
  694. for (count = 0; count < arr->n_reserved; ++i) {
  695. sync_cell_t* cell;
  696. cell = sync_array_get_nth_cell(arr, i);
  697. if (cell->wait_object != NULL) {
  698. count++;
  699. if (sync_arr_cell_can_wake_up(cell)) {
  700. os_event_t event;
  701. event = sync_cell_get_event(cell);
  702. os_event_set(event);
  703. }
  704. }
  705. }
  706. sync_array_exit(arr);
  707. }
  708. /**********************************************************************//**
  709. If the wakeup algorithm does not work perfectly at semaphore relases,
  710. this function will do the waking (see the comment in mutex_exit). This
  711. function should be called about every 1 second in the server.
  712. Note that there's a race condition between this thread and mutex_exit
  713. changing the lock_word and calling signal_object, so sometimes this finds
  714. threads to wake up even when nothing has gone wrong. */
  715. UNIV_INTERN
  716. void
  717. sync_arr_wake_threads_if_sema_free(void)
  718. /*====================================*/
  719. {
  720. ulint i;
  721. for (i = 0; i < sync_array_size; ++i) {
  722. sync_array_wake_threads_if_sema_free_low(
  723. sync_wait_array[i]);
  724. }
  725. }
  726. /**********************************************************************//**
  727. Prints warnings of long semaphore waits to stderr.
  728. @return TRUE if fatal semaphore wait threshold was exceeded */
  729. static
  730. ibool
  731. sync_array_print_long_waits_low(
  732. /*============================*/
  733. sync_array_t* arr, /*!< in: sync array instance */
  734. os_thread_id_t* waiter, /*!< out: longest waiting thread */
  735. const void** sema, /*!< out: longest-waited-for semaphore */
  736. ibool* noticed)/*!< out: TRUE if long wait noticed */
  737. {
  738. ulint i;
  739. ulint fatal_timeout = srv_fatal_semaphore_wait_threshold;
  740. ibool fatal = FALSE;
  741. double longest_diff = 0;
  742. /* For huge tables, skip the check during CHECK TABLE etc... */
  743. if (fatal_timeout > SRV_SEMAPHORE_WAIT_EXTENSION) {
  744. return(FALSE);
  745. }
  746. #ifdef UNIV_DEBUG_VALGRIND
  747. /* Increase the timeouts if running under valgrind because it executes
  748. extremely slowly. UNIV_DEBUG_VALGRIND does not necessary mean that
  749. we are running under valgrind but we have no better way to tell.
  750. See Bug#58432 innodb.innodb_bug56143 fails under valgrind
  751. for an example */
  752. # define SYNC_ARRAY_TIMEOUT 2400
  753. fatal_timeout *= 10;
  754. #else
  755. # define SYNC_ARRAY_TIMEOUT 240
  756. #endif
  757. for (i = 0; i < arr->n_cells; i++) {
  758. double diff;
  759. sync_cell_t* cell;
  760. void* wait_object;
  761. os_thread_id_t reserver=0;
  762. cell = sync_array_get_nth_cell(arr, i);
  763. wait_object = cell->wait_object;
  764. if (wait_object == NULL || !cell->waiting) {
  765. continue;
  766. }
  767. diff = difftime(time(NULL), cell->reservation_time);
  768. if (diff > SYNC_ARRAY_TIMEOUT) {
  769. fputs("InnoDB: Warning: a long semaphore wait:\n",
  770. stderr);
  771. sync_array_cell_print(stderr, cell, &reserver);
  772. *noticed = TRUE;
  773. }
  774. if (diff > fatal_timeout) {
  775. fatal = TRUE;
  776. }
  777. if (diff > longest_diff) {
  778. longest_diff = diff;
  779. *sema = wait_object;
  780. *waiter = cell->thread;
  781. }
  782. }
  783. /* We found a long semaphore wait, wait all threads that are
  784. waiting for a semaphore. */
  785. if (*noticed) {
  786. for (i = 0; i < arr->n_cells; i++) {
  787. void* wait_object;
  788. os_thread_id_t reserver=(os_thread_id_t)ULINT_UNDEFINED;
  789. sync_cell_t* cell;
  790. ulint loop = 0;
  791. cell = sync_array_get_nth_cell(arr, i);
  792. wait_object = cell->wait_object;
  793. if (wait_object == NULL || !cell->waiting) {
  794. continue;
  795. }
  796. fputs("InnoDB: Warning: semaphore wait:\n",
  797. stderr);
  798. sync_array_cell_print(stderr, cell, &reserver);
  799. /* Try to output cell information for writer recursive way */
  800. while (reserver != (os_thread_id_t)ULINT_UNDEFINED) {
  801. sync_cell_t* reserver_wait;
  802. reserver_wait = sync_array_find_thread(arr, reserver);
  803. if (reserver_wait &&
  804. reserver_wait->wait_object != NULL &&
  805. reserver_wait->waiting) {
  806. fputs("InnoDB: Warning: Writer thread is waiting this semaphore:\n",
  807. stderr);
  808. reserver = (os_thread_id_t)ULINT_UNDEFINED;
  809. sync_array_cell_print(stderr, reserver_wait, &reserver);
  810. loop++;
  811. if (reserver_wait->thread == reserver) {
  812. reserver = (os_thread_id_t)ULINT_UNDEFINED;
  813. }
  814. } else {
  815. reserver = (os_thread_id_t)ULINT_UNDEFINED;
  816. }
  817. /* This is protection against loop */
  818. if (loop > 100) {
  819. fputs("InnoDB: Warning: Too many waiting threads.\n", stderr);
  820. break;
  821. }
  822. }
  823. }
  824. }
  825. #undef SYNC_ARRAY_TIMEOUT
  826. return(fatal);
  827. }
  828. /**********************************************************************//**
  829. Prints warnings of long semaphore waits to stderr.
  830. @return TRUE if fatal semaphore wait threshold was exceeded */
  831. UNIV_INTERN
  832. ibool
  833. sync_array_print_long_waits(
  834. /*========================*/
  835. os_thread_id_t* waiter, /*!< out: longest waiting thread */
  836. const void** sema) /*!< out: longest-waited-for semaphore */
  837. {
  838. ulint i;
  839. ibool fatal = FALSE;
  840. ibool noticed = FALSE;
  841. for (i = 0; i < sync_array_size; ++i) {
  842. sync_array_t* arr = sync_wait_array[i];
  843. sync_array_enter(arr);
  844. if (sync_array_print_long_waits_low(
  845. arr, waiter, sema, &noticed)) {
  846. fatal = TRUE;
  847. }
  848. sync_array_exit(arr);
  849. }
  850. if (noticed) {
  851. ibool old_val;
  852. fprintf(stderr,
  853. "InnoDB: ###### Starts InnoDB Monitor"
  854. " for 30 secs to print diagnostic info:\n");
  855. old_val = srv_print_innodb_monitor;
  856. /* If some crucial semaphore is reserved, then also the InnoDB
  857. Monitor can hang, and we do not get diagnostics. Since in
  858. many cases an InnoDB hang is caused by a pwrite() or a pread()
  859. call hanging inside the operating system, let us print right
  860. now the values of pending calls of these. */
  861. fprintf(stderr,
  862. "InnoDB: Pending reads " UINT64PF
  863. ", writes " UINT64PF "\n",
  864. MONITOR_VALUE(MONITOR_OS_PENDING_READS),
  865. MONITOR_VALUE(MONITOR_OS_PENDING_WRITES));
  866. srv_print_innodb_monitor = TRUE;
  867. os_event_set(srv_monitor_event);
  868. os_thread_sleep(30000000);
  869. srv_print_innodb_monitor = static_cast<my_bool>(old_val);
  870. fprintf(stderr,
  871. "InnoDB: ###### Diagnostic info printed"
  872. " to the standard error stream\n");
  873. }
  874. return(fatal);
  875. }
  876. /**********************************************************************//**
  877. Prints info of the wait array. */
  878. static
  879. void
  880. sync_array_print_info_low(
  881. /*======================*/
  882. FILE* file, /*!< in: file where to print */
  883. sync_array_t* arr) /*!< in: wait array */
  884. {
  885. ulint i;
  886. ulint count = 0;
  887. os_thread_id_t r = 0;
  888. fprintf(file,
  889. "OS WAIT ARRAY INFO: reservation count " ULINTPF "\n",
  890. arr->res_count);
  891. for (i = 0; count < arr->n_reserved; ++i) {
  892. sync_cell_t* cell;
  893. cell = sync_array_get_nth_cell(arr, i);
  894. if (cell->wait_object != NULL) {
  895. count++;
  896. sync_array_cell_print(file, cell, &r);
  897. }
  898. }
  899. }
  900. /**********************************************************************//**
  901. Prints info of the wait array. */
  902. static
  903. void
  904. sync_array_print_info(
  905. /*==================*/
  906. FILE* file, /*!< in: file where to print */
  907. sync_array_t* arr) /*!< in: wait array */
  908. {
  909. sync_array_enter(arr);
  910. sync_array_print_info_low(file, arr);
  911. sync_array_exit(arr);
  912. }
  913. /**********************************************************************//**
  914. Create the primary system wait array(s), they are protected by an OS mutex */
  915. UNIV_INTERN
  916. void
  917. sync_array_init(
  918. /*============*/
  919. ulint n_threads) /*!< in: Number of slots to
  920. create in all arrays */
  921. {
  922. ulint i;
  923. ulint n_slots;
  924. ut_a(sync_wait_array == NULL);
  925. ut_a(srv_sync_array_size > 0);
  926. ut_a(n_threads > 0);
  927. sync_array_size = srv_sync_array_size;
  928. /* We have to use ut_malloc() because the mutex infrastructure
  929. hasn't been initialised yet. It is required by mem_alloc() and
  930. the heap functions. */
  931. sync_wait_array = static_cast<sync_array_t**>(
  932. ut_malloc(sizeof(*sync_wait_array) * sync_array_size));
  933. n_slots = 1 + (n_threads - 1) / sync_array_size;
  934. for (i = 0; i < sync_array_size; ++i) {
  935. sync_wait_array[i] = sync_array_create(n_slots);
  936. }
  937. }
  938. /**********************************************************************//**
  939. Close sync array wait sub-system. */
  940. UNIV_INTERN
  941. void
  942. sync_array_close(void)
  943. /*==================*/
  944. {
  945. ulint i;
  946. for (i = 0; i < sync_array_size; ++i) {
  947. sync_array_free(sync_wait_array[i]);
  948. }
  949. ut_free(sync_wait_array);
  950. sync_wait_array = NULL;
  951. }
  952. /**********************************************************************//**
  953. Print info about the sync array(s). */
  954. UNIV_INTERN
  955. void
  956. sync_array_print(
  957. /*=============*/
  958. FILE* file) /*!< in/out: Print to this stream */
  959. {
  960. ulint i;
  961. for (i = 0; i < sync_array_size; ++i) {
  962. sync_array_print_info(file, sync_wait_array[i]);
  963. }
  964. fprintf(file,
  965. "OS WAIT ARRAY INFO: signal count " ULINTPF "\n", sg_count);
  966. }
  967. /**********************************************************************//**
  968. Get an instance of the sync wait array. */
  969. UNIV_INTERN
  970. sync_array_t*
  971. sync_array_get(void)
  972. /*================*/
  973. {
  974. ulint i;
  975. static ulint count;
  976. #ifdef HAVE_ATOMIC_BUILTINS
  977. i = os_atomic_increment_ulint(&count, 1);
  978. #else
  979. i = count++;
  980. #endif /* HAVE_ATOMIC_BUILTINS */
  981. return(sync_wait_array[i % sync_array_size]);
  982. }
  983. /**********************************************************************//**
  984. Prints info of the wait array without using any mutexes/semaphores. */
  985. UNIV_INTERN
  986. void
  987. sync_array_print_innodb(void)
  988. /*=========================*/
  989. {
  990. ulint i;
  991. sync_array_t* arr = sync_array_get();
  992. fputs("InnoDB: Semaphore wait debug output started for InnoDB:\n", stderr);
  993. for (i = 0; i < arr->n_cells; i++) {
  994. void* wait_object;
  995. sync_cell_t* cell;
  996. os_thread_id_t reserver=(os_thread_id_t)ULINT_UNDEFINED;
  997. ulint loop=0;
  998. cell = sync_array_get_nth_cell(arr, i);
  999. wait_object = cell->wait_object;
  1000. if (wait_object == NULL || !cell->waiting) {
  1001. continue;
  1002. }
  1003. fputs("InnoDB: Warning: semaphore wait:\n",
  1004. stderr);
  1005. sync_array_cell_print(stderr, cell, &reserver);
  1006. /* Try to output cell information for writer recursive way */
  1007. while (reserver != (os_thread_id_t)ULINT_UNDEFINED) {
  1008. sync_cell_t* reserver_wait;
  1009. reserver_wait = sync_array_find_thread(arr, reserver);
  1010. if (reserver_wait &&
  1011. reserver_wait->wait_object != NULL &&
  1012. reserver_wait->waiting) {
  1013. fputs("InnoDB: Warning: Writer thread is waiting this semaphore:\n",
  1014. stderr);
  1015. sync_array_cell_print(stderr, reserver_wait, &reserver);
  1016. if (reserver_wait->thread == reserver) {
  1017. reserver = (os_thread_id_t)ULINT_UNDEFINED;
  1018. }
  1019. } else {
  1020. reserver = (os_thread_id_t)ULINT_UNDEFINED;
  1021. }
  1022. /* This is protection against loop */
  1023. if (loop > 100) {
  1024. fputs("InnoDB: Warning: Too many waiting threads.\n", stderr);
  1025. break;
  1026. }
  1027. }
  1028. }
  1029. fputs("InnoDB: Semaphore wait debug output ended:\n", stderr);
  1030. }