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