2013-09-06 22:31:30 +02:00
|
|
|
/* Copyright (c) 2009, 2013, Oracle and/or its affiliates.
|
2009-09-29 17:38:40 +02:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; version 2 of the License.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
2013-07-02 19:43:35 +02:00
|
|
|
along with this program; if not, write to the Free Software Foundation,
|
|
|
|
51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA */
|
2009-09-29 17:38:40 +02:00
|
|
|
|
2012-03-28 19:26:00 +02:00
|
|
|
/* see include/mysql/service_debug_sync.h for debug sync documentation */
|
2009-09-29 17:38:40 +02:00
|
|
|
|
2014-09-30 19:31:14 +02:00
|
|
|
#include <my_global.h>
|
2009-09-29 17:38:40 +02:00
|
|
|
#include "debug_sync.h"
|
|
|
|
|
|
|
|
#if defined(ENABLED_DEBUG_SYNC)
|
|
|
|
|
|
|
|
/*
|
|
|
|
Due to weaknesses in our include files, we need to include
|
2010-03-31 16:05:33 +02:00
|
|
|
sql_priv.h here. To have THD declared, we need to include
|
2009-09-29 17:38:40 +02:00
|
|
|
sql_class.h. This includes log_event.h, which in turn requires
|
2010-03-31 16:05:33 +02:00
|
|
|
declarations from sql_priv.h (e.g. OPTION_AUTO_IS_NULL).
|
|
|
|
sql_priv.h includes almost everything, so is sufficient here.
|
2009-09-29 17:38:40 +02:00
|
|
|
*/
|
2010-03-31 16:05:33 +02:00
|
|
|
#include "sql_priv.h"
|
|
|
|
#include "sql_parse.h"
|
2009-09-29 17:38:40 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
Action to perform at a synchronization point.
|
|
|
|
NOTE: This structure is moved around in memory by realloc(), qsort(),
|
|
|
|
and memmove(). Do not add objects with non-trivial constuctors
|
|
|
|
or destructors, which might prevent moving of this structure
|
|
|
|
with these functions.
|
|
|
|
*/
|
|
|
|
struct st_debug_sync_action
|
|
|
|
{
|
2013-03-25 23:03:13 +01:00
|
|
|
ulong activation_count; /* MY_MAX(hit_limit, execute) */
|
2009-09-29 17:38:40 +02:00
|
|
|
ulong hit_limit; /* hits before kill query */
|
|
|
|
ulong execute; /* executes before self-clear */
|
|
|
|
ulong timeout; /* wait_for timeout */
|
|
|
|
String signal; /* signal to emit */
|
|
|
|
String wait_for; /* signal to wait for */
|
|
|
|
String sync_point; /* sync point name */
|
|
|
|
bool need_sort; /* if new action, array needs sort */
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Debug sync control. Referenced by THD. */
|
|
|
|
struct st_debug_sync_control
|
|
|
|
{
|
|
|
|
st_debug_sync_action *ds_action; /* array of actions */
|
|
|
|
uint ds_active; /* # active actions */
|
|
|
|
uint ds_allocated; /* # allocated actions */
|
|
|
|
ulonglong dsp_hits; /* statistics */
|
|
|
|
ulonglong dsp_executed; /* statistics */
|
|
|
|
ulonglong dsp_max_active; /* statistics */
|
|
|
|
/*
|
|
|
|
thd->proc_info points at unsynchronized memory.
|
|
|
|
It must not go away as long as the thread exists.
|
|
|
|
*/
|
|
|
|
char ds_proc_info[80]; /* proc_info string */
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Definitions for the debug sync facility.
|
|
|
|
1. Global string variable to hold a "signal" ("signal post", "flag mast").
|
|
|
|
2. Global condition variable for signaling and waiting.
|
|
|
|
3. Global mutex to synchronize access to the above.
|
|
|
|
*/
|
|
|
|
struct st_debug_sync_globals
|
|
|
|
{
|
|
|
|
String ds_signal; /* signal variable */
|
2009-12-10 04:19:51 +01:00
|
|
|
mysql_cond_t ds_cond; /* condition variable */
|
|
|
|
mysql_mutex_t ds_mutex; /* mutex variable */
|
2009-09-29 17:38:40 +02:00
|
|
|
ulonglong dsp_hits; /* statistics */
|
|
|
|
ulonglong dsp_executed; /* statistics */
|
|
|
|
ulonglong dsp_max_active; /* statistics */
|
|
|
|
};
|
|
|
|
static st_debug_sync_globals debug_sync_global; /* All globals in one object */
|
|
|
|
|
2010-06-05 21:39:03 +02:00
|
|
|
/**
|
|
|
|
Callbacks from C files.
|
|
|
|
*/
|
|
|
|
C_MODE_START
|
2012-03-28 19:26:00 +02:00
|
|
|
static void debug_sync(THD *thd, const char *sync_point_name, size_t name_len);
|
2010-06-05 21:39:03 +02:00
|
|
|
static int debug_sync_qsort_cmp(const void *, const void *);
|
|
|
|
C_MODE_END
|
2009-09-29 17:38:40 +02:00
|
|
|
|
2009-12-10 04:19:51 +01:00
|
|
|
#ifdef HAVE_PSI_INTERFACE
|
|
|
|
static PSI_mutex_key key_debug_sync_globals_ds_mutex;
|
|
|
|
|
|
|
|
static PSI_mutex_info all_debug_sync_mutexes[]=
|
|
|
|
{
|
|
|
|
{ &key_debug_sync_globals_ds_mutex, "DEBUG_SYNC::mutex", PSI_FLAG_GLOBAL}
|
|
|
|
};
|
|
|
|
|
|
|
|
static PSI_cond_key key_debug_sync_globals_ds_cond;
|
|
|
|
|
|
|
|
static PSI_cond_info all_debug_sync_conds[]=
|
|
|
|
{
|
|
|
|
{ &key_debug_sync_globals_ds_cond, "DEBUG_SYNC::cond", PSI_FLAG_GLOBAL}
|
|
|
|
};
|
|
|
|
|
|
|
|
static void init_debug_sync_psi_keys(void)
|
|
|
|
{
|
|
|
|
const char* category= "sql";
|
|
|
|
int count;
|
|
|
|
|
|
|
|
count= array_elements(all_debug_sync_mutexes);
|
2013-07-02 19:43:35 +02:00
|
|
|
mysql_mutex_register(category, all_debug_sync_mutexes, count);
|
2009-12-10 04:19:51 +01:00
|
|
|
|
|
|
|
count= array_elements(all_debug_sync_conds);
|
2013-07-02 19:43:35 +02:00
|
|
|
mysql_cond_register(category, all_debug_sync_conds, count);
|
2009-12-10 04:19:51 +01:00
|
|
|
}
|
|
|
|
#endif /* HAVE_PSI_INTERFACE */
|
|
|
|
|
2009-09-29 17:38:40 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
Initialize the debug sync facility at server start.
|
|
|
|
|
|
|
|
@return status
|
|
|
|
@retval 0 ok
|
|
|
|
@retval != 0 error
|
|
|
|
*/
|
|
|
|
|
|
|
|
int debug_sync_init(void)
|
|
|
|
{
|
|
|
|
DBUG_ENTER("debug_sync_init");
|
|
|
|
|
2009-12-10 04:19:51 +01:00
|
|
|
#ifdef HAVE_PSI_INTERFACE
|
|
|
|
init_debug_sync_psi_keys();
|
|
|
|
#endif
|
|
|
|
|
2009-09-29 17:38:40 +02:00
|
|
|
if (opt_debug_sync_timeout)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
/* Initialize the global variables. */
|
|
|
|
debug_sync_global.ds_signal.length(0);
|
2009-12-10 04:19:51 +01:00
|
|
|
if ((rc= mysql_cond_init(key_debug_sync_globals_ds_cond,
|
|
|
|
&debug_sync_global.ds_cond, NULL)) ||
|
|
|
|
(rc= mysql_mutex_init(key_debug_sync_globals_ds_mutex,
|
|
|
|
&debug_sync_global.ds_mutex,
|
|
|
|
MY_MUTEX_INIT_FAST)))
|
2009-09-29 17:38:40 +02:00
|
|
|
DBUG_RETURN(rc); /* purecov: inspected */
|
|
|
|
|
|
|
|
/* Set the call back pointer in C files. */
|
2012-03-28 19:26:00 +02:00
|
|
|
debug_sync_C_callback_ptr= debug_sync;
|
2009-09-29 17:38:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
DBUG_RETURN(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
End the debug sync facility.
|
|
|
|
|
|
|
|
@description
|
|
|
|
This is called at server shutdown or after a thread initialization error.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void debug_sync_end(void)
|
|
|
|
{
|
|
|
|
DBUG_ENTER("debug_sync_end");
|
|
|
|
|
|
|
|
/* End the facility only if it had been initialized. */
|
|
|
|
if (debug_sync_C_callback_ptr)
|
|
|
|
{
|
|
|
|
/* Clear the call back pointer in C files. */
|
|
|
|
debug_sync_C_callback_ptr= NULL;
|
|
|
|
|
|
|
|
/* Destroy the global variables. */
|
|
|
|
debug_sync_global.ds_signal.free();
|
2009-12-10 04:19:51 +01:00
|
|
|
mysql_cond_destroy(&debug_sync_global.ds_cond);
|
|
|
|
mysql_mutex_destroy(&debug_sync_global.ds_mutex);
|
2009-09-29 17:38:40 +02:00
|
|
|
|
|
|
|
/* Print statistics. */
|
|
|
|
{
|
|
|
|
char llbuff[22];
|
|
|
|
sql_print_information("Debug sync points hit: %22s",
|
|
|
|
llstr(debug_sync_global.dsp_hits, llbuff));
|
|
|
|
sql_print_information("Debug sync points executed: %22s",
|
|
|
|
llstr(debug_sync_global.dsp_executed, llbuff));
|
|
|
|
sql_print_information("Debug sync points max active per thread: %22s",
|
|
|
|
llstr(debug_sync_global.dsp_max_active, llbuff));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DBUG_VOID_RETURN;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* purecov: begin tested */
|
|
|
|
|
|
|
|
/**
|
|
|
|
Disable the facility after lack of memory if no error can be returned.
|
|
|
|
|
|
|
|
@note
|
|
|
|
Do not end the facility here because the global variables can
|
|
|
|
be in use by other threads.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void debug_sync_emergency_disable(void)
|
|
|
|
{
|
|
|
|
DBUG_ENTER("debug_sync_emergency_disable");
|
|
|
|
|
|
|
|
opt_debug_sync_timeout= 0;
|
|
|
|
|
|
|
|
DBUG_PRINT("debug_sync",
|
|
|
|
("Debug Sync Facility disabled due to lack of memory."));
|
|
|
|
sql_print_error("Debug Sync Facility disabled due to lack of memory.");
|
|
|
|
|
|
|
|
DBUG_VOID_RETURN;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* purecov: end */
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Initialize the debug sync facility at thread start.
|
|
|
|
|
|
|
|
@param[in] thd thread handle
|
|
|
|
*/
|
|
|
|
|
|
|
|
void debug_sync_init_thread(THD *thd)
|
|
|
|
{
|
|
|
|
DBUG_ENTER("debug_sync_init_thread");
|
|
|
|
DBUG_ASSERT(thd);
|
|
|
|
|
|
|
|
if (opt_debug_sync_timeout)
|
|
|
|
{
|
|
|
|
thd->debug_sync_control= (st_debug_sync_control*)
|
MDEV-4011 Added per thread memory counting and usage
Base code and idea from a patch from by plinux at Taobao.
The idea is that we mark all memory that are thread specific with MY_THREAD_SPECIFIC.
Memory counting is done per thread in the my_malloc_size_cb_func callback function from my_malloc().
There are plenty of new asserts to ensure that for a debug server the counting is correct.
Information_schema.processlist gets two new columns: MEMORY_USED and EXAMINED_ROWS.
- The later is there mainly to show how query is progressing.
The following changes in interfaces was needed to get this to work:
- init_alloc_root() amd init_sql_alloc() has extra option so that one can mark memory with MY_THREAD_SPECIFIC
- One now have to use alloc_root_set_min_malloc() to set min memory to be allocated by alloc_root()
- my_init_dynamic_array() has extra option so that one can mark memory with MY_THREAD_SPECIFIC
- my_net_init() has extra option so that one can mark memory with MY_THREAD_SPECIFIC
- Added flag for hash_init() so that one can mark hash table to be thread specific.
- Added flags to init_tree() so that one can mark tree to be thread specific.
- Removed with_delete option to init_tree(). Now one should instead use MY_TREE_WITH_DELETE_FLAG.
- Added flag to Warning_info::Warning_info() if the structure should be fully initialized.
- String elements can now be marked as thread specific.
- Internal HEAP tables are now marking it's memory as MY_THREAD_SPECIFIC.
- Changed type of myf from int to ulong, as this is always a set of bit flags.
Other things:
- Removed calls to net_end() and thd->cleanup() as these are now done in ~THD()
- We now also show EXAMINED_ROWS in SHOW PROCESSLIST
- Added new variable 'memory_used'
- Fixed bug where kill_threads_for_user() was using the wrong mem_root to allocate memory.
- Removed calls to the obsoleted function init_dynamic_array()
- Use set_current_thd() instead of my_pthread_setspecific_ptr(THR_THD,...)
client/completion_hash.cc:
Updated call to init_alloc_root()
client/mysql.cc:
Updated call to init_alloc_root()
client/mysqlbinlog.cc:
init_dynamic_array() -> my_init_dynamic_array()
Updated call to init_alloc_root()
client/mysqlcheck.c:
Updated call to my_init_dynamic_array()
client/mysqldump.c:
Updated call to init_alloc_root()
client/mysqltest.cc:
Updated call to init_alloc_root()
Updated call to my_init_dynamic_array()
Fixed compiler warnings
extra/comp_err.c:
Updated call to my_init_dynamic_array()
extra/resolve_stack_dump.c:
Updated call to my_init_dynamic_array()
include/hash.h:
Added HASH_THREAD_SPECIFIC
include/heap.h:
Added flag is internal temporary table.
include/my_dir.h:
Safety fix: Ensure that MY_DONT_SORT and MY_WANT_STAT don't interfer with other mysys flags
include/my_global.h:
Changed type of myf from int to ulong, as this is always a set of bit flags.
include/my_sys.h:
Added MY_THREAD_SPECIFIC and MY_THREAD_MOVE
Added malloc_flags to DYNAMIC_ARRAY
Added extra mysys flag argument to my_init_dynamic_array()
Removed deprecated functions init_dynamic_array() and my_init_dynamic_array.._ci
Updated paramaters for init_alloc_root()
include/my_tree.h:
Added my_flags to allow one to use MY_THREAD_SPECIFIC with hash tables.
Removed with_delete. One should now instead use MY_TREE_WITH_DELETE_FLAG
Updated parameters to init_tree()
include/myisamchk.h:
Added malloc_flags to allow one to use MY_THREAD_SPECIFIC for checks.
include/mysql.h:
Added MYSQL_THREAD_SPECIFIC_MALLOC
Used 'unused1' to mark memory as thread specific.
include/mysql.h.pp:
Updated file
include/mysql_com.h:
Used 'unused1' to mark memory as thread specific.
Updated parameters for my_net_init()
libmysql/libmysql.c:
Updated call to init_alloc_root() to mark memory thread specific.
libmysqld/emb_qcache.cc:
Updated call to init_alloc_root()
libmysqld/lib_sql.cc:
Updated call to init_alloc_root()
mysql-test/r/create.result:
Updated results
mysql-test/r/user_var.result:
Updated results
mysql-test/suite/funcs_1/datadict/processlist_priv.inc:
Update to handle new format of SHOW PROCESSLIST
mysql-test/suite/funcs_1/datadict/processlist_val.inc:
Update to handle new format of SHOW PROCESSLIST
mysql-test/suite/funcs_1/r/is_columns_is.result:
Update to handle new format of SHOW PROCESSLIST
mysql-test/suite/funcs_1/r/processlist_priv_no_prot.result:
Updated results
mysql-test/suite/funcs_1/r/processlist_val_no_prot.result:
Updated results
mysql-test/t/show_explain.test:
Fixed usage of debug variable so that one can run test with --debug
mysql-test/t/user_var.test:
Added test of memory_usage variable.
mysys/array.c:
Added extra my_flags option to init_dynamic_array() and init_dynamic_array2() so that one can mark memory with MY_THREAD_SPECIFIC
All allocated memory is marked with the given my_flags.
Removed obsolete function init_dynamic_array()
mysys/default.c:
Updated call to init_alloc_root()
Updated call to my_init_dynamic_array()
mysys/hash.c:
Updated call to my_init_dynamic_array_ci().
Allocated memory is marked with MY_THREAD_SPECIFIC if HASH_THREAD_SPECIFIC is used.
mysys/ma_dyncol.c:
init_dynamic_array() -> my_init_dynamic_array()
Added #if to get rid of compiler warnings
mysys/mf_tempdir.c:
Updated call to my_init_dynamic_array()
mysys/my_alloc.c:
Added extra parameter to init_alloc_root() so that one can mark memory with MY_THREAD_SPECIFIC
Extend MEM_ROOT with a flag if memory is thread specific.
This is stored in block_size, to keep the size of the MEM_ROOT object identical as before.
Allocated memory is marked with MY_THREAD_SPECIFIC if used with init_alloc_root()
mysys/my_chmod.c:
Updated DBUG_PRINT because of change of myf type
mysys/my_chsize.c:
Updated DBUG_PRINT because of change of myf type
mysys/my_copy.c:
Updated DBUG_PRINT because of change of myf type
mysys/my_create.c:
Updated DBUG_PRINT because of change of myf type
mysys/my_delete.c:
Updated DBUG_PRINT because of change of myf type
mysys/my_error.c:
Updated DBUG_PRINT because of change of myf type
mysys/my_fopen.c:
Updated DBUG_PRINT because of change of myf type
mysys/my_fstream.c:
Updated DBUG_PRINT because of change of myf type
mysys/my_getwd.c:
Updated DBUG_PRINT because of change of myf type
mysys/my_lib.c:
Updated call to init_alloc_root()
Updated call to my_init_dynamic_array()
Updated DBUG_PRINT because of change of myf type
mysys/my_lock.c:
Updated DBUG_PRINT because of change of myf type
mysys/my_malloc.c:
Store at start of each allocated memory block the size of the block and if the block is thread specific.
Call malloc_size_cb_func, if set, with the memory allocated/freed.
Updated DBUG_PRINT because of change of myf type
mysys/my_open.c:
Updated DBUG_PRINT because of change of myf type
mysys/my_pread.c:
Updated DBUG_PRINT because of change of myf type
mysys/my_read.c:
Updated DBUG_PRINT because of change of myf type
mysys/my_redel.c:
Updated DBUG_PRINT because of change of myf type
mysys/my_rename.c:
Updated DBUG_PRINT because of change of myf type
mysys/my_seek.c:
Updated DBUG_PRINT because of change of myf type
mysys/my_sync.c:
Updated DBUG_PRINT because of change of myf type
mysys/my_thr_init.c:
Ensure that one can call my_thread_dbug_id() even if thread is not properly initialized.
mysys/my_write.c:
Updated DBUG_PRINT because of change of myf type
mysys/mysys_priv.h:
Updated parameters to sf_malloc and sf_realloc()
mysys/safemalloc.c:
Added checking that for memory marked with MY_THREAD_SPECIFIC that it's the same thread that is allocation and freeing the memory.
Added sf_malloc_dbug_id() to allow MariaDB to specify which THD is handling the memory.
Added my_flags arguments to sf_malloc() and sf_realloc() to be able to mark memory with MY_THREAD_SPECIFIC.
Added sf_report_leaked_memory() to get list of memory not freed by a thread.
mysys/tree.c:
Added flags to init_tree() so that one can mark tree to be thread specific.
Removed with_delete option to init_tree(). Now one should instead use MY_TREE_WITH_DELETE_FLAG.
Updated call to init_alloc_root()
All allocated memory is marked with the given malloc flags
mysys/waiting_threads.c:
Updated call to my_init_dynamic_array()
sql-common/client.c:
Updated call to init_alloc_root() and my_net_init() to mark memory thread specific.
Updated call to my_init_dynamic_array().
Added MYSQL_THREAD_SPECIFIC_MALLOC so that client can mark memory as MY_THREAD_SPECIFIC.
sql-common/client_plugin.c:
Updated call to init_alloc_root()
sql/debug_sync.cc:
Added MY_THREAD_SPECIFIC to allocated memory.
sql/event_scheduler.cc:
Removed calls to net_end() as this is now done in ~THD()
Call set_current_thd() to ensure that memory is assigned to right thread.
sql/events.cc:
my_pthread_setspecific_ptr(THR_THD,...) -> set_current_thd()
sql/filesort.cc:
Added MY_THREAD_SPECIFIC to allocated memory.
sql/filesort_utils.cc:
Added MY_THREAD_SPECIFIC to allocated memory.
sql/ha_ndbcluster.cc:
Updated call to init_alloc_root()
Updated call to my_net_init()
Removed calls to net_end() and thd->cleanup() as these are now done in ~THD()
sql/ha_ndbcluster_binlog.cc:
Updated call to my_net_init()
Updated call to init_sql_alloc()
Removed calls to net_end() and thd->cleanup() as these are now done in ~THD()
sql/ha_partition.cc:
Updated call to init_alloc_root()
sql/handler.cc:
Added MY_THREAD_SPECIFIC to allocated memory.
Added missing call to my_dir_end()
sql/item_func.cc:
Added MY_THREAD_SPECIFIC to allocated memory.
sql/item_subselect.cc:
Added MY_THREAD_SPECIFIC to allocated memory.
sql/item_sum.cc:
Added MY_THREAD_SPECIFIC to allocated memory.
sql/log.cc:
More DBUG
Updated call to init_alloc_root()
sql/mdl.cc:
Added MY_THREAD_SPECIFIC to allocated memory.
sql/mysqld.cc:
Added total_memory_used
Updated call to init_alloc_root()
Move mysql_cond_broadcast() before my_thread_end()
Added mariadb_dbug_id() to count memory per THD instead of per thread.
Added my_malloc_size_cb_func() callback function for my_malloc() to count memory.
Move initialization of mysqld_server_started and mysqld_server_initialized earlier.
Updated call to my_init_dynamic_array().
Updated call to my_net_init().
Call my_pthread_setspecific_ptr(THR_THD,...) to ensure that memory is assigned to right thread.
Added status variable 'memory_used'.
Updated call to init_alloc_root()
my_pthread_setspecific_ptr(THR_THD,...) -> set_current_thd()
sql/mysqld.h:
Added set_current_thd()
sql/net_serv.cc:
Added new parameter to my_net_init() so that one can mark memory with MY_THREAD_SPECIFIC.
Store in net->thread_specific_malloc if memory is thread specific.
Mark memory to be thread specific if requested.
sql/opt_range.cc:
Updated call to my_init_dynamic_array()
Updated call to init_sql_alloc()
Added MY_THREAD_SPECIFIC to allocated memory.
sql/opt_subselect.cc:
Updated call to init_sql_alloc() to mark memory thread specific.
sql/protocol.cc:
Fixed compiler warning
sql/records.cc:
Added MY_THREAD_SPECIFIC to allocated memory.
sql/rpl_filter.cc:
Updated call to my_init_dynamic_array()
sql/rpl_handler.cc:
Updated call to my_init_dynamic_array2()
sql/rpl_handler.h:
Updated call to init_sql_alloc()
sql/rpl_mi.cc:
Updated call to my_init_dynamic_array()
sql/rpl_tblmap.cc:
Updated call to init_alloc_root()
sql/rpl_utility.cc:
Updated call to my_init_dynamic_array()
sql/slave.cc:
Initialize things properly before calling functions that allocate memory.
Removed calls to net_end() as this is now done in ~THD()
sql/sp_head.cc:
Updated call to init_sql_alloc()
Updated call to my_init_dynamic_array()
Added parameter to warning_info() that it should be fully initialized.
sql/sp_pcontext.cc:
Updated call to my_init_dynamic_array()
sql/sql_acl.cc:
Updated call to init_sql_alloc()
Updated call to my_init_dynamic_array()
my_pthread_setspecific_ptr(THR_THD,...) -> set_current_thd()
sql/sql_admin.cc:
Added parameter to warning_info() that it should be fully initialized.
sql/sql_analyse.h:
Updated call to init_tree() to mark memory thread specific.
sql/sql_array.h:
Updated call to my_init_dynamic_array() to mark memory thread specific.
sql/sql_audit.cc:
Updated call to my_init_dynamic_array()
sql/sql_base.cc:
Updated call to init_sql_alloc()
my_pthread_setspecific_ptr(THR_THD,...) -> set_current_thd()
sql/sql_cache.cc:
Updated comment
sql/sql_class.cc:
Added parameter to warning_info() that not initialize it until THD is fully created.
Updated call to init_sql_alloc()
Mark THD::user_vars has to be thread specific.
Updated call to my_init_dynamic_array()
Ensure that memory allocated by THD is assigned to the THD.
More DBUG
Always acll net_end() in ~THD()
Assert that all memory signed to this THD is really deleted at ~THD.
Fixed set_status_var_init() to not reset memory_used.
my_pthread_setspecific_ptr(THR_THD,...) -> set_current_thd()
sql/sql_class.h:
Added MY_THREAD_SPECIFIC to allocated memory.
Added malloc_size to THD to record allocated memory per THD.
sql/sql_delete.cc:
Added MY_THREAD_SPECIFIC to allocated memory.
sql/sql_error.cc:
Added 'initialize' parameter to Warning_info() to say if should allocate memory for it's structures.
This is used by THD::THD() to not allocate memory until THD is ready.
Added Warning_info::free_memory()
sql/sql_error.h:
Updated Warning_info() class.
sql/sql_handler.cc:
Updated call to init_alloc_root() to mark memory thread specific.
sql/sql_insert.cc:
More DBUG
sql/sql_join_cache.cc:
Added MY_THREAD_SPECIFIC to allocated memory.
sql/sql_lex.cc:
Updated call to my_init_dynamic_array()
sql/sql_lex.h:
Updated call to my_init_dynamic_array()
sql/sql_load.cc:
Added MY_THREAD_SPECIFIC to allocated memory.
sql/sql_parse.cc:
Removed calls to net_end() and thd->cleanup() as these are now done in ~THD()
Ensure that examined_row_count() is reset before query.
Fixed bug where kill_threads_for_user() was using the wrong mem_root to allocate memory.
my_pthread_setspecific_ptr(THR_THD,...) -> set_current_thd()
Don't restore thd->status_var.memory_used when restoring thd->status_var
sql/sql_plugin.cc:
Updated call to init_alloc_root()
Updated call to my_init_dynamic_array()
Don't allocate THD on the stack, as this causes problems with valgrind when doing thd memory counting.
my_pthread_setspecific_ptr(THR_THD,...) -> set_current_thd()
sql/sql_prepare.cc:
Added parameter to warning_info() that it should be fully initialized.
Updated call to init_sql_alloc() to mark memory thread specific.
sql/sql_reload.cc:
my_pthread_setspecific_ptr(THR_THD,...) -> set_current_thd()
sql/sql_select.cc:
Updated call to my_init_dynamic_array() and init_sql_alloc() to mark memory thread specific.
Added MY_THREAD_SPECIFIC to allocated memory.
More DBUG
sql/sql_servers.cc:
Updated call to init_sql_alloc() to mark memory some memory thread specific.
my_pthread_setspecific_ptr(THR_THD,...) -> set_current_thd()
sql/sql_show.cc:
Updated call to my_init_dynamic_array()
Mark my_dir() memory thread specific.
Use my_pthread_setspecific_ptr(THR_THD,...) to mark that allocated memory should be allocated to calling thread.
More DBUG.
Added malloc_size and examined_row_count to SHOW PROCESSLIST.
Added MY_THREAD_SPECIFIC to allocated memory.
Updated call to init_sql_alloc()
Added parameter to warning_info() that it should be fully initialized.
sql/sql_statistics.cc:
Fixed compiler warning
sql/sql_string.cc:
String elements can now be marked as thread specific.
sql/sql_string.h:
String elements can now be marked as thread specific.
sql/sql_table.cc:
Updated call to init_sql_alloc() and my_malloc() to mark memory thread specific
my_pthread_setspecific_ptr(THR_THD,...) -> set_current_thd()
Fixed compiler warning
sql/sql_test.cc:
Updated call to my_init_dynamic_array() to mark memory thread specific.
sql/sql_trigger.cc:
Updated call to init_sql_alloc()
sql/sql_udf.cc:
Updated call to init_sql_alloc()
my_pthread_setspecific_ptr(THR_THD,...) -> set_current_thd()
sql/sql_update.cc:
Added MY_THREAD_SPECIFIC to allocated memory.
sql/table.cc:
Updated call to init_sql_alloc().
Mark memory used by temporary tables, that are not for slave threads, as MY_THREAD_SPECIFIC
Updated call to init_sql_alloc()
sql/thr_malloc.cc:
Added my_flags argument to init_sql_alloc() to be able to mark memory as MY_THREAD_SPECIFIC.
sql/thr_malloc.h:
Updated prototype for init_sql_alloc()
sql/tztime.cc:
Updated call to init_sql_alloc()
Updated call to init_alloc_root() to mark memory thread specific.
my_pthread_setspecific_ptr(THR_THD,...) -> set_current_thd()
sql/uniques.cc:
Updated calls to init_tree(), my_init_dynamic_array() and my_malloc() to mark memory thread specific.
sql/unireg.cc:
Added MY_THREAD_SPECIFIC to allocated memory.
storage/csv/ha_tina.cc:
Updated call to init_alloc_root()
storage/federated/ha_federated.cc:
Updated call to init_alloc_root()
Updated call to my_init_dynamic_array()
Ensure that memory allocated by fedarated is registered for the system, not for the thread.
storage/federatedx/federatedx_io_mysql.cc:
Updated call to my_init_dynamic_array()
storage/federatedx/ha_federatedx.cc:
Updated call to init_alloc_root()
Updated call to my_init_dynamic_array()
storage/heap/ha_heap.cc:
Added MY_THREAD_SPECIFIC to allocated memory.
storage/heap/heapdef.h:
Added parameter to hp_get_new_block() to be able to do thread specific memory tagging.
storage/heap/hp_block.c:
Added parameter to hp_get_new_block() to be able to do thread specific memory tagging.
storage/heap/hp_create.c:
- Internal HEAP tables are now marking it's memory as MY_THREAD_SPECIFIC.
- Use MY_TREE_WITH_DELETE instead of removed option 'with_delete'.
storage/heap/hp_open.c:
Internal HEAP tables are now marking it's memory as MY_THREAD_SPECIFIC.
storage/heap/hp_write.c:
Added new parameter to hp_get_new_block()
storage/maria/ma_bitmap.c:
Updated call to my_init_dynamic_array()
storage/maria/ma_blockrec.c:
Updated call to my_init_dynamic_array()
storage/maria/ma_check.c:
Updated call to init_alloc_root()
storage/maria/ma_ft_boolean_search.c:
Updated calls to init_tree() and init_alloc_root()
storage/maria/ma_ft_nlq_search.c:
Updated call to init_tree()
storage/maria/ma_ft_parser.c:
Updated call to init_tree()
Updated call to init_alloc_root()
storage/maria/ma_loghandler.c:
Updated call to my_init_dynamic_array()
storage/maria/ma_open.c:
Updated call to my_init_dynamic_array()
storage/maria/ma_sort.c:
Updated call to my_init_dynamic_array()
storage/maria/ma_write.c:
Updated calls to my_init_dynamic_array() and init_tree()
storage/maria/maria_pack.c:
Updated call to init_tree()
storage/maria/unittest/sequence_storage.c:
Updated call to my_init_dynamic_array()
storage/myisam/ft_boolean_search.c:
Updated call to init_tree()
Updated call to init_alloc_root()
storage/myisam/ft_nlq_search.c:
Updated call to init_tree()
storage/myisam/ft_parser.c:
Updated call to init_tree()
Updated call to init_alloc_root()
storage/myisam/ft_stopwords.c:
Updated call to init_tree()
storage/myisam/mi_check.c:
Updated call to init_alloc_root()
storage/myisam/mi_write.c:
Updated call to my_init_dynamic_array()
Updated call to init_tree()
storage/myisam/myisamlog.c:
Updated call to init_tree()
storage/myisam/myisampack.c:
Updated call to init_tree()
storage/myisam/sort.c:
Updated call to my_init_dynamic_array()
storage/myisammrg/ha_myisammrg.cc:
Updated call to init_sql_alloc()
storage/perfschema/pfs_check.cc:
Rest current_thd
storage/perfschema/pfs_instr.cc:
Removed DBUG_ENTER/DBUG_VOID_RETURN as at this point my_thread_var is not allocated anymore, which can cause problems.
support-files/compiler_warnings.supp:
Disable compiler warning from offsetof macro.
2013-01-23 16:16:14 +01:00
|
|
|
my_malloc(sizeof(st_debug_sync_control),
|
|
|
|
MYF(MY_WME | MY_ZEROFILL | MY_THREAD_SPECIFIC));
|
2009-09-29 17:38:40 +02:00
|
|
|
if (!thd->debug_sync_control)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
Error is reported by my_malloc().
|
|
|
|
We must disable the facility. We have no way to return an error.
|
|
|
|
*/
|
|
|
|
debug_sync_emergency_disable(); /* purecov: tested */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DBUG_VOID_RETURN;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
End the debug sync facility at thread end.
|
|
|
|
|
|
|
|
@param[in] thd thread handle
|
|
|
|
*/
|
|
|
|
|
|
|
|
void debug_sync_end_thread(THD *thd)
|
|
|
|
{
|
|
|
|
DBUG_ENTER("debug_sync_end_thread");
|
|
|
|
DBUG_ASSERT(thd);
|
|
|
|
|
|
|
|
if (thd->debug_sync_control)
|
|
|
|
{
|
|
|
|
st_debug_sync_control *ds_control= thd->debug_sync_control;
|
|
|
|
|
|
|
|
/*
|
|
|
|
This synchronization point can be used to synchronize on thread end.
|
|
|
|
This is the latest point in a THD's life, where this can be done.
|
|
|
|
*/
|
|
|
|
DEBUG_SYNC(thd, "thread_end");
|
|
|
|
|
|
|
|
if (ds_control->ds_action)
|
|
|
|
{
|
|
|
|
st_debug_sync_action *action= ds_control->ds_action;
|
|
|
|
st_debug_sync_action *action_end= action + ds_control->ds_allocated;
|
|
|
|
for (; action < action_end; action++)
|
|
|
|
{
|
|
|
|
action->signal.free();
|
|
|
|
action->wait_for.free();
|
|
|
|
action->sync_point.free();
|
|
|
|
}
|
Bug#34043: Server loops excessively in _checkchunk() when safemalloc is enabled
Essentially, the problem is that safemalloc is excruciatingly
slow as it checks all allocated blocks for overrun at each
memory management primitive, yielding a almost exponential
slowdown for the memory management functions (malloc, realloc,
free). The overrun check basically consists of verifying some
bytes of a block for certain magic keys, which catches some
simple forms of overrun. Another minor problem is violation
of aliasing rules and that its own internal list of blocks
is prone to corruption.
Another issue with safemalloc is rather the maintenance cost
as the tool has a significant impact on the server code.
Given the magnitude of memory debuggers available nowadays,
especially those that are provided with the platform malloc
implementation, maintenance of a in-house and largely obsolete
memory debugger becomes a burden that is not worth the effort
due to its slowness and lack of support for detecting more
common forms of heap corruption.
Since there are third-party tools that can provide the same
functionality at a lower or comparable performance cost, the
solution is to simply remove safemalloc. Third-party tools
can provide the same functionality at a lower or comparable
performance cost.
The removal of safemalloc also allows a simplification of the
malloc wrappers, removing quite a bit of kludge: redefinition
of my_malloc, my_free and the removal of the unused second
argument of my_free. Since free() always check whether the
supplied pointer is null, redudant checks are also removed.
Also, this patch adds unit testing for my_malloc and moves
my_realloc implementation into the same file as the other
memory allocation primitives.
client/mysqldump.c:
Pass my_free directly as its signature is compatible with the
callback type -- which wasn't the case for free_table_ent.
2010-07-08 23:20:08 +02:00
|
|
|
my_free(ds_control->ds_action);
|
2009-09-29 17:38:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Statistics. */
|
2009-12-10 04:19:51 +01:00
|
|
|
mysql_mutex_lock(&debug_sync_global.ds_mutex);
|
2009-09-29 17:38:40 +02:00
|
|
|
debug_sync_global.dsp_hits+= ds_control->dsp_hits;
|
|
|
|
debug_sync_global.dsp_executed+= ds_control->dsp_executed;
|
|
|
|
if (debug_sync_global.dsp_max_active < ds_control->dsp_max_active)
|
|
|
|
debug_sync_global.dsp_max_active= ds_control->dsp_max_active;
|
2009-12-10 04:19:51 +01:00
|
|
|
mysql_mutex_unlock(&debug_sync_global.ds_mutex);
|
2009-09-29 17:38:40 +02:00
|
|
|
|
Bug#34043: Server loops excessively in _checkchunk() when safemalloc is enabled
Essentially, the problem is that safemalloc is excruciatingly
slow as it checks all allocated blocks for overrun at each
memory management primitive, yielding a almost exponential
slowdown for the memory management functions (malloc, realloc,
free). The overrun check basically consists of verifying some
bytes of a block for certain magic keys, which catches some
simple forms of overrun. Another minor problem is violation
of aliasing rules and that its own internal list of blocks
is prone to corruption.
Another issue with safemalloc is rather the maintenance cost
as the tool has a significant impact on the server code.
Given the magnitude of memory debuggers available nowadays,
especially those that are provided with the platform malloc
implementation, maintenance of a in-house and largely obsolete
memory debugger becomes a burden that is not worth the effort
due to its slowness and lack of support for detecting more
common forms of heap corruption.
Since there are third-party tools that can provide the same
functionality at a lower or comparable performance cost, the
solution is to simply remove safemalloc. Third-party tools
can provide the same functionality at a lower or comparable
performance cost.
The removal of safemalloc also allows a simplification of the
malloc wrappers, removing quite a bit of kludge: redefinition
of my_malloc, my_free and the removal of the unused second
argument of my_free. Since free() always check whether the
supplied pointer is null, redudant checks are also removed.
Also, this patch adds unit testing for my_malloc and moves
my_realloc implementation into the same file as the other
memory allocation primitives.
client/mysqldump.c:
Pass my_free directly as its signature is compatible with the
callback type -- which wasn't the case for free_table_ent.
2010-07-08 23:20:08 +02:00
|
|
|
my_free(ds_control);
|
2009-09-29 17:38:40 +02:00
|
|
|
thd->debug_sync_control= NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
DBUG_VOID_RETURN;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Move a string by length.
|
|
|
|
|
|
|
|
@param[out] to buffer for the resulting string
|
|
|
|
@param[in] to_end end of buffer
|
|
|
|
@param[in] from source string
|
|
|
|
@param[in] length number of bytes to copy
|
|
|
|
|
|
|
|
@return pointer to end of copied string
|
|
|
|
*/
|
|
|
|
|
|
|
|
static char *debug_sync_bmove_len(char *to, char *to_end,
|
|
|
|
const char *from, size_t length)
|
|
|
|
{
|
|
|
|
DBUG_ASSERT(to);
|
|
|
|
DBUG_ASSERT(to_end);
|
|
|
|
DBUG_ASSERT(!length || from);
|
|
|
|
set_if_smaller(length, (size_t) (to_end - to));
|
|
|
|
memcpy(to, from, length);
|
|
|
|
return (to + length);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#if !defined(DBUG_OFF)
|
|
|
|
|
|
|
|
/**
|
|
|
|
Create a string that describes an action.
|
|
|
|
|
|
|
|
@param[out] result buffer for the resulting string
|
|
|
|
@param[in] size size of result buffer
|
|
|
|
@param[in] action action to describe
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void debug_sync_action_string(char *result, uint size,
|
|
|
|
st_debug_sync_action *action)
|
|
|
|
{
|
|
|
|
char *wtxt= result;
|
|
|
|
char *wend= wtxt + size - 1; /* Allow emergency '\0'. */
|
|
|
|
DBUG_ASSERT(result);
|
|
|
|
DBUG_ASSERT(action);
|
|
|
|
|
|
|
|
/* If an execute count is present, signal or wait_for are needed too. */
|
|
|
|
DBUG_ASSERT(!action->execute ||
|
|
|
|
action->signal.length() || action->wait_for.length());
|
|
|
|
|
|
|
|
if (action->execute)
|
|
|
|
{
|
|
|
|
if (action->signal.length())
|
|
|
|
{
|
|
|
|
wtxt= debug_sync_bmove_len(wtxt, wend, STRING_WITH_LEN("SIGNAL "));
|
|
|
|
wtxt= debug_sync_bmove_len(wtxt, wend, action->signal.ptr(),
|
|
|
|
action->signal.length());
|
|
|
|
}
|
|
|
|
if (action->wait_for.length())
|
|
|
|
{
|
|
|
|
if ((wtxt == result) && (wtxt < wend))
|
|
|
|
*(wtxt++)= ' ';
|
|
|
|
wtxt= debug_sync_bmove_len(wtxt, wend, STRING_WITH_LEN(" WAIT_FOR "));
|
|
|
|
wtxt= debug_sync_bmove_len(wtxt, wend, action->wait_for.ptr(),
|
|
|
|
action->wait_for.length());
|
|
|
|
|
|
|
|
if (action->timeout != opt_debug_sync_timeout)
|
|
|
|
{
|
|
|
|
wtxt+= my_snprintf(wtxt, wend - wtxt, " TIMEOUT %lu", action->timeout);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (action->execute != 1)
|
|
|
|
{
|
|
|
|
wtxt+= my_snprintf(wtxt, wend - wtxt, " EXECUTE %lu", action->execute);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (action->hit_limit)
|
|
|
|
{
|
|
|
|
wtxt+= my_snprintf(wtxt, wend - wtxt, "%sHIT_LIMIT %lu",
|
|
|
|
(wtxt == result) ? "" : " ", action->hit_limit);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
If (wtxt == wend) string may not be terminated.
|
|
|
|
There is one byte left for an emergency termination.
|
|
|
|
*/
|
|
|
|
*wtxt= '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Print actions.
|
|
|
|
|
|
|
|
@param[in] thd thread handle
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void debug_sync_print_actions(THD *thd)
|
|
|
|
{
|
|
|
|
st_debug_sync_control *ds_control= thd->debug_sync_control;
|
|
|
|
uint idx;
|
|
|
|
DBUG_ENTER("debug_sync_print_actions");
|
|
|
|
DBUG_ASSERT(thd);
|
|
|
|
|
|
|
|
if (!ds_control)
|
2009-10-01 15:54:11 +02:00
|
|
|
DBUG_VOID_RETURN;
|
2009-09-29 17:38:40 +02:00
|
|
|
|
|
|
|
for (idx= 0; idx < ds_control->ds_active; idx++)
|
|
|
|
{
|
|
|
|
const char *dsp_name= ds_control->ds_action[idx].sync_point.c_ptr();
|
|
|
|
char action_string[256];
|
|
|
|
|
|
|
|
debug_sync_action_string(action_string, sizeof(action_string),
|
|
|
|
ds_control->ds_action + idx);
|
|
|
|
DBUG_PRINT("debug_sync_list", ("%s %s", dsp_name, action_string));
|
|
|
|
}
|
|
|
|
|
|
|
|
DBUG_VOID_RETURN;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* !defined(DBUG_OFF) */
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Compare two actions by sync point name length, string.
|
|
|
|
|
|
|
|
@param[in] arg1 reference to action1
|
|
|
|
@param[in] arg2 reference to action2
|
|
|
|
|
|
|
|
@return difference
|
|
|
|
@retval == 0 length1/string1 is same as length2/string2
|
|
|
|
@retval < 0 length1/string1 is smaller
|
|
|
|
@retval > 0 length1/string1 is bigger
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int debug_sync_qsort_cmp(const void* arg1, const void* arg2)
|
|
|
|
{
|
|
|
|
st_debug_sync_action *action1= (st_debug_sync_action*) arg1;
|
|
|
|
st_debug_sync_action *action2= (st_debug_sync_action*) arg2;
|
|
|
|
int diff;
|
|
|
|
DBUG_ASSERT(action1);
|
|
|
|
DBUG_ASSERT(action2);
|
|
|
|
|
|
|
|
if (!(diff= action1->sync_point.length() - action2->sync_point.length()))
|
|
|
|
diff= memcmp(action1->sync_point.ptr(), action2->sync_point.ptr(),
|
|
|
|
action1->sync_point.length());
|
|
|
|
|
|
|
|
return diff;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Find a debug sync action.
|
|
|
|
|
|
|
|
@param[in] actionarr array of debug sync actions
|
|
|
|
@param[in] quantity number of actions in array
|
|
|
|
@param[in] dsp_name name of debug sync point to find
|
|
|
|
@param[in] name_len length of name of debug sync point
|
|
|
|
|
|
|
|
@return action
|
|
|
|
@retval != NULL found sync point in array
|
|
|
|
@retval NULL not found
|
|
|
|
|
|
|
|
@description
|
|
|
|
Binary search. Array needs to be sorted by length, sync point name.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static st_debug_sync_action *debug_sync_find(st_debug_sync_action *actionarr,
|
|
|
|
int quantity,
|
|
|
|
const char *dsp_name,
|
|
|
|
uint name_len)
|
|
|
|
{
|
|
|
|
st_debug_sync_action *action;
|
|
|
|
int low ;
|
|
|
|
int high ;
|
|
|
|
int mid ;
|
|
|
|
int diff ;
|
|
|
|
DBUG_ASSERT(actionarr);
|
|
|
|
DBUG_ASSERT(dsp_name);
|
|
|
|
DBUG_ASSERT(name_len);
|
|
|
|
|
|
|
|
low= 0;
|
|
|
|
high= quantity;
|
|
|
|
|
|
|
|
while (low < high)
|
|
|
|
{
|
|
|
|
mid= (low + high) / 2;
|
|
|
|
action= actionarr + mid;
|
|
|
|
if (!(diff= name_len - action->sync_point.length()) &&
|
|
|
|
!(diff= memcmp(dsp_name, action->sync_point.ptr(), name_len)))
|
|
|
|
return action;
|
|
|
|
if (diff > 0)
|
|
|
|
low= mid + 1;
|
|
|
|
else
|
|
|
|
high= mid - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (low < quantity)
|
|
|
|
{
|
|
|
|
action= actionarr + low;
|
|
|
|
if ((name_len == action->sync_point.length()) &&
|
|
|
|
!memcmp(dsp_name, action->sync_point.ptr(), name_len))
|
|
|
|
return action;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Reset the debug sync facility.
|
|
|
|
|
|
|
|
@param[in] thd thread handle
|
|
|
|
|
|
|
|
@description
|
|
|
|
Remove all actions of this thread.
|
|
|
|
Clear the global signal.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void debug_sync_reset(THD *thd)
|
|
|
|
{
|
|
|
|
st_debug_sync_control *ds_control= thd->debug_sync_control;
|
|
|
|
DBUG_ENTER("debug_sync_reset");
|
|
|
|
DBUG_ASSERT(thd);
|
|
|
|
DBUG_ASSERT(ds_control);
|
|
|
|
|
|
|
|
/* Remove all actions of this thread. */
|
|
|
|
ds_control->ds_active= 0;
|
|
|
|
|
|
|
|
/* Clear the global signal. */
|
2009-12-10 04:19:51 +01:00
|
|
|
mysql_mutex_lock(&debug_sync_global.ds_mutex);
|
2009-09-29 17:38:40 +02:00
|
|
|
debug_sync_global.ds_signal.length(0);
|
2009-12-10 04:19:51 +01:00
|
|
|
mysql_mutex_unlock(&debug_sync_global.ds_mutex);
|
2009-09-29 17:38:40 +02:00
|
|
|
|
|
|
|
DBUG_VOID_RETURN;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Remove a debug sync action.
|
|
|
|
|
|
|
|
@param[in] ds_control control object
|
|
|
|
@param[in] action action to be removed
|
|
|
|
|
|
|
|
@description
|
|
|
|
Removing an action mainly means to decrement the ds_active counter.
|
|
|
|
But if the action is between other active action in the array, then
|
|
|
|
the array needs to be shrinked. The active actions above the one to
|
|
|
|
be removed have to be moved down by one slot.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void debug_sync_remove_action(st_debug_sync_control *ds_control,
|
|
|
|
st_debug_sync_action *action)
|
|
|
|
{
|
|
|
|
uint dsp_idx= action - ds_control->ds_action;
|
|
|
|
DBUG_ENTER("debug_sync_remove_action");
|
|
|
|
DBUG_ASSERT(ds_control);
|
|
|
|
DBUG_ASSERT(ds_control == current_thd->debug_sync_control);
|
|
|
|
DBUG_ASSERT(action);
|
|
|
|
DBUG_ASSERT(dsp_idx < ds_control->ds_active);
|
|
|
|
|
|
|
|
/* Decrement the number of currently active actions. */
|
|
|
|
ds_control->ds_active--;
|
|
|
|
|
|
|
|
/*
|
|
|
|
If this was not the last active action in the array, we need to
|
|
|
|
shift remaining active actions down to keep the array gap-free.
|
|
|
|
Otherwise binary search might fail or take longer than necessary at
|
|
|
|
least. Also new actions are always put to the end of the array.
|
|
|
|
*/
|
|
|
|
if (ds_control->ds_active > dsp_idx)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
Do not make save_action an object of class st_debug_sync_action.
|
|
|
|
Its destructor would tamper with the String pointers.
|
|
|
|
*/
|
|
|
|
uchar save_action[sizeof(st_debug_sync_action)];
|
|
|
|
|
|
|
|
/*
|
|
|
|
Copy the to-be-removed action object to temporary storage before
|
|
|
|
the shift copies the string pointers over. Do not use assignment
|
|
|
|
because it would use assignment operator methods for the Strings.
|
|
|
|
This would copy the strings. The shift below overwrite the string
|
|
|
|
pointers without freeing them first. By using memmove() we save
|
|
|
|
the pointers, which are overwritten by the shift.
|
|
|
|
*/
|
|
|
|
memmove(save_action, action, sizeof(st_debug_sync_action));
|
|
|
|
|
|
|
|
/* Move actions down. */
|
|
|
|
memmove(ds_control->ds_action + dsp_idx,
|
|
|
|
ds_control->ds_action + dsp_idx + 1,
|
|
|
|
(ds_control->ds_active - dsp_idx) *
|
|
|
|
sizeof(st_debug_sync_action));
|
|
|
|
|
|
|
|
/*
|
|
|
|
Copy back the saved action object to the now free array slot. This
|
|
|
|
replaces the double references of String pointers that have been
|
|
|
|
produced by the shift. Again do not use an assignment operator to
|
|
|
|
avoid string allocation/copy.
|
|
|
|
*/
|
|
|
|
memmove(ds_control->ds_action + ds_control->ds_active, save_action,
|
|
|
|
sizeof(st_debug_sync_action));
|
|
|
|
}
|
|
|
|
|
|
|
|
DBUG_VOID_RETURN;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Get a debug sync action.
|
|
|
|
|
|
|
|
@param[in] thd thread handle
|
|
|
|
@param[in] dsp_name debug sync point name
|
|
|
|
@param[in] name_len length of sync point name
|
|
|
|
|
|
|
|
@return action
|
|
|
|
@retval != NULL ok
|
|
|
|
@retval NULL error
|
|
|
|
|
|
|
|
@description
|
|
|
|
Find the debug sync action for a debug sync point or make a new one.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static st_debug_sync_action *debug_sync_get_action(THD *thd,
|
|
|
|
const char *dsp_name,
|
|
|
|
uint name_len)
|
|
|
|
{
|
|
|
|
st_debug_sync_control *ds_control= thd->debug_sync_control;
|
|
|
|
st_debug_sync_action *action;
|
|
|
|
DBUG_ENTER("debug_sync_get_action");
|
|
|
|
DBUG_ASSERT(thd);
|
|
|
|
DBUG_ASSERT(dsp_name);
|
|
|
|
DBUG_ASSERT(name_len);
|
|
|
|
DBUG_ASSERT(ds_control);
|
|
|
|
DBUG_PRINT("debug_sync", ("sync_point: '%.*s'", (int) name_len, dsp_name));
|
|
|
|
DBUG_PRINT("debug_sync", ("active: %u allocated: %u",
|
|
|
|
ds_control->ds_active, ds_control->ds_allocated));
|
|
|
|
|
|
|
|
/* There cannot be more active actions than allocated. */
|
|
|
|
DBUG_ASSERT(ds_control->ds_active <= ds_control->ds_allocated);
|
|
|
|
/* If there are active actions, the action array must be present. */
|
|
|
|
DBUG_ASSERT(!ds_control->ds_active || ds_control->ds_action);
|
|
|
|
|
|
|
|
/* Try to reuse existing action if there is one for this sync point. */
|
|
|
|
if (ds_control->ds_active &&
|
|
|
|
(action= debug_sync_find(ds_control->ds_action, ds_control->ds_active,
|
|
|
|
dsp_name, name_len)))
|
|
|
|
{
|
|
|
|
/* Reuse an already active sync point action. */
|
|
|
|
DBUG_ASSERT((uint)(action - ds_control->ds_action) < ds_control->ds_active);
|
|
|
|
DBUG_PRINT("debug_sync", ("reuse action idx: %ld",
|
|
|
|
(long) (action - ds_control->ds_action)));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Create a new action. */
|
|
|
|
int dsp_idx= ds_control->ds_active++;
|
|
|
|
set_if_bigger(ds_control->dsp_max_active, ds_control->ds_active);
|
|
|
|
if (ds_control->ds_active > ds_control->ds_allocated)
|
|
|
|
{
|
|
|
|
uint new_alloc= ds_control->ds_active + 3;
|
|
|
|
void *new_action= my_realloc(ds_control->ds_action,
|
|
|
|
new_alloc * sizeof(st_debug_sync_action),
|
|
|
|
MYF(MY_WME | MY_ALLOW_ZERO_PTR));
|
|
|
|
if (!new_action)
|
|
|
|
{
|
|
|
|
/* Error is reported by my_malloc(). */
|
|
|
|
goto err; /* purecov: tested */
|
|
|
|
}
|
|
|
|
ds_control->ds_action= (st_debug_sync_action*) new_action;
|
|
|
|
ds_control->ds_allocated= new_alloc;
|
|
|
|
/* Clear memory as we do not run string constructors here. */
|
|
|
|
bzero((uchar*) (ds_control->ds_action + dsp_idx),
|
|
|
|
(new_alloc - dsp_idx) * sizeof(st_debug_sync_action));
|
|
|
|
}
|
|
|
|
DBUG_PRINT("debug_sync", ("added action idx: %u", dsp_idx));
|
|
|
|
action= ds_control->ds_action + dsp_idx;
|
|
|
|
if (action->sync_point.copy(dsp_name, name_len, system_charset_info))
|
|
|
|
{
|
|
|
|
/* Error is reported by my_malloc(). */
|
|
|
|
goto err; /* purecov: tested */
|
|
|
|
}
|
|
|
|
action->need_sort= TRUE;
|
|
|
|
}
|
|
|
|
DBUG_ASSERT(action >= ds_control->ds_action);
|
|
|
|
DBUG_ASSERT(action < ds_control->ds_action + ds_control->ds_active);
|
|
|
|
DBUG_PRINT("debug_sync", ("action: 0x%lx array: 0x%lx count: %u",
|
|
|
|
(long) action, (long) ds_control->ds_action,
|
|
|
|
ds_control->ds_active));
|
|
|
|
|
|
|
|
DBUG_RETURN(action);
|
|
|
|
|
|
|
|
/* purecov: begin tested */
|
|
|
|
err:
|
|
|
|
DBUG_RETURN(NULL);
|
|
|
|
/* purecov: end */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Set a debug sync action.
|
|
|
|
|
|
|
|
@param[in] thd thread handle
|
|
|
|
@param[in] action synchronization action
|
|
|
|
|
|
|
|
@return status
|
|
|
|
@retval FALSE ok
|
|
|
|
@retval TRUE error
|
|
|
|
|
|
|
|
@description
|
|
|
|
This is called from the debug sync parser. It arms the action for
|
|
|
|
the requested sync point. If the action parsed into an empty action,
|
|
|
|
it is removed instead.
|
|
|
|
|
|
|
|
Setting an action for a sync point means to make the sync point
|
|
|
|
active. When it is hit it will execute this action.
|
|
|
|
|
|
|
|
Before parsing, we "get" an action object. This is placed at the
|
|
|
|
end of the thread's action array unless the requested sync point
|
|
|
|
has an action already.
|
|
|
|
|
|
|
|
Then the parser fills the action object from the request string.
|
|
|
|
|
|
|
|
Finally the action is "set" for the sync point. If it was parsed
|
|
|
|
to be empty, it is removed from the array. If it did belong to a
|
|
|
|
sync point before, the sync point becomes inactive. If the action
|
|
|
|
became non-empty and it did not belong to a sync point before (it
|
|
|
|
was added at the end of the action array), the action array needs
|
|
|
|
to be sorted by sync point.
|
|
|
|
|
|
|
|
If the sync point name is "now", it is executed immediately.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static bool debug_sync_set_action(THD *thd, st_debug_sync_action *action)
|
|
|
|
{
|
|
|
|
st_debug_sync_control *ds_control= thd->debug_sync_control;
|
|
|
|
bool is_dsp_now= FALSE;
|
|
|
|
DBUG_ENTER("debug_sync_set_action");
|
|
|
|
DBUG_ASSERT(thd);
|
|
|
|
DBUG_ASSERT(action);
|
|
|
|
DBUG_ASSERT(ds_control);
|
|
|
|
|
2013-03-25 23:03:13 +01:00
|
|
|
action->activation_count= MY_MAX(action->hit_limit, action->execute);
|
2009-09-29 17:38:40 +02:00
|
|
|
if (!action->activation_count)
|
|
|
|
{
|
|
|
|
debug_sync_remove_action(ds_control, action);
|
|
|
|
DBUG_PRINT("debug_sync", ("action cleared"));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const char *dsp_name= action->sync_point.c_ptr();
|
|
|
|
DBUG_EXECUTE("debug_sync", {
|
|
|
|
/* Functions as DBUG_PRINT args can change keyword and line nr. */
|
|
|
|
const char *sig_emit= action->signal.c_ptr();
|
|
|
|
const char *sig_wait= action->wait_for.c_ptr();
|
|
|
|
DBUG_PRINT("debug_sync",
|
|
|
|
("sync_point: '%s' activation_count: %lu hit_limit: %lu "
|
|
|
|
"execute: %lu timeout: %lu signal: '%s' wait_for: '%s'",
|
|
|
|
dsp_name, action->activation_count,
|
|
|
|
action->hit_limit, action->execute, action->timeout,
|
|
|
|
sig_emit, sig_wait));});
|
|
|
|
|
|
|
|
/* Check this before sorting the array. action may move. */
|
|
|
|
is_dsp_now= !my_strcasecmp(system_charset_info, dsp_name, "now");
|
|
|
|
|
|
|
|
if (action->need_sort)
|
|
|
|
{
|
|
|
|
action->need_sort= FALSE;
|
|
|
|
/* Sort actions by (name_len, name). */
|
|
|
|
my_qsort(ds_control->ds_action, ds_control->ds_active,
|
|
|
|
sizeof(st_debug_sync_action), debug_sync_qsort_cmp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DBUG_EXECUTE("debug_sync_list", debug_sync_print_actions(thd););
|
|
|
|
|
|
|
|
/* Execute the special sync point 'now' if activated above. */
|
|
|
|
if (is_dsp_now)
|
|
|
|
{
|
|
|
|
DEBUG_SYNC(thd, "now");
|
|
|
|
/*
|
|
|
|
If HIT_LIMIT for sync point "now" was 1, the execution of the sync
|
|
|
|
point decremented it to 0. In this case the following happened:
|
|
|
|
|
|
|
|
- an error message was reported with my_error() and
|
2013-07-02 19:43:35 +02:00
|
|
|
- the statement was killed with thd->killed= THD::KILL_QUERY.
|
2009-09-29 17:38:40 +02:00
|
|
|
|
|
|
|
If a statement reports an error, it must not call send_ok().
|
|
|
|
The calling functions will not call send_ok(), if we return TRUE
|
|
|
|
from this function.
|
|
|
|
|
|
|
|
thd->killed is also set if the wait is interrupted from a
|
|
|
|
KILL or KILL QUERY statement. In this case, no error is reported
|
|
|
|
and shall not be reported as a result of SET DEBUG_SYNC.
|
|
|
|
Hence, we check for the first condition above.
|
|
|
|
*/
|
|
|
|
if (thd->is_error())
|
|
|
|
DBUG_RETURN(TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
DBUG_RETURN(FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Extract a token from a string.
|
|
|
|
|
|
|
|
@param[out] token_p returns start of token
|
|
|
|
@param[out] token_length_p returns length of token
|
|
|
|
@param[in,out] ptr current string pointer, adds '\0' terminators
|
|
|
|
|
|
|
|
@return string pointer or NULL
|
|
|
|
@retval != NULL ptr behind token terminator or at string end
|
|
|
|
@retval NULL no token found in remainder of string
|
|
|
|
|
|
|
|
@note
|
|
|
|
This function assumes that the string is in system_charset_info,
|
|
|
|
that this charset is single byte for ASCII NUL ('\0'), that no
|
|
|
|
character except of ASCII NUL ('\0') contains a byte with value 0,
|
|
|
|
and that ASCII NUL ('\0') is used as the string terminator.
|
|
|
|
|
|
|
|
This function needs to return tokens that are terminated with ASCII
|
|
|
|
NUL ('\0'). The tokens are used in my_strcasecmp(). Unfortunately
|
|
|
|
there is no my_strncasecmp().
|
|
|
|
|
|
|
|
To return the last token without copying it, we require the input
|
|
|
|
string to be nul terminated.
|
|
|
|
|
|
|
|
@description
|
|
|
|
This function skips space characters at string begin.
|
|
|
|
|
|
|
|
It returns a pointer to the first non-space character in *token_p.
|
|
|
|
|
|
|
|
If no non-space character is found before the string terminator
|
|
|
|
ASCII NUL ('\0'), the function returns NULL. *token_p and
|
|
|
|
*token_length_p remain unchanged in this case (they are not set).
|
|
|
|
|
|
|
|
The function takes a space character or an ASCII NUL ('\0') as a
|
|
|
|
terminator of the token. The space character could be multi-byte.
|
|
|
|
|
|
|
|
It returns the length of the token in bytes, excluding the
|
|
|
|
terminator, in *token_length_p.
|
|
|
|
|
|
|
|
If the terminator of the token is ASCII NUL ('\0'), it returns a
|
|
|
|
pointer to the terminator (string end).
|
|
|
|
|
|
|
|
If the terminator is a space character, it replaces the the first
|
|
|
|
byte of the terminator character by ASCII NUL ('\0'), skips the (now
|
|
|
|
corrupted) terminator character, and skips all following space
|
|
|
|
characters. It returns a pointer to the next non-space character or
|
|
|
|
to the string terminator ASCII NUL ('\0').
|
|
|
|
*/
|
|
|
|
|
2016-05-17 13:27:10 +02:00
|
|
|
static char *debug_sync_token(char **token_p, uint *token_length_p,
|
|
|
|
char *ptr, char *ptrend)
|
2009-09-29 17:38:40 +02:00
|
|
|
{
|
|
|
|
DBUG_ASSERT(token_p);
|
|
|
|
DBUG_ASSERT(token_length_p);
|
|
|
|
DBUG_ASSERT(ptr);
|
|
|
|
|
|
|
|
/* Skip leading space */
|
2016-05-17 13:27:10 +02:00
|
|
|
ptr+= system_charset_info->cset->scan(system_charset_info,
|
|
|
|
ptr, ptrend, MY_SEQ_SPACES);
|
2009-09-29 17:38:40 +02:00
|
|
|
if (!*ptr)
|
|
|
|
{
|
|
|
|
ptr= NULL;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get token start. */
|
|
|
|
*token_p= ptr;
|
|
|
|
|
|
|
|
/* Find token end. */
|
2016-05-17 13:27:10 +02:00
|
|
|
ptr+= system_charset_info->cset->scan(system_charset_info,
|
|
|
|
ptr, ptrend, MY_SEQ_NONSPACES);
|
2009-09-29 17:38:40 +02:00
|
|
|
|
|
|
|
/* Get token length. */
|
|
|
|
*token_length_p= ptr - *token_p;
|
|
|
|
|
|
|
|
/* If necessary, terminate token. */
|
|
|
|
if (*ptr)
|
|
|
|
{
|
2016-05-17 13:27:10 +02:00
|
|
|
DBUG_ASSERT(ptr < ptrend);
|
2009-09-29 17:38:40 +02:00
|
|
|
/* Get terminator character length. */
|
2016-05-17 13:27:10 +02:00
|
|
|
uint mbspacelen= my_charlen_fix(system_charset_info, ptr, ptrend);
|
2009-09-29 17:38:40 +02:00
|
|
|
|
|
|
|
/* Terminate token. */
|
|
|
|
*ptr= '\0';
|
|
|
|
|
|
|
|
/* Skip the terminator. */
|
|
|
|
ptr+= mbspacelen;
|
|
|
|
|
|
|
|
/* Skip trailing space */
|
2016-05-17 13:27:10 +02:00
|
|
|
ptr+= system_charset_info->cset->scan(system_charset_info,
|
|
|
|
ptr, ptrend, MY_SEQ_SPACES);
|
2009-09-29 17:38:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
end:
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Extract a number from a string.
|
|
|
|
|
|
|
|
@param[out] number_p returns number
|
|
|
|
@param[in] actstrptr current pointer in action string
|
|
|
|
|
|
|
|
@return string pointer or NULL
|
|
|
|
@retval != NULL ptr behind token terminator or at string end
|
|
|
|
@retval NULL no token found or token is not valid number
|
|
|
|
|
|
|
|
@note
|
|
|
|
The same assumptions about charset apply as for debug_sync_token().
|
|
|
|
|
|
|
|
@description
|
|
|
|
This function fetches a token from the string and converts it
|
|
|
|
into a number.
|
|
|
|
|
|
|
|
If there is no token left in the string, or the token is not a valid
|
|
|
|
decimal number, NULL is returned. The result in *number_p is
|
|
|
|
undefined in this case.
|
|
|
|
*/
|
|
|
|
|
2016-05-17 13:27:10 +02:00
|
|
|
static char *debug_sync_number(ulong *number_p, char *actstrptr,
|
|
|
|
char *actstrend)
|
2009-09-29 17:38:40 +02:00
|
|
|
{
|
|
|
|
char *ptr;
|
|
|
|
char *ept;
|
|
|
|
char *token;
|
|
|
|
uint token_length;
|
|
|
|
DBUG_ASSERT(number_p);
|
|
|
|
DBUG_ASSERT(actstrptr);
|
|
|
|
|
|
|
|
/* Get token from string. */
|
2016-05-17 13:27:10 +02:00
|
|
|
if (!(ptr= debug_sync_token(&token, &token_length, actstrptr, actstrend)))
|
2009-09-29 17:38:40 +02:00
|
|
|
goto end;
|
|
|
|
|
|
|
|
*number_p= strtoul(token, &ept, 10);
|
|
|
|
if (*ept)
|
|
|
|
ptr= NULL;
|
|
|
|
|
|
|
|
end:
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Evaluate a debug sync action string.
|
|
|
|
|
|
|
|
@param[in] thd thread handle
|
|
|
|
@param[in,out] action_str action string to receive '\0' terminators
|
|
|
|
|
|
|
|
@return status
|
|
|
|
@retval FALSE ok
|
|
|
|
@retval TRUE error
|
|
|
|
|
|
|
|
@description
|
|
|
|
This is called when the DEBUG_SYNC system variable is set.
|
|
|
|
Parse action string, build a debug sync action, activate it.
|
|
|
|
|
|
|
|
Before parsing, we "get" an action object. This is placed at the
|
|
|
|
end of the thread's action array unless the requested sync point
|
|
|
|
has an action already.
|
|
|
|
|
|
|
|
Then the parser fills the action object from the request string.
|
|
|
|
|
|
|
|
Finally the action is "set" for the sync point. This means that the
|
|
|
|
sync point becomes active or inactive, depending on the action
|
|
|
|
values.
|
|
|
|
|
|
|
|
@note
|
|
|
|
The input string needs to be ASCII NUL ('\0') terminated. We split
|
|
|
|
nul-terminated tokens in it without copy.
|
|
|
|
|
|
|
|
@see the function comment of debug_sync_token() for more constraints
|
|
|
|
for the string.
|
|
|
|
*/
|
|
|
|
|
2016-05-17 13:27:10 +02:00
|
|
|
static bool debug_sync_eval_action(THD *thd, char *action_str, char *action_end)
|
2009-09-29 17:38:40 +02:00
|
|
|
{
|
|
|
|
st_debug_sync_action *action= NULL;
|
|
|
|
const char *errmsg;
|
|
|
|
char *ptr;
|
|
|
|
char *token;
|
2009-10-04 11:53:02 +02:00
|
|
|
uint token_length= 0;
|
2009-09-29 17:38:40 +02:00
|
|
|
DBUG_ENTER("debug_sync_eval_action");
|
|
|
|
DBUG_ASSERT(thd);
|
|
|
|
DBUG_ASSERT(action_str);
|
2013-07-02 19:43:35 +02:00
|
|
|
DBUG_PRINT("debug_sync", ("action_str: '%s'", action_str));
|
2009-09-29 17:38:40 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
Get debug sync point name. Or a special command.
|
|
|
|
*/
|
2016-05-17 13:27:10 +02:00
|
|
|
if (!(ptr= debug_sync_token(&token, &token_length, action_str, action_end)))
|
2009-09-29 17:38:40 +02:00
|
|
|
{
|
|
|
|
errmsg= "Missing synchronization point name";
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
If there is a second token, the first one is the sync point name.
|
|
|
|
*/
|
|
|
|
if (*ptr)
|
|
|
|
{
|
|
|
|
/* Get an action object to collect the requested action parameters. */
|
|
|
|
action= debug_sync_get_action(thd, token, token_length);
|
|
|
|
if (!action)
|
|
|
|
{
|
|
|
|
/* Error message is sent. */
|
|
|
|
DBUG_RETURN(TRUE); /* purecov: tested */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Get kind of action to be taken at sync point.
|
|
|
|
*/
|
2016-05-17 13:27:10 +02:00
|
|
|
if (!(ptr= debug_sync_token(&token, &token_length, ptr, action_end)))
|
2009-09-29 17:38:40 +02:00
|
|
|
{
|
|
|
|
/* No action present. Try special commands. Token unchanged. */
|
|
|
|
|
|
|
|
/*
|
|
|
|
Try RESET.
|
|
|
|
*/
|
|
|
|
if (!my_strcasecmp(system_charset_info, token, "RESET"))
|
|
|
|
{
|
|
|
|
/* It is RESET. Reset all actions and global signal. */
|
|
|
|
debug_sync_reset(thd);
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Token unchanged. It still contains sync point name. */
|
|
|
|
errmsg= "Missing action after synchronization point name '%.*s'";
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Check for pseudo actions first. Start with actions that work on
|
|
|
|
an existing action.
|
|
|
|
*/
|
|
|
|
DBUG_ASSERT(action);
|
|
|
|
|
|
|
|
/*
|
|
|
|
Try TEST.
|
|
|
|
*/
|
|
|
|
if (!my_strcasecmp(system_charset_info, token, "TEST"))
|
|
|
|
{
|
|
|
|
/* It is TEST. Nothing must follow it. */
|
|
|
|
if (*ptr)
|
|
|
|
{
|
|
|
|
errmsg= "Nothing must follow action TEST";
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Execute sync point. */
|
|
|
|
debug_sync(thd, action->sync_point.ptr(), action->sync_point.length());
|
|
|
|
/* Fix statistics. This was not a real hit of the sync point. */
|
|
|
|
thd->debug_sync_control->dsp_hits--;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Now check for actions that define a new action.
|
|
|
|
Initialize action. Do not use bzero(). Strings may have malloced.
|
|
|
|
*/
|
|
|
|
action->activation_count= 0;
|
|
|
|
action->hit_limit= 0;
|
|
|
|
action->execute= 0;
|
|
|
|
action->timeout= 0;
|
|
|
|
action->signal.length(0);
|
|
|
|
action->wait_for.length(0);
|
|
|
|
|
|
|
|
/*
|
|
|
|
Try CLEAR.
|
|
|
|
*/
|
|
|
|
if (!my_strcasecmp(system_charset_info, token, "CLEAR"))
|
|
|
|
{
|
|
|
|
/* It is CLEAR. Nothing must follow it. */
|
|
|
|
if (*ptr)
|
|
|
|
{
|
|
|
|
errmsg= "Nothing must follow action CLEAR";
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set (clear/remove) action. */
|
|
|
|
goto set_action;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Now check for real sync point actions.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
Try SIGNAL.
|
|
|
|
*/
|
|
|
|
if (!my_strcasecmp(system_charset_info, token, "SIGNAL"))
|
|
|
|
{
|
|
|
|
/* It is SIGNAL. Signal name must follow. */
|
2016-05-17 13:27:10 +02:00
|
|
|
if (!(ptr= debug_sync_token(&token, &token_length, ptr, action_end)))
|
2009-09-29 17:38:40 +02:00
|
|
|
{
|
|
|
|
errmsg= "Missing signal name after action SIGNAL";
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (action->signal.copy(token, token_length, system_charset_info))
|
|
|
|
{
|
|
|
|
/* Error is reported by my_malloc(). */
|
|
|
|
/* purecov: begin tested */
|
|
|
|
errmsg= NULL;
|
|
|
|
goto err;
|
|
|
|
/* purecov: end */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set default for EXECUTE option. */
|
|
|
|
action->execute= 1;
|
|
|
|
|
|
|
|
/* Get next token. If none follows, set action. */
|
2016-05-17 13:27:10 +02:00
|
|
|
if (!(ptr= debug_sync_token(&token, &token_length, ptr, action_end)))
|
2009-09-29 17:38:40 +02:00
|
|
|
goto set_action;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Try WAIT_FOR.
|
|
|
|
*/
|
|
|
|
if (!my_strcasecmp(system_charset_info, token, "WAIT_FOR"))
|
|
|
|
{
|
|
|
|
/* It is WAIT_FOR. Wait_for signal name must follow. */
|
2016-05-17 13:27:10 +02:00
|
|
|
if (!(ptr= debug_sync_token(&token, &token_length, ptr, action_end)))
|
2009-09-29 17:38:40 +02:00
|
|
|
{
|
|
|
|
errmsg= "Missing signal name after action WAIT_FOR";
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (action->wait_for.copy(token, token_length, system_charset_info))
|
|
|
|
{
|
|
|
|
/* Error is reported by my_malloc(). */
|
|
|
|
/* purecov: begin tested */
|
|
|
|
errmsg= NULL;
|
|
|
|
goto err;
|
|
|
|
/* purecov: end */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set default for EXECUTE and TIMEOUT options. */
|
|
|
|
action->execute= 1;
|
|
|
|
action->timeout= opt_debug_sync_timeout;
|
|
|
|
|
|
|
|
/* Get next token. If none follows, set action. */
|
2016-05-17 13:27:10 +02:00
|
|
|
if (!(ptr= debug_sync_token(&token, &token_length, ptr, action_end)))
|
2009-09-29 17:38:40 +02:00
|
|
|
goto set_action;
|
|
|
|
|
|
|
|
/*
|
|
|
|
Try TIMEOUT.
|
|
|
|
*/
|
|
|
|
if (!my_strcasecmp(system_charset_info, token, "TIMEOUT"))
|
|
|
|
{
|
|
|
|
/* It is TIMEOUT. Number must follow. */
|
2016-05-17 13:27:10 +02:00
|
|
|
if (!(ptr= debug_sync_number(&action->timeout, ptr, action_end)))
|
2009-09-29 17:38:40 +02:00
|
|
|
{
|
|
|
|
errmsg= "Missing valid number after TIMEOUT";
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get next token. If none follows, set action. */
|
2016-05-17 13:27:10 +02:00
|
|
|
if (!(ptr= debug_sync_token(&token, &token_length, ptr, action_end)))
|
2009-09-29 17:38:40 +02:00
|
|
|
goto set_action;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Try EXECUTE.
|
|
|
|
*/
|
|
|
|
if (!my_strcasecmp(system_charset_info, token, "EXECUTE"))
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
EXECUTE requires either SIGNAL and/or WAIT_FOR to be present.
|
|
|
|
In this case action->execute has been preset to 1.
|
|
|
|
*/
|
|
|
|
if (!action->execute)
|
|
|
|
{
|
|
|
|
errmsg= "Missing action before EXECUTE";
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Number must follow. */
|
2016-05-17 13:27:10 +02:00
|
|
|
if (!(ptr= debug_sync_number(&action->execute, ptr, action_end)))
|
2009-09-29 17:38:40 +02:00
|
|
|
{
|
|
|
|
errmsg= "Missing valid number after EXECUTE";
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get next token. If none follows, set action. */
|
2016-05-17 13:27:10 +02:00
|
|
|
if (!(ptr= debug_sync_token(&token, &token_length, ptr, action_end)))
|
2009-09-29 17:38:40 +02:00
|
|
|
goto set_action;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Try HIT_LIMIT.
|
|
|
|
*/
|
|
|
|
if (!my_strcasecmp(system_charset_info, token, "HIT_LIMIT"))
|
|
|
|
{
|
|
|
|
/* Number must follow. */
|
2016-05-17 13:27:10 +02:00
|
|
|
if (!(ptr= debug_sync_number(&action->hit_limit, ptr, action_end)))
|
2009-09-29 17:38:40 +02:00
|
|
|
{
|
|
|
|
errmsg= "Missing valid number after HIT_LIMIT";
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get next token. If none follows, set action. */
|
2016-05-17 13:27:10 +02:00
|
|
|
if (!(ptr= debug_sync_token(&token, &token_length, ptr, action_end)))
|
2009-09-29 17:38:40 +02:00
|
|
|
goto set_action;
|
|
|
|
}
|
|
|
|
|
|
|
|
errmsg= "Illegal or out of order stuff: '%.*s'";
|
|
|
|
|
|
|
|
err:
|
|
|
|
if (errmsg)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
NOTE: errmsg must either have %.*s or none % at all.
|
|
|
|
It can be NULL if an error message is already reported
|
|
|
|
(e.g. by my_malloc()).
|
|
|
|
*/
|
|
|
|
set_if_smaller(token_length, 64); /* Limit error message length. */
|
|
|
|
my_printf_error(ER_PARSE_ERROR, errmsg, MYF(0), token_length, token);
|
|
|
|
}
|
|
|
|
if (action)
|
|
|
|
debug_sync_remove_action(thd->debug_sync_control, action);
|
|
|
|
DBUG_RETURN(TRUE);
|
|
|
|
|
|
|
|
set_action:
|
|
|
|
DBUG_RETURN(debug_sync_set_action(thd, action));
|
|
|
|
|
|
|
|
end:
|
|
|
|
DBUG_RETURN(FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Set the system variable 'debug_sync'.
|
|
|
|
|
|
|
|
@param[in] thd thread handle
|
|
|
|
@param[in] var set variable request
|
|
|
|
|
|
|
|
@return status
|
|
|
|
@retval FALSE ok, variable is set
|
|
|
|
@retval TRUE error, variable could not be set
|
|
|
|
|
|
|
|
@note
|
|
|
|
"Setting" of the system variable 'debug_sync' does not mean to
|
|
|
|
assign a value to it as usual. Instead a debug sync action is parsed
|
|
|
|
from the input string and stored apart from the variable value.
|
|
|
|
|
|
|
|
@note
|
|
|
|
For efficiency reasons, the action string parser places '\0'
|
|
|
|
terminators in the string. So we need to take a copy here.
|
|
|
|
*/
|
|
|
|
|
2016-05-17 13:27:10 +02:00
|
|
|
bool debug_sync_update(THD *thd, char *val_str, size_t len)
|
2009-09-29 17:38:40 +02:00
|
|
|
{
|
2009-12-22 10:35:56 +01:00
|
|
|
DBUG_ENTER("debug_sync_update");
|
2009-09-29 17:38:40 +02:00
|
|
|
DBUG_PRINT("debug_sync", ("set action: '%s'", val_str));
|
|
|
|
|
|
|
|
/*
|
|
|
|
debug_sync_eval_action() places '\0' in the string, which itself
|
|
|
|
must be '\0' terminated.
|
|
|
|
*/
|
2016-05-17 13:27:10 +02:00
|
|
|
DBUG_ASSERT(val_str[len] == '\0');
|
2009-09-29 17:38:40 +02:00
|
|
|
DBUG_RETURN(opt_debug_sync_timeout ?
|
2016-05-17 13:27:10 +02:00
|
|
|
debug_sync_eval_action(thd, val_str, val_str + len) :
|
2009-09-29 17:38:40 +02:00
|
|
|
FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Retrieve the value of the system variable 'debug_sync'.
|
|
|
|
|
|
|
|
@param[in] thd thread handle
|
|
|
|
|
|
|
|
@return string
|
|
|
|
@retval != NULL ok, string pointer
|
|
|
|
@retval NULL memory allocation error
|
|
|
|
|
|
|
|
@note
|
|
|
|
The value of the system variable 'debug_sync' reflects if
|
|
|
|
the facility is enabled ("ON") or disabled (default, "OFF").
|
|
|
|
|
|
|
|
When "ON", the current signal is added.
|
|
|
|
*/
|
|
|
|
|
2009-12-22 10:35:56 +01:00
|
|
|
uchar *debug_sync_value_ptr(THD *thd)
|
2009-09-29 17:38:40 +02:00
|
|
|
{
|
|
|
|
char *value;
|
2009-12-22 10:35:56 +01:00
|
|
|
DBUG_ENTER("debug_sync_value_ptr");
|
2009-09-29 17:38:40 +02:00
|
|
|
|
|
|
|
if (opt_debug_sync_timeout)
|
|
|
|
{
|
|
|
|
static char on[]= "ON - current signal: '";
|
|
|
|
|
|
|
|
// Ensure exclusive access to debug_sync_global.ds_signal
|
2009-12-10 04:19:51 +01:00
|
|
|
mysql_mutex_lock(&debug_sync_global.ds_mutex);
|
2009-09-29 17:38:40 +02:00
|
|
|
|
|
|
|
size_t lgt= (sizeof(on) /* includes '\0' */ +
|
|
|
|
debug_sync_global.ds_signal.length() + 1 /* for '\'' */);
|
|
|
|
char *vend;
|
|
|
|
char *vptr;
|
|
|
|
|
|
|
|
if ((value= (char*) alloc_root(thd->mem_root, lgt)))
|
|
|
|
{
|
|
|
|
vend= value + lgt - 1; /* reserve space for '\0'. */
|
|
|
|
vptr= debug_sync_bmove_len(value, vend, STRING_WITH_LEN(on));
|
|
|
|
vptr= debug_sync_bmove_len(vptr, vend, debug_sync_global.ds_signal.ptr(),
|
|
|
|
debug_sync_global.ds_signal.length());
|
|
|
|
if (vptr < vend)
|
|
|
|
*(vptr++)= '\'';
|
|
|
|
*vptr= '\0'; /* We have one byte reserved for the worst case. */
|
|
|
|
}
|
2009-12-10 04:19:51 +01:00
|
|
|
mysql_mutex_unlock(&debug_sync_global.ds_mutex);
|
2009-09-29 17:38:40 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* purecov: begin tested */
|
2009-12-22 10:35:56 +01:00
|
|
|
value= const_cast<char*>("OFF");
|
2009-09-29 17:38:40 +02:00
|
|
|
/* purecov: end */
|
|
|
|
}
|
|
|
|
|
|
|
|
DBUG_RETURN((uchar*) value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Execute requested action at a synchronization point.
|
|
|
|
|
|
|
|
@param[in] thd thread handle
|
|
|
|
@param[in] action action to be executed
|
|
|
|
|
|
|
|
@note
|
|
|
|
This is to be called only if activation count > 0.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void debug_sync_execute(THD *thd, st_debug_sync_action *action)
|
|
|
|
{
|
2009-10-30 19:13:58 +01:00
|
|
|
#ifndef DBUG_OFF
|
|
|
|
const char *dsp_name= action->sync_point.c_ptr();
|
|
|
|
const char *sig_emit= action->signal.c_ptr();
|
|
|
|
const char *sig_wait= action->wait_for.c_ptr();
|
|
|
|
#endif
|
2009-09-29 17:38:40 +02:00
|
|
|
DBUG_ENTER("debug_sync_execute");
|
|
|
|
DBUG_ASSERT(thd);
|
|
|
|
DBUG_ASSERT(action);
|
|
|
|
DBUG_PRINT("debug_sync",
|
|
|
|
("sync_point: '%s' activation_count: %lu hit_limit: %lu "
|
|
|
|
"execute: %lu timeout: %lu signal: '%s' wait_for: '%s'",
|
|
|
|
dsp_name, action->activation_count, action->hit_limit,
|
|
|
|
action->execute, action->timeout, sig_emit, sig_wait));
|
|
|
|
|
|
|
|
DBUG_ASSERT(action->activation_count);
|
|
|
|
action->activation_count--;
|
|
|
|
|
|
|
|
if (action->execute)
|
|
|
|
{
|
2015-02-10 11:05:49 +01:00
|
|
|
const char *UNINIT_VAR(old_proc_info);
|
2009-09-29 17:38:40 +02:00
|
|
|
|
|
|
|
action->execute--;
|
|
|
|
|
|
|
|
/*
|
|
|
|
If we will be going to wait, set proc_info for the PROCESSLIST table.
|
|
|
|
Do this before emitting the signal, so other threads can see it
|
|
|
|
if they awake before we enter_cond() below.
|
|
|
|
*/
|
|
|
|
if (action->wait_for.length())
|
|
|
|
{
|
|
|
|
st_debug_sync_control *ds_control= thd->debug_sync_control;
|
|
|
|
strxnmov(ds_control->ds_proc_info, sizeof(ds_control->ds_proc_info)-1,
|
|
|
|
"debug sync point: ", action->sync_point.c_ptr(), NullS);
|
|
|
|
old_proc_info= thd->proc_info;
|
|
|
|
thd_proc_info(thd, ds_control->ds_proc_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Take mutex to ensure that only one thread access
|
|
|
|
debug_sync_global.ds_signal at a time. Need to take mutex for
|
|
|
|
read access too, to create a memory barrier in order to avoid that
|
|
|
|
threads just reads an old cached version of the signal.
|
|
|
|
*/
|
2009-12-10 04:19:51 +01:00
|
|
|
mysql_mutex_lock(&debug_sync_global.ds_mutex);
|
2009-09-29 17:38:40 +02:00
|
|
|
|
|
|
|
if (action->signal.length())
|
|
|
|
{
|
|
|
|
/* Copy the signal to the global variable. */
|
|
|
|
if (debug_sync_global.ds_signal.copy(action->signal))
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
Error is reported by my_malloc().
|
|
|
|
We must disable the facility. We have no way to return an error.
|
|
|
|
*/
|
|
|
|
debug_sync_emergency_disable(); /* purecov: tested */
|
|
|
|
}
|
|
|
|
/* Wake threads waiting in a sync point. */
|
2009-12-10 04:19:51 +01:00
|
|
|
mysql_cond_broadcast(&debug_sync_global.ds_cond);
|
2009-09-29 17:38:40 +02:00
|
|
|
DBUG_PRINT("debug_sync_exec", ("signal '%s' at: '%s'",
|
|
|
|
sig_emit, dsp_name));
|
|
|
|
} /* end if (action->signal.length()) */
|
|
|
|
|
|
|
|
if (action->wait_for.length())
|
|
|
|
{
|
MDEV-6582: DEBUG_SYNC does not reset mysys_var->current_mutex, causes assertion "Trying to unlock mutex that wasn't locked"
The bug was in DEBUG_SYNC. When waiting, debug_sync_execute() temporarily sets
thd->mysys_var->current_mutex to a new value while waiting. However, if the
old value of current_mutex was NULL, it was not restored, current_mutex
remained set to the temporary value (debug_sync_global.ds_mutex).
This made possible the following race: Thread T1 goes to KILL thread T2. In
THD::awake(), T1 loads T2->mysys_var->current_mutex, it is set to ds_mutex, T1
locks this mutex.
Now T2 runs, it does ENTER_COND, it sets T2->mysys_var->current_mutex to
LOCK_wait_commit (for example).
Then T1 resumes, it reloads mysys_var->current_mutex, now it is set to
LOCK_wait_commit, T1 unlocks this mutex instead of the ds_mutex that it locked
previously.
This causes safe_mutex to assert with the message: "Trying to unlock mutex
LOCK_wait_commit that wasn't locked".
The fix is to ensure that DEBUG_SYNC also will restore
mysys_var->current_mutex in the case where the original value was NULL.
2014-11-26 11:07:32 +01:00
|
|
|
mysql_mutex_t *old_mutex= NULL;
|
2013-06-14 10:52:23 +02:00
|
|
|
mysql_cond_t *old_cond= NULL;
|
MDEV-6582: DEBUG_SYNC does not reset mysys_var->current_mutex, causes assertion "Trying to unlock mutex that wasn't locked"
The bug was in DEBUG_SYNC. When waiting, debug_sync_execute() temporarily sets
thd->mysys_var->current_mutex to a new value while waiting. However, if the
old value of current_mutex was NULL, it was not restored, current_mutex
remained set to the temporary value (debug_sync_global.ds_mutex).
This made possible the following race: Thread T1 goes to KILL thread T2. In
THD::awake(), T1 loads T2->mysys_var->current_mutex, it is set to ds_mutex, T1
locks this mutex.
Now T2 runs, it does ENTER_COND, it sets T2->mysys_var->current_mutex to
LOCK_wait_commit (for example).
Then T1 resumes, it reloads mysys_var->current_mutex, now it is set to
LOCK_wait_commit, T1 unlocks this mutex instead of the ds_mutex that it locked
previously.
This causes safe_mutex to assert with the message: "Trying to unlock mutex
LOCK_wait_commit that wasn't locked".
The fix is to ensure that DEBUG_SYNC also will restore
mysys_var->current_mutex in the case where the original value was NULL.
2014-11-26 11:07:32 +01:00
|
|
|
bool restore_current_mutex;
|
2009-09-29 17:38:40 +02:00
|
|
|
int error= 0;
|
|
|
|
struct timespec abstime;
|
|
|
|
|
|
|
|
/*
|
|
|
|
We don't use enter_cond()/exit_cond(). They do not save old
|
|
|
|
mutex and cond. This would prohibit the use of DEBUG_SYNC
|
|
|
|
between other places of enter_cond() and exit_cond().
|
2011-03-04 12:44:49 +01:00
|
|
|
|
|
|
|
We need to check for existence of thd->mysys_var to also make
|
|
|
|
it possible to use DEBUG_SYNC framework in scheduler when this
|
|
|
|
variable has been set to NULL.
|
2009-09-29 17:38:40 +02:00
|
|
|
*/
|
2010-11-09 16:33:40 +01:00
|
|
|
if (thd->mysys_var)
|
|
|
|
{
|
|
|
|
old_mutex= thd->mysys_var->current_mutex;
|
|
|
|
old_cond= thd->mysys_var->current_cond;
|
MDEV-6582: DEBUG_SYNC does not reset mysys_var->current_mutex, causes assertion "Trying to unlock mutex that wasn't locked"
The bug was in DEBUG_SYNC. When waiting, debug_sync_execute() temporarily sets
thd->mysys_var->current_mutex to a new value while waiting. However, if the
old value of current_mutex was NULL, it was not restored, current_mutex
remained set to the temporary value (debug_sync_global.ds_mutex).
This made possible the following race: Thread T1 goes to KILL thread T2. In
THD::awake(), T1 loads T2->mysys_var->current_mutex, it is set to ds_mutex, T1
locks this mutex.
Now T2 runs, it does ENTER_COND, it sets T2->mysys_var->current_mutex to
LOCK_wait_commit (for example).
Then T1 resumes, it reloads mysys_var->current_mutex, now it is set to
LOCK_wait_commit, T1 unlocks this mutex instead of the ds_mutex that it locked
previously.
This causes safe_mutex to assert with the message: "Trying to unlock mutex
LOCK_wait_commit that wasn't locked".
The fix is to ensure that DEBUG_SYNC also will restore
mysys_var->current_mutex in the case where the original value was NULL.
2014-11-26 11:07:32 +01:00
|
|
|
restore_current_mutex = true;
|
2010-11-09 16:33:40 +01:00
|
|
|
thd->mysys_var->current_mutex= &debug_sync_global.ds_mutex;
|
|
|
|
thd->mysys_var->current_cond= &debug_sync_global.ds_cond;
|
|
|
|
}
|
|
|
|
else
|
MDEV-6582: DEBUG_SYNC does not reset mysys_var->current_mutex, causes assertion "Trying to unlock mutex that wasn't locked"
The bug was in DEBUG_SYNC. When waiting, debug_sync_execute() temporarily sets
thd->mysys_var->current_mutex to a new value while waiting. However, if the
old value of current_mutex was NULL, it was not restored, current_mutex
remained set to the temporary value (debug_sync_global.ds_mutex).
This made possible the following race: Thread T1 goes to KILL thread T2. In
THD::awake(), T1 loads T2->mysys_var->current_mutex, it is set to ds_mutex, T1
locks this mutex.
Now T2 runs, it does ENTER_COND, it sets T2->mysys_var->current_mutex to
LOCK_wait_commit (for example).
Then T1 resumes, it reloads mysys_var->current_mutex, now it is set to
LOCK_wait_commit, T1 unlocks this mutex instead of the ds_mutex that it locked
previously.
This causes safe_mutex to assert with the message: "Trying to unlock mutex
LOCK_wait_commit that wasn't locked".
The fix is to ensure that DEBUG_SYNC also will restore
mysys_var->current_mutex in the case where the original value was NULL.
2014-11-26 11:07:32 +01:00
|
|
|
restore_current_mutex = false;
|
2009-09-29 17:38:40 +02:00
|
|
|
|
|
|
|
set_timespec(abstime, action->timeout);
|
|
|
|
DBUG_EXECUTE("debug_sync_exec", {
|
|
|
|
/* Functions as DBUG_PRINT args can change keyword and line nr. */
|
|
|
|
const char *sig_glob= debug_sync_global.ds_signal.c_ptr();
|
|
|
|
DBUG_PRINT("debug_sync_exec",
|
|
|
|
("wait for '%s' at: '%s' curr: '%s'",
|
|
|
|
sig_wait, dsp_name, sig_glob));});
|
|
|
|
|
|
|
|
/*
|
|
|
|
Wait until global signal string matches the wait_for string.
|
|
|
|
Interrupt when thread or query is killed or facility disabled.
|
|
|
|
The facility can become disabled when some thread cannot get
|
|
|
|
the required dynamic memory allocated.
|
|
|
|
*/
|
|
|
|
while (stringcmp(&debug_sync_global.ds_signal, &action->wait_for) &&
|
|
|
|
!thd->killed && opt_debug_sync_timeout)
|
|
|
|
{
|
2009-12-10 04:19:51 +01:00
|
|
|
error= mysql_cond_timedwait(&debug_sync_global.ds_cond,
|
|
|
|
&debug_sync_global.ds_mutex,
|
|
|
|
&abstime);
|
2009-09-29 17:38:40 +02:00
|
|
|
DBUG_EXECUTE("debug_sync", {
|
|
|
|
/* Functions as DBUG_PRINT args can change keyword and line nr. */
|
|
|
|
const char *sig_glob= debug_sync_global.ds_signal.c_ptr();
|
|
|
|
DBUG_PRINT("debug_sync",
|
|
|
|
("awoke from %s global: %s error: %d",
|
|
|
|
sig_wait, sig_glob, error));});
|
|
|
|
if (error == ETIMEDOUT || error == ETIME)
|
|
|
|
{
|
2013-07-02 19:43:35 +02:00
|
|
|
// We should not make the statement fail, even if in strict mode.
|
|
|
|
const bool save_abort_on_warning= thd->abort_on_warning;
|
|
|
|
thd->abort_on_warning= false;
|
2013-06-15 17:32:08 +02:00
|
|
|
push_warning(thd, Sql_condition::WARN_LEVEL_WARN,
|
2015-07-06 19:24:14 +02:00
|
|
|
ER_DEBUG_SYNC_TIMEOUT,
|
|
|
|
ER_THD(thd, ER_DEBUG_SYNC_TIMEOUT));
|
2013-07-02 19:43:35 +02:00
|
|
|
thd->abort_on_warning= save_abort_on_warning;
|
|
|
|
DBUG_EXECUTE_IF("debug_sync_abort_on_timeout", DBUG_ABORT(););
|
2009-09-29 17:38:40 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
error= 0;
|
|
|
|
}
|
|
|
|
DBUG_EXECUTE("debug_sync_exec",
|
|
|
|
if (thd->killed)
|
|
|
|
DBUG_PRINT("debug_sync_exec",
|
|
|
|
("killed %d from '%s' at: '%s'",
|
|
|
|
thd->killed, sig_wait, dsp_name));
|
|
|
|
else
|
|
|
|
DBUG_PRINT("debug_sync_exec",
|
|
|
|
("%s from '%s' at: '%s'",
|
|
|
|
error ? "timeout" : "resume",
|
|
|
|
sig_wait, dsp_name)););
|
|
|
|
|
|
|
|
/*
|
|
|
|
We don't use enter_cond()/exit_cond(). They do not save old
|
|
|
|
mutex and cond. This would prohibit the use of DEBUG_SYNC
|
|
|
|
between other places of enter_cond() and exit_cond(). The
|
|
|
|
protected mutex must always unlocked _before_ mysys_var->mutex
|
|
|
|
is locked. (See comment in THD::exit_cond().)
|
|
|
|
*/
|
2009-12-10 04:19:51 +01:00
|
|
|
mysql_mutex_unlock(&debug_sync_global.ds_mutex);
|
MDEV-6582: DEBUG_SYNC does not reset mysys_var->current_mutex, causes assertion "Trying to unlock mutex that wasn't locked"
The bug was in DEBUG_SYNC. When waiting, debug_sync_execute() temporarily sets
thd->mysys_var->current_mutex to a new value while waiting. However, if the
old value of current_mutex was NULL, it was not restored, current_mutex
remained set to the temporary value (debug_sync_global.ds_mutex).
This made possible the following race: Thread T1 goes to KILL thread T2. In
THD::awake(), T1 loads T2->mysys_var->current_mutex, it is set to ds_mutex, T1
locks this mutex.
Now T2 runs, it does ENTER_COND, it sets T2->mysys_var->current_mutex to
LOCK_wait_commit (for example).
Then T1 resumes, it reloads mysys_var->current_mutex, now it is set to
LOCK_wait_commit, T1 unlocks this mutex instead of the ds_mutex that it locked
previously.
This causes safe_mutex to assert with the message: "Trying to unlock mutex
LOCK_wait_commit that wasn't locked".
The fix is to ensure that DEBUG_SYNC also will restore
mysys_var->current_mutex in the case where the original value was NULL.
2014-11-26 11:07:32 +01:00
|
|
|
if (restore_current_mutex)
|
2010-11-09 16:33:40 +01:00
|
|
|
{
|
|
|
|
mysql_mutex_lock(&thd->mysys_var->mutex);
|
|
|
|
thd->mysys_var->current_mutex= old_mutex;
|
|
|
|
thd->mysys_var->current_cond= old_cond;
|
|
|
|
thd_proc_info(thd, old_proc_info);
|
|
|
|
mysql_mutex_unlock(&thd->mysys_var->mutex);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
thd_proc_info(thd, old_proc_info);
|
2009-09-29 17:38:40 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* In case we don't wait, we just release the mutex. */
|
2009-12-10 04:19:51 +01:00
|
|
|
mysql_mutex_unlock(&debug_sync_global.ds_mutex);
|
2009-09-29 17:38:40 +02:00
|
|
|
} /* end if (action->wait_for.length()) */
|
|
|
|
|
|
|
|
} /* end if (action->execute) */
|
|
|
|
|
|
|
|
/* hit_limit is zero for infinite. Don't decrement unconditionally. */
|
|
|
|
if (action->hit_limit)
|
|
|
|
{
|
|
|
|
if (!--action->hit_limit)
|
|
|
|
{
|
2011-09-23 00:13:38 +02:00
|
|
|
thd->killed= KILL_QUERY;
|
2009-09-29 17:38:40 +02:00
|
|
|
my_error(ER_DEBUG_SYNC_HIT_LIMIT, MYF(0));
|
|
|
|
}
|
|
|
|
DBUG_PRINT("debug_sync_exec", ("hit_limit: %lu at: '%s'",
|
|
|
|
action->hit_limit, dsp_name));
|
|
|
|
}
|
|
|
|
|
|
|
|
DBUG_VOID_RETURN;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Execute requested action at a synchronization point.
|
|
|
|
|
|
|
|
@param[in] thd thread handle
|
|
|
|
@param[in] sync_point_name name of synchronization point
|
|
|
|
@param[in] name_len length of sync point name
|
|
|
|
*/
|
|
|
|
|
2012-03-28 19:26:00 +02:00
|
|
|
static void debug_sync(THD *thd, const char *sync_point_name, size_t name_len)
|
2009-09-29 17:38:40 +02:00
|
|
|
{
|
2012-03-28 19:26:00 +02:00
|
|
|
if (!thd)
|
2013-03-25 23:03:13 +01:00
|
|
|
{
|
|
|
|
if (!(thd= current_thd))
|
|
|
|
return;
|
|
|
|
}
|
2012-03-28 19:26:00 +02:00
|
|
|
|
2009-09-29 17:38:40 +02:00
|
|
|
st_debug_sync_control *ds_control= thd->debug_sync_control;
|
|
|
|
st_debug_sync_action *action;
|
|
|
|
DBUG_ENTER("debug_sync");
|
|
|
|
DBUG_ASSERT(sync_point_name);
|
|
|
|
DBUG_ASSERT(name_len);
|
|
|
|
DBUG_ASSERT(ds_control);
|
|
|
|
DBUG_PRINT("debug_sync_point", ("hit: '%s'", sync_point_name));
|
|
|
|
|
|
|
|
/* Statistics. */
|
|
|
|
ds_control->dsp_hits++;
|
|
|
|
|
|
|
|
if (ds_control->ds_active &&
|
|
|
|
(action= debug_sync_find(ds_control->ds_action, ds_control->ds_active,
|
|
|
|
sync_point_name, name_len)) &&
|
|
|
|
action->activation_count)
|
|
|
|
{
|
|
|
|
/* Sync point is active (action exists). */
|
|
|
|
debug_sync_execute(thd, action);
|
|
|
|
|
|
|
|
/* Statistics. */
|
|
|
|
ds_control->dsp_executed++;
|
|
|
|
|
|
|
|
/* If action became inactive, remove it to shrink the search array. */
|
|
|
|
if (!action->activation_count)
|
|
|
|
debug_sync_remove_action(ds_control, action);
|
|
|
|
}
|
|
|
|
|
|
|
|
DBUG_VOID_RETURN;
|
|
|
|
}
|
|
|
|
|
2010-03-19 10:06:40 +01:00
|
|
|
/**
|
|
|
|
Define debug sync action.
|
|
|
|
|
|
|
|
@param[in] thd thread handle
|
|
|
|
@param[in] action_str action string
|
|
|
|
|
|
|
|
@return status
|
|
|
|
@retval FALSE ok
|
|
|
|
@retval TRUE error
|
|
|
|
|
|
|
|
@description
|
|
|
|
The function is similar to @c debug_sync_eval_action but is
|
|
|
|
to be called immediately from the server code rather than
|
|
|
|
to be triggered by setting a value to DEBUG_SYNC system variable.
|
|
|
|
|
|
|
|
@note
|
|
|
|
The input string is copied prior to be fed to
|
|
|
|
@c debug_sync_eval_action to let the latter modify it.
|
|
|
|
|
|
|
|
Caution.
|
|
|
|
The function allocates in THD::mem_root and therefore
|
|
|
|
is not recommended to be deployed inside big loops.
|
|
|
|
*/
|
|
|
|
|
|
|
|
bool debug_sync_set_action(THD *thd, const char *action_str, size_t len)
|
|
|
|
{
|
|
|
|
bool rc;
|
|
|
|
char *value;
|
|
|
|
DBUG_ENTER("debug_sync_set_action");
|
|
|
|
DBUG_ASSERT(thd);
|
|
|
|
DBUG_ASSERT(action_str);
|
|
|
|
|
|
|
|
value= strmake_root(thd->mem_root, action_str, len);
|
2016-05-17 13:27:10 +02:00
|
|
|
rc= debug_sync_eval_action(thd, value, value + len);
|
2010-03-19 10:06:40 +01:00
|
|
|
DBUG_RETURN(rc);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-02-19 15:16:31 +01:00
|
|
|
#else /* defined(ENABLED_DEBUG_SYNC) */
|
|
|
|
/* prevent linker/lib warning about file without public symbols */
|
|
|
|
int debug_sync_dummy;
|
2009-09-29 17:38:40 +02:00
|
|
|
#endif /* defined(ENABLED_DEBUG_SYNC) */
|