timeval.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2022, 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. struct curltime Curl_now(void)
  57. {
  58. /*
  59. ** clock_gettime() is granted to be increased monotonically when the
  60. ** monotonic clock is queried. Time starting point is unspecified, it
  61. ** could be the system start-up time, the Epoch, or something else,
  62. ** in any case the time starting point does not change once that the
  63. ** system has started up.
  64. */
  65. #ifdef HAVE_GETTIMEOFDAY
  66. struct timeval now;
  67. #endif
  68. struct curltime cnow;
  69. struct timespec tsnow;
  70. /*
  71. ** clock_gettime() may be defined by Apple's SDK as weak symbol thus
  72. ** code compiles but fails during run-time if clock_gettime() is
  73. ** called on unsupported OS version.
  74. */
  75. #if defined(__APPLE__) && defined(HAVE_BUILTIN_AVAILABLE) && \
  76. (HAVE_BUILTIN_AVAILABLE == 1)
  77. bool have_clock_gettime = FALSE;
  78. if(__builtin_available(macOS 10.12, iOS 10, tvOS 10, watchOS 3, *))
  79. have_clock_gettime = TRUE;
  80. #endif
  81. if(
  82. #if defined(__APPLE__) && defined(HAVE_BUILTIN_AVAILABLE) && \
  83. (HAVE_BUILTIN_AVAILABLE == 1)
  84. have_clock_gettime &&
  85. #endif
  86. (0 == clock_gettime(CLOCK_MONOTONIC, &tsnow))) {
  87. cnow.tv_sec = tsnow.tv_sec;
  88. cnow.tv_usec = (unsigned int)(tsnow.tv_nsec / 1000);
  89. }
  90. /*
  91. ** Even when the configure process has truly detected monotonic clock
  92. ** availability, it might happen that it is not actually available at
  93. ** run-time. When this occurs simply fallback to other time source.
  94. */
  95. #ifdef HAVE_GETTIMEOFDAY
  96. else {
  97. (void)gettimeofday(&now, NULL);
  98. cnow.tv_sec = now.tv_sec;
  99. cnow.tv_usec = (unsigned int)now.tv_usec;
  100. }
  101. #else
  102. else {
  103. cnow.tv_sec = time(NULL);
  104. cnow.tv_usec = 0;
  105. }
  106. #endif
  107. return cnow;
  108. }
  109. #elif defined(HAVE_MACH_ABSOLUTE_TIME)
  110. #include <stdint.h>
  111. #include <mach/mach_time.h>
  112. struct curltime Curl_now(void)
  113. {
  114. /*
  115. ** Monotonic timer on Mac OS is provided by mach_absolute_time(), which
  116. ** returns time in Mach "absolute time units," which are platform-dependent.
  117. ** To convert to nanoseconds, one must use conversion factors specified by
  118. ** mach_timebase_info().
  119. */
  120. static mach_timebase_info_data_t timebase;
  121. struct curltime cnow;
  122. uint64_t usecs;
  123. if(0 == timebase.denom)
  124. (void) mach_timebase_info(&timebase);
  125. usecs = mach_absolute_time();
  126. usecs *= timebase.numer;
  127. usecs /= timebase.denom;
  128. usecs /= 1000;
  129. cnow.tv_sec = usecs / 1000000;
  130. cnow.tv_usec = (int)(usecs % 1000000);
  131. return cnow;
  132. }
  133. #elif defined(HAVE_GETTIMEOFDAY)
  134. struct curltime Curl_now(void)
  135. {
  136. /*
  137. ** gettimeofday() is not granted to be increased monotonically, due to
  138. ** clock drifting and external source time synchronization it can jump
  139. ** forward or backward in time.
  140. */
  141. struct timeval now;
  142. struct curltime ret;
  143. (void)gettimeofday(&now, NULL);
  144. ret.tv_sec = now.tv_sec;
  145. ret.tv_usec = (int)now.tv_usec;
  146. return ret;
  147. }
  148. #else
  149. struct curltime Curl_now(void)
  150. {
  151. /*
  152. ** time() returns the value of time in seconds since the Epoch.
  153. */
  154. struct curltime now;
  155. now.tv_sec = time(NULL);
  156. now.tv_usec = 0;
  157. return now;
  158. }
  159. #endif
  160. /*
  161. * Returns: time difference in number of milliseconds. For too large diffs it
  162. * returns max value.
  163. *
  164. * @unittest: 1323
  165. */
  166. timediff_t Curl_timediff(struct curltime newer, struct curltime older)
  167. {
  168. timediff_t diff = (timediff_t)newer.tv_sec-older.tv_sec;
  169. if(diff >= (TIMEDIFF_T_MAX/1000))
  170. return TIMEDIFF_T_MAX;
  171. else if(diff <= (TIMEDIFF_T_MIN/1000))
  172. return TIMEDIFF_T_MIN;
  173. return diff * 1000 + (newer.tv_usec-older.tv_usec)/1000;
  174. }
  175. /*
  176. * Returns: time difference in number of microseconds. For too large diffs it
  177. * returns max value.
  178. */
  179. timediff_t Curl_timediff_us(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/1000000))
  183. return TIMEDIFF_T_MAX;
  184. else if(diff <= (TIMEDIFF_T_MIN/1000000))
  185. return TIMEDIFF_T_MIN;
  186. return diff * 1000000 + newer.tv_usec-older.tv_usec;
  187. }