|
RTAI-Comedi PluginsPlugins for dynamic clamp based on RTAI and comedi.
RTAI-Comedi PluginsPlugins for dynamic clamp based on RTAI and comedi. Content
IntroductionFor using dynamic clamp, i.e. some realtime computation, as it is implemented by the DynClampAnalogInput and DynClampAnalogOutput plugins, you need to install an RTAI-patched real-time linux kernel, RTAI kernel modules, and comedi device drivers. RELACS provides a script that helps you in the installation process. This is described in the following section Install an RTAI-patched linux kernel, RTAI, and comedi. The script should work on debian based systems, but is has not been tested on other Linux distributions. Alternatively, you can do the installation manually as explained in Manual installation of an RTAI-patched linux kernel . In either case, continue with building the dynamic clamp module of RELACS as explained in Setting up dynamic clamp for RELACS and configuring RELACS as explained in Configure dynamic clamp in RELACS . If you only want to simulate a dynamic clamp, then skip all installation procedures and just read Dynamic clamp simulation . Install an RTAI-patched linux kernel, RTAI, and comediThe The script is so far only used and tested on debian based systems and is likely to fail on other Linux distributions. Feel free to adapt the script to your Linux distribution. PreparationsWhen you use
For further options of the ./makertaikernel.sh help
For example, to show some information about your machine, RTAI patches, grub menue, etc. run ./makertaikernel.sh info
Basic kernel configurationFor making the Linux kernel work with RTAI you should check the following settings in the kernel configuration dialog. This list is updated for RTAI 5.1. For other RTAI version read
Leave the configuration dialog by pressing "Exit" until you are asked "Save kernel config?". Select "Yes". Then the new kernel is being compiled - be patient. Testing the RTAI-patched kernelTesting your RTAI patched kernel is crucial for a good real-time performance! The See ./makertaikernel.sh help test
for a summary of test options that are described in more detail in the following sections. The script will first ask for a short description of your kernel configuration and parameter. This string is then used for naming the files for the test results.
Test resultsThe result of the tests are written into files named View the test results with a text editor or with less -S latencies-4.4.115-rtai-5.1-aeshna-004-2018-03-29-regularnohz-good
Along with the test results the configuration of the tested kenel is saved in the sudo ./makertaikernel.sh -c <config-file> reconfigure
(simply leave the kernel menu without changing anything). The first view lines in a Test summary (in nanoseconds):
RTH| general | kern latencies | kern switches | kern preempt | kthreads latencies | kthreads switches | kthreads preempt | user latencies | user switches | user preempt | kernel
RTH| description | progress| ovlmax| avgmax| std| n| maxover| susp| sem| rpc| max| jitfast| jitslow| ovlmax| avgmax| std| n| maxover| susp| sem| rpc| max| jitfast| jitslow| ovlmax| avgmax| std| n| maxover| susp| sem| rpc| max| jitfast| jitslow| configuration
RTD| smp4smtmulticore-idle-highres-plain | hsmk | 17156| 2838| 824| 599| 0| 217| 240| 284| 2482| 5852| 5491| -| -| -| -| -| -| -| -| -| -| -| -| -| -| -| -| -| -| -| -| -| -| config-4.4.115-rtai-5.1-aeshna-025-2018-04-17-smp4smtmulticore-idle-highres-plain-ok
This is followed by a summary of the load that was applied during the tests: Load: 8.03 7.34 4.25
cpu: stress -c 2
io : stress --hdd-bytes 128M -d 2
mem: stress -m 2
net: ping -f localhost
Then the output of the RTAI tests is listed. This is followed by a list of loaded modules loaded modules (lsmod):
Module Size Used by
btrfs 819200 0
xor 24576 1 btrfs
raid6_pq 102400 1 btrfs
ufs 69632 0
...
and the output of interrupts (/proc/interrupts):
CPU0 CPU1 CPU2 CPU3
0: 26 0 0 0 IO-APIC 2-edge timer
1: 10 0 0 0 IO-APIC 1-edge i8042
8: 1 0 0 0 IO-APIC 8-edge rtc0
9: 169 0 0 0 IO-APIC 9-fasteoi acpi
12: 1949 0 0 0 IO-APIC 12-edge i8042
16: 254 0 0 0 IO-APIC 16-fasteoi ehci_hcd:usb1, mmc0
19: 13 0 0 0 IO-APIC 19-fasteoi
23: 34 0 0 0 IO-APIC 23-fasteoi ehci_hcd:usb2
24: 0 0 0 0 PCI-MSI 327680-edge xhci_hcd
25: 7 0 397 0 PCI-MSI 409600-edge eth0
26: 21558 0 919 0 PCI-MSI 512000-edge 0000:00:1f.2
27: 74 0 0 0 PCI-MSI 32768-edge i915
28: 25 0 0 0 PCI-MSI 360448-edge mei_me
29: 371 0 0 0 PCI-MSI 442368-edge snd_hda_intel
30: 28553 0 0 0 PCI-MSI 1572864-edge iwlwifi
NMI: 0 0 0 0 Non-maskable interrupts
LOC: 685217 666148 642070 652469 Local timer interrupts
SPU: 0 0 0 0 Spurious interrupts
...
Ideally you do not want any interrupt on the CPU where the RTAI test runs. When playing around with NO_HZ look at the Then various information about the system, the CPU, the grub menu, and the settings used by Finally the output of Test reportsYou can generate a summary report from all your tested kernels by means of ./makertaikernel.sh report | less -S # report of all latencies-* files
./makertaikernel.sh report tests/latencies-* | less -S # report of all latencies-* files in tests/
Interpreting test resultsRead Latency testsThe first test that is run is the latency test. The output looks like this: RTAI Testsuite - KERNEL latency (all data in nanoseconds)
RTH| lat min| ovl min| lat avg| lat max| ovl max| overruns
RTD| 60| 60| 141| 1040| 1040| 0
RTD| 61| 60| 126| 372| 1040| 0
RTD| 66| 60| 127| 364| 1040| 0
RTD| 66| 60| 128| 748| 1040| 0
RTD| 92| 60| 129| 719| 1040| 0
In order to reduce latency jitter you need to improve your kernel configuration (see Improve the RTAI-patched kernel). Switch testThen the switch test is run: The output looks like this: Apr 7 16:08:45 knifefish kernel: [ 178.940333]
Apr 7 16:08:45 knifefish kernel: [ 178.940333] Wait for it ...
Apr 7 16:08:45 knifefish kernel: [ 178.959986]
Apr 7 16:08:45 knifefish kernel: [ 178.959986]
Apr 7 16:08:45 knifefish kernel: [ 178.959986] FOR 10 TASKS: TIME 5 (ms), SUSP/RES SWITCHES 40000, SWITCH TIME (INCLUDING FULL FP SUPPORT) 145 (ns)
Apr 7 16:08:45 knifefish kernel: [ 178.959988]
Apr 7 16:08:45 knifefish kernel: [ 178.959988] FOR 10 TASKS: TIME 6 (ms), SEM SIG/WAIT SWITCHES 40000, SWITCH TIME (INCLUDING FULL FP SUPPORT) 158 (ns)
Apr 7 16:08:45 knifefish kernel: [ 178.959990]
Apr 7 16:08:45 knifefish kernel: [ 178.959990] FOR 10 TASKS: TIME 7 (ms), RPC/RCV-RET SWITCHES 40000, SWITCH TIME (INCLUDING FULL FP SUPPORT) 186 (ns)
The reported switching times should be well below 1000ns. Preemption testFinally the preempt test is run. Run tests under loadTo really check out the performance of the kernel you should run the tests under heavy load. This can be easily controlled by adding one or several of the following keywords to the test options:
For example ./makertaikernel.sh test cpu net
will run the kern tests with cpu and network load. Run tests on a specific CPU coreIn particular when checking the ./makertaikernel.sh test cpu=2
will run the kern tests on the third CPU. Automatized testingFor automatic termination of the tests (no sudo ./makertaikernel.sh test 60 # run the kern latency test for 60 seconds
For preventing any user interaction you can also provide the test description after the "auto" keyword (here "basic"): sudo ./makertaikernel.sh test 60 auto basic
Test batchesFor a completely automized series of tests of various kernel parameters and kernel configurations under different loads you can prepare a file with the necessary instructions (see below) and pass it to the sript with the sudo ./makertaikernel.sh test batch <test-batch-file>
This will successively reboot into the RTAI kernel with the kernel parameter set to the ones specified by the KERNEL_PARAM variable and as specified in <test-batch-file>, and runs the tests as specified by the previous commands (without the "auto" command). For example, sudo ./makertaikernel.sh test sched kern kthreads 240 batch <test-batch-file>
would test loading of the Special lines in <test-batch-file> cause reboots into the default kernel and building an RTAI-patched kernel with a new configuration. Format of test-batch filesIn a batch file
Example batch file: lowlatency : CONFIG :
idlenohz : : idle=poll nohz=off
nodyntics : CONFIG : config-nodyntics
idleisol : cpu io : idle=poll isolcpus=0,1
isol2 : io cpu=2 : isolcpus=2
Use makertaikernel.sh prepare
to quickly generate various kernel configurations that you can use in a batch file.
If you reboot or restart your computer during a running test batch (because a test hangs), the test batch stops itself automatically. If you want to abort a running test batch then log in and run sudo killall makertaikernel.sh # to kill the already scheduled reboot
makertaikernel.sh restore testbatch # to really stop the test batch
Improve the RTAI-patched kernelIf your test results are not satisfactory, then you need to pass kernel parameters to the RTAI kernel or reconfigure the kernel, probably disabling some devices, compile and install the kernel again, and compile and install rtai again. The main culprits are power saving modes, frequency scaling, interrupts, some devices and their drivers. Which ones are bad usually depends on your specific machine. Read the file How to modify your systemThere are four levels, at which you can modify your system:
BIOS settings will affect all the kernels you run on your computer. Better is if you can achieve the same effect via a kernel configuration that will be specific for your kernel. But for this you need to compile a new kernel. Even better is if there is a kernel parameter that allows you to set the configuration at boot time. This only requires a reboot. Use these three options to find a configuration with the lowest latency jitters. Some configurations can be achieved even in the running kernel. For conveniently using them one should write a littel wrapper around the real time application (e.g. RELACS) the sets these parameters only when running the application, maybe even specific for the CPU on which the real-time task runs. Simply check your BIOS whether there is anything of interest (e.g. hyperthreading, powersaving, force all fans to run at full speed) that you might want to try. Modify the kernel configurationThe kernel configuration is changed in the menu that you get when building a new kernel. That is when running sudo ./makertaikernel.sh reconfigure
or sudo ./makertaikernel.sh prepare
for generating kernel configurations to be used in a test batch. If you do not want to override your RTAI-patched kernel when reconfiguring you can give it a new name via the sudo ./makertaikernel.sh -n 2 reconfigure
sudo ./makertaikernel.sh -n 2 reboot
...
sudo ./makertaikernel.sh -n 2 test
Note that You can recreate a kernel or use this kernel's configuration for further modifications by specifying the kernel configuration file saved by sudo ./makertaikernel.sh -c config-3.14.17-rtai-4.1-002-basic-good reconfigure
The Set kernel parameterKernel parameter can be passed directly to the reboot command: ./makertaikernel.sh reboot param1=xxx param2=yyy
boots the RTAI-patched kernel and passes the kernel parameter "param1=xxx param2=yyy" to it. See ./makertaikernel.sh help reboot
for further options and details. Kernel parameter that you want to set all the time while testing can be specified via the For applying the kernel parameter permanently add them to the There are several interesting kernel parameter that influence the real-time performance. See the file The following is a collection of hints on influential configuration parameter from all levels. Disable CPU power saving modesMost importantly, for CPUs like Intel i3/i5/i7 CPUs, disabling CPU power saving modes improves real-time performance dramatically. Before you try anything else do:
Hyperthreading
So, check whether you have hyperthreading. Run ./makertaikernel.sh info cpus
The top of the output looks like this (Intel i7-4770): CPU topology, frequencies, and idle states (/sys/devices/system/cpu/*):
cpu topology cpu frequency scaling
logical socket core online freq/MHz governor
cpu0 0 0 1 0.800 ondemand
cpu1 0 1 1 0.800 ondemand
cpu2 0 2 1 0.800 ondemand
cpu3 0 3 1 0.800 ondemand
cpu4 0 0 1 0.800 ondemand
cpu5 0 1 1 1.000 ondemand
cpu6 0 2 1 1.200 ondemand
cpu7 0 3 1 1.400 ondemand
If in the "core_id" column the numbers appear twice or more often, then you run in hyperthreading mode (as is the case in the example). By reducing the number of CPUs to four, you can eliminate hyperthreading (see below). If the topology looks like this (Intel i7-3520M): CPU topology, frequencies, and idle states (/sys/devices/system/cpu/*):
cpu topology cpu frequency scaling
logical socket core online freq/MHz governor
cpu0 0 0 1 2.700 ondemand
cpu1 0 0 1 2.901 ondemand
cpu2 0 1 1 2.901 ondemand
cpu3 0 1 1 2.901 ondemand
the trick to simply reduce the number of cpus to the number of actual cores does not work. You need to switch off hyperthreading in the BIOS. This is an example of a machine without hyperthreading (Intel i5-3570): CPU topology, frequencies, and idle states (/sys/devices/system/cpu/*):
cpu topology cpu frequency scaling
logical socket core online freq/MHz governor
cpu0 0 0 1 3.400 powersave
cpu1 0 1 1 3.144 powersave
cpu2 0 2 1 2.863 powersave
cpu3 0 3 1 3.260 powersave
In this case you do not need to do anything. This is what you need to do when configuring the kernel:
You can also do this via the kernel parameter:
Switch off individual CPUs: echo 0 > /sys/devices/system/cpu/cpuX/online
This, however, crashes when inserting rtai_hal (even if CONFIG_RTAI_CPUS is adapted to the lower CPU count). For more information see:
The related kernel configs in the "Processor type and features" submenu
do not seem to matter. Cached memory disruption
??? CPU power management and frequency scaling
See ./makertaikernel.sh info cpu
for information on frequency scaling and idle states of your CPUs. The output might look like this: CPU topology, frequencies, and idle states (/sys/devices/system/cpu/*):
CPU topology CPU frequency scaling CPU idle states (enabled fraction%)
logical socket core online freq/MHz governor transitions POLL C1-IVB C1E-IVB C3-IVB C6-IVB C7-IVB
cpu0 0 0 1 2.901 ondemand 278047 0 0.0% 0 3.5% 0 6.9% 0 5.8% 0 0.0% 0 83.6%
cpu1 0 0 1 1.800 ondemand 303763 0 0.0% 0 6.1% 0 7.2% 0 3.6% 0 0.0% 0 82.9%
cpu2 0 1 1 1.200 ondemand 251421 0 0.0% 0 3.5% 0 6.6% 0 4.3% 0 0.0% 0 85.4%
cpu3 0 1 1 1.800 ondemand 297520 0 0.0% 0 6.1% 0 7.1% 0 3.6% 0 0.0% 0 83.1%
...
CPU (/proc/cpuinfo):
model name : Intel(R) Core(TM) i7-3520M CPU @ 2.90GHz
number of CPUs : 4
max CPU frequency : 2.901 MHz
CPU family : 6
machine (uname -m): x86_64
memory (free -h) : 7.5G RAM
Here, CPU frequency scaling is active ("ondemand" governor and different frequencies in the 5-th column, that are below the maximum possible CPU frequency at "max CPU frequency" below). Also, CPU powersaving is enabled because there are "CPU idle states" available ("POLL", "C1-IVB", etc.). You are on the safe side when you configure the ACPI properties of your kernel as follows (as you did according to Basic kernel configuration): In "Power management and ACPI options":
Instead you can try to only disable in "Power management and ACPI options":
In addition, make sure your CPU stays in C0 idle state by passing Try also disabling CPU power management in the BIOS. All the other kernel parameter that control CPU idle states are usually not sufficient:
C-states can be nicely monitored with the In "Processor type and features":
Check frequency scaling of CPUs with With ./makertaikernel.sh test batch cstates
you can generate a test-batch file to check these kernel parameter. For more information on CPU power management and frequency scaling read in the
More infos:
Select processor family
Check your processor by running ./makertaikernel.sh info cpus
The last part looks like this: CPU (/proc/cpuinfo):
model name : Intel(R) Core(TM) i7-3520M CPU @ 2.90GHz
number of cpus: 4
cpu family: 6
cpuidle driver: intel_idle
machine (uname -m): x86_64
memory (free -h) : 7.5G RAM
Select your processor in the kernel configuration:
In the kernel menu check Low-latency kernel configuration
This has no effect on the RTAI performance! Disable device drivers you do not needA good strategy is to disable as many as possible device drivers. See lsmod
for listing all the currently loaded kernel modules. Or lsmod -k
for a list of PCI devices on your system and their associated kernel modules. When configuring the kernel you can hit '/', enter a search term (the module name) and you get a list of matching configuration parameters. More information on how select/deselect device drivers for RTAI kernel can be found at Devices to consider are:
Some more hints for kernel configuration parametersHere is a list of some kernel configuration parameter that you might try to improve your real-time perfomance (low latencies):
Kernel parameterHere is a list of potentially influential kernel parameter: Clocks and timers:
You get these kernel parameters for a test batch file by running ./makertaikernel.sh test batch clocks
Advanced configuration and power interface (ACPI):
With disabled acpi your rtai-patched linux kernel might not properly halt or reboot. Try You get these kernel parameters for a test batch file by running ./makertaikernel.sh test batch acpi
Advanced programmable interrupt controller (APIC):
You get these kernel parameters for a test batch file by running ./makertaikernel.sh test batch apic
Others:
Isolate CPUs for real time tasksFor further improving the RTAI performance, you might want to reserve at least one CPU for RTAI on a multi-core machine. You can isolate CPUs by using the kernel parameter isolcpus=2,3
(for isolating the cores no. 2 and 3). Additional parameters are isolcpus=2,3 nohz_full=2,3 rcu_nocbs=2,3
See the file You should check each of your CPUs. In particular running and isolating an RTAI task on the first CPU may result in worse perfromance than on the other CPUs. In addition to isolating a CPU you also need to make sure that the RTAI tests are run on that CPU. For this you need to modify the test sources. With the script this can be easily achieved by passing the sudo ./makertaikernel.sh test cpu=2
will run the tests on CPU 2 (the third CPU). Running RTAI on isolated CPUs may reduce maximum jitter on an idle machine. Under load, isolation can improve mean and maximum latencies considerably. You get these kernel parameters for a test batch file by running ./makertaikernel.sh test batch isolcpus
It will test RTAI performance for each of your CPUs. Edit the file to adapt it to the number of CPUs you have on your machine.
Disable SMI interruptsFinally, there are the evil SMIs. They periodically produce some long latencies. See Setting up dynamic clamp for RELACSYou should install
Configure dynamic clamp in RELACSJust make sure your *Analog Input Devices
Device1:
plugin: [ DynClampAnalogInput, DynClampAISim ]
device: /dev/comedi0
ident : ai-1
*Analog Output Devices
Device1:
plugin: [ DynClampAnalogOutput, DynClampAOSim ]
device: /dev/comedi0
ident : ao-1
Input tracesAs usual, input traces can be assigned to channels of the analog input device. All the input traces the dynamic clamp model requires (its The dynamic clamp plugins provide some additional input traces. They can be accessed like normal analog input traces, but the channel nummber needs to be set to 1000 or larger. The following three types of additional inputs are supported:
Output tracesAs usual, output traces can be assigned to channels of the analog output device. All the output traces the dynamic clamp model requires (its Additional output traces as defined by the dynamic clamp model via its Dynamic clamp simulationYou can run the dynamic clamp in the relacs simulation mode as well. This is in particular handy when it comes to testing your The relacs::DynClampAISim and relacs::DynClampAOSim modules do the trick. Whenever you change cd plugins/linuxdevices/rtaicomedi
make
Then call ./relacslocal -3
and enjoy the simulation. |