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
ulonglong my_getsystime()
30
#ifdef HAVE_CLOCK_GETTIME
32
clock_gettime(CLOCK_REALTIME, &tp);
33
return (ulonglong)tp.tv_sec*10000000+(ulonglong)tp.tv_nsec/100;
35
/* TODO: check for other possibilities for hi-res timestamping */
37
gettimeofday(&tv,NULL);
38
return (ulonglong)tv.tv_sec*10000000+(ulonglong)tv.tv_usec*10;
48
flags If MY_WME is set, write error if time call fails
52
time_t my_time(myf flags __attribute__((unused)))
56
(void) my_micro_time_and_time(&t);
59
/* The following loop is here beacuse time() may fail on some systems */
60
while ((t= time(0)) == (time_t) -1)
63
fprintf(stderr, "%s: Warning: time() call failed\n", my_progname);
71
Return time in micro seconds
77
This function is to be used to measure performance in micro seconds.
78
As it's not defined whats the start time for the clock, this function
79
us only useful to measure time between two moments.
81
For windows platforms we need the frequency value of the CUP. This is
82
initalized in my_init.c through QueryPerformanceFrequency().
84
If Windows platform doesn't support QueryPerformanceFrequency() we will
85
obtain the time via GetClockCount, which only supports milliseconds.
88
Value in microseconds from some undefined point in time
91
ulonglong my_micro_time()
93
#if defined(HAVE_GETHRTIME)
94
return gethrtime()/1000;
99
The following loop is here because gettimeofday may fail on some systems
101
while (gettimeofday(&t, NULL) != 0)
103
newtime= (ulonglong)t.tv_sec * 1000000 + t.tv_usec;
105
#endif /* defined(HAVE_GETHRTIME) */
110
Return time in seconds and timer in microseconds (not different start!)
113
my_micro_time_and_time()
114
time_arg Will be set to seconds since epoch (00:00:00 UTC,
118
This function is to be useful when we need both the time and microtime.
119
For example in MySQL this is used to get the query time start of a query
120
and to measure the time of a query (for the slow query log)
123
Value of time is as in time() call.
124
Value of microtime is same as my_micro_time(), which may be totally
128
Value in microseconds from some undefined point in time
131
#define DELTA_FOR_SECONDS LL(500000000) /* Half a second */
133
ulonglong my_micro_time_and_time(time_t *time_arg)
135
#if defined(HAVE_GETHRTIME)
137
Solaris has a very slow time() call. We optimize this by using the very
138
fast gethrtime() call and only calling time() every 1/2 second
140
static hrtime_t prev_gethrtime= 0;
141
static time_t cur_time= 0;
142
hrtime_t cur_gethrtime;
144
pthread_mutex_lock(&THR_LOCK_time);
145
cur_gethrtime= gethrtime();
146
if ((cur_gethrtime - prev_gethrtime) > DELTA_FOR_SECONDS)
149
prev_gethrtime= cur_gethrtime;
152
pthread_mutex_unlock(&THR_LOCK_time);
153
return cur_gethrtime/1000;
158
The following loop is here because gettimeofday may fail on some systems
160
while (gettimeofday(&t, NULL) != 0)
163
newtime= (ulonglong)t.tv_sec * 1000000 + t.tv_usec;
165
#endif /* defined(HAVE_GETHRTIME) */
173
my_time_possible_from_micro()
174
microtime Value from very recent my_micro_time()
177
This function returns the current time. The microtime argument is only used
178
if my_micro_time() uses a function that can safely be converted to the
185
time_t my_time_possible_from_micro(ulonglong microtime __attribute__((unused)))
187
#if defined(HAVE_GETHRTIME)
188
return my_time(0); /* Cached time */
190
return (time_t) (microtime / 1000000);
191
#endif /* defined(HAVE_GETHRTIME) */