blob: 48c02c8d06822cc2065e040680145f54151bd4d6 [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
12#include <time.h> // clock_gettime, CLOCK_MONOTONIC and CLOCK_REALTIME
Eric Fiselier7eba47e2018-07-25 20:51:49 +000013#include "include/apple_availability.h"
Ed Schoutenf7805c32015-05-14 20:54:18 +000014
Mara Sophie Grosch1bd707a2020-05-07 12:10:33 -040015#if !defined(_WIN32)
16#include <unistd.h>
17#endif
18
19#if !defined(__APPLE__) && _POSIX_TIMERS > 0
Eric Fiselier7eba47e2018-07-25 20:51:49 +000020#define _LIBCPP_USE_CLOCK_GETTIME
Mara Sophie Grosch1bd707a2020-05-07 12:10:33 -040021#endif
Bruno Cardoso Lopesd0027142017-01-09 19:21:48 +000022
Saleem Abdulrasoolee302e42017-01-03 21:53:51 +000023#if defined(_LIBCPP_WIN32API)
Saleem Abdulrasool594b0422017-01-01 20:20:41 +000024#define WIN32_LEAN_AND_MEAN
25#define VC_EXTRA_LEAN
Eric Fiselierbb999f92017-05-31 22:14:05 +000026#include <windows.h>
Saleem Abdulrasool594b0422017-01-01 20:20:41 +000027#if _WIN32_WINNT >= _WIN32_WINNT_WIN8
28#include <winapifamily.h>
29#endif
30#else
Eric Fiselier7eba47e2018-07-25 20:51:49 +000031#if !defined(CLOCK_REALTIME) || !defined(_LIBCPP_USE_CLOCK_GETTIME)
Louis Dionnec420ced2020-05-04 11:39:16 -040032#include <sys/time.h> // for gettimeofday and timeval
33#endif
Saleem Abdulrasoolee302e42017-01-03 21:53:51 +000034#endif // defined(_LIBCPP_WIN32API)
Ed Schoutenf7805c32015-05-14 20:54:18 +000035
Saleem Abdulrasoolae249922017-01-01 20:20:43 +000036#if !defined(_LIBCPP_HAS_NO_MONOTONIC_CLOCK)
Ed Schoutenf7805c32015-05-14 20:54:18 +000037#if __APPLE__
38#include <mach/mach_time.h> // mach_absolute_time, mach_timebase_info_data_t
Saleem Abdulrasoolee302e42017-01-03 21:53:51 +000039#elif !defined(_LIBCPP_WIN32API) && !defined(CLOCK_MONOTONIC)
Ed Schoutenf7805c32015-05-14 20:54:18 +000040#error "Monotonic clock not implemented"
41#endif
42#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +000043
Michał Górny8d676fb2019-12-02 11:49:20 +010044#if defined(__ELF__) && defined(_LIBCPP_LINK_RT_LIB)
Petr Hosek99575aa2019-05-30 01:34:41 +000045#pragma comment(lib, "rt")
46#endif
47
Howard Hinnantc51e1022010-05-11 19:42:16 +000048_LIBCPP_BEGIN_NAMESPACE_STD
49
50namespace chrono
51{
52
53// system_clock
54
Howard Hinnant2c45cb42012-12-12 21:14:28 +000055const bool system_clock::is_steady;
56
Howard Hinnantc51e1022010-05-11 19:42:16 +000057system_clock::time_point
Howard Hinnantaa54ac42011-05-28 18:34:36 +000058system_clock::now() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +000059{
Saleem Abdulrasoolee302e42017-01-03 21:53:51 +000060#if defined(_LIBCPP_WIN32API)
Saleem Abdulrasool9e367702017-01-01 22:04:38 +000061 // FILETIME is in 100ns units
62 using filetime_duration =
63 _VSTD::chrono::duration<__int64,
64 _VSTD::ratio_multiply<_VSTD::ratio<100, 1>,
65 nanoseconds::period>>;
Saleem Abdulrasool594b0422017-01-01 20:20:41 +000066
Saleem Abdulrasool9e367702017-01-01 22:04:38 +000067 // The Windows epoch is Jan 1 1601, the Unix epoch Jan 1 1970.
Saleem Abdulrasool25c0d402017-01-02 18:41:50 +000068 static _LIBCPP_CONSTEXPR const seconds nt_to_unix_epoch{11644473600};
Saleem Abdulrasool9e367702017-01-01 22:04:38 +000069
70 FILETIME ft;
Saleem Abdulrasool594b0422017-01-01 20:20:41 +000071#if _WIN32_WINNT >= _WIN32_WINNT_WIN8
72#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
Saleem Abdulrasool9e367702017-01-01 22:04:38 +000073 GetSystemTimePreciseAsFileTime(&ft);
Saleem Abdulrasool594b0422017-01-01 20:20:41 +000074#else
Saleem Abdulrasool9e367702017-01-01 22:04:38 +000075 GetSystemTimeAsFileTime(&ft);
Saleem Abdulrasool594b0422017-01-01 20:20:41 +000076#endif
77#else
Saleem Abdulrasool9e367702017-01-01 22:04:38 +000078 GetSystemTimeAsFileTime(&ft);
Saleem Abdulrasool594b0422017-01-01 20:20:41 +000079#endif
Saleem Abdulrasool9e367702017-01-01 22:04:38 +000080
81 filetime_duration d{(static_cast<__int64>(ft.dwHighDateTime) << 32) |
82 static_cast<__int64>(ft.dwLowDateTime)};
83 return time_point(duration_cast<duration>(d - nt_to_unix_epoch));
Saleem Abdulrasool594b0422017-01-01 20:20:41 +000084#else
Eric Fiselier7eba47e2018-07-25 20:51:49 +000085#if defined(_LIBCPP_USE_CLOCK_GETTIME) && defined(CLOCK_REALTIME)
86 struct timespec tp;
87 if (0 != clock_gettime(CLOCK_REALTIME, &tp))
88 __throw_system_error(errno, "clock_gettime(CLOCK_REALTIME) failed");
89 return time_point(seconds(tp.tv_sec) + microseconds(tp.tv_nsec / 1000));
Bruno Cardoso Lopesd0027142017-01-09 19:21:48 +000090#else
Howard Hinnantc51e1022010-05-11 19:42:16 +000091 timeval tv;
92 gettimeofday(&tv, 0);
93 return time_point(seconds(tv.tv_sec) + microseconds(tv.tv_usec));
Eric Fiselier7eba47e2018-07-25 20:51:49 +000094#endif // _LIBCPP_USE_CLOCK_GETTIME && CLOCK_REALTIME
Saleem Abdulrasool594b0422017-01-01 20:20:41 +000095#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +000096}
97
98time_t
Howard Hinnantaa54ac42011-05-28 18:34:36 +000099system_clock::to_time_t(const time_point& t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000100{
101 return time_t(duration_cast<seconds>(t.time_since_epoch()).count());
102}
103
104system_clock::time_point
Howard Hinnantaa54ac42011-05-28 18:34:36 +0000105system_clock::from_time_t(time_t t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000106{
107 return system_clock::time_point(seconds(t));
108}
109
Jonathan Roelofscce96eb2014-09-02 21:14:38 +0000110#ifndef _LIBCPP_HAS_NO_MONOTONIC_CLOCK
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000111// steady_clock
Ed Schoutenf7805c32015-05-14 20:54:18 +0000112//
113// Warning: If this is not truly steady, then it is non-conforming. It is
114// better for it to not exist and have the rest of libc++ use system_clock
115// instead.
Howard Hinnantc51e1022010-05-11 19:42:16 +0000116
Howard Hinnant2c45cb42012-12-12 21:14:28 +0000117const bool steady_clock::is_steady;
118
Saleem Abdulrasoolae249922017-01-01 20:20:43 +0000119#if defined(__APPLE__)
Ed Schoutenf7805c32015-05-14 20:54:18 +0000120
Louis Dionnea2dac172020-02-10 18:30:43 +0100121// Darwin libc versions >= 1133 provide ns precision via CLOCK_MONOTONIC_RAW
122#if defined(_LIBCPP_USE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC_RAW)
Bruno Cardoso Lopesd0027142017-01-09 19:21:48 +0000123steady_clock::time_point
124steady_clock::now() _NOEXCEPT
125{
126 struct timespec tp;
Louis Dionnea2dac172020-02-10 18:30:43 +0100127 if (0 != clock_gettime(CLOCK_MONOTONIC_RAW, &tp))
128 __throw_system_error(errno, "clock_gettime(CLOCK_MONOTONIC_RAW) failed");
Bruno Cardoso Lopesd0027142017-01-09 19:21:48 +0000129 return time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec));
130}
131
132#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000133// mach_absolute_time() * MachInfo.numer / MachInfo.denom is the number of
134// nanoseconds since the computer booted up. MachInfo.numer and MachInfo.denom
135// are run time constants supplied by the OS. This clock has no relationship
136// to the Gregorian calendar. It's main use is as a high resolution timer.
137
138// MachInfo.numer / MachInfo.denom is often 1 on the latest equipment. Specialize
139// for that case as an optimization.
140
Howard Hinnantc51e1022010-05-11 19:42:16 +0000141static
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000142steady_clock::rep
143steady_simplified()
Howard Hinnantc51e1022010-05-11 19:42:16 +0000144{
Howard Hinnant28b24882011-12-01 20:21:04 +0000145 return static_cast<steady_clock::rep>(mach_absolute_time());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000146}
147
148static
149double
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000150compute_steady_factor()
Howard Hinnantc51e1022010-05-11 19:42:16 +0000151{
152 mach_timebase_info_data_t MachInfo;
153 mach_timebase_info(&MachInfo);
154 return static_cast<double>(MachInfo.numer) / MachInfo.denom;
155}
156
157static
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000158steady_clock::rep
159steady_full()
Howard Hinnantc51e1022010-05-11 19:42:16 +0000160{
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000161 static const double factor = compute_steady_factor();
162 return static_cast<steady_clock::rep>(mach_absolute_time() * factor);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000163}
164
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000165typedef steady_clock::rep (*FP)();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000166
167static
168FP
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000169init_steady_clock()
Howard Hinnantc51e1022010-05-11 19:42:16 +0000170{
171 mach_timebase_info_data_t MachInfo;
172 mach_timebase_info(&MachInfo);
173 if (MachInfo.numer == MachInfo.denom)
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000174 return &steady_simplified;
175 return &steady_full;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000176}
177
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000178steady_clock::time_point
Howard Hinnantaa54ac42011-05-28 18:34:36 +0000179steady_clock::now() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000180{
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000181 static FP fp = init_steady_clock();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000182 return time_point(duration(fp()));
183}
Louis Dionnea2dac172020-02-10 18:30:43 +0100184#endif // defined(_LIBCPP_USE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC_RAW)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000185
Saleem Abdulrasoolee302e42017-01-03 21:53:51 +0000186#elif defined(_LIBCPP_WIN32API)
Saleem Abdulrasoolae249922017-01-01 20:20:43 +0000187
Marshall Clow132ccac2019-04-01 17:23:30 +0000188// https://msdn.microsoft.com/en-us/library/windows/desktop/ms644905(v=vs.85).aspx says:
189// If the function fails, the return value is zero. <snip>
Stephan T. Lavavejfb39ad72019-10-23 11:45:36 -0700190// On systems that run Windows XP or later, the function will always succeed
Marshall Clow132ccac2019-04-01 17:23:30 +0000191// and will thus never return zero.
192
193static LARGE_INTEGER
194__QueryPerformanceFrequency()
195{
196 LARGE_INTEGER val;
197 (void) QueryPerformanceFrequency(&val);
198 return val;
199}
200
Saleem Abdulrasoolae249922017-01-01 20:20:43 +0000201steady_clock::time_point
202steady_clock::now() _NOEXCEPT
203{
Marshall Clow132ccac2019-04-01 17:23:30 +0000204 static const LARGE_INTEGER freq = __QueryPerformanceFrequency();
Saleem Abdulrasoolae249922017-01-01 20:20:43 +0000205
Marshall Clow5dbb0d22019-04-02 14:00:36 +0000206 LARGE_INTEGER counter;
207 (void) QueryPerformanceCounter(&counter);
Saleem Abdulrasool91e1d612017-01-01 22:04:36 +0000208 return time_point(duration(counter.QuadPart * nano::den / freq.QuadPart));
Saleem Abdulrasoolae249922017-01-01 20:20:43 +0000209}
210
211#elif defined(CLOCK_MONOTONIC)
212
Louis Dionnea2dac172020-02-10 18:30:43 +0100213// On Apple platforms only CLOCK_UPTIME_RAW, CLOCK_MONOTONIC_RAW or
214// mach_absolute_time are able to time functions in the nanosecond range.
215// Thus, they are the only acceptable implementations of steady_clock.
Bruno Cardoso Lopesd0027142017-01-09 19:21:48 +0000216#ifdef __APPLE__
217#error "Never use CLOCK_MONOTONIC for steady_clock::now on Apple platforms"
218#endif
219
Saleem Abdulrasoolae249922017-01-01 20:20:43 +0000220steady_clock::time_point
221steady_clock::now() _NOEXCEPT
222{
223 struct timespec tp;
224 if (0 != clock_gettime(CLOCK_MONOTONIC, &tp))
225 __throw_system_error(errno, "clock_gettime(CLOCK_MONOTONIC) failed");
226 return time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec));
227}
228
Ed Schoutenf7805c32015-05-14 20:54:18 +0000229#else
230#error "Monotonic clock not implemented"
231#endif
Howard Hinnant155c2af2010-05-24 17:49:41 +0000232
Jonathan Roelofscce96eb2014-09-02 21:14:38 +0000233#endif // !_LIBCPP_HAS_NO_MONOTONIC_CLOCK
234
Howard Hinnantc51e1022010-05-11 19:42:16 +0000235}
236
237_LIBCPP_END_NAMESPACE_STD