[Developers] Proposed Cactus Timer API Completion (granularity)

Steve White swhite at aei.mpg.de
Wed Aug 4 10:38:25 CDT 2004


Concerning granularity:

This is a useful quantity that tells you the smallest time difference that
is reported by a timer.  It keeps you from saying things like "zero time
was spent in a function" when really 10ms were spent, and from saying "one
routine took twice as long as another" when really it only took one clock
cycle more.

I haven't found a nice API that reports this for either gettimeofday or
getrusage, although I've seen other timers that do report things like
granularity and assured accuracy. (Accuracy denotes how well the reported
duration agrees with "real time", whatever that means in the context.)

There are several things that might affect the granularity.  One is that
an underlying counter might only hold so many digits--this imposes an
absolute upper bound on granularity.  Another is the update rate: a
counter that is capable of representing, say, microseconds, might only be
updated every 10 microseconds--this sort of thing affects granularity on a
system-by-system basis.

One could write a sort of brain-dead code to produce a number that is
greater than or equal to the granularity of a timer.  I would like 
something better though.

Attached find a couple of simple programs demonstrating granularity. On my
laptop, the value for gettimeofday wavers between 2 and 3 microseconds,
but sometimes can be much larger.  The getrusage command however seems to
have a resolution of one millisecond:  it always rolls over after about
1000 microseconds.

------------------------------------------------------------------------
Steve White : Programmer
Max-Planck-Institut für Gravitationsphysik      Albert-Einstein-Institut
Am Mühlenberg 1, D-14476 Golm, Germany                  +49-331-567-7329

On Tue, 3 Aug 2004, Gabrielle Allen wrote:

> 
> 
> I just looked at the bug reports for timers, I think I am missing the 
> point about units and granularity?
> 
-------------- next part --------------
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
#include <sys/resource.h>

int
main()
{
	int i;
	struct rusage ru_start, ru_end;
	getrusage( RUSAGE_SELF, &ru_start );
	for( i = 0; i < 100000; i++ )
	{
		getrusage( RUSAGE_SELF, &ru_end );
		if( ru_end.ru_utime.tv_usec > ru_start.ru_utime.tv_usec )
		{
			printf( "%ld usec; %d iterations\n",
				ru_end.ru_utime.tv_usec
			       	- ru_start.ru_utime.tv_usec, i );
			break;
		}
	}
	return 0;
}
-------------- next part --------------
#include <stdio.h>
#include <sys/time.h>
#include <time.h>

int
main()
{
	int i;
	struct timeval start, end;

	printf( "gettimeofday\n" );
	for( i = 0; i < 30; i++ )
	{
		gettimeofday( &start, NULL );
		gettimeofday( &end, NULL );
		printf( "%ld usec\n", end.tv_usec - start.tv_usec );
	}
	return 0;
}


More information about the Developers mailing list