blob: b586f6fa1b146985ab941375a17a740109f9a7ac [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001//===------------------------- chrono.cpp ---------------------------------===//
2//
Chandler Carruthd2012102019-01-19 10:56:40 +00003// 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
Howard Hinnantc51e1022010-05-11 19:42:16 +00006//
7//===----------------------------------------------------------------------===//
Eric Fiselierfc779202016-07-01 23:31:55 +00008
Zbigniew Sarbinowski15410732021-02-12 18:36:15 +00009#if defined(__MVS__)
10// As part of monotonic clock support on z/OS we need macro _LARGE_TIME_API
11// to be defined before any system header to include definition of struct timespec64.
12#define _LARGE_TIME_API
13#endif
14
Howard Hinnantc51e1022010-05-11 19:42:16 +000015#include "chrono"
Marshall Clowccb92572015-06-23 14:45:02 +000016#include "cerrno" // errno
17#include "system_error" // __throw_system_error
Zbigniew Sarbinowski15410732021-02-12 18:36:15 +000018
19#if defined(__MVS__)
20#include <__support/ibm/gettod_zos.h> // gettimeofdayMonotonic
21#endif
22
Louis Dionne678dc852020-02-12 17:01:19 +010023#include <time.h> // clock_gettime and CLOCK_{MONOTONIC,REALTIME,MONOTONIC_RAW}
Eric Fiselier7eba47e2018-07-25 20:51:49 +000024#include "include/apple_availability.h"
Ed Schoutenf7805c32015-05-14 20:54:18 +000025
John Brawnfeea69a2020-05-14 11:51:13 +010026#if __has_include(<unistd.h>)
Hafiz Abid Qadeerde28d5f2020-10-05 17:28:25 -040027# include <unistd.h>
28#endif
29
30#if __has_include(<sys/time.h>)
31# include <sys/time.h> // for gettimeofday and timeval
Mara Sophie Grosch1bd707a2020-05-07 12:10:33 -040032#endif
33
34#if !defined(__APPLE__) && _POSIX_TIMERS > 0
Hafiz Abid Qadeerde28d5f2020-10-05 17:28:25 -040035# define _LIBCPP_USE_CLOCK_GETTIME
Mara Sophie Grosch1bd707a2020-05-07 12:10:33 -040036#endif
Bruno Cardoso Lopesd0027142017-01-09 19:21:48 +000037
Saleem Abdulrasoolee302e42017-01-03 21:53:51 +000038#if defined(_LIBCPP_WIN32API)
Louis Dionne678dc852020-02-12 17:01:19 +010039# define WIN32_LEAN_AND_MEAN
40# define VC_EXTRA_LEAN
41# include <windows.h>
42# if _WIN32_WINNT >= _WIN32_WINNT_WIN8
43# include <winapifamily.h>
44# endif
Saleem Abdulrasoolee302e42017-01-03 21:53:51 +000045#endif // defined(_LIBCPP_WIN32API)
Ed Schoutenf7805c32015-05-14 20:54:18 +000046
Louis Dionne69897ab2021-01-21 17:53:29 -050047#if __has_include(<mach/mach_time.h>)
48# include <mach/mach_time.h>
49#endif
50
Michał Górny8d676fb2019-12-02 11:49:20 +010051#if defined(__ELF__) && defined(_LIBCPP_LINK_RT_LIB)
Louis Dionne678dc852020-02-12 17:01:19 +010052# pragma comment(lib, "rt")
Petr Hosek99575aa2019-05-30 01:34:41 +000053#endif
54
Howard Hinnantc51e1022010-05-11 19:42:16 +000055_LIBCPP_BEGIN_NAMESPACE_STD
56
57namespace chrono
58{
59
Louis Dionnedf6a9a82020-11-04 10:14:13 -050060//
Howard Hinnantc51e1022010-05-11 19:42:16 +000061// system_clock
Louis Dionnedf6a9a82020-11-04 10:14:13 -050062//
Howard Hinnantc51e1022010-05-11 19:42:16 +000063
Saleem Abdulrasoolee302e42017-01-03 21:53:51 +000064#if defined(_LIBCPP_WIN32API)
Louis Dionnedf6a9a82020-11-04 10:14:13 -050065
66static system_clock::time_point __libcpp_system_clock_now() {
Saleem Abdulrasool9e367702017-01-01 22:04:38 +000067 // FILETIME is in 100ns units
68 using filetime_duration =
69 _VSTD::chrono::duration<__int64,
70 _VSTD::ratio_multiply<_VSTD::ratio<100, 1>,
71 nanoseconds::period>>;
Saleem Abdulrasool594b0422017-01-01 20:20:41 +000072
Saleem Abdulrasool9e367702017-01-01 22:04:38 +000073 // The Windows epoch is Jan 1 1601, the Unix epoch Jan 1 1970.
Saleem Abdulrasool25c0d402017-01-02 18:41:50 +000074 static _LIBCPP_CONSTEXPR const seconds nt_to_unix_epoch{11644473600};
Saleem Abdulrasool9e367702017-01-01 22:04:38 +000075
76 FILETIME ft;
Louis Dionnedf6a9a82020-11-04 10:14:13 -050077#if _WIN32_WINNT >= _WIN32_WINNT_WIN8 && WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
Saleem Abdulrasool9e367702017-01-01 22:04:38 +000078 GetSystemTimePreciseAsFileTime(&ft);
Saleem Abdulrasool594b0422017-01-01 20:20:41 +000079#else
Saleem Abdulrasool9e367702017-01-01 22:04:38 +000080 GetSystemTimeAsFileTime(&ft);
Saleem Abdulrasool594b0422017-01-01 20:20:41 +000081#endif
Saleem Abdulrasool9e367702017-01-01 22:04:38 +000082
83 filetime_duration d{(static_cast<__int64>(ft.dwHighDateTime) << 32) |
84 static_cast<__int64>(ft.dwLowDateTime)};
Louis Dionnedf6a9a82020-11-04 10:14:13 -050085 return system_clock::time_point(duration_cast<system_clock::duration>(d - nt_to_unix_epoch));
86}
87
88#elif defined(CLOCK_REALTIME) && defined(_LIBCPP_USE_CLOCK_GETTIME)
89
90static system_clock::time_point __libcpp_system_clock_now() {
Eric Fiselier7eba47e2018-07-25 20:51:49 +000091 struct timespec tp;
92 if (0 != clock_gettime(CLOCK_REALTIME, &tp))
93 __throw_system_error(errno, "clock_gettime(CLOCK_REALTIME) failed");
Louis Dionnedf6a9a82020-11-04 10:14:13 -050094 return system_clock::time_point(seconds(tp.tv_sec) + microseconds(tp.tv_nsec / 1000));
95}
96
Bruno Cardoso Lopesd0027142017-01-09 19:21:48 +000097#else
Louis Dionnedf6a9a82020-11-04 10:14:13 -050098
99static system_clock::time_point __libcpp_system_clock_now() {
Howard Hinnantc51e1022010-05-11 19:42:16 +0000100 timeval tv;
101 gettimeofday(&tv, 0);
Louis Dionnedf6a9a82020-11-04 10:14:13 -0500102 return system_clock::time_point(seconds(tv.tv_sec) + microseconds(tv.tv_usec));
103}
104
Hafiz Abid Qadeerde28d5f2020-10-05 17:28:25 -0400105#endif
Louis Dionnedf6a9a82020-11-04 10:14:13 -0500106
107const bool system_clock::is_steady;
108
109system_clock::time_point
110system_clock::now() _NOEXCEPT
111{
112 return __libcpp_system_clock_now();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000113}
114
115time_t
Howard Hinnantaa54ac42011-05-28 18:34:36 +0000116system_clock::to_time_t(const time_point& t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000117{
118 return time_t(duration_cast<seconds>(t.time_since_epoch()).count());
119}
120
121system_clock::time_point
Howard Hinnantaa54ac42011-05-28 18:34:36 +0000122system_clock::from_time_t(time_t t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000123{
124 return system_clock::time_point(seconds(t));
125}
126
Louis Dionnedf6a9a82020-11-04 10:14:13 -0500127//
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000128// steady_clock
Ed Schoutenf7805c32015-05-14 20:54:18 +0000129//
130// Warning: If this is not truly steady, then it is non-conforming. It is
131// better for it to not exist and have the rest of libc++ use system_clock
132// instead.
Louis Dionnedf6a9a82020-11-04 10:14:13 -0500133//
Howard Hinnantc51e1022010-05-11 19:42:16 +0000134
Louis Dionnedf6a9a82020-11-04 10:14:13 -0500135#ifndef _LIBCPP_HAS_NO_MONOTONIC_CLOCK
Howard Hinnant2c45cb42012-12-12 21:14:28 +0000136
Saleem Abdulrasoolae249922017-01-01 20:20:43 +0000137#if defined(__APPLE__)
Ed Schoutenf7805c32015-05-14 20:54:18 +0000138
Louis Dionne69897ab2021-01-21 17:53:29 -0500139// TODO(ldionne):
140// This old implementation of steady_clock is retained until Chrome drops supports
141// for macOS < 10.12. The issue is that they link libc++ statically into their
142// application, which means that libc++ must support being built for such deployment
143// targets. See https://llvm.org/D74489 for details.
144#if (defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 101200) || \
145 (defined(__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__ < 100000) || \
146 (defined(__ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__ < 100000) || \
147 (defined(__ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__ < 30000)
148# define _LIBCPP_USE_OLD_MACH_ABSOLUTE_TIME
149#endif
150
151#if defined(_LIBCPP_USE_OLD_MACH_ABSOLUTE_TIME)
152
153// mach_absolute_time() * MachInfo.numer / MachInfo.denom is the number of
154// nanoseconds since the computer booted up. MachInfo.numer and MachInfo.denom
155// are run time constants supplied by the OS. This clock has no relationship
156// to the Gregorian calendar. It's main use is as a high resolution timer.
157
158// MachInfo.numer / MachInfo.denom is often 1 on the latest equipment. Specialize
159// for that case as an optimization.
160
161static steady_clock::rep steady_simplified() {
162 return static_cast<steady_clock::rep>(mach_absolute_time());
163}
164static double compute_steady_factor() {
165 mach_timebase_info_data_t MachInfo;
166 mach_timebase_info(&MachInfo);
167 return static_cast<double>(MachInfo.numer) / MachInfo.denom;
168}
169
170static steady_clock::rep steady_full() {
171 static const double factor = compute_steady_factor();
172 return static_cast<steady_clock::rep>(mach_absolute_time() * factor);
173}
174
175typedef steady_clock::rep (*FP)();
176
177static FP init_steady_clock() {
178 mach_timebase_info_data_t MachInfo;
179 mach_timebase_info(&MachInfo);
180 if (MachInfo.numer == MachInfo.denom)
181 return &steady_simplified;
182 return &steady_full;
183}
184
185static steady_clock::time_point __libcpp_steady_clock_now() {
186 static FP fp = init_steady_clock();
187 return steady_clock::time_point(steady_clock::duration(fp()));
188}
189
190#else // vvvvv default behavior for Apple platforms vvvvv
191
Louis Dionne678dc852020-02-12 17:01:19 +0100192// On Apple platforms, only CLOCK_UPTIME_RAW, CLOCK_MONOTONIC_RAW or
193// mach_absolute_time are able to time functions in the nanosecond range.
194// Furthermore, only CLOCK_MONOTONIC_RAW is truly monotonic, because it
195// also counts cycles when the system is asleep. Thus, it is the only
196// acceptable implementation of steady_clock.
Louis Dionnedf6a9a82020-11-04 10:14:13 -0500197static steady_clock::time_point __libcpp_steady_clock_now() {
Bruno Cardoso Lopesd0027142017-01-09 19:21:48 +0000198 struct timespec tp;
Louis Dionnea2dac172020-02-10 18:30:43 +0100199 if (0 != clock_gettime(CLOCK_MONOTONIC_RAW, &tp))
200 __throw_system_error(errno, "clock_gettime(CLOCK_MONOTONIC_RAW) failed");
Louis Dionnedf6a9a82020-11-04 10:14:13 -0500201 return steady_clock::time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec));
Bruno Cardoso Lopesd0027142017-01-09 19:21:48 +0000202}
203
Louis Dionne69897ab2021-01-21 17:53:29 -0500204#endif
205
Saleem Abdulrasoolee302e42017-01-03 21:53:51 +0000206#elif defined(_LIBCPP_WIN32API)
Saleem Abdulrasoolae249922017-01-01 20:20:43 +0000207
Marshall Clow132ccac2019-04-01 17:23:30 +0000208// https://msdn.microsoft.com/en-us/library/windows/desktop/ms644905(v=vs.85).aspx says:
209// If the function fails, the return value is zero. <snip>
Stephan T. Lavavejfb39ad72019-10-23 11:45:36 -0700210// On systems that run Windows XP or later, the function will always succeed
Marshall Clow132ccac2019-04-01 17:23:30 +0000211// and will thus never return zero.
212
213static LARGE_INTEGER
214__QueryPerformanceFrequency()
215{
Louis Dionnedf6a9a82020-11-04 10:14:13 -0500216 LARGE_INTEGER val;
217 (void) QueryPerformanceFrequency(&val);
218 return val;
Marshall Clow132ccac2019-04-01 17:23:30 +0000219}
220
Louis Dionnedf6a9a82020-11-04 10:14:13 -0500221static steady_clock::time_point __libcpp_steady_clock_now() {
Marshall Clow132ccac2019-04-01 17:23:30 +0000222 static const LARGE_INTEGER freq = __QueryPerformanceFrequency();
Saleem Abdulrasoolae249922017-01-01 20:20:43 +0000223
Marshall Clow5dbb0d22019-04-02 14:00:36 +0000224 LARGE_INTEGER counter;
225 (void) QueryPerformanceCounter(&counter);
Martin Storsjö94011152020-12-17 15:40:06 +0200226 auto seconds = counter.QuadPart / freq.QuadPart;
227 auto fractions = counter.QuadPart % freq.QuadPart;
228 auto dur = seconds * nano::den + fractions * nano::den / freq.QuadPart;
229 return steady_clock::time_point(steady_clock::duration(dur));
Saleem Abdulrasoolae249922017-01-01 20:20:43 +0000230}
231
Zbigniew Sarbinowski15410732021-02-12 18:36:15 +0000232#elif defined(__MVS__)
233
234static steady_clock::time_point __libcpp_steady_clock_now() {
235 struct timespec64 ts;
236 if (0 != gettimeofdayMonotonic(&ts))
237 __throw_system_error(errno, "failed to obtain time of day");
238
239 return steady_clock::time_point(seconds(ts.tv_sec) + nanoseconds(ts.tv_nsec));
240}
241
Saleem Abdulrasoolae249922017-01-01 20:20:43 +0000242#elif defined(CLOCK_MONOTONIC)
243
Louis Dionnedf6a9a82020-11-04 10:14:13 -0500244static steady_clock::time_point __libcpp_steady_clock_now() {
Saleem Abdulrasoolae249922017-01-01 20:20:43 +0000245 struct timespec tp;
246 if (0 != clock_gettime(CLOCK_MONOTONIC, &tp))
247 __throw_system_error(errno, "clock_gettime(CLOCK_MONOTONIC) failed");
Louis Dionnedf6a9a82020-11-04 10:14:13 -0500248 return steady_clock::time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec));
Saleem Abdulrasoolae249922017-01-01 20:20:43 +0000249}
250
Ed Schoutenf7805c32015-05-14 20:54:18 +0000251#else
Louis Dionnedf6a9a82020-11-04 10:14:13 -0500252# error "Monotonic clock not implemented on this platform"
Ed Schoutenf7805c32015-05-14 20:54:18 +0000253#endif
Howard Hinnant155c2af2010-05-24 17:49:41 +0000254
Louis Dionnedf6a9a82020-11-04 10:14:13 -0500255const bool steady_clock::is_steady;
256
257steady_clock::time_point
258steady_clock::now() _NOEXCEPT
259{
260 return __libcpp_steady_clock_now();
261}
262
Jonathan Roelofscce96eb2014-09-02 21:14:38 +0000263#endif // !_LIBCPP_HAS_NO_MONOTONIC_CLOCK
264
Howard Hinnantc51e1022010-05-11 19:42:16 +0000265}
266
267_LIBCPP_END_NAMESPACE_STD