public inbox for newlib@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] RISC-V: Reliably initialize t0 in _times()
@ 2021-08-02 16:46 Christoph Muellner
  2021-08-04  9:44 ` Corinna Vinschen
  0 siblings, 1 reply; 2+ messages in thread
From: Christoph Muellner @ 2021-08-02 16:46 UTC (permalink / raw)
  To: newlib; +Cc: Kito Cheng, Christoph Muellner, Christoph Muellner

From: Christoph Muellner <cmuellner@linux.com>

The current implementation does not reliably initialize t0 once.
Additionally the initialization requires two calls to _gettimeofday().
Let's sacrifice a byte to keep the initialization status
and reduce the maximum number of calls to _gettimeofday().

This has caused issues in an application that invokes clock().
The problematic situation is as follows:

1) The program calls clock() which calls _times().
2) _gettimeofday(&t0, 0) puts 0 in t0.tv_usec (because less than 1 us has
   elapsed since the beginning of time).
3) _gettimeofday(&t, 0) puts 1 in t.tv_usec (since now more than 1 us has
   elapsed since the beginning of time).
4) That call to clock() returns 1 (the value from step 3 minus the value in
   step 2).
5) The program does a second call to clock().
6) The code above still sees 0 in t0 so it tries to update t0 again and
   _gettimeofday(&t0, 0) puts 1 in t0.tv_usec.
7) The _gettimeofday(&t, 0) puts 1 in t.tv_usec (since less than 1us has
   elapsed since step 3).
8) clock() returns 0 (step 7 minus step 6) and indicates that time is
   moving backwards.

Signed-off-by: Christoph Muellner <cmuellner@gcc.gnu.org>
---
 libgloss/riscv/sys_times.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/libgloss/riscv/sys_times.c b/libgloss/riscv/sys_times.c
index fc8133ade..a029bcfb5 100644
--- a/libgloss/riscv/sys_times.c
+++ b/libgloss/riscv/sys_times.c
@@ -21,14 +21,19 @@ extern int _gettimeofday(struct timeval *, void *);
 clock_t
 _times(struct tms *buf)
 {
-  // when called for the first time, initialize t0
+  static char initialized;
   static struct timeval t0;
-  if (t0.tv_sec == 0 && t0.tv_usec == 0)
-    _gettimeofday (&t0, 0);
-
   struct timeval t;
+
   _gettimeofday (&t, 0);
 
+  // when called for the first time, initialize t0
+  if (!initialized) {
+    t0.tv_sec = t.tv_sec;
+    t0.tv_usec = t.tv_usec;
+    initialized = 1;
+  }
+
   long long utime = (t.tv_sec - t0.tv_sec) * 1000000 + (t.tv_usec - t0.tv_usec);
   buf->tms_utime = utime * CLOCKS_PER_SEC / 1000000;
   buf->tms_stime = buf->tms_cstime = buf->tms_cutime = 0;
-- 
2.31.1


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] RISC-V: Reliably initialize t0 in _times()
  2021-08-02 16:46 [PATCH] RISC-V: Reliably initialize t0 in _times() Christoph Muellner
@ 2021-08-04  9:44 ` Corinna Vinschen
  0 siblings, 0 replies; 2+ messages in thread
From: Corinna Vinschen @ 2021-08-04  9:44 UTC (permalink / raw)
  To: Christoph Muellner; +Cc: newlib, Christoph Muellner, Kito Cheng

On Aug  2 18:46, Christoph Muellner wrote:
> From: Christoph Muellner <cmuellner@linux.com>
> 
> The current implementation does not reliably initialize t0 once.
> Additionally the initialization requires two calls to _gettimeofday().
> Let's sacrifice a byte to keep the initialization status
> and reduce the maximum number of calls to _gettimeofday().
> 
> This has caused issues in an application that invokes clock().
> The problematic situation is as follows:
> 
> 1) The program calls clock() which calls _times().
> 2) _gettimeofday(&t0, 0) puts 0 in t0.tv_usec (because less than 1 us has
>    elapsed since the beginning of time).
> 3) _gettimeofday(&t, 0) puts 1 in t.tv_usec (since now more than 1 us has
>    elapsed since the beginning of time).
> 4) That call to clock() returns 1 (the value from step 3 minus the value in
>    step 2).
> 5) The program does a second call to clock().
> 6) The code above still sees 0 in t0 so it tries to update t0 again and
>    _gettimeofday(&t0, 0) puts 1 in t0.tv_usec.
> 7) The _gettimeofday(&t, 0) puts 1 in t.tv_usec (since less than 1us has
>    elapsed since step 3).
> 8) clock() returns 0 (step 7 minus step 6) and indicates that time is
>    moving backwards.
> 
> Signed-off-by: Christoph Muellner <cmuellner@gcc.gnu.org>
> ---
>  libgloss/riscv/sys_times.c | 13 +++++++++----
>  1 file changed, 9 insertions(+), 4 deletions(-)

Pushed.


Thanks,
Corinna


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-08-04  9:45 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-02 16:46 [PATCH] RISC-V: Reliably initialize t0 in _times() Christoph Muellner
2021-08-04  9:44 ` Corinna Vinschen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).