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.

178 lines
6.0 KiB

  1. /* Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; version 2 of the License.
  5. This program is distributed in the hope that it will be useful,
  6. but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License
  10. along with this program; if not, write to the Free Software Foundation,
  11. 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA */
  12. /**
  13. @file storage/perfschema/pfs_events_waits.cc
  14. Events waits data structures (implementation).
  15. */
  16. #include "my_global.h"
  17. #include "my_sys.h"
  18. #include "pfs_global.h"
  19. #include "pfs_instr.h"
  20. #include "pfs_events_waits.h"
  21. #include "pfs_atomic.h"
  22. #include "m_string.h"
  23. ulong events_waits_history_long_size= 0;
  24. /** Consumer flag for table EVENTS_WAITS_CURRENT. */
  25. bool flag_events_waits_current= true;
  26. /** Consumer flag for table EVENTS_WAITS_HISTORY. */
  27. bool flag_events_waits_history= true;
  28. /** Consumer flag for table EVENTS_WAITS_HISTORY_LONG. */
  29. bool flag_events_waits_history_long= true;
  30. /** Consumer flag for table EVENTS_WAITS_SUMMARY_BY_THREAD_BY_EVENT_NAME. */
  31. bool flag_events_waits_summary_by_thread_by_event_name= true;
  32. /** Consumer flag for table EVENTS_WAITS_SUMMARY_BY_EVENT_NAME. */
  33. bool flag_events_waits_summary_by_event_name= true;
  34. /** Consumer flag for table EVENTS_WAITS_SUMMARY_BY_INSTANCE. */
  35. bool flag_events_waits_summary_by_instance= true;
  36. bool flag_events_locks_summary_by_event_name= true;
  37. bool flag_events_locks_summary_by_instance= true;
  38. /** Consumer flag for table FILE_SUMMARY_BY_EVENT_NAME. */
  39. bool flag_file_summary_by_event_name= true;
  40. /** Consumer flag for table FILE_SUMMARY_BY_INSTANCE. */
  41. bool flag_file_summary_by_instance= true;
  42. /** True if EVENTS_WAITS_HISTORY_LONG circular buffer is full. */
  43. bool events_waits_history_long_full= false;
  44. /** Index in EVENTS_WAITS_HISTORY_LONG circular buffer. */
  45. volatile uint32 events_waits_history_long_index= 0;
  46. /** EVENTS_WAITS_HISTORY_LONG circular buffer. */
  47. PFS_events_waits *events_waits_history_long_array= NULL;
  48. /**
  49. Initialize table EVENTS_WAITS_HISTORY_LONG.
  50. @param events_waits_history_long_sizing table sizing
  51. */
  52. int init_events_waits_history_long(uint events_waits_history_long_sizing)
  53. {
  54. events_waits_history_long_size= events_waits_history_long_sizing;
  55. events_waits_history_long_full= false;
  56. PFS_atomic::store_u32(&events_waits_history_long_index, 0);
  57. if (events_waits_history_long_size == 0)
  58. return 0;
  59. events_waits_history_long_array=
  60. PFS_MALLOC_ARRAY(events_waits_history_long_size, PFS_events_waits,
  61. MYF(MY_ZEROFILL));
  62. return (events_waits_history_long_array ? 0 : 1);
  63. }
  64. /** Cleanup table EVENTS_WAITS_HISTORY_LONG. */
  65. void cleanup_events_waits_history_long(void)
  66. {
  67. pfs_free(events_waits_history_long_array);
  68. events_waits_history_long_array= NULL;
  69. }
  70. static inline void copy_events_waits(PFS_events_waits *dest,
  71. const PFS_events_waits *source)
  72. {
  73. memcpy(dest, source, sizeof(PFS_events_waits));
  74. }
  75. /**
  76. Insert a wait record in table EVENTS_WAITS_HISTORY.
  77. @param thread thread that executed the wait
  78. @param wait record to insert
  79. */
  80. void insert_events_waits_history(PFS_thread *thread, PFS_events_waits *wait)
  81. {
  82. uint index= thread->m_waits_history_index;
  83. /*
  84. A concurrent thread executing TRUNCATE TABLE EVENTS_WAITS_CURRENT
  85. could alter the data that this thread is inserting,
  86. causing a potential race condition.
  87. We are not testing for this and insert a possibly empty record,
  88. to make this thread (the writer) faster.
  89. This is ok, the readers of m_waits_history will filter this out.
  90. */
  91. copy_events_waits(&thread->m_waits_history[index], wait);
  92. index++;
  93. if (index >= events_waits_history_per_thread)
  94. {
  95. index= 0;
  96. thread->m_waits_history_full= true;
  97. }
  98. thread->m_waits_history_index= index;
  99. }
  100. /**
  101. Insert a wait record in table EVENTS_WAITS_HISTORY_LONG.
  102. @param wait record to insert
  103. */
  104. void insert_events_waits_history_long(PFS_events_waits *wait)
  105. {
  106. uint index= PFS_atomic::add_u32(&events_waits_history_long_index, 1);
  107. index= index % events_waits_history_long_size;
  108. if (index == 0)
  109. events_waits_history_long_full= true;
  110. /* See related comment in insert_events_waits_history. */
  111. copy_events_waits(&events_waits_history_long_array[index], wait);
  112. }
  113. /** Reset table EVENTS_WAITS_CURRENT data. */
  114. void reset_events_waits_current(void)
  115. {
  116. PFS_thread *pfs_thread= thread_array;
  117. PFS_thread *pfs_thread_last= thread_array + thread_max;
  118. for ( ; pfs_thread < pfs_thread_last; pfs_thread++)
  119. {
  120. PFS_wait_locker *locker= pfs_thread->m_wait_locker_stack;
  121. PFS_wait_locker *locker_last= locker + LOCKER_STACK_SIZE;
  122. for ( ; locker < locker_last; locker++)
  123. locker->m_waits_current.m_wait_class= NO_WAIT_CLASS;
  124. }
  125. }
  126. /** Reset table EVENTS_WAITS_HISTORY data. */
  127. void reset_events_waits_history(void)
  128. {
  129. PFS_thread *pfs_thread= thread_array;
  130. PFS_thread *pfs_thread_last= thread_array + thread_max;
  131. for ( ; pfs_thread < pfs_thread_last; pfs_thread++)
  132. {
  133. PFS_events_waits *wait= pfs_thread->m_waits_history;
  134. PFS_events_waits *wait_last= wait + events_waits_history_per_thread;
  135. pfs_thread->m_waits_history_index= 0;
  136. pfs_thread->m_waits_history_full= false;
  137. for ( ; wait < wait_last; wait++)
  138. wait->m_wait_class= NO_WAIT_CLASS;
  139. }
  140. }
  141. /** Reset table EVENTS_WAITS_HISTORY_LONG data. */
  142. void reset_events_waits_history_long(void)
  143. {
  144. PFS_atomic::store_u32(&events_waits_history_long_index, 0);
  145. events_waits_history_long_full= false;
  146. PFS_events_waits *wait= events_waits_history_long_array;
  147. PFS_events_waits *wait_last= wait + events_waits_history_long_size;
  148. for ( ; wait < wait_last; wait++)
  149. wait->m_wait_class= NO_WAIT_CLASS;
  150. }