EPICS Controls Argonne National Laboratory

Experimental Physics and
Industrial Control System

1994  1995  1996  1997  1998  1999  2000  2001  2002  2003  2004  2005  2006  2007  2008  2009  2010  2011  2012  2013  2014  2015  2016  2017  2018  2019  2020  2021  2022  <20232024  Index 1994  1995  1996  1997  1998  1999  2000  2001  2002  2003  2004  2005  2006  2007  2008  2009  2010  2011  2012  2013  2014  2015  2016  2017  2018  2019  2020  2021  2022  <20232024 
<== Date ==> <== Thread ==>

Subject: Re: Questions about Function: epicsEventWaitWithTimeout (id, timeout)
From: Till Straumann via Tech-talk <tech-talk at aps.anl.gov>
To: 김대영 <dykim1114 at dawonsys.com>, tech-talk at aps.anl.gov
Cc: 박상욱/수석/SnE팀 <screenbe at dawonsys.com>, 박진용/책임/CnS팀 <pjy0429 at dawonsys.com>
Date: Tue, 2 May 2023 09:26:30 +0200
Hello 김대영

epicsEventWaitWithTimeout() is executed on your computer like any other code.
The number of machine instructions it takes to complete is never fully deterministic
on a multi-threaded, multi-core computer.
Even if we assume (for the sake of the argument) that you have hardware timers
to generate timeouts there have to be many CPU instructions involved to arm the timer,
handle the timer interrupt which signals the end of the timeout and to read out the clock
(epicsTimeGetCurrent()) you use to measure the actually elapsed time.
The time it takes a CPU to perform these operations is subject to pseudo-random
delays:
 - the scheduler may decide that there is higher-priority work and put your task
   to sleep for a while. If that happens, e.g., between the first epicsTimeGetCurrent() and
   epicsEventWaitWithTimeout() then the effective time that has expired is longer than
   what was requested. You'll notice that while the reported result is never exactly
   what you wanted but it is *always* more than what you had asked for.
   These delays also can happen in the routines themselves or even in kernel services
   they rely on.
 - the thread that executes the code may have to wait for a resource that is currently
   in use by another thread and therefore be delayed.
 - interrupts and interrupt-handlers introduce delays very similar to what was already
   mentioned.
 - there are also hardware-induced delays such as cache-misses, limited memory
   bandwidth and other causes (e.g., on intel: system-management mode).

There are operating systems explicitly designed ('hard real-time OS') to minimize
the delays introduced by software and thus guarantee a maximal *latency* or delay
that the user may expect (note that the delays are still random but bounded).
Obviously, a computer's resources (CPUs etc.) are always limited and the number
of events that can be handled with a defined response-time goes hand-in-hand.
Therefore, properly designing a real-time application requires careful planning and
insight.

The difference to a general purpose OS is often only visible under heavy load where
the real-time OS maintains low-latency whereas under a general-purpose OS you will
observe occasional but much higher delays under load. The classic trade-off is throughput
vs. latency, i.e., a real-time OS has quicker and deterministic response time but less
throughput than a general-purpose OS.

This would be a long answer to the second part of Q1. I don't understand the first part
("...indicating seconds. But the result is not.").

Q2: use a real-time OS such as linux with RT_PREEMPT. However, to properly use its
features I'd suggest some studying of the topic to a point where you can answer your
questions yourself.

In my experience with this kind of OS you can expect max. latencies of several 10s
of micro-seconds. Note that is pretty much what you observe already; however, I'm
not sure if your system could maintain this performance under load.

Q3: if you really need deterministic timing of < 10us accuracy then you have to
look for hardware solutions (fpga).

HTH
- Till

On 5/2/23 07:30, 김대영 via Tech-talk wrote:


Dear All

First of all, thank you for your help.
We are developing a multi-core program in Epics 7.0.4. 
(Scientific Linux 7.9 / kernel 3.10.0-1160.42.2.rt56.1182.el7.x86_64)

We use a function called epicsEventWaitWithTimeout(id,timeOut).
We obtained the following Time Test results.

#1 epicsEventWaitWithTimeout( pSTDev->ST_RTthread.threadEventId,  1);
     >  Result: 1000067us (MAX) / 1000014us (MIN) 

#2 epicsEventWaitWithTimeout( pSTDev->ST_RTthread.threadEventId,  0.0005);
     >  Result: 550us (MAX) / 549us (MIN) 

#3 epicsEventWaitWithTimeout( pSTDev->ST_RTthread.threadEventId,  0.0001);
     >  Result: 154us (MAX) / 112us (MIN) 

#4 epicsEventWaitWithTimeout( pSTDev->ST_RTthread.threadEventId,  0.00005);
     >  Result: 104us (MAX) / 97us (MIN) 

#5 epicsEventWaitWithTimeout( pSTDev->ST_RTthread.threadEventId,  0.00001);
     >  Result: 64.9us (MAX) / 60.5us (MIN) 

#6 epicsEventWaitWithTimeout( pSTDev->ST_RTthread.threadEventId,  0.000001);
     >  Result: 56us (MAX) / 55.9us (MIN) 
     
#7 epicsEventWaitWithTimeout( pSTDev->ST_RTthread.threadEventId,  0);
     >  Result: 54.9us (MAX) / 54.8us (MIN) 

The time measurement method is as follows.


There are some questions based on the test results.
Q1) The parameter, timeout, is a double type, indicating seconds.
     But the result is not. Something is not accurate. What could be the cause?
Q2) How do I improve time accuracy?    
Q3) Is there any other way than using epicsEventWaitWithTimeout?

 

 

Thank you and Best Regards,

 

 

 


References:
Questions about Function: epicsEventWaitWithTimeout (id, timeout) 김대영 via Tech-talk

Navigate by Date:
Prev: Questions about Function: epicsEventWaitWithTimeout (id, timeout) 김대영 via Tech-talk
Next: Re: External: Questions about Function: epicsEventWaitWithTimeout (id, timeout) Niko Kivel via Tech-talk
Index: 1994  1995  1996  1997  1998  1999  2000  2001  2002  2003  2004  2005  2006  2007  2008  2009  2010  2011  2012  2013  2014  2015  2016  2017  2018  2019  2020  2021  2022  <20232024 
Navigate by Thread:
Prev: Questions about Function: epicsEventWaitWithTimeout (id, timeout) 김대영 via Tech-talk
Next: Re: External: Questions about Function: epicsEventWaitWithTimeout (id, timeout) Niko Kivel via Tech-talk
Index: 1994  1995  1996  1997  1998  1999  2000  2001  2002  2003  2004  2005  2006  2007  2008  2009  2010  2011  2012  2013  2014  2015  2016  2017  2018  2019  2020  2021  2022  <20232024 
ANJ, 02 May 2023 Valid HTML 4.01! · Home · News · About · Base · Modules · Extensions · Distributions · Download ·
· Search · EPICS V4 · IRMIS · Talk · Bugs · Documents · Links · Licensing ·