From e5b67a46bcb75a751f5906f19694971bc803d67c Mon Sep 17 00:00:00 2001 From: Vladislav Vaintroub Date: Thu, 23 Mar 2017 11:48:56 +0000 Subject: [PATCH] MDEV-12345 Performance : replace calls to clock() inside trx_start_low() by THD::start_utime --- sql/sql_class.cc | 9 +++++++++ storage/innobase/handler/ha_innodb.cc | 9 +++++++++ storage/innobase/handler/ha_innodb.h | 9 +++++++++ storage/innobase/include/ha_prototypes.h | 7 +++++++ storage/innobase/include/trx0trx.h | 2 +- storage/innobase/trx/trx0trx.cc | 3 ++- storage/xtradb/handler/ha_innodb.cc | 9 +++++++++ storage/xtradb/handler/ha_innodb.h | 9 +++++++++ storage/xtradb/include/ha_prototypes.h | 7 +++++++ storage/xtradb/include/trx0trx.h | 2 +- storage/xtradb/trx/trx0trx.cc | 3 ++- 11 files changed, 65 insertions(+), 4 deletions(-) diff --git a/sql/sql_class.cc b/sql/sql_class.cc index 3a7e501c6c2..5dba389115a 100644 --- a/sql/sql_class.cc +++ b/sql/sql_class.cc @@ -4534,6 +4534,15 @@ extern "C" int thd_rpl_is_parallel(const MYSQL_THD thd) return thd->rgi_slave && thd->rgi_slave->is_parallel_exec; } + +/* Returns high resolution timestamp for the start + of the current query. */ +extern "C" unsigned long long thd_start_utime(const MYSQL_THD thd) +{ + return thd->start_utime; +} + + /* This function can optionally be called to check if thd_report_wait_for() needs to be called for waits done by a given transaction. diff --git a/storage/innobase/handler/ha_innodb.cc b/storage/innobase/handler/ha_innodb.cc index c9d78d68092..17c0b45cffe 100644 --- a/storage/innobase/handler/ha_innodb.cc +++ b/storage/innobase/handler/ha_innodb.cc @@ -1705,6 +1705,15 @@ thd_has_edited_nontrans_tables( return((ibool) thd_non_transactional_update(thd)); } +/* Return high resolution timestamp for the start of the current query */ +UNIV_INTERN +ib_uint64_t +thd_query_start_micro( + const THD* thd) /*!< in: thread handle */ +{ + return thd_start_utime(thd); +} + /******************************************************************//** Returns true if the thread is executing a SELECT statement. @return true if thd is executing SELECT */ diff --git a/storage/innobase/handler/ha_innodb.h b/storage/innobase/handler/ha_innodb.h index 478187e0b23..90dfb6102b3 100644 --- a/storage/innobase/handler/ha_innodb.h +++ b/storage/innobase/handler/ha_innodb.h @@ -420,6 +420,15 @@ int thd_slave_thread(const MYSQL_THD thd); */ int thd_non_transactional_update(const MYSQL_THD thd); +/** + Get high resolution timestamp for the current query start time. + The timestamp is not anchored to any specific point in time, + but can be used for comparison. + + @retval timestamp in microseconds precision +*/ +unsigned long long thd_start_utime(const MYSQL_THD thd); + /** Get the user thread's binary logging format @param thd user thread diff --git a/storage/innobase/include/ha_prototypes.h b/storage/innobase/include/ha_prototypes.h index 12453099ef7..2a8f133cb3a 100644 --- a/storage/innobase/include/ha_prototypes.h +++ b/storage/innobase/include/ha_prototypes.h @@ -150,6 +150,13 @@ thd_has_edited_nontrans_tables( /*===========================*/ THD* thd); /*!< in: thread handle */ +/** +Get high resolution timestamp for the current query start time. + +@retval timestamp in microseconds precision +*/ +unsigned long long thd_query_start_micro(const MYSQL_THD thd); + /*************************************************************//** Prints info of a THD object (== user session thread) to the given file. */ UNIV_INTERN diff --git a/storage/innobase/include/trx0trx.h b/storage/innobase/include/trx0trx.h index 11836183d57..5936fa90e84 100644 --- a/storage/innobase/include/trx0trx.h +++ b/storage/innobase/include/trx0trx.h @@ -849,7 +849,7 @@ struct trx_t{ time_t start_time; /*!< time the trx state last time became TRX_STATE_ACTIVE */ - clock_t start_time_micro; /*!< start time of transaction in + ib_uint64_t start_time_micro; /*!< start time of transaction in microseconds */ trx_id_t id; /*!< transaction id */ XID xid; /*!< X/Open XA transaction diff --git a/storage/innobase/trx/trx0trx.cc b/storage/innobase/trx/trx0trx.cc index 301ef17197e..318f1e284ec 100644 --- a/storage/innobase/trx/trx0trx.cc +++ b/storage/innobase/trx/trx0trx.cc @@ -923,7 +923,8 @@ trx_start_low( trx->start_time = ut_time(); - trx->start_time_micro = clock(); + trx->start_time_micro = + trx->mysql_thd ? thd_query_start_micro(trx->mysql_thd) : 0; MONITOR_INC(MONITOR_TRX_ACTIVE); } diff --git a/storage/xtradb/handler/ha_innodb.cc b/storage/xtradb/handler/ha_innodb.cc index 4ca4259273f..edc0f3d90f4 100644 --- a/storage/xtradb/handler/ha_innodb.cc +++ b/storage/xtradb/handler/ha_innodb.cc @@ -1981,6 +1981,15 @@ thd_has_edited_nontrans_tables( return((ibool) thd_non_transactional_update(thd)); } +/* Return high resolution timestamp for the start of the current query */ +UNIV_INTERN +ib_uint64_t +thd_query_start_micro( + const THD* thd) /*!< in: thread handle */ +{ + return thd_start_utime(thd); +} + /******************************************************************//** Returns true if the thread is executing a SELECT statement. @return true if thd is executing SELECT */ diff --git a/storage/xtradb/handler/ha_innodb.h b/storage/xtradb/handler/ha_innodb.h index f91c0fb4703..62b80c492a1 100644 --- a/storage/xtradb/handler/ha_innodb.h +++ b/storage/xtradb/handler/ha_innodb.h @@ -427,6 +427,15 @@ int thd_slave_thread(const MYSQL_THD thd); */ int thd_non_transactional_update(const MYSQL_THD thd); +/** + Get high resolution timestamp for the current query start time. + The timestamp is not anchored to any specific point in time, + but can be used for comparison. + + @retval timestamp in microseconds precision +*/ +unsigned long long thd_start_utime(const MYSQL_THD thd); + /** Get the user thread's binary logging format @param thd user thread diff --git a/storage/xtradb/include/ha_prototypes.h b/storage/xtradb/include/ha_prototypes.h index dbb23d81eec..69d4d9c175c 100644 --- a/storage/xtradb/include/ha_prototypes.h +++ b/storage/xtradb/include/ha_prototypes.h @@ -157,6 +157,13 @@ thd_has_edited_nontrans_tables( /*===========================*/ THD* thd); /*!< in: thread handle */ +/** +Get high resolution timestamp for the current query start time. + +@retval timestamp in microseconds precision +*/ +unsigned long long thd_query_start_micro(const MYSQL_THD thd); + /*************************************************************//** Prints info of a THD object (== user session thread) to the given file. */ UNIV_INTERN diff --git a/storage/xtradb/include/trx0trx.h b/storage/xtradb/include/trx0trx.h index fc710a86d74..9e2064c3dc2 100644 --- a/storage/xtradb/include/trx0trx.h +++ b/storage/xtradb/include/trx0trx.h @@ -882,7 +882,7 @@ struct trx_t{ time_t start_time; /*!< time the trx state last time became TRX_STATE_ACTIVE */ - clock_t start_time_micro; /*!< start time of transaction in + ib_uint64_t start_time_micro; /*!< start time of transaction in microseconds */ trx_id_t id; /*!< transaction id */ XID xid; /*!< X/Open XA transaction diff --git a/storage/xtradb/trx/trx0trx.cc b/storage/xtradb/trx/trx0trx.cc index 73c1172ff0f..439897a5b96 100644 --- a/storage/xtradb/trx/trx0trx.cc +++ b/storage/xtradb/trx/trx0trx.cc @@ -1121,7 +1121,8 @@ trx_start_low( trx->start_time = ut_time(); - trx->start_time_micro = clock(); + trx->start_time_micro = + trx->mysql_thd ? thd_query_start_micro(trx->mysql_thd) : 0; MONITOR_INC(MONITOR_TRX_ACTIVE); }