Book Image

Linux: Embedded Development

By : Alexandru Vaduva, Alex Gonzalez, Chris Simmonds
Book Image

Linux: Embedded Development

By: Alexandru Vaduva, Alex Gonzalez, Chris Simmonds

Overview of this book

Embedded Linux is a complete Linux distribution employed to operate embedded devices such as smartphones, tablets, PDAs, set-top boxes, and many more. An example of an embedded Linux distribution is Android, developed by Google. This learning path starts with the module Learning Embedded Linux Using the Yocto Project. It introduces embedded Linux software and hardware architecture and presents information about the bootloader. You will go through Linux kernel features and source code and get an overview of the Yocto Project components available. The next module Embedded Linux Projects Using Yocto Project Cookbook takes you through the installation of a professional embedded Yocto setup, then advises you on best practices. Finally, it explains how to quickly get hands-on with the Freescale ARM ecosystem and community layer using the affordable and open source Wandboard embedded board. Moving ahead, the final module Mastering Embedded Linux Programming takes you through the product cycle and gives you an in-depth description of the components and options that are available at each stage. You will see how functions are split between processes and the usage of POSIX threads. By the end of this learning path, your capabilities will be enhanced to create robust and versatile embedded projects. This Learning Path combines some of the best that Packt has to offer in one complete, curated package. It includes content from the following Packt products: ? Learning Embedded Linux Using the Yocto Project by Alexandru Vaduva ? Embedded Linux Projects Using Yocto Project Cookbook by Alex González ? Mastering Embedded Linux Programming by Chris Simmonds
Table of Contents (6 chapters)

In this chapter, you will be presented with information on the real-time component of the Yocto Project. Also, in the same context, a short discussion regarding the general purpose of an operating system and a real-time operating system will be explained. We will then move toward the PREEMPT_RT patches that try to change normal Linux into a full powered real-time operating system; we will try to look at it from more angles and at the end, sum it up and draw a conclusion out of it. This is not all, any real-time operation needs its applications, so a short presentation on the do's and don'ts of application writing that is suitable in the context of a real-time operating system, will also be presented. Keeping all of this in mind, I believe it's time to proceed with this chapter content; I hope you enjoy it.

You will find a more detailed explanation of real-time components in this chapter. Also, the relation between Linux and real-time will be shown to you. As everyone knows already, the Linux operation system was designed as a general purpose OS very similar to the already available UNIX. It is very easy to see the fact that a multiuser system, such as Linux, and a real-time one are somewhat in conflict. The main reason for this is that for a general purpose, multiple user operating systems, such as Linux, are configured to obtain a maximal average throughput. This sacrifices latencies that offer exactly the opposite requirements for a real-time operating system.

The definition for real time is fairly easy to understand. The main idea behind it in computing is that a computer or any embedded device is able to offer feedback to its environment in time. This is very different from being fast; it is, in fact, fast enough in the context of a system and fast enough is different for the automobile industry or nuclear power plants. Also, this kind of a system will offer reliable responses to take decisions that don't not affect any exterior system. For example, in a nuclear power plant, it should detect and prevent any abnormal conditions to ensure that a catastrophe is avoided.

When Linux is mentioned, usually General Purpose Operating System (GPOS) is related to it, but over time, the need to have the same benefits as Real-Time Operating System (RTOS) for Linux has become more stringent. The challenge for any real-time system is to meet the given timing constrains in spite of the number and type of random asynchronous events. This is no simple task and an extensive number of papers and researches were done on theory of the real-time systems. Another challenge for a real-time system would be to have an upper limit on latency, called a scheduling deadline. Depending on how systems meet this challenge, they can be split into hard, firm, and soft:

There are multiple reasons for Linux not being suitable as a RTOS:

All the preceding characteristics constitute the reason why an upper boundary cannot be applied to the latency of a task or process, and also why Linux cannot become a hard real-time operating system. Let's take a look at the following diagram which illustrates the approaches of Linux OS to offer real-time characteristics:

