MySQL 8.0.40
Source Code Documentation
opt_trace_context.h
Go to the documentation of this file.
1/* Copyright (c) 2011, 2024, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#ifndef OPT_TRACE_CONTEXT_INCLUDED
25#define OPT_TRACE_CONTEXT_INCLUDED
26
27#include <assert.h>
28
29#include "my_inttypes.h"
31#include "prealloced_array.h"
32
33/**
34 @file
35 This contains the declaration of class Opt_trace_context, which is needed
36 to declare THD.
37 It is recommend to read opt_trace.h first.
38*/
39
40class Opt_trace_stmt; // implementation detail local to opt_trace.cc
41
43
44/**
45 @class Opt_trace_context
46
47 A per-session context which is always available at any point of execution,
48 because in practice it's accessible from THD which contains:
49 @verbatim Opt_trace_context opt_trace; @endverbatim
50 It maintains properties of the session's regarding tracing: enabled/disabled
51 state, style (all trace on one line, or not, etc), a list of all remembered
52 traces of previous and current SQL statement (as restricted by
53 OFFSET/LIMIT), and a pointer to the current (being-generated) trace (which
54 itself has a pointer to its current open object/array).
55
56 Here is why the context needs to provide the current open object/array:
57
58 @li When adding a value (scalar or array or object) to an array, or adding a
59 key/value pair to an object, we need this outer object or array (from now
60 on, we will use the term "structure" for "object or array", as both are
61 structured types).
62
63 @li The most natural way would be that the outer object would be passed in
64 argument to the adder (the function which wants to add the value or
65 key/value).
66
67 @li But tracing is sometimes produced from deep down the stack trace, with
68 many intermediate frames doing no tracing (writing nothing to the trace), so
69 it would require passing the outer structure through many levels, thus
70 modifying many function prototypes.
71 Example (in gdb "backtrace" notation: inner frames first):
72@verbatim
73 #0 Item_in_subselect::single_value_transformer
74 - opens an object for key "transformation"
75 #1 Item_in_subselect::select_in_like_transformer - does no tracing
76 #2 Item_allany_subselect::select_transformer - does no tracing
77 #3 Query_block::prepare - opens an object for key "join_preparation"
78@endverbatim
79 So the object opened in #3 would have to be passed in argument to #2 and #1
80 in order to finally reach #0 where object "transformation" would be added to
81 it.
82
83 @li So, as we cannot practically pass the object down, we rather maintain a
84 "current object or array" accessible from the Opt_trace_context context;
85 it's a pointer to an instance of Opt_trace_struct, and the function deep
86 down (frame #0) grabs it from the context, where it was depositted by the
87 function high up (frame #3 in the last example).
88*/
89
91 public:
94
95 /**
96 Starts a new trace.
97 @param support_I_S Whether this statement should have its trace in
98 information_schema
99 @param support_dbug_or_missing_priv 'true' if this statement
100 should have its trace in the dbug log (--debug),
101 or if missing_privilege() may be called on this
102 trace
103 @param end_marker For a key/(object|array) pair, should the key be
104 repeated in a comment when the object|array
105 closes? like
106 @verbatim
107 "key_foo": {
108 multi-line blah
109 } / * key_foo * /
110 @endverbatim
111 This is for human-readability only, not valid in
112 JSON. Note that YAML supports #-prefixed
113 comments (we would just need to put the next
114 item's "," before the current item's "#").
115 @param one_line Should the trace be on a single line without
116 indentation? (More compact for network transfer
117 to programs, less human-readable.)
118 @param offset Offset for restricting trace production.
119 @param limit Limit for restricting trace production.
120 @param max_mem_size Maximum allowed for cumulated size of all
121 remembered traces.
122 @param features Only these optimizer features should be traced.
123
124 @retval false ok
125 @retval true error (OOM): instance is unusable, so only
126 destructor is permitted on it; any other
127 member function has undefined effects.
128 */
129 bool start(bool support_I_S, bool support_dbug_or_missing_priv,
130 bool end_marker, bool one_line, long offset, long limit,
131 ulong max_mem_size, ulonglong features);
132
133 /**
134 Ends the current (=open, unfinished, being-generated) trace.
135
136 If @c missing_privilege() has been called between start() and end(),
137 end() restores I_S support to what it was before the call to
138 @c missing_privilege(). This is the key to ensure that missing_privilege()
139 does not disable I_S support for the rest of the connection's life!
140 */
141 void end();
142
143 /// Returns whether there is a current trace
144 bool is_started() const {
145 return unlikely(pimpl != nullptr) && pimpl->current_stmt_in_gen != nullptr;
146 }
147
148 /**
149 @returns whether the current trace writes to I_S.
150 This function should rarely be used. Don't you use this for some clever
151 optimizations bypassing opt trace!
152 */
153 bool support_I_S() const;
154
155 /**
156 Set the "original" query (not transformed, as sent by client) for the
157 current trace.
158 @param query query
159 @param length query's length
160 @param charset charset which was used to encode this query
161 */
162 void set_query(const char *query, size_t length, const CHARSET_INFO *charset);
163
164 /**
165 Brainwash: deletes all remembered traces and resets counters regarding
166 OFFSET/LIMIT (so that the next statement is considered as "at offset
167 0"). Does not reset the @@@@optimizer_trace_offset/limit variables.
168 */
169 void reset();
170
171 /// @sa parameters of Opt_trace_context::start()
172 bool get_end_marker() const { return pimpl->end_marker; }
173 /// @sa parameters of Opt_trace_context::start()
174 bool get_one_line() const { return pimpl->one_line; }
175
176 /**
177 Names of flags for @@@@optimizer_trace variable of @c sys_vars.cc :
178 @li "enabled" = tracing enabled
179 @li "one_line"= see parameter of @ref Opt_trace_context::start
180 @li "default".
181 */
182 static const char *flag_names[];
183
184 /** Flags' numeric values for @@@@optimizer_trace variable */
185 enum { FLAG_DEFAULT = 0, FLAG_ENABLED = 1 << 0, FLAG_ONE_LINE = 1 << 1 };
186
187 /**
188 Features' names for @@@@optimizer_trace_features variable of
189 @c sys_vars.cc:
190 @li "greedy_search" = the greedy search for a plan
191 @li "range_optimizer" = the cost analysis of accessing data through
192 ranges in indexes
193 @li "dynamic_range" = the range optimization performed for each record
194 when access method is dynamic range
195 @li "repeated_subselect" = the repeated execution of subselects
196 @li "default".
197 */
198 static const char *feature_names[];
199
200 /** Features' numeric values for @@@@optimizer_trace_features variable */
206 /*
207 If you add here, update feature_value of empty implementation
208 and default_features!
209 */
210 /**
211 Anything unclassified, including the top object (thus, by "inheritance
212 from parent", disabling MISC makes an empty trace).
213 This feature cannot be disabled by the user; for this it is important
214 that it always has biggest flag; flag's value itself does not matter.
215 */
216 MISC = 1 << 7
217 };
218
219 /**
220 User lacks privileges to see the current trace. Make the trace appear
221 empty in Opt_trace_info, and disable I_S for all its upcoming children.
222
223 Once a call to this function has been made, subsequent calls to it before
224 @c end() have no effects.
225 */
226 void missing_privilege();
227
228 /// Optimizer features which are traced by default.
230
231 /**
232 @returns whether an optimizer feature should be traced.
233 @param f feature
234 */
236 return unlikely(pimpl != nullptr) && (pimpl->features & f);
237 }
238
239 /**
240 Opt_trace_struct is passed Opt_trace_context*, and needs to know
241 to which statement's trace to attach, so Opt_trace_context must provide
242 this information.
243 */
246 }
247
248 /**
249 @returns the next statement to show in I_S.
250 @param[in,out] got_so_far How many statements the caller got so far
251 (by previous calls to this function); function updates this count.
252 @note traces are returned from oldest to newest.
253 */
254 const Opt_trace_stmt *get_next_stmt_for_I_S(long *got_so_far) const;
255
256 /// Temporarily disables I_S for this trace and its children.
258 ++I_S_disabled;
260 }
261
262 /**
263 Restores I_S support to what it was before the previous call to
264 disable_I_S_for_this_and_children().
265 */
266 void restore_I_S() {
267 --I_S_disabled;
268 assert(I_S_disabled >= 0);
269 if (unlikely(pimpl != nullptr)) pimpl->restore_I_S();
270 }
271
272 private:
273 /**
274 To have the smallest impact on THD's size, most of the implementation is
275 moved to a separate class Opt_trace_context_impl which is instantiated on
276 the heap when really needed. So if a connection never sets
277 @@@@optimizer_trace to "enabled=on" and does not use --debug, this heap
278 allocation never happens.
279 This class is declared here so that frequently called functions like
280 Opt_trace_context::is_started() can be inlined.
281 */
283 public:
290 offset(0),
291 limit(0),
292 since_offset_0(0) {}
293
295 void restore_I_S();
296
297 /**
298 Trace which is currently being generated, where structures are being
299 added. "in_gen" stands for "in generation", being-generated.
300
301 In simple cases it is equal to the last element of array
302 all_stmts_for_I_S. But it can be prior to it, for example when calling
303 a stored routine:
304@verbatim
305 CALL statement starts executing
306 create trace of CALL (call it "trace #1")
307 add structure to trace #1
308 add structure to trace #1
309 First sub-statement executing
310 create trace of sub-statement (call it "trace #2")
311 add structure to trace #2
312 add structure to trace #2
313 First sub-statement ends
314 add structure to trace #1
315@endverbatim
316 In the beginning, the CALL statement's trace is the newest and current;
317 when the first sub-statement is executing, that sub-statement's trace
318 is the newest and current; when the first sub-statement ends, it is
319 still the newest but it's not the current anymore: the current is then
320 again the CALL's one, where structures will be added, until a second
321 sub-statement is executed.
322 Another case is when the current statement sends only to DBUG:
323 all_stmts_for_I_S lists only traces shown in OPTIMIZER_TRACE.
324 */
326
327 /**
328 To keep track of what is the current statement, as execution goes into
329 a sub-statement, and back to the upper statement, we have a stack of
330 successive values of current_stmt_in_gen:
331 when in a statement we enter a substatement (a new trace), we push the
332 statement's trace on the stack and change current_stmt_in_gen to the
333 substatement's trace; when leaving the substatement we pop from the
334 stack and set current_stmt_in_gen to the popped value.
335 */
337
338 /**
339 List of remembered traces for putting into the OPTIMIZER_TRACE
340 table. Element 0 is the one created first, will be first row of
341 OPTIMIZER_TRACE table. The array structure fulfills those needs:
342 - to output traces "oldest first" in OPTIMIZER_TRACE
343 - to preserve traces "newest first" when @@@@optimizer_trace_offset<0
344 - to delete a trace in the middle of the list when it is permanently
345 out of the offset/limit showable window.
346 */
348 /**
349 List of traces which are unneeded because of OFFSET/LIMIT, and
350 scheduled for deletion from memory.
351 */
353
354 bool end_marker; ///< copy of parameter of Opt_trace_context::start
357 long offset;
358 long limit;
360
361 /**
362 Number of statements traced so far since "offset 0", for comparison
363 with OFFSET and LIMIT, when OFFSET >= 0.
364 */
366 };
367
368 Opt_trace_context_impl *pimpl; /// Dynamically allocated implementation.
369
370 /**
371 <>0 <=> any to-be-created statement's trace should not be in
372 information_schema. This applies to next statements, their substatements,
373 etc.
374 */
376
377 /**
378 Find and delete unneeded traces.
379 For example if OFFSET=-1,LIMIT=1, only the last trace is needed. When a
380 new trace is started, the previous traces becomes unneeded and this
381 function deletes them which frees memory.
382 @param purge_all if true, ignore OFFSET and thus delete everything
383 */
384 void purge_stmts(bool purge_all);
385
386 /**
387 Compute maximum allowed memory size for current trace. The current trace
388 is the only one active. Other traces break down in two groups:
389 - the finished ones (from previously executed statements),
390 - the "started but not finished ones": they are not current, are not
391 being updated at this moment: this must be the trace of a top
392 statement calling a substatement which is the current trace now: trace's
393 top statement is not being updated at this moment.
394 So the current trace can grow in the room left by all traces above.
395 */
397
398 /// Not defined copy constructor, to disallow copy.
400 /// Not defined assignment operator, to disallow assignment.
402};
403
404#endif /* OPT_TRACE_CONTEXT_INCLUDED */
To have the smallest impact on THD's size, most of the implementation is moved to a separate class Op...
Definition: opt_trace_context.h:282
Opt_trace_stmt_array stack_of_current_stmts
To keep track of what is the current statement, as execution goes into a sub-statement,...
Definition: opt_trace_context.h:336
Opt_trace_stmt_array all_stmts_to_del
List of traces which are unneeded because of OFFSET/LIMIT, and scheduled for deletion from memory.
Definition: opt_trace_context.h:352
size_t max_mem_size
Definition: opt_trace_context.h:359
long since_offset_0
Number of statements traced so far since "offset 0", for comparison with OFFSET and LIMIT,...
Definition: opt_trace_context.h:365
void restore_I_S()
Definition: opt_trace.cc:1112
void disable_I_S_for_this_and_children()
Definition: opt_trace.cc:1108
long offset
Definition: opt_trace_context.h:357
Opt_trace_context_impl()
Definition: opt_trace_context.h:284
bool end_marker
copy of parameter of Opt_trace_context::start
Definition: opt_trace_context.h:354
Opt_trace_stmt * current_stmt_in_gen
Definition: opt_trace_context.h:325
feature_value features
Definition: opt_trace_context.h:356
long limit
Definition: opt_trace_context.h:358
Opt_trace_stmt_array all_stmts_for_I_S
List of remembered traces for putting into the OPTIMIZER_TRACE table.
Definition: opt_trace_context.h:347
bool one_line
Definition: opt_trace_context.h:355
A per-session context which is always available at any point of execution, because in practice it's a...
Definition: opt_trace_context.h:90
bool get_end_marker() const
Definition: opt_trace_context.h:172
bool get_one_line() const
Definition: opt_trace_context.h:174
bool start(bool support_I_S, bool support_dbug_or_missing_priv, bool end_marker, bool one_line, long offset, long limit, ulong max_mem_size, ulonglong features)
Starts a new trace.
Definition: opt_trace.cc:818
void purge_stmts(bool purge_all)
Find and delete unneeded traces.
Definition: opt_trace.cc:991
void disable_I_S_for_this_and_children()
Temporarily disables I_S for this trace and its children.
Definition: opt_trace_context.h:257
void set_query(const char *query, size_t length, const CHARSET_INFO *charset)
Set the "original" query (not transformed, as sent by client) for the current trace.
Definition: opt_trace.cc:1096
Opt_trace_stmt * get_current_stmt_in_gen()
Opt_trace_struct is passed Opt_trace_context*, and needs to know to which statement's trace to attach...
Definition: opt_trace_context.h:244
const Opt_trace_stmt * get_next_stmt_for_I_S(long *got_so_far) const
Definition: opt_trace.cc:1132
feature_value
Features' numeric values for @@optimizer_trace_features variable.
Definition: opt_trace_context.h:201
@ REPEATED_SUBSELECT
Definition: opt_trace_context.h:205
@ RANGE_OPTIMIZER
Definition: opt_trace_context.h:203
@ MISC
Anything unclassified, including the top object (thus, by "inheritance from parent",...
Definition: opt_trace_context.h:216
@ GREEDY_SEARCH
Definition: opt_trace_context.h:202
@ DYNAMIC_RANGE
Definition: opt_trace_context.h:204
~Opt_trace_context()
Definition: opt_trace.cc:791
void reset()
Brainwash: deletes all remembered traces and resets counters regarding OFFSET/LIMIT (so that the next...
Definition: opt_trace.cc:1101
size_t allowed_mem_size_for_current_stmt() const
Compute maximum allowed memory size for current trace.
Definition: opt_trace.cc:1075
static const char * flag_names[]
Names of flags for @@optimizer_trace variable of sys_vars.cc :
Definition: opt_trace_context.h:182
bool support_I_S() const
Definition: opt_trace.cc:986
@ FLAG_ONE_LINE
Definition: opt_trace_context.h:185
@ FLAG_ENABLED
Definition: opt_trace_context.h:185
@ FLAG_DEFAULT
Definition: opt_trace_context.h:185
int I_S_disabled
Dynamically allocated implementation.
Definition: opt_trace_context.h:375
Opt_trace_context()
Definition: opt_trace_context.h:92
bool is_started() const
Returns whether there is a current trace.
Definition: opt_trace_context.h:144
void missing_privilege()
User lacks privileges to see the current trace.
Definition: opt_trace.cc:1116
static const char * feature_names[]
Features' names for @@optimizer_trace_features variable of sys_vars.cc:
Definition: opt_trace_context.h:198
static const feature_value default_features
Optimizer features which are traced by default.
Definition: opt_trace_context.h:229
void restore_I_S()
Restores I_S support to what it was before the previous call to disable_I_S_for_this_and_children().
Definition: opt_trace_context.h:266
Opt_trace_context & operator=(const Opt_trace_context &)
Not defined assignment operator, to disallow assignment.
Opt_trace_context_impl * pimpl
Definition: opt_trace_context.h:368
void end()
Ends the current (=open, unfinished, being-generated) trace.
Definition: opt_trace.cc:944
Opt_trace_context(const Opt_trace_context &)
Not defined copy constructor, to disallow copy.
bool feature_enabled(feature_value f) const
Definition: opt_trace_context.h:235
The trace of one statement.
Definition: opt_trace.cc:108
Fido Client Authentication nullptr
Definition: fido_client_plugin.cc:222
static constexpr unsigned PSI_INSTRUMENT_ME
Definition: psi_bits.h:43
constexpr bool unlikely(bool expr)
Definition: my_compiler.h:56
Some integer typedefs for easier portability.
unsigned long long int ulonglong
Definition: my_inttypes.h:56
static char * query
Definition: myisam_ftdump.cc:45
const std::string charset("charset")
bool length(const dd::Spatial_reference_system *srs, const Geometry *g1, double *length, bool *null) noexcept
Computes the length of linestrings and multilinestrings.
Definition: length.cc:76
Prealloced_array< Opt_trace_stmt *, 16 > Opt_trace_stmt_array
Definition: opt_trace_context.h:40
Performance schema instrumentation interface.
Definition: m_ctype.h:385