All of lore.kernel.org
 help / color / mirror / Atom feed
* [Xenomai-help]  system locks when calling Linux clock_gettime
@ 2012-03-13 18:06 Herrera-Bendezu, Luis
  2012-03-13 18:22 ` Gilles Chanteperdrix
  0 siblings, 1 reply; 3+ messages in thread
From: Herrera-Bendezu, Luis @ 2012-03-13 18:06 UTC (permalink / raw)
  To: xenomai

[-- Attachment #1: Type: text/plain, Size: 3663 bytes --]

Hello,

Xenomai version 2.4.10, Linux version 2.6.30.3, CPU PPC.

I am trying to work around an issue where the Linux and Xenomai realtime clocks
drifts when Linux clock is updated by NTP. It was suggested in the mail exchange:
http://www.mail-archive.com/xenomai@xenomai.org

to use clock_gettime()/clock_settime() to keep both clocks in sync.

However, calling linux clock_gettime() from a Xenomai  thread is causing system to
freeze after a few minutes as shown in the following test program where the interval
to re-sync clocks is small, 50 msec, to force error faster:

#include <pthread.h>
#include <time.h>
#include <rtdk.h>
#include <sys/mman.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>


int __real_clock_gettime(clockid_t clock_id,
                   struct timespec *tp);


// Interval to sync clocks, 50 msec.
#define CLOCK_SYNC_NSEC_INTERVAL 50000000

// Maximum clock drift tolerated, 2 msec.
#define MAX_NSEC_DRIFT 2000000


void
make_rt_thread()
{
    int retval = 0;
    struct sched_param sparam;

    sparam.sched_priority = 1;

    if ((retval = pthread_setschedparam(pthread_self(),
                              SCHED_FIFO, &sparam)) != 0) {

        rt_printf("pthread_setschedparam: %s\n", strerror(retval));

        exit(retval);
    }
}

// Get Xenomai and Linux realtime clocks.
//
// return 0 on success and -1 otherwise.
int
get_realtime_clocks(struct timespec *pxeno,
                struct timespec *pposix)
{
    if (clock_gettime(CLOCK_REALTIME, pxeno) != 0) {
      rt_printf("%s: error clock_gettime\n", __FUNCTION__);

      return -1;
    }

    if (__real_clock_gettime(CLOCK_REALTIME, pposix) != 0) {
      rt_printf("%s: error __real_clock_gettime\n", __FUNCTION__);

      return -1;
    }

    return 0;
}


// Synchronize Xenomai clock to Linux's.
int
sync_xeno_clock(struct timespec *pposix)
{
    int retval;

    retval = clock_settime(CLOCK_REALTIME, pposix);
    if (retval != 0) {
      return -1;
    }

    return 0;
}

int
main (int argc, char** argv)
{
    struct timespec xeno_real, posix_real;
    struct timespec sync_interval;
    int retval;

    mlockall(MCL_CURRENT | MCL_FUTURE);
    make_rt_thread();

    rt_print_auto_init(1);
    rt_print_init(16384, "SYNC_RT");

    // sync clocks.
    retval = get_realtime_clocks(&xeno_real, &posix_real);
    if (retval != 0) {
      rt_printf("%s: cannot read clocks\n", __FUNCTION__);

      exit(1);
    }

    retval = sync_xeno_clock(&posix_real);
    if (retval != 0) {
      rt_printf("%s: cannot sync clock\n", __FUNCTION__);

      exit(1);
    }

    sync_interval.tv_sec = 0;
    sync_interval.tv_nsec = CLOCK_SYNC_NSEC_INTERVAL;

    // loop to keep clocks in sync.
    while (1) {
      nanosleep(&sync_interval, NULL);

      retval = get_realtime_clocks(&xeno_real, &posix_real);
      if (retval != 0) {
          continue;
      }
      if (xeno_real.tv_sec != posix_real.tv_sec) {
          // clocks drift, resync.
          retval = sync_xeno_clock(&posix_real);
          if (retval != 0) {
            rt_printf("%s: cannot resync clock\n", __FUNCTION__);

            continue;
          }
      } else if (labs(xeno_real.tv_nsec - posix_real.tv_nsec) >
               MAX_NSEC_DRIFT) {
          // clocks drift, resync.
          retval = sync_xeno_clock(&posix_real);
          if (retval != 0) {
            rt_printf("%s: cannot resync clock\n", __FUNCTION__);

            continue;
          }
      }
    }

    exit(1);
}

Thanks,
Luis

[-- Attachment #2: Type: text/html, Size: 27241 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2012-03-13 18:35 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-03-13 18:06 [Xenomai-help] system locks when calling Linux clock_gettime Herrera-Bendezu, Luis
2012-03-13 18:22 ` Gilles Chanteperdrix
2012-03-13 18:35   ` Herrera-Bendezu, Luis

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.