1 /* Copyright (c) 2004, 2017, Oracle and/or its affiliates. All rights reserved.
2 
3    This program is free software; you can redistribute it and/or modify
4    it under the terms of the GNU General Public License, version 2.0,
5    as published by the Free Software Foundation.
6 
7    This program is also distributed with certain software (including
8    but not limited to OpenSSL) that is licensed under separate terms,
9    as designated in a particular file or component or in included license
10    documentation.  The authors of MySQL hereby grant you an additional
11    permission to link the program and your derivative works with the
12    separately licensed software that they have included with MySQL.
13 
14    Without limiting anything contained in the foregoing, this file,
15    which is part of C Driver for MySQL (Connector/C), is also subject to the
16    Universal FOSS Exception, version 1.0, a copy of which can be found at
17    http://oss.oracle.com/licenses/universal-foss-exception.
18 
19    This program is distributed in the hope that it will be useful,
20    but WITHOUT ANY WARRANTY; without even the implied warranty of
21    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22    GNU General Public License, version 2.0, for more details.
23 
24    You should have received a copy of the GNU General Public License
25    along with this program; if not, write to the Free Software
26    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */
27 
28 /* get time since epoc in 100 nanosec units */
29 /* thus to get the current time we should use the system function
30    with the highest possible resolution */
31 
32 /*
33    TODO: in functions my_micro_time() and my_micro_time_and_time() there
34    exists some common code that should be merged into a function.
35 */
36 
37 #include "mysys_priv.h"
38 #include "my_static.h"
39 
40 #if defined(_WIN32)
41 #include "my_sys.h" /* for my_printf_error */
42 typedef VOID(WINAPI *time_fn)(LPFILETIME);
43 static time_fn my_get_system_time_as_file_time= GetSystemTimeAsFileTime;
44 
45 /**
46 Initialise highest available time resolution API on Windows
47 @return Initialization result
48 @retval FALSE Success
49 @retval TRUE  Error. Couldn't initialize environment
50 */
win_init_get_system_time_as_file_time()51 my_bool win_init_get_system_time_as_file_time()
52 {
53   DWORD error;
54   HMODULE h;
55   h= LoadLibrary("kernel32.dll");
56   if (h != NULL)
57   {
58     time_fn pfn= (time_fn) GetProcAddress(h, "GetSystemTimePreciseAsFileTime");
59     if (pfn)
60       my_get_system_time_as_file_time= pfn;
61 
62     return FALSE;
63   }
64 
65   error= GetLastError();
66   my_printf_error(0,
67     "LoadLibrary(\"kernel32.dll\") failed: GetLastError returns %lu",
68     MYF(0),
69     error);
70 
71   return TRUE;
72 }
73 #endif
74 
75 
76 /**
77   Get high-resolution time.
78 
79   @remark For windows platforms we need the frequency value of
80           the CPU. This is initialized in my_init.c through
81           QueryPerformanceFrequency(). If the Windows platform
82           doesn't support QueryPerformanceFrequency(), zero is
83           returned.
84 
85   @retval current high-resolution time.
86 */
87 
my_getsystime()88 ulonglong my_getsystime()
89 {
90 #ifdef HAVE_CLOCK_GETTIME
91   struct timespec tp;
92   clock_gettime(CLOCK_REALTIME, &tp);
93   return (ulonglong)tp.tv_sec*10000000+(ulonglong)tp.tv_nsec/100;
94 #elif defined(_WIN32)
95   LARGE_INTEGER t_cnt;
96   if (query_performance_frequency)
97   {
98     QueryPerformanceCounter(&t_cnt);
99     return ((t_cnt.QuadPart / query_performance_frequency * 10000000) +
100             ((t_cnt.QuadPart % query_performance_frequency) * 10000000 /
101              query_performance_frequency) + query_performance_offset);
102   }
103   return 0;
104 #else
105   /* TODO: check for other possibilities for hi-res timestamping */
106   struct timeval tv;
107   gettimeofday(&tv,NULL);
108   return (ulonglong)tv.tv_sec*10000000+(ulonglong)tv.tv_usec*10;
109 #endif
110 }
111 
112 
113 /**
114   Return current time.
115 
116   @param  flags   If MY_WME is set, write error if time call fails.
117 
118   @retval current time.
119 */
120 
my_time(myf flags)121 time_t my_time(myf flags)
122 {
123   time_t t;
124   /* The following loop is here beacuse time() may fail on some systems */
125   while ((t= time(0)) == (time_t) -1)
126   {
127     if (flags & MY_WME)
128       fprintf(stderr, "%s: Warning: time() call failed\n", my_progname);
129   }
130   return t;
131 }
132 
133 
134 #define OFFSET_TO_EPOCH 116444736000000000ULL
135 
136 /**
137   Return time in microseconds.
138 
139   @remark This function is to be used to measure performance in
140   micro seconds.
141 
142   @retval Number of microseconds since the Epoch, 1970-01-01 00:00:00 +0000 (UTC)
143 */
144 
my_micro_time()145 ulonglong my_micro_time()
146 {
147 #ifdef _WIN32
148   ulonglong newtime;
149   my_get_system_time_as_file_time((FILETIME*)&newtime);
150   newtime-= OFFSET_TO_EPOCH;
151   return (newtime/10);
152 #else
153   ulonglong newtime;
154   struct timeval t;
155   /*
156     The following loop is here because gettimeofday may fail on some systems
157   */
158   while (gettimeofday(&t, NULL) != 0)
159   {}
160   newtime= (ulonglong)t.tv_sec * 1000000 + t.tv_usec;
161   return newtime;
162 #endif
163 }
164 
165 
166 /**
167   Return time in seconds and timer in microseconds (not different start!)
168 
169   @param  time_arg  Will be set to seconds since epoch.
170 
171   @remark This function is to be useful when we need both the time and
172           microtime. For example in MySQL this is used to get the query
173           time start of a query and to measure the time of a query (for
174           the slow query log)
175 
176   @remark The time source is the same as for my_micro_time(), meaning
177           that time values returned by both functions can be intermixed
178           in meaningful ways (i.e. for comparison purposes).
179 
180   @retval Value in microseconds from some undefined point in time.
181 */
182 
183 /* Difference between GetSystemTimeAsFileTime() and now() */
184 
my_micro_time_and_time(time_t * time_arg)185 ulonglong my_micro_time_and_time(time_t *time_arg)
186 {
187 #ifdef _WIN32
188   ulonglong newtime;
189   GetSystemTimeAsFileTime((FILETIME*)&newtime);
190   *time_arg= (time_t) ((newtime - OFFSET_TO_EPOCH) / 10000000);
191   return (newtime/10);
192 #else
193   ulonglong newtime;
194   struct timeval t;
195   /*
196     The following loop is here because gettimeofday may fail on some systems
197   */
198   while (gettimeofday(&t, NULL) != 0)
199   {}
200   *time_arg= t.tv_sec;
201   newtime= (ulonglong)t.tv_sec * 1000000 + t.tv_usec;
202   return newtime;
203 #endif
204 }
205 
206 
207 /**
208   Returns current time.
209 
210   @param  microtime Value from very recent my_micro_time().
211 
212   @remark This function returns the current time. The microtime argument
213           is only used if my_micro_time() uses a function that can safely
214           be converted to the current time.
215 
216   @retval current time.
217 */
218 
my_time_possible_from_micro(ulonglong microtime MY_ATTRIBUTE ((unused)))219 time_t my_time_possible_from_micro(ulonglong microtime MY_ATTRIBUTE((unused)))
220 {
221 #ifdef _WIN32
222   time_t t;
223   while ((t= time(0)) == (time_t) -1)
224   {}
225   return t;
226 #else
227   return (time_t) (microtime / 1000000);
228 #endif
229 }
230 
231