[libc-commits] [libc] [libc] Implement `timespec_get` (PR #116102)
Petr Hosek via libc-commits
libc-commits at lists.llvm.org
Wed Nov 13 14:04:52 PST 2024
https://github.com/petrhosek updated https://github.com/llvm/llvm-project/pull/116102
>From f0f442df54468a7f6af36aa94482e4491eff148c Mon Sep 17 00:00:00 2001
From: Petr Hosek <phosek at google.com>
Date: Fri, 19 Jul 2024 22:51:38 -0700
Subject: [PATCH 1/3] [libc] Implement `timespec_get`
`timespec_get` is C standard counterpart to POSIX `clock_gettime`. On
Linux we simply use `clock_gettime`. On baremetal we introduce a new
external API `__llvm_libc_time_utc_get` that should be implemented by
the vendor.
---
libc/config/baremetal/arm/entrypoints.txt | 1 +
libc/config/baremetal/riscv/entrypoints.txt | 1 +
libc/config/linux/aarch64/entrypoints.txt | 1 +
libc/config/linux/riscv/entrypoints.txt | 1 +
libc/config/linux/x86_64/entrypoints.txt | 1 +
libc/include/llvm-libc-macros/time-macros.h | 2 ++
libc/newhdrgen/yaml/time.yaml | 7 ++++
.../__support/OSUtil/baremetal/CMakeLists.txt | 2 ++
libc/src/__support/OSUtil/baremetal/time.cpp | 21 ++++++++++++
libc/src/__support/OSUtil/baremetal/time.h | 21 ++++++++++++
libc/src/time/CMakeLists.txt | 7 ++++
libc/src/time/baremetal/CMakeLists.txt | 11 +++++++
libc/src/time/baremetal/timespec_get.cpp | 25 +++++++++++++++
libc/src/time/linux/CMakeLists.txt | 13 ++++++++
libc/src/time/linux/timespec_get.cpp | 31 ++++++++++++++++++
libc/src/time/timespec_get.h | 21 ++++++++++++
libc/test/src/time/CMakeLists.txt | 10 ++++++
libc/test/src/time/timespec_get_test.cpp | 32 +++++++++++++++++++
18 files changed, 208 insertions(+)
create mode 100644 libc/src/__support/OSUtil/baremetal/time.cpp
create mode 100644 libc/src/__support/OSUtil/baremetal/time.h
create mode 100644 libc/src/time/baremetal/CMakeLists.txt
create mode 100644 libc/src/time/baremetal/timespec_get.cpp
create mode 100644 libc/src/time/linux/timespec_get.cpp
create mode 100644 libc/src/time/timespec_get.h
create mode 100644 libc/test/src/time/timespec_get_test.cpp
diff --git a/libc/config/baremetal/arm/entrypoints.txt b/libc/config/baremetal/arm/entrypoints.txt
index b85ae1119345dd..9027717acb4dae 100644
--- a/libc/config/baremetal/arm/entrypoints.txt
+++ b/libc/config/baremetal/arm/entrypoints.txt
@@ -209,6 +209,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.time.gmtime
libc.src.time.gmtime_r
libc.src.time.mktime
+ libc.src.time.timespec_get
# internal entrypoints
libc.startup.baremetal.init
diff --git a/libc/config/baremetal/riscv/entrypoints.txt b/libc/config/baremetal/riscv/entrypoints.txt
index 199a030ee6371e..afae2b3e082be3 100644
--- a/libc/config/baremetal/riscv/entrypoints.txt
+++ b/libc/config/baremetal/riscv/entrypoints.txt
@@ -205,6 +205,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.time.gmtime
libc.src.time.gmtime_r
libc.src.time.mktime
+ libc.src.time.timespec_get
# internal entrypoints
libc.startup.baremetal.init
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 74ca3742977a5f..effa5b12d87ac4 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -999,6 +999,7 @@ if(LLVM_LIBC_FULL_BUILD)
libc.src.time.mktime
libc.src.time.nanosleep
libc.src.time.time
+ libc.src.time.timespec_get
# unistd.h entrypoints
libc.src.unistd.__llvm_libc_syscall
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 5419462d4f5b3b..5a48baf104159f 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -937,6 +937,7 @@ if(LLVM_LIBC_FULL_BUILD)
libc.src.time.mktime
libc.src.time.nanosleep
libc.src.time.time
+ libc.src.time.timespec_get
# unistd.h entrypoints
libc.src.unistd.__llvm_libc_syscall
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 957e28bd66cc4c..7365b53e8add82 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -1082,6 +1082,7 @@ if(LLVM_LIBC_FULL_BUILD)
libc.src.time.mktime
libc.src.time.nanosleep
libc.src.time.time
+ libc.src.time.timespec_get
# locale.h entrypoints
libc.src.locale.localeconv
diff --git a/libc/include/llvm-libc-macros/time-macros.h b/libc/include/llvm-libc-macros/time-macros.h
index 6d49ed484d5d48..02f2f7bfa7051d 100644
--- a/libc/include/llvm-libc-macros/time-macros.h
+++ b/libc/include/llvm-libc-macros/time-macros.h
@@ -7,4 +7,6 @@
#include "linux/time-macros.h"
#endif
+#define TIME_UTC 1
+
#endif // LLVM_LIBC_MACROS_TIME_MACROS_H
diff --git a/libc/newhdrgen/yaml/time.yaml b/libc/newhdrgen/yaml/time.yaml
index 69b40bef3160dd..3f745e5ee33868 100644
--- a/libc/newhdrgen/yaml/time.yaml
+++ b/libc/newhdrgen/yaml/time.yaml
@@ -96,3 +96,10 @@ functions:
return_type: time_t
arguments:
- type: time_t *
+ - name: timespec_get
+ standard:
+ - stdc
+ return_type: int
+ arguments:
+ - type: struct timespec *
+ - type: int
diff --git a/libc/src/__support/OSUtil/baremetal/CMakeLists.txt b/libc/src/__support/OSUtil/baremetal/CMakeLists.txt
index 8f9200ad0bd96e..5b02709315f9b1 100644
--- a/libc/src/__support/OSUtil/baremetal/CMakeLists.txt
+++ b/libc/src/__support/OSUtil/baremetal/CMakeLists.txt
@@ -3,8 +3,10 @@ add_object_library(
SRCS
io.cpp
exit.cpp
+ time.cpp
HDRS
io.h
+ time.h
DEPENDS
libc.src.__support.common
libc.src.__support.CPP.string_view
diff --git a/libc/src/__support/OSUtil/baremetal/time.cpp b/libc/src/__support/OSUtil/baremetal/time.cpp
new file mode 100644
index 00000000000000..63f0624b4cda4e
--- /dev/null
+++ b/libc/src/__support/OSUtil/baremetal/time.cpp
@@ -0,0 +1,21 @@
+//===---------- Baremetal implementation of time utils ----------*- 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 "time.h"
+
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+extern "C" int __llvm_libc_time_utc_get(struct timespec *ts);
+
+int time_utc_get(struct timespec *ts) {
+ return __llvm_libc_time_utc_get(ts);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/__support/OSUtil/baremetal/time.h b/libc/src/__support/OSUtil/baremetal/time.h
new file mode 100644
index 00000000000000..8c3ded377c7376
--- /dev/null
+++ b/libc/src/__support/OSUtil/baremetal/time.h
@@ -0,0 +1,21 @@
+//===---------- Baremetal implementation of time utils ----------*- 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___SUPPORT_OSUTIL_BAREMETAL_TIME_H
+#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_BAREMETAL_TIME_H
+
+#include "include/llvm-libc-types/struct_timespec.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int time_utc_get(struct timespec *ts);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_BAREMETAL_IO_H
diff --git a/libc/src/time/CMakeLists.txt b/libc/src/time/CMakeLists.txt
index b3318e7ca87fa5..f18e74a15e6fc2 100644
--- a/libc/src/time/CMakeLists.txt
+++ b/libc/src/time/CMakeLists.txt
@@ -111,6 +111,13 @@ add_entrypoint_object(
.${LIBC_TARGET_OS}.time
)
+add_entrypoint_object(
+ timespec_get
+ ALIAS
+ DEPENDS
+ .${LIBC_TARGET_OS}.timespec_get
+)
+
add_entrypoint_object(
clock
ALIAS
diff --git a/libc/src/time/baremetal/CMakeLists.txt b/libc/src/time/baremetal/CMakeLists.txt
new file mode 100644
index 00000000000000..4bddfa691d7c2a
--- /dev/null
+++ b/libc/src/time/baremetal/CMakeLists.txt
@@ -0,0 +1,11 @@
+add_entrypoint_object(
+ timespec_get
+ SRCS
+ timespec_get.cpp
+ HDRS
+ ../timespec_get.h
+ DEPENDS
+ libc.hdr.time_macros
+ libc.hdr.types.struct_timespec
+ libc.src.__support.OSUtil.osutil
+)
diff --git a/libc/src/time/baremetal/timespec_get.cpp b/libc/src/time/baremetal/timespec_get.cpp
new file mode 100644
index 00000000000000..93ae4e9c9d1731
--- /dev/null
+++ b/libc/src/time/baremetal/timespec_get.cpp
@@ -0,0 +1,25 @@
+//===-- 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"
+#include "src/__support/OSUtil/baremetal/time.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, timespec_get, (struct timespec *ts, int base)) {
+ if (base != TIME_UTC) {
+ return 0;
+ }
+
+ return time_utc_get(ts);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/time/linux/CMakeLists.txt b/libc/src/time/linux/CMakeLists.txt
index c15fb44ad5d123..31fd7d1e64c85c 100644
--- a/libc/src/time/linux/CMakeLists.txt
+++ b/libc/src/time/linux/CMakeLists.txt
@@ -11,6 +11,19 @@ add_entrypoint_object(
libc.src.errno.errno
)
+add_entrypoint_object(
+ timespec_get
+ SRCS
+ timespec_get.cpp
+ HDRS
+ ../timespec_get.h
+ DEPENDS
+ libc.hdr.time_macros
+ libc.hdr.types.struct_timespec
+ libc.src.__support.time.linux.clock_gettime
+ libc.src.errno.errno
+)
+
add_entrypoint_object(
clock
SRCS
diff --git a/libc/src/time/linux/timespec_get.cpp b/libc/src/time/linux/timespec_get.cpp
new file mode 100644
index 00000000000000..fa236cd3eaebee
--- /dev/null
+++ b/libc/src/time/linux/timespec_get.cpp
@@ -0,0 +1,31 @@
+//===-- 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)) {
+ if (base != TIME_UTC) {
+ return 0;
+ }
+
+ auto result = internal::clock_gettime(CLOCK_REALTIME, ts);
+ if (!result.has_value()) {
+ libc_errno = result.error();
+ return 0;
+ }
+ return base;
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/time/timespec_get.h b/libc/src/time/timespec_get.h
new file mode 100644
index 00000000000000..32ba159a941606
--- /dev/null
+++ b/libc/src/time/timespec_get.h
@@ -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 "src/__support/macros/config.h"
+#include <time.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
diff --git a/libc/test/src/time/CMakeLists.txt b/libc/test/src/time/CMakeLists.txt
index bba01f063fed27..7151526b72b26d 100644
--- a/libc/test/src/time/CMakeLists.txt
+++ b/libc/test/src/time/CMakeLists.txt
@@ -162,6 +162,16 @@ add_libc_unittest(
libc.src.errno.errno
)
+add_libc_test(
+ timespec_get_test
+ SUITE
+ libc_time_unittests
+ SRCS
+ timespec_get_test.cpp
+ DEPENDS
+ libc.src.time.timespec_get
+)
+
add_libc_test(
clock_test
SUITE
diff --git a/libc/test/src/time/timespec_get_test.cpp b/libc/test/src/time/timespec_get_test.cpp
new file mode 100644
index 00000000000000..5bbd3db651d8a7
--- /dev/null
+++ b/libc/test/src/time/timespec_get_test.cpp
@@ -0,0 +1,32 @@
+//===-- 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 "src/__support/macros/properties/architectures.h"
+#include "src/time/timespec_get.h"
+#include "test/UnitTest/Test.h"
+
+#include <time.h>
+
+TEST(LlvmLibcTimespecGet, Utc) {
+#ifndef LIBC_TARGET_ARCH_IS_GPU
+ timespec ts;
+ int result;
+ result = LIBC_NAMESPACE::timespec_get(&ts, TIME_UTC);
+ ASSERT_EQ(result, TIME_UTC);
+ ASSERT_GT(ts.tv_sec, time_t(0));
+#endif
+}
+
+TEST(LlvmLibcTimespecGet, Unknown) {
+#ifndef LIBC_TARGET_ARCH_IS_GPU
+ timespec ts;
+ int result;
+ result = LIBC_NAMESPACE::timespec_get(&ts, 0);
+ ASSERT_EQ(result, 0);
+#endif
+}
>From 9fc2d098deedff143264d6348233cb81896c14f5 Mon Sep 17 00:00:00 2001
From: Petr Hosek <phosek at google.com>
Date: Wed, 13 Nov 2024 13:56:01 -0800
Subject: [PATCH 2/3] Rename __llvm_libc_time_utc_get to
__llvm_libc_timespec_get_utc
---
libc/src/__support/OSUtil/baremetal/time.cpp | 8 +++++---
libc/src/__support/OSUtil/baremetal/time.h | 4 +++-
libc/src/time/baremetal/timespec_get.cpp | 9 ++++++---
libc/src/time/linux/timespec_get.cpp | 2 +-
libc/src/time/timespec_get.h | 2 +-
5 files changed, 16 insertions(+), 9 deletions(-)
diff --git a/libc/src/__support/OSUtil/baremetal/time.cpp b/libc/src/__support/OSUtil/baremetal/time.cpp
index 63f0624b4cda4e..70f1d210f2a661 100644
--- a/libc/src/__support/OSUtil/baremetal/time.cpp
+++ b/libc/src/__support/OSUtil/baremetal/time.cpp
@@ -11,11 +11,13 @@
#include "src/__support/macros/config.h"
namespace LIBC_NAMESPACE_DECL {
+namespace internal {
-extern "C" int __llvm_libc_time_utc_get(struct timespec *ts);
+extern "C" int __llvm_libc_timespec_get_utc(struct timespec *ts);
-int time_utc_get(struct timespec *ts) {
- return __llvm_libc_time_utc_get(ts);
+bool timespec_get_utc(struct timespec *ts) {
+ return __llvm_libc_timespec_get_utc(ts);
}
+} // namespace internal
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/__support/OSUtil/baremetal/time.h b/libc/src/__support/OSUtil/baremetal/time.h
index 8c3ded377c7376..a114e3061ed6c3 100644
--- a/libc/src/__support/OSUtil/baremetal/time.h
+++ b/libc/src/__support/OSUtil/baremetal/time.h
@@ -13,9 +13,11 @@
#include "src/__support/macros/config.h"
namespace LIBC_NAMESPACE_DECL {
+namespace internal{
-int time_utc_get(struct timespec *ts);
+bool timespec_get_utc(struct timespec *ts);
+} // namespace internal
} // namespace LIBC_NAMESPACE_DECL
#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_BAREMETAL_IO_H
diff --git a/libc/src/time/baremetal/timespec_get.cpp b/libc/src/time/baremetal/timespec_get.cpp
index 93ae4e9c9d1731..9bd329f6e83198 100644
--- a/libc/src/time/baremetal/timespec_get.cpp
+++ b/libc/src/time/baremetal/timespec_get.cpp
@@ -8,18 +8,21 @@
#include "src/time/timespec_get.h"
#include "hdr/time_macros.h"
+#include "src/__support/OSUtil/baremetal/time.h"
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
-#include "src/__support/OSUtil/baremetal/time.h"
namespace LIBC_NAMESPACE_DECL {
-LLVM_LIBC_FUNCTION(int, timespec_get, (struct timespec *ts, int base)) {
+LLVM_LIBC_FUNCTION(int, timespec_get, (struct timespec * ts, int base)) {
if (base != TIME_UTC) {
return 0;
}
- return time_utc_get(ts);
+ if (!internal::timespec_get_utc(ts)) {
+ return 0;
+ }
+ return base;
}
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/time/linux/timespec_get.cpp b/libc/src/time/linux/timespec_get.cpp
index fa236cd3eaebee..88de72c6573ef4 100644
--- a/libc/src/time/linux/timespec_get.cpp
+++ b/libc/src/time/linux/timespec_get.cpp
@@ -15,7 +15,7 @@
namespace LIBC_NAMESPACE_DECL {
-LLVM_LIBC_FUNCTION(int, timespec_get, (struct timespec *ts, int base)) {
+LLVM_LIBC_FUNCTION(int, timespec_get, (struct timespec * ts, int base)) {
if (base != TIME_UTC) {
return 0;
}
diff --git a/libc/src/time/timespec_get.h b/libc/src/time/timespec_get.h
index 32ba159a941606..4e5d28b0a1cc23 100644
--- a/libc/src/time/timespec_get.h
+++ b/libc/src/time/timespec_get.h
@@ -1,4 +1,4 @@
-//===-- Implementation header of timespec_get --------------------*- C++ -*-===//
+//===-- 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.
>From 5560670ddb86ebc5526277f55f77c327bb6ebe2b Mon Sep 17 00:00:00 2001
From: Petr Hosek <phosek at google.com>
Date: Wed, 13 Nov 2024 14:04:34 -0800
Subject: [PATCH 3/3] Formatting
---
libc/src/__support/OSUtil/baremetal/time.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libc/src/__support/OSUtil/baremetal/time.h b/libc/src/__support/OSUtil/baremetal/time.h
index a114e3061ed6c3..15a1bf37454f8f 100644
--- a/libc/src/__support/OSUtil/baremetal/time.h
+++ b/libc/src/__support/OSUtil/baremetal/time.h
@@ -13,7 +13,7 @@
#include "src/__support/macros/config.h"
namespace LIBC_NAMESPACE_DECL {
-namespace internal{
+namespace internal {
bool timespec_get_utc(struct timespec *ts);
More information about the libc-commits
mailing list