timeval.c 5.8 KB

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