How can we better understand the real-time technology of embedded operating system Linux?

Linux supports a variety of embedded processors such as PowerPC, MIPS, ARM, DSP, etc., and is gradually being used in a variety of key occasions. Among them, specific applications such as real-time multimedia processing, industrial control, and automotive electronics put forward strong real-time requirements for Linux. Linux provides some real-time extensions, but needs real-time transformation. This article discusses some key issues in embedded Linux real-time technology, such as Linux kernel delay, mainstream real-time technology solutions and their evaluations.

Linux kernel latency

Although mainstream Linux partially meets the POSIX 1003.1b real-time extension standard, it is not completely a real-time operating system. The main manifestations are:

* Task scheduling and kernel preemption

The 2.6 version of the kernel has added a lot of preemption points, so that the process can also be preempted when the kernel code is executed. In order to support the preemption of kernel code, the critical section is protected by using a spin lock that disables interrupts in the 2.6 kernel. But if there is a low-priority process executing in the critical section at this time, the high-priority process must wait for the low-priority process to exit the critical section even if it does not access the critical section protected by the low priority.

* Interrupt delay

In the mainstream Linux kernel design, interrupts can preempt the highest-priority tasks, making the longest time for high-priority tasks blocked to be uncertain. Moreover, because the kernel needs to turn off interrupts to protect the critical section, the blocking time of high-priority tasks is increased.

* Clock accuracy

Linux generates millisecond-level periodic clock interrupts for kernel time management through hardware clock programming, which cannot meet the high-precision scheduling requirements of real-time systems. The accuracy of the core timer is also limited by the clock interrupt, which cannot meet the high-precision timing requirements of the real-time system.

* Other delays

In addition, there are multiple delays in other subsystems of the Linux kernel. For example, in order to enhance kernel performance and reduce memory consumption, Linux loads the corresponding memory pages in the program address space only when needed. When the accessed content (such as code) is not in RAM, the memory management unit (MMU) will generate a page-fault (Page-Fault) to trigger page loading, resulting in uncertain real-time process response time.

Linux real-time technology development

The mainstream Linux kernel 1.x, 2.2.x and 2.4.x versions of the Linux kernel have no preemption support. Until the 2.6 version of the Linux kernel, the preemptible kernel is supported, and the kernel preemption outside the critical section and the preemptible large kernel lock are supported. On this basis, Linux uses the following two types of real-time technologies.

* Dual core mode

The real-time dual-kernel mode of Linux kernel is represented by RTLinux, RTAI and Xenomai. Among them, RT-Linux implements a micro-kernel real-time operating system to support low-level task management, interrupt service routines, and low-level task communication queues. Ordinary Linux is the lowest priority task of the real-time operating system. The tasks under Linux communicate with real-time tasks through FIFO named pipes, as shown in Figure 1.

How can we better understand the real-time technology of embedded operating system Linux?

When Linux wants to turn off the interrupt, the real-time microkernel will intercept and record this request, and simulate the interrupt controller through software, without actually turning off the hardware interrupt, which avoids the response delay caused by turning off the interrupt. RT-Linux sets the system real-time clock to single trigger mode, providing microsecond-level clock accuracy. RTAI is similar to the implementation of RTLinux. The difference is that it modifies the architecture-related code to form a real-time hardware abstraction layer (RTHAL), so that its real-time tasks can interrupt ordinary Linux tasks at any time, and the two pass through a non-blocking queue. To communicate. RTAI minimizes the code that directly modifies the Linux kernel and has better portability. Xenomai is based on RTAI, also known as RTAI/Fusion. The Adeos microkernel is used to replace RTAI's hardware abstraction layer. Its characteristic also lies in imitating the API interface of traditional RTOS, promoting the transplantation of traditional RTOS application under GNU/Linux. Similarly, there are open source projects such as L4Linux based on the Fiasco microkernel.

* Kernel patch method

Under the dual-core real-time solution, real-time tasks need to be designed according to another set of APIs provided by the micro-kernel real-time operating system. The kernel patch method does not change the Linux API. The original applications can run on the real-time operating system. Typical examples include the early research Kurt-Linux and Red-Linux, and the commercial versions of MontaVista, TimeSys and Wind River. Linux, and the real-time preemptive patch kernel developed by Ingo Monlnar and others at this stage.