Understanding GPOS and RTOS

The first thing anyone can do to improve the latency of the standard Linux operating system would be to try and make a change to the scheduling policies. The default Linux time sharing scheduling policies are called SCHED_OTHER, and they use a fairness algorithm, giving all processes zero priority, the lowest one available. Other such scheduling policies are SCHED_BATCH for batch scheduling of the processes and the SCHED_IDLE, which is suitable for the scheduling of extremely low priority jobs. The alternatives to this scheduling policy are SCHED_FIFO and SCHED_RR. Both of them are intended as real-time policies and are time-critical applications that require precise control processes and their latencies.

To offer more real-time characteristics to a Linux operating system, there are also two more approaches that can be presented. The first one refers to a more preemptive implementation of the Linux kernel. This approach can take advantage of the already available spinlock mechanism used for SMP support, making sure that multiple processes are prevented from executing simultaneously, though in the context of a single processor, the spinlocks are no ops. The interrupt handling also requires modifications this rescheduling to make possible if another higher priority process appears; in this situation, a new scheduler might also be required. This approach offers the advantage of not changing the interaction of a user space and the advantage of using APIs, such as POSIX or others. The drawback of this is that the kernel changes are very serious and every time a kernel version changes, these changes need to be adapted accordingly. If this work was not enough already, the end result is not fully real-time operating system, but one that reduces the latency of the operating system.

The other available implementation is interrupt abstraction. This approach is based on the fact that not all systems require a hard real-time determinism and most of them only require a section of their task to be executed in a real-time context. The idea behind this approach is to run Linux with the priority of an idle task under a real-time kernel and non-real-time tasks to continue to execute them as they normally do. This implementation fakes the disabling of an interrupt for the real-time kernel, but in fact, it is passed to the real-time kernel. For this type of implementation, there are three available solutions:

The following diagram presents a basic RTLinux architecture.

Understanding GPOS and RTOS

A similar architecture, as shown in the preceding diagram, applies to the two other solutions since both of them were born from the RTLinux implementation. The difference between them is at the implementation level and each offers various benefits.

The PREEMPT_RT patches are the first option for every developer when a real-time solution is required. For some developers, the PREEMPT_RT patches transform Linux into a real-time solution suitable for their needs. This solution could not replace a real-time operation system, but is, in fact, suitable for a large number of systems.

The biggest advantage that PREEMPT_RT has over other real-time solutions for Linux is that it actually transforms Linux into a real-time operating system. All the other alternatives usually create a microkernel that is executed as a hypervisor and Linux is only executed as a task of it, so the communication of real-time tasks with the non-real-time ones is done through this microkernel. For the PREEMPT_RT patch, this problem is no more.

The standard version of the Linux kernel is only able to offer soft real-time requirements, such as basic POSIX user space operations where no deadline is guaranteed. Adding patches, such as Ingo Molnar's PREEMPT_RT patch, and also Thomas Gheixner's patch with regards to a generic clock event layer that offers a high resolution support, you can say that you have a Linux kernel that offers high real-time capabilities.

With the presence of the real-time preemption patch in the industry, a number of interesting opportunities have appeared, making it an option for firm and hard real-time applications in areas, such as industrial control or professional audio. This is mainly because of the design of the PREEMPT_RT patch and its aim toward integration inside the mainline kernel. We will learn about its usage further in the chapter. The following diagram shows the working of the Preemptible Linux Kernel:

PREEMPT_RT

The PREEMPT_RT patch transforms Linux from a general purpose operating system into a preemptible one using the following tricks:

Before moving to the actual configuration part, you should download a suitable version for the kernel. The best inspiration source is https://www.kernel.org/, which should be the starting point because it does not contain any extra patches. After the source code is received, the corresponding rt patches version can be downloaded from https://www.kernel.org/pub/linux/kernel/projects/rt/. The kernel version chosen for this demonstration is the 3.12 kernel version, but if any other kernel version is required, the same steps can be taken with a similar end result. The development of the real-time preemption patches is very active, so any missing version support is covered very fast. Also, for other sublevel versions, the patches can be found in the incr or older subdirectories of that particular kernel version. The following is the example for sublevel versions:

