All of lore.kernel.org
 help / color / mirror / Atom feed
* Posix periodic time first tick
@ 2020-08-07 15:12 Petr Červenka
  2020-08-10  6:45 ` Per Oberg
  0 siblings, 1 reply; 6+ messages in thread
From: Petr Červenka @ 2020-08-07 15:12 UTC (permalink / raw)
  To: Xenomai

Hello.
I have created small example of periodic task in posix.
If I compile it without xenomai it works fine.
When I add Xenomai linking flags, the first tick of periodic task take around 16s. After that it continues as it should.
Used clock type for timer_fd is CLOCK_MONOTONIC. The example should in attachment.
Do you have any suggestions?
Best regards
-------------- next part --------------
A non-text attachment was scrubbed...
Name: timer_test.cpp
Type: text/x-c++src
Size: 4351 bytes
Desc: timer_test.cpp
URL: <http://xenomai.org/pipermail/xenomai/attachments/20200807/d4203f59/attachment.cpp>

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

* Re: Posix periodic time first tick
  2020-08-07 15:12 Posix periodic time first tick Petr Červenka
@ 2020-08-10  6:45 ` Per Oberg
  2020-08-10  7:52   ` Petr Červenka
  0 siblings, 1 reply; 6+ messages in thread
From: Per Oberg @ 2020-08-10  6:45 UTC (permalink / raw)
  To: xenomai; +Cc: Petr Červenka

----- Den 7 aug 2020, på kl 17:12, xenomai xenomai@xenomai.org skrev:

> Hello.
> I have created small example of periodic task in posix.
> If I compile it without xenomai it works fine.
> When I add Xenomai linking flags, the first tick of periodic task take around
> 16s. After that it continues as it should.
> Used clock type for timer_fd is CLOCK_MONOTONIC. The example should in
> attachment.
> Do you have any suggestions?

Just had a extremely quick look at it. Are you sure you don't get any rounding errors when doing your period arithmetics ? 

> Best regards
> -------------- next part --------------
> A non-text attachment was scrubbed...
> Name: timer_test.cpp
> Type: text/x-c++src
> Size: 4351 bytes
> Desc: timer_test.cpp
> URL:
> <http://xenomai.org/pipermail/xenomai/attachments/20200807/d4203f59/attachment.cpp>

Per Öberg 


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

* Re: Posix periodic time first tick
  2020-08-10  6:45 ` Per Oberg
@ 2020-08-10  7:52   ` Petr Červenka
  2020-08-10  8:54     ` Per Oberg
  2020-08-10  9:49     ` Philippe Gerum
  0 siblings, 2 replies; 6+ messages in thread
From: Petr Červenka @ 2020-08-10  7:52 UTC (permalink / raw)
  To: Per Oberg, xenomai

Hello.

The problem is only when I use Xenomai linking flags and only with 
thestart time. Period is OK.

That aprox. 16s time delay can be "compensated" by substracting of any 
number of seconds from the start time.

     // Create timer
     timer_fd = timerfd_create(CLOCK_MONOTONIC, 0);

     // Get current time
     struct timespec start;
     clock_gettime(CLOCK_MONOTONIC, &start);

     // Set periodic
     struct itimerspec timer_conf;
     memset(&timer_conf, 0, sizeof (timer_conf));
     timer_conf.it_value = timespec_add(start, -15 * NANOSEC_PER_SEC); 
// compensation of the delay
     ...
     timerfd_settime(timer_fd, TFD_TIMER_ABSTIME, &timer_conf, NULL);

There is strange unexpected difference/time shift between product of 
clock_gettime and timerfd_settime. Only with Xenomai posix wrappers.

Best regards

Petr Cervenka


Dne 10.08.2020 v 8:45 Per Oberg napsal(a):
> ----- Den 7 aug 2020, på kl 17:12, xenomai xenomai@xenomai.org skrev:
>
>> Hello.
>> I have created small example of periodic task in posix.
>> If I compile it without xenomai it works fine.
>> When I add Xenomai linking flags, the first tick of periodic task take around
>> 16s. After that it continues as it should.
>> Used clock type for timer_fd is CLOCK_MONOTONIC. The example should in
>> attachment.
>> Do you have any suggestions?
> Just had a extremely quick look at it. Are you sure you don't get any rounding errors when doing your period arithmetics ?
>
>> Best regards
>> -------------- next part --------------
>> A non-text attachment was scrubbed...
>> Name: timer_test.cpp
>> Type: text/x-c++src
>> Size: 4351 bytes
>> Desc: timer_test.cpp
>> URL:
>> <http://xenomai.org/pipermail/xenomai/attachments/20200807/d4203f59/attachment.cpp>
> Per Öberg


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

* Re: Posix periodic time first tick
  2020-08-10  7:52   ` Petr Červenka
