Skip site navigation (1)Skip section navigation (2)
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:   misc/1998: clock() wraps after only 548775 ticks
Message-ID:  <199611130756.IAA05900@diamant.noc.dfn.de>
Resent-Message-ID: <199611130800.AAA01151@freefall.freebsd.org>

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

>Number:         1998
>Category:       misc
>Synopsis:       clock() wraps after only 548775 ticks
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    freebsd-bugs
>State:          open
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Wed Nov 13 00:00:00 PST 1996
>Last-Modified:
>Originator:     schweikh@noc.dfn.de
>Organization:
DFN Network Operation Center
>Release:        FreeBSD 2.1-STABLE i386
>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:
>Audit-Trail:
>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





Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199611130756.IAA05900>