Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 10 Oct 1999 12:41:55 -0400 (EDT)
From:      Robert Watson <robert@cyrus.watson.org>
To:        hackers@freebsd.org
Subject:   [Fwd: clock(3) runs backwards! (fwd)] (fwd)
Message-ID:  <Pine.BSF.3.96.991010124008.5692A-100000@fledge.watson.org>

next in thread | raw e-mail | index | archive | help

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



To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-hackers" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?Pine.BSF.3.96.991010124008.5692A-100000>