xref: /llvm-project-15.0.7/libcxx/src/chrono.cpp (revision ecad92b0)
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
29b2826a1dSSaleem Abdulrasool #endif // !defined(CLOCK_REALTIME)
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 
403e519524SHoward Hinnant _LIBCPP_BEGIN_NAMESPACE_STD
413e519524SHoward Hinnant 
423e519524SHoward Hinnant namespace chrono
433e519524SHoward Hinnant {
443e519524SHoward Hinnant 
453e519524SHoward Hinnant // system_clock
463e519524SHoward Hinnant 
4716694b5dSHoward Hinnant const bool system_clock::is_steady;
4816694b5dSHoward Hinnant 
493e519524SHoward Hinnant system_clock::time_point
50d53d8152SHoward Hinnant system_clock::now() _NOEXCEPT
513e519524SHoward Hinnant {
52b2826a1dSSaleem Abdulrasool #if defined(_LIBCPP_WIN32API)
53e91473c3SSaleem Abdulrasool   // FILETIME is in 100ns units
54e91473c3SSaleem Abdulrasool   using filetime_duration =
55e91473c3SSaleem Abdulrasool       _VSTD::chrono::duration<__int64,
56e91473c3SSaleem Abdulrasool                               _VSTD::ratio_multiply<_VSTD::ratio<100, 1>,
57e91473c3SSaleem Abdulrasool                                                     nanoseconds::period>>;
586e3a0cc4SSaleem Abdulrasool 
59e91473c3SSaleem Abdulrasool   // The Windows epoch is Jan 1 1601, the Unix epoch Jan 1 1970.
60d1c346a4SSaleem Abdulrasool   static _LIBCPP_CONSTEXPR const seconds nt_to_unix_epoch{11644473600};
61e91473c3SSaleem Abdulrasool 
62e91473c3SSaleem Abdulrasool   FILETIME ft;
636e3a0cc4SSaleem Abdulrasool #if _WIN32_WINNT >= _WIN32_WINNT_WIN8
646e3a0cc4SSaleem Abdulrasool #if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
65e91473c3SSaleem Abdulrasool   GetSystemTimePreciseAsFileTime(&ft);
666e3a0cc4SSaleem Abdulrasool #else
67e91473c3SSaleem Abdulrasool   GetSystemTimeAsFileTime(&ft);
686e3a0cc4SSaleem Abdulrasool #endif
696e3a0cc4SSaleem Abdulrasool #else
70e91473c3SSaleem Abdulrasool   GetSystemTimeAsFileTime(&ft);
716e3a0cc4SSaleem Abdulrasool #endif
72e91473c3SSaleem Abdulrasool 
73e91473c3SSaleem Abdulrasool   filetime_duration d{(static_cast<__int64>(ft.dwHighDateTime) << 32) |
74e91473c3SSaleem Abdulrasool                        static_cast<__int64>(ft.dwLowDateTime)};
75e91473c3SSaleem Abdulrasool   return time_point(duration_cast<duration>(d - nt_to_unix_epoch));
766e3a0cc4SSaleem Abdulrasool #else
77c55ac105SEric Fiselier #if defined(_LIBCPP_USE_CLOCK_GETTIME) && defined(CLOCK_REALTIME)
784f0f708bSEd Schouten   struct timespec tp;
794f0f708bSEd Schouten   if (0 != clock_gettime(CLOCK_REALTIME, &tp))
804f0f708bSEd Schouten     __throw_system_error(errno, "clock_gettime(CLOCK_REALTIME) failed");
814f0f708bSEd Schouten   return time_point(seconds(tp.tv_sec) + microseconds(tp.tv_nsec / 1000));
82e5423739SBruno Cardoso Lopes #else
833e519524SHoward Hinnant     timeval tv;
843e519524SHoward Hinnant     gettimeofday(&tv, 0);
853e519524SHoward Hinnant     return time_point(seconds(tv.tv_sec) + microseconds(tv.tv_usec));
86c55ac105SEric Fiselier #endif // _LIBCPP_USE_CLOCK_GETTIME && CLOCK_REALTIME
876e3a0cc4SSaleem Abdulrasool #endif
883e519524SHoward Hinnant }
893e519524SHoward Hinnant 
903e519524SHoward Hinnant time_t
91d53d8152SHoward Hinnant system_clock::to_time_t(const time_point& t) _NOEXCEPT
923e519524SHoward Hinnant {
933e519524SHoward Hinnant     return time_t(duration_cast<seconds>(t.time_since_epoch()).count());
943e519524SHoward Hinnant }
953e519524SHoward Hinnant 
963e519524SHoward Hinnant system_clock::time_point
97d53d8152SHoward Hinnant system_clock::from_time_t(time_t t) _NOEXCEPT
983e519524SHoward Hinnant {
993e519524SHoward Hinnant     return system_clock::time_point(seconds(t));
1003e519524SHoward Hinnant }
1013e519524SHoward Hinnant 
102c59e5851SJonathan Roelofs #ifndef _LIBCPP_HAS_NO_MONOTONIC_CLOCK
1033dc6455fSHoward Hinnant // steady_clock
1044f0f708bSEd Schouten //
1054f0f708bSEd Schouten // Warning:  If this is not truly steady, then it is non-conforming.  It is
1064f0f708bSEd Schouten //  better for it to not exist and have the rest of libc++ use system_clock
1074f0f708bSEd Schouten //  instead.
1083e519524SHoward Hinnant 
10916694b5dSHoward Hinnant const bool steady_clock::is_steady;
11016694b5dSHoward Hinnant 
11178f51f95SSaleem Abdulrasool #if defined(__APPLE__)
1124f0f708bSEd Schouten 
113e5423739SBruno Cardoso Lopes // Darwin libc versions >= 1133 provide ns precision via CLOCK_UPTIME_RAW
114c55ac105SEric Fiselier #if defined(_LIBCPP_USE_CLOCK_GETTIME) && defined(CLOCK_UPTIME_RAW)
115e5423739SBruno Cardoso Lopes steady_clock::time_point
116e5423739SBruno Cardoso Lopes steady_clock::now() _NOEXCEPT
117e5423739SBruno Cardoso Lopes {
118e5423739SBruno Cardoso Lopes     struct timespec tp;
119e5423739SBruno Cardoso Lopes     if (0 != clock_gettime(CLOCK_UPTIME_RAW, &tp))
120e5423739SBruno Cardoso Lopes         __throw_system_error(errno, "clock_gettime(CLOCK_UPTIME_RAW) failed");
121e5423739SBruno Cardoso Lopes     return time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec));
122e5423739SBruno Cardoso Lopes }
123e5423739SBruno Cardoso Lopes 
124e5423739SBruno Cardoso Lopes #else
1253e519524SHoward Hinnant //   mach_absolute_time() * MachInfo.numer / MachInfo.denom is the number of
1263e519524SHoward Hinnant //   nanoseconds since the computer booted up.  MachInfo.numer and MachInfo.denom
1273e519524SHoward Hinnant //   are run time constants supplied by the OS.  This clock has no relationship
1283e519524SHoward Hinnant //   to the Gregorian calendar.  It's main use is as a high resolution timer.
1293e519524SHoward Hinnant 
1303e519524SHoward Hinnant // MachInfo.numer / MachInfo.denom is often 1 on the latest equipment.  Specialize
1313e519524SHoward Hinnant //   for that case as an optimization.
1323e519524SHoward Hinnant 
1333e519524SHoward Hinnant static
1343dc6455fSHoward Hinnant steady_clock::rep
1353dc6455fSHoward Hinnant steady_simplified()
1363e519524SHoward Hinnant {
137c206366fSHoward Hinnant     return static_cast<steady_clock::rep>(mach_absolute_time());
1383e519524SHoward Hinnant }
1393e519524SHoward Hinnant 
1403e519524SHoward Hinnant static
1413e519524SHoward Hinnant double
1423dc6455fSHoward Hinnant compute_steady_factor()
1433e519524SHoward Hinnant {
1443e519524SHoward Hinnant     mach_timebase_info_data_t MachInfo;
1453e519524SHoward Hinnant     mach_timebase_info(&MachInfo);
1463e519524SHoward Hinnant     return static_cast<double>(MachInfo.numer) / MachInfo.denom;
1473e519524SHoward Hinnant }
1483e519524SHoward Hinnant 
1493e519524SHoward Hinnant static
1503dc6455fSHoward Hinnant steady_clock::rep
1513dc6455fSHoward Hinnant steady_full()
1523e519524SHoward Hinnant {
1533dc6455fSHoward Hinnant     static const double factor = compute_steady_factor();
1543dc6455fSHoward Hinnant     return static_cast<steady_clock::rep>(mach_absolute_time() * factor);
1553e519524SHoward Hinnant }
1563e519524SHoward Hinnant 
1573dc6455fSHoward Hinnant typedef steady_clock::rep (*FP)();
1583e519524SHoward Hinnant 
1593e519524SHoward Hinnant static
1603e519524SHoward Hinnant FP
1613dc6455fSHoward Hinnant init_steady_clock()
1623e519524SHoward Hinnant {
1633e519524SHoward Hinnant     mach_timebase_info_data_t MachInfo;
1643e519524SHoward Hinnant     mach_timebase_info(&MachInfo);
1653e519524SHoward Hinnant     if (MachInfo.numer == MachInfo.denom)
1663dc6455fSHoward Hinnant         return &steady_simplified;
1673dc6455fSHoward Hinnant     return &steady_full;
1683e519524SHoward Hinnant }
1693e519524SHoward Hinnant 
1703dc6455fSHoward Hinnant steady_clock::time_point
171d53d8152SHoward Hinnant steady_clock::now() _NOEXCEPT
1723e519524SHoward Hinnant {
1733dc6455fSHoward Hinnant     static FP fp = init_steady_clock();
1743e519524SHoward Hinnant     return time_point(duration(fp()));
1753e519524SHoward Hinnant }
176c55ac105SEric Fiselier #endif // defined(_LIBCPP_USE_CLOCK_GETTIME) && defined(CLOCK_UPTIME_RAW)
1773e519524SHoward Hinnant 
178b2826a1dSSaleem Abdulrasool #elif defined(_LIBCPP_WIN32API)
17978f51f95SSaleem Abdulrasool 
180*ecad92b0SMarshall Clow // https://msdn.microsoft.com/en-us/library/windows/desktop/ms644905(v=vs.85).aspx says:
181*ecad92b0SMarshall Clow //    If the function fails, the return value is zero. <snip>
182*ecad92b0SMarshall Clow //    On systems that run Windows XP or later, the function will always succeed
183*ecad92b0SMarshall Clow //      and will thus never return zero.
184*ecad92b0SMarshall Clow 
185*ecad92b0SMarshall Clow static LARGE_INTEGER
186*ecad92b0SMarshall Clow __QueryPerformanceFrequency()
187*ecad92b0SMarshall Clow {
188*ecad92b0SMarshall Clow 	LARGE_INTEGER val;
189*ecad92b0SMarshall Clow 	(void) QueryPerformanceFrequency(&val);
190*ecad92b0SMarshall Clow 	return val;
191*ecad92b0SMarshall Clow }
192*ecad92b0SMarshall Clow 
19378f51f95SSaleem Abdulrasool steady_clock::time_point
19478f51f95SSaleem Abdulrasool steady_clock::now() _NOEXCEPT
19578f51f95SSaleem Abdulrasool {
196*ecad92b0SMarshall Clow   static const LARGE_INTEGER freq = __QueryPerformanceFrequency();
19778f51f95SSaleem Abdulrasool 
198*ecad92b0SMarshall Clow   LARGE_INTEGER counter = __QueryPerformanceFrequency();
199cfc01154SSaleem Abdulrasool   return time_point(duration(counter.QuadPart * nano::den / freq.QuadPart));
20078f51f95SSaleem Abdulrasool }
20178f51f95SSaleem Abdulrasool 
20278f51f95SSaleem Abdulrasool #elif defined(CLOCK_MONOTONIC)
20378f51f95SSaleem Abdulrasool 
204e5423739SBruno Cardoso Lopes // On Apple platforms only CLOCK_UPTIME_RAW or mach_absolute_time are able to
205e5423739SBruno Cardoso Lopes // time functions in the nanosecond range. Thus, they are the only acceptable
206e5423739SBruno Cardoso Lopes // implementations of steady_clock.
207e5423739SBruno Cardoso Lopes #ifdef __APPLE__
208e5423739SBruno Cardoso Lopes #error "Never use CLOCK_MONOTONIC for steady_clock::now on Apple platforms"
209e5423739SBruno Cardoso Lopes #endif
210e5423739SBruno Cardoso Lopes 
21178f51f95SSaleem Abdulrasool steady_clock::time_point
21278f51f95SSaleem Abdulrasool steady_clock::now() _NOEXCEPT
21378f51f95SSaleem Abdulrasool {
21478f51f95SSaleem Abdulrasool     struct timespec tp;
21578f51f95SSaleem Abdulrasool     if (0 != clock_gettime(CLOCK_MONOTONIC, &tp))
21678f51f95SSaleem Abdulrasool         __throw_system_error(errno, "clock_gettime(CLOCK_MONOTONIC) failed");
21778f51f95SSaleem Abdulrasool     return time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec));
21878f51f95SSaleem Abdulrasool }
21978f51f95SSaleem Abdulrasool 
2204f0f708bSEd Schouten #else
2214f0f708bSEd Schouten #error "Monotonic clock not implemented"
2224f0f708bSEd Schouten #endif
223128ba719SHoward Hinnant 
224c59e5851SJonathan Roelofs #endif // !_LIBCPP_HAS_NO_MONOTONIC_CLOCK
225c59e5851SJonathan Roelofs 
2263e519524SHoward Hinnant }
2273e519524SHoward Hinnant 
2283e519524SHoward Hinnant _LIBCPP_END_NAMESPACE_STD
229