blob: 085fbfde26c1d75ea94f7caff5e2b845627e6a7e [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
Howard Hinnantc51e1022010-05-11 19:42:16 +00009#include "chrono"
Marshall Clowccb92572015-06-23 14:45:02 +000010#include "cerrno" // errno
11#include "system_error" // __throw_system_error
Louis Dionne678dc852020-02-12 17:01:19 +010012#include <time.h> // clock_gettime and CLOCK_{MONOTONIC,REALTIME,MONOTONIC_RAW}
Eric Fiselier7eba47e2018-07-25 20:51:49 +000013#include "include/apple_availability.h"
Ed Schoutenf7805c32015-05-14 20:54:18 +000014
John Brawnfeea69a2020-05-14 11:51:13 +010015#if __has_include(<unistd.h>)
Hafiz Abid Qadeerde28d5f2020-10-05 17:28:25 -040016# include <unistd.h>
17#endif
18
19#if __has_include(<sys/time.h>)
20# include <sys/time.h> // for gettimeofday and timeval
Mara Sophie Grosch1bd707a2020-05-07 12:10:33 -040021#endif
22
23#if !defined(__APPLE__) && _POSIX_TIMERS > 0
Hafiz Abid Qadeerde28d5f2020-10-05 17:28:25 -040024# define _LIBCPP_USE_CLOCK_GETTIME
Mara Sophie Grosch1bd707a2020-05-07 12:10:33 -040025#endif
Bruno Cardoso Lopesd0027142017-01-09 19:21:48 +000026
Saleem Abdulrasoolee302e42017-01-03 21:53:51 +000027#if defined(_LIBCPP_WIN32API)
Louis Dionne678dc852020-02-12 17:01:19 +010028# define WIN32_LEAN_AND_MEAN
29# define VC_EXTRA_LEAN
30# include <windows.h>
31# if _WIN32_WINNT >= _WIN32_WINNT_WIN8
32# include <winapifamily.h>
33# endif
Saleem Abdulrasoolee302e42017-01-03 21:53:51 +000034#endif // defined(_LIBCPP_WIN32API)
Ed Schoutenf7805c32015-05-14 20:54:18 +000035
Louis Dionne69897ab2021-01-21 17:53:29 -050036#if __has_include(<mach/mach_time.h>)
37# include <mach/mach_time.h>
38#endif
39
Michał Górny8d676fb2019-12-02 11:49:20 +010040#if defined(__ELF__) && defined(_LIBCPP_LINK_RT_LIB)
Louis Dionne678dc852020-02-12 17:01:19 +010041# pragma comment(lib, "rt")
Petr Hosek99575aa2019-05-30 01:34:41 +000042#endif
43
Howard Hinnantc51e1022010-05-11 19:42:16 +000044_LIBCPP_BEGIN_NAMESPACE_STD
45
46namespace chrono
47{
48
Louis Dionnedf6a9a82020-11-04 10:14:13 -050049//
Howard Hinnantc51e1022010-05-11 19:42:16 +000050// system_clock
Louis Dionnedf6a9a82020-11-04 10:14:13 -050051//
Howard Hinnantc51e1022010-05-11 19:42:16 +000052
Saleem Abdulrasoolee302e42017-01-03 21:53:51 +000053#if defined(_LIBCPP_WIN32API)
Louis Dionnedf6a9a82020-11-04 10:14:13 -050054
55static system_clock::time_point __libcpp_system_clock_now() {
Saleem Abdulrasool9e367702017-01-01 22:04:38 +000056 // FILETIME is in 100ns units
57 using filetime_duration =
58 _VSTD::chrono::duration<__int64,
59 _VSTD::ratio_multiply<_VSTD::ratio<100, 1>,
60 nanoseconds::period>>;
Saleem Abdulrasool594b0422017-01-01 20:20:41 +000061
Saleem Abdulrasool9e367702017-01-01 22:04:38 +000062 // The Windows epoch is Jan 1 1601, the Unix epoch Jan 1 1970.
Saleem Abdulrasool25c0d402017-01-02 18:41:50 +000063 static _LIBCPP_CONSTEXPR const seconds nt_to_unix_epoch{11644473600};
Saleem Abdulrasool9e367702017-01-01 22:04:38 +000064
65 FILETIME ft;
Louis Dionnedf6a9a82020-11-04 10:14:13 -050066#if _WIN32_WINNT >= _WIN32_WINNT_WIN8 && WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
Saleem Abdulrasool9e367702017-01-01 22:04:38 +000067 GetSystemTimePreciseAsFileTime(&ft);
Saleem Abdulrasool594b0422017-01-01 20:20:41 +000068#else
Saleem Abdulrasool9e367702017-01-01 22:04:38 +000069 GetSystemTimeAsFileTime(&ft);
Saleem Abdulrasool594b0422017-01-01 20:20:41 +000070#endif
Saleem Abdulrasool9e367702017-01-01 22:04:38 +000071
72 filetime_duration d{(static_cast<__int64>(ft.dwHighDateTime) << 32) |
73 static_cast<__int64>(ft.dwLowDateTime)};
Louis Dionnedf6a9a82020-11-04 10:14:13 -050074 return system_clock::time_point(duration_cast<system_clock::duration>(d - nt_to_unix_epoch));
75}
76
77#elif defined(CLOCK_REALTIME) && defined(_LIBCPP_USE_CLOCK_GETTIME)
78
79static system_clock::time_point __libcpp_system_clock_now() {
Eric Fiselier7eba47e2018-07-25 20:51:49 +000080 struct timespec tp;
81 if (0 != clock_gettime(CLOCK_REALTIME, &tp))
82 __throw_system_error(errno, "clock_gettime(CLOCK_REALTIME) failed");
Louis Dionnedf6a9a82020-11-04 10:14:13 -050083 return system_clock::time_point(seconds(tp.tv_sec) + microseconds(tp.tv_nsec / 1000));
84}
85
Bruno Cardoso Lopesd0027142017-01-09 19:21:48 +000086#else
Louis Dionnedf6a9a82020-11-04 10:14:13 -050087
88static system_clock::time_point __libcpp_system_clock_now() {
Howard Hinnantc51e1022010-05-11 19:42:16 +000089 timeval tv;
90 gettimeofday(&tv, 0);
Louis Dionnedf6a9a82020-11-04 10:14:13 -050091 return system_clock::time_point(seconds(tv.tv_sec) + microseconds(tv.tv_usec));
92}
93
Hafiz Abid Qadeerde28d5f2020-10-05 17:28:25 -040094#endif
Louis Dionnedf6a9a82020-11-04 10:14:13 -050095
96const bool system_clock::is_steady;
97
98system_clock::time_point
99system_clock::now() _NOEXCEPT
100{
101 return __libcpp_system_clock_now();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000102}
103
104time_t
Howard Hinnantaa54ac42011-05-28 18:34:36 +0000105system_clock::to_time_t(const time_point& t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000106{
107 return time_t(duration_cast<seconds>(t.time_since_epoch()).count());
108}
109
110system_clock::time_point
Howard Hinnantaa54ac42011-05-28 18:34:36 +0000111system_clock::from_time_t(time_t t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000112{
113 return system_clock::time_point(seconds(t));
114}
115
Louis Dionnedf6a9a82020-11-04 10:14:13 -0500116//
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000117// steady_clock
Ed Schoutenf7805c32015-05-14 20:54:18 +0000118//
119// Warning: If this is not truly steady, then it is non-conforming. It is
120// better for it to not exist and have the rest of libc++ use system_clock
121// instead.
Louis Dionnedf6a9a82020-11-04 10:14:13 -0500122//
Howard Hinnantc51e1022010-05-11 19:42:16 +0000123
Louis Dionnedf6a9a82020-11-04 10:14:13 -0500124#ifndef _LIBCPP_HAS_NO_MONOTONIC_CLOCK
Howard Hinnant2c45cb42012-12-12 21:14:28 +0000125
Saleem Abdulrasoolae249922017-01-01 20:20:43 +0000126#if defined(__APPLE__)
Ed Schoutenf7805c32015-05-14 20:54:18 +0000127
Louis Dionne69897ab2021-01-21 17:53:29 -0500128// TODO(ldionne):
129// This old implementation of steady_clock is retained until Chrome drops supports
130// for macOS < 10.12. The issue is that they link libc++ statically into their
131// application, which means that libc++ must support being built for such deployment
132// targets. See https://llvm.org/D74489 for details.
133#if (defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 101200) || \
134 (defined(__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__ < 100000) || \
135 (defined(__ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__ < 100000) || \
136 (defined(__ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__ < 30000)
137# define _LIBCPP_USE_OLD_MACH_ABSOLUTE_TIME
138#endif
139
140#if defined(_LIBCPP_USE_OLD_MACH_ABSOLUTE_TIME)
141
142// mach_absolute_time() * MachInfo.numer / MachInfo.denom is the number of
143// nanoseconds since the computer booted up. MachInfo.numer and MachInfo.denom
144// are run time constants supplied by the OS. This clock has no relationship
145// to the Gregorian calendar. It's main use is as a high resolution timer.
146
147// MachInfo.numer / MachInfo.denom is often 1 on the latest equipment. Specialize
148// for that case as an optimization.
149
150static steady_clock::rep steady_simplified() {
151 return static_cast<steady_clock::rep>(mach_absolute_time());
152}
153static double compute_steady_factor() {
154 mach_timebase_info_data_t MachInfo;
155 mach_timebase_info(&MachInfo);
156 return static_cast<double>(MachInfo.numer) / MachInfo.denom;
157}
158
159static steady_clock::rep steady_full() {
160 static const double factor = compute_steady_factor();
161 return static_cast<steady_clock::rep>(mach_absolute_time() * factor);
162}
163
164typedef steady_clock::rep (*FP)();
165
166static FP init_steady_clock() {
167 mach_timebase_info_data_t MachInfo;
168 mach_timebase_info(&MachInfo);
169 if (MachInfo.numer == MachInfo.denom)
170 return &steady_simplified;
171 return &steady_full;
172}
173
174static steady_clock::time_point __libcpp_steady_clock_now() {
175 static FP fp = init_steady_clock();
176 return steady_clock::time_point(steady_clock::duration(fp()));
177}
178
179#else // vvvvv default behavior for Apple platforms vvvvv
180
Louis Dionne678dc852020-02-12 17:01:19 +0100181// On Apple platforms, only CLOCK_UPTIME_RAW, CLOCK_MONOTONIC_RAW or
182// mach_absolute_time are able to time functions in the nanosecond range.
183// Furthermore, only CLOCK_MONOTONIC_RAW is truly monotonic, because it
184// also counts cycles when the system is asleep. Thus, it is the only
185// acceptable implementation of steady_clock.
Louis Dionnedf6a9a82020-11-04 10:14:13 -0500186static steady_clock::time_point __libcpp_steady_clock_now() {
Bruno Cardoso Lopesd0027142017-01-09 19:21:48 +0000187 struct timespec tp;
Louis Dionnea2dac172020-02-10 18:30:43 +0100188 if (0 != clock_gettime(CLOCK_MONOTONIC_RAW, &tp))
189 __throw_system_error(errno, "clock_gettime(CLOCK_MONOTONIC_RAW) failed");
Louis Dionnedf6a9a82020-11-04 10:14:13 -0500190 return steady_clock::time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec));
Bruno Cardoso Lopesd0027142017-01-09 19:21:48 +0000191}
192
Louis Dionne69897ab2021-01-21 17:53:29 -0500193#endif
194
Saleem Abdulrasoolee302e42017-01-03 21:53:51 +0000195#elif defined(_LIBCPP_WIN32API)
Saleem Abdulrasoolae249922017-01-01 20:20:43 +0000196
Marshall Clow132ccac2019-04-01 17:23:30 +0000197// https://msdn.microsoft.com/en-us/library/windows/desktop/ms644905(v=vs.85).aspx says:
198// If the function fails, the return value is zero. <snip>
Stephan T. Lavavejfb39ad72019-10-23 11:45:36 -0700199// On systems that run Windows XP or later, the function will always succeed
Marshall Clow132ccac2019-04-01 17:23:30 +0000200// and will thus never return zero.
201
202static LARGE_INTEGER
203__QueryPerformanceFrequency()
204{
Louis Dionnedf6a9a82020-11-04 10:14:13 -0500205 LARGE_INTEGER val;
206 (void) QueryPerformanceFrequency(&val);
207 return val;
Marshall Clow132ccac2019-04-01 17:23:30 +0000208}
209
Louis Dionnedf6a9a82020-11-04 10:14:13 -0500210static steady_clock::time_point __libcpp_steady_clock_now() {
Marshall Clow132ccac2019-04-01 17:23:30 +0000211 static const LARGE_INTEGER freq = __QueryPerformanceFrequency();
Saleem Abdulrasoolae249922017-01-01 20:20:43 +0000212
Marshall Clow5dbb0d22019-04-02 14:00:36 +0000213 LARGE_INTEGER counter;
214 (void) QueryPerformanceCounter(&counter);
Martin Storsjö94011152020-12-17 15:40:06 +0200215 auto seconds = counter.QuadPart / freq.QuadPart;
216 auto fractions = counter.QuadPart % freq.QuadPart;
217 auto dur = seconds * nano::den + fractions * nano::den / freq.QuadPart;
218 return steady_clock::time_point(steady_clock::duration(dur));
Saleem Abdulrasoolae249922017-01-01 20:20:43 +0000219}
220
221#elif defined(CLOCK_MONOTONIC)
222
Louis Dionnedf6a9a82020-11-04 10:14:13 -0500223static steady_clock::time_point __libcpp_steady_clock_now() {
Saleem Abdulrasoolae249922017-01-01 20:20:43 +0000224 struct timespec tp;
225 if (0 != clock_gettime(CLOCK_MONOTONIC, &tp))
226 __throw_system_error(errno, "clock_gettime(CLOCK_MONOTONIC) failed");
Louis Dionnedf6a9a82020-11-04 10:14:13 -0500227 return steady_clock::time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec));
Saleem Abdulrasoolae249922017-01-01 20:20:43 +0000228}
229
Ed Schoutenf7805c32015-05-14 20:54:18 +0000230#else
Louis Dionnedf6a9a82020-11-04 10:14:13 -0500231# error "Monotonic clock not implemented on this platform"
Ed Schoutenf7805c32015-05-14 20:54:18 +0000232#endif
Howard Hinnant155c2af2010-05-24 17:49:41 +0000233
Louis Dionnedf6a9a82020-11-04 10:14:13 -0500234const bool steady_clock::is_steady;
235
236steady_clock::time_point
237steady_clock::now() _NOEXCEPT
238{
239 return __libcpp_steady_clock_now();
240}
241
Jonathan Roelofscce96eb2014-09-02 21:14:38 +0000242#endif // !_LIBCPP_HAS_NO_MONOTONIC_CLOCK
243
Howard Hinnantc51e1022010-05-11 19:42:16 +0000244}
245
246_LIBCPP_END_NAMESPACE_STD