2 * Copyright (c) 2019-2020, Yann Collet, Facebook, Inc.
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.
12 /* === Dependencies === */
17 /*-****************************************
19 ******************************************/
21 #if defined(_WIN32) /* Windows */
23 #include <stdlib.h> /* abort */
24 #include <stdio.h> /* perror */
26 UTIL_time_t UTIL_getTime(void) { UTIL_time_t x; QueryPerformanceCounter(&x); return x; }
28 PTime UTIL_getSpanTimeMicro(UTIL_time_t clockStart, UTIL_time_t clockEnd)
30 static LARGE_INTEGER ticksPerSecond;
33 if (!QueryPerformanceFrequency(&ticksPerSecond)) {
34 perror("timefn::QueryPerformanceFrequency");
39 return 1000000ULL*(clockEnd.QuadPart - clockStart.QuadPart)/ticksPerSecond.QuadPart;
42 PTime UTIL_getSpanTimeNano(UTIL_time_t clockStart, UTIL_time_t clockEnd)
44 static LARGE_INTEGER ticksPerSecond;
47 if (!QueryPerformanceFrequency(&ticksPerSecond)) {
48 perror("timefn::QueryPerformanceFrequency");
53 return 1000000000ULL*(clockEnd.QuadPart - clockStart.QuadPart)/ticksPerSecond.QuadPart;
58 #elif defined(__APPLE__) && defined(__MACH__)
60 UTIL_time_t UTIL_getTime(void) { return mach_absolute_time(); }
62 PTime UTIL_getSpanTimeMicro(UTIL_time_t clockStart, UTIL_time_t clockEnd)
64 static mach_timebase_info_data_t rate;
67 mach_timebase_info(&rate);
70 return (((clockEnd - clockStart) * (PTime)rate.numer) / ((PTime)rate.denom))/1000ULL;
73 PTime UTIL_getSpanTimeNano(UTIL_time_t clockStart, UTIL_time_t clockEnd)
75 static mach_timebase_info_data_t rate;
78 mach_timebase_info(&rate);
81 return ((clockEnd - clockStart) * (PTime)rate.numer) / ((PTime)rate.denom);
85 /* C11 requires timespec_get, but FreeBSD 11 lacks it, while still claiming C11 compliance.
86 Android also lacks it but does define TIME_UTC. */
87 #elif (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L) /* C11 */) \
88 && defined(TIME_UTC) && !defined(__ANDROID__)
90 #include <stdlib.h> /* abort */
91 #include <stdio.h> /* perror */
93 UTIL_time_t UTIL_getTime(void)
95 /* time must be initialized, othersize it may fail msan test.
96 * No good reason, likely a limitation of timespec_get() for some target */
97 UTIL_time_t time = UTIL_TIME_INITIALIZER;
98 if (timespec_get(&time, TIME_UTC) != TIME_UTC) {
99 perror("timefn::timespec_get");
105 static UTIL_time_t UTIL_getSpanTime(UTIL_time_t begin, UTIL_time_t end)
108 if (end.tv_nsec < begin.tv_nsec) {
109 diff.tv_sec = (end.tv_sec - 1) - begin.tv_sec;
110 diff.tv_nsec = (end.tv_nsec + 1000000000ULL) - begin.tv_nsec;
112 diff.tv_sec = end.tv_sec - begin.tv_sec;
113 diff.tv_nsec = end.tv_nsec - begin.tv_nsec;
118 PTime UTIL_getSpanTimeMicro(UTIL_time_t begin, UTIL_time_t end)
120 UTIL_time_t const diff = UTIL_getSpanTime(begin, end);
122 micro += 1000000ULL * diff.tv_sec;
123 micro += diff.tv_nsec / 1000ULL;
127 PTime UTIL_getSpanTimeNano(UTIL_time_t begin, UTIL_time_t end)
129 UTIL_time_t const diff = UTIL_getSpanTime(begin, end);
131 nano += 1000000000ULL * diff.tv_sec;
132 nano += diff.tv_nsec;
138 #else /* relies on standard C90 (note : clock_t measurements can be wrong when using multi-threading) */
140 UTIL_time_t UTIL_getTime(void) { return clock(); }
141 PTime UTIL_getSpanTimeMicro(UTIL_time_t clockStart, UTIL_time_t clockEnd) { return 1000000ULL * (clockEnd - clockStart) / CLOCKS_PER_SEC; }
142 PTime UTIL_getSpanTimeNano(UTIL_time_t clockStart, UTIL_time_t clockEnd) { return 1000000000ULL * (clockEnd - clockStart) / CLOCKS_PER_SEC; }
148 /* returns time span in microseconds */
149 PTime UTIL_clockSpanMicro(UTIL_time_t clockStart )
151 UTIL_time_t const clockEnd = UTIL_getTime();
152 return UTIL_getSpanTimeMicro(clockStart, clockEnd);
155 /* returns time span in microseconds */
156 PTime UTIL_clockSpanNano(UTIL_time_t clockStart )
158 UTIL_time_t const clockEnd = UTIL_getTime();
159 return UTIL_getSpanTimeNano(clockStart, clockEnd);
162 void UTIL_waitForNextTick(void)
164 UTIL_time_t const clockStart = UTIL_getTime();
165 UTIL_time_t clockEnd;
167 clockEnd = UTIL_getTime();
168 } while (UTIL_getSpanTimeNano(clockStart, clockEnd) == 0);