1 | /*************************************************************************** |
2 | * _ _ ____ _ |
3 | * Project ___| | | | _ \| | |
4 | * / __| | | | |_) | | |
5 | * | (__| |_| | _ <| |___ |
6 | * \___|\___/|_| \_\_____| |
7 | * |
8 | * Copyright (C) 1998 - 2021, Daniel Stenberg, <daniel@haxx.se>, et al. |
9 | * |
10 | * This software is licensed as described in the file COPYING, which |
11 | * you should have received as part of this distribution. The terms |
12 | * are also available at https://curl.se/docs/copyright.html. |
13 | * |
14 | * You may opt to use, copy, modify, merge, publish, distribute and/or sell |
15 | * copies of the Software, and permit persons to whom the Software is |
16 | * furnished to do so, under the terms of the COPYING file. |
17 | * |
18 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
19 | * KIND, either express or implied. |
20 | * |
21 | ***************************************************************************/ |
22 | |
23 | #include "timeval.h" |
24 | |
25 | #if defined(WIN32) && !defined(MSDOS) |
26 | |
27 | /* set in win32_init() */ |
28 | extern LARGE_INTEGER Curl_freq; |
29 | extern bool Curl_isVistaOrGreater; |
30 | |
31 | /* In case of bug fix this function has a counterpart in tool_util.c */ |
32 | struct curltime Curl_now(void) |
33 | { |
34 | struct curltime now; |
35 | if(Curl_isVistaOrGreater) { /* QPC timer might have issues pre-Vista */ |
36 | LARGE_INTEGER count; |
37 | QueryPerformanceCounter(&count); |
38 | now.tv_sec = (time_t)(count.QuadPart / Curl_freq.QuadPart); |
39 | now.tv_usec = (int)((count.QuadPart % Curl_freq.QuadPart) * 1000000 / |
40 | Curl_freq.QuadPart); |
41 | } |
42 | else { |
43 | /* Disable /analyze warning that GetTickCount64 is preferred */ |
44 | #if defined(_MSC_VER) |
45 | #pragma warning(push) |
46 | #pragma warning(disable:28159) |
47 | #endif |
48 | DWORD milliseconds = GetTickCount(); |
49 | #if defined(_MSC_VER) |
50 | #pragma warning(pop) |
51 | #endif |
52 | |
53 | now.tv_sec = milliseconds / 1000; |
54 | now.tv_usec = (milliseconds % 1000) * 1000; |
55 | } |
56 | return now; |
57 | } |
58 | |
59 | #elif defined(HAVE_CLOCK_GETTIME_MONOTONIC) |
60 | |
61 | struct curltime Curl_now(void) |
62 | { |
63 | /* |
64 | ** clock_gettime() is granted to be increased monotonically when the |
65 | ** monotonic clock is queried. Time starting point is unspecified, it |
66 | ** could be the system start-up time, the Epoch, or something else, |
67 | ** in any case the time starting point does not change once that the |
68 | ** system has started up. |
69 | */ |
70 | #ifdef HAVE_GETTIMEOFDAY |
71 | struct timeval now; |
72 | #endif |
73 | struct curltime cnow; |
74 | struct timespec tsnow; |
75 | |
76 | /* |
77 | ** clock_gettime() may be defined by Apple's SDK as weak symbol thus |
78 | ** code compiles but fails during run-time if clock_gettime() is |
79 | ** called on unsupported OS version. |
80 | */ |
81 | #if defined(__APPLE__) && defined(HAVE_BUILTIN_AVAILABLE) && \ |
82 | (HAVE_BUILTIN_AVAILABLE == 1) |
83 | bool have_clock_gettime = FALSE; |
84 | if(__builtin_available(macOS 10.12, iOS 10, tvOS 10, watchOS 3, *)) |
85 | have_clock_gettime = TRUE; |
86 | #endif |
87 | |
88 | if( |
89 | #if defined(__APPLE__) && defined(HAVE_BUILTIN_AVAILABLE) && \ |
90 | (HAVE_BUILTIN_AVAILABLE == 1) |
91 | have_clock_gettime && |
92 | #endif |
93 | (0 == clock_gettime(CLOCK_MONOTONIC, &tsnow))) { |
94 | cnow.tv_sec = tsnow.tv_sec; |
95 | cnow.tv_usec = (unsigned int)(tsnow.tv_nsec / 1000); |
96 | } |
97 | /* |
98 | ** Even when the configure process has truly detected monotonic clock |
99 | ** availability, it might happen that it is not actually available at |
100 | ** run-time. When this occurs simply fallback to other time source. |
101 | */ |
102 | #ifdef HAVE_GETTIMEOFDAY |
103 | else { |
104 | (void)gettimeofday(&now, NULL); |
105 | cnow.tv_sec = now.tv_sec; |
106 | cnow.tv_usec = (unsigned int)now.tv_usec; |
107 | } |
108 | #else |
109 | else { |
110 | cnow.tv_sec = time(NULL); |
111 | cnow.tv_usec = 0; |
112 | } |
113 | #endif |
114 | return cnow; |
115 | } |
116 | |
117 | #elif defined(HAVE_MACH_ABSOLUTE_TIME) |
118 | |
119 | #include <stdint.h> |
120 | #include <mach/mach_time.h> |
121 | |
122 | struct curltime Curl_now(void) |
123 | { |
124 | /* |
125 | ** Monotonic timer on Mac OS is provided by mach_absolute_time(), which |
126 | ** returns time in Mach "absolute time units," which are platform-dependent. |
127 | ** To convert to nanoseconds, one must use conversion factors specified by |
128 | ** mach_timebase_info(). |
129 | */ |
130 | static mach_timebase_info_data_t timebase; |
131 | struct curltime cnow; |
132 | uint64_t usecs; |
133 | |
134 | if(0 == timebase.denom) |
135 | (void) mach_timebase_info(&timebase); |
136 | |
137 | usecs = mach_absolute_time(); |
138 | usecs *= timebase.numer; |
139 | usecs /= timebase.denom; |
140 | usecs /= 1000; |
141 | |
142 | cnow.tv_sec = usecs / 1000000; |
143 | cnow.tv_usec = (int)(usecs % 1000000); |
144 | |
145 | return cnow; |
146 | } |
147 | |
148 | #elif defined(HAVE_GETTIMEOFDAY) |
149 | |
150 | struct curltime Curl_now(void) |
151 | { |
152 | /* |
153 | ** gettimeofday() is not granted to be increased monotonically, due to |
154 | ** clock drifting and external source time synchronization it can jump |
155 | ** forward or backward in time. |
156 | */ |
157 | struct timeval now; |
158 | struct curltime ret; |
159 | (void)gettimeofday(&now, NULL); |
160 | ret.tv_sec = now.tv_sec; |
161 | ret.tv_usec = (int)now.tv_usec; |
162 | return ret; |
163 | } |
164 | |
165 | #else |
166 | |
167 | struct curltime Curl_now(void) |
168 | { |
169 | /* |
170 | ** time() returns the value of time in seconds since the Epoch. |
171 | */ |
172 | struct curltime now; |
173 | now.tv_sec = time(NULL); |
174 | now.tv_usec = 0; |
175 | return now; |
176 | } |
177 | |
178 | #endif |
179 | |
180 | /* |
181 | * Returns: time difference in number of milliseconds. For too large diffs it |
182 | * returns max value. |
183 | * |
184 | * @unittest: 1323 |
185 | */ |
186 | timediff_t Curl_timediff(struct curltime newer, struct curltime older) |
187 | { |
188 | timediff_t diff = (timediff_t)newer.tv_sec-older.tv_sec; |
189 | if(diff >= (TIMEDIFF_T_MAX/1000)) |
190 | return TIMEDIFF_T_MAX; |
191 | else if(diff <= (TIMEDIFF_T_MIN/1000)) |
192 | return TIMEDIFF_T_MIN; |
193 | return diff * 1000 + (newer.tv_usec-older.tv_usec)/1000; |
194 | } |
195 | |
196 | /* |
197 | * Returns: time difference in number of microseconds. For too large diffs it |
198 | * returns max value. |
199 | */ |
200 | timediff_t Curl_timediff_us(struct curltime newer, struct curltime older) |
201 | { |
202 | timediff_t diff = (timediff_t)newer.tv_sec-older.tv_sec; |
203 | if(diff >= (TIMEDIFF_T_MAX/1000000)) |
204 | return TIMEDIFF_T_MAX; |
205 | else if(diff <= (TIMEDIFF_T_MIN/1000000)) |
206 | return TIMEDIFF_T_MIN; |
207 | return diff * 1000000 + newer.tv_usec-older.tv_usec; |
208 | } |
209 | |