@ 2020-08-10  8:54     ` Per Oberg
  2020-08-10  9:49     ` Philippe Gerum
  1 sibling, 0 replies; 6+ messages in thread
From: Per Oberg @ 2020-08-10  8:54 UTC (permalink / raw)
  To: xenomai; +Cc: Petr Červenka

----- Den 10 aug 2020, på kl 9:52, Petr Červenka grugh@centrum.cz skrev:

> Hello.

> The problem is only when I use Xenomai linking flags and only with
> thestart time. Period is OK.

> That aprox. 16s time delay can be "compensated" by substracting of any
> number of seconds from the start time.

You could try using CLOCK_REALTIME or CLOCK_HOST_REALTIME instead of CLOCK_MONOTONIC. I use timerfd and similar heuristics without issues afaik.


>From the top of my head I don't recall exactly how Xenomai timers correspond to Linux timers but you may have an issue if you check your starting time a non RT thread and use that value in a RT thread, depending on your choice of timer.  

Check out: https://gitlab.denx.de/Xenomai/xenomai/-/wikis/Porting_To_Xenomai_POSIX

I have had some issues with time-keeping of my machines and I had to do a bit of manual tuning for it to work properly (talking 0.x pct drifts but also a bit of a offset). For debugging I used the  following:

* Hard coding freq using boot time kernel argument: "clocksource=tsc tsc=reliable xenomai.clockfreq=2807909955"
* Using (in my case ) "adjtimex -f 2080674" to adjust the time-keeping scaling
* "clocktest" from Xenomai test-suite. 
 Right now I have (for a setting tuned to some machine and then run on another)
CPU      ToD offset [us] ToD drift [us/s]      warps max delta [us]
--- -------------------- ---------------- ---------- --------------
  0          110881217.1       316239.964          0            0.0
  1          110881224.7       316239.943          0            0.0
  2          110881216.8       316239.928          0            0.0
  3          110881217.4       316239.921          0            0.0


Which is about 110s and drifting 0.3s per second. 

Regards
Per Öberg

> // Create timer
> timer_fd = timerfd_create(CLOCK_MONOTONIC, 0);

> // Get current time
> struct timespec start;
> clock_gettime(CLOCK_MONOTONIC, &start);

> // Set periodic
> struct itimerspec timer_conf;
> memset(&timer_conf, 0, sizeof (timer_conf));
> timer_conf.it_value = timespec_add(start, -15 * NANOSEC_PER_SEC);
> // compensation of the delay
> ...
> timerfd_settime(timer_fd, TFD_TIMER_ABSTIME, &timer_conf, NULL);

> There is strange unexpected difference/time shift between product of
> clock_gettime and timerfd_settime. Only with Xenomai posix wrappers.

> Best regards

> Petr Cervenka

> Dne 10.08.2020 v 8:45 Per Oberg napsal(a):
> > ----- Den 7 aug 2020, på kl 17:12, xenomai xenomai@xenomai.org skrev:

> >> Hello.
> >> I have created small example of periodic task in posix.
> >> If I compile it without xenomai it works fine.
> >> When I add Xenomai linking flags, the first tick of periodic task take around
> >> 16s. After that it continues as it should.
> >> Used clock type for timer_fd is CLOCK_MONOTONIC. The example should in
> >> attachment.
> >> Do you have any suggestions?
>> Just had a extremely quick look at it. Are you sure you don't get any rounding
> > errors when doing your period arithmetics ?

> >> Best regards
> >> -------------- next part --------------
> >> A non-text attachment was scrubbed...
> >> Name: timer_test.cpp
> >> Type: text/x-c++src
> >> Size: 4351 bytes
> >> Desc: timer_test.cpp
> >> URL:
> >> <http://xenomai.org/pipermail/xenomai/attachments/20200807/d4203f59/attachment.cpp>
> > Per Öberg


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

* Re: Posix periodic time first tick
  2020-08-10  7:52   ` Petr Červenka
  2020-08-10  8:54     ` Per Oberg
@ 2020-08-10  9:49     ` Philippe Gerum
  2020-08-18 15:21       ` Petr Červenka
  1 sibling, 1 reply; 6+ messages in thread
From: Philippe Gerum @ 2020-08-10  9:49 UTC (permalink / raw)
  To: Petr Červenka, Per Oberg, xenomai

