MySQL 8.0.37
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
37 is responsible for creating the set of cost constant, giving new
38 sessions access to the latest versions of the cost constants, and
39 for re-reading the cost constant tables in the case where these have
40 been updated.
41
42 The cost constant cache keeps a copy of the current set of cost
43 constants. Each time a new session initializes its Cost_model_server
44 object (by calling Cost_model_server::init() in lex_start()), the
45 Cost_model_server object will request the cost constant cache to
46 give it the current version of the cost constants. This is done by
47 calling Cost_constant_cache::get_cost_constants(). This function
48 will just return a pointer to the current set of cost constants. As
49 time goes, new cost constant sets might be created and added to the
50 cost constant cache. In order to know when a cost constant set can
51 be deleted, reference counting is used. Each time a session asks for
52 the cost constants, the reference counter is incremented. When the
53 session releases the cost constant set by calling
54 @c release_cost_constants(), the reference counter will be
55 decremented. When the reference counter becomes zero, the cost
56 constant set is deleted.
57*/
58
60 public:
61 /**
62 Creates an empty cost constant cache. To initialize it with default
63 cost constants, @c init() must be called. To use cost constants from
64 the cost constant tables, @c reload() must be called.
65 */
67
68 /**
69 Destructor for the cost constant cache. Before the cost constant cache
70 is deleted, @c close() must have been called.
71 */
73
74 /**
75 Initialize the cost module.
76
77 The cost constants will be initialized with the default values found in
78 the source code. To start using the cost constant values found in
79 the configuration tables, the @c reload() function must be called.
80 */
81
82 void init();
83
84 /**
85 Close the cost constant cache.
86
87 All resources owned by the cost constant cache are released.
88 */
89
90 void close();
91
92 /**
93 Reload all cost constants from the configuration tables.
94 */
95
96 void reload();
97
98 /**
99 Get the currently used set of cost constants.
100
101 This function will just return a pointer to a shared version of the
102 cost constants. For tracking of how many sessions that is using the
103 set and to be able to know when it is safe to delete the cost constant
104 object, reference counting is used. This function will increase the
105 ref count for the returned cost constant object. To decrease the reference
106 counter when the cost constants are no longer used,
107 @c release_cost_constants() must be called.
108
109 @note To ensure that the reference counter is only incremented once for
110 each session that uses the cost constant set, this function should only
111 be called once per session.
112
113 @return pointer to the cost constants
114 */
115
118
119 // Increase the ref count on the cost constant object
121
123
125 }
126
127 /**
128 Releases the cost constant set.
129
130 This will decrement the reference counter on the cost constant set and
131 if nobody is using it, it will be deleted. This function should be
132 called each time a client (a session) no longer has any use for a
133 cost constant set that it has previously gotten from calling
134 @c get_cost_constants()
135
136 @param cost_constants pointer to the cost constant set
137 */
138
139 void release_cost_constants(const Cost_model_constants *cost_constants) {
140 assert(cost_constants != nullptr);
141
142 /*
143 The reason for using a const cast here is to be able to keep
144 the cost constant object const outside of this module.
145 */
147 const_cast<Cost_model_constants *>(cost_constants);
148
150
151 const unsigned int ref_count = cost->dec_ref_count();
152
154
155 // If none is using these cost constants then delete them
156 if (ref_count == 0) delete cost;
157 }
158
159 private:
160 /**
161 Create default cost constants.
162
163 This will create cost constants based on default values defined in the
164 source code.
165 */
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 */
174
175 void update_current_cost_constants(Cost_model_constants *new_cost_constants);
176
177 /**
178 The current set of cost constants that will be used by new sessions.
179 */
181
182 /**
183 Mutex protecting the pointer to the current cost constant set and
184 reference counting on all cost constant sets.
185 */
187
189};
190
191/**
192 Initializes the optimizer cost module. This should be done during
193 startup from mysqld.cc.
194*/
195
196void init_optimizer_cost_module(bool enable_plugins);
197
198/**
199 Deletes the optimizer cost module. This should be called when
200 the server stops to release allocated resources.
201*/
202
204
205/**
206 Reloads the optimizer cost constants from the cost constant tables.
207
208 @note In order to read the cost constant tables, a THD is needed. This
209 function will create a new temporary THD that will be used for
210 this. In case the caller already has a THD this will not be used.
211*/
213
214#endif /* OPT_COSTCONSTANTCACHE_INCLUDED */
This class implements a cache for "cost constant sets".
Definition: opt_costconstantcache.h:59
Cost_model_constants * create_defaults() const
Create default cost constants.
Definition: opt_costconstantcache.cc:125
void init()
Initialize the cost module.
Definition: opt_costconstantcache.cc:72
~Cost_constant_cache()
Destructor for the cost constant cache.
Definition: opt_costconstantcache.cc:66
void reload()
Reload all cost constants from the configuration tables.
Definition: opt_costconstantcache.cc:111
bool m_inited
Definition: opt_costconstantcache.h:188
Cost_model_constants * current_cost_constants
The current set of cost constants that will be used by new sessions.
Definition: opt_costconstantcache.h:180
void update_current_cost_constants(Cost_model_constants *new_cost_constants)
Replace the current cost constants with a new set of cost constants.
Definition: opt_costconstantcache.cc:132
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:186
const Cost_model_constants * get_cost_constants()
Get the currently used set of cost constants.
Definition: opt_costconstantcache.h:116
void close()
Close the cost constant cache.
Definition: opt_costconstantcache.cc:89
void release_cost_constants(const Cost_model_constants *cost_constants)
Releases the cost constant set.
Definition: opt_costconstantcache.h:139
Cost_constant_cache()
Creates an empty cost constant cache.
Definition: opt_costconstantcache.cc:63
Set of all cost constants used by the server and all storage engines.
Definition: opt_costconstants.h:417
unsigned int dec_ref_count()
Decrement the reference counter for this cost constant set.
Definition: opt_costconstants.h:503
void inc_ref_count()
Increment the reference counter for this cost constant set.
Definition: opt_costconstants.h:492
#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:449
void init_optimizer_cost_module(bool enable_plugins)
Initializes the optimizer cost module.
Definition: opt_costconstantcache.cc:438
void reload_optimizer_cost_constants()
Reloads the optimizer cost constants from the cost constant tables.
Definition: opt_costconstantcache.cc:457
Instrumentation helpers for mutexes.
An instrumented mutex structure.
Definition: mysql_mutex_bits.h:50