Bug 14254

Summary: [Fwd: clock(3) runs backwards! (fwd)] (fwd)
Product: Base System Reporter: Robert Watson <robert>
Component: miscAssignee: freebsd-bugs (Nobody) <bugs>
Status: Closed FIXED    
Severity: Affects Only Me    
Priority: Normal    
Version: 1.0-RELEASE   
Hardware: Any   
OS: Any   

Description Robert Watson 1999-10-11 02:00:01 UTC
 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
Comment 1 Steve Price freebsd_committer freebsd_triage 1999-11-07 21:36:29 UTC
Responsible Changed
From-To: gnats-admin->freebsd-bugs

Misfiled PR. 
Comment 2 Mike Barcroft freebsd_committer freebsd_triage 2001-07-21 03:05:23 UTC
State Changed
From-To: open->closed


This PR is a duplicate of PR: kern/12305