Real Time in Embedded Linux Systems
Total Page:16
File Type:pdf, Size:1020Kb
Real Time in Embedded Linux Systems Real Time in Embedded Linux Systems Michael Opdenacker Free Electrons http://free-electrons.com Created with OpenOffice.org 2.0 Thanks to Nicolas Rougier (Copyright 2003, http://webloria.loria.fr/~rougier/) for the Tux image Real Time in Embedded Linux Systems © Copyright 2004-2005, Michael Opdenacker Creative Commons Attribution-ShareAlike 2.0 license http://free-electrons.com Nov 25, 2005 1 Rights to copy Attribution ± ShareAlike 2.0 © Copyright 2004-2005 You are free Michael Opdenacker to copy, distribute, display, and perform the work [email protected] to make derivative works to make commercial use of the work Document sources, updates and translations: Under the following conditions http://free-electrons.com/articles/realtime Attribution. You must give the original author credit. Corrections, suggestions, contributions and Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work only under a license translations are welcome! identical to this one. For any reuse or distribution, you must make clear to others the license terms of this work. Any of these conditions can be waived if you get permission from the copyright holder. Your fair use and other rights are in no way affected by the above. License text: http://creativecommons.org/licenses/by-sa/2.0/legalcode Real Time in Embedded Linux Systems © Copyright 2004-2005, Michael Opdenacker Creative Commons Attribution-ShareAlike 2.0 license http://free-electrons.com Nov 25, 2005 2 Best viewed with... This document is best viewed with a recent PDF reader or with OpenOffice.org itself! Take advantage of internal or external hyperlinks. So, don't hesitate to click on them! Find pages quickly thanks to automatic search Use thumbnails to navigate in the document in a quick way If you're reading a paper or HTML copy, you should get your copy in PDF or OpenOffice.org format on http://free-electrons.com/articles/realtime! Real Time in Embedded Linux Systems © Copyright 2004-2005, Michael Opdenacker Creative Commons Attribution-ShareAlike 2.0 license http://free-electrons.com Nov 25, 2005 3 Contents Introduction Reducing latency in Linux Linux hard real-time extensions Commercial Linux real-time distributions Conclusion References Real Time in Embedded Linux Systems © Copyright 2004-2005, Michael Opdenacker Creative Commons Attribution-ShareAlike 2.0 license http://free-electrons.com Nov 25, 2005 4 Real Time in Embedded Linux Systems Introduction Real Time in Embedded Linux Systems © Copyright 2004-2005, Michael Opdenacker Creative Commons Attribution-ShareAlike 2.0 license http://free-electrons.com Nov 25, 2005 5 Hard Real Time A system is considered as a hard real time if it can answer to an internal or external stimulus within a given maximum amount of time. ªGuaranteed worst caseº Hard real time systems are used wherever failing to react in time can cause a system failure or damage, or put its users in danger. Typical examples Industrial process control Transportation Medicine (pacemakers, etc.) Real Time in Embedded Linux Systems © Copyright 2004-2005, Michael Opdenacker Creative Commons Attribution-ShareAlike 2.0 license http://free-electrons.com Nov 25, 2005 6 Soft Real Time A system is considered as soft real time if it is built to react to stimuli as quickly as it can. ªBest effortº However, if the system loses events or fails to process them in time, there is no catastrophic consequence on its operation. There is just a degradation in quality. Typical examples Audio, video Airline reservation systems Real Time in Embedded Linux Systems © Copyright 2004-2005, Michael Opdenacker Creative Commons Attribution-ShareAlike 2.0 license http://free-electrons.com Nov 25, 2005 7 Linux and Real Time Linux cannot be considered as a hard real time system There are long sections of code where all interrupts are masked Kernel code (system calls) not preemptible until they complete or decide to release the processor by calling the scheduler. Real Time in Embedded Linux Systems © Copyright 2004-2005, Michael Opdenacker Creative Commons Attribution-ShareAlike 2.0 license http://free-electrons.com Nov 25, 2005 8 Typical response time A Linux system can be considered as soft real time if it can react within a few hundreds of µs in most cases. A Linux system can be considered as hard real time if it can react within a few tens of µs in all cases. Real Time in Embedded Linux Systems © Copyright 2004-2005, Michael Opdenacker Creative Commons Attribution-ShareAlike 2.0 license http://free-electrons.com Nov 25, 2005 9 Linux 2.6 improvements Linux 2.6 improves this by offering a preemptible option (CONFIG_PREEMPT) Kernel code can be interrupted at almost any time (except when spinlocks are held) This reduces latency for high priority processes Implications for driver writers Anything can happen between 2 instructions Uniprocessor machines behave like SMP ones. No issue for clean drivers written with SMP in mind. Caution with per-CPU variables Real Time in Embedded Linux Systems © Copyright 2004-2005, Michael Opdenacker Creative Commons Attribution-ShareAlike 2.0 license http://free-electrons.com Nov 25, 2005 10 Other Linux 2.6 improvements Much more efficient scheduler Can be built with no virtual memory support. Note: lots of RTOS systems don©t use an MMU, even if one is available in the CPU. MMU management can add too much overhead. Improved POSIX threads (now available in mainstream) POSIX signals can©t be lost Make it easier to set task priorities and schedule periodic tasks with precision (useful for polling resources) Real Time in Embedded Linux Systems © Copyright 2004-2005, Michael Opdenacker Creative Commons Attribution-ShareAlike 2.0 license http://free-electrons.com Nov 25, 2005 11 Low latency patches Linux 2.4 approach: add more scheduler calls throughout the code. Addressed by the preemptible option in 2.6. Voluntary preemption patch (Ingo Molnar) http://people.redhat.com/mingo/voluntary-preempt/ Reduces latency by calling cond_reschedule() in the might_sleep() calls (used for debugging). However, Andrew Morton argued that it©s redundant with the preemptible option. See http://lwn.net/Articles/92806/ for details. August 2004: Some audio users still complain that Linux 2.6 latency is still not sufficient, even with the low latency patches. Real Time in Embedded Linux Systems © Copyright 2004-2005, Michael Opdenacker Creative Commons Attribution-ShareAlike 2.0 license http://free-electrons.com Nov 25, 2005 12 Real Time in Embedded Linux Systems Linux hard real-time extensions Real Time in Embedded Linux Systems © Copyright 2004-2005, Michael Opdenacker Creative Commons Attribution-ShareAlike 2.0 license http://free-electrons.com Nov 25, 2005 13 RTLinux First real-time extension for Linux However, the author filed a software patent covering the addition of real-time support to general operating systems as implemented in RTLinux. ªRTLinux Open Patent (!) Licenseº: 2 conditions for royalty-free use of this license Either using only GNU GPL software. No room for proprietary software or even other free software (BSD, Apache...) Or using an unmodified version of RTLinux. See http://www.fsmlabs.com/openpatentlicense.html FSF: RTLinux and distributing RTLinux violates the GPL license Real Time in Embedded Linux Systems © Copyright 2004-2005, Michael Opdenacker Creative Commons Attribution-ShareAlike 2.0 license http://free-electrons.com Nov 25, 2005 14 Other RTLinux issues Community unfriendly. Example: http://www.rtlinux.org/ and http://www.realtimelinux.org/ now redirect to http://www.fsmlabs.com/ The patent issue and uncertainty drew many developers away and frightened users. Linux RTAI attracts much more attention, developers and users. Offers 2 products RTLinuxPro: much more advertised. In particular, includes major rewrites to the original common code base. RTLinuxFree: doesn©t include the RTLinuxPro improvements... See http://www.fsmlabs.com/rtlinuxfree.html If you need commercial RT distributions, better use the other ones (Real Free Software with no patent burden). Real Time in Embedded Linux Systems © Copyright 2004-2005, Michael Opdenacker Creative Commons Attribution-ShareAlike 2.0 license http://free-electrons.com Nov 25, 2005 15 The RTAI project http://www.rtai.org/ Real-Time Application Interface for Linux Dipartimento di Ingegneria Aerospaziale - Politecnico di Milano (DIAPM) Founded in 1999, by Prof. Paolo Montegazza (long time contributor to RTLinux). 1.x series for Linux 2.2 Supports Linux 2.6 since April 2004 (3.1 series) Real Time in Embedded Linux Systems © Copyright 2004-2005, Michael Opdenacker Creative Commons Attribution-ShareAlike 2.0 license http://free-electrons.com Nov 25, 2005 16 RTAI strengths Community project (mainly European contributors now). Large user base. Excellent documentation: beginners guide, articles, conference pages, user and reference manuals... Supported architectures (+ SMP support) x86, PowerPC, ARM (StrongARM; ARM7: clps711x-family, Cirrus Logic EP7xxx, CS89712, PXA25x), MIPS, CRIS (ongoing) License: LGPL (only requires RTAI source changes to be shared) Modularity Support for POSIX and other RTOS APIs (through Xenomai) Hard real-time support in user space Real Time in Embedded Linux Systems © Copyright 2004-2005, Michael Opdenacker Creative Commons Attribution-ShareAlike 2.0 license http://free-electrons.com Nov 25, 2005 17 System architecture User land Linux process Linux process (data acquisition) (GUI) Real-time Kernel modules task 3 Real-time FIFO Linux Kernel Real-time Real-time task 1 task 2 Linux interrupt handlers Software interrupts Real-time kernel Hardware interrupts Hardware. Interrupt Controller Real Time in Embedded Linux Systems © Copyright 2004-2005, Michael Opdenacker Creative Commons Attribution-ShareAlike 2.0 license http://free-electrons.com Nov 25, 2005 18 RTAI concepts RTAI adds a layer between the Linux kernel and the hardware The RTAI kernel manages real-time tasks according to their priorities. The Linux kernel is also a real-time task, with the lowest priority. All non real-time interrupts are handed out to the Linux kernel.