xref: /freebsd-12.1/contrib/libc++/src/chrono.cpp (revision 4ba319b5)
17a984708SDavid Chisnall //===------------------------- chrono.cpp ---------------------------------===//
27a984708SDavid Chisnall //
37a984708SDavid Chisnall //                     The LLVM Compiler Infrastructure
47a984708SDavid Chisnall //
57a984708SDavid Chisnall // This file is dual licensed under the MIT and the University of Illinois Open
67a984708SDavid Chisnall // Source Licenses. See LICENSE.TXT for details.
77a984708SDavid Chisnall //
87a984708SDavid Chisnall //===----------------------------------------------------------------------===//
97a984708SDavid Chisnall 
107a984708SDavid Chisnall #include "chrono"
11854fa44bSDimitry Andric #include "cerrno"        // errno
12854fa44bSDimitry Andric #include "system_error"  // __throw_system_error
13854fa44bSDimitry Andric #include <time.h>        // clock_gettime, CLOCK_MONOTONIC and CLOCK_REALTIME
14*4ba319b5SDimitry Andric #include "include/apple_availability.h"
15854fa44bSDimitry Andric 
16*4ba319b5SDimitry Andric #if !defined(__APPLE__)
17*4ba319b5SDimitry Andric #define _LIBCPP_USE_CLOCK_GETTIME
1880779b37SDimitry Andric #endif // __APPLE__
1980779b37SDimitry Andric 
20aed8d94eSDimitry Andric #if defined(_LIBCPP_WIN32API)
21aed8d94eSDimitry Andric #define WIN32_LEAN_AND_MEAN
22aed8d94eSDimitry Andric #define VC_EXTRA_LEAN
23f9448bf3SDimitry Andric #include <windows.h>
24aed8d94eSDimitry Andric #if _WIN32_WINNT >= _WIN32_WINNT_WIN8
25aed8d94eSDimitry Andric #include <winapifamily.h>
26aed8d94eSDimitry Andric #endif
27aed8d94eSDimitry Andric #else
28*4ba319b5SDimitry Andric #if !defined(CLOCK_REALTIME) || !defined(_LIBCPP_USE_CLOCK_GETTIME)
297a984708SDavid Chisnall #include <sys/time.h>        // for gettimeofday and timeval
30aed8d94eSDimitry Andric #endif // !defined(CLOCK_REALTIME)
31aed8d94eSDimitry Andric #endif // defined(_LIBCPP_WIN32API)
32854fa44bSDimitry Andric 
33aed8d94eSDimitry Andric #if !defined(_LIBCPP_HAS_NO_MONOTONIC_CLOCK)
34854fa44bSDimitry Andric #if __APPLE__
357a984708SDavid Chisnall #include <mach/mach_time.h>  // mach_absolute_time, mach_timebase_info_data_t
36aed8d94eSDimitry Andric #elif !defined(_LIBCPP_WIN32API) && !defined(CLOCK_MONOTONIC)
37854fa44bSDimitry Andric #error "Monotonic clock not implemented"
38854fa44bSDimitry Andric #endif
39854fa44bSDimitry Andric #endif
407a984708SDavid Chisnall 
417a984708SDavid Chisnall _LIBCPP_BEGIN_NAMESPACE_STD
427a984708SDavid Chisnall 
437a984708SDavid Chisnall namespace chrono
447a984708SDavid Chisnall {
457a984708SDavid Chisnall 
467a984708SDavid Chisnall // system_clock
477a984708SDavid Chisnall 
48cfdf2879SDavid Chisnall const bool system_clock::is_steady;
49cfdf2879SDavid Chisnall 
507a984708SDavid Chisnall system_clock::time_point
now()517a984708SDavid Chisnall system_clock::now() _NOEXCEPT
527a984708SDavid Chisnall {
53aed8d94eSDimitry Andric #if defined(_LIBCPP_WIN32API)
54aed8d94eSDimitry Andric   // FILETIME is in 100ns units
55aed8d94eSDimitry Andric   using filetime_duration =
56aed8d94eSDimitry Andric       _VSTD::chrono::duration<__int64,
57aed8d94eSDimitry Andric                               _VSTD::ratio_multiply<_VSTD::ratio<100, 1>,
58aed8d94eSDimitry Andric                                                     nanoseconds::period>>;
59aed8d94eSDimitry Andric 
60aed8d94eSDimitry Andric   // The Windows epoch is Jan 1 1601, the Unix epoch Jan 1 1970.
61aed8d94eSDimitry Andric   static _LIBCPP_CONSTEXPR const seconds nt_to_unix_epoch{11644473600};
62aed8d94eSDimitry Andric 
63aed8d94eSDimitry Andric   FILETIME ft;
64aed8d94eSDimitry Andric #if _WIN32_WINNT >= _WIN32_WINNT_WIN8
65aed8d94eSDimitry Andric #if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
66aed8d94eSDimitry Andric   GetSystemTimePreciseAsFileTime(&ft);
67aed8d94eSDimitry Andric #else
68aed8d94eSDimitry Andric   GetSystemTimeAsFileTime(&ft);
69aed8d94eSDimitry Andric #endif
70aed8d94eSDimitry Andric #else
71aed8d94eSDimitry Andric   GetSystemTimeAsFileTime(&ft);
72aed8d94eSDimitry Andric #endif
73aed8d94eSDimitry Andric 
74aed8d94eSDimitry Andric   filetime_duration d{(static_cast<__int64>(ft.dwHighDateTime) << 32) |
75aed8d94eSDimitry Andric                        static_cast<__int64>(ft.dwLowDateTime)};
76aed8d94eSDimitry Andric   return time_point(duration_cast<duration>(d - nt_to_unix_epoch));
77aed8d94eSDimitry Andric #else
78*4ba319b5SDimitry Andric #if defined(_LIBCPP_USE_CLOCK_GETTIME) && defined(CLOCK_REALTIME)
79854fa44bSDimitry Andric   struct timespec tp;
80854fa44bSDimitry Andric   if (0 != clock_gettime(CLOCK_REALTIME, &tp))
81854fa44bSDimitry Andric     __throw_system_error(errno, "clock_gettime(CLOCK_REALTIME) failed");
82854fa44bSDimitry Andric   return time_point(seconds(tp.tv_sec) + microseconds(tp.tv_nsec / 1000));
8380779b37SDimitry Andric #else
847a984708SDavid Chisnall     timeval tv;
857a984708SDavid Chisnall     gettimeofday(&tv, 0);
867a984708SDavid Chisnall     return time_point(seconds(tv.tv_sec) + microseconds(tv.tv_usec));
87*4ba319b5SDimitry Andric #endif // _LIBCPP_USE_CLOCK_GETTIME && CLOCK_REALTIME
88aed8d94eSDimitry Andric #endif
897a984708SDavid Chisnall }
907a984708SDavid Chisnall 
917a984708SDavid Chisnall time_t
to_time_t(const time_point & t)927a984708SDavid Chisnall system_clock::to_time_t(const time_point& t) _NOEXCEPT
937a984708SDavid Chisnall {
947a984708SDavid Chisnall     return time_t(duration_cast<seconds>(t.time_since_epoch()).count());
957a984708SDavid Chisnall }
967a984708SDavid Chisnall 
977a984708SDavid Chisnall system_clock::time_point
from_time_t(time_t t)987a984708SDavid Chisnall system_clock::from_time_t(time_t t) _NOEXCEPT
997a984708SDavid Chisnall {
1007a984708SDavid Chisnall     return system_clock::time_point(seconds(t));
1017a984708SDavid Chisnall }
1027a984708SDavid Chisnall 
103d72607e9SDimitry Andric #ifndef _LIBCPP_HAS_NO_MONOTONIC_CLOCK
1047a984708SDavid Chisnall // steady_clock
105854fa44bSDimitry Andric //
106854fa44bSDimitry Andric // Warning:  If this is not truly steady, then it is non-conforming.  It is
107854fa44bSDimitry Andric //  better for it to not exist and have the rest of libc++ use system_clock
108854fa44bSDimitry Andric //  instead.
1097a984708SDavid Chisnall 
110cfdf2879SDavid Chisnall const bool steady_clock::is_steady;
111cfdf2879SDavid Chisnall 
112aed8d94eSDimitry Andric #if defined(__APPLE__)
113854fa44bSDimitry Andric 
11480779b37SDimitry Andric // Darwin libc versions >= 1133 provide ns precision via CLOCK_UPTIME_RAW
115*4ba319b5SDimitry Andric #if defined(_LIBCPP_USE_CLOCK_GETTIME) && defined(CLOCK_UPTIME_RAW)
11680779b37SDimitry Andric steady_clock::time_point
now()11780779b37SDimitry Andric steady_clock::now() _NOEXCEPT
11880779b37SDimitry Andric {
11980779b37SDimitry Andric     struct timespec tp;
12080779b37SDimitry Andric     if (0 != clock_gettime(CLOCK_UPTIME_RAW, &tp))
12180779b37SDimitry Andric         __throw_system_error(errno, "clock_gettime(CLOCK_UPTIME_RAW) failed");
12280779b37SDimitry Andric     return time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec));
12380779b37SDimitry Andric }
12480779b37SDimitry Andric 
12580779b37SDimitry Andric #else
1267a984708SDavid Chisnall //   mach_absolute_time() * MachInfo.numer / MachInfo.denom is the number of
1277a984708SDavid Chisnall //   nanoseconds since the computer booted up.  MachInfo.numer and MachInfo.denom
1287a984708SDavid Chisnall //   are run time constants supplied by the OS.  This clock has no relationship
1297a984708SDavid Chisnall //   to the Gregorian calendar.  It's main use is as a high resolution timer.
1307a984708SDavid Chisnall 
1317a984708SDavid Chisnall // MachInfo.numer / MachInfo.denom is often 1 on the latest equipment.  Specialize
1327a984708SDavid Chisnall //   for that case as an optimization.
1337a984708SDavid Chisnall 
1347a984708SDavid Chisnall static
1357a984708SDavid Chisnall steady_clock::rep
steady_simplified()1367a984708SDavid Chisnall steady_simplified()
1377a984708SDavid Chisnall {
13894e3ee44SDavid Chisnall     return static_cast<steady_clock::rep>(mach_absolute_time());
1397a984708SDavid Chisnall }
1407a984708SDavid Chisnall 
1417a984708SDavid Chisnall static
1427a984708SDavid Chisnall double
compute_steady_factor()1437a984708SDavid Chisnall compute_steady_factor()
1447a984708SDavid Chisnall {
1457a984708SDavid Chisnall     mach_timebase_info_data_t MachInfo;
1467a984708SDavid Chisnall     mach_timebase_info(&MachInfo);
1477a984708SDavid Chisnall     return static_cast<double>(MachInfo.numer) / MachInfo.denom;
1487a984708SDavid Chisnall }
1497a984708SDavid Chisnall 
1507a984708SDavid Chisnall static
1517a984708SDavid Chisnall steady_clock::rep
steady_full()1527a984708SDavid Chisnall steady_full()
1537a984708SDavid Chisnall {
1547a984708SDavid Chisnall     static const double factor = compute_steady_factor();
1557a984708SDavid Chisnall     return static_cast<steady_clock::rep>(mach_absolute_time() * factor);
1567a984708SDavid Chisnall }
1577a984708SDavid Chisnall 
1587a984708SDavid Chisnall typedef steady_clock::rep (*FP)();
1597a984708SDavid Chisnall 
1607a984708SDavid Chisnall static
1617a984708SDavid Chisnall FP
init_steady_clock()1627a984708SDavid Chisnall init_steady_clock()
1637a984708SDavid Chisnall {
1647a984708SDavid Chisnall     mach_timebase_info_data_t MachInfo;
1657a984708SDavid Chisnall     mach_timebase_info(&MachInfo);
1667a984708SDavid Chisnall     if (MachInfo.numer == MachInfo.denom)
1677a984708SDavid Chisnall         return &steady_simplified;
1687a984708SDavid Chisnall     return &steady_full;
1697a984708SDavid Chisnall }
1707a984708SDavid Chisnall 
1717a984708SDavid Chisnall steady_clock::time_point
now()1727a984708SDavid Chisnall steady_clock::now() _NOEXCEPT
1737a984708SDavid Chisnall {
1747a984708SDavid Chisnall     static FP fp = init_steady_clock();
1757a984708SDavid Chisnall     return time_point(duration(fp()));
1767a984708SDavid Chisnall }
177*4ba319b5SDimitry Andric #endif // defined(_LIBCPP_USE_CLOCK_GETTIME) && defined(CLOCK_UPTIME_RAW)
1787a984708SDavid Chisnall 
179aed8d94eSDimitry Andric #elif defined(_LIBCPP_WIN32API)
180aed8d94eSDimitry Andric 
181aed8d94eSDimitry Andric steady_clock::time_point
now()182aed8d94eSDimitry Andric steady_clock::now() _NOEXCEPT
183aed8d94eSDimitry Andric {
184aed8d94eSDimitry Andric   static LARGE_INTEGER freq;
185aed8d94eSDimitry Andric   static BOOL initialized = FALSE;
186aed8d94eSDimitry Andric   if (!initialized)
187aed8d94eSDimitry Andric     initialized = QueryPerformanceFrequency(&freq); // always succceeds
188aed8d94eSDimitry Andric 
189aed8d94eSDimitry Andric   LARGE_INTEGER counter;
190aed8d94eSDimitry Andric   QueryPerformanceCounter(&counter);
191aed8d94eSDimitry Andric   return time_point(duration(counter.QuadPart * nano::den / freq.QuadPart));
192aed8d94eSDimitry Andric }
193aed8d94eSDimitry Andric 
194aed8d94eSDimitry Andric #elif defined(CLOCK_MONOTONIC)
195aed8d94eSDimitry Andric 
19680779b37SDimitry Andric // On Apple platforms only CLOCK_UPTIME_RAW or mach_absolute_time are able to
19780779b37SDimitry Andric // time functions in the nanosecond range. Thus, they are the only acceptable
19880779b37SDimitry Andric // implementations of steady_clock.
19980779b37SDimitry Andric #ifdef __APPLE__
20080779b37SDimitry Andric #error "Never use CLOCK_MONOTONIC for steady_clock::now on Apple platforms"
20180779b37SDimitry Andric #endif
20280779b37SDimitry Andric 
203aed8d94eSDimitry Andric steady_clock::time_point
now()204aed8d94eSDimitry Andric steady_clock::now() _NOEXCEPT
205aed8d94eSDimitry Andric {
206aed8d94eSDimitry Andric     struct timespec tp;
207aed8d94eSDimitry Andric     if (0 != clock_gettime(CLOCK_MONOTONIC, &tp))
208aed8d94eSDimitry Andric         __throw_system_error(errno, "clock_gettime(CLOCK_MONOTONIC) failed");
209aed8d94eSDimitry Andric     return time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec));
210aed8d94eSDimitry Andric }
211aed8d94eSDimitry Andric 
212854fa44bSDimitry Andric #else
213854fa44bSDimitry Andric #error "Monotonic clock not implemented"
214854fa44bSDimitry Andric #endif
2157a984708SDavid Chisnall 
216d72607e9SDimitry Andric #endif // !_LIBCPP_HAS_NO_MONOTONIC_CLOCK
217d72607e9SDimitry Andric 
2187a984708SDavid Chisnall }
2197a984708SDavid Chisnall 
2207a984708SDavid Chisnall _LIBCPP_END_NAMESPACE_STD
221