[libc] Implement clock_getres in time
Summary
Implement the standard POSIX.1-2008 / POSIX.1-2024 function clock_getres in <time.h> on Linux.
POSIX specification: https://pubs.opengroup.org/onlinepubs/9799919799/functions/clock_getres.html
Background & Context
While Windows has an implementation in libc/src/time/windows/clock_getres.cpp (which causes docgen to show a checkmark), clock_getres is currently unimplemented on Linux:
libc/src/time/linux/clock_getres.cppdoes not exist.- It is not yet declared in
libc/include/time.yaml. - It is missing from Linux target entrypoints (
libc/config/linux/*/entrypoints.txt).
int clock_getres(clockid_t clock_id, struct timespec *res);
The underlying Linux syscall wrapper linux_syscalls::clock_getres is already available in libc/src/__support/OSUtil/linux/syscall_wrappers/clock_getres.h, and hermetic unit tests already exist in libc/test/src/time/clock_getres_test.cpp.
This issue tracks adding the Linux implementation and registering the entrypoint.
I would like to work on this issue.
@llvm/issue-subscribers-libc
Author: Aman Maurya (amanmaurya92)
POSIX specification: https://pubs.opengroup.org/onlinepubs/9799919799/functions/clock_getres.html
Background & Context
clock_getres is currently listed as missing in libc/utils/docgen/time.yaml and is not yet declared in libc/include/time.yaml.
Function signature:
c int clock_getres(clockid_t clock_id, struct timespec *res);
The public implementation header libc/src/time/clock_getres.h already exists, and the underlying Linux syscall wrapper is already implemented in libc/src/__support/OSUtil/linux/syscall_wrappers/clock_getres.h (linux_syscalls::clock_getres), supporting both SYS_clock_getres_time64 and SYS_clock_getres with 64-bit imespec validation:
`cpp namespace LIBC_NAMESPACE_DECL { namespace linux_syscalls {
LIBC_INLINE ErrorOr<int> clock_getres(clockid_t clockid, timespec *res);
} // namespace linux_syscalls } // namespace LIBC_NAMESPACE_DECL `
In fact, clock_getcpuclockid in libc/src/time/linux/clock_getcpuclockid.cpp already invokes linux_syscalls::clock_getres(pid_clockid, nullptr), and hermetic unit tests are already present in libc/test/src/time/clock_getres_test.cpp.
Implementing clock_getres on Linux will:
- Declare clock_getres in libc/include/time.yaml and update libc/utils/docgen/time.yaml.
- Implement libc/src/time/linux/clock_getres.cpp delegating to linux_syscalls::clock_getres.
- Register the entrypoint in libc/src/time/linux/CMakeLists.txt and expose libc.src.time.clock_getres across Linux target entrypoints (x86_64, arch64, iscv).
I would like to work on this issue.
the current header implementation status page says both clock_getres and clock_gettime (#230535) are implemented, am i missing something?
3.
the current header implementation status page says both
clock_getresandclock_gettime(#230535) are implemented, am i missing something?
clock_getres is unimplemented on Linux. and clock_getres shows a green checkmark only because the Windows file libc/src/time/windows/clock_getres.cpp exists. i will update the issue discription to make that more clear.
thanks for the clarification @amanmaurya92.
the current header implementation status page says both