13e519524SHoward Hinnant //===------------------------- chrono.cpp ---------------------------------===// 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 93e519524SHoward Hinnant #include "chrono" 104daa9f1eSMarshall Clow #include "cerrno" // errno 114daa9f1eSMarshall Clow #include "system_error" // __throw_system_error 12babd3aefSLouis Dionne #include <time.h> // clock_gettime and CLOCK_{MONOTONIC,REALTIME,MONOTONIC_RAW} 13c55ac105SEric Fiselier #include "include/apple_availability.h" 144f0f708bSEd Schouten 154d25f445SJohn Brawn #if __has_include(<unistd.h>) 1659b31027SMara Sophie Grosch # include <unistd.h> 1759b31027SMara Sophie Grosch #endif 1859b31027SMara Sophie Grosch 19f78bb4d8SHafiz Abid Qadeer #if __has_include(<sys/time.h>) 20f78bb4d8SHafiz Abid Qadeer # include <sys/time.h> // for gettimeofday and timeval 21f78bb4d8SHafiz Abid Qadeer #endif 22f78bb4d8SHafiz Abid Qadeer 2359b31027SMara Sophie Grosch #if !defined(__APPLE__) && _POSIX_TIMERS > 0 24c55ac105SEric Fiselier # define _LIBCPP_USE_CLOCK_GETTIME 2559b31027SMara Sophie Grosch #endif 26e5423739SBruno Cardoso Lopes 27b2826a1dSSaleem Abdulrasool #if defined(_LIBCPP_WIN32API) 286e3a0cc4SSaleem Abdulrasool # define WIN32_LEAN_AND_MEAN 296e3a0cc4SSaleem Abdulrasool # define VC_EXTRA_LEAN 301ec02625SEric Fiselier # include <windows.h> 316e3a0cc4SSaleem Abdulrasool # if _WIN32_WINNT >= _WIN32_WINNT_WIN8 326e3a0cc4SSaleem Abdulrasool # include <winapifamily.h> 336e3a0cc4SSaleem Abdulrasool # endif 34b2826a1dSSaleem Abdulrasool #endif // defined(_LIBCPP_WIN32API) 354f0f708bSEd Schouten 36a9b5fff5SMichał Górny #if defined(__ELF__) && defined(_LIBCPP_LINK_RT_LIB) 37996e62eeSPetr Hosek # pragma comment(lib, "rt") 38996e62eeSPetr Hosek #endif 39996e62eeSPetr Hosek 403e519524SHoward Hinnant _LIBCPP_BEGIN_NAMESPACE_STD 413e519524SHoward Hinnant 423e519524SHoward Hinnant namespace chrono 433e519524SHoward Hinnant { 443e519524SHoward Hinnant 45*2dec36e5SLouis Dionne // 463e519524SHoward Hinnant // system_clock 47*2dec36e5SLouis Dionne // 483e519524SHoward Hinnant 49b2826a1dSSaleem Abdulrasool #if defined(_LIBCPP_WIN32API) 50*2dec36e5SLouis Dionne 51*2dec36e5SLouis Dionne static system_clock::time_point __libcpp_system_clock_now() { 52e91473c3SSaleem Abdulrasool // FILETIME is in 100ns units 53e91473c3SSaleem Abdulrasool using filetime_duration = 54e91473c3SSaleem Abdulrasool _VSTD::chrono::duration<__int64, 55e91473c3SSaleem Abdulrasool _VSTD::ratio_multiply<_VSTD::ratio<100, 1>, 56e91473c3SSaleem Abdulrasool nanoseconds::period>>; 576e3a0cc4SSaleem Abdulrasool 58e91473c3SSaleem Abdulrasool // The Windows epoch is Jan 1 1601, the Unix epoch Jan 1 1970. 59d1c346a4SSaleem Abdulrasool static _LIBCPP_CONSTEXPR const seconds nt_to_unix_epoch{11644473600}; 60e91473c3SSaleem Abdulrasool 61e91473c3SSaleem Abdulrasool FILETIME ft; 62*2dec36e5SLouis Dionne #if _WIN32_WINNT >= _WIN32_WINNT_WIN8 && WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP) 63e91473c3SSaleem Abdulrasool GetSystemTimePreciseAsFileTime(&ft); 646e3a0cc4SSaleem Abdulrasool #else 65e91473c3SSaleem Abdulrasool GetSystemTimeAsFileTime(&ft); 666e3a0cc4SSaleem Abdulrasool #endif 67e91473c3SSaleem Abdulrasool 68e91473c3SSaleem Abdulrasool filetime_duration d{(static_cast<__int64>(ft.dwHighDateTime) << 32) | 69e91473c3SSaleem Abdulrasool static_cast<__int64>(ft.dwLowDateTime)}; 70*2dec36e5SLouis Dionne return system_clock::time_point(duration_cast<system_clock::duration>(d - nt_to_unix_epoch)); 71*2dec36e5SLouis Dionne } 72*2dec36e5SLouis Dionne 73*2dec36e5SLouis Dionne #elif defined(CLOCK_REALTIME) && defined(_LIBCPP_USE_CLOCK_GETTIME) 74*2dec36e5SLouis Dionne 75*2dec36e5SLouis Dionne static system_clock::time_point __libcpp_system_clock_now() { 764f0f708bSEd Schouten struct timespec tp; 774f0f708bSEd Schouten if (0 != clock_gettime(CLOCK_REALTIME, &tp)) 784f0f708bSEd Schouten __throw_system_error(errno, "clock_gettime(CLOCK_REALTIME) failed"); 79*2dec36e5SLouis Dionne return system_clock::time_point(seconds(tp.tv_sec) + microseconds(tp.tv_nsec / 1000)); 80*2dec36e5SLouis Dionne } 81*2dec36e5SLouis Dionne 82e5423739SBruno Cardoso Lopes #else 83*2dec36e5SLouis Dionne 84*2dec36e5SLouis Dionne static system_clock::time_point __libcpp_system_clock_now() { 853e519524SHoward Hinnant timeval tv; 863e519524SHoward Hinnant gettimeofday(&tv, 0); 87*2dec36e5SLouis Dionne return system_clock::time_point(seconds(tv.tv_sec) + microseconds(tv.tv_usec)); 88*2dec36e5SLouis Dionne } 89*2dec36e5SLouis Dionne 90f78bb4d8SHafiz Abid Qadeer #endif 91*2dec36e5SLouis Dionne 92*2dec36e5SLouis Dionne const bool system_clock::is_steady; 93*2dec36e5SLouis Dionne 94*2dec36e5SLouis Dionne system_clock::time_point 95*2dec36e5SLouis Dionne system_clock::now() _NOEXCEPT 96*2dec36e5SLouis Dionne { 97*2dec36e5SLouis Dionne return __libcpp_system_clock_now(); 983e519524SHoward Hinnant } 993e519524SHoward Hinnant 1003e519524SHoward Hinnant time_t 101d53d8152SHoward Hinnant system_clock::to_time_t(const time_point& t) _NOEXCEPT 1023e519524SHoward Hinnant { 1033e519524SHoward Hinnant return time_t(duration_cast<seconds>(t.time_since_epoch()).count()); 1043e519524SHoward Hinnant } 1053e519524SHoward Hinnant 1063e519524SHoward Hinnant system_clock::time_point 107d53d8152SHoward Hinnant system_clock::from_time_t(time_t t) _NOEXCEPT 1083e519524SHoward Hinnant { 1093e519524SHoward Hinnant return system_clock::time_point(seconds(t)); 1103e519524SHoward Hinnant } 1113e519524SHoward Hinnant 112*2dec36e5SLouis Dionne // 1133dc6455fSHoward Hinnant // steady_clock 1144f0f708bSEd Schouten // 1154f0f708bSEd Schouten // Warning: If this is not truly steady, then it is non-conforming. It is 1164f0f708bSEd Schouten // better for it to not exist and have the rest of libc++ use system_clock 1174f0f708bSEd Schouten // instead. 118*2dec36e5SLouis Dionne // 1193e519524SHoward Hinnant 120*2dec36e5SLouis Dionne #ifndef _LIBCPP_HAS_NO_MONOTONIC_CLOCK 12116694b5dSHoward Hinnant 12278f51f95SSaleem Abdulrasool #if defined(__APPLE__) 1234f0f708bSEd Schouten 124babd3aefSLouis Dionne // On Apple platforms, only CLOCK_UPTIME_RAW, CLOCK_MONOTONIC_RAW or 125babd3aefSLouis Dionne // mach_absolute_time are able to time functions in the nanosecond range. 126babd3aefSLouis Dionne // Furthermore, only CLOCK_MONOTONIC_RAW is truly monotonic, because it 127babd3aefSLouis Dionne // also counts cycles when the system is asleep. Thus, it is the only 128babd3aefSLouis Dionne // acceptable implementation of steady_clock. 129*2dec36e5SLouis Dionne static steady_clock::time_point __libcpp_steady_clock_now() { 130e5423739SBruno Cardoso Lopes struct timespec tp; 1318bec8927SLouis Dionne if (0 != clock_gettime(CLOCK_MONOTONIC_RAW, &tp)) 1328bec8927SLouis Dionne __throw_system_error(errno, "clock_gettime(CLOCK_MONOTONIC_RAW) failed"); 133*2dec36e5SLouis Dionne return steady_clock::time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec)); 134e5423739SBruno Cardoso Lopes } 135e5423739SBruno Cardoso Lopes 136b2826a1dSSaleem Abdulrasool #elif defined(_LIBCPP_WIN32API) 13778f51f95SSaleem Abdulrasool 138ecad92b0SMarshall Clow // https://msdn.microsoft.com/en-us/library/windows/desktop/ms644905(v=vs.85).aspx says: 139ecad92b0SMarshall Clow // If the function fails, the return value is zero. <snip> 140ecad92b0SMarshall Clow // On systems that run Windows XP or later, the function will always succeed 141ecad92b0SMarshall Clow // and will thus never return zero. 142ecad92b0SMarshall Clow 143ecad92b0SMarshall Clow static LARGE_INTEGER 144ecad92b0SMarshall Clow __QueryPerformanceFrequency() 145ecad92b0SMarshall Clow { 146ecad92b0SMarshall Clow LARGE_INTEGER val; 147ecad92b0SMarshall Clow (void) QueryPerformanceFrequency(&val); 148ecad92b0SMarshall Clow return val; 149ecad92b0SMarshall Clow } 150ecad92b0SMarshall Clow 151*2dec36e5SLouis Dionne static steady_clock::time_point __libcpp_steady_clock_now() { 152ecad92b0SMarshall Clow static const LARGE_INTEGER freq = __QueryPerformanceFrequency(); 15378f51f95SSaleem Abdulrasool 15468ad5c34SMarshall Clow LARGE_INTEGER counter; 15568ad5c34SMarshall Clow (void) QueryPerformanceCounter(&counter); 156*2dec36e5SLouis Dionne return steady_clock::time_point(steady_clock::duration(counter.QuadPart * nano::den / freq.QuadPart)); 15778f51f95SSaleem Abdulrasool } 15878f51f95SSaleem Abdulrasool 15978f51f95SSaleem Abdulrasool #elif defined(CLOCK_MONOTONIC) 16078f51f95SSaleem Abdulrasool 161*2dec36e5SLouis Dionne static steady_clock::time_point __libcpp_steady_clock_now() { 16278f51f95SSaleem Abdulrasool struct timespec tp; 16378f51f95SSaleem Abdulrasool if (0 != clock_gettime(CLOCK_MONOTONIC, &tp)) 16478f51f95SSaleem Abdulrasool __throw_system_error(errno, "clock_gettime(CLOCK_MONOTONIC) failed"); 165*2dec36e5SLouis Dionne return steady_clock::time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec)); 16678f51f95SSaleem Abdulrasool } 16778f51f95SSaleem Abdulrasool 1684f0f708bSEd Schouten #else 169*2dec36e5SLouis Dionne # error "Monotonic clock not implemented on this platform" 1704f0f708bSEd Schouten #endif 171128ba719SHoward Hinnant 172*2dec36e5SLouis Dionne const bool steady_clock::is_steady; 173*2dec36e5SLouis Dionne 174*2dec36e5SLouis Dionne steady_clock::time_point 175*2dec36e5SLouis Dionne steady_clock::now() _NOEXCEPT 176*2dec36e5SLouis Dionne { 177*2dec36e5SLouis Dionne return __libcpp_steady_clock_now(); 178*2dec36e5SLouis Dionne } 179*2dec36e5SLouis Dionne 180c59e5851SJonathan Roelofs #endif // !_LIBCPP_HAS_NO_MONOTONIC_CLOCK 181c59e5851SJonathan Roelofs 1823e519524SHoward Hinnant } 1833e519524SHoward Hinnant 1843e519524SHoward Hinnant _LIBCPP_END_NAMESPACE_STD 185