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