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.

2622 lines
74 KiB

20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
  1. /******************************************************
  2. The database server main program
  3. NOTE: SQL Server 7 uses something which the documentation
  4. calls user mode scheduled threads (UMS threads). One such
  5. thread is usually allocated per processor. Win32
  6. documentation does not know any UMS threads, which suggests
  7. that the concept is internal to SQL Server 7. It may mean that
  8. SQL Server 7 does all the scheduling of threads itself, even
  9. in i/o waits. We should maybe modify InnoDB to use the same
  10. technique, because thread switches within NT may be too slow.
  11. SQL Server 7 also mentions fibers, which are cooperatively
  12. scheduled threads. They can boost performance by 5 %,
  13. according to the Delaney and Soukup's book.
  14. Windows 2000 will have something called thread pooling
  15. (see msdn website), which we could possibly use.
  16. Another possibility could be to use some very fast user space
  17. thread library. This might confuse NT though.
  18. (c) 1995 Innobase Oy
  19. Created 10/8/1995 Heikki Tuuri
  20. *******************************************************/
  21. /* Dummy comment */
  22. #include "srv0srv.h"
  23. #include "ut0mem.h"
  24. #include "ut0ut.h"
  25. #include "os0proc.h"
  26. #include "mem0mem.h"
  27. #include "mem0pool.h"
  28. #include "sync0sync.h"
  29. #include "thr0loc.h"
  30. #include "que0que.h"
  31. #include "srv0que.h"
  32. #include "log0recv.h"
  33. #include "pars0pars.h"
  34. #include "usr0sess.h"
  35. #include "lock0lock.h"
  36. #include "trx0purge.h"
  37. #include "ibuf0ibuf.h"
  38. #include "buf0flu.h"
  39. #include "btr0sea.h"
  40. #include "dict0load.h"
  41. #include "dict0boot.h"
  42. #include "srv0start.h"
  43. #include "row0mysql.h"
  44. #include "ha_prototypes.h"
  45. #include "trx0i_s.h"
  46. /* This is set to TRUE if the MySQL user has set it in MySQL; currently
  47. affects only FOREIGN KEY definition parsing */
  48. ibool srv_lower_case_table_names = FALSE;
  49. /* The following counter is incremented whenever there is some user activity
  50. in the server */
  51. ulint srv_activity_count = 0;
  52. /* The following is the maximum allowed duration of a lock wait. */
  53. ulint srv_fatal_semaphore_wait_threshold = 600;
  54. /* How much data manipulation language (DML) statements need to be delayed,
  55. in microseconds, in order to reduce the lagging of the purge thread. */
  56. ulint srv_dml_needed_delay = 0;
  57. ibool srv_lock_timeout_and_monitor_active = FALSE;
  58. ibool srv_error_monitor_active = FALSE;
  59. const char* srv_main_thread_op_info = "";
  60. /* Prefix used by MySQL to indicate pre-5.1 table name encoding */
  61. const char srv_mysql50_table_name_prefix[9] = "#mysql50#";
  62. /* Server parameters which are read from the initfile */
  63. /* The following three are dir paths which are catenated before file
  64. names, where the file name itself may also contain a path */
  65. char* srv_data_home = NULL;
  66. #ifdef UNIV_LOG_ARCHIVE
  67. char* srv_arch_dir = NULL;
  68. #endif /* UNIV_LOG_ARCHIVE */
  69. ibool srv_file_per_table = FALSE; /* store to its own file each table
  70. created by an user; data dictionary
  71. tables are in the system tablespace
  72. 0 */
  73. ibool srv_locks_unsafe_for_binlog = FALSE; /* Place locks to
  74. records only i.e. do
  75. not use next-key
  76. locking except on
  77. duplicate key checking
  78. and foreign key
  79. checking */
  80. ulint srv_n_data_files = 0;
  81. char** srv_data_file_names = NULL;
  82. ulint* srv_data_file_sizes = NULL; /* size in database pages */
  83. ibool srv_auto_extend_last_data_file = FALSE; /* if TRUE, then we
  84. auto-extend the last data
  85. file */
  86. ulint srv_last_file_size_max = 0; /* if != 0, this tells
  87. the max size auto-extending
  88. may increase the last data
  89. file size */
  90. ulong srv_auto_extend_increment = 8; /* If the last data file is
  91. auto-extended, we add this
  92. many pages to it at a time */
  93. ulint* srv_data_file_is_raw_partition = NULL;
  94. /* If the following is TRUE we do not allow inserts etc. This protects
  95. the user from forgetting the 'newraw' keyword to my.cnf */
  96. ibool srv_created_new_raw = FALSE;
  97. char** srv_log_group_home_dirs = NULL;
  98. ulint srv_n_log_groups = ULINT_MAX;
  99. ulint srv_n_log_files = ULINT_MAX;
  100. ulint srv_log_file_size = ULINT_MAX; /* size in database pages */
  101. ulint srv_log_buffer_size = ULINT_MAX; /* size in database pages */
  102. ulong srv_flush_log_at_trx_commit = 1;
  103. byte srv_latin1_ordering[256] /* The sort order table of the latin1
  104. character set. The following table is
  105. the MySQL order as of Feb 10th, 2002 */
  106. = {
  107. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  108. , 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  109. , 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17
  110. , 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
  111. , 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27
  112. , 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F
  113. , 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  114. , 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F
  115. , 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47
  116. , 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F
  117. , 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57
  118. , 0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F
  119. , 0x60, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47
  120. , 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F
  121. , 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57
  122. , 0x58, 0x59, 0x5A, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F
  123. , 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87
  124. , 0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F
  125. , 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97
  126. , 0x98, 0x99, 0x9A, 0x9B, 0x9C, 0x9D, 0x9E, 0x9F
  127. , 0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7
  128. , 0xA8, 0xA9, 0xAA, 0xAB, 0xAC, 0xAD, 0xAE, 0xAF
  129. , 0xB0, 0xB1, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7
  130. , 0xB8, 0xB9, 0xBA, 0xBB, 0xBC, 0xBD, 0xBE, 0xBF
  131. , 0x41, 0x41, 0x41, 0x41, 0x5C, 0x5B, 0x5C, 0x43
  132. , 0x45, 0x45, 0x45, 0x45, 0x49, 0x49, 0x49, 0x49
  133. , 0x44, 0x4E, 0x4F, 0x4F, 0x4F, 0x4F, 0x5D, 0xD7
  134. , 0xD8, 0x55, 0x55, 0x55, 0x59, 0x59, 0xDE, 0xDF
  135. , 0x41, 0x41, 0x41, 0x41, 0x5C, 0x5B, 0x5C, 0x43
  136. , 0x45, 0x45, 0x45, 0x45, 0x49, 0x49, 0x49, 0x49
  137. , 0x44, 0x4E, 0x4F, 0x4F, 0x4F, 0x4F, 0x5D, 0xF7
  138. , 0xD8, 0x55, 0x55, 0x55, 0x59, 0x59, 0xDE, 0xFF
  139. };
  140. ulong srv_buf_pool_size = ULINT_MAX; /* requested size
  141. in kilobytes */
  142. ulong srv_buf_pool_old_size; /* previously requested size */
  143. ulong srv_buf_pool_curr_size = 0; /* current size
  144. in kilobytes */
  145. ulint srv_mem_pool_size = ULINT_MAX; /* size in bytes */
  146. ulint srv_lock_table_size = ULINT_MAX;
  147. ulint srv_n_file_io_threads = ULINT_MAX;
  148. #ifdef UNIV_LOG_ARCHIVE
  149. ibool srv_log_archive_on = FALSE;
  150. ibool srv_archive_recovery = 0;
  151. ib_uint64_t srv_archive_recovery_limit_lsn;
  152. #endif /* UNIV_LOG_ARCHIVE */
  153. ulint srv_lock_wait_timeout = 1024 * 1024 * 1024;
  154. /* This parameter is used to throttle the number of insert buffers that are
  155. merged in a batch. By increasing this parameter on a faster disk you can
  156. possibly reduce the number of I/O operations performed to complete the
  157. merge operation. The value of this parameter is used as is by the
  158. background loop when the system is idle (low load), on a busy system
  159. the parameter is scaled down by a factor of 4, this is to avoid putting
  160. a heavier load on the I/O sub system. */
  161. ulong srv_insert_buffer_batch_size = 20;
  162. char* srv_file_flush_method_str = NULL;
  163. ulint srv_unix_file_flush_method = SRV_UNIX_FDATASYNC;
  164. ulint srv_win_file_flush_method = SRV_WIN_IO_UNBUFFERED;
  165. ulint srv_max_n_open_files = 300;
  166. /* The InnoDB main thread tries to keep the ratio of modified pages
  167. in the buffer pool to all database pages in the buffer pool smaller than
  168. the following number. But it is not guaranteed that the value stays below
  169. that during a time of heavy update/insert activity. */
  170. ulong srv_max_buf_pool_modified_pct = 90;
  171. /* variable counts amount of data read in total (in bytes) */
  172. ulint srv_data_read = 0;
  173. /* here we count the amount of data written in total (in bytes) */
  174. ulint srv_data_written = 0;
  175. /* the number of the log write requests done */
  176. ulint srv_log_write_requests = 0;
  177. /* the number of physical writes to the log performed */
  178. ulint srv_log_writes = 0;
  179. /* amount of data written to the log files in bytes */
  180. ulint srv_os_log_written = 0;
  181. /* amount of writes being done to the log files */
  182. ulint srv_os_log_pending_writes = 0;
  183. /* we increase this counter, when there we don't have enough space in the
  184. log buffer and have to flush it */
  185. ulint srv_log_waits = 0;
  186. /* this variable counts the amount of times, when the doublewrite buffer
  187. was flushed */
  188. ulint srv_dblwr_writes = 0;
  189. /* here we store the number of pages that have been flushed to the
  190. doublewrite buffer */
  191. ulint srv_dblwr_pages_written = 0;
  192. /* in this variable we store the number of write requests issued */
  193. ulint srv_buf_pool_write_requests = 0;
  194. /* here we store the number of times when we had to wait for a free page
  195. in the buffer pool. It happens when the buffer pool is full and we need
  196. to make a flush, in order to be able to read or create a page. */
  197. ulint srv_buf_pool_wait_free = 0;
  198. /* variable to count the number of pages that were written from buffer
  199. pool to the disk */
  200. ulint srv_buf_pool_flushed = 0;
  201. /* variable to count the number of buffer pool reads that led to the
  202. reading of a disk page */
  203. ulint srv_buf_pool_reads = 0;
  204. /* variable to count the number of sequential read-aheads */
  205. ulint srv_read_ahead_seq = 0;
  206. /* variable to count the number of random read-aheads */
  207. ulint srv_read_ahead_rnd = 0;
  208. /* structure to pass status variables to MySQL */
  209. export_struc export_vars;
  210. /* If the following is != 0 we do not allow inserts etc. This protects
  211. the user from forgetting the innodb_force_recovery keyword to my.cnf */
  212. ulint srv_force_recovery = 0;
  213. /*-----------------------*/
  214. /* We are prepared for a situation that we have this many threads waiting for
  215. a semaphore inside InnoDB. innobase_start_or_create_for_mysql() sets the
  216. value. */
  217. ulint srv_max_n_threads = 0;
  218. /* The following controls how many threads we let inside InnoDB concurrently:
  219. threads waiting for locks are not counted into the number because otherwise
  220. we could get a deadlock. MySQL creates a thread for each user session, and
  221. semaphore contention and convoy problems can occur withput this restriction.
  222. Value 10 should be good if there are less than 4 processors + 4 disks in the
  223. computer. Bigger computers need bigger values. Value 0 will disable the
  224. concurrency check. */
  225. ulong srv_thread_concurrency = 0;
  226. ulong srv_commit_concurrency = 0;
  227. os_fast_mutex_t srv_conc_mutex; /* this mutex protects srv_conc data
  228. structures */
  229. lint srv_conc_n_threads = 0; /* number of OS threads currently
  230. inside InnoDB; it is not an error
  231. if this drops temporarily below zero
  232. because we do not demand that every
  233. thread increments this, but a thread
  234. waiting for a lock decrements this
  235. temporarily */
  236. ulint srv_conc_n_waiting_threads = 0; /* number of OS threads waiting in the
  237. FIFO for a permission to enter InnoDB
  238. */
  239. typedef struct srv_conc_slot_struct srv_conc_slot_t;
  240. struct srv_conc_slot_struct{
  241. os_event_t event; /* event to wait */
  242. ibool reserved; /* TRUE if slot
  243. reserved */
  244. ibool wait_ended; /* TRUE when another
  245. thread has already set
  246. the event and the
  247. thread in this slot is
  248. free to proceed; but
  249. reserved may still be
  250. TRUE at that point */
  251. UT_LIST_NODE_T(srv_conc_slot_t) srv_conc_queue; /* queue node */
  252. };
  253. UT_LIST_BASE_NODE_T(srv_conc_slot_t) srv_conc_queue; /* queue of threads
  254. waiting to get in */
  255. srv_conc_slot_t* srv_conc_slots; /* array of wait
  256. slots */
  257. /* Number of times a thread is allowed to enter InnoDB within the same
  258. SQL query after it has once got the ticket at srv_conc_enter_innodb */
  259. #define SRV_FREE_TICKETS_TO_ENTER srv_n_free_tickets_to_enter
  260. #define SRV_THREAD_SLEEP_DELAY srv_thread_sleep_delay
  261. /*-----------------------*/
  262. /* If the following is set to 1 then we do not run purge and insert buffer
  263. merge to completion before shutdown. If it is set to 2, do not even flush the
  264. buffer pool to data files at the shutdown: we effectively 'crash'
  265. InnoDB (but lose no committed transactions). */
  266. ulint srv_fast_shutdown = 0;
  267. /* Generate a innodb_status.<pid> file */
  268. ibool srv_innodb_status = FALSE;
  269. ibool srv_stats_on_metadata = TRUE;
  270. ibool srv_use_doublewrite_buf = TRUE;
  271. ibool srv_use_checksums = TRUE;
  272. ibool srv_set_thread_priorities = TRUE;
  273. int srv_query_thread_priority = 0;
  274. ulint srv_replication_delay = 0;
  275. /*-------------------------------------------*/
  276. ulong srv_n_spin_wait_rounds = 20;
  277. ulong srv_n_free_tickets_to_enter = 500;
  278. ulong srv_thread_sleep_delay = 10000;
  279. ulint srv_spin_wait_delay = 5;
  280. ibool srv_priority_boost = TRUE;
  281. #ifdef UNIV_DEBUG
  282. ibool srv_print_thread_releases = FALSE;
  283. ibool srv_print_lock_waits = FALSE;
  284. ibool srv_print_buf_io = FALSE;
  285. ibool srv_print_log_io = FALSE;
  286. ibool srv_print_latch_waits = FALSE;
  287. #endif /* UNIV_DEBUG */
  288. ulint srv_n_rows_inserted = 0;
  289. ulint srv_n_rows_updated = 0;
  290. ulint srv_n_rows_deleted = 0;
  291. ulint srv_n_rows_read = 0;
  292. #ifndef UNIV_HOTBACKUP
  293. static ulint srv_n_rows_inserted_old = 0;
  294. static ulint srv_n_rows_updated_old = 0;
  295. static ulint srv_n_rows_deleted_old = 0;
  296. static ulint srv_n_rows_read_old = 0;
  297. #endif /* !UNIV_HOTBACKUP */
  298. ulint srv_n_lock_wait_count = 0;
  299. ulint srv_n_lock_wait_current_count = 0;
  300. ib_longlong srv_n_lock_wait_time = 0;
  301. ulint srv_n_lock_max_wait_time = 0;
  302. /*
  303. Set the following to 0 if you want InnoDB to write messages on
  304. stderr on startup/shutdown
  305. */
  306. ibool srv_print_verbose_log = TRUE;
  307. ibool srv_print_innodb_monitor = FALSE;
  308. ibool srv_print_innodb_lock_monitor = FALSE;
  309. ibool srv_print_innodb_tablespace_monitor = FALSE;
  310. ibool srv_print_innodb_table_monitor = FALSE;
  311. /* Array of English strings describing the current state of an
  312. i/o handler thread */
  313. const char* srv_io_thread_op_info[SRV_MAX_N_IO_THREADS];
  314. const char* srv_io_thread_function[SRV_MAX_N_IO_THREADS];
  315. time_t srv_last_monitor_time;
  316. mutex_t srv_innodb_monitor_mutex;
  317. /* Mutex for locking srv_monitor_file */
  318. mutex_t srv_monitor_file_mutex;
  319. /* Temporary file for innodb monitor output */
  320. FILE* srv_monitor_file;
  321. /* Mutex for locking srv_dict_tmpfile.
  322. This mutex has a very high rank; threads reserving it should not
  323. be holding any InnoDB latches. */
  324. mutex_t srv_dict_tmpfile_mutex;
  325. /* Temporary file for output from the data dictionary */
  326. FILE* srv_dict_tmpfile;
  327. /* Mutex for locking srv_misc_tmpfile.
  328. This mutex has a very low rank; threads reserving it should not
  329. acquire any further latches or sleep before releasing this one. */
  330. mutex_t srv_misc_tmpfile_mutex;
  331. /* Temporary file for miscellanous diagnostic output */
  332. FILE* srv_misc_tmpfile;
  333. ulint srv_main_thread_process_no = 0;
  334. ulint srv_main_thread_id = 0;
  335. /*
  336. IMPLEMENTATION OF THE SERVER MAIN PROGRAM
  337. =========================================
  338. There is the following analogue between this database
  339. server and an operating system kernel:
  340. DB concept equivalent OS concept
  341. ---------- ---------------------
  342. transaction -- process;
  343. query thread -- thread;
  344. lock -- semaphore;
  345. transaction set to
  346. the rollback state -- kill signal delivered to a process;
  347. kernel -- kernel;
  348. query thread execution:
  349. (a) without kernel mutex
  350. reserved -- process executing in user mode;
  351. (b) with kernel mutex reserved
  352. -- process executing in kernel mode;
  353. The server is controlled by a master thread which runs at
  354. a priority higher than normal, that is, higher than user threads.
  355. It sleeps most of the time, and wakes up, say, every 300 milliseconds,
  356. to check whether there is anything happening in the server which
  357. requires intervention of the master thread. Such situations may be,
  358. for example, when flushing of dirty blocks is needed in the buffer
  359. pool or old version of database rows have to be cleaned away.
  360. The threads which we call user threads serve the queries of
  361. the clients and input from the console of the server.
  362. They run at normal priority. The server may have several
  363. communications endpoints. A dedicated set of user threads waits
  364. at each of these endpoints ready to receive a client request.
  365. Each request is taken by a single user thread, which then starts
  366. processing and, when the result is ready, sends it to the client
  367. and returns to wait at the same endpoint the thread started from.
  368. So, we do not have dedicated communication threads listening at
  369. the endpoints and dealing the jobs to dedicated worker threads.
  370. Our architecture saves one thread swithch per request, compared
  371. to the solution with dedicated communication threads
  372. which amounts to 15 microseconds on 100 MHz Pentium
  373. running NT. If the client
  374. is communicating over a network, this saving is negligible, but
  375. if the client resides in the same machine, maybe in an SMP machine
  376. on a different processor from the server thread, the saving
  377. can be important as the threads can communicate over shared
  378. memory with an overhead of a few microseconds.
  379. We may later implement a dedicated communication thread solution
  380. for those endpoints which communicate over a network.
  381. Our solution with user threads has two problems: for each endpoint
  382. there has to be a number of listening threads. If there are many
  383. communication endpoints, it may be difficult to set the right number
  384. of concurrent threads in the system, as many of the threads
  385. may always be waiting at less busy endpoints. Another problem
  386. is queuing of the messages, as the server internally does not
  387. offer any queue for jobs.
  388. Another group of user threads is intended for splitting the
  389. queries and processing them in parallel. Let us call these
  390. parallel communication threads. These threads are waiting for
  391. parallelized tasks, suspended on event semaphores.
  392. A single user thread waits for input from the console,
  393. like a command to shut the database.
  394. Utility threads are a different group of threads which takes
  395. care of the buffer pool flushing and other, mainly background
  396. operations, in the server.
  397. Some of these utility threads always run at a lower than normal
  398. priority, so that they are always in background. Some of them
  399. may dynamically boost their priority by the pri_adjust function,
  400. even to higher than normal priority, if their task becomes urgent.
  401. The running of utilities is controlled by high- and low-water marks
  402. of urgency. The urgency may be measured by the number of dirty blocks
  403. in the buffer pool, in the case of the flush thread, for example.
  404. When the high-water mark is exceeded, an utility starts running, until
  405. the urgency drops under the low-water mark. Then the utility thread
  406. suspend itself to wait for an event. The master thread is
  407. responsible of signaling this event when the utility thread is
  408. again needed.
  409. For each individual type of utility, some threads always remain
  410. at lower than normal priority. This is because pri_adjust is implemented
  411. so that the threads at normal or higher priority control their
  412. share of running time by calling sleep. Thus, if the load of the
  413. system sudenly drops, these threads cannot necessarily utilize
  414. the system fully. The background priority threads make up for this,
  415. starting to run when the load drops.
  416. When there is no activity in the system, also the master thread
  417. suspends itself to wait for an event making
  418. the server totally silent. The responsibility to signal this
  419. event is on the user thread which again receives a message
  420. from a client.
  421. There is still one complication in our server design. If a
  422. background utility thread obtains a resource (e.g., mutex) needed by a user
  423. thread, and there is also some other user activity in the system,
  424. the user thread may have to wait indefinitely long for the
  425. resource, as the OS does not schedule a background thread if
  426. there is some other runnable user thread. This problem is called
  427. priority inversion in real-time programming.
  428. One solution to the priority inversion problem would be to
  429. keep record of which thread owns which resource and
  430. in the above case boost the priority of the background thread
  431. so that it will be scheduled and it can release the resource.
  432. This solution is called priority inheritance in real-time programming.
  433. A drawback of this solution is that the overhead of acquiring a mutex
  434. increases slightly, maybe 0.2 microseconds on a 100 MHz Pentium, because
  435. the thread has to call os_thread_get_curr_id.
  436. This may be compared to 0.5 microsecond overhead for a mutex lock-unlock
  437. pair. Note that the thread
  438. cannot store the information in the resource, say mutex, itself,
  439. because competing threads could wipe out the information if it is
  440. stored before acquiring the mutex, and if it stored afterwards,
  441. the information is outdated for the time of one machine instruction,
  442. at least. (To be precise, the information could be stored to
  443. lock_word in mutex if the machine supports atomic swap.)
  444. The above solution with priority inheritance may become actual in the
  445. future, but at the moment we plan to implement a more coarse solution,
  446. which could be called a global priority inheritance. If a thread
  447. has to wait for a long time, say 300 milliseconds, for a resource,
  448. we just guess that it may be waiting for a resource owned by a background
  449. thread, and boost the the priority of all runnable background threads
  450. to the normal level. The background threads then themselves adjust
  451. their fixed priority back to background after releasing all resources
  452. they had (or, at some fixed points in their program code).
  453. What is the performance of the global priority inheritance solution?
  454. We may weigh the length of the wait time 300 milliseconds, during
  455. which the system processes some other thread
  456. to the cost of boosting the priority of each runnable background
  457. thread, rescheduling it, and lowering the priority again.
  458. On 100 MHz Pentium + NT this overhead may be of the order 100
  459. microseconds per thread. So, if the number of runnable background
  460. threads is not very big, say < 100, the cost is tolerable.
  461. Utility threads probably will access resources used by
  462. user threads not very often, so collisions of user threads
  463. to preempted utility threads should not happen very often.
  464. The thread table contains
  465. information of the current status of each thread existing in the system,
  466. and also the event semaphores used in suspending the master thread
  467. and utility and parallel communication threads when they have nothing to do.
  468. The thread table can be seen as an analogue to the process table
  469. in a traditional Unix implementation.
  470. The thread table is also used in the global priority inheritance
  471. scheme. This brings in one additional complication: threads accessing
  472. the thread table must have at least normal fixed priority,
  473. because the priority inheritance solution does not work if a background
  474. thread is preempted while possessing the mutex protecting the thread table.
  475. So, if a thread accesses the thread table, its priority has to be
  476. boosted at least to normal. This priority requirement can be seen similar to
  477. the privileged mode used when processing the kernel calls in traditional
  478. Unix.*/
  479. /* Thread slot in the thread table */
  480. struct srv_slot_struct{
  481. os_thread_id_t id; /* thread id */
  482. os_thread_t handle; /* thread handle */
  483. unsigned type:3; /* thread type: user, utility etc. */
  484. unsigned in_use:1; /* TRUE if this slot is in use */
  485. unsigned suspended:1; /* TRUE if the thread is waiting
  486. for the event of this slot */
  487. ib_time_t suspend_time; /* time when the thread was
  488. suspended */
  489. os_event_t event; /* event used in suspending the
  490. thread when it has nothing to do */
  491. que_thr_t* thr; /* suspended query thread (only
  492. used for MySQL threads) */
  493. };
  494. /* Table for MySQL threads where they will be suspended to wait for locks */
  495. srv_slot_t* srv_mysql_table = NULL;
  496. os_event_t srv_lock_timeout_thread_event;
  497. srv_sys_t* srv_sys = NULL;
  498. byte srv_pad1[64]; /* padding to prevent other memory update
  499. hotspots from residing on the same memory
  500. cache line */
  501. mutex_t* kernel_mutex_temp;/* mutex protecting the server, trx structs,
  502. query threads, and lock table */
  503. byte srv_pad2[64]; /* padding to prevent other memory update
  504. hotspots from residing on the same memory
  505. cache line */
  506. #if 0
  507. /* The following three values measure the urgency of the jobs of
  508. buffer, version, and insert threads. They may vary from 0 - 1000.
  509. The server mutex protects all these variables. The low-water values
  510. tell that the server can acquiesce the utility when the value
  511. drops below this low-water mark. */
  512. ulint srv_meter[SRV_MASTER + 1];
  513. ulint srv_meter_low_water[SRV_MASTER + 1];
  514. ulint srv_meter_high_water[SRV_MASTER + 1];
  515. ulint srv_meter_high_water2[SRV_MASTER + 1];
  516. ulint srv_meter_foreground[SRV_MASTER + 1];
  517. #endif
  518. /* The following values give info about the activity going on in
  519. the database. They are protected by the server mutex. The arrays
  520. are indexed by the type of the thread. */
  521. ulint srv_n_threads_active[SRV_MASTER + 1];
  522. ulint srv_n_threads[SRV_MASTER + 1];
  523. /*************************************************************************
  524. Sets the info describing an i/o thread current state. */
  525. void
  526. srv_set_io_thread_op_info(
  527. /*======================*/
  528. ulint i, /* in: the 'segment' of the i/o thread */
  529. const char* str) /* in: constant char string describing the
  530. state */
  531. {
  532. ut_a(i < SRV_MAX_N_IO_THREADS);
  533. srv_io_thread_op_info[i] = str;
  534. }
  535. /*************************************************************************
  536. Accessor function to get pointer to n'th slot in the server thread
  537. table. */
  538. static
  539. srv_slot_t*
  540. srv_table_get_nth_slot(
  541. /*===================*/
  542. /* out: pointer to the slot */
  543. ulint index) /* in: index of the slot */
  544. {
  545. ut_a(index < OS_THREAD_MAX_N);
  546. return(srv_sys->threads + index);
  547. }
  548. #ifndef UNIV_HOTBACKUP
  549. /*************************************************************************
  550. Gets the number of threads in the system. */
  551. ulint
  552. srv_get_n_threads(void)
  553. /*===================*/
  554. {
  555. ulint i;
  556. ulint n_threads = 0;
  557. mutex_enter(&kernel_mutex);
  558. for (i = SRV_COM; i < SRV_MASTER + 1; i++) {
  559. n_threads += srv_n_threads[i];
  560. }
  561. mutex_exit(&kernel_mutex);
  562. return(n_threads);
  563. }
  564. /*************************************************************************
  565. Reserves a slot in the thread table for the current thread. Also creates the
  566. thread local storage struct for the current thread. NOTE! The server mutex
  567. has to be reserved by the caller! */
  568. static
  569. ulint
  570. srv_table_reserve_slot(
  571. /*===================*/
  572. /* out: reserved slot index */
  573. enum srv_thread_type type) /* in: type of the thread */
  574. {
  575. srv_slot_t* slot;
  576. ulint i;
  577. ut_a(type > 0);
  578. ut_a(type <= SRV_MASTER);
  579. i = 0;
  580. slot = srv_table_get_nth_slot(i);
  581. while (slot->in_use) {
  582. i++;
  583. slot = srv_table_get_nth_slot(i);
  584. }
  585. ut_a(slot->in_use == FALSE);
  586. slot->in_use = TRUE;
  587. slot->suspended = FALSE;
  588. slot->type = type;
  589. slot->id = os_thread_get_curr_id();
  590. slot->handle = os_thread_get_curr();
  591. thr_local_create();
  592. thr_local_set_slot_no(os_thread_get_curr_id(), i);
  593. return(i);
  594. }
  595. /*************************************************************************
  596. Suspends the calling thread to wait for the event in its thread slot.
  597. NOTE! The server mutex has to be reserved by the caller! */
  598. static
  599. os_event_t
  600. srv_suspend_thread(void)
  601. /*====================*/
  602. /* out: event for the calling thread to wait */
  603. {
  604. srv_slot_t* slot;
  605. os_event_t event;
  606. ulint slot_no;
  607. enum srv_thread_type type;
  608. ut_ad(mutex_own(&kernel_mutex));
  609. slot_no = thr_local_get_slot_no(os_thread_get_curr_id());
  610. if (srv_print_thread_releases) {
  611. fprintf(stderr,
  612. "Suspending thread %lu to slot %lu\n",
  613. (ulong) os_thread_get_curr_id(), (ulong) slot_no);
  614. }
  615. slot = srv_table_get_nth_slot(slot_no);
  616. type = slot->type;
  617. ut_ad(type >= SRV_WORKER);
  618. ut_ad(type <= SRV_MASTER);
  619. event = slot->event;
  620. slot->suspended = TRUE;
  621. ut_ad(srv_n_threads_active[type] > 0);
  622. srv_n_threads_active[type]--;
  623. os_event_reset(event);
  624. return(event);
  625. }
  626. #endif /* !UNIV_HOTBACKUP */
  627. /*************************************************************************
  628. Releases threads of the type given from suspension in the thread table.
  629. NOTE! The server mutex has to be reserved by the caller! */
  630. ulint
  631. srv_release_threads(
  632. /*================*/
  633. /* out: number of threads
  634. released: this may be < n if
  635. not enough threads were
  636. suspended at the moment */
  637. enum srv_thread_type type, /* in: thread type */
  638. ulint n) /* in: number of threads to release */
  639. {
  640. srv_slot_t* slot;
  641. ulint i;
  642. ulint count = 0;
  643. ut_ad(type >= SRV_WORKER);
  644. ut_ad(type <= SRV_MASTER);
  645. ut_ad(n > 0);
  646. ut_ad(mutex_own(&kernel_mutex));
  647. for (i = 0; i < OS_THREAD_MAX_N; i++) {
  648. slot = srv_table_get_nth_slot(i);
  649. if (slot->in_use && slot->type == type && slot->suspended) {
  650. slot->suspended = FALSE;
  651. srv_n_threads_active[type]++;
  652. os_event_set(slot->event);
  653. if (srv_print_thread_releases) {
  654. fprintf(stderr,
  655. "Releasing thread %lu type %lu"
  656. " from slot %lu\n",
  657. (ulong) slot->id, (ulong) type,
  658. (ulong) i);
  659. }
  660. count++;
  661. if (count == n) {
  662. break;
  663. }
  664. }
  665. }
  666. return(count);
  667. }
  668. /*************************************************************************
  669. Returns the calling thread type. */
  670. enum srv_thread_type
  671. srv_get_thread_type(void)
  672. /*=====================*/
  673. /* out: SRV_COM, ... */
  674. {
  675. ulint slot_no;
  676. srv_slot_t* slot;
  677. enum srv_thread_type type;
  678. mutex_enter(&kernel_mutex);
  679. slot_no = thr_local_get_slot_no(os_thread_get_curr_id());
  680. slot = srv_table_get_nth_slot(slot_no);
  681. type = slot->type;
  682. ut_ad(type >= SRV_WORKER);
  683. ut_ad(type <= SRV_MASTER);
  684. mutex_exit(&kernel_mutex);
  685. return(type);
  686. }
  687. /*************************************************************************
  688. Initializes the server. */
  689. void
  690. srv_init(void)
  691. /*==========*/
  692. {
  693. srv_conc_slot_t* conc_slot;
  694. srv_slot_t* slot;
  695. dict_table_t* table;
  696. ulint i;
  697. srv_sys = mem_alloc(sizeof(srv_sys_t));
  698. kernel_mutex_temp = mem_alloc(sizeof(mutex_t));
  699. mutex_create(&kernel_mutex, SYNC_KERNEL);
  700. mutex_create(&srv_innodb_monitor_mutex, SYNC_NO_ORDER_CHECK);
  701. srv_sys->threads = mem_alloc(OS_THREAD_MAX_N * sizeof(srv_slot_t));
  702. for (i = 0; i < OS_THREAD_MAX_N; i++) {
  703. slot = srv_table_get_nth_slot(i);
  704. slot->in_use = FALSE;
  705. slot->type=0; /* Avoid purify errors */
  706. slot->event = os_event_create(NULL);
  707. ut_a(slot->event);
  708. }
  709. srv_mysql_table = mem_alloc(OS_THREAD_MAX_N * sizeof(srv_slot_t));
  710. for (i = 0; i < OS_THREAD_MAX_N; i++) {
  711. slot = srv_mysql_table + i;
  712. slot->in_use = FALSE;
  713. slot->type = 0;
  714. slot->event = os_event_create(NULL);
  715. ut_a(slot->event);
  716. }
  717. srv_lock_timeout_thread_event = os_event_create(NULL);
  718. for (i = 0; i < SRV_MASTER + 1; i++) {
  719. srv_n_threads_active[i] = 0;
  720. srv_n_threads[i] = 0;
  721. #if 0
  722. srv_meter[i] = 30;
  723. srv_meter_low_water[i] = 50;
  724. srv_meter_high_water[i] = 100;
  725. srv_meter_high_water2[i] = 200;
  726. srv_meter_foreground[i] = 250;
  727. #endif
  728. }
  729. UT_LIST_INIT(srv_sys->tasks);
  730. /* create dummy table and index for old-style infimum and supremum */
  731. table = dict_mem_table_create("SYS_DUMMY1",
  732. DICT_HDR_SPACE, 1, 0);
  733. dict_mem_table_add_col(table, NULL, NULL, DATA_CHAR,
  734. DATA_ENGLISH | DATA_NOT_NULL, 8);
  735. srv_sys->dummy_ind1 = dict_mem_index_create(
  736. "SYS_DUMMY1", "SYS_DUMMY1", DICT_HDR_SPACE, 0, 1);
  737. dict_index_add_col(srv_sys->dummy_ind1, table,
  738. dict_table_get_nth_col(table, 0), 0);
  739. srv_sys->dummy_ind1->table = table;
  740. /* create dummy table and index for new-style infimum and supremum */
  741. table = dict_mem_table_create("SYS_DUMMY2",
  742. DICT_HDR_SPACE, 1, DICT_TF_COMPACT);
  743. dict_mem_table_add_col(table, NULL, NULL, DATA_CHAR,
  744. DATA_ENGLISH | DATA_NOT_NULL, 8);
  745. srv_sys->dummy_ind2 = dict_mem_index_create(
  746. "SYS_DUMMY2", "SYS_DUMMY2", DICT_HDR_SPACE, 0, 1);
  747. dict_index_add_col(srv_sys->dummy_ind2, table,
  748. dict_table_get_nth_col(table, 0), 0);
  749. srv_sys->dummy_ind2->table = table;
  750. /* avoid ut_ad(index->cached) in dict_index_get_n_unique_in_tree */
  751. srv_sys->dummy_ind1->cached = srv_sys->dummy_ind2->cached = TRUE;
  752. /* Init the server concurrency restriction data structures */
  753. os_fast_mutex_init(&srv_conc_mutex);
  754. UT_LIST_INIT(srv_conc_queue);
  755. srv_conc_slots = mem_alloc(OS_THREAD_MAX_N * sizeof(srv_conc_slot_t));
  756. for (i = 0; i < OS_THREAD_MAX_N; i++) {
  757. conc_slot = srv_conc_slots + i;
  758. conc_slot->reserved = FALSE;
  759. conc_slot->event = os_event_create(NULL);
  760. ut_a(conc_slot->event);
  761. }
  762. /* Initialize some INFORMATION SCHEMA internal structures */
  763. trx_i_s_cache_init(trx_i_s_cache);
  764. }
  765. /*************************************************************************
  766. Frees the OS fast mutex created in srv_init(). */
  767. void
  768. srv_free(void)
  769. /*==========*/
  770. {
  771. os_fast_mutex_free(&srv_conc_mutex);
  772. }
  773. /*************************************************************************
  774. Initializes the synchronization primitives, memory system, and the thread
  775. local storage. */
  776. void
  777. srv_general_init(void)
  778. /*==================*/
  779. {
  780. os_sync_init();
  781. sync_init();
  782. mem_init(srv_mem_pool_size);
  783. thr_local_init();
  784. }
  785. /*======================= InnoDB Server FIFO queue =======================*/
  786. /* Maximum allowable purge history length. <=0 means 'infinite'. */
  787. ulong srv_max_purge_lag = 0;
  788. /*************************************************************************
  789. Puts an OS thread to wait if there are too many concurrent threads
  790. (>= srv_thread_concurrency) inside InnoDB. The threads wait in a FIFO queue. */
  791. void
  792. srv_conc_enter_innodb(
  793. /*==================*/
  794. trx_t* trx) /* in: transaction object associated with the
  795. thread */
  796. {
  797. ibool has_slept = FALSE;
  798. srv_conc_slot_t* slot = NULL;
  799. ulint i;
  800. if (trx->mysql_thd != NULL
  801. && thd_is_replication_slave_thread(trx->mysql_thd)) {
  802. UT_WAIT_FOR(srv_conc_n_threads
  803. < (lint)srv_thread_concurrency,
  804. srv_replication_delay * 1000);
  805. return;
  806. }
  807. /* If trx has 'free tickets' to enter the engine left, then use one
  808. such ticket */
  809. if (trx->n_tickets_to_enter_innodb > 0) {
  810. trx->n_tickets_to_enter_innodb--;
  811. return;
  812. }
  813. os_fast_mutex_lock(&srv_conc_mutex);
  814. retry:
  815. if (trx->declared_to_be_inside_innodb) {
  816. ut_print_timestamp(stderr);
  817. fputs(" InnoDB: Error: trying to declare trx"
  818. " to enter InnoDB, but\n"
  819. "InnoDB: it already is declared.\n", stderr);
  820. trx_print(stderr, trx, 0);
  821. putc('\n', stderr);
  822. os_fast_mutex_unlock(&srv_conc_mutex);
  823. return;
  824. }
  825. if (srv_conc_n_threads < (lint)srv_thread_concurrency) {
  826. srv_conc_n_threads++;
  827. trx->declared_to_be_inside_innodb = TRUE;
  828. trx->n_tickets_to_enter_innodb = SRV_FREE_TICKETS_TO_ENTER;
  829. os_fast_mutex_unlock(&srv_conc_mutex);
  830. return;
  831. }
  832. /* If the transaction is not holding resources, let it sleep
  833. for SRV_THREAD_SLEEP_DELAY microseconds, and try again then */
  834. if (!has_slept && !trx->has_search_latch
  835. && NULL == UT_LIST_GET_FIRST(trx->trx_locks)) {
  836. has_slept = TRUE; /* We let it sleep only once to avoid
  837. starvation */
  838. srv_conc_n_waiting_threads++;
  839. os_fast_mutex_unlock(&srv_conc_mutex);
  840. trx->op_info = "sleeping before joining InnoDB queue";
  841. /* Peter Zaitsev suggested that we take the sleep away
  842. altogether. But the sleep may be good in pathological
  843. situations of lots of thread switches. Simply put some
  844. threads aside for a while to reduce the number of thread
  845. switches. */
  846. if (SRV_THREAD_SLEEP_DELAY > 0) {
  847. os_thread_sleep(SRV_THREAD_SLEEP_DELAY);
  848. }
  849. trx->op_info = "";
  850. os_fast_mutex_lock(&srv_conc_mutex);
  851. srv_conc_n_waiting_threads--;
  852. goto retry;
  853. }
  854. /* Too many threads inside: put the current thread to a queue */
  855. for (i = 0; i < OS_THREAD_MAX_N; i++) {
  856. slot = srv_conc_slots + i;
  857. if (!slot->reserved) {
  858. break;
  859. }
  860. }
  861. if (i == OS_THREAD_MAX_N) {
  862. /* Could not find a free wait slot, we must let the
  863. thread enter */
  864. srv_conc_n_threads++;
  865. trx->declared_to_be_inside_innodb = TRUE;
  866. trx->n_tickets_to_enter_innodb = 0;
  867. os_fast_mutex_unlock(&srv_conc_mutex);
  868. return;
  869. }
  870. /* Release possible search system latch this thread has */
  871. if (trx->has_search_latch) {
  872. trx_search_latch_release_if_reserved(trx);
  873. }
  874. /* Add to the queue */
  875. slot->reserved = TRUE;
  876. slot->wait_ended = FALSE;
  877. UT_LIST_ADD_LAST(srv_conc_queue, srv_conc_queue, slot);
  878. os_event_reset(slot->event);
  879. srv_conc_n_waiting_threads++;
  880. os_fast_mutex_unlock(&srv_conc_mutex);
  881. /* Go to wait for the event; when a thread leaves InnoDB it will
  882. release this thread */
  883. trx->op_info = "waiting in InnoDB queue";
  884. os_event_wait(slot->event);
  885. trx->op_info = "";
  886. os_fast_mutex_lock(&srv_conc_mutex);
  887. srv_conc_n_waiting_threads--;
  888. /* NOTE that the thread which released this thread already
  889. incremented the thread counter on behalf of this thread */
  890. slot->reserved = FALSE;
  891. UT_LIST_REMOVE(srv_conc_queue, srv_conc_queue, slot);
  892. trx->declared_to_be_inside_innodb = TRUE;
  893. trx->n_tickets_to_enter_innodb = SRV_FREE_TICKETS_TO_ENTER;
  894. os_fast_mutex_unlock(&srv_conc_mutex);
  895. }
  896. /*************************************************************************
  897. This lets a thread enter InnoDB regardless of the number of threads inside
  898. InnoDB. This must be called when a thread ends a lock wait. */
  899. void
  900. srv_conc_force_enter_innodb(
  901. /*========================*/
  902. trx_t* trx) /* in: transaction object associated with the
  903. thread */
  904. {
  905. if (UNIV_LIKELY(!srv_thread_concurrency)) {
  906. return;
  907. }
  908. os_fast_mutex_lock(&srv_conc_mutex);
  909. srv_conc_n_threads++;
  910. trx->declared_to_be_inside_innodb = TRUE;
  911. trx->n_tickets_to_enter_innodb = 1;
  912. os_fast_mutex_unlock(&srv_conc_mutex);
  913. }
  914. /*************************************************************************
  915. This must be called when a thread exits InnoDB in a lock wait or at the
  916. end of an SQL statement. */
  917. void
  918. srv_conc_force_exit_innodb(
  919. /*=======================*/
  920. trx_t* trx) /* in: transaction object associated with the
  921. thread */
  922. {
  923. srv_conc_slot_t* slot = NULL;
  924. if (UNIV_LIKELY(!srv_thread_concurrency)) {
  925. return;
  926. }
  927. if (trx->mysql_thd != NULL
  928. && thd_is_replication_slave_thread(trx->mysql_thd)) {
  929. return;
  930. }
  931. if (trx->declared_to_be_inside_innodb == FALSE) {
  932. return;
  933. }
  934. os_fast_mutex_lock(&srv_conc_mutex);
  935. srv_conc_n_threads--;
  936. trx->declared_to_be_inside_innodb = FALSE;
  937. trx->n_tickets_to_enter_innodb = 0;
  938. if (srv_conc_n_threads < (lint)srv_thread_concurrency) {
  939. /* Look for a slot where a thread is waiting and no other
  940. thread has yet released the thread */
  941. slot = UT_LIST_GET_FIRST(srv_conc_queue);
  942. while (slot && slot->wait_ended == TRUE) {
  943. slot = UT_LIST_GET_NEXT(srv_conc_queue, slot);
  944. }
  945. if (slot != NULL) {
  946. slot->wait_ended = TRUE;
  947. /* We increment the count on behalf of the released
  948. thread */
  949. srv_conc_n_threads++;
  950. }
  951. }
  952. os_fast_mutex_unlock(&srv_conc_mutex);
  953. if (slot != NULL) {
  954. os_event_set(slot->event);
  955. }
  956. }
  957. /*************************************************************************
  958. This must be called when a thread exits InnoDB. */
  959. void
  960. srv_conc_exit_innodb(
  961. /*=================*/
  962. trx_t* trx) /* in: transaction object associated with the
  963. thread */
  964. {
  965. if (trx->n_tickets_to_enter_innodb > 0) {
  966. /* We will pretend the thread is still inside InnoDB though it
  967. now leaves the InnoDB engine. In this way we save
  968. a lot of semaphore operations. srv_conc_force_exit_innodb is
  969. used to declare the thread definitely outside InnoDB. It
  970. should be called when there is a lock wait or an SQL statement
  971. ends. */
  972. return;
  973. }
  974. srv_conc_force_exit_innodb(trx);
  975. }
  976. /*========================================================================*/
  977. /*************************************************************************
  978. Normalizes init parameter values to use units we use inside InnoDB. */
  979. static
  980. ulint
  981. srv_normalize_init_values(void)
  982. /*===========================*/
  983. /* out: DB_SUCCESS or error code */
  984. {
  985. ulint n;
  986. ulint i;
  987. n = srv_n_data_files;
  988. for (i = 0; i < n; i++) {
  989. srv_data_file_sizes[i] = srv_data_file_sizes[i]
  990. * ((1024 * 1024) / UNIV_PAGE_SIZE);
  991. }
  992. srv_last_file_size_max = srv_last_file_size_max
  993. * ((1024 * 1024) / UNIV_PAGE_SIZE);
  994. srv_log_file_size = srv_log_file_size / UNIV_PAGE_SIZE;
  995. srv_log_buffer_size = srv_log_buffer_size / UNIV_PAGE_SIZE;
  996. srv_lock_table_size = 5 * (srv_buf_pool_size / UNIV_PAGE_SIZE);
  997. return(DB_SUCCESS);
  998. }
  999. /*************************************************************************
  1000. Boots the InnoDB server. */
  1001. ulint
  1002. srv_boot(void)
  1003. /*==========*/
  1004. /* out: DB_SUCCESS or error code */
  1005. {
  1006. ulint err;
  1007. /* Transform the init parameter values given by MySQL to
  1008. use units we use inside InnoDB: */
  1009. err = srv_normalize_init_values();
  1010. if (err != DB_SUCCESS) {
  1011. return(err);
  1012. }
  1013. /* Initialize synchronization primitives, memory management, and thread
  1014. local storage */
  1015. srv_general_init();
  1016. /* Initialize this module */
  1017. srv_init();
  1018. return(DB_SUCCESS);
  1019. }
  1020. #ifndef UNIV_HOTBACKUP
  1021. /*************************************************************************
  1022. Reserves a slot in the thread table for the current MySQL OS thread.
  1023. NOTE! The kernel mutex has to be reserved by the caller! */
  1024. static
  1025. srv_slot_t*
  1026. srv_table_reserve_slot_for_mysql(void)
  1027. /*==================================*/
  1028. /* out: reserved slot */
  1029. {
  1030. srv_slot_t* slot;
  1031. ulint i;
  1032. ut_ad(mutex_own(&kernel_mutex));
  1033. i = 0;
  1034. slot = srv_mysql_table + i;
  1035. while (slot->in_use) {
  1036. i++;
  1037. if (i >= OS_THREAD_MAX_N) {
  1038. ut_print_timestamp(stderr);
  1039. fprintf(stderr,
  1040. " InnoDB: There appear to be %lu MySQL"
  1041. " threads currently waiting\n"
  1042. "InnoDB: inside InnoDB, which is the"
  1043. " upper limit. Cannot continue operation.\n"
  1044. "InnoDB: We intentionally generate"
  1045. " a seg fault to print a stack trace\n"
  1046. "InnoDB: on Linux. But first we print"
  1047. " a list of waiting threads.\n", (ulong) i);
  1048. for (i = 0; i < OS_THREAD_MAX_N; i++) {
  1049. slot = srv_mysql_table + i;
  1050. fprintf(stderr,
  1051. "Slot %lu: thread id %lu, type %lu,"
  1052. " in use %lu, susp %lu, time %lu\n",
  1053. (ulong) i,
  1054. (ulong) os_thread_pf(slot->id),
  1055. (ulong) slot->type,
  1056. (ulong) slot->in_use,
  1057. (ulong) slot->suspended,
  1058. (ulong) difftime(ut_time(),
  1059. slot->suspend_time));
  1060. }
  1061. ut_error;
  1062. }
  1063. slot = srv_mysql_table + i;
  1064. }
  1065. ut_a(slot->in_use == FALSE);
  1066. slot->in_use = TRUE;
  1067. slot->id = os_thread_get_curr_id();
  1068. slot->handle = os_thread_get_curr();
  1069. return(slot);
  1070. }
  1071. #endif /* !UNIV_HOTBACKUP */
  1072. /*******************************************************************
  1073. Puts a MySQL OS thread to wait for a lock to be released. If an error
  1074. occurs during the wait trx->error_state associated with thr is
  1075. != DB_SUCCESS when we return. DB_LOCK_WAIT_TIMEOUT and DB_DEADLOCK
  1076. are possible errors. DB_DEADLOCK is returned if selective deadlock
  1077. resolution chose this transaction as a victim. */
  1078. void
  1079. srv_suspend_mysql_thread(
  1080. /*=====================*/
  1081. que_thr_t* thr) /* in: query thread associated with the MySQL
  1082. OS thread */
  1083. {
  1084. #ifndef UNIV_HOTBACKUP
  1085. srv_slot_t* slot;
  1086. os_event_t event;
  1087. double wait_time;
  1088. trx_t* trx;
  1089. ulint had_dict_lock;
  1090. ibool was_declared_inside_innodb = FALSE;
  1091. ib_longlong start_time = 0;
  1092. ib_longlong finish_time;
  1093. ulint diff_time;
  1094. ulint sec;
  1095. ulint ms;
  1096. ut_ad(!mutex_own(&kernel_mutex));
  1097. trx = thr_get_trx(thr);
  1098. os_event_set(srv_lock_timeout_thread_event);
  1099. mutex_enter(&kernel_mutex);
  1100. trx->error_state = DB_SUCCESS;
  1101. if (thr->state == QUE_THR_RUNNING) {
  1102. ut_ad(thr->is_active == TRUE);
  1103. /* The lock has already been released or this transaction
  1104. was chosen as a deadlock victim: no need to suspend */
  1105. if (trx->was_chosen_as_deadlock_victim) {
  1106. trx->error_state = DB_DEADLOCK;
  1107. trx->was_chosen_as_deadlock_victim = FALSE;
  1108. }
  1109. mutex_exit(&kernel_mutex);
  1110. return;
  1111. }
  1112. ut_ad(thr->is_active == FALSE);
  1113. slot = srv_table_reserve_slot_for_mysql();
  1114. event = slot->event;
  1115. slot->thr = thr;
  1116. os_event_reset(event);
  1117. slot->suspend_time = ut_time();
  1118. if (thr->lock_state == QUE_THR_LOCK_ROW) {
  1119. srv_n_lock_wait_count++;
  1120. srv_n_lock_wait_current_count++;
  1121. ut_usectime(&sec, &ms);
  1122. start_time = (ib_longlong)sec * 1000000 + ms;
  1123. }
  1124. /* Wake the lock timeout monitor thread, if it is suspended */
  1125. os_event_set(srv_lock_timeout_thread_event);
  1126. mutex_exit(&kernel_mutex);
  1127. if (trx->declared_to_be_inside_innodb) {
  1128. was_declared_inside_innodb = TRUE;
  1129. /* We must declare this OS thread to exit InnoDB, since a
  1130. possible other thread holding a lock which this thread waits
  1131. for must be allowed to enter, sooner or later */
  1132. srv_conc_force_exit_innodb(trx);
  1133. }
  1134. had_dict_lock = trx->dict_operation_lock_mode;
  1135. switch (had_dict_lock) {
  1136. case RW_S_LATCH:
  1137. /* Release foreign key check latch */
  1138. row_mysql_unfreeze_data_dictionary(trx);
  1139. break;
  1140. case RW_X_LATCH:
  1141. /* Release fast index creation latch */
  1142. row_mysql_unlock_data_dictionary(trx);
  1143. break;
  1144. }
  1145. ut_a(trx->dict_operation_lock_mode == 0);
  1146. /* Wait for the release */
  1147. os_event_wait(event);
  1148. switch (had_dict_lock) {
  1149. case RW_S_LATCH:
  1150. row_mysql_freeze_data_dictionary(trx);
  1151. break;
  1152. case RW_X_LATCH:
  1153. row_mysql_lock_data_dictionary(trx);
  1154. break;
  1155. }
  1156. if (was_declared_inside_innodb) {
  1157. /* Return back inside InnoDB */
  1158. srv_conc_force_enter_innodb(trx);
  1159. }
  1160. mutex_enter(&kernel_mutex);
  1161. /* Release the slot for others to use */
  1162. slot->in_use = FALSE;
  1163. wait_time = ut_difftime(ut_time(), slot->suspend_time);
  1164. if (thr->lock_state == QUE_THR_LOCK_ROW) {
  1165. ut_usectime(&sec, &ms);
  1166. finish_time = (ib_longlong)sec * 1000000 + ms;
  1167. diff_time = (ulint) (finish_time - start_time);
  1168. srv_n_lock_wait_current_count--;
  1169. srv_n_lock_wait_time = srv_n_lock_wait_time + diff_time;
  1170. if (diff_time > srv_n_lock_max_wait_time) {
  1171. srv_n_lock_max_wait_time = diff_time;
  1172. }
  1173. }
  1174. if (trx->was_chosen_as_deadlock_victim) {
  1175. trx->error_state = DB_DEADLOCK;
  1176. trx->was_chosen_as_deadlock_victim = FALSE;
  1177. }
  1178. mutex_exit(&kernel_mutex);
  1179. if (srv_lock_wait_timeout < 100000000
  1180. && wait_time > (double)srv_lock_wait_timeout) {
  1181. trx->error_state = DB_LOCK_WAIT_TIMEOUT;
  1182. }
  1183. #else /* UNIV_HOTBACKUP */
  1184. /* This function depends on MySQL code that is not included in
  1185. InnoDB Hot Backup builds. Besides, this function should never
  1186. be called in InnoDB Hot Backup. */
  1187. ut_error;
  1188. #endif /* UNIV_HOTBACKUP */
  1189. }
  1190. /************************************************************************
  1191. Releases a MySQL OS thread waiting for a lock to be released, if the
  1192. thread is already suspended. */
  1193. void
  1194. srv_release_mysql_thread_if_suspended(
  1195. /*==================================*/
  1196. que_thr_t* thr) /* in: query thread associated with the
  1197. MySQL OS thread */
  1198. {
  1199. #ifndef UNIV_HOTBACKUP
  1200. srv_slot_t* slot;
  1201. ulint i;
  1202. ut_ad(mutex_own(&kernel_mutex));
  1203. for (i = 0; i < OS_THREAD_MAX_N; i++) {
  1204. slot = srv_mysql_table + i;
  1205. if (slot->in_use && slot->thr == thr) {
  1206. /* Found */
  1207. os_event_set(slot->event);
  1208. return;
  1209. }
  1210. }
  1211. /* not found */
  1212. #else /* UNIV_HOTBACKUP */
  1213. /* This function depends on MySQL code that is not included in
  1214. InnoDB Hot Backup builds. Besides, this function should never
  1215. be called in InnoDB Hot Backup. */
  1216. ut_error;
  1217. #endif /* UNIV_HOTBACKUP */
  1218. }
  1219. #ifndef UNIV_HOTBACKUP
  1220. /**********************************************************************
  1221. Refreshes the values used to calculate per-second averages. */
  1222. static
  1223. void
  1224. srv_refresh_innodb_monitor_stats(void)
  1225. /*==================================*/
  1226. {
  1227. mutex_enter(&srv_innodb_monitor_mutex);
  1228. srv_last_monitor_time = time(NULL);
  1229. os_aio_refresh_stats();
  1230. btr_cur_n_sea_old = btr_cur_n_sea;
  1231. btr_cur_n_non_sea_old = btr_cur_n_non_sea;
  1232. log_refresh_stats();
  1233. buf_refresh_io_stats();
  1234. srv_n_rows_inserted_old = srv_n_rows_inserted;
  1235. srv_n_rows_updated_old = srv_n_rows_updated;
  1236. srv_n_rows_deleted_old = srv_n_rows_deleted;
  1237. srv_n_rows_read_old = srv_n_rows_read;
  1238. mutex_exit(&srv_innodb_monitor_mutex);
  1239. }
  1240. /**********************************************************************
  1241. Outputs to a file the output of the InnoDB Monitor. */
  1242. void
  1243. srv_printf_innodb_monitor(
  1244. /*======================*/
  1245. FILE* file, /* in: output stream */
  1246. ulint* trx_start, /* out: file position of the start of
  1247. the list of active transactions */
  1248. ulint* trx_end) /* out: file position of the end of
  1249. the list of active transactions */
  1250. {
  1251. double time_elapsed;
  1252. time_t current_time;
  1253. ulint n_reserved;
  1254. mutex_enter(&srv_innodb_monitor_mutex);
  1255. current_time = time(NULL);
  1256. /* We add 0.001 seconds to time_elapsed to prevent division
  1257. by zero if two users happen to call SHOW INNODB STATUS at the same
  1258. time */
  1259. time_elapsed = difftime(current_time, srv_last_monitor_time)
  1260. + 0.001;
  1261. srv_last_monitor_time = time(NULL);
  1262. fputs("\n=====================================\n", file);
  1263. ut_print_timestamp(file);
  1264. fprintf(file,
  1265. " INNODB MONITOR OUTPUT\n"
  1266. "=====================================\n"
  1267. "Per second averages calculated from the last %lu seconds\n",
  1268. (ulong)time_elapsed);
  1269. fputs("----------\n"
  1270. "SEMAPHORES\n"
  1271. "----------\n", file);
  1272. sync_print(file);
  1273. /* Conceptually, srv_innodb_monitor_mutex has a very high latching
  1274. order level in sync0sync.h, while dict_foreign_err_mutex has a very
  1275. low level 135. Therefore we can reserve the latter mutex here without
  1276. a danger of a deadlock of threads. */
  1277. mutex_enter(&dict_foreign_err_mutex);
  1278. if (ftell(dict_foreign_err_file) != 0L) {
  1279. fputs("------------------------\n"
  1280. "LATEST FOREIGN KEY ERROR\n"
  1281. "------------------------\n", file);
  1282. ut_copy_file(file, dict_foreign_err_file);
  1283. }
  1284. mutex_exit(&dict_foreign_err_mutex);
  1285. lock_print_info_summary(file);
  1286. if (trx_start) {
  1287. long t = ftell(file);
  1288. if (t < 0) {
  1289. *trx_start = ULINT_UNDEFINED;
  1290. } else {
  1291. *trx_start = (ulint) t;
  1292. }
  1293. }
  1294. lock_print_info_all_transactions(file);
  1295. if (trx_end) {
  1296. long t = ftell(file);
  1297. if (t < 0) {
  1298. *trx_end = ULINT_UNDEFINED;
  1299. } else {
  1300. *trx_end = (ulint) t;
  1301. }
  1302. }
  1303. fputs("--------\n"
  1304. "FILE I/O\n"
  1305. "--------\n", file);
  1306. os_aio_print(file);
  1307. fputs("-------------------------------------\n"
  1308. "INSERT BUFFER AND ADAPTIVE HASH INDEX\n"
  1309. "-------------------------------------\n", file);
  1310. ibuf_print(file);
  1311. ha_print_info(file, btr_search_sys->hash_index);
  1312. fprintf(file,
  1313. "%.2f hash searches/s, %.2f non-hash searches/s\n",
  1314. (btr_cur_n_sea - btr_cur_n_sea_old)
  1315. / time_elapsed,
  1316. (btr_cur_n_non_sea - btr_cur_n_non_sea_old)
  1317. / time_elapsed);
  1318. btr_cur_n_sea_old = btr_cur_n_sea;
  1319. btr_cur_n_non_sea_old = btr_cur_n_non_sea;
  1320. fputs("---\n"
  1321. "LOG\n"
  1322. "---\n", file);
  1323. log_print(file);
  1324. fputs("----------------------\n"
  1325. "BUFFER POOL AND MEMORY\n"
  1326. "----------------------\n", file);
  1327. fprintf(file,
  1328. "Total memory allocated " ULINTPF
  1329. "; in additional pool allocated " ULINTPF "\n",
  1330. ut_total_allocated_memory,
  1331. mem_pool_get_reserved(mem_comm_pool));
  1332. fprintf(file, "Dictionary memory allocated " ULINTPF "\n",
  1333. dict_sys->size);
  1334. buf_print_io(file);
  1335. fputs("--------------\n"
  1336. "ROW OPERATIONS\n"
  1337. "--------------\n", file);
  1338. fprintf(file, "%ld queries inside InnoDB, %lu queries in queue\n",
  1339. (long) srv_conc_n_threads,
  1340. (ulong) srv_conc_n_waiting_threads);
  1341. fprintf(file, "%lu read views open inside InnoDB\n",
  1342. UT_LIST_GET_LEN(trx_sys->view_list));
  1343. n_reserved = fil_space_get_n_reserved_extents(0);
  1344. if (n_reserved > 0) {
  1345. fprintf(file,
  1346. "%lu tablespace extents now reserved for"
  1347. " B-tree split operations\n",
  1348. (ulong) n_reserved);
  1349. }
  1350. #ifdef UNIV_LINUX
  1351. fprintf(file, "Main thread process no. %lu, id %lu, state: %s\n",
  1352. (ulong) srv_main_thread_process_no,
  1353. (ulong) srv_main_thread_id,
  1354. srv_main_thread_op_info);
  1355. #else
  1356. fprintf(file, "Main thread id %lu, state: %s\n",
  1357. (ulong) srv_main_thread_id,
  1358. srv_main_thread_op_info);
  1359. #endif
  1360. fprintf(file,
  1361. "Number of rows inserted " ULINTPF
  1362. ", updated " ULINTPF ", deleted " ULINTPF
  1363. ", read " ULINTPF "\n",
  1364. srv_n_rows_inserted,
  1365. srv_n_rows_updated,
  1366. srv_n_rows_deleted,
  1367. srv_n_rows_read);
  1368. fprintf(file,
  1369. "%.2f inserts/s, %.2f updates/s,"
  1370. " %.2f deletes/s, %.2f reads/s\n",
  1371. (srv_n_rows_inserted - srv_n_rows_inserted_old)
  1372. / time_elapsed,
  1373. (srv_n_rows_updated - srv_n_rows_updated_old)
  1374. / time_elapsed,
  1375. (srv_n_rows_deleted - srv_n_rows_deleted_old)
  1376. / time_elapsed,
  1377. (srv_n_rows_read - srv_n_rows_read_old)
  1378. / time_elapsed);
  1379. srv_n_rows_inserted_old = srv_n_rows_inserted;
  1380. srv_n_rows_updated_old = srv_n_rows_updated;
  1381. srv_n_rows_deleted_old = srv_n_rows_deleted;
  1382. srv_n_rows_read_old = srv_n_rows_read;
  1383. fputs("----------------------------\n"
  1384. "END OF INNODB MONITOR OUTPUT\n"
  1385. "============================\n", file);
  1386. mutex_exit(&srv_innodb_monitor_mutex);
  1387. fflush(file);
  1388. }
  1389. /**********************************************************************
  1390. Function to pass InnoDB status variables to MySQL */
  1391. void
  1392. srv_export_innodb_status(void)
  1393. {
  1394. mutex_enter(&srv_innodb_monitor_mutex);
  1395. export_vars.innodb_data_pending_reads
  1396. = os_n_pending_reads;
  1397. export_vars.innodb_data_pending_writes
  1398. = os_n_pending_writes;
  1399. export_vars.innodb_data_pending_fsyncs
  1400. = fil_n_pending_log_flushes
  1401. + fil_n_pending_tablespace_flushes;
  1402. export_vars.innodb_data_fsyncs = os_n_fsyncs;
  1403. export_vars.innodb_data_read = srv_data_read;
  1404. export_vars.innodb_data_reads = os_n_file_reads;
  1405. export_vars.innodb_data_writes = os_n_file_writes;
  1406. export_vars.innodb_data_written = srv_data_written;
  1407. export_vars.innodb_buffer_pool_read_requests = buf_pool->n_page_gets;
  1408. export_vars.innodb_buffer_pool_write_requests
  1409. = srv_buf_pool_write_requests;
  1410. export_vars.innodb_buffer_pool_wait_free = srv_buf_pool_wait_free;
  1411. export_vars.innodb_buffer_pool_pages_flushed = srv_buf_pool_flushed;
  1412. export_vars.innodb_buffer_pool_reads = srv_buf_pool_reads;
  1413. export_vars.innodb_buffer_pool_read_ahead_rnd = srv_read_ahead_rnd;
  1414. export_vars.innodb_buffer_pool_read_ahead_seq = srv_read_ahead_seq;
  1415. export_vars.innodb_buffer_pool_pages_data
  1416. = UT_LIST_GET_LEN(buf_pool->LRU);
  1417. export_vars.innodb_buffer_pool_pages_dirty
  1418. = UT_LIST_GET_LEN(buf_pool->flush_list);
  1419. export_vars.innodb_buffer_pool_pages_free
  1420. = UT_LIST_GET_LEN(buf_pool->free);
  1421. export_vars.innodb_buffer_pool_pages_latched
  1422. = buf_get_latched_pages_number();
  1423. export_vars.innodb_buffer_pool_pages_total = buf_pool->curr_size;
  1424. export_vars.innodb_buffer_pool_pages_misc = buf_pool->curr_size
  1425. - UT_LIST_GET_LEN(buf_pool->LRU)
  1426. - UT_LIST_GET_LEN(buf_pool->free);
  1427. export_vars.innodb_page_size = UNIV_PAGE_SIZE;
  1428. export_vars.innodb_log_waits = srv_log_waits;
  1429. export_vars.innodb_os_log_written = srv_os_log_written;
  1430. export_vars.innodb_os_log_fsyncs = fil_n_log_flushes;
  1431. export_vars.innodb_os_log_pending_fsyncs = fil_n_pending_log_flushes;
  1432. export_vars.innodb_os_log_pending_writes = srv_os_log_pending_writes;
  1433. export_vars.innodb_log_write_requests = srv_log_write_requests;
  1434. export_vars.innodb_log_writes = srv_log_writes;
  1435. export_vars.innodb_dblwr_pages_written = srv_dblwr_pages_written;
  1436. export_vars.innodb_dblwr_writes = srv_dblwr_writes;
  1437. export_vars.innodb_pages_created = buf_pool->n_pages_created;
  1438. export_vars.innodb_pages_read = buf_pool->n_pages_read;
  1439. export_vars.innodb_pages_written = buf_pool->n_pages_written;
  1440. export_vars.innodb_row_lock_waits = srv_n_lock_wait_count;
  1441. export_vars.innodb_row_lock_current_waits
  1442. = srv_n_lock_wait_current_count;
  1443. export_vars.innodb_row_lock_time = srv_n_lock_wait_time / 1000;
  1444. if (srv_n_lock_wait_count > 0) {
  1445. export_vars.innodb_row_lock_time_avg = (ulint)
  1446. (srv_n_lock_wait_time / 1000 / srv_n_lock_wait_count);
  1447. } else {
  1448. export_vars.innodb_row_lock_time_avg = 0;
  1449. }
  1450. export_vars.innodb_row_lock_time_max
  1451. = srv_n_lock_max_wait_time / 1000;
  1452. export_vars.innodb_rows_read = srv_n_rows_read;
  1453. export_vars.innodb_rows_inserted = srv_n_rows_inserted;
  1454. export_vars.innodb_rows_updated = srv_n_rows_updated;
  1455. export_vars.innodb_rows_deleted = srv_n_rows_deleted;
  1456. mutex_exit(&srv_innodb_monitor_mutex);
  1457. }
  1458. /*************************************************************************
  1459. A thread which wakes up threads whose lock wait may have lasted too long.
  1460. This also prints the info output by various InnoDB monitors. */
  1461. os_thread_ret_t
  1462. srv_lock_timeout_and_monitor_thread(
  1463. /*================================*/
  1464. /* out: a dummy parameter */
  1465. void* arg __attribute__((unused)))
  1466. /* in: a dummy parameter required by
  1467. os_thread_create */
  1468. {
  1469. srv_slot_t* slot;
  1470. double time_elapsed;
  1471. time_t current_time;
  1472. time_t last_table_monitor_time;
  1473. time_t last_tablespace_monitor_time;
  1474. time_t last_monitor_time;
  1475. ibool some_waits;
  1476. double wait_time;
  1477. ulint i;
  1478. #ifdef UNIV_DEBUG_THREAD_CREATION
  1479. fprintf(stderr, "Lock timeout thread starts, id %lu\n",
  1480. os_thread_pf(os_thread_get_curr_id()));
  1481. #endif
  1482. UT_NOT_USED(arg);
  1483. srv_last_monitor_time = time(NULL);
  1484. last_table_monitor_time = time(NULL);
  1485. last_tablespace_monitor_time = time(NULL);
  1486. last_monitor_time = time(NULL);
  1487. loop:
  1488. srv_lock_timeout_and_monitor_active = TRUE;
  1489. /* When someone is waiting for a lock, we wake up every second
  1490. and check if a timeout has passed for a lock wait */
  1491. os_thread_sleep(1000000);
  1492. /* In case mutex_exit is not a memory barrier, it is
  1493. theoretically possible some threads are left waiting though
  1494. the semaphore is already released. Wake up those threads: */
  1495. sync_arr_wake_threads_if_sema_free();
  1496. current_time = time(NULL);
  1497. time_elapsed = difftime(current_time, last_monitor_time);
  1498. if (time_elapsed > 15) {
  1499. last_monitor_time = time(NULL);
  1500. if (srv_print_innodb_monitor) {
  1501. srv_printf_innodb_monitor(stderr, NULL, NULL);
  1502. }
  1503. if (srv_innodb_status) {
  1504. mutex_enter(&srv_monitor_file_mutex);
  1505. rewind(srv_monitor_file);
  1506. srv_printf_innodb_monitor(srv_monitor_file, NULL,
  1507. NULL);
  1508. os_file_set_eof(srv_monitor_file);
  1509. mutex_exit(&srv_monitor_file_mutex);
  1510. }
  1511. if (srv_print_innodb_tablespace_monitor
  1512. && difftime(current_time,
  1513. last_tablespace_monitor_time) > 60) {
  1514. last_tablespace_monitor_time = time(NULL);
  1515. fputs("========================"
  1516. "========================\n",
  1517. stderr);
  1518. ut_print_timestamp(stderr);
  1519. fputs(" INNODB TABLESPACE MONITOR OUTPUT\n"
  1520. "========================"
  1521. "========================\n",
  1522. stderr);
  1523. fsp_print(0);
  1524. fputs("Validating tablespace\n", stderr);
  1525. fsp_validate(0);
  1526. fputs("Validation ok\n"
  1527. "---------------------------------------\n"
  1528. "END OF INNODB TABLESPACE MONITOR OUTPUT\n"
  1529. "=======================================\n",
  1530. stderr);
  1531. }
  1532. if (srv_print_innodb_table_monitor
  1533. && difftime(current_time, last_table_monitor_time) > 60) {
  1534. last_table_monitor_time = time(NULL);
  1535. fputs("===========================================\n",
  1536. stderr);
  1537. ut_print_timestamp(stderr);
  1538. fputs(" INNODB TABLE MONITOR OUTPUT\n"
  1539. "===========================================\n",
  1540. stderr);
  1541. dict_print();
  1542. fputs("-----------------------------------\n"
  1543. "END OF INNODB TABLE MONITOR OUTPUT\n"
  1544. "==================================\n",
  1545. stderr);
  1546. }
  1547. }
  1548. mutex_enter(&kernel_mutex);
  1549. some_waits = FALSE;
  1550. /* Check of all slots if a thread is waiting there, and if it
  1551. has exceeded the time limit */
  1552. for (i = 0; i < OS_THREAD_MAX_N; i++) {
  1553. slot = srv_mysql_table + i;
  1554. if (slot->in_use) {
  1555. some_waits = TRUE;
  1556. wait_time = ut_difftime(ut_time(), slot->suspend_time);
  1557. if (srv_lock_wait_timeout < 100000000
  1558. && (wait_time > (double) srv_lock_wait_timeout
  1559. || wait_time < 0)) {
  1560. /* Timeout exceeded or a wrap-around in system
  1561. time counter: cancel the lock request queued
  1562. by the transaction and release possible
  1563. other transactions waiting behind; it is
  1564. possible that the lock has already been
  1565. granted: in that case do nothing */
  1566. if (thr_get_trx(slot->thr)->wait_lock) {
  1567. lock_cancel_waiting_and_release(
  1568. thr_get_trx(slot->thr)
  1569. ->wait_lock);
  1570. }
  1571. }
  1572. }
  1573. }
  1574. os_event_reset(srv_lock_timeout_thread_event);
  1575. mutex_exit(&kernel_mutex);
  1576. if (srv_shutdown_state >= SRV_SHUTDOWN_CLEANUP) {
  1577. goto exit_func;
  1578. }
  1579. if (some_waits || srv_print_innodb_monitor
  1580. || srv_print_innodb_lock_monitor
  1581. || srv_print_innodb_tablespace_monitor
  1582. || srv_print_innodb_table_monitor) {
  1583. goto loop;
  1584. }
  1585. /* No one was waiting for a lock and no monitor was active:
  1586. suspend this thread */
  1587. srv_lock_timeout_and_monitor_active = FALSE;
  1588. #if 0
  1589. /* The following synchronisation is disabled, since
  1590. the InnoDB monitor output is to be updated every 15 seconds. */
  1591. os_event_wait(srv_lock_timeout_thread_event);
  1592. #endif
  1593. goto loop;
  1594. exit_func:
  1595. srv_lock_timeout_and_monitor_active = FALSE;
  1596. /* We count the number of threads in os_thread_exit(). A created
  1597. thread should always use that to exit and not use return() to exit. */
  1598. os_thread_exit(NULL);
  1599. OS_THREAD_DUMMY_RETURN;
  1600. }
  1601. /*************************************************************************
  1602. A thread which prints warnings about semaphore waits which have lasted
  1603. too long. These can be used to track bugs which cause hangs. */
  1604. os_thread_ret_t
  1605. srv_error_monitor_thread(
  1606. /*=====================*/
  1607. /* out: a dummy parameter */
  1608. void* arg __attribute__((unused)))
  1609. /* in: a dummy parameter required by
  1610. os_thread_create */
  1611. {
  1612. /* number of successive fatal timeouts observed */
  1613. ulint fatal_cnt = 0;
  1614. ib_uint64_t old_lsn;
  1615. ib_uint64_t new_lsn;
  1616. old_lsn = srv_start_lsn;
  1617. #ifdef UNIV_DEBUG_THREAD_CREATION
  1618. fprintf(stderr, "Error monitor thread starts, id %lu\n",
  1619. os_thread_pf(os_thread_get_curr_id()));
  1620. #endif
  1621. loop:
  1622. srv_error_monitor_active = TRUE;
  1623. /* Try to track a strange bug reported by Harald Fuchs and others,
  1624. where the lsn seems to decrease at times */
  1625. new_lsn = log_get_lsn();
  1626. if (new_lsn < old_lsn) {
  1627. ut_print_timestamp(stderr);
  1628. fprintf(stderr,
  1629. " InnoDB: Error: old log sequence number %llu"
  1630. " was greater\n"
  1631. "InnoDB: than the new log sequence number %llu!\n"
  1632. "InnoDB: Please submit a bug report"
  1633. " to http://bugs.mysql.com\n",
  1634. old_lsn, new_lsn);
  1635. }
  1636. old_lsn = new_lsn;
  1637. if (difftime(time(NULL), srv_last_monitor_time) > 60) {
  1638. /* We referesh InnoDB Monitor values so that averages are
  1639. printed from at most 60 last seconds */
  1640. srv_refresh_innodb_monitor_stats();
  1641. }
  1642. if (sync_array_print_long_waits()) {
  1643. fatal_cnt++;
  1644. if (fatal_cnt > 5) {
  1645. fprintf(stderr,
  1646. "InnoDB: Error: semaphore wait has lasted"
  1647. " > %lu seconds\n"
  1648. "InnoDB: We intentionally crash the server,"
  1649. " because it appears to be hung.\n",
  1650. (ulong) srv_fatal_semaphore_wait_threshold);
  1651. ut_error;
  1652. }
  1653. } else {
  1654. fatal_cnt = 0;
  1655. }
  1656. /* Flush stderr so that a database user gets the output
  1657. to possible MySQL error file */
  1658. fflush(stderr);
  1659. os_thread_sleep(2000000);
  1660. if (srv_shutdown_state < SRV_SHUTDOWN_CLEANUP) {
  1661. goto loop;
  1662. }
  1663. srv_error_monitor_active = FALSE;
  1664. /* We count the number of threads in os_thread_exit(). A created
  1665. thread should always use that to exit and not use return() to exit. */
  1666. os_thread_exit(NULL);
  1667. OS_THREAD_DUMMY_RETURN;
  1668. }
  1669. /***********************************************************************
  1670. Tells the InnoDB server that there has been activity in the database
  1671. and wakes up the master thread if it is suspended (not sleeping). Used
  1672. in the MySQL interface. Note that there is a small chance that the master
  1673. thread stays suspended (we do not protect our operation with the kernel
  1674. mutex, for performace reasons). */
  1675. void
  1676. srv_active_wake_master_thread(void)
  1677. /*===============================*/
  1678. {
  1679. srv_activity_count++;
  1680. if (srv_n_threads_active[SRV_MASTER] == 0) {
  1681. mutex_enter(&kernel_mutex);
  1682. srv_release_threads(SRV_MASTER, 1);
  1683. mutex_exit(&kernel_mutex);
  1684. }
  1685. }
  1686. /***********************************************************************
  1687. Wakes up the master thread if it is suspended or being suspended. */
  1688. void
  1689. srv_wake_master_thread(void)
  1690. /*========================*/
  1691. {
  1692. srv_activity_count++;
  1693. mutex_enter(&kernel_mutex);
  1694. srv_release_threads(SRV_MASTER, 1);
  1695. mutex_exit(&kernel_mutex);
  1696. }
  1697. /*************************************************************************
  1698. The master thread controlling the server. */
  1699. os_thread_ret_t
  1700. srv_master_thread(
  1701. /*==============*/
  1702. /* out: a dummy parameter */
  1703. void* arg __attribute__((unused)))
  1704. /* in: a dummy parameter required by
  1705. os_thread_create */
  1706. {
  1707. os_event_t event;
  1708. time_t last_flush_time;
  1709. time_t current_time;
  1710. ulint old_activity_count;
  1711. ulint n_pages_purged = 0;
  1712. ulint n_bytes_merged;
  1713. ulint n_pages_flushed;
  1714. ulint n_bytes_archived;
  1715. ulint n_tables_to_drop;
  1716. ulint n_ios;
  1717. ulint n_ios_old;
  1718. ulint n_ios_very_old;
  1719. ulint n_pend_ios;
  1720. ibool skip_sleep = FALSE;
  1721. ulint i;
  1722. #ifdef UNIV_DEBUG_THREAD_CREATION
  1723. fprintf(stderr, "Master thread starts, id %lu\n",
  1724. os_thread_pf(os_thread_get_curr_id()));
  1725. #endif
  1726. srv_main_thread_process_no = os_proc_get_number();
  1727. srv_main_thread_id = os_thread_pf(os_thread_get_curr_id());
  1728. srv_table_reserve_slot(SRV_MASTER);
  1729. mutex_enter(&kernel_mutex);
  1730. srv_n_threads_active[SRV_MASTER]++;
  1731. mutex_exit(&kernel_mutex);
  1732. loop:
  1733. /*****************************************************************/
  1734. /* ---- When there is database activity by users, we cycle in this
  1735. loop */
  1736. srv_main_thread_op_info = "reserving kernel mutex";
  1737. n_ios_very_old = log_sys->n_log_ios + buf_pool->n_pages_read
  1738. + buf_pool->n_pages_written;
  1739. mutex_enter(&kernel_mutex);
  1740. /* Store the user activity counter at the start of this loop */
  1741. old_activity_count = srv_activity_count;
  1742. mutex_exit(&kernel_mutex);
  1743. if (srv_force_recovery >= SRV_FORCE_NO_BACKGROUND) {
  1744. goto suspend_thread;
  1745. }
  1746. /* ---- We run the following loop approximately once per second
  1747. when there is database activity */
  1748. skip_sleep = FALSE;
  1749. for (i = 0; i < 10; i++) {
  1750. n_ios_old = log_sys->n_log_ios + buf_pool->n_pages_read
  1751. + buf_pool->n_pages_written;
  1752. srv_main_thread_op_info = "sleeping";
  1753. if (!skip_sleep) {
  1754. os_thread_sleep(1000000);
  1755. }
  1756. skip_sleep = FALSE;
  1757. /* ALTER TABLE in MySQL requires on Unix that the table handler
  1758. can drop tables lazily after there no longer are SELECT
  1759. queries to them. */
  1760. srv_main_thread_op_info = "doing background drop tables";
  1761. row_drop_tables_for_mysql_in_background();
  1762. srv_main_thread_op_info = "";
  1763. if (srv_fast_shutdown && srv_shutdown_state > 0) {
  1764. goto background_loop;
  1765. }
  1766. /* We flush the log once in a second even if no commit
  1767. is issued or the we have specified in my.cnf no flush
  1768. at transaction commit */
  1769. srv_main_thread_op_info = "flushing log";
  1770. log_buffer_flush_to_disk();
  1771. srv_main_thread_op_info = "making checkpoint";
  1772. log_free_check();
  1773. /* If there were less than 5 i/os during the
  1774. one second sleep, we assume that there is free
  1775. disk i/o capacity available, and it makes sense to
  1776. do an insert buffer merge. */
  1777. n_pend_ios = buf_get_n_pending_ios()
  1778. + log_sys->n_pending_writes;
  1779. n_ios = log_sys->n_log_ios + buf_pool->n_pages_read
  1780. + buf_pool->n_pages_written;
  1781. if (n_pend_ios < 3 && (n_ios - n_ios_old < 5)) {
  1782. srv_main_thread_op_info = "doing insert buffer merge";
  1783. ibuf_contract_for_n_pages(
  1784. TRUE, srv_insert_buffer_batch_size / 4);
  1785. srv_main_thread_op_info = "flushing log";
  1786. log_buffer_flush_to_disk();
  1787. }
  1788. if (UNIV_UNLIKELY(buf_get_modified_ratio_pct()
  1789. > srv_max_buf_pool_modified_pct)) {
  1790. /* Try to keep the number of modified pages in the
  1791. buffer pool under the limit wished by the user */
  1792. n_pages_flushed = buf_flush_batch(BUF_FLUSH_LIST, 100,
  1793. IB_ULONGLONG_MAX);
  1794. /* If we had to do the flush, it may have taken
  1795. even more than 1 second, and also, there may be more
  1796. to flush. Do not sleep 1 second during the next
  1797. iteration of this loop. */
  1798. skip_sleep = TRUE;
  1799. }
  1800. if (srv_activity_count == old_activity_count) {
  1801. /* There is no user activity at the moment, go to
  1802. the background loop */
  1803. goto background_loop;
  1804. }
  1805. }
  1806. /* ---- We perform the following code approximately once per
  1807. 10 seconds when there is database activity */
  1808. #ifdef MEM_PERIODIC_CHECK
  1809. /* Check magic numbers of every allocated mem block once in 10
  1810. seconds */
  1811. mem_validate_all_blocks();
  1812. #endif
  1813. /* If there were less than 200 i/os during the 10 second period,
  1814. we assume that there is free disk i/o capacity available, and it
  1815. makes sense to flush 100 pages. */
  1816. n_pend_ios = buf_get_n_pending_ios() + log_sys->n_pending_writes;
  1817. n_ios = log_sys->n_log_ios + buf_pool->n_pages_read
  1818. + buf_pool->n_pages_written;
  1819. if (n_pend_ios < 3 && (n_ios - n_ios_very_old < 200)) {
  1820. srv_main_thread_op_info = "flushing buffer pool pages";
  1821. buf_flush_batch(BUF_FLUSH_LIST, 100, IB_ULONGLONG_MAX);
  1822. srv_main_thread_op_info = "flushing log";
  1823. log_buffer_flush_to_disk();
  1824. }
  1825. /* We run a batch of insert buffer merge every 10 seconds,
  1826. even if the server were active */
  1827. srv_main_thread_op_info = "doing insert buffer merge";
  1828. ibuf_contract_for_n_pages(TRUE, srv_insert_buffer_batch_size / 4);
  1829. srv_main_thread_op_info = "flushing log";
  1830. log_buffer_flush_to_disk();
  1831. /* We run a full purge every 10 seconds, even if the server
  1832. were active */
  1833. last_flush_time = time(NULL);
  1834. do {
  1835. if (srv_fast_shutdown && srv_shutdown_state > 0) {
  1836. goto background_loop;
  1837. }
  1838. srv_main_thread_op_info = "purging";
  1839. n_pages_purged = trx_purge();
  1840. current_time = time(NULL);
  1841. if (difftime(current_time, last_flush_time) > 1) {
  1842. srv_main_thread_op_info = "flushing log";
  1843. log_buffer_flush_to_disk();
  1844. last_flush_time = current_time;
  1845. }
  1846. } while (n_pages_purged);
  1847. srv_main_thread_op_info = "flushing buffer pool pages";
  1848. /* Flush a few oldest pages to make a new checkpoint younger */
  1849. if (buf_get_modified_ratio_pct() > 70) {
  1850. /* If there are lots of modified pages in the buffer pool
  1851. (> 70 %), we assume we can afford reserving the disk(s) for
  1852. the time it requires to flush 100 pages */
  1853. n_pages_flushed = buf_flush_batch(BUF_FLUSH_LIST, 100,
  1854. IB_ULONGLONG_MAX);
  1855. } else {
  1856. /* Otherwise, we only flush a small number of pages so that
  1857. we do not unnecessarily use much disk i/o capacity from
  1858. other work */
  1859. n_pages_flushed = buf_flush_batch(BUF_FLUSH_LIST, 10,
  1860. IB_ULONGLONG_MAX);
  1861. }
  1862. srv_main_thread_op_info = "making checkpoint";
  1863. /* Make a new checkpoint about once in 10 seconds */
  1864. log_checkpoint(TRUE, FALSE);
  1865. srv_main_thread_op_info = "reserving kernel mutex";
  1866. mutex_enter(&kernel_mutex);
  1867. /* ---- When there is database activity, we jump from here back to
  1868. the start of loop */
  1869. if (srv_activity_count != old_activity_count) {
  1870. mutex_exit(&kernel_mutex);
  1871. goto loop;
  1872. }
  1873. mutex_exit(&kernel_mutex);
  1874. /* If the database is quiet, we enter the background loop */
  1875. /*****************************************************************/
  1876. background_loop:
  1877. /* ---- In this loop we run background operations when the server
  1878. is quiet from user activity. Also in the case of a shutdown, we
  1879. loop here, flushing the buffer pool to the data files. */
  1880. /* The server has been quiet for a while: start running background
  1881. operations */
  1882. srv_main_thread_op_info = "doing background drop tables";
  1883. n_tables_to_drop = row_drop_tables_for_mysql_in_background();
  1884. if (n_tables_to_drop > 0) {
  1885. /* Do not monopolize the CPU even if there are tables waiting
  1886. in the background drop queue. (It is essentially a bug if
  1887. MySQL tries to drop a table while there are still open handles
  1888. to it and we had to put it to the background drop queue.) */
  1889. os_thread_sleep(100000);
  1890. }
  1891. srv_main_thread_op_info = "purging";
  1892. /* Run a full purge */
  1893. last_flush_time = time(NULL);
  1894. do {
  1895. if (srv_fast_shutdown && srv_shutdown_state > 0) {
  1896. break;
  1897. }
  1898. srv_main_thread_op_info = "purging";
  1899. n_pages_purged = trx_purge();
  1900. current_time = time(NULL);
  1901. if (difftime(current_time, last_flush_time) > 1) {
  1902. srv_main_thread_op_info = "flushing log";
  1903. log_buffer_flush_to_disk();
  1904. last_flush_time = current_time;
  1905. }
  1906. } while (n_pages_purged);
  1907. srv_main_thread_op_info = "reserving kernel mutex";
  1908. mutex_enter(&kernel_mutex);
  1909. if (srv_activity_count != old_activity_count) {
  1910. mutex_exit(&kernel_mutex);
  1911. goto loop;
  1912. }
  1913. mutex_exit(&kernel_mutex);
  1914. srv_main_thread_op_info = "doing insert buffer merge";
  1915. if (srv_fast_shutdown && srv_shutdown_state > 0) {
  1916. n_bytes_merged = 0;
  1917. } else {
  1918. n_bytes_merged = ibuf_contract_for_n_pages(
  1919. TRUE, srv_insert_buffer_batch_size);
  1920. }
  1921. srv_main_thread_op_info = "reserving kernel mutex";
  1922. mutex_enter(&kernel_mutex);
  1923. if (srv_activity_count != old_activity_count) {
  1924. mutex_exit(&kernel_mutex);
  1925. goto loop;
  1926. }
  1927. mutex_exit(&kernel_mutex);
  1928. flush_loop:
  1929. srv_main_thread_op_info = "flushing buffer pool pages";
  1930. if (srv_fast_shutdown < 2) {
  1931. n_pages_flushed = buf_flush_batch(BUF_FLUSH_LIST, 100,
  1932. IB_ULONGLONG_MAX);
  1933. } else {
  1934. /* In the fastest shutdown we do not flush the buffer pool
  1935. to data files: we set n_pages_flushed to 0 artificially. */
  1936. n_pages_flushed = 0;
  1937. }
  1938. srv_main_thread_op_info = "reserving kernel mutex";
  1939. mutex_enter(&kernel_mutex);
  1940. if (srv_activity_count != old_activity_count) {
  1941. mutex_exit(&kernel_mutex);
  1942. goto loop;
  1943. }
  1944. mutex_exit(&kernel_mutex);
  1945. srv_main_thread_op_info = "waiting for buffer pool flush to end";
  1946. buf_flush_wait_batch_end(BUF_FLUSH_LIST);
  1947. srv_main_thread_op_info = "flushing log";
  1948. log_buffer_flush_to_disk();
  1949. srv_main_thread_op_info = "making checkpoint";
  1950. log_checkpoint(TRUE, FALSE);
  1951. if (buf_get_modified_ratio_pct() > srv_max_buf_pool_modified_pct) {
  1952. /* Try to keep the number of modified pages in the
  1953. buffer pool under the limit wished by the user */
  1954. goto flush_loop;
  1955. }
  1956. srv_main_thread_op_info = "reserving kernel mutex";
  1957. mutex_enter(&kernel_mutex);
  1958. if (srv_activity_count != old_activity_count) {
  1959. mutex_exit(&kernel_mutex);
  1960. goto loop;
  1961. }
  1962. mutex_exit(&kernel_mutex);
  1963. /*
  1964. srv_main_thread_op_info = "archiving log (if log archive is on)";
  1965. log_archive_do(FALSE, &n_bytes_archived);
  1966. */
  1967. n_bytes_archived = 0;
  1968. /* Keep looping in the background loop if still work to do */
  1969. if (srv_fast_shutdown && srv_shutdown_state > 0) {
  1970. if (n_tables_to_drop + n_pages_flushed
  1971. + n_bytes_archived != 0) {
  1972. /* If we are doing a fast shutdown (= the default)
  1973. we do not do purge or insert buffer merge. But we
  1974. flush the buffer pool completely to disk.
  1975. In a 'very fast' shutdown we do not flush the buffer
  1976. pool to data files: we have set n_pages_flushed to
  1977. 0 artificially. */
  1978. goto background_loop;
  1979. }
  1980. } else if (n_tables_to_drop
  1981. + n_pages_purged + n_bytes_merged + n_pages_flushed
  1982. + n_bytes_archived != 0) {
  1983. /* In a 'slow' shutdown we run purge and the insert buffer
  1984. merge to completion */
  1985. goto background_loop;
  1986. }
  1987. /* There is no work for background operations either: suspend
  1988. master thread to wait for more server activity */
  1989. suspend_thread:
  1990. srv_main_thread_op_info = "suspending";
  1991. mutex_enter(&kernel_mutex);
  1992. if (row_get_background_drop_list_len_low() > 0) {
  1993. mutex_exit(&kernel_mutex);
  1994. goto loop;
  1995. }
  1996. event = srv_suspend_thread();
  1997. mutex_exit(&kernel_mutex);
  1998. /* DO NOT CHANGE THIS STRING. innobase_start_or_create_for_mysql()
  1999. waits for database activity to die down when converting < 4.1.x
  2000. databases, and relies on this string being exactly as it is. InnoDB
  2001. manual also mentions this string in several places. */
  2002. srv_main_thread_op_info = "waiting for server activity";
  2003. os_event_wait(event);
  2004. if (srv_shutdown_state == SRV_SHUTDOWN_EXIT_THREADS) {
  2005. /* This is only extra safety, the thread should exit
  2006. already when the event wait ends */
  2007. os_thread_exit(NULL);
  2008. }
  2009. /* When there is user activity, InnoDB will set the event and the
  2010. main thread goes back to loop. */
  2011. goto loop;
  2012. OS_THREAD_DUMMY_RETURN; /* Not reached, avoid compiler warning */
  2013. }
  2014. #endif /* !UNIV_HOTBACKUP */