wget https://www.kernel.org/pub/linux/kernel/v3.x/linux-3.12.38.tar.xz
wget https://www.kernel.org/pub/linux/kernel/projects/rt/3.12/patch-3.12.38-rt52.patch.gz

After the source code is received, the sources need to be unpacked and the patches applied:

tar xf linux-3.12.38.tar.xz
cd linux-3.12.38/
gzip -cd ../patch-3.12.38-rt52.patch.gz | patch -p1

The next step involves the configuration of the kernel sources. The configuration differs from one architecture to another, but the general idea remains. The following configurations are required for a QEMU ARM machine supported inside Poky. To enable the PREEMPT_RT support for a machine, there are multiple options available. You can implement a low-latency support version, which is most suitable for a desktop computer using a kernel configuration fragment similar to this:

CONFIG_GENERIC_LOCKBREAK=y
CONFIG_TREE_PREEMPT_RCU=y
CONFIG_PREEMPT_RCU=y
CONFIG_UNINLINE_SPIN_UNLOCK=y
CONFIG_PREEMPT=y
CONFIG_PREEMPT__LL=y
CONFIG_PREEMPT_COUNT=y
CONFIG_DEBUG_PREEMPT=y
CONFIG_RCU_CPU_STALL_VERBOSE=y

This option is one of the most often used and it also constitutes the primary source of usage of the PREEMPT_RT patches. The alternative of this would be to enable the fully preemptive support for the PREEMPT_RT patches using a configuration similar to this:

CONFIG_PREEMPT_RT_FULL=y
CONFIG_HZ_1000=y
CONFIG_HZ=1000

If you're interested in configuring the kernel manually, it can use the menuconfig option. The following CONFIG_PREEMPT* configurations are available for easier access to the required options. The first image mainly contains the CONFIG_PREEMPT and CONFIG_PREEMPT_COUNT variables, which should be the first ones to enable. There is also a configuration option called CONFIG_PREEMPT_NONE that is used for no forced preemptive actions.

Applying the PREEMPT_RT patch

In the following image, the CONFIG_PREEMPT_RCU and CONFIG_PREEMPT_RT_FULL configurations are available. More information related to RCU is available at https://lwn.net/Articles/262464/.

Applying the PREEMPT_RT patch

The third image contains the CONFIG_PREEMPT__LL configuration. Another interesting configuration is CONFIG_PREEMPT_VOLUNTARY, which also reduces the latency along with the CONFIG_PREEMPT__LL configuration, for a desktop computer.

One interesting argument against the low-latency desktop option is available at https://sevencapitalsins.wordpress.com/2007/08/10/low-latency-kernel-wtf/.

Applying the PREEMPT_RT patch

The last one contains the CONFIG_TREE_PREEMPT_RCU configuration used to change the RCU implementation. The same process can be used to search and enable the other configurations that do not contain the search word in their name.

Applying the PREEMPT_RT patch

For more information regarding the PREEMPT_RT patch, refer to http://varun-anand.com/preempt.html and http://www.versalogic.com/mediacenter/whitepapers/wp_linux_rt.asp.

After the kernel image is obtained with the newly applied and configured real-time preemptible kernel patch, it needs to be booted to make sure the activity is done appropriately so that the end result can be usable. Using the uname –a command, the patch rt* revision number is visible and should be applied to the kernel version. Of course, there are other methods that can used to identify this information. An alternative for the uname –a command is the dmesg command on its output the string real-time preemption support should be visible, but only one method should be enough. The following image offers a representation of how the uname –a command output should look:

Applying the PREEMPT_RT patch

