Embedded Linux Realtime
Embedded Linux Realtime
Realtime in
embedded Linux
systems
Michael Opdenacker
Thomas Petazzoni
Gilles Chanteperdrix
Free Electrons
1
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://freeelectrons.com
Real Time in Embedded Linux Systems
Introduction
2
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://freeelectrons.com
Embedded Linux and real time
3
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://freeelectrons.com
Embedded Linux and real time
4
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://freeelectrons.com
Linux and realtime approaches
Over time, two major approaches have been taken to bring real
time requirements into Linux
Approach 1
Improve the Linux kernel itself so that it matches realtime
requirements, by providing bounded latencies, realtime APIs, etc.
Approach taken by the mainline Linux kernel and the
PREEMPT_RT project.
Approach 2
Add a layer below the Linux kernel that will handle all the realtime
requirements, so that the behaviour of Linux doesn't affect realtime
tasks.
Approach taken by RTLinux, RTAI and Xenomai
5
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://freeelectrons.com
Approach 1
Improving the main Linux kernel with
PREEMPT_RT
6
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://freeelectrons.com
Understanding latency
Your important
Something not very important...
realtime task !
Interrupt ! ? 7
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://freeelectrons.com
Linux kernel latency components
Waiting Process
Running task context
task Makes the
task runnable
Scheduling latency
Waiting
Running task
task Makes the
task runnable
interrupt Interrupt
handler Scheduler
latency
Interrupt
Scheduling latency
9
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://freeelectrons.com
Source of interrupt latency
Interrupt ?
10
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://freeelectrons.com
Interrupt handler duration
Waiting
Running task
task Makes the
task runnable
interrupt Interrupt
handler Scheduler
latency
Interrupt
Scheduling latency
11
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://freeelectrons.com
Interrupt handler implementation
In Linux, many interrupt handlers are split in two parts
A tophalf, started by the CPU as soon as interrupt are
enabled. It runs with the interrupt line disabled and is
supposed to complete as quickly as possible.
A bottomhalf, scheduled by the tophalf, which starts after all
pending tophalf have completed their execution.
Therefore, for realtime critical interrupts, bottomhalf
shouldn't be used: their execution is delayed by all other
interrupts in the system.
Other interrupt
Top half Bottom half
handlers...
Interrupt ACK Schedule Exit Handle Wake up
bottom device waiting User space...
half data... tasks
12
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://freeelectrons.com
Scheduler latency
Waiting
Running task
task Makes the
task runnable
interrupt Interrupt
handler Scheduler
latency
Interrupt
Scheduling latency
13
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://freeelectrons.com
Understanding preemption (1)
Interrupt handler
Wakes up Task B
Task A Task B
(running in user mode) (running in user mode)
Interrupt
14
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://freeelectrons.com
Understanding preemption (2)
Waiting
Running task
task Makes the
task runnable
interrupt Interrupt
handler Scheduler
latency
Interrupt
Scheduling latency
16
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://freeelectrons.com
Other nondeterministic mechanisms
17
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://freeelectrons.com
Priority inversion
Priority
Tries to get
the same
lock
waits
preempted
Acquires
a lock
Time
18
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://freeelectrons.com
Interrupt handler priority
19
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://freeelectrons.com
The PREEMPT_RT project
Longterm project lead by Linux kernel developers Ingo Molnar,
Thomas Gleixner and Steven Rostedt
https://rt.wiki.kernel.org
The goal is to gradually improve the Linux kernel regarding real
time requirements and to get these improvements merged into
the mainline kernel
PREEMPT_RT development works very closely with the mainline
development
Many of the improvements designed, developed and debugged
inside PREEMPT_RT over the years are now part of the mainline
Linux kernel
The project is a longterm branch of the Linux kernel that ultimately
should disappear as everything will have been merged
20
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://freeelectrons.com
Improvements in the mainline kernel
22
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://freeelectrons.com
st
1 option: no forced preemption
CONFIG_PREEMPT_NONE
Kernel code (interrupts, exceptions, system calls) never preempted.
Default behavior in standard kernels.
Best for systems making intense computations,
on which overall throughput is key.
Best to reduce task switching to maximize CPU and cache usage
(by reducing context switching).
Still benefits from some Linux 2.6 improvements:
O(1) scheduler, increased multiprocessor safety (work on RT
preemption was useful to identify hard to find SMP bugs).
Can also benefit from a lower timer frequency
(100 Hz instead of 250 or 1000).
23
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://freeelectrons.com
nd
2 option: voluntary kernel preemption
CONFIG_PREEMPT_VOLUNTARY
Kernel code can preempt itself
Typically for desktop systems, for quicker application reaction to
user input.
Adds explicit rescheduling points throughout kernel code.
Minor impact on throughput.
24
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://freeelectrons.com
rd
3 option: preemptible kernel
CONFIG_PREEMPT
Most kernel code can be involuntarily preempted at any time.
When a process becomes runnable, no more need to wait for
kernel code (typically a system call) to return before running the
scheduler.
Exception: kernel critical sections (holding spinlocks), but a
rescheduling point occurs when exiting the outer critical section,
in case a preemption opportunity would have been signaled while
in the critical section.
Typically for desktop or embedded systems with latency
requirements in the milliseconds range.
Still a relatively minor impact on throughput.
25
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://freeelectrons.com
Priority inheritance
26
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://freeelectrons.com
High resolution timers
The resolution of the timers used to be bound to the resolution of
the regular system tick
Usually 100 Hz or 250 Hz, depending on the architecture and the
configuration
A resolution of only 10 ms or 4 ms.
Increasing the regular system tick frequency is not an option as it
would consume too much resources
The highresolution timers infrastructure, merged in 2.6.21,
allows to use the available hardware timers to program interrupts
at the right moment.
Hardware timers are multiplexed, so that a single hardware timer is
sufficient to handle a large number of softwareprogrammed timers.
Usable directly from userspace using the usual timer APIs
27
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://freeelectrons.com
Threaded interrupts
To solve the interrupt inversion problem, PREEMPT_RT has
introduced the concept of threaded interrupts
The interrupt handlers run in normal kernel threads, so that the
priorities of the different interrupt handlers can be configured
The real interrupt handler, as executed by the CPU, is only in
charge of masking the interrupt and wakingup the corresponding
thread
The idea of threaded interrupts also allows to use sleeping
spinlocks (see later)
Merged since 2.6.30, the conversion of interrupt handlers to
threaded interrupts is not automatic : drivers must be modified
In PREEMPT_RT, all interrupt handlers are switched to threaded
interrupts
28
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://freeelectrons.com
PREEMPT_RT specifics
29
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://freeelectrons.com
CONFIG_PREEMPT_RT (1)
30
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://freeelectrons.com
CONFIG_PREEMPT_RT (2)
31
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://freeelectrons.com
Threaded interrupts
32
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://freeelectrons.com
Setting up PREEMPT_RT
33
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://freeelectrons.com
PREEMPT_RT setup (1)
34
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://freeelectrons.com
PREEMPT_RT setup (2)
35
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://freeelectrons.com
Realtime application development
36
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://freeelectrons.com
Development and compilation
37
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://freeelectrons.com
Process, thread ?
38
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://freeelectrons.com
Process, thread: kernel point of view
39
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://freeelectrons.com
Creating threads
40
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://freeelectrons.com
Scheduling classes (1)
42
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://freeelectrons.com
Scheduling classes (3)
pthread_attr_init(&attr);
pthread_attr_setinheritsched(&attr,
PTHREAD_EXPLICIT_SCHED);
pthread_attr_setschedpolicy(&attr, SCHED_FIFO);
parm.sched_priority = 42;
pthread_attr_setschedparam(&attr, &parm);
45
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://freeelectrons.com
Timers
timer_create(clockid_t clockid,
struct sigevent *evp,
timer_t *timerid)
Create a timer. clockid is usually CLOCK_MONOTONIC.
sigevent defines what happens upon timer expiration : send a
signal or start a function in a new thread. timerid is the returned
timer identifier.
timer_settime(timer_t timerid, int flags,
struct itimerspec *newvalue,
struct itimerspec *oldvalue)
Configures the timer for expiration at a given time.
timer_delete(timer_t timerid), delete a timer
clock_getres(), get the resolution of a clock
Other functions: timer_getoverrun(), timer_gettime()
46
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://freeelectrons.com
Signals
Signals are an asynchronous notification mechanism
Notification occurs either
By the call of a signal handler. Be careful with the limitations of
signal handlers!
By being unblocked from the sigwait(), sigtimedwait() or
sigwaitinfo() functions. Usually better.
Signal behaviour can be configured using sigaction()
Mask of blocked signals can be changed with
pthread_sigmask()
Delivery of a signal using pthread_kill() or tgkill()
All signals between SIGRTMIN and SIGRTMAX, 32 signals under
Linux.
47
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://freeelectrons.com
Interprocess communication
Semaphores
Usable between different processes using named semaphores
sem_open(), sem_close(), sem_unlink(), sem_init(),
sem_destroy(), sem_wait(), sem_post(), etc.
Message queues
Allows processes to exchange data in the form of messages.
mq_open(), mq_close(), mq_unlink(), mq_send(),
mq_receive(), etc.
Shared memory
Allows processes to communicate by sharing a segment of memory
shm_open(), ftruncate(), mmap(), munmap(),
close(), shm_unlink()
48
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://freeelectrons.com
Debugging realtime latencies
49
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://freeelectrons.com
ftrace Kernel function tracer
50
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://freeelectrons.com
Using ftrace
51
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://freeelectrons.com
Scheduling latency tracer
CONFIG_SCHED_TRACER (Kernel Hacking section)
Maximum recorded time between waking up a top priority task
and its scheduling on a CPU, expressed in µs.
Check that wakeup is listed in
/debug/tracing/available_tracers
To select, reset and enable this tracer:
echo wakeup > /debug/tracing/current_tracer
echo 0 > /debug/tracing/tracing_max_latency
echo 1 > /debug/tracing/tracing_enabled
Let your system run, in particular realtime tasks.
Example: chrt f 5 sleep 1
Disable tracing:
echo 0 > /debug/tracing/tracing_enabled
Read the maximum recorded latency and the corresponding trace:
cat /debug/tracing/tracing_max_latency
52
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://freeelectrons.com
Useful reading
53
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://freeelectrons.com
Approach 2
Realtime extensions to the Linux kernel
54
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://freeelectrons.com
Linux realtime extensions
Microkernel
Hardware
55
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://freeelectrons.com
RTLinux
56
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://freeelectrons.com
RTAI
57
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://freeelectrons.com
Xenomai project
http://www.xenomai.org/
Started in 2001 as a project aiming at emulating
traditional RTOS.
Initial goals: facilitate the porting of programs to GNU / Linux.
Initially related to the RTAI project (as the RTAI / fusion
branch), now independent.
Skins mimicking the APIs of traditional
RTOS such as VxWorks, pSOS+, and VRTXsa as well as the
POSIX API, and a “native” API.
Aims at working both as a cokernel and on top of
PREEMPT_RT in the upcoming 3.0 branch.
Will never be merged in the mainline kernel.
58
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://freeelectrons.com
Xenomai architecture
Adeos IPipe
59
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://freeelectrons.com
The Adeos interrupt pipeline abstraction
60
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://freeelectrons.com
Adeos virtualized interrupts disabling
61
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://freeelectrons.com
Adeos additional features
62
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://freeelectrons.com
Xenomai features
63
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://freeelectrons.com
Xenomai userspace realtime support.
Xenomai supports realtime in userspace on 5 architectures,
including 32 and 64 bits variants.
Two modes are defined for a thread
the primary mode, where the thread is handled by Xenomai
scheduler
the secondary mode, when it is handled by Linux scheduler.
Thanks to the services of the Adeos Ipipe service, Xenomai
system calls are defined.
A thread migrates from secondary mode to primary mode when
such a system call is issued
It migrates from primary mode to secondary mode when a Linux
system call is issued, or to handle gracefully exceptional events
such as exceptions or Linux signals.
64
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://freeelectrons.com
Life of a Xenomai application
Xenomai applications are started like normal Linux processes,
they are initially handled by the Linux scheduler and have access
to all Linux services
After their initialization, they declare themselves as realtime
application, which migrates them to primary mode. In this mode:
They are scheduled directly by the Xenomai scheduler, so they
have the realtime properties offered by Xenomai
They don't have access to any Linux service, otherwise they get
migrated back to secondary mode and looses all realtime
properties
They can only use device drivers that are implemented in Xenomai,
not the ones of the Linux kernel
Need to implement device drivers in Xenomai, and to split real
time and non realtime parts of your applications.
65
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://freeelectrons.com
Real Time Driver Model (RTDM)
An approach to unify the interfaces for developing device drivers
and associated applications under realtime Linux
An API very similar to the native Linux kernel driver API
Allows the development, in kernel space, of
Characterstyle device drivers
Networkstyle device drivers
See the whitepaper on
http://www.xenomai.org/documentation/xenomai2.4/pdf/RTDMandApplications.pdf
67
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://freeelectrons.com
How to build Xenomai
68
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://freeelectrons.com
Linux options for Xenomai configuration
69
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://freeelectrons.com
Xenomai userspace support
70
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://freeelectrons.com
Developing applications on Xenomai
71
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://freeelectrons.com
The POSIX skin
72
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://freeelectrons.com
Communication with a normal task
74
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://freeelectrons.com
The native API (2)
Task management services
rt_task_create(), rt_task_start(),
rt_task_suspend(), rt_task_resume(),
rt_task_delete(), rt_task_join(), etc.
Counting semaphore services
rt_sem_create(), rt_sem_delete(), rt_sem_p(),
rt_sem_v(), etc.
Message queue services
rt_queue_create(), rt_queue_delete(),
rt_queue_alloc(), rt_queue_free(),
rt_queue_send(), rt_queue_receive(), etc.
Mutex services
rt_mutex_create(), rt_mutex_delete(),
rt_mutex_acquire(), rt_mutex_release(), etc.
75
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://freeelectrons.com
The native API (3)
Alarm services
rt_alarm_create(), rt_alarm_delete(),
rt_alarm_start(), rt_alarm_stop(),
rt_alarm_wait(), etc.
Memory heap services
Allows to share memory between processes and/or to preallocate
a pool of memory
rt_heap_create(), rt_heap_delete(),
rt_heap_alloc(), rt_heap_bind()
Condition variable services
rt_cond_create(), rt_cond_delete(),
rt_cond_signal(), rt_cond_broadcast(),
rt_cond_wait(), etc.
76
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://freeelectrons.com
Xenomai and normal task communication
Using rt_pipes
In the native Xenomai application, use the Pipe API
rt_pipe_create(), rt_pipe_delete(),
rt_pipe_receive(), rt_pipe_send(),
rt_pipe_alloc(), rt_pipe_free()
In the normal Linux application
Open the corresponding /dev/rtpX file, the minor is specified at
rt_pipe_create() time
Then, just read() and write() to the opened file
77
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://freeelectrons.com
Realtime approaches
79
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://freeelectrons.com
Organizations
http://www.realtimelinuxfoundation.org/
Community portal for realtime Linux.
Organizes a yearly workshop.
http://www.osadl.org
Open Source Automation Development Lab (OSADL)
Created as an equivalent of OSDL for machine and plant control
systems. Member companies are German so far (Thomas Gleixner
is on board). One of their goals is to supports the development of
RT preempt patches in the mainline Linux kernel (HOWTOs, live
CD, patches).
80
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://freeelectrons.com
Related documents
Linux kernel
Device drivers
Architecture specifics
Embedded Linux system development
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//freeelectrons.com
How to help
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//freeelectrons.com
Linux kernel
Linux device drivers
Free Electrons
Board support code Our services
Mainstreaming kernel code
Kernel debugging
Custom Development
System integration
Embedded Linux Training
Embedded Linux demos and prototypes
All materials released with a free license! System optimization
Unix and GNU/Linux basics Application and interface development
Linux kernel and drivers development
Realtime Linux, uClinux Consulting and technical support
Development and profiling tools Help in decision making
Lightweight tools for embedded systems System architecture
Root filesystem creation System design and performance review
Audio and multimedia Development tool and application support
System optimization Investigating issues and fixing tool bugs