HOWTO: Build an RT-application

From RTwiki
(Difference between revisions)
Jump to: navigation, search
(The impact of the Big Kernel Lock)
m (Priority 99)
 
(29 intermediate revisions by 9 users not shown)
Line 1: Line 1:
<p>This document describes the steps to writing hard real time Linux programs while using the real time Preemption Patch.
+
This document describes the steps to writing hard real time Linux programs while using the real time Preemption Patch. It also describes the pitfalls that destroy the real time responsiveness. It focuses on x86 and ARM, although the concepts are also valid on other architectures, as long as Glibc is used. (Some fundamental parts lack in uClibc, like for example PI-mutex support and the control of malloc/new behaviour, so uClibc is not recommended)
It also describes the pitfalls that destroy the real time responsiveness. It focuses on x86 and ARM, although the concepts are also valid on other architectures, as long as Glibc is used. (Some fundamental parts lack in uClibc, like for example PI-mutex support and the control of malloc/new behavior, so uClibc is not recommended)</p>
+
  
==Latencies==
+
==Introduction==
===Hardware causes of ISR latency===
+
An RT-application is only able to operate correctly if the underlying OS and hardware are able to provide the needed determinism. That means a higher priority task can preempt a lower priority task. If for example a BIOS decides to use all CPU cycles for a very long time, no operating system or application can provide any latency guarantees. The whole system needs to be tuned and configured correctly.
  
