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.

3696 lines
110 KiB

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