Hi, I'm currently developing a time benchmark application where I want to measure a real time thread latency. My application is based on Cyclictest from the rt-tests suit. > https://wiki.linuxfoundation.org/realtime/documentation/howto/tools/rt-tests I programmed in such way that it would be equivalent to Cyclict test running with the following command line: > cyclictest -m -n -N --threads=1 --interval=10000 --priority=80 --distance=0 --loops=1000 --clock=1 At least that's what I expected, but my latencies are much bigger than Cyclictest ones. >From cyclictest I get an average in my computer of something between 2300 and 2500 nanoseconds. However, in my application I'm having something between 47000 and 55000 nanoseconds. As you can see it is much higher! I'm not sure of what I'm doing wrong, could you help or suggest something? Processor Info: > Intel(R) Core(TM) i3-9100F CPU @ 3.60GHz System info: > 4.4.208-rt198 #1 SMP PREEMPT RT Wed Jul 8 16:23:16 -03 2020 x86_64 x86_64 x86_64 GNU/Linux My application code: > #include <sys/mman.h> > #include <stdio.h> > #include <stdlib.h> > #include <sched.h> > #include <stdint.h> > #include <time.h> > #include <pthread.h> > #define LOOPS 1000 > #define INTERVAL 0.01 //0.01 s = 10 ms = 10000 us = 10000000 ns > #define USE_NS 1 > #define NSEC_PER_SEC 1000000000 > #define USEC_PER_SEC 1000000 > static inline long int calcdiff_ns(struct timespec t1, struct timespec t2); > static inline long int calcdiff_us(struct timespec t1, struct timespec t2); > static inline void time_norm(struct timespec *ts); > > void* sim_Thread( void* notUsed ); > pthread_t sim_ThreadID; > pthread_attr_t sim_AttrThread; > struct sched_param sim_MySched; > // Compiler flags: gcc -O0 time_eval_4.c -o time_eval_4 -Wall -lm -lpthread > int main () > { > if(mlockall(MCL_CURRENT|MCL_FUTURE) == -1) > { > printf("mlockall failed: %m\n"); > exit(-2); > } > pthread_attr_init( &sim_AttrThread ); > sim_MySched.sched_priority = 80; > pthread_attr_setschedpolicy( &sim_AttrThread, SCHED_FIFO ); > pthread_attr_setinheritsched( &sim_AttrThread, PTHREAD_EXPLICIT_SCHED ); > pthread_attr_setschedparam( &sim_AttrThread, &sim_MySched ); > pthread_create( &sim_ThreadID, &sim_AttrThread, sim_Thread, NULL ); > pthread_join( sim_ThreadID, NULL ); > return 0; > } > void* sim_Thread( void* notUsed ) > { > struct timespec now; > struct timespec next; > struct timespec periodicInterval; > unsigned long int diff; > unsigned long int j; > FILE *benchmark_fp; > benchmark_fp = fopen("Benchmark_4.txt","w+"); > fprintf(benchmark_fp, "loops: %ld \n", (unsigned long int) LOOPS); > periodicInterval.tv_sec = (unsigned long int)( INTERVAL ); > periodicInterval.tv_nsec = ( (unsigned long int)( (unsigned long int)( INTERVAL * 1000000000 ) % 1000000000 ) ); > #if USE_NS > fprintf(benchmark_fp, "interval: %f ns\n",(double) INTERVAL*NSEC_PER_SEC); > #else > fprintf(benchmark_fp, "interval: %f us\n",(double) INTERVAL*USEC_PER_SEC); > #endif > if( clock_gettime( CLOCK_REALTIME, &now ) != 0 ) return 0; > next.tv_nsec = now.tv_nsec + periodicInterval.tv_nsec; > next.tv_sec = now.tv_sec + periodicInterval.tv_sec; > time_norm(&next); > for(j=0;j< (unsigned long int) LOOPS; j++ ) > { > > // Wait for the next period to wake up... > clock_nanosleep( CLOCK_REALTIME, TIMER_ABSTIME, &next, NULL ); > // Get the "now" time moment... > clock_gettime( CLOCK_REALTIME, &now ); > > #if USE_NS > diff=calcdiff_ns(now, next); > #else > diff=calcdiff_us(now, next); > #endif > // Set the timer for interrupt in the next moment... > next.tv_nsec += periodicInterval.tv_nsec; > next.tv_sec += periodicInterval.tv_sec; > time_norm(&next); > fprintf(benchmark_fp, "%ld\n", diff); > } > fclose(benchmark_fp); > } > static inline long int calcdiff_ns(struct timespec t1, struct timespec t2) > { > long int diff; > diff = NSEC_PER_SEC * (long int)((int) t1.tv_sec - (int) t2.tv_sec); > diff += ((int) t1.tv_nsec - (int) t2.tv_nsec); > return diff; > } > > static inline long int calcdiff_us(struct timespec t1, struct timespec t2) > { > long int diff; > diff = USEC_PER_SEC * (long long)((int) t1.tv_sec - (int) t2.tv_sec); > diff += ((int) t1.tv_nsec - (int) t2.tv_nsec) / 1000; > return diff; > } > static inline void time_norm(struct timespec *ts) > { > while (ts->tv_nsec >= NSEC_PER_SEC) { > ts->tv_nsec -= NSEC_PER_SEC; > ts->tv_sec++; > } > } Thanks! Michel