The entire point of using real-time Linux is that it is Linux - all the existing Linux and POSIX services are available, and you should avoid using proprietary non-POSIX OS services if portability is an issue - otherwise you may as well be using a true RTOS.
The standard function clock()
has a CLOCKS_PER_SEC
of 1000000 (i.e. microseconds) on POSIX. The actual granularity is implementation dependent however. You can check this for your system with:
#include <time.h>
#include <stdio.h>
int main()
{
clock_t t0 = clock() ;
clock_t t1 = t0 ;
while( t0 == t1 )
{
t1 = clock() ;
}
printf( "Clock granularity = %lf seconds\n", (double)(t1 - t0)/(double)(CLOCKS_PER_SEC) ) ;
return 0 ;
}
On ideone.com, this gave the following result:
Clock granularity = 0.010000 seconds
i.e. 10ms.
If that is not sufficient, a number of clock sources are defined for use with clock_gettime()
and the resolution of each can be determined by clock_getres()
. The clock ID CLOCK_PROCESS_CPUTIME_ID
is likely to be appropriate. Note that resolution and granularity are not the same here either. I performed the following test on ideone.com:
#include <time.h>
#include <stdio.h>
int main()
{
struct timespec t0 ;
struct timespec t1 ;
clock_gettime( CLOCK_PROCESS_CPUTIME_ID, &t0) ;
t1 = t0 ;
while( t0.tv_nsec == t1.tv_nsec )
{
clock_gettime( CLOCK_PROCESS_CPUTIME_ID, &t1) ;
}
printf( "Clock granularity = %lf seconds\n", (double)(t1.tv_nsec - t0.tv_nsec)/1e9 ) ;
struct timespec res ;
clock_getres( CLOCK_PROCESS_CPUTIME_ID, &res ) ;
printf( "CPUTIME resolution = %.15lf seconds\n", e, (double)(res.tv_nsec)/1e9) ;
return 0 ;
}
With results:
Clock granularity = 0.000001 seconds
CPUTIME resolution = 0.000000001000000 seconds
So the granularity is 1 microsecond.