EMC runs on Linux using real time extensions.
Realtime extensions allow processes to guarantee strict timing requirements, often down to the tens of microseconds.
Support currently exists for version 2.2, 2.4 and 2.6 Linux kernels with real time extensions applied by RT-Linux or RTAI patches.
Because EMC2 needs real time extensions it cannot be run with the standard kernel supplied by most Linux distributions. However, a properly patched kernel will be installed automatically when the "emc2" .deb package is installed, greatly simplifying this task for the end user. Previously this requirement meant you needed a custom Linux distribution, but now emc2 can be installed on most debian-based distributions.
Why RTLinux
(stolen shamelessly from the [[RTlinux HOWTO
http://www.faqs.org/docs/Linux-HOWTO/RTLinux-HOWTO.html]]
The reasons for the design of RTLinux can be understood by examining the working of the standard Linux kernel. The Linux kernel separates the hardware from the user-level tasks. The kernel uses scheduling algorithms and assigns priority to each task for providing good average performances or throughput. Thus the kernel has the ability to suspend any user-level task, once that task has outrun the time-slice allotted to it by the CPU. This scheduling algorithms along with device drivers, uninterruptible system calls, the use of interrupt disabling and virtual memory operations are sources of unpredictability. That is to say, these sources cause hindrance to the realtime performance of a task.
You might already be familiar with the non-realtime performance, say, when you are listening to the music played using 'mpg123' or any other player. After executing this process for a pre-determined time-slice, the standard Linux kernel could preempt the task and give the CPU to another one (e.g. one that boots up the X server or Netscape). Consequently, the continuity of the music is lost. Thus, in trying to ensure fair distribution of CPU time among all processes, the kernel can prevent other events from occurring.
A realtime kernel should be able to guarantee the timing requirements of the processes under it. The RTLinux kernel accomplishes realtime performances by removing such sources of unpredictability as discussed above. We can consider the RTLinux kernel as sitting between the standard Linux kernel and the hardware. The Linux kernel sees the realtime layer as the actual hardware. Now, the user can both introduce and set priorities to each and every task. The user can achieve correct timing for the processes by deciding on the scheduling algorithms, priorities, frequency of execution etc. The RTLinux kernel assigns lowest priority to the standard Linux kernel. Thus the user-task will be executed in realtime.
The actual realtime performance is obtained by intercepting all hardware interrupts. Only for those interrupts that are related to the RTLinux, the appropriate interrupt service routine is run. All other interrupts are held and passed to the Linux kernel as software interrupts when the RTLinux kernel is idle and then the standard Linux kernel runs. The RTLinux executive is itself nonpreemptible.
Realtime tasks are privileged (that is, they have direct access to hardware), and they do not use virtual memory. Realtime tasks are written as special Linux modules that can be dynamically loaded into memory. The initialization code for a realtime tasks initializes the realtime task structure and informs RTLinux kernel of its deadline, period, and release-time constraints.
RTLinux co-exists along with the Linux kernel since it leaves the Linux kernel untouched. Via a set of relatively simple modifications, it manages to convert the existing Linux kernel into a hard realtime environment without hindering future Linux development.
How to improve RealTime performance
First of all, see
TroubleShooting.
- disable hyperthreading. It's rare for HT to help at all these days, and for many workloads it's a net loss.
- try restricting Linux from using one processor core
- You do this by adding a kernel boot parameter to GRUB: "isolcpus=1" (or "isolcpus=2,3" if you leave HT on) This will prevent Linux from scheduling processes on the second core. If you have compiled RTAPI against SMP kernel headers, then RTAPI will automatically use the highest numbered CPU for realtime tasks.
- If you do this, and don't see much improvement, then here's one more thing to try. You'll need two terminals open for this. Run latency-test in one terminal, and in the other, enter the following line, which will run forever (until you press ctrl-C):
while true ; do echo "nothing" > /dev/null ; done
- In my experience, the "cpu hog" is able to reduce latencies from 10-20 microseconds down to perhaps 5-7uS.
- jmkasunich theorizes that the hog uses very little memory, and since it keeps one CPU busy, that CPU never runs any other code. So the RT code doesn't get flushed out of cache, and doesn't have to get fetched back into cache later.
I saw some other cache related behavior a long time ago when doing some
latency testing. The latency results improved noticeably when I lowered
the thread period below some threshold. (I don't remember the threshold
period, it was at least a year ago.)
I eventually realized that when I was running the thread very
frequently, the RT code never got pushed out of cache. When I increased
the period, other processes had enough time to replace the RT code in
cache between invocations of the thread.