blob: 4f045226b8bf4a2b7cf9e4681cf3a6b0e39b2efd [file] [log] [blame]
Yann Collet59a71162019-04-10 12:37:03 -07001/*
Elliott Hughes44aba642023-09-12 20:18:59 +00002 * Copyright (c) Meta Platforms, Inc. and affiliates.
Yann Collet59a71162019-04-10 12:37:03 -07003 * 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
11
12/* === Dependencies === */
13
14#include "timefn.h"
Elliott Hughes44aba642023-09-12 20:18:59 +000015#include "platform.h" /* set _POSIX_C_SOURCE */
16#include <time.h> /* CLOCK_MONOTONIC, TIME_UTC */
Yann Collet59a71162019-04-10 12:37:03 -070017
18/*-****************************************
19* Time functions
20******************************************/
21
22#if defined(_WIN32) /* Windows */
23
Elliott Hughes44aba642023-09-12 20:18:59 +000024#include <windows.h> /* LARGE_INTEGER */
Yann Collet36d2dfd2019-04-10 14:15:11 -070025#include <stdlib.h> /* abort */
26#include <stdio.h> /* perror */
27
Elliott Hughes44aba642023-09-12 20:18:59 +000028UTIL_time_t UTIL_getTime(void)
Yann Collet59a71162019-04-10 12:37:03 -070029{
30 static LARGE_INTEGER ticksPerSecond;
31 static int init = 0;
32 if (!init) {
Yann Collet70802cd2019-04-10 14:01:18 -070033 if (!QueryPerformanceFrequency(&ticksPerSecond)) {
34 perror("timefn::QueryPerformanceFrequency");
35 abort();
36 }
Yann Collet59a71162019-04-10 12:37:03 -070037 init = 1;
38 }
Elliott Hughes44aba642023-09-12 20:18:59 +000039 { UTIL_time_t r;
40 LARGE_INTEGER x;
41 QueryPerformanceCounter(&x);
42 r.t = (PTime)(x.QuadPart * 1000000000ULL / ticksPerSecond.QuadPart);
43 return r;
Evan Witt79934932023-09-06 22:46:11 +000044 }
Evan Witt79934932023-09-06 22:46:11 +000045}
46
47
Yann Collet59a71162019-04-10 12:37:03 -070048#elif defined(__APPLE__) && defined(__MACH__)
49
Elliott Hughes44aba642023-09-12 20:18:59 +000050#include <mach/mach_time.h> /* mach_timebase_info_data_t, mach_timebase_info, mach_absolute_time */
Yann Collet59a71162019-04-10 12:37:03 -070051
Elliott Hughes44aba642023-09-12 20:18:59 +000052UTIL_time_t UTIL_getTime(void)
Yann Collet59a71162019-04-10 12:37:03 -070053{
54 static mach_timebase_info_data_t rate;
55 static int init = 0;
56 if (!init) {
57 mach_timebase_info(&rate);
58 init = 1;
59 }
Elliott Hughes44aba642023-09-12 20:18:59 +000060 { UTIL_time_t r;
61 r.t = mach_absolute_time() * (PTime)rate.numer / (PTime)rate.denom;
62 return r;
Yann Colleta2ef23d2023-01-12 20:45:11 -080063 }
Elliott Hughes44aba642023-09-12 20:18:59 +000064}
65
66/* POSIX.1-2001 (optional) */
67#elif defined(CLOCK_MONOTONIC)
68
69#include <stdlib.h> /* abort */
70#include <stdio.h> /* perror */
71
72UTIL_time_t UTIL_getTime(void)
73{
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)");
79 abort();
80 }
81 { UTIL_time_t r;
82 r.t = (PTime)time.tv_sec * 1000000000ULL + (PTime)time.tv_nsec;
83 return r;
84 }
Yann Colleta2ef23d2023-01-12 20:45:11 -080085}
86
87
Elliott Hughes44aba642023-09-12 20:18:59 +000088/* 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... */
Yann Collet885476f2019-04-10 15:22:18 -070093#elif (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L) /* C11 */) \
Nick Terrell7ec87cf2020-01-22 11:17:30 -080094 && defined(TIME_UTC) && !defined(__ANDROID__)
Yann Collet36d2dfd2019-04-10 14:15:11 -070095
96#include <stdlib.h> /* abort */
97#include <stdio.h> /* perror */
Yann Collet59a71162019-04-10 12:37:03 -070098
99UTIL_time_t UTIL_getTime(void)
100{
Yann Collet1e015602019-04-11 13:46:30 -0700101 /* time must be initialized, othersize it may fail msan test.
102 * No good reason, likely a limitation of timespec_get() for some target */
Elliott Hughes44aba642023-09-12 20:18:59 +0000103 struct timespec time = { 0, 0 };
Yann Collet1e015602019-04-11 13:46:30 -0700104 if (timespec_get(&time, TIME_UTC) != TIME_UTC) {
Elliott Hughes44aba642023-09-12 20:18:59 +0000105 perror("timefn::timespec_get(TIME_UTC)");
Yann Collet70802cd2019-04-10 14:01:18 -0700106 abort();
107 }
Elliott Hughes44aba642023-09-12 20:18:59 +0000108 { UTIL_time_t r;
109 r.t = (PTime)time.tv_sec * 1000000000ULL + (PTime)time.tv_nsec;
110 return r;
111 }
Evan Witt79934932023-09-06 22:46:11 +0000112}
113
Elliott Hughes44aba642023-09-12 20:18:59 +0000114
115#else /* relies on standard C90 (note : clock_t produces wrong measurements for multi-threaded workloads) */
116
117UTIL_time_t UTIL_getTime(void)
Evan Witt79934932023-09-06 22:46:11 +0000118{
Elliott Hughes44aba642023-09-12 20:18:59 +0000119 UTIL_time_t r;
120 r.t = (PTime)clock() * 1000000000ULL / CLOCKS_PER_SEC;
121 return r;
122}
123
124#define TIME_MT_MEASUREMENTS_NOT_SUPPORTED
125
126#endif
127
128/* ==== Common functions, valid for all time API ==== */
129
130PTime UTIL_getSpanTimeNano(UTIL_time_t clockStart, UTIL_time_t clockEnd)
131{
132 return clockEnd.t - clockStart.t;
Yann Colletbcfb7ad2023-01-12 19:00:27 -0800133}
134
Yann Collet8b130002023-01-06 15:25:36 -0800135PTime UTIL_getSpanTimeMicro(UTIL_time_t begin, UTIL_time_t end)
136{
Elliott Hughes44aba642023-09-12 20:18:59 +0000137 return UTIL_getSpanTimeNano(begin, end) / 1000ULL;
Yann Collet8b130002023-01-06 15:25:36 -0800138}
Yann Collet70802cd2019-04-10 14:01:18 -0700139
Yann Collet59a71162019-04-10 12:37:03 -0700140PTime UTIL_clockSpanMicro(UTIL_time_t clockStart )
141{
142 UTIL_time_t const clockEnd = UTIL_getTime();
143 return UTIL_getSpanTimeMicro(clockStart, clockEnd);
144}
145
Yann Collet59a71162019-04-10 12:37:03 -0700146PTime UTIL_clockSpanNano(UTIL_time_t clockStart )
147{
148 UTIL_time_t const clockEnd = UTIL_getTime();
149 return UTIL_getSpanTimeNano(clockStart, clockEnd);
150}
151
152void UTIL_waitForNextTick(void)
153{
154 UTIL_time_t const clockStart = UTIL_getTime();
155 UTIL_time_t clockEnd;
156 do {
157 clockEnd = UTIL_getTime();
158 } while (UTIL_getSpanTimeNano(clockStart, clockEnd) == 0);
159}
Elliott Hughes44aba642023-09-12 20:18:59 +0000160
161int UTIL_support_MT_measurements(void)
162{
163# if defined(TIME_MT_MEASUREMENTS_NOT_SUPPORTED)
164 return 0;
165# else
166 return 1;
167# endif
168}