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.

3634 lines
106 KiB

17 years ago
16 years ago
17 years ago
17 years ago
17 years ago
16 years ago
17 years ago
15 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
15 years ago
15 years ago
15 years ago
16 years ago
17 years ago
17 years ago
15 years ago
17 years ago
17 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
15 years ago
15 years ago
15 years ago
16 years ago
16 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
16 years ago
16 years ago
15 years ago
15 years ago
15 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
15 years ago
15 years ago
15 years ago
15 years ago
16 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
16 years ago
15 years ago
15 years ago
16 years ago
16 years ago
15 years ago
16 years ago
15 years ago
16 years ago
15 years ago
15 years ago
15 years ago
15 years ago
16 years ago
16 years ago
16 years ago
17 years ago
16 years ago
15 years ago
16 years ago
15 years ago
16 years ago
16 years ago
16 years ago
16 years ago
15 years ago
15 years ago
15 years ago
16 years ago
16 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
16 years ago
17 years ago
15 years ago
15 years ago
15 years ago
16 years ago
16 years ago
16 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
16 years ago
16 years ago
16 years ago
15 years ago
15 years ago
16 years ago
16 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
16 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
16 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
16 years ago
16 years ago
15 years ago
15 years ago
15 years ago
16 years ago
16 years ago
15 years ago
16 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
  1. /*****************************************************************************
  2. Copyright (c) 1995, 2013, Oracle and/or its affiliates. All Rights Reserved.
  3. Copyright (c) 2008, 2009 Google Inc.
  4. Copyright (c) 2009, Percona Inc.
  5. Copyright (c) 2013, 2014, SkySQL Ab.
  6. Portions of this file contain modifications contributed and copyrighted by
  7. Google, Inc. Those modifications are gratefully acknowledged and are described
  8. briefly in the InnoDB documentation. The contributions by Google are
  9. incorporated with their permission, and subject to the conditions contained in
  10. the file COPYING.Google.
  11. Portions of this file contain modifications contributed and copyrighted
  12. by Percona Inc.. Those modifications are
  13. gratefully acknowledged and are described briefly in the InnoDB
  14. documentation. The contributions by Percona Inc. are incorporated with
  15. their permission, and subject to the conditions contained in the file
  16. COPYING.Percona.
  17. This program is free software; you can redistribute it and/or modify it under
  18. the terms of the GNU General Public License as published by the Free Software
  19. Foundation; version 2 of the License.
  20. This program is distributed in the hope that it will be useful, but WITHOUT
  21. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  22. FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  23. You should have received a copy of the GNU General Public License along with
  24. this program; if not, write to the Free Software Foundation, Inc.,
  25. 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
  26. *****************************************************************************/
  27. /**************************************************//**
  28. @file srv/srv0srv.cc
  29. The database server main program
  30. Created 10/8/1995 Heikki Tuuri
  31. *******************************************************/
  32. /* Dummy comment */
  33. #include "srv0srv.h"
  34. #include "ut0mem.h"
  35. #include "ut0ut.h"
  36. #include "os0proc.h"
  37. #include "mem0mem.h"
  38. #include "mem0pool.h"
  39. #include "sync0sync.h"
  40. #include "que0que.h"
  41. #include "log0online.h"
  42. #include "log0recv.h"
  43. #include "pars0pars.h"
  44. #include "usr0sess.h"
  45. #include "lock0lock.h"
  46. #include "trx0purge.h"
  47. #include "ibuf0ibuf.h"
  48. #include "buf0flu.h"
  49. #include "buf0lru.h"
  50. #include "btr0sea.h"
  51. #include "dict0load.h"
  52. #include "dict0boot.h"
  53. #include "dict0stats_bg.h" /* dict_stats_event */
  54. #include "srv0start.h"
  55. #include "row0mysql.h"
  56. #include "row0log.h"
  57. #include "ha_prototypes.h"
  58. #include "trx0i_s.h"
  59. #include "os0sync.h" /* for HAVE_ATOMIC_BUILTINS */
  60. #include "srv0mon.h"
  61. #include "ut0crc32.h"
  62. #include "os0file.h"
  63. #include "btr0defragment.h"
  64. #include "mysql/plugin.h"
  65. #include "mysql/service_thd_wait.h"
  66. #include "fil0pagecompress.h"
  67. #include <my_rdtsc.h>
  68. /* prototypes of new functions added to ha_innodb.cc for kill_idle_transaction */
  69. ibool innobase_thd_is_idle(const void* thd);
  70. ib_int64_t innobase_thd_get_start_time(const void* thd);
  71. void innobase_thd_kill(ulong thd_id);
  72. ulong innobase_thd_get_thread_id(const void* thd);
  73. /* prototypes for new functions added to ha_innodb.cc */
  74. ibool innobase_get_slow_log();
  75. #ifdef WITH_WSREP
  76. extern int wsrep_debug;
  77. extern int wsrep_trx_is_aborting(void *thd_ptr);
  78. #endif
  79. /* The following counter is incremented whenever there is some user activity
  80. in the server */
  81. UNIV_INTERN ulint srv_activity_count = 0;
  82. /* The following is the maximum allowed duration of a lock wait. */
  83. UNIV_INTERN ulint srv_fatal_semaphore_wait_threshold = 600;
  84. /**/
  85. UNIV_INTERN long long srv_kill_idle_transaction = 0;
  86. /* How much data manipulation language (DML) statements need to be delayed,
  87. in microseconds, in order to reduce the lagging of the purge thread. */
  88. UNIV_INTERN ulint srv_dml_needed_delay = 0;
  89. UNIV_INTERN ibool srv_monitor_active = FALSE;
  90. UNIV_INTERN ibool srv_error_monitor_active = FALSE;
  91. UNIV_INTERN ibool srv_buf_dump_thread_active = FALSE;
  92. UNIV_INTERN ibool srv_dict_stats_thread_active = FALSE;
  93. UNIV_INTERN const char* srv_main_thread_op_info = "";
  94. /** Prefix used by MySQL to indicate pre-5.1 table name encoding */
  95. const char srv_mysql50_table_name_prefix[10] = "#mysql50#";
  96. /* Server parameters which are read from the initfile */
  97. /* The following three are dir paths which are catenated before file
  98. names, where the file name itself may also contain a path */
  99. UNIV_INTERN char* srv_data_home = NULL;
  100. /** Rollback files directory, can be absolute. */
  101. UNIV_INTERN char* srv_undo_dir = NULL;
  102. /** The number of tablespaces to use for rollback segments. */
  103. UNIV_INTERN ulong srv_undo_tablespaces = 8;
  104. /** The number of UNDO tablespaces that are open and ready to use. */
  105. UNIV_INTERN ulint srv_undo_tablespaces_open = 8;
  106. /* The number of rollback segments to use */
  107. UNIV_INTERN ulong srv_undo_logs = 1;
  108. #ifdef UNIV_LOG_ARCHIVE
  109. UNIV_INTERN char* srv_arch_dir = NULL;
  110. UNIV_INTERN ulong srv_log_arch_expire_sec = 0;
  111. #endif /* UNIV_LOG_ARCHIVE */
  112. /** Set if InnoDB must operate in read-only mode. We don't do any
  113. recovery and open all tables in RO mode instead of RW mode. We don't
  114. sync the max trx id to disk either. */
  115. UNIV_INTERN my_bool srv_read_only_mode;
  116. /** store to its own file each table created by an user; data
  117. dictionary tables are in the system tablespace 0 */
  118. UNIV_INTERN my_bool srv_file_per_table;
  119. /** The file format to use on new *.ibd files. */
  120. UNIV_INTERN ulint srv_file_format = 0;
  121. /** Whether to check file format during startup. A value of
  122. UNIV_FORMAT_MAX + 1 means no checking ie. FALSE. The default is to
  123. set it to the highest format we support. */
  124. UNIV_INTERN ulint srv_max_file_format_at_startup = UNIV_FORMAT_MAX;
  125. #if UNIV_FORMAT_A
  126. # error "UNIV_FORMAT_A must be 0!"
  127. #endif
  128. /** Place locks to records only i.e. do not use next-key locking except
  129. on duplicate key checking and foreign key checking */
  130. UNIV_INTERN ibool srv_locks_unsafe_for_binlog = FALSE;
  131. /** Sort buffer size in index creation */
  132. UNIV_INTERN ulong srv_sort_buf_size = 1048576;
  133. /** Maximum modification log file size for online index creation */
  134. UNIV_INTERN unsigned long long srv_online_max_size;
  135. /* If this flag is TRUE, then we will use the native aio of the
  136. OS (provided we compiled Innobase with it in), otherwise we will
  137. use simulated aio we build below with threads.
  138. Currently we support native aio on windows and linux */
  139. UNIV_INTERN my_bool srv_use_native_aio = TRUE;
  140. /* Default compression level if page compression is used and no compression
  141. level is set for the table*/
  142. UNIV_INTERN long srv_compress_zlib_level = 6;
  143. /* If this flag is TRUE, then we will use fallocate(PUCH_HOLE)
  144. to the pages */
  145. UNIV_INTERN my_bool srv_use_trim = FALSE;
  146. /* If this flag is TRUE, then we will use posix fallocate for file extentsion */
  147. UNIV_INTERN my_bool srv_use_posix_fallocate = FALSE;
  148. /* If this flag is TRUE, then we disable doublewrite buffer */
  149. UNIV_INTERN my_bool srv_use_atomic_writes = FALSE;
  150. /* If this flag IS TRUE, then we use this algorithm for page compressing the pages */
  151. UNIV_INTERN ulong innodb_compression_algorithm = PAGE_ZLIB_ALGORITHM;
  152. /* Number of threads used for multi-threaded flush */
  153. UNIV_INTERN long srv_mtflush_threads = MTFLUSH_DEFAULT_WORKER;
  154. /* If this flag is TRUE, then we will use multi threaded flush. */
  155. UNIV_INTERN my_bool srv_use_mtflush = FALSE;
  156. #ifdef __WIN__
  157. /* Windows native condition variables. We use runtime loading / function
  158. pointers, because they are not available on Windows Server 2003 and
  159. Windows XP/2000.
  160. We use condition for events on Windows if possible, even if os_event
  161. resembles Windows kernel event object well API-wise. The reason is
  162. performance, kernel objects are heavyweights and WaitForSingleObject() is a
  163. performance killer causing calling thread to context switch. Besides, Innodb
  164. is preallocating large number (often millions) of os_events. With kernel event
  165. objects it takes a big chunk out of non-paged pool, which is better suited
  166. for tasks like IO than for storing idle event objects. */
  167. UNIV_INTERN ibool srv_use_native_conditions = FALSE;
  168. #endif /* __WIN__ */
  169. UNIV_INTERN ulint srv_n_data_files = 0;
  170. UNIV_INTERN char** srv_data_file_names = NULL;
  171. /* size in database pages */
  172. UNIV_INTERN ulint* srv_data_file_sizes = NULL;
  173. UNIV_INTERN my_bool srv_track_changed_pages = FALSE;
  174. UNIV_INTERN ulonglong srv_max_bitmap_file_size = 100 * 1024 * 1024;
  175. UNIV_INTERN ulonglong srv_max_changed_pages = 0;
  176. /** When TRUE, fake change transcations take S rather than X row locks.
  177. When FALSE, row locks are not taken at all. */
  178. UNIV_INTERN my_bool srv_fake_changes_locks = TRUE;
  179. /* if TRUE, then we auto-extend the last data file */
  180. UNIV_INTERN ibool srv_auto_extend_last_data_file = FALSE;
  181. /* if != 0, this tells the max size auto-extending may increase the
  182. last data file size */
  183. UNIV_INTERN ulint srv_last_file_size_max = 0;
  184. /* If the last data file is auto-extended, we add this
  185. many pages to it at a time */
  186. UNIV_INTERN ulong srv_auto_extend_increment = 8;
  187. UNIV_INTERN ulint* srv_data_file_is_raw_partition = NULL;
  188. /* If the following is TRUE we do not allow inserts etc. This protects
  189. the user from forgetting the 'newraw' keyword to my.cnf */
  190. UNIV_INTERN ibool srv_created_new_raw = FALSE;
  191. UNIV_INTERN char* srv_log_group_home_dir = NULL;
  192. UNIV_INTERN ulong srv_n_log_files = SRV_N_LOG_FILES_MAX;
  193. /* size in database pages */
  194. UNIV_INTERN ib_uint64_t srv_log_file_size = IB_UINT64_MAX;
  195. UNIV_INTERN ib_uint64_t srv_log_file_size_requested;
  196. /* size in database pages */
  197. UNIV_INTERN ulint srv_log_buffer_size = ULINT_MAX;
  198. UNIV_INTERN uint srv_flush_log_at_timeout = 1;
  199. UNIV_INTERN ulong srv_page_size = UNIV_PAGE_SIZE_DEF;
  200. UNIV_INTERN ulong srv_page_size_shift = UNIV_PAGE_SIZE_SHIFT_DEF;
  201. UNIV_INTERN char srv_use_global_flush_log_at_trx_commit = TRUE;
  202. /* Try to flush dirty pages so as to avoid IO bursts at
  203. the checkpoints. */
  204. UNIV_INTERN char srv_adaptive_flushing = TRUE;
  205. UNIV_INTERN ulong srv_show_locks_held = 10;
  206. UNIV_INTERN ulong srv_show_verbose_locks = 0;
  207. /** Maximum number of times allowed to conditionally acquire
  208. mutex before switching to blocking wait on the mutex */
  209. #define MAX_MUTEX_NOWAIT 20
  210. /** Check whether the number of failed nonblocking mutex
  211. acquisition attempts exceeds maximum allowed value. If so,
  212. srv_printf_innodb_monitor() will request mutex acquisition
  213. with mutex_enter(), which will wait until it gets the mutex. */
  214. #define MUTEX_NOWAIT(mutex_skipped) ((mutex_skipped) < MAX_MUTEX_NOWAIT)
  215. #ifdef WITH_INNODB_DISALLOW_WRITES
  216. UNIV_INTERN os_event_t srv_allow_writes_event;
  217. #endif /* WITH_INNODB_DISALLOW_WRITES */
  218. /** The sort order table of the MySQL latin1_swedish_ci character set
  219. collation */
  220. UNIV_INTERN const byte* srv_latin1_ordering;
  221. /* use os/external memory allocator */
  222. UNIV_INTERN my_bool srv_use_sys_malloc = TRUE;
  223. /* requested size in kilobytes */
  224. UNIV_INTERN ulint srv_buf_pool_size = ULINT_MAX;
  225. /* force virtual page preallocation (prefault) */
  226. UNIV_INTERN my_bool srv_buf_pool_populate = FALSE;
  227. /* requested number of buffer pool instances */
  228. UNIV_INTERN ulint srv_buf_pool_instances = 1;
  229. /* number of locks to protect buf_pool->page_hash */
  230. UNIV_INTERN ulong srv_n_page_hash_locks = 16;
  231. /** Scan depth for LRU flush batch i.e.: number of blocks scanned*/
  232. UNIV_INTERN ulong srv_LRU_scan_depth = 1024;
  233. /** whether or not to flush neighbors of a block */
  234. UNIV_INTERN ulong srv_flush_neighbors = 1;
  235. /* previously requested size */
  236. UNIV_INTERN ulint srv_buf_pool_old_size;
  237. /* current size in kilobytes */
  238. UNIV_INTERN ulint srv_buf_pool_curr_size = 0;
  239. /* size in bytes */
  240. UNIV_INTERN ulint srv_mem_pool_size = ULINT_MAX;
  241. UNIV_INTERN ulint srv_lock_table_size = ULINT_MAX;
  242. /* Defragmentation */
  243. UNIV_INTERN my_bool srv_defragment = FALSE;
  244. UNIV_INTERN uint srv_defragment_n_pages = 7;
  245. UNIV_INTERN uint srv_defragment_stats_accuracy = 0;
  246. UNIV_INTERN uint srv_defragment_fill_factor_n_recs = 20;
  247. UNIV_INTERN double srv_defragment_fill_factor = 0.9;
  248. UNIV_INTERN uint srv_defragment_frequency =
  249. SRV_DEFRAGMENT_FREQUENCY_DEFAULT;
  250. UNIV_INTERN ulonglong srv_defragment_interval = 0;
  251. /** Query thread preflush algorithm */
  252. UNIV_INTERN ulong srv_foreground_preflush
  253. = SRV_FOREGROUND_PREFLUSH_EXP_BACKOFF;
  254. /** The maximum time limit for a single LRU tail flush iteration by the page
  255. cleaner thread */
  256. UNIV_INTERN ulint srv_cleaner_max_lru_time = 1000;
  257. /** The maximum time limit for a single flush list flush iteration by the page
  258. cleaner thread */
  259. UNIV_INTERN ulint srv_cleaner_max_flush_time = 1000;
  260. /** Page cleaner flush list flush batches are further divided into this chunk
  261. size */
  262. UNIV_INTERN ulint srv_cleaner_flush_chunk_size = 100;
  263. /** Page cleaner LRU list flush batches are further divided into this chunk
  264. size */
  265. UNIV_INTERN ulint srv_cleaner_lru_chunk_size = 100;
  266. /** If free list length is lower than this percentage of srv_LRU_scan_depth,
  267. page cleaner LRU flushes will issue flush batches to the same instance in a
  268. row */
  269. UNIV_INTERN ulint srv_cleaner_free_list_lwm = 10;
  270. /** If TRUE, page cleaner heuristics use evicted instead of flushed page counts
  271. for its heuristics */
  272. UNIV_INTERN my_bool srv_cleaner_eviction_factor = FALSE;
  273. /** Page cleaner LSN age factor formula option */
  274. UNIV_INTERN ulong srv_cleaner_lsn_age_factor
  275. = SRV_CLEANER_LSN_AGE_FACTOR_HIGH_CHECKPOINT;
  276. /** Empty free list for a query thread handling algorithm option */
  277. UNIV_INTERN ulong srv_empty_free_list_algorithm
  278. = SRV_EMPTY_FREE_LIST_BACKOFF;
  279. UNIV_INTERN ulint srv_idle_flush_pct = 100;
  280. /* This parameter is deprecated. Use srv_n_io_[read|write]_threads
  281. instead. */
  282. UNIV_INTERN ulint srv_n_file_io_threads = ULINT_MAX;
  283. UNIV_INTERN ulint srv_n_read_io_threads = ULINT_MAX;
  284. UNIV_INTERN ulint srv_n_write_io_threads = ULINT_MAX;
  285. /* Switch to enable random read ahead. */
  286. UNIV_INTERN my_bool srv_random_read_ahead = FALSE;
  287. /* The log block size */
  288. UNIV_INTERN ulint srv_log_block_size = 0;
  289. /* User settable value of the number of pages that must be present
  290. in the buffer cache and accessed sequentially for InnoDB to trigger a
  291. readahead request. */
  292. UNIV_INTERN ulong srv_read_ahead_threshold = 56;
  293. #ifdef UNIV_LOG_ARCHIVE
  294. UNIV_INTERN ibool srv_log_archive_on = FALSE;
  295. UNIV_INTERN ibool srv_archive_recovery = 0;
  296. UNIV_INTERN ib_uint64_t srv_archive_recovery_limit_lsn;
  297. #endif /* UNIV_LOG_ARCHIVE */
  298. /* This parameter is used to throttle the number of insert buffers that are
  299. merged in a batch. By increasing this parameter on a faster disk you can
  300. possibly reduce the number of I/O operations performed to complete the
  301. merge operation. The value of this parameter is used as is by the
  302. background loop when the system is idle (low load), on a busy system
  303. the parameter is scaled down by a factor of 4, this is to avoid putting
  304. a heavier load on the I/O sub system. */
  305. UNIV_INTERN ulong srv_insert_buffer_batch_size = 20;
  306. UNIV_INTERN char* srv_file_flush_method_str = NULL;
  307. UNIV_INTERN ulint srv_unix_file_flush_method = SRV_UNIX_FSYNC;
  308. UNIV_INTERN ulint srv_win_file_flush_method = SRV_WIN_IO_UNBUFFERED;
  309. UNIV_INTERN ulint srv_max_n_open_files = 300;
  310. /* Number of IO operations per second the server can do */
  311. UNIV_INTERN ulong srv_io_capacity = 200;
  312. UNIV_INTERN ulong srv_max_io_capacity = 400;
  313. /* The InnoDB main thread tries to keep the ratio of modified pages
  314. in the buffer pool to all database pages in the buffer pool smaller than
  315. the following number. But it is not guaranteed that the value stays below
  316. that during a time of heavy update/insert activity. */
  317. UNIV_INTERN ulong srv_max_buf_pool_modified_pct = 75;
  318. UNIV_INTERN ulong srv_max_dirty_pages_pct_lwm = 50;
  319. /* This is the percentage of log capacity at which adaptive flushing,
  320. if enabled, will kick in. */
  321. UNIV_INTERN ulong srv_adaptive_flushing_lwm = 10;
  322. /* Number of iterations over which adaptive flushing is averaged. */
  323. UNIV_INTERN ulong srv_flushing_avg_loops = 30;
  324. /* The tid of the cleaner thread */
  325. UNIV_INTERN os_tid_t srv_cleaner_tid;
  326. /* The tid of the LRU manager thread */
  327. UNIV_INTERN os_tid_t srv_lru_manager_tid;
  328. /* The tids of the purge threads */
  329. UNIV_INTERN os_tid_t srv_purge_tids[SRV_MAX_N_PURGE_THREADS];
  330. /* The tids of the I/O threads */
  331. UNIV_INTERN os_tid_t srv_io_tids[SRV_MAX_N_IO_THREADS];
  332. /* The tid of the master thread */
  333. UNIV_INTERN os_tid_t srv_master_tid;
  334. /* The relative scheduling priority of the cleaner and LRU manager threads */
  335. UNIV_INTERN ulint srv_sched_priority_cleaner = 19;
  336. /* The relative scheduling priority of the purge threads */
  337. UNIV_INTERN ulint srv_sched_priority_purge = 19;
  338. /* The relative scheduling priority of the I/O threads */
  339. UNIV_INTERN ulint srv_sched_priority_io = 19;
  340. /* The relative scheduling priority of the master thread */
  341. UNIV_INTERN ulint srv_sched_priority_master = 19;
  342. /* The relative priority of the current thread. If 0, low priority; if 1, high
  343. priority. */
  344. UNIV_INTERN UNIV_THREAD_LOCAL ulint srv_current_thread_priority = 0;
  345. /* The relative priority of the purge coordinator and worker threads. */
  346. UNIV_INTERN my_bool srv_purge_thread_priority = FALSE;
  347. /* The relative priority of the I/O threads. */
  348. UNIV_INTERN my_bool srv_io_thread_priority = FALSE;
  349. /* The relative priority of the cleaner thread. */
  350. UNIV_INTERN my_bool srv_cleaner_thread_priority = FALSE;
  351. /* The relative priority of the master thread. */
  352. UNIV_INTERN my_bool srv_master_thread_priority = FALSE;
  353. /* The number of purge threads to use.*/
  354. UNIV_INTERN ulong srv_n_purge_threads = 1;
  355. /* the number of pages to purge in one batch */
  356. UNIV_INTERN ulong srv_purge_batch_size = 20;
  357. /* Internal setting for "innodb_stats_method". Decides how InnoDB treats
  358. NULL value when collecting statistics. By default, it is set to
  359. SRV_STATS_NULLS_EQUAL(0), ie. all NULL value are treated equal */
  360. UNIV_INTERN ulong srv_innodb_stats_method = SRV_STATS_NULLS_EQUAL;
  361. UNIV_INTERN srv_stats_t srv_stats;
  362. /* structure to pass status variables to MySQL */
  363. UNIV_INTERN export_var_t export_vars;
  364. /** Normally 0. When nonzero, skip some phases of crash recovery,
  365. starting from SRV_FORCE_IGNORE_CORRUPT, so that data can be recovered
  366. by SELECT or mysqldump. When this is nonzero, we do not allow any user
  367. modifications to the data. */
  368. UNIV_INTERN ulong srv_force_recovery;
  369. #ifndef DBUG_OFF
  370. /** Inject a crash at different steps of the recovery process.
  371. This is for testing and debugging only. */
  372. UNIV_INTERN ulong srv_force_recovery_crash;
  373. #endif /* !DBUG_OFF */
  374. /** Print all user-level transactions deadlocks to mysqld stderr */
  375. UNIV_INTERN my_bool srv_print_all_deadlocks = FALSE;
  376. /* Produce a stacktrace on long semaphore wait */
  377. UNIV_INTERN my_bool srv_use_stacktrace = FALSE;
  378. /** Enable INFORMATION_SCHEMA.innodb_cmp_per_index */
  379. UNIV_INTERN my_bool srv_cmp_per_index_enabled = FALSE;
  380. /* If the following is set to 1 then we do not run purge and insert buffer
  381. merge to completion before shutdown. If it is set to 2, do not even flush the
  382. buffer pool to data files at the shutdown: we effectively 'crash'
  383. InnoDB (but lose no committed transactions). */
  384. UNIV_INTERN ulint srv_fast_shutdown = 0;
  385. /* Generate a innodb_status.<pid> file */
  386. UNIV_INTERN ibool srv_innodb_status = FALSE;
  387. /* Optimize prefix index queries to skip cluster index lookup when possible */
  388. /* Enables or disables this prefix optimization. Disabled by default. */
  389. UNIV_INTERN my_bool srv_prefix_index_cluster_optimization = 0;
  390. /* When estimating number of different key values in an index, sample
  391. this many index pages, there are 2 ways to calculate statistics:
  392. * persistent stats that are calculated by ANALYZE TABLE and saved
  393. in the innodb database.
  394. * quick transient stats, that are used if persistent stats for the given
  395. table/index are not found in the innodb database */
  396. UNIV_INTERN unsigned long long srv_stats_transient_sample_pages = 8;
  397. UNIV_INTERN my_bool srv_stats_persistent = TRUE;
  398. UNIV_INTERN unsigned long long srv_stats_persistent_sample_pages = 20;
  399. UNIV_INTERN my_bool srv_stats_auto_recalc = TRUE;
  400. UNIV_INTERN ibool srv_use_doublewrite_buf = TRUE;
  401. /** doublewrite buffer is 1MB is size i.e.: it can hold 128 16K pages.
  402. The following parameter is the size of the buffer that is used for
  403. batch flushing i.e.: LRU flushing and flush_list flushing. The rest
  404. of the pages are used for single page flushing. */
  405. UNIV_INTERN ulong srv_doublewrite_batch_size = 120;
  406. UNIV_INTERN ulong srv_replication_delay = 0;
  407. UNIV_INTERN ulong srv_pass_corrupt_table = 0; /* 0:disable 1:enable */
  408. UNIV_INTERN ulong srv_log_checksum_algorithm =
  409. SRV_CHECKSUM_ALGORITHM_INNODB;
  410. /*-------------------------------------------*/
  411. #ifdef HAVE_MEMORY_BARRIER
  412. /* No idea to wait long with memory barriers */
  413. UNIV_INTERN ulong srv_n_spin_wait_rounds = 15;
  414. #else
  415. UNIV_INTERN ulong srv_n_spin_wait_rounds = 30;
  416. #endif
  417. UNIV_INTERN ulong srv_spin_wait_delay = 6;
  418. UNIV_INTERN ibool srv_priority_boost = TRUE;
  419. #ifdef UNIV_DEBUG
  420. UNIV_INTERN ibool srv_print_thread_releases = FALSE;
  421. UNIV_INTERN ibool srv_print_lock_waits = FALSE;
  422. UNIV_INTERN ibool srv_print_buf_io = FALSE;
  423. UNIV_INTERN ibool srv_print_log_io = FALSE;
  424. UNIV_INTERN ibool srv_print_latch_waits = FALSE;
  425. #endif /* UNIV_DEBUG */
  426. static ulint srv_n_rows_inserted_old = 0;
  427. static ulint srv_n_rows_updated_old = 0;
  428. static ulint srv_n_rows_deleted_old = 0;
  429. static ulint srv_n_rows_read_old = 0;
  430. UNIV_INTERN ulint srv_truncated_status_writes = 0;
  431. UNIV_INTERN ulint srv_available_undo_logs = 0;
  432. UNIV_INTERN ib_uint64_t srv_page_compression_saved = 0;
  433. UNIV_INTERN ib_uint64_t srv_page_compression_trim_sect512 = 0;
  434. UNIV_INTERN ib_uint64_t srv_page_compression_trim_sect4096 = 0;
  435. UNIV_INTERN ib_uint64_t srv_index_pages_written = 0;
  436. UNIV_INTERN ib_uint64_t srv_non_index_pages_written = 0;
  437. UNIV_INTERN ib_uint64_t srv_pages_page_compressed = 0;
  438. UNIV_INTERN ib_uint64_t srv_page_compressed_trim_op = 0;
  439. UNIV_INTERN ib_uint64_t srv_page_compressed_trim_op_saved = 0;
  440. UNIV_INTERN ib_uint64_t srv_index_page_decompressed = 0;
  441. /* Ensure status variables are on separate cache lines */
  442. #define CACHE_LINE_SIZE 64
  443. #define CACHE_ALIGNED __attribute__ ((aligned (CACHE_LINE_SIZE)))
  444. UNIV_INTERN byte
  445. counters_pad_start[CACHE_LINE_SIZE] __attribute__((unused)) = {0};
  446. UNIV_INTERN ulint srv_read_views_memory CACHE_ALIGNED = 0;
  447. UNIV_INTERN ulint srv_descriptors_memory CACHE_ALIGNED = 0;
  448. UNIV_INTERN byte
  449. counters_pad_end[CACHE_LINE_SIZE] __attribute__((unused)) = {0};
  450. /* Set the following to 0 if you want InnoDB to write messages on
  451. stderr on startup/shutdown. */
  452. UNIV_INTERN ibool srv_print_verbose_log = TRUE;
  453. UNIV_INTERN my_bool srv_print_innodb_monitor = FALSE;
  454. UNIV_INTERN my_bool srv_print_innodb_lock_monitor = FALSE;
  455. UNIV_INTERN ibool srv_print_innodb_tablespace_monitor = FALSE;
  456. UNIV_INTERN ibool srv_print_innodb_table_monitor = FALSE;
  457. /** If this flag is set tables without primary key are not allowed */
  458. UNIV_INTERN my_bool srv_force_primary_key = FALSE;
  459. /* Array of English strings describing the current state of an
  460. i/o handler thread */
  461. UNIV_INTERN const char* srv_io_thread_op_info[SRV_MAX_N_IO_THREADS];
  462. UNIV_INTERN const char* srv_io_thread_function[SRV_MAX_N_IO_THREADS];
  463. UNIV_INTERN time_t srv_last_monitor_time;
  464. UNIV_INTERN ib_mutex_t srv_innodb_monitor_mutex;
  465. /* Mutex for locking srv_monitor_file. Not created if srv_read_only_mode */
  466. UNIV_INTERN ib_mutex_t srv_monitor_file_mutex;
  467. #ifdef UNIV_PFS_MUTEX
  468. # ifndef HAVE_ATOMIC_BUILTINS
  469. /* Key to register server_mutex with performance schema */
  470. UNIV_INTERN mysql_pfs_key_t server_mutex_key;
  471. # endif /* !HAVE_ATOMIC_BUILTINS */
  472. /** Key to register srv_innodb_monitor_mutex with performance schema */
  473. UNIV_INTERN mysql_pfs_key_t srv_innodb_monitor_mutex_key;
  474. /** Key to register srv_monitor_file_mutex with performance schema */
  475. UNIV_INTERN mysql_pfs_key_t srv_monitor_file_mutex_key;
  476. /** Key to register srv_dict_tmpfile_mutex with performance schema */
  477. UNIV_INTERN mysql_pfs_key_t srv_dict_tmpfile_mutex_key;
  478. /** Key to register the mutex with performance schema */
  479. UNIV_INTERN mysql_pfs_key_t srv_misc_tmpfile_mutex_key;
  480. /** Key to register srv_sys_t::mutex with performance schema */
  481. UNIV_INTERN mysql_pfs_key_t srv_sys_mutex_key;
  482. /** Key to register srv_sys_t::tasks_mutex with performance schema */
  483. UNIV_INTERN mysql_pfs_key_t srv_sys_tasks_mutex_key;
  484. #endif /* UNIV_PFS_MUTEX */
  485. /** Temporary file for innodb monitor output */
  486. UNIV_INTERN FILE* srv_monitor_file;
  487. /** Mutex for locking srv_dict_tmpfile. Not created if srv_read_only_mode.
  488. This mutex has a very high rank; threads reserving it should not
  489. be holding any InnoDB latches. */
  490. UNIV_INTERN ib_mutex_t srv_dict_tmpfile_mutex;
  491. /** Temporary file for output from the data dictionary */
  492. UNIV_INTERN FILE* srv_dict_tmpfile;
  493. /** Mutex for locking srv_misc_tmpfile. Not created if srv_read_only_mode.
  494. This mutex has a very low rank; threads reserving it should not
  495. acquire any further latches or sleep before releasing this one. */
  496. UNIV_INTERN ib_mutex_t srv_misc_tmpfile_mutex;
  497. /** Temporary file for miscellanous diagnostic output */
  498. UNIV_INTERN FILE* srv_misc_tmpfile;
  499. UNIV_INTERN ulint srv_main_thread_process_no = 0;
  500. UNIV_INTERN ulint srv_main_thread_id = 0;
  501. /* The following counts are used by the srv_master_thread. */
  502. /** Iterations of the loop bounded by 'srv_active' label. */
  503. static ulint srv_main_active_loops = 0;
  504. /** Iterations of the loop bounded by the 'srv_idle' label. */
  505. static ulint srv_main_idle_loops = 0;
  506. /** Iterations of the loop bounded by the 'srv_shutdown' label. */
  507. static ulint srv_main_shutdown_loops = 0;
  508. /** Log writes involving flush. */
  509. static ulint srv_log_writes_and_flush = 0;
  510. /* This is only ever touched by the master thread. It records the
  511. time when the last flush of log file has happened. The master
  512. thread ensures that we flush the log files at least once per
  513. second. */
  514. static time_t srv_last_log_flush_time;
  515. /* Interval in seconds at which various tasks are performed by the
  516. master thread when server is active. In order to balance the workload,
  517. we should try to keep intervals such that they are not multiple of
  518. each other. For example, if we have intervals for various tasks
  519. defined as 5, 10, 15, 60 then all tasks will be performed when
  520. current_time % 60 == 0 and no tasks will be performed when
  521. current_time % 5 != 0. */
  522. # define SRV_MASTER_CHECKPOINT_INTERVAL (7)
  523. # define SRV_MASTER_PURGE_INTERVAL (10)
  524. #ifdef MEM_PERIODIC_CHECK
  525. # define SRV_MASTER_MEM_VALIDATE_INTERVAL (13)
  526. #endif /* MEM_PERIODIC_CHECK */
  527. # define SRV_MASTER_DICT_LRU_INTERVAL (47)
  528. /** Acquire the system_mutex. */
  529. #define srv_sys_mutex_enter() do { \
  530. mutex_enter(&srv_sys->mutex); \
  531. } while (0)
  532. /** Test if the system mutex is owned. */
  533. #define srv_sys_mutex_own() (mutex_own(&srv_sys->mutex) \
  534. && !srv_read_only_mode)
  535. /** Release the system mutex. */
  536. #define srv_sys_mutex_exit() do { \
  537. mutex_exit(&srv_sys->mutex); \
  538. } while (0)
  539. #define fetch_lock_wait_timeout(trx) \
  540. ((trx)->lock.allowed_to_wait \
  541. ? thd_lock_wait_timeout((trx)->mysql_thd) \
  542. : 0)
  543. /** Simulate compression failures. */
  544. UNIV_INTERN uint srv_simulate_comp_failures = 0;
  545. /*
  546. IMPLEMENTATION OF THE SERVER MAIN PROGRAM
  547. =========================================
  548. There is the following analogue between this database
  549. server and an operating system kernel:
  550. DB concept equivalent OS concept
  551. ---------- ---------------------
  552. transaction -- process;
  553. query thread -- thread;
  554. lock -- semaphore;
  555. kernel -- kernel;
  556. query thread execution:
  557. (a) without lock mutex
  558. reserved -- process executing in user mode;
  559. (b) with lock mutex reserved
  560. -- process executing in kernel mode;
  561. The server has several backgroind threads all running at the same
  562. priority as user threads. It periodically checks if here is anything
  563. happening in the server which requires intervention of the master
  564. thread. Such situations may be, for example, when flushing of dirty
  565. blocks is needed in the buffer pool or old version of database rows
  566. have to be cleaned away (purged). The user can configure a separate
  567. dedicated purge thread(s) too, in which case the master thread does not
  568. do any purging.
  569. The threads which we call user threads serve the queries of the MySQL
  570. server. They run at normal priority.
  571. When there is no activity in the system, also the master thread
  572. suspends itself to wait for an event making the server totally silent.
  573. There is still one complication in our server design. If a
  574. background utility thread obtains a resource (e.g., mutex) needed by a user
  575. thread, and there is also some other user activity in the system,
  576. the user thread may have to wait indefinitely long for the
  577. resource, as the OS does not schedule a background thread if
  578. there is some other runnable user thread. This problem is called
  579. priority inversion in real-time programming.
  580. One solution to the priority inversion problem would be to keep record
  581. of which thread owns which resource and in the above case boost the
  582. priority of the background thread so that it will be scheduled and it
  583. can release the resource. This solution is called priority inheritance
  584. in real-time programming. A drawback of this solution is that the overhead
  585. of acquiring a mutex increases slightly, maybe 0.2 microseconds on a 100
  586. MHz Pentium, because the thread has to call os_thread_get_curr_id. This may
  587. be compared to 0.5 microsecond overhead for a mutex lock-unlock pair. Note
  588. that the thread cannot store the information in the resource , say mutex,
  589. itself, because competing threads could wipe out the information if it is
  590. stored before acquiring the mutex, and if it stored afterwards, the
  591. information is outdated for the time of one machine instruction, at least.
  592. (To be precise, the information could be stored to lock_word in mutex if
  593. the machine supports atomic swap.)
  594. The above solution with priority inheritance may become actual in the
  595. future, currently we do not implement any priority twiddling solution.
  596. Our general aim is to reduce the contention of all mutexes by making
  597. them more fine grained.
  598. The thread table contains information of the current status of each
  599. thread existing in the system, and also the event semaphores used in
  600. suspending the master thread and utility threads when they have nothing
  601. to do. The thread table can be seen as an analogue to the process table
  602. in a traditional Unix implementation. */
  603. /** The server system struct */
  604. struct srv_sys_t{
  605. ib_mutex_t tasks_mutex; /*!< variable protecting the
  606. tasks queue */
  607. UT_LIST_BASE_NODE_T(que_thr_t)
  608. tasks; /*!< task queue */
  609. ib_mutex_t mutex; /*!< variable protecting the
  610. fields below. */
  611. ulint n_sys_threads; /*!< size of the sys_threads
  612. array */
  613. srv_slot_t* sys_threads; /*!< server thread table */
  614. ulint n_threads_active[SRV_MASTER + 1];
  615. /*!< number of threads active
  616. in a thread class */
  617. srv_stats_t::ulint_ctr_1_t
  618. activity_count; /*!< For tracking server
  619. activity */
  620. };
  621. #ifndef HAVE_ATOMIC_BUILTINS
  622. /** Mutex protecting some server global variables. */
  623. UNIV_INTERN ib_mutex_t server_mutex;
  624. #endif /* !HAVE_ATOMIC_BUILTINS */
  625. static srv_sys_t* srv_sys = NULL;
  626. /** Event to signal the monitor thread. */
  627. UNIV_INTERN os_event_t srv_monitor_event;
  628. /** Event to signal the error thread */
  629. UNIV_INTERN os_event_t srv_error_event;
  630. /** Event to signal the buffer pool dump/load thread */
  631. UNIV_INTERN os_event_t srv_buf_dump_event;
  632. /** The buffer pool dump/load file name */
  633. UNIV_INTERN char* srv_buf_dump_filename;
  634. /** Boolean config knobs that tell InnoDB to dump the buffer pool at shutdown
  635. and/or load it during startup. */
  636. UNIV_INTERN char srv_buffer_pool_dump_at_shutdown = FALSE;
  637. UNIV_INTERN char srv_buffer_pool_load_at_startup = FALSE;
  638. /** Slot index in the srv_sys->sys_threads array for the purge thread. */
  639. static const ulint SRV_PURGE_SLOT = 1;
  640. /** Slot index in the srv_sys->sys_threads array for the master thread. */
  641. static const ulint SRV_MASTER_SLOT = 0;
  642. UNIV_INTERN os_event_t srv_checkpoint_completed_event;
  643. UNIV_INTERN os_event_t srv_redo_log_tracked_event;
  644. UNIV_INTERN bool srv_redo_log_thread_started = false;
  645. /*********************************************************************//**
  646. Prints counters for work done by srv_master_thread. */
  647. static
  648. void
  649. srv_print_master_thread_info(
  650. /*=========================*/
  651. FILE *file) /* in: output stream */
  652. {
  653. fprintf(file, "srv_master_thread loops: %lu srv_active, "
  654. "%lu srv_shutdown, %lu srv_idle\n",
  655. srv_main_active_loops,
  656. srv_main_shutdown_loops,
  657. srv_main_idle_loops);
  658. fprintf(file, "srv_master_thread log flush and writes: %lu\n",
  659. srv_log_writes_and_flush);
  660. }
  661. /*********************************************************************//**
  662. Sets the info describing an i/o thread current state. */
  663. UNIV_INTERN
  664. void
  665. srv_set_io_thread_op_info(
  666. /*======================*/
  667. ulint i, /*!< in: the 'segment' of the i/o thread */
  668. const char* str) /*!< in: constant char string describing the
  669. state */
  670. {
  671. ut_a(i < SRV_MAX_N_IO_THREADS);
  672. srv_io_thread_op_info[i] = str;
  673. }
  674. /*********************************************************************//**
  675. Resets the info describing an i/o thread current state. */
  676. UNIV_INTERN
  677. void
  678. srv_reset_io_thread_op_info()
  679. /*=========================*/
  680. {
  681. for (ulint i = 0; i < UT_ARR_SIZE(srv_io_thread_op_info); ++i) {
  682. srv_io_thread_op_info[i] = "not started yet";
  683. }
  684. }
  685. #ifdef UNIV_DEBUG
  686. /*********************************************************************//**
  687. Validates the type of a thread table slot.
  688. @return TRUE if ok */
  689. static
  690. ibool
  691. srv_thread_type_validate(
  692. /*=====================*/
  693. srv_thread_type type) /*!< in: thread type */
  694. {
  695. switch (type) {
  696. case SRV_NONE:
  697. break;
  698. case SRV_WORKER:
  699. case SRV_PURGE:
  700. case SRV_MASTER:
  701. return(TRUE);
  702. }
  703. ut_error;
  704. return(FALSE);
  705. }
  706. #endif /* UNIV_DEBUG */
  707. /*********************************************************************//**
  708. Gets the type of a thread table slot.
  709. @return thread type */
  710. static
  711. srv_thread_type
  712. srv_slot_get_type(
  713. /*==============*/
  714. const srv_slot_t* slot) /*!< in: thread slot */
  715. {
  716. srv_thread_type type = slot->type;
  717. ut_ad(srv_thread_type_validate(type));
  718. return(type);
  719. }
  720. /*********************************************************************//**
  721. Reserves a slot in the thread table for the current thread.
  722. @return reserved slot */
  723. static
  724. srv_slot_t*
  725. srv_reserve_slot(
  726. /*=============*/
  727. srv_thread_type type) /*!< in: type of the thread */
  728. {
  729. srv_slot_t* slot = 0;
  730. srv_sys_mutex_enter();
  731. ut_ad(srv_thread_type_validate(type));
  732. switch (type) {
  733. case SRV_MASTER:
  734. slot = &srv_sys->sys_threads[SRV_MASTER_SLOT];
  735. break;
  736. case SRV_PURGE:
  737. slot = &srv_sys->sys_threads[SRV_PURGE_SLOT];
  738. break;
  739. case SRV_WORKER:
  740. /* Find an empty slot, skip the master and purge slots. */
  741. for (slot = &srv_sys->sys_threads[2];
  742. slot->in_use;
  743. ++slot) {
  744. ut_a(slot < &srv_sys->sys_threads[
  745. srv_sys->n_sys_threads]);
  746. }
  747. break;
  748. case SRV_NONE:
  749. ut_error;
  750. }
  751. ut_a(!slot->in_use);
  752. slot->in_use = TRUE;
  753. slot->suspended = FALSE;
  754. slot->type = type;
  755. ut_ad(srv_slot_get_type(slot) == type);
  756. ++srv_sys->n_threads_active[type];
  757. srv_sys_mutex_exit();
  758. return(slot);
  759. }
  760. /*********************************************************************//**
  761. Suspends the calling thread to wait for the event in its thread slot.
  762. @return the current signal count of the event. */
  763. static
  764. ib_int64_t
  765. srv_suspend_thread_low(
  766. /*===================*/
  767. srv_slot_t* slot) /*!< in/out: thread slot */
  768. {
  769. ut_ad(!srv_read_only_mode);
  770. ut_ad(srv_sys_mutex_own());
  771. ut_ad(slot->in_use);
  772. srv_thread_type type = srv_slot_get_type(slot);
  773. switch (type) {
  774. case SRV_NONE:
  775. ut_error;
  776. case SRV_MASTER:
  777. /* We have only one master thread and it
  778. should be the first entry always. */
  779. ut_a(srv_sys->n_threads_active[type] == 1);
  780. break;
  781. case SRV_PURGE:
  782. /* We have only one purge coordinator thread
  783. and it should be the second entry always. */
  784. ut_a(srv_sys->n_threads_active[type] == 1);
  785. break;
  786. case SRV_WORKER:
  787. ut_a(srv_n_purge_threads > 1);
  788. ut_a(srv_sys->n_threads_active[type] > 0);
  789. break;
  790. }
  791. ut_a(!slot->suspended);
  792. slot->suspended = TRUE;
  793. ut_a(srv_sys->n_threads_active[type] > 0);
  794. srv_sys->n_threads_active[type]--;
  795. return(os_event_reset(slot->event));
  796. }
  797. /*********************************************************************//**
  798. Suspends the calling thread to wait for the event in its thread slot.
  799. @return the current signal count of the event. */
  800. static
  801. ib_int64_t
  802. srv_suspend_thread(
  803. /*===============*/
  804. srv_slot_t* slot) /*!< in/out: thread slot */
  805. {
  806. srv_sys_mutex_enter();
  807. ib_int64_t sig_count = srv_suspend_thread_low(slot);
  808. srv_sys_mutex_exit();
  809. return(sig_count);
  810. }
  811. /*********************************************************************//**
  812. Releases threads of the type given from suspension in the thread table.
  813. NOTE! The server mutex has to be reserved by the caller!
  814. @return number of threads released: this may be less than n if not
  815. enough threads were suspended at the moment. */
  816. UNIV_INTERN
  817. ulint
  818. srv_release_threads(
  819. /*================*/
  820. srv_thread_type type, /*!< in: thread type */
  821. ulint n) /*!< in: number of threads to release */
  822. {
  823. ulint i;
  824. ulint count = 0;
  825. ut_ad(srv_thread_type_validate(type));
  826. ut_ad(n > 0);
  827. srv_sys_mutex_enter();
  828. for (i = 0; i < srv_sys->n_sys_threads; i++) {
  829. srv_slot_t* slot;
  830. slot = &srv_sys->sys_threads[i];
  831. if (slot->in_use
  832. && srv_slot_get_type(slot) == type
  833. && slot->suspended) {
  834. switch (type) {
  835. case SRV_NONE:
  836. ut_error;
  837. case SRV_MASTER:
  838. /* We have only one master thread and it
  839. should be the first entry always. */
  840. ut_a(n == 1);
  841. ut_a(i == SRV_MASTER_SLOT);
  842. ut_a(srv_sys->n_threads_active[type] == 0);
  843. break;
  844. case SRV_PURGE:
  845. /* We have only one purge coordinator thread
  846. and it should be the second entry always. */
  847. ut_a(n == 1);
  848. ut_a(i == SRV_PURGE_SLOT);
  849. ut_a(srv_n_purge_threads > 0);
  850. ut_a(srv_sys->n_threads_active[type] == 0);
  851. break;
  852. case SRV_WORKER:
  853. ut_a(srv_n_purge_threads > 1);
  854. ut_a(srv_sys->n_threads_active[type]
  855. < srv_n_purge_threads - 1);
  856. break;
  857. }
  858. slot->suspended = FALSE;
  859. ++srv_sys->n_threads_active[type];
  860. os_event_set(slot->event);
  861. if (++count == n) {
  862. break;
  863. }
  864. }
  865. }
  866. srv_sys_mutex_exit();
  867. return(count);
  868. }
  869. /*********************************************************************//**
  870. Release a thread's slot. */
  871. static
  872. void
  873. srv_free_slot(
  874. /*==========*/
  875. srv_slot_t* slot) /*!< in/out: thread slot */
  876. {
  877. srv_sys_mutex_enter();
  878. if (!slot->suspended) {
  879. /* Mark the thread as inactive. */
  880. srv_suspend_thread_low(slot);
  881. }
  882. /* Free the slot for reuse. */
  883. ut_ad(slot->in_use);
  884. slot->in_use = FALSE;
  885. srv_sys_mutex_exit();
  886. }
  887. /*********************************************************************//**
  888. Initializes the server. */
  889. UNIV_INTERN
  890. void
  891. srv_init(void)
  892. /*==========*/
  893. {
  894. ulint n_sys_threads = 0;
  895. ulint srv_sys_sz = sizeof(*srv_sys);
  896. #ifndef HAVE_ATOMIC_BUILTINS
  897. mutex_create(server_mutex_key, &server_mutex, SYNC_ANY_LATCH);
  898. #endif /* !HAVE_ATOMIC_BUILTINS */
  899. mutex_create(srv_innodb_monitor_mutex_key,
  900. &srv_innodb_monitor_mutex, SYNC_NO_ORDER_CHECK);
  901. if (!srv_read_only_mode) {
  902. /* Number of purge threads + master thread */
  903. n_sys_threads = srv_n_purge_threads + 1;
  904. srv_sys_sz += n_sys_threads * sizeof(*srv_sys->sys_threads);
  905. }
  906. srv_sys = static_cast<srv_sys_t*>(mem_zalloc(srv_sys_sz));
  907. srv_sys->n_sys_threads = n_sys_threads;
  908. if (!srv_read_only_mode) {
  909. mutex_create(srv_sys_mutex_key, &srv_sys->mutex, SYNC_THREADS);
  910. mutex_create(srv_sys_tasks_mutex_key,
  911. &srv_sys->tasks_mutex, SYNC_ANY_LATCH);
  912. srv_sys->sys_threads = (srv_slot_t*) &srv_sys[1];
  913. for (ulint i = 0; i < srv_sys->n_sys_threads; ++i) {
  914. srv_slot_t* slot = &srv_sys->sys_threads[i];
  915. slot->event = os_event_create();
  916. ut_a(slot->event);
  917. }
  918. srv_error_event = os_event_create();
  919. srv_monitor_event = os_event_create();
  920. srv_buf_dump_event = os_event_create();
  921. srv_checkpoint_completed_event = os_event_create();
  922. if (srv_track_changed_pages) {
  923. srv_redo_log_tracked_event = os_event_create();
  924. os_event_set(srv_redo_log_tracked_event);
  925. }
  926. UT_LIST_INIT(srv_sys->tasks);
  927. }
  928. /* page_zip_stat_per_index_mutex is acquired from:
  929. 1. page_zip_compress() (after SYNC_FSP)
  930. 2. page_zip_decompress()
  931. 3. i_s_cmp_per_index_fill_low() (where SYNC_DICT is acquired)
  932. 4. innodb_cmp_per_index_update(), no other latches
  933. since we do not acquire any other latches while holding this mutex,
  934. it can have very low level. We pick SYNC_ANY_LATCH for it. */
  935. mutex_create(
  936. page_zip_stat_per_index_mutex_key,
  937. &page_zip_stat_per_index_mutex, SYNC_ANY_LATCH);
  938. /* Create dummy indexes for infimum and supremum records */
  939. dict_ind_init();
  940. srv_conc_init();
  941. #ifdef WITH_INNODB_DISALLOW_WRITES
  942. /* Writes have to be enabled on init or else we hang. Thus, we
  943. always set the event here regardless of innobase_disallow_writes.
  944. That flag will always be 0 at this point because it isn't settable
  945. via my.cnf or command line arg. */
  946. srv_allow_writes_event = os_event_create();
  947. os_event_set(srv_allow_writes_event);
  948. #endif /* WITH_INNODB_DISALLOW_WRITES */
  949. /* Initialize some INFORMATION SCHEMA internal structures */
  950. trx_i_s_cache_init(trx_i_s_cache);
  951. ut_crc32_init();
  952. }
  953. /*********************************************************************//**
  954. Frees the data structures created in srv_init(). */
  955. UNIV_INTERN
  956. void
  957. srv_free(void)
  958. /*==========*/
  959. {
  960. srv_conc_free();
  961. /* The mutexes srv_sys->mutex and srv_sys->tasks_mutex should have
  962. been freed by sync_close() already. */
  963. mem_free(srv_sys);
  964. srv_sys = NULL;
  965. trx_i_s_cache_free(trx_i_s_cache);
  966. if (!srv_read_only_mode) {
  967. os_event_free(srv_buf_dump_event);
  968. srv_buf_dump_event = NULL;
  969. }
  970. }
  971. /*********************************************************************//**
  972. Initializes the synchronization primitives, memory system, and the thread
  973. local storage. */
  974. UNIV_INTERN
  975. void
  976. srv_general_init(void)
  977. /*==================*/
  978. {
  979. ut_mem_init();
  980. /* Reset the system variables in the recovery module. */
  981. recv_sys_var_init();
  982. os_sync_init();
  983. sync_init();
  984. mem_init(srv_mem_pool_size);
  985. que_init();
  986. row_mysql_init();
  987. }
  988. /*********************************************************************//**
  989. Normalizes init parameter values to use units we use inside InnoDB. */
  990. static
  991. void
  992. srv_normalize_init_values(void)
  993. /*===========================*/
  994. {
  995. ulint n;
  996. ulint i;
  997. n = srv_n_data_files;
  998. for (i = 0; i < n; i++) {
  999. srv_data_file_sizes[i] = srv_data_file_sizes[i]
  1000. * ((1024 * 1024) / UNIV_PAGE_SIZE);
  1001. }
  1002. srv_last_file_size_max = srv_last_file_size_max
  1003. * ((1024 * 1024) / UNIV_PAGE_SIZE);
  1004. srv_log_file_size = srv_log_file_size / UNIV_PAGE_SIZE;
  1005. srv_log_buffer_size = srv_log_buffer_size / UNIV_PAGE_SIZE;
  1006. srv_lock_table_size = 5 * (srv_buf_pool_size / UNIV_PAGE_SIZE);
  1007. }
  1008. /*********************************************************************//**
  1009. Boots the InnoDB server. */
  1010. UNIV_INTERN
  1011. void
  1012. srv_boot(void)
  1013. /*==========*/
  1014. {
  1015. /* Transform the init parameter values given by MySQL to
  1016. use units we use inside InnoDB: */
  1017. srv_normalize_init_values();
  1018. /* Initialize synchronization primitives, memory management, and thread
  1019. local storage */
  1020. srv_general_init();
  1021. /* Initialize this module */
  1022. srv_init();
  1023. srv_mon_create();
  1024. }
  1025. /******************************************************************//**
  1026. Refreshes the values used to calculate per-second averages. */
  1027. static
  1028. void
  1029. srv_refresh_innodb_monitor_stats(void)
  1030. /*==================================*/
  1031. {
  1032. mutex_enter(&srv_innodb_monitor_mutex);
  1033. srv_last_monitor_time = time(NULL);
  1034. os_aio_refresh_stats();
  1035. btr_cur_n_sea_old = btr_cur_n_sea;
  1036. btr_cur_n_non_sea_old = btr_cur_n_non_sea;
  1037. log_refresh_stats();
  1038. buf_refresh_io_stats_all();
  1039. srv_n_rows_inserted_old = srv_stats.n_rows_inserted;
  1040. srv_n_rows_updated_old = srv_stats.n_rows_updated;
  1041. srv_n_rows_deleted_old = srv_stats.n_rows_deleted;
  1042. srv_n_rows_read_old = srv_stats.n_rows_read;
  1043. mutex_exit(&srv_innodb_monitor_mutex);
  1044. }
  1045. /******************************************************************//**
  1046. Outputs to a file the output of the InnoDB Monitor.
  1047. @return FALSE if not all information printed
  1048. due to failure to obtain necessary mutex */
  1049. UNIV_INTERN
  1050. ibool
  1051. srv_printf_innodb_monitor(
  1052. /*======================*/
  1053. FILE* file, /*!< in: output stream */
  1054. ibool nowait, /*!< in: whether to wait for the
  1055. lock_sys_t:: mutex */
  1056. ulint* trx_start_pos, /*!< out: file position of the start of
  1057. the list of active transactions */
  1058. ulint* trx_end) /*!< out: file position of the end of
  1059. the list of active transactions */
  1060. {
  1061. double time_elapsed;
  1062. time_t current_time;
  1063. ulint n_reserved;
  1064. ibool ret;
  1065. ulong btr_search_sys_constant;
  1066. ulong btr_search_sys_variable;
  1067. ulint lock_sys_subtotal;
  1068. ulint recv_sys_subtotal;
  1069. ulint i;
  1070. trx_t* trx;
  1071. mutex_enter(&srv_innodb_monitor_mutex);
  1072. current_time = time(NULL);
  1073. /* We add 0.001 seconds to time_elapsed to prevent division
  1074. by zero if two users happen to call SHOW ENGINE INNODB STATUS at the
  1075. same time */
  1076. time_elapsed = difftime(current_time, srv_last_monitor_time)
  1077. + 0.001;
  1078. srv_last_monitor_time = time(NULL);
  1079. fputs("\n=====================================\n", file);
  1080. ut_print_timestamp(file);
  1081. fprintf(file,
  1082. " INNODB MONITOR OUTPUT\n"
  1083. "=====================================\n"
  1084. "Per second averages calculated from the last %lu seconds\n",
  1085. (ulong) time_elapsed);
  1086. fputs("-----------------\n"
  1087. "BACKGROUND THREAD\n"
  1088. "-----------------\n", file);
  1089. srv_print_master_thread_info(file);
  1090. fputs("----------\n"
  1091. "SEMAPHORES\n"
  1092. "----------\n", file);
  1093. sync_print(file);
  1094. /* Conceptually, srv_innodb_monitor_mutex has a very high latching
  1095. order level in sync0sync.h, while dict_foreign_err_mutex has a very
  1096. low level 135. Therefore we can reserve the latter mutex here without
  1097. a danger of a deadlock of threads. */
  1098. mutex_enter(&dict_foreign_err_mutex);
  1099. if (!srv_read_only_mode && ftell(dict_foreign_err_file) != 0L) {
  1100. fputs("------------------------\n"
  1101. "LATEST FOREIGN KEY ERROR\n"
  1102. "------------------------\n", file);
  1103. ut_copy_file(file, dict_foreign_err_file);
  1104. }
  1105. mutex_exit(&dict_foreign_err_mutex);
  1106. /* Only if lock_print_info_summary proceeds correctly,
  1107. before we call the lock_print_info_all_transactions
  1108. to print all the lock information. IMPORTANT NOTE: This
  1109. function acquires the lock mutex on success. */
  1110. ret = lock_print_info_summary(file, nowait);
  1111. if (ret) {
  1112. if (trx_start_pos) {
  1113. long t = ftell(file);
  1114. if (t < 0) {
  1115. *trx_start_pos = ULINT_UNDEFINED;
  1116. } else {
  1117. *trx_start_pos = (ulint) t;
  1118. }
  1119. }
  1120. /* NOTE: If we get here then we have the lock mutex. This
  1121. function will release the lock mutex that we acquired when
  1122. we called the lock_print_info_summary() function earlier. */
  1123. lock_print_info_all_transactions(file);
  1124. if (trx_end) {
  1125. long t = ftell(file);
  1126. if (t < 0) {
  1127. *trx_end = ULINT_UNDEFINED;
  1128. } else {
  1129. *trx_end = (ulint) t;
  1130. }
  1131. }
  1132. }
  1133. fputs("--------\n"
  1134. "FILE I/O\n"
  1135. "--------\n", file);
  1136. os_aio_print(file);
  1137. fputs("-------------------------------------\n"
  1138. "INSERT BUFFER AND ADAPTIVE HASH INDEX\n"
  1139. "-------------------------------------\n", file);
  1140. ibuf_print(file);
  1141. fprintf(file,
  1142. "%.2f hash searches/s, %.2f non-hash searches/s\n",
  1143. (btr_cur_n_sea - btr_cur_n_sea_old)
  1144. / time_elapsed,
  1145. (btr_cur_n_non_sea - btr_cur_n_non_sea_old)
  1146. / time_elapsed);
  1147. btr_cur_n_sea_old = btr_cur_n_sea;
  1148. btr_cur_n_non_sea_old = btr_cur_n_non_sea;
  1149. fputs("---\n"
  1150. "LOG\n"
  1151. "---\n", file);
  1152. log_print(file);
  1153. fputs("----------------------\n"
  1154. "BUFFER POOL AND MEMORY\n"
  1155. "----------------------\n", file);
  1156. fprintf(file,
  1157. "Total memory allocated " ULINTPF
  1158. "; in additional pool allocated " ULINTPF "\n",
  1159. ut_total_allocated_memory,
  1160. mem_pool_get_reserved(mem_comm_pool));
  1161. fprintf(file,
  1162. "Total memory allocated by read views " ULINTPF "\n",
  1163. os_atomic_increment_ulint(&srv_read_views_memory, 0));
  1164. /* Calculate AHI constant and variable memory allocations */
  1165. btr_search_sys_constant = 0;
  1166. btr_search_sys_variable = 0;
  1167. ut_ad(btr_search_sys->hash_tables);
  1168. for (i = 0; i < btr_search_index_num; i++) {
  1169. hash_table_t* ht = btr_search_sys->hash_tables[i];
  1170. ut_ad(ht);
  1171. ut_ad(ht->heap);
  1172. /* Multiple mutexes/heaps are currently never used for adaptive
  1173. hash index tables. */
  1174. ut_ad(!ht->n_sync_obj);
  1175. ut_ad(!ht->heaps);
  1176. btr_search_sys_variable += mem_heap_get_size(ht->heap);
  1177. btr_search_sys_constant += ht->n_cells * sizeof(hash_cell_t);
  1178. }
  1179. lock_sys_subtotal = 0;
  1180. if (trx_sys) {
  1181. mutex_enter(&trx_sys->mutex);
  1182. trx = UT_LIST_GET_FIRST(trx_sys->mysql_trx_list);
  1183. while (trx) {
  1184. lock_sys_subtotal
  1185. += ((trx->lock.lock_heap)
  1186. ? mem_heap_get_size(trx->lock.lock_heap)
  1187. : 0);
  1188. trx = UT_LIST_GET_NEXT(mysql_trx_list, trx);
  1189. }
  1190. mutex_exit(&trx_sys->mutex);
  1191. }
  1192. recv_sys_subtotal = ((recv_sys && recv_sys->addr_hash)
  1193. ? mem_heap_get_size(recv_sys->heap) : 0);
  1194. fprintf(file,
  1195. "Internal hash tables (constant factor + variable factor)\n"
  1196. " Adaptive hash index %lu \t(%lu + " ULINTPF ")\n"
  1197. " Page hash %lu (buffer pool 0 only)\n"
  1198. " Dictionary cache %lu \t(%lu + " ULINTPF ")\n"
  1199. " File system %lu \t(%lu + " ULINTPF ")\n"
  1200. " Lock system %lu \t(%lu + " ULINTPF ")\n"
  1201. " Recovery system %lu \t(%lu + " ULINTPF ")\n",
  1202. btr_search_sys_constant + btr_search_sys_variable,
  1203. btr_search_sys_constant,
  1204. btr_search_sys_variable,
  1205. (ulong) (buf_pool_from_array(0)->page_hash->n_cells * sizeof(hash_cell_t)),
  1206. (ulong) (dict_sys ? ((dict_sys->table_hash->n_cells
  1207. + dict_sys->table_id_hash->n_cells
  1208. ) * sizeof(hash_cell_t)
  1209. + dict_sys->size) : 0),
  1210. (ulong) (dict_sys ? ((dict_sys->table_hash->n_cells
  1211. + dict_sys->table_id_hash->n_cells
  1212. ) * sizeof(hash_cell_t)) : 0),
  1213. dict_sys ? (dict_sys->size) : 0,
  1214. (ulong) (fil_system_hash_cells() * sizeof(hash_cell_t)
  1215. + fil_system_hash_nodes()),
  1216. (ulong) (fil_system_hash_cells() * sizeof(hash_cell_t)),
  1217. fil_system_hash_nodes(),
  1218. (ulong) ((lock_sys ? (lock_sys->rec_hash->n_cells * sizeof(hash_cell_t)) : 0)
  1219. + lock_sys_subtotal),
  1220. (ulong) (lock_sys ? (lock_sys->rec_hash->n_cells * sizeof(hash_cell_t)) : 0),
  1221. lock_sys_subtotal,
  1222. (ulong) (((recv_sys && recv_sys->addr_hash)
  1223. ? (recv_sys->addr_hash->n_cells * sizeof(hash_cell_t)) : 0)
  1224. + recv_sys_subtotal),
  1225. (ulong) ((recv_sys && recv_sys->addr_hash)
  1226. ? (recv_sys->addr_hash->n_cells * sizeof(hash_cell_t)) : 0),
  1227. recv_sys_subtotal);
  1228. fprintf(file, "Dictionary memory allocated " ULINTPF "\n",
  1229. dict_sys->size);
  1230. buf_print_io(file);
  1231. fputs("--------------\n"
  1232. "ROW OPERATIONS\n"
  1233. "--------------\n", file);
  1234. fprintf(file, "%ld queries inside InnoDB, %lu queries in queue\n",
  1235. (long) srv_conc_get_active_threads(),
  1236. srv_conc_get_waiting_threads());
  1237. mutex_enter(&trx_sys->mutex);
  1238. fprintf(file, "%lu read views open inside InnoDB\n",
  1239. UT_LIST_GET_LEN(trx_sys->view_list));
  1240. fprintf(file, "%lu RW transactions active inside InnoDB\n",
  1241. UT_LIST_GET_LEN(trx_sys->rw_trx_list));
  1242. fprintf(file, "%lu RO transactions active inside InnoDB\n",
  1243. UT_LIST_GET_LEN(trx_sys->ro_trx_list));
  1244. fprintf(file, "%lu out of %lu descriptors used\n",
  1245. trx_sys->descr_n_used, trx_sys->descr_n_max);
  1246. if (UT_LIST_GET_LEN(trx_sys->view_list)) {
  1247. read_view_t* view = UT_LIST_GET_LAST(trx_sys->view_list);
  1248. if (view) {
  1249. fprintf(file, "---OLDEST VIEW---\n");
  1250. read_view_print(file, view);
  1251. fprintf(file, "-----------------\n");
  1252. }
  1253. }
  1254. mutex_exit(&trx_sys->mutex);
  1255. n_reserved = fil_space_get_n_reserved_extents(0);
  1256. if (n_reserved > 0) {
  1257. fprintf(file,
  1258. "%lu tablespace extents now reserved for"
  1259. " B-tree split operations\n",
  1260. (ulong) n_reserved);
  1261. }
  1262. #ifdef UNIV_LINUX
  1263. fprintf(file, "Main thread process no. %lu, id %lu, state: %s\n",
  1264. (ulong) srv_main_thread_process_no,
  1265. (ulong) srv_main_thread_id,
  1266. srv_main_thread_op_info);
  1267. #else
  1268. fprintf(file, "Main thread id %lu, state: %s\n",
  1269. (ulong) srv_main_thread_id,
  1270. srv_main_thread_op_info);
  1271. #endif
  1272. fprintf(file,
  1273. "Number of rows inserted " ULINTPF
  1274. ", updated " ULINTPF ", deleted " ULINTPF
  1275. ", read " ULINTPF "\n",
  1276. (ulint) srv_stats.n_rows_inserted,
  1277. (ulint) srv_stats.n_rows_updated,
  1278. (ulint) srv_stats.n_rows_deleted,
  1279. (ulint) srv_stats.n_rows_read);
  1280. fprintf(file,
  1281. "%.2f inserts/s, %.2f updates/s,"
  1282. " %.2f deletes/s, %.2f reads/s\n",
  1283. ((ulint) srv_stats.n_rows_inserted - srv_n_rows_inserted_old)
  1284. / time_elapsed,
  1285. ((ulint) srv_stats.n_rows_updated - srv_n_rows_updated_old)
  1286. / time_elapsed,
  1287. ((ulint) srv_stats.n_rows_deleted - srv_n_rows_deleted_old)
  1288. / time_elapsed,
  1289. ((ulint) srv_stats.n_rows_read - srv_n_rows_read_old)
  1290. / time_elapsed);
  1291. srv_n_rows_inserted_old = srv_stats.n_rows_inserted;
  1292. srv_n_rows_updated_old = srv_stats.n_rows_updated;
  1293. srv_n_rows_deleted_old = srv_stats.n_rows_deleted;
  1294. srv_n_rows_read_old = srv_stats.n_rows_read;
  1295. /* Only if lock_print_info_summary proceeds correctly,
  1296. before we call the lock_print_info_all_transactions
  1297. to print all the lock information. */
  1298. ret = lock_print_info_summary(file, nowait);
  1299. if (ret) {
  1300. lock_print_info_all_transactions(file);
  1301. }
  1302. fputs("----------------------------\n"
  1303. "END OF INNODB MONITOR OUTPUT\n"
  1304. "============================\n", file);
  1305. mutex_exit(&srv_innodb_monitor_mutex);
  1306. fflush(file);
  1307. return(ret);
  1308. }
  1309. /******************************************************************//**
  1310. Function to pass InnoDB status variables to MySQL */
  1311. UNIV_INTERN
  1312. void
  1313. srv_export_innodb_status(void)
  1314. /*==========================*/
  1315. {
  1316. buf_pool_stat_t stat;
  1317. buf_pools_list_size_t buf_pools_list_size;
  1318. ulint LRU_len;
  1319. ulint free_len;
  1320. ulint flush_list_len;
  1321. ulint mem_adaptive_hash, mem_dictionary;
  1322. read_view_t* oldest_view;
  1323. ulint i;
  1324. buf_get_total_stat(&stat);
  1325. buf_get_total_list_len(&LRU_len, &free_len, &flush_list_len);
  1326. buf_get_total_list_size_in_bytes(&buf_pools_list_size);
  1327. mem_adaptive_hash = 0;
  1328. ut_ad(btr_search_sys->hash_tables);
  1329. for (i = 0; i < btr_search_index_num; i++) {
  1330. hash_table_t* ht = btr_search_sys->hash_tables[i];
  1331. ut_ad(ht);
  1332. ut_ad(ht->heap);
  1333. /* Multiple mutexes/heaps are currently never used for adaptive
  1334. hash index tables. */
  1335. ut_ad(!ht->n_sync_obj);
  1336. ut_ad(!ht->heaps);
  1337. mem_adaptive_hash += mem_heap_get_size(ht->heap);
  1338. mem_adaptive_hash += ht->n_cells * sizeof(hash_cell_t);
  1339. }
  1340. mem_dictionary = (dict_sys ? ((dict_sys->table_hash->n_cells
  1341. + dict_sys->table_id_hash->n_cells
  1342. ) * sizeof(hash_cell_t)
  1343. + dict_sys->size) : 0);
  1344. mutex_enter(&srv_innodb_monitor_mutex);
  1345. export_vars.innodb_data_pending_reads =
  1346. os_n_pending_reads;
  1347. export_vars.innodb_data_pending_writes =
  1348. os_n_pending_writes;
  1349. export_vars.innodb_data_pending_fsyncs =
  1350. fil_n_pending_log_flushes
  1351. + fil_n_pending_tablespace_flushes;
  1352. export_vars.innodb_adaptive_hash_hash_searches
  1353. = btr_cur_n_sea;
  1354. export_vars.innodb_adaptive_hash_non_hash_searches
  1355. = btr_cur_n_non_sea;
  1356. export_vars.innodb_background_log_sync
  1357. = srv_log_writes_and_flush;
  1358. export_vars.innodb_data_fsyncs = os_n_fsyncs;
  1359. export_vars.innodb_data_read = srv_stats.data_read;
  1360. export_vars.innodb_data_reads = os_n_file_reads;
  1361. export_vars.innodb_data_writes = os_n_file_writes;
  1362. export_vars.innodb_data_written = srv_stats.data_written;
  1363. export_vars.innodb_buffer_pool_read_requests = stat.n_page_gets;
  1364. export_vars.innodb_buffer_pool_write_requests =
  1365. srv_stats.buf_pool_write_requests;
  1366. export_vars.innodb_buffer_pool_wait_free =
  1367. srv_stats.buf_pool_wait_free;
  1368. export_vars.innodb_buffer_pool_pages_flushed =
  1369. srv_stats.buf_pool_flushed;
  1370. export_vars.innodb_buffer_pool_reads = srv_stats.buf_pool_reads;
  1371. export_vars.innodb_buffer_pool_read_ahead_rnd =
  1372. stat.n_ra_pages_read_rnd;
  1373. export_vars.innodb_buffer_pool_read_ahead =
  1374. stat.n_ra_pages_read;
  1375. export_vars.innodb_buffer_pool_read_ahead_evicted =
  1376. stat.n_ra_pages_evicted;
  1377. export_vars.innodb_buffer_pool_pages_LRU_flushed =
  1378. stat.buf_lru_flush_page_count;
  1379. export_vars.innodb_buffer_pool_pages_data = LRU_len;
  1380. export_vars.innodb_buffer_pool_bytes_data =
  1381. buf_pools_list_size.LRU_bytes
  1382. + buf_pools_list_size.unzip_LRU_bytes;
  1383. export_vars.innodb_buffer_pool_pages_dirty = flush_list_len;
  1384. export_vars.innodb_buffer_pool_bytes_dirty =
  1385. buf_pools_list_size.flush_list_bytes;
  1386. export_vars.innodb_buffer_pool_pages_free = free_len;
  1387. export_vars.innodb_deadlocks = srv_stats.lock_deadlock_count;
  1388. #ifdef UNIV_DEBUG
  1389. export_vars.innodb_buffer_pool_pages_latched =
  1390. buf_get_latched_pages_number();
  1391. #endif /* UNIV_DEBUG */
  1392. export_vars.innodb_buffer_pool_pages_total = buf_pool_get_n_pages();
  1393. export_vars.innodb_buffer_pool_pages_misc =
  1394. buf_pool_get_n_pages() - LRU_len - free_len;
  1395. export_vars.innodb_buffer_pool_pages_made_young
  1396. = stat.n_pages_made_young;
  1397. export_vars.innodb_buffer_pool_pages_made_not_young
  1398. = stat.n_pages_not_made_young;
  1399. export_vars.innodb_buffer_pool_pages_old = 0;
  1400. for (i = 0; i < srv_buf_pool_instances; i++) {
  1401. buf_pool_t* buf_pool = buf_pool_from_array(i);
  1402. export_vars.innodb_buffer_pool_pages_old
  1403. += buf_pool->LRU_old_len;
  1404. }
  1405. export_vars.innodb_checkpoint_age
  1406. = (log_sys->lsn - log_sys->last_checkpoint_lsn);
  1407. export_vars.innodb_checkpoint_max_age
  1408. = log_sys->max_checkpoint_age;
  1409. export_vars.innodb_history_list_length
  1410. = trx_sys->rseg_history_len;
  1411. ibuf_export_ibuf_status(
  1412. &export_vars.innodb_ibuf_size,
  1413. &export_vars.innodb_ibuf_free_list,
  1414. &export_vars.innodb_ibuf_segment_size,
  1415. &export_vars.innodb_ibuf_merges,
  1416. &export_vars.innodb_ibuf_merged_inserts,
  1417. &export_vars.innodb_ibuf_merged_delete_marks,
  1418. &export_vars.innodb_ibuf_merged_deletes,
  1419. &export_vars.innodb_ibuf_discarded_inserts,
  1420. &export_vars.innodb_ibuf_discarded_delete_marks,
  1421. &export_vars.innodb_ibuf_discarded_deletes);
  1422. export_vars.innodb_lsn_current
  1423. = log_sys->lsn;
  1424. export_vars.innodb_lsn_flushed
  1425. = log_sys->flushed_to_disk_lsn;
  1426. export_vars.innodb_lsn_last_checkpoint
  1427. = log_sys->last_checkpoint_lsn;
  1428. export_vars.innodb_master_thread_active_loops
  1429. = srv_main_active_loops;
  1430. export_vars.innodb_master_thread_idle_loops
  1431. = srv_main_idle_loops;
  1432. export_vars.innodb_max_trx_id
  1433. = trx_sys->max_trx_id;
  1434. export_vars.innodb_mem_adaptive_hash
  1435. = mem_adaptive_hash;
  1436. export_vars.innodb_mem_dictionary
  1437. = mem_dictionary;
  1438. export_vars.innodb_mem_total
  1439. = ut_total_allocated_memory;
  1440. export_vars.innodb_mutex_os_waits
  1441. = mutex_os_wait_count;
  1442. export_vars.innodb_mutex_spin_rounds
  1443. = mutex_spin_round_count;
  1444. export_vars.innodb_mutex_spin_waits
  1445. = mutex_spin_wait_count;
  1446. export_vars.innodb_s_lock_os_waits
  1447. = rw_lock_stats.rw_s_os_wait_count;
  1448. export_vars.innodb_s_lock_spin_rounds
  1449. = rw_lock_stats.rw_s_spin_round_count;
  1450. export_vars.innodb_s_lock_spin_waits
  1451. = rw_lock_stats.rw_s_spin_wait_count;
  1452. export_vars.innodb_x_lock_os_waits
  1453. = rw_lock_stats.rw_x_os_wait_count;
  1454. export_vars.innodb_x_lock_spin_rounds
  1455. = rw_lock_stats.rw_x_spin_round_count;
  1456. export_vars.innodb_x_lock_spin_waits
  1457. = rw_lock_stats.rw_x_spin_wait_count;
  1458. oldest_view = UT_LIST_GET_LAST(trx_sys->view_list);
  1459. export_vars.innodb_oldest_view_low_limit_trx_id
  1460. = oldest_view ? oldest_view->low_limit_id : 0;
  1461. export_vars.innodb_purge_trx_id = purge_sys->limit.trx_no;
  1462. export_vars.innodb_purge_undo_no = purge_sys->limit.undo_no;
  1463. export_vars.innodb_current_row_locks
  1464. = lock_sys->rec_num;
  1465. #ifdef HAVE_ATOMIC_BUILTINS
  1466. export_vars.innodb_have_atomic_builtins = 1;
  1467. #else
  1468. export_vars.innodb_have_atomic_builtins = 0;
  1469. #endif
  1470. export_vars.innodb_page_size = UNIV_PAGE_SIZE;
  1471. export_vars.innodb_log_waits = srv_stats.log_waits;
  1472. export_vars.innodb_os_log_written = srv_stats.os_log_written;
  1473. export_vars.innodb_os_log_fsyncs = fil_n_log_flushes;
  1474. export_vars.innodb_os_log_pending_fsyncs = fil_n_pending_log_flushes;
  1475. export_vars.innodb_os_log_pending_writes =
  1476. srv_stats.os_log_pending_writes;
  1477. export_vars.innodb_log_write_requests = srv_stats.log_write_requests;
  1478. export_vars.innodb_log_writes = srv_stats.log_writes;
  1479. export_vars.innodb_dblwr_pages_written =
  1480. srv_stats.dblwr_pages_written;
  1481. export_vars.innodb_dblwr_writes = srv_stats.dblwr_writes;
  1482. export_vars.innodb_pages_created = stat.n_pages_created;
  1483. export_vars.innodb_pages_read = stat.n_pages_read;
  1484. export_vars.innodb_pages_written = stat.n_pages_written;
  1485. export_vars.innodb_row_lock_waits = srv_stats.n_lock_wait_count;
  1486. export_vars.innodb_row_lock_current_waits =
  1487. srv_stats.n_lock_wait_current_count;
  1488. export_vars.innodb_row_lock_time = srv_stats.n_lock_wait_time / 1000;
  1489. if (srv_stats.n_lock_wait_count > 0) {
  1490. export_vars.innodb_row_lock_time_avg = (ulint)
  1491. (srv_stats.n_lock_wait_time
  1492. / 1000 / srv_stats.n_lock_wait_count);
  1493. } else {
  1494. export_vars.innodb_row_lock_time_avg = 0;
  1495. }
  1496. export_vars.innodb_row_lock_time_max =
  1497. lock_sys->n_lock_max_wait_time / 1000;
  1498. export_vars.innodb_rows_read = srv_stats.n_rows_read;
  1499. export_vars.innodb_rows_inserted = srv_stats.n_rows_inserted;
  1500. export_vars.innodb_rows_updated = srv_stats.n_rows_updated;
  1501. export_vars.innodb_rows_deleted = srv_stats.n_rows_deleted;
  1502. export_vars.innodb_num_open_files = fil_n_file_opened;
  1503. export_vars.innodb_truncated_status_writes =
  1504. srv_truncated_status_writes;
  1505. export_vars.innodb_available_undo_logs = srv_available_undo_logs;
  1506. export_vars.innodb_read_views_memory
  1507. = os_atomic_increment_ulint(&srv_read_views_memory, 0);
  1508. export_vars.innodb_descriptors_memory
  1509. = os_atomic_increment_ulint(&srv_descriptors_memory, 0);
  1510. export_vars.innodb_page_compression_saved = srv_stats.page_compression_saved;
  1511. export_vars.innodb_page_compression_trim_sect512 = srv_stats.page_compression_trim_sect512;
  1512. export_vars.innodb_page_compression_trim_sect4096 = srv_stats.page_compression_trim_sect4096;
  1513. export_vars.innodb_index_pages_written = srv_stats.index_pages_written;
  1514. export_vars.innodb_non_index_pages_written = srv_stats.non_index_pages_written;
  1515. export_vars.innodb_pages_page_compressed = srv_stats.pages_page_compressed;
  1516. export_vars.innodb_page_compressed_trim_op = srv_stats.page_compressed_trim_op;
  1517. export_vars.innodb_page_compressed_trim_op_saved = srv_stats.page_compressed_trim_op_saved;
  1518. export_vars.innodb_pages_page_decompressed = srv_stats.pages_page_decompressed;
  1519. export_vars.innodb_defragment_compression_failures =
  1520. btr_defragment_compression_failures;
  1521. export_vars.innodb_defragment_failures = btr_defragment_failures;
  1522. export_vars.innodb_defragment_count = btr_defragment_count;
  1523. export_vars.innodb_onlineddl_rowlog_rows = onlineddl_rowlog_rows;
  1524. export_vars.innodb_onlineddl_rowlog_pct_used = onlineddl_rowlog_pct_used;
  1525. export_vars.innodb_onlineddl_pct_progress = onlineddl_pct_progress;
  1526. #ifdef UNIV_DEBUG
  1527. rw_lock_s_lock(&purge_sys->latch);
  1528. trx_id_t done_trx_no = purge_sys->done.trx_no;
  1529. trx_id_t up_limit_id = purge_sys->view
  1530. ? purge_sys->view->up_limit_id
  1531. : 0;
  1532. rw_lock_s_unlock(&purge_sys->latch);
  1533. mutex_enter(&trx_sys->mutex);
  1534. trx_id_t max_trx_id = trx_sys->rw_max_trx_id;
  1535. mutex_exit(&trx_sys->mutex);
  1536. if (!done_trx_no || max_trx_id < done_trx_no - 1) {
  1537. export_vars.innodb_purge_trx_id_age = 0;
  1538. } else {
  1539. export_vars.innodb_purge_trx_id_age =
  1540. (ulint) (max_trx_id - done_trx_no + 1);
  1541. }
  1542. if (!up_limit_id
  1543. || max_trx_id < up_limit_id) {
  1544. export_vars.innodb_purge_view_trx_id_age = 0;
  1545. } else {
  1546. export_vars.innodb_purge_view_trx_id_age =
  1547. (ulint) (max_trx_id - up_limit_id);
  1548. }
  1549. #endif /* UNIV_DEBUG */
  1550. export_vars.innodb_sec_rec_cluster_reads =
  1551. srv_stats.n_sec_rec_cluster_reads;
  1552. export_vars.innodb_sec_rec_cluster_reads_avoided =
  1553. srv_stats.n_sec_rec_cluster_reads_avoided;
  1554. mutex_exit(&srv_innodb_monitor_mutex);
  1555. }
  1556. /*********************************************************************//**
  1557. A thread which prints the info output by various InnoDB monitors.
  1558. @return a dummy parameter */
  1559. extern "C" UNIV_INTERN
  1560. os_thread_ret_t
  1561. DECLARE_THREAD(srv_monitor_thread)(
  1562. /*===============================*/
  1563. void* arg __attribute__((unused)))
  1564. /*!< in: a dummy parameter required by
  1565. os_thread_create */
  1566. {
  1567. ib_int64_t sig_count;
  1568. double time_elapsed;
  1569. time_t current_time;
  1570. time_t last_table_monitor_time;
  1571. time_t last_tablespace_monitor_time;
  1572. time_t last_monitor_time;
  1573. ulint mutex_skipped;
  1574. ibool last_srv_print_monitor;
  1575. ut_ad(!srv_read_only_mode);
  1576. #ifdef UNIV_DEBUG_THREAD_CREATION
  1577. fprintf(stderr, "Lock timeout thread starts, id %lu\n",
  1578. os_thread_pf(os_thread_get_curr_id()));
  1579. #endif /* UNIV_DEBUG_THREAD_CREATION */
  1580. #ifdef UNIV_PFS_THREAD
  1581. pfs_register_thread(srv_monitor_thread_key);
  1582. #endif /* UNIV_PFS_THREAD */
  1583. srv_monitor_active = TRUE;
  1584. UT_NOT_USED(arg);
  1585. srv_last_monitor_time = ut_time();
  1586. last_table_monitor_time = ut_time();
  1587. last_tablespace_monitor_time = ut_time();
  1588. last_monitor_time = ut_time();
  1589. mutex_skipped = 0;
  1590. last_srv_print_monitor = srv_print_innodb_monitor;
  1591. loop:
  1592. /* Wake up every 5 seconds to see if we need to print
  1593. monitor information or if signalled at shutdown. */
  1594. sig_count = os_event_reset(srv_monitor_event);
  1595. os_event_wait_time_low(srv_monitor_event, 5000000, sig_count);
  1596. current_time = ut_time();
  1597. time_elapsed = difftime(current_time, last_monitor_time);
  1598. if (time_elapsed > 15) {
  1599. last_monitor_time = ut_time();
  1600. if (srv_print_innodb_monitor) {
  1601. /* Reset mutex_skipped counter everytime
  1602. srv_print_innodb_monitor changes. This is to
  1603. ensure we will not be blocked by lock_sys->mutex
  1604. for short duration information printing,
  1605. such as requested by sync_array_print_long_waits() */
  1606. if (!last_srv_print_monitor) {
  1607. mutex_skipped = 0;
  1608. last_srv_print_monitor = TRUE;
  1609. }
  1610. if (!srv_printf_innodb_monitor(stderr,
  1611. MUTEX_NOWAIT(mutex_skipped),
  1612. NULL, NULL)) {
  1613. mutex_skipped++;
  1614. } else {
  1615. /* Reset the counter */
  1616. mutex_skipped = 0;
  1617. }
  1618. } else {
  1619. last_srv_print_monitor = FALSE;
  1620. }
  1621. /* We don't create the temp files or associated
  1622. mutexes in read-only-mode */
  1623. if (!srv_read_only_mode && srv_innodb_status) {
  1624. mutex_enter(&srv_monitor_file_mutex);
  1625. rewind(srv_monitor_file);
  1626. if (!srv_printf_innodb_monitor(srv_monitor_file,
  1627. MUTEX_NOWAIT(mutex_skipped),
  1628. NULL, NULL)) {
  1629. mutex_skipped++;
  1630. } else {
  1631. mutex_skipped = 0;
  1632. }
  1633. os_file_set_eof(srv_monitor_file);
  1634. mutex_exit(&srv_monitor_file_mutex);
  1635. }
  1636. if (srv_print_innodb_tablespace_monitor
  1637. && difftime(current_time,
  1638. last_tablespace_monitor_time) > 60) {
  1639. last_tablespace_monitor_time = ut_time();
  1640. fputs("========================"
  1641. "========================\n",
  1642. stderr);
  1643. ut_print_timestamp(stderr);
  1644. fputs(" INNODB TABLESPACE MONITOR OUTPUT\n"
  1645. "========================"
  1646. "========================\n",
  1647. stderr);
  1648. fsp_print(0);
  1649. fputs("Validating tablespace\n", stderr);
  1650. fsp_validate(0);
  1651. fputs("Validation ok\n"
  1652. "---------------------------------------\n"
  1653. "END OF INNODB TABLESPACE MONITOR OUTPUT\n"
  1654. "=======================================\n",
  1655. stderr);
  1656. }
  1657. if (srv_print_innodb_table_monitor
  1658. && difftime(current_time, last_table_monitor_time) > 60) {
  1659. last_table_monitor_time = ut_time();
  1660. fprintf(stderr, "Warning: %s\n",
  1661. DEPRECATED_MSG_INNODB_TABLE_MONITOR);
  1662. fputs("===========================================\n",
  1663. stderr);
  1664. ut_print_timestamp(stderr);
  1665. fputs(" INNODB TABLE MONITOR OUTPUT\n"
  1666. "===========================================\n",
  1667. stderr);
  1668. dict_print();
  1669. fputs("-----------------------------------\n"
  1670. "END OF INNODB TABLE MONITOR OUTPUT\n"
  1671. "==================================\n",
  1672. stderr);
  1673. fprintf(stderr, "Warning: %s\n",
  1674. DEPRECATED_MSG_INNODB_TABLE_MONITOR);
  1675. }
  1676. }
  1677. if (srv_shutdown_state >= SRV_SHUTDOWN_CLEANUP) {
  1678. goto exit_func;
  1679. }
  1680. if (srv_print_innodb_monitor
  1681. || srv_print_innodb_lock_monitor
  1682. || srv_print_innodb_tablespace_monitor
  1683. || srv_print_innodb_table_monitor) {
  1684. goto loop;
  1685. }
  1686. goto loop;
  1687. exit_func:
  1688. srv_monitor_active = FALSE;
  1689. /* We count the number of threads in os_thread_exit(). A created
  1690. thread should always use that to exit and not use return() to exit. */
  1691. os_thread_exit(NULL);
  1692. OS_THREAD_DUMMY_RETURN;
  1693. }
  1694. /*********************************************************************//**
  1695. A thread which prints warnings about semaphore waits which have lasted
  1696. too long. These can be used to track bugs which cause hangs.
  1697. @return a dummy parameter */
  1698. extern "C" UNIV_INTERN
  1699. os_thread_ret_t
  1700. DECLARE_THREAD(srv_error_monitor_thread)(
  1701. /*=====================================*/
  1702. void* arg __attribute__((unused)))
  1703. /*!< in: a dummy parameter required by
  1704. os_thread_create */
  1705. {
  1706. /* number of successive fatal timeouts observed */
  1707. ulint fatal_cnt = 0;
  1708. lsn_t old_lsn;
  1709. lsn_t new_lsn;
  1710. ib_int64_t sig_count;
  1711. /* longest waiting thread for a semaphore */
  1712. os_thread_id_t waiter = os_thread_get_curr_id();
  1713. os_thread_id_t old_waiter = waiter;
  1714. /* the semaphore that is being waited for */
  1715. const void* sema = NULL;
  1716. const void* old_sema = NULL;
  1717. ut_ad(!srv_read_only_mode);
  1718. old_lsn = srv_start_lsn;
  1719. #ifdef UNIV_DEBUG_THREAD_CREATION
  1720. fprintf(stderr, "Error monitor thread starts, id %lu\n",
  1721. os_thread_pf(os_thread_get_curr_id()));
  1722. #endif /* UNIV_DEBUG_THREAD_CREATION */
  1723. #ifdef UNIV_PFS_THREAD
  1724. pfs_register_thread(srv_error_monitor_thread_key);
  1725. #endif /* UNIV_PFS_THREAD */
  1726. srv_error_monitor_active = TRUE;
  1727. loop:
  1728. /* Try to track a strange bug reported by Harald Fuchs and others,
  1729. where the lsn seems to decrease at times */
  1730. /* We have to use nowait to ensure we don't block */
  1731. new_lsn= log_get_lsn_nowait();
  1732. if (new_lsn && new_lsn < old_lsn) {
  1733. ut_print_timestamp(stderr);
  1734. fprintf(stderr,
  1735. " InnoDB: Error: old log sequence number " LSN_PF
  1736. " was greater\n"
  1737. "InnoDB: than the new log sequence number " LSN_PF "!\n"
  1738. "InnoDB: Please submit a bug report"
  1739. " to http://bugs.mysql.com\n",
  1740. old_lsn, new_lsn);
  1741. ut_ad(0);
  1742. }
  1743. if (new_lsn)
  1744. old_lsn = new_lsn;
  1745. if (difftime(time(NULL), srv_last_monitor_time) > 60) {
  1746. /* We referesh InnoDB Monitor values so that averages are
  1747. printed from at most 60 last seconds */
  1748. srv_refresh_innodb_monitor_stats();
  1749. }
  1750. /* Update the statistics collected for deciding LRU
  1751. eviction policy. */
  1752. buf_LRU_stat_update();
  1753. /* In case mutex_exit is not a memory barrier, it is
  1754. theoretically possible some threads are left waiting though
  1755. the semaphore is already released. Wake up those threads: */
  1756. sync_arr_wake_threads_if_sema_free();
  1757. if (sync_array_print_long_waits(&waiter, &sema)
  1758. && sema == old_sema && os_thread_eq(waiter, old_waiter)) {
  1759. #if defined(WITH_WSREP) && defined(WITH_INNODB_DISALLOW_WRITES)
  1760. if (srv_allow_writes_event->is_set) {
  1761. #endif /* WITH_WSREP */
  1762. fatal_cnt++;
  1763. #if defined(WITH_WSREP) && defined(WITH_INNODB_DISALLOW_WRITES)
  1764. } else {
  1765. fprintf(stderr,
  1766. "WSREP: avoiding InnoDB self crash due to long "
  1767. "semaphore wait of > %lu seconds\n"
  1768. "Server is processing SST donor operation, "
  1769. "fatal_cnt now: %lu",
  1770. (ulong) srv_fatal_semaphore_wait_threshold, fatal_cnt);
  1771. }
  1772. #endif /* WITH_WSREP */
  1773. if (fatal_cnt > 10) {
  1774. fprintf(stderr,
  1775. "InnoDB: Error: semaphore wait has lasted"
  1776. " > %lu seconds\n"
  1777. "InnoDB: We intentionally crash the server,"
  1778. " because it appears to be hung.\n",
  1779. (ulong) srv_fatal_semaphore_wait_threshold);
  1780. ut_error;
  1781. }
  1782. } else {
  1783. fatal_cnt = 0;
  1784. old_waiter = waiter;
  1785. old_sema = sema;
  1786. }
  1787. if (srv_kill_idle_transaction && trx_sys) {
  1788. trx_t* trx;
  1789. time_t now;
  1790. rescan_idle:
  1791. now = time(NULL);
  1792. mutex_enter(&trx_sys->mutex);
  1793. trx = UT_LIST_GET_FIRST(trx_sys->mysql_trx_list);
  1794. while (trx) {
  1795. if (!trx_state_eq(trx, TRX_STATE_NOT_STARTED)
  1796. && trx_state_eq(trx, TRX_STATE_ACTIVE)
  1797. && trx->mysql_thd
  1798. && innobase_thd_is_idle(trx->mysql_thd)) {
  1799. ib_int64_t start_time = innobase_thd_get_start_time(trx->mysql_thd);
  1800. ulong thd_id = innobase_thd_get_thread_id(trx->mysql_thd);
  1801. if (trx->last_stmt_start != start_time) {
  1802. trx->idle_start = now;
  1803. trx->last_stmt_start = start_time;
  1804. } else if (difftime(now, trx->idle_start)
  1805. > srv_kill_idle_transaction) {
  1806. /* kill the session */
  1807. mutex_exit(&trx_sys->mutex);
  1808. innobase_thd_kill(thd_id);
  1809. goto rescan_idle;
  1810. }
  1811. }
  1812. trx = UT_LIST_GET_NEXT(mysql_trx_list, trx);
  1813. }
  1814. mutex_exit(&trx_sys->mutex);
  1815. }
  1816. /* Flush stderr so that a database user gets the output
  1817. to possible MySQL error file */
  1818. fflush(stderr);
  1819. sig_count = os_event_reset(srv_error_event);
  1820. os_event_wait_time_low(srv_error_event, 1000000, sig_count);
  1821. if (srv_shutdown_state < SRV_SHUTDOWN_CLEANUP) {
  1822. goto loop;
  1823. }
  1824. srv_error_monitor_active = FALSE;
  1825. /* We count the number of threads in os_thread_exit(). A created
  1826. thread should always use that to exit and not use return() to exit. */
  1827. os_thread_exit(NULL);
  1828. OS_THREAD_DUMMY_RETURN;
  1829. }
  1830. /******************************************************************//**
  1831. Increment the server activity count. */
  1832. UNIV_INTERN
  1833. void
  1834. srv_inc_activity_count(void)
  1835. /*========================*/
  1836. {
  1837. srv_sys->activity_count.inc();
  1838. }
  1839. /**********************************************************************//**
  1840. Check whether any background thread is active. If so return the thread
  1841. type.
  1842. @return SRV_NONE if all are suspended or have exited, thread
  1843. type if any are still active. */
  1844. UNIV_INTERN
  1845. srv_thread_type
  1846. srv_get_active_thread_type(void)
  1847. /*============================*/
  1848. {
  1849. srv_thread_type ret = SRV_NONE;
  1850. if (srv_read_only_mode) {
  1851. return(SRV_NONE);
  1852. }
  1853. srv_sys_mutex_enter();
  1854. for (ulint i = SRV_WORKER; i <= SRV_MASTER; ++i) {
  1855. if (srv_sys->n_threads_active[i] != 0) {
  1856. ret = static_cast<srv_thread_type>(i);
  1857. break;
  1858. }
  1859. }
  1860. srv_sys_mutex_exit();
  1861. /* Check only on shutdown. */
  1862. if (ret == SRV_NONE
  1863. && srv_shutdown_state != SRV_SHUTDOWN_NONE
  1864. && trx_purge_state() != PURGE_STATE_DISABLED
  1865. && trx_purge_state() != PURGE_STATE_EXIT) {
  1866. ret = SRV_PURGE;
  1867. }
  1868. return(ret);
  1869. }
  1870. /**********************************************************************//**
  1871. Check whether any background thread are active. If so print which thread
  1872. is active. Send the threads wakeup signal.
  1873. @return name of thread that is active or NULL */
  1874. UNIV_INTERN
  1875. const char*
  1876. srv_any_background_threads_are_active(void)
  1877. /*=======================================*/
  1878. {
  1879. const char* thread_active = NULL;
  1880. if (srv_read_only_mode) {
  1881. return(NULL);
  1882. } else if (srv_error_monitor_active) {
  1883. thread_active = "srv_error_monitor_thread";
  1884. } else if (lock_sys->timeout_thread_active) {
  1885. thread_active = "srv_lock_timeout thread";
  1886. } else if (srv_monitor_active) {
  1887. thread_active = "srv_monitor_thread";
  1888. } else if (srv_buf_dump_thread_active) {
  1889. thread_active = "buf_dump_thread";
  1890. } else if (srv_dict_stats_thread_active) {
  1891. thread_active = "dict_stats_thread";
  1892. }
  1893. os_event_set(srv_error_event);
  1894. os_event_set(srv_monitor_event);
  1895. os_event_set(srv_buf_dump_event);
  1896. os_event_set(lock_sys->timeout_event);
  1897. os_event_set(dict_stats_event);
  1898. return(thread_active);
  1899. }
  1900. /******************************************************************//**
  1901. A thread which follows the redo log and outputs the changed page bitmap.
  1902. @return a dummy value */
  1903. extern "C" UNIV_INTERN
  1904. os_thread_ret_t
  1905. DECLARE_THREAD(srv_redo_log_follow_thread)(
  1906. /*=======================================*/
  1907. void* arg __attribute__((unused))) /*!< in: a dummy parameter
  1908. required by
  1909. os_thread_create */
  1910. {
  1911. ut_ad(!srv_read_only_mode);
  1912. #ifdef UNIV_DEBUG_THREAD_CREATION
  1913. fprintf(stderr, "Redo log follower thread starts, id %lu\n",
  1914. os_thread_pf(os_thread_get_curr_id()));
  1915. #endif
  1916. #ifdef UNIV_PFS_THREAD
  1917. pfs_register_thread(srv_log_tracking_thread_key);
  1918. #endif
  1919. my_thread_init();
  1920. srv_redo_log_thread_started = true;
  1921. do {
  1922. os_event_wait(srv_checkpoint_completed_event);
  1923. os_event_reset(srv_checkpoint_completed_event);
  1924. #ifdef UNIV_DEBUG
  1925. if (!srv_track_changed_pages) {
  1926. continue;
  1927. }
  1928. #endif
  1929. if (srv_shutdown_state < SRV_SHUTDOWN_LAST_PHASE) {
  1930. if (!log_online_follow_redo_log()) {
  1931. /* TODO: sync with I_S log tracking status? */
  1932. ib_logf(IB_LOG_LEVEL_ERROR,
  1933. "log tracking bitmap write failed, "
  1934. "stopping log tracking thread!\n");
  1935. break;
  1936. }
  1937. os_event_set(srv_redo_log_tracked_event);
  1938. }
  1939. } while (srv_shutdown_state < SRV_SHUTDOWN_LAST_PHASE);
  1940. srv_track_changed_pages = FALSE;
  1941. log_online_read_shutdown();
  1942. os_event_set(srv_redo_log_tracked_event);
  1943. srv_redo_log_thread_started = false; /* Defensive, not required */
  1944. my_thread_end();
  1945. os_thread_exit(NULL);
  1946. OS_THREAD_DUMMY_RETURN;
  1947. }
  1948. /*************************************************************//**
  1949. Removes old archived transaction log files.
  1950. Both parameters couldn't be provided at the same time */
  1951. dberr_t
  1952. purge_archived_logs(
  1953. time_t before_date, /*!< in: all files modified
  1954. before timestamp should be removed */
  1955. lsn_t before_no) /*!< in: files with this number in name
  1956. and earler should be removed */
  1957. {
  1958. log_group_t* group = UT_LIST_GET_FIRST(log_sys->log_groups);
  1959. os_file_dir_t dir;
  1960. os_file_stat_t fileinfo;
  1961. char archived_log_filename[OS_FILE_MAX_PATH];
  1962. char namegen[OS_FILE_MAX_PATH];
  1963. ulint dirnamelen;
  1964. if (srv_arch_dir) {
  1965. dir = os_file_opendir(srv_arch_dir, FALSE);
  1966. if (!dir) {
  1967. ib_logf(IB_LOG_LEVEL_WARN,
  1968. "opening archived log directory %s failed. "
  1969. "Purge archived logs are not available\n",
  1970. srv_arch_dir);
  1971. /* failed to open directory */
  1972. return(DB_ERROR);
  1973. }
  1974. } else {
  1975. /* log archive directory is not specified */
  1976. return(DB_ERROR);
  1977. }
  1978. dirnamelen = strlen(srv_arch_dir);
  1979. memcpy(archived_log_filename, srv_arch_dir, dirnamelen);
  1980. if (dirnamelen &&
  1981. archived_log_filename[dirnamelen - 1] != SRV_PATH_SEPARATOR) {
  1982. archived_log_filename[dirnamelen++] = SRV_PATH_SEPARATOR;
  1983. }
  1984. memset(&fileinfo, 0, sizeof(fileinfo));
  1985. while(!os_file_readdir_next_file(srv_arch_dir, dir,
  1986. &fileinfo) ) {
  1987. if (strncmp(fileinfo.name,
  1988. IB_ARCHIVED_LOGS_PREFIX, IB_ARCHIVED_LOGS_PREFIX_LEN)) {
  1989. continue;
  1990. }
  1991. if (dirnamelen + strlen(fileinfo.name) + 2 > OS_FILE_MAX_PATH)
  1992. continue;
  1993. snprintf(archived_log_filename + dirnamelen, OS_FILE_MAX_PATH,
  1994. "%s", fileinfo.name);
  1995. if (before_no) {
  1996. ib_uint64_t log_file_no = strtoull(fileinfo.name +
  1997. IB_ARCHIVED_LOGS_PREFIX_LEN,
  1998. NULL, 10);
  1999. if (log_file_no == 0 || before_no <= log_file_no) {
  2000. continue;
  2001. }
  2002. } else {
  2003. fileinfo.mtime = 0;
  2004. if (os_file_get_status(archived_log_filename,
  2005. &fileinfo, false) != DB_SUCCESS ||
  2006. fileinfo.mtime == 0) {
  2007. continue;
  2008. }
  2009. if (before_date == 0 || fileinfo.mtime > before_date) {
  2010. continue;
  2011. }
  2012. }
  2013. /* We are going to delete archived file. Acquire log_sys->mutex
  2014. to make sure that we are the only who try to delete file. This
  2015. also prevents log system from using this file. Do not delete
  2016. file if it is currently in progress of writting or have
  2017. pending IO. This is enforced by checking:
  2018. 1. fil_space_contains_node.
  2019. 2. group->archived_offset % group->file_size != 0, i.e.
  2020. there is archive in progress and we are going to delete it.
  2021. This covers 3 cases:
  2022. a. Usual case when we have one archive in progress,
  2023. both 1 and 2 are TRUE
  2024. b. When we have more then 1 archive in fil_space,
  2025. this can happen when flushed LSN range crosses file
  2026. boundary
  2027. c. When we have empty fil_space, but existing file will be
  2028. opened once archiving operation is requested. This usually
  2029. happens on startup.
  2030. */
  2031. mutex_enter(&log_sys->mutex);
  2032. log_archived_file_name_gen(namegen, sizeof(namegen),
  2033. group->id, group->archived_file_no);
  2034. if (fil_space_contains_node(group->archive_space_id,
  2035. archived_log_filename) ||
  2036. (group->archived_offset % group->file_size != 0 &&
  2037. strcmp(namegen, archived_log_filename) == 0)) {
  2038. mutex_exit(&log_sys->mutex);
  2039. continue;
  2040. }
  2041. if (!os_file_delete_if_exists(innodb_file_data_key,
  2042. archived_log_filename)) {
  2043. ib_logf(IB_LOG_LEVEL_WARN,
  2044. "can't delete archived log file %s.\n",
  2045. archived_log_filename);
  2046. mutex_exit(&log_sys->mutex);
  2047. os_file_closedir(dir);
  2048. return(DB_ERROR);
  2049. }
  2050. mutex_exit(&log_sys->mutex);
  2051. }
  2052. os_file_closedir(dir);
  2053. return(DB_SUCCESS);
  2054. }
  2055. /*******************************************************************//**
  2056. Tells the InnoDB server that there has been activity in the database
  2057. and wakes up the master thread if it is suspended (not sleeping). Used
  2058. in the MySQL interface. Note that there is a small chance that the master
  2059. thread stays suspended (we do not protect our operation with the
  2060. srv_sys_t->mutex, for performance reasons). */
  2061. UNIV_INTERN
  2062. void
  2063. srv_active_wake_master_thread(void)
  2064. /*===============================*/
  2065. {
  2066. if (srv_read_only_mode) {
  2067. return;
  2068. }
  2069. ut_ad(!srv_sys_mutex_own());
  2070. srv_inc_activity_count();
  2071. if (srv_sys->n_threads_active[SRV_MASTER] == 0) {
  2072. srv_slot_t* slot;
  2073. srv_sys_mutex_enter();
  2074. slot = &srv_sys->sys_threads[SRV_MASTER_SLOT];
  2075. /* Only if the master thread has been started. */
  2076. if (slot->in_use) {
  2077. ut_a(srv_slot_get_type(slot) == SRV_MASTER);
  2078. if (slot->suspended) {
  2079. slot->suspended = FALSE;
  2080. ++srv_sys->n_threads_active[SRV_MASTER];
  2081. os_event_set(slot->event);
  2082. }
  2083. }
  2084. srv_sys_mutex_exit();
  2085. }
  2086. }
  2087. /*******************************************************************//**
  2088. Tells the purge thread that there has been activity in the database
  2089. and wakes up the purge thread if it is suspended (not sleeping). Note
  2090. that there is a small chance that the purge thread stays suspended
  2091. (we do not protect our check with the srv_sys_t:mutex and the
  2092. purge_sys->latch, for performance reasons). */
  2093. UNIV_INTERN
  2094. void
  2095. srv_wake_purge_thread_if_not_active(void)
  2096. /*=====================================*/
  2097. {
  2098. ut_ad(!srv_sys_mutex_own());
  2099. if (purge_sys->state == PURGE_STATE_RUN
  2100. && srv_sys->n_threads_active[SRV_PURGE] == 0) {
  2101. srv_release_threads(SRV_PURGE, 1);
  2102. }
  2103. }
  2104. /*******************************************************************//**
  2105. Wakes up the master thread if it is suspended or being suspended. */
  2106. UNIV_INTERN
  2107. void
  2108. srv_wake_master_thread(void)
  2109. /*========================*/
  2110. {
  2111. ut_ad(!srv_sys_mutex_own());
  2112. srv_inc_activity_count();
  2113. srv_release_threads(SRV_MASTER, 1);
  2114. }
  2115. /*******************************************************************//**
  2116. Get current server activity count. We don't hold srv_sys::mutex while
  2117. reading this value as it is only used in heuristics.
  2118. @return activity count. */
  2119. UNIV_INTERN
  2120. ulint
  2121. srv_get_activity_count(void)
  2122. /*========================*/
  2123. {
  2124. return(srv_sys->activity_count);
  2125. }
  2126. /*******************************************************************//**
  2127. Check if there has been any activity.
  2128. @return FALSE if no change in activity counter. */
  2129. UNIV_INTERN
  2130. ibool
  2131. srv_check_activity(
  2132. /*===============*/
  2133. ulint old_activity_count) /*!< in: old activity count */
  2134. {
  2135. return(srv_sys->activity_count != old_activity_count);
  2136. }
  2137. /********************************************************************//**
  2138. The master thread is tasked to ensure that flush of log file happens
  2139. once every second in the background. This is to ensure that not more
  2140. than one second of trxs are lost in case of crash when
  2141. innodb_flush_logs_at_trx_commit != 1 */
  2142. static
  2143. void
  2144. srv_sync_log_buffer_in_background(void)
  2145. /*===================================*/
  2146. {
  2147. time_t current_time = time(NULL);
  2148. srv_main_thread_op_info = "flushing log";
  2149. if (difftime(current_time, srv_last_log_flush_time)
  2150. >= srv_flush_log_at_timeout) {
  2151. log_buffer_sync_in_background(TRUE);
  2152. srv_last_log_flush_time = current_time;
  2153. srv_log_writes_and_flush++;
  2154. }
  2155. }
  2156. /********************************************************************//**
  2157. Make room in the table cache by evicting an unused table.
  2158. @return number of tables evicted. */
  2159. static
  2160. ulint
  2161. srv_master_evict_from_table_cache(
  2162. /*==============================*/
  2163. ulint pct_check) /*!< in: max percent to check */
  2164. {
  2165. ulint n_tables_evicted = 0;
  2166. rw_lock_x_lock(&dict_operation_lock);
  2167. dict_mutex_enter_for_mysql();
  2168. n_tables_evicted = dict_make_room_in_cache(
  2169. innobase_get_table_cache_size(), pct_check);
  2170. dict_mutex_exit_for_mysql();
  2171. rw_lock_x_unlock(&dict_operation_lock);
  2172. return(n_tables_evicted);
  2173. }
  2174. /*********************************************************************//**
  2175. This function prints progress message every 60 seconds during server
  2176. shutdown, for any activities that master thread is pending on. */
  2177. static
  2178. void
  2179. srv_shutdown_print_master_pending(
  2180. /*==============================*/
  2181. ib_time_t* last_print_time, /*!< last time the function
  2182. print the message */
  2183. ulint n_tables_to_drop, /*!< number of tables to
  2184. be dropped */
  2185. ulint n_bytes_merged) /*!< number of change buffer
  2186. just merged */
  2187. {
  2188. ib_time_t current_time;
  2189. double time_elapsed;
  2190. current_time = ut_time();
  2191. time_elapsed = ut_difftime(current_time, *last_print_time);
  2192. if (time_elapsed > 60) {
  2193. *last_print_time = ut_time();
  2194. if (n_tables_to_drop) {
  2195. ut_print_timestamp(stderr);
  2196. fprintf(stderr, " InnoDB: Waiting for "
  2197. "%lu table(s) to be dropped\n",
  2198. (ulong) n_tables_to_drop);
  2199. }
  2200. /* Check change buffer merge, we only wait for change buffer
  2201. merge if it is a slow shutdown */
  2202. if (!srv_fast_shutdown && n_bytes_merged) {
  2203. ut_print_timestamp(stderr);
  2204. fprintf(stderr, " InnoDB: Waiting for change "
  2205. "buffer merge to complete\n"
  2206. " InnoDB: number of bytes of change buffer "
  2207. "just merged: %lu\n",
  2208. n_bytes_merged);
  2209. }
  2210. }
  2211. }
  2212. /*********************************************************************//**
  2213. Perform the tasks that the master thread is supposed to do when the
  2214. server is active. There are two types of tasks. The first category is
  2215. of such tasks which are performed at each inovcation of this function.
  2216. We assume that this function is called roughly every second when the
  2217. server is active. The second category is of such tasks which are
  2218. performed at some interval e.g.: purge, dict_LRU cleanup etc. */
  2219. static
  2220. void
  2221. srv_master_do_active_tasks(void)
  2222. /*============================*/
  2223. {
  2224. ib_time_t cur_time = ut_time();
  2225. ullint counter_time = ut_time_us(NULL);
  2226. /* First do the tasks that we are suppose to do at each
  2227. invocation of this function. */
  2228. ++srv_main_active_loops;
  2229. MONITOR_INC(MONITOR_MASTER_ACTIVE_LOOPS);
  2230. /* ALTER TABLE in MySQL requires on Unix that the table handler
  2231. can drop tables lazily after there no longer are SELECT
  2232. queries to them. */
  2233. srv_main_thread_op_info = "doing background drop tables";
  2234. row_drop_tables_for_mysql_in_background();
  2235. MONITOR_INC_TIME_IN_MICRO_SECS(
  2236. MONITOR_SRV_BACKGROUND_DROP_TABLE_MICROSECOND, counter_time);
  2237. if (srv_shutdown_state > 0) {
  2238. return;
  2239. }
  2240. /* make sure that there is enough reusable space in the redo
  2241. log files */
  2242. srv_main_thread_op_info = "checking free log space";
  2243. log_free_check();
  2244. /* Do an ibuf merge */
  2245. srv_main_thread_op_info = "doing insert buffer merge";
  2246. counter_time = ut_time_us(NULL);
  2247. ibuf_contract_in_background(0, FALSE);
  2248. MONITOR_INC_TIME_IN_MICRO_SECS(
  2249. MONITOR_SRV_IBUF_MERGE_MICROSECOND, counter_time);
  2250. /* Flush logs if needed */
  2251. srv_main_thread_op_info = "flushing log";
  2252. srv_sync_log_buffer_in_background();
  2253. MONITOR_INC_TIME_IN_MICRO_SECS(
  2254. MONITOR_SRV_LOG_FLUSH_MICROSECOND, counter_time);
  2255. /* Now see if various tasks that are performed at defined
  2256. intervals need to be performed. */
  2257. #ifdef MEM_PERIODIC_CHECK
  2258. /* Check magic numbers of every allocated mem block once in
  2259. SRV_MASTER_MEM_VALIDATE_INTERVAL seconds */
  2260. if (cur_time % SRV_MASTER_MEM_VALIDATE_INTERVAL == 0) {
  2261. mem_validate_all_blocks();
  2262. MONITOR_INC_TIME_IN_MICRO_SECS(
  2263. MONITOR_SRV_MEM_VALIDATE_MICROSECOND, counter_time);
  2264. }
  2265. #endif
  2266. if (srv_shutdown_state > 0) {
  2267. return;
  2268. }
  2269. if (srv_shutdown_state > 0) {
  2270. return;
  2271. }
  2272. if (cur_time % SRV_MASTER_DICT_LRU_INTERVAL == 0) {
  2273. srv_main_thread_op_info = "enforcing dict cache limit";
  2274. srv_master_evict_from_table_cache(50);
  2275. MONITOR_INC_TIME_IN_MICRO_SECS(
  2276. MONITOR_SRV_DICT_LRU_MICROSECOND, counter_time);
  2277. }
  2278. if (srv_shutdown_state > 0) {
  2279. return;
  2280. }
  2281. /* Make a new checkpoint */
  2282. if (cur_time % SRV_MASTER_CHECKPOINT_INTERVAL == 0) {
  2283. srv_main_thread_op_info = "making checkpoint";
  2284. log_checkpoint(TRUE, FALSE, TRUE);
  2285. MONITOR_INC_TIME_IN_MICRO_SECS(
  2286. MONITOR_SRV_CHECKPOINT_MICROSECOND, counter_time);
  2287. }
  2288. }
  2289. /*********************************************************************//**
  2290. Perform the tasks that the master thread is supposed to do whenever the
  2291. server is idle. We do check for the server state during this function
  2292. and if the server has entered the shutdown phase we may return from
  2293. the function without completing the required tasks.
  2294. Note that the server can move to active state when we are executing this
  2295. function but we don't check for that as we are suppose to perform more
  2296. or less same tasks when server is active. */
  2297. static
  2298. void
  2299. srv_master_do_idle_tasks(void)
  2300. /*==========================*/
  2301. {
  2302. ullint counter_time;
  2303. ++srv_main_idle_loops;
  2304. MONITOR_INC(MONITOR_MASTER_IDLE_LOOPS);
  2305. /* ALTER TABLE in MySQL requires on Unix that the table handler
  2306. can drop tables lazily after there no longer are SELECT
  2307. queries to them. */
  2308. counter_time = ut_time_us(NULL);
  2309. srv_main_thread_op_info = "doing background drop tables";
  2310. row_drop_tables_for_mysql_in_background();
  2311. MONITOR_INC_TIME_IN_MICRO_SECS(
  2312. MONITOR_SRV_BACKGROUND_DROP_TABLE_MICROSECOND,
  2313. counter_time);
  2314. if (srv_shutdown_state > 0) {
  2315. return;
  2316. }
  2317. /* make sure that there is enough reusable space in the redo
  2318. log files */
  2319. srv_main_thread_op_info = "checking free log space";
  2320. log_free_check();
  2321. /* Do an ibuf merge */
  2322. counter_time = ut_time_us(NULL);
  2323. srv_main_thread_op_info = "doing insert buffer merge";
  2324. ibuf_contract_in_background(0, TRUE);
  2325. MONITOR_INC_TIME_IN_MICRO_SECS(
  2326. MONITOR_SRV_IBUF_MERGE_MICROSECOND, counter_time);
  2327. if (srv_shutdown_state > 0) {
  2328. return;
  2329. }
  2330. srv_main_thread_op_info = "enforcing dict cache limit";
  2331. srv_master_evict_from_table_cache(100);
  2332. MONITOR_INC_TIME_IN_MICRO_SECS(
  2333. MONITOR_SRV_DICT_LRU_MICROSECOND, counter_time);
  2334. /* Flush logs if needed */
  2335. srv_sync_log_buffer_in_background();
  2336. MONITOR_INC_TIME_IN_MICRO_SECS(
  2337. MONITOR_SRV_LOG_FLUSH_MICROSECOND, counter_time);
  2338. if (srv_shutdown_state > 0) {
  2339. return;
  2340. }
  2341. /* Make a new checkpoint */
  2342. srv_main_thread_op_info = "making checkpoint";
  2343. log_checkpoint(TRUE, FALSE, TRUE);
  2344. MONITOR_INC_TIME_IN_MICRO_SECS(MONITOR_SRV_CHECKPOINT_MICROSECOND,
  2345. counter_time);
  2346. if (srv_shutdown_state > 0) {
  2347. return;
  2348. }
  2349. if (srv_log_arch_expire_sec) {
  2350. srv_main_thread_op_info = "purging archived logs";
  2351. purge_archived_logs(ut_time() - srv_log_arch_expire_sec,
  2352. 0);
  2353. }
  2354. }
  2355. /*********************************************************************//**
  2356. Perform the tasks during shutdown. The tasks that we do at shutdown
  2357. depend on srv_fast_shutdown:
  2358. 2 => very fast shutdown => do no book keeping
  2359. 1 => normal shutdown => clear drop table queue and make checkpoint
  2360. 0 => slow shutdown => in addition to above do complete purge and ibuf
  2361. merge
  2362. @return TRUE if some work was done. FALSE otherwise */
  2363. static
  2364. ibool
  2365. srv_master_do_shutdown_tasks(
  2366. /*=========================*/
  2367. ib_time_t* last_print_time)/*!< last time the function
  2368. print the message */
  2369. {
  2370. ulint n_bytes_merged = 0;
  2371. ulint n_tables_to_drop = 0;
  2372. ut_ad(!srv_read_only_mode);
  2373. ++srv_main_shutdown_loops;
  2374. ut_a(srv_shutdown_state > 0);
  2375. /* In very fast shutdown none of the following is necessary */
  2376. if (srv_fast_shutdown == 2) {
  2377. return(FALSE);
  2378. }
  2379. /* ALTER TABLE in MySQL requires on Unix that the table handler
  2380. can drop tables lazily after there no longer are SELECT
  2381. queries to them. */
  2382. srv_main_thread_op_info = "doing background drop tables";
  2383. n_tables_to_drop = row_drop_tables_for_mysql_in_background();
  2384. /* make sure that there is enough reusable space in the redo
  2385. log files */
  2386. srv_main_thread_op_info = "checking free log space";
  2387. log_free_check();
  2388. /* In case of normal shutdown we don't do ibuf merge or purge */
  2389. if (srv_fast_shutdown == 1) {
  2390. goto func_exit;
  2391. }
  2392. /* Do an ibuf merge */
  2393. srv_main_thread_op_info = "doing insert buffer merge";
  2394. n_bytes_merged = ibuf_contract_in_background(0, TRUE);
  2395. /* Flush logs if needed */
  2396. srv_sync_log_buffer_in_background();
  2397. func_exit:
  2398. /* Make a new checkpoint about once in 10 seconds */
  2399. srv_main_thread_op_info = "making checkpoint";
  2400. log_checkpoint(TRUE, FALSE, FALSE);
  2401. /* Print progress message every 60 seconds during shutdown */
  2402. if (srv_shutdown_state > 0 && srv_print_verbose_log) {
  2403. srv_shutdown_print_master_pending(
  2404. last_print_time, n_tables_to_drop, n_bytes_merged);
  2405. }
  2406. return(n_bytes_merged || n_tables_to_drop);
  2407. }
  2408. /*********************************************************************//**
  2409. Puts master thread to sleep. At this point we are using polling to
  2410. service various activities. Master thread sleeps for one second before
  2411. checking the state of the server again */
  2412. static
  2413. void
  2414. srv_master_sleep(void)
  2415. /*==================*/
  2416. {
  2417. srv_main_thread_op_info = "sleeping";
  2418. os_thread_sleep(1000000);
  2419. srv_main_thread_op_info = "";
  2420. }
  2421. /*********************************************************************//**
  2422. The master thread controlling the server.
  2423. @return a dummy parameter */
  2424. extern "C" UNIV_INTERN
  2425. os_thread_ret_t
  2426. DECLARE_THREAD(srv_master_thread)(
  2427. /*==============================*/
  2428. void* arg __attribute__((unused)))
  2429. /*!< in: a dummy parameter required by
  2430. os_thread_create */
  2431. {
  2432. srv_slot_t* slot;
  2433. ulint old_activity_count = srv_get_activity_count();
  2434. ib_time_t last_print_time;
  2435. ut_ad(!srv_read_only_mode);
  2436. srv_master_tid = os_thread_get_tid();
  2437. os_thread_set_priority(srv_master_tid, srv_sched_priority_master);
  2438. #ifdef UNIV_DEBUG_THREAD_CREATION
  2439. fprintf(stderr, "Master thread starts, id %lu\n",
  2440. os_thread_pf(os_thread_get_curr_id()));
  2441. #endif /* UNIV_DEBUG_THREAD_CREATION */
  2442. #ifdef UNIV_PFS_THREAD
  2443. pfs_register_thread(srv_master_thread_key);
  2444. #endif /* UNIV_PFS_THREAD */
  2445. srv_main_thread_process_no = os_proc_get_number();
  2446. srv_main_thread_id = os_thread_pf(os_thread_get_curr_id());
  2447. slot = srv_reserve_slot(SRV_MASTER);
  2448. ut_a(slot == srv_sys->sys_threads);
  2449. last_print_time = ut_time();
  2450. loop:
  2451. if (srv_force_recovery >= SRV_FORCE_NO_BACKGROUND) {
  2452. goto suspend_thread;
  2453. }
  2454. while (srv_shutdown_state == SRV_SHUTDOWN_NONE) {
  2455. srv_master_sleep();
  2456. MONITOR_INC(MONITOR_MASTER_THREAD_SLEEP);
  2457. srv_current_thread_priority = srv_master_thread_priority;
  2458. if (srv_check_activity(old_activity_count)) {
  2459. old_activity_count = srv_get_activity_count();
  2460. srv_master_do_active_tasks();
  2461. } else {
  2462. srv_master_do_idle_tasks();
  2463. }
  2464. }
  2465. while (srv_master_do_shutdown_tasks(&last_print_time)) {
  2466. /* Shouldn't loop here in case of very fast shutdown */
  2467. ut_ad(srv_fast_shutdown < 2);
  2468. }
  2469. suspend_thread:
  2470. srv_main_thread_op_info = "suspending";
  2471. srv_suspend_thread(slot);
  2472. /* DO NOT CHANGE THIS STRING. innobase_start_or_create_for_mysql()
  2473. waits for database activity to die down when converting < 4.1.x
  2474. databases, and relies on this string being exactly as it is. InnoDB
  2475. manual also mentions this string in several places. */
  2476. srv_main_thread_op_info = "waiting for server activity";
  2477. os_event_wait(slot->event);
  2478. if (srv_shutdown_state == SRV_SHUTDOWN_EXIT_THREADS) {
  2479. os_thread_exit(NULL);
  2480. }
  2481. goto loop;
  2482. OS_THREAD_DUMMY_RETURN; /* Not reached, avoid compiler warning */
  2483. }
  2484. /*********************************************************************//**
  2485. Check if purge should stop.
  2486. @return true if it should shutdown. */
  2487. static
  2488. bool
  2489. srv_purge_should_exit(
  2490. /*==============*/
  2491. ulint n_purged) /*!< in: pages purged in last batch */
  2492. {
  2493. switch (srv_shutdown_state) {
  2494. case SRV_SHUTDOWN_NONE:
  2495. /* Normal operation. */
  2496. break;
  2497. case SRV_SHUTDOWN_CLEANUP:
  2498. case SRV_SHUTDOWN_EXIT_THREADS:
  2499. /* Exit unless slow shutdown requested or all done. */
  2500. return(srv_fast_shutdown != 0 || n_purged == 0);
  2501. case SRV_SHUTDOWN_LAST_PHASE:
  2502. case SRV_SHUTDOWN_FLUSH_PHASE:
  2503. ut_error;
  2504. }
  2505. return(false);
  2506. }
  2507. /*********************************************************************//**
  2508. Fetch and execute a task from the work queue.
  2509. @return true if a task was executed */
  2510. static
  2511. bool
  2512. srv_task_execute(void)
  2513. /*==================*/
  2514. {
  2515. que_thr_t* thr = NULL;
  2516. ut_ad(!srv_read_only_mode);
  2517. ut_a(srv_force_recovery < SRV_FORCE_NO_BACKGROUND);
  2518. mutex_enter(&srv_sys->tasks_mutex);
  2519. if (UT_LIST_GET_LEN(srv_sys->tasks) > 0) {
  2520. thr = UT_LIST_GET_FIRST(srv_sys->tasks);
  2521. ut_a(que_node_get_type(thr->child) == QUE_NODE_PURGE);
  2522. UT_LIST_REMOVE(queue, srv_sys->tasks, thr);
  2523. }
  2524. mutex_exit(&srv_sys->tasks_mutex);
  2525. if (thr != NULL) {
  2526. que_run_threads(thr);
  2527. os_atomic_inc_ulint(
  2528. &purge_sys->bh_mutex, &purge_sys->n_completed, 1);
  2529. srv_inc_activity_count();
  2530. }
  2531. return(thr != NULL);
  2532. }
  2533. static ulint purge_tid_i = 0;
  2534. /*********************************************************************//**
  2535. Worker thread that reads tasks from the work queue and executes them.
  2536. @return a dummy parameter */
  2537. extern "C" UNIV_INTERN
  2538. os_thread_ret_t
  2539. DECLARE_THREAD(srv_worker_thread)(
  2540. /*==============================*/
  2541. void* arg __attribute__((unused))) /*!< in: a dummy parameter
  2542. required by os_thread_create */
  2543. {
  2544. srv_slot_t* slot;
  2545. ulint tid_i = os_atomic_increment_ulint(&purge_tid_i, 1);
  2546. ut_ad(tid_i < srv_n_purge_threads);
  2547. ut_ad(!srv_read_only_mode);
  2548. ut_a(srv_force_recovery < SRV_FORCE_NO_BACKGROUND);
  2549. srv_purge_tids[tid_i] = os_thread_get_tid();
  2550. os_thread_set_priority(srv_purge_tids[tid_i],
  2551. srv_sched_priority_purge);
  2552. #ifdef UNIV_DEBUG_THREAD_CREATION
  2553. ut_print_timestamp(stderr);
  2554. fprintf(stderr, " InnoDB: worker thread starting, id %lu\n",
  2555. os_thread_pf(os_thread_get_curr_id()));
  2556. #endif /* UNIV_DEBUG_THREAD_CREATION */
  2557. slot = srv_reserve_slot(SRV_WORKER);
  2558. ut_a(srv_n_purge_threads > 1);
  2559. srv_sys_mutex_enter();
  2560. ut_a(srv_sys->n_threads_active[SRV_WORKER] < srv_n_purge_threads);
  2561. srv_sys_mutex_exit();
  2562. /* We need to ensure that the worker threads exit after the
  2563. purge coordinator thread. Otherwise the purge coordinaor can
  2564. end up waiting forever in trx_purge_wait_for_workers_to_complete() */
  2565. do {
  2566. srv_suspend_thread(slot);
  2567. os_event_wait(slot->event);
  2568. srv_current_thread_priority = srv_purge_thread_priority;
  2569. if (srv_task_execute()) {
  2570. /* If there are tasks in the queue, wakeup
  2571. the purge coordinator thread. */
  2572. srv_wake_purge_thread_if_not_active();
  2573. }
  2574. /* Note: we are checking the state without holding the
  2575. purge_sys->latch here. */
  2576. } while (purge_sys->state != PURGE_STATE_EXIT);
  2577. srv_free_slot(slot);
  2578. rw_lock_x_lock(&purge_sys->latch);
  2579. ut_a(!purge_sys->running);
  2580. ut_a(purge_sys->state == PURGE_STATE_EXIT);
  2581. ut_a(srv_shutdown_state > SRV_SHUTDOWN_NONE);
  2582. rw_lock_x_unlock(&purge_sys->latch);
  2583. #ifdef UNIV_DEBUG_THREAD_CREATION
  2584. ut_print_timestamp(stderr);
  2585. fprintf(stderr, " InnoDB: Purge worker thread exiting, id %lu\n",
  2586. os_thread_pf(os_thread_get_curr_id()));
  2587. #endif /* UNIV_DEBUG_THREAD_CREATION */
  2588. /* We count the number of threads in os_thread_exit(). A created
  2589. thread should always use that to exit and not use return() to exit. */
  2590. os_thread_exit(NULL);
  2591. OS_THREAD_DUMMY_RETURN; /* Not reached, avoid compiler warning */
  2592. }
  2593. /*********************************************************************//**
  2594. Do the actual purge operation.
  2595. @return length of history list before the last purge batch. */
  2596. static
  2597. ulint
  2598. srv_do_purge(
  2599. /*=========*/
  2600. ulint n_threads, /*!< in: number of threads to use */
  2601. ulint* n_total_purged) /*!< in/out: total pages purged */
  2602. {
  2603. ulint n_pages_purged;
  2604. static ulint count = 0;
  2605. static ulint n_use_threads = 0;
  2606. static ulint rseg_history_len = 0;
  2607. ulint old_activity_count = srv_get_activity_count();
  2608. ut_a(n_threads > 0);
  2609. ut_ad(!srv_read_only_mode);
  2610. /* Purge until there are no more records to purge and there is
  2611. no change in configuration or server state. If the user has
  2612. configured more than one purge thread then we treat that as a
  2613. pool of threads and only use the extra threads if purge can't
  2614. keep up with updates. */
  2615. if (n_use_threads == 0) {
  2616. n_use_threads = n_threads;
  2617. }
  2618. do {
  2619. srv_current_thread_priority = srv_purge_thread_priority;
  2620. if (trx_sys->rseg_history_len > rseg_history_len
  2621. || (srv_max_purge_lag > 0
  2622. && rseg_history_len > srv_max_purge_lag)) {
  2623. /* History length is now longer than what it was
  2624. when we took the last snapshot. Use more threads. */
  2625. if (n_use_threads < n_threads) {
  2626. ++n_use_threads;
  2627. }
  2628. } else if (srv_check_activity(old_activity_count)
  2629. && n_use_threads > 1) {
  2630. /* History length same or smaller since last snapshot,
  2631. use fewer threads. */
  2632. --n_use_threads;
  2633. old_activity_count = srv_get_activity_count();
  2634. }
  2635. /* Ensure that the purge threads are less than what
  2636. was configured. */
  2637. ut_a(n_use_threads > 0);
  2638. ut_a(n_use_threads <= n_threads);
  2639. /* Take a snapshot of the history list before purge. */
  2640. if ((rseg_history_len = trx_sys->rseg_history_len) == 0) {
  2641. break;
  2642. }
  2643. n_pages_purged = trx_purge(
  2644. n_use_threads, srv_purge_batch_size, false);
  2645. if (!(count++ % TRX_SYS_N_RSEGS)) {
  2646. /* Force a truncate of the history list. */
  2647. n_pages_purged += trx_purge(
  2648. 1, srv_purge_batch_size, true);
  2649. }
  2650. *n_total_purged += n_pages_purged;
  2651. } while (!srv_purge_should_exit(n_pages_purged) && n_pages_purged > 0);
  2652. return(rseg_history_len);
  2653. }
  2654. /*********************************************************************//**
  2655. Suspend the purge coordinator thread. */
  2656. static
  2657. void
  2658. srv_purge_coordinator_suspend(
  2659. /*==========================*/
  2660. srv_slot_t* slot, /*!< in/out: Purge coordinator
  2661. thread slot */
  2662. ulint rseg_history_len) /*!< in: history list length
  2663. before last purge */
  2664. {
  2665. ut_ad(!srv_read_only_mode);
  2666. ut_a(slot->type == SRV_PURGE);
  2667. bool stop = false;
  2668. /** Maximum wait time on the purge event, in micro-seconds. */
  2669. static const ulint SRV_PURGE_MAX_TIMEOUT = 10000;
  2670. ib_int64_t sig_count = srv_suspend_thread(slot);
  2671. do {
  2672. ulint ret;
  2673. rw_lock_x_lock(&purge_sys->latch);
  2674. purge_sys->running = false;
  2675. rw_lock_x_unlock(&purge_sys->latch);
  2676. /* We don't wait right away on the the non-timed wait because
  2677. we want to signal the thread that wants to suspend purge. */
  2678. if (stop) {
  2679. os_event_wait_low(slot->event, sig_count);
  2680. ret = 0;
  2681. } else if (rseg_history_len <= trx_sys->rseg_history_len) {
  2682. ret = os_event_wait_time_low(
  2683. slot->event, SRV_PURGE_MAX_TIMEOUT, sig_count);
  2684. } else {
  2685. /* We don't want to waste time waiting, if the
  2686. history list increased by the time we got here,
  2687. unless purge has been stopped. */
  2688. ret = 0;
  2689. }
  2690. srv_sys_mutex_enter();
  2691. /* The thread can be in state !suspended after the timeout
  2692. but before this check if another thread sent a wakeup signal. */
  2693. if (slot->suspended) {
  2694. slot->suspended = FALSE;
  2695. ++srv_sys->n_threads_active[slot->type];
  2696. ut_a(srv_sys->n_threads_active[slot->type] == 1);
  2697. }
  2698. srv_sys_mutex_exit();
  2699. sig_count = srv_suspend_thread(slot);
  2700. rw_lock_x_lock(&purge_sys->latch);
  2701. stop = (srv_shutdown_state == SRV_SHUTDOWN_NONE
  2702. && purge_sys->state == PURGE_STATE_STOP);
  2703. if (!stop) {
  2704. ut_a(purge_sys->n_stop == 0);
  2705. purge_sys->running = true;
  2706. } else {
  2707. ut_a(purge_sys->n_stop > 0);
  2708. /* Signal that we are suspended. */
  2709. os_event_set(purge_sys->event);
  2710. }
  2711. rw_lock_x_unlock(&purge_sys->latch);
  2712. if (ret == OS_SYNC_TIME_EXCEEDED) {
  2713. /* No new records added since wait started then simply
  2714. wait for new records. The magic number 5000 is an
  2715. approximation for the case where we have cached UNDO
  2716. log records which prevent truncate of the UNDO
  2717. segments. */
  2718. if (rseg_history_len == trx_sys->rseg_history_len
  2719. && trx_sys->rseg_history_len < 5000) {
  2720. stop = true;
  2721. }
  2722. }
  2723. } while (stop);
  2724. srv_sys_mutex_enter();
  2725. if (slot->suspended) {
  2726. slot->suspended = FALSE;
  2727. ++srv_sys->n_threads_active[slot->type];
  2728. ut_a(srv_sys->n_threads_active[slot->type] == 1);
  2729. }
  2730. srv_sys_mutex_exit();
  2731. }
  2732. /*********************************************************************//**
  2733. Purge coordinator thread that schedules the purge tasks.
  2734. @return a dummy parameter */
  2735. extern "C" UNIV_INTERN
  2736. os_thread_ret_t
  2737. DECLARE_THREAD(srv_purge_coordinator_thread)(
  2738. /*=========================================*/
  2739. void* arg __attribute__((unused))) /*!< in: a dummy parameter
  2740. required by os_thread_create */
  2741. {
  2742. srv_slot_t* slot;
  2743. ulint n_total_purged = ULINT_UNDEFINED;
  2744. ut_ad(!srv_read_only_mode);
  2745. ut_a(srv_n_purge_threads >= 1);
  2746. ut_a(trx_purge_state() == PURGE_STATE_INIT);
  2747. ut_a(srv_force_recovery < SRV_FORCE_NO_BACKGROUND);
  2748. srv_purge_tids[0] = os_thread_get_tid();
  2749. os_thread_set_priority(srv_purge_tids[0], srv_sched_priority_purge);
  2750. rw_lock_x_lock(&purge_sys->latch);
  2751. purge_sys->running = true;
  2752. purge_sys->state = PURGE_STATE_RUN;
  2753. rw_lock_x_unlock(&purge_sys->latch);
  2754. #ifdef UNIV_PFS_THREAD
  2755. pfs_register_thread(srv_purge_thread_key);
  2756. #endif /* UNIV_PFS_THREAD */
  2757. #ifdef UNIV_DEBUG_THREAD_CREATION
  2758. ut_print_timestamp(stderr);
  2759. fprintf(stderr, " InnoDB: Purge coordinator thread created, id %lu\n",
  2760. os_thread_pf(os_thread_get_curr_id()));
  2761. #endif /* UNIV_DEBUG_THREAD_CREATION */
  2762. slot = srv_reserve_slot(SRV_PURGE);
  2763. ulint rseg_history_len = trx_sys->rseg_history_len;
  2764. do {
  2765. /* If there are no records to purge or the last
  2766. purge didn't purge any records then wait for activity. */
  2767. if (srv_shutdown_state == SRV_SHUTDOWN_NONE
  2768. && (purge_sys->state == PURGE_STATE_STOP
  2769. || n_total_purged == 0)) {
  2770. srv_purge_coordinator_suspend(slot, rseg_history_len);
  2771. }
  2772. if (srv_purge_should_exit(n_total_purged)) {
  2773. ut_a(!slot->suspended);
  2774. break;
  2775. }
  2776. n_total_purged = 0;
  2777. srv_current_thread_priority = srv_purge_thread_priority;
  2778. rseg_history_len = srv_do_purge(
  2779. srv_n_purge_threads, &n_total_purged);
  2780. srv_inc_activity_count();
  2781. } while (!srv_purge_should_exit(n_total_purged));
  2782. /* Ensure that we don't jump out of the loop unless the
  2783. exit condition is satisfied. */
  2784. ut_a(srv_purge_should_exit(n_total_purged));
  2785. ulint n_pages_purged = ULINT_MAX;
  2786. /* Ensure that all records are purged if it is not a fast shutdown.
  2787. This covers the case where a record can be added after we exit the
  2788. loop above. */
  2789. while (srv_fast_shutdown == 0 && n_pages_purged > 0) {
  2790. n_pages_purged = trx_purge(1, srv_purge_batch_size, false);
  2791. }
  2792. /* Force a truncate of the history list. */
  2793. n_pages_purged = trx_purge(1, srv_purge_batch_size, true);
  2794. ut_a(n_pages_purged == 0 || srv_fast_shutdown != 0);
  2795. /* The task queue should always be empty, independent of fast
  2796. shutdown state. */
  2797. ut_a(srv_get_task_queue_length() == 0);
  2798. srv_free_slot(slot);
  2799. /* Note that we are shutting down. */
  2800. rw_lock_x_lock(&purge_sys->latch);
  2801. purge_sys->state = PURGE_STATE_EXIT;
  2802. purge_sys->running = false;
  2803. rw_lock_x_unlock(&purge_sys->latch);
  2804. #ifdef UNIV_DEBUG_THREAD_CREATION
  2805. ut_print_timestamp(stderr);
  2806. fprintf(stderr, " InnoDB: Purge coordinator exiting, id %lu\n",
  2807. os_thread_pf(os_thread_get_curr_id()));
  2808. #endif /* UNIV_DEBUG_THREAD_CREATION */
  2809. /* Ensure that all the worker threads quit. */
  2810. if (srv_n_purge_threads > 1) {
  2811. srv_release_threads(SRV_WORKER, srv_n_purge_threads - 1);
  2812. }
  2813. /* We count the number of threads in os_thread_exit(). A created
  2814. thread should always use that to exit and not use return() to exit. */
  2815. os_thread_exit(NULL);
  2816. OS_THREAD_DUMMY_RETURN; /* Not reached, avoid compiler warning */
  2817. }
  2818. /**********************************************************************//**
  2819. Enqueues a task to server task queue and releases a worker thread, if there
  2820. is a suspended one. */
  2821. UNIV_INTERN
  2822. void
  2823. srv_que_task_enqueue_low(
  2824. /*=====================*/
  2825. que_thr_t* thr) /*!< in: query thread */
  2826. {
  2827. ut_ad(!srv_read_only_mode);
  2828. mutex_enter(&srv_sys->tasks_mutex);
  2829. UT_LIST_ADD_LAST(queue, srv_sys->tasks, thr);
  2830. mutex_exit(&srv_sys->tasks_mutex);
  2831. srv_release_threads(SRV_WORKER, 1);
  2832. }
  2833. /**********************************************************************//**
  2834. Get count of tasks in the queue.
  2835. @return number of tasks in queue */
  2836. UNIV_INTERN
  2837. ulint
  2838. srv_get_task_queue_length(void)
  2839. /*===========================*/
  2840. {
  2841. ulint n_tasks;
  2842. ut_ad(!srv_read_only_mode);
  2843. mutex_enter(&srv_sys->tasks_mutex);
  2844. n_tasks = UT_LIST_GET_LEN(srv_sys->tasks);
  2845. mutex_exit(&srv_sys->tasks_mutex);
  2846. return(n_tasks);
  2847. }
  2848. /**********************************************************************//**
  2849. Wakeup the purge threads. */
  2850. UNIV_INTERN
  2851. void
  2852. srv_purge_wakeup(void)
  2853. /*==================*/
  2854. {
  2855. ut_ad(!srv_read_only_mode);
  2856. if (srv_force_recovery < SRV_FORCE_NO_BACKGROUND) {
  2857. srv_release_threads(SRV_PURGE, 1);
  2858. if (srv_n_purge_threads > 1) {
  2859. ulint n_workers = srv_n_purge_threads - 1;
  2860. srv_release_threads(SRV_WORKER, n_workers);
  2861. }
  2862. }
  2863. }