Taking a look at the list of processes, it can be seen, as mentioned earlier, that the IRQ handler is treated using kernel threads. This information is visible in the next ps command output due to the fact that it is put between square brackets. Single IRQ handlers are represented by the task_struct structures that are similar to the user space ones, making them easily controllable from the user space:

The next bit of information that needs to be gathered involves the formatting of the interrupt process entries, which are a bit different than the ones used for a vanilla kernel. This output is visible by inspecting the /proc/interrupts file:

Then, information available in the fourth column provides the IRQ line notifications, such as: [........N/ 0]. Here, each dot represents an attribute and each attribute is a value, as described in the following points. Here is the order of their presence:

In the preceding example, you can see that multiple IRQs are marked as visible and hard IRQs that are run in the kernel context. When an IRQ status is marked as IRQ_NODELAY, it shows the user that the handler of the IRQ is a kernel thread and it will be executed as one. The description of an IRQ can be changed manually, but this is not an activity that will be described here.

Inside Yocto, kernel recipes with PREEMPT_RT patches are applied. For the moment, there are only two recipes that incorporate the PREEMPT_RT patch; both are available inside the meta layer. The recipes that refer to kernel versions 3.10 and 3.14 and their naming are linux-yocto-rt_3.10.bb and linux-yocto-rt_3.14.bb. The –rt ending in the naming indicates that these recipes fetch the PREEMPT_RT branches of the Linux kernel versions maintained by the Yocto community.

The format for the 3.14 kernel recipe is presented here:

As shown, one of the recipes seemed to have a duplicated line and a patch is necessary to remove it:

The preceding recipe is very similar to the base one. Here, I am referring to linux-yocto_3.14.bb; they are the recipes on which the PREEMPT_RT patches have been applied. The difference between them is that each one is taken from its specific branch, and until now, none of the Linux kernel versions with the PREEMPT_RT patches have provided support for the qemumips64 compatible machine.

Linux, a general purpose operating system that is optimized for throughput, is the exact opposite of what a real-time operating system is all about. Of course it offers a high throughput by using a large, multilayered cache, which is a nightmare for a hard real-time operating process.

In order to have a real-time Linux, there are two available options:

The variations of the second option imply various solution from the isolation of the cores for real-time activities to the assignation of one for such tasks. There are also a lot of solutions that involve the usage of a hypervisor or a hook below the Linux kernel to serve a number of interrupts to the RTOS. The existence of these alternatives have been made available to the reader not only with other options, but also due to the fact that the PREEMPT_RT patch has its disadvantages.

One notable disadvantage is that the reduction of latency was done by forcing the kernel to preempt a task when a higher priority one appeared. This, of course, reduces the throughput for the system because it not only adds a number of context switches in the process but also makes the lower priority tasks wait longer than they would do the normal Linux kernel.

Another disadvantage of the preempt-rt patches is that they need to be ported from one kernel version to another and adapted from one architecture or software vendor to another. This only implies that knowledge of the Linux kernel should be available in-house for a particular vendor and it should adapt the solution for each of its available kernels. This fact alone has made it less likeable for BSP or Linux operating system providers.

Applying the PREEMPT_RT patch

Before moving to the

actual configuration part, you should download a suitable version for the kernel. The best inspiration source is https://www.kernel.org/, which should be the starting point because it does not contain any extra patches. After the source code is received, the corresponding rt patches version can be downloaded from https://www.kernel.org/pub/linux/kernel/projects/rt/. The kernel version chosen for this demonstration is the 3.12 kernel version, but if any other kernel version is required, the same steps can be taken with a similar end result. The development of the real-time preemption patches is very active, so any missing version support is covered very fast. Also, for other sublevel versions, the patches can be found in the incr or older subdirectories of that particular kernel version. The following is the example for sublevel versions:

wget https://www.kernel.org/pub/linux/kernel/v3.x/linux-3.12.38.tar.xz
wget https://www.kernel.org/pub/linux/kernel/projects/rt/3.12/patch-3.12.38-rt52.patch.gz

After the source code is received, the sources need to be unpacked and the patches applied:

