1 /*
2 * Copyright (c) 2019-2020, 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
11
12 /* === Dependencies === */
13
14 #include "timefn.h"
15
16
17 /*-****************************************
18 * Time functions
19 ******************************************/
20
21 #if defined(_WIN32) /* Windows */
22
23 #include <stdlib.h> /* abort */
24 #include <stdio.h> /* perror */
25
UTIL_getTime(void)26 UTIL_time_t UTIL_getTime(void) { UTIL_time_t x; QueryPerformanceCounter(&x); return x; }
27
UTIL_getSpanTimeMicro(UTIL_time_t clockStart,UTIL_time_t clockEnd)28 PTime UTIL_getSpanTimeMicro(UTIL_time_t clockStart, UTIL_time_t clockEnd)
29 {
30 static LARGE_INTEGER ticksPerSecond;
31 static int init = 0;
32 if (!init) {
33 if (!QueryPerformanceFrequency(&ticksPerSecond)) {
34 perror("timefn::QueryPerformanceFrequency");
35 abort();
36 }
37 init = 1;
38 }
39 return 1000000ULL*(clockEnd.QuadPart - clockStart.QuadPart)/ticksPerSecond.QuadPart;
40 }
41
UTIL_getSpanTimeNano(UTIL_time_t clockStart,UTIL_time_t clockEnd)42 PTime UTIL_getSpanTimeNano(UTIL_time_t clockStart, UTIL_time_t clockEnd)
43 {
44 static LARGE_INTEGER ticksPerSecond;
45 static int init = 0;
46 if (!init) {
47 if (!QueryPerformanceFrequency(&ticksPerSecond)) {
48 perror("timefn::QueryPerformanceFrequency");
49 abort();
50 }
51 init = 1;
52 }
53 return 1000000000ULL*(clockEnd.QuadPart - clockStart.QuadPart)/ticksPerSecond.QuadPart;
54 }
55
56
57
58 #elif defined(__APPLE__) && defined(__MACH__)
59
UTIL_getTime(void)60 UTIL_time_t UTIL_getTime(void) { return mach_absolute_time(); }
61
UTIL_getSpanTimeMicro(UTIL_time_t clockStart,UTIL_time_t clockEnd)62 PTime UTIL_getSpanTimeMicro(UTIL_time_t clockStart, UTIL_time_t clockEnd)
63 {
64 static mach_timebase_info_data_t rate;
65 static int init = 0;
66 if (!init) {
67 mach_timebase_info(&rate);
68 init = 1;
69 }
70 return (((clockEnd - clockStart) * (PTime)rate.numer) / ((PTime)rate.denom))/1000ULL;
71 }
72
UTIL_getSpanTimeNano(UTIL_time_t clockStart,UTIL_time_t clockEnd)73 PTime UTIL_getSpanTimeNano(UTIL_time_t clockStart, UTIL_time_t clockEnd)
74 {
75 static mach_timebase_info_data_t rate;
76 static int init = 0;
77 if (!init) {
78 mach_timebase_info(&rate);
79 init = 1;
80 }
81 return ((clockEnd - clockStart) * (PTime)rate.numer) / ((PTime)rate.denom);
82 }
83
84
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__)
89
90 #include <stdlib.h> /* abort */
91 #include <stdio.h> /* perror */
92
UTIL_getTime(void)93 UTIL_time_t UTIL_getTime(void)
94 {
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");
100 abort();
101 }
102 return time;
103 }
104
UTIL_getSpanTime(UTIL_time_t begin,UTIL_time_t end)105 static UTIL_time_t UTIL_getSpanTime(UTIL_time_t begin, UTIL_time_t end)
106 {
107 UTIL_time_t diff;
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;
111 } else {
112 diff.tv_sec = end.tv_sec - begin.tv_sec;
113 diff.tv_nsec = end.tv_nsec - begin.tv_nsec;
114 }
115 return diff;
116 }
117
UTIL_getSpanTimeMicro(UTIL_time_t begin,UTIL_time_t end)118 PTime UTIL_getSpanTimeMicro(UTIL_time_t begin, UTIL_time_t end)
119 {
120 UTIL_time_t const diff = UTIL_getSpanTime(begin, end);
121 PTime micro = 0;
122 micro += 1000000ULL * diff.tv_sec;
123 micro += diff.tv_nsec / 1000ULL;
124 return micro;
125 }
126
UTIL_getSpanTimeNano(UTIL_time_t begin,UTIL_time_t end)127 PTime UTIL_getSpanTimeNano(UTIL_time_t begin, UTIL_time_t end)
128 {
129 UTIL_time_t const diff = UTIL_getSpanTime(begin, end);
130 PTime nano = 0;
131 nano += 1000000000ULL * diff.tv_sec;
132 nano += diff.tv_nsec;
133 return nano;
134 }
135
136
137
138 #else /* relies on standard C90 (note : clock_t measurements can be wrong when using multi-threading) */
139
UTIL_getTime(void)140 UTIL_time_t UTIL_getTime(void) { return clock(); }
UTIL_getSpanTimeMicro(UTIL_time_t clockStart,UTIL_time_t clockEnd)141 PTime UTIL_getSpanTimeMicro(UTIL_time_t clockStart, UTIL_time_t clockEnd) { return 1000000ULL * (clockEnd - clockStart) / CLOCKS_PER_SEC; }
UTIL_getSpanTimeNano(UTIL_time_t clockStart,UTIL_time_t clockEnd)142 PTime UTIL_getSpanTimeNano(UTIL_time_t clockStart, UTIL_time_t clockEnd) { return 1000000000ULL * (clockEnd - clockStart) / CLOCKS_PER_SEC; }
143
144 #endif
145
146
147
148 /* returns time span in microseconds */
UTIL_clockSpanMicro(UTIL_time_t clockStart)149 PTime UTIL_clockSpanMicro(UTIL_time_t clockStart )
150 {
151 UTIL_time_t const clockEnd = UTIL_getTime();
152 return UTIL_getSpanTimeMicro(clockStart, clockEnd);
153 }
154
155 /* returns time span in microseconds */
UTIL_clockSpanNano(UTIL_time_t clockStart)156 PTime UTIL_clockSpanNano(UTIL_time_t clockStart )
157 {
158 UTIL_time_t const clockEnd = UTIL_getTime();
159 return UTIL_getSpanTimeNano(clockStart, clockEnd);
160 }
161
UTIL_waitForNextTick(void)162 void UTIL_waitForNextTick(void)
163 {
164 UTIL_time_t const clockStart = UTIL_getTime();
165 UTIL_time_t clockEnd;
166 do {
167 clockEnd = UTIL_getTime();
168 } while (UTIL_getSpanTimeNano(clockStart, clockEnd) == 0);
169 }
170