Use clock_gettime to implement time.

Change the default implementation of time to call clock_gettime,
to align with new Linux ports that are expected to only implement
__NR_clock_gettime.  Arch-specific implementation that either call
the time vDSO or route to gettimeofday vDSO are not removed.

Also for Linux, CLOCK_REALTIME_COARSE is used instead of generic
CLOCK_REALTIME clockid.  This takes less CPU time and its behavior
better matches what the current glibc does.

Checked on x86_64-linux-gnu, i686-linux-gnu, powerpc64le-linux-gnu,
powerpc64-linux-gnu, powerpc-linux-gnu, and aarch64-linux-gnu.

Co-authored-by: Zack Weinberg <zackw@panix.com>
Reviewed-by: Lukasz Majewski <lukma@denx.de>
This commit is contained in:
Adhemerval Zanella 2019-10-24 17:52:30 +00:00
parent c3f9aef063
commit 0d56378349
4 changed files with 17 additions and 57 deletions

View File

@ -1,4 +1,5 @@
/* Copyright (C) 2005-2019 Free Software Foundation, Inc. /* System specific time definitions. Generic Version.
Copyright 2019 Free Software Foundation, Inc.
This file is part of the GNU C Library. This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or The GNU C Library is free software; you can redistribute it and/or
@ -15,27 +16,5 @@
License along with the GNU C Library; if not, see License along with the GNU C Library; if not, see
<https://www.gnu.org/licenses/>. */ <https://www.gnu.org/licenses/>. */
#include <stddef.h> /* Timer used on clock_gettime for time implementation. */
#include <time.h> #define TIME_CLOCK_GETTIME_CLOCKID CLOCK_REALTIME
#include <sysdep.h>
#ifdef __NR_time
time_t
time (time_t *t)
{
INTERNAL_SYSCALL_DECL (err);
time_t res = INTERNAL_SYSCALL (time, err, 1, NULL);
/* There cannot be any error. */
if (t != NULL)
*t = res;
return res;
}
libc_hidden_def (time)
#else
# include <sysdeps/posix/time.c>
#endif

View File

@ -78,6 +78,6 @@ libc_hidden_def (time)
#else #else
#include <sysdeps/posix/time.c> #include <time/time.c>
#endif /* !SHARED */ #endif /* !SHARED */

View File

@ -1,4 +1,5 @@
/* Copyright (C) 1991-2019 Free Software Foundation, Inc. /* System specific time definitions. Generic Version.
Copyright 2019 Free Software Foundation, Inc.
This file is part of the GNU C Library. This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or The GNU C Library is free software; you can redistribute it and/or
@ -15,26 +16,7 @@
License along with the GNU C Library; if not, see License along with the GNU C Library; if not, see
<https://www.gnu.org/licenses/>. */ <https://www.gnu.org/licenses/>. */
#include <stddef.h> /* For NULL. */ /* Timer used on clock_gettime for time implementation. For Linux
#include <time.h> it uses the coarse version which returns the time at the last tick
#include <sys/time.h> and mimic what time as syscall should return. */
#define TIME_CLOCK_GETTIME_CLOCKID CLOCK_REALTIME_COARSE
/* Return the current time as a `time_t' and also put it in *T if T is
not NULL. Time is represented as seconds from Jan 1 00:00:00 1970. */
time_t
time (time_t *t)
{
struct timeval tv;
time_t result;
if (__gettimeofday (&tv, (struct timezone *) NULL))
result = (time_t) -1;
else
result = (time_t) tv.tv_sec;
if (t != NULL)
*t = result;
return result;
}
libc_hidden_def (time)

View File

@ -15,19 +15,18 @@
License along with the GNU C Library; if not, see License along with the GNU C Library; if not, see
<https://www.gnu.org/licenses/>. */ <https://www.gnu.org/licenses/>. */
#include <errno.h>
#include <time.h> #include <time.h>
#include <time-clockid.h>
/* Return the time now, and store it in *TIMER if not NULL. */ /* Return the time now, and store it in *TIMER if not NULL. */
time_t time_t
time (time_t *timer) time (time_t *timer)
{ {
__set_errno (ENOSYS); struct timespec ts;
__clock_gettime (TIME_CLOCK_GETTIME_CLOCKID, &ts);
if (timer != NULL) if (timer)
*timer = (time_t) -1; *timer = ts.tv_sec;
return (time_t) -1; return ts.tv_sec;
} }
libc_hidden_def (time) libc_hidden_def (time)
stub_warning (time)