tar xf linux-3.12.38.tar.xz
cd linux-3.12.38/
gzip -cd ../patch-3.12.38-rt52.patch.gz | patch -p1

The next step involves the configuration of the kernel sources. The configuration differs from one architecture to another, but the general idea remains. The following configurations are required for a QEMU ARM machine supported inside Poky. To enable the PREEMPT_RT support for a machine, there are multiple options available. You can implement a low-latency support version, which is most suitable for a desktop computer using a kernel configuration fragment similar to this:

CONFIG_GENERIC_LOCKBREAK=y
CONFIG_TREE_PREEMPT_RCU=y
CONFIG_PREEMPT_RCU=y
CONFIG_UNINLINE_SPIN_UNLOCK=y
CONFIG_PREEMPT=y
CONFIG_PREEMPT__LL=y
CONFIG_PREEMPT_COUNT=y
CONFIG_DEBUG_PREEMPT=y
CONFIG_RCU_CPU_STALL_VERBOSE=y

This option is one of the most often used and it also constitutes the primary source of usage of the PREEMPT_RT patches. The alternative of this would be to enable the fully preemptive support for the PREEMPT_RT patches using a configuration similar to this:

CONFIG_PREEMPT_RT_FULL=y
CONFIG_HZ_1000=y
CONFIG_HZ=1000

If you're interested in configuring the kernel manually, it can use the menuconfig option. The following CONFIG_PREEMPT* configurations are available for easier access to the required options. The first image mainly contains the CONFIG_PREEMPT and CONFIG_PREEMPT_COUNT variables, which should be the first ones to enable. There is also a configuration option called CONFIG_PREEMPT_NONE that is used for no forced preemptive actions.

Applying the PREEMPT_RT patch

In the following image, the CONFIG_PREEMPT_RCU and CONFIG_PREEMPT_RT_FULL configurations are available. More information related to RCU is available at https://lwn.net/Articles/262464/.

Applying the PREEMPT_RT patch

The third image contains the CONFIG_PREEMPT__LL configuration. Another interesting configuration is CONFIG_PREEMPT_VOLUNTARY, which also reduces the latency along with the CONFIG_PREEMPT__LL configuration, for a desktop computer.

One interesting argument against the low-latency desktop option is available at https://sevencapitalsins.wordpress.com/2007/08/10/low-latency-kernel-wtf/.

Applying the PREEMPT_RT patch

The last one contains the CONFIG_TREE_PREEMPT_RCU configuration used to change the RCU implementation. The same process can be used to search and enable the other configurations that do not contain the search word in their name.

Applying the PREEMPT_RT patch

For more information regarding the PREEMPT_RT patch, refer to http://varun-anand.com/preempt.html and http://www.versalogic.com/mediacenter/whitepapers/wp_linux_rt.asp.

After the kernel image is obtained with the newly applied and configured real-time preemptible kernel patch, it needs to be booted to make sure the activity is done appropriately so that the end result can be usable. Using the uname –a command, the patch rt* revision number is visible and should be applied to the kernel version. Of course, there are other methods that can used to identify this information. An alternative for the uname –a command is the dmesg command on its output the string real-time preemption support should be visible, but only one method should be enough. The following image offers a representation of how the uname –a command output should look:

Applying the PREEMPT_RT patch

Taking a look at the list of processes, it can be seen, as mentioned earlier, that the IRQ handler is treated using kernel threads. This information is visible in the next ps command output due to the fact that it is put between square brackets. Single IRQ handlers are represented by the task_struct structures that are similar to the user space ones, making them easily controllable from the user space:

The next bit of information that needs to be gathered involves the formatting of the interrupt process entries, which are a bit different than the ones used for a vanilla kernel. This output is visible by inspecting the /proc/interrupts file:

Then, information available in the fourth column provides the IRQ line notifications, such as: [........N/ 0]. Here, each dot represents an attribute and each attribute is a value, as described in the following points. Here is the order of their presence:

