MySQL  8.0.18
Source Code Documentation
sql_profile.h
Go to the documentation of this file.
1 /* Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.
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 
37 class Item;
38 class THD;
39 
40 typedef int64 query_id_t;
41 
44 int 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 
67 class PROFILING;
68 class 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 */
74 template <class T>
75 class Queue {
76  private:
77  struct queue_item {
78  T *payload;
79  struct queue_item *next, *previous;
80  };
81 
82  struct queue_item *first, *last;
83 
84  public:
85  Queue() {
86  elements = 0;
87  first = last = NULL;
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 == NULL) first = new_item;
110  if (last != NULL) {
111  DBUG_ASSERT(last->next == NULL);
112  last->next = new_item;
113  }
114  new_item->previous = last;
115  new_item->next = NULL;
116  last = new_item;
117 
118  elements++;
119  }
120 
121  T *pop() {
122  struct queue_item *old_item = first;
123  T *ret = NULL;
124 
125  if (first == NULL) {
126  DBUG_PRINT("warning", ("tried to pop nonexistent item from Queue"));
127  return NULL;
128  }
129 
130  ret = old_item->payload;
131  if (first->next != NULL)
132  first->next->previous = NULL;
133  else
134  last = NULL;
135  first = first->next;
136 
137  my_free(old_item);
138  elements--;
139 
140  return ret;
141  }
142 
143  bool is_empty() {
144  DBUG_ASSERT(((elements > 0) && (first != NULL)) ||
145  ((elements == 0) || (first == NULL)));
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 */
163 class PROF_MEASUREMENT {
164  private:
165  friend class QUERY_PROFILE;
166  friend class PROFILING;
167 
168  QUERY_PROFILE *profile;
169  const char *status;
170 #ifdef HAVE_GETRUSAGE
171  struct rusage rusage;
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;
181  double time_usecs;
182  char *allocated_status_memory;
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);
190  ~PROF_MEASUREMENT();
191  void collect();
192 };
193 
194 /**
195  The full profile for a single query, and includes multiple PROF_MEASUREMENT
196  objects.
197 */
198 class QUERY_PROFILE {
199  private:
200  friend class PROFILING;
201 
202  PROFILING *profiling;
203 
204  query_id_t profiling_query_id; /* Session-specific id. */
205  LEX_STRING m_query_source;
206 
207  double m_start_time_usecs;
208  double m_end_time_usecs;
209  ulong m_seq_counter;
210  Queue<PROF_MEASUREMENT> entries;
211 
212  QUERY_PROFILE(PROFILING *profiling_arg, const char *status_arg);
213  ~QUERY_PROFILE();
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 */
225 class PROFILING {
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  */
233  query_id_t profile_id_counter;
234  THD *thd;
235  bool keeping;
236  bool enabled;
237 
238  QUERY_PROFILE *current;
239  QUERY_PROFILE *last;
240  Queue<QUERY_PROFILE> history;
241 
242  query_id_t next_profile_id() { return (profile_id_counter++); }
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 
251  void discard_current_query();
252 
253  void finish_current_query();
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_LIST *tables);
265  void cleanup();
266 };
267 
268 #endif /* HAVE_PROFILING */
269 #endif /* _SQL_PROFILE_H */
Definition: mysql_lex_string.h:34
int make_profile_table_for_show(THD *thd, ST_SCHEMA_TABLE *schema_table)
Definition: sql_profile.cc:125
Some integer typedefs for easier portability.
Definition: table.h:2195
Definition: win32.h:60
void my_free(void *ptr)
Frees the memory pointed by the ptr.
Definition: my_memory.cc:81
unsigned int PSI_memory_key
Instrumented memory key.
Definition: psi_memory_bits.h:46
#define DBUG_PRINT(keyword, arglist)
Definition: my_dbug.h:179
#define DBUG_ASSERT(A)
Definition: my_dbug.h:197
Definition: table.h:2227
int64_t int64
Definition: my_inttypes.h:67
PSI_memory_key key_memory_queue_item
Definition: psi_memory_key.cc:136
Definition: item.h:668
#define MYF(v)
Definition: my_inttypes.h:114
int fill_query_profile_statistics_info(THD *thd, TABLE_LIST *tables, Item *)
Connects Information_Schema and Profiling.
Definition: sql_profile.cc:81
#define PROFILING
Definition: dbug.cc:152
int64 query_id_t
Definition: binlog.h:69
#define NULL
Definition: types.h:55
static STATUS status
Definition: mysql.cc:193
Definition: table.h:2468
int64 query_id_t
Definition: sql_profile.h:38
void * my_malloc(PSI_memory_key key, size_t size, int flags)
Below functions are used by the components.
Definition: my_memory.cc:57
unsigned long ulong
Definition: my_inttypes.h:48
ST_FIELD_INFO query_profile_statistics_info[]
Definition: sql_profile.cc:99
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_class.h:778