timefn.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Copyright (c) Yann Collet, Facebook, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under both the BSD-style license (found in the
  6. * LICENSE file in the root directory of this source tree) and the GPLv2 (found
  7. * in the COPYING file in the root directory of this source tree).
  8. * You may select, at your option, one of the above-listed licenses.
  9. */
  10. /* === Dependencies === */
  11. #include "timefn.h"
  12. /*-****************************************
  13. * Time functions
  14. ******************************************/
  15. #if defined(_WIN32) /* Windows */
  16. #include <stdlib.h> /* abort */
  17. #include <stdio.h> /* perror */
  18. UTIL_time_t UTIL_getTime(void) { UTIL_time_t x; QueryPerformanceCounter(&x); return x; }
  19. PTime UTIL_getSpanTimeMicro(UTIL_time_t clockStart, UTIL_time_t clockEnd)
  20. {
  21. static LARGE_INTEGER ticksPerSecond;
  22. static int init = 0;
  23. if (!init) {
  24. if (!QueryPerformanceFrequency(&ticksPerSecond)) {
  25. perror("timefn::QueryPerformanceFrequency");
  26. abort();
  27. }
  28. init = 1;
  29. }
  30. return 1000000ULL*(clockEnd.QuadPart - clockStart.QuadPart)/ticksPerSecond.QuadPart;
  31. }
  32. PTime UTIL_getSpanTimeNano(UTIL_time_t clockStart, UTIL_time_t clockEnd)
  33. {
  34. static LARGE_INTEGER ticksPerSecond;
  35. static int init = 0;
  36. if (!init) {
  37. if (!QueryPerformanceFrequency(&ticksPerSecond)) {
  38. perror("timefn::QueryPerformanceFrequency");
  39. abort();
  40. }
  41. init = 1;
  42. }
  43. return 1000000000ULL*(clockEnd.QuadPart - clockStart.QuadPart)/ticksPerSecond.QuadPart;
  44. }
  45. #elif defined(__APPLE__) && defined(__MACH__)
  46. UTIL_time_t UTIL_getTime(void) { return mach_absolute_time(); }
  47. PTime UTIL_getSpanTimeMicro(UTIL_time_t clockStart, UTIL_time_t clockEnd)
  48. {
  49. static mach_timebase_info_data_t rate;
  50. static int init = 0;
  51. if (!init) {
  52. mach_timebase_info(&rate);
  53. init = 1;
  54. }
  55. return (((clockEnd - clockStart) * (PTime)rate.numer) / ((PTime)rate.denom))/1000ULL;
  56. }
  57. PTime UTIL_getSpanTimeNano(UTIL_time_t clockStart, UTIL_time_t clockEnd)
  58. {
  59. static mach_timebase_info_data_t rate;
  60. static int init = 0;
  61. if (!init) {
  62. mach_timebase_info(&rate);
  63. init = 1;
  64. }
  65. return ((clockEnd - clockStart) * (PTime)rate.numer) / ((PTime)rate.denom);
  66. }
  67. /* C11 requires timespec_get, but FreeBSD 11 lacks it, while still claiming C11 compliance.
  68. Android also lacks it but does define TIME_UTC. */
  69. #elif (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L) /* C11 */) \
  70. && defined(TIME_UTC) && !defined(__ANDROID__)
  71. #include <stdlib.h> /* abort */
  72. #include <stdio.h> /* perror */
  73. UTIL_time_t UTIL_getTime(void)
  74. {
  75. /* time must be initialized, othersize it may fail msan test.
  76. * No good reason, likely a limitation of timespec_get() for some target */
  77. UTIL_time_t time = UTIL_TIME_INITIALIZER;
  78. if (timespec_get(&time, TIME_UTC) != TIME_UTC) {
  79. perror("timefn::timespec_get");
  80. abort();
  81. }
  82. return time;
  83. }
  84. static UTIL_time_t UTIL_getSpanTime(UTIL_time_t begin, UTIL_time_t end)
  85. {
  86. UTIL_time_t diff;
  87. if (end.tv_nsec < begin.tv_nsec) {
  88. diff.tv_sec = (end.tv_sec - 1) - begin.tv_sec;
  89. diff.tv_nsec = (end.tv_nsec + 1000000000ULL) - begin.tv_nsec;
  90. } else {
  91. diff.tv_sec = end.tv_sec - begin.tv_sec;
  92. diff.tv_nsec = end.tv_nsec - begin.tv_nsec;
  93. }
  94. return diff;
  95. }
  96. PTime UTIL_getSpanTimeMicro(UTIL_time_t begin, UTIL_time_t end)
  97. {
  98. UTIL_time_t const diff = UTIL_getSpanTime(begin, end);
  99. PTime micro = 0;
  100. micro += 1000000ULL * diff.tv_sec;
  101. micro += diff.tv_nsec / 1000ULL;
  102. return micro;
  103. }
  104. PTime UTIL_getSpanTimeNano(UTIL_time_t begin, UTIL_time_t end)
  105. {
  106. UTIL_time_t const diff = UTIL_getSpanTime(begin, end);
  107. PTime nano = 0;
  108. nano += 1000000000ULL * diff.tv_sec;
  109. nano += diff.tv_nsec;
  110. return nano;
  111. }
  112. #else /* relies on standard C90 (note : clock_t measurements can be wrong when using multi-threading) */
  113. UTIL_time_t UTIL_getTime(void) { return clock(); }
  114. PTime UTIL_getSpanTimeMicro(UTIL_time_t clockStart, UTIL_time_t clockEnd) { return 1000000ULL * (clockEnd - clockStart) / CLOCKS_PER_SEC; }
  115. PTime UTIL_getSpanTimeNano(UTIL_time_t clockStart, UTIL_time_t clockEnd) { return 1000000000ULL * (clockEnd - clockStart) / CLOCKS_PER_SEC; }
  116. #endif
  117. /* returns time span in microseconds */
  118. PTime UTIL_clockSpanMicro(UTIL_time_t clockStart )
  119. {
  120. UTIL_time_t const clockEnd = UTIL_getTime();
  121. return UTIL_getSpanTimeMicro(clockStart, clockEnd);
  122. }
  123. /* returns time span in microseconds */
  124. PTime UTIL_clockSpanNano(UTIL_time_t clockStart )
  125. {
  126. UTIL_time_t const clockEnd = UTIL_getTime();
  127. return UTIL_getSpanTimeNano(clockStart, clockEnd);
  128. }
  129. void UTIL_waitForNextTick(void)
  130. {
  131. UTIL_time_t const clockStart = UTIL_getTime();
  132. UTIL_time_t clockEnd;
  133. do {
  134. clockEnd = UTIL_getTime();
  135. } while (UTIL_getSpanTimeNano(clockStart, clockEnd) == 0);
  136. }