From robert@cyrus.watson.org Sun Oct 10 17:50:40 1999
Return-Path: <robert@cyrus.watson.org>
Received: from fledge.watson.org (fledge.watson.org [204.156.12.50])
	by hub.freebsd.org (Postfix) with ESMTP id DE20E14ECA
	for <FreeBSD-gnats-submit@freebsd.org>; Sun, 10 Oct 1999 17:50:39 -0700 (PDT)
	(envelope-from robert@cyrus.watson.org)
Received: from fledge.watson.org (robert@fledge.pr.watson.org [192.0.2.3])
	by fledge.watson.org (8.9.3/8.9.3) with SMTP id UAA07537
	for <FreeBSD-gnats-submit@freebsd.org>; Sun, 10 Oct 1999 20:50:38 -0400 (EDT)
	(envelope-from robert@cyrus.watson.org)
Message-Id: <Pine.BSF.3.96.991010204912.7447B-100000@fledge.watson.org>
Date: Sun, 10 Oct 1999 20:50:38 -0400 (EDT)
From: Robert Watson <robert@cyrus.watson.org>
Reply-To: Robert Watson <robert+freebsd@cyrus.watson.org>
To: FreeBSD-gnats-submit@freebsd.org
Subject: [Fwd: clock(3) runs backwards! (fwd)] (fwd)

