Linux As A Real-Time Operating System: Manas Saksena Timesys Corporation
Linux As A Real-Time Operating System: Manas Saksena Timesys Corporation
System
Manas Saksena
TimeSys Corporation
Trends in Embedded Systems
Multi-Function Devices
Dynamic Functionality
2
RTOS Requirements
Hardware Support
• What processors, boards, devices, etc. are supported?
3
Embedded OS Trends
4
Why Linux?
Hardware support
• Wide variety of processors and devices supported
• Portable code-base makes it easy to extend to newer
platforms
• Dedicated open-source community makes it easy to track new
hardware
Application Functional Support
• Unix, POSIX lineage of a full-function OS including rich multi-
tasking, IPC, networking, file systems, memory protection,
multi-user, security and protection, SMP, etc.
• Standard well-known API’s makes it easy for third party
developers to develop middleware, libraries, applications.
• Quickly improving in the state of development tools. A solid
based of GNU development tools, and other command line
tools exists.
• Many open-source developers and interested commercial
vendors keep pushing the boundaries.
5
Why Linux?
6
Open-Source and Commercial Linux Vendors
7
Understanding Linux
Linux Tasking & Scheduling Architecture
User Process
Application Level P1 P2 P3 or Thread
Preemptive Scheduling
Kernel Level
Non-Preemptive Scheduling
Kernel Thread P4
Soft IRQ Queue
Task Queue
Task Scheduler
Device Driver
Interrupt handler
9
Linux Scheduling Architecture
Low Priority
Time-Sharing
Scheduled Tasks Process or
by Task Thread
Scheduler (POSIX) Real-Time Context
Tasks
High Priority
10
Linux Device Driver Architecture
wakeup task
Soft-IRQ Handler
Interrupt handler
Device Driver
11
Standard Linux Networking Architecture
FTP
BSD Socket
INET Socket
soft
UDP TCP
irq
IP
FIB input forward output
12
Linux Clocks and Timers
Jiffy
• A selectable time quantum (default 10ms)
Jiffy Timer
• A periodic heartbeat timer that interrupts once every jiffy
Software Timers
• Maintained in a list, when a jiffy timer expires, then all expired
software timers are handled
– Implication: Recognition of timer expiry can be delayed by
as much as one jiffy.
– Note:
– Speeding up Jiffy increases overhead
13
Interrupt Latency
priority inversion
Hardware
HardwareDelay
Delay execution path
Interrupt
InterruptVectoring
Vectoring Linux kernel framework for
handling interrupts.
do_IRQ()
do_IRQ() Interrupts are not necessarily
masked throughout; can get
nested interrupts. Priority
handle_IRQ_Event()
handle_IRQ_Event() inversion?
Interrupt
InterruptHandler
Handler In device driver
14
Interrupt/Task Response -- Sample
Interrupt
InterruptLatency
Latency priority inversion
execution path
Interrupt
InterruptHandler
Handler
(wake
(wakeup
uptask)
task)
Interrupted
Interrupted Non-preemptible kernel
System
SystemCall
CallProcessing
Processing
Scheduler
Scheduler
(context -switch)
(context-switch)
Task
Task
15
Doing Better Real-Time
Priority Inversion
• Lower Priority Work (Classic priority inversion)
– Avoid/Reduce non-preemptible or interrupt-masked regions
– Avoid coarse-grained mutexes for synchronization
– Don’t use semaphores for mutexes
– Use priority inheritance mutexes to protect shared data
16
Approaches to Real-Time Linux
Approaches to Real-Time Linux
18
Compliant Kernel Approach
19
Compliant Kernel Approach Philosophy
20
Compliant Kernel Approach
Pros
• (Some) Applications developed for Linux can easily port to
real-time kernel
• Standard API familiar to programmers
• Applications can be developed on a Linux box and then easily
ported to real-time kernel
Cons
• Does not benefit from innovations in Linux Kernel
– E.g., devices, network protocols, etc.
• API compliance gives you a false sense of portability
– Many API’s are overloaded to access different features in
the kernel (e.g., drivers, network protocols, file systems,
etc.)
21
The Dual Kernel Approach
Linux Linux
Process Process
User-Level
Kernel-Level
Hardware
22
Dual Kernel Approach Philosophy
23
Dual Kernel Approach
Pros
• Requires very little changes to the Linux kernel
• The Linux side can be preempted at any time by the real-time
side
– Very tight response times possible for real-time tasks
• Significant development can be done on desktop running Linux
and inserting the real-time kernel
• Linux applications can be used in conjunction with real-time
tasks
Cons
• No memory protection in the real-time side
• Duplication of functionality
– Kernel bloat
– Future extensibility is questionable
• Sharing devices between real-time and Linux
• Starvation of non real-time tasks
• Static decomposition of real-time and non real-time
24
Core Kernel Approach
Hardware
25
Core Kernel Approach
Loadable Kernel
Module
Linux kernel Modified
for Real-Time
Linux Linux Loadable Kernel
Driver Driver Module
Hardware
26
Core Kernel Approach Philosophy
Is it even feasible?
• Yes, this is do-able without sacrificing Linux benefits, but does require
careful modifications to the kernel
Some Constraints
• The modifications should be limited and localized so that the benefits
of Linux are maintained
• Existing drivers and kernel modules should work “as-is” (can only be
made source-compatible)
• Existing user-level code (libraries, applications, middleware) should
work “as-is” in binary form, but can be used with better real-time
performance.
• New code can be written to maintain conformance with Linux, but can
benefit from real-time design guidelines.
27
Summary of Concerns and Solutions
Non-Preemptible Kernel
• Preemption Points via Low-Latency Patches
• Preemptible Kernels
– Two different approaches
Execution Path Latencies
• Real-Time Scheduler
Non-Schedulable Work
• Schedulable Soft-IRQ and IRQ Processing
Coarse Accuracy/Resolution Timers
• High Accuracy/Resolution Timers
User Level (POSIX) Mutexes without Priority Inheritance
• User-Level priority inheritance mutexes
28
Preemption Points
Basic Idea
• Introduce preemption points on long kernel paths
Approach
• Instrument kernel; identify long kernel paths
• Add Preemption points (call scheduler, if needed) at various
places in the path
– Find safe preemption points, or
– Modify code to introduce safe preemption points
29
Turning Linux into a Preemptible Kernel
Why is it non-preemptible?
• A non-preemptible kernel does not need to be reentrant
– Note: it has to be interrupt-safe
• Reduces overheads to make the kernel reentrant
30
Preemptible Kernel Approaches
31
Preemptible Kernel Approaches
32
Latency Problems due to Long Critical Sections
Unnecessary Coupling
• A low priority task can cause priority inversion for a high priority task
even if they don’t conflict with each other
33
Real-Time Scheduler
Problem
• Standard Linux scheduler is inefficient when there are large
number of runnable tasks
Solution
• Efficient real-time schedulers
– constant time dispatching decisions
34
Schedulable Interrupt and Soft-Interrupt Threads
35
High Accuracy Timers
High Resolution:
• Use a finer-granularity time structure
• POSIX timespec: seconds and nano-seconds
High Accuracy:
• Software Timers
– Maintain logically as a queue of timers sorted by expiry
time
– Include jiffy timer as a software timer
• Hardware Timer
– Use a one-shot hardware timer that expires at the next
software timer’s expiry time
– Reprogram on timer expiry and (if needed) when new
software timers are added
36
Resource Reservations in Linux
Limitation of Priority based Scheduling
38
CPU Reservations
39
Implications of CPU Reservations
40
Network Reservation Architecture
FTP
BSD Socket
INET Socket
UDP TCP
IP
FIB input forward output
41
Implications of Network Reservations
Transmit Reservations
• Ensure buffers are available for reserved streams and not
hogged by unreserved streams
• Ensure bandwidth is available for reserved streams
• Ensure reserved streams don’t hog bandwidth and buffers
beyond what they are allocated
Receive Reservations
• Ensures buffer space is available for incoming reservations, but
is not hogged unduly
• A separate execution/data path for reserved streams allows
latencies to be tightly controlled for reserved streams
• CPU reservation needed for thread processing on receive path
42
Questions?
Manas Saksena
TimeSys Corporation
[email protected]