Comparing timestamps generated in rt and non-rt contexts

All,

This is a slightly OT question, but I'd like to hear your opinion on the
subject, if you have one.

I have a realtime compoent that needs to compare timestamps coming from the
outside via clock_gettime(CLOCK_REALTIME, &tv) with timestamps generated by
itself, currently via Xenomai's rt_timer_read(). The two timestamps do not
share a common origin, and their offset is subject to drift (eg. as a
consequence of NTP syncing).

The only clean solution I've found so far is to upgrade to the very recent
(Nov-2011) Xenomai 2.6, and use clock_gettime(CLOCK_HOST_REALTIME, &tv) of
the posix skin, which is a real-time clock synchronized with the Linux
clock [1, 2].

This is the first time I face this issue, but I'm pretty sure that I'm not
the first one to face it, so I'm interested in knowing how some of you
might have dealt with it: manually messing around with offsets?, how about
drift over time?.

TIA,

Adolfo.

[1]
http://www.xenomai.org/index.php/Xenomai:News#2011-09-04_Xenomai_2.6.0-rc1

[2] http://www.digipedia.pl/usenet/thread/19088/12251/#post12251

Comparing timestamps generated in rt and non-rt contexts

On Wed, Jan 18, 2012 at 5:55 PM, Adolfo Rodríguez Tsouroukdissian <
adolfo [dot] rodriguez [..] ...> wrote:

> All,
>
> This is a slightly OT question, but I'd like to hear your opinion on the
> subject, if you have one.
>
> I have a realtime compoent that needs to compare timestamps coming from
> the outside via clock_gettime(CLOCK_REALTIME, &tv) with timestamps
> generated by itself, currently via Xenomai's rt_timer_read(). The two
> timestamps do not share a common origin, and their offset is subject to
> drift (eg. as a consequence of NTP syncing).
>
> The only clean solution I've found so far is to upgrade to the very recent
> (Nov-2011) Xenomai 2.6, and use clock_gettime(CLOCK_HOST_REALTIME, &tv)of the posix skin, which is a real-time clock synchronized with the Linux
> clock [1, 2].
>
> This is the first time I face this issue, but I'm pretty sure that I'm not
> the first one to face it, so I'm interested in knowing how some of you
> might have dealt with it: manually messing around with offsets?, how about
> drift over time?.
>

More datail on how I'd code a solution without Xenomai 2.6: Create a
component linked against the realtime libraries, xenomai in my case, but
scheduled by ORO_SHED_OTHER, and periodically compute the current offset
between clocks, hence taking into account clock drift issues. Hackish and
unelegant, but the best I can think of.

Adolfo.

> TIA,
>
> Adolfo.
>
> [1]
> http://www.xenomai.org/index.php/Xenomai:News#2011-09-04_Xenomai_2.6.0-rc1
>
> [2] http://www.digipedia.pl/usenet/thread/19088/12251/#post12251
>
> --
> Adolfo Rodríguez Tsouroukdissian
> Robotics engineer
> adolfo [dot] rodriguez [..] ...
> http://www.pal-robotics.com
>
> PAL ROBOTICS S.L
> c/ Pujades 77-79, 4º4ª
> 08005 Barcelona, Spain.
> Tel. +34.93.414.53.47
> Fax.+34.93.209.11.09
>
> AVISO DE CONFIDENCIALIDAD: Este mensaje y sus documentos adjuntos, pueden
> contener información privilegiada y/o confidencial que está dirigida
> exclusivamente a su destinatario. Si usted recibe este mensaje y no es el
> destinatario indicado, o el empleado encargado de su entrega a dicha
> persona, por favor, notifíquelo inmediatamente y remita el mensaje original
> a la dirección de correo electrónico indicada. Cualquier copia, uso o
> distribución no autorizados de esta comunicación queda estrictamente
> prohibida.
>
> CONFIDENTIALITY NOTICE: This e-mail and the accompanying document(s) may
> contain confidential information which is privileged and intended only for
> the individual or entity to whom they are addressed. If you are not the
> intended recipient, you are hereby notified that any disclosure, copying,
> distribution or use of this e-mail and/or accompanying document(s) is
> strictly prohibited. If you have received this e-mail in error, please
> immediately notify the sender at the above e-mail address.
>

Comparing timestamps generated in rt and non-rt contexts

