blob: ea0a638acc53914ed625d0d6ebad9cd5b024f085 [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
Eric Fiselier7eba47e2018-07-25 20:51:49 +000015#if !defined(__APPLE__)
16#define _LIBCPP_USE_CLOCK_GETTIME
Bruno Cardoso Lopesd0027142017-01-09 19:21:48 +000017#endif // __APPLE__
18
Saleem Abdulrasoolee302e42017-01-03 21:53:51 +000019#if defined(_LIBCPP_WIN32API)
Saleem Abdulrasool594b0422017-01-01 20:20:41 +000020#define WIN32_LEAN_AND_MEAN
21#define VC_EXTRA_LEAN
Eric Fiselierbb999f92017-05-31 22:14:05 +000022#include <windows.h>
Saleem Abdulrasool594b0422017-01-01 20:20:41 +000023#if _WIN32_WINNT >= _WIN32_WINNT_WIN8
24#include <winapifamily.h>
25#endif
26#else
Eric Fiselier7eba47e2018-07-25 20:51:49 +000027#if !defined(CLOCK_REALTIME) || !defined(_LIBCPP_USE_CLOCK_GETTIME)
Ed Schoutenf7805c32015-05-14 20:54:18 +000028#include <sys/time.h> // for gettimeofday and timeval
Saleem Abdulrasoolee302e42017-01-03 21:53:51 +000029#endif // !defined(CLOCK_REALTIME)
30#endif // defined(_LIBCPP_WIN32API)
Ed Schoutenf7805c32015-05-14 20:54:18 +000031
Saleem Abdulrasoolae249922017-01-01 20:20:43 +000032#if !defined(_LIBCPP_HAS_NO_MONOTONIC_CLOCK)
Ed Schoutenf7805c32015-05-14 20:54:18 +000033#if __APPLE__
34#include <mach/mach_time.h> // mach_absolute_time, mach_timebase_info_data_t
Saleem Abdulrasoolee302e42017-01-03 21:53:51 +000035#elif !defined(_LIBCPP_WIN32API) && !defined(CLOCK_MONOTONIC)
Ed Schoutenf7805c32015-05-14 20:54:18 +000036#error "Monotonic clock not implemented"
37#endif
38#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +000039
40_LIBCPP_BEGIN_NAMESPACE_STD
41
42namespace chrono
43{
44
45// system_clock
46
Howard Hinnant2c45cb42012-12-12 21:14:28 +000047const bool system_clock::is_steady;
48
Howard Hinnantc51e1022010-05-11 19:42:16 +000049system_clock::time_point
Howard Hinnantaa54ac42011-05-28 18:34:36 +000050system_clock::now() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +000051{
Saleem Abdulrasoolee302e42017-01-03 21:53:51 +000052#if defined(_LIBCPP_WIN32API)
Saleem Abdulrasool9e367702017-01-01 22:04:38 +000053 // FILETIME is in 100ns units
54 using filetime_duration =
55 _VSTD::chrono::duration<__int64,
56 _VSTD::ratio_multiply<_VSTD::ratio<100, 1>,
57 nanoseconds::period>>;
Saleem Abdulrasool594b0422017-01-01 20:20:41 +000058
Saleem Abdulrasool9e367702017-01-01 22:04:38 +000059 // The Windows epoch is Jan 1 1601, the Unix epoch Jan 1 1970.
Saleem Abdulrasool25c0d402017-01-02 18:41:50 +000060 static _LIBCPP_CONSTEXPR const seconds nt_to_unix_epoch{11644473600};
Saleem Abdulrasool9e367702017-01-01 22:04:38 +000061
62 FILETIME ft;
Saleem Abdulrasool594b0422017-01-01 20:20:41 +000063#if _WIN32_WINNT >= _WIN32_WINNT_WIN8
64#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
Saleem Abdulrasool9e367702017-01-01 22:04:38 +000065 GetSystemTimePreciseAsFileTime(&ft);
Saleem Abdulrasool594b0422017-01-01 20:20:41 +000066#else
Saleem Abdulrasool9e367702017-01-01 22:04:38 +000067 GetSystemTimeAsFileTime(&ft);
Saleem Abdulrasool594b0422017-01-01 20:20:41 +000068#endif
69#else
Saleem Abdulrasool9e367702017-01-01 22:04:38 +000070 GetSystemTimeAsFileTime(&ft);
Saleem Abdulrasool594b0422017-01-01 20:20:41 +000071#endif
Saleem Abdulrasool9e367702017-01-01 22:04:38 +000072
73 filetime_duration d{(static_cast<__int64>(ft.dwHighDateTime) << 32) |
74 static_cast<__int64>(ft.dwLowDateTime)};
75 return time_point(duration_cast<duration>(d - nt_to_unix_epoch));
Saleem Abdulrasool594b0422017-01-01 20:20:41 +000076#else
Eric Fiselier7eba47e2018-07-25 20:51:49 +000077#if defined(_LIBCPP_USE_CLOCK_GETTIME) && defined(CLOCK_REALTIME)
78 struct timespec tp;
79 if (0 != clock_gettime(CLOCK_REALTIME, &tp))
80 __throw_system_error(errno, "clock_gettime(CLOCK_REALTIME) failed");
81 return time_point(seconds(tp.tv_sec) + microseconds(tp.tv_nsec / 1000));
Bruno Cardoso Lopesd0027142017-01-09 19:21:48 +000082#else
Howard Hinnantc51e1022010-05-11 19:42:16 +000083 timeval tv;
84 gettimeofday(&tv, 0);
85 return time_point(seconds(tv.tv_sec) + microseconds(tv.tv_usec));
Eric Fiselier7eba47e2018-07-25 20:51:49 +000086#endif // _LIBCPP_USE_CLOCK_GETTIME && CLOCK_REALTIME
Saleem Abdulrasool594b0422017-01-01 20:20:41 +000087#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +000088}
89
90time_t
Howard Hinnantaa54ac42011-05-28 18:34:36 +000091system_clock::to_time_t(const time_point& t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +000092{
93 return time_t(duration_cast<seconds>(t.time_since_epoch()).count());
94}
95
96system_clock::time_point
Howard Hinnantaa54ac42011-05-28 18:34:36 +000097system_clock::from_time_t(time_t t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +000098{
99 return system_clock::time_point(seconds(t));
100}
101
Jonathan Roelofscce96eb2014-09-02 21:14:38 +0000102#ifndef _LIBCPP_HAS_NO_MONOTONIC_CLOCK
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000103// steady_clock
Ed Schoutenf7805c32015-05-14 20:54:18 +0000104//
105// Warning: If this is not truly steady, then it is non-conforming. It is
106// better for it to not exist and have the rest of libc++ use system_clock
107// instead.
Howard Hinnantc51e1022010-05-11 19:42:16 +0000108
Howard Hinnant2c45cb42012-12-12 21:14:28 +0000109const bool steady_clock::is_steady;
110
Saleem Abdulrasoolae249922017-01-01 20:20:43 +0000111#if defined(__APPLE__)
Ed Schoutenf7805c32015-05-14 20:54:18 +0000112
Bruno Cardoso Lopesd0027142017-01-09 19:21:48 +0000113// Darwin libc versions >= 1133 provide ns precision via CLOCK_UPTIME_RAW
Eric Fiselier7eba47e2018-07-25 20:51:49 +0000114#if defined(_LIBCPP_USE_CLOCK_GETTIME) && defined(CLOCK_UPTIME_RAW)
Bruno Cardoso Lopesd0027142017-01-09 19:21:48 +0000115steady_clock::time_point
116steady_clock::now() _NOEXCEPT
117{
118 struct timespec tp;
119 if (0 != clock_gettime(CLOCK_UPTIME_RAW, &tp))
120 __throw_system_error(errno, "clock_gettime(CLOCK_UPTIME_RAW) failed");
121 return time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec));
122}
123
124#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000125// mach_absolute_time() * MachInfo.numer / MachInfo.denom is the number of
126// nanoseconds since the computer booted up. MachInfo.numer and MachInfo.denom
127// are run time constants supplied by the OS. This clock has no relationship
128// to the Gregorian calendar. It's main use is as a high resolution timer.
129
130// MachInfo.numer / MachInfo.denom is often 1 on the latest equipment. Specialize
131// for that case as an optimization.
132
Howard Hinnantc51e1022010-05-11 19:42:16 +0000133static
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000134steady_clock::rep
135steady_simplified()
Howard Hinnantc51e1022010-05-11 19:42:16 +0000136{
Howard Hinnant28b24882011-12-01 20:21:04 +0000137 return static_cast<steady_clock::rep>(mach_absolute_time());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000138}
139
140static
141double
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000142compute_steady_factor()
Howard Hinnantc51e1022010-05-11 19:42:16 +0000143{
144 mach_timebase_info_data_t MachInfo;
145 mach_timebase_info(&MachInfo);
146 return static_cast<double>(MachInfo.numer) / MachInfo.denom;
147}
148
149static
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000150steady_clock::rep
151steady_full()
Howard Hinnantc51e1022010-05-11 19:42:16 +0000152{
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000153 static const double factor = compute_steady_factor();
154 return static_cast<steady_clock::rep>(mach_absolute_time() * factor);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000155}
156
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000157typedef steady_clock::rep (*FP)();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000158
159static
160FP
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000161init_steady_clock()
Howard Hinnantc51e1022010-05-11 19:42:16 +0000162{
163 mach_timebase_info_data_t MachInfo;
164 mach_timebase_info(&MachInfo);
165 if (MachInfo.numer == MachInfo.denom)
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000166 return &steady_simplified;
167 return &steady_full;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000168}
169
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000170steady_clock::time_point
Howard Hinnantaa54ac42011-05-28 18:34:36 +0000171steady_clock::now() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000172{
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000173 static FP fp = init_steady_clock();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000174 return time_point(duration(fp()));
175}
Eric Fiselier7eba47e2018-07-25 20:51:49 +0000176#endif // defined(_LIBCPP_USE_CLOCK_GETTIME) && defined(CLOCK_UPTIME_RAW)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000177
Saleem Abdulrasoolee302e42017-01-03 21:53:51 +0000178#elif defined(_LIBCPP_WIN32API)
Saleem Abdulrasoolae249922017-01-01 20:20:43 +0000179
180steady_clock::time_point
181steady_clock::now() _NOEXCEPT
182{
Saleem Abdulrasool91e1d612017-01-01 22:04:36 +0000183 static LARGE_INTEGER freq;
184 static BOOL initialized = FALSE;
185 if (!initialized)
186 initialized = QueryPerformanceFrequency(&freq); // always succceeds
Saleem Abdulrasoolae249922017-01-01 20:20:43 +0000187
Saleem Abdulrasool91e1d612017-01-01 22:04:36 +0000188 LARGE_INTEGER counter;
189 QueryPerformanceCounter(&counter);
190 return time_point(duration(counter.QuadPart * nano::den / freq.QuadPart));
Saleem Abdulrasoolae249922017-01-01 20:20:43 +0000191}
192
193#elif defined(CLOCK_MONOTONIC)
194
Bruno Cardoso Lopesd0027142017-01-09 19:21:48 +0000195// On Apple platforms only CLOCK_UPTIME_RAW or mach_absolute_time are able to
196// time functions in the nanosecond range. Thus, they are the only acceptable
197// implementations of steady_clock.
198#ifdef __APPLE__
199#error "Never use CLOCK_MONOTONIC for steady_clock::now on Apple platforms"
200#endif
201
Saleem Abdulrasoolae249922017-01-01 20:20:43 +0000202steady_clock::time_point
203steady_clock::now() _NOEXCEPT
204{
205 struct timespec tp;
206 if (0 != clock_gettime(CLOCK_MONOTONIC, &tp))
207 __throw_system_error(errno, "clock_gettime(CLOCK_MONOTONIC) failed");
208 return time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec));
209}
210
Ed Schoutenf7805c32015-05-14 20:54:18 +0000211#else
212#error "Monotonic clock not implemented"
213#endif
Howard Hinnant155c2af2010-05-24 17:49:41 +0000214
Jonathan Roelofscce96eb2014-09-02 21:14:38 +0000215#endif // !_LIBCPP_HAS_NO_MONOTONIC_CLOCK
216
Howard Hinnantc51e1022010-05-11 19:42:16 +0000217}
218
219_LIBCPP_END_NAMESPACE_STD