MySQL 8.0.39
Source Code Documentation
sql_profile.h
Go to the documentation of this file.
1/* Copyright (c) 2007, 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 _SQL_PROFILE_H
25#define _SQL_PROFILE_H
26
27#include "my_config.h"
28
29#include <stddef.h>
30#include <sys/types.h>
31
32#include "lex_string.h"
33#include "my_dbug.h"
34#include "my_inttypes.h"
35#include "sql/table.h"
36#include "sql/thr_malloc.h"
37
38class Item;
39class THD;
40
42
45int make_profile_table_for_show(THD *thd, ST_SCHEMA_TABLE *schema_table);
46
47#define PROFILE_NONE (uint)0
48#define PROFILE_CPU (uint)(1 << 0)
49#define PROFILE_MEMORY (uint)(1 << 1)
50#define PROFILE_BLOCK_IO (uint)(1 << 2)
51#define PROFILE_CONTEXT (uint)(1 << 3)
52#define PROFILE_PAGE_FAULTS (uint)(1 << 4)
53#define PROFILE_IPC (uint)(1 << 5)
54#define PROFILE_SWAPS (uint)(1 << 6)
55#define PROFILE_SOURCE (uint)(1 << 16)
56#define PROFILE_ALL (uint)(~0)
57
58#if defined(ENABLED_PROFILING)
59
60#ifdef HAVE_SYS_RESOURCE_H
61#include <sys/resource.h>
62#endif
63
65
67
68class PROFILING;
69class QUERY_PROFILE;
70
71/**
72 Implements a persistent FIFO using server List method names. Not
73 thread-safe. Intended to be used on thread-local data only.
74*/
75template <class T>
76class Queue {
77 private:
78 struct queue_item {
81 };
82
84
85 public:
87 elements = 0;
88 first = last = nullptr;
89 }
90
91 void empty() {
92 struct queue_item *i, *after_i;
93 for (i = first; i != NULL; i = after_i) {
94 after_i = i->next;
95 my_free(i);
96 }
97 elements = 0;
98 }
99
100 ulong elements; /* The count of items in the Queue */
101
102 void push_back(T *payload) {
103 struct queue_item *new_item;
104
105 new_item = (struct queue_item *)my_malloc(
106 key_memory_queue_item, sizeof(struct queue_item), MYF(0));
107
108 new_item->payload = payload;
109
110 if (first == nullptr) first = new_item;
111 if (last != nullptr) {
112 assert(last->next == nullptr);
113 last->next = new_item;
114 }
115 new_item->previous = last;
116 new_item->next = nullptr;
117 last = new_item;
118
119 elements++;
120 }
121
122 T *pop() {
123 struct queue_item *old_item = first;
124 T *ret = nullptr;
125
126 if (first == nullptr) {
127 DBUG_PRINT("warning", ("tried to pop nonexistent item from Queue"));
128 return nullptr;
129 }
130
131 ret = old_item->payload;
132 if (first->next != nullptr)
133 first->next->previous = nullptr;
134 else
135 last = nullptr;
136 first = first->next;
137
138 my_free(old_item);
139 elements--;
140
141 return ret;
142 }
143
144 bool is_empty() {
145 assert(((elements > 0) && (first != nullptr)) ||
146 ((elements == 0) || (first == nullptr)));
147 return (elements == 0);
148 }
149
150 void *new_iterator() { return first; }
151
152 void *iterator_next(void *current) {
153 return ((struct queue_item *)current)->next;
154 }
155
156 T *iterator_value(void *current) {
157 return ((struct queue_item *)current)->payload;
158 }
159};
160
161/**
162 A single entry in a single profile.
163*/
165 private:
166 friend class QUERY_PROFILE;
167 friend class PROFILING;
168
170 const char *status;
171#ifdef HAVE_GETRUSAGE
173#elif defined(_WIN32)
174 FILETIME ftKernel, ftUser;
175#endif
176
177 const char *function;
178 const char *file;
179 unsigned int line;
180
181 ulong m_seq;
184
185 void set_label(const char *status_arg, const char *function_arg,
186 const char *file_arg, unsigned int line_arg);
187 PROF_MEASUREMENT(QUERY_PROFILE *profile_arg, const char *status_arg);
188 PROF_MEASUREMENT(QUERY_PROFILE *profile_arg, const char *status_arg,
189 const char *function_arg, const char *file_arg,
190 unsigned int line_arg);
192 void collect();
193};
194
195/**
196 The full profile for a single query, and includes multiple PROF_MEASUREMENT
197 objects.
198*/
200 private:
201 friend class PROFILING;
202
204
205 query_id_t profiling_query_id; /* Session-specific id. */
207
212
213 QUERY_PROFILE(PROFILING *profiling_arg, const char *status_arg);
215
216 void set_query_source(const char *query_source_arg, size_t query_length_arg);
217
218 /* Add a profile status change to the current profile. */
219 void new_status(const char *status_arg, const char *function_arg,
220 const char *file_arg, unsigned int line_arg);
221};
222
223/**
224 Profiling state for a single THD; contains multiple QUERY_PROFILE objects.
225*/
227 private:
228 friend class PROF_MEASUREMENT;
229 friend class QUERY_PROFILE;
230
231 /*
232 Not the system query_id, but a counter unique to profiling.
233 */
238
242
244
245 public:
246 PROFILING();
247 ~PROFILING();
248 void set_query_source(const char *query_source_arg, size_t query_length_arg);
249
250 void start_new_query(const char *initial_state = "starting");
251
253
255
256 void status_change(const char *status_arg, const char *function_arg,
257 const char *file_arg, unsigned int line_arg);
258
259 inline void set_thd(THD *thd_arg) { thd = thd_arg; }
260
261 /* SHOW PROFILES */
262 bool show_profiles();
263
264 /* ... from INFORMATION_SCHEMA.PROFILING ... */
265 int fill_statistics_info(THD *thd, Table_ref *tables);
266 void cleanup();
267};
268
269#endif /* HAVE_PROFILING */
270#endif /* _SQL_PROFILE_H */
int64 query_id_t
Definition: binlog.h:72
Base class that is used to represent any kind of expression in a relational query.
Definition: item.h:853
Profiling state for a single THD; contains multiple QUERY_PROFILE objects.
Definition: sql_profile.h:226
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:259
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:237
bool show_profiles()
Definition: sql_profile.cc:431
bool keeping
Definition: sql_profile.h:236
query_id_t profile_id_counter
Definition: sql_profile.h:234
THD * thd
Definition: sql_profile.h:235
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:241
~PROFILING()
Definition: sql_profile.cc:334
QUERY_PROFILE * current
Definition: sql_profile.h:239
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:240
query_id_t next_profile_id()
Definition: sql_profile.h:243
A single entry in a single profile.
Definition: sql_profile.h:164
PROF_MEASUREMENT(QUERY_PROFILE *profile_arg, const char *status_arg)
Definition: sql_profile.cc:190
double time_usecs
Definition: sql_profile.h:182
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:172
ulong m_seq
Definition: sql_profile.h:181
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:178
QUERY_PROFILE * profile
Definition: sql_profile.h:169
char * allocated_status_memory
Definition: sql_profile.h:183
const char * status
Definition: sql_profile.h:170
unsigned int line
Definition: sql_profile.h:179
The full profile for a single query, and includes multiple PROF_MEASUREMENT objects.
Definition: sql_profile.h:199
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:208
~QUERY_PROFILE()
Definition: sql_profile.cc:288
query_id_t profiling_query_id
Definition: sql_profile.h:205
QUERY_PROFILE(PROFILING *profiling_arg, const char *status_arg)
Definition: sql_profile.cc:276
LEX_STRING m_query_source
Definition: sql_profile.h:206
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:211
ulong m_seq_counter
Definition: sql_profile.h:210
PROFILING * profiling
Definition: sql_profile.h:203
double m_end_time_usecs
Definition: sql_profile.h:209
Implements a persistent FIFO using server List method names.
Definition: sql_profile.h:76
void * iterator_next(void *current)
Definition: sql_profile.h:152
struct queue_item * first
Definition: sql_profile.h:83
ulong elements
Definition: sql_profile.h:100
bool is_empty()
Definition: sql_profile.h:144
void push_back(T *payload)
Definition: sql_profile.h:102
struct queue_item * last
Definition: sql_profile.h:83
void empty()
Definition: sql_profile.h:91
T * iterator_value(void *current)
Definition: sql_profile.h:156
Queue()
Definition: sql_profile.h:86
void * new_iterator()
Definition: sql_profile.h:150
T * pop()
Definition: sql_profile.h:122
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:34
Definition: table.h:2791
unsigned int PSI_memory_key
Instrumented memory key.
Definition: psi_memory_bits.h:49
#define DBUG_PRINT(keyword, arglist)
Definition: my_dbug.h:181
Some integer typedefs for easier portability.
int64_t int64
Definition: my_inttypes.h:68
#define MYF(v)
Definition: my_inttypes.h:97
void * my_malloc(PSI_memory_key key, size_t size, int flags)
Allocates size bytes of memory.
Definition: my_memory.cc:57
void my_free(void *ptr)
Frees the memory pointed by the ptr.
Definition: my_memory.cc:81
int64 query_id_t
Definition: sql_profile.h:39
PSI_memory_key key_memory_queue_item
Definition: psi_memory_key.cc:126
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:35
Definition: sql_profile.h:78
struct queue_item * previous
Definition: sql_profile.h:80
T * payload
Definition: sql_profile.h:79
struct queue_item * next
Definition: sql_profile.h:80
Definition: table.h:2426
Definition: table.h:2458
#define NULL
Definition: types.h:55