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
23 #define __func__ __FUNCTION__
25 #define __func__ "unknown function"
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>
54 class PROF_MEASUREMENT
;
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
70 struct queue_item
*next
, *previous
;
73 struct queue_item
*first
, *last
;
84 struct queue_item
*i
, *after_i
;
85 for (i
= first
; i
!= NULL
; i
= after_i
)
88 my_free((char *) i
, MYF(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
;
107 DBUG_ASSERT(last
->next
== NULL
);
108 last
->next
= new_item
;
110 new_item
->previous
= last
;
111 new_item
->next
= NULL
;
119 struct queue_item
*old_item
= first
;
124 DBUG_PRINT("warning", ("tried to pop nonexistent item from Queue"));
128 ret
= old_item
->payload
;
129 if (first
->next
!= NULL
)
130 first
->next
->previous
= NULL
;
135 my_free((char *)old_item
, MYF(0));
143 DBUG_ASSERT(((elements
> 0) && (first
!= NULL
)) || ((elements
== 0) || (first
== NULL
)));
144 return (elements
== 0);
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
171 friend class QUERY_PROFILE
;
172 friend class PROFILING
;
174 QUERY_PROFILE
*profile
;
176 #ifdef HAVE_GETRUSAGE
177 struct rusage rusage
;
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
);
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
);
203 The full profile for a single query, and includes multiple PROF_MEASUREMENT
209 friend class PROFILING
;
211 PROFILING
*profiling
;
213 query_id_t profiling_query_id
; /* Session-specific id. */
216 double m_start_time_usecs
;
217 double m_end_time_usecs
;
219 Queue
<PROF_MEASUREMENT
> entries
;
222 QUERY_PROFILE(PROFILING
*profiling_arg
, const char *status_arg
);
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. */
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.
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
;
257 QUERY_PROFILE
*current
;
259 Queue
<QUERY_PROFILE
> history
;
261 query_id_t
next_profile_id() { return(profile_id_counter
++); }
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
; };
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 */