blob: b5dd0eef3962ceae1212310dbe2845697c9ab8ac [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
Martin Storsjö2567ea82021-06-16 15:22:28 +030034#if !defined(__APPLE__) && defined(_POSIX_TIMERS) && _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
Sizhe Zhaoa6efafc2021-08-26 23:11:54 +030066#if _WIN32_WINNT < _WIN32_WINNT_WIN8
67
68namespace {
69
70typedef void(WINAPI *GetSystemTimeAsFileTimePtr)(LPFILETIME);
71
72class GetSystemTimeInit {
73public:
74 GetSystemTimeInit() {
75 fp = (GetSystemTimeAsFileTimePtr)GetProcAddress(
76 GetModuleHandleW(L"kernel32.dll"), "GetSystemTimePreciseAsFileTime");
77 if (fp == nullptr)
78 fp = GetSystemTimeAsFileTime;
79 }
80 GetSystemTimeAsFileTimePtr fp;
81};
82
Martin Storsjö0f84cd12021-10-07 10:00:46 +030083# 83 "chrono.cpp" 1 3
Sizhe Zhaoa6efafc2021-08-26 23:11:54 +030084GetSystemTimeInit GetSystemTimeAsFileTimeFunc _LIBCPP_INIT_PRIORITY_MAX;
Martin Storsjö0f84cd12021-10-07 10:00:46 +030085# 85 "chrono.cpp" 2
Sizhe Zhaoa6efafc2021-08-26 23:11:54 +030086} // namespace
87
88#endif
89
Louis Dionnedf6a9a82020-11-04 10:14:13 -050090static system_clock::time_point __libcpp_system_clock_now() {
Saleem Abdulrasool9e367702017-01-01 22:04:38 +000091 // FILETIME is in 100ns units
92 using filetime_duration =
93 _VSTD::chrono::duration<__int64,
94 _VSTD::ratio_multiply<_VSTD::ratio<100, 1>,
95 nanoseconds::period>>;
Saleem Abdulrasool594b0422017-01-01 20:20:41 +000096
Saleem Abdulrasool9e367702017-01-01 22:04:38 +000097 // The Windows epoch is Jan 1 1601, the Unix epoch Jan 1 1970.
Saleem Abdulrasool25c0d402017-01-02 18:41:50 +000098 static _LIBCPP_CONSTEXPR const seconds nt_to_unix_epoch{11644473600};
Saleem Abdulrasool9e367702017-01-01 22:04:38 +000099
100 FILETIME ft;
Sizhe Zhaoa6efafc2021-08-26 23:11:54 +0300101#if (_WIN32_WINNT >= _WIN32_WINNT_WIN8 && WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)) || \
102 (_WIN32_WINNT >= _WIN32_WINNT_WIN10)
Saleem Abdulrasool9e367702017-01-01 22:04:38 +0000103 GetSystemTimePreciseAsFileTime(&ft);
Sizhe Zhaoa6efafc2021-08-26 23:11:54 +0300104#elif !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
Saleem Abdulrasool9e367702017-01-01 22:04:38 +0000105 GetSystemTimeAsFileTime(&ft);
Sizhe Zhaoa6efafc2021-08-26 23:11:54 +0300106#else
107 GetSystemTimeAsFileTimeFunc.fp(&ft);
Saleem Abdulrasool594b0422017-01-01 20:20:41 +0000108#endif
Saleem Abdulrasool9e367702017-01-01 22:04:38 +0000109
110 filetime_duration d{(static_cast<__int64>(ft.dwHighDateTime) << 32) |
111 static_cast<__int64>(ft.dwLowDateTime)};
Louis Dionnedf6a9a82020-11-04 10:14:13 -0500112 return system_clock::time_point(duration_cast<system_clock::duration>(d - nt_to_unix_epoch));
113}
114
115#elif defined(CLOCK_REALTIME) && defined(_LIBCPP_USE_CLOCK_GETTIME)
116
117static system_clock::time_point __libcpp_system_clock_now() {
Eric Fiselier7eba47e2018-07-25 20:51:49 +0000118 struct timespec tp;
119 if (0 != clock_gettime(CLOCK_REALTIME, &tp))
120 __throw_system_error(errno, "clock_gettime(CLOCK_REALTIME) failed");
Louis Dionnedf6a9a82020-11-04 10:14:13 -0500121 return system_clock::time_point(seconds(tp.tv_sec) + microseconds(tp.tv_nsec / 1000));
122}
123
Bruno Cardoso Lopesd0027142017-01-09 19:21:48 +0000124#else
Louis Dionnedf6a9a82020-11-04 10:14:13 -0500125
126static system_clock::time_point __libcpp_system_clock_now() {
Howard Hinnantc51e1022010-05-11 19:42:16 +0000127 timeval tv;
128 gettimeofday(&tv, 0);
Louis Dionnedf6a9a82020-11-04 10:14:13 -0500129 return system_clock::time_point(seconds(tv.tv_sec) + microseconds(tv.tv_usec));
130}
131
Hafiz Abid Qadeerde28d5f2020-10-05 17:28:25 -0400132#endif
Louis Dionnedf6a9a82020-11-04 10:14:13 -0500133
134const bool system_clock::is_steady;
135
136system_clock::time_point
Louis Dionne65358e12021-03-01 12:09:45 -0500137system_clock::now() noexcept
Louis Dionnedf6a9a82020-11-04 10:14:13 -0500138{
139 return __libcpp_system_clock_now();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000140}
141
142time_t
Louis Dionne65358e12021-03-01 12:09:45 -0500143system_clock::to_time_t(const time_point& t) noexcept
Howard Hinnantc51e1022010-05-11 19:42:16 +0000144{
145 return time_t(duration_cast<seconds>(t.time_since_epoch()).count());
146}
147
148system_clock::time_point
Louis Dionne65358e12021-03-01 12:09:45 -0500149system_clock::from_time_t(time_t t) noexcept
Howard Hinnantc51e1022010-05-11 19:42:16 +0000150{
151 return system_clock::time_point(seconds(t));
152}
153
Louis Dionnedf6a9a82020-11-04 10:14:13 -0500154//
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000155// steady_clock
Ed Schoutenf7805c32015-05-14 20:54:18 +0000156//
157// Warning: If this is not truly steady, then it is non-conforming. It is
158// better for it to not exist and have the rest of libc++ use system_clock
159// instead.
Louis Dionnedf6a9a82020-11-04 10:14:13 -0500160//
Howard Hinnantc51e1022010-05-11 19:42:16 +0000161
Louis Dionnedf6a9a82020-11-04 10:14:13 -0500162#ifndef _LIBCPP_HAS_NO_MONOTONIC_CLOCK
Howard Hinnant2c45cb42012-12-12 21:14:28 +0000163
Saleem Abdulrasoolae249922017-01-01 20:20:43 +0000164#if defined(__APPLE__)
Ed Schoutenf7805c32015-05-14 20:54:18 +0000165
Louis Dionne69897ab2021-01-21 17:53:29 -0500166// TODO(ldionne):
167// This old implementation of steady_clock is retained until Chrome drops supports
168// for macOS < 10.12. The issue is that they link libc++ statically into their
169// application, which means that libc++ must support being built for such deployment
170// targets. See https://llvm.org/D74489 for details.
171#if (defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 101200) || \
172 (defined(__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__ < 100000) || \
173 (defined(__ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__ < 100000) || \
174 (defined(__ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__ < 30000)
175# define _LIBCPP_USE_OLD_MACH_ABSOLUTE_TIME
176#endif
177
178#if defined(_LIBCPP_USE_OLD_MACH_ABSOLUTE_TIME)
179
180// mach_absolute_time() * MachInfo.numer / MachInfo.denom is the number of
181// nanoseconds since the computer booted up. MachInfo.numer and MachInfo.denom
182// are run time constants supplied by the OS. This clock has no relationship
183// to the Gregorian calendar. It's main use is as a high resolution timer.
184
185// MachInfo.numer / MachInfo.denom is often 1 on the latest equipment. Specialize
186// for that case as an optimization.
187
188static steady_clock::rep steady_simplified() {
189 return static_cast<steady_clock::rep>(mach_absolute_time());
190}
191static double compute_steady_factor() {
192 mach_timebase_info_data_t MachInfo;
193 mach_timebase_info(&MachInfo);
194 return static_cast<double>(MachInfo.numer) / MachInfo.denom;
195}
196
197static steady_clock::rep steady_full() {
198 static const double factor = compute_steady_factor();
199 return static_cast<steady_clock::rep>(mach_absolute_time() * factor);
200}
201
202typedef steady_clock::rep (*FP)();
203
204static FP init_steady_clock() {
205 mach_timebase_info_data_t MachInfo;
206 mach_timebase_info(&MachInfo);
207 if (MachInfo.numer == MachInfo.denom)
208 return &steady_simplified;
209 return &steady_full;
210}
211
212static steady_clock::time_point __libcpp_steady_clock_now() {
213 static FP fp = init_steady_clock();
214 return steady_clock::time_point(steady_clock::duration(fp()));
215}
216
217#else // vvvvv default behavior for Apple platforms vvvvv
218
Louis Dionne678dc852020-02-12 17:01:19 +0100219// On Apple platforms, only CLOCK_UPTIME_RAW, CLOCK_MONOTONIC_RAW or
220// mach_absolute_time are able to time functions in the nanosecond range.
221// Furthermore, only CLOCK_MONOTONIC_RAW is truly monotonic, because it
222// also counts cycles when the system is asleep. Thus, it is the only
223// acceptable implementation of steady_clock.
Louis Dionnedf6a9a82020-11-04 10:14:13 -0500224static steady_clock::time_point __libcpp_steady_clock_now() {
Bruno Cardoso Lopesd0027142017-01-09 19:21:48 +0000225 struct timespec tp;
Louis Dionnea2dac172020-02-10 18:30:43 +0100226 if (0 != clock_gettime(CLOCK_MONOTONIC_RAW, &tp))
227 __throw_system_error(errno, "clock_gettime(CLOCK_MONOTONIC_RAW) failed");
Louis Dionnedf6a9a82020-11-04 10:14:13 -0500228 return steady_clock::time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec));
Bruno Cardoso Lopesd0027142017-01-09 19:21:48 +0000229}
230
Louis Dionne69897ab2021-01-21 17:53:29 -0500231#endif
232
Saleem Abdulrasoolee302e42017-01-03 21:53:51 +0000233#elif defined(_LIBCPP_WIN32API)
Saleem Abdulrasoolae249922017-01-01 20:20:43 +0000234
Marshall Clow132ccac2019-04-01 17:23:30 +0000235// https://msdn.microsoft.com/en-us/library/windows/desktop/ms644905(v=vs.85).aspx says:
236// If the function fails, the return value is zero. <snip>
Stephan T. Lavavejfb39ad72019-10-23 11:45:36 -0700237// On systems that run Windows XP or later, the function will always succeed
Marshall Clow132ccac2019-04-01 17:23:30 +0000238// and will thus never return zero.
239
240static LARGE_INTEGER
241__QueryPerformanceFrequency()
242{
Louis Dionnedf6a9a82020-11-04 10:14:13 -0500243 LARGE_INTEGER val;
244 (void) QueryPerformanceFrequency(&val);
245 return val;
Marshall Clow132ccac2019-04-01 17:23:30 +0000246}
247
Louis Dionnedf6a9a82020-11-04 10:14:13 -0500248static steady_clock::time_point __libcpp_steady_clock_now() {
Marshall Clow132ccac2019-04-01 17:23:30 +0000249 static const LARGE_INTEGER freq = __QueryPerformanceFrequency();
Saleem Abdulrasoolae249922017-01-01 20:20:43 +0000250
Marshall Clow5dbb0d22019-04-02 14:00:36 +0000251 LARGE_INTEGER counter;
252 (void) QueryPerformanceCounter(&counter);
Martin Storsjö94011152020-12-17 15:40:06 +0200253 auto seconds = counter.QuadPart / freq.QuadPart;
254 auto fractions = counter.QuadPart % freq.QuadPart;
255 auto dur = seconds * nano::den + fractions * nano::den / freq.QuadPart;
256 return steady_clock::time_point(steady_clock::duration(dur));
Saleem Abdulrasoolae249922017-01-01 20:20:43 +0000257}
258
Zbigniew Sarbinowski15410732021-02-12 18:36:15 +0000259#elif defined(__MVS__)
260
261static steady_clock::time_point __libcpp_steady_clock_now() {
262 struct timespec64 ts;
263 if (0 != gettimeofdayMonotonic(&ts))
264 __throw_system_error(errno, "failed to obtain time of day");
265
266 return steady_clock::time_point(seconds(ts.tv_sec) + nanoseconds(ts.tv_nsec));
267}
268
Saleem Abdulrasoolae249922017-01-01 20:20:43 +0000269#elif defined(CLOCK_MONOTONIC)
270
Louis Dionnedf6a9a82020-11-04 10:14:13 -0500271static steady_clock::time_point __libcpp_steady_clock_now() {
Saleem Abdulrasoolae249922017-01-01 20:20:43 +0000272 struct timespec tp;
273 if (0 != clock_gettime(CLOCK_MONOTONIC, &tp))
274 __throw_system_error(errno, "clock_gettime(CLOCK_MONOTONIC) failed");
Louis Dionnedf6a9a82020-11-04 10:14:13 -0500275 return steady_clock::time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec));
Saleem Abdulrasoolae249922017-01-01 20:20:43 +0000276}
277
Ed Schoutenf7805c32015-05-14 20:54:18 +0000278#else
Louis Dionnedf6a9a82020-11-04 10:14:13 -0500279# error "Monotonic clock not implemented on this platform"
Ed Schoutenf7805c32015-05-14 20:54:18 +0000280#endif
Howard Hinnant155c2af2010-05-24 17:49:41 +0000281
Louis Dionnedf6a9a82020-11-04 10:14:13 -0500282const bool steady_clock::is_steady;
283
284steady_clock::time_point
Louis Dionne65358e12021-03-01 12:09:45 -0500285steady_clock::now() noexcept
Louis Dionnedf6a9a82020-11-04 10:14:13 -0500286{
287 return __libcpp_steady_clock_now();
288}
289
Jonathan Roelofscce96eb2014-09-02 21:14:38 +0000290#endif // !_LIBCPP_HAS_NO_MONOTONIC_CLOCK
291
Howard Hinnantc51e1022010-05-11 19:42:16 +0000292}
293
294_LIBCPP_END_NAMESPACE_STD