| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 2012, 2026, Oracle and/or its affiliates. |
| 4 |
|
| 5 |
This program is free software; you can redistribute it and/or modify it under |
| 6 |
the terms of the GNU General Public License, version 2.0, as published by the |
| 7 |
Free Software Foundation. |
| 8 |
|
| 9 |
This program is designed to work with certain software (including |
| 10 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 11 |
as designated in a particular file or component or in included license |
| 12 |
documentation. The authors of MySQL hereby grant you an additional |
| 13 |
permission to link the program and your derivative works with the |
| 14 |
separately licensed software that they have either included with |
| 15 |
the program or referenced in the documentation. |
| 16 |
|
| 17 |
This program is distributed in the hope that it will be useful, but WITHOUT |
| 18 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 19 |
FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0, |
| 20 |
for more details. |
| 21 |
|
| 22 |
You should have received a copy of the GNU General Public License along with |
| 23 |
this program; if not, write to the Free Software Foundation, Inc., |
| 24 |
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 25 |
|
| 26 |
*****************************************************************************/ |
| 27 |
|
| 28 |
/** @file include/dict0stats_bg.h |
| 29 |
Code used for background table and index stats gathering. |
| 30 |
|
| 31 |
Created Apr 26, 2012 Vasil Dimov |
| 32 |
*******************************************************/ |
| 33 |
|
| 34 |
#ifndef dict0stats_bg_h |
| 35 |
#define dict0stats_bg_h |
| 36 |
|
| 37 |
#include "univ.i" |
| 38 |
|
| 39 |
#include "dict0types.h" |
| 40 |
#include "os0event.h" |
| 41 |
#include "os0thread.h" |
| 42 |
#include "row0mysql.h" |
| 43 |
|
| 44 |
/** Event to wake up the stats thread */ |
| 45 |
extern os_event_t dict_stats_event; |
| 46 |
|
| 47 |
#ifdef HAVE_PSI_INTERFACE |
| 48 |
extern mysql_pfs_key_t dict_stats_recalc_pool_mutex_key; |
| 49 |
#endif /* HAVE_PSI_INTERFACE */ |
| 50 |
|
| 51 |
#ifdef UNIV_DEBUG |
| 52 |
/** Value of MySQL global used to disable dict_stats thread. */ |
| 53 |
extern bool innodb_dict_stats_disabled_debug; |
| 54 |
#endif /* UNIV_DEBUG */ |
| 55 |
|
| 56 |
/** Add a table to the recalc pool, which is processed by the |
| 57 |
background stats gathering thread. Only the table id is added to the |
| 58 |
list, so the table can be closed after being enqueued and it will be |
| 59 |
opened when needed. If the table does not exist later (has been DROPped), |
| 60 |
then it will be removed from the pool and skipped. */ |
| 61 |
void dict_stats_recalc_pool_add( |
| 62 |
const dict_table_t *table); /*!< in: table to add */ |
| 63 |
|
| 64 |
/** Delete a given table from the auto recalc pool. |
| 65 |
dict_stats_recalc_pool_del() */ |
| 66 |
void dict_stats_recalc_pool_del( |
| 67 |
const dict_table_t *table); /*!< in: table to remove */ |
| 68 |
|
| 69 |
/** Yield the data dictionary latch when waiting |
| 70 |
for the background thread to stop accessing a table. |
| 71 |
@param[in] trx transaction holding the data dictionary locks |
| 72 |
@param[in] location location where called */ |
| 73 |
static inline void DICT_STATS_BG_YIELD(trx_t *trx, ut::Location location) { |
| 74 |
row_mysql_unlock_data_dictionary(trx); |
| 75 |
std::this_thread::sleep_for(std::chrono::milliseconds(250)); |
| 76 |
row_mysql_lock_data_dictionary(trx, location); |
| 77 |
} |
| 78 |
|
| 79 |
/** Request the background collection of statistics to stop for a table. |
| 80 |
@retval true when no background process is active |
| 81 |
@retval false when it is not safe to modify the table definition */ |
| 82 |
[[nodiscard]] static inline bool dict_stats_stop_bg( |
| 83 |
dict_table_t *table); /*!< in/out: table */ |
| 84 |
|
| 85 |
/** Wait until background stats thread has stopped using the specified table. |
| 86 |
The caller must have locked the data dictionary using |
| 87 |
row_mysql_lock_data_dictionary() and this function may unlock it temporarily |
| 88 |
and restore the lock before it exits. |
| 89 |
The background stats thread is guaranteed not to start using the specified |
| 90 |
table after this function returns and before the caller unlocks the data |
| 91 |
dictionary because it sets the BG_STAT_IN_PROGRESS bit in table->stats_bg_flag |
| 92 |
under dict_sys->mutex. */ |
| 93 |
void dict_stats_wait_bg_to_stop_using_table( |
| 94 |
dict_table_t *table, /*!< in/out: table */ |
| 95 |
trx_t *trx); /*!< in/out: transaction to use for |
| 96 |
unlocking/locking the data dict */ |
| 97 |
/** Initialize global variables needed for the operation of dict_stats_thread(). |
| 98 |
Must be called before dict_stats_thread() is started. */ |
| 99 |
void dict_stats_thread_init(); |
| 100 |
|
| 101 |
/** Free resources allocated by dict_stats_thread_init(), must be called |
| 102 |
after dict_stats_thread() has exited. */ |
| 103 |
void dict_stats_thread_deinit(); |
| 104 |
|
| 105 |
#ifdef UNIV_DEBUG |
| 106 |
/** Disables dict stats thread. It's used by: |
| 107 |
SET GLOBAL innodb_dict_stats_disabled_debug = 1 (0). |
| 108 |
@param[in] thd thread handle |
| 109 |
@param[in] var pointer to system variable |
| 110 |
@param[out] var_ptr where the formal string goes |
| 111 |
@param[in] save immediate result from check function */ |
| 112 |
void dict_stats_disabled_debug_update(THD *thd, SYS_VAR *var, void *var_ptr, |
| 113 |
const void *save); |
| 114 |
#endif /* UNIV_DEBUG */ |
| 115 |
|
| 116 |
/** This is the thread for background stats gathering. It pops tables, from |
| 117 |
the auto recalc list and proceeds them, eventually recalculating their |
| 118 |
statistics. */ |
| 119 |
void dict_stats_thread(); |
| 120 |
|
| 121 |
/** Shutdown the dict stats thread. */ |
| 122 |
void dict_stats_shutdown(); |
| 123 |
|
| 124 |
#include "dict0stats_bg.ic" |
| 125 |
|
| 126 |
#endif /* dict0stats_bg_h */ |