timeval.c 7.2 KB

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