1 /* Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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"
32 ulonglong
my_getsystime()
34 #ifdef HAVE_CLOCK_GETTIME
36 clock_gettime(CLOCK_REALTIME
, &tp
);
37 return (ulonglong
)tp
.tv_sec
*10000000+(ulonglong
)tp
.tv_nsec
/100;
38 #elif defined(__WIN__)
40 if (query_performance_frequency
)
42 QueryPerformanceCounter(&t_cnt
);
43 return ((t_cnt
.QuadPart
/ query_performance_frequency
* 10000000) +
44 ((t_cnt
.QuadPart
% query_performance_frequency
) * 10000000 /
45 query_performance_frequency
) + query_performance_offset
);
48 #elif defined(__NETWARE__)
50 NXGetTime(NX_SINCE_1970
, NX_NSECONDS
, &tm
);
51 return (ulonglong
)tm
/100;
53 /* TODO: check for other possibilities for hi-res timestamping */
55 gettimeofday(&tv
,NULL
);
56 return (ulonglong
)tv
.tv_sec
*10000000+(ulonglong
)tv
.tv_usec
*10;
66 flags If MY_WME is set, write error if time call fails
70 time_t my_time(myf flags
__attribute__((unused
)))
74 (void) my_micro_time_and_time(&t
);
77 /* The following loop is here beacuse time() may fail on some systems */
78 while ((t
= time(0)) == (time_t) -1)
81 fprintf(stderr
, "%s: Warning: time() call failed\n", my_progname
);
89 Return time in micro seconds
95 This function is to be used to measure performance in micro seconds.
96 As it's not defined whats the start time for the clock, this function
97 us only useful to measure time between two moments.
99 For windows platforms we need the frequency value of the CUP. This is
100 initalized in my_init.c through QueryPerformanceFrequency().
102 If Windows platform doesn't support QueryPerformanceFrequency() we will
103 obtain the time via GetClockCount, which only supports milliseconds.
106 Value in microseconds from some undefined point in time
109 ulonglong
my_micro_time()
113 GetSystemTimeAsFileTime((FILETIME
*)&newtime
);
115 #elif defined(HAVE_GETHRTIME)
116 return gethrtime()/1000;
121 The following loop is here because gettimeofday may fail on some systems
123 while (gettimeofday(&t
, NULL
) != 0)
125 newtime
= (ulonglong
)t
.tv_sec
* 1000000 + t
.tv_usec
;
127 #endif /* defined(__WIN__) */
132 Return time in seconds and timer in microseconds (not different start!)
135 my_micro_time_and_time()
136 time_arg Will be set to seconds since epoch (00:00:00 UTC,
140 This function is to be useful when we need both the time and microtime.
141 For example in MySQL this is used to get the query time start of a query
142 and to measure the time of a query (for the slow query log)
145 Value of time is as in time() call.
146 Value of microtime is same as my_micro_time(), which may be totally
150 Value in microseconds from some undefined point in time
153 #define DELTA_FOR_SECONDS LL(500000000) /* Half a second */
155 ulonglong
my_micro_time_and_time(time_t *time_arg
)
159 GetSystemTimeAsFileTime((FILETIME
*)&newtime
);
160 *time_arg
= (time_t) ((newtime
- OFFSET_TO_EPOCH
) / 10000000);
162 #elif defined(HAVE_GETHRTIME)
164 Solaris has a very slow time() call. We optimize this by using the very
165 fast gethrtime() call and only calling time() every 1/2 second
167 static hrtime_t prev_gethrtime
= 0;
168 static time_t cur_time
= 0;
169 hrtime_t cur_gethrtime
;
171 pthread_mutex_lock(&THR_LOCK_time
);
172 cur_gethrtime
= gethrtime();
174 Due to bugs in the Solaris (x86) implementation of gethrtime(),
175 the time returned by it might not be monotonic. Don't use the
176 cached time(2) value if this is a case.
178 if ((prev_gethrtime
> cur_gethrtime
) ||
179 ((cur_gethrtime
- prev_gethrtime
) > DELTA_FOR_SECONDS
))
182 prev_gethrtime
= cur_gethrtime
;
185 pthread_mutex_unlock(&THR_LOCK_time
);
186 return cur_gethrtime
/1000;
191 The following loop is here because gettimeofday may fail on some systems
193 while (gettimeofday(&t
, NULL
) != 0)
196 newtime
= (ulonglong
)t
.tv_sec
* 1000000 + t
.tv_usec
;
198 #endif /* defined(__WIN__) */
206 my_time_possible_from_micro()
207 microtime Value from very recent my_micro_time()
210 This function returns the current time. The microtime argument is only used
211 if my_micro_time() uses a function that can safely be converted to the
218 time_t my_time_possible_from_micro(ulonglong microtime
__attribute__((unused
)))
222 while ((t
= time(0)) == (time_t) -1)
225 #elif defined(HAVE_GETHRTIME)
226 return my_time(0); /* Cached time */
228 return (time_t) (microtime
/ 1000000);
229 #endif /* defined(__WIN__) */