163
163
#include " sql/thd_raii.h"
164
164
#include " sql/transaction.h" // trans_rollback_implicit
165
165
#include " sql/transaction_info.h"
166
+ #include " sql/userstat.h"
166
167
#include " sql_string.h"
167
168
#include " thr_lock.h"
168
169
#include " violite.h"
@@ -1448,6 +1449,12 @@ bool dispatch_command(THD *thd, const COM_DATA *com_data,
1448
1449
if (!(server_command_flags[command] & CF_SKIP_QUESTIONS))
1449
1450
thd->status_var .questions ++;
1450
1451
1452
+ /* Declare userstat variables and start timer */
1453
+ double start_busy_usecs = 0.0 ;
1454
+ double start_cpu_nsecs = 0.0 ;
1455
+ if (unlikely (opt_userstat))
1456
+ userstat_start_timer (&start_busy_usecs, &start_cpu_nsecs);
1457
+
1451
1458
/* *
1452
1459
Clear the set of flags that are expected to be cleared at the
1453
1460
beginning of each command.
@@ -1644,7 +1651,7 @@ bool dispatch_command(THD *thd, const COM_DATA *com_data,
1644
1651
Parser_state parser_state;
1645
1652
if (parser_state.init (thd, thd->query ().str , thd->query ().length )) break ;
1646
1653
1647
- mysql_parse (thd, &parser_state);
1654
+ mysql_parse (thd, &parser_state, false );
1648
1655
1649
1656
DBUG_EXECUTE_IF (" parser_stmt_to_error_log" , {
1650
1657
LogErr (INFORMATION_LEVEL, ER_PARSER_TRACE, thd->query ().str );
@@ -1716,7 +1723,7 @@ bool dispatch_command(THD *thd, const COM_DATA *com_data,
1716
1723
thd->set_time (); /* Reset the query start time. */
1717
1724
parser_state.reset (beginning_of_next_stmt, length);
1718
1725
/* TODO: set thd->lex->sql_command to SQLCOM_END here */
1719
- mysql_parse (thd, &parser_state);
1726
+ mysql_parse (thd, &parser_state, false );
1720
1727
}
1721
1728
1722
1729
/* Need to set error to true for graceful shutdown */
@@ -1993,6 +2000,15 @@ bool dispatch_command(THD *thd, const COM_DATA *com_data,
1993
2000
DBUG_ASSERT (thd->open_tables == NULL ||
1994
2001
(thd->locked_tables_mode == LTM_LOCK_TABLES));
1995
2002
2003
+ /* Update user statistics only if at least one timer was initialized */
2004
+ if (unlikely (start_busy_usecs > 0.0 || start_cpu_nsecs > 0.0 )) {
2005
+ userstat_finish_timer (start_busy_usecs, start_cpu_nsecs, &thd->busy_time ,
2006
+ &thd->cpu_time );
2007
+ /* Updates THD stats and the global user stats. */
2008
+ thd->update_stats (true );
2009
+ update_global_user_stats (thd, true , time (nullptr ));
2010
+ }
2011
+
1996
2012
/* Finalize server status flags after executing a command. */
1997
2013
thd->update_slow_query_status ();
1998
2014
if (thd->killed ) thd->send_kill_message ();
@@ -5002,7 +5018,7 @@ bool create_select_for_variable(Parse_context *pc, const char *var_name) {
5002
5018
@param parser_state Parser state.
5003
5019
*/
5004
5020
5005
- void mysql_parse (THD *thd, Parser_state *parser_state) {
5021
+ void mysql_parse (THD *thd, Parser_state *parser_state, bool update_userstat ) {
5006
5022
DBUG_ENTER (" mysql_parse" );
5007
5023
DBUG_PRINT (" mysql_parse" , (" query: '%s'" , thd->query ().str ));
5008
5024
@@ -5011,31 +5027,11 @@ void mysql_parse(THD *thd, Parser_state *parser_state) {
5011
5027
mysql_reset_thd_for_next_command (thd);
5012
5028
lex_start (thd);
5013
5029
5014
- int start_time_error = 0 ;
5015
- int end_time_error = 0 ;
5016
- struct timeval start_time, end_time;
5017
- double start_usecs = 0 ;
5018
- double end_usecs = 0 ;
5019
- /* cpu time */
5020
- int cputime_error = 0 ;
5021
- #ifdef HAVE_CLOCK_GETTIME
5022
- struct timespec tp;
5023
- #endif
5024
- double start_cpu_nsecs = 0 ;
5025
- double end_cpu_nsecs = 0 ;
5026
-
5027
- if (opt_userstat) {
5028
- #ifdef HAVE_CLOCK_GETTIME
5029
- /* get start cputime */
5030
- if (!(cputime_error = clock_gettime (CLOCK_THREAD_CPUTIME_ID, &tp)))
5031
- start_cpu_nsecs = tp.tv_sec * 1000000000.0 + tp.tv_nsec ;
5032
- #endif
5033
-
5034
- // Gets the start time, in order to measure how long this command takes.
5035
- if (!(start_time_error = gettimeofday (&start_time, nullptr ))) {
5036
- start_usecs = start_time.tv_sec * 1000000.0 + start_time.tv_usec ;
5037
- }
5038
- }
5030
+ /* Declare userstat variables and start timer */
5031
+ double start_busy_usecs = 0.0 ;
5032
+ double start_cpu_nsecs = 0.0 ;
5033
+ if (unlikely (opt_userstat && update_userstat))
5034
+ userstat_start_timer (&start_busy_usecs, &start_cpu_nsecs);
5039
5035
5040
5036
thd->m_parser_state = parser_state;
5041
5037
invoke_pre_parse_rewrite_plugins (thd);
@@ -5190,45 +5186,14 @@ void mysql_parse(THD *thd, Parser_state *parser_state) {
5190
5186
thd->cleanup_after_query ();
5191
5187
DBUG_ASSERT (thd->change_list .is_empty ());
5192
5188
5193
- if (opt_userstat) {
5194
- // Gets the end time.
5195
- if (!(end_time_error = gettimeofday (&end_time, NULL ))) {
5196
- end_usecs = end_time.tv_sec * 1000000.0 + end_time.tv_usec ;
5197
- }
5198
-
5199
- // Calculates the difference between the end and start times.
5200
- if (start_usecs && end_usecs >= start_usecs && !start_time_error &&
5201
- !end_time_error) {
5202
- thd->busy_time = (end_usecs - start_usecs) / 1000000 ;
5203
- // In case there are bad values, 2629743 is the #seconds in a month.
5204
- if (thd->busy_time > 2629743 ) {
5205
- thd->busy_time = 0 ;
5206
- }
5207
- } else {
5208
- // end time went back in time, or gettimeofday() failed.
5209
- thd->busy_time = 0 ;
5210
- }
5211
-
5212
- #ifdef HAVE_CLOCK_GETTIME
5213
- /* get end cputime */
5214
- if (!cputime_error &&
5215
- !(cputime_error = clock_gettime (CLOCK_THREAD_CPUTIME_ID, &tp)))
5216
- end_cpu_nsecs = tp.tv_sec * 1000000000.0 + tp.tv_nsec ;
5217
- #endif
5218
- if (start_cpu_nsecs && !cputime_error) {
5219
- thd->cpu_time = (end_cpu_nsecs - start_cpu_nsecs) / 1000000000 ;
5220
- // In case there are bad values, 2629743 is the #seconds in a month.
5221
- if (thd->cpu_time > 2629743 ) {
5222
- thd->cpu_time = 0 ;
5223
- }
5224
- } else
5225
- thd->cpu_time = 0 ;
5226
- }
5227
-
5228
- // Updates THD stats and the global user stats.
5229
- if (unlikely (opt_userstat)) {
5189
+ /* Update user statistics only if at least one timer was initialized */
5190
+ if (unlikely (update_userstat &&
5191
+ (start_busy_usecs > 0.0 || start_cpu_nsecs > 0.0 ))) {
5192
+ userstat_finish_timer (start_busy_usecs, start_cpu_nsecs, &thd->busy_time ,
5193
+ &thd->cpu_time );
5194
+ /* Updates THD stats and the global user stats. */
5230
5195
thd->update_stats (true );
5231
- update_global_user_stats (thd, true , time (NULL ));
5196
+ update_global_user_stats (thd, true , time (nullptr ));
5232
5197
}
5233
5198
5234
5199
DBUG_VOID_RETURN;
0 commit comments