c - Create a free running timer in real time linux -
i studying real time linux , want create free running timer. not able find information regarding real time linux. suggests me document or tell me how create free running high precision timer ??
achievement : after creating free running timer, can time stamp value.
the entire point of using real-time linux is linux - existing linux , posix services available, , should avoid using proprietary non-posix os services if portability issue - otherwise may using true rtos.
the standard function clock() has clocks_per_sec of 1000000 (i.e. microseconds) on posix. actual granularity implementation dependent however. can check 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, gave following result:
clock granularity = 0.010000 seconds i.e. 10ms.
if not sufficient, number of clock sources defined use clock_gettime() , resolution of each can determined clock_getres(). clock id clock_process_cputime_id appropriate. note resolution , granularity not same here either. performed 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 granularity 1 microsecond.
Comments
Post a Comment