xref: /llvm-project-15.0.7/libcxx/src/chrono.cpp (revision d28f69d9)
13e519524SHoward Hinnant //===------------------------- chrono.cpp ---------------------------------===//
23e519524SHoward Hinnant //
357b08b09SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
457b08b09SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
557b08b09SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
63e519524SHoward Hinnant //
73e519524SHoward Hinnant //===----------------------------------------------------------------------===//
8d3ac6ae7SEric Fiselier 
93e519524SHoward Hinnant #include "chrono"
104daa9f1eSMarshall Clow #include "cerrno"        // errno
114daa9f1eSMarshall Clow #include "system_error"  // __throw_system_error
124f0f708bSEd Schouten #include <time.h>        // clock_gettime, CLOCK_MONOTONIC and CLOCK_REALTIME
13c55ac105SEric Fiselier #include "include/apple_availability.h"
144f0f708bSEd Schouten 
15c55ac105SEric Fiselier #if !defined(__APPLE__)
16c55ac105SEric Fiselier #define _LIBCPP_USE_CLOCK_GETTIME
17e5423739SBruno Cardoso Lopes #endif // __APPLE__
18e5423739SBruno Cardoso Lopes 
19b2826a1dSSaleem Abdulrasool #if defined(_LIBCPP_WIN32API)
206e3a0cc4SSaleem Abdulrasool #define WIN32_LEAN_AND_MEAN
216e3a0cc4SSaleem Abdulrasool #define VC_EXTRA_LEAN
221ec02625SEric Fiselier #include <windows.h>
236e3a0cc4SSaleem Abdulrasool #if _WIN32_WINNT >= _WIN32_WINNT_WIN8
246e3a0cc4SSaleem Abdulrasool #include <winapifamily.h>
256e3a0cc4SSaleem Abdulrasool #endif
266e3a0cc4SSaleem Abdulrasool #else
27c55ac105SEric Fiselier #if !defined(CLOCK_REALTIME) || !defined(_LIBCPP_USE_CLOCK_GETTIME)
284f0f708bSEd Schouten #include <sys/time.h> // for gettimeofday and timeval
29*d28f69d9SLouis Dionne #endif
30b2826a1dSSaleem Abdulrasool #endif // defined(_LIBCPP_WIN32API)
314f0f708bSEd Schouten 
3278f51f95SSaleem Abdulrasool #if !defined(_LIBCPP_HAS_NO_MONOTONIC_CLOCK)
334f0f708bSEd Schouten #if __APPLE__
344f0f708bSEd Schouten #include <mach/mach_time.h>  // mach_absolute_time, mach_timebase_info_data_t
35b2826a1dSSaleem Abdulrasool #elif !defined(_LIBCPP_WIN32API) && !defined(CLOCK_MONOTONIC)
364f0f708bSEd Schouten #error "Monotonic clock not implemented"
374f0f708bSEd Schouten #endif
384f0f708bSEd Schouten #endif
393e519524SHoward Hinnant 
40a9b5fff5SMichał Górny #if defined(__ELF__) && defined(_LIBCPP_LINK_RT_LIB)
41996e62eeSPetr Hosek #pragma comment(lib, "rt")
42996e62eeSPetr Hosek #endif
43996e62eeSPetr Hosek 
443e519524SHoward Hinnant _LIBCPP_BEGIN_NAMESPACE_STD
453e519524SHoward Hinnant 
463e519524SHoward Hinnant namespace chrono
473e519524SHoward Hinnant {
483e519524SHoward Hinnant 
493e519524SHoward Hinnant // system_clock
503e519524SHoward Hinnant 
5116694b5dSHoward Hinnant const bool system_clock::is_steady;
5216694b5dSHoward Hinnant 
533e519524SHoward Hinnant system_clock::time_point
54d53d8152SHoward Hinnant system_clock::now() _NOEXCEPT
553e519524SHoward Hinnant {
56b2826a1dSSaleem Abdulrasool #if defined(_LIBCPP_WIN32API)
57e91473c3SSaleem Abdulrasool   // FILETIME is in 100ns units
58e91473c3SSaleem Abdulrasool   using filetime_duration =
59e91473c3SSaleem Abdulrasool       _VSTD::chrono::duration<__int64,
60e91473c3SSaleem Abdulrasool                               _VSTD::ratio_multiply<_VSTD::ratio<100, 1>,
61e91473c3SSaleem Abdulrasool                                                     nanoseconds::period>>;
626e3a0cc4SSaleem Abdulrasool 
63e91473c3SSaleem Abdulrasool   // The Windows epoch is Jan 1 1601, the Unix epoch Jan 1 1970.
64d1c346a4SSaleem Abdulrasool   static _LIBCPP_CONSTEXPR const seconds nt_to_unix_epoch{11644473600};
65e91473c3SSaleem Abdulrasool 
66e91473c3SSaleem Abdulrasool   FILETIME ft;
676e3a0cc4SSaleem Abdulrasool #if _WIN32_WINNT >= _WIN32_WINNT_WIN8
686e3a0cc4SSaleem Abdulrasool #if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
69e91473c3SSaleem Abdulrasool   GetSystemTimePreciseAsFileTime(&ft);
706e3a0cc4SSaleem Abdulrasool #else
71e91473c3SSaleem Abdulrasool   GetSystemTimeAsFileTime(&ft);
726e3a0cc4SSaleem Abdulrasool #endif
736e3a0cc4SSaleem Abdulrasool #else
74e91473c3SSaleem Abdulrasool   GetSystemTimeAsFileTime(&ft);
756e3a0cc4SSaleem Abdulrasool #endif
76e91473c3SSaleem Abdulrasool 
77e91473c3SSaleem Abdulrasool   filetime_duration d{(static_cast<__int64>(ft.dwHighDateTime) << 32) |
78e91473c3SSaleem Abdulrasool                        static_cast<__int64>(ft.dwLowDateTime)};
79e91473c3SSaleem Abdulrasool   return time_point(duration_cast<duration>(d - nt_to_unix_epoch));
806e3a0cc4SSaleem Abdulrasool #else
81c55ac105SEric Fiselier #if defined(_LIBCPP_USE_CLOCK_GETTIME) && defined(CLOCK_REALTIME)
824f0f708bSEd Schouten   struct timespec tp;
834f0f708bSEd Schouten   if (0 != clock_gettime(CLOCK_REALTIME, &tp))
844f0f708bSEd Schouten     __throw_system_error(errno, "clock_gettime(CLOCK_REALTIME) failed");
854f0f708bSEd Schouten   return time_point(seconds(tp.tv_sec) + microseconds(tp.tv_nsec / 1000));
86e5423739SBruno Cardoso Lopes #else
873e519524SHoward Hinnant     timeval tv;
883e519524SHoward Hinnant     gettimeofday(&tv, 0);
893e519524SHoward Hinnant     return time_point(seconds(tv.tv_sec) + microseconds(tv.tv_usec));
90c55ac105SEric Fiselier #endif // _LIBCPP_USE_CLOCK_GETTIME && CLOCK_REALTIME
916e3a0cc4SSaleem Abdulrasool #endif
923e519524SHoward Hinnant }
933e519524SHoward Hinnant 
943e519524SHoward Hinnant time_t
95d53d8152SHoward Hinnant system_clock::to_time_t(const time_point& t) _NOEXCEPT
963e519524SHoward Hinnant {
973e519524SHoward Hinnant     return time_t(duration_cast<seconds>(t.time_since_epoch()).count());
983e519524SHoward Hinnant }
993e519524SHoward Hinnant 
1003e519524SHoward Hinnant system_clock::time_point
101d53d8152SHoward Hinnant system_clock::from_time_t(time_t t) _NOEXCEPT
1023e519524SHoward Hinnant {
1033e519524SHoward Hinnant     return system_clock::time_point(seconds(t));
1043e519524SHoward Hinnant }
1053e519524SHoward Hinnant 
106c59e5851SJonathan Roelofs #ifndef _LIBCPP_HAS_NO_MONOTONIC_CLOCK
1073dc6455fSHoward Hinnant // steady_clock
1084f0f708bSEd Schouten //
1094f0f708bSEd Schouten // Warning:  If this is not truly steady, then it is non-conforming.  It is
1104f0f708bSEd Schouten //  better for it to not exist and have the rest of libc++ use system_clock
1114f0f708bSEd Schouten //  instead.
1123e519524SHoward Hinnant 
11316694b5dSHoward Hinnant const bool steady_clock::is_steady;
11416694b5dSHoward Hinnant 
11578f51f95SSaleem Abdulrasool #if defined(__APPLE__)
1164f0f708bSEd Schouten 
1178bec8927SLouis Dionne // Darwin libc versions >= 1133 provide ns precision via CLOCK_MONOTONIC_RAW
1188bec8927SLouis Dionne #if defined(_LIBCPP_USE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC_RAW)
119e5423739SBruno Cardoso Lopes steady_clock::time_point
120e5423739SBruno Cardoso Lopes steady_clock::now() _NOEXCEPT
121e5423739SBruno Cardoso Lopes {
122e5423739SBruno Cardoso Lopes     struct timespec tp;
1238bec8927SLouis Dionne     if (0 != clock_gettime(CLOCK_MONOTONIC_RAW, &tp))
1248bec8927SLouis Dionne         __throw_system_error(errno, "clock_gettime(CLOCK_MONOTONIC_RAW) failed");
125e5423739SBruno Cardoso Lopes     return time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec));
126e5423739SBruno Cardoso Lopes }
127e5423739SBruno Cardoso Lopes 
128e5423739SBruno Cardoso Lopes #else
1293e519524SHoward Hinnant //   mach_absolute_time() * MachInfo.numer / MachInfo.denom is the number of
1303e519524SHoward Hinnant //   nanoseconds since the computer booted up.  MachInfo.numer and MachInfo.denom
1313e519524SHoward Hinnant //   are run time constants supplied by the OS.  This clock has no relationship
1323e519524SHoward Hinnant //   to the Gregorian calendar.  It's main use is as a high resolution timer.
1333e519524SHoward Hinnant 
1343e519524SHoward Hinnant // MachInfo.numer / MachInfo.denom is often 1 on the latest equipment.  Specialize
1353e519524SHoward Hinnant //   for that case as an optimization.
1363e519524SHoward Hinnant 
1373e519524SHoward Hinnant static
1383dc6455fSHoward Hinnant steady_clock::rep
1393dc6455fSHoward Hinnant steady_simplified()
1403e519524SHoward Hinnant {
141c206366fSHoward Hinnant     return static_cast<steady_clock::rep>(mach_absolute_time());
1423e519524SHoward Hinnant }
1433e519524SHoward Hinnant 
1443e519524SHoward Hinnant static
1453e519524SHoward Hinnant double
1463dc6455fSHoward Hinnant compute_steady_factor()
1473e519524SHoward Hinnant {
1483e519524SHoward Hinnant     mach_timebase_info_data_t MachInfo;
1493e519524SHoward Hinnant     mach_timebase_info(&MachInfo);
1503e519524SHoward Hinnant     return static_cast<double>(MachInfo.numer) / MachInfo.denom;
1513e519524SHoward Hinnant }
1523e519524SHoward Hinnant 
1533e519524SHoward Hinnant static
1543dc6455fSHoward Hinnant steady_clock::rep
1553dc6455fSHoward Hinnant steady_full()
1563e519524SHoward Hinnant {
1573dc6455fSHoward Hinnant     static const double factor = compute_steady_factor();
1583dc6455fSHoward Hinnant     return static_cast<steady_clock::rep>(mach_absolute_time() * factor);
1593e519524SHoward Hinnant }
1603e519524SHoward Hinnant 
1613dc6455fSHoward Hinnant typedef steady_clock::rep (*FP)();
1623e519524SHoward Hinnant 
1633e519524SHoward Hinnant static
1643e519524SHoward Hinnant FP
1653dc6455fSHoward Hinnant init_steady_clock()
1663e519524SHoward Hinnant {
1673e519524SHoward Hinnant     mach_timebase_info_data_t MachInfo;
1683e519524SHoward Hinnant     mach_timebase_info(&MachInfo);
1693e519524SHoward Hinnant     if (MachInfo.numer == MachInfo.denom)
1703dc6455fSHoward Hinnant         return &steady_simplified;
1713dc6455fSHoward Hinnant     return &steady_full;
1723e519524SHoward Hinnant }
1733e519524SHoward Hinnant 
1743dc6455fSHoward Hinnant steady_clock::time_point
175d53d8152SHoward Hinnant steady_clock::now() _NOEXCEPT
1763e519524SHoward Hinnant {
1773dc6455fSHoward Hinnant     static FP fp = init_steady_clock();
1783e519524SHoward Hinnant     return time_point(duration(fp()));
1793e519524SHoward Hinnant }
1808bec8927SLouis Dionne #endif // defined(_LIBCPP_USE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC_RAW)
1813e519524SHoward Hinnant 
182b2826a1dSSaleem Abdulrasool #elif defined(_LIBCPP_WIN32API)
18378f51f95SSaleem Abdulrasool 
184ecad92b0SMarshall Clow // https://msdn.microsoft.com/en-us/library/windows/desktop/ms644905(v=vs.85).aspx says:
185ecad92b0SMarshall Clow //    If the function fails, the return value is zero. <snip>
186ecad92b0SMarshall Clow //    On systems that run Windows XP or later, the function will always succeed
187ecad92b0SMarshall Clow //      and will thus never return zero.
188ecad92b0SMarshall Clow 
189ecad92b0SMarshall Clow static LARGE_INTEGER
190ecad92b0SMarshall Clow __QueryPerformanceFrequency()
191ecad92b0SMarshall Clow {
192ecad92b0SMarshall Clow 	LARGE_INTEGER val;
193ecad92b0SMarshall Clow 	(void) QueryPerformanceFrequency(&val);
194ecad92b0SMarshall Clow 	return val;
195ecad92b0SMarshall Clow }
196ecad92b0SMarshall Clow 
19778f51f95SSaleem Abdulrasool steady_clock::time_point
19878f51f95SSaleem Abdulrasool steady_clock::now() _NOEXCEPT
19978f51f95SSaleem Abdulrasool {
200ecad92b0SMarshall Clow   static const LARGE_INTEGER freq = __QueryPerformanceFrequency();
20178f51f95SSaleem Abdulrasool 
20268ad5c34SMarshall Clow   LARGE_INTEGER counter;
20368ad5c34SMarshall Clow   (void) QueryPerformanceCounter(&counter);
204cfc01154SSaleem Abdulrasool   return time_point(duration(counter.QuadPart * nano::den / freq.QuadPart));
20578f51f95SSaleem Abdulrasool }
20678f51f95SSaleem Abdulrasool 
20778f51f95SSaleem Abdulrasool #elif defined(CLOCK_MONOTONIC)
20878f51f95SSaleem Abdulrasool 
2098bec8927SLouis Dionne // On Apple platforms only CLOCK_UPTIME_RAW, CLOCK_MONOTONIC_RAW or
2108bec8927SLouis Dionne // mach_absolute_time are able to time functions in the nanosecond range.
2118bec8927SLouis Dionne // Thus, they are the only acceptable implementations of steady_clock.
212e5423739SBruno Cardoso Lopes #ifdef __APPLE__
213e5423739SBruno Cardoso Lopes #error "Never use CLOCK_MONOTONIC for steady_clock::now on Apple platforms"
214e5423739SBruno Cardoso Lopes #endif
215e5423739SBruno Cardoso Lopes 
21678f51f95SSaleem Abdulrasool steady_clock::time_point
21778f51f95SSaleem Abdulrasool steady_clock::now() _NOEXCEPT
21878f51f95SSaleem Abdulrasool {
21978f51f95SSaleem Abdulrasool     struct timespec tp;
22078f51f95SSaleem Abdulrasool     if (0 != clock_gettime(CLOCK_MONOTONIC, &tp))
22178f51f95SSaleem Abdulrasool         __throw_system_error(errno, "clock_gettime(CLOCK_MONOTONIC) failed");
22278f51f95SSaleem Abdulrasool     return time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec));
22378f51f95SSaleem Abdulrasool }
22478f51f95SSaleem Abdulrasool 
2254f0f708bSEd Schouten #else
2264f0f708bSEd Schouten #error "Monotonic clock not implemented"
2274f0f708bSEd Schouten #endif
228128ba719SHoward Hinnant 
229c59e5851SJonathan Roelofs #endif // !_LIBCPP_HAS_NO_MONOTONIC_CLOCK
230c59e5851SJonathan Roelofs 
2313e519524SHoward Hinnant }
2323e519524SHoward Hinnant 
2333e519524SHoward Hinnant _LIBCPP_END_NAMESPACE_STD
234