1*58b9f456SAndroid Build Coastguard Worker //===------------------------- chrono.cpp ---------------------------------===//
2*58b9f456SAndroid Build Coastguard Worker //
3*58b9f456SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure
4*58b9f456SAndroid Build Coastguard Worker //
5*58b9f456SAndroid Build Coastguard Worker // This file is dual licensed under the MIT and the University of Illinois Open
6*58b9f456SAndroid Build Coastguard Worker // Source Licenses. See LICENSE.TXT for details.
7*58b9f456SAndroid Build Coastguard Worker //
8*58b9f456SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*58b9f456SAndroid Build Coastguard Worker
10*58b9f456SAndroid Build Coastguard Worker #include "chrono"
11*58b9f456SAndroid Build Coastguard Worker #include "cerrno" // errno
12*58b9f456SAndroid Build Coastguard Worker #include "system_error" // __throw_system_error
13*58b9f456SAndroid Build Coastguard Worker #include <time.h> // clock_gettime, CLOCK_MONOTONIC and CLOCK_REALTIME
14*58b9f456SAndroid Build Coastguard Worker #include "include/apple_availability.h"
15*58b9f456SAndroid Build Coastguard Worker
16*58b9f456SAndroid Build Coastguard Worker #if !defined(__APPLE__)
17*58b9f456SAndroid Build Coastguard Worker #define _LIBCPP_USE_CLOCK_GETTIME
18*58b9f456SAndroid Build Coastguard Worker #endif // __APPLE__
19*58b9f456SAndroid Build Coastguard Worker
20*58b9f456SAndroid Build Coastguard Worker #if defined(_LIBCPP_WIN32API)
21*58b9f456SAndroid Build Coastguard Worker #define WIN32_LEAN_AND_MEAN
22*58b9f456SAndroid Build Coastguard Worker #define VC_EXTRA_LEAN
23*58b9f456SAndroid Build Coastguard Worker #include <windows.h>
24*58b9f456SAndroid Build Coastguard Worker #if _WIN32_WINNT >= _WIN32_WINNT_WIN8
25*58b9f456SAndroid Build Coastguard Worker #include <winapifamily.h>
26*58b9f456SAndroid Build Coastguard Worker #endif
27*58b9f456SAndroid Build Coastguard Worker #else
28*58b9f456SAndroid Build Coastguard Worker #if !defined(CLOCK_REALTIME) || !defined(_LIBCPP_USE_CLOCK_GETTIME)
29*58b9f456SAndroid Build Coastguard Worker #include <sys/time.h> // for gettimeofday and timeval
30*58b9f456SAndroid Build Coastguard Worker #endif // !defined(CLOCK_REALTIME)
31*58b9f456SAndroid Build Coastguard Worker #endif // defined(_LIBCPP_WIN32API)
32*58b9f456SAndroid Build Coastguard Worker
33*58b9f456SAndroid Build Coastguard Worker #if !defined(_LIBCPP_HAS_NO_MONOTONIC_CLOCK)
34*58b9f456SAndroid Build Coastguard Worker #if __APPLE__
35*58b9f456SAndroid Build Coastguard Worker #include <mach/mach_time.h> // mach_absolute_time, mach_timebase_info_data_t
36*58b9f456SAndroid Build Coastguard Worker #elif !defined(_LIBCPP_WIN32API) && !defined(CLOCK_MONOTONIC)
37*58b9f456SAndroid Build Coastguard Worker #error "Monotonic clock not implemented"
38*58b9f456SAndroid Build Coastguard Worker #endif
39*58b9f456SAndroid Build Coastguard Worker #endif
40*58b9f456SAndroid Build Coastguard Worker
41*58b9f456SAndroid Build Coastguard Worker _LIBCPP_BEGIN_NAMESPACE_STD
42*58b9f456SAndroid Build Coastguard Worker
43*58b9f456SAndroid Build Coastguard Worker namespace chrono
44*58b9f456SAndroid Build Coastguard Worker {
45*58b9f456SAndroid Build Coastguard Worker
46*58b9f456SAndroid Build Coastguard Worker // system_clock
47*58b9f456SAndroid Build Coastguard Worker
48*58b9f456SAndroid Build Coastguard Worker const bool system_clock::is_steady;
49*58b9f456SAndroid Build Coastguard Worker
50*58b9f456SAndroid Build Coastguard Worker system_clock::time_point
now()51*58b9f456SAndroid Build Coastguard Worker system_clock::now() _NOEXCEPT
52*58b9f456SAndroid Build Coastguard Worker {
53*58b9f456SAndroid Build Coastguard Worker #if defined(_LIBCPP_WIN32API)
54*58b9f456SAndroid Build Coastguard Worker // FILETIME is in 100ns units
55*58b9f456SAndroid Build Coastguard Worker using filetime_duration =
56*58b9f456SAndroid Build Coastguard Worker _VSTD::chrono::duration<__int64,
57*58b9f456SAndroid Build Coastguard Worker _VSTD::ratio_multiply<_VSTD::ratio<100, 1>,
58*58b9f456SAndroid Build Coastguard Worker nanoseconds::period>>;
59*58b9f456SAndroid Build Coastguard Worker
60*58b9f456SAndroid Build Coastguard Worker // The Windows epoch is Jan 1 1601, the Unix epoch Jan 1 1970.
61*58b9f456SAndroid Build Coastguard Worker static _LIBCPP_CONSTEXPR const seconds nt_to_unix_epoch{11644473600};
62*58b9f456SAndroid Build Coastguard Worker
63*58b9f456SAndroid Build Coastguard Worker FILETIME ft;
64*58b9f456SAndroid Build Coastguard Worker #if _WIN32_WINNT >= _WIN32_WINNT_WIN8
65*58b9f456SAndroid Build Coastguard Worker #if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
66*58b9f456SAndroid Build Coastguard Worker GetSystemTimePreciseAsFileTime(&ft);
67*58b9f456SAndroid Build Coastguard Worker #else
68*58b9f456SAndroid Build Coastguard Worker GetSystemTimeAsFileTime(&ft);
69*58b9f456SAndroid Build Coastguard Worker #endif
70*58b9f456SAndroid Build Coastguard Worker #else
71*58b9f456SAndroid Build Coastguard Worker GetSystemTimeAsFileTime(&ft);
72*58b9f456SAndroid Build Coastguard Worker #endif
73*58b9f456SAndroid Build Coastguard Worker
74*58b9f456SAndroid Build Coastguard Worker filetime_duration d{(static_cast<__int64>(ft.dwHighDateTime) << 32) |
75*58b9f456SAndroid Build Coastguard Worker static_cast<__int64>(ft.dwLowDateTime)};
76*58b9f456SAndroid Build Coastguard Worker return time_point(duration_cast<duration>(d - nt_to_unix_epoch));
77*58b9f456SAndroid Build Coastguard Worker #else
78*58b9f456SAndroid Build Coastguard Worker #if defined(_LIBCPP_USE_CLOCK_GETTIME) && defined(CLOCK_REALTIME)
79*58b9f456SAndroid Build Coastguard Worker struct timespec tp;
80*58b9f456SAndroid Build Coastguard Worker if (0 != clock_gettime(CLOCK_REALTIME, &tp))
81*58b9f456SAndroid Build Coastguard Worker __throw_system_error(errno, "clock_gettime(CLOCK_REALTIME) failed");
82*58b9f456SAndroid Build Coastguard Worker return time_point(seconds(tp.tv_sec) + microseconds(tp.tv_nsec / 1000));
83*58b9f456SAndroid Build Coastguard Worker #else
84*58b9f456SAndroid Build Coastguard Worker timeval tv;
85*58b9f456SAndroid Build Coastguard Worker gettimeofday(&tv, 0);
86*58b9f456SAndroid Build Coastguard Worker return time_point(seconds(tv.tv_sec) + microseconds(tv.tv_usec));
87*58b9f456SAndroid Build Coastguard Worker #endif // _LIBCPP_USE_CLOCK_GETTIME && CLOCK_REALTIME
88*58b9f456SAndroid Build Coastguard Worker #endif
89*58b9f456SAndroid Build Coastguard Worker }
90*58b9f456SAndroid Build Coastguard Worker
91*58b9f456SAndroid Build Coastguard Worker time_t
to_time_t(const time_point & t)92*58b9f456SAndroid Build Coastguard Worker system_clock::to_time_t(const time_point& t) _NOEXCEPT
93*58b9f456SAndroid Build Coastguard Worker {
94*58b9f456SAndroid Build Coastguard Worker return time_t(duration_cast<seconds>(t.time_since_epoch()).count());
95*58b9f456SAndroid Build Coastguard Worker }
96*58b9f456SAndroid Build Coastguard Worker
97*58b9f456SAndroid Build Coastguard Worker system_clock::time_point
from_time_t(time_t t)98*58b9f456SAndroid Build Coastguard Worker system_clock::from_time_t(time_t t) _NOEXCEPT
99*58b9f456SAndroid Build Coastguard Worker {
100*58b9f456SAndroid Build Coastguard Worker return system_clock::time_point(seconds(t));
101*58b9f456SAndroid Build Coastguard Worker }
102*58b9f456SAndroid Build Coastguard Worker
103*58b9f456SAndroid Build Coastguard Worker #ifndef _LIBCPP_HAS_NO_MONOTONIC_CLOCK
104*58b9f456SAndroid Build Coastguard Worker // steady_clock
105*58b9f456SAndroid Build Coastguard Worker //
106*58b9f456SAndroid Build Coastguard Worker // Warning: If this is not truly steady, then it is non-conforming. It is
107*58b9f456SAndroid Build Coastguard Worker // better for it to not exist and have the rest of libc++ use system_clock
108*58b9f456SAndroid Build Coastguard Worker // instead.
109*58b9f456SAndroid Build Coastguard Worker
110*58b9f456SAndroid Build Coastguard Worker const bool steady_clock::is_steady;
111*58b9f456SAndroid Build Coastguard Worker
112*58b9f456SAndroid Build Coastguard Worker #if defined(__APPLE__)
113*58b9f456SAndroid Build Coastguard Worker
114*58b9f456SAndroid Build Coastguard Worker // Darwin libc versions >= 1133 provide ns precision via CLOCK_UPTIME_RAW
115*58b9f456SAndroid Build Coastguard Worker #if defined(_LIBCPP_USE_CLOCK_GETTIME) && defined(CLOCK_UPTIME_RAW)
116*58b9f456SAndroid Build Coastguard Worker steady_clock::time_point
now()117*58b9f456SAndroid Build Coastguard Worker steady_clock::now() _NOEXCEPT
118*58b9f456SAndroid Build Coastguard Worker {
119*58b9f456SAndroid Build Coastguard Worker struct timespec tp;
120*58b9f456SAndroid Build Coastguard Worker if (0 != clock_gettime(CLOCK_UPTIME_RAW, &tp))
121*58b9f456SAndroid Build Coastguard Worker __throw_system_error(errno, "clock_gettime(CLOCK_UPTIME_RAW) failed");
122*58b9f456SAndroid Build Coastguard Worker return time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec));
123*58b9f456SAndroid Build Coastguard Worker }
124*58b9f456SAndroid Build Coastguard Worker
125*58b9f456SAndroid Build Coastguard Worker #else
126*58b9f456SAndroid Build Coastguard Worker // mach_absolute_time() * MachInfo.numer / MachInfo.denom is the number of
127*58b9f456SAndroid Build Coastguard Worker // nanoseconds since the computer booted up. MachInfo.numer and MachInfo.denom
128*58b9f456SAndroid Build Coastguard Worker // are run time constants supplied by the OS. This clock has no relationship
129*58b9f456SAndroid Build Coastguard Worker // to the Gregorian calendar. It's main use is as a high resolution timer.
130*58b9f456SAndroid Build Coastguard Worker
131*58b9f456SAndroid Build Coastguard Worker // MachInfo.numer / MachInfo.denom is often 1 on the latest equipment. Specialize
132*58b9f456SAndroid Build Coastguard Worker // for that case as an optimization.
133*58b9f456SAndroid Build Coastguard Worker
134*58b9f456SAndroid Build Coastguard Worker static
135*58b9f456SAndroid Build Coastguard Worker steady_clock::rep
steady_simplified()136*58b9f456SAndroid Build Coastguard Worker steady_simplified()
137*58b9f456SAndroid Build Coastguard Worker {
138*58b9f456SAndroid Build Coastguard Worker return static_cast<steady_clock::rep>(mach_absolute_time());
139*58b9f456SAndroid Build Coastguard Worker }
140*58b9f456SAndroid Build Coastguard Worker
141*58b9f456SAndroid Build Coastguard Worker static
142*58b9f456SAndroid Build Coastguard Worker double
compute_steady_factor()143*58b9f456SAndroid Build Coastguard Worker compute_steady_factor()
144*58b9f456SAndroid Build Coastguard Worker {
145*58b9f456SAndroid Build Coastguard Worker mach_timebase_info_data_t MachInfo;
146*58b9f456SAndroid Build Coastguard Worker mach_timebase_info(&MachInfo);
147*58b9f456SAndroid Build Coastguard Worker return static_cast<double>(MachInfo.numer) / MachInfo.denom;
148*58b9f456SAndroid Build Coastguard Worker }
149*58b9f456SAndroid Build Coastguard Worker
150*58b9f456SAndroid Build Coastguard Worker static
151*58b9f456SAndroid Build Coastguard Worker steady_clock::rep
steady_full()152*58b9f456SAndroid Build Coastguard Worker steady_full()
153*58b9f456SAndroid Build Coastguard Worker {
154*58b9f456SAndroid Build Coastguard Worker static const double factor = compute_steady_factor();
155*58b9f456SAndroid Build Coastguard Worker return static_cast<steady_clock::rep>(mach_absolute_time() * factor);
156*58b9f456SAndroid Build Coastguard Worker }
157*58b9f456SAndroid Build Coastguard Worker
158*58b9f456SAndroid Build Coastguard Worker typedef steady_clock::rep (*FP)();
159*58b9f456SAndroid Build Coastguard Worker
160*58b9f456SAndroid Build Coastguard Worker static
161*58b9f456SAndroid Build Coastguard Worker FP
init_steady_clock()162*58b9f456SAndroid Build Coastguard Worker init_steady_clock()
163*58b9f456SAndroid Build Coastguard Worker {
164*58b9f456SAndroid Build Coastguard Worker mach_timebase_info_data_t MachInfo;
165*58b9f456SAndroid Build Coastguard Worker mach_timebase_info(&MachInfo);
166*58b9f456SAndroid Build Coastguard Worker if (MachInfo.numer == MachInfo.denom)
167*58b9f456SAndroid Build Coastguard Worker return &steady_simplified;
168*58b9f456SAndroid Build Coastguard Worker return &steady_full;
169*58b9f456SAndroid Build Coastguard Worker }
170*58b9f456SAndroid Build Coastguard Worker
171*58b9f456SAndroid Build Coastguard Worker steady_clock::time_point
now()172*58b9f456SAndroid Build Coastguard Worker steady_clock::now() _NOEXCEPT
173*58b9f456SAndroid Build Coastguard Worker {
174*58b9f456SAndroid Build Coastguard Worker static FP fp = init_steady_clock();
175*58b9f456SAndroid Build Coastguard Worker return time_point(duration(fp()));
176*58b9f456SAndroid Build Coastguard Worker }
177*58b9f456SAndroid Build Coastguard Worker #endif // defined(_LIBCPP_USE_CLOCK_GETTIME) && defined(CLOCK_UPTIME_RAW)
178*58b9f456SAndroid Build Coastguard Worker
179*58b9f456SAndroid Build Coastguard Worker #elif defined(_LIBCPP_WIN32API)
180*58b9f456SAndroid Build Coastguard Worker
181*58b9f456SAndroid Build Coastguard Worker steady_clock::time_point
now()182*58b9f456SAndroid Build Coastguard Worker steady_clock::now() _NOEXCEPT
183*58b9f456SAndroid Build Coastguard Worker {
184*58b9f456SAndroid Build Coastguard Worker static LARGE_INTEGER freq;
185*58b9f456SAndroid Build Coastguard Worker static BOOL initialized = FALSE;
186*58b9f456SAndroid Build Coastguard Worker if (!initialized)
187*58b9f456SAndroid Build Coastguard Worker initialized = QueryPerformanceFrequency(&freq); // always succceeds
188*58b9f456SAndroid Build Coastguard Worker
189*58b9f456SAndroid Build Coastguard Worker LARGE_INTEGER counter;
190*58b9f456SAndroid Build Coastguard Worker QueryPerformanceCounter(&counter);
191*58b9f456SAndroid Build Coastguard Worker return time_point(duration(counter.QuadPart * nano::den / freq.QuadPart));
192*58b9f456SAndroid Build Coastguard Worker }
193*58b9f456SAndroid Build Coastguard Worker
194*58b9f456SAndroid Build Coastguard Worker #elif defined(CLOCK_MONOTONIC)
195*58b9f456SAndroid Build Coastguard Worker
196*58b9f456SAndroid Build Coastguard Worker // On Apple platforms only CLOCK_UPTIME_RAW or mach_absolute_time are able to
197*58b9f456SAndroid Build Coastguard Worker // time functions in the nanosecond range. Thus, they are the only acceptable
198*58b9f456SAndroid Build Coastguard Worker // implementations of steady_clock.
199*58b9f456SAndroid Build Coastguard Worker #ifdef __APPLE__
200*58b9f456SAndroid Build Coastguard Worker #error "Never use CLOCK_MONOTONIC for steady_clock::now on Apple platforms"
201*58b9f456SAndroid Build Coastguard Worker #endif
202*58b9f456SAndroid Build Coastguard Worker
203*58b9f456SAndroid Build Coastguard Worker steady_clock::time_point
now()204*58b9f456SAndroid Build Coastguard Worker steady_clock::now() _NOEXCEPT
205*58b9f456SAndroid Build Coastguard Worker {
206*58b9f456SAndroid Build Coastguard Worker struct timespec tp;
207*58b9f456SAndroid Build Coastguard Worker if (0 != clock_gettime(CLOCK_MONOTONIC, &tp))
208*58b9f456SAndroid Build Coastguard Worker __throw_system_error(errno, "clock_gettime(CLOCK_MONOTONIC) failed");
209*58b9f456SAndroid Build Coastguard Worker return time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec));
210*58b9f456SAndroid Build Coastguard Worker }
211*58b9f456SAndroid Build Coastguard Worker
212*58b9f456SAndroid Build Coastguard Worker #else
213*58b9f456SAndroid Build Coastguard Worker #error "Monotonic clock not implemented"
214*58b9f456SAndroid Build Coastguard Worker #endif
215*58b9f456SAndroid Build Coastguard Worker
216*58b9f456SAndroid Build Coastguard Worker #endif // !_LIBCPP_HAS_NO_MONOTONIC_CLOCK
217*58b9f456SAndroid Build Coastguard Worker
218*58b9f456SAndroid Build Coastguard Worker }
219*58b9f456SAndroid Build Coastguard Worker
220*58b9f456SAndroid Build Coastguard Worker _LIBCPP_END_NAMESPACE_STD
221