This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
[PATCH v2.1][BZ #12515] Improve precision of clock function
- From: Siddhesh Poyarekar <siddhesh at redhat dot com>
- To: Roland McGrath <roland at hack dot frob dot com>
- Cc: Paul Eggert <eggert at cs dot ucla dot edu>, libc-alpha at sourceware dot org
- Date: Wed, 12 Jun 2013 10:33:07 +0530
- Subject: [PATCH v2.1][BZ #12515] Improve precision of clock function
- References: <20130521145611 dot GM8927 at spoyarek dot pnq dot redhat dot com> <20130521151839 dot GA18430 at domone dot kolej dot mff dot cuni dot cz> <20130521153442 dot GO8927 at spoyarek dot pnq dot redhat dot com> <519B9A09 dot 6030305 at cs dot ucla dot edu> <20130521161441 dot GQ8927 at spoyarek dot pnq dot redhat dot com> <20130603092604 dot GL2145 at spoyarek dot pnq dot redhat dot com> <20130610083821 dot GD1570 at spoyarek dot pnq dot redhat dot com> <51B66522 dot 1060008 at cs dot ucla dot edu> <20130611135422 dot GK8010 at spoyarek dot pnq dot redhat dot com> <20130611171952 dot EB3CF2C0A2 at topped-with-meat dot com>
On Tue, Jun 11, 2013 at 10:19:52AM -0700, Roland McGrath wrote:
> > + if (__clock_gettime (CLOCK_PROCESS_CPUTIME_ID, &ts) == 0)
> > + return (ts.tv_sec * CLOCKS_PER_SEC
> > + + ts.tv_nsec / (1000000000 / CLOCKS_PER_SEC));
> > +
> > + /* clock_gettime failed. This will only occur if CLOCK_PROCESS_CPUTIME_ID is
> > + not supported by the kernel. Fall back to __times. */
>
> This is fine for now. But we already assume that CLOCK_PROCESS_CPUTIME_ID
> is actually supported in the kernel. So the fallback code here should be
> removed at some point.
Oh, I didn't notice that CLOCK_PROCESS_CPUTIME_ID has been in the
kernel since 2.6.12. I've removed the fallback code since it's not a
major change. OK to commit with this change?
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..b8450b9 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,14 @@
#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);
-
- 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);
+ /* clock_gettime won't fail here since CLOCK_PROCESS_CPUTIME_ID is
+ supported since 2.6.12. */
+ __clock_gettime (CLOCK_PROCESS_CPUTIME_ID, &ts);
+ return (ts.tv_sec * CLOCKS_PER_SEC
+ + ts.tv_nsec / (1000000000 / CLOCKS_PER_SEC));
}