mySQL 5.0.11 sources for tomato
[tomato.git] / release / src / router / mysql / sql / sql_profile.h
blobfca17a60af607d669bfa28b5b211c61829afcac5
1 /*
2 Copyright (c) 2007, 2012, Oracle and/or its affiliates. All rights reserved.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; version 2 of the License.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 #ifndef _SQL_PROFILE_H
19 #define _SQL_PROFILE_H
21 #ifndef __func__
22 #ifdef __FUNCTION__
23 #define __func__ __FUNCTION__
24 #else
25 #define __func__ "unknown function"
26 #endif
27 #endif
29 extern ST_FIELD_INFO query_profile_statistics_info[];
30 int fill_query_profile_statistics_info(THD *thd, TABLE_LIST *tables, Item *cond);
31 int make_profile_table_for_show(THD *thd, ST_SCHEMA_TABLE *schema_table);
34 #define PROFILE_NONE (uint)0
35 #define PROFILE_CPU (uint)(1<<0)
36 #define PROFILE_MEMORY (uint)(1<<1)
37 #define PROFILE_BLOCK_IO (uint)(1<<2)
38 #define PROFILE_CONTEXT (uint)(1<<3)
39 #define PROFILE_PAGE_FAULTS (uint)(1<<4)
40 #define PROFILE_IPC (uint)(1<<5)
41 #define PROFILE_SWAPS (uint)(1<<6)
42 #define PROFILE_SOURCE (uint)(1<<16)
43 #define PROFILE_ALL (uint)(~0)
46 #if defined(ENABLED_PROFILING) && defined(COMMUNITY_SERVER)
47 #include "mysql_priv.h"
49 #ifdef HAVE_SYS_RESOURCE_H
50 #include <sys/resource.h>
51 #endif
54 class PROF_MEASUREMENT;
55 class QUERY_PROFILE;
56 class PROFILING;
59 /**
60 Implements a persistent FIFO using server List method names. Not
61 thread-safe. Intended to be used on thread-local data only.
63 template <class T> class Queue
65 private:
67 struct queue_item
69 T *payload;
70 struct queue_item *next, *previous;
73 struct queue_item *first, *last;
75 public:
76 Queue()
78 elements= 0;
79 first= last= NULL;
82 void empty()
84 struct queue_item *i, *after_i;
85 for (i= first; i != NULL; i= after_i)
87 after_i= i->next;
88 my_free((char *) i, MYF(0));
90 elements= 0;
93 ulong elements; /* The count of items in the Queue */
95 void push_back(T *payload)
97 struct queue_item *new_item;
99 new_item= (struct queue_item *) my_malloc(sizeof(struct queue_item), MYF(0));
101 new_item->payload= payload;
103 if (first == NULL)
104 first= new_item;
105 if (last != NULL)
107 DBUG_ASSERT(last->next == NULL);
108 last->next= new_item;
110 new_item->previous= last;
111 new_item->next= NULL;
112 last= new_item;
114 elements++;
117 T *pop()
119 struct queue_item *old_item= first;
120 T *ret= NULL;
122 if (first == NULL)
124 DBUG_PRINT("warning", ("tried to pop nonexistent item from Queue"));
125 return NULL;
128 ret= old_item->payload;
129 if (first->next != NULL)
130 first->next->previous= NULL;
131 else
132 last= NULL;
133 first= first->next;
135 my_free((char *)old_item, MYF(0));
136 elements--;
138 return ret;
141 bool is_empty()
143 DBUG_ASSERT(((elements > 0) && (first != NULL)) || ((elements == 0) || (first == NULL)));
144 return (elements == 0);
147 void *new_iterator()
149 return first;
152 void *iterator_next(void *current)
154 return ((struct queue_item *) current)->next;
157 T *iterator_value(void *current)
159 return ((struct queue_item *) current)->payload;
166 A single entry in a single profile.
168 class PROF_MEASUREMENT
170 private:
171 friend class QUERY_PROFILE;
172 friend class PROFILING;
174 QUERY_PROFILE *profile;
175 char *status;
176 #ifdef HAVE_GETRUSAGE
177 struct rusage rusage;
178 #endif
180 char *function;
181 char *file;
182 unsigned int line;
184 ulong m_seq;
185 double time_usecs;
186 char *allocated_status_memory;
188 void set_label(const char *status_arg, const char *function_arg,
189 const char *file_arg, unsigned int line_arg);
190 void clean_up();
192 PROF_MEASUREMENT();
193 PROF_MEASUREMENT(QUERY_PROFILE *profile_arg, const char *status_arg);
194 PROF_MEASUREMENT(QUERY_PROFILE *profile_arg, const char *status_arg,
195 const char *function_arg,
196 const char *file_arg, unsigned int line_arg);
197 ~PROF_MEASUREMENT();
198 void collect();
203 The full profile for a single query, and includes multiple PROF_MEASUREMENT
204 objects.
206 class QUERY_PROFILE
208 private:
209 friend class PROFILING;
211 PROFILING *profiling;
213 query_id_t profiling_query_id; /* Session-specific id. */
214 char *query_source;
216 double m_start_time_usecs;
217 double m_end_time_usecs;
218 ulong m_seq_counter;
219 Queue<PROF_MEASUREMENT> entries;
222 QUERY_PROFILE(PROFILING *profiling_arg, const char *status_arg);
223 ~QUERY_PROFILE();
225 void set_query_source(char *query_source_arg, uint query_length_arg);
227 /* Add a profile status change to the current profile. */
228 void new_status(const char *status_arg,
229 const char *function_arg,
230 const char *file_arg, unsigned int line_arg);
232 /* Reset the contents of this profile entry. */
233 void reset();
235 /* Show this profile. This is called by PROFILING. */
236 bool show(uint options);
241 Profiling state for a single THD; contains multiple QUERY_PROFILE objects.
243 class PROFILING
245 private:
246 friend class PROF_MEASUREMENT;
247 friend class QUERY_PROFILE;
250 Not the system query_id, but a counter unique to profiling.
252 query_id_t profile_id_counter;
253 THD *thd;
254 bool keeping;
255 bool enabled;
257 QUERY_PROFILE *current;
258 QUERY_PROFILE *last;
259 Queue<QUERY_PROFILE> history;
261 query_id_t next_profile_id() { return(profile_id_counter++); }
263 public:
264 PROFILING();
265 ~PROFILING();
266 void set_query_source(char *query_source_arg, uint query_length_arg);
268 void start_new_query(const char *initial_state= "starting");
270 void discard_current_query();
272 void finish_current_query();
274 void status_change(const char *status_arg,
275 const char *function_arg,
276 const char *file_arg, unsigned int line_arg);
278 inline void set_thd(THD *thd_arg) { thd= thd_arg; };
280 /* SHOW PROFILES */
281 bool show_profiles();
283 /* ... from INFORMATION_SCHEMA.PROFILING ... */
284 int fill_statistics_info(THD *thd, TABLE_LIST *tables, Item *cond);
287 # endif /* HAVE_PROFILING */
288 #endif /* _SQL_PROFILE_H */