1eb8650a7SLouis Dionne //===----------------------------------------------------------------------===// 23e519524SHoward Hinnant // 357b08b09SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 457b08b09SChandler Carruth // See https://llvm.org/LICENSE.txt for license information. 557b08b09SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 63e519524SHoward Hinnant // 73e519524SHoward Hinnant //===----------------------------------------------------------------------===// 8d3ac6ae7SEric Fiselier 9cb2d2ae5SZbigniew Sarbinowski #if defined(__MVS__) 10cb2d2ae5SZbigniew Sarbinowski // As part of monotonic clock support on z/OS we need macro _LARGE_TIME_API 11cb2d2ae5SZbigniew Sarbinowski // to be defined before any system header to include definition of struct timespec64. 12cb2d2ae5SZbigniew Sarbinowski #define _LARGE_TIME_API 13cb2d2ae5SZbigniew Sarbinowski #endif 14cb2d2ae5SZbigniew Sarbinowski 153e519524SHoward Hinnant #include "chrono" 164daa9f1eSMarshall Clow #include "cerrno" // errno 174daa9f1eSMarshall Clow #include "system_error" // __throw_system_error 18cb2d2ae5SZbigniew Sarbinowski 19cb2d2ae5SZbigniew Sarbinowski #if defined(__MVS__) 20cb2d2ae5SZbigniew Sarbinowski #include <__support/ibm/gettod_zos.h> // gettimeofdayMonotonic 21cb2d2ae5SZbigniew Sarbinowski #endif 22cb2d2ae5SZbigniew Sarbinowski 23babd3aefSLouis Dionne #include <time.h> // clock_gettime and CLOCK_{MONOTONIC,REALTIME,MONOTONIC_RAW} 24c55ac105SEric Fiselier #include "include/apple_availability.h" 254f0f708bSEd Schouten 264d25f445SJohn Brawn #if __has_include(<unistd.h>) 2759b31027SMara Sophie Grosch # include <unistd.h> 2859b31027SMara Sophie Grosch #endif 2959b31027SMara Sophie Grosch 30f78bb4d8SHafiz Abid Qadeer #if __has_include(<sys/time.h>) 31f78bb4d8SHafiz Abid Qadeer # include <sys/time.h> // for gettimeofday and timeval 32f78bb4d8SHafiz Abid Qadeer #endif 33f78bb4d8SHafiz Abid Qadeer 34d7550e5dSMartin Storsjö #if !defined(__APPLE__) && defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0 35c55ac105SEric Fiselier # define _LIBCPP_USE_CLOCK_GETTIME 3659b31027SMara Sophie Grosch #endif 37e5423739SBruno Cardoso Lopes 38b2826a1dSSaleem Abdulrasool #if defined(_LIBCPP_WIN32API) 396e3a0cc4SSaleem Abdulrasool # define WIN32_LEAN_AND_MEAN 406e3a0cc4SSaleem Abdulrasool # define VC_EXTRA_LEAN 411ec02625SEric Fiselier # include <windows.h> 426e3a0cc4SSaleem Abdulrasool # if _WIN32_WINNT >= _WIN32_WINNT_WIN8 436e3a0cc4SSaleem Abdulrasool # include <winapifamily.h> 446e3a0cc4SSaleem Abdulrasool # endif 45b2826a1dSSaleem Abdulrasool #endif // defined(_LIBCPP_WIN32API) 464f0f708bSEd Schouten 47*ce167c6fSRoland McGrath #if defined(__Fuchsia__) 48*ce167c6fSRoland McGrath # include <zircon/syscalls.h> 49*ce167c6fSRoland McGrath #endif 50*ce167c6fSRoland McGrath 51faa44078SLouis Dionne #if __has_include(<mach/mach_time.h>) 52faa44078SLouis Dionne # include <mach/mach_time.h> 53faa44078SLouis Dionne #endif 54faa44078SLouis Dionne 55a9b5fff5SMichał Górny #if defined(__ELF__) && defined(_LIBCPP_LINK_RT_LIB) 56996e62eeSPetr Hosek # pragma comment(lib, "rt") 57996e62eeSPetr Hosek #endif 58996e62eeSPetr Hosek 593e519524SHoward Hinnant _LIBCPP_BEGIN_NAMESPACE_STD 603e519524SHoward Hinnant 613e519524SHoward Hinnant namespace chrono 623e519524SHoward Hinnant { 633e519524SHoward Hinnant 642dec36e5SLouis Dionne // 653e519524SHoward Hinnant // system_clock 662dec36e5SLouis Dionne // 673e519524SHoward Hinnant 68b2826a1dSSaleem Abdulrasool #if defined(_LIBCPP_WIN32API) 692dec36e5SLouis Dionne 703ec634e6SSizhe Zhao #if _WIN32_WINNT < _WIN32_WINNT_WIN8 713ec634e6SSizhe Zhao 723ec634e6SSizhe Zhao namespace { 733ec634e6SSizhe Zhao 743ec634e6SSizhe Zhao typedef void(WINAPI *GetSystemTimeAsFileTimePtr)(LPFILETIME); 753ec634e6SSizhe Zhao 763ec634e6SSizhe Zhao class GetSystemTimeInit { 773ec634e6SSizhe Zhao public: 783ec634e6SSizhe Zhao GetSystemTimeInit() { 793ec634e6SSizhe Zhao fp = (GetSystemTimeAsFileTimePtr)GetProcAddress( 803ec634e6SSizhe Zhao GetModuleHandleW(L"kernel32.dll"), "GetSystemTimePreciseAsFileTime"); 813ec634e6SSizhe Zhao if (fp == nullptr) 823ec634e6SSizhe Zhao fp = GetSystemTimeAsFileTime; 833ec634e6SSizhe Zhao } 843ec634e6SSizhe Zhao GetSystemTimeAsFileTimePtr fp; 853ec634e6SSizhe Zhao }; 863ec634e6SSizhe Zhao 87515afe8bSLouis Dionne // Pretend we're inside a system header so the compiler doesn't flag the use of the init_priority 88515afe8bSLouis Dionne // attribute with a value that's reserved for the implementation (we're the implementation). 89515afe8bSLouis Dionne #include "chrono_system_time_init.h" 903ec634e6SSizhe Zhao } // namespace 913ec634e6SSizhe Zhao 923ec634e6SSizhe Zhao #endif 933ec634e6SSizhe Zhao 942dec36e5SLouis Dionne static system_clock::time_point __libcpp_system_clock_now() { 95e91473c3SSaleem Abdulrasool // FILETIME is in 100ns units 96e91473c3SSaleem Abdulrasool using filetime_duration = 97e91473c3SSaleem Abdulrasool _VSTD::chrono::duration<__int64, 98e91473c3SSaleem Abdulrasool _VSTD::ratio_multiply<_VSTD::ratio<100, 1>, 99e91473c3SSaleem Abdulrasool nanoseconds::period>>; 1006e3a0cc4SSaleem Abdulrasool 101e91473c3SSaleem Abdulrasool // The Windows epoch is Jan 1 1601, the Unix epoch Jan 1 1970. 102d1c346a4SSaleem Abdulrasool static _LIBCPP_CONSTEXPR const seconds nt_to_unix_epoch{11644473600}; 103e91473c3SSaleem Abdulrasool 104e91473c3SSaleem Abdulrasool FILETIME ft; 1053ec634e6SSizhe Zhao #if (_WIN32_WINNT >= _WIN32_WINNT_WIN8 && WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)) || \ 1063ec634e6SSizhe Zhao (_WIN32_WINNT >= _WIN32_WINNT_WIN10) 107e91473c3SSaleem Abdulrasool GetSystemTimePreciseAsFileTime(&ft); 1083ec634e6SSizhe Zhao #elif !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP) 109e91473c3SSaleem Abdulrasool GetSystemTimeAsFileTime(&ft); 1103ec634e6SSizhe Zhao #else 1113ec634e6SSizhe Zhao GetSystemTimeAsFileTimeFunc.fp(&ft); 1126e3a0cc4SSaleem Abdulrasool #endif 113e91473c3SSaleem Abdulrasool 114e91473c3SSaleem Abdulrasool filetime_duration d{(static_cast<__int64>(ft.dwHighDateTime) << 32) | 115e91473c3SSaleem Abdulrasool static_cast<__int64>(ft.dwLowDateTime)}; 1162dec36e5SLouis Dionne return system_clock::time_point(duration_cast<system_clock::duration>(d - nt_to_unix_epoch)); 1172dec36e5SLouis Dionne } 1182dec36e5SLouis Dionne 1192dec36e5SLouis Dionne #elif defined(CLOCK_REALTIME) && defined(_LIBCPP_USE_CLOCK_GETTIME) 1202dec36e5SLouis Dionne 1212dec36e5SLouis Dionne static system_clock::time_point __libcpp_system_clock_now() { 1224f0f708bSEd Schouten struct timespec tp; 1234f0f708bSEd Schouten if (0 != clock_gettime(CLOCK_REALTIME, &tp)) 1244f0f708bSEd Schouten __throw_system_error(errno, "clock_gettime(CLOCK_REALTIME) failed"); 1252dec36e5SLouis Dionne return system_clock::time_point(seconds(tp.tv_sec) + microseconds(tp.tv_nsec / 1000)); 1262dec36e5SLouis Dionne } 1272dec36e5SLouis Dionne 128e5423739SBruno Cardoso Lopes #else 1292dec36e5SLouis Dionne 1302dec36e5SLouis Dionne static system_clock::time_point __libcpp_system_clock_now() { 1313e519524SHoward Hinnant timeval tv; 1323e519524SHoward Hinnant gettimeofday(&tv, 0); 1332dec36e5SLouis Dionne return system_clock::time_point(seconds(tv.tv_sec) + microseconds(tv.tv_usec)); 1342dec36e5SLouis Dionne } 1352dec36e5SLouis Dionne 136f78bb4d8SHafiz Abid Qadeer #endif 1372dec36e5SLouis Dionne 1382dec36e5SLouis Dionne const bool system_clock::is_steady; 1392dec36e5SLouis Dionne 1402dec36e5SLouis Dionne system_clock::time_point 1415601305fSLouis Dionne system_clock::now() noexcept 1422dec36e5SLouis Dionne { 1432dec36e5SLouis Dionne return __libcpp_system_clock_now(); 1443e519524SHoward Hinnant } 1453e519524SHoward Hinnant 1463e519524SHoward Hinnant time_t 1475601305fSLouis Dionne system_clock::to_time_t(const time_point& t) noexcept 1483e519524SHoward Hinnant { 1493e519524SHoward Hinnant return time_t(duration_cast<seconds>(t.time_since_epoch()).count()); 1503e519524SHoward Hinnant } 1513e519524SHoward Hinnant 1523e519524SHoward Hinnant system_clock::time_point 1535601305fSLouis Dionne system_clock::from_time_t(time_t t) noexcept 1543e519524SHoward Hinnant { 1553e519524SHoward Hinnant return system_clock::time_point(seconds(t)); 1563e519524SHoward Hinnant } 1573e519524SHoward Hinnant 1582dec36e5SLouis Dionne // 1593dc6455fSHoward Hinnant // steady_clock 1604f0f708bSEd Schouten // 1614f0f708bSEd Schouten // Warning: If this is not truly steady, then it is non-conforming. It is 1624f0f708bSEd Schouten // better for it to not exist and have the rest of libc++ use system_clock 1634f0f708bSEd Schouten // instead. 1642dec36e5SLouis Dionne // 1653e519524SHoward Hinnant 1662dec36e5SLouis Dionne #ifndef _LIBCPP_HAS_NO_MONOTONIC_CLOCK 16716694b5dSHoward Hinnant 16878f51f95SSaleem Abdulrasool #if defined(__APPLE__) 1694f0f708bSEd Schouten 170faa44078SLouis Dionne // TODO(ldionne): 171faa44078SLouis Dionne // This old implementation of steady_clock is retained until Chrome drops supports 172faa44078SLouis Dionne // for macOS < 10.12. The issue is that they link libc++ statically into their 173faa44078SLouis Dionne // application, which means that libc++ must support being built for such deployment 174faa44078SLouis Dionne // targets. See https://llvm.org/D74489 for details. 175faa44078SLouis Dionne #if (defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 101200) || \ 176faa44078SLouis Dionne (defined(__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__ < 100000) || \ 177faa44078SLouis Dionne (defined(__ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__ < 100000) || \ 178faa44078SLouis Dionne (defined(__ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__ < 30000) 179faa44078SLouis Dionne # define _LIBCPP_USE_OLD_MACH_ABSOLUTE_TIME 180faa44078SLouis Dionne #endif 181faa44078SLouis Dionne 182faa44078SLouis Dionne #if defined(_LIBCPP_USE_OLD_MACH_ABSOLUTE_TIME) 183faa44078SLouis Dionne 184faa44078SLouis Dionne // mach_absolute_time() * MachInfo.numer / MachInfo.denom is the number of 185faa44078SLouis Dionne // nanoseconds since the computer booted up. MachInfo.numer and MachInfo.denom 186faa44078SLouis Dionne // are run time constants supplied by the OS. This clock has no relationship 187faa44078SLouis Dionne // to the Gregorian calendar. It's main use is as a high resolution timer. 188faa44078SLouis Dionne 189faa44078SLouis Dionne // MachInfo.numer / MachInfo.denom is often 1 on the latest equipment. Specialize 190faa44078SLouis Dionne // for that case as an optimization. 191faa44078SLouis Dionne 192faa44078SLouis Dionne static steady_clock::rep steady_simplified() { 193faa44078SLouis Dionne return static_cast<steady_clock::rep>(mach_absolute_time()); 194faa44078SLouis Dionne } 195faa44078SLouis Dionne static double compute_steady_factor() { 196faa44078SLouis Dionne mach_timebase_info_data_t MachInfo; 197faa44078SLouis Dionne mach_timebase_info(&MachInfo); 198faa44078SLouis Dionne return static_cast<double>(MachInfo.numer) / MachInfo.denom; 199faa44078SLouis Dionne } 200faa44078SLouis Dionne 201faa44078SLouis Dionne static steady_clock::rep steady_full() { 202faa44078SLouis Dionne static const double factor = compute_steady_factor(); 203faa44078SLouis Dionne return static_cast<steady_clock::rep>(mach_absolute_time() * factor); 204faa44078SLouis Dionne } 205faa44078SLouis Dionne 206faa44078SLouis Dionne typedef steady_clock::rep (*FP)(); 207faa44078SLouis Dionne 208faa44078SLouis Dionne static FP init_steady_clock() { 209faa44078SLouis Dionne mach_timebase_info_data_t MachInfo; 210faa44078SLouis Dionne mach_timebase_info(&MachInfo); 211faa44078SLouis Dionne if (MachInfo.numer == MachInfo.denom) 212faa44078SLouis Dionne return &steady_simplified; 213faa44078SLouis Dionne return &steady_full; 214faa44078SLouis Dionne } 215faa44078SLouis Dionne 216faa44078SLouis Dionne static steady_clock::time_point __libcpp_steady_clock_now() { 217faa44078SLouis Dionne static FP fp = init_steady_clock(); 218faa44078SLouis Dionne return steady_clock::time_point(steady_clock::duration(fp())); 219faa44078SLouis Dionne } 220faa44078SLouis Dionne 221faa44078SLouis Dionne #else // vvvvv default behavior for Apple platforms vvvvv 222faa44078SLouis Dionne 223babd3aefSLouis Dionne // On Apple platforms, only CLOCK_UPTIME_RAW, CLOCK_MONOTONIC_RAW or 224babd3aefSLouis Dionne // mach_absolute_time are able to time functions in the nanosecond range. 225babd3aefSLouis Dionne // Furthermore, only CLOCK_MONOTONIC_RAW is truly monotonic, because it 226babd3aefSLouis Dionne // also counts cycles when the system is asleep. Thus, it is the only 227babd3aefSLouis Dionne // acceptable implementation of steady_clock. 2282dec36e5SLouis Dionne static steady_clock::time_point __libcpp_steady_clock_now() { 229e5423739SBruno Cardoso Lopes struct timespec tp; 2308bec8927SLouis Dionne if (0 != clock_gettime(CLOCK_MONOTONIC_RAW, &tp)) 2318bec8927SLouis Dionne __throw_system_error(errno, "clock_gettime(CLOCK_MONOTONIC_RAW) failed"); 2322dec36e5SLouis Dionne return steady_clock::time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec)); 233e5423739SBruno Cardoso Lopes } 234e5423739SBruno Cardoso Lopes 235faa44078SLouis Dionne #endif 236faa44078SLouis Dionne 237b2826a1dSSaleem Abdulrasool #elif defined(_LIBCPP_WIN32API) 23878f51f95SSaleem Abdulrasool 239ecad92b0SMarshall Clow // https://msdn.microsoft.com/en-us/library/windows/desktop/ms644905(v=vs.85).aspx says: 240ecad92b0SMarshall Clow // If the function fails, the return value is zero. <snip> 241ecad92b0SMarshall Clow // On systems that run Windows XP or later, the function will always succeed 242ecad92b0SMarshall Clow // and will thus never return zero. 243ecad92b0SMarshall Clow 244ecad92b0SMarshall Clow static LARGE_INTEGER 245ecad92b0SMarshall Clow __QueryPerformanceFrequency() 246ecad92b0SMarshall Clow { 247ecad92b0SMarshall Clow LARGE_INTEGER val; 248ecad92b0SMarshall Clow (void) QueryPerformanceFrequency(&val); 249ecad92b0SMarshall Clow return val; 250ecad92b0SMarshall Clow } 251ecad92b0SMarshall Clow 2522dec36e5SLouis Dionne static steady_clock::time_point __libcpp_steady_clock_now() { 253ecad92b0SMarshall Clow static const LARGE_INTEGER freq = __QueryPerformanceFrequency(); 25478f51f95SSaleem Abdulrasool 25568ad5c34SMarshall Clow LARGE_INTEGER counter; 25668ad5c34SMarshall Clow (void) QueryPerformanceCounter(&counter); 25702f1d28eSMartin Storsjö auto seconds = counter.QuadPart / freq.QuadPart; 25802f1d28eSMartin Storsjö auto fractions = counter.QuadPart % freq.QuadPart; 25902f1d28eSMartin Storsjö auto dur = seconds * nano::den + fractions * nano::den / freq.QuadPart; 26002f1d28eSMartin Storsjö return steady_clock::time_point(steady_clock::duration(dur)); 26178f51f95SSaleem Abdulrasool } 26278f51f95SSaleem Abdulrasool 263cb2d2ae5SZbigniew Sarbinowski #elif defined(__MVS__) 264cb2d2ae5SZbigniew Sarbinowski 265cb2d2ae5SZbigniew Sarbinowski static steady_clock::time_point __libcpp_steady_clock_now() { 266cb2d2ae5SZbigniew Sarbinowski struct timespec64 ts; 267cb2d2ae5SZbigniew Sarbinowski if (0 != gettimeofdayMonotonic(&ts)) 268cb2d2ae5SZbigniew Sarbinowski __throw_system_error(errno, "failed to obtain time of day"); 269cb2d2ae5SZbigniew Sarbinowski 270cb2d2ae5SZbigniew Sarbinowski return steady_clock::time_point(seconds(ts.tv_sec) + nanoseconds(ts.tv_nsec)); 271cb2d2ae5SZbigniew Sarbinowski } 272cb2d2ae5SZbigniew Sarbinowski 273*ce167c6fSRoland McGrath # elif defined(__Fuchsia__) 274*ce167c6fSRoland McGrath 275*ce167c6fSRoland McGrath static steady_clock::time_point __libcpp_steady_clock_now() noexcept { 276*ce167c6fSRoland McGrath // Implicitly link against the vDSO system call ABI without 277*ce167c6fSRoland McGrath // requiring the final link to specify -lzircon explicitly when 278*ce167c6fSRoland McGrath // statically linking libc++. 279*ce167c6fSRoland McGrath # pragma comment(lib, "zircon") 280*ce167c6fSRoland McGrath 281*ce167c6fSRoland McGrath return steady_clock::time_point(nanoseconds(_zx_clock_get_monotonic())); 282*ce167c6fSRoland McGrath } 283*ce167c6fSRoland McGrath 28478f51f95SSaleem Abdulrasool # elif defined(CLOCK_MONOTONIC) 28578f51f95SSaleem Abdulrasool 2862dec36e5SLouis Dionne static steady_clock::time_point __libcpp_steady_clock_now() { 28778f51f95SSaleem Abdulrasool struct timespec tp; 28878f51f95SSaleem Abdulrasool if (0 != clock_gettime(CLOCK_MONOTONIC, &tp)) 28978f51f95SSaleem Abdulrasool __throw_system_error(errno, "clock_gettime(CLOCK_MONOTONIC) failed"); 2902dec36e5SLouis Dionne return steady_clock::time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec)); 29178f51f95SSaleem Abdulrasool } 29278f51f95SSaleem Abdulrasool 2934f0f708bSEd Schouten # else 2942dec36e5SLouis Dionne # error "Monotonic clock not implemented on this platform" 2954f0f708bSEd Schouten # endif 296128ba719SHoward Hinnant 2972dec36e5SLouis Dionne const bool steady_clock::is_steady; 2982dec36e5SLouis Dionne 2992dec36e5SLouis Dionne steady_clock::time_point 3005601305fSLouis Dionne steady_clock::now() noexcept 3012dec36e5SLouis Dionne { 3022dec36e5SLouis Dionne return __libcpp_steady_clock_now(); 3032dec36e5SLouis Dionne } 3042dec36e5SLouis Dionne 305c59e5851SJonathan Roelofs #endif // !_LIBCPP_HAS_NO_MONOTONIC_CLOCK 306c59e5851SJonathan Roelofs 3073e519524SHoward Hinnant } 3083e519524SHoward Hinnant 3093e519524SHoward Hinnant _LIBCPP_END_NAMESPACE_STD 310