Kurt-Linux is the first real-time operating system based on ordinary Linux. Real-time and non-real-time tasks are divided into normal state, real-time state and mixed state. RED-Linux can realize a variety of scheduling algorithms through multiple attributes and scheduling programs of tasks. Using software to simulate interrupt management, and inserting a lot of preemption points in the kernel, improve the system scheduling accuracy.

MontaVista Linux developed a kernel O(1) real-time scheduler and improved and tested the preemptible kernel on the basis of low-latency patches and preemptible kernel patches. In the era of Linux 2.4 kernel, MontaVista Linux, as a commercial mature product, has real-time performance. Strong advantage. TImeSys Linux also provides high-precision clock and priority inheritance mutex support through the kernel module.

The mainstream kernel of version 2.6 has absorbed the above technologies and supports multiple configuration options such as CONFIG_PREEMPT_NONE, CONFIG_PREEMPT_VOLUNTARY and CONFIG_PREEMPT. Respectively suitable for computing task system, desktop user system and millisecond delay embedded system. In 2005, the real-time Linux project was launched for the 2.6 kernel MontaVista, which promoted the real-time process of the Linux kernel. Subsequently, Ingo Molnar released a new real-time preemption patch, which has gradually become the real-time mainstream technology of the Linux kernel. It is also adopted and supplemented by MontaVista Linux and Wind River Linux. The follow-up content of this article will involve the real-time preemption patch.

Linux real-time technology and evaluation

The real-time performance of the 2.6 version of the Linux kernel has been enhanced, and the dual-kernel Linux real-time technology is also under continuous development. The copyright of RTLinux, originally maintained by FSMLab, was purchased by Wind River in February 2007. It was not very active in the open source community at first. RTAI supports architectures such as x386, but due to its code is more difficult to maintain and bugs are more difficult to debug. , Many developers have joined the Xenomai project. Xenomai supports the latest version 2.6 of Linux. In contrast, the code is relatively stable and maintainable, and the development model is more active.

The Linux real-time technology in kernel patch mode has made a lot of improvements on the basis of the 2.6 kernel, so that most of the code in the kernel except for interrupt shutdown and IRQ thread dispatching, scheduling, and context switching can be preempted. The critical area of ​​spinlock protection has been reduced from more than 1,000 to dozens, which greatly improves the real-time performance of the kernel, gains widespread support from the community and gradually becomes the mainstream technology of Linux real-time.

Linux kernel real-time improvement

The real-time preemption kernel patch has made real-time improvements for various delays of Linux, mainly including several aspects of technology.

* Real-time preemption of the kernel

In order to realize that the kernel is completely preemptible, the real-time kernel critical section replaces the original spin-lock with a high-performance priority inheritance mutex for protection, so that the execution in the critical section can also be preempted. Only when a thread wants to access a critical section that other threads are accessing, it is scheduled to sleep and is awakened when the protected critical section is released.

In the real-time preemption kernel, through the priority inheritance mechanism (PI), when a thread is blocked by a resource held by a low-priority thread, the low-priority thread inherits the priority of the blocked thread to execute as soon as possible and release the resources it holds without being blocked. Preempted by other threads.

* New lock mechanism brings improved kernel performance

The real-time preemption patch replaces the Big Kernel Lock (BKL), changing BKL from spin lock to mutex, and the thread holding BKL can also be preempted, reducing the kernel scheduling delay. In addition, the real-time preemption patch replaces semaphore with mutex, avoiding unnecessary time load. The real-time preemption patch implements preemptible RCU (Read Copy Update) locks and serialized read-write locks, ensuring the predictability of execution and improving performance.

* Interrupt threading

The real-time preemption patch implements some hardware interrupt and software interrupt service routines through kernel threads. The architecture-related processing code sets the IRQ status, checks whether the threaded interrupt is enabled, and wakes up the relevant thread. After the interrupt thread is scheduled for execution, interrupt service processing is performed. In the real-time preemption kernel, the user thread priority can be higher than the device interrupt service thread. The real-time task does not need to wait for the execution of the device driver processing program, which reduces the delay of real-time preemption.

