[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
This commit is contained in:
parent
dd8fd6437e
commit
dbd31935ed
|
@ -45,9 +45,6 @@ def ConstStructDirentPtrPtr : ConstType<StructDirentPtrPtr>;
|
||||||
def StructTimeSpec : NamedType<"struct timespec">;
|
def StructTimeSpec : NamedType<"struct timespec">;
|
||||||
def StructTimeSpecPtr : PtrType<StructTimeSpec>;
|
def StructTimeSpecPtr : PtrType<StructTimeSpec>;
|
||||||
|
|
||||||
def StructTimeVal : NamedType<"struct timeval">;
|
|
||||||
def StructTimeValPtr : PtrType<StructTimeVal>;
|
|
||||||
|
|
||||||
def ExecArgvT : NamedType<"__exec_argv_t">;
|
def ExecArgvT : NamedType<"__exec_argv_t">;
|
||||||
def ExecEnvpT : NamedType<"__exec_envp_t">;
|
def ExecEnvpT : NamedType<"__exec_envp_t">;
|
||||||
|
|
||||||
|
@ -733,7 +730,7 @@ def POSIX : StandardSpec<"POSIX"> {
|
||||||
UidT,
|
UidT,
|
||||||
GidT,
|
GidT,
|
||||||
StructTimeSpec,
|
StructTimeSpec,
|
||||||
StructTimeVal,
|
StructTimevalType,
|
||||||
BlkSizeT,
|
BlkSizeT,
|
||||||
BlkCntT,
|
BlkCntT,
|
||||||
OffTType,
|
OffTType,
|
||||||
|
@ -1077,7 +1074,7 @@ def POSIX : StandardSpec<"POSIX"> {
|
||||||
HeaderSpec Time = HeaderSpec<
|
HeaderSpec Time = HeaderSpec<
|
||||||
"time.h",
|
"time.h",
|
||||||
[], // Macros
|
[], // Macros
|
||||||
[ClockIdT, StructTimeSpec, StructTimeVal], // Types
|
[ClockIdT, StructTimeSpec, StructTimevalType], // Types
|
||||||
[], // Enumerations
|
[], // Enumerations
|
||||||
[
|
[
|
||||||
FunctionSpec<
|
FunctionSpec<
|
||||||
|
@ -1088,7 +1085,7 @@ def POSIX : StandardSpec<"POSIX"> {
|
||||||
FunctionSpec<
|
FunctionSpec<
|
||||||
"gettimeofday",
|
"gettimeofday",
|
||||||
RetValSpec<IntType>,
|
RetValSpec<IntType>,
|
||||||
[ArgSpec<StructTimeValPtr>, ArgSpec<VoidPtr>]
|
[ArgSpec<StructTimevalPtr>, ArgSpec<VoidPtr>]
|
||||||
>,
|
>,
|
||||||
FunctionSpec<
|
FunctionSpec<
|
||||||
"nanosleep",
|
"nanosleep",
|
||||||
|
|
|
@ -10,7 +10,6 @@
|
||||||
|
|
||||||
#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
|
#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
|
||||||
#include "src/__support/common.h"
|
#include "src/__support/common.h"
|
||||||
#include "src/time/clock_gettime.h"
|
|
||||||
|
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <sys/syscall.h> // For syscall numbers.
|
#include <sys/syscall.h> // For syscall numbers.
|
||||||
|
@ -23,9 +22,15 @@ LLVM_LIBC_FUNCTION(int, gettimeofday,
|
||||||
return 0;
|
return 0;
|
||||||
clockid_t clockid = CLOCK_REALTIME;
|
clockid_t clockid = CLOCK_REALTIME;
|
||||||
struct timespec tp;
|
struct timespec tp;
|
||||||
long ret_val = __llvm_libc::clock_gettime(clockid, &tp);
|
long ret_val =
|
||||||
if (ret_val < 0)
|
__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;
|
return -1;
|
||||||
|
}
|
||||||
tv->tv_sec = tp.tv_sec;
|
tv->tv_sec = tp.tv_sec;
|
||||||
tv->tv_usec = tp.tv_nsec / 1000;
|
tv->tv_usec = tp.tv_nsec / 1000;
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
@ -20,19 +20,23 @@ TEST(LlvmLibcGettimeofday, SmokeTest) {
|
||||||
using __llvm_libc::testing::ErrnoSetterMatcher::Succeeds;
|
using __llvm_libc::testing::ErrnoSetterMatcher::Succeeds;
|
||||||
void *tz = nullptr;
|
void *tz = nullptr;
|
||||||
struct timeval tv;
|
struct timeval tv;
|
||||||
|
|
||||||
|
int sleep_times[2] = {200, 1000};
|
||||||
|
for (int i = 0; i < 2; i++) {
|
||||||
int ret = __llvm_libc::gettimeofday(&tv, tz);
|
int ret = __llvm_libc::gettimeofday(&tv, tz);
|
||||||
ASSERT_EQ(ret, 0);
|
ASSERT_EQ(ret, 0);
|
||||||
|
|
||||||
// Sleep for 200 microsceconds.
|
int sleep_time = -sleep_times[i];
|
||||||
struct timespec tim = {0, 200 * 1000};
|
// Sleep for {sleep_time} microsceconds.
|
||||||
|
struct timespec tim = {0, sleep_time * 1000};
|
||||||
struct timespec tim2 = {0, 0};
|
struct timespec tim2 = {0, 0};
|
||||||
ret = __llvm_libc::nanosleep(&tim, &tim2);
|
ret = __llvm_libc::nanosleep(&tim, &tim2);
|
||||||
|
|
||||||
// Call gettimeofday again and verify that it is more 100 microscecods and
|
// Call gettimeofday again and verify that it is more {sleep_time}
|
||||||
// less than 300 microseconds,
|
// microscecods.
|
||||||
struct timeval tv1;
|
struct timeval tv1;
|
||||||
ret = __llvm_libc::gettimeofday(&tv1, tz);
|
ret = __llvm_libc::gettimeofday(&tv1, tz);
|
||||||
ASSERT_EQ(ret, 0);
|
ASSERT_EQ(ret, 0);
|
||||||
ASSERT_GT(tv1.tv_usec - tv.tv_usec, 100);
|
ASSERT_GE(tv1.tv_usec - tv.tv_usec, sleep_time);
|
||||||
ASSERT_LT(tv1.tv_usec - tv.tv_usec, 300);
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue