This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: g77 questions


> 
> > (3) Can I get the system time down to 1/100 of a second?
> 
> That depends on resolution of the gettimeofday system call on your OS. 
> Consult the man page of gettimeofday to get the answer to this question.
>
atleast on Linux gettimeofday could do it - but even on an idle system
two consecutive calls to gettimeofday can exhibit jitter of up to 1s (for 
instance when reiserfs is balancing its trees or if external interrupts
hit between the getttimeofday calls).

here is a littl program (should work on other platforms as long as you 
don't use the x86 specific TSC reads). jitt.c executes in scheduling
policy SCHED_FIFO, so unless you have that posibility you should run
it with policy set to SCHED_OTHER.

if you let it run long enough I guess all systems will show jitter far above
1/100 of a second - so don't try to rely on a timestamp obtained by 
gettimeofday. If you realy need it you need an RTOS.

hofrat

----jitt.c mesur user-space jitter--- 
#include <sched.h>
#include <stdio.h>
#include <sys/time.h>
#include <unistd.h>
#include <stdlib.h>
#include <math.h>

__inline__ unsigned long long int hwtime_in_us( void )
    {
	unsigned long long int x;
	__asm__("rdtsc\n\t"
		"mov %%edx, %%ecx\n\t"
		:"=A" (x));
	return (x/1000);
    }

inline unsigned long long int timeofday_in_us( void )
    {
	struct timeval  tv;
	struct timezone tz;
	unsigned long long int min = 0, sec = 0, usec = 0;
	
	if ( gettimeofday( &tv, &tz ) != -1 )
		{
		min  = tz.tz_minuteswest;
		sec  = tv.tv_sec;
		usec = tv.tv_usec;
		}
	
	return (unsigned long long int)( ( ((min)*60) + (sec) )*1000000 + (usec) );
    }

// on x86 you can use the TSC on otheres use timeofday - hofrat
//unsigned long long int (*time_in_us)(void) = hwtime_in_us;
unsigned long long int (*time_in_us)(void) = timeofday_in_us;

#define C 1000  /* count of loops */
#define b 2     /* base for logarithmic jitter */
#define N 1024  /* number of points in distribution */
#define S 1    /* sleep time in usecs */

int main(void)
    {
    float a;
    unsigned int i;
    unsigned long long int count=0;
    unsigned long long int time1, time2;
	unsigned int jitter=0, jitter_max=0, jitter_min=10000;
    unsigned int distribution[N];
    
    struct sched_param p;
    int error;

/*
    error = sched_getparam( 0, &p );
    if ( error < 0 )
        {
        printf("count not get sched_param\n");
        exit(1);
        }
    p.sched_priority = sched_get_priority_max( SCHED_FIFO );

    error = sched_setscheduler( 0, SCHED_FIFO, &p );
    if ( error < 0 )
        {
        printf("count not set priority SCHED_FIFO\n");
        exit(1);
        }
*/

    for ( i=0; i<N; i++ ) distribution[i] = 0;

    time1 = time_in_us();
	while(count<C)
        {
        time1  = time_in_us();
        time2  = time_in_us();
        jitter = (unsigned int)( time2 - time1 );

        if ( jitter > 0 )
            i = (int)(log((float)(jitter))/log((float)(b)))+1;
        else
            i = 0;
        if (i>=N) i = N-1;
        distribution[i]++;
 
        if ( jitter > jitter_max ) jitter_max = jitter;
		if ( jitter < jitter_min ) jitter_min = jitter;
 
        printf( "jitter = %d\n", jitter);


        for ( i=0;i<100;i++ )
           a = sqrt(4.0);
            
        count++;
        usleep(S);
        }
                
        for ( i=0; i<N; i++ )
            {
            if ( distribution[i] > 0 )
                printf( "jitter less than %8.0f us = %d\n", 
                    pow((float)(b),(float)(i)), distribution[i] );
            }
        printf( "jitter worst case  = %8u us\n", jitter_max );
	
    return 0;
    }


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]