xref: /llvm-project-15.0.7/libcxx/src/chrono.cpp (revision 3dee12e4)
1 //===------------------------- chrono.cpp ---------------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "chrono"
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"
14 
15 #if !defined(__APPLE__)
16 #define _LIBCPP_USE_CLOCK_GETTIME
17 #endif // __APPLE__
18 
19 #if defined(_LIBCPP_WIN32API)
20 #define WIN32_LEAN_AND_MEAN
21 #define VC_EXTRA_LEAN
22 #include <windows.h>
23 #if _WIN32_WINNT >= _WIN32_WINNT_WIN8
24 #include <winapifamily.h>
25 #endif
26 #else
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)
31 
32 #if !defined(_LIBCPP_HAS_NO_MONOTONIC_CLOCK)
33 #if __APPLE__
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"
37 #endif
38 #endif
39 
40 _LIBCPP_BEGIN_NAMESPACE_STD
41 
42 namespace chrono
43 {
44 
45 // system_clock
46 
47 const bool system_clock::is_steady;
48 
49 system_clock::time_point
50 system_clock::now() _NOEXCEPT
51 {
52 #if defined(_LIBCPP_WIN32API)
53   // FILETIME is in 100ns units
54   using filetime_duration =
55       _VSTD::chrono::duration<__int64,
56                               _VSTD::ratio_multiply<_VSTD::ratio<100, 1>,
57                                                     nanoseconds::period>>;
58 
59   // The Windows epoch is Jan 1 1601, the Unix epoch Jan 1 1970.
60   static _LIBCPP_CONSTEXPR const seconds nt_to_unix_epoch{11644473600};
61 
62   FILETIME ft;
63 #if _WIN32_WINNT >= _WIN32_WINNT_WIN8
64 #if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
65   GetSystemTimePreciseAsFileTime(&ft);
66 #else
67   GetSystemTimeAsFileTime(&ft);
68 #endif
69 #else
70   GetSystemTimeAsFileTime(&ft);
71 #endif
72 
73   filetime_duration d{(static_cast<__int64>(ft.dwHighDateTime) << 32) |
74                        static_cast<__int64>(ft.dwLowDateTime)};
75   return time_point(duration_cast<duration>(d - nt_to_unix_epoch));
76 #else
77 #if defined(_LIBCPP_USE_CLOCK_GETTIME) && defined(CLOCK_REALTIME)
78   struct timespec tp;
79   if (0 != clock_gettime(CLOCK_REALTIME, &tp))
80     __throw_system_error(errno, "clock_gettime(CLOCK_REALTIME) failed");
81   return time_point(seconds(tp.tv_sec) + microseconds(tp.tv_nsec / 1000));
82 #else
83     timeval tv;
84     gettimeofday(&tv, 0);
85     return time_point(seconds(tv.tv_sec) + microseconds(tv.tv_usec));
86 #endif // _LIBCPP_USE_CLOCK_GETTIME && CLOCK_REALTIME
87 #endif
88 }
89 
90 time_t
91 system_clock::to_time_t(const time_point& t) _NOEXCEPT
92 {
93     return time_t(duration_cast<seconds>(t.time_since_epoch()).count());
94 }
95 
96 system_clock::time_point
97 system_clock::from_time_t(time_t t) _NOEXCEPT
98 {
99     return system_clock::time_point(seconds(t));
100 }
101 
102 #ifndef _LIBCPP_HAS_NO_MONOTONIC_CLOCK
103 // steady_clock
104 //
105 // Warning:  If this is not truly steady, then it is non-conforming.  It is
106 //  better for it to not exist and have the rest of libc++ use system_clock
107 //  instead.
108 
109 const bool steady_clock::is_steady;
110 
111 #if defined(__APPLE__)
112 
113 // Darwin libc versions >= 1133 provide ns precision via CLOCK_UPTIME_RAW
114 #if defined(_LIBCPP_USE_CLOCK_GETTIME) && defined(CLOCK_UPTIME_RAW)
115 steady_clock::time_point
116 steady_clock::now() _NOEXCEPT
117 {
118     struct timespec tp;
119     if (0 != clock_gettime(CLOCK_UPTIME_RAW, &tp))
120         __throw_system_error(errno, "clock_gettime(CLOCK_UPTIME_RAW) failed");
121     return time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec));
122 }
123 
124 #else
125 //   mach_absolute_time() * MachInfo.numer / MachInfo.denom is the number of
126 //   nanoseconds since the computer booted up.  MachInfo.numer and MachInfo.denom
127 //   are run time constants supplied by the OS.  This clock has no relationship
128 //   to the Gregorian calendar.  It's main use is as a high resolution timer.
129 
130 // MachInfo.numer / MachInfo.denom is often 1 on the latest equipment.  Specialize
131 //   for that case as an optimization.
132 
133 static
134 steady_clock::rep
135 steady_simplified()
136 {
137     return static_cast<steady_clock::rep>(mach_absolute_time());
138 }
139 
140 static
141 double
142 compute_steady_factor()
143 {
144     mach_timebase_info_data_t MachInfo;
145     mach_timebase_info(&MachInfo);
146     return static_cast<double>(MachInfo.numer) / MachInfo.denom;
147 }
148 
149 static
150 steady_clock::rep
151 steady_full()
152 {
153     static const double factor = compute_steady_factor();
154     return static_cast<steady_clock::rep>(mach_absolute_time() * factor);
155 }
156 
157 typedef steady_clock::rep (*FP)();
158 
159 static
160 FP
161 init_steady_clock()
162 {
163     mach_timebase_info_data_t MachInfo;
164     mach_timebase_info(&MachInfo);
165     if (MachInfo.numer == MachInfo.denom)
166         return &steady_simplified;
167     return &steady_full;
168 }
169 
170 steady_clock::time_point
171 steady_clock::now() _NOEXCEPT
172 {
173     static FP fp = init_steady_clock();
174     return time_point(duration(fp()));
175 }
176 #endif // defined(_LIBCPP_USE_CLOCK_GETTIME) && defined(CLOCK_UPTIME_RAW)
177 
178 #elif defined(_LIBCPP_WIN32API)
179 
180 // https://msdn.microsoft.com/en-us/library/windows/desktop/ms644905(v=vs.85).aspx says:
181 //    If the function fails, the return value is zero. <snip>
182 //    On systems that run Windows XP or later, the function will always succeed
183 //      and will thus never return zero.
184 
185 static LARGE_INTEGER
186 __QueryPerformanceFrequency()
187 {
188 	LARGE_INTEGER val;
189 	(void) QueryPerformanceFrequency(&val);
190 	return val;
191 }
192 
193 steady_clock::time_point
194 steady_clock::now() _NOEXCEPT
195 {
196   static const LARGE_INTEGER freq = __QueryPerformanceFrequency();
197 
198   LARGE_INTEGER counter;
199   (void) QueryPerformanceCounter(&counter);
200   return time_point(duration(counter.QuadPart * nano::den / freq.QuadPart));
201 }
202 
203 #elif defined(CLOCK_MONOTONIC)
204 
205 // On Apple platforms only CLOCK_UPTIME_RAW or mach_absolute_time are able to
206 // time functions in the nanosecond range. Thus, they are the only acceptable
207 // implementations of steady_clock.
208 #ifdef __APPLE__
209 #error "Never use CLOCK_MONOTONIC for steady_clock::now on Apple platforms"
210 #endif
211 
212 steady_clock::time_point
213 steady_clock::now() _NOEXCEPT
214 {
215     struct timespec tp;
216     if (0 != clock_gettime(CLOCK_MONOTONIC, &tp))
217         __throw_system_error(errno, "clock_gettime(CLOCK_MONOTONIC) failed");
218     return time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec));
219 }
220 
221 #else
222 #error "Monotonic clock not implemented"
223 #endif
224 
225 #endif // !_LIBCPP_HAS_NO_MONOTONIC_CLOCK
226 
227 }
228 
229 _LIBCPP_END_NAMESPACE_STD
230