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 12*babd3aefSLouis 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 1959b31027SMara Sophie Grosch #if !defined(__APPLE__) && _POSIX_TIMERS > 0 20c55ac105SEric Fiselier #define _LIBCPP_USE_CLOCK_GETTIME 2159b31027SMara Sophie Grosch #endif 22e5423739SBruno Cardoso Lopes 23b2826a1dSSaleem Abdulrasool #if defined(_LIBCPP_WIN32API) 246e3a0cc4SSaleem Abdulrasool # define WIN32_LEAN_AND_MEAN 256e3a0cc4SSaleem Abdulrasool # define VC_EXTRA_LEAN 261ec02625SEric Fiselier # include <windows.h> 276e3a0cc4SSaleem Abdulrasool # if _WIN32_WINNT >= _WIN32_WINNT_WIN8 286e3a0cc4SSaleem Abdulrasool # include <winapifamily.h> 296e3a0cc4SSaleem Abdulrasool # endif 306e3a0cc4SSaleem Abdulrasool #else 31*babd3aefSLouis Dionne # if !defined(CLOCK_REALTIME) 324f0f708bSEd Schouten # include <sys/time.h> // for gettimeofday and timeval 33*babd3aefSLouis Dionne # endif // !defined(CLOCK_REALTIME) 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 453e519524SHoward Hinnant // system_clock 463e519524SHoward Hinnant 4716694b5dSHoward Hinnant const bool system_clock::is_steady; 4816694b5dSHoward Hinnant 493e519524SHoward Hinnant system_clock::time_point 50d53d8152SHoward Hinnant system_clock::now() _NOEXCEPT 513e519524SHoward Hinnant { 52b2826a1dSSaleem Abdulrasool #if defined(_LIBCPP_WIN32API) 53e91473c3SSaleem Abdulrasool // FILETIME is in 100ns units 54e91473c3SSaleem Abdulrasool using filetime_duration = 55e91473c3SSaleem Abdulrasool _VSTD::chrono::duration<__int64, 56e91473c3SSaleem Abdulrasool _VSTD::ratio_multiply<_VSTD::ratio<100, 1>, 57e91473c3SSaleem Abdulrasool nanoseconds::period>>; 586e3a0cc4SSaleem Abdulrasool 59e91473c3SSaleem Abdulrasool // The Windows epoch is Jan 1 1601, the Unix epoch Jan 1 1970. 60d1c346a4SSaleem Abdulrasool static _LIBCPP_CONSTEXPR const seconds nt_to_unix_epoch{11644473600}; 61e91473c3SSaleem Abdulrasool 62e91473c3SSaleem Abdulrasool FILETIME ft; 636e3a0cc4SSaleem Abdulrasool #if _WIN32_WINNT >= _WIN32_WINNT_WIN8 646e3a0cc4SSaleem Abdulrasool #if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP) 65e91473c3SSaleem Abdulrasool GetSystemTimePreciseAsFileTime(&ft); 666e3a0cc4SSaleem Abdulrasool #else 67e91473c3SSaleem Abdulrasool GetSystemTimeAsFileTime(&ft); 686e3a0cc4SSaleem Abdulrasool #endif 696e3a0cc4SSaleem Abdulrasool #else 70e91473c3SSaleem Abdulrasool GetSystemTimeAsFileTime(&ft); 716e3a0cc4SSaleem Abdulrasool #endif 72e91473c3SSaleem Abdulrasool 73e91473c3SSaleem Abdulrasool filetime_duration d{(static_cast<__int64>(ft.dwHighDateTime) << 32) | 74e91473c3SSaleem Abdulrasool static_cast<__int64>(ft.dwLowDateTime)}; 75e91473c3SSaleem Abdulrasool return time_point(duration_cast<duration>(d - nt_to_unix_epoch)); 766e3a0cc4SSaleem Abdulrasool #else 77*babd3aefSLouis Dionne #if defined(CLOCK_REALTIME) 784f0f708bSEd Schouten struct timespec tp; 794f0f708bSEd Schouten if (0 != clock_gettime(CLOCK_REALTIME, &tp)) 804f0f708bSEd Schouten __throw_system_error(errno, "clock_gettime(CLOCK_REALTIME) failed"); 814f0f708bSEd Schouten return time_point(seconds(tp.tv_sec) + microseconds(tp.tv_nsec / 1000)); 82e5423739SBruno Cardoso Lopes #else 833e519524SHoward Hinnant timeval tv; 843e519524SHoward Hinnant gettimeofday(&tv, 0); 853e519524SHoward Hinnant return time_point(seconds(tv.tv_sec) + microseconds(tv.tv_usec)); 86*babd3aefSLouis Dionne #endif // CLOCK_REALTIME 876e3a0cc4SSaleem Abdulrasool #endif 883e519524SHoward Hinnant } 893e519524SHoward Hinnant 903e519524SHoward Hinnant time_t 91d53d8152SHoward Hinnant system_clock::to_time_t(const time_point& t) _NOEXCEPT 923e519524SHoward Hinnant { 933e519524SHoward Hinnant return time_t(duration_cast<seconds>(t.time_since_epoch()).count()); 943e519524SHoward Hinnant } 953e519524SHoward Hinnant 963e519524SHoward Hinnant system_clock::time_point 97d53d8152SHoward Hinnant system_clock::from_time_t(time_t t) _NOEXCEPT 983e519524SHoward Hinnant { 993e519524SHoward Hinnant return system_clock::time_point(seconds(t)); 1003e519524SHoward Hinnant } 1013e519524SHoward Hinnant 102c59e5851SJonathan Roelofs #ifndef _LIBCPP_HAS_NO_MONOTONIC_CLOCK 1033dc6455fSHoward Hinnant // steady_clock 1044f0f708bSEd Schouten // 1054f0f708bSEd Schouten // Warning: If this is not truly steady, then it is non-conforming. It is 1064f0f708bSEd Schouten // better for it to not exist and have the rest of libc++ use system_clock 1074f0f708bSEd Schouten // instead. 1083e519524SHoward Hinnant 10916694b5dSHoward Hinnant const bool steady_clock::is_steady; 11016694b5dSHoward Hinnant 11178f51f95SSaleem Abdulrasool #if defined(__APPLE__) 1124f0f708bSEd Schouten 113*babd3aefSLouis Dionne #if !defined(CLOCK_MONOTONIC_RAW) 114*babd3aefSLouis Dionne # error "Building libc++ on Apple platforms requires CLOCK_MONOTONIC_RAW" 115*babd3aefSLouis Dionne #endif 116*babd3aefSLouis Dionne 117*babd3aefSLouis Dionne // On Apple platforms, only CLOCK_UPTIME_RAW, CLOCK_MONOTONIC_RAW or 118*babd3aefSLouis Dionne // mach_absolute_time are able to time functions in the nanosecond range. 119*babd3aefSLouis Dionne // Furthermore, only CLOCK_MONOTONIC_RAW is truly monotonic, because it 120*babd3aefSLouis Dionne // also counts cycles when the system is asleep. Thus, it is the only 121*babd3aefSLouis Dionne // acceptable implementation of steady_clock. 122e5423739SBruno Cardoso Lopes steady_clock::time_point 123e5423739SBruno Cardoso Lopes steady_clock::now() _NOEXCEPT 124e5423739SBruno Cardoso Lopes { 125e5423739SBruno Cardoso Lopes struct timespec tp; 1268bec8927SLouis Dionne if (0 != clock_gettime(CLOCK_MONOTONIC_RAW, &tp)) 1278bec8927SLouis Dionne __throw_system_error(errno, "clock_gettime(CLOCK_MONOTONIC_RAW) failed"); 128e5423739SBruno Cardoso Lopes return time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec)); 129e5423739SBruno Cardoso Lopes } 130e5423739SBruno Cardoso Lopes 131b2826a1dSSaleem Abdulrasool #elif defined(_LIBCPP_WIN32API) 13278f51f95SSaleem Abdulrasool 133ecad92b0SMarshall Clow // https://msdn.microsoft.com/en-us/library/windows/desktop/ms644905(v=vs.85).aspx says: 134ecad92b0SMarshall Clow // If the function fails, the return value is zero. <snip> 135ecad92b0SMarshall Clow // On systems that run Windows XP or later, the function will always succeed 136ecad92b0SMarshall Clow // and will thus never return zero. 137ecad92b0SMarshall Clow 138ecad92b0SMarshall Clow static LARGE_INTEGER 139ecad92b0SMarshall Clow __QueryPerformanceFrequency() 140ecad92b0SMarshall Clow { 141ecad92b0SMarshall Clow LARGE_INTEGER val; 142ecad92b0SMarshall Clow (void) QueryPerformanceFrequency(&val); 143ecad92b0SMarshall Clow return val; 144ecad92b0SMarshall Clow } 145ecad92b0SMarshall Clow 14678f51f95SSaleem Abdulrasool steady_clock::time_point 14778f51f95SSaleem Abdulrasool steady_clock::now() _NOEXCEPT 14878f51f95SSaleem Abdulrasool { 149ecad92b0SMarshall Clow static const LARGE_INTEGER freq = __QueryPerformanceFrequency(); 15078f51f95SSaleem Abdulrasool 15168ad5c34SMarshall Clow LARGE_INTEGER counter; 15268ad5c34SMarshall Clow (void) QueryPerformanceCounter(&counter); 153cfc01154SSaleem Abdulrasool return time_point(duration(counter.QuadPart * nano::den / freq.QuadPart)); 15478f51f95SSaleem Abdulrasool } 15578f51f95SSaleem Abdulrasool 15678f51f95SSaleem Abdulrasool #elif defined(CLOCK_MONOTONIC) 15778f51f95SSaleem Abdulrasool 15878f51f95SSaleem Abdulrasool steady_clock::time_point 15978f51f95SSaleem Abdulrasool steady_clock::now() _NOEXCEPT 16078f51f95SSaleem Abdulrasool { 16178f51f95SSaleem Abdulrasool struct timespec tp; 16278f51f95SSaleem Abdulrasool if (0 != clock_gettime(CLOCK_MONOTONIC, &tp)) 16378f51f95SSaleem Abdulrasool __throw_system_error(errno, "clock_gettime(CLOCK_MONOTONIC) failed"); 16478f51f95SSaleem Abdulrasool return time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec)); 16578f51f95SSaleem Abdulrasool } 16678f51f95SSaleem Abdulrasool 1674f0f708bSEd Schouten #else 1684f0f708bSEd Schouten # error "Monotonic clock not implemented" 1694f0f708bSEd Schouten #endif 170128ba719SHoward Hinnant 171c59e5851SJonathan Roelofs #endif // !_LIBCPP_HAS_NO_MONOTONIC_CLOCK 172c59e5851SJonathan Roelofs 1733e519524SHoward Hinnant } 1743e519524SHoward Hinnant 1753e519524SHoward Hinnant _LIBCPP_END_NAMESPACE_STD 176