1 //===----------------------------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #if defined(__MVS__) 10 // As part of monotonic clock support on z/OS we need macro _LARGE_TIME_API 11 // to be defined before any system header to include definition of struct timespec64. 12 #define _LARGE_TIME_API 13 #endif 14 15 #include "chrono" 16 #include "cerrno" // errno 17 #include "system_error" // __throw_system_error 18 19 #if defined(__MVS__) 20 #include <__support/ibm/gettod_zos.h> // gettimeofdayMonotonic 21 #endif 22 23 #include <time.h> // clock_gettime and CLOCK_{MONOTONIC,REALTIME,MONOTONIC_RAW} 24 #include "include/apple_availability.h" 25 26 #if __has_include(<unistd.h>) 27 # include <unistd.h> 28 #endif 29 30 #if __has_include(<sys/time.h>) 31 # include <sys/time.h> // for gettimeofday and timeval 32 #endif 33 34 #if !defined(__APPLE__) && defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0 35 # define _LIBCPP_USE_CLOCK_GETTIME 36 #endif 37 38 #if defined(_LIBCPP_WIN32API) 39 # define WIN32_LEAN_AND_MEAN 40 # define VC_EXTRA_LEAN 41 # include <windows.h> 42 # if _WIN32_WINNT >= _WIN32_WINNT_WIN8 43 # include <winapifamily.h> 44 # endif 45 #endif // defined(_LIBCPP_WIN32API) 46 47 #if __has_include(<mach/mach_time.h>) 48 # include <mach/mach_time.h> 49 #endif 50 51 #if defined(__ELF__) && defined(_LIBCPP_LINK_RT_LIB) 52 # pragma comment(lib, "rt") 53 #endif 54 55 _LIBCPP_BEGIN_NAMESPACE_STD 56 57 namespace chrono 58 { 59 60 // 61 // system_clock 62 // 63 64 #if defined(_LIBCPP_WIN32API) 65 66 #if _WIN32_WINNT < _WIN32_WINNT_WIN8 67 68 namespace { 69 70 typedef void(WINAPI *GetSystemTimeAsFileTimePtr)(LPFILETIME); 71 72 class GetSystemTimeInit { 73 public: 74 GetSystemTimeInit() { 75 fp = (GetSystemTimeAsFileTimePtr)GetProcAddress( 76 GetModuleHandleW(L"kernel32.dll"), "GetSystemTimePreciseAsFileTime"); 77 if (fp == nullptr) 78 fp = GetSystemTimeAsFileTime; 79 } 80 GetSystemTimeAsFileTimePtr fp; 81 }; 82 83 # 83 "chrono.cpp" 1 3 84 GetSystemTimeInit GetSystemTimeAsFileTimeFunc _LIBCPP_INIT_PRIORITY_MAX; 85 # 85 "chrono.cpp" 2 86 } // namespace 87 88 #endif 89 90 static system_clock::time_point __libcpp_system_clock_now() { 91 // FILETIME is in 100ns units 92 using filetime_duration = 93 _VSTD::chrono::duration<__int64, 94 _VSTD::ratio_multiply<_VSTD::ratio<100, 1>, 95 nanoseconds::period>>; 96 97 // The Windows epoch is Jan 1 1601, the Unix epoch Jan 1 1970. 98 static _LIBCPP_CONSTEXPR const seconds nt_to_unix_epoch{11644473600}; 99 100 FILETIME ft; 101 #if (_WIN32_WINNT >= _WIN32_WINNT_WIN8 && WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)) || \ 102 (_WIN32_WINNT >= _WIN32_WINNT_WIN10) 103 GetSystemTimePreciseAsFileTime(&ft); 104 #elif !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP) 105 GetSystemTimeAsFileTime(&ft); 106 #else 107 GetSystemTimeAsFileTimeFunc.fp(&ft); 108 #endif 109 110 filetime_duration d{(static_cast<__int64>(ft.dwHighDateTime) << 32) | 111 static_cast<__int64>(ft.dwLowDateTime)}; 112 return system_clock::time_point(duration_cast<system_clock::duration>(d - nt_to_unix_epoch)); 113 } 114 115 #elif defined(CLOCK_REALTIME) && defined(_LIBCPP_USE_CLOCK_GETTIME) 116 117 static system_clock::time_point __libcpp_system_clock_now() { 118 struct timespec tp; 119 if (0 != clock_gettime(CLOCK_REALTIME, &tp)) 120 __throw_system_error(errno, "clock_gettime(CLOCK_REALTIME) failed"); 121 return system_clock::time_point(seconds(tp.tv_sec) + microseconds(tp.tv_nsec / 1000)); 122 } 123 124 #else 125 126 static system_clock::time_point __libcpp_system_clock_now() { 127 timeval tv; 128 gettimeofday(&tv, 0); 129 return system_clock::time_point(seconds(tv.tv_sec) + microseconds(tv.tv_usec)); 130 } 131 132 #endif 133 134 const bool system_clock::is_steady; 135 136 system_clock::time_point 137 system_clock::now() noexcept 138 { 139 return __libcpp_system_clock_now(); 140 } 141 142 time_t 143 system_clock::to_time_t(const time_point& t) noexcept 144 { 145 return time_t(duration_cast<seconds>(t.time_since_epoch()).count()); 146 } 147 148 system_clock::time_point 149 system_clock::from_time_t(time_t t) noexcept 150 { 151 return system_clock::time_point(seconds(t)); 152 } 153 154 // 155 // steady_clock 156 // 157 // Warning: If this is not truly steady, then it is non-conforming. It is 158 // better for it to not exist and have the rest of libc++ use system_clock 159 // instead. 160 // 161 162 #ifndef _LIBCPP_HAS_NO_MONOTONIC_CLOCK 163 164 #if defined(__APPLE__) 165 166 // TODO(ldionne): 167 // This old implementation of steady_clock is retained until Chrome drops supports 168 // for macOS < 10.12. The issue is that they link libc++ statically into their 169 // application, which means that libc++ must support being built for such deployment 170 // targets. See https://llvm.org/D74489 for details. 171 #if (defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 101200) || \ 172 (defined(__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__ < 100000) || \ 173 (defined(__ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__ < 100000) || \ 174 (defined(__ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__ < 30000) 175 # define _LIBCPP_USE_OLD_MACH_ABSOLUTE_TIME 176 #endif 177 178 #if defined(_LIBCPP_USE_OLD_MACH_ABSOLUTE_TIME) 179 180 // mach_absolute_time() * MachInfo.numer / MachInfo.denom is the number of 181 // nanoseconds since the computer booted up. MachInfo.numer and MachInfo.denom 182 // are run time constants supplied by the OS. This clock has no relationship 183 // to the Gregorian calendar. It's main use is as a high resolution timer. 184 185 // MachInfo.numer / MachInfo.denom is often 1 on the latest equipment. Specialize 186 // for that case as an optimization. 187 188 static steady_clock::rep steady_simplified() { 189 return static_cast<steady_clock::rep>(mach_absolute_time()); 190 } 191 static double compute_steady_factor() { 192 mach_timebase_info_data_t MachInfo; 193 mach_timebase_info(&MachInfo); 194 return static_cast<double>(MachInfo.numer) / MachInfo.denom; 195 } 196 197 static steady_clock::rep steady_full() { 198 static const double factor = compute_steady_factor(); 199 return static_cast<steady_clock::rep>(mach_absolute_time() * factor); 200 } 201 202 typedef steady_clock::rep (*FP)(); 203 204 static FP init_steady_clock() { 205 mach_timebase_info_data_t MachInfo; 206 mach_timebase_info(&MachInfo); 207 if (MachInfo.numer == MachInfo.denom) 208 return &steady_simplified; 209 return &steady_full; 210 } 211 212 static steady_clock::time_point __libcpp_steady_clock_now() { 213 static FP fp = init_steady_clock(); 214 return steady_clock::time_point(steady_clock::duration(fp())); 215 } 216 217 #else // vvvvv default behavior for Apple platforms vvvvv 218 219 // On Apple platforms, only CLOCK_UPTIME_RAW, CLOCK_MONOTONIC_RAW or 220 // mach_absolute_time are able to time functions in the nanosecond range. 221 // Furthermore, only CLOCK_MONOTONIC_RAW is truly monotonic, because it 222 // also counts cycles when the system is asleep. Thus, it is the only 223 // acceptable implementation of steady_clock. 224 static steady_clock::time_point __libcpp_steady_clock_now() { 225 struct timespec tp; 226 if (0 != clock_gettime(CLOCK_MONOTONIC_RAW, &tp)) 227 __throw_system_error(errno, "clock_gettime(CLOCK_MONOTONIC_RAW) failed"); 228 return steady_clock::time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec)); 229 } 230 231 #endif 232 233 #elif defined(_LIBCPP_WIN32API) 234 235 // https://msdn.microsoft.com/en-us/library/windows/desktop/ms644905(v=vs.85).aspx says: 236 // If the function fails, the return value is zero. <snip> 237 // On systems that run Windows XP or later, the function will always succeed 238 // and will thus never return zero. 239 240 static LARGE_INTEGER 241 __QueryPerformanceFrequency() 242 { 243 LARGE_INTEGER val; 244 (void) QueryPerformanceFrequency(&val); 245 return val; 246 } 247 248 static steady_clock::time_point __libcpp_steady_clock_now() { 249 static const LARGE_INTEGER freq = __QueryPerformanceFrequency(); 250 251 LARGE_INTEGER counter; 252 (void) QueryPerformanceCounter(&counter); 253 auto seconds = counter.QuadPart / freq.QuadPart; 254 auto fractions = counter.QuadPart % freq.QuadPart; 255 auto dur = seconds * nano::den + fractions * nano::den / freq.QuadPart; 256 return steady_clock::time_point(steady_clock::duration(dur)); 257 } 258 259 #elif defined(__MVS__) 260 261 static steady_clock::time_point __libcpp_steady_clock_now() { 262 struct timespec64 ts; 263 if (0 != gettimeofdayMonotonic(&ts)) 264 __throw_system_error(errno, "failed to obtain time of day"); 265 266 return steady_clock::time_point(seconds(ts.tv_sec) + nanoseconds(ts.tv_nsec)); 267 } 268 269 #elif defined(CLOCK_MONOTONIC) 270 271 static steady_clock::time_point __libcpp_steady_clock_now() { 272 struct timespec tp; 273 if (0 != clock_gettime(CLOCK_MONOTONIC, &tp)) 274 __throw_system_error(errno, "clock_gettime(CLOCK_MONOTONIC) failed"); 275 return steady_clock::time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec)); 276 } 277 278 #else 279 # error "Monotonic clock not implemented on this platform" 280 #endif 281 282 const bool steady_clock::is_steady; 283 284 steady_clock::time_point 285 steady_clock::now() noexcept 286 { 287 return __libcpp_steady_clock_now(); 288 } 289 290 #endif // !_LIBCPP_HAS_NO_MONOTONIC_CLOCK 291 292 } 293 294 _LIBCPP_END_NAMESPACE_STD 295