0

I have installed Linux kernel 4.6 with rt patch and after uname -a it shows rt and PREEMPT but I have to prove by writing application so I have come with idea as below. I will make sleep for five milliseconds wake up and check hardware time and store in an array and do it for some 50 times now after I just check the difference in the hardware time which should be same if its real time. so i need to know if its right way and tips on implementing it. thanks in advance any help would be great.

this is my code /* compile using "gcc -o swave swave.c -lrt -Wall" */

    #
    include < stdlib.h > #include < stdio.h > #include < time.h > #include < sched.h > #include < sys / io.h >

      #define PORT 0x378# define NSEC_PER_SEC 1000000000

    /* using clock_nanosleep of librt */
    extern int clock_nanosleep(clockid_t __clock_id, int __flags,
      __const struct timespec * __req,
      struct timespec * __rem);

    /* the struct timespec consists of nanoseconds
     * and seconds. if the nanoseconds are getting
     * bigger than 1000000000 (= 1 second) the
     * variable containing seconds has to be
     * incremented and the nanoseconds decremented
     * by 1000000000.
     */
    static inline void tsnorm(struct timespec * ts) {
      while (ts - > tv_nsec >= NSEC_PER_SEC) {
        ts - > tv_nsec -= NSEC_PER_SEC;
        ts - > tv_sec++;
      }
    }

    /* increment counter and write to parallelport */
    void out() {
      static unsigned char state = 0;
      outb(state++, PORT);
    }

    int main(int argc, char * * argv) {
      struct timespec t;
      struct sched_param param;
      /* default interval = 50000ns = 50us
       * cycle duration = 100us
       */
      long array[500001], diff[500001];
      int n = 500000;
      int interval = 50000;

      /* set permissions of parallelport */
      ioperm(PORT, 1, 1);

      if (argc >= 2 && atoi(argv[1]) > 0) {
        printf("using realtime, priority: %d\n", atoi(argv[1]));
        param.sched_priority = atoi(argv[1]);
        /* enable realtime fifo scheduling */
        if (sched_setscheduler(0, SCHED_FIFO, & param) == -1) {
          perror("sched_setscheduler failed");
          exit(-1);
        }
      }
      if (argc >= 3)
        interval = atoi(argv[2]);

      /* get current time */
      clock_gettime(0, & t);
      /* start after one second */
      t.tv_sec++;
      while (n) {
        /* wait untill next shot */
        clock_nanosleep(0, TIMER_ABSTIME, & t, NULL);
        /* do the stuff */
        array[n] = t.tv_nsec;
        /* calculate next shot */
        t.tv_nsec += interval;
        tsnorm( & t);
        n--;
      }
      printf("hell\n\n");
      n = 500000;

      while (array[n]) {
        diff[n] = array[n - 1] - array[n];
        if (diff[n] != 50000) {
          printf("%ld n:%d ", diff[n], n);
        }
        n--;
      }
      return 0;
    }
Claudio
  • 10,614
  • 4
  • 31
  • 71
RPK
  • 53
  • 1
  • 13
  • Im not sure, but i doubt you can fight hardware nonmaskable interrupts. So, time you'll measure will differ. You can monitor /proc/interrupts for interrupt information. Even more, if you talking only about multitasking, than scheduler uses software interrupt witch priority much lower than simple hardware interrupt. On other hand 5 ms is a small time interval so you can slip between any interrupts. :-) – nopasara Nov 05 '16 at 05:59
  • I am trying to do [ https://rt.wiki.kernel.org/index.php/Squarewave-example ] but don't have oscilloscope so using time. – RPK Nov 05 '16 at 07:09
  • Don't get me wrong, i'm just telling you that due to existence of hardware interrupts, instruction reordering, caches and other hardware issues you'll never get exact the same result. Generated wave in example is also **almost** perfect. Just be ready to see some time deviation. And the more accurate counter you'll use to measure time the more deviation you'll see. All depends on what time precision you need to solve your real task. – nopasara Nov 05 '16 at 07:41
  • i have modified the code to get the difference. there should be some difference when its RT. and tell me some sources to read about this. – RPK Nov 05 '16 at 08:18
  • If you'll compare 2 processes (RT and not RT) execution times and execution times deviations no doubt you'll see big difference. The best document about time measurement i've ever seen [here](http://www.intel.com/content/dam/www/public/us/en/documents/white-papers/ia-32-ia-64-benchmark-code-execution-paper.pdf) . If i helped please rate my comments. – nopasara Nov 05 '16 at 08:27
  • maybe you can read uname -a und check for the rt part? – 12431234123412341234123 Nov 06 '16 at 01:15
  • i did its rt and preempt – RPK Nov 07 '16 at 05:11

0 Answers0