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