In the preceding example, you can see that multiple IRQs are marked as visible and hard IRQs that are run in the kernel context. When an IRQ status is marked as IRQ_NODELAY, it shows the user that the handler of the IRQ is a kernel thread and it will be executed as one. The description of an IRQ can be changed manually, but this is not an activity that will be described here.

Inside Yocto, kernel recipes with PREEMPT_RT patches are applied. For the moment, there are only two recipes that incorporate the PREEMPT_RT patch; both are available inside the meta layer. The recipes that refer to kernel versions 3.10 and 3.14 and their naming are linux-yocto-rt_3.10.bb and linux-yocto-rt_3.14.bb. The –rt ending in the naming indicates that these recipes fetch the PREEMPT_RT branches of the Linux kernel versions maintained by the Yocto community.

The format for the 3.14 kernel recipe is presented here:

As shown, one of the recipes seemed to have a duplicated line and a patch is necessary to remove it:

The preceding recipe is very similar to the base one. Here, I am referring to linux-yocto_3.14.bb; they are the recipes on which the PREEMPT_RT patches have been applied. The difference between them is that each one is taken from its specific branch, and until now, none of the Linux kernel versions with the PREEMPT_RT patches have provided support for the qemumips64 compatible machine.

Linux, a general purpose operating system that is optimized for throughput, is the exact opposite of what a real-time operating system is all about. Of course it offers a high throughput by using a large, multilayered cache, which is a nightmare for a hard real-time operating process.

In order to have a real-time Linux, there are two available options:

The variations of the second option imply various solution from the isolation of the cores for real-time activities to the assignation of one for such tasks. There are also a lot of solutions that involve the usage of a hypervisor or a hook below the Linux kernel to serve a number of interrupts to the RTOS. The existence of these alternatives have been made available to the reader not only with other options, but also due to the fact that the PREEMPT_RT patch has its disadvantages.

One notable disadvantage is that the reduction of latency was done by forcing the kernel to preempt a task when a higher priority one appeared. This, of course, reduces the throughput for the system because it not only adds a number of context switches in the process but also makes the lower priority tasks wait longer than they would do the normal Linux kernel.

Another disadvantage of the preempt-rt patches is that they need to be ported from one kernel version to another and adapted from one architecture or software vendor to another. This only implies that knowledge of the Linux kernel should be available in-house for a particular vendor and it should adapt the solution for each of its available kernels. This fact alone has made it less likeable for BSP or Linux operating system providers.

The Yocto Project -rt kernel

Inside Yocto, kernel

recipes with PREEMPT_RT patches are applied. For the moment, there are only two recipes that incorporate the PREEMPT_RT patch; both are available inside the meta layer. The recipes that refer to kernel versions 3.10 and 3.14 and their naming are linux-yocto-rt_3.10.bb and linux-yocto-rt_3.14.bb. The –rt ending in the naming indicates that these recipes fetch the PREEMPT_RT branches of the Linux kernel versions maintained by the Yocto community.

The format for the 3.14 kernel recipe is presented here:

As shown, one of the recipes seemed to have a duplicated line and a patch is necessary to remove it:

The preceding recipe is very similar to the base one. Here, I am referring to linux-yocto_3.14.bb; they are the recipes on which the PREEMPT_RT patches have been applied. The difference between them is that each one is taken from its specific branch, and until now, none of the Linux kernel versions with the PREEMPT_RT patches have provided support for the qemumips64 compatible machine.

Linux, a general purpose operating system that is optimized for throughput, is the exact opposite of what a real-time operating system is all about. Of course it offers a high throughput by using a large, multilayered cache, which is a nightmare for a hard real-time operating process.

In order to have a real-time Linux, there are two available options:

The variations of the second option imply various solution from the isolation of the cores for real-time activities to the assignation of one for such tasks. There are also a lot of solutions that involve the usage of a hypervisor or a hook below the Linux kernel to serve a number of interrupts to the RTOS. The existence of these alternatives have been made available to the reader not only with other options, but also due to the fact that the PREEMPT_RT patch has its disadvantages.