2012/1/19 Adolfo Rodríguez Tsouroukdissian <adolfo [dot] rodriguez [..] ...>:
>
>
> On Wed, Jan 18, 2012 at 5:55 PM, Adolfo Rodríguez Tsouroukdissian
> <adolfo [dot] rodriguez [..] ...> wrote:
>>
>> All,
>>
>> This is a slightly OT question, but I'd like to hear your opinion on the
>> subject, if you have one.
>>
>> I have a realtime compoent that needs to compare timestamps coming from
>> the outside via clock_gettime(CLOCK_REALTIME, &tv) with timestamps generated
>> by itself, currently via Xenomai's rt_timer_read(). The two timestamps do
>> not share a common origin, and their offset is subject to drift (eg. as a
>> consequence of NTP syncing).
>>
>> The only clean solution I've found so far is to upgrade to the very recent
>> (Nov-2011) Xenomai 2.6, and use clock_gettime(CLOCK_HOST_REALTIME, &tv) of
>> the posix skin, which is a real-time clock synchronized with the Linux clock
>> [1, 2].
>>
>> This is the first time I face this issue, but I'm pretty sure that I'm not
>> the first one to face it, so I'm interested in knowing how some of you might
>> have dealt with it: manually messing around with offsets?, how about drift
>> over time?.
>
>
> More datail on how I'd code a solution without Xenomai 2.6: Create a
> component linked against the realtime libraries, xenomai in my case, but
> scheduled by ORO_SHED_OTHER, and periodically compute the current offset
> between clocks, hence taking into account clock drift issues. Hackish and
> unelegant, but the best I can think of.

There are discussions about this on the Xenomai mailing list. Most
people have a non-Xenomai thread (plain posix) reading gettimeofday
and storing this somewhere, protected by a posix (non-Xenomai) mutex.
A xenomai thread then regularly reads this time (grabbing the mutex
and switching to secondary mode) and calculates the offset wrt the
Xenomai clock.

As I understood it, A Xenomai task should never-never read
gettimeofday, since this can deadlock. See also:
http://bugs.orocos.org/show_bug.cgi?id=881

Peter

Comparing timestamps generated in rt and non-rt contexts

On Thu, Jan 26, 2012 at 11:56 PM, Peter Soetens <peter [..] ...>wrote:

> 2012/1/19 Adolfo Rodríguez Tsouroukdissian <
> adolfo [dot] rodriguez [..] ...>:
> >
> >
> > On Wed, Jan 18, 2012 at 5:55 PM, Adolfo Rodríguez Tsouroukdissian
> > <adolfo [dot] rodriguez [..] ...> wrote:
> >>
> >> All,
> >>
> >> This is a slightly OT question, but I'd like to hear your opinion on the
> >> subject, if you have one.
> >>
> >> I have a realtime compoent that needs to compare timestamps coming from
> >> the outside via clock_gettime(CLOCK_REALTIME, &tv) with timestamps
> generated
> >> by itself, currently via Xenomai's rt_timer_read(). The two timestamps
> do
> >> not share a common origin, and their offset is subject to drift (eg. as
> a
> >> consequence of NTP syncing).
> >>
> >> The only clean solution I've found so far is to upgrade to the very
> recent
> >> (Nov-2011) Xenomai 2.6, and use clock_gettime(CLOCK_HOST_REALTIME, &tv)
> of
> >> the posix skin, which is a real-time clock synchronized with the Linux
> clock
> >> [1, 2].
> >>
> >> This is the first time I face this issue, but I'm pretty sure that I'm
> not
> >> the first one to face it, so I'm interested in knowing how some of you
> might
> >> have dealt with it: manually messing around with offsets?, how about
> drift
> >> over time?.
> >
> >
> > More datail on how I'd code a solution without Xenomai 2.6: Create a
> > component linked against the realtime libraries, xenomai in my case, but
> > scheduled by ORO_SHED_OTHER, and periodically compute the current offset
> > between clocks, hence taking into account clock drift issues. Hackish and
> > unelegant, but the best I can think of.
>
> There are discussions about this on the Xenomai mailing list. Most
> people have a non-Xenomai thread (plain posix) reading gettimeofday
> and storing this somewhere, protected by a posix (non-Xenomai) mutex.
> A xenomai thread then regularly reads this time (grabbing the mutex
> and switching to secondary mode) and calculates the offset wrt the
> Xenomai clock.

I ended up doing something similar to this, but instead of storing time, I
store the offset between the rt and system clock references. If you store
system time, and need to query it at high rates, you need to spin a
non-realtime thread pretty fast, and your timestamps may be offset by as
much as a thread frequency period (or more if your scheduler can't keep
up). Not nice.
Conversely, if you store the offset between clocks, you can spin your
non-realtime thread at a more lazy frequency (inter-clock-drift should have
pretty slow dynamics), and generate timestamps from your realtime thread
with as much granularity as you wish.

I have attached a stripped-down 1.x example component that does this. I
have left out the interface to other components, as it can be implemented
in different ways, depending on what you want (offset, timestamp conversion
or timestamp generation) or how you want it (data flow, operations).

> As I understood it, A Xenomai task should never-never read
> gettimeofday, since this can deadlock. See also:
> http://bugs.orocos.org/show_bug.cgi?id=881
>

Correct, gettimeofday should not be used, but equally important,
clock_gettime(CLOCK_HOST_REALTIME, &tv) shouldn't be used either. I believe
that in some implementations they even share the same backend.

Thanks for your thoughts.

Adolfo.

> Peter
>