public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: quade <quade@hsnr.de>
To: linux-kernel@vger.kernel.org
Cc: quade@hsnr.de
Subject: latency error (~2ms) with nanosleep
Date: Mon, 13 Jun 2005 15:30:47 +0200	[thread overview]
Message-ID: <20050613133047.GA11979@hsnr.de> (raw)

[-- Attachment #1: Type: text/plain, Size: 641 bytes --]


Playing around with the (simple) measurement of latency-times
I noticed, that the systemcall "nanosleep" has always a minimal
latency from about ~2ms (haven't run it all night, so...). It
seems to be a systematical error.

A short investigation shows, that "sys_nanosleep()" uses
schedule_timeout(), but schedule_timeout() is working exactly
as expected. Therefore I think it has something to do with
the scheduling?

Has someone an explanation for the ~2ms error?
If it is indeed a systematical error, does it make sense to
"adjust" (correct) this error in the systemcall "sys_nanosleep()"?

Find attached my small test program.

Juergen.

[-- Attachment #2: nano.c --]
[-- Type: text/x-csrc, Size: 1633 bytes --]

#include <stdio.h>
#include <time.h>
#include <sched.h>
#include <sys/time.h>

#define TAKTFREQ  600

static inline unsigned long long int rdtsc()
{
	unsigned long long int x;
	__asm__ volatile (".byte 0x0f, 0x31" : "=A" (x));
	return x;
}

// XXX - I know, there can be an overrun ...
static inline unsigned long time_in_usec( struct timeval *tv )
{
	return (tv->tv_sec*1000000)+tv->tv_usec;
}

int main( int argc, char **argv )
{
	int i;
	struct timespec delay;
	struct timeval tvstart, tvend;
	unsigned long start, end, timediff, maxdiff, shouldbetime;
	unsigned long mindiff;
#if 1
	struct sched_param SchedulingParameter;

	SchedulingParameter.sched_priority = 50;
	if( sched_setscheduler( 0, SCHED_RR, &SchedulingParameter )!= 0 ) {
		perror( "Set Scheduling Priority" );
		return -1;
	}
#endif

	for( shouldbetime=1000; shouldbetime<51000;shouldbetime+=1000 ) {
		mindiff = 0xffffffff;
		maxdiff = 0;
		for( i=0; i<30; i++ ) {
			delay.tv_sec  = 0;
			delay.tv_nsec = shouldbetime*1000; // in nsec
			gettimeofday(&tvstart,NULL);
			start = rdtsc();
			nanosleep( &delay, NULL );
			end = rdtsc();
			gettimeofday(&tvend,NULL);
#if 0
			printf("timdiff: %ld - %ld\n",
				(end-start)/TAKTFREQ,  // 600 MHz - in usec
				time_in_usec(&tvend)-time_in_usec(&tvstart));
#endif
			timediff = time_in_usec(&tvend)-time_in_usec(&tvstart);
			timediff -= shouldbetime;
			if( (timediff > maxdiff)&&i>0 )
				maxdiff = timediff;
			if( (timediff < mindiff)&&i>0 )
				mindiff = timediff;
		}
		//printf("%7.1ld-> max diff: %ld\n", shouldbetime, maxdiff );
		printf("%7.1ld %ld %ld\n", shouldbetime, maxdiff, mindiff );
	}
	return 0;
}

             reply	other threads:[~2005-06-13 13:31 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-06-13 13:30 quade [this message]
2005-06-13 14:21 ` latency error (~2ms) with nanosleep Richard B. Johnson
2005-06-13 14:27   ` Eric Piel
2005-06-13 16:48 ` Chris Friesen
2005-06-13 16:54   ` Nish Aravamudan
2005-06-14  8:57     ` quade

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20050613133047.GA11979@hsnr.de \
    --to=quade@hsnr.de \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox