MySQL 8.2.0
Source Code Documentation
sql_profile.h
Go to the documentation of this file.
1/* Copyright (c) 2007, 2023, 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 also distributed 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 included with MySQL.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License, version 2.0, for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22
23#ifndef _SQL_PROFILE_H
24#define _SQL_PROFILE_H
25
26#include "my_config.h"
27
28#include <stddef.h>
29#include <sys/types.h>
30
31#include "lex_string.h"
32#include "my_dbug.h"
33#include "my_inttypes.h"
34#include "sql/table.h"
35#include "sql/thr_malloc.h"
36
37class Item;
38class THD;
39
41
44int make_profile_table_for_show(THD *thd, ST_SCHEMA_TABLE *schema_table);
45
46#define PROFILE_NONE (uint)0
47#define PROFILE_CPU (uint)(1 << 0)
48#define PROFILE_MEMORY (uint)(1 << 1)
49#define PROFILE_BLOCK_IO (uint)(1 << 2)
50#define PROFILE_CONTEXT (uint)(1 << 3)
51#define PROFILE_PAGE_FAULTS (uint)(1 << 4)
52#define PROFILE_IPC (uint)(1 << 5)
53#define PROFILE_SWAPS (uint)(1 << 6)
54#define PROFILE_SOURCE (uint)(1 << 16)
55#define PROFILE_ALL (uint)(~0)
56
57#if defined(ENABLED_PROFILING)
58
59#ifdef HAVE_SYS_RESOURCE_H
60#include <sys/resource.h>
61#endif
62
64
66
67class PROFILING;
68class QUERY_PROFILE;
69
70/**
71 Implements a persistent FIFO using server List method names. Not
72 thread-safe. Intended to be used on thread-local data only.
73*/
74template <class T>
75class Queue {
76 private:
77 struct queue_item {
80 };
81
83
84 public:
86 elements = 0;
87 first = last = nullptr;
88 }
89
90 void empty() {
91 struct queue_item *i, *after_i;
92 for (i = first; i != NULL; i = after_i) {
93 after_i = i->next;
94 my_free(i);
95 }
96 elements = 0;
97 }
98
99 ulong elements; /* The count of items in the Queue */
100
101 void push_back(T *payload) {
102 struct queue_item *new_item;
103
104 new_item = (struct queue_item *)my_malloc(
105 key_memory_queue_item, sizeof(struct queue_item), MYF(0));
106
107 new_item->payload = payload;
108
109 if (first == nullptr) first = new_item;
110 if (last != nullptr) {
111 assert(last->next == nullptr);
112 last->next = new_item;
113 }
114 new_item->previous = last;
115 new_item->next = nullptr;
116 last = new_item;
117
118 elements++;
119 }
120
121 T *pop() {
122 struct queue_item *old_item = first;
123 T *ret = nullptr;
124
125 if (first == nullptr) {
126 DBUG_PRINT("warning", ("tried to pop nonexistent item from Queue"));
127 return nullptr;
128 }
129
130 ret = old_item->payload;
131 if (first->next != nullptr)
132 first->next->previous = nullptr;
133 else
134 last = nullptr;
135 first = first->next;
136
137 my_free(old_item);
138 elements--;
139
140 return ret;
141 }
142
143 bool is_empty() {
144 assert(((elements > 0) && (first != nullptr)) ||
145 ((elements == 0) || (first == nullptr)));
146 return (elements == 0);
147 }
148
149 void *new_iterator() { return first; }
150
151 void *iterator_next(void *current) {
152 return ((struct queue_item *)current)->next;
153 }
154
155 T *iterator_value(void *current) {
156 return ((struct queue_item *)current)->payload;
157 }
158};
159
160/**
161 A single entry in a single profile.
162*/
164 private:
165 friend class QUERY_PROFILE;
166 friend class PROFILING;
167
169 const char *status;
170#ifdef HAVE_GETRUSAGE
172#elif defined(_WIN32)
173 FILETIME ftKernel, ftUser;
174#endif
175
176 const char *function;
177 const char *file;
178 unsigned int line;
179
180 ulong m_seq;
183
184 void set_label(const char *status_arg, const char *function_arg,
185 const char *file_arg, unsigned int line_arg);
186 PROF_MEASUREMENT(QUERY_PROFILE *profile_arg, const char *status_arg);
187 PROF_MEASUREMENT(QUERY_PROFILE *profile_arg, const char *status_arg,
188 const char *function_arg, const char *file_arg,
189 unsigned int line_arg);
191 void collect();
192};
193
194/**
195 The full profile for a single query, and includes multiple PROF_MEASUREMENT
196 objects.
197*/
199 private:
200 friend class PROFILING;
201
203
204 query_id_t profiling_query_id; /* Session-specific id. */
206
211
212 QUERY_PROFILE(PROFILING *profiling_arg, const char *status_arg);
214
215 void set_query_source(const char *query_source_arg, size_t query_length_arg);
216
217 /* Add a profile status change to the current profile. */
218 void new_status(const char *status_arg, const char *function_arg,
219 const char *file_arg, unsigned int line_arg);
220};
221
222/**
223 Profiling state for a single THD; contains multiple QUERY_PROFILE objects.
224*/
226 private:
227 friend class PROF_MEASUREMENT;
228 friend class QUERY_PROFILE;
229
230 /*
231 Not the system query_id, but a counter unique to profiling.
232 */
237
241
243
244 public:
245 PROFILING();
246 ~PROFILING();
247 void set_query_source(const char *query_source_arg, size_t query_length_arg);
248
249 void start_new_query(const char *initial_state = "starting");
250
252
254
255 void status_change(const char *status_arg, const char *function_arg,
256 const char *file_arg, unsigned int line_arg);
257
258 inline void set_thd(THD *thd_arg) { thd = thd_arg; }
259
260 /* SHOW PROFILES */
261 bool show_profiles();
262
263 /* ... from INFORMATION_SCHEMA.PROFILING ... */
264 int fill_statistics_info(THD *thd, Table_ref *tables);
265 void cleanup();
266};
267
268#endif /* HAVE_PROFILING */
269#endif /* _SQL_PROFILE_H */
int64 query_id_t
Definition: binlog.h:71
Base class that is used to represent any kind of expression in a relational query.
Definition: item.h:932
Profiling state for a single THD; contains multiple QUERY_PROFILE objects.
Definition: sql_profile.h:225
void discard_current_query()
Throw away the current profile, because it's useless or unwanted or corrupted.
Definition: sql_profile.cc:392
void set_thd(THD *thd_arg)
Definition: sql_profile.h:258
void status_change(const char *status_arg, const char *function_arg, const char *file_arg, unsigned int line_arg)
A new state is given, and that signals the profiler to start a new timed step for the current query's...
Definition: sql_profile.cc:349
void cleanup()
Clear all the profiling information.
Definition: sql_profile.cc:697
bool enabled
Definition: sql_profile.h:236
bool show_profiles()
Definition: sql_profile.cc:431
bool keeping
Definition: sql_profile.h:235
query_id_t profile_id_counter
Definition: sql_profile.h:233
THD * thd
Definition: sql_profile.h:234
void finish_current_query()
Try to save the current profile entry, clean up the data if it shouldn't be saved,...
Definition: sql_profile.cc:404
int fill_statistics_info(THD *thd, Table_ref *tables)
Fill the information schema table, "query_profile", as defined in show.cc .
Definition: sql_profile.cc:501
Queue< QUERY_PROFILE > history
Definition: sql_profile.h:240
~PROFILING()
Definition: sql_profile.cc:334
QUERY_PROFILE * current
Definition: sql_profile.h:238
void set_query_source(const char *query_source_arg, size_t query_length_arg)
At a point in execution where we know the query source, save the text of it in the query profile.
Definition: sql_profile.cc:484
PROFILING()
Definition: sql_profile.cc:331
void start_new_query(const char *initial_state="starting")
Prepare to start processing a new query.
Definition: sql_profile.cc:369
QUERY_PROFILE * last
Definition: sql_profile.h:239
query_id_t next_profile_id()
Definition: sql_profile.h:242
A single entry in a single profile.
Definition: sql_profile.h:163
PROF_MEASUREMENT(QUERY_PROFILE *profile_arg, const char *status_arg)
Definition: sql_profile.cc:190
double time_usecs
Definition: sql_profile.h:181
void collect()
This updates the statistics for this moment of time.
Definition: sql_profile.cc:263
~PROF_MEASUREMENT()
Definition: sql_profile.cc:206
struct rusage rusage
Definition: sql_profile.h:171
ulong m_seq
Definition: sql_profile.h:180
void set_label(const char *status_arg, const char *function_arg, const char *file_arg, unsigned int line_arg)
Definition: sql_profile.cc:211
const char * file
Definition: sql_profile.h:177
QUERY_PROFILE * profile
Definition: sql_profile.h:168
char * allocated_status_memory
Definition: sql_profile.h:182
const char * status
Definition: sql_profile.h:169
unsigned int line
Definition: sql_profile.h:178
The full profile for a single query, and includes multiple PROF_MEASUREMENT objects.
Definition: sql_profile.h:198
void set_query_source(const char *query_source_arg, size_t query_length_arg)
Definition: sql_profile.cc:297
double m_start_time_usecs
Definition: sql_profile.h:207
~QUERY_PROFILE()
Definition: sql_profile.cc:288
query_id_t profiling_query_id
Definition: sql_profile.h:204
QUERY_PROFILE(PROFILING *profiling_arg, const char *status_arg)
Definition: sql_profile.cc:276
LEX_STRING m_query_source
Definition: sql_profile.h:205
void new_status(const char *status_arg, const char *function_arg, const char *file_arg, unsigned int line_arg)
Definition: sql_profile.cc:310
Queue< PROF_MEASUREMENT > entries
Definition: sql_profile.h:210
ulong m_seq_counter
Definition: sql_profile.h:209
PROFILING * profiling
Definition: sql_profile.h:202
double m_end_time_usecs
Definition: sql_profile.h:208
Implements a persistent FIFO using server List method names.
Definition: sql_profile.h:75
void * iterator_next(void *current)
Definition: sql_profile.h:151
struct queue_item * first
Definition: sql_profile.h:82
ulong elements
Definition: sql_profile.h:99
bool is_empty()
Definition: sql_profile.h:143
void push_back(T *payload)
Definition: sql_profile.h:101
struct queue_item * last
Definition: sql_profile.h:82
void empty()
Definition: sql_profile.h:90
T * iterator_value(void *current)
Definition: sql_profile.h:155
Queue()
Definition: sql_profile.h:85
void * new_iterator()
Definition: sql_profile.h:149
T * pop()
Definition: sql_profile.h:121
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:35
Definition: table.h:2846
unsigned int PSI_memory_key
Instrumented memory key.
Definition: psi_memory_bits.h:48
#define DBUG_PRINT(keyword, arglist)
Definition: my_dbug.h:180
Some integer typedefs for easier portability.
int64_t int64
Definition: my_inttypes.h:67
#define MYF(v)
Definition: my_inttypes.h:96
void * my_malloc(PSI_memory_key key, size_t size, int flags)
Allocates size bytes of memory.
Definition: my_memory.cc:56
void my_free(void *ptr)
Frees the memory pointed by the ptr.
Definition: my_memory.cc:80
int64 query_id_t
Definition: sql_profile.h:38
PSI_memory_key key_memory_queue_item
Definition: psi_memory_key.cc:129
int make_profile_table_for_show(THD *thd, ST_SCHEMA_TABLE *schema_table)
Definition: sql_profile.cc:126
ST_FIELD_INFO query_profile_statistics_info[]
Definition: sql_profile.cc:100
int fill_query_profile_statistics_info(THD *thd, Table_ref *tables, Item *)
Connects Information_Schema and Profiling.
Definition: sql_profile.cc:82
Definition: mysql_lex_string.h:34
Definition: sql_profile.h:77
struct queue_item * previous
Definition: sql_profile.h:79
T * payload
Definition: sql_profile.h:78
struct queue_item * next
Definition: sql_profile.h:79
Definition: table.h:2479
Definition: table.h:2511
#define NULL
Definition: types.h:54