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