2
0

timeval.c 5.7 KB

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