One notable disadvantage is that the reduction of latency was done by forcing the kernel to preempt a task when a higher priority one appeared. This, of course, reduces the throughput for the system because it not only adds a number of context switches in the process but also makes the lower priority tasks wait longer than they would do the normal Linux kernel.

Another disadvantage of the preempt-rt patches is that they need to be ported from one kernel version to another and adapted from one architecture or software vendor to another. This only implies that knowledge of the Linux kernel should be available in-house for a particular vendor and it should adapt the solution for each of its available kernels. This fact alone has made it less likeable for BSP or Linux operating system providers.

Disadvantages of the PREEMPT_RT patches

Linux, a general purpose

operating system that is optimized for throughput, is the exact opposite of what a real-time operating system is all about. Of course it offers a high throughput by using a large, multilayered cache, which is a nightmare for a hard real-time operating process.

In order to have a real-time Linux, there are two available options:

The variations of the second option imply various solution from the isolation of the cores for real-time activities to the assignation of one for such tasks. There are also a lot of solutions that involve the usage of a hypervisor or a hook below the Linux kernel to serve a number of interrupts to the RTOS. The existence of these alternatives have been made available to the reader not only with other options, but also due to the fact that the PREEMPT_RT patch has its disadvantages.

One notable disadvantage is that the reduction of latency was done by forcing the kernel to preempt a task when a higher priority one appeared. This, of course, reduces the throughput for the system because it not only adds a number of context switches in the process but also makes the lower priority tasks wait longer than they would do the normal Linux kernel.

Another disadvantage of the preempt-rt patches is that they need to be ported from one kernel version to another and adapted from one architecture or software vendor to another. This only implies that knowledge of the Linux kernel should be available in-house for a particular vendor and it should adapt the solution for each of its available kernels. This fact alone has made it less likeable for BSP or Linux operating system providers.

Having a real-time operating system may not always be enough for everyone. Some people would also require real-time optimized applications running over the operating system. To make sure an rt-application can be designed and interacted with, the required determinism is necessary on the operating system and hardware. With regard to the hardware configuration, the requirements involve a low-latency interrupt handling. The mechanisms causing the ISR latencies should register values around tens of microseconds.

Regarding the kernel configuration required by real-time applications, the following configurations are necessary:

To write an application, there are some things that need to be taken care of, such as making sure that the use of swap is disabled to diminish latencies caused by page faults. The use of global variables or arrays should be kept to a minimum. The 99 priority number is not configured to run an application, and other spin locks are not implemented instead, it uses priority inheritance futexes. Also avoid input/output operations and data sharing between applications.

For a device driver, the advice is a bit different. Previously, we mentioned that the interrupt handling for a real-time kernel is done in a thread context, but the hardware interrupt context can still play a role here. To recognize the hardware interrupt context from the interrupt handler, the IRQF_NODELAY flag can be used. If you use the IRQF_NODELAY context, make sure you avoid functions such as wake_up(), up(), or complete().

The Linux operating system was for a very long time seen as a GPOS, but in the last couple of years, some projects tried to change this by modifying the Linux kernel into a RTOS. One such project is the PREEMPT_RT patch, which was mentioned previously.

In this section of the chapter, I will discuss a series of tests that could be executed for both versions of the Linux OS with or without applying the PREEMPT_RT patches. I should mention that for those of you who are interested in some actual results, there are a number of papers available that try to investigate the latency effect of the PREEMPT_RT or its advantages or disadvantages. One such example is available at http://www.versalogic.com/downloads/whitepapers/real-time_linux_benchmark.pdf.

Before continuing further, I believe it is my duty to define a number of technical terms that are necessary to properly understand some information:

