-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[libc][time][windows] implement clock_getres #118931
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
SchrodingerZhu
merged 4 commits into
llvm:main
from
SchrodingerZhu:libc/time/win/clock_getres
Dec 10, 2024
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
//===--- Cached Performance Counter Frequency ----------------------------===// | ||
// | ||
// 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 "src/__support/CPP/atomic.h" | ||
#include "src/__support/common.h" | ||
|
||
#define WIN32_LEAN_AND_MEAN | ||
#define NOMINMAX | ||
#include <Windows.h> | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
namespace performance_counter { | ||
LIBC_INLINE 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; | ||
} | ||
} // namespace performance_counter | ||
} // 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
//===-- Implementation header of clock_getres -------------------*- 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
#ifndef LLVM_LIBC_SRC_TIME_CLOCK_GETRES_H | ||
#define LLVM_LIBC_SRC_TIME_CLOCK_GETRES_H | ||
|
||
#include "hdr/types/clockid_t.h" | ||
#include "hdr/types/struct_timespec.h" | ||
#include "src/__support/macros/config.h" | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
|
||
int clock_getres(clockid_t clockid, timespec *tp); | ||
|
||
} // namespace LIBC_NAMESPACE_DECL | ||
|
||
#endif // LLVM_LIBC_SRC_TIME_CLOCK_GETRES_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,13 @@ | ||
add_entrypoint_object( | ||
clock_getres | ||
SRCS | ||
clock_getres.cpp | ||
DEPENDS | ||
libc.src.__support.time.windows.performance_counter | ||
libc.src.__support.time.units | ||
libc.src.__support.common | ||
libc.src.__support.macros.optimization | ||
libc.hdr.time_macros | ||
libc.hdr.types.time_t | ||
libc.hdr.types.struct_timespec | ||
) |
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,110 @@ | ||
//===-- Windows implementation of clock_getres ------------------*- 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/errno_macros.h" | ||
#include "hdr/time_macros.h" | ||
#include "hdr/types/clockid_t.h" | ||
#include "hdr/types/struct_timespec.h" | ||
|
||
#include "src/__support/CPP/limits.h" | ||
#include "src/__support/common.h" | ||
#include "src/__support/macros/optimization.h" | ||
#include "src/__support/time/units.h" | ||
#include "src/__support/time/windows/performance_counter.h" | ||
#include "src/errno/libc_errno.h" | ||
#include "src/time/clock_getres.h" | ||
|
||
#define WIN32_LEAN_AND_MEAN | ||
#define NOMINMAX | ||
#include <Windows.h> | ||
|
||
// add in dependencies for GetSystemTimeAdjustmentPrecise | ||
#pragma comment(lib, "mincore.lib") | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
LLVM_LIBC_FUNCTION(int, clock_getres, (clockid_t id, struct timespec *res)) { | ||
using namespace time_units; | ||
// POSIX allows nullptr to be passed as res, in which case the function should | ||
// do nothing. | ||
if (res == nullptr) | ||
return 0; | ||
constexpr unsigned long long HNS_PER_SEC = 1_s_ns / 100ULL; | ||
constexpr unsigned long long SEC_LIMIT = | ||
cpp::numeric_limits<decltype(res->tv_sec)>::max(); | ||
// For CLOCK_MONOTONIC, we are using performance counter | ||
// https://learn.microsoft.com/en-us/windows/win32/sysinfo/acquiring-high-resolution-time-stamps | ||
// Hence, the resolution is given by the performance counter frequency. | ||
// For CLOCK_REALTIME, the precision is given by | ||
// GetSystemTimeAdjustmentPrecise | ||
// (https://learn.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getsystemtimeadjustmentprecise) | ||
// For CLOCK_PROCESS_CPUTIME_ID, CLOCK_THREAD_CPUTIME_ID, the precision is | ||
// given by GetSystemTimeAdjustment | ||
// (https://learn.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getsystemtimeadjustment) | ||
switch (id) { | ||
default: | ||
libc_errno = EINVAL; | ||
return -1; | ||
|
||
case CLOCK_MONOTONIC: { | ||
long long freq = performance_counter::get_ticks_per_second(); | ||
__builtin_assume(freq != 0); | ||
// division of 1 second by frequency, rounded up. | ||
long long tv_sec = static_cast<long long>(freq == 1); | ||
long long tv_nsec = | ||
LIBC_LIKELY(freq != 1) ? 1ll + ((1_s_ns - 1ll) / freq) : 0ll; | ||
// not possible to overflow tv_sec, tv_nsec | ||
res->tv_sec = static_cast<decltype(res->tv_sec)>(tv_sec); | ||
res->tv_nsec = static_cast<decltype(res->tv_nsec)>(tv_nsec); | ||
break; | ||
} | ||
|
||
case CLOCK_REALTIME: { | ||
[[clang::uninitialized]] DWORD64 time_adjustment; | ||
[[clang::uninitialized]] DWORD64 time_increment; | ||
[[clang::uninitialized]] BOOL time_adjustment_disabled; | ||
if (!::GetSystemTimeAdjustmentPrecise(&time_adjustment, &time_increment, | ||
&time_adjustment_disabled)) { | ||
libc_errno = EINVAL; | ||
return -1; | ||
} | ||
DWORD64 tv_sec = time_increment / HNS_PER_SEC; | ||
DWORD64 tv_nsec = (time_increment % HNS_PER_SEC) * 100ULL; | ||
if (LIBC_UNLIKELY(tv_sec > SEC_LIMIT)) { | ||
libc_errno = EOVERFLOW; | ||
return -1; | ||
} | ||
res->tv_sec = static_cast<decltype(res->tv_sec)>(tv_sec); | ||
res->tv_nsec = static_cast<decltype(res->tv_nsec)>(tv_nsec); | ||
break; | ||
} | ||
case CLOCK_PROCESS_CPUTIME_ID: | ||
case CLOCK_THREAD_CPUTIME_ID: { | ||
[[clang::uninitialized]] DWORD time_adjustment; | ||
[[clang::uninitialized]] DWORD time_increment; | ||
[[clang::uninitialized]] BOOL time_adjustment_disabled; | ||
if (!::GetSystemTimeAdjustment(&time_adjustment, &time_increment, | ||
&time_adjustment_disabled)) { | ||
libc_errno = EINVAL; | ||
return -1; | ||
} | ||
DWORD hns_per_sec = static_cast<DWORD>(HNS_PER_SEC); | ||
DWORD sec_limit = static_cast<DWORD>(SEC_LIMIT); | ||
DWORD tv_sec = time_increment / hns_per_sec; | ||
DWORD tv_nsec = (time_increment % hns_per_sec) * 100UL; | ||
if (LIBC_UNLIKELY(tv_sec > sec_limit)) { | ||
libc_errno = EOVERFLOW; | ||
return -1; | ||
} | ||
res->tv_sec = static_cast<decltype(res->tv_sec)>(tv_sec); | ||
res->tv_nsec = static_cast<decltype(res->tv_nsec)>(tv_nsec); | ||
break; | ||
} | ||
} | ||
return 0; | ||
} | ||
} // 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
//===-- Unittests for clock_getres- ---------------------------------------===// | ||
// | ||
// 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/time/clock_getres.h" | ||
#include "test/UnitTest/ErrnoSetterMatcher.h" | ||
#include "test/UnitTest/Test.h" | ||
|
||
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails; | ||
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds; | ||
|
||
TEST(LlvmLibcClockGetRes, Invalid) { | ||
timespec tp; | ||
EXPECT_THAT(LIBC_NAMESPACE::clock_getres(-1, &tp), Fails(EINVAL)); | ||
} | ||
|
||
TEST(LlvmLibcClockGetRes, NullSpec) { | ||
EXPECT_THAT(LIBC_NAMESPACE::clock_getres(CLOCK_REALTIME, nullptr), | ||
Succeeds()); | ||
} | ||
|
||
TEST(LlvmLibcClockGetRes, Realtime) { | ||
timespec tp; | ||
EXPECT_THAT(LIBC_NAMESPACE::clock_getres(CLOCK_REALTIME, &tp), Succeeds()); | ||
EXPECT_GE(tp.tv_sec, static_cast<decltype(tp.tv_sec)>(0)); | ||
EXPECT_GE(tp.tv_nsec, static_cast<decltype(tp.tv_nsec)>(0)); | ||
} | ||
|
||
TEST(LlvmLibcClockGetRes, Monotonic) { | ||
timespec tp; | ||
ASSERT_THAT(LIBC_NAMESPACE::clock_getres(CLOCK_MONOTONIC, &tp), Succeeds()); | ||
EXPECT_GE(tp.tv_sec, static_cast<decltype(tp.tv_sec)>(0)); | ||
EXPECT_GE(tp.tv_nsec, static_cast<decltype(tp.tv_nsec)>(0)); | ||
} | ||
|
||
TEST(LlvmLibcClockGetRes, ProcessCpuTime) { | ||
timespec tp; | ||
ASSERT_THAT(LIBC_NAMESPACE::clock_getres(CLOCK_PROCESS_CPUTIME_ID, &tp), | ||
Succeeds()); | ||
EXPECT_GE(tp.tv_sec, static_cast<decltype(tp.tv_sec)>(0)); | ||
EXPECT_GE(tp.tv_nsec, static_cast<decltype(tp.tv_nsec)>(0)); | ||
} | ||
|
||
TEST(LlvmLibcClockGetRes, ThreadCpuTime) { | ||
timespec tp; | ||
ASSERT_THAT(LIBC_NAMESPACE::clock_getres(CLOCK_THREAD_CPUTIME_ID, &tp), | ||
Succeeds()); | ||
EXPECT_GE(tp.tv_sec, static_cast<decltype(tp.tv_sec)>(0)); | ||
EXPECT_GE(tp.tv_nsec, static_cast<decltype(tp.tv_nsec)>(0)); | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just curious: is there any reason we have to explicitly round the division results up here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Resolution is the unit of time that should be increasable for API like clock_settime. In fact, the manpage saids that clock_settime always truncate the time to multiples of the resolution. If we round down, user may expect a finer unit of increment is feasible, thus propagating to further issues.
Rounding up gives exact span from 1 to (1s - 1).