Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | //===------------------------- chrono.cpp ---------------------------------===// |
| 2 | // |
Howard Hinnant | c566dc3 | 2010-05-11 21:36:01 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 4 | // |
Howard Hinnant | ee11c31 | 2010-11-16 22:09:02 +0000 | [diff] [blame] | 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.TXT for details. |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Eric Fiselier | fc77920 | 2016-07-01 23:31:55 +0000 | [diff] [blame] | 9 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 10 | #include "chrono" |
Marshall Clow | ccb9257 | 2015-06-23 14:45:02 +0000 | [diff] [blame] | 11 | #include "cerrno" // errno |
| 12 | #include "system_error" // __throw_system_error |
| 13 | #include <time.h> // clock_gettime, CLOCK_MONOTONIC and CLOCK_REALTIME |
Eric Fiselier | 7eba47e | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 14 | #include "include/apple_availability.h" |
Ed Schouten | f7805c3 | 2015-05-14 20:54:18 +0000 | [diff] [blame] | 15 | |
Eric Fiselier | 7eba47e | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 16 | #if !defined(__APPLE__) |
| 17 | #define _LIBCPP_USE_CLOCK_GETTIME |
Bruno Cardoso Lopes | d002714 | 2017-01-09 19:21:48 +0000 | [diff] [blame] | 18 | #endif // __APPLE__ |
| 19 | |
Saleem Abdulrasool | ee302e4 | 2017-01-03 21:53:51 +0000 | [diff] [blame] | 20 | #if defined(_LIBCPP_WIN32API) |
Saleem Abdulrasool | 594b042 | 2017-01-01 20:20:41 +0000 | [diff] [blame] | 21 | #define WIN32_LEAN_AND_MEAN |
| 22 | #define VC_EXTRA_LEAN |
Eric Fiselier | bb999f9 | 2017-05-31 22:14:05 +0000 | [diff] [blame] | 23 | #include <windows.h> |
Saleem Abdulrasool | 594b042 | 2017-01-01 20:20:41 +0000 | [diff] [blame] | 24 | #if _WIN32_WINNT >= _WIN32_WINNT_WIN8 |
| 25 | #include <winapifamily.h> |
| 26 | #endif |
| 27 | #else |
Eric Fiselier | 7eba47e | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 28 | #if !defined(CLOCK_REALTIME) || !defined(_LIBCPP_USE_CLOCK_GETTIME) |
Ed Schouten | f7805c3 | 2015-05-14 20:54:18 +0000 | [diff] [blame] | 29 | #include <sys/time.h> // for gettimeofday and timeval |
Saleem Abdulrasool | ee302e4 | 2017-01-03 21:53:51 +0000 | [diff] [blame] | 30 | #endif // !defined(CLOCK_REALTIME) |
| 31 | #endif // defined(_LIBCPP_WIN32API) |
Ed Schouten | f7805c3 | 2015-05-14 20:54:18 +0000 | [diff] [blame] | 32 | |
Saleem Abdulrasool | ae24992 | 2017-01-01 20:20:43 +0000 | [diff] [blame] | 33 | #if !defined(_LIBCPP_HAS_NO_MONOTONIC_CLOCK) |
Ed Schouten | f7805c3 | 2015-05-14 20:54:18 +0000 | [diff] [blame] | 34 | #if __APPLE__ |
| 35 | #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] | 36 | #elif !defined(_LIBCPP_WIN32API) && !defined(CLOCK_MONOTONIC) |
Ed Schouten | f7805c3 | 2015-05-14 20:54:18 +0000 | [diff] [blame] | 37 | #error "Monotonic clock not implemented" |
| 38 | #endif |
| 39 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 40 | |
| 41 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 42 | |
| 43 | namespace chrono |
| 44 | { |
| 45 | |
| 46 | // system_clock |
| 47 | |
Howard Hinnant | 2c45cb4 | 2012-12-12 21:14:28 +0000 | [diff] [blame] | 48 | const bool system_clock::is_steady; |
| 49 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 50 | system_clock::time_point |
Howard Hinnant | aa54ac4 | 2011-05-28 18:34:36 +0000 | [diff] [blame] | 51 | system_clock::now() _NOEXCEPT |
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) |
Saleem Abdulrasool | 9e36770 | 2017-01-01 22:04:38 +0000 | [diff] [blame] | 54 | // FILETIME is in 100ns units |
| 55 | using filetime_duration = |
| 56 | _VSTD::chrono::duration<__int64, |
| 57 | _VSTD::ratio_multiply<_VSTD::ratio<100, 1>, |
| 58 | nanoseconds::period>>; |
Saleem Abdulrasool | 594b042 | 2017-01-01 20:20:41 +0000 | [diff] [blame] | 59 | |
Saleem Abdulrasool | 9e36770 | 2017-01-01 22:04:38 +0000 | [diff] [blame] | 60 | // 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] | 61 | static _LIBCPP_CONSTEXPR const seconds nt_to_unix_epoch{11644473600}; |
Saleem Abdulrasool | 9e36770 | 2017-01-01 22:04:38 +0000 | [diff] [blame] | 62 | |
| 63 | FILETIME ft; |
Saleem Abdulrasool | 594b042 | 2017-01-01 20:20:41 +0000 | [diff] [blame] | 64 | #if _WIN32_WINNT >= _WIN32_WINNT_WIN8 |
| 65 | #if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP) |
Saleem Abdulrasool | 9e36770 | 2017-01-01 22:04:38 +0000 | [diff] [blame] | 66 | GetSystemTimePreciseAsFileTime(&ft); |
Saleem Abdulrasool | 594b042 | 2017-01-01 20:20:41 +0000 | [diff] [blame] | 67 | #else |
Saleem Abdulrasool | 9e36770 | 2017-01-01 22:04:38 +0000 | [diff] [blame] | 68 | GetSystemTimeAsFileTime(&ft); |
Saleem Abdulrasool | 594b042 | 2017-01-01 20:20:41 +0000 | [diff] [blame] | 69 | #endif |
| 70 | #else |
Saleem Abdulrasool | 9e36770 | 2017-01-01 22:04:38 +0000 | [diff] [blame] | 71 | GetSystemTimeAsFileTime(&ft); |
Saleem Abdulrasool | 594b042 | 2017-01-01 20:20:41 +0000 | [diff] [blame] | 72 | #endif |
Saleem Abdulrasool | 9e36770 | 2017-01-01 22:04:38 +0000 | [diff] [blame] | 73 | |
| 74 | filetime_duration d{(static_cast<__int64>(ft.dwHighDateTime) << 32) | |
| 75 | static_cast<__int64>(ft.dwLowDateTime)}; |
| 76 | return time_point(duration_cast<duration>(d - nt_to_unix_epoch)); |
Saleem Abdulrasool | 594b042 | 2017-01-01 20:20:41 +0000 | [diff] [blame] | 77 | #else |
Eric Fiselier | 7eba47e | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 78 | #if defined(_LIBCPP_USE_CLOCK_GETTIME) && defined(CLOCK_REALTIME) |
| 79 | struct timespec tp; |
| 80 | if (0 != clock_gettime(CLOCK_REALTIME, &tp)) |
| 81 | __throw_system_error(errno, "clock_gettime(CLOCK_REALTIME) failed"); |
| 82 | 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] | 83 | #else |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 84 | timeval tv; |
| 85 | gettimeofday(&tv, 0); |
| 86 | return time_point(seconds(tv.tv_sec) + microseconds(tv.tv_usec)); |
Eric Fiselier | 7eba47e | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 87 | #endif // _LIBCPP_USE_CLOCK_GETTIME && CLOCK_REALTIME |
Saleem Abdulrasool | 594b042 | 2017-01-01 20:20:41 +0000 | [diff] [blame] | 88 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | time_t |
Howard Hinnant | aa54ac4 | 2011-05-28 18:34:36 +0000 | [diff] [blame] | 92 | system_clock::to_time_t(const time_point& t) _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 93 | { |
| 94 | return time_t(duration_cast<seconds>(t.time_since_epoch()).count()); |
| 95 | } |
| 96 | |
| 97 | system_clock::time_point |
Howard Hinnant | aa54ac4 | 2011-05-28 18:34:36 +0000 | [diff] [blame] | 98 | system_clock::from_time_t(time_t t) _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 99 | { |
| 100 | return system_clock::time_point(seconds(t)); |
| 101 | } |
| 102 | |
Jonathan Roelofs | cce96eb | 2014-09-02 21:14:38 +0000 | [diff] [blame] | 103 | #ifndef _LIBCPP_HAS_NO_MONOTONIC_CLOCK |
Howard Hinnant | c8dbd22 | 2010-11-20 19:16:30 +0000 | [diff] [blame] | 104 | // steady_clock |
Ed Schouten | f7805c3 | 2015-05-14 20:54:18 +0000 | [diff] [blame] | 105 | // |
| 106 | // Warning: If this is not truly steady, then it is non-conforming. It is |
| 107 | // better for it to not exist and have the rest of libc++ use system_clock |
| 108 | // instead. |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 109 | |
Howard Hinnant | 2c45cb4 | 2012-12-12 21:14:28 +0000 | [diff] [blame] | 110 | const bool steady_clock::is_steady; |
| 111 | |
Saleem Abdulrasool | ae24992 | 2017-01-01 20:20:43 +0000 | [diff] [blame] | 112 | #if defined(__APPLE__) |
Ed Schouten | f7805c3 | 2015-05-14 20:54:18 +0000 | [diff] [blame] | 113 | |
Bruno Cardoso Lopes | d002714 | 2017-01-09 19:21:48 +0000 | [diff] [blame] | 114 | // Darwin libc versions >= 1133 provide ns precision via CLOCK_UPTIME_RAW |
Eric Fiselier | 7eba47e | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 115 | #if defined(_LIBCPP_USE_CLOCK_GETTIME) && defined(CLOCK_UPTIME_RAW) |
Bruno Cardoso Lopes | d002714 | 2017-01-09 19:21:48 +0000 | [diff] [blame] | 116 | steady_clock::time_point |
| 117 | steady_clock::now() _NOEXCEPT |
| 118 | { |
| 119 | struct timespec tp; |
| 120 | if (0 != clock_gettime(CLOCK_UPTIME_RAW, &tp)) |
| 121 | __throw_system_error(errno, "clock_gettime(CLOCK_UPTIME_RAW) failed"); |
| 122 | return time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec)); |
| 123 | } |
| 124 | |
| 125 | #else |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 126 | // mach_absolute_time() * MachInfo.numer / MachInfo.denom is the number of |
| 127 | // nanoseconds since the computer booted up. MachInfo.numer and MachInfo.denom |
| 128 | // are run time constants supplied by the OS. This clock has no relationship |
| 129 | // to the Gregorian calendar. It's main use is as a high resolution timer. |
| 130 | |
| 131 | // MachInfo.numer / MachInfo.denom is often 1 on the latest equipment. Specialize |
| 132 | // for that case as an optimization. |
| 133 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 134 | static |
Howard Hinnant | c8dbd22 | 2010-11-20 19:16:30 +0000 | [diff] [blame] | 135 | steady_clock::rep |
| 136 | steady_simplified() |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 137 | { |
Howard Hinnant | 28b2488 | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 138 | return static_cast<steady_clock::rep>(mach_absolute_time()); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | static |
| 142 | double |
Howard Hinnant | c8dbd22 | 2010-11-20 19:16:30 +0000 | [diff] [blame] | 143 | compute_steady_factor() |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 144 | { |
| 145 | mach_timebase_info_data_t MachInfo; |
| 146 | mach_timebase_info(&MachInfo); |
| 147 | return static_cast<double>(MachInfo.numer) / MachInfo.denom; |
| 148 | } |
| 149 | |
| 150 | static |
Howard Hinnant | c8dbd22 | 2010-11-20 19:16:30 +0000 | [diff] [blame] | 151 | steady_clock::rep |
| 152 | steady_full() |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 153 | { |
Howard Hinnant | c8dbd22 | 2010-11-20 19:16:30 +0000 | [diff] [blame] | 154 | static const double factor = compute_steady_factor(); |
| 155 | return static_cast<steady_clock::rep>(mach_absolute_time() * factor); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Howard Hinnant | c8dbd22 | 2010-11-20 19:16:30 +0000 | [diff] [blame] | 158 | typedef steady_clock::rep (*FP)(); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 159 | |
| 160 | static |
| 161 | FP |
Howard Hinnant | c8dbd22 | 2010-11-20 19:16:30 +0000 | [diff] [blame] | 162 | init_steady_clock() |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 163 | { |
| 164 | mach_timebase_info_data_t MachInfo; |
| 165 | mach_timebase_info(&MachInfo); |
| 166 | if (MachInfo.numer == MachInfo.denom) |
Howard Hinnant | c8dbd22 | 2010-11-20 19:16:30 +0000 | [diff] [blame] | 167 | return &steady_simplified; |
| 168 | return &steady_full; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Howard Hinnant | c8dbd22 | 2010-11-20 19:16:30 +0000 | [diff] [blame] | 171 | steady_clock::time_point |
Howard Hinnant | aa54ac4 | 2011-05-28 18:34:36 +0000 | [diff] [blame] | 172 | steady_clock::now() _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 173 | { |
Howard Hinnant | c8dbd22 | 2010-11-20 19:16:30 +0000 | [diff] [blame] | 174 | static FP fp = init_steady_clock(); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 175 | return time_point(duration(fp())); |
| 176 | } |
Eric Fiselier | 7eba47e | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 177 | #endif // defined(_LIBCPP_USE_CLOCK_GETTIME) && defined(CLOCK_UPTIME_RAW) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 178 | |
Saleem Abdulrasool | ee302e4 | 2017-01-03 21:53:51 +0000 | [diff] [blame] | 179 | #elif defined(_LIBCPP_WIN32API) |
Saleem Abdulrasool | ae24992 | 2017-01-01 20:20:43 +0000 | [diff] [blame] | 180 | |
| 181 | steady_clock::time_point |
| 182 | steady_clock::now() _NOEXCEPT |
| 183 | { |
Saleem Abdulrasool | 91e1d61 | 2017-01-01 22:04:36 +0000 | [diff] [blame] | 184 | static LARGE_INTEGER freq; |
| 185 | static BOOL initialized = FALSE; |
| 186 | if (!initialized) |
| 187 | initialized = QueryPerformanceFrequency(&freq); // always succceeds |
Saleem Abdulrasool | ae24992 | 2017-01-01 20:20:43 +0000 | [diff] [blame] | 188 | |
Saleem Abdulrasool | 91e1d61 | 2017-01-01 22:04:36 +0000 | [diff] [blame] | 189 | LARGE_INTEGER counter; |
| 190 | QueryPerformanceCounter(&counter); |
| 191 | return time_point(duration(counter.QuadPart * nano::den / freq.QuadPart)); |
Saleem Abdulrasool | ae24992 | 2017-01-01 20:20:43 +0000 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | #elif defined(CLOCK_MONOTONIC) |
| 195 | |
Bruno Cardoso Lopes | d002714 | 2017-01-09 19:21:48 +0000 | [diff] [blame] | 196 | // On Apple platforms only CLOCK_UPTIME_RAW or mach_absolute_time are able to |
| 197 | // time functions in the nanosecond range. Thus, they are the only acceptable |
| 198 | // implementations of steady_clock. |
| 199 | #ifdef __APPLE__ |
| 200 | #error "Never use CLOCK_MONOTONIC for steady_clock::now on Apple platforms" |
| 201 | #endif |
| 202 | |
Saleem Abdulrasool | ae24992 | 2017-01-01 20:20:43 +0000 | [diff] [blame] | 203 | steady_clock::time_point |
| 204 | steady_clock::now() _NOEXCEPT |
| 205 | { |
| 206 | struct timespec tp; |
| 207 | if (0 != clock_gettime(CLOCK_MONOTONIC, &tp)) |
| 208 | __throw_system_error(errno, "clock_gettime(CLOCK_MONOTONIC) failed"); |
| 209 | return time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec)); |
| 210 | } |
| 211 | |
Ed Schouten | f7805c3 | 2015-05-14 20:54:18 +0000 | [diff] [blame] | 212 | #else |
| 213 | #error "Monotonic clock not implemented" |
| 214 | #endif |
Howard Hinnant | 155c2af | 2010-05-24 17:49:41 +0000 | [diff] [blame] | 215 | |
Jonathan Roelofs | cce96eb | 2014-09-02 21:14:38 +0000 | [diff] [blame] | 216 | #endif // !_LIBCPP_HAS_NO_MONOTONIC_CLOCK |
| 217 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | _LIBCPP_END_NAMESPACE_STD |