tool_util.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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.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 "tool_setup.h"
  23. #include "tool_util.h"
  24. #include "memdebug.h" /* keep this as LAST include */
  25. #if defined(WIN32) && !defined(MSDOS)
  26. /* set in win32_init() */
  27. extern LARGE_INTEGER tool_freq;
  28. extern bool tool_isVistaOrGreater;
  29. /* In case of bug fix this function has a counterpart in timeval.c */
  30. struct timeval tvnow(void)
  31. {
  32. struct timeval now;
  33. if(tool_isVistaOrGreater) { /* QPC timer might have issues pre-Vista */
  34. LARGE_INTEGER count;
  35. QueryPerformanceCounter(&count);
  36. now.tv_sec = (long)(count.QuadPart / tool_freq.QuadPart);
  37. now.tv_usec = (long)((count.QuadPart % tool_freq.QuadPart) * 1000000 /
  38. tool_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 = (long)(milliseconds / 1000);
  51. now.tv_usec = (long)((milliseconds % 1000) * 1000);
  52. }
  53. return now;
  54. }
  55. #elif defined(HAVE_CLOCK_GETTIME_MONOTONIC)
  56. struct timeval tvnow(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. struct timeval now;
  66. struct timespec tsnow;
  67. if(0 == clock_gettime(CLOCK_MONOTONIC, &tsnow)) {
  68. now.tv_sec = tsnow.tv_sec;
  69. now.tv_usec = (int)(tsnow.tv_nsec / 1000);
  70. }
  71. /*
  72. ** Even when the configure process has truly detected monotonic clock
  73. ** availability, it might happen that it is not actually available at
  74. ** run-time. When this occurs simply fallback to other time source.
  75. */
  76. #ifdef HAVE_GETTIMEOFDAY
  77. else
  78. (void)gettimeofday(&now, NULL);
  79. #else
  80. else {
  81. now.tv_sec = (long)time(NULL);
  82. now.tv_usec = 0;
  83. }
  84. #endif
  85. return now;
  86. }
  87. #elif defined(HAVE_GETTIMEOFDAY)
  88. struct timeval tvnow(void)
  89. {
  90. /*
  91. ** gettimeofday() is not granted to be increased monotonically, due to
  92. ** clock drifting and external source time synchronization it can jump
  93. ** forward or backward in time.
  94. */
  95. struct timeval now;
  96. (void)gettimeofday(&now, NULL);
  97. return now;
  98. }
  99. #else
  100. struct timeval tvnow(void)
  101. {
  102. /*
  103. ** time() returns the value of time in seconds since the Epoch.
  104. */
  105. struct timeval now;
  106. now.tv_sec = (long)time(NULL);
  107. now.tv_usec = 0;
  108. return now;
  109. }
  110. #endif
  111. /*
  112. * Make sure that the first argument is the more recent time, as otherwise
  113. * we'll get a weird negative time-diff back...
  114. *
  115. * Returns: the time difference in number of milliseconds.
  116. */
  117. long tvdiff(struct timeval newer, struct timeval older)
  118. {
  119. return (long)(newer.tv_sec-older.tv_sec)*1000+
  120. (long)(newer.tv_usec-older.tv_usec)/1000;
  121. }