* Clock system improvement

The clock system of the real-time preemption kernel has been redesigned to realize a high-precision timer. Clock accuracy no longer depends on jiffies, so that the accuracy of POSIX timers and nanosleep is determined by the accuracy provided by the specific hardware, so that getTImeofday can provide the precise time value required by the real-time system.

* Other improvements

Linux supports futex with good performance at the user level. The implementation principle is similar to that of kernel priority inheritance mutex. It only enters the kernel when there is a race condition, which improves the performance of the application. In addition, the real-time preemption patch kernel also provides mutex deadlock detection, delay tracking and measurement, interrupt close tracking and delay measurement, preemption delay measurement and other kernel debugging and diagnosis, kernel performance measurement and tuning and other tools, real-time trace support (Ftrace), etc. stand by.

At this stage, real-time technology has gradually been supported in various architectures, as shown in Table 1.

How can we better understand the real-time technology of embedded operating system Linux?

Real-time preemption of kernel latency

At this stage, the real-time preemption patch technology is still in the process of improvement, which is manifested in the following deficiencies.

* Interrupt delay

Even if the interrupt thread preemption does not occur, the real-time preemption kernel adds a pair of context switching time to the original interrupt service mechanism, which is used to wake up the interrupt service thread to execute and enter the sleep state. In addition, there are a small number of critical areas protected by raw_spinlock locks to disable interrupts in the kernel, and the interrupt delay caused by these locks needs to be calculated.

* Task preemption delay

The kernel preemption delay is mainly caused by the use of various lock mechanisms in the kernel to control tasks and interrupt access to the critical section, especially the real-time preemption kernel in order to avoid priority reversal increased lock mechanism brings additional time load .

* Other delays of kernel modules

In the real-time preemption patch, the memory management module also needs to reduce the delay caused by page table errors and reduce the impact of performance degradation caused by mlockall memory latching. The use of high-precision timers in the real-time preemption kernel results in additional timer management time load. In addition, some drivers in the kernel need to be optimized for real-time applications to improve real-time response. Soft floating point processing and soft floating point kernel simulation need to be compatible with real-time preemption patches, and the energy management subsystem also needs to have real-time system awareness.

Real-time preemption kernel performance test

This article was tested on the Intel PenTIum M 1.7 GHz processor. The test environment includes: Linux kernel 2.6.25.8 minimum configuration; patch-2.6.25.8-rt7 real-time patch; libc 2.5+ and busybox-1.10.0 build initrdfs root file system.

* Interrupt delay

The kernel interrupt latency measurement tool supported by the real-time preemption patch is used to measure the IRQ OFF time. In the case of 100% load, among 100,000 sampling points, the maximum value is around 31 us, and most of them are around 1 us, as shown in Figure 2.

How can we better understand the real-time technology of embedded operating system Linux?

* Task preemption delay

The kernel preemption shutdown time is measured by the kernel preemption shutdown measurement tool supported by the real-time preemption patch. Table 2 shows the comparison between the real-time preemption kernel and the normal Linux kernel.

How can we better understand the real-time technology of embedded operating system Linux?

Periodic tasks in real-time applications need to be executed within a certain time. It can be seen from the comparison of the periodic task delay between the real-time preemption kernel and the normal kernel that the real-time preemption kernel provides accurate execution of real-time tasks, as shown in Figure 3.

How can we better understand the real-time technology of embedded operating system Linux?

Conclusion

Embedded applications have more and more real-time requirements for Linux, and the mainstream kernel is gradually adding real-time technology, which will eventually provide a perfect solution for real-time applications. This article summarizes the Linux kernel time delay, introduces the real-time development of the Linux kernel, analyzes the mainstream real-time technology of the kernel, and analyzes the shortcomings of the real-time technology, and provides a reference for a better understanding of the real-time technology of Linux.

Ego E cigarette

Suizhou simi intelligent technology development co., LTD , https://www.msmsmart.com