2 * Copyright (c) Meta Platforms, Inc. and affiliates.
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 === */
15 #include "platform.h" /* set _POSIX_C_SOURCE */
16 #include <time.h> /* CLOCK_MONOTONIC, TIME_UTC */
18 /*-****************************************
20 ******************************************/
22 #if defined(_WIN32) /* Windows */
24 #include <windows.h> /* LARGE_INTEGER */
25 #include <stdlib.h> /* abort */
26 #include <stdio.h> /* perror */
28 UTIL_time_t UTIL_getTime(void)
30 static LARGE_INTEGER ticksPerSecond;
33 if (!QueryPerformanceFrequency(&ticksPerSecond)) {
34 perror("timefn::QueryPerformanceFrequency");
41 QueryPerformanceCounter(&x);
42 r.t = (PTime)(x.QuadPart * 1000000000ULL / ticksPerSecond.QuadPart);
48 #elif defined(__APPLE__) && defined(__MACH__)
50 #include <mach/mach_time.h> /* mach_timebase_info_data_t, mach_timebase_info, mach_absolute_time */
52 UTIL_time_t UTIL_getTime(void)
54 static mach_timebase_info_data_t rate;
57 mach_timebase_info(&rate);
61 r.t = mach_absolute_time() * (PTime)rate.numer / (PTime)rate.denom;
66 /* POSIX.1-2001 (optional) */
67 #elif defined(CLOCK_MONOTONIC)
69 #include <stdlib.h> /* abort */
70 #include <stdio.h> /* perror */
72 UTIL_time_t UTIL_getTime(void)
74 /* time must be initialized, othersize it may fail msan test.
75 * No good reason, likely a limitation of timespec_get() for some target */
76 struct timespec time = { 0, 0 };
77 if (clock_gettime(CLOCK_MONOTONIC, &time) != 0) {
78 perror("timefn::clock_gettime(CLOCK_MONOTONIC)");
82 r.t = (PTime)time.tv_sec * 1000000000ULL + (PTime)time.tv_nsec;
88 /* C11 requires support of timespec_get().
89 * However, FreeBSD 11 claims C11 compliance while lacking timespec_get().
90 * Double confirm timespec_get() support by checking the definition of TIME_UTC.
91 * However, some versions of Android manage to simultaneously define TIME_UTC
92 * and lack timespec_get() support... */
93 #elif (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L) /* C11 */) \
94 && defined(TIME_UTC) && !defined(__ANDROID__)
96 #include <stdlib.h> /* abort */
97 #include <stdio.h> /* perror */
99 UTIL_time_t UTIL_getTime(void)
101 /* time must be initialized, othersize it may fail msan test.
102 * No good reason, likely a limitation of timespec_get() for some target */
103 struct timespec time = { 0, 0 };
104 if (timespec_get(&time, TIME_UTC) != TIME_UTC) {
105 perror("timefn::timespec_get(TIME_UTC)");
109 r.t = (PTime)time.tv_sec * 1000000000ULL + (PTime)time.tv_nsec;
115 #else /* relies on standard C90 (note : clock_t produces wrong measurements for multi-threaded workloads) */
117 UTIL_time_t UTIL_getTime(void)
120 r.t = (PTime)clock() * 1000000000ULL / CLOCKS_PER_SEC;
124 #define TIME_MT_MEASUREMENTS_NOT_SUPPORTED
128 /* ==== Common functions, valid for all time API ==== */
130 PTime UTIL_getSpanTimeNano(UTIL_time_t clockStart, UTIL_time_t clockEnd)
132 return clockEnd.t - clockStart.t;
135 PTime UTIL_getSpanTimeMicro(UTIL_time_t begin, UTIL_time_t end)
137 return UTIL_getSpanTimeNano(begin, end) / 1000ULL;
140 PTime UTIL_clockSpanMicro(UTIL_time_t clockStart )
142 UTIL_time_t const clockEnd = UTIL_getTime();
143 return UTIL_getSpanTimeMicro(clockStart, clockEnd);
146 PTime UTIL_clockSpanNano(UTIL_time_t clockStart )
148 UTIL_time_t const clockEnd = UTIL_getTime();
149 return UTIL_getSpanTimeNano(clockStart, clockEnd);
152 void UTIL_waitForNextTick(void)
154 UTIL_time_t const clockStart = UTIL_getTime();
155 UTIL_time_t clockEnd;
157 clockEnd = UTIL_getTime();
158 } while (UTIL_getSpanTimeNano(clockStart, clockEnd) == 0);
161 int UTIL_support_MT_measurements(void)
163 # if defined(TIME_MT_MEASUREMENTS_NOT_SUPPORTED)