1
/* Copyright (C) 2004 MySQL AB
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
16
/* get time since epoc in 100 nanosec units */
17
/* thus to get the current time we should use the system function
18
with the highest possible resolution */
21
TODO: in functions my_micro_time() and my_micro_time_and_time() there
22
exists some common code that should be merged into a function.
25
#include "mysys_priv.h"
26
#include "my_static.h"
28
#if TIME_WITH_SYS_TIME
29
# include <sys/time.h>
33
# include <sys/time.h>
39
uint64_t my_getsystime()
41
#ifdef HAVE_CLOCK_GETTIME
43
clock_gettime(CLOCK_REALTIME, &tp);
44
return (uint64_t)tp.tv_sec*10000000+(uint64_t)tp.tv_nsec/100;
46
/* TODO: check for other possibilities for hi-res timestamping */
48
gettimeofday(&tv,NULL);
49
return (uint64_t)tv.tv_sec*10000000+(uint64_t)tv.tv_usec*10;
54
Return time in micro seconds
60
This function is to be used to measure performance in micro seconds.
61
As it's not defined whats the start time for the clock, this function
62
us only useful to measure time between two moments.
64
For windows platforms we need the frequency value of the CUP. This is
65
initalized in my_init.c through QueryPerformanceFrequency().
67
If Windows platform doesn't support QueryPerformanceFrequency() we will
68
obtain the time via GetClockCount, which only supports milliseconds.
71
Value in microseconds from some undefined point in time
74
uint64_t my_micro_time()
76
#if defined(HAVE_GETHRTIME)
77
return gethrtime()/1000;
82
The following loop is here because gettimeofday may fail on some systems
84
while (gettimeofday(&t, NULL) != 0)
86
newtime= (uint64_t)t.tv_sec * 1000000 + t.tv_usec;
88
#endif /* defined(HAVE_GETHRTIME) */
93
Return time in seconds and timer in microseconds (not different start!)
96
my_micro_time_and_time()
97
time_arg Will be set to seconds since epoch (00:00:00 UTC,
101
This function is to be useful when we need both the time and microtime.
102
For example in MySQL this is used to get the query time start of a query
103
and to measure the time of a query (for the slow query log)
106
Value of time is as in time() call.
107
Value of microtime is same as my_micro_time(), which may be totally
111
Value in microseconds from some undefined point in time
115
uint64_t my_micro_time_and_time(time_t *time_arg)
117
#if defined(HAVE_GETHRTIME)
119
Solaris has a very slow time() call. We optimize this by using the very
120
fast gethrtime() call and only calling time() every 1/2 second
123
#define DELTA_FOR_SECONDS 500000000LL /* Half a second */
125
static hrtime_t prev_gethrtime= 0;
126
static time_t cur_time= 0;
127
hrtime_t cur_gethrtime;
129
pthread_mutex_lock(&THR_LOCK_time);
130
cur_gethrtime= gethrtime();
131
if ((cur_gethrtime - prev_gethrtime) > DELTA_FOR_SECONDS)
134
prev_gethrtime= cur_gethrtime;
137
pthread_mutex_unlock(&THR_LOCK_time);
138
return cur_gethrtime/1000;
143
The following loop is here because gettimeofday may fail on some systems
145
while (gettimeofday(&t, NULL) != 0)
148
newtime= (uint64_t)t.tv_sec * 1000000 + t.tv_usec;
150
#endif /* defined(HAVE_GETHRTIME) */
158
my_time_possible_from_micro()
159
microtime Value from very recent my_micro_time()
162
This function returns the current time. The microtime argument is only used
163
if my_micro_time() uses a function that can safely be converted to the
170
time_t my_time_possible_from_micro(uint64_t microtime)
172
#if defined(HAVE_GETHRTIME)
174
return time(0); /* Cached time */
176
return (time_t) (microtime / 1000000);
177
#endif /* defined(HAVE_GETHRTIME) */