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 |
| 12 | #include <time.h> // clock_gettime, CLOCK_MONOTONIC and CLOCK_REALTIME |
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>) |
Mara Sophie Grosch | 1bd707a | 2020-05-07 12:10:33 -0400 | [diff] [blame] | 16 | #include <unistd.h> |
| 17 | #endif |
| 18 | |
| 19 | #if !defined(__APPLE__) && _POSIX_TIMERS > 0 |
Eric Fiselier | 7eba47e | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 20 | #define _LIBCPP_USE_CLOCK_GETTIME |
Mara Sophie Grosch | 1bd707a | 2020-05-07 12:10:33 -0400 | [diff] [blame] | 21 | #endif |
Bruno Cardoso Lopes | d002714 | 2017-01-09 19:21:48 +0000 | [diff] [blame] | 22 | |
Saleem Abdulrasool | ee302e4 | 2017-01-03 21:53:51 +0000 | [diff] [blame] | 23 | #if defined(_LIBCPP_WIN32API) |
Saleem Abdulrasool | 594b042 | 2017-01-01 20:20:41 +0000 | [diff] [blame] | 24 | #define WIN32_LEAN_AND_MEAN |
| 25 | #define VC_EXTRA_LEAN |
Eric Fiselier | bb999f9 | 2017-05-31 22:14:05 +0000 | [diff] [blame] | 26 | #include <windows.h> |
Saleem Abdulrasool | 594b042 | 2017-01-01 20:20:41 +0000 | [diff] [blame] | 27 | #if _WIN32_WINNT >= _WIN32_WINNT_WIN8 |
| 28 | #include <winapifamily.h> |
| 29 | #endif |
| 30 | #else |
Eric Fiselier | 7eba47e | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 31 | #if !defined(CLOCK_REALTIME) || !defined(_LIBCPP_USE_CLOCK_GETTIME) |
Louis Dionne | c420ced | 2020-05-04 11:39:16 -0400 | [diff] [blame] | 32 | #include <sys/time.h> // for gettimeofday and timeval |
| 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 | |
Saleem Abdulrasool | ae24992 | 2017-01-01 20:20:43 +0000 | [diff] [blame] | 36 | #if !defined(_LIBCPP_HAS_NO_MONOTONIC_CLOCK) |
Ed Schouten | f7805c3 | 2015-05-14 20:54:18 +0000 | [diff] [blame] | 37 | #if __APPLE__ |
| 38 | #include <mach/mach_time.h> // mach_absolute_time, mach_timebase_info_data_t |
Saleem Abdulrasool | ee302e4 | 2017-01-03 21:53:51 +0000 | [diff] [blame] | 39 | #elif !defined(_LIBCPP_WIN32API) && !defined(CLOCK_MONOTONIC) |
Ed Schouten | f7805c3 | 2015-05-14 20:54:18 +0000 | [diff] [blame] | 40 | #error "Monotonic clock not implemented" |
| 41 | #endif |
| 42 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 43 | |
Michał Górny | 8d676fb | 2019-12-02 11:49:20 +0100 | [diff] [blame] | 44 | #if defined(__ELF__) && defined(_LIBCPP_LINK_RT_LIB) |
Petr Hosek | 99575aa | 2019-05-30 01:34:41 +0000 | [diff] [blame] | 45 | #pragma comment(lib, "rt") |
| 46 | #endif |
| 47 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 48 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 49 | |
| 50 | namespace chrono |
| 51 | { |
| 52 | |
| 53 | // system_clock |
| 54 | |
Howard Hinnant | 2c45cb4 | 2012-12-12 21:14:28 +0000 | [diff] [blame] | 55 | const bool system_clock::is_steady; |
| 56 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 57 | system_clock::time_point |
Howard Hinnant | aa54ac4 | 2011-05-28 18:34:36 +0000 | [diff] [blame] | 58 | system_clock::now() _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 59 | { |
Saleem Abdulrasool | ee302e4 | 2017-01-03 21:53:51 +0000 | [diff] [blame] | 60 | #if defined(_LIBCPP_WIN32API) |
Saleem Abdulrasool | 9e36770 | 2017-01-01 22:04:38 +0000 | [diff] [blame] | 61 | // FILETIME is in 100ns units |
| 62 | using filetime_duration = |
| 63 | _VSTD::chrono::duration<__int64, |
| 64 | _VSTD::ratio_multiply<_VSTD::ratio<100, 1>, |
| 65 | nanoseconds::period>>; |
Saleem Abdulrasool | 594b042 | 2017-01-01 20:20:41 +0000 | [diff] [blame] | 66 | |
Saleem Abdulrasool | 9e36770 | 2017-01-01 22:04:38 +0000 | [diff] [blame] | 67 | // 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] | 68 | static _LIBCPP_CONSTEXPR const seconds nt_to_unix_epoch{11644473600}; |
Saleem Abdulrasool | 9e36770 | 2017-01-01 22:04:38 +0000 | [diff] [blame] | 69 | |
| 70 | FILETIME ft; |
Saleem Abdulrasool | 594b042 | 2017-01-01 20:20:41 +0000 | [diff] [blame] | 71 | #if _WIN32_WINNT >= _WIN32_WINNT_WIN8 |
| 72 | #if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP) |
Saleem Abdulrasool | 9e36770 | 2017-01-01 22:04:38 +0000 | [diff] [blame] | 73 | GetSystemTimePreciseAsFileTime(&ft); |
Saleem Abdulrasool | 594b042 | 2017-01-01 20:20:41 +0000 | [diff] [blame] | 74 | #else |
Saleem Abdulrasool | 9e36770 | 2017-01-01 22:04:38 +0000 | [diff] [blame] | 75 | GetSystemTimeAsFileTime(&ft); |
Saleem Abdulrasool | 594b042 | 2017-01-01 20:20:41 +0000 | [diff] [blame] | 76 | #endif |
| 77 | #else |
Saleem Abdulrasool | 9e36770 | 2017-01-01 22:04:38 +0000 | [diff] [blame] | 78 | GetSystemTimeAsFileTime(&ft); |
Saleem Abdulrasool | 594b042 | 2017-01-01 20:20:41 +0000 | [diff] [blame] | 79 | #endif |
Saleem Abdulrasool | 9e36770 | 2017-01-01 22:04:38 +0000 | [diff] [blame] | 80 | |
| 81 | filetime_duration d{(static_cast<__int64>(ft.dwHighDateTime) << 32) | |
| 82 | static_cast<__int64>(ft.dwLowDateTime)}; |
| 83 | return time_point(duration_cast<duration>(d - nt_to_unix_epoch)); |
Saleem Abdulrasool | 594b042 | 2017-01-01 20:20:41 +0000 | [diff] [blame] | 84 | #else |
Eric Fiselier | 7eba47e | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 85 | #if defined(_LIBCPP_USE_CLOCK_GETTIME) && defined(CLOCK_REALTIME) |
| 86 | struct timespec tp; |
| 87 | if (0 != clock_gettime(CLOCK_REALTIME, &tp)) |
| 88 | __throw_system_error(errno, "clock_gettime(CLOCK_REALTIME) failed"); |
| 89 | 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] | 90 | #else |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 91 | timeval tv; |
| 92 | gettimeofday(&tv, 0); |
| 93 | return time_point(seconds(tv.tv_sec) + microseconds(tv.tv_usec)); |
Eric Fiselier | 7eba47e | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 94 | #endif // _LIBCPP_USE_CLOCK_GETTIME && CLOCK_REALTIME |
Saleem Abdulrasool | 594b042 | 2017-01-01 20:20:41 +0000 | [diff] [blame] | 95 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | time_t |
Howard Hinnant | aa54ac4 | 2011-05-28 18:34:36 +0000 | [diff] [blame] | 99 | system_clock::to_time_t(const time_point& t) _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 100 | { |
| 101 | return time_t(duration_cast<seconds>(t.time_since_epoch()).count()); |
| 102 | } |
| 103 | |
| 104 | system_clock::time_point |
Howard Hinnant | aa54ac4 | 2011-05-28 18:34:36 +0000 | [diff] [blame] | 105 | system_clock::from_time_t(time_t t) _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 106 | { |
| 107 | return system_clock::time_point(seconds(t)); |
| 108 | } |
| 109 | |
Jonathan Roelofs | cce96eb | 2014-09-02 21:14:38 +0000 | [diff] [blame] | 110 | #ifndef _LIBCPP_HAS_NO_MONOTONIC_CLOCK |
Howard Hinnant | c8dbd22 | 2010-11-20 19:16:30 +0000 | [diff] [blame] | 111 | // steady_clock |
Ed Schouten | f7805c3 | 2015-05-14 20:54:18 +0000 | [diff] [blame] | 112 | // |
| 113 | // Warning: If this is not truly steady, then it is non-conforming. It is |
| 114 | // better for it to not exist and have the rest of libc++ use system_clock |
| 115 | // instead. |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 116 | |
Howard Hinnant | 2c45cb4 | 2012-12-12 21:14:28 +0000 | [diff] [blame] | 117 | const bool steady_clock::is_steady; |
| 118 | |
Saleem Abdulrasool | ae24992 | 2017-01-01 20:20:43 +0000 | [diff] [blame] | 119 | #if defined(__APPLE__) |
Ed Schouten | f7805c3 | 2015-05-14 20:54:18 +0000 | [diff] [blame] | 120 | |
Louis Dionne | a2dac17 | 2020-02-10 18:30:43 +0100 | [diff] [blame] | 121 | // Darwin libc versions >= 1133 provide ns precision via CLOCK_MONOTONIC_RAW |
| 122 | #if defined(_LIBCPP_USE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC_RAW) |
Bruno Cardoso Lopes | d002714 | 2017-01-09 19:21:48 +0000 | [diff] [blame] | 123 | steady_clock::time_point |
| 124 | steady_clock::now() _NOEXCEPT |
| 125 | { |
| 126 | struct timespec tp; |
Louis Dionne | a2dac17 | 2020-02-10 18:30:43 +0100 | [diff] [blame] | 127 | if (0 != clock_gettime(CLOCK_MONOTONIC_RAW, &tp)) |
| 128 | __throw_system_error(errno, "clock_gettime(CLOCK_MONOTONIC_RAW) failed"); |
Bruno Cardoso Lopes | d002714 | 2017-01-09 19:21:48 +0000 | [diff] [blame] | 129 | return time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec)); |
| 130 | } |
| 131 | |
| 132 | #else |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 133 | // mach_absolute_time() * MachInfo.numer / MachInfo.denom is the number of |
| 134 | // nanoseconds since the computer booted up. MachInfo.numer and MachInfo.denom |
| 135 | // are run time constants supplied by the OS. This clock has no relationship |
| 136 | // to the Gregorian calendar. It's main use is as a high resolution timer. |
| 137 | |
| 138 | // MachInfo.numer / MachInfo.denom is often 1 on the latest equipment. Specialize |
| 139 | // for that case as an optimization. |
| 140 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 141 | static |
Howard Hinnant | c8dbd22 | 2010-11-20 19:16:30 +0000 | [diff] [blame] | 142 | steady_clock::rep |
| 143 | steady_simplified() |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 144 | { |
Howard Hinnant | 28b2488 | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 145 | return static_cast<steady_clock::rep>(mach_absolute_time()); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | static |
| 149 | double |
Howard Hinnant | c8dbd22 | 2010-11-20 19:16:30 +0000 | [diff] [blame] | 150 | compute_steady_factor() |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 151 | { |
| 152 | mach_timebase_info_data_t MachInfo; |
| 153 | mach_timebase_info(&MachInfo); |
| 154 | return static_cast<double>(MachInfo.numer) / MachInfo.denom; |
| 155 | } |
| 156 | |
| 157 | static |
Howard Hinnant | c8dbd22 | 2010-11-20 19:16:30 +0000 | [diff] [blame] | 158 | steady_clock::rep |
| 159 | steady_full() |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 160 | { |
Howard Hinnant | c8dbd22 | 2010-11-20 19:16:30 +0000 | [diff] [blame] | 161 | static const double factor = compute_steady_factor(); |
| 162 | return static_cast<steady_clock::rep>(mach_absolute_time() * factor); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Howard Hinnant | c8dbd22 | 2010-11-20 19:16:30 +0000 | [diff] [blame] | 165 | typedef steady_clock::rep (*FP)(); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 166 | |
| 167 | static |
| 168 | FP |
Howard Hinnant | c8dbd22 | 2010-11-20 19:16:30 +0000 | [diff] [blame] | 169 | init_steady_clock() |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 170 | { |
| 171 | mach_timebase_info_data_t MachInfo; |
| 172 | mach_timebase_info(&MachInfo); |
| 173 | if (MachInfo.numer == MachInfo.denom) |
Howard Hinnant | c8dbd22 | 2010-11-20 19:16:30 +0000 | [diff] [blame] | 174 | return &steady_simplified; |
| 175 | return &steady_full; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 176 | } |
| 177 | |
Howard Hinnant | c8dbd22 | 2010-11-20 19:16:30 +0000 | [diff] [blame] | 178 | steady_clock::time_point |
Howard Hinnant | aa54ac4 | 2011-05-28 18:34:36 +0000 | [diff] [blame] | 179 | steady_clock::now() _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 180 | { |
Howard Hinnant | c8dbd22 | 2010-11-20 19:16:30 +0000 | [diff] [blame] | 181 | static FP fp = init_steady_clock(); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 182 | return time_point(duration(fp())); |
| 183 | } |
Louis Dionne | a2dac17 | 2020-02-10 18:30:43 +0100 | [diff] [blame] | 184 | #endif // defined(_LIBCPP_USE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC_RAW) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 185 | |
Saleem Abdulrasool | ee302e4 | 2017-01-03 21:53:51 +0000 | [diff] [blame] | 186 | #elif defined(_LIBCPP_WIN32API) |
Saleem Abdulrasool | ae24992 | 2017-01-01 20:20:43 +0000 | [diff] [blame] | 187 | |
Marshall Clow | 132ccac | 2019-04-01 17:23:30 +0000 | [diff] [blame] | 188 | // https://msdn.microsoft.com/en-us/library/windows/desktop/ms644905(v=vs.85).aspx says: |
| 189 | // If the function fails, the return value is zero. <snip> |
Stephan T. Lavavej | fb39ad7 | 2019-10-23 11:45:36 -0700 | [diff] [blame] | 190 | // 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] | 191 | // and will thus never return zero. |
| 192 | |
| 193 | static LARGE_INTEGER |
| 194 | __QueryPerformanceFrequency() |
| 195 | { |
| 196 | LARGE_INTEGER val; |
| 197 | (void) QueryPerformanceFrequency(&val); |
| 198 | return val; |
| 199 | } |
| 200 | |
Saleem Abdulrasool | ae24992 | 2017-01-01 20:20:43 +0000 | [diff] [blame] | 201 | steady_clock::time_point |
| 202 | steady_clock::now() _NOEXCEPT |
| 203 | { |
Marshall Clow | 132ccac | 2019-04-01 17:23:30 +0000 | [diff] [blame] | 204 | static const LARGE_INTEGER freq = __QueryPerformanceFrequency(); |
Saleem Abdulrasool | ae24992 | 2017-01-01 20:20:43 +0000 | [diff] [blame] | 205 | |
Marshall Clow | 5dbb0d2 | 2019-04-02 14:00:36 +0000 | [diff] [blame] | 206 | LARGE_INTEGER counter; |
| 207 | (void) QueryPerformanceCounter(&counter); |
Saleem Abdulrasool | 91e1d61 | 2017-01-01 22:04:36 +0000 | [diff] [blame] | 208 | return time_point(duration(counter.QuadPart * nano::den / freq.QuadPart)); |
Saleem Abdulrasool | ae24992 | 2017-01-01 20:20:43 +0000 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | #elif defined(CLOCK_MONOTONIC) |
| 212 | |
Louis Dionne | a2dac17 | 2020-02-10 18:30:43 +0100 | [diff] [blame] | 213 | // On Apple platforms only CLOCK_UPTIME_RAW, CLOCK_MONOTONIC_RAW or |
| 214 | // mach_absolute_time are able to time functions in the nanosecond range. |
| 215 | // Thus, they are the only acceptable implementations of steady_clock. |
Bruno Cardoso Lopes | d002714 | 2017-01-09 19:21:48 +0000 | [diff] [blame] | 216 | #ifdef __APPLE__ |
| 217 | #error "Never use CLOCK_MONOTONIC for steady_clock::now on Apple platforms" |
| 218 | #endif |
| 219 | |
Saleem Abdulrasool | ae24992 | 2017-01-01 20:20:43 +0000 | [diff] [blame] | 220 | steady_clock::time_point |
| 221 | steady_clock::now() _NOEXCEPT |
| 222 | { |
| 223 | struct timespec tp; |
| 224 | if (0 != clock_gettime(CLOCK_MONOTONIC, &tp)) |
| 225 | __throw_system_error(errno, "clock_gettime(CLOCK_MONOTONIC) failed"); |
| 226 | return time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec)); |
| 227 | } |
| 228 | |
Ed Schouten | f7805c3 | 2015-05-14 20:54:18 +0000 | [diff] [blame] | 229 | #else |
| 230 | #error "Monotonic clock not implemented" |
| 231 | #endif |
Howard Hinnant | 155c2af | 2010-05-24 17:49:41 +0000 | [diff] [blame] | 232 | |
Jonathan Roelofs | cce96eb | 2014-09-02 21:14:38 +0000 | [diff] [blame] | 233 | #endif // !_LIBCPP_HAS_NO_MONOTONIC_CLOCK |
| 234 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | _LIBCPP_END_NAMESPACE_STD |