MySQL 8.3.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, 2023, 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 also distributed 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 included with MySQL.
17
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License, version 2.0, for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
26
27#include <assert.h>
28#include <stddef.h>
29
32#include "sql/opt_costconstants.h" // Cost_model_constants
33
34/**
35 This class implements a cache for "cost constant sets". This cache is
36 responsible for creating the set of cost constant, giving new sessions access
37 to the latest versions of the cost constants, and for re-reading the cost
38 constant tables in the case where these have been updated.
39
40 To initialize the cost constant cache with default cost constants, @c init()
41 must be called. To load cost constants from the mysql.server_cost and
42 mysql.engine_cost tables, @c reload() must be called.
43
44 The cost constant cache keeps a copy of the current set of cost constants.
45 Each time a new session initializes its Cost_model_server object (by calling
46 Cost_model_server::init() in lex_start()), the Cost_model_server object will
47 request the cost constant cache to give it the current version of the cost
48 constants. This is done by calling Cost_constant_cache::get_cost_constants().
49 This function will just return a pointer to the current set of cost constants.
50 As time goes, new cost constant sets might be created and added to the cost
51 constant cache. In order to know when a cost constant set can be deleted,
52 reference counting is used. Each time a session asks for the cost constants,
53 the reference counter is incremented. When the session releases the cost
54 constant set by calling @c release_cost_constants(), the reference counter
55 will be decremented. When the reference counter becomes zero, the cost
56 constant set is deleted.
57*/
59 public:
60 /**
61 Destructor for the cost constant cache. Before the cost constant cache
62 is deleted, @c close() must have been called.
63 */
65
66 /**
67 Initialize the cost module.
68
69 The cost constants will be initialized with the default values found in
70 the source code. To start using the cost constant values found in
71 the configuration tables, the @c reload() function must be called.
72 */
73 void init();
74
75 /**
76 Close the cost constant cache.
77
78 All resources owned by the cost constant cache are released.
79 */
80 void close();
81
82 /**
83 Reload all cost constants from the configuration tables.
84 */
85 void reload();
86
87 /**
88 Get the currently used set of cost constants.
89
90 This function will just return a pointer to a shared version of the
91 cost constants. For tracking of how many sessions that is using the
92 set and to be able to know when it is safe to delete the cost constant
93 object, reference counting is used. This function will increase the
94 ref count for the returned cost constant object. To decrease the reference
95 counter when the cost constants are no longer used,
96 @c release_cost_constants() must be called.
97
98 @note To ensure that the reference counter is only incremented once for
99 each session that uses the cost constant set, this function should only
100 be called once per session.
101
102 @return pointer to the cost constants
103 */
106
107 // Increase the ref count on the cost constant object
109
111
113 }
114
115 /*
116 Same as above, but for hypergraph cost constants.
117 */
120
121 // Increase the ref count on the cost constant object
123
125
127 }
128
129 /**
130 Releases the cost constant set.
131
132 This will decrement the reference counter on the cost constant set and
133 if nobody is using it, it will be deleted. This function should be
134 called each time a client (a session) no longer has any use for a
135 cost constant set that it has previously gotten from calling
136 @c get_cost_constants()
137
138 @param cost_constants pointer to the cost constant set
139 */
140 void release_cost_constants(const Cost_model_constants *cost_constants) {
141 assert(cost_constants != nullptr);
142
143 /*
144 The reason for using a const cast here is to be able to keep
145 the cost constant object const outside of this module.
146 */
148 const_cast<Cost_model_constants *>(cost_constants);
149
151
152 const unsigned int ref_count = cost->dec_ref_count();
153
154 // If none is using these cost constants then delete them
155 if (ref_count == 0) delete cost;
156
158 }
159
160 private:
161 /**
162 Create default cost constants.
163
164 This will create cost constants based on default values defined in the
165 source code.
166 */
168
169 /**
170 Replace the current cost constants with a new set of cost constants.
171
172 @param new_cost_constants The new cost constants.
173 @param optimizer The optimizer to update cost constants for.
174 */
176 Optimizer optimizer);
177
178 /**
179 The current set of cost constants that will be used by new sessions.
180 */
182
183 /**
184 The current set of cost constants that will be used with the hypergraph
185 optimizer by new sessions.
186 */
188
189 /**
190 Mutex protecting the pointer to the current cost constant set and
191 reference counting on all cost constant sets.
192 */
194
195 bool m_inited = false;
196};
197
198/**
199 Initializes the optimizer cost module. This should be done during
200 startup from mysqld.cc.
201*/
202void init_optimizer_cost_module(bool enable_plugins);
203
204/**
205 Deletes the optimizer cost module. This should be called when
206 the server stops to release allocated resources.
207*/
209
210/**
211 Reloads the optimizer cost constants from the cost constant tables.
212
213 @note In order to read the cost constant tables, a THD is needed. This
214 function will create a new temporary THD that will be used for
215 this. In case the caller already has a THD this will not be used.
216*/
218
219#endif /* OPT_COSTCONSTANTCACHE_INCLUDED */
This class implements a cache for "cost constant sets".
Definition: opt_costconstantcache.h:58
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:195
Cost_model_constants * current_cost_constants
The current set of cost constants that will be used by new sessions.
Definition: opt_costconstantcache.h:181
const Cost_model_constants * get_cost_constants_hypergraph()
Definition: opt_costconstantcache.h:118
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:187
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:193
const Cost_model_constants * get_cost_constants()
Get the currently used set of cost constants.
Definition: opt_costconstantcache.h:104
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:140
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:423
unsigned int dec_ref_count()
Decrement the reference counter for this cost constant set.
Definition: opt_costconstants.h:509
void inc_ref_count()
Increment the reference counter for this cost constant set.
Definition: opt_costconstants.h:498
#define mysql_mutex_lock(M)
Definition: mysql_mutex.h:49
#define mysql_mutex_unlock(M)
Definition: mysql_mutex.h:56
ABI for instrumented mutexes.
void delete_optimizer_cost_module()
Deletes the optimizer cost module.
Definition: opt_costconstantcache.cc:474
void init_optimizer_cost_module(bool enable_plugins)
Initializes the optimizer cost module.
Definition: opt_costconstantcache.cc:463
void reload_optimizer_cost_constants()
Reloads the optimizer cost constants from the cost constant tables.
Definition: opt_costconstantcache.cc:482
Optimizer
Definition: opt_costconstants.h:37
Instrumentation helpers for mutexes.
An instrumented mutex structure.
Definition: mysql_mutex_bits.h:49