Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 23 Dec 2002 09:04:04 -0200
From:      Mario Sergio Fujikawa Ferreira <lioux@FreeBSD.org>
To:        FreeBSD-hackers@FreeBSD.org
Cc:        Garrett Wollman <wollman@lcs.mit.edu>
Subject:   Timing with clock_gettime(2) (sysutils/clockspeed port)
Message-ID:  <20021223110426.2848.qmail@exxodus.fedaykin.here>

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

--vtzGhvizbBRQ85DL
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

Hi,

	clockspeed is a port that "Uses a hardware tick counter to
compensate for deviant system clock". From pkg-descr:

"clockspeed uses a hardware tick counter to compensate for a persistently
fast or slow system clock. Given a few time measurements from a reliable
source, it computes and then eliminates the clock skew.

"sntpclock checks another system's NTP clock, and prints the results in a
format suitable for input to clockspeed. sntpclock is the simplest
available NTP/SNTP client.

"taiclock and taiclockd form an even simpler alternative to SNTP. They
are suitable for precise time synchronization over a local area network,
without the hassles and potential security problems of an NTP server.

"This version of clockspeed can use the Pentium RDTSC tick counter or the
Solaris gethrtime() nanosecond counter."

	Well, as you can see this port will not work without RDTSC
which means that it will not work in either alpha or sparc64 ARCHes. We
do not have gethrtime() but we have clock_gettime(2) as suggested
by Garret Wollman.

	I did cook a small test program to find out how would I
would use clock_gettime(2) to achieve an output similar to that of
RDTSC.

	I am using an idea taken from tar sources

((x)->t.tv_sec - (y)->t.tv_sec) + \
	((x)->t.tv_nsec - (y)->t.tv_nsec) * 4294967296.0 / 1e9)

That's where the * 4294967296.0 / 1e9 came from.

	However, my timing with clock_gettime() does not
sufficiently resembles that of RDTSC. I need a closer value. I'm
getting big differences as shown below.

	Ideas? This will help us get yet another port
under both alpha and sparc64. :) My timing test code is attached.

	Regards,

[exxodus:lioux:75]./timing  
RDTSC
Avg getpid() time = 4647397804253831496 nsec
clock_gettime()
Avg getpid() time = 4652939263933680218 nsec
[exxodus:lioux:76]./timing  
RDTSC
Avg getpid() time = 4647692649291935908 nsec
clock_gettime()
Avg getpid() time = 4652939075039020903 nsec
[exxodus:lioux:77]./timing  
RDTSC
Avg getpid() time = 4647378628771043082 nsec
clock_gettime()
Avg getpid() time = 4652939075039020903 nsec
[exxodus:lioux:78]./timing  
RDTSC
Avg getpid() time = 4647343972164535583 nsec
clock_gettime()
Avg getpid() time = 4652939263933680218 nsec
[exxodus:lioux:79]./timing  
RDTSC
Avg getpid() time = 4655770321416555069 nsec
clock_gettime()
Avg getpid() time = 4652939075039020903 nsec
[exxodus:lioux:80]./timing  
RDTSC
Avg getpid() time = 4647110875699447071 nsec
clock_gettime()
Avg getpid() time = 4652780781314515112 nsec
[exxodus:lioux:81]./timing  
RDTSC
Avg getpid() time = 4647597475565435617 nsec
clock_gettime()
Avg getpid() time = 4652939263933680218 nsec
[exxodus:lioux:82]./timing  
RDTSC
Avg getpid() time = 4647755189513323807 nsec
clock_gettime()
Avg getpid() time = 4653097557658186008 nsec
[exxodus:lioux:83]./timing  
RDTSC
Avg getpid() time = 4647490163230564680 nsec
clock_gettime()
Avg getpid() time = 4652939075039020903 nsec

-- 
Mario S F Ferreira - DF - Brazil - "I guess this is a signature."
Computer Science Undergraduate | FreeBSD Committer | CS Developer
flames to beloved devnull@someotherworldbeloworabove.org
feature, n: a documented bug | bug, n: an undocumented feature

--vtzGhvizbBRQ85DL
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="timing.c"

#include <sys/time.h>
#include <sys/types.h>

#include <stdio.h>
#include <unistd.h>

void main(void) {

	int i,iters = 100;

	{
		typedef struct { unsigned long t[2]; } timing;
#define timing_now(x) asm volatile(".byte 15;.byte 49" : "=a"((x)->t[0]),"=d"((x)->t[1]))
#define timing_diff(x,y) (((x)->t[0] - (double) (y)->t[0]) + 4294967296.0 * ((x)->t[1] - (double) (y)->t[1]))

		timing start,end;

		timing_now(&start);
		for (i = 0; i < iters; i++)
			getpid();
 
		timing_now(&end); 
		printf("RDTSC\n");
		printf("Avg getpid() time = %lld nsec\n", timing_diff(&end,&start)/iters); 
	
	}

	{
		typedef struct { struct timespec t; } timingi;
#define timing_nowi(x) ((void) clock_gettime(CLOCK_REALTIME, &((x)->t)))
#define timing_diffi(x,y) \
( \
	(double) ( ((x)->t.tv_sec - (y)->t.tv_sec) + \
		(double) ( \
				((x)->t.tv_nsec - (double) (y)->t.tv_nsec) * 4294967296.0 / 1e9 \
			) \
	) \
)

		timingi start,end;

		timing_nowi(&start);
		for (i = 0; i < iters; i++)
			getpid();
 
		timing_nowi(&end);
		printf("clock_gettime()\n");
		printf("Avg getpid() time = %lld nsec\n", timing_diffi(&end,&start)/iters); 
	
	}

}

--vtzGhvizbBRQ85DL--

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?20021223110426.2848.qmail>