Skip to content

Commit af569cd

Browse files
[libc] Unify gettime implmenetations
Similar to D159208, this patch unifies the calls to a syscall, in this patch it is the syscall SYS_clock_gettime/SYS_clock_gettime64. This patch also fixes calls to SYS_clock_gettime64 by creating a timespec64 object, passing it to the syscall and rewriting the timespec given by the caller with timespec64 object's contents. This fixes cases where timespec has a 4 bytes long time_t member, but SYS_clock_gettime is not available (e.g., rv32).
1 parent ba6af8f commit af569cd

File tree

5 files changed

+71
-55
lines changed

5 files changed

+71
-55
lines changed

libc/src/time/clock_gettime.cpp

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
1212
#include "src/__support/common.h"
1313
#include "src/errno/libc_errno.h"
14+
#include "src/time/linux/clockGetTimeImpl.h"
1415

1516
#include <sys/syscall.h> // For syscall numbers.
1617
#include <time.h>
@@ -19,26 +20,15 @@ namespace __llvm_libc {
1920

2021
// TODO(michaelrj): Move this into time/linux with the other syscalls.
2122
LLVM_LIBC_FUNCTION(int, clock_gettime,
22-
(clockid_t clockid, struct timespec *tp)) {
23-
#if SYS_clock_gettime
24-
int ret = __llvm_libc::syscall_impl<int>(SYS_clock_gettime,
25-
static_cast<long>(clockid),
26-
reinterpret_cast<long>(tp));
27-
#elif defined(SYS_clock_gettime64)
28-
int ret = __llvm_libc::syscall_impl<int>(SYS_clock_gettime64,
29-
static_cast<long>(clockid),
30-
reinterpret_cast<long>(tp));
31-
#else
32-
#error "SYS_clock_gettime and SYS_clock_gettime64 syscalls not available."
33-
#endif
23+
(clockid_t clockid, struct timespec *ts)) {
24+
auto result = internal::clock_gettimeimpl(clockid, ts);
3425

3526
// A negative return value indicates an error with the magnitude of the
3627
// value being the error code.
37-
if (ret < 0) {
38-
libc_errno = -ret;
28+
if (!result.has_value()) {
29+
libc_errno = result.error();
3930
return -1;
4031
}
41-
4232
return 0;
4333
}
4434

libc/src/time/gettimeofday.cpp

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
1212
#include "src/__support/common.h"
1313
#include "src/errno/libc_errno.h"
14+
#include "src/time/linux/clockGetTimeImpl.h"
1415

1516
#include <sys/syscall.h> // For syscall numbers.
1617

@@ -21,26 +22,19 @@ LLVM_LIBC_FUNCTION(int, gettimeofday,
2122
(struct timeval * tv, [[maybe_unused]] void *unused)) {
2223
if (tv == nullptr)
2324
return 0;
24-
struct timespec tp;
25-
#if SYS_clock_gettime
26-
int ret = __llvm_libc::syscall_impl<int>(SYS_clock_gettime,
27-
static_cast<long>(CLOCK_REALTIME),
28-
reinterpret_cast<long>(&tp));
29-
#elif defined(SYS_clock_gettime64)
30-
int ret = __llvm_libc::syscall_impl<int>(SYS_clock_gettime64,
31-
static_cast<long>(CLOCK_REALTIME),
32-
reinterpret_cast<long>(&tp));
33-
#else
34-
#error "SYS_clock_gettime and SYS_clock_gettime64 syscalls not available."
35-
#endif
25+
26+
struct timespec ts;
27+
auto result = internal::clock_gettimeimpl(CLOCK_REALTIME, &ts);
28+
3629
// A negative return value indicates an error with the magnitude of the
3730
// value being the error code.
38-
if (ret < 0) {
39-
libc_errno = -ret;
31+
if (!result.has_value()) {
32+
libc_errno = result.error();
4033
return -1;
4134
}
42-
tv->tv_sec = tp.tv_sec;
43-
tv->tv_usec = static_cast<suseconds_t>(tp.tv_nsec / 1000);
35+
36+
tv->tv_sec = ts.tv_sec;
37+
tv->tv_usec = static_cast<suseconds_t>(ts.tv_nsec / 1000);
4438
return 0;
4539
}
4640

libc/src/time/linux/clock.cpp

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
1313
#include "src/__support/common.h"
1414
#include "src/errno/libc_errno.h"
15+
#include "src/time/linux/clockGetTimeImpl.h"
1516

1617
#include <sys/syscall.h> // For syscall numbers.
1718
#include <time.h>
@@ -20,19 +21,10 @@ namespace __llvm_libc {
2021

2122
LLVM_LIBC_FUNCTION(clock_t, clock, ()) {
2223
struct timespec ts;
23-
#if SYS_clock_gettime
24-
int ret = __llvm_libc::syscall_impl<int>(
25-
SYS_clock_gettime, CLOCK_PROCESS_CPUTIME_ID, reinterpret_cast<long>(&ts));
26-
#elif defined(SYS_clock_gettime64)
27-
int ret = __llvm_libc::syscall_impl<int>(SYS_clock_gettime64,
28-
CLOCK_PROCESS_CPUTIME_ID,
29-
reinterpret_cast<long>(&ts));
30-
#else
31-
#error "SYS_clock_gettime and SYS_clock_gettime64 syscalls not available."
32-
#endif
33-
if (ret < 0) {
34-
libc_errno = -ret;
35-
return clock_t(-1);
24+
auto result = internal::clock_gettimeimpl(CLOCK_PROCESS_CPUTIME_ID, &ts);
25+
if (!result.has_value()) {
26+
libc_errno = result.error();
27+
return -1;
3628
}
3729

3830
// The above syscall gets the CPU time in seconds plus nanoseconds.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//===- Linux implementation of the POSIX clock_gettime function -*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_TIME_LINUX_CLOCKGETTIMEIMPL_H
10+
#define LLVM_LIBC_SRC_TIME_LINUX_CLOCKGETTIMEIMPL_H
11+
12+
#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
13+
#include "src/__support/common.h"
14+
#include "src/__support/error_or.h"
15+
#include "src/errno/libc_errno.h"
16+
17+
#include <sys/syscall.h> // For syscall numbers.
18+
#include <time.h>
19+
20+
namespace __llvm_libc {
21+
namespace internal {
22+
23+
LIBC_INLINE ErrorOr<int> clock_gettimeimpl(clockid_t clockid,
24+
struct timespec *ts) {
25+
#if SYS_clock_gettime
26+
int ret = __llvm_libc::syscall_impl<int>(SYS_clock_gettime,
27+
static_cast<long>(clockid),
28+
reinterpret_cast<long>(ts));
29+
#elif defined(SYS_clock_gettime64)
30+
struct timespec64 ts64;
31+
int ret = __llvm_libc::syscall_impl<int>(SYS_clock_gettime64,
32+
static_cast<long>(clockid),
33+
reinterpret_cast<long>(&ts64));
34+
ts->tv_sec = static_cast<time_t>(ts64.tv_sec);
35+
ts->tv_nsec = static_cast<long>(ts64.tv_nsec);
36+
#else
37+
#error "SYS_clock_gettime and SYS_clock_gettime64 syscalls not available."
38+
#endif
39+
if (ret < 0)
40+
return Error(-ret);
41+
return ret;
42+
}
43+
44+
} // namespace internal
45+
} // namespace __llvm_libc
46+
47+
#endif // LLVM_LIBC_SRC_TIME_LINUX_CLOCKGETTIMEIMPL_H

libc/src/time/linux/time.cpp

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
1212
#include "src/__support/common.h"
1313
#include "src/errno/libc_errno.h"
14+
#include "src/time/linux/clockGetTimeImpl.h"
1415

1516
#include <sys/syscall.h> // For syscall numbers.
1617
#include <time.h>
@@ -20,17 +21,9 @@ namespace __llvm_libc {
2021
LLVM_LIBC_FUNCTION(time_t, time, (time_t * tp)) {
2122
// TODO: Use the Linux VDSO to fetch the time and avoid the syscall.
2223
struct timespec ts;
23-
#if SYS_clock_gettime
24-
int ret = __llvm_libc::syscall_impl<int>(SYS_clock_gettime, CLOCK_REALTIME,
25-
reinterpret_cast<long>(&ts));
26-
#elif defined(SYS_clock_gettime64)
27-
int ret = __llvm_libc::syscall_impl<int>(SYS_clock_gettime64, CLOCK_REALTIME,
28-
reinterpret_cast<long>(&ts));
29-
#else
30-
#error "SYS_clock_gettime and SYS_clock_gettime64 syscalls not available."
31-
#endif
32-
if (ret < 0) {
33-
libc_errno = -ret;
24+
auto result = internal::clock_gettimeimpl(CLOCK_REALTIME, &ts);
25+
if (!result.has_value()) {
26+
libc_errno = result.error();
3427
return -1;
3528
}
3629

0 commit comments

Comments
 (0)