1/*****************************************************************************
2
3Copyright (c) 2012, 2017, Oracle and/or its affiliates. All Rights Reserved.
4Copyright (c) 2017, 2018, MariaDB Corporation.
5
6This program is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free Software
8Foundation; version 2 of the License.
9
10This program is distributed in the hope that it will be useful, but WITHOUT
11ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License along with
15this program; if not, write to the Free Software Foundation, Inc.,
1651 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
17
18*****************************************************************************/
19
20/**************************************************//**
21@file include/dict0stats_bg.h
22Code used for background table and index stats gathering.
23
24Created 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()
37or shutdown. Not protected by any mutex. */
38extern os_event_t dict_stats_event;
39
40#ifdef HAVE_PSI_INTERFACE
41extern 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. */
46extern my_bool innodb_dict_stats_disabled_debug;
47#endif /* UNIV_DEBUG */
48
49/*****************************************************************//**
50Delete a given table from the auto recalc pool.
51dict_stats_recalc_pool_del() */
52void
53dict_stats_recalc_pool_del(
54/*=======================*/
55 const dict_table_t* table); /*!< in: table to remove */
56
57/** Yield the data dictionary latch when waiting
58for 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/*****************************************************************//**
67Request 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 */
70UNIV_INLINE
71bool
72dict_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/*****************************************************************//**
88Wait until background stats thread has stopped using the specified table.
89The caller must have locked the data dictionary using
90row_mysql_lock_data_dictionary() and this function may unlock it temporarily
91and restore the lock before it exits.
92The background stats thread is guaranteed not to start using the specified
93table after this function returns and before the caller unlocks the data
94dictionary because it sets the BG_STAT_IN_PROGRESS bit in table->stats_bg_flag
95under dict_sys->mutex. */
96void
97dict_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/*****************************************************************//**
103Initialize global variables needed for the operation of dict_stats_thread().
104Must be called before dict_stats_thread() is started. */
105void
106dict_stats_thread_init();
107/*====================*/
108
109/*****************************************************************//**
110Free resources allocated by dict_stats_thread_init(), must be called
111after dict_stats_thread() has exited. */
112void
113dict_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 */
120void dict_stats_disabled_debug_update(THD*, st_mysql_sys_var*, void*,
121 const void* save);
122#endif /* UNIV_DEBUG */
123
124/*****************************************************************//**
125This is the thread for background stats gathering. It pops tables, from
126the auto recalc list and proceeds them, eventually recalculating their
127statistics.
128@return this function does not return, it calls os_thread_exit() */
129extern "C"
130os_thread_ret_t
131DECLARE_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. */
137void
138dict_stats_shutdown();
139
140#endif /* dict0stats_bg_h */
141