This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
[ping][PATCH v2][BZ #12515] Improve precision of clock function
- From: Siddhesh Poyarekar <siddhesh at redhat dot com>
- To: Paul Eggert <eggert at cs dot ucla dot edu>
- Cc: libc-alpha at sourceware dot org
- Date: Mon, 3 Jun 2013 14:56:04 +0530
- Subject: [ping][PATCH v2][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>
Ping!
On Tue, May 21, 2013 at 09:44:41PM +0530, Siddhesh Poyarekar wrote:
> On Tue, May 21, 2013 at 09:00:09AM -0700, Paul Eggert wrote:
> > On 05/21/13 08:34, Siddhesh Poyarekar wrote:
> >
> > > + return (clock_t) ((ts.tv_nsec * CLOCKS_PER_SEC) / 1000000000
> > > + + ts.tv_sec * CLOCKS_PER_SEC);
> >
> > First, there's no need to cast to clock_t, and in general it's better
> > to avoid casts if they're not needed.
>
> OK, cast removed.
>
> > Second, ts.tv_nsec * CLOCKS_PER_SEC might overflow on a machine
> > with 32-bit 'long', and afterwards the resulting division by 1000000000
> > won't wrap around to the "correct" value.
> >
> > How about this instead?
> >
> > return (ts.tv_sec * CLOCKS_PER_SEC
> > + ts.tv_nsec / (1000000000 / CLOCKS_PER_SEC));
>
> Thanks, patch updated and tested. OK to go in with this change?
> After the PLT fix is reviewed of course.
>
> Siddhesh
>
> 2013-05-21 Siddhesh Poyarekar <siddhesh@redhat.com>
> Paul Eggert <eggert@cs.ucla.edu>
>
> [BZ #12515]
> * sysdeps/unix/sysv/linux/clock.c (clock): Use result from
> CLOCK_PROCESS_CPUTIME_ID clock if available.
>
>
> diff --git a/sysdeps/unix/sysv/linux/clock.c b/sysdeps/unix/sysv/linux/clock.c
> index 5268d33..e4e2065 100644
> --- a/sysdeps/unix/sysv/linux/clock.c
> +++ b/sysdeps/unix/sysv/linux/clock.c
> @@ -23,6 +23,14 @@
> clock_t
> clock (void)
> {
> + struct timespec ts;
> +
> + 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. */
> struct tms buf;
> long clk_tck = __sysconf (_SC_CLK_TCK);
>