blob: df5cabdb1845743ab1839b773e52e8054a39716b [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001//===------------------------- chrono.cpp ---------------------------------===//
2//
Howard Hinnantc566dc32010-05-11 21:36:01 +00003// The LLVM Compiler Infrastructure
Howard Hinnantc51e1022010-05-11 19:42:16 +00004//
Howard Hinnantee11c312010-11-16 22:09:02 +00005// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantc51e1022010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
Eric Fiselierfc779202016-07-01 23:31:55 +00009
Howard Hinnantc51e1022010-05-11 19:42:16 +000010#include "chrono"
Marshall Clowccb92572015-06-23 14:45:02 +000011#include "cerrno" // errno
12#include "system_error" // __throw_system_error
13#include <time.h> // clock_gettime, CLOCK_MONOTONIC and CLOCK_REALTIME
Ed Schoutenf7805c32015-05-14 20:54:18 +000014
Bruno Cardoso Lopesd0027142017-01-09 19:21:48 +000015#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 Abdulrasoolee302e42017-01-03 21:53:51 +000037#if defined(_LIBCPP_WIN32API)
Saleem Abdulrasool594b0422017-01-01 20:20:41 +000038#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 Schoutenf7805c32015-05-14 20:54:18 +000045#if !defined(CLOCK_REALTIME)
46#include <sys/time.h> // for gettimeofday and timeval
Saleem Abdulrasoolee302e42017-01-03 21:53:51 +000047#endif // !defined(CLOCK_REALTIME)
48#endif // defined(_LIBCPP_WIN32API)
Ed Schoutenf7805c32015-05-14 20:54:18 +000049
Saleem Abdulrasoolae249922017-01-01 20:20:43 +000050#if !defined(_LIBCPP_HAS_NO_MONOTONIC_CLOCK)
Ed Schoutenf7805c32015-05-14 20:54:18 +000051#if __APPLE__
52#include <mach/mach_time.h> // mach_absolute_time, mach_timebase_info_data_t
Saleem Abdulrasoolee302e42017-01-03 21:53:51 +000053#elif !defined(_LIBCPP_WIN32API) && !defined(CLOCK_MONOTONIC)
Ed Schoutenf7805c32015-05-14 20:54:18 +000054#error "Monotonic clock not implemented"
55#endif
56#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +000057
58_LIBCPP_BEGIN_NAMESPACE_STD
59
60namespace chrono
61{
62
63// system_clock
64
Howard Hinnant2c45cb42012-12-12 21:14:28 +000065const bool system_clock::is_steady;
66
Howard Hinnantc51e1022010-05-11 19:42:16 +000067system_clock::time_point
Howard Hinnantaa54ac42011-05-28 18:34:36 +000068system_clock::now() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +000069{
Saleem Abdulrasoolee302e42017-01-03 21:53:51 +000070#if defined(_LIBCPP_WIN32API)
Saleem Abdulrasool9e367702017-01-01 22:04:38 +000071 // 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 Abdulrasool594b0422017-01-01 20:20:41 +000076
Saleem Abdulrasool9e367702017-01-01 22:04:38 +000077 // The Windows epoch is Jan 1 1601, the Unix epoch Jan 1 1970.
Saleem Abdulrasool25c0d402017-01-02 18:41:50 +000078 static _LIBCPP_CONSTEXPR const seconds nt_to_unix_epoch{11644473600};
Saleem Abdulrasool9e367702017-01-01 22:04:38 +000079
80 FILETIME ft;
Saleem Abdulrasool594b0422017-01-01 20:20:41 +000081#if _WIN32_WINNT >= _WIN32_WINNT_WIN8
82#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
Saleem Abdulrasool9e367702017-01-01 22:04:38 +000083 GetSystemTimePreciseAsFileTime(&ft);
Saleem Abdulrasool594b0422017-01-01 20:20:41 +000084#else
Saleem Abdulrasool9e367702017-01-01 22:04:38 +000085 GetSystemTimeAsFileTime(&ft);
Saleem Abdulrasool594b0422017-01-01 20:20:41 +000086#endif
87#else
Saleem Abdulrasool9e367702017-01-01 22:04:38 +000088 GetSystemTimeAsFileTime(&ft);
Saleem Abdulrasool594b0422017-01-01 20:20:41 +000089#endif
Saleem Abdulrasool9e367702017-01-01 22:04:38 +000090
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 Abdulrasool594b0422017-01-01 20:20:41 +000094#else
Bruno Cardoso Lopesd0027142017-01-09 19:21:48 +000095#if defined(_LIBCXX_USE_CLOCK_GETTIME) && defined(CLOCK_REALTIME)
Ed Schoutenf7805c32015-05-14 20:54:18 +000096 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 Lopesd0027142017-01-09 19:21:48 +0000100#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000101 timeval tv;
102 gettimeofday(&tv, 0);
103 return time_point(seconds(tv.tv_sec) + microseconds(tv.tv_usec));
Bruno Cardoso Lopesd0027142017-01-09 19:21:48 +0000104#endif // _LIBCXX_USE_CLOCK_GETTIME && CLOCK_REALTIME
Saleem Abdulrasool594b0422017-01-01 20:20:41 +0000105#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000106}
107
108time_t
Howard Hinnantaa54ac42011-05-28 18:34:36 +0000109system_clock::to_time_t(const time_point& t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000110{
111 return time_t(duration_cast<seconds>(t.time_since_epoch()).count());
112}
113
114system_clock::time_point
Howard Hinnantaa54ac42011-05-28 18:34:36 +0000115system_clock::from_time_t(time_t t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000116{
117 return system_clock::time_point(seconds(t));
118}
119
Jonathan Roelofscce96eb2014-09-02 21:14:38 +0000120#ifndef _LIBCPP_HAS_NO_MONOTONIC_CLOCK
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000121// steady_clock
Ed Schoutenf7805c32015-05-14 20:54:18 +0000122//
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 Hinnantc51e1022010-05-11 19:42:16 +0000126
Howard Hinnant2c45cb42012-12-12 21:14:28 +0000127const bool steady_clock::is_steady;
128
Saleem Abdulrasoolae249922017-01-01 20:20:43 +0000129#if defined(__APPLE__)
Ed Schoutenf7805c32015-05-14 20:54:18 +0000130
Bruno Cardoso Lopesd0027142017-01-09 19:21:48 +0000131// Darwin libc versions >= 1133 provide ns precision via CLOCK_UPTIME_RAW
132#if defined(_LIBCXX_USE_CLOCK_GETTIME) && defined(CLOCK_UPTIME_RAW)
133steady_clock::time_point
134steady_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 Hinnantc51e1022010-05-11 19:42:16 +0000143// 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 Hinnantc51e1022010-05-11 19:42:16 +0000151static
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000152steady_clock::rep
153steady_simplified()
Howard Hinnantc51e1022010-05-11 19:42:16 +0000154{
Howard Hinnant28b24882011-12-01 20:21:04 +0000155 return static_cast<steady_clock::rep>(mach_absolute_time());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000156}
157
158static
159double
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000160compute_steady_factor()
Howard Hinnantc51e1022010-05-11 19:42:16 +0000161{
162 mach_timebase_info_data_t MachInfo;
163 mach_timebase_info(&MachInfo);
164 return static_cast<double>(MachInfo.numer) / MachInfo.denom;
165}
166
167static
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000168steady_clock::rep
169steady_full()
Howard Hinnantc51e1022010-05-11 19:42:16 +0000170{
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000171 static const double factor = compute_steady_factor();
172 return static_cast<steady_clock::rep>(mach_absolute_time() * factor);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000173}
174
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000175typedef steady_clock::rep (*FP)();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000176
177static
178FP
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000179init_steady_clock()
Howard Hinnantc51e1022010-05-11 19:42:16 +0000180{
181 mach_timebase_info_data_t MachInfo;
182 mach_timebase_info(&MachInfo);
183 if (MachInfo.numer == MachInfo.denom)
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000184 return &steady_simplified;
185 return &steady_full;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000186}
187
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000188steady_clock::time_point
Howard Hinnantaa54ac42011-05-28 18:34:36 +0000189steady_clock::now() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000190{
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000191 static FP fp = init_steady_clock();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000192 return time_point(duration(fp()));
193}
Bruno Cardoso Lopesd0027142017-01-09 19:21:48 +0000194#endif // defined(_LIBCXX_USE_CLOCK_GETTIME) && defined(CLOCK_UPTIME_RAW)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000195
Saleem Abdulrasoolee302e42017-01-03 21:53:51 +0000196#elif defined(_LIBCPP_WIN32API)
Saleem Abdulrasoolae249922017-01-01 20:20:43 +0000197
198steady_clock::time_point
199steady_clock::now() _NOEXCEPT
200{
Saleem Abdulrasool91e1d612017-01-01 22:04:36 +0000201 static LARGE_INTEGER freq;
202 static BOOL initialized = FALSE;
203 if (!initialized)
204 initialized = QueryPerformanceFrequency(&freq); // always succceeds
Saleem Abdulrasoolae249922017-01-01 20:20:43 +0000205
Saleem Abdulrasool91e1d612017-01-01 22:04:36 +0000206 LARGE_INTEGER counter;
207 QueryPerformanceCounter(&counter);
208 return time_point(duration(counter.QuadPart * nano::den / freq.QuadPart));
Saleem Abdulrasoolae249922017-01-01 20:20:43 +0000209}
210
211#elif defined(CLOCK_MONOTONIC)
212
Bruno Cardoso Lopesd0027142017-01-09 19:21:48 +0000213// 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 Abdulrasoolae249922017-01-01 20:20:43 +0000220steady_clock::time_point
221steady_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 Schoutenf7805c32015-05-14 20:54:18 +0000229#else
230#error "Monotonic clock not implemented"
231#endif
Howard Hinnant155c2af2010-05-24 17:49:41 +0000232
Jonathan Roelofscce96eb2014-09-02 21:14:38 +0000233#endif // !_LIBCPP_HAS_NO_MONOTONIC_CLOCK
234
Howard Hinnantc51e1022010-05-11 19:42:16 +0000235}
236
237_LIBCPP_END_NAMESPACE_STD