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 #include "mysys_priv.h"
33 #include "my_static.h"
34
35 #if HAVE_SYS_TIME_H
36 #include <sys/time.h>
37 #endif
38
39 #if defined(_WIN32)
40 #include "my_sys.h" /* for my_printf_error */
41 typedef VOID(WINAPI *time_fn)(LPFILETIME);
42 static time_fn my_get_system_time_as_file_time= GetSystemTimeAsFileTime;
43
44 /**
45 Initialise highest available time resolution API on Windows
46 @return Initialization result
47 @retval FALSE Success
48 @retval TRUE Error. Couldn't initialize environment
49 */
win_init_get_system_time_as_file_time()50 my_bool win_init_get_system_time_as_file_time()
51 {
52 DWORD error;
53 HMODULE h;
54 h= LoadLibrary("kernel32.dll");
55 if (h != NULL)
56 {
57 time_fn pfn= (time_fn) GetProcAddress(h, "GetSystemTimePreciseAsFileTime");
58 if (pfn)
59 my_get_system_time_as_file_time= pfn;
60
61 return FALSE;
62 }
63
64 error= GetLastError();
65 my_printf_error(0,
66 "LoadLibrary(\"kernel32.dll\") failed: GetLastError returns %lu",
67 MYF(0),
68 error);
69
70 return TRUE;
71 }
72 #endif
73
74
75 /**
76 Get high-resolution time.
77
78 @remark For windows platforms we need the frequency value of
79 the CPU. This is initialized in my_init.c through
80 QueryPerformanceFrequency(). If the Windows platform
81 doesn't support QueryPerformanceFrequency(), zero is
82 returned.
83
84 @retval current high-resolution time.
85 */
86
my_getsystime()87 ulonglong my_getsystime()
88 {
89 #ifdef HAVE_CLOCK_GETTIME
90 struct timespec tp;
91 clock_gettime(CLOCK_REALTIME, &tp);
92 return (ulonglong)tp.tv_sec*10000000+(ulonglong)tp.tv_nsec/100;
93 #elif defined(_WIN32)
94 LARGE_INTEGER t_cnt;
95 if (query_performance_frequency)
96 {
97 QueryPerformanceCounter(&t_cnt);
98 return ((t_cnt.QuadPart / query_performance_frequency * 10000000) +
99 ((t_cnt.QuadPart % query_performance_frequency) * 10000000 /
100 query_performance_frequency) + query_performance_offset);
101 }
102 return 0;
103 #else
104 /* TODO: check for other possibilities for hi-res timestamping */
105 struct timeval tv;
106 gettimeofday(&tv,NULL);
107 return (ulonglong)tv.tv_sec*10000000+(ulonglong)tv.tv_usec*10;
108 #endif
109 }
110
111
112 /**
113 Return current time.
114
115 @param flags If MY_WME is set, write error if time call fails.
116
117 @retval current time.
118 */
119
my_time(myf flags)120 time_t my_time(myf flags)
121 {
122 time_t t;
123 /*
124 The following loop is here beacuse time() may fail on some systems.
125 We're using a hardcoded my_message_stderr() here rather than going
126 through the hook in my_message_local() because it's far too easy to
127 come full circle with any logging function that writes timestamps ...
128 */
129 while ((t= time(0)) == (time_t) -1)
130 {
131 if (flags & MY_WME)
132 my_message_stderr(0, "time() call failed", MYF(0));
133 }
134 return t;
135 }
136
137
138 #define OFFSET_TO_EPOCH 116444736000000000ULL
139
140 /**
141 Return time in microseconds.
142
143 @remark This function is to be used to measure performance in
144 micro seconds.
145
146 @retval Number of microseconds since the Epoch, 1970-01-01 00:00:00 +0000 (UTC)
147 */
148
my_micro_time()149 ulonglong my_micro_time()
150 {
151 #ifdef _WIN32
152 ulonglong newtime;
153 my_get_system_time_as_file_time((FILETIME*)&newtime);
154 newtime-= OFFSET_TO_EPOCH;
155 return (newtime/10);
156 #else
157 ulonglong newtime;
158 struct timeval t;
159 /*
160 The following loop is here because gettimeofday may fail on some systems
161 */
162 while (gettimeofday(&t, NULL) != 0)
163 {}
164 newtime= (ulonglong)t.tv_sec * 1000000 + t.tv_usec;
165 return newtime;
166 #endif
167 }
168
169