timefn.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Copyright (c) Meta Platforms, Inc. and affiliates.
  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. #include "platform.h" /* set _POSIX_C_SOURCE */
  13. #include <time.h> /* CLOCK_MONOTONIC, TIME_UTC */
  14. /*-****************************************
  15. * Time functions
  16. ******************************************/
  17. #if defined(_WIN32) /* Windows */
  18. #include <windows.h> /* LARGE_INTEGER */
  19. #include <stdlib.h> /* abort */
  20. #include <stdio.h> /* perror */
  21. UTIL_time_t UTIL_getTime(void)
  22. {
  23. static LARGE_INTEGER ticksPerSecond;
  24. static int init = 0;
  25. if (!init) {
  26. if (!QueryPerformanceFrequency(&ticksPerSecond)) {
  27. perror("timefn::QueryPerformanceFrequency");
  28. abort();
  29. }
  30. init = 1;
  31. }
  32. { UTIL_time_t r;
  33. LARGE_INTEGER x;
  34. QueryPerformanceCounter(&x);
  35. r.t = (PTime)(x.QuadPart * 1000000000ULL / ticksPerSecond.QuadPart);
  36. return r;
  37. }
  38. }
  39. #elif defined(__APPLE__) && defined(__MACH__)
  40. #include <mach/mach_time.h> /* mach_timebase_info_data_t, mach_timebase_info, mach_absolute_time */
  41. UTIL_time_t UTIL_getTime(void)
  42. {
  43. static mach_timebase_info_data_t rate;
  44. static int init = 0;
  45. if (!init) {
  46. mach_timebase_info(&rate);
  47. init = 1;
  48. }
  49. { UTIL_time_t r;
  50. r.t = mach_absolute_time() * (PTime)rate.numer / (PTime)rate.denom;
  51. return r;
  52. }
  53. }
  54. /* POSIX.1-2001 (optional) */
  55. #elif defined(CLOCK_MONOTONIC)
  56. #include <stdlib.h> /* abort */
  57. #include <stdio.h> /* perror */
  58. UTIL_time_t UTIL_getTime(void)
  59. {
  60. /* time must be initialized, othersize it may fail msan test.
  61. * No good reason, likely a limitation of timespec_get() for some target */
  62. struct timespec time = { 0, 0 };
  63. if (clock_gettime(CLOCK_MONOTONIC, &time) != 0) {
  64. perror("timefn::clock_gettime(CLOCK_MONOTONIC)");
  65. abort();
  66. }
  67. { UTIL_time_t r;
  68. r.t = (PTime)time.tv_sec * 1000000000ULL + (PTime)time.tv_nsec;
  69. return r;
  70. }
  71. }
  72. /* C11 requires support of timespec_get().
  73. * However, FreeBSD 11 claims C11 compliance while lacking timespec_get().
  74. * Double confirm timespec_get() support by checking the definition of TIME_UTC.
  75. * However, some versions of Android manage to simultaneously define TIME_UTC
  76. * and lack timespec_get() support... */
  77. #elif (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L) /* C11 */) \
  78. && defined(TIME_UTC) && !defined(__ANDROID__)
  79. #include <stdlib.h> /* abort */
  80. #include <stdio.h> /* perror */
  81. UTIL_time_t UTIL_getTime(void)
  82. {
  83. /* time must be initialized, othersize it may fail msan test.
  84. * No good reason, likely a limitation of timespec_get() for some target */
  85. struct timespec time = { 0, 0 };
  86. if (timespec_get(&time, TIME_UTC) != TIME_UTC) {
  87. perror("timefn::timespec_get(TIME_UTC)");
  88. abort();
  89. }
  90. { UTIL_time_t r;
  91. r.t = (PTime)time.tv_sec * 1000000000ULL + (PTime)time.tv_nsec;
  92. return r;
  93. }
  94. }
  95. #else /* relies on standard C90 (note : clock_t produces wrong measurements for multi-threaded workloads) */
  96. UTIL_time_t UTIL_getTime(void)
  97. {
  98. UTIL_time_t r;
  99. r.t = (PTime)clock() * 1000000000ULL / CLOCKS_PER_SEC;
  100. return r;
  101. }
  102. #define TIME_MT_MEASUREMENTS_NOT_SUPPORTED
  103. #endif
  104. /* ==== Common functions, valid for all time API ==== */
  105. PTime UTIL_getSpanTimeNano(UTIL_time_t clockStart, UTIL_time_t clockEnd)
  106. {
  107. return clockEnd.t - clockStart.t;
  108. }
  109. PTime UTIL_getSpanTimeMicro(UTIL_time_t begin, UTIL_time_t end)
  110. {
  111. return UTIL_getSpanTimeNano(begin, end) / 1000ULL;
  112. }
  113. PTime UTIL_clockSpanMicro(UTIL_time_t clockStart )
  114. {
  115. UTIL_time_t const clockEnd = UTIL_getTime();
  116. return UTIL_getSpanTimeMicro(clockStart, clockEnd);
  117. }
  118. PTime UTIL_clockSpanNano(UTIL_time_t clockStart )
  119. {
  120. UTIL_time_t const clockEnd = UTIL_getTime();
  121. return UTIL_getSpanTimeNano(clockStart, clockEnd);
  122. }
  123. void UTIL_waitForNextTick(void)
  124. {
  125. UTIL_time_t const clockStart = UTIL_getTime();
  126. UTIL_time_t clockEnd;
  127. do {
  128. clockEnd = UTIL_getTime();
  129. } while (UTIL_getSpanTimeNano(clockStart, clockEnd) == 0);
  130. }
  131. int UTIL_support_MT_measurements(void)
  132. {
  133. # if defined(TIME_MT_MEASUREMENTS_NOT_SUPPORTED)
  134. return 0;
  135. # else
  136. return 1;
  137. # endif
  138. }