This is the mail archive of the libc-alpha@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH v2.2][BZ #12515] Improve precision of clock function


On Wed, Jun 12, 2013 at 10:38:19AM -0700, Roland McGrath wrote:
> Removing the fallback is OK, but ignoring the result of
> clock_gettime is not.

I reckoned it ought to be OK since the only way to get an unintended
error was through a kernel bug.  However I guess we ought to cover
that too.  Here's an updated patch that checks the return value and
returns (clock_t) -1 if clock_gettime fails.

Siddhesh

	[BZ #12515]
	* sysdeps/unix/sysv/linux/clock.c (clock): Use result from
	CLOCK_PROCESS_CPUTIME_ID clock instead of __times.

diff --git a/sysdeps/unix/sysv/linux/clock.c b/sysdeps/unix/sysv/linux/clock.c
index 5268d33..25c3b38 100644
--- a/sysdeps/unix/sysv/linux/clock.c
+++ b/sysdeps/unix/sysv/linux/clock.c
@@ -1,4 +1,5 @@
-/* Copyright (C) 1991-2013 Free Software Foundation, Inc.
+/* Return the time used by the program so far (user time + system time).
+   Copyright (C) 1991-2013 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
@@ -19,26 +20,17 @@
 #include <time.h>
 #include <unistd.h>
 
-/* Return the time used by the program so far (user time + system time).  */
 clock_t
 clock (void)
 {
-  struct tms buf;
-  long clk_tck = __sysconf (_SC_CLK_TCK);
+  struct timespec ts;
 
-  /* We don't check for errors here.  The only error the kernel
-     returns is EFAULT if the value cannot be written to the struct we
-     pass a pointer to.  Otherwise the kernel returns an `unsigned
-     long' value which is the number of jiffies since system start.
-     But this number can be negative (when read as `long') when the
-     system is up for some time.  Ignoring errors should therefore
-     have no negative impacts but solve the problem.  */
-  __times (&buf);
+  /* clock_gettime shouldn't fail here since CLOCK_PROCESS_CPUTIME_ID is
+     supported since 2.6.12.  Check the return value anyway in case the kernel
+     barfs on us for some reason.  */
+  if (__glibc_unlikely (__clock_gettime (CLOCK_PROCESS_CPUTIME_ID, &ts) != 0))
+    return (clock_t) -1;
 
-  return
-    (clk_tck <= CLOCKS_PER_SEC)
-    ? ((unsigned long) buf.tms_utime + buf.tms_stime) * (CLOCKS_PER_SEC
-							 / clk_tck)
-    : ((unsigned long) buf.tms_utime + buf.tms_stime) / (clk_tck
-							 / CLOCKS_PER_SEC);
+  return (ts.tv_sec * CLOCKS_PER_SEC
+	  + ts.tv_nsec / (1000000000 / CLOCKS_PER_SEC));
 }


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]