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