Line data Source code
1 : /* Copyright (C) 2026 Wildfire Games. 2 : * 3 : * Permission is hereby granted, free of charge, to any person obtaining 4 : * a copy of this software and associated documentation files (the 5 : * "Software"), to deal in the Software without restriction, including 6 : * without limitation the rights to use, copy, modify, merge, publish, 7 : * distribute, sublicense, and/or sell copies of the Software, and to 8 : * permit persons to whom the Software is furnished to do so, subject to 9 : * the following conditions: 10 : * 11 : * The above copyright notice and this permission notice shall be included 12 : * in all copies or substantial portions of the Software. 13 : * 14 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 : * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 : * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 17 : * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 18 : * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 19 : * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 20 : * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 : */ 22 : 23 : /* 24 : * platform-independent high resolution timer 25 : */ 26 : 27 : #include "precompiled.h" 28 : 29 : #include "timer.h" 30 : 31 : #include "lib/module_init.h" 32 : #include "lib/posix/posix_types.h" 33 : #include "lib/status.h" 34 : #include "lib/sysdep/os.h" 35 : #include "lib/utf8.h" 36 : 37 : #include <algorithm> 38 : #include <ctime> 39 : #include <mutex> 40 : #include <sstream> 41 : 42 : #if OS_WIN 43 : #include "lib/sysdep/os/win/win.h" 44 : #endif 45 : #if OS_UNIX 46 : # include <unistd.h> 47 : # include <time.h> 48 : #endif 49 : 50 : #if OS_UNIX || OS_WIN 51 : # define HAVE_GETTIMEOFDAY 1 52 : #else 53 : # define HAVE_GETTIMEOFDAY 0 54 : #endif 55 : 56 : #if (defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0) || OS_MACOSX 57 : # define HAVE_CLOCK_GETTIME 1 58 : #else 59 : # define HAVE_CLOCK_GETTIME 0 60 : #endif 61 : 62 : // rationale for wrapping gettimeofday and clock_gettime, instead of just 63 : // emulating them where not available: allows returning higher-resolution 64 : // timer values than their us / ns interface, via double [seconds]. 65 : // they're also not guaranteed to be monotonic. 66 : 67 : #if OS_WIN 68 : static LARGE_INTEGER start; 69 : #elif HAVE_CLOCK_GETTIME 70 : static struct timespec start; 71 : #elif HAVE_GETTIMEOFDAY 72 : static struct timeval start; 73 : #endif 74 : 75 : 76 : //----------------------------------------------------------------------------- 77 : // timer API 78 : 79 : 80 : // Cached because the default implementation may take several milliseconds. 81 : static double resolution; 82 1 : static Status InitResolution() 83 : { 84 : #if OS_WIN 85 : LARGE_INTEGER frequency; 86 : ENSURE(QueryPerformanceFrequency(&frequency)); 87 : resolution = 1.0 / static_cast<double>(frequency.QuadPart); 88 : #elif HAVE_CLOCK_GETTIME 89 1 : struct timespec ts; 90 1 : if (clock_getres(CLOCK_MONOTONIC, &ts) == 0) 91 1 : resolution = ts.tv_nsec * 1e-9; 92 : else 93 0 : resolution = 1e-9; 94 : #elif HAVE_GETTIMEOFDAY 95 : resolution = 1e-6; 96 : #else 97 : const double t0 = timer_Time(); 98 : double t1, t2; 99 : do t1 = timer_Time(); while (t1 == t0); 100 : do t2 = timer_Time(); while (t2 == t1); 101 : resolution = t2 - t1; 102 : #endif 103 1 : return INFO::OK; 104 : } 105 : 106 1 : void timer_Init() 107 : { 108 : #if OS_WIN 109 : ENSURE(QueryPerformanceCounter(&start)); 110 : #elif HAVE_CLOCK_GETTIME 111 1 : ENSURE(clock_gettime(CLOCK_MONOTONIC, &start) == 0); 112 : #elif HAVE_GETTIMEOFDAY 113 : ENSURE(gettimeofday(&start, 0) == 0); 114 : #endif 115 : 116 1 : static ModuleInitState initState{ 0 }; 117 1 : ModuleInit(&initState, InitResolution); 118 1 : ENSURE(resolution != 0.0); 119 1 : } 120 : 121 : static std::mutex ensure_monotonic_mutex; 122 : // NB: does not guarantee strict monotonicity - callers must avoid 123 : // dividing by the difference of two equal times. 124 57893 : static void EnsureMonotonic(double& newTime) 125 : { 126 57893 : std::lock_guard<std::mutex> lock(ensure_monotonic_mutex); 127 57899 : static double maxTime; 128 57899 : maxTime = std::max(maxTime, newTime); 129 57899 : newTime = maxTime; 130 57899 : } 131 : 132 57890 : double timer_Time() 133 : { 134 57890 : double t; 135 : 136 : #if OS_WIN 137 : ENSURE(start.QuadPart); // must have called timer_LatchStartTime first 138 : LARGE_INTEGER now; 139 : ENSURE(QueryPerformanceCounter(&now)); 140 : t = static_cast<double>(now.QuadPart - start.QuadPart) * resolution; 141 : #elif HAVE_CLOCK_GETTIME 142 57890 : ENSURE(start.tv_sec || start.tv_nsec); // must have called timer_LatchStartTime first 143 57890 : struct timespec cur; 144 57890 : ENSURE(clock_gettime(CLOCK_MONOTONIC, &cur) == 0); 145 57888 : t = (cur.tv_sec - start.tv_sec) + (cur.tv_nsec - start.tv_nsec) * 1e-9; 146 : #elif HAVE_GETTIMEOFDAY 147 : ENSURE(start.tv_sec || start.tv_usec); // must have called timer_LatchStartTime first 148 : struct timeval cur; 149 : ENSURE(gettimeofday(&cur, 0) == 0); 150 : t = (cur.tv_sec - start.tv_sec) + (cur.tv_usec - start.tv_usec) * 1e-6; 151 : #else 152 : # error "timer_Time: add timer implementation for this platform!" 153 : #endif 154 : 155 57888 : EnsureMonotonic(t); 156 57899 : return t; 157 : } 158 : 159 : 160 0 : double timer_Resolution() 161 : { 162 0 : return resolution; 163 : }