xref: /freebsd/contrib/llvm-project/libcxx/src/chrono.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1349cc55cSDimitry Andric //===----------------------------------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
9fe6060f1SDimitry Andric #if defined(__MVS__)
10fe6060f1SDimitry Andric // As part of monotonic clock support on z/OS we need macro _LARGE_TIME_API
11fe6060f1SDimitry Andric // to be defined before any system header to include definition of struct timespec64.
12fe6060f1SDimitry Andric #  define _LARGE_TIME_API
13fe6060f1SDimitry Andric #endif
14fe6060f1SDimitry Andric 
1506c3fb27SDimitry Andric #include <__system_error/system_error.h>
1681ad6265SDimitry Andric #include <cerrno> // errno
1781ad6265SDimitry Andric #include <chrono>
18fe6060f1SDimitry Andric 
19fe6060f1SDimitry Andric #if defined(__MVS__)
20fe6060f1SDimitry Andric #  include <__support/ibm/gettod_zos.h> // gettimeofdayMonotonic
21fe6060f1SDimitry Andric #endif
22fe6060f1SDimitry Andric 
230b57cec5SDimitry Andric #include "include/apple_availability.h"
24cb14a3feSDimitry Andric #include <time.h> // clock_gettime and CLOCK_{MONOTONIC,REALTIME,MONOTONIC_RAW}
250b57cec5SDimitry Andric 
265ffd83dbSDimitry Andric #if __has_include(<unistd.h>)
2706c3fb27SDimitry Andric #  include <unistd.h> // _POSIX_TIMERS
285ffd83dbSDimitry Andric #endif
295ffd83dbSDimitry Andric 
30e8d8bef9SDimitry Andric #if __has_include(<sys/time.h>)
31e8d8bef9SDimitry Andric #  include <sys/time.h> // for gettimeofday and timeval
32e8d8bef9SDimitry Andric #endif
33e8d8bef9SDimitry Andric 
343a079333SDimitry Andric // OpenBSD does not have a fully conformant suite of POSIX timers, but
353a079333SDimitry Andric // it does have clock_gettime and CLOCK_MONOTONIC which is all we need.
363a079333SDimitry Andric #if defined(__APPLE__) || defined(__gnu_hurd__) || defined(__OpenBSD__) || (defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0)
3706c3fb27SDimitry Andric #  define _LIBCPP_HAS_CLOCK_GETTIME
385ffd83dbSDimitry Andric #endif
390b57cec5SDimitry Andric 
400b57cec5SDimitry Andric #if defined(_LIBCPP_WIN32API)
410b57cec5SDimitry Andric #  define WIN32_LEAN_AND_MEAN
420b57cec5SDimitry Andric #  define VC_EXTRA_LEAN
430b57cec5SDimitry Andric #  include <windows.h>
440b57cec5SDimitry Andric #  if _WIN32_WINNT >= _WIN32_WINNT_WIN8
450b57cec5SDimitry Andric #    include <winapifamily.h>
460b57cec5SDimitry Andric #  endif
470b57cec5SDimitry Andric #endif // defined(_LIBCPP_WIN32API)
480b57cec5SDimitry Andric 
4904eeddc0SDimitry Andric #if defined(__Fuchsia__)
5004eeddc0SDimitry Andric #  include <zircon/syscalls.h>
5104eeddc0SDimitry Andric #endif
5204eeddc0SDimitry Andric 
53e8d8bef9SDimitry Andric #if __has_include(<mach/mach_time.h>)
54e8d8bef9SDimitry Andric #  include <mach/mach_time.h>
55e8d8bef9SDimitry Andric #endif
56e8d8bef9SDimitry Andric 
57480093f4SDimitry Andric #if defined(__ELF__) && defined(_LIBCPP_LINK_RT_LIB)
580b57cec5SDimitry Andric #  pragma comment(lib, "rt")
590b57cec5SDimitry Andric #endif
600b57cec5SDimitry Andric 
610b57cec5SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
620b57cec5SDimitry Andric 
63cb14a3feSDimitry Andric namespace chrono {
640b57cec5SDimitry Andric 
65e8d8bef9SDimitry Andric //
660b57cec5SDimitry Andric // system_clock
67e8d8bef9SDimitry Andric //
680b57cec5SDimitry Andric 
690b57cec5SDimitry Andric #if defined(_LIBCPP_WIN32API)
70e8d8bef9SDimitry Andric 
71349cc55cSDimitry Andric #  if _WIN32_WINNT < _WIN32_WINNT_WIN8
72349cc55cSDimitry Andric 
73349cc55cSDimitry Andric namespace {
74349cc55cSDimitry Andric 
75349cc55cSDimitry Andric typedef void(WINAPI* GetSystemTimeAsFileTimePtr)(LPFILETIME);
76349cc55cSDimitry Andric 
77349cc55cSDimitry Andric class GetSystemTimeInit {
78349cc55cSDimitry Andric public:
GetSystemTimeInit()79349cc55cSDimitry Andric   GetSystemTimeInit() {
80*0fca6ea1SDimitry Andric     fp = (GetSystemTimeAsFileTimePtr)(void*)GetProcAddress(
81*0fca6ea1SDimitry Andric         GetModuleHandleW(L"kernel32.dll"), "GetSystemTimePreciseAsFileTime");
82349cc55cSDimitry Andric     if (fp == nullptr)
83349cc55cSDimitry Andric       fp = GetSystemTimeAsFileTime;
84349cc55cSDimitry Andric   }
85349cc55cSDimitry Andric   GetSystemTimeAsFileTimePtr fp;
86349cc55cSDimitry Andric };
87349cc55cSDimitry Andric 
880eae32dcSDimitry Andric // Pretend we're inside a system header so the compiler doesn't flag the use of the init_priority
890eae32dcSDimitry Andric // attribute with a value that's reserved for the implementation (we're the implementation).
900eae32dcSDimitry Andric #    include "chrono_system_time_init.h"
91349cc55cSDimitry Andric } // namespace
92349cc55cSDimitry Andric 
93349cc55cSDimitry Andric #  endif
94349cc55cSDimitry Andric 
__libcpp_system_clock_now()95e8d8bef9SDimitry Andric static system_clock::time_point __libcpp_system_clock_now() {
960b57cec5SDimitry Andric   // FILETIME is in 100ns units
970b57cec5SDimitry Andric   using filetime_duration =
98cb14a3feSDimitry Andric       std::chrono::duration<__int64, std::ratio_multiply<std::ratio<100, 1>, nanoseconds::period>>;
990b57cec5SDimitry Andric 
1000b57cec5SDimitry Andric   // The Windows epoch is Jan 1 1601, the Unix epoch Jan 1 1970.
1015f757f3fSDimitry Andric   static constexpr const seconds nt_to_unix_epoch{11644473600};
1020b57cec5SDimitry Andric 
1030b57cec5SDimitry Andric   FILETIME ft;
104349cc55cSDimitry Andric #  if (_WIN32_WINNT >= _WIN32_WINNT_WIN8 && WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)) ||                      \
105349cc55cSDimitry Andric       (_WIN32_WINNT >= _WIN32_WINNT_WIN10)
1060b57cec5SDimitry Andric   GetSystemTimePreciseAsFileTime(&ft);
107349cc55cSDimitry Andric #  elif !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
1080b57cec5SDimitry Andric   GetSystemTimeAsFileTime(&ft);
109349cc55cSDimitry Andric #  else
110349cc55cSDimitry Andric   GetSystemTimeAsFileTimeFunc.fp(&ft);
1110b57cec5SDimitry Andric #  endif
1120b57cec5SDimitry Andric 
113cb14a3feSDimitry Andric   filetime_duration d{(static_cast<__int64>(ft.dwHighDateTime) << 32) | static_cast<__int64>(ft.dwLowDateTime)};
114e8d8bef9SDimitry Andric   return system_clock::time_point(duration_cast<system_clock::duration>(d - nt_to_unix_epoch));
115e8d8bef9SDimitry Andric }
116e8d8bef9SDimitry Andric 
11706c3fb27SDimitry Andric #elif defined(_LIBCPP_HAS_CLOCK_GETTIME)
118e8d8bef9SDimitry Andric 
119e8d8bef9SDimitry Andric static system_clock::time_point __libcpp_system_clock_now() {
1200b57cec5SDimitry Andric   struct timespec tp;
1210b57cec5SDimitry Andric   if (0 != clock_gettime(CLOCK_REALTIME, &tp))
1220b57cec5SDimitry Andric     __throw_system_error(errno, "clock_gettime(CLOCK_REALTIME) failed");
123e8d8bef9SDimitry Andric   return system_clock::time_point(seconds(tp.tv_sec) + microseconds(tp.tv_nsec / 1000));
124e8d8bef9SDimitry Andric }
125e8d8bef9SDimitry Andric 
1260b57cec5SDimitry Andric #else
127e8d8bef9SDimitry Andric 
128e8d8bef9SDimitry Andric static system_clock::time_point __libcpp_system_clock_now() {
1290b57cec5SDimitry Andric   timeval tv;
1300b57cec5SDimitry Andric   gettimeofday(&tv, 0);
131e8d8bef9SDimitry Andric   return system_clock::time_point(seconds(tv.tv_sec) + microseconds(tv.tv_usec));
132e8d8bef9SDimitry Andric }
133e8d8bef9SDimitry Andric 
1340b57cec5SDimitry Andric #endif
135e8d8bef9SDimitry Andric 
136e8d8bef9SDimitry Andric const bool system_clock::is_steady;
137e8d8bef9SDimitry Andric 
now()138cb14a3feSDimitry Andric system_clock::time_point system_clock::now() noexcept { return __libcpp_system_clock_now(); }
1390b57cec5SDimitry Andric 
to_time_t(const time_point & t)140cb14a3feSDimitry Andric time_t system_clock::to_time_t(const time_point& t) noexcept {
1410b57cec5SDimitry Andric   return time_t(duration_cast<seconds>(t.time_since_epoch()).count());
1420b57cec5SDimitry Andric }
1430b57cec5SDimitry Andric 
from_time_t(time_t t)144cb14a3feSDimitry Andric system_clock::time_point system_clock::from_time_t(time_t t) noexcept { return system_clock::time_point(seconds(t)); }
1450b57cec5SDimitry Andric 
146e8d8bef9SDimitry Andric //
1470b57cec5SDimitry Andric // steady_clock
1480b57cec5SDimitry Andric //
1490b57cec5SDimitry Andric // Warning:  If this is not truly steady, then it is non-conforming.  It is
1500b57cec5SDimitry Andric //  better for it to not exist and have the rest of libc++ use system_clock
1510b57cec5SDimitry Andric //  instead.
152e8d8bef9SDimitry Andric //
1530b57cec5SDimitry Andric 
154e8d8bef9SDimitry Andric #ifndef _LIBCPP_HAS_NO_MONOTONIC_CLOCK
1550b57cec5SDimitry Andric 
1560b57cec5SDimitry Andric #  if defined(__APPLE__)
1570b57cec5SDimitry Andric 
1585ffd83dbSDimitry Andric // On Apple platforms, only CLOCK_UPTIME_RAW, CLOCK_MONOTONIC_RAW or
1595ffd83dbSDimitry Andric // mach_absolute_time are able to time functions in the nanosecond range.
1605ffd83dbSDimitry Andric // Furthermore, only CLOCK_MONOTONIC_RAW is truly monotonic, because it
1615ffd83dbSDimitry Andric // also counts cycles when the system is asleep. Thus, it is the only
1625ffd83dbSDimitry Andric // acceptable implementation of steady_clock.
__libcpp_steady_clock_now()163e8d8bef9SDimitry Andric static steady_clock::time_point __libcpp_steady_clock_now() {
1640b57cec5SDimitry Andric   struct timespec tp;
1655ffd83dbSDimitry Andric   if (0 != clock_gettime(CLOCK_MONOTONIC_RAW, &tp))
1665ffd83dbSDimitry Andric     __throw_system_error(errno, "clock_gettime(CLOCK_MONOTONIC_RAW) failed");
167e8d8bef9SDimitry Andric   return steady_clock::time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec));
1680b57cec5SDimitry Andric }
1690b57cec5SDimitry Andric 
1700b57cec5SDimitry Andric #  elif defined(_LIBCPP_WIN32API)
1710b57cec5SDimitry Andric 
1720b57cec5SDimitry Andric // https://msdn.microsoft.com/en-us/library/windows/desktop/ms644905(v=vs.85).aspx says:
1730b57cec5SDimitry Andric //    If the function fails, the return value is zero. <snip>
1740b57cec5SDimitry Andric //    On systems that run Windows XP or later, the function will always succeed
1750b57cec5SDimitry Andric //      and will thus never return zero.
1760b57cec5SDimitry Andric 
__QueryPerformanceFrequency()177cb14a3feSDimitry Andric static LARGE_INTEGER __QueryPerformanceFrequency() {
1780b57cec5SDimitry Andric   LARGE_INTEGER val;
1790b57cec5SDimitry Andric   (void)QueryPerformanceFrequency(&val);
1800b57cec5SDimitry Andric   return val;
1810b57cec5SDimitry Andric }
1820b57cec5SDimitry Andric 
__libcpp_steady_clock_now()183e8d8bef9SDimitry Andric static steady_clock::time_point __libcpp_steady_clock_now() {
1840b57cec5SDimitry Andric   static const LARGE_INTEGER freq = __QueryPerformanceFrequency();
1850b57cec5SDimitry Andric 
1860b57cec5SDimitry Andric   LARGE_INTEGER counter;
1870b57cec5SDimitry Andric   (void)QueryPerformanceCounter(&counter);
188e8d8bef9SDimitry Andric   auto seconds   = counter.QuadPart / freq.QuadPart;
189e8d8bef9SDimitry Andric   auto fractions = counter.QuadPart % freq.QuadPart;
190e8d8bef9SDimitry Andric   auto dur       = seconds * nano::den + fractions * nano::den / freq.QuadPart;
191e8d8bef9SDimitry Andric   return steady_clock::time_point(steady_clock::duration(dur));
1920b57cec5SDimitry Andric }
1930b57cec5SDimitry Andric 
194fe6060f1SDimitry Andric #  elif defined(__MVS__)
195fe6060f1SDimitry Andric 
__libcpp_steady_clock_now()196fe6060f1SDimitry Andric static steady_clock::time_point __libcpp_steady_clock_now() {
197fe6060f1SDimitry Andric   struct timespec64 ts;
198fe6060f1SDimitry Andric   if (0 != gettimeofdayMonotonic(&ts))
199fe6060f1SDimitry Andric     __throw_system_error(errno, "failed to obtain time of day");
200fe6060f1SDimitry Andric 
201fe6060f1SDimitry Andric   return steady_clock::time_point(seconds(ts.tv_sec) + nanoseconds(ts.tv_nsec));
202fe6060f1SDimitry Andric }
203fe6060f1SDimitry Andric 
20404eeddc0SDimitry Andric #  elif defined(__Fuchsia__)
20504eeddc0SDimitry Andric 
__libcpp_steady_clock_now()20604eeddc0SDimitry Andric static steady_clock::time_point __libcpp_steady_clock_now() noexcept {
20704eeddc0SDimitry Andric   // Implicitly link against the vDSO system call ABI without
20804eeddc0SDimitry Andric   // requiring the final link to specify -lzircon explicitly when
20904eeddc0SDimitry Andric   // statically linking libc++.
21004eeddc0SDimitry Andric #    pragma comment(lib, "zircon")
21104eeddc0SDimitry Andric 
21204eeddc0SDimitry Andric   return steady_clock::time_point(nanoseconds(_zx_clock_get_monotonic()));
21304eeddc0SDimitry Andric }
21404eeddc0SDimitry Andric 
21506c3fb27SDimitry Andric #  elif defined(_LIBCPP_HAS_CLOCK_GETTIME)
2160b57cec5SDimitry Andric 
__libcpp_steady_clock_now()217e8d8bef9SDimitry Andric static steady_clock::time_point __libcpp_steady_clock_now() {
2180b57cec5SDimitry Andric   struct timespec tp;
2190b57cec5SDimitry Andric   if (0 != clock_gettime(CLOCK_MONOTONIC, &tp))
2200b57cec5SDimitry Andric     __throw_system_error(errno, "clock_gettime(CLOCK_MONOTONIC) failed");
221e8d8bef9SDimitry Andric   return steady_clock::time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec));
2220b57cec5SDimitry Andric }
2230b57cec5SDimitry Andric 
2240b57cec5SDimitry Andric #  else
225e8d8bef9SDimitry Andric #    error "Monotonic clock not implemented on this platform"
2260b57cec5SDimitry Andric #  endif
2270b57cec5SDimitry Andric 
228e8d8bef9SDimitry Andric const bool steady_clock::is_steady;
229e8d8bef9SDimitry Andric 
now()230cb14a3feSDimitry Andric steady_clock::time_point steady_clock::now() noexcept { return __libcpp_steady_clock_now(); }
231e8d8bef9SDimitry Andric 
2320b57cec5SDimitry Andric #endif // !_LIBCPP_HAS_NO_MONOTONIC_CLOCK
2330b57cec5SDimitry Andric 
234cb14a3feSDimitry Andric } // namespace chrono
2350b57cec5SDimitry Andric 
2360b57cec5SDimitry Andric _LIBCPP_END_NAMESPACE_STD
237