-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[libc] Implement timespec_get
#116102
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
[libc] Implement timespec_get
#116102
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f0f442d
[libc] Implement `timespec_get`
petrhosek 9fc2d09
Rename __llvm_libc_time_utc_get to __llvm_libc_timespec_get_utc
petrhosek 5560670
Formatting
petrhosek d7c77be
Address review feedback
petrhosek 4c65922
Avoid introducing new __support file for simplicity
petrhosek 0d52071
Support old hdrgen
petrhosek f4edc9b
Support gpu
petrhosek 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
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,10 @@ | ||
add_entrypoint_object( | ||
timespec_get | ||
SRCS | ||
timespec_get.cpp | ||
HDRS | ||
../timespec_get.h | ||
DEPENDS | ||
libc.hdr.time_macros | ||
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,27 @@ | ||
//===-- Implementation of timespec_get for baremetal ----------------------===// | ||
// | ||
// 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/time/timespec_get.h" | ||
#include "hdr/time_macros.h" | ||
#include "src/__support/common.h" | ||
#include "src/__support/macros/config.h" | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
|
||
extern "C" bool __llvm_libc_timespec_get_utc(struct timespec *ts); | ||
|
||
LLVM_LIBC_FUNCTION(int, timespec_get, (struct timespec * ts, int base)) { | ||
if (base != TIME_UTC) | ||
return 0; | ||
|
||
if (!__llvm_libc_timespec_get_utc(ts)) | ||
return 0; | ||
return base; | ||
} | ||
|
||
} // 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,29 @@ | ||
//===-- Implementation of timespec_get for gpu ----------------------------===// | ||
// | ||
// 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/time/timespec_get.h" | ||
#include "hdr/time_macros.h" | ||
#include "src/__support/common.h" | ||
#include "src/__support/macros/config.h" | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
|
||
LLVM_LIBC_FUNCTION(int, timespec_get, (struct timespec * ts, int base)) { | ||
if (base != TIME_MONOTONIC || !ts) | ||
return 0; | ||
|
||
uint64_t ns_per_tick = TICKS_PER_SEC / GPU_CLOCKS_PER_SEC; | ||
uint64_t ticks = gpu::fixed_frequency_clock(); | ||
|
||
ts->tv_nsec = (ticks * ns_per_tick) % TICKS_PER_SEC; | ||
ts->tv_sec = (ticks * ns_per_tick) / TICKS_PER_SEC; | ||
|
||
return base; | ||
} | ||
|
||
} // 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,45 @@ | ||
//===-- Implementation of timespec_get for Linux --------------------------===// | ||
// | ||
// 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/time/timespec_get.h" | ||
#include "hdr/time_macros.h" | ||
#include "src/__support/common.h" | ||
#include "src/__support/macros/config.h" | ||
#include "src/__support/time/linux/clock_gettime.h" | ||
#include "src/errno/libc_errno.h" | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
|
||
LLVM_LIBC_FUNCTION(int, timespec_get, (struct timespec * ts, int base)) { | ||
clockid_t clockid; | ||
switch (base) { | ||
case TIME_UTC: | ||
clockid = CLOCK_REALTIME; | ||
break; | ||
case TIME_MONOTONIC: | ||
clockid = CLOCK_MONOTONIC; | ||
break; | ||
case TIME_ACTIVE: | ||
clockid = CLOCK_PROCESS_CPUTIME_ID; | ||
break; | ||
case TIME_THREAD_ACTIVE: | ||
clockid = CLOCK_THREAD_CPUTIME_ID; | ||
break; | ||
default: | ||
return 0; | ||
} | ||
petrhosek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
auto result = internal::clock_gettime(clockid, ts); | ||
if (!result.has_value()) { | ||
libc_errno = result.error(); | ||
return 0; | ||
} | ||
return base; | ||
} | ||
|
||
} // 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
//===-- Implementation header of timespec_get -------------------*- 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_TIMESPEC_GET_H | ||
#define LLVM_LIBC_SRC_TIME_TIMESPEC_GET_H | ||
|
||
#include "hdr/types/struct_timespec.h" | ||
#include "src/__support/macros/config.h" | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
|
||
int timespec_get(struct timespec *ts, int base); | ||
|
||
} // namespace LIBC_NAMESPACE_DECL | ||
|
||
#endif // LLVM_LIBC_SRC_TIME_TIMESPEC_GET_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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
//===-- Unittests for timespec_get ----------------------------------------===// | ||
// | ||
// 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 "hdr/types/struct_timespec.h" | ||
#include "src/__support/macros/properties/architectures.h" | ||
#include "src/time/timespec_get.h" | ||
#include "test/UnitTest/Test.h" | ||
|
||
TEST(LlvmLibcTimespecGet, Utc) { | ||
timespec ts; | ||
int result; | ||
result = LIBC_NAMESPACE::timespec_get(&ts, TIME_UTC); | ||
#ifdef LIBC_TARGET_ARCH_IS_GPU | ||
ASSERT_EQ(result, 0); | ||
#else | ||
ASSERT_EQ(result, TIME_UTC); | ||
ASSERT_GT(ts.tv_sec, time_t(0)); | ||
#endif | ||
} | ||
|
||
TEST(LlvmLibcTimespecGet, Monotonic) { | ||
timespec ts1, ts2; | ||
int result; | ||
result = LIBC_NAMESPACE::timespec_get(&ts1, TIME_MONOTONIC); | ||
ASSERT_EQ(result, TIME_MONOTONIC); | ||
ASSERT_GT(ts1.tv_sec, time_t(0)); | ||
result = LIBC_NAMESPACE::timespec_get(&ts2, TIME_MONOTONIC); | ||
ASSERT_EQ(result, TIME_MONOTONIC); | ||
ASSERT_GE(ts2.tv_sec, ts1.tv_sec); // The monotonic time should increase. | ||
if (ts2.tv_sec == ts1.tv_sec) { | ||
ASSERT_GE(ts2.tv_nsec, ts1.tv_nsec); | ||
} | ||
} | ||
|
||
TEST(LlvmLibcTimespecGet, Unknown) { | ||
timespec ts; | ||
int result; | ||
result = LIBC_NAMESPACE::timespec_get(&ts, 0); | ||
ASSERT_EQ(result, 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.
since #117220 is close to landing, it might be best to skip adding anything to the old headergen files.