Commit 615d51b6 authored by Michael Widenius's avatar Michael Widenius

Speed up connection time:

-Change my_rnd() slightly to make it safer if two threads use it at the same time.
-Avoid some sprintf and strmov in vio.
-Changed thread_count to be automically incremented (instead of under LOCK_thread_count).
-Thread cache now uses LOCK_thread_cache instead of LOCK_thread_count.
-Moved delete thd out from LOCK_thread_count.
-Save some mysql_cond_broadcast(&COND_thread_count) calls.
-Removed call to getsockname() during connect.
-Initialize random generator without locks.

Other things:
-Added thread_safe_decrement32() and thread_safe_increment32()
-Removed sql_rnd_with_mutex() and get_thread_running()

Thanks to Yoshinori Matsunobu for the benchmark of connection speed and to
Domas Mituzas for the inspiration for many of the fixes.
 

include/violite.h:
  Change desc to a string pointer
mysql-test/suite/perfschema/r/all_instances.result:
  Added new mutex
mysys/my_rnd.c:
  Change my_rnd() slightly to make it safer if two threads use it at the same time.
sql/event_scheduler.cc:
  Changed thread_count to be automically incremented
  Moved some safe things out from LOCK_thread_count.
  Simplify deleting of THD for running thread.
sql/mysqld.cc:
  Changed thread_count to be automically incremented
  Thread cache now uses LOCK_thread_cache instead of LOCK_thread_count
  Added delete_running_thd()
  Moved delete thd out from LOCK_thread_count
  More DBUG_ENTER
  Only call  mysql_cond_broadcast(&COND_thread_count) if thread_count is 0
  Removed call to getsockname() (old not anymore needed check)
sql/mysqld.h:
  Removed sql_rnd_with_mutex() (not needed anymore)
  Removed not used function get_thread_running()
  Added thread_safe_decrement32() and thread_safe_increment32()
  Simplified dec_thread_running() and inc_thread_running()
sql/sql_class.cc:
  Removed thd_lock_thread_count() and thd_unlock_thread_count()
  Initialize random generator without locks
sql/sql_insert.cc:
  Changed thread_count to be automically incremented
sql/sql_parse.cc:
  Changed thread_count to be automically incremented
vio/vio.c:
  Don't generate 'desc' with sprintf/strmov. Assign a pointer instead.
  (Good enough as this is just for debugging)
