[libc-commits] [libc] dbd3193 - [libc] cleanup changes to gettimeofday.
Raman Tenneti via libc-commits
libc-commits at lists.llvm.org
Tue Nov 15 15:06:43 PST 2022
Author: Raman Tenneti
Date: 2022-11-15T15:06:35-08:00
New Revision: dbd31935ed0384b5d72dd4fc5f0309853b2da844
URL: https://github.com/llvm/llvm-project/commit/dbd31935ed0384b5d72dd4fc5f0309853b2da844
DIFF: https://github.com/llvm/llvm-project/commit/dbd31935ed0384b5d72dd4fc5f0309853b2da844.diff
LOG: [libc] cleanup changes to gettimeofday.
+ Deleted duplicate definitions of StructTimeVal and StructTimeValPtr.
+ Caled syscall clock_gettime to get timespec data.
+ Added tests to test for sleeping 200 and 1000 microseconds.
+ Fixed comments from https://reviews.llvm.org/D137881
Reviewed By: sivachandra
Differential Revision: https://reviews.llvm.org/D138064
Added:
Modified:
libc/spec/posix.td
libc/src/time/gettimeofday.cpp
libc/test/src/time/gettimeofday_test.cpp
Removed:
################################################################################
diff --git a/libc/spec/posix.td b/libc/spec/posix.td
index dd1be611bdd96..43cc031953b65 100644
--- a/libc/spec/posix.td
+++ b/libc/spec/posix.td
@@ -45,9 +45,6 @@ def ConstStructDirentPtrPtr : ConstType<StructDirentPtrPtr>;
def StructTimeSpec : NamedType<"struct timespec">;
def StructTimeSpecPtr : PtrType<StructTimeSpec>;
-def StructTimeVal : NamedType<"struct timeval">;
-def StructTimeValPtr : PtrType<StructTimeVal>;
-
def ExecArgvT : NamedType<"__exec_argv_t">;
def ExecEnvpT : NamedType<"__exec_envp_t">;
@@ -733,7 +730,7 @@ def POSIX : StandardSpec<"POSIX"> {
UidT,
GidT,
StructTimeSpec,
- StructTimeVal,
+ StructTimevalType,
BlkSizeT,
BlkCntT,
OffTType,
@@ -1077,7 +1074,7 @@ def POSIX : StandardSpec<"POSIX"> {
HeaderSpec Time = HeaderSpec<
"time.h",
[], // Macros
- [ClockIdT, StructTimeSpec, StructTimeVal], // Types
+ [ClockIdT, StructTimeSpec, StructTimevalType], // Types
[], // Enumerations
[
FunctionSpec<
@@ -1088,7 +1085,7 @@ def POSIX : StandardSpec<"POSIX"> {
FunctionSpec<
"gettimeofday",
RetValSpec<IntType>,
- [ArgSpec<StructTimeValPtr>, ArgSpec<VoidPtr>]
+ [ArgSpec<StructTimevalPtr>, ArgSpec<VoidPtr>]
>,
FunctionSpec<
"nanosleep",
diff --git a/libc/src/time/gettimeofday.cpp b/libc/src/time/gettimeofday.cpp
index 911e587caf99b..115a8b7869634 100644
--- a/libc/src/time/gettimeofday.cpp
+++ b/libc/src/time/gettimeofday.cpp
@@ -10,7 +10,6 @@
#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
#include "src/__support/common.h"
-#include "src/time/clock_gettime.h"
#include <errno.h>
#include <sys/syscall.h> // For syscall numbers.
@@ -23,9 +22,15 @@ LLVM_LIBC_FUNCTION(int, gettimeofday,
return 0;
clockid_t clockid = CLOCK_REALTIME;
struct timespec tp;
- long ret_val = __llvm_libc::clock_gettime(clockid, &tp);
- if (ret_val < 0)
+ long ret_val =
+ __llvm_libc::syscall_impl(SYS_clock_gettime, static_cast<long>(clockid),
+ reinterpret_cast<long>(&tp));
+ // A negative return value indicates an error with the magnitude of the
+ // value being the error code.
+ if (ret_val < 0) {
+ errno = -ret_val;
return -1;
+ }
tv->tv_sec = tp.tv_sec;
tv->tv_usec = tp.tv_nsec / 1000;
return 0;
diff --git a/libc/test/src/time/gettimeofday_test.cpp b/libc/test/src/time/gettimeofday_test.cpp
index 2cd8d51670635..9fbc2b413b6c0 100644
--- a/libc/test/src/time/gettimeofday_test.cpp
+++ b/libc/test/src/time/gettimeofday_test.cpp
@@ -20,19 +20,23 @@ TEST(LlvmLibcGettimeofday, SmokeTest) {
using __llvm_libc::testing::ErrnoSetterMatcher::Succeeds;
void *tz = nullptr;
struct timeval tv;
- int ret = __llvm_libc::gettimeofday(&tv, tz);
- ASSERT_EQ(ret, 0);
- // Sleep for 200 microsceconds.
- struct timespec tim = {0, 200 * 1000};
- struct timespec tim2 = {0, 0};
- ret = __llvm_libc::nanosleep(&tim, &tim2);
+ int sleep_times[2] = {200, 1000};
+ for (int i = 0; i < 2; i++) {
+ int ret = __llvm_libc::gettimeofday(&tv, tz);
+ ASSERT_EQ(ret, 0);
- // Call gettimeofday again and verify that it is more 100 microscecods and
- // less than 300 microseconds,
- struct timeval tv1;
- ret = __llvm_libc::gettimeofday(&tv1, tz);
- ASSERT_EQ(ret, 0);
- ASSERT_GT(tv1.tv_usec - tv.tv_usec, 100);
- ASSERT_LT(tv1.tv_usec - tv.tv_usec, 300);
+ int sleep_time = -sleep_times[i];
+ // Sleep for {sleep_time} microsceconds.
+ struct timespec tim = {0, sleep_time * 1000};
+ struct timespec tim2 = {0, 0};
+ ret = __llvm_libc::nanosleep(&tim, &tim2);
+
+ // Call gettimeofday again and verify that it is more {sleep_time}
+ // microscecods.
+ struct timeval tv1;
+ ret = __llvm_libc::gettimeofday(&tv1, tz);
+ ASSERT_EQ(ret, 0);
+ ASSERT_GE(tv1.tv_usec - tv.tv_usec, sleep_time);
+ }
}
More information about the libc-commits
mailing list