All of lore.kernel.org
 help / color / mirror / Atom feed
From: Michel Macena Oliveira <michel@austral-dynamics.com>
To: linux-rt-users@vger.kernel.org
Subject: PREEMPT_RT benchmark
Date: Wed, 10 Mar 2021 17:07:47 -0300	[thread overview]
Message-ID: <CAKkbJ-ypaPZ5JKpXD+PykvO6oYzxEUdxXY2RbVJKWdGY3ALuLg@mail.gmail.com> (raw)

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

             reply	other threads:[~2021-03-10 20:10 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-10 20:07 Michel Macena Oliveira [this message]
2021-03-11 12:27 ` PREEMPT_RT benchmark Daniel Wagner
2021-03-11 13:38   ` John Ogness
2021-03-11 14:56   ` Michel Macena Oliveira
2021-03-11 16:17     ` Ahmed S. Darwish
2021-03-11 17:02       ` Michel Macena Oliveira
2021-03-11 17:56         ` Ahmed S. Darwish
2021-03-12 15:45           ` Michel Macena Oliveira
2021-03-11 12:58 ` Ahmed S. Darwish
2021-03-11 15:03   ` Michel Macena Oliveira
2021-03-11 16:01     ` Ahmed S. Darwish

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=CAKkbJ-ypaPZ5JKpXD+PykvO6oYzxEUdxXY2RbVJKWdGY3ALuLg@mail.gmail.com \
    --to=michel@austral-dynamics.com \
    --cc=linux-rt-users@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.