| 1 | /***************************************************************************** |
| 2 | |
| 3 | Copyright (c) 2012, 2017, Oracle and/or its affiliates. All Rights Reserved. |
| 4 | Copyright (c) 2017, 2018, MariaDB Corporation. |
| 5 | |
| 6 | This program is free software; you can redistribute it and/or modify it under |
| 7 | the terms of the GNU General Public License as published by the Free Software |
| 8 | Foundation; version 2 of the License. |
| 9 | |
| 10 | This program is distributed in the hope that it will be useful, but WITHOUT |
| 11 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 12 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU General Public License along with |
| 15 | this program; if not, write to the Free Software Foundation, Inc., |
| 16 | 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA |
| 17 | |
| 18 | *****************************************************************************/ |
| 19 | |
| 20 | /**************************************************//** |
| 21 | @file include/dict0stats_bg.h |
| 22 | Code used for background table and index stats gathering. |
| 23 | |
| 24 | Created Apr 26, 2012 Vasil Dimov |
| 25 | *******************************************************/ |
| 26 | |
| 27 | #ifndef dict0stats_bg_h |
| 28 | #define dict0stats_bg_h |
| 29 | |
| 30 | #include "univ.i" |
| 31 | |
| 32 | #include "dict0types.h" |
| 33 | #include "os0event.h" |
| 34 | #include "os0thread.h" |
| 35 | |
| 36 | /** Event to wake up dict_stats_thread on dict_stats_recalc_pool_add() |
| 37 | or shutdown. Not protected by any mutex. */ |
| 38 | extern os_event_t dict_stats_event; |
| 39 | |
| 40 | #ifdef HAVE_PSI_INTERFACE |
| 41 | extern mysql_pfs_key_t dict_stats_recalc_pool_mutex_key; |
| 42 | #endif /* HAVE_PSI_INTERFACE */ |
| 43 | |
| 44 | #ifdef UNIV_DEBUG |
| 45 | /** Value of MySQL global used to disable dict_stats thread. */ |
| 46 | extern my_bool innodb_dict_stats_disabled_debug; |
| 47 | #endif /* UNIV_DEBUG */ |
| 48 | |
| 49 | /*****************************************************************//** |
| 50 | Delete a given table from the auto recalc pool. |
| 51 | dict_stats_recalc_pool_del() */ |
| 52 | void |
| 53 | dict_stats_recalc_pool_del( |
| 54 | /*=======================*/ |
| 55 | const dict_table_t* table); /*!< in: table to remove */ |
| 56 | |
| 57 | /** Yield the data dictionary latch when waiting |
| 58 | for the background thread to stop accessing a table. |
| 59 | @param trx transaction holding the data dictionary locks */ |
| 60 | #define DICT_BG_YIELD(trx) do { \ |
| 61 | row_mysql_unlock_data_dictionary(trx); \ |
| 62 | os_thread_sleep(250000); \ |
| 63 | row_mysql_lock_data_dictionary(trx); \ |
| 64 | } while (0) |
| 65 | |
| 66 | /*****************************************************************//** |
| 67 | Request the background collection of statistics to stop for a table. |
| 68 | @retval true when no background process is active |
| 69 | @retval false when it is not safe to modify the table definition */ |
| 70 | UNIV_INLINE |
| 71 | bool |
| 72 | dict_stats_stop_bg( |
| 73 | /*===============*/ |
| 74 | dict_table_t* table) /*!< in/out: table */ |
| 75 | { |
| 76 | ut_ad(!srv_read_only_mode); |
| 77 | ut_ad(mutex_own(&dict_sys->mutex)); |
| 78 | |
| 79 | if (!(table->stats_bg_flag & BG_STAT_IN_PROGRESS)) { |
| 80 | return(true); |
| 81 | } |
| 82 | |
| 83 | table->stats_bg_flag |= BG_STAT_SHOULD_QUIT; |
| 84 | return(false); |
| 85 | } |
| 86 | |
| 87 | /*****************************************************************//** |
| 88 | Wait until background stats thread has stopped using the specified table. |
| 89 | The caller must have locked the data dictionary using |
| 90 | row_mysql_lock_data_dictionary() and this function may unlock it temporarily |
| 91 | and restore the lock before it exits. |
| 92 | The background stats thread is guaranteed not to start using the specified |
| 93 | table after this function returns and before the caller unlocks the data |
| 94 | dictionary because it sets the BG_STAT_IN_PROGRESS bit in table->stats_bg_flag |
| 95 | under dict_sys->mutex. */ |
| 96 | void |
| 97 | dict_stats_wait_bg_to_stop_using_table( |
| 98 | /*===================================*/ |
| 99 | dict_table_t* table, /*!< in/out: table */ |
| 100 | trx_t* trx); /*!< in/out: transaction to use for |
| 101 | unlocking/locking the data dict */ |
| 102 | /*****************************************************************//** |
| 103 | Initialize global variables needed for the operation of dict_stats_thread(). |
| 104 | Must be called before dict_stats_thread() is started. */ |
| 105 | void |
| 106 | dict_stats_thread_init(); |
| 107 | /*====================*/ |
| 108 | |
| 109 | /*****************************************************************//** |
| 110 | Free resources allocated by dict_stats_thread_init(), must be called |
| 111 | after dict_stats_thread() has exited. */ |
| 112 | void |
| 113 | dict_stats_thread_deinit(); |
| 114 | /*======================*/ |
| 115 | |
| 116 | #ifdef UNIV_DEBUG |
| 117 | /** Disables dict stats thread. It's used by: |
| 118 | SET GLOBAL innodb_dict_stats_disabled_debug = 1 (0). |
| 119 | @param[in] save immediate result from check function */ |
| 120 | void dict_stats_disabled_debug_update(THD*, st_mysql_sys_var*, void*, |
| 121 | const void* save); |
| 122 | #endif /* UNIV_DEBUG */ |
| 123 | |
| 124 | /*****************************************************************//** |
| 125 | This is the thread for background stats gathering. It pops tables, from |
| 126 | the auto recalc list and proceeds them, eventually recalculating their |
| 127 | statistics. |
| 128 | @return this function does not return, it calls os_thread_exit() */ |
| 129 | extern "C" |
| 130 | os_thread_ret_t |
| 131 | DECLARE_THREAD(dict_stats_thread)( |
| 132 | /*==============================*/ |
| 133 | void* arg); /*!< in: a dummy parameter |
| 134 | required by os_thread_create */ |
| 135 | |
| 136 | /** Shut down the dict_stats_thread. */ |
| 137 | void |
| 138 | dict_stats_shutdown(); |
| 139 | |
| 140 | #endif /* dict0stats_bg_h */ |
| 141 | |