parent 8b047ac5
...@@ -216,7 +216,7 @@ struct st_vio ...@@ -216,7 +216,7 @@ struct st_vio
struct sockaddr_storage remote; /* Remote internet address */ struct sockaddr_storage remote; /* Remote internet address */
int addrLen; /* Length of remote address */ int addrLen; /* Length of remote address */
enum enum_vio_type type; /* Type of connection */ enum enum_vio_type type; /* Type of connection */
char desc[30]; /* String description */ const char *desc; /* String description */
char *read_buffer; /* buffer for vio_read_buff */ char *read_buffer; /* buffer for vio_read_buff */
char *read_pos; /* start of unfetched data in the char *read_pos; /* start of unfetched data in the
read buffer */ read buffer */
......
...@@ -64,6 +64,7 @@ wait/synch/mutex/sql/LOCK_server_started ...@@ -64,6 +64,7 @@ wait/synch/mutex/sql/LOCK_server_started
wait/synch/mutex/sql/LOCK_slave_list wait/synch/mutex/sql/LOCK_slave_list
wait/synch/mutex/sql/LOCK_stats wait/synch/mutex/sql/LOCK_stats
wait/synch/mutex/sql/LOCK_status wait/synch/mutex/sql/LOCK_status
wait/synch/mutex/sql/LOCK_thread_cache
wait/synch/mutex/sql/LOCK_thread_count wait/synch/mutex/sql/LOCK_thread_count
wait/synch/mutex/sql/LOCK_user_conn wait/synch/mutex/sql/LOCK_user_conn
wait/synch/mutex/sql/LOCK_user_locks wait/synch/mutex/sql/LOCK_user_locks
......
...@@ -45,11 +45,20 @@ void my_rnd_init(struct my_rnd_struct *rand_st, ulong seed1, ulong seed2) ...@@ -45,11 +45,20 @@ void my_rnd_init(struct my_rnd_struct *rand_st, ulong seed1, ulong seed2)
RETURN VALUE RETURN VALUE
generated pseudo random number generated pseudo random number
NOTE:
This is codes so that it can be called by two threads at the same time
with minimum impact.
(As the number is supposed to be random, it doesn't matter much if
rand->seed1 or rand->seed2 are updated with slightly wrong numbers or
if two threads gets the same number.
*/ */
double my_rnd(struct my_rnd_struct *rand_st) double my_rnd(struct my_rnd_struct *rand_st)
{ {
rand_st->seed1=(rand_st->seed1*3+rand_st->seed2) % rand_st->max_value; unsigned long seed1;
rand_st->seed2=(rand_st->seed1+rand_st->seed2+33) % rand_st->max_value; seed1= (rand_st->seed1*3+rand_st->seed2) % rand_st->max_value;
return (((double) rand_st->seed1)/rand_st->max_value_dbl); rand_st->seed2=(seed1+rand_st->seed2+33) % rand_st->max_value;
rand_st->seed1= seed1;
return (((double) seed1)/rand_st->max_value_dbl);
} }
...@@ -132,11 +132,11 @@ post_init_event_thread(THD *thd) ...@@ -132,11 +132,11 @@ post_init_event_thread(THD *thd)
return TRUE; return TRUE;
} }
thread_safe_increment32(&thread_count, &thread_count_lock);
mysql_mutex_lock(&LOCK_thread_count); mysql_mutex_lock(&LOCK_thread_count);
threads.append(thd); threads.append(thd);
thread_count++;
inc_thread_running();
mysql_mutex_unlock(&LOCK_thread_count); mysql_mutex_unlock(&LOCK_thread_count);
inc_thread_running();
return FALSE; return FALSE;
} }
...@@ -154,12 +154,8 @@ deinit_event_thread(THD *thd) ...@@ -154,12 +154,8 @@ deinit_event_thread(THD *thd)
{ {
thd->proc_info= "Clearing"; thd->proc_info= "Clearing";
DBUG_PRINT("exit", ("Event thread finishing")); DBUG_PRINT("exit", ("Event thread finishing"));
mysql_mutex_lock(&LOCK_thread_count);
thread_count--; delete_running_thd(thd);
dec_thread_running();
delete thd;
mysql_cond_broadcast(&COND_thread_count);
mysql_mutex_unlock(&LOCK_thread_count);
} }
...@@ -436,12 +432,7 @@ Event_scheduler::start() ...@@ -436,12 +432,7 @@ Event_scheduler::start()
ret= TRUE; ret= TRUE;
new_thd->proc_info= "Clearing"; new_thd->proc_info= "Clearing";
mysql_mutex_lock(&LOCK_thread_count); delete_running_thd(new_thd);
thread_count--;
dec_thread_running();
delete new_thd;
mysql_cond_broadcast(&COND_thread_count);
mysql_mutex_unlock(&LOCK_thread_count);
} }
end: end:
UNLOCK_DATA(); UNLOCK_DATA();
...@@ -570,12 +561,7 @@ error: ...@@ -570,12 +561,7 @@ error:
if (new_thd) if (new_thd)
{ {
new_thd->proc_info= "Clearing"; new_thd->proc_info= "Clearing";
mysql_mutex_lock(&LOCK_thread_count); delete_running_thd(new_thd);
thread_count--;
dec_thread_running();
delete new_thd;
mysql_cond_broadcast(&COND_thread_count);
mysql_mutex_unlock(&LOCK_thread_count);
} }
delete event_name; delete event_name;
DBUG_RETURN(TRUE); DBUG_RETURN(TRUE);
......
This diff is collapsed.
...@@ -56,6 +56,7 @@ void kill_mysql(void); ...@@ -56,6 +56,7 @@ void kill_mysql(void);
void close_connection(THD *thd, uint sql_errno= 0); void close_connection(THD *thd, uint sql_errno= 0);
void handle_connection_in_main_thread(THD *thd); void handle_connection_in_main_thread(THD *thd);
void create_thread_to_handle_connection(THD *thd); void create_thread_to_handle_connection(THD *thd);
void delete_running_thd(THD *thd);
void unlink_thd(THD *thd); void unlink_thd(THD *thd);
bool one_thread_per_connection_end(THD *thd, bool put_in_cache); bool one_thread_per_connection_end(THD *thd, bool put_in_cache);
void flush_thread_cache(); void flush_thread_cache();
...@@ -89,7 +90,6 @@ extern bool opt_ignore_builtin_innodb; ...@@ -89,7 +90,6 @@ extern bool opt_ignore_builtin_innodb;
extern my_bool opt_character_set_client_handshake; extern my_bool opt_character_set_client_handshake;
extern bool volatile abort_loop; extern bool volatile abort_loop;
extern bool in_bootstrap; extern bool in_bootstrap;
extern uint volatile thread_count;
extern uint connection_count; extern uint connection_count;
extern my_bool opt_safe_user_create; extern my_bool opt_safe_user_create;
extern my_bool opt_safe_show_db, opt_local_infile, opt_myisam_use_mmap; extern my_bool opt_safe_show_db, opt_local_infile, opt_myisam_use_mmap;
...@@ -352,7 +352,8 @@ extern mysql_rwlock_t LOCK_system_variables_hash; ...@@ -352,7 +352,8 @@ extern mysql_rwlock_t LOCK_system_variables_hash;
extern mysql_cond_t COND_thread_count; extern mysql_cond_t COND_thread_count;
extern mysql_cond_t COND_manager; extern mysql_cond_t COND_manager;
extern int32 thread_running; extern int32 thread_running;
extern my_atomic_rwlock_t thread_running_lock; extern int32 thread_count;
extern my_atomic_rwlock_t thread_running_lock, thread_count_lock;
extern char *opt_ssl_ca, *opt_ssl_capath, *opt_ssl_cert, *opt_ssl_cipher, extern char *opt_ssl_ca, *opt_ssl_capath, *opt_ssl_cert, *opt_ssl_cipher,
*opt_ssl_key; *opt_ssl_key;
...@@ -479,42 +480,30 @@ inline void table_case_convert(char * name, uint length) ...@@ -479,42 +480,30 @@ inline void table_case_convert(char * name, uint length)
name, length, name, length); name, length, name, length);
} }
inline ulong sql_rnd_with_mutex() inline void thread_safe_increment32(int32 *value, my_atomic_rwlock_t *lock)
{ {
mysql_mutex_lock(&LOCK_thread_count); my_atomic_rwlock_wrlock(lock);
ulong tmp=(ulong) (my_rnd(&sql_rand) * 0xffffffff); /* make all bits random */ (void) my_atomic_add32(value, 1);
mysql_mutex_unlock(&LOCK_thread_count); my_atomic_rwlock_wrunlock(lock);
return tmp;
} }
inline int32 inline void thread_safe_decrement32(int32 *value, my_atomic_rwlock_t *lock)
inc_thread_running()
{ {
int32 num_thread_running; my_atomic_rwlock_wrlock(lock);
my_atomic_rwlock_wrlock(&thread_running_lock); (void) my_atomic_add32(value, -1);
num_thread_running= my_atomic_add32(&thread_running, 1); my_atomic_rwlock_wrunlock(lock);
my_atomic_rwlock_wrunlock(&thread_running_lock);
return (num_thread_running+1);
} }
inline int32 inline void
dec_thread_running() inc_thread_running()
{ {
int32 num_thread_running; thread_safe_increment32(&thread_running, &thread_running_lock);
my_atomic_rwlock_wrlock(&thread_running_lock);
num_thread_running= my_atomic_add32(&thread_running, -1);
my_atomic_rwlock_wrunlock(&thread_running_lock);
return (num_thread_running-1);
} }
inline int32 inline void
get_thread_running() dec_thread_running()
{ {
int32 num_thread_running; thread_safe_decrement32(&thread_running, &thread_running_lock);
my_atomic_rwlock_wrlock(&thread_running_lock);
num_thread_running= my_atomic_load32(&thread_running);
my_atomic_rwlock_wrunlock(&thread_running_lock);
return num_thread_running;
} }
void set_server_version(void); void set_server_version(void);
......
...@@ -349,29 +349,6 @@ void thd_set_thread_stack(THD *thd, char *stack_start) ...@@ -349,29 +349,6 @@ void thd_set_thread_stack(THD *thd, char *stack_start)
thd->thread_stack= stack_start; thd->thread_stack= stack_start;
} }
/**
Lock connection data for the set of connections this connection
belongs to
@param thd THD object
*/
void thd_lock_thread_count(THD *)
{
mysql_mutex_lock(&LOCK_thread_count);
}
/**
Lock connection data for the set of connections this connection
belongs to
@param thd THD object
*/
void thd_unlock_thread_count(THD *)
{
mysql_cond_broadcast(&COND_thread_count);
mysql_mutex_unlock(&LOCK_thread_count);
}
/** /**
Close the socket used by this connection Close the socket used by this connection
...@@ -947,7 +924,14 @@ THD::THD() ...@@ -947,7 +924,14 @@ THD::THD()
protocol_binary.init(this); protocol_binary.init(this);
tablespace_op=FALSE; tablespace_op=FALSE;
tmp= sql_rnd_with_mutex();
/*
Initialize the random generator. We call my_rnd() without a lock as
it's not really critical if two threads modifies the structure at the
same time. We ensure that we have an unique number foreach thread
by adding the address of the stack.
*/
tmp= (ulong) (my_rnd(&sql_rand) * 0xffffffff);
my_rnd_init(&rand, tmp + (ulong) &rand, tmp + (ulong) ::global_query_id); my_rnd_init(&rand, tmp + (ulong) &rand, tmp + (ulong) ::global_query_id);
substitute_null_with_insert_id = FALSE; substitute_null_with_insert_id = FALSE;
thr_lock_info_init(&lock_info); /* safety: will be reset after start */ thr_lock_info_init(&lock_info); /* safety: will be reset after start */
......
...@@ -2038,9 +2038,9 @@ public: ...@@ -2038,9 +2038,9 @@ public:
thd.unlink(); // Must be unlinked under lock thd.unlink(); // Must be unlinked under lock
my_free(thd.query()); my_free(thd.query());
thd.security_ctx->user= thd.security_ctx->host=0; thd.security_ctx->user= thd.security_ctx->host=0;
thread_count--;
delayed_insert_threads--; delayed_insert_threads--;
mysql_mutex_unlock(&LOCK_thread_count); mysql_mutex_unlock(&LOCK_thread_count);
thread_safe_decrement32(&thread_count, &thread_count_lock);
mysql_cond_broadcast(&COND_thread_count); /* Tell main we are ready */ mysql_cond_broadcast(&COND_thread_count); /* Tell main we are ready */
} }
...@@ -2175,9 +2175,9 @@ bool delayed_get_table(THD *thd, MDL_request *grl_protection_request, ...@@ -2175,9 +2175,9 @@ bool delayed_get_table(THD *thd, MDL_request *grl_protection_request,
{ {
if (!(di= new Delayed_insert())) if (!(di= new Delayed_insert()))
goto end_create; goto end_create;
mysql_mutex_lock(&LOCK_thread_count);
thread_count++; thread_safe_increment32(&thread_count, &thread_count_lock);
mysql_mutex_unlock(&LOCK_thread_count);
/* /*
Annotating delayed inserts is not supported. Annotating delayed inserts is not supported.
*/ */
......
...@@ -645,9 +645,10 @@ end: ...@@ -645,9 +645,10 @@ end:
delete thd; delete thd;
#ifndef EMBEDDED_LIBRARY #ifndef EMBEDDED_LIBRARY
mysql_mutex_lock(&LOCK_thread_count); thread_safe_decrement32(&thread_count, &thread_count_lock);
thread_count--;
in_bootstrap= FALSE; in_bootstrap= FALSE;
mysql_mutex_lock(&LOCK_thread_count);
mysql_cond_broadcast(&COND_thread_count); mysql_cond_broadcast(&COND_thread_count);
mysql_mutex_unlock(&LOCK_thread_count); mysql_mutex_unlock(&LOCK_thread_count);
my_thread_end(); my_thread_end();
......
...@@ -213,9 +213,7 @@ Vio *vio_new(my_socket sd, enum enum_vio_type type, uint flags) ...@@ -213,9 +213,7 @@ Vio *vio_new(my_socket sd, enum enum_vio_type type, uint flags)
if ((vio = (Vio*) my_malloc(sizeof(*vio),MYF(MY_WME)))) if ((vio = (Vio*) my_malloc(sizeof(*vio),MYF(MY_WME))))
{ {
vio_init(vio, type, sd, 0, flags); vio_init(vio, type, sd, 0, flags);
sprintf(vio->desc, vio->desc= (vio->type == VIO_TYPE_SOCKET ? "socket" : "TCP/IP");
(vio->type == VIO_TYPE_SOCKET ? "socket (%d)" : "TCP/IP (%d)"),
vio->sd);
#if !defined(__WIN__) #if !defined(__WIN__)
#if !defined(NO_FCNTL_NONBLOCK) #if !defined(NO_FCNTL_NONBLOCK)
/* /*
...@@ -257,7 +255,7 @@ Vio *vio_new_win32pipe(HANDLE hPipe) ...@@ -257,7 +255,7 @@ Vio *vio_new_win32pipe(HANDLE hPipe)
if ((vio = (Vio*) my_malloc(sizeof(Vio),MYF(MY_WME)))) if ((vio = (Vio*) my_malloc(sizeof(Vio),MYF(MY_WME))))
{ {
vio_init(vio, VIO_TYPE_NAMEDPIPE, 0, hPipe, VIO_LOCALHOST); vio_init(vio, VIO_TYPE_NAMEDPIPE, 0, hPipe, VIO_LOCALHOST);
strmov(vio->desc, "named pipe"); vio->desc= "named pipe";
} }
DBUG_RETURN(vio); DBUG_RETURN(vio);
} }
...@@ -282,7 +280,7 @@ Vio *vio_new_win32shared_memory(HANDLE handle_file_map, HANDLE handle_map, ...@@ -282,7 +280,7 @@ Vio *vio_new_win32shared_memory(HANDLE handle_file_map, HANDLE handle_map,
vio->event_conn_closed= event_conn_closed; vio->event_conn_closed= event_conn_closed;
vio->shared_memory_remain= 0; vio->shared_memory_remain= 0;
vio->shared_memory_pos= handle_map; vio->shared_memory_pos= handle_map;
strmov(vio->desc, "shared memory"); vio->desc= "shared memory";
} }
DBUG_RETURN(vio); DBUG_RETURN(vio);
} }
......
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