3 changes: 2 additions & 1 deletion libc/src/time/gpu/time_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
#ifndef LLVM_LIBC_SRC_TIME_GPU_TIME_UTILS_H
#define LLVM_LIBC_SRC_TIME_GPU_TIME_UTILS_H

#include "hdr/time_macros.h"
#include "hdr/types/clock_t.h"
#include "src/__support/GPU/utils.h"

namespace LIBC_NAMESPACE {

#if defined(LIBC_TARGET_ARCH_IS_AMDGPU)
Expand Down
30 changes: 16 additions & 14 deletions libc/src/time/linux/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ add_entrypoint_object(
HDRS
../time_func.h
DEPENDS
libc.include.time
libc.include.sys_syscall
libc.src.__support.OSUtil.osutil
libc.hdr.time_macros
libc.hdr.types.time_t
libc.src.__support.time.linux.clock_gettime
libc.src.errno.errno
)

Expand All @@ -18,10 +18,11 @@ add_entrypoint_object(
HDRS
../clock.h
DEPENDS
libc.include.time
libc.include.sys_syscall
libc.hdr.time_macros
libc.hdr.types.clock_t
libc.src.__support.time.units
libc.src.__support.time.linux.clock_gettime
libc.src.__support.CPP.limits
libc.src.__support.OSUtil.osutil
libc.src.errno.errno
)

Expand All @@ -32,10 +33,10 @@ add_entrypoint_object(
HDRS
../nanosleep.h
DEPENDS
libc.include.time
libc.hdr.types.struct_timespec
libc.include.sys_syscall
libc.src.__support.CPP.limits
libc.src.__support.OSUtil.osutil
libc.src.__support.CPP.limits
libc.src.errno.errno
)

Expand All @@ -46,9 +47,9 @@ add_entrypoint_object(
HDRS
../clock_gettime.h
DEPENDS
libc.include.time
libc.include.sys_syscall
libc.src.__support.OSUtil.osutil
libc.hdr.types.clockid_t
libc.hdr.types.struct_timespec
libc.src.__support.time.linux.clock_gettime
libc.src.errno.errno
)

Expand All @@ -59,8 +60,9 @@ add_entrypoint_object(
HDRS
../gettimeofday.h
DEPENDS
libc.include.time
libc.include.sys_syscall
libc.src.__support.OSUtil.osutil
libc.hdr.time_macros
libc.hdr.types.suseconds_t
libc.src.__support.time.linux.clock_gettime
libc.src.__support.time.units
libc.src.errno.errno
)
20 changes: 9 additions & 11 deletions libc/src/time/linux/clock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,19 @@
//===----------------------------------------------------------------------===//

#include "src/time/clock.h"

#include "hdr/time_macros.h"
#include "src/__support/CPP/limits.h"
#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
#include "src/__support/common.h"
#include "src/__support/time/linux/clock_gettime.h"
#include "src/__support/time/units.h"
#include "src/errno/libc_errno.h"
#include "src/time/linux/clockGetTimeImpl.h"

#include <sys/syscall.h> // For syscall numbers.
#include <time.h>

namespace LIBC_NAMESPACE {

LLVM_LIBC_FUNCTION(clock_t, clock, ()) {
using namespace time_units;
struct timespec ts;
auto result = internal::clock_gettimeimpl(CLOCK_PROCESS_CPUTIME_ID, &ts);
auto result = internal::clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);
if (!result.has_value()) {
libc_errno = result.error();
return -1;
Expand All @@ -34,15 +32,15 @@ LLVM_LIBC_FUNCTION(clock_t, clock, ()) {
cpp::numeric_limits<clock_t>::max() / CLOCKS_PER_SEC;
if (ts.tv_sec > CLOCK_SECS_MAX)
return clock_t(-1);
if (ts.tv_nsec / 1000000000 > CLOCK_SECS_MAX - ts.tv_sec)
if (ts.tv_nsec / 1_s_ns > CLOCK_SECS_MAX - ts.tv_sec)
return clock_t(-1);

// For the integer computation converting tv_nsec to clocks to work
// correctly, we want CLOCKS_PER_SEC to be less than 1000000000.
static_assert(1000000000 > CLOCKS_PER_SEC,
"Expected CLOCKS_PER_SEC to be less than 1000000000.");
static_assert(1_s_ns > CLOCKS_PER_SEC,
"Expected CLOCKS_PER_SEC to be less than 1'000'000'000.");
return clock_t(ts.tv_sec * CLOCKS_PER_SEC +
ts.tv_nsec / (1000000000 / CLOCKS_PER_SEC));
ts.tv_nsec / (1_s_ns / CLOCKS_PER_SEC));
}

} // namespace LIBC_NAMESPACE
9 changes: 2 additions & 7 deletions libc/src/time/linux/clock_gettime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,16 @@
//===----------------------------------------------------------------------===//

#include "src/time/clock_gettime.h"

#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
#include "src/__support/common.h"
#include "src/__support/time/linux/clock_gettime.h"
#include "src/errno/libc_errno.h"
#include "src/time/linux/clockGetTimeImpl.h"

#include <sys/syscall.h> // For syscall numbers.
#include <time.h>

namespace LIBC_NAMESPACE {

// TODO(michaelrj): Move this into time/linux with the other syscalls.
LLVM_LIBC_FUNCTION(int, clock_gettime,
(clockid_t clockid, struct timespec *ts)) {
auto result = internal::clock_gettimeimpl(clockid, ts);
auto result = internal::clock_gettime(clockid, ts);

// A negative return value indicates an error with the magnitude of the
// value being the error code.
Expand Down
14 changes: 7 additions & 7 deletions libc/src/time/linux/gettimeofday.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,24 @@
//===----------------------------------------------------------------------===//

#include "src/time/gettimeofday.h"

#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
#include "hdr/time_macros.h"
#include "hdr/types/suseconds_t.h"
#include "src/__support/common.h"
#include "src/__support/time/linux/clock_gettime.h"
#include "src/__support/time/units.h"
#include "src/errno/libc_errno.h"
#include "src/time/linux/clockGetTimeImpl.h"

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

namespace LIBC_NAMESPACE {

// TODO(michaelrj): Move this into time/linux with the other syscalls.
LLVM_LIBC_FUNCTION(int, gettimeofday,
(struct timeval * tv, [[maybe_unused]] void *unused)) {
using namespace time_units;
if (tv == nullptr)
return 0;

struct timespec ts;
auto result = internal::clock_gettimeimpl(CLOCK_REALTIME, &ts);
auto result = internal::clock_gettime(CLOCK_REALTIME, &ts);

// A negative return value indicates an error with the magnitude of the
// value being the error code.
Expand All @@ -34,7 +34,7 @@ LLVM_LIBC_FUNCTION(int, gettimeofday,
}

tv->tv_sec = ts.tv_sec;
tv->tv_usec = static_cast<suseconds_t>(ts.tv_nsec / 1000);
tv->tv_usec = static_cast<suseconds_t>(ts.tv_nsec / 1_us_ns);
return 0;
}

Expand Down
12 changes: 4 additions & 8 deletions libc/src/time/linux/time.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,18 @@
//
//===----------------------------------------------------------------------===//

#include "src/time/time_func.h"

#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
#include "hdr/time_macros.h"
#include "src/__support/common.h"
#include "src/__support/time/linux/clock_gettime.h"
#include "src/errno/libc_errno.h"
#include "src/time/linux/clockGetTimeImpl.h"

#include <sys/syscall.h> // For syscall numbers.
#include <time.h>
#include "src/time/time_func.h"

namespace LIBC_NAMESPACE {

LLVM_LIBC_FUNCTION(time_t, time, (time_t * tp)) {
// TODO: Use the Linux VDSO to fetch the time and avoid the syscall.
struct timespec ts;
auto result = internal::clock_gettimeimpl(CLOCK_REALTIME, &ts);
auto result = internal::clock_gettime(CLOCK_REALTIME, &ts);
if (!result.has_value()) {
libc_errno = result.error();
return -1;
Expand Down
4 changes: 2 additions & 2 deletions libc/src/time/nanosleep.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
#ifndef LLVM_LIBC_SRC_TIME_NANOSLEEP_H
#define LLVM_LIBC_SRC_TIME_NANOSLEEP_H

#include <time.h>
#include "hdr/types/struct_timespec.h"

namespace LIBC_NAMESPACE {

int nanosleep(const struct timespec *req, struct timespec *rem);
int nanosleep(const timespec *req, timespec *rem);

} // namespace LIBC_NAMESPACE

Expand Down
2 changes: 1 addition & 1 deletion libc/src/time/time_func.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#ifndef LLVM_LIBC_SRC_TIME_TIME_FUNC_H
#define LLVM_LIBC_SRC_TIME_TIME_FUNC_H

#include <time.h>
#include "hdr/types/time_t.h"

// Note this header file is named time_func.h to avoid conflicts with the
// public header file time.h.
Expand Down