blob: 1419cf2f74a850dbb0476f4f54d169bc0e450a31 [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
Michał Górny8d676fb2019-12-02 11:49:20 +010036#if defined(__ELF__) && defined(_LIBCPP_LINK_RT_LIB)
Louis Dionne678dc852020-02-12 17:01:19 +010037# pragma comment(lib, "rt")
Petr Hosek99575aa2019-05-30 01:34:41 +000038#endif
39
Howard Hinnantc51e1022010-05-11 19:42:16 +000040_LIBCPP_BEGIN_NAMESPACE_STD
41
42namespace chrono
43{
44
Louis Dionnedf6a9a82020-11-04 10:14:13 -050045//
Howard Hinnantc51e1022010-05-11 19:42:16 +000046// system_clock
Louis Dionnedf6a9a82020-11-04 10:14:13 -050047//
Howard Hinnantc51e1022010-05-11 19:42:16 +000048
Saleem Abdulrasoolee302e42017-01-03 21:53:51 +000049#if defined(_LIBCPP_WIN32API)
Louis Dionnedf6a9a82020-11-04 10:14:13 -050050
51static system_clock::time_point __libcpp_system_clock_now() {
Saleem Abdulrasool9e367702017-01-01 22:04:38 +000052 // FILETIME is in 100ns units
53 using filetime_duration =
54 _VSTD::chrono::duration<__int64,
55 _VSTD::ratio_multiply<_VSTD::ratio<100, 1>,
56 nanoseconds::period>>;
Saleem Abdulrasool594b0422017-01-01 20:20:41 +000057
Saleem Abdulrasool9e367702017-01-01 22:04:38 +000058 // The Windows epoch is Jan 1 1601, the Unix epoch Jan 1 1970.
Saleem Abdulrasool25c0d402017-01-02 18:41:50 +000059 static _LIBCPP_CONSTEXPR const seconds nt_to_unix_epoch{11644473600};
Saleem Abdulrasool9e367702017-01-01 22:04:38 +000060
61 FILETIME ft;
Louis Dionnedf6a9a82020-11-04 10:14:13 -050062#if _WIN32_WINNT >= _WIN32_WINNT_WIN8 && WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
Saleem Abdulrasool9e367702017-01-01 22:04:38 +000063 GetSystemTimePreciseAsFileTime(&ft);
Saleem Abdulrasool594b0422017-01-01 20:20:41 +000064#else
Saleem Abdulrasool9e367702017-01-01 22:04:38 +000065 GetSystemTimeAsFileTime(&ft);
Saleem Abdulrasool594b0422017-01-01 20:20:41 +000066#endif
Saleem Abdulrasool9e367702017-01-01 22:04:38 +000067
68 filetime_duration d{(static_cast<__int64>(ft.dwHighDateTime) << 32) |
69 static_cast<__int64>(ft.dwLowDateTime)};
Louis Dionnedf6a9a82020-11-04 10:14:13 -050070 return system_clock::time_point(duration_cast<system_clock::duration>(d - nt_to_unix_epoch));
71}
72
73#elif defined(CLOCK_REALTIME) && defined(_LIBCPP_USE_CLOCK_GETTIME)
74
75static system_clock::time_point __libcpp_system_clock_now() {
Eric Fiselier7eba47e2018-07-25 20:51:49 +000076 struct timespec tp;
77 if (0 != clock_gettime(CLOCK_REALTIME, &tp))
78 __throw_system_error(errno, "clock_gettime(CLOCK_REALTIME) failed");
Louis Dionnedf6a9a82020-11-04 10:14:13 -050079 return system_clock::time_point(seconds(tp.tv_sec) + microseconds(tp.tv_nsec / 1000));
80}
81
Bruno Cardoso Lopesd0027142017-01-09 19:21:48 +000082#else
Louis Dionnedf6a9a82020-11-04 10:14:13 -050083
84static system_clock::time_point __libcpp_system_clock_now() {
Howard Hinnantc51e1022010-05-11 19:42:16 +000085 timeval tv;
86 gettimeofday(&tv, 0);
Louis Dionnedf6a9a82020-11-04 10:14:13 -050087 return system_clock::time_point(seconds(tv.tv_sec) + microseconds(tv.tv_usec));
88}
89
Hafiz Abid Qadeerde28d5f2020-10-05 17:28:25 -040090#endif
Louis Dionnedf6a9a82020-11-04 10:14:13 -050091
92const bool system_clock::is_steady;
93
94system_clock::time_point
95system_clock::now() _NOEXCEPT
96{
97 return __libcpp_system_clock_now();
Howard Hinnantc51e1022010-05-11 19:42:16 +000098}
99
100time_t
Howard Hinnantaa54ac42011-05-28 18:34:36 +0000101system_clock::to_time_t(const time_point& t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000102{
103 return time_t(duration_cast<seconds>(t.time_since_epoch()).count());
104}
105
106system_clock::time_point
Howard Hinnantaa54ac42011-05-28 18:34:36 +0000107system_clock::from_time_t(time_t t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000108{
109 return system_clock::time_point(seconds(t));
110}
111
Louis Dionnedf6a9a82020-11-04 10:14:13 -0500112//
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000113// steady_clock
Ed Schoutenf7805c32015-05-14 20:54:18 +0000114//
115// Warning: If this is not truly steady, then it is non-conforming. It is
116// better for it to not exist and have the rest of libc++ use system_clock
117// instead.
Louis Dionnedf6a9a82020-11-04 10:14:13 -0500118//
Howard Hinnantc51e1022010-05-11 19:42:16 +0000119
Louis Dionnedf6a9a82020-11-04 10:14:13 -0500120#ifndef _LIBCPP_HAS_NO_MONOTONIC_CLOCK
Howard Hinnant2c45cb42012-12-12 21:14:28 +0000121
Saleem Abdulrasoolae249922017-01-01 20:20:43 +0000122#if defined(__APPLE__)
Ed Schoutenf7805c32015-05-14 20:54:18 +0000123
Louis Dionne678dc852020-02-12 17:01:19 +0100124// On Apple platforms, only CLOCK_UPTIME_RAW, CLOCK_MONOTONIC_RAW or
125// mach_absolute_time are able to time functions in the nanosecond range.
126// Furthermore, only CLOCK_MONOTONIC_RAW is truly monotonic, because it
127// also counts cycles when the system is asleep. Thus, it is the only
128// acceptable implementation of steady_clock.
Louis Dionnedf6a9a82020-11-04 10:14:13 -0500129static steady_clock::time_point __libcpp_steady_clock_now() {
Bruno Cardoso Lopesd0027142017-01-09 19:21:48 +0000130 struct timespec tp;
Louis Dionnea2dac172020-02-10 18:30:43 +0100131 if (0 != clock_gettime(CLOCK_MONOTONIC_RAW, &tp))
132 __throw_system_error(errno, "clock_gettime(CLOCK_MONOTONIC_RAW) failed");
Louis Dionnedf6a9a82020-11-04 10:14:13 -0500133 return steady_clock::time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec));
Bruno Cardoso Lopesd0027142017-01-09 19:21:48 +0000134}
135
Saleem Abdulrasoolee302e42017-01-03 21:53:51 +0000136#elif defined(_LIBCPP_WIN32API)
Saleem Abdulrasoolae249922017-01-01 20:20:43 +0000137
Marshall Clow132ccac2019-04-01 17:23:30 +0000138// https://msdn.microsoft.com/en-us/library/windows/desktop/ms644905(v=vs.85).aspx says:
139// If the function fails, the return value is zero. <snip>
Stephan T. Lavavejfb39ad72019-10-23 11:45:36 -0700140// On systems that run Windows XP or later, the function will always succeed
Marshall Clow132ccac2019-04-01 17:23:30 +0000141// and will thus never return zero.
142
143static LARGE_INTEGER
144__QueryPerformanceFrequency()
145{
Louis Dionnedf6a9a82020-11-04 10:14:13 -0500146 LARGE_INTEGER val;
147 (void) QueryPerformanceFrequency(&val);
148 return val;
Marshall Clow132ccac2019-04-01 17:23:30 +0000149}
150
Louis Dionnedf6a9a82020-11-04 10:14:13 -0500151static steady_clock::time_point __libcpp_steady_clock_now() {
Marshall Clow132ccac2019-04-01 17:23:30 +0000152 static const LARGE_INTEGER freq = __QueryPerformanceFrequency();
Saleem Abdulrasoolae249922017-01-01 20:20:43 +0000153
Marshall Clow5dbb0d22019-04-02 14:00:36 +0000154 LARGE_INTEGER counter;
155 (void) QueryPerformanceCounter(&counter);
Louis Dionnedf6a9a82020-11-04 10:14:13 -0500156 return steady_clock::time_point(steady_clock::duration(counter.QuadPart * nano::den / freq.QuadPart));
Saleem Abdulrasoolae249922017-01-01 20:20:43 +0000157}
158
159#elif defined(CLOCK_MONOTONIC)
160
Louis Dionnedf6a9a82020-11-04 10:14:13 -0500161static steady_clock::time_point __libcpp_steady_clock_now() {
Saleem Abdulrasoolae249922017-01-01 20:20:43 +0000162 struct timespec tp;
163 if (0 != clock_gettime(CLOCK_MONOTONIC, &tp))
164 __throw_system_error(errno, "clock_gettime(CLOCK_MONOTONIC) failed");
Louis Dionnedf6a9a82020-11-04 10:14:13 -0500165 return steady_clock::time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec));
Saleem Abdulrasoolae249922017-01-01 20:20:43 +0000166}
167
Ed Schoutenf7805c32015-05-14 20:54:18 +0000168#else
Louis Dionnedf6a9a82020-11-04 10:14:13 -0500169# error "Monotonic clock not implemented on this platform"
Ed Schoutenf7805c32015-05-14 20:54:18 +0000170#endif
Howard Hinnant155c2af2010-05-24 17:49:41 +0000171
Louis Dionnedf6a9a82020-11-04 10:14:13 -0500172const bool steady_clock::is_steady;
173
174steady_clock::time_point
175steady_clock::now() _NOEXCEPT
176{
177 return __libcpp_steady_clock_now();
178}
179
Jonathan Roelofscce96eb2014-09-02 21:14:38 +0000180#endif // !_LIBCPP_HAS_NO_MONOTONIC_CLOCK
181
Howard Hinnantc51e1022010-05-11 19:42:16 +0000182}
183
184_LIBCPP_END_NAMESPACE_STD