On 8/10/20 9:52 AM, Petr Červenka via Xenomai wrote:
> Hello.
> 
> The problem is only when I use Xenomai linking flags and only with thestart
> time. Period is OK.
> 
> That aprox. 16s time delay can be "compensated" by substracting of any number
> of seconds from the start time.
> 
>     // Create timer
>     timer_fd = timerfd_create(CLOCK_MONOTONIC, 0);
> 
>     // Get current time
>     struct timespec start;
>     clock_gettime(CLOCK_MONOTONIC, &start);
> 
>     // Set periodic
>     struct itimerspec timer_conf;
>     memset(&timer_conf, 0, sizeof (timer_conf));
>     timer_conf.it_value = timespec_add(start, -15 * NANOSEC_PER_SEC); //
> compensation of the delay
>     ...
>     timerfd_settime(timer_fd, TFD_TIMER_ABSTIME, &timer_conf, NULL);
> 
> There is strange unexpected difference/time shift between product of
> clock_gettime and timerfd_settime. Only with Xenomai posix wrappers.
> 

To debug that kind of issues, you first need to check whether the symbol
wrapping is ok in the executable image. A common way to do this is to do a
visual inspection of the name list with nm, e.g. for the 'latency' program
which uses the timerfd services, one could do:

{rpm@cobalt} aarch64-linux-gnu-nm -o .../latency | grep -e __wrap
.../latency:                 U __wrap_clock_gettime
.../latency:                 U __wrap_close
.../latency:                 U __wrap_fclose
.../latency:                 U __wrap_fprintf
.../latency:                 U __wrap_fputc
.../latency:                 U __wrap_fputs
.../latency:                 U __wrap_free
.../latency:                 U __wrap_fwrite
.../latency:                 U __wrap_ioctl
.../latency:                 U __wrap_kill
.../latency:0000000000402728 W __wrap_main
.../latency:                 U __wrap_open
.../latency:                 U __wrap_printf
.../latency:                 U __wrap_pthread_attr_init
.../latency:                 U __wrap_pthread_create
.../latency:                 U __wrap_pthread_join
.../latency:                 U __wrap_pthread_setname_np
.../latency:                 U __wrap_puts
.../latency:                 U __wrap_read
.../latency:                 U __wrap_sem_close
.../latency:                 U __wrap_sem_open
.../latency:                 U __wrap_sem_post
.../latency:                 U __wrap_sem_unlink
.../latency:                 U __wrap_sem_wait
.../latency:                 U __wrap_time
.../latency:                 U __wrap_timerfd_create
.../latency:                 U __wrap_timerfd_settime
.../latency:                 U __wrap_write

We can see that all calls to the timerfd and clock_gettime services are
wrapped to libcobalt, which is right.

Weird delays may be caused by discrepancies in the call set, e.g. feeding
cobalt's timerfd services with the output of glibc's regular clock_gettime()
call. Since cobalt clocks defines epochs which differ from the common
kernel's, they won't mix nicely.

IOW, the issue may be in your build recipe, affecting such wrapping, not in
your code.

PS: you could also annotate the calls to clock_gettime() and friends in a way
which guarantees that the cobalt version is going to be used, e.g.

ret = __RT(clock_gettime(...));
...
ret = __RT(timerfd_ceate(...));

Granted, this looks ugly, but in some cases this is helpful, particularly in
order to figure out any issue with the wrapping.

-- 
Philippe.


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

* Re: Posix periodic time first tick
  2020-08-10  9:49     ` Philippe Gerum
@ 2020-08-18 15:21       ` Petr Červenka
  0 siblings, 0 replies; 6+ messages in thread
From: Petr Červenka @ 2020-08-18 15:21 UTC (permalink / raw)
  To: Philippe Gerum, Per Oberg, Xenomai

Hello.
Sorry for late answer, I was on vacations.
I originally thought that the issue was present on Xenomai 2.6.3 and Xenomai 3.1.
Now I can confirm, that Xenomai 2.6.3 has that issue, but I can't reproduce it on Xenomai 3.1 anymore. So it could be only my imagination.
So ignore most of my comments and thank you for your support.
Best regards
Petr C.


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

end of thread, other threads:[~2020-08-18 15:21 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-08-07 15:12 Posix periodic time first tick Petr Červenka
2020-08-10  6:45 ` Per Oberg
2020-08-10  7:52   ` Petr Červenka
2020-08-10  8:54     ` Per Oberg
2020-08-10  9:49     ` Philippe Gerum
2020-08-18 15:21       ` Petr Červenka

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.