1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2019, 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.haxx.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 
Curl_now(void)31 struct curltime Curl_now(void)
32 {
33   struct curltime now;
34   if(Curl_isVistaOrGreater) { /* QPC timer might have issues pre-Vista */
35     LARGE_INTEGER count;
36     QueryPerformanceCounter(&count);
37     now.tv_sec = (time_t)(count.QuadPart / Curl_freq.QuadPart);
38     now.tv_usec = (int)((count.QuadPart % Curl_freq.QuadPart) * 1000000 /
39                         Curl_freq.QuadPart);
40   }
41   else {
42     /* Disable /analyze warning that GetTickCount64 is preferred  */
43 #if defined(_MSC_VER)
44 #pragma warning(push)
45 #pragma warning(disable:28159)
46 #endif
47     DWORD milliseconds = GetTickCount();
48 #if defined(_MSC_VER)
49 #pragma warning(pop)
50 #endif
51 
52     now.tv_sec = milliseconds / 1000;
53     now.tv_usec = (milliseconds % 1000) * 1000;
54   }
55   return now;
56 }
57 
58 #elif defined(HAVE_CLOCK_GETTIME_MONOTONIC)
59 
Curl_now(void)60 struct curltime Curl_now(void)
61 {
62   /*
63   ** clock_gettime() is granted to be increased monotonically when the
64   ** monotonic clock is queried. Time starting point is unspecified, it
65   ** could be the system start-up time, the Epoch, or something else,
66   ** in any case the time starting point does not change once that the
67   ** system has started up.
68   */
69   struct timeval now;
70   struct curltime cnow;
71   struct timespec tsnow;
72 
73   /*
74   ** clock_gettime() may be defined by Apple's SDK as weak symbol thus
75   ** code compiles but fails during run-time if clock_gettime() is
76   ** called on unsupported OS version.
77   */
78 #if defined(__APPLE__) && (HAVE_BUILTIN_AVAILABLE == 1)
79   bool have_clock_gettime = FALSE;
80   if(__builtin_available(macOS 10.12, iOS 10, tvOS 10, watchOS 3, *))
81     have_clock_gettime = TRUE;
82 #endif
83 
84   if(
85 #if defined(__APPLE__) && (HAVE_BUILTIN_AVAILABLE == 1)
86     have_clock_gettime &&
87 #endif
88     (0 == clock_gettime(CLOCK_MONOTONIC, &tsnow))) {
89     cnow.tv_sec = tsnow.tv_sec;
90     cnow.tv_usec = (unsigned int)(tsnow.tv_nsec / 1000);
91   }
92   /*
93   ** Even when the configure process has truly detected monotonic clock
94   ** availability, it might happen that it is not actually available at
95   ** run-time. When this occurs simply fallback to other time source.
96   */
97 #ifdef HAVE_GETTIMEOFDAY
98   else {
99     (void)gettimeofday(&now, NULL);
100     cnow.tv_sec = now.tv_sec;
101     cnow.tv_usec = (unsigned int)now.tv_usec;
102   }
103 #else
104   else {
105     cnow.tv_sec = time(NULL);
106     cnow.tv_usec = 0;
107   }
108 #endif
109   return cnow;
110 }
111 
112 #elif defined(HAVE_MACH_ABSOLUTE_TIME)
113 
114 #include <stdint.h>
115 #include <mach/mach_time.h>
116 
Curl_now(void)117 struct curltime Curl_now(void)
118 {
119   /*
120   ** Monotonic timer on Mac OS is provided by mach_absolute_time(), which
121   ** returns time in Mach "absolute time units," which are platform-dependent.
122   ** To convert to nanoseconds, one must use conversion factors specified by
123   ** mach_timebase_info().
124   */
125   static mach_timebase_info_data_t timebase;
126   struct curltime cnow;
127   uint64_t usecs;
128 
129   if(0 == timebase.denom)
130     (void) mach_timebase_info(&timebase);
131 
132   usecs = mach_absolute_time();
133   usecs *= timebase.numer;
134   usecs /= timebase.denom;
135   usecs /= 1000;
136 
137   cnow.tv_sec = usecs / 1000000;
138   cnow.tv_usec = (int)(usecs % 1000000);
139 
140   return cnow;
141 }
142 
143 #elif defined(HAVE_GETTIMEOFDAY)
144 
Curl_now(void)145 struct curltime Curl_now(void)
146 {
147   /*
148   ** gettimeofday() is not granted to be increased monotonically, due to
149   ** clock drifting and external source time synchronization it can jump
150   ** forward or backward in time.
151   */
152   struct timeval now;
153   struct curltime ret;
154   (void)gettimeofday(&now, NULL);
155   ret.tv_sec = now.tv_sec;
156   ret.tv_usec = (int)now.tv_usec;
157   return ret;
158 }
159 
160 #else
161 
Curl_now(void)162 struct curltime Curl_now(void)
163 {
164   /*
165   ** time() returns the value of time in seconds since the Epoch.
166   */
167   struct curltime now;
168   now.tv_sec = time(NULL);
169   now.tv_usec = 0;
170   return now;
171 }
172 
173 #endif
174 
175 #if SIZEOF_TIME_T < 8
176 #define TIME_MAX INT_MAX
177 #define TIME_MIN INT_MIN
178 #else
179 #define TIME_MAX 9223372036854775807LL
180 #define TIME_MIN -9223372036854775807LL
181 #endif
182 
183 /*
184  * Returns: time difference in number of milliseconds. For too large diffs it
185  * returns max value.
186  *
187  * @unittest: 1323
188  */
Curl_timediff(struct curltime newer,struct curltime older)189 timediff_t Curl_timediff(struct curltime newer, struct curltime older)
190 {
191   timediff_t diff = (timediff_t)newer.tv_sec-older.tv_sec;
192   if(diff >= (TIME_MAX/1000))
193     return TIME_MAX;
194   else if(diff <= (TIME_MIN/1000))
195     return TIME_MIN;
196   return diff * 1000 + (newer.tv_usec-older.tv_usec)/1000;
197 }
198 
199 /*
200  * Returns: time difference in number of microseconds. For too large diffs it
201  * returns max value.
202  */
Curl_timediff_us(struct curltime newer,struct curltime older)203 timediff_t Curl_timediff_us(struct curltime newer, struct curltime older)
204 {
205   timediff_t diff = (timediff_t)newer.tv_sec-older.tv_sec;
206   if(diff >= (TIME_MAX/1000000))
207     return TIME_MAX;
208   else if(diff <= (TIME_MIN/1000000))
209     return TIME_MIN;
210   return diff * 1000000 + newer.tv_usec-older.tv_usec;
211 }
212