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