MySQL 8.4.0
Source Code Documentation
opt_costconstantcache.h
Go to the documentation of this file.
1#ifndef OPT_COSTCONSTANTCACHE_INCLUDED
2#define OPT_COSTCONSTANTCACHE_INCLUDED
3
4/*
5 Copyright (c) 2014, 2024, Oracle and/or its affiliates.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License, version 2.0,
9 as published by the Free Software Foundation.
10
11 This program is designed to work with certain software (including
12 but not limited to OpenSSL) that is licensed under separate terms,
13 as designated in a particular file or component or in included license
14 documentation. The authors of MySQL hereby grant you an additional
15 permission to link the program and your derivative works with the
16 separately licensed software that they have either included with
17 the program or referenced in the documentation.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License, version 2.0, for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
27
28#include <assert.h>
29#include <stddef.h>
30
33#include "sql/opt_costconstants.h" // Cost_model_constants
34
35/**
36 This class implements a cache for "cost constant sets". This cache is
37 responsible for creating the set of cost constant, giving new sessions access
38 to the latest versions of the cost constants, and for re-reading the cost
39 constant tables in the case where these have been updated.
40
41 To initialize the cost constant cache with default cost constants, @c init()
42 must be called. To load cost constants from the mysql.server_cost and
43 mysql.engine_cost tables, @c reload() must be called.
44
45 The cost constant cache keeps a copy of the current set of cost constants.
46 Each time a new session initializes its Cost_model_server object (by calling
47 Cost_model_server::init() in lex_start()), the Cost_model_server object will
48 request the cost constant cache to give it the current version of the cost
49 constants. This is done by calling Cost_constant_cache::get_cost_constants().
50 This function will just return a pointer to the current set of cost constants.
51 As time goes, new cost constant sets might be created and added to the cost
52 constant cache. In order to know when a cost constant set can be deleted,
53 reference counting is used. Each time a session asks for the cost constants,
54 the reference counter is incremented. When the session releases the cost
55 constant set by calling @c release_cost_constants(), the reference counter
56 will be decremented. When the reference counter becomes zero, the cost
57 constant set is deleted.
58*/
60 public:
61 /**
62 Destructor for the cost constant cache. Before the cost constant cache
63 is deleted, @c close() must have been called.
64 */
66
67 /**
68 Initialize the cost module.
69
70 The cost constants will be initialized with the default values found in
71 the source code. To start using the cost constant values found in
72 the configuration tables, the @c reload() function must be called.
73 */
74 void init();
75
76 /**
77 Close the cost constant cache.
78
79 All resources owned by the cost constant cache are released.
80 */
81 void close();
82
83 /**
84 Reload all cost constants from the configuration tables.
85 */
86 void reload();
87
88 /**
89 Get the currently used set of cost constants.
90
91 This function will just return a pointer to a shared version of the
92 cost constants. For tracking of how many sessions that is using the
93 set and to be able to know when it is safe to delete the cost constant
94 object, reference counting is used. This function will increase the
95 ref count for the returned cost constant object. To decrease the reference
96 counter when the cost constants are no longer used,
97 @c release_cost_constants() must be called.
98
99 @note To ensure that the reference counter is only incremented once for
100 each session that uses the cost constant set, this function should only
101 be called once per session.
102
103 @return pointer to the cost constants
104 */
107
108 // Increase the ref count on the cost constant object
110
112
114 }
115
116 /*
117 Same as above, but for hypergraph cost constants.
118 */
121
122 // Increase the ref count on the cost constant object
124
126
128 }
129
130 /**
131 Releases the cost constant set.
132
133 This will decrement the reference counter on the cost constant set and
134 if nobody is using it, it will be deleted. This function should be
135 called each time a client (a session) no longer has any use for a
136 cost constant set that it has previously gotten from calling
137 @c get_cost_constants()
138
139 @param cost_constants pointer to the cost constant set
140 */
141 void release_cost_constants(const Cost_model_constants *cost_constants) {
142 assert(cost_constants != nullptr);
143
144 /*
145 The reason for using a const cast here is to be able to keep
146 the cost constant object const outside of this module.
147 */
149 const_cast<Cost_model_constants *>(cost_constants);
150
152
153 const unsigned int ref_count = cost->dec_ref_count();
154
155 // If none is using these cost constants then delete them
156 if (ref_count == 0) delete cost;
157
159 }
160
161 private:
162 /**
163 Create default cost constants.
164
165 This will create cost constants based on default values defined in the
166 source code.
167 */
169
170 /**
171 Replace the current cost constants with a new set of cost constants.
172
173 @param new_cost_constants The new cost constants.
174 @param optimizer The optimizer to update cost constants for.
175 */
177 Optimizer optimizer);
178
179 /**
180 The current set of cost constants that will be used by new sessions.
181 */
183
184 /**
185 The current set of cost constants that will be used with the hypergraph
186 optimizer by new sessions.
187 */
189
190 /**
191 Mutex protecting the pointer to the current cost constant set and
192 reference counting on all cost constant sets.
193 */
195
196 bool m_inited = false;
197};
198
199/**
200 Initializes the optimizer cost module. This should be done during
201 startup from mysqld.cc.
202*/
203void init_optimizer_cost_module(bool enable_plugins);
204
205/**
206 Deletes the optimizer cost module. This should be called when
207 the server stops to release allocated resources.
208*/
210
211/**
212 Reloads the optimizer cost constants from the cost constant tables.
213
214 @note In order to read the cost constant tables, a THD is needed. This
215 function will create a new temporary THD that will be used for
216 this. In case the caller already has a THD this will not be used.
217*/
219
220#endif /* OPT_COSTCONSTANTCACHE_INCLUDED */
This class implements a cache for "cost constant sets".
Definition: opt_costconstantcache.h:59
void init()
Initialize the cost module.
Definition: opt_costconstantcache.cc:65
~Cost_constant_cache()
Destructor for the cost constant cache.
Definition: opt_costconstantcache.cc:58
Cost_model_constants * create_defaults(Optimizer optimizer) const
Create default cost constants.
Definition: opt_costconstantcache.cc:147
void reload()
Reload all cost constants from the configuration tables.
Definition: opt_costconstantcache.cc:121
bool m_inited
Definition: opt_costconstantcache.h:196
Cost_model_constants * current_cost_constants
The current set of cost constants that will be used by new sessions.
Definition: opt_costconstantcache.h:182
const Cost_model_constants * get_cost_constants_hypergraph()
Definition: opt_costconstantcache.h:119
Cost_model_constants * current_cost_constants_hypergraph
The current set of cost constants that will be used with the hypergraph optimizer by new sessions.
Definition: opt_costconstantcache.h:188
mysql_mutex_t LOCK_cost_const
Mutex protecting the pointer to the current cost constant set and reference counting on all cost cons...
Definition: opt_costconstantcache.h:194
const Cost_model_constants * get_cost_constants()
Get the currently used set of cost constants.
Definition: opt_costconstantcache.h:105
void close()
Close the cost constant cache.
Definition: opt_costconstantcache.cc:94
void release_cost_constants(const Cost_model_constants *cost_constants)
Releases the cost constant set.
Definition: opt_costconstantcache.h:141
void update_current_cost_constants(Cost_model_constants *new_cost_constants, Optimizer optimizer)
Replace the current cost constants with a new set of cost constants.
Definition: opt_costconstantcache.cc:153
Set of all cost constants used by the server and all storage engines.
Definition: opt_costconstants.h:424
unsigned int dec_ref_count()
Decrement the reference counter for this cost constant set.
Definition: opt_costconstants.h:510
void inc_ref_count()
Increment the reference counter for this cost constant set.
Definition: opt_costconstants.h:499
#define mysql_mutex_lock(M)
Definition: mysql_mutex.h:50
#define mysql_mutex_unlock(M)
Definition: mysql_mutex.h:57
ABI for instrumented mutexes.
void delete_optimizer_cost_module()
Deletes the optimizer cost module.
Definition: opt_costconstantcache.cc:468
void init_optimizer_cost_module(bool enable_plugins)
Initializes the optimizer cost module.
Definition: opt_costconstantcache.cc:457
void reload_optimizer_cost_constants()
Reloads the optimizer cost constants from the cost constant tables.
Definition: opt_costconstantcache.cc:476
Optimizer
Definition: opt_costconstants.h:38
Instrumentation helpers for mutexes.
An instrumented mutex structure.
Definition: mysql_mutex_bits.h:50