Path: blob/main/contrib/llvm-project/libcxx/src/chrono.cpp
35154 views
//===----------------------------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#if defined(__MVS__)9// As part of monotonic clock support on z/OS we need macro _LARGE_TIME_API10// to be defined before any system header to include definition of struct timespec64.11# define _LARGE_TIME_API12#endif1314#include <__system_error/system_error.h>15#include <cerrno> // errno16#include <chrono>1718#if defined(__MVS__)19# include <__support/ibm/gettod_zos.h> // gettimeofdayMonotonic20#endif2122#include "include/apple_availability.h"23#include <time.h> // clock_gettime and CLOCK_{MONOTONIC,REALTIME,MONOTONIC_RAW}2425#if __has_include(<unistd.h>)26# include <unistd.h> // _POSIX_TIMERS27#endif2829#if __has_include(<sys/time.h>)30# include <sys/time.h> // for gettimeofday and timeval31#endif3233// OpenBSD does not have a fully conformant suite of POSIX timers, but34// it does have clock_gettime and CLOCK_MONOTONIC which is all we need.35#if defined(__APPLE__) || defined(__gnu_hurd__) || defined(__OpenBSD__) || (defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0)36# define _LIBCPP_HAS_CLOCK_GETTIME37#endif3839#if defined(_LIBCPP_WIN32API)40# define WIN32_LEAN_AND_MEAN41# define VC_EXTRA_LEAN42# include <windows.h>43# if _WIN32_WINNT >= _WIN32_WINNT_WIN844# include <winapifamily.h>45# endif46#endif // defined(_LIBCPP_WIN32API)4748#if defined(__Fuchsia__)49# include <zircon/syscalls.h>50#endif5152#if __has_include(<mach/mach_time.h>)53# include <mach/mach_time.h>54#endif5556#if defined(__ELF__) && defined(_LIBCPP_LINK_RT_LIB)57# pragma comment(lib, "rt")58#endif5960_LIBCPP_BEGIN_NAMESPACE_STD6162namespace chrono {6364//65// system_clock66//6768#if defined(_LIBCPP_WIN32API)6970# if _WIN32_WINNT < _WIN32_WINNT_WIN87172namespace {7374typedef void(WINAPI* GetSystemTimeAsFileTimePtr)(LPFILETIME);7576class GetSystemTimeInit {77public:78GetSystemTimeInit() {79fp = (GetSystemTimeAsFileTimePtr)(void*)GetProcAddress(80GetModuleHandleW(L"kernel32.dll"), "GetSystemTimePreciseAsFileTime");81if (fp == nullptr)82fp = GetSystemTimeAsFileTime;83}84GetSystemTimeAsFileTimePtr fp;85};8687// Pretend we're inside a system header so the compiler doesn't flag the use of the init_priority88// attribute with a value that's reserved for the implementation (we're the implementation).89# include "chrono_system_time_init.h"90} // namespace9192# endif9394static system_clock::time_point __libcpp_system_clock_now() {95// FILETIME is in 100ns units96using filetime_duration =97std::chrono::duration<__int64, std::ratio_multiply<std::ratio<100, 1>, nanoseconds::period>>;9899// The Windows epoch is Jan 1 1601, the Unix epoch Jan 1 1970.100static constexpr const seconds nt_to_unix_epoch{11644473600};101102FILETIME ft;103# if (_WIN32_WINNT >= _WIN32_WINNT_WIN8 && WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)) || \104(_WIN32_WINNT >= _WIN32_WINNT_WIN10)105GetSystemTimePreciseAsFileTime(&ft);106# elif !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)107GetSystemTimeAsFileTime(&ft);108# else109GetSystemTimeAsFileTimeFunc.fp(&ft);110# endif111112filetime_duration d{(static_cast<__int64>(ft.dwHighDateTime) << 32) | static_cast<__int64>(ft.dwLowDateTime)};113return system_clock::time_point(duration_cast<system_clock::duration>(d - nt_to_unix_epoch));114}115116#elif defined(_LIBCPP_HAS_CLOCK_GETTIME)117118static system_clock::time_point __libcpp_system_clock_now() {119struct timespec tp;120if (0 != clock_gettime(CLOCK_REALTIME, &tp))121__throw_system_error(errno, "clock_gettime(CLOCK_REALTIME) failed");122return system_clock::time_point(seconds(tp.tv_sec) + microseconds(tp.tv_nsec / 1000));123}124125#else126127static system_clock::time_point __libcpp_system_clock_now() {128timeval tv;129gettimeofday(&tv, 0);130return system_clock::time_point(seconds(tv.tv_sec) + microseconds(tv.tv_usec));131}132133#endif134135const bool system_clock::is_steady;136137system_clock::time_point system_clock::now() noexcept { return __libcpp_system_clock_now(); }138139time_t system_clock::to_time_t(const time_point& t) noexcept {140return time_t(duration_cast<seconds>(t.time_since_epoch()).count());141}142143system_clock::time_point system_clock::from_time_t(time_t t) noexcept { return system_clock::time_point(seconds(t)); }144145//146// steady_clock147//148// Warning: If this is not truly steady, then it is non-conforming. It is149// better for it to not exist and have the rest of libc++ use system_clock150// instead.151//152153#ifndef _LIBCPP_HAS_NO_MONOTONIC_CLOCK154155# if defined(__APPLE__)156157// On Apple platforms, only CLOCK_UPTIME_RAW, CLOCK_MONOTONIC_RAW or158// mach_absolute_time are able to time functions in the nanosecond range.159// Furthermore, only CLOCK_MONOTONIC_RAW is truly monotonic, because it160// also counts cycles when the system is asleep. Thus, it is the only161// acceptable implementation of steady_clock.162static steady_clock::time_point __libcpp_steady_clock_now() {163struct timespec tp;164if (0 != clock_gettime(CLOCK_MONOTONIC_RAW, &tp))165__throw_system_error(errno, "clock_gettime(CLOCK_MONOTONIC_RAW) failed");166return steady_clock::time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec));167}168169# elif defined(_LIBCPP_WIN32API)170171// https://msdn.microsoft.com/en-us/library/windows/desktop/ms644905(v=vs.85).aspx says:172// If the function fails, the return value is zero. <snip>173// On systems that run Windows XP or later, the function will always succeed174// and will thus never return zero.175176static LARGE_INTEGER __QueryPerformanceFrequency() {177LARGE_INTEGER val;178(void)QueryPerformanceFrequency(&val);179return val;180}181182static steady_clock::time_point __libcpp_steady_clock_now() {183static const LARGE_INTEGER freq = __QueryPerformanceFrequency();184185LARGE_INTEGER counter;186(void)QueryPerformanceCounter(&counter);187auto seconds = counter.QuadPart / freq.QuadPart;188auto fractions = counter.QuadPart % freq.QuadPart;189auto dur = seconds * nano::den + fractions * nano::den / freq.QuadPart;190return steady_clock::time_point(steady_clock::duration(dur));191}192193# elif defined(__MVS__)194195static steady_clock::time_point __libcpp_steady_clock_now() {196struct timespec64 ts;197if (0 != gettimeofdayMonotonic(&ts))198__throw_system_error(errno, "failed to obtain time of day");199200return steady_clock::time_point(seconds(ts.tv_sec) + nanoseconds(ts.tv_nsec));201}202203# elif defined(__Fuchsia__)204205static steady_clock::time_point __libcpp_steady_clock_now() noexcept {206// Implicitly link against the vDSO system call ABI without207// requiring the final link to specify -lzircon explicitly when208// statically linking libc++.209# pragma comment(lib, "zircon")210211return steady_clock::time_point(nanoseconds(_zx_clock_get_monotonic()));212}213214# elif defined(_LIBCPP_HAS_CLOCK_GETTIME)215216static steady_clock::time_point __libcpp_steady_clock_now() {217struct timespec tp;218if (0 != clock_gettime(CLOCK_MONOTONIC, &tp))219__throw_system_error(errno, "clock_gettime(CLOCK_MONOTONIC) failed");220return steady_clock::time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec));221}222223# else224# error "Monotonic clock not implemented on this platform"225# endif226227const bool steady_clock::is_steady;228229steady_clock::time_point steady_clock::now() noexcept { return __libcpp_steady_clock_now(); }230231#endif // !_LIBCPP_HAS_NO_MONOTONIC_CLOCK232233} // namespace chrono234235_LIBCPP_END_NAMESPACE_STD236237238