>Number:         14254
>Category:       misc
>Synopsis:       [Fwd: clock(3) runs backwards! (fwd)] (fwd)
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    freebsd-bugs
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Sun Oct 10 18:00:01 PDT 1999
>Closed-Date:    Fri Jul 20 19:05:23 PDT 2001
>Last-Modified:  Fri Jul 20 19:06:01 PDT 2001
>Originator:     
>Release:        
>Organization:
>Environment:
>Description:
 Archie Cobbs recommended sending this in as a pr also.
 
 FreeBSD fledge.watson.org 3.3-19990909-RC FreeBSD 3.3-19990909-RC #0: Tue
 Sep 14 19:44:30 EDT 1999
 root@fledge.watson.org:/data/fbsd-stable/src/sys/compile/FLEDGE  i386
 
 (actually cvsup'd to -RELEASE, I believe).
 
   Robert N M Watson 
 
 robert@fledge.watson.org              http://www.watson.org/~robert/
 PGP key fingerprint: AF B5 5F FF A6 4A 79 37  ED 5F 55 E9 58 04 6A B1
 TIS Labs at Network Associates, Safeport Network Services
 
 ---------- Forwarded message ----------
 Date: Sun, 10 Oct 1999 12:41:55 -0400 (EDT)
 From: Robert Watson <robert@cyrus.watson.org>
 Reply-To: Robert Watson <robert+freebsd@cyrus.watson.org>
 To: hackers@freebsd.org
 Subject: [Fwd: clock(3) runs backwards! (fwd)] (fwd)
 
 
 This was a BSDI mailing list post, but I gave it a try on my 3.3-RELEASE
 FreeBSD machine and got the following:
 
 fledge:/tmp> ./clocktest 
  1.000000 - 0.000000 = 1.000000
  2.000000 - 1.000000 = 1.000000
  1.000000 - 2.000000 = 4294967295.000000
  3.000000 - 1.000000 = 2.000000
  2.000000 - 3.000000 = 4294967295.000000
  4.000000 - 2.000000 = 2.000000
  3.000000 - 4.000000 = 4294967295.000000
  5.000000 - 3.000000 = 2.000000
  4.000000 - 5.000000 = 4294967295.000000
  6.000000 - 4.000000 = 2.000000
  5.000000 - 6.000000 = 4294967295.000000
  7.000000 - 5.000000 = 2.000000
  6.000000 - 7.000000 = 4294967295.000000
  8.000000 - 6.000000 = 2.000000
  7.000000 - 8.000000 = 4294967295.000000
  9.000000 - 7.000000 = 2.000000
  10.000000 - 9.000000 = 1.000000
  9.000000 - 10.000000 = 4294967295.000000
  10.000000 - 11.000000 = 4294967295.000000
  12.000000 - 10.000000 = 2.000000
 
 Seems like I'm seeing the behavior they describe--whether that's bad is
 another question, but seemed interesting.  Any suggestions?
 
   Robert N M Watson 
 
 robert@fledge.watson.org              http://www.watson.org/~robert/
 PGP key fingerprint: AF B5 5F FF A6 4A 79 37  ED 5F 55 E9 58 04 6A B1
 TIS Labs at Network Associates, Safeport Network Services
 
 ---------- Forwarded message ----------
 Date: Sun, 10 Oct 1999 12:33:16 -0400
 From: support@safeport.com
 To: Robert Watson <robert@cyrus.watson.org>
 Subject: [Fwd: clock(3) runs backwards! (fwd)]
 
 Did you see this? If its not on FreeBSD, and you are not archiving the BSDI
 list, I can send you the thread. It turns our this is not a silly programming
 error, this guy wrote the current C ANSI standard.
 
 -------- Original Message --------
 Subject: clock(3) runs backwards! (fwd)
 Date: Thu, 7 Oct 1999 14:53:48 -0400 (EDT)
 From: larry.jones@sdrc.com (Larry Jones)
 To: bsdi-users@mailinglists.org
 
 FYI.  Also sent to problems@bsdi.com.
 
 > Description:
 > 	Occasionally, clock(3) will return a value which is less than
 > 	the value returned by a previous call.
 > 
 > Release:
 > 	4.0.1
 > 
 > Repeat-By:
 > 	Run the following test program; a single run is usually sufficient
 > 	to observe the problem.
 > 
 > #include <stdio.h>
 > #include <stdlib.h>
 > #include <time.h>
 > 
 > int main()
 > {
 > 	int i;
 > 	clock_t t0, t1;
 > 
 > 	for (i = 0; i < 20; i++) {
 > 		t0 = clock();
 > 		do {
 > 			t1 = clock();
 > 		} while(t0 == t1);
 > 		printf( " %f - %f = %f\n", (double)t1, (double)t0,
 > 			(double)(t1-t0));
 > 	}
 > 	return 0;
 > }
 > 
 > Fix:
 > 	Converting the user and system time to clock ticks before adding
 > 	them together causes the problem due to roundoff error and the
 > 	system time not being monotonically increasing.  Assuming that
 > 	the actuall sum of user and system time *is* monotonically
 > 	increasing (which it seems to be), adding them together before
 > 	converting will solve the problem:
 > 
 > --- /cdrom/lib/libc/gen/clock.c	Thu Feb  2 18:20:13 1995
 > +++ ./clock.c	Wed Oct  6 13:14:20 1999
 > @@ -39,12 +39,6 @@
 >  #include <sys/time.h>
 >  #include <sys/resource.h>
 >  
 > -/*
 > - * Convert usec to clock ticks; could do (usec * CLK_TCK) / 1000000,
 > - * but this would overflow if we switch to nanosec.
 > - */
 > -#define	CONVTCK(r)	(r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
 > -
 >  clock_t
 >  clock()
 >  {
 > @@ -52,5 +46,7 @@
 >  
 >  	if (getrusage(RUSAGE_SELF, &ru))
 >  		return ((clock_t) -1);
 > -	return((clock_t)((CONVTCK(ru.ru_utime) + CONVTCK(ru.ru_stime))));
 > +	return((clock_t)((ru.ru_utime.tv_sec + ru.ru_stime.tv_sec) * CLK_TCK +
 > +		(ru.ru_utime.tv_usec + ru.ru_stime.tv_usec) /
 > +			(1000000 / CLK_TCK)));
 >  }
 
 -Larry Jones
 
 Whatever it is, it's driving me crazy! -- Calvin
 
 ---------------------------------------------------------------------
 To unsubscribe, e-mail: bsdi-users-unsubscribe@mailinglists.org
 For additional commands, e-mail: bsdi-users-help@mailinglists.org
 
 
 
>How-To-Repeat:
>Fix:
>Release-Note:
>Audit-Trail:
Responsible-Changed-From-To: gnats-admin->freebsd-bugs 
Responsible-Changed-By: steve 
Responsible-Changed-When: Sun Nov 7 13:36:29 PST 1999 
Responsible-Changed-Why:  
Misfiled PR. 
State-Changed-From-To: open->closed 
State-Changed-By: mike 
State-Changed-When: Fri Jul 20 19:05:23 PDT 2001 
State-Changed-Why:  

This PR is a duplicate of PR: kern/12305 

http://www.FreeBSD.org/cgi/query-pr.cgi?pr=14254 
>Unformatted:
