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