timeval.c 6.2 KB

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