blob: 882d50b9d7316ea9482f80fd707a6ccd857be89d [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
Eric Fiselier7eba47e2018-07-25 20:51:49 +000014#include "include/apple_availability.h"
Ed Schoutenf7805c32015-05-14 20:54:18 +000015
Eric Fiselier7eba47e2018-07-25 20:51:49 +000016#if !defined(__APPLE__)
17#define _LIBCPP_USE_CLOCK_GETTIME
Bruno Cardoso Lopesd0027142017-01-09 19:21:48 +000018#endif // __APPLE__
19
Saleem Abdulrasoolee302e42017-01-03 21:53:51 +000020#if defined(_LIBCPP_WIN32API)
Saleem Abdulrasool594b0422017-01-01 20:20:41 +000021#define WIN32_LEAN_AND_MEAN
22#define VC_EXTRA_LEAN
Eric Fiselierbb999f92017-05-31 22:14:05 +000023#include <windows.h>
Saleem Abdulrasool594b0422017-01-01 20:20:41 +000024#if _WIN32_WINNT >= _WIN32_WINNT_WIN8
25#include <winapifamily.h>
26#endif
27#else
Eric Fiselier7eba47e2018-07-25 20:51:49 +000028#if !defined(CLOCK_REALTIME) || !defined(_LIBCPP_USE_CLOCK_GETTIME)
Ed Schoutenf7805c32015-05-14 20:54:18 +000029#include <sys/time.h> // for gettimeofday and timeval
Saleem Abdulrasoolee302e42017-01-03 21:53:51 +000030#endif // !defined(CLOCK_REALTIME)
31#endif // defined(_LIBCPP_WIN32API)
Ed Schoutenf7805c32015-05-14 20:54:18 +000032
Saleem Abdulrasoolae249922017-01-01 20:20:43 +000033#if !defined(_LIBCPP_HAS_NO_MONOTONIC_CLOCK)
Ed Schoutenf7805c32015-05-14 20:54:18 +000034#if __APPLE__
35#include <mach/mach_time.h> // mach_absolute_time, mach_timebase_info_data_t
Saleem Abdulrasoolee302e42017-01-03 21:53:51 +000036#elif !defined(_LIBCPP_WIN32API) && !defined(CLOCK_MONOTONIC)
Ed Schoutenf7805c32015-05-14 20:54:18 +000037#error "Monotonic clock not implemented"
38#endif
39#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +000040
41_LIBCPP_BEGIN_NAMESPACE_STD
42
43namespace chrono
44{
45
46// system_clock
47
Howard Hinnant2c45cb42012-12-12 21:14:28 +000048const bool system_clock::is_steady;
49
Howard Hinnantc51e1022010-05-11 19:42:16 +000050system_clock::time_point
Howard Hinnantaa54ac42011-05-28 18:34:36 +000051system_clock::now() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +000052{
Saleem Abdulrasoolee302e42017-01-03 21:53:51 +000053#if defined(_LIBCPP_WIN32API)
Saleem Abdulrasool9e367702017-01-01 22:04:38 +000054 // FILETIME is in 100ns units
55 using filetime_duration =
56 _VSTD::chrono::duration<__int64,
57 _VSTD::ratio_multiply<_VSTD::ratio<100, 1>,
58 nanoseconds::period>>;
Saleem Abdulrasool594b0422017-01-01 20:20:41 +000059
Saleem Abdulrasool9e367702017-01-01 22:04:38 +000060 // The Windows epoch is Jan 1 1601, the Unix epoch Jan 1 1970.
Saleem Abdulrasool25c0d402017-01-02 18:41:50 +000061 static _LIBCPP_CONSTEXPR const seconds nt_to_unix_epoch{11644473600};
Saleem Abdulrasool9e367702017-01-01 22:04:38 +000062
63 FILETIME ft;
Saleem Abdulrasool594b0422017-01-01 20:20:41 +000064#if _WIN32_WINNT >= _WIN32_WINNT_WIN8
65#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
Saleem Abdulrasool9e367702017-01-01 22:04:38 +000066 GetSystemTimePreciseAsFileTime(&ft);
Saleem Abdulrasool594b0422017-01-01 20:20:41 +000067#else
Saleem Abdulrasool9e367702017-01-01 22:04:38 +000068 GetSystemTimeAsFileTime(&ft);
Saleem Abdulrasool594b0422017-01-01 20:20:41 +000069#endif
70#else
Saleem Abdulrasool9e367702017-01-01 22:04:38 +000071 GetSystemTimeAsFileTime(&ft);
Saleem Abdulrasool594b0422017-01-01 20:20:41 +000072#endif
Saleem Abdulrasool9e367702017-01-01 22:04:38 +000073
74 filetime_duration d{(static_cast<__int64>(ft.dwHighDateTime) << 32) |
75 static_cast<__int64>(ft.dwLowDateTime)};
76 return time_point(duration_cast<duration>(d - nt_to_unix_epoch));
Saleem Abdulrasool594b0422017-01-01 20:20:41 +000077#else
Eric Fiselier7eba47e2018-07-25 20:51:49 +000078#if defined(_LIBCPP_USE_CLOCK_GETTIME) && defined(CLOCK_REALTIME)
79 struct timespec tp;
80 if (0 != clock_gettime(CLOCK_REALTIME, &tp))
81 __throw_system_error(errno, "clock_gettime(CLOCK_REALTIME) failed");
82 return time_point(seconds(tp.tv_sec) + microseconds(tp.tv_nsec / 1000));
Bruno Cardoso Lopesd0027142017-01-09 19:21:48 +000083#else
Howard Hinnantc51e1022010-05-11 19:42:16 +000084 timeval tv;
85 gettimeofday(&tv, 0);
86 return time_point(seconds(tv.tv_sec) + microseconds(tv.tv_usec));
Eric Fiselier7eba47e2018-07-25 20:51:49 +000087#endif // _LIBCPP_USE_CLOCK_GETTIME && CLOCK_REALTIME
Saleem Abdulrasool594b0422017-01-01 20:20:41 +000088#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +000089}
90
91time_t
Howard Hinnantaa54ac42011-05-28 18:34:36 +000092system_clock::to_time_t(const time_point& t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +000093{
94 return time_t(duration_cast<seconds>(t.time_since_epoch()).count());
95}
96
97system_clock::time_point
Howard Hinnantaa54ac42011-05-28 18:34:36 +000098system_clock::from_time_t(time_t t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +000099{
100 return system_clock::time_point(seconds(t));
101}
102
Jonathan Roelofscce96eb2014-09-02 21:14:38 +0000103#ifndef _LIBCPP_HAS_NO_MONOTONIC_CLOCK
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000104// steady_clock
Ed Schoutenf7805c32015-05-14 20:54:18 +0000105//
106// Warning: If this is not truly steady, then it is non-conforming. It is
107// better for it to not exist and have the rest of libc++ use system_clock
108// instead.
Howard Hinnantc51e1022010-05-11 19:42:16 +0000109
Howard Hinnant2c45cb42012-12-12 21:14:28 +0000110const bool steady_clock::is_steady;
111
Saleem Abdulrasoolae249922017-01-01 20:20:43 +0000112#if defined(__APPLE__)
Ed Schoutenf7805c32015-05-14 20:54:18 +0000113
Bruno Cardoso Lopesd0027142017-01-09 19:21:48 +0000114// Darwin libc versions >= 1133 provide ns precision via CLOCK_UPTIME_RAW
Eric Fiselier7eba47e2018-07-25 20:51:49 +0000115#if defined(_LIBCPP_USE_CLOCK_GETTIME) && defined(CLOCK_UPTIME_RAW)
Bruno Cardoso Lopesd0027142017-01-09 19:21:48 +0000116steady_clock::time_point
117steady_clock::now() _NOEXCEPT
118{
119 struct timespec tp;
120 if (0 != clock_gettime(CLOCK_UPTIME_RAW, &tp))
121 __throw_system_error(errno, "clock_gettime(CLOCK_UPTIME_RAW) failed");
122 return time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec));
123}
124
125#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000126// mach_absolute_time() * MachInfo.numer / MachInfo.denom is the number of
127// nanoseconds since the computer booted up. MachInfo.numer and MachInfo.denom
128// are run time constants supplied by the OS. This clock has no relationship
129// to the Gregorian calendar. It's main use is as a high resolution timer.
130
131// MachInfo.numer / MachInfo.denom is often 1 on the latest equipment. Specialize
132// for that case as an optimization.
133
Howard Hinnantc51e1022010-05-11 19:42:16 +0000134static
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000135steady_clock::rep
136steady_simplified()
Howard Hinnantc51e1022010-05-11 19:42:16 +0000137{
Howard Hinnant28b24882011-12-01 20:21:04 +0000138 return static_cast<steady_clock::rep>(mach_absolute_time());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000139}
140
141static
142double
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000143compute_steady_factor()
Howard Hinnantc51e1022010-05-11 19:42:16 +0000144{
145 mach_timebase_info_data_t MachInfo;
146 mach_timebase_info(&MachInfo);
147 return static_cast<double>(MachInfo.numer) / MachInfo.denom;
148}
149
150static
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000151steady_clock::rep
152steady_full()
Howard Hinnantc51e1022010-05-11 19:42:16 +0000153{
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000154 static const double factor = compute_steady_factor();
155 return static_cast<steady_clock::rep>(mach_absolute_time() * factor);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000156}
157
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000158typedef steady_clock::rep (*FP)();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000159
160static
161FP
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000162init_steady_clock()
Howard Hinnantc51e1022010-05-11 19:42:16 +0000163{
164 mach_timebase_info_data_t MachInfo;
165 mach_timebase_info(&MachInfo);
166 if (MachInfo.numer == MachInfo.denom)
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000167 return &steady_simplified;
168 return &steady_full;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000169}
170
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000171steady_clock::time_point
Howard Hinnantaa54ac42011-05-28 18:34:36 +0000172steady_clock::now() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000173{
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000174 static FP fp = init_steady_clock();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000175 return time_point(duration(fp()));
176}
Eric Fiselier7eba47e2018-07-25 20:51:49 +0000177#endif // defined(_LIBCPP_USE_CLOCK_GETTIME) && defined(CLOCK_UPTIME_RAW)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000178
Saleem Abdulrasoolee302e42017-01-03 21:53:51 +0000179#elif defined(_LIBCPP_WIN32API)
Saleem Abdulrasoolae249922017-01-01 20:20:43 +0000180
181steady_clock::time_point
182steady_clock::now() _NOEXCEPT
183{
Saleem Abdulrasool91e1d612017-01-01 22:04:36 +0000184 static LARGE_INTEGER freq;
185 static BOOL initialized = FALSE;
186 if (!initialized)
187 initialized = QueryPerformanceFrequency(&freq); // always succceeds
Saleem Abdulrasoolae249922017-01-01 20:20:43 +0000188
Saleem Abdulrasool91e1d612017-01-01 22:04:36 +0000189 LARGE_INTEGER counter;
190 QueryPerformanceCounter(&counter);
191 return time_point(duration(counter.QuadPart * nano::den / freq.QuadPart));
Saleem Abdulrasoolae249922017-01-01 20:20:43 +0000192}
193
194#elif defined(CLOCK_MONOTONIC)
195
Bruno Cardoso Lopesd0027142017-01-09 19:21:48 +0000196// On Apple platforms only CLOCK_UPTIME_RAW or mach_absolute_time are able to
197// time functions in the nanosecond range. Thus, they are the only acceptable
198// implementations of steady_clock.
199#ifdef __APPLE__
200#error "Never use CLOCK_MONOTONIC for steady_clock::now on Apple platforms"
201#endif
202
Saleem Abdulrasoolae249922017-01-01 20:20:43 +0000203steady_clock::time_point
204steady_clock::now() _NOEXCEPT
205{
206 struct timespec tp;
207 if (0 != clock_gettime(CLOCK_MONOTONIC, &tp))
208 __throw_system_error(errno, "clock_gettime(CLOCK_MONOTONIC) failed");
209 return time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec));
210}
211
Ed Schoutenf7805c32015-05-14 20:54:18 +0000212#else
213#error "Monotonic clock not implemented"
214#endif
Howard Hinnant155c2af2010-05-24 17:49:41 +0000215
Jonathan Roelofscce96eb2014-09-02 21:14:38 +0000216#endif // !_LIBCPP_HAS_NO_MONOTONIC_CLOCK
217
Howard Hinnantc51e1022010-05-11 19:42:16 +0000218}
219
220_LIBCPP_END_NAMESPACE_STD