All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Herrera-Bendezu, Luis" <lherrera@domain.hid>
To: "xenomai@xenomai.org" <xenomai@xenomai.org>
Subject: [Xenomai-help]  system locks when calling Linux clock_gettime
Date: Tue, 13 Mar 2012 18:06:43 +0000	[thread overview]
Message-ID: <584FFDF8F96BB74593285A7D65886EC008B91F25@domain.hid> (raw)

[-- 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 --]

             reply	other threads:[~2012-03-13 18:06 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-03-13 18:06 Herrera-Bendezu, Luis [this message]
2012-03-13 18:22 ` [Xenomai-help] system locks when calling Linux clock_gettime Gilles Chanteperdrix
2012-03-13 18:35   ` Herrera-Bendezu, Luis

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=584FFDF8F96BB74593285A7D65886EC008B91F25@domain.hid \
    --to=lherrera@domain.hid \
    --cc=xenomai@xenomai.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.