1 /* 2 * Copyright (c) 2019-present, 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 26 UTIL_time_t UTIL_getTime(void) { UTIL_time_t x; QueryPerformanceCounter(&x); return x; } 27 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 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 60 UTIL_time_t UTIL_getTime(void) { return mach_absolute_time(); } 61 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 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 86 #elif (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L) /* C11 */) \ 87 && defined(TIME_UTC) /* C11 requires timespec_get, but FreeBSD 11 lacks it, while still claiming C11 compliance */ 88 89 #include <stdlib.h> /* abort */ 90 #include <stdio.h> /* perror */ 91 92 UTIL_time_t UTIL_getTime(void) 93 { 94 /* time must be initialized, othersize it may fail msan test. 95 * No good reason, likely a limitation of timespec_get() for some target */ 96 UTIL_time_t time = UTIL_TIME_INITIALIZER; 97 if (timespec_get(&time, TIME_UTC) != TIME_UTC) { 98 perror("timefn::timespec_get"); 99 abort(); 100 } 101 return time; 102 } 103 104 static UTIL_time_t UTIL_getSpanTime(UTIL_time_t begin, UTIL_time_t end) 105 { 106 UTIL_time_t diff; 107 if (end.tv_nsec < begin.tv_nsec) { 108 diff.tv_sec = (end.tv_sec - 1) - begin.tv_sec; 109 diff.tv_nsec = (end.tv_nsec + 1000000000ULL) - begin.tv_nsec; 110 } else { 111 diff.tv_sec = end.tv_sec - begin.tv_sec; 112 diff.tv_nsec = end.tv_nsec - begin.tv_nsec; 113 } 114 return diff; 115 } 116 117 PTime UTIL_getSpanTimeMicro(UTIL_time_t begin, UTIL_time_t end) 118 { 119 UTIL_time_t const diff = UTIL_getSpanTime(begin, end); 120 PTime micro = 0; 121 micro += 1000000ULL * diff.tv_sec; 122 micro += diff.tv_nsec / 1000ULL; 123 return micro; 124 } 125 126 PTime UTIL_getSpanTimeNano(UTIL_time_t begin, UTIL_time_t end) 127 { 128 UTIL_time_t const diff = UTIL_getSpanTime(begin, end); 129 PTime nano = 0; 130 nano += 1000000000ULL * diff.tv_sec; 131 nano += diff.tv_nsec; 132 return nano; 133 } 134 135 136 137 #else /* relies on standard C90 (note : clock_t measurements can be wrong when using multi-threading) */ 138 139 UTIL_time_t UTIL_getTime(void) { return clock(); } 140 PTime UTIL_getSpanTimeMicro(UTIL_time_t clockStart, UTIL_time_t clockEnd) { return 1000000ULL * (clockEnd - clockStart) / CLOCKS_PER_SEC; } 141 PTime UTIL_getSpanTimeNano(UTIL_time_t clockStart, UTIL_time_t clockEnd) { return 1000000000ULL * (clockEnd - clockStart) / CLOCKS_PER_SEC; } 142 143 #endif 144 145 146 147 /* returns time span in microseconds */ 148 PTime UTIL_clockSpanMicro(UTIL_time_t clockStart ) 149 { 150 UTIL_time_t const clockEnd = UTIL_getTime(); 151 return UTIL_getSpanTimeMicro(clockStart, clockEnd); 152 } 153 154 /* returns time span in microseconds */ 155 PTime UTIL_clockSpanNano(UTIL_time_t clockStart ) 156 { 157 UTIL_time_t const clockEnd = UTIL_getTime(); 158 return UTIL_getSpanTimeNano(clockStart, clockEnd); 159 } 160 161 void UTIL_waitForNextTick(void) 162 { 163 UTIL_time_t const clockStart = UTIL_getTime(); 164 UTIL_time_t clockEnd; 165 do { 166 clockEnd = UTIL_getTime(); 167 } while (UTIL_getSpanTimeNano(clockStart, clockEnd) == 0); 168 } 169