timeval.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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)
  26. #include <curl/curl.h>
  27. #include "system_win32.h"
  28. /* In case of bug fix this function has a counterpart in tool_util.c */
  29. struct curltime Curl_now(void)
  30. {
  31. struct curltime now;
  32. if(Curl_isVistaOrGreater) { /* QPC timer might have issues pre-Vista */
  33. LARGE_INTEGER count;
  34. QueryPerformanceCounter(&count);
  35. now.tv_sec = (time_t)(count.QuadPart / Curl_freq.QuadPart);
  36. now.tv_usec = (int)((count.QuadPart % Curl_freq.QuadPart) * 1000000 /
  37. Curl_freq.QuadPart);
  38. }
  39. else {
  40. /* Disable /analyze warning that GetTickCount64 is preferred */
  41. #if defined(_MSC_VER)
  42. #pragma warning(push)
  43. #pragma warning(disable:28159)
  44. #endif
  45. DWORD milliseconds = GetTickCount();
  46. #if defined(_MSC_VER)
  47. #pragma warning(pop)
  48. #endif
  49. now.tv_sec = milliseconds / 1000;
  50. now.tv_usec = (milliseconds % 1000) * 1000;
  51. }
  52. return now;
  53. }
  54. #elif defined(HAVE_CLOCK_GETTIME_MONOTONIC) || \
  55. defined(HAVE_CLOCK_GETTIME_MONOTONIC_RAW)
  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. #ifdef HAVE_CLOCK_GETTIME_MONOTONIC_RAW
  82. if(
  83. #if defined(__APPLE__) && defined(HAVE_BUILTIN_AVAILABLE) && \
  84. (HAVE_BUILTIN_AVAILABLE == 1)
  85. have_clock_gettime &&
  86. #endif
  87. (0 == clock_gettime(CLOCK_MONOTONIC_RAW, &tsnow))) {
  88. cnow.tv_sec = tsnow.tv_sec;
  89. cnow.tv_usec = (unsigned int)(tsnow.tv_nsec / 1000);
  90. }
  91. else
  92. #endif
  93. if(
  94. #if defined(__APPLE__) && defined(HAVE_BUILTIN_AVAILABLE) && \
  95. (HAVE_BUILTIN_AVAILABLE == 1)
  96. have_clock_gettime &&
  97. #endif
  98. (0 == clock_gettime(CLOCK_MONOTONIC, &tsnow))) {
  99. cnow.tv_sec = tsnow.tv_sec;
  100. cnow.tv_usec = (unsigned int)(tsnow.tv_nsec / 1000);
  101. }
  102. /*
  103. ** Even when the configure process has truly detected monotonic clock
  104. ** availability, it might happen that it is not actually available at
  105. ** run-time. When this occurs simply fallback to other time source.
  106. */
  107. #ifdef HAVE_GETTIMEOFDAY
  108. else {
  109. (void)gettimeofday(&now, NULL);
  110. cnow.tv_sec = now.tv_sec;
  111. cnow.tv_usec = (unsigned int)now.tv_usec;
  112. }
  113. #else
  114. else {
  115. cnow.tv_sec = time(NULL);
  116. cnow.tv_usec = 0;
  117. }
  118. #endif
  119. return cnow;
  120. }
  121. #elif defined(HAVE_MACH_ABSOLUTE_TIME)
  122. #include <stdint.h>
  123. #include <mach/mach_time.h>
  124. struct curltime Curl_now(void)
  125. {
  126. /*
  127. ** Monotonic timer on Mac OS is provided by mach_absolute_time(), which
  128. ** returns time in Mach "absolute time units," which are platform-dependent.
  129. ** To convert to nanoseconds, one must use conversion factors specified by
  130. ** mach_timebase_info().
  131. */
  132. static mach_timebase_info_data_t timebase;
  133. struct curltime cnow;
  134. uint64_t usecs;
  135. if(0 == timebase.denom)
  136. (void) mach_timebase_info(&timebase);
  137. usecs = mach_absolute_time();
  138. usecs *= timebase.numer;
  139. usecs /= timebase.denom;
  140. usecs /= 1000;
  141. cnow.tv_sec = usecs / 1000000;
  142. cnow.tv_usec = (int)(usecs % 1000000);
  143. return cnow;
  144. }
  145. #elif defined(HAVE_GETTIMEOFDAY)
  146. struct curltime Curl_now(void)
  147. {
  148. /*
  149. ** gettimeofday() is not granted to be increased monotonically, due to
  150. ** clock drifting and external source time synchronization it can jump
  151. ** forward or backward in time.
  152. */
  153. struct timeval now;
  154. struct curltime ret;
  155. (void)gettimeofday(&now, NULL);
  156. ret.tv_sec = now.tv_sec;
  157. ret.tv_usec = (int)now.tv_usec;
  158. return ret;
  159. }
  160. #else
  161. struct curltime Curl_now(void)
  162. {
  163. /*
  164. ** time() returns the value of time in seconds since the Epoch.
  165. */
  166. struct curltime now;
  167. now.tv_sec = time(NULL);
  168. now.tv_usec = 0;
  169. return now;
  170. }
  171. #endif
  172. /*
  173. * Returns: time difference in number of milliseconds. For too large diffs it
  174. * returns max value.
  175. *
  176. * @unittest: 1323
  177. */
  178. timediff_t Curl_timediff(struct curltime newer, struct curltime older)
  179. {
  180. timediff_t diff = (timediff_t)newer.tv_sec-older.tv_sec;
  181. if(diff >= (TIMEDIFF_T_MAX/1000))
  182. return TIMEDIFF_T_MAX;
  183. else if(diff <= (TIMEDIFF_T_MIN/1000))
  184. return TIMEDIFF_T_MIN;
  185. return diff * 1000 + (newer.tv_usec-older.tv_usec)/1000;
  186. }
  187. /*
  188. * Returns: time difference in number of milliseconds, rounded up.
  189. * For too large diffs it returns max value.
  190. */
  191. timediff_t Curl_timediff_ceil(struct curltime newer, struct curltime older)
  192. {
  193. timediff_t diff = (timediff_t)newer.tv_sec-older.tv_sec;
  194. if(diff >= (TIMEDIFF_T_MAX/1000))
  195. return TIMEDIFF_T_MAX;
  196. else if(diff <= (TIMEDIFF_T_MIN/1000))
  197. return TIMEDIFF_T_MIN;
  198. return diff * 1000 + (newer.tv_usec - older.tv_usec + 999)/1000;
  199. }
  200. /*
  201. * Returns: time difference in number of microseconds. For too large diffs it
  202. * returns max value.
  203. */
  204. timediff_t Curl_timediff_us(struct curltime newer, struct curltime older)
  205. {
  206. timediff_t diff = (timediff_t)newer.tv_sec-older.tv_sec;
  207. if(diff >= (TIMEDIFF_T_MAX/1000000))
  208. return TIMEDIFF_T_MAX;
  209. else if(diff <= (TIMEDIFF_T_MIN/1000000))
  210. return TIMEDIFF_T_MIN;
  211. return diff * 1000000 + newer.tv_usec-older.tv_usec;
  212. }