The LPPTest is included in the PREEMPT_RT patch and it contains a Linux driver that only changes a bit value on a parallel port to identify the response time. Another driver responds to the change in a bit value and a user space application that measures the results. The files to look for are drivers/char/lpptest.c and scripts/testlpp.c. To perform this test, two machines are required: one to send the signal and the other one to receive and send the response. This requirement is stringent since the use of a loopback cable can mess with the measurements.

RealFeel is a test for interrupt processing. The program uses /dev/rtc to fire a periodic interrupt, measures the duration between one interrupt to another, and compares it with the expected value. At the end, it prints the variation from the expected value indefinitely so that the variations can be exported in a log file to process later.

Linux Real-Time Benchmarking Framework (LRTB) represents a set of scripts and drivers that are used to evaluate various performance counters for the Linux kernel with a real-time addition. It measures the load imposed by real-time patches and their ability to obtain a more deterministic response to interrupts.

For the benchmarking phase, programs such as hackbench, lmbench, or even the Ingo Molnar dohell script can be used. There are, of course, a number of other tools that can be used for both testing (cyclictest, hourglass, and so on) or benchmarking (unixbench, cache-calibrator, or any other stress test that takes real-time performances to their limit), but I will let the user test them and apply the ones that suit their needs best.

The PREEMPT_RT patch improves the preemptiveness of the Linux kernel, but this does not mean it is the best solution to use. The usefulness of PREEMPT_RT patch can differ if various aspects of the application domain changes. With regard to the PREEMPT_RT patch, it is ready to be used in a hard real-time system. One conclusion cannot be made, but I must admit that it can be considered hard real-time material if it is used in life sustaining or mission-critical systems. This is a decision for everybody to make, and for this testing is required. One opinion that supports this is from Steven Rostedt, a Linux kernel developer who is the maintainer of the stable version of the real-time Linux kernel patch for Red Hat. It is available at http://www.linux.com/news/featured-blogs/200-libby-clark/710319-intro-to-real-time-linux-for-embedded-developers.

The meta-realtime layer is an initiative maintained by Bruce Ashfield from WindRiver, which planned to create a place where real-time activities related to the Linux kernel or system development. It was created as the placeholder for PREEMPT_RT, SCHED_DEADLINE, POSIX real-time, and alternative paring of general purpose operating systems and real-time operating systems, whether this involved a user space RTOS, a hypervisor, or an AMP solution. Also, this is where system partitioning, CPU isolation, and other related applications s reside. Of course, none of this would be considered complete without some performance profiling and benchmarking applications available for the whole Linux operating system.

Although this layer description sounds really exciting at first, its content is really poor. It is only able to incorporate a number of testing tools, more accurately, two of them: schedtool-dl and rt-app, as well as extra scripts that try to remotely run rt-app on the target machine and gather the resulting data.

The first schedtool-dl application is a scheduler testing tool used for deadline scheduling. It appears from the need to change or make queries of the CPU-scheduling policies and even processes levels available under Linux. It can also be used to lock processes on various CPUs for SMP/NUMA systems, to avoid skipping in audio/video applications, and in general, to maintain a high level of interaction and responsiveness even under high loads.

The next and last available application is rt-app, which is used as a test application for the simulation of real-time loads on a system. It does this by starting multiple threads at given periods of time. It offers support for SCHED_FIFO, SCHED_OTHER, SCHED_RR, SCHED_DEADLINE, as well as the Adaptive Quality of Service Architecture (AQuoSA) framework, which is an open source project that tries to offer adaptive Quality of Service (QoS) for the Linux kernel.

Besides the included packages, the layer also contains an image that incorporates them, but this is not nearly enough to make this layer one that contains substantial content. Although it does not contain a vast amount of information inside it, this layer has been presented in this chapter because it contains the starting point and offers a development point of view of all the information presented until now. Of course, a number of applications that should reside in this layer are already spread across multiple other layers, such as the idlestat package that is available in meta-linaro. However, this does not constitute the central point of this explanation. I only wanted to point out the most suitable place that can contain any real-time relate activities, and in my opinion, meta-realtime is this place.