A good real time behavior of a system depends a lot on low latency interrupt handling.
+
The goal is to reduce (random) latency. This document is divided into for sections which explain how you can reduce latencies (if possibe)
Taking a look at the X86 platform, it shows that this platform is not optimized for RT usage. Several mechanisms cause ISR latencies that can run into the 10's or 100's of microseconds. Knowing them will enable you to make the best design choices on this platform to enable you to work around the negative impact.
+
* [[#Hardware|Hardware]]
* System Management Interrupt (SMI) on Intel x86 ICH chipsets: System Management Interrupts are being generated by the power management hardware on the board. SMI's are evil if real-time is required. First off, they can last for hundreds of microseconds, which for many RT applications causes unacceptable jitter. Second, they are the highest priority interrupt in the system (even higher than the NMI). Third, you can't intercept the SMI because it doesn't have a vector in the CPU. Instead, when the CPU gets an SMI it goes into a special mode and jumps to a hard-wired location in a special SMM address space (which is probably in BIOS ROM). Essentially SMI interrupts are "invisible" to the Operating System. Although SMI interrupts are handled by 1 processor at a time, it even effects real-time responsiveness on dual-core/SMP systems, because if the processor handling the SMI interrupt has locked a mutex or spinlock, which is needed by some other core, that other core has to wait until the SMI interrupt handler has been completed and a mutex/spinlock has been released. This problem also exists on RTAI and other OS-es, see for more info [http://cvs.gna.org/cvsweb/magma/base/arch/i386/calibration/README.SMI?cvsroot=rtai;rev=1.1]
+
* [[#Kernel configuration|Kernel configuration]]
* DMA bus mastering: Bus mastering events can cause long-latency CPU stalls of many microseconds. It can be generated by every device that uses DMA, such as SATA/PATA/SCSI devices and even network adapters. Also video cards that insert wait cycles on the bus in response to a CPU access can cause this kind of latency. Sometimes the behavior of such peripherals can be controlled from the driver, trading off throughput for lower latency. The negative impact of bus mastering is independent from the chosen OS, so this is not a unique problem for Linux-RT, even other RTOS-es experience these type of latency!
+
* [[#Application|Application]]
* On-demand CPU scaling: creates long-latency events when the CPU is put in a low-power-consumption state after a period of inactivity. Such problems are usually quite easy to detect. (e.g. On Fedora the 'cpuspeed' tool should be disabled, as this tool loads the on-demand scaling_governor driver)
+
* [[#Device Drivers|Device drivers]]
* VGA Console: When the system is fulfilling its RT requirements the VGA Text Console must be left untouched. Nothing is allowed to be written to that console, even printk's are not allowed. This VGA text console causes very large latencies, up to more than hundreds of microseconds. It is better to use a serial console and have no login shell on the VGA text console. Also SSH or Telnet sessions can be used. The 'quiet' option on the kernel command line could also be useful to prevent preventing any printk to reach the console. Notice that using a graphical UI of X has no RT-impact, it is just the VGA text console that causes latencies.
+
  
====Hints for getting rid of SMI interrupts on x86====
+
==Hardware==
    1) Use PS/2 mouse and keyboard,
+
A good real time behaviour of a system depends a lot on low latency interrupt handling. Taking a look at the x86 platform, it shows that this platform is not optimised for RT usage. Several mechanisms cause ISR latencies that can run into the 10's or 100's of microseconds. Knowing them will enable you to make the best design choices on this platform to enable you to work around the negative impact.
    2) Disable USB mouse and keyboard in BIOS,
+
 
    3) Compile an ACPI-enabled Kernel.
+
===System Management Interrupt (SMI) on Intel x86 ICH chipsets===
    4) Disable TCO timer generation of SMIs (TCO_EN bit in the SMI_EN register).
+
System Management Interrupts are being generated by the power management hardware on the board. SMI's are evil if real-time is required. First off, they can last for hundreds of microseconds, which for many RT applications causes unacceptable jitter. Second, they are the highest priority interrupt in the system (even higher than the NMI). Third, you can't intercept the SMI because it doesn't have a vector in the CPU. Instead, when the CPU gets an SMI it goes into a special mode and jumps to a hard-wired location in a special SMM address space (which is probably in BIOS ROM). Essentially SMI interrupts are "invisible" to the Operating System. Although SMI interrupts are handled by 1 processor at a time, it even effects real-time responsiveness on dual-core/SMP systems, because if the processor handling the SMI interrupt has locked a mutex or spinlock, which is needed by some other core, that other core has to wait until the SMI interrupt handler has been completed and a mutex/spinlock has been released. This problem also exists on RTAI and other OS-es, see for more info [http://cvs.gna.org/cvsweb/magma/base/arch/i386/calibration/README.SMI?cvsroot=rtai;rev=1.1]
The latency should drop to ~10us permanently, at the expense of not being able to use the i8xx_tco watchdog.
+
 
<BR>One user of RTAI reported: In all cases, do not boot the computer with the USB flash stick plugged in. The latency will raise to 500us if you do so. Connecting and using the USB stick later does no harm, however.
+
{{HINT|Hints for getting rid of SMI interrupts on x86
 +
# Use PS/2 mouse and keyboard,
 +
# Disable USB mouse and keyboard in BIOS,
 +
# Compile an ACPI-enabled Kernel.
 +
# Disable TCO timer generation of SMIs (TCO_EN bit in the SMI_EN register).
 +
}}
  
 
{{WARN|Do not ever disable the SMI interrupts globally. Disabling SMI may cause serious harm to your computer. On P4 systems you can '''burn your CPU to death''', when SMI is disabled. SMIs are also used to fix up chip bugs, so certain components may not work as expected when SMI is disabled. So, be very sure you '''know what you are doing''' before disabling any SMI interrupt. }}
 
{{WARN|Do not ever disable the SMI interrupts globally. Disabling SMI may cause serious harm to your computer. On P4 systems you can '''burn your CPU to death''', when SMI is disabled. SMIs are also used to fix up chip bugs, so certain components may not work as expected when SMI is disabled. So, be very sure you '''know what you are doing''' before disabling any SMI interrupt. }}
 +
 +
===DMA bus mastering===
 +
Bus mastering events can cause long-latency CPU stalls of many microseconds. It can be generated by every device that uses DMA, such as SATA/PATA/SCSI devices and even network adapters. Also video cards that insert wait cycles on the bus in response to a CPU access can cause this kind of latency. Sometimes the behaviour of such peripherals can be controlled from the driver, trading off throughput for lower latency. The negative impact of bus mastering is independent from the chosen OS, so this is not a unique problem for Linux-RT, even other RTOS-es experience these type of latency!
 +
 +
===Power management===
 +
Many BIOS support power management for different hardware types. Obviously, enabling power management saves a few watts on the expensive of latency. Therefore, it is recommended to disable power management options or benchmark the performance of the whole system carefully for each options and their impact on latency.
 +
 +
===Hyper threading===
 +
Hyper threading and also out of order execution of CPUs introduces 'random' latencies. As mentioned in power management, it is recommended to disable these feature (if possible) or carefully benchmark the performance.
 +
 +
===Miscellaneous===
 +
The latency should drop to ~10us permanently, at the expense of not being able to use the i8xx_tco watchdog.
 +
 +
One user of RTAI reported: In all cases, do not boot the computer with the USB flash stick plugged in. The latency will raise to 500us if you do so. Connecting and using the USB stick later does no harm, however.
 +
 +
==Kernel configuration==
 +
 +
===On-demand CPU scaling===
 +
Creates long-latency events when the CPU is put in a low-power-consumption state after a period of inactivity. Such problems are usually quite easy to detect. (e.g. On Fedora the 'cpuspeed' tool should be disabled, as this tool loads the on-demand scaling_governor driver)
 +
 +
===NOHZ===
 +
With this configuration option it is possible to disable the regular timer interrupt send all idle CPUs to deep sleep. Waking up CPUs and maintaining the time on the boot CPU (which never sleeps) is not for free. It adds considerable to latency.
 +
 +
==Application==
 +
 +
===VGA Console===
 +
When the system is fulfilling its RT requirements the VGA Text Console must be left untouched. Nothing is allowed to be written to that console, even printk's are not allowed. This VGA text console causes very large latencies, up to more than hundreds of microseconds. It is better to use a serial console and have no login shell on the VGA text console. Also SSH or Telnet sessions can be used. The 'quiet' option on the kernel command line could also be useful to prevent preventing any printk to reach the console. Notice that using a graphical UI of X has no RT-impact, it is just the VGA text console that causes latencies.
  
 
===Latencies caused by Page-faults===
 
===Latencies caused by Page-faults===
Whenever the RT process runs into a page-fault the kernel freezes the entire process (with all its threads in it), until the kernel has handled the page fault. There are 2 types of pagefaults, major and minor pagefaults. Minor pagefaults are handled without IO accesses. Major pagefaults are pagefaults that are handled by means of IO activity.
+
There are 2 types of page-faults, major and minor pagefaults. Minor pagefaults are handled without IO accesses. Major page-faults are page-faults that are handled by means of IO activity. The Linux page swapping mechanism can swap code pages of an application to disk, it will take a long time to swap those pages back into RAM. If such a page belongs to the realtime process, latencies are hugely increased. Page-faults are therefore dangerous for RT applications and need to be prevented.
Page faults are therefor dangerous for RT applications and need to be prevented.
+
  
If there is no Swap space used and no other applications stress the memory boundaries, then there is enough free RAM ready for the RT application to be used. In this case the RT-application will likely only run into minor pagefaults, which cause relatively small latencies.
+
If there is no Swap space being used and no other applications stress the memory boundaries, then there is probably enough free RAM ready for the RT application to be used. In this case the RT-application will likely only run into minor pagefaults, which cause relatively small latencies. Notice that pagefaults of one application cannot interfere the RT-behavior of another application.
But, if the RT application is just one of the many applications on the system, and there is Swap space used, then special actions has to be taken to protect the memory of the RT-application.
+
If memory has to be retrieved from disk or pushed towards the disk to handle a page fault, the RT-application will experience very large latencies, sometimes up to more than a second! Notice that pagefaults of one application cannot interfere the RT-behavior of another application.
+
  
During startup a RT-application will always experience a lot of pagefaults. These cannot be prevented. In fact, this startup period must be used to claim and lock enough memory for the RT-process in RAM. This must be done in such a way that when the application needs to expose its RT capabilities, pagefaults do not occur anymore.
+
During startup a RT-application will always experience a lot of pagefaults. These cannot be prevented. In fact, this startup period must be used to claim and lock enough memory for the RT-process in RAM. This must be done in such a way that when the application needs to expose its RT capabilities, pagefaults do not occur any more.
  
 
This can be done by taking care of the following during the initial startup phase:
 
This can be done by taking care of the following during the initial startup phase:
 
* Call directly from the main() entry the mlockall() call.
 
* Call directly from the main() entry the mlockall() call.
* Create all threads at startup time of the application. Never start threads dynamically during RT show time, this will ruin RT behavior.
+
* Create all threads at startup time of the application. Never start threads dynamically during RT show time, this will ruin RT behaviour.
 
* Reserve a pool of memory to do new/delete or malloc/free in, if you require dynamic memory allocation.
 
* Reserve a pool of memory to do new/delete or malloc/free in, if you require dynamic memory allocation.
* Never use system calls that are known to generate pagefaults, such as fopen(). (Opening of files does the mmap() system call, which generates a page-fault).
+
* Never use system calls that are known to generate pagefaults, like system calls that allocate memory inside the kernel.
<BR>
+
 
 
There are several examples that show the several aspects of preventing page-faults. It depends on the your requirements which suits best for your purpose.
 
There are several examples that show the several aspects of preventing page-faults. It depends on the your requirements which suits best for your purpose.
 
* [[Simple memory locking example]]: Single threaded application doing a malloc() and make it safe to use.
 
* [[Simple memory locking example]]: Single threaded application doing a malloc() and make it safe to use.
 
* [[Dynamic memory allocation example]]: Same as [[Simple memory locking example]], except it creates a pool of memory to be used for dynamic memory allocation
 
* [[Dynamic memory allocation example]]: Same as [[Simple memory locking example]], except it creates a pool of memory to be used for dynamic memory allocation
 
* [[Threaded RT-application with memory locking and stack handling example]]: Same as [[Dynamic memory allocation example]], but now supports threads.
 
* [[Threaded RT-application with memory locking and stack handling example]]: Same as [[Dynamic memory allocation example]], but now supports threads.
<BR>
+
* mlockall() should be called within the application to prevent the page out of memory for the real time application.
====File handling====
+
 
File handling is known to generate disastrous pagefaults. So, if there is a need for file access from the context of the RT-application, then this can be done best by splitting the application in an RT part and a file-handling part. Both parts are allowed to communicate through sockets. I have never seen a page fault caused by socket traffic.
+
===Global variables and arrays===
Note: While accessing files the low-level fopen() call will do a mmap() to allocate new memory to the process, resulting in a new pagefault.
+
Global variables and arrays are not part of the binary, but are allocated by the OS at process startup. The virtual memory pages associated to this data is not immediately mapped to physical pages of RAM, meaning that page faults occur on access. It turns out that the mlockall() call forces all global variables and arrays into RAM, meaning that subsequent access to this memory does not result in page faults. As such, using global variables and arrays do not introduce any additional problems for real time applications. You can verify this behaviour using the following program (run as 'root' to allow the mlockall() operation)
<BR><BR>
+
 
====Global variables and arrays====
+
Global variables and arrays are not part of the binary, but are allocated by the OS at process startup. The virtual memory pages associated to this data is not immediately mapped to physical pages of RAM, meaning that page faults occur on access. It turns out that the mlockall() call forces all global variables and arrays into RAM, meaning that subsequent access to this memory does not result in page faults. As such, using global variables and arrays does not introduce any additional problems for real time applications. You can verify this behavior using the following program (run as 'root' to allow the mlockall() operation)
+
<BR>
+
 
[[Verifying the absence of page faults in global arrays proof]]
 
[[Verifying the absence of page faults in global arrays proof]]
<BR>
 
<BR>
 
===Priority Inheritance Mutex support===
 
A real-time system '''cannot''' be real-time if there is no solution for priority inversion, this will cause undesired latencies and even deadlocks. (see [http://en.wikipedia.org/wiki/Priority_inversion])
 
<BR>On Linux luckily there is a solution for it in user-land since kernel version 2.6.18 together with Glibc 2.5 (PTHREAD_PRIO_INHERIT).
 
<BR>So, if user-land real-time is important, I highly encourage you to upgrade to at least these 2 versions. Other C-libraries like uClibc do not support PI-futexes at this moment, and are therefor less suitable for realtime!
 
  
Errata for ARM:
+
===[[Priority Inheritance]] Mutex support===
On ARM the slow-path for PI-futexes is first integrated in the RT-patch 2.6.23.rc4-rt1. The patch is however easily back-portable to older kernels (>= 2.6.18) without breaking things. (Just check the file 'include/asm/futex.h' in the kernel code.)
+
A real-time system '''cannot''' be real-time if there is no solution for [[priority inversion]], this will cause undesired latencies and even deadlocks. (see [http://en.wikipedia.org/wiki/Priority_inversion])
The futex slowpath on ARM requires the memory locking scheme as described above. The futex administration is never allowed to be paged out to disk, because the futex-administration memory is accessed with interrupts disabled. This was necessary because the ARM9 v4 and v5 cores do not have the required test-and-set atomic instructions to do it nicely.  
+
This errata is not relevant to X86, because X86 supports the required atomic assembler instructions to do it properly without interrupt locking.
+
<BR><BR>
+
===The impact of the Big Kernel Lock===
+
The Big Kernel Lock (BKL) is preemptible on Preempt-RT. BKL is backed by a mutex (rtmutex) in -rt instead of a regular spinlock. BKL is a special case lock that is released at schedule() then reacquired when the thread is woken up. This is a coarse grained lock that is use to protect the kernel in places that are not thread safe. It has special rules regarding its use.
+
  
Several system calls still use the BKL, so if a RT-thread uses a system call that locks the BKL; it can experience unbounded latencies when the BKL is locked by another thread.
+
On Linux luckily there is a solution for it in user-land since kernel version 2.6.18 together with Glibc 2.5 (PTHREAD_PRIO_INHERIT).  
So, one must know the system calls that use the BKL, and must prevent a RT-thread from using these calls to minimize the latencies.
+
  
For example: The ioctl() handler in a character driver normally uses a BKL-locked variant of the handler, unless it is specified otherwise inside the driver:
+
So, if user-land real-time is important, I highly encourage you to use a recent kernel and Glibc-library. Other C-libraries like uClibc do not support PI-futexes at this moment, and are therefore less suitable for realtime!
  
    static struct file_operations my_fops = {
+
===Priority 99===
        .ioctl          = my_ioctl, /* This line makes my ioctl() a BKL locked variant. */
+
Do '''not''' configure your application to run with priority 99. There are a few management threads which need to run with higher priority then your application, e.g. watchdogs threads.
        .unlocked_ioctl = my_ioctl, /* This version does not use the BKL (Notice that this version requires a slightly different ioctl() argument list) */
+
    };
+
  
==Building Device Drivers==
+
===Userland spin locks===
(This Chapter is under construction)<BR>
+
Do '''not''' implement your own spin locks. Use the priority inheritance futexes.
===Interrupt Handling===
+
The RT-kernel handles all the Interrupt handlers in thread context. However, the real hardware interrupt context is still available. This context can be recognised on the IRQF_NODELAY flag that is assigned to a certain interrupt handler during request_irq() or setup_irq(). Within this context a much more limited kernel API is allowed to be used.
+
====Things you should not do in IRQF_NODELAY context====
+
* Calling any kernel API that uses normal spinlocks. Spinlocks are converted to mutexes on RT, and mutexes can sleep due its nature. (Note: the raw_spinlock_t types behave the same as on a non-RT kernel) Some kernel API's that can block on a spinlock/RT-mutex:
+
** wake_up() shall not be used, use wake_up_process() instead.
+
** up() shall not be used in this context, this is valid for all semaphore types, thus both ''struct compat_semaphore'', as well as ''struct semaphore''. (of course the same is valid for down()...)
+
** complete(): Uses also a normal spinlock which is defined in 'struct __wait_queue_head' in wait.h, thus not safe.
+
  
==Author/Maintainer==
+
===Input/Output===
<p>
+
In general, I/O is dangerous to keep in an RT code path. This is due to the nature of most filesystems and the fact that I/O devices will have to abide to the laws of physics (mechanical movement, voltage adjustments, <whatever an I/O device does to retrieve the magic bits from cold storage>). For this reason, if you have to access I/O in an RT-application, make sure to wrap it securely in a dedicated thread running on a disjoint CPU from the RT-application.
[[User:Remy | Remy Bohmer]]<br>
+
 
</p>
+
===Sharing data between applications===
 +
Use mmap to pass data around.
 +
 
 +
==Device Drivers==
 +
Here are some tips on common pitfalls when writing a device driver.
 +
 
 +
===Interrupt Handling===
 +
The RT-kernel handles all the interrupt handlers in thread context. However, the real hardware interrupt context is still available. This context can be recognised on the IRQF_NODELAY flag that is assigned to a certain interrupt handler during request_irq() or setup_irq(). Within this context a much more limited kernel API is allowed to be used.
  
==Revision==
+
===Things you should not do in IRQF_NODELAY context===
{| border="1" width="100%" summary="Revision history"
+
Calling any kernel API that uses normal spinlocks. Spinlocks are converted to mutexes on RT, and mutexes can sleep due its nature. (Note: the atomic_spinlock_t types behave the same as on a non-RT kernel) Some kernel API's that can block on a spinlock/RT-mutex:
! align="left" valign="top" colspan="2" | <b>Revision History</b>
+
* wake_up() shall not be used, use wake_up_process() instead.
|-  
+
* up() shall not be used in this context, this is valid for all semaphore types, thus both ''struct compat_semaphore'', as well as ''struct semaphore''. (of course the same is valid for down()...)
| align="left" | Revision 7
+
* complete(): Uses also a normal spinlock which is defined in 'struct __wait_queue_head' in wait.h, thus not safe.
| align="left" | 2008-01-29
+
|}
+

Latest revision as of 08:05, 8 January 2014

This document describes the steps to writing hard real time Linux programs while using the real time Preemption Patch. It also describes the pitfalls that destroy the real time responsiveness. It focuses on x86 and ARM, although the concepts are also valid on other architectures, as long as Glibc is used. (Some fundamental parts lack in uClibc, like for example PI-mutex support and the control of malloc/new behaviour, so uClibc is not recommended)

Contents

[edit] Introduction

An RT-application is only able to operate correctly if the underlying OS and hardware are able to provide the needed determinism. That means a higher priority task can preempt a lower priority task. If for example a BIOS decides to use all CPU cycles for a very long time, no operating system or application can provide any latency guarantees. The whole system needs to be tuned and configured correctly.

The goal is to reduce (random) latency. This document is divided into for sections which explain how you can reduce latencies (if possibe)

[edit] Hardware

A good real time behaviour of a system depends a lot on low latency interrupt handling. Taking a look at the x86 platform, it shows that this platform is not optimised for RT usage. Several mechanisms cause ISR latencies that can run into the 10's or 100's of microseconds. Knowing them will enable you to make the best design choices on this platform to enable you to work around the negative impact.

[edit] System Management Interrupt (SMI) on Intel x86 ICH chipsets

System Management Interrupts are being generated by the power management hardware on the board. SMI's are evil if real-time is required. First off, they can last for hundreds of microseconds, which for many RT applications causes unacceptable jitter. Second, they are the highest priority interrupt in the system (even higher than the NMI). Third, you can't intercept the SMI because it doesn't have a vector in the CPU. Instead, when the CPU gets an SMI it goes into a special mode and jumps to a hard-wired location in a special SMM address space (which is probably in BIOS ROM). Essentially SMI interrupts are "invisible" to the Operating System. Although SMI interrupts are handled by 1 processor at a time, it even effects real-time responsiveness on dual-core/SMP systems, because if the processor handling the SMI interrupt has locked a mutex or spinlock, which is needed by some other core, that other core has to wait until the SMI interrupt handler has been completed and a mutex/spinlock has been released. This problem also exists on RTAI and other OS-es, see for more info [1]

Hint:
Hints for getting rid of SMI interrupts on x86
  1. Use PS/2 mouse and keyboard,
  2. Disable USB mouse and keyboard in BIOS,
  3. Compile an ACPI-enabled Kernel.
  4. Disable TCO timer generation of SMIs (TCO_EN bit in the SMI_EN register).
ATTENTION!
Do not ever disable the SMI interrupts globally. Disabling SMI may cause serious harm to your computer. On P4 systems you can burn your CPU to death, when SMI is disabled. SMIs are also used to fix up chip bugs, so certain components may not work as expected when SMI is disabled. So, be very sure you know what you are doing before disabling any SMI interrupt.

[edit] DMA bus mastering

Bus mastering events can cause long-latency CPU stalls of many microseconds. It can be generated by every device that uses DMA, such as SATA/PATA/SCSI devices and even network adapters. Also video cards that insert wait cycles on the bus in response to a CPU access can cause this kind of latency. Sometimes the behaviour of such peripherals can be controlled from the driver, trading off throughput for lower latency. The negative impact of bus mastering is independent from the chosen OS, so this is not a unique problem for Linux-RT, even other RTOS-es experience these type of latency!

[edit] Power management

Many BIOS support power management for different hardware types. Obviously, enabling power management saves a few watts on the expensive of latency. Therefore, it is recommended to disable power management options or benchmark the performance of the whole system carefully for each options and their impact on latency.

[edit] Hyper threading

Hyper threading and also out of order execution of CPUs introduces 'random' latencies. As mentioned in power management, it is recommended to disable these feature (if possible) or carefully benchmark the performance.

[edit] Miscellaneous

The latency should drop to ~10us permanently, at the expense of not being able to use the i8xx_tco watchdog.

One user of RTAI reported: In all cases, do not boot the computer with the USB flash stick plugged in. The latency will raise to 500us if you do so. Connecting and using the USB stick later does no harm, however.

[edit] Kernel configuration

[edit] On-demand CPU scaling

Creates long-latency events when the CPU is put in a low-power-consumption state after a period of inactivity. Such problems are usually quite easy to detect. (e.g. On Fedora the 'cpuspeed' tool should be disabled, as this tool loads the on-demand scaling_governor driver)

[edit] NOHZ

With this configuration option it is possible to disable the regular timer interrupt send all idle CPUs to deep sleep. Waking up CPUs and maintaining the time on the boot CPU (which never sleeps) is not for free. It adds considerable to latency.

[edit] Application

[edit] VGA Console

When the system is fulfilling its RT requirements the VGA Text Console must be left untouched. Nothing is allowed to be written to that console, even printk's are not allowed. This VGA text console causes very large latencies, up to more than hundreds of microseconds. It is better to use a serial console and have no login shell on the VGA text console. Also SSH or Telnet sessions can be used. The 'quiet' option on the kernel command line could also be useful to prevent preventing any printk to reach the console. Notice that using a graphical UI of X has no RT-impact, it is just the VGA text console that causes latencies.

[edit] Latencies caused by Page-faults

There are 2 types of page-faults, major and minor pagefaults. Minor pagefaults are handled without IO accesses. Major page-faults are page-faults that are handled by means of IO activity. The Linux page swapping mechanism can swap code pages of an application to disk, it will take a long time to swap those pages back into RAM. If such a page belongs to the realtime process, latencies are hugely increased. Page-faults are therefore dangerous for RT applications and need to be prevented.

If there is no Swap space being used and no other applications stress the memory boundaries, then there is probably enough free RAM ready for the RT application to be used. In this case the RT-application will likely only run into minor pagefaults, which cause relatively small latencies. Notice that pagefaults of one application cannot interfere the RT-behavior of another application.

During startup a RT-application will always experience a lot of pagefaults. These cannot be prevented. In fact, this startup period must be used to claim and lock enough memory for the RT-process in RAM. This must be done in such a way that when the application needs to expose its RT capabilities, pagefaults do not occur any more.

This can be done by taking care of the following during the initial startup phase:

  • Call directly from the main() entry the mlockall() call.
  • Create all threads at startup time of the application. Never start threads dynamically during RT show time, this will ruin RT behaviour.
  • Reserve a pool of memory to do new/delete or malloc/free in, if you require dynamic memory allocation.
  • Never use system calls that are known to generate pagefaults, like system calls that allocate memory inside the kernel.

There are several examples that show the several aspects of preventing page-faults. It depends on the your requirements which suits best for your purpose.

[edit] Global variables and arrays

Global variables and arrays are not part of the binary, but are allocated by the OS at process startup. The virtual memory pages associated to this data is not immediately mapped to physical pages of RAM, meaning that page faults occur on access. It turns out that the mlockall() call forces all global variables and arrays into RAM, meaning that subsequent access to this memory does not result in page faults. As such, using global variables and arrays do not introduce any additional problems for real time applications. You can verify this behaviour using the following program (run as 'root' to allow the mlockall() operation)

Verifying the absence of page faults in global arrays proof

[edit] Priority Inheritance Mutex support

A real-time system cannot be real-time if there is no solution for priority inversion, this will cause undesired latencies and even deadlocks. (see [2])

On Linux luckily there is a solution for it in user-land since kernel version 2.6.18 together with Glibc 2.5 (PTHREAD_PRIO_INHERIT).

So, if user-land real-time is important, I highly encourage you to use a recent kernel and Glibc-library. Other C-libraries like uClibc do not support PI-futexes at this moment, and are therefore less suitable for realtime!

[edit] Priority 99

Do not configure your application to run with priority 99. There are a few management threads which need to run with higher priority then your application, e.g. watchdogs threads.

[edit] Userland spin locks

Do not implement your own spin locks. Use the priority inheritance futexes.

[edit] Input/Output

In general, I/O is dangerous to keep in an RT code path. This is due to the nature of most filesystems and the fact that I/O devices will have to abide to the laws of physics (mechanical movement, voltage adjustments, <whatever an I/O device does to retrieve the magic bits from cold storage>). For this reason, if you have to access I/O in an RT-application, make sure to wrap it securely in a dedicated thread running on a disjoint CPU from the RT-application.

[edit] Sharing data between applications

Use mmap to pass data around.

[edit] Device Drivers

Here are some tips on common pitfalls when writing a device driver.

[edit] Interrupt Handling

The RT-kernel handles all the interrupt handlers in thread context. However, the real hardware interrupt context is still available. This context can be recognised on the IRQF_NODELAY flag that is assigned to a certain interrupt handler during request_irq() or setup_irq(). Within this context a much more limited kernel API is allowed to be used.

[edit] Things you should not do in IRQF_NODELAY context

Calling any kernel API that uses normal spinlocks. Spinlocks are converted to mutexes on RT, and mutexes can sleep due its nature. (Note: the atomic_spinlock_t types behave the same as on a non-RT kernel) Some kernel API's that can block on a spinlock/RT-mutex:

  • wake_up() shall not be used, use wake_up_process() instead.
  • up() shall not be used in this context, this is valid for all semaphore types, thus both struct compat_semaphore, as well as struct semaphore. (of course the same is valid for down()...)
  • complete(): Uses also a normal spinlock which is defined in 'struct __wait_queue_head' in wait.h, thus not safe.
Personal tools