mySQL 5.0.11 sources for tomato
[tomato.git] / release / src / router / mysql / mysys / my_getsystime.c
blob9568d639231776537357907587532cc8e5a80cac
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 */
20 /*
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 #ifdef __NETWARE__
29 #include <nks/time.h>
30 #endif
32 ulonglong my_getsystime()
34 #ifdef HAVE_CLOCK_GETTIME
35 struct timespec tp;
36 clock_gettime(CLOCK_REALTIME, &tp);
37 return (ulonglong)tp.tv_sec*10000000+(ulonglong)tp.tv_nsec/100;
38 #elif defined(__WIN__)
39 LARGE_INTEGER t_cnt;
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);
47 return 0;
48 #elif defined(__NETWARE__)
49 NXTime_t tm;
50 NXGetTime(NX_SINCE_1970, NX_NSECONDS, &tm);
51 return (ulonglong)tm/100;
52 #else
53 /* TODO: check for other possibilities for hi-res timestamping */
54 struct timeval tv;
55 gettimeofday(&tv,NULL);
56 return (ulonglong)tv.tv_sec*10000000+(ulonglong)tv.tv_usec*10;
57 #endif
62 Return current time
64 SYNOPSIS
65 my_time()
66 flags If MY_WME is set, write error if time call fails
70 time_t my_time(myf flags __attribute__((unused)))
72 time_t t;
73 #ifdef HAVE_GETHRTIME
74 (void) my_micro_time_and_time(&t);
75 return t;
76 #else
77 /* The following loop is here beacuse time() may fail on some systems */
78 while ((t= time(0)) == (time_t) -1)
80 if (flags & MY_WME)
81 fprintf(stderr, "%s: Warning: time() call failed\n", my_progname);
83 return t;
84 #endif
89 Return time in micro seconds
91 SYNOPSIS
92 my_micro_time()
94 NOTES
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.
105 RETURN
106 Value in microseconds from some undefined point in time
109 ulonglong my_micro_time()
111 #if defined(__WIN__)
112 ulonglong newtime;
113 GetSystemTimeAsFileTime((FILETIME*)&newtime);
114 return (newtime/10);
115 #elif defined(HAVE_GETHRTIME)
116 return gethrtime()/1000;
117 #else
118 ulonglong newtime;
119 struct timeval t;
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;
126 return newtime;
127 #endif /* defined(__WIN__) */
132 Return time in seconds and timer in microseconds (not different start!)
134 SYNOPSIS
135 my_micro_time_and_time()
136 time_arg Will be set to seconds since epoch (00:00:00 UTC,
137 January 1, 1970)
139 NOTES
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)
144 IMPLEMENTATION
145 Value of time is as in time() call.
146 Value of microtime is same as my_micro_time(), which may be totally
147 unrealated to time()
149 RETURN
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)
157 #if defined(__WIN__)
158 ulonglong newtime;
159 GetSystemTimeAsFileTime((FILETIME*)&newtime);
160 *time_arg= (time_t) ((newtime - OFFSET_TO_EPOCH) / 10000000);
161 return (newtime/10);
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))
181 cur_time= time(0);
182 prev_gethrtime= cur_gethrtime;
184 *time_arg= cur_time;
185 pthread_mutex_unlock(&THR_LOCK_time);
186 return cur_gethrtime/1000;
187 #else
188 ulonglong newtime;
189 struct timeval t;
191 The following loop is here because gettimeofday may fail on some systems
193 while (gettimeofday(&t, NULL) != 0)
195 *time_arg= t.tv_sec;
196 newtime= (ulonglong)t.tv_sec * 1000000 + t.tv_usec;
197 return newtime;
198 #endif /* defined(__WIN__) */
203 Returns current time
205 SYNOPSIS
206 my_time_possible_from_micro()
207 microtime Value from very recent my_micro_time()
209 NOTES
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
212 current time.
214 RETURN
215 current time
218 time_t my_time_possible_from_micro(ulonglong microtime __attribute__((unused)))
220 #if defined(__WIN__)
221 time_t t;
222 while ((t= time(0)) == (time_t) -1)
224 return t;
225 #elif defined(HAVE_GETHRTIME)
226 return my_time(0); /* Cached time */
227 #else
228 return (time_t) (microtime / 1000000);
229 #endif /* defined(__WIN__) */