Repository navigation
rtapi: add opt-in virtual time for accelerated uspace simulation - #4893
grenzland64 wants to merge 4 commits into
Conversation
|
Have you tried to run the test suite in sim mode? cd /your/lcnc/git/checkout/scripts
export RTAPI_VIRTUAL_TIME=1
./runtests(or parallel tests |
f68e185 to
c5080aa
Compare
|
Yes, but... Without With The cause is the mode itself, not the tests:
So this mode is only useful for pure HAL simulations under If you think a HAL-only simulation mode is too narrow to be worth carrying, I'm happy to hear that. |
|
Without the env variable set or at zero, then all tests must succeed. But, I think we need to look into making the tests function properly with virtual time too. There are lots of RT/non-RT sync problems and letting the HAL thread spin very fast exposes them. We need to look into that a bit more. The The tests that have a wall/virtual time mismatch, well, that is a challenge. These may also be disabled in sim mode if there is no apparent fix. But it should be investigated a bit. |
|
Interestingly enough,... running all tests on my machine just stalled at The third run made it stall again and it stalls in running |
|
FYI, the test exposes a serious bug. |
thread_task() read hal_data->threads_running twice: once to decide on the init pass, once for the cyclic pass. A start that lands between the two reads runs the cyclic functions once before the init functions, and the init pass that follows runs no cyclic functions, so that cycle is missing for every function on the thread. Read it once per pass. Seen with the virtual time of LinuxCNC#4893 under load: timedelta reported a maximum of two periods in about one run out of a hundred, none in 130 runs with the fix.
When the reader of rtapi_app's stderr went away, for example halrun x.hal | grep -q pattern, the next message killed the master with SIGPIPE. If it held the HAL mutex at that moment, every later halcmd spun on the mutex forever. Bertho Stultiens hit this as a hang in the tests/virtual-time test of LinuxCNC#4893 under load. Ignore SIGPIPE at the start of main(), so the master and every client get EPIPE from the write instead. tests/uspace/closed-stderr runs halrun with a stderr pipe that has no reader: before the fix halrun exits 141 and leaves the shared memory behind, with it the thread is created and halrun exits 0.
When the reader of rtapi_app's stderr went away, for example halrun x.hal | grep -q pattern, the next message killed the master with SIGPIPE. If it held the HAL mutex at that moment, every later halcmd spun on the mutex forever. Bertho Stultiens hit this as a hang in the tests/virtual-time test of LinuxCNC#4893 under load. Ignore SIGPIPE at the start of main(), so the master and every client get EPIPE from the write instead. tests/uspace/closed-stderr runs halrun with a stderr pipe that has no reader: before the fix halrun exits 141 and leaves the shared memory behind, with it the thread is created and halrun exits 0.
thread_task() read hal_data->threads_running twice: once to decide on the init pass, once for the cyclic pass. A start that lands between the two reads runs the cyclic functions once before the init functions, and the init pass that follows runs no cyclic functions, so that cycle is missing for every function on the thread. Read it once per pass. Seen with the virtual time of LinuxCNC#4893 under load: timedelta reported a maximum of two periods in about one run out of a hundred, none in 130 runs with the fix.
|
I reproduced the stall and found the cause. It is not halcmd, and it is my test's fault. The stall in The test piped Evidence from a hung run under load (6 CPUs, 24 busy loops): Fixed in 6be9c88: the test now uses Refusing the second thread earlier Also in 6be9c88: the refusal moved from A second race the test exposed, in Looping the fixed test under load, about 1 run in 100 printed 7795363 reads the flag once. Before: 4 bad of 200 and 3 bad of 300. After: 300 of 300 good, no hang. It is a separate commit, so I can move it to its own PR if you prefer. The six failures without the variable I ran the six on a build of the merge base (65f7467), same container:
Not done: I have not rerun the full suite on the build that includes 7795363, only |
|
A fix for the SIGPIPE problem has been committed to master. Now The You need to rebase to resolve the conflict. |
RTAPI_VIRTUAL_TIME=1 selects the POSIX non-realtime backend, skips the period sleep and makes rtapi_get_time() advance by exactly one period per cycle. It is off by default and limited to a single realtime thread. rtapi_app drops root and all capabilities in this mode, so hardware drivers that need privileged access cannot be loaded.
Address review comments on the virtual-time change: - rtapi_virtual_time() is now declared in uspace_rtapi_app.hh and defined in uspace_rtapi_main.cc, where the environment is read once instead of on every call. The comment says "non-zero", like the manual page. - PosixApp uses its virtual_time member: the note is printed from the constructor, and the thread wrapper sets vtime unconditionally, since do_get_time() only reads it in virtual-time mode. - The refused-second-thread case uses a static two-threads.hal and checks for the refusal message instead of accepting any failure.
A full LinuxCNC session does not work in this mode: motion rewrites its status without pause, so task cannot read a consistent copy, and everything waiting on wall-clock time no longer matches the realtime thread.
The refusal sat in task_start(), so hal_create_thread() failed after the task existed and left it behind. Refuse in do_task_new() instead and hand the task slot back. tests/virtual-time: no grep -q. It quits at the first match, the pipe closes and the next write to stderr kills rtapi_app with SIGPIPE. When that happens while it holds the HAL mutex, halcmd waits forever.
|
Update: I rebased onto current master and force-pushed.
On the rebased tree in my container: |
6be9c88 to
711069b
Compare
|
Running all tests (parallel) normally works as expected. However, running in sim mode fails quite a lot. Everything (most?) with the sampler will cause the disk to overflow. The attached file is an incomplete summary of failures. I had to abort the parallel run because the container(s) ran out of space. I noticed that running parallel in sim mode is actually slower. There are a lot of sync problems when running a real config. This is a starting point to look at the failing tests and see why they fail and if they can be repaired for sim mode or we must drop them in sim mode. The fact that some tests crashed (shared memory not cleaned up) is a sure sign that there are things that need fixing. |
|
The full machine tests fail because the RT Probably, in sim-mode, you need a mutex to forcefully block the motion controller for a moment and allow non-RT to read a consistent data snapshot. For this we need to add a configuration that is interfaced from non-RT to RT. The Similar issues will also be seen with the |
Follow-up to the discussion in #4863. This is the small in-tree part I proposed there: an opt-in switch for uspace
rtapi_appthat lets a simulation run faster than real time. Everything specific to my use case (trace capture, gating of task and I/O) stays out of tree.What it does
With
RTAPI_VIRTUAL_TIME=1in the environment ofrtapi_app:rtapi_get_time()returns the thread start time plus exactly one period per completed cycle, so components see a jitter-free clock,rtapi_appdrops root and all capabilities at startup.Without the variable nothing changes; all new code paths are behind the check.
Limits
-EBUSY. Several threads would need a scheduler that orders them in virtual time; I left that out to keep the change small.hm2_ethover UDP or gpiod). If you would prefer an explicit refusal, I am happy to add one; suggestions for the right place are welcome.Testing
tests/virtual-time: every cycle sees exactly one period ontimedelta, more than 10 s of virtual time pass within 1 s of real time, and a second thread is refused.scripts/runtestsontests/virtual-time,tests/threads.*,tests/rtapi*,tests/hal*,tests/uspace: 35 of 36 pass. The one failure istests/halrmt, which needsnc; it was not installed in my build container, so I have not confirmed the cause.rtapi_appruns with the real uid and empty capability sets, and without it behaves as before.Not tested: RT-PREEMPT, Xenomai and RTAI hosts (the switch is meant to bypass them, but I could only run on a non-realtime kernel), and the documentation build for the new
ENVIRONMENTsection inrtapi_app.1.