1 //===------------------------- chrono.cpp ---------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
10 #include "cerrno" // errno
11 #include "system_error" // __throw_system_error
12 #include <time.h> // clock_gettime, CLOCK_MONOTONIC and CLOCK_REALTIME
13 #include "include/apple_availability.h"
15 #if !defined(__APPLE__)
16 #define _LIBCPP_USE_CLOCK_GETTIME
19 #if defined(_LIBCPP_WIN32API)
20 #define WIN32_LEAN_AND_MEAN
23 #if _WIN32_WINNT >= _WIN32_WINNT_WIN8
24 #include <winapifamily.h>
27 #if !defined(CLOCK_REALTIME) || !defined(_LIBCPP_USE_CLOCK_GETTIME)
28 #include <sys/time.h> // for gettimeofday and timeval
29 #endif // !defined(CLOCK_REALTIME)
30 #endif // defined(_LIBCPP_WIN32API)
32 #if !defined(_LIBCPP_HAS_NO_MONOTONIC_CLOCK)
34 #include <mach/mach_time.h> // mach_absolute_time, mach_timebase_info_data_t
35 #elif !defined(_LIBCPP_WIN32API) && !defined(CLOCK_MONOTONIC)
36 #error "Monotonic clock not implemented"
40 #if defined(__ELF__) && defined(_LIBCPP_LINK_RT_LIB)
41 #pragma comment(lib, "rt")
44 _LIBCPP_BEGIN_NAMESPACE_STD
51 const bool system_clock::is_steady;
53 system_clock::time_point
54 system_clock::now() _NOEXCEPT
56 #if defined(_LIBCPP_WIN32API)
57 // FILETIME is in 100ns units
58 using filetime_duration =
59 _VSTD::chrono::duration<__int64,
60 _VSTD::ratio_multiply<_VSTD::ratio<100, 1>,
61 nanoseconds::period>>;
63 // The Windows epoch is Jan 1 1601, the Unix epoch Jan 1 1970.
64 static _LIBCPP_CONSTEXPR const seconds nt_to_unix_epoch{11644473600};
67 #if _WIN32_WINNT >= _WIN32_WINNT_WIN8
68 #if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
69 GetSystemTimePreciseAsFileTime(&ft);
71 GetSystemTimeAsFileTime(&ft);
74 GetSystemTimeAsFileTime(&ft);
77 filetime_duration d{(static_cast<__int64>(ft.dwHighDateTime) << 32) |
78 static_cast<__int64>(ft.dwLowDateTime)};
79 return time_point(duration_cast<duration>(d - nt_to_unix_epoch));
81 #if defined(_LIBCPP_USE_CLOCK_GETTIME) && defined(CLOCK_REALTIME)
83 if (0 != clock_gettime(CLOCK_REALTIME, &tp))
84 __throw_system_error(errno, "clock_gettime(CLOCK_REALTIME) failed");
85 return time_point(seconds(tp.tv_sec) + microseconds(tp.tv_nsec / 1000));
89 return time_point(seconds(tv.tv_sec) + microseconds(tv.tv_usec));
90 #endif // _LIBCPP_USE_CLOCK_GETTIME && CLOCK_REALTIME
95 system_clock::to_time_t(const time_point& t) _NOEXCEPT
97 return time_t(duration_cast<seconds>(t.time_since_epoch()).count());
100 system_clock::time_point
101 system_clock::from_time_t(time_t t) _NOEXCEPT
103 return system_clock::time_point(seconds(t));
106 #ifndef _LIBCPP_HAS_NO_MONOTONIC_CLOCK
109 // Warning: If this is not truly steady, then it is non-conforming. It is
110 // better for it to not exist and have the rest of libc++ use system_clock
113 const bool steady_clock::is_steady;
115 #if defined(__APPLE__)
117 // Darwin libc versions >= 1133 provide ns precision via CLOCK_UPTIME_RAW
118 #if defined(_LIBCPP_USE_CLOCK_GETTIME) && defined(CLOCK_UPTIME_RAW)
119 steady_clock::time_point
120 steady_clock::now() _NOEXCEPT
123 if (0 != clock_gettime(CLOCK_UPTIME_RAW, &tp))
124 __throw_system_error(errno, "clock_gettime(CLOCK_UPTIME_RAW) failed");
125 return time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec));
129 // mach_absolute_time() * MachInfo.numer / MachInfo.denom is the number of
130 // nanoseconds since the computer booted up. MachInfo.numer and MachInfo.denom
131 // are run time constants supplied by the OS. This clock has no relationship
132 // to the Gregorian calendar. It's main use is as a high resolution timer.
134 // MachInfo.numer / MachInfo.denom is often 1 on the latest equipment. Specialize
135 // for that case as an optimization.
141 return static_cast<steady_clock::rep>(mach_absolute_time());
146 compute_steady_factor()
148 mach_timebase_info_data_t MachInfo;
149 mach_timebase_info(&MachInfo);
150 return static_cast<double>(MachInfo.numer) / MachInfo.denom;
157 static const double factor = compute_steady_factor();
158 return static_cast<steady_clock::rep>(mach_absolute_time() * factor);
161 typedef steady_clock::rep (*FP)();
167 mach_timebase_info_data_t MachInfo;
168 mach_timebase_info(&MachInfo);
169 if (MachInfo.numer == MachInfo.denom)
170 return &steady_simplified;
174 steady_clock::time_point
175 steady_clock::now() _NOEXCEPT
177 static FP fp = init_steady_clock();
178 return time_point(duration(fp()));
180 #endif // defined(_LIBCPP_USE_CLOCK_GETTIME) && defined(CLOCK_UPTIME_RAW)
182 #elif defined(_LIBCPP_WIN32API)
184 // https://msdn.microsoft.com/en-us/library/windows/desktop/ms644905(v=vs.85).aspx says:
185 // If the function fails, the return value is zero. <snip>
186 // On systems that run Windows XP or later, the function will always succeed
187 // and will thus never return zero.
190 __QueryPerformanceFrequency()
193 (void) QueryPerformanceFrequency(&val);
197 steady_clock::time_point
198 steady_clock::now() _NOEXCEPT
200 static const LARGE_INTEGER freq = __QueryPerformanceFrequency();
202 LARGE_INTEGER counter;
203 (void) QueryPerformanceCounter(&counter);
204 return time_point(duration(counter.QuadPart * nano::den / freq.QuadPart));
207 #elif defined(CLOCK_MONOTONIC)
209 // On Apple platforms only CLOCK_UPTIME_RAW or mach_absolute_time are able to
210 // time functions in the nanosecond range. Thus, they are the only acceptable
211 // implementations of steady_clock.
213 #error "Never use CLOCK_MONOTONIC for steady_clock::now on Apple platforms"
216 steady_clock::time_point
217 steady_clock::now() _NOEXCEPT
220 if (0 != clock_gettime(CLOCK_MONOTONIC, &tp))
221 __throw_system_error(errno, "clock_gettime(CLOCK_MONOTONIC) failed");
222 return time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec));
226 #error "Monotonic clock not implemented"
229 #endif // !_LIBCPP_HAS_NO_MONOTONIC_CLOCK
233 _LIBCPP_END_NAMESPACE_STD