RTC precision?

Jon H. LaBadie jon at jonlab.UUCP
Tue Mar 5 16:25:19 AEST 1991


In article <1991Feb27.195148.4122 at sci.ccny.cuny.edu>, jeffrey at sci.ccny.cuny.edu (Jeffrey L Bromberger) writes:
> I know that in weeks past, there was a big string on how to get an
> accurate timer to deal with times less than one second.  Here's a
> trick question - does the hardware keep track of time less than one
> second in duration?  A VAX (and I know that a 3B1 is *no* vax :-)
> keeps a register that increments every microsecond.  Is there some
> sort of hidden register like that on our machine?

Though I know of none, there may be such a register; the 68000 micro-
processor mimics much of the VAX architecture.

However, there is access to a parameter that is the number of "ticks"
since system boot.  Ticks are defined in sys/param.h as "HZ" to be 
number of clock ticks per second.  Many things use HZ.  For example,
the cursor is defined to be on for 2 HZ and off for 4 HZ.  Process
accounting is logged each clock tick, not each second.

On the 3B1, HZ is defined as 60 ticks per second.

The access to the tick counter is a system call times(2).  Its
primary purpose is to report the cpu usage of a process and its
children (it fills a 4 member struct tms with this information).

However, assuming no errors, it also returns the tick count.

So, if before timing some event you save the return value of times(2),
and determine again the return value or times(2) upon completion of
the event, the difference is the number of clock ticks between start
and end of event.

NOTE, there is a considerable element of uncertainty here for small
measured times due to system call overhead and system load.  Given
the granularity of 1/60th of a second, use of clock ticks to report
times to the 1/100th (as is done by timex(1) and the ksh builtin time)
is rather nebulous.

A short program demonstrating the use of times(2) in this context
and a portion of the programs output follow.

/*     Sample program     */

#include <stdio.h>
#include <sys/types.h>
#include <sys/times.h>
#include <sys/param.h>

main()
{
	time_t start_tms, cur_tms, dif_tms;
	struct tms tms;		/* needed, but data is unused */
	double elapsed;

	start_tms = times(&tms);

	while(1) {

	    /* process til some event */

		cur_tms = times(&tms);

		dif_tms = cur_tms - start_tms;

		elapsed = dif_tms / (double) HZ;

		printf("Timer: %10ld, %10ld, %8ld, %10.2f\n",
		    start_tms, cur_tms, dif_tms, elapsed);
	}
}

/* Portion of sample output  (run while news was expiring )

Timer:   15571070,   15571279,      209,       3.48
Timer:   15571070,   15571290,      220,       3.67
Timer:   15571070,   15571290,      220,       3.67
Timer:   15571070,   15571293,      223,       3.72
Timer:   15571070,   15571293,      223,       3.72
Timer:   15571070,   15571296,      226,       3.77
Timer:   15571070,   15571297,      227,       3.78
Timer:   15571070,   15571297,      227,       3.78
Timer:   15571070,   15571298,      228,       3.80
Timer:   15571070,   15571298,      228,       3.80
Timer:   15571070,   15571302,      232,       3.87
Timer:   15571070,   15571302,      232,       3.87
Timer:   15571070,   15571303,      233,       3.88
Timer:   15571070,   15571303,      233,       3.88
Timer:   15571070,   15571304,      234,       3.90
Timer:   15571070,   15571307,      237,       3.95
Timer:   15571070,   15571307,      237,       3.95
Timer:   15571070,   15571308,      238,       3.97
Timer:   15571070,   15571308,      238,       3.97

*/

-- 
Jon LaBadie
{att, princeton, bcr, attmail!auxnj}!jonlab!jon



More information about the Comp.sys.3b1 mailing list