-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[libc][windows] start time API implementation #117775
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
6013745
[libc][windows] start time API implementation
SchrodingerZhu 60ce186
[libc][windows] enable test and fix namespace placement
SchrodingerZhu 471fb1e
[libc][windows] fix format
SchrodingerZhu 734a4f3
[libc][windows] remove SEH
SchrodingerZhu c7e61ba
adjust frequency caching
SchrodingerZhu 7bf98be
address CRs
SchrodingerZhu de9887c
adjust dependency
SchrodingerZhu 717e609
adjust dependency
SchrodingerZhu b0844cc
fix timespec_get
SchrodingerZhu e7f8e70
avoid inconsistent clang-format
SchrodingerZhu 5b5ed2e
add two more clock
SchrodingerZhu 6605c20
use explicit quadpart time
SchrodingerZhu cdf7455
fix
SchrodingerZhu b033ba8
address more comments
SchrodingerZhu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
add_header( | ||
time_macros_ext | ||
HDR | ||
time-macros-ext.h | ||
) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
//===-- Windows Time Macros Extension -------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_LIBC_MACROS_WINDOWS_TIME_MACROS_EXT_H | ||
#define LLVM_LIBC_MACROS_WINDOWS_TIME_MACROS_EXT_H | ||
|
||
#define CLOCK_MONOTONIC 0 | ||
#define CLOCK_REALTIME 1 | ||
#define CLOCK_PROCESS_CPUTIME_ID 2 | ||
#define CLOCK_THREAD_CPUTIME_ID 3 | ||
|
||
#endif // LLVM_LIBC_MACROS_WINDOWS_TIME_MACROS_EXT_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
add_object_library( | ||
clock_gettime | ||
HDRS | ||
clock_gettime.h | ||
../clock_gettime.h | ||
SRCS | ||
clock_gettime.cpp | ||
DEPENDS | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
add_object_library( | ||
clock_gettime | ||
HDRS | ||
../clock_gettime.h | ||
SRCS | ||
clock_gettime.cpp | ||
DEPENDS | ||
libc.hdr.types.struct_timespec | ||
libc.hdr.types.clockid_t | ||
libc.hdr.errno_macros | ||
libc.src.__support.common | ||
libc.src.__support.error_or | ||
libc.src.__support.OSUtil.osutil | ||
libc.src.__support.CPP.atomic | ||
libc.src.__support.CPP.limits | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
//===--- clock_gettime windows implementation -------------------*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "hdr/time_macros.h" | ||
|
||
#include "src/__support/CPP/atomic.h" | ||
#include "src/__support/CPP/bit.h" | ||
#include "src/__support/CPP/limits.h" | ||
#include "src/__support/macros/optimization.h" | ||
#include "src/__support/time/clock_gettime.h" | ||
#include "src/__support/time/units.h" | ||
|
||
#define WIN32_LEAN_AND_MEAN | ||
#define NOMINMAX | ||
#include <Windows.h> | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
namespace internal { | ||
static long long get_ticks_per_second() { | ||
static cpp::Atomic<long long> frequency = 0; | ||
// Relaxed ordering is enough. It is okay to record the frequency multiple | ||
// times. The store operation itself is atomic and the value must propagate | ||
// as required by cache coherence. | ||
auto freq = frequency.load(cpp::MemoryOrder::RELAXED); | ||
if (!freq) { | ||
[[clang::uninitialized]] LARGE_INTEGER buffer; | ||
// On systems that run Windows XP or later, the function will always | ||
// succeed and will thus never return zero. | ||
::QueryPerformanceFrequency(&buffer); | ||
frequency.store(buffer.QuadPart, cpp::MemoryOrder::RELAXED); | ||
return buffer.QuadPart; | ||
} | ||
return freq; | ||
} | ||
|
||
ErrorOr<int> clock_gettime(clockid_t clockid, timespec *ts) { | ||
using namespace time_units; | ||
constexpr unsigned long long HNS_PER_SEC = 1_s_ns / 100ULL; | ||
constexpr long long SEC_LIMIT = | ||
cpp::numeric_limits<decltype(ts->tv_sec)>::max(); | ||
ErrorOr<int> ret = 0; | ||
switch (clockid) { | ||
default: | ||
ret = cpp::unexpected(EINVAL); | ||
break; | ||
|
||
case CLOCK_MONOTONIC: { | ||
// see | ||
// https://learn.microsoft.com/en-us/windows/win32/sysinfo/acquiring-high-resolution-time-stamps | ||
// Is the performance counter monotonic (non-decreasing)? | ||
// Yes. QPC does not go backward. | ||
[[clang::uninitialized]] LARGE_INTEGER buffer; | ||
// On systems that run Windows XP or later, the function will always | ||
// succeed and will thus never return zero. | ||
::QueryPerformanceCounter(&buffer); | ||
long long freq = get_ticks_per_second(); | ||
long long ticks = buffer.QuadPart; | ||
long long tv_sec = ticks / freq; | ||
long long tv_nsec = (ticks % freq) * 1_s_ns / freq; | ||
if (LIBC_UNLIKELY(tv_sec > SEC_LIMIT)) { | ||
ret = cpp::unexpected(EOVERFLOW); | ||
break; | ||
} | ||
ts->tv_sec = static_cast<decltype(ts->tv_sec)>(tv_sec); | ||
SchrodingerZhu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ts->tv_nsec = static_cast<decltype(ts->tv_nsec)>(tv_nsec); | ||
break; | ||
} | ||
case CLOCK_REALTIME: { | ||
// https://learn.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getsystemtimepreciseasfiletime | ||
// GetSystemTimePreciseAsFileTime | ||
// This function is best suited for high-resolution time-of-day | ||
// measurements, or time stamps that are synchronized to UTC | ||
[[clang::uninitialized]] FILETIME file_time; | ||
[[clang::uninitialized]] ULARGE_INTEGER time; | ||
::GetSystemTimePreciseAsFileTime(&file_time); | ||
time.LowPart = file_time.dwLowDateTime; | ||
time.HighPart = file_time.dwHighDateTime; | ||
|
||
// adjust to POSIX epoch (from Jan 1, 1601 to Jan 1, 1970) | ||
constexpr unsigned long long POSIX_TIME_SHIFT = | ||
(11644473600ULL * HNS_PER_SEC); | ||
if (LIBC_UNLIKELY(POSIX_TIME_SHIFT > time.QuadPart)) { | ||
ret = cpp::unexpected(EOVERFLOW); | ||
break; | ||
} | ||
time.QuadPart -= (11644473600ULL * HNS_PER_SEC); | ||
unsigned long long tv_sec = time.QuadPart / HNS_PER_SEC; | ||
unsigned long long tv_nsec = (time.QuadPart % HNS_PER_SEC) * 100ULL; | ||
if (LIBC_UNLIKELY(tv_sec > SEC_LIMIT)) { | ||
ret = cpp::unexpected(EOVERFLOW); | ||
break; | ||
} | ||
ts->tv_sec = static_cast<decltype(ts->tv_sec)>(tv_sec); | ||
SchrodingerZhu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ts->tv_nsec = static_cast<decltype(ts->tv_nsec)>(tv_nsec); | ||
break; | ||
} | ||
case CLOCK_PROCESS_CPUTIME_ID: | ||
case CLOCK_THREAD_CPUTIME_ID: { | ||
[[clang::uninitialized]] FILETIME creation_time; | ||
[[clang::uninitialized]] FILETIME exit_time; | ||
[[clang::uninitialized]] FILETIME kernel_time; | ||
[[clang::uninitialized]] FILETIME user_time; | ||
bool success; | ||
if (clockid == CLOCK_PROCESS_CPUTIME_ID) { | ||
// https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getprocesstimes | ||
success = ::GetProcessTimes(::GetCurrentProcess(), &creation_time, | ||
&exit_time, &kernel_time, &user_time); | ||
} else { | ||
// https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getthreadtimes | ||
success = ::GetThreadTimes(::GetCurrentThread(), &creation_time, | ||
&exit_time, &kernel_time, &user_time); | ||
} | ||
if (!success) { | ||
ret = cpp::unexpected(EINVAL); | ||
break; | ||
} | ||
// https://learn.microsoft.com/en-us/windows/win32/api/minwinbase/ns-minwinbase-filetime | ||
// It is not recommended that you add and subtract values from the FILETIME | ||
// structure to obtain relative times. Instead, you should copy the low- and | ||
// high-order parts of the file time to a ULARGE_INTEGER structure, perform | ||
// 64-bit arithmetic on the QuadPart member, and copy the LowPart and | ||
// HighPart members into the FILETIME structure. | ||
auto kernel_time_hns = cpp::bit_cast<ULARGE_INTEGER>(kernel_time); | ||
auto user_time_hns = cpp::bit_cast<ULARGE_INTEGER>(user_time); | ||
unsigned long long total_time_hns = | ||
kernel_time_hns.QuadPart + user_time_hns.QuadPart; | ||
|
||
unsigned long long tv_sec = total_time_hns / HNS_PER_SEC; | ||
unsigned long long tv_nsec = (total_time_hns % HNS_PER_SEC) * 100ULL; | ||
|
||
if (LIBC_UNLIKELY(tv_sec > SEC_LIMIT)) { | ||
ret = cpp::unexpected(EOVERFLOW); | ||
break; | ||
} | ||
|
||
ts->tv_sec = static_cast<decltype(ts->tv_sec)>(tv_sec); | ||
ts->tv_nsec = static_cast<decltype(ts->tv_nsec)>(tv_nsec); | ||
|
||
break; | ||
} | ||
} | ||
return ret; | ||
} | ||
} // namespace internal | ||
} // namespace LIBC_NAMESPACE_DECL |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.