timeval.c 5.8 KB

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