mariadb/mysys/my_getsystime.c
Michael Widenius ab0905c6d7 This is based on the userstatv2 patch from Percona and OurDelta.
The original code comes, as far as I know, from Google (Mark Callaghan's team) with additional work from Percona, Ourdelta and Weldon Whipple.

This code provides the same functionallity, but with a lot of changes to make it faster and better fit the MariaDB infrastucture.

Added new status variables:
- Com_show_client_statistics, Com_show_index_statistics, Com_show_table_statistics, Com_show_user_statistics
- Access_denied_errors, Busy_time (clock time), Binlog_bytes_written, Cpu_time, Empty_queries, Rows_sent, Rows_read

Added new variable / startup option 'userstat' to control if user statistics should be enabled or not

Added my_getcputime(); Returns cpu time used by this thread.
New FLUSH commands:
- FLUSH SLOW QUERY LOG
- FLUSH TABLE_STATISTICS
- FLUSH INDEX_STATISTICS
- FLUSH USER_STATISTICS
- FLUSH CLIENT_STATISTICS

New SHOW commands:
- SHOW CLIENT_STATISTICS
- SHOW USER_STATISTICS
- SHOW TABLE_STATISTICS
- SHOW INDEX_STATISTICS

New Information schemas:
- CLIENT_STATISTICS
- USER_STATISTICS
- INDEX_STATISTICS
- TABLE_STATISTICS

Added support for all new flush commands to mysqladmin

Added handler::ha_... wrappers for all handler read calls to do statistics counting
- Changed all code to use new ha_... calls
- Count number of read rows, changed rows and rows read trough an index

Added counting of number of bytes sent to binary log (status variable Binlog_bytes_written)
Added counting of access denied errors (status variable Access_denied_erors)

Bugs fixed:
- Fixed bug in add_to_status() and add_diff_to_status() where longlong variables where threated as long
- CLOCK_GETTIME was not propely working on Linuxm

client/mysqladmin.cc:
  Added support for all new flush commmands and some common combinations:
  
  flush-slow-log
  flush-table-statistics
  flush-index-statistics
  flush-user-statistics
  flush-client-statistics
  flush-all-status
  flush-all-statistics
configure.in:
  Added checking if clock_gettime needs the librt.
  (Fixes Bug #37639 clock_gettime is never used/enabled in Linux/Unix)
include/my_sys.h:
  Added my_getcputime()
include/mysql_com.h:
  Added LIST_PROCESS_HOST_LEN & new REFRESH target defines
mysql-test/r/information_schema.result:
  New information schema tables added
mysql-test/r/information_schema_all_engines.result:
  New information schema tables added
mysql-test/r/information_schema_db.result:
  New information schema tables added
mysql-test/r/log_slow.result:
  Added testing that flosh slow query logs is accepted
mysql-test/r/status_user.result:
  Basic testing of user, client, table and index statistics
mysql-test/t/log_slow.test:
  Added testing that flosh slow query logs is accepted
mysql-test/t/status_user-master.opt:
  Ensure that we get a fresh restart before running status_user.test
mysql-test/t/status_user.test:
  Basic testing of user, client, table and index statistics
mysys/my_getsystime.c:
  Added my_getcputime()
  Returns cpu time used by this thread.
sql/authors.h:
  Updated authors to have core and original MySQL developers first.
sql/event_data_objects.cc:
  Updated call to mysql_reset_thd_for_next_command()
sql/event_db_repository.cc:
  Changed to use new ha_... calls
sql/filesort.cc:
  Changed to use new ha_... calls
sql/ha_partition.cc:
  Changed to use new ha_... calls
  Fixed comment syntax
sql/handler.cc:
  Changed to use new ha_... calls
  Reset table statistics
  Added code to update global table and index status
  Added counting of rows changed
sql/handler.h:
  Added table and index statistics variables
  Added function reset_statistics()
  Added handler::ha_... wrappers for all handler read calls to do statistics counting
  Protected all normal read calls to ensure we use the new calls in the server.
  Made ha_partition a friend class so that partition code can call the old read functions
sql/item_subselect.cc:
  Changed to use new ha_... calls
sql/lex.h:
  Added keywords for new information schema tables and flush commands
sql/log.cc:
  Added flush_slow_log()
  Added counting of number of bytes sent to binary log
  Removed not needed test of thd (It's used before, so it's safe to use)
  Added THD object to MYSQL_BIN_LOG::write_cache() to simplify statistics counting
sql/log.h:
  Added new parameter to write_cache()
  Added flush_slow_log() functions.
sql/log_event.cc:
  Updated call to mysql_reset_thd_for_next_command()
  Changed to use new ha_... calls
sql/log_event_old.cc:
  Updated call to mysql_reset_thd_for_next_command()
  Changed to use new ha_... calls
sql/mysql_priv.h:
  Updated call to mysql_reset_thd_for_next_command()
  Added new statistics functions and variables needed by these.
sql/mysqld.cc:
  Added new statistics variables and structures to handle these
  Added new status variables:
  - Com_show_client_statistics, Com_show_index_statistics, Com_show_table_statistics, Com_show_user_statistics
  - Access_denied_errors, Busy_time (clock time), Binlog_bytes_written, Cpu_time, Empty_queries, Rows_set, Rows_read
  Added new option 'userstat' to control if user statistics should be enabled or not
sql/opt_range.cc:
  Changed to use new ha_... calls
sql/opt_range.h:
  Changed to use new ha_... calls
sql/opt_sum.cc:
  Changed to use new ha_... calls
sql/records.cc:
  Changed to use new ha_... calls
sql/set_var.cc:
  Added variable 'userstat'
sql/sp.cc:
  Changed to use new ha_... calls
sql/sql_acl.cc:
  Changed to use new ha_... calls
  Added counting of access_denied_errors
sql/sql_base.cc:
  Added call to statistics functions
sql/sql_class.cc:
  Added usage of org_status_var, to store status variables at start of command
  Added functions THD::update_stats(), THD::update_all_stats()
  Fixed bug in add_to_status() and add_diff_to_status() where longlong variables where threated as long
sql/sql_class.h:
  Added new status variables to status_var
  Moved variables that was not ulong in status_var last.
  Added variables to THD for storing temporary values during statistics counting
sql/sql_connect.cc:
  Variables and functions to calculate user and client statistics
  Added counting of access_denied_errors and lost_connections
sql/sql_cursor.cc:
  Changed to use new ha_... calls
sql/sql_handler.cc:
  Changed to use new ha_... calls
sql/sql_help.cc:
  Changed to use new ha_... calls
sql/sql_insert.cc:
  Changed to use new ha_... calls
sql/sql_lex.h:
  Added SQLCOM_SHOW_USER_STATS, SQLCOM_SHOW_TABLE_STATS, SQLCOM_SHOW_INDEX_STATS, SQLCOM_SHOW_CLIENT_STATS
sql/sql_parse.cc:
  Added handling of:
  - SHOW CLIENT_STATISTICS
  - SHOW USER_STATISTICS
  - SHOW TABLE_STATISTICS
  - SHOW INDEX_STATISTICS
  Added handling of new FLUSH commands:
  - FLUSH SLOW QUERY LOGS
  - FLUSH TABLE_STATISTICS
  - FLUSH INDEX_STATISTICS
  - FLUSH USER_STATISTICS
  - FLUSH CLIENT_STATISTICS
  Added THD parameter to mysql_reset_thd_for_next_command()
  Added initialization and calls to user statistics functions
  Added increment of statistics variables empty_queries, rows_sent and access_denied_errors.
  Added counting of cpu time per query
sql/sql_plugin.cc:
  Changed to use new ha_... calls
sql/sql_prepare.cc:
  Updated call to mysql_reset_thd_for_next_command()
sql/sql_select.cc:
  Changed to use new ha_... calls
  Indentation changes
sql/sql_servers.cc:
  Changed to use new ha_... calls
sql/sql_show.cc:
  Added counting of access denied errors
  Added function for new information schema tables:
  - CLIENT_STATISTICS
  - USER_STATISTICS
  - INDEX_STATISTICS
  - TABLE_STATISTICS
  Changed to use new ha_... calls
sql/sql_table.cc:
  Changed to use new ha_... calls
sql/sql_udf.cc:
  Changed to use new ha_... calls
sql/sql_update.cc:
  Changed to use new ha_... calls
sql/sql_yacc.yy:
  Add new show and flush commands
sql/structs.h:
  Add name_length to KEY to avoid some strlen
  Added cache_name to KEY for fast storage of keyvalue in cache
  Added structs USER_STATS, TABLE_STATS, INDEX_STATS
  Added function prototypes for statistics functions
sql/table.cc:
  Store db+table+index name into keyinfo->cache_name
sql/table.h:
  Added new information schema tables
sql/tztime.cc:
  Changed to use new ha_... calls
2009-10-19 20:14:48 +03:00

250 lines
6.5 KiB
C

/* Copyright (C) 2004 MySQL AB
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
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
/* get time since epoc in 100 nanosec units */
/* thus to get the current time we should use the system function
with the highest possible resolution */
/*
TODO: in functions my_micro_time() and my_micro_time_and_time() there
exists some common code that should be merged into a function.
*/
#include "mysys_priv.h"
#include "my_static.h"
#ifdef __NETWARE__
#include <nks/time.h>
#endif
#ifdef HAVE_LINUX_UNISTD_H
#include <linux/unistd.h>
#endif
ulonglong my_getsystime()
{
#ifdef HAVE_CLOCK_GETTIME
struct timespec tp;
clock_gettime(CLOCK_REALTIME, &tp);
return (ulonglong)tp.tv_sec*10000000+(ulonglong)tp.tv_nsec/100;
#elif defined(__WIN__)
LARGE_INTEGER t_cnt;
if (query_performance_frequency)
{
QueryPerformanceCounter(&t_cnt);
return ((t_cnt.QuadPart / query_performance_frequency * 10000000) +
((t_cnt.QuadPart % query_performance_frequency) * 10000000 /
query_performance_frequency) + query_performance_offset);
}
return 0;
#elif defined(__NETWARE__)
NXTime_t tm;
NXGetTime(NX_SINCE_1970, NX_NSECONDS, &tm);
return (ulonglong)tm/100;
#else
/* TODO: check for other possibilities for hi-res timestamping */
struct timeval tv;
gettimeofday(&tv,NULL);
return (ulonglong)tv.tv_sec*10000000+(ulonglong)tv.tv_usec*10;
#endif
}
/*
Return current time
SYNOPSIS
my_time()
flags If MY_WME is set, write error if time call fails
*/
time_t my_time(myf flags __attribute__((unused)))
{
time_t t;
#ifdef HAVE_GETHRTIME
(void) my_micro_time_and_time(&t);
return t;
#else
/* The following loop is here beacuse time() may fail on some systems */
while ((t= time(0)) == (time_t) -1)
{
if (flags & MY_WME)
fprintf(stderr, "%s: Warning: time() call failed\n", my_progname);
}
return t;
#endif
}
/*
Return time in micro seconds
SYNOPSIS
my_micro_time()
NOTES
This function is to be used to measure performance in micro seconds.
As it's not defined whats the start time for the clock, this function
us only useful to measure time between two moments.
For windows platforms we need the frequency value of the CUP. This is
initalized in my_init.c through QueryPerformanceFrequency().
If Windows platform doesn't support QueryPerformanceFrequency() we will
obtain the time via GetClockCount, which only supports milliseconds.
RETURN
Value in microseconds from some undefined point in time
*/
ulonglong my_micro_time()
{
#if defined(__WIN__)
ulonglong newtime;
GetSystemTimeAsFileTime((FILETIME*)&newtime);
return (newtime/10);
#elif defined(HAVE_GETHRTIME)
return gethrtime()/1000;
#else
ulonglong newtime;
struct timeval t;
/*
The following loop is here because gettimeofday may fail on some systems
*/
while (gettimeofday(&t, NULL) != 0)
{}
newtime= (ulonglong)t.tv_sec * 1000000 + t.tv_usec;
return newtime;
#endif /* defined(__WIN__) */
}
/*
Return time in seconds and timer in microseconds (not different start!)
SYNOPSIS
my_micro_time_and_time()
time_arg Will be set to seconds since epoch (00:00:00 UTC,
January 1, 1970)
NOTES
This function is to be useful when we need both the time and microtime.
For example in MySQL this is used to get the query time start of a query
and to measure the time of a query (for the slow query log)
IMPLEMENTATION
Value of time is as in time() call.
Value of microtime is same as my_micro_time(), which may be totally
unrealated to time()
RETURN
Value in microseconds from some undefined point in time
*/
#define DELTA_FOR_SECONDS LL(500000000) /* Half a second */
ulonglong my_micro_time_and_time(time_t *time_arg)
{
#if defined(__WIN__)
ulonglong newtime;
GetSystemTimeAsFileTime((FILETIME*)&newtime);
*time_arg= (time_t) ((newtime - OFFSET_TO_EPOCH) / 10000000);
return (newtime/10);
#elif defined(HAVE_GETHRTIME)
/*
Solaris has a very slow time() call. We optimize this by using the very
fast gethrtime() call and only calling time() every 1/2 second
*/
static hrtime_t prev_gethrtime= 0;
static time_t cur_time= 0;
hrtime_t cur_gethrtime;
pthread_mutex_lock(&THR_LOCK_time);
cur_gethrtime= gethrtime();
if ((cur_gethrtime - prev_gethrtime) > DELTA_FOR_SECONDS)
{
cur_time= time(0);
prev_gethrtime= cur_gethrtime;
}
*time_arg= cur_time;
pthread_mutex_unlock(&THR_LOCK_time);
return cur_gethrtime/1000;
#else
ulonglong newtime;
struct timeval t;
/*
The following loop is here because gettimeofday may fail on some systems
*/
while (gettimeofday(&t, NULL) != 0)
{}
*time_arg= t.tv_sec;
newtime= (ulonglong)t.tv_sec * 1000000 + t.tv_usec;
return newtime;
#endif /* defined(__WIN__) */
}
/*
Returns current time
SYNOPSIS
my_time_possible_from_micro()
microtime Value from very recent my_micro_time()
NOTES
This function returns the current time. The microtime argument is only used
if my_micro_time() uses a function that can safely be converted to the
current time.
RETURN
current time
*/
time_t my_time_possible_from_micro(ulonglong microtime __attribute__((unused)))
{
#if defined(__WIN__)
time_t t;
while ((t= time(0)) == (time_t) -1)
{}
return t;
#elif defined(HAVE_GETHRTIME)
return my_time(0); /* Cached time */
#else
return (time_t) (microtime / 1000000);
#endif /* defined(__WIN__) */
}
/*
Return cpu time in milliseconds * 10
*/
ulonglong my_getcputime()
{
#ifdef HAVE_CLOCK_GETTIME
struct timespec tp;
if (clock_gettime(CLOCK_THREAD_CPUTIME_ID, &tp))
return 0;
return (ulonglong)tp.tv_sec*10000000+(ulonglong)tp.tv_nsec/100;
#elif defined(__NR_clock_gettime)
struct timespec tp;
if (syscall(__NR_clock_gettime, CLOCK_THREAD_CPUTIME_ID, &tp))
return 0;
return (ulonglong)tp.tv_sec*10000000+(ulonglong)tp.tv_nsec/100;
#else
return 0;
#endif /* HAVE_CLOCK_GETTIME */
}