Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | //===------------------------- chrono.cpp ---------------------------------===// |
| 2 | // |
Chandler Carruth | d201210 | 2019-01-19 10:56:40 +0000 | [diff] [blame] | 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 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
Eric Fiselier | fc77920 | 2016-07-01 23:31:55 +0000 | [diff] [blame] | 8 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 9 | #include "chrono" |
Marshall Clow | ccb9257 | 2015-06-23 14:45:02 +0000 | [diff] [blame] | 10 | #include "cerrno" // errno |
| 11 | #include "system_error" // __throw_system_error |
Louis Dionne | 678dc85 | 2020-02-12 17:01:19 +0100 | [diff] [blame] | 12 | #include <time.h> // clock_gettime and CLOCK_{MONOTONIC,REALTIME,MONOTONIC_RAW} |
Eric Fiselier | 7eba47e | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 13 | #include "include/apple_availability.h" |
Ed Schouten | f7805c3 | 2015-05-14 20:54:18 +0000 | [diff] [blame] | 14 | |
John Brawn | feea69a | 2020-05-14 11:51:13 +0100 | [diff] [blame] | 15 | #if __has_include(<unistd.h>) |
Hafiz Abid Qadeer | de28d5f | 2020-10-05 17:28:25 -0400 | [diff] [blame^] | 16 | # include <unistd.h> |
| 17 | #endif |
| 18 | |
| 19 | #if __has_include(<sys/time.h>) |
| 20 | # include <sys/time.h> // for gettimeofday and timeval |
Mara Sophie Grosch | 1bd707a | 2020-05-07 12:10:33 -0400 | [diff] [blame] | 21 | #endif |
| 22 | |
| 23 | #if !defined(__APPLE__) && _POSIX_TIMERS > 0 |
Hafiz Abid Qadeer | de28d5f | 2020-10-05 17:28:25 -0400 | [diff] [blame^] | 24 | # define _LIBCPP_USE_CLOCK_GETTIME |
Mara Sophie Grosch | 1bd707a | 2020-05-07 12:10:33 -0400 | [diff] [blame] | 25 | #endif |
Bruno Cardoso Lopes | d002714 | 2017-01-09 19:21:48 +0000 | [diff] [blame] | 26 | |
Saleem Abdulrasool | ee302e4 | 2017-01-03 21:53:51 +0000 | [diff] [blame] | 27 | #if defined(_LIBCPP_WIN32API) |
Louis Dionne | 678dc85 | 2020-02-12 17:01:19 +0100 | [diff] [blame] | 28 | # define WIN32_LEAN_AND_MEAN |
| 29 | # define VC_EXTRA_LEAN |
| 30 | # include <windows.h> |
| 31 | # if _WIN32_WINNT >= _WIN32_WINNT_WIN8 |
| 32 | # include <winapifamily.h> |
| 33 | # endif |
Saleem Abdulrasool | ee302e4 | 2017-01-03 21:53:51 +0000 | [diff] [blame] | 34 | #endif // defined(_LIBCPP_WIN32API) |
Ed Schouten | f7805c3 | 2015-05-14 20:54:18 +0000 | [diff] [blame] | 35 | |
Michał Górny | 8d676fb | 2019-12-02 11:49:20 +0100 | [diff] [blame] | 36 | #if defined(__ELF__) && defined(_LIBCPP_LINK_RT_LIB) |
Louis Dionne | 678dc85 | 2020-02-12 17:01:19 +0100 | [diff] [blame] | 37 | # pragma comment(lib, "rt") |
Petr Hosek | 99575aa | 2019-05-30 01:34:41 +0000 | [diff] [blame] | 38 | #endif |
| 39 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 40 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 41 | |
| 42 | namespace chrono |
| 43 | { |
| 44 | |
| 45 | // system_clock |
| 46 | |
Howard Hinnant | 2c45cb4 | 2012-12-12 21:14:28 +0000 | [diff] [blame] | 47 | const bool system_clock::is_steady; |
| 48 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 49 | system_clock::time_point |
Howard Hinnant | aa54ac4 | 2011-05-28 18:34:36 +0000 | [diff] [blame] | 50 | system_clock::now() _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 51 | { |
Saleem Abdulrasool | ee302e4 | 2017-01-03 21:53:51 +0000 | [diff] [blame] | 52 | #if defined(_LIBCPP_WIN32API) |
Saleem Abdulrasool | 9e36770 | 2017-01-01 22:04:38 +0000 | [diff] [blame] | 53 | // FILETIME is in 100ns units |
| 54 | using filetime_duration = |
| 55 | _VSTD::chrono::duration<__int64, |
| 56 | _VSTD::ratio_multiply<_VSTD::ratio<100, 1>, |
| 57 | nanoseconds::period>>; |
Saleem Abdulrasool | 594b042 | 2017-01-01 20:20:41 +0000 | [diff] [blame] | 58 | |
Saleem Abdulrasool | 9e36770 | 2017-01-01 22:04:38 +0000 | [diff] [blame] | 59 | // The Windows epoch is Jan 1 1601, the Unix epoch Jan 1 1970. |
Saleem Abdulrasool | 25c0d40 | 2017-01-02 18:41:50 +0000 | [diff] [blame] | 60 | static _LIBCPP_CONSTEXPR const seconds nt_to_unix_epoch{11644473600}; |
Saleem Abdulrasool | 9e36770 | 2017-01-01 22:04:38 +0000 | [diff] [blame] | 61 | |
| 62 | FILETIME ft; |
Saleem Abdulrasool | 594b042 | 2017-01-01 20:20:41 +0000 | [diff] [blame] | 63 | #if _WIN32_WINNT >= _WIN32_WINNT_WIN8 |
| 64 | #if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP) |
Saleem Abdulrasool | 9e36770 | 2017-01-01 22:04:38 +0000 | [diff] [blame] | 65 | GetSystemTimePreciseAsFileTime(&ft); |
Saleem Abdulrasool | 594b042 | 2017-01-01 20:20:41 +0000 | [diff] [blame] | 66 | #else |
Saleem Abdulrasool | 9e36770 | 2017-01-01 22:04:38 +0000 | [diff] [blame] | 67 | GetSystemTimeAsFileTime(&ft); |
Saleem Abdulrasool | 594b042 | 2017-01-01 20:20:41 +0000 | [diff] [blame] | 68 | #endif |
| 69 | #else |
Saleem Abdulrasool | 9e36770 | 2017-01-01 22:04:38 +0000 | [diff] [blame] | 70 | GetSystemTimeAsFileTime(&ft); |
Saleem Abdulrasool | 594b042 | 2017-01-01 20:20:41 +0000 | [diff] [blame] | 71 | #endif |
Saleem Abdulrasool | 9e36770 | 2017-01-01 22:04:38 +0000 | [diff] [blame] | 72 | |
| 73 | filetime_duration d{(static_cast<__int64>(ft.dwHighDateTime) << 32) | |
| 74 | static_cast<__int64>(ft.dwLowDateTime)}; |
| 75 | return time_point(duration_cast<duration>(d - nt_to_unix_epoch)); |
Saleem Abdulrasool | 594b042 | 2017-01-01 20:20:41 +0000 | [diff] [blame] | 76 | #else |
Hafiz Abid Qadeer | de28d5f | 2020-10-05 17:28:25 -0400 | [diff] [blame^] | 77 | #if defined(CLOCK_REALTIME) && defined(_LIBCPP_USE_CLOCK_GETTIME) |
Eric Fiselier | 7eba47e | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 78 | struct timespec tp; |
| 79 | if (0 != clock_gettime(CLOCK_REALTIME, &tp)) |
| 80 | __throw_system_error(errno, "clock_gettime(CLOCK_REALTIME) failed"); |
| 81 | return time_point(seconds(tp.tv_sec) + microseconds(tp.tv_nsec / 1000)); |
Bruno Cardoso Lopes | d002714 | 2017-01-09 19:21:48 +0000 | [diff] [blame] | 82 | #else |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 83 | timeval tv; |
| 84 | gettimeofday(&tv, 0); |
| 85 | return time_point(seconds(tv.tv_sec) + microseconds(tv.tv_usec)); |
Hafiz Abid Qadeer | de28d5f | 2020-10-05 17:28:25 -0400 | [diff] [blame^] | 86 | #endif |
Saleem Abdulrasool | 594b042 | 2017-01-01 20:20:41 +0000 | [diff] [blame] | 87 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | time_t |
Howard Hinnant | aa54ac4 | 2011-05-28 18:34:36 +0000 | [diff] [blame] | 91 | system_clock::to_time_t(const time_point& t) _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 92 | { |
| 93 | return time_t(duration_cast<seconds>(t.time_since_epoch()).count()); |
| 94 | } |
| 95 | |
| 96 | system_clock::time_point |
Howard Hinnant | aa54ac4 | 2011-05-28 18:34:36 +0000 | [diff] [blame] | 97 | system_clock::from_time_t(time_t t) _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 98 | { |
| 99 | return system_clock::time_point(seconds(t)); |
| 100 | } |
| 101 | |
Jonathan Roelofs | cce96eb | 2014-09-02 21:14:38 +0000 | [diff] [blame] | 102 | #ifndef _LIBCPP_HAS_NO_MONOTONIC_CLOCK |
Howard Hinnant | c8dbd22 | 2010-11-20 19:16:30 +0000 | [diff] [blame] | 103 | // steady_clock |
Ed Schouten | f7805c3 | 2015-05-14 20:54:18 +0000 | [diff] [blame] | 104 | // |
| 105 | // Warning: If this is not truly steady, then it is non-conforming. It is |
| 106 | // better for it to not exist and have the rest of libc++ use system_clock |
| 107 | // instead. |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 108 | |
Howard Hinnant | 2c45cb4 | 2012-12-12 21:14:28 +0000 | [diff] [blame] | 109 | const bool steady_clock::is_steady; |
| 110 | |
Saleem Abdulrasool | ae24992 | 2017-01-01 20:20:43 +0000 | [diff] [blame] | 111 | #if defined(__APPLE__) |
Ed Schouten | f7805c3 | 2015-05-14 20:54:18 +0000 | [diff] [blame] | 112 | |
Louis Dionne | 678dc85 | 2020-02-12 17:01:19 +0100 | [diff] [blame] | 113 | #if !defined(CLOCK_MONOTONIC_RAW) |
| 114 | # error "Building libc++ on Apple platforms requires CLOCK_MONOTONIC_RAW" |
| 115 | #endif |
| 116 | |
| 117 | // On Apple platforms, only CLOCK_UPTIME_RAW, CLOCK_MONOTONIC_RAW or |
| 118 | // mach_absolute_time are able to time functions in the nanosecond range. |
| 119 | // Furthermore, only CLOCK_MONOTONIC_RAW is truly monotonic, because it |
| 120 | // also counts cycles when the system is asleep. Thus, it is the only |
| 121 | // acceptable implementation of steady_clock. |
Bruno Cardoso Lopes | d002714 | 2017-01-09 19:21:48 +0000 | [diff] [blame] | 122 | steady_clock::time_point |
| 123 | steady_clock::now() _NOEXCEPT |
| 124 | { |
| 125 | struct timespec tp; |
Louis Dionne | a2dac17 | 2020-02-10 18:30:43 +0100 | [diff] [blame] | 126 | if (0 != clock_gettime(CLOCK_MONOTONIC_RAW, &tp)) |
| 127 | __throw_system_error(errno, "clock_gettime(CLOCK_MONOTONIC_RAW) failed"); |
Bruno Cardoso Lopes | d002714 | 2017-01-09 19:21:48 +0000 | [diff] [blame] | 128 | return time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec)); |
| 129 | } |
| 130 | |
Saleem Abdulrasool | ee302e4 | 2017-01-03 21:53:51 +0000 | [diff] [blame] | 131 | #elif defined(_LIBCPP_WIN32API) |
Saleem Abdulrasool | ae24992 | 2017-01-01 20:20:43 +0000 | [diff] [blame] | 132 | |
Marshall Clow | 132ccac | 2019-04-01 17:23:30 +0000 | [diff] [blame] | 133 | // https://msdn.microsoft.com/en-us/library/windows/desktop/ms644905(v=vs.85).aspx says: |
| 134 | // If the function fails, the return value is zero. <snip> |
Stephan T. Lavavej | fb39ad7 | 2019-10-23 11:45:36 -0700 | [diff] [blame] | 135 | // On systems that run Windows XP or later, the function will always succeed |
Marshall Clow | 132ccac | 2019-04-01 17:23:30 +0000 | [diff] [blame] | 136 | // and will thus never return zero. |
| 137 | |
| 138 | static LARGE_INTEGER |
| 139 | __QueryPerformanceFrequency() |
| 140 | { |
| 141 | LARGE_INTEGER val; |
| 142 | (void) QueryPerformanceFrequency(&val); |
| 143 | return val; |
| 144 | } |
| 145 | |
Saleem Abdulrasool | ae24992 | 2017-01-01 20:20:43 +0000 | [diff] [blame] | 146 | steady_clock::time_point |
| 147 | steady_clock::now() _NOEXCEPT |
| 148 | { |
Marshall Clow | 132ccac | 2019-04-01 17:23:30 +0000 | [diff] [blame] | 149 | static const LARGE_INTEGER freq = __QueryPerformanceFrequency(); |
Saleem Abdulrasool | ae24992 | 2017-01-01 20:20:43 +0000 | [diff] [blame] | 150 | |
Marshall Clow | 5dbb0d2 | 2019-04-02 14:00:36 +0000 | [diff] [blame] | 151 | LARGE_INTEGER counter; |
| 152 | (void) QueryPerformanceCounter(&counter); |
Saleem Abdulrasool | 91e1d61 | 2017-01-01 22:04:36 +0000 | [diff] [blame] | 153 | return time_point(duration(counter.QuadPart * nano::den / freq.QuadPart)); |
Saleem Abdulrasool | ae24992 | 2017-01-01 20:20:43 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | #elif defined(CLOCK_MONOTONIC) |
| 157 | |
| 158 | steady_clock::time_point |
| 159 | steady_clock::now() _NOEXCEPT |
| 160 | { |
| 161 | struct timespec tp; |
| 162 | if (0 != clock_gettime(CLOCK_MONOTONIC, &tp)) |
| 163 | __throw_system_error(errno, "clock_gettime(CLOCK_MONOTONIC) failed"); |
| 164 | return time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec)); |
| 165 | } |
| 166 | |
Ed Schouten | f7805c3 | 2015-05-14 20:54:18 +0000 | [diff] [blame] | 167 | #else |
Louis Dionne | 678dc85 | 2020-02-12 17:01:19 +0100 | [diff] [blame] | 168 | # error "Monotonic clock not implemented" |
Ed Schouten | f7805c3 | 2015-05-14 20:54:18 +0000 | [diff] [blame] | 169 | #endif |
Howard Hinnant | 155c2af | 2010-05-24 17:49:41 +0000 | [diff] [blame] | 170 | |
Jonathan Roelofs | cce96eb | 2014-09-02 21:14:38 +0000 | [diff] [blame] | 171 | #endif // !_LIBCPP_HAS_NO_MONOTONIC_CLOCK |
| 172 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | _LIBCPP_END_NAMESPACE_STD |