From Schweikhardt@RUS.Uni-Stuttgart.DE  Tue Nov 12 23:57:06 1996
Received: from noc.belwue.de (root@noc.BelWue.DE [129.143.2.1])
          by freefall.freebsd.org (8.7.5/8.7.3) with ESMTP id XAA00956
          for <FreeBSD-gnats-submit@freebsd.org>; Tue, 12 Nov 1996 23:57:04 -0800 (PST)
Received: from rubin.noc.dfn.de (rubin.noc.dfn.de [193.174.247.201]) by noc.belwue.de with SMTP id IAA10238
  (8.6.13/IDA-1.6 for <FreeBSD-gnats-submit@freebsd.org>); Wed, 13 Nov 1996 08:56:53 +0100
Received: from diamant.noc.dfn.de by rubin.noc.dfn.de (4.1/BelWue-2.0SUN)
	id AA02189; Wed, 13 Nov 96 08:56:53 +0100
Received: by diamant.noc.dfn.de (SMI-8.6/SVR4/BelWue-1.0.3)
	id IAA05900; Wed, 13 Nov 1996 08:56:10 +0100
Message-Id: <199611130756.IAA05900@diamant.noc.dfn.de>
Date: Wed, 13 Nov 1996 08:56:09 +0100 (MET)
From: Schweikhardt@RUS.Uni-Stuttgart.DE (Jens Schweikhardt)
To: FreeBSD-gnats-submit@freebsd.org
Cc: schweikh@diamant.noc.dfn.de (Jens Schweikhardt)
Subject: clock() wraps after only 548775 ticks

>Number:         1998
>Category:       misc
>Synopsis:       clock() wraps after only 548775 ticks
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    freebsd-bugs
>State:          closed
>Quarter:
>Keywords:
>Date-Required:
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Wed Nov 13 00:00:00 PST 1996
>Closed-Date:    Sun Nov 17 12:16:51 PST 1996
>Last-Modified:  Sun Nov 17 12:20:56 PST 1996
>Originator:     schweikh@noc.dfn.de
>Release:        FreeBSD 2.1-STABLE i386
>Organization:
DFN Network Operation Center
>Environment:

	i486 DX2/66

>Description:

	The value returned by clock() wraps after about 548775 ticks
	(4288 CPU seconds = 1 hour and then some).
	This is the behaviour I would expect if CLOCKS_PER_SEC were 1000000,
	but it's only 128 according to <time.h>. So FreeBSD needlessly wastes
	a great potential of accurate resource usage measurement. This
	surely seems realted to the kernel's inability to measure resource
	usages of more than this amount of user and system time (with getrusage()).
	If this is the intended behaviour and will not be changed then please
	add a note in the clock(3) and getrusage(2) man pages about the
	premature wrap... (yuck, FreeBSD :-)

>How-To-Repeat:

	Compile and run this:

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/time.h>
#include <sys/resource.h>

static time_t start;
static struct rusage rus;
static void alarm_handler (int);

	static void
alarm_handler (int sig)
{
	(void) getrusage (RUSAGE_SELF, &rus);
	(void) printf ("%lu %lu %6.0f\n",
		 (unsigned long) (time (NULL) - start),
		 (unsigned long) clock (),
		 (double) (rus.ru_utime.tv_sec  + rus.ru_stime.tv_sec +
			      (rus.ru_utime.tv_usec + rus.ru_stime.tv_usec)/1.0E6));
	(void) fflush (NULL);
}

	int
main (void)
{
	struct sigaction sa = { 0 };
	struct itimerval alarm_time = {{1, 0}, {1, 0}};

	sa.sa_handler = alarm_handler;
	(void) sigemptyset (&sa.sa_mask);
	(void) sigaddset (&sa.sa_mask, SIGVTALRM);
	if (sigaction (SIGVTALRM, &sa, NULL) == -1) {
		perror ("sigaction");
		return EXIT_FAILURE;
	}
	if (setitimer (ITIMER_VIRTUAL, &alarm_time, NULL) == -1) {
		perror ("setitimer");
		return EXIT_FAILURE;
	}
	start = time (NULL);
	for (;;)
		;
	return EXIT_SUCCESS;
}

Run it for 4300 (CPU) seconds.
The output will look like this:
1 133      1
3 260      2
4 384      3
5 511      4
6 635      5
7 751      6
8 869      7
...
4487 548007   4281
4488 548127   4282
4490 548254   4283
4491 548375   4284
4492 548494   4285
4493 548618   4286
4494 121      1
4496 248      2
4497 379      3
4498 504      4
4499 630      5
4500 756      6


>Fix:
>Release-Note:
>Audit-Trail:

From: Bruce Evans <bde@zeta.org.au>
To: FreeBSD-gnats-submit@FreeBSD.org, Schweikhardt@RUS.Uni-Stuttgart.DE
Cc: schweikh@diamant.noc.dfn.de
Subject: Re: misc/1998: clock() wraps after only 548775 ticks
Date: Wed, 13 Nov 1996 20:38:41 +1100

 >>Synopsis:       clock() wraps after only 548775 ticks
 
 This was fixed 13 months ago in -current and 4 months ago in -stable.
 It was reported in PR 788.
 
 >	The value returned by clock() wraps after about 548775 ticks
 >	(4288 CPU seconds = 1 hour and then some).
 >	This is the behaviour I would expect if CLOCKS_PER_SEC were 1000000,
 >	but it's only 128 according to <time.h>. So FreeBSD needlessly wastes
 >	a great potential of accurate resource usage measurement. This
 
 Using clock() needlessly wastes a factor of 1000000/128 of precision.  This
 can't be fixed until clock_t is larger so that CLOCKS_PER_SECOND can be 10^6
 or more.
 
 Bruce
State-Changed-From-To: open->closed 
State-Changed-By: bde 
State-Changed-When: Sun Nov 17 12:16:51 PST 1996 
State-Changed-Why:  
Essentially a duplicate of #788. 
Fixed in rev.1.13-1.14 on 1995/10/21-23 in -current. 
Fixed in rev.1.12.4.1 on 1996/07/02 in -stable. 
>Unformatted:
To: FreeBSD-gnats-submit@freebsd.org
Subject: clock() wraps after only 548775 ticks
From: schweikh@noc.dfn.de
Reply-To: schweikh@noc.dfn.de
X-send-pr-version: 3.2


