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