Commit fcb52609 authored by unknown's avatar unknown

Changing the state of whether we're recording profiling information

halfway through a query (as happens in "SET SESSION PROFILING = ...")
has a few side-effects, the worst of which is a memory leak for
prepared statements, which poke directly from the parser into the 
profiling code (we don't have the query text when we need it) and 
that overwrites a pointer to heap-allocated memory when the previous
statement turns on profiling.

Instead, now set a flag when we begin a new statement that tracks 
whether profiling is on _at the start_ of the query.  Use that to
track whether we gather info.

Additionally, use that AND use the state of the profiling variable
after the end of a query to know whether to store information about 
the query that just finished.


mysql-test/r/profiling.result:
  Testing whether profiling is on at the beginning of a query and at
  the end of a query makes "SET SESSION PROFILING = ..." statements
  disappear from the profiling.  They were never reliable before.
sql/sql_profile.cc:
  Check to see if profiling was enabled at the beginning of this query
  before trying to store query_source.  This avoids a memory leak for
  prepared statements, which get here by direct means.
  
  If profiling was toggled in this query, then don't store this query
  profile.
sql/sql_profile.h:
  Keep track of whether profiling is on.
parent c717287c
This diff is collapsed.
...@@ -168,10 +168,14 @@ QUERY_PROFILE::QUERY_PROFILE(PROFILING *profiling_arg, char *query_source_arg, ...@@ -168,10 +168,14 @@ QUERY_PROFILE::QUERY_PROFILE(PROFILING *profiling_arg, char *query_source_arg,
void QUERY_PROFILE::set_query_source(char *query_source_arg, void QUERY_PROFILE::set_query_source(char *query_source_arg,
uint query_length_arg) uint query_length_arg)
{ {
if (! profiling->enabled)
return;
/* Truncate to avoid DoS attacks. */ /* Truncate to avoid DoS attacks. */
uint length= min(MAX_QUERY_LENGTH, query_length_arg); uint length= min(MAX_QUERY_LENGTH, query_length_arg);
/* TODO?: Provide a way to include the full text, as in SHOW PROCESSLIST. */ /* TODO?: Provide a way to include the full text, as in SHOW PROCESSLIST. */
DBUG_ASSERT(query_source == NULL);
if (query_source_arg != NULL) if (query_source_arg != NULL)
query_source= my_strdup_with_length(query_source_arg, length, MYF(0)); query_source= my_strdup_with_length(query_source_arg, length, MYF(0));
} }
...@@ -445,7 +449,7 @@ bool QUERY_PROFILE::show(uint options) ...@@ -445,7 +449,7 @@ bool QUERY_PROFILE::show(uint options)
} }
PROFILING::PROFILING() PROFILING::PROFILING()
:profile_id_counter(1), keeping(1), current(NULL), last(NULL) :profile_id_counter(1), keeping(TRUE), enabled(FALSE), current(NULL), last(NULL)
{ {
} }
...@@ -464,7 +468,7 @@ void PROFILING::status_change(const char *status_arg, ...@@ -464,7 +468,7 @@ void PROFILING::status_change(const char *status_arg,
{ {
DBUG_ENTER("PROFILING::status_change"); DBUG_ENTER("PROFILING::status_change");
if (unlikely((thd->options & OPTION_PROFILING) != 0)) if (unlikely(enabled))
{ {
if (unlikely(current == NULL)) if (unlikely(current == NULL))
reset(); reset();
...@@ -499,6 +503,8 @@ void PROFILING::store() ...@@ -499,6 +503,8 @@ void PROFILING::store()
if (current != NULL) if (current != NULL)
{ {
if (keeping && if (keeping &&
(enabled) && /* ON at start? */
(((thd)->options & OPTION_PROFILING) != 0) && /* and ON at end? */
(current->query_source != NULL) && (current->query_source != NULL) &&
(current->query_source[0] != '\0') && (current->query_source[0] != '\0') &&
(!current->entries.is_empty())) (!current->entries.is_empty()))
...@@ -517,7 +523,8 @@ void PROFILING::store() ...@@ -517,7 +523,8 @@ void PROFILING::store()
} }
DBUG_ASSERT(current == NULL); DBUG_ASSERT(current == NULL);
current= new QUERY_PROFILE(this, thd->query, thd->query_length); if (enabled)
current= new QUERY_PROFILE(this, thd->query, thd->query_length);
DBUG_VOID_RETURN; DBUG_VOID_RETURN;
} }
...@@ -534,7 +541,12 @@ void PROFILING::reset() ...@@ -534,7 +541,12 @@ void PROFILING::reset()
store(); store();
if (likely(((thd)->options & OPTION_PROFILING) == 0)) if (likely(((thd)->options & OPTION_PROFILING) == 0))
{
enabled= FALSE;
DBUG_VOID_RETURN; DBUG_VOID_RETURN;
}
else
enabled= TRUE;
if (current != NULL) if (current != NULL)
current->reset(); current->reset();
......
...@@ -277,6 +277,7 @@ class PROFILING ...@@ -277,6 +277,7 @@ class PROFILING
query_id_t profile_id_counter; query_id_t profile_id_counter;
THD *thd; THD *thd;
bool keeping; bool keeping;
bool enabled;
QUERY_PROFILE *current; QUERY_PROFILE *current;
QUERY_PROFILE *last; QUERY_PROFILE *last;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment