diff --git a/mysql-test/r/status.result b/mysql-test/r/status.result new file mode 100644 index 00000000000..661009b6cda --- /dev/null +++ b/mysql-test/r/status.result @@ -0,0 +1,6 @@ +Variable_name Value +Table_locks_immediate 5 +Table_locks_waited 0 +Variable_name Value +Table_locks_immediate 8 +Table_locks_waited 1 diff --git a/mysql-test/t/status.test b/mysql-test/t/status.test new file mode 100644 index 00000000000..2f65f87f29f --- /dev/null +++ b/mysql-test/t/status.test @@ -0,0 +1,23 @@ +connect (con1,localhost,root,,test,0,mysql-master.sock); +connect (con2,localhost,root,,test,0,mysql-master.sock); + +#don't polute binlog +show status like 'Table_lock%'; +connection con1; +SET SQL_LOG_BIN=0; +drop table if exists t1; +create table t1(n int); +insert into t1 values(1); +connection con2; +lock tables t1 read; +unlock tables; +lock tables t1 read; +connection con1; +send update t1 set n = 3; +connection con2; +sleep 0.5; +unlock tables; +connection con1; +reap; +show status like 'Table_lock%'; + diff --git a/mysys/thr_lock.c b/mysys/thr_lock.c index 5c48ad435a4..6f5f8afac3c 100644 --- a/mysys/thr_lock.c +++ b/mysys/thr_lock.c @@ -83,7 +83,7 @@ multiple read locks. #include my_bool thr_lock_inited=0; - +ulong locks_immediate = 0L, locks_waited = 0L; /* The following constants are only for debug output */ #define MAX_THREADS 100 @@ -387,6 +387,7 @@ static my_bool wait_for_lock(struct st_lock_list *wait, THR_LOCK_DATA *data, else { result=0; + ++locks_waited; if (data->lock->get_status) (*data->lock->get_status)(data->status_param); check_locks(data->lock,"got wait_for_lock",0); @@ -447,6 +448,7 @@ int thr_lock(THR_LOCK_DATA *data,enum thr_lock_type lock_type) check_locks(lock,"read lock with old write lock",0); if (lock->get_status) (*lock->get_status)(data->status_param); + ++locks_immediate; goto end; } if (lock->write.data->type == TL_WRITE_ONLY) @@ -470,6 +472,7 @@ int thr_lock(THR_LOCK_DATA *data,enum thr_lock_type lock_type) if ((int) lock_type == (int) TL_READ_NO_INSERT) lock->read_no_write_count++; check_locks(lock,"read lock with no write locks",0); + ++locks_immediate; goto end; } /* Can't get lock yet; Wait for it */ @@ -501,6 +504,7 @@ int thr_lock(THR_LOCK_DATA *data,enum thr_lock_type lock_type) data->cond=get_cond(); if (lock->get_status) (*lock->get_status)(data->status_param); + ++locks_immediate; goto end; } } @@ -535,6 +539,7 @@ int thr_lock(THR_LOCK_DATA *data,enum thr_lock_type lock_type) check_locks(lock,"second write lock",0); if (data->lock->get_status) (*data->lock->get_status)(data->status_param); + ++locks_immediate; goto end; } DBUG_PRINT("lock",("write locked by thread: %ld", @@ -560,6 +565,7 @@ int thr_lock(THR_LOCK_DATA *data,enum thr_lock_type lock_type) if (data->lock->get_status) (*data->lock->get_status)(data->status_param); check_locks(lock,"only write lock",0); + ++locks_immediate; goto end; } } diff --git a/sql/mysqld.cc b/sql/mysqld.cc index f9c4d10f46f..36fc5a49148 100644 --- a/sql/mysqld.cc +++ b/sql/mysqld.cc @@ -260,6 +260,7 @@ ulong query_id=1L,long_query_count,long_query_time,aborted_threads, delayed_queue_size,delayed_insert_threads,delayed_insert_writes, delayed_rows_in_use,delayed_insert_errors,flush_time, thread_created; ulong filesort_rows, filesort_range_count, filesort_scan_count; +extern "C" ulong locks_immediate, locks_waited; ulong filesort_merge_passes; ulong select_range_check_count, select_range_count, select_scan_count; ulong select_full_range_join_count,select_full_join_count; @@ -2804,6 +2805,8 @@ struct show_var_st status_vars[]= { {"Sort_range", (char*) &filesort_range_count, SHOW_LONG}, {"Sort_rows", (char*) &filesort_rows, SHOW_LONG}, {"Sort_scan", (char*) &filesort_scan_count, SHOW_LONG}, + {"Table_locks_immediate", (char*) &locks_immediate, SHOW_LONG}, + {"Table_locks_waited", (char*) &locks_waited, SHOW_LONG}, {"Threads_cached", (char*) &cached_thread_count, SHOW_LONG_CONST}, {"Threads_created", (char*) &thread_created, SHOW_LONG_CONST}, {"Threads_connected", (char*) &thread_count, SHOW_INT_CONST}, diff --git a/sql/sql_base.cc b/sql/sql_base.cc index 96d6ea660aa..ed7a8027079 100644 --- a/sql/sql_base.cc +++ b/sql/sql_base.cc @@ -32,6 +32,7 @@ TABLE *unused_tables; /* Used by mysql_test */ HASH open_cache; /* Used by mysql_test */ +extern "C" ulong locks_waited, locks_immediate; static int open_unireg_entry(THD *thd,TABLE *entry,const char *db, const char *name, const char *alias, bool locked); @@ -1166,7 +1167,8 @@ bool wait_for_tables(THD *thd) { /* Now we can open all tables without any interference */ thd->proc_info="Reopen tables"; - result=reopen_tables(thd,0,0); + if(!(result=reopen_tables(thd,0,0))) + ++locks_waited; } pthread_mutex_unlock(&LOCK_open); thd->proc_info=0;