diff mbox series

[v2,05/11] Use clock_gettime to implement time.

Message ID 20191025120832.27667-6-adhemerval.zanella@linaro.org
State New
Headers show
Series Y2038 preparation: use clock_[gs]ettime to implement the other time-getting and -setting functions. | expand

Commit Message

Adhemerval Zanella Netto Oct. 25, 2019, 12:08 p.m. UTC
Use clock_gettime to implement time.

Changes from previous version:

  - Do not remove Linux arch-specific implementations.

  - Use CLOCK_REALTIME_COARSE for Linux.

--

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>
---
 sysdeps/unix/sysv/linux/powerpc/time.c        |  2 +-
 .../sysv/linux/{time.c => time-internal.h}    | 31 ++++---------------
 sysdeps/posix/time.c => time/time-internal.h  | 28 +++--------------
 time/time.c                                   | 13 ++++----
 4 files changed, 17 insertions(+), 57 deletions(-)
 rename sysdeps/unix/sysv/linux/{time.c => time-internal.h} (64%)
 rename sysdeps/posix/time.c => time/time-internal.h (57%)

-- 
2.17.1

Comments

Lukasz Majewski Oct. 25, 2019, 8:59 p.m. UTC | #1
On Fri, 25 Oct 2019 09:08:26 -0300
Adhemerval Zanella <adhemerval.zanella@linaro.org> wrote:

> Use clock_gettime to implement time.

> 

> Changes from previous version:

> 

>   - Do not remove Linux arch-specific implementations.

> 

>   - Use CLOCK_REALTIME_COARSE for Linux.

> 

> --

> 

> 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>

> ---

>  sysdeps/unix/sysv/linux/powerpc/time.c        |  2 +-

>  .../sysv/linux/{time.c => time-internal.h}    | 31

> ++++--------------- sysdeps/posix/time.c => time/time-internal.h  |

> 28 +++-------------- time/time.c                                   |

> 13 ++++---- 4 files changed, 17 insertions(+), 57 deletions(-)

>  rename sysdeps/unix/sysv/linux/{time.c => time-internal.h} (64%)

>  rename sysdeps/posix/time.c => time/time-internal.h (57%)

> 

> diff --git a/sysdeps/unix/sysv/linux/powerpc/time.c

> b/sysdeps/unix/sysv/linux/powerpc/time.c index c35b80fad1..e957b81751

> 100644 --- a/sysdeps/unix/sysv/linux/powerpc/time.c

> +++ b/sysdeps/unix/sysv/linux/powerpc/time.c

> @@ -78,6 +78,6 @@ libc_hidden_def (time)

>  

>  #else

>  

> -#include <sysdeps/posix/time.c>

> +#include <time/time.c>

>  

>  #endif /* !SHARED */

> diff --git a/sysdeps/unix/sysv/linux/time.c

> b/sysdeps/unix/sysv/linux/time-internal.h similarity index 64%

> rename from sysdeps/unix/sysv/linux/time.c

> rename to sysdeps/unix/sysv/linux/time-internal.h

> index f461733678..c4c5f6b643 100644

> --- a/sysdeps/unix/sysv/linux/time.c

> +++ b/sysdeps/unix/sysv/linux/time-internal.h

> @@ -1,4 +1,5 @@

> -/* Copyright (C) 2005-2019 Free Software Foundation, Inc.

> +/* Internals of time and related functions.  Linux version.

> +   Copyright 2019 Free Software Foundation, Inc.

>     This file is part of the GNU C Library.

>  

>     The GNU C Library is free software; you can redistribute it and/or

> @@ -15,27 +16,7 @@

>     License along with the GNU C Library; if not, see

>     <https://www.gnu.org/licenses/>.  */

>  

> -#include <stddef.h>

> -#include <time.h>

> -

> -#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

> +/* Timer used on clock_gettime for time implementation.  For Linux

> +   it uses the coarse version which returns the time at the last tick

> +   and mimic what time as syscall should return.  */

> +#define TIME_CLOCK_GETTIME_CLOCKID CLOCK_REALTIME_COARSE

> diff --git a/sysdeps/posix/time.c b/time/time-internal.h

> similarity index 57%

> rename from sysdeps/posix/time.c

> rename to time/time-internal.h

> index 0ab31bd7b6..f448f64191 100644

> --- a/sysdeps/posix/time.c

> +++ b/time/time-internal.h

> @@ -1,4 +1,5 @@

> -/* Copyright (C) 1991-2019 Free Software Foundation, Inc.

> +/* Internals of time and related functions.

> +   Copyright 2019 Free Software Foundation, Inc.

>     This file is part of the GNU C Library.

>  

>     The GNU C Library is free software; you can redistribute it and/or

> @@ -15,26 +16,5 @@

>     License along with the GNU C Library; if not, see

>     <https://www.gnu.org/licenses/>.  */

>  

> -#include <stddef.h>		/* For NULL.  */

> -#include <time.h>

> -#include <sys/time.h>

> -

> -

> -/* 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)

> +/* Timer used on clock_gettime for time implementation.  */

> +#define TIME_CLOCK_GETTIME_CLOCKID CLOCK_REALTIME

> diff --git a/time/time.c b/time/time.c

> index b53a06e29c..09a2b4fc88 100644

> --- a/time/time.c

> +++ b/time/time.c

> @@ -15,19 +15,18 @@

>     License along with the GNU C Library; if not, see

>     <https://www.gnu.org/licenses/>.  */

>  

> -#include <errno.h>

>  #include <time.h>

> +#include <time-internal.h>

>  

>  /* Return the time now, and store it in *TIMER if not NULL.  */

>  time_t

>  time (time_t *timer)

>  {

> -  __set_errno (ENOSYS);

> +  struct timespec ts;

> +  __clock_gettime (TIME_CLOCK_GETTIME_CLOCKID, &ts);

>  

> -  if (timer != NULL)

> -    *timer = (time_t) -1;

> -  return (time_t) -1;

> +  if (timer)

> +    *timer = ts.tv_sec;

> +  return ts.tv_sec;

>  }

>  libc_hidden_def (time)

> -

> -stub_warning (time)


Reviewed-by: Lukasz Majewski <lukma@denx.de>



Best regards,

Lukasz Majewski

--

DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-59 Fax: (+49)-8142-66989-80 Email: lukma@denx.de
diff mbox series

Patch

diff --git a/sysdeps/unix/sysv/linux/powerpc/time.c b/sysdeps/unix/sysv/linux/powerpc/time.c
index c35b80fad1..e957b81751 100644
--- a/sysdeps/unix/sysv/linux/powerpc/time.c
+++ b/sysdeps/unix/sysv/linux/powerpc/time.c
@@ -78,6 +78,6 @@  libc_hidden_def (time)
 
 #else
 
-#include <sysdeps/posix/time.c>
+#include <time/time.c>
 
 #endif /* !SHARED */
diff --git a/sysdeps/unix/sysv/linux/time.c b/sysdeps/unix/sysv/linux/time-internal.h
similarity index 64%
rename from sysdeps/unix/sysv/linux/time.c
rename to sysdeps/unix/sysv/linux/time-internal.h
index f461733678..c4c5f6b643 100644
--- a/sysdeps/unix/sysv/linux/time.c
+++ b/sysdeps/unix/sysv/linux/time-internal.h
@@ -1,4 +1,5 @@ 
-/* Copyright (C) 2005-2019 Free Software Foundation, Inc.
+/* Internals of time and related functions.  Linux version.
+   Copyright 2019 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
 
    The GNU C Library is free software; you can redistribute it and/or
@@ -15,27 +16,7 @@ 
    License along with the GNU C Library; if not, see
    <https://www.gnu.org/licenses/>.  */
 
-#include <stddef.h>
-#include <time.h>
-
-#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
+/* Timer used on clock_gettime for time implementation.  For Linux
+   it uses the coarse version which returns the time at the last tick
+   and mimic what time as syscall should return.  */
+#define TIME_CLOCK_GETTIME_CLOCKID CLOCK_REALTIME_COARSE
diff --git a/sysdeps/posix/time.c b/time/time-internal.h
similarity index 57%
rename from sysdeps/posix/time.c
rename to time/time-internal.h
index 0ab31bd7b6..f448f64191 100644
--- a/sysdeps/posix/time.c
+++ b/time/time-internal.h
@@ -1,4 +1,5 @@ 
-/* Copyright (C) 1991-2019 Free Software Foundation, Inc.
+/* Internals of time and related functions.
+   Copyright 2019 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
 
    The GNU C Library is free software; you can redistribute it and/or
@@ -15,26 +16,5 @@ 
    License along with the GNU C Library; if not, see
    <https://www.gnu.org/licenses/>.  */
 
-#include <stddef.h>		/* For NULL.  */
-#include <time.h>
-#include <sys/time.h>
-
-
-/* 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)
+/* Timer used on clock_gettime for time implementation.  */
+#define TIME_CLOCK_GETTIME_CLOCKID CLOCK_REALTIME
diff --git a/time/time.c b/time/time.c
index b53a06e29c..09a2b4fc88 100644
--- a/time/time.c
+++ b/time/time.c
@@ -15,19 +15,18 @@ 
    License along with the GNU C Library; if not, see
    <https://www.gnu.org/licenses/>.  */
 
-#include <errno.h>
 #include <time.h>
+#include <time-internal.h>
 
 /* Return the time now, and store it in *TIMER if not NULL.  */
 time_t
 time (time_t *timer)
 {
-  __set_errno (ENOSYS);
+  struct timespec ts;
+  __clock_gettime (TIME_CLOCK_GETTIME_CLOCKID, &ts);
 
-  if (timer != NULL)
-    *timer = (time_t) -1;
-  return (time_t) -1;
+  if (timer)
+    *timer = ts.tv_sec;
+  return ts.tv_sec;
 }
 libc_hidden_def (time)
-
-stub_warning (time)