Preempt RT Slides
Preempt RT Slides
Corrections, suggestions, contributions and translations are welcome! embedded Linux and kernel engineering
Send them to [email protected]
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 1/116
Understanding Linux real-time with PREEMPT_RT training
About Bootlin
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 3/116
Bootlin introduction
▶ Engineering company
• In business since 2004
• Before 2018: Free Electrons
▶ Team based in France and Italy
▶ Serving customers worldwide
▶ Highly focused and recognized expertise
• Embedded Linux
• Linux kernel
• Embedded Linux build systems
▶ Strong open-source contributor
▶ Activities
• Engineering services
• Training courses
▶ https://bootlin.com
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 4/116
Bootlin engineering services
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 5/116
Bootlin training courses
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 6/116
Bootlin, an open-source contributor
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 7/116
Bootlin on-line resources
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 8/116
Generic course information
Generic course
information
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 9/116
Supported hardware
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 10/116
Training quiz and certificate
▶ You have been given a quiz to test your knowledge on the topics covered by the
course. That’s not too late to take it if you haven’t done it yet!
▶ At the end of the course, we will submit this quiz to you again. That time, you
will see the correct answers.
▶ It allows Bootlin to assess your progress thanks to the course. That’s also a kind
of challenge, to look for clues throughout the lectures and labs / demos, as all the
answers are in the course!
▶ Another reason is that we only give training certificates to people who achieve at
least a 50% score in the final quiz and who attended all the sessions.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 11/116
Participate!
During the lectures...
▶ Don’t hesitate to ask questions. Other people in the audience may have similar
questions too.
▶ Don’t hesitate to share your experience too, for example to compare Linux with
other operating systems you know.
▶ Your point of view is most valuable, because it can be similar to your colleagues’
and different from the trainer’s.
▶ In on-line sessions
• Please keep your camera on too if you have one.
• Also make sure your name is properly filled.
• If Jitsi Meet is used, you can also use the ”Raise your hand” button when you wish
to ask a question but don’t want to interrupt.
▶ All this helps the trainer to engage with participants, see when something needs
clarifying and make the session more interactive, enjoyable and useful for everyone.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 12/116
Collaborate!
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 13/116
Practical lab - Training Setup
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 14/116
Realtime Systems
Realtime Systems
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 15/116
Realtime Operating System
Wikipedia
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 16/116
Determinism
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 17/116
Latencies
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 18/116
Design constraints - Throughput
Determinism
▶ Optimize most-likely scenario
▶ Might have a fast path and a slow path
▶ Use Hardware Offloading and caches
▶ Use Branch-Prediction and Speculative execution
▶ Latencies are acceptable for cold-start
Throughput Low-power
▶ Most modern hardware implement such features
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 19/116
Design constraints - Low Power
Determinism
▶ Oportunistic sleeping modes
▶ Dynamic Frequency Scaling
▶ Only go fast when required
▶ Long wakeup latencies
Throughput
▶ Power-Management firmwares can preempt the
Low-power
whole system
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 20/116
Design constraints - Determinism
Determinism
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 21/116
Security Features and Fixes
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 22/116
Multi-tasking
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 23/116
Preemption
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 24/116
Understanding preemption (1)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 25/116
Understanding preemption (2)
▶ However, when the interrupt comes while the task is executing a system call, this
system call has to finish before another task can be scheduled.
▶ By default, the Linux kernel does not do kernel preemption.
▶ This means that the time before which the scheduler will be called to schedule
another task is unbounded.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 26/116
Interrupts and events
priority
hard interrupt
critical task
preempted
time
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 27/116
Scheduling and proritizing
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 28/116
Locking
▶ mutex : Two states (taken, free). The task that has taken the mutex is the owner
▶ semaphore : Shared variable that is incremented by multiple users.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 29/116
Lock Families
Semaphores
Mutexes
▶ Mutually exclusive
▶ Have two states : Taken, Free
▶ The task that has taken the mutex is the owner
▶ Other tasks wait for the mutex to be free before taking it
▶ A Mutex is a semaphore with a counter that can only be incremented once
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 30/116
Priority inversion
▶ Priority inversion arises when strict priority-based scheduling interfers with locking
▶ It creates a scenario where a critical task is prevented to run by a lower priority
task
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 31/116
Priority inversion
time
▶ Task A (high priority) needs to access a lock, hold by task C (low priority)
▶ The scheduler runs task C so that it can release the lock
▶ Task B has a higher priority than C, but lower than A, preempts task C
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 32/116
Priority Inheritance
time
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 33/116
Priority Inheritance (2)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 34/116
Preempt RT
Preempt RT
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 35/116
The PREEMPT_RT Patch
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 36/116
PREEMPT_RT mainlining status (1)
▶ The project is making good progress since it got funding from the Linux
Foundation in 2015 (Linux version 4.1 at that time).
▶ We can now observe a slow but steady reduction of the size of the
PREEMPT_RT patchset:
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 37/116
PREEMPT_RT mainlining status (2)
▶ However, the mainline Linux kernel is a moving target too, introducing new issues
for real-time (such as disabling preemption in BPF... see
https://lwn.net/Articles/802884/).
▶ In 5.12, new CONFIG_PREEMPT_DYNAMIC switch to change the preemption model at
boot time: preempt=none, preempt=voluntary or preempt=full
▶ In 5.15, the realtime preemption locking core is merged in mainline. This was the
biggest part to mainline. A few pending changes are still left. See
https://lwn.net/Articles/867919/.
▶ printk() is one of the remaining pieces to be upstreamed
▶ See the latest news on https://wiki.linuxfoundation.org/realtime/
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 38/116
Why use PREEMPT_RT ?
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 39/116
Why not to use PREEMPT_RT ?
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 40/116
Getting the patch
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 41/116
The legacy of the RT patch
Many current features in the Linux Kernel originated from the RT patch :
▶ Kernel-side preemption
▶ High Resolution Timers
▶ Threaded interrupts
▶ Priority-Inheritance support for locking primitives
▶ Tickless operation
▶ Earliest-Deadline First scheduler
▶ Realtime locks - Locking primitives conversion
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 42/116
Locking
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 43/116
Critical sections
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 44/116
Sleeping locks
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 45/116
Spinlocks
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 46/116
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 many resources
▶ The high-resolution timers infrastructure 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 software-programmed timers.
• Usable directly from user space using the usual timer APIs
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 47/116
Printk
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 48/116
Interrupt handlers
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 49/116
Hard interrupts vs. Threaded interrupts
interrupt
priority
hard interrupt
critical task
▶ Small, well-defined hard irq handlers
preempted
▶ Irq handlers run in a dedicated task
time
Figure: Hardware interrupt processing ▶ It has a PID, and can be assigned a
priority
▶ Critical tasks can run regardless of
interrupt
interrupts
priority
schedules interrupt handler
▶ Use ps -e to list tasks
critical task ▶ Use chrt -p <prio> <pid> to
threaded interrupt change the priority
time
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 51/116
Preemption models
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 52/116
1st 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 real-time 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 (several possible values between
100 Hz and 1000 Hz).
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 53/116
2nd 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 (might_sleep()) throughout kernel code.
▶ Minor impact on throughput.
▶ Still used in: Ubuntu Desktop 20.04
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 54/116
3rd 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):
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 55/116
4th option: fully preemptible kernel
CONFIG_PREEMPT_RT
Almost all kernel code can be involuntarily preempted at any time.
▶ spinlocks are turned into sleeping locks
▶ Only raw_spinlock_t remains a real spinning lock
▶ All interrupt handlers are threaded, except for a few that explicitely need hard irq
• This is the case for drivers involved in interrupt dispatching
• cpufreq and cpuidle drivers too
▶ For use on systems with Realtime requirements
▶ If you find a kernel-side unbounded latency, this is a bug
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 56/116
Practical lab - Download and Boot a RT Kernel
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 57/116
Hardware
Hardware
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 58/116
Hardware latencies
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 59/116
Non-Maskable Interrupts
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 60/116
Deep Firmwares
On modern hardware, several firmwares can run outside the control of the Kernel
▶ The ARM TrustZone can run firmware like OP-TEE, to handle sensitive tasks
▶ Low-level firmware calls can be made by drivers through SMC calls or similar
▶ These firmwares can disable interrupts while they run
▶ Depending on the SoC, we might or might not have control over these firmwares
▶ hwlatdetect and careful driver analysis can help identify these issues
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 61/116
Memory access
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 62/116
Hyperthreading
Some CPU cores have 2 pipelines feeding the same ALU. This is known as
Hyperthreading
▶ The ALU executes instructions for one pipeline while the other fetches instructions
▶ This maximizes CPU usage and throughput
▶ Hyperthreads are very sensitive to what the co-thread runs
▶ Usually we recommend that hyperthreading is disabled for RT
Core Scheduling can help with hyperthreading
▶ Recent kernel development introduced core scheduling, in v5.14
▶ The scheduler is aware of hyperthreads, mostly for security reasons
▶ RT tasks won’t have sibling processes scheduled on the same core
▶ Non-RT process can still benefit from hyperthreading
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 63/116
IO Memory and DMA
▶ When writing drivers that are RT-critical, several considerations should be taken
▶ Some memory busses can buffer accesses and create latency spikes
▶ PCI accesses can buffer writes until the next read
▶ Some control busses such as i2c can be shared between devices
▶ DMA accesses can also introduce latencies due to bus mastering
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 64/116
NUMA
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 65/116
CPU Idle
▶ Modern CPUs have several Idle States for better power management
▶ The deeper the CPU core is sleeping, the longer it takes to wake it up
▶ Idle states are often called C-States
▶ Other Idle states can also exists, such as PC-States on a per-package basis
▶ We can limit the CPU idle states to lightweight states
▶ This can have a big impact on power consumption and thermal management
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 66/116
Idle States
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 67/116
Limiting the Idle states
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 68/116
CPU Frequency scaling
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 69/116
powertop
Powertop is a tool to monitor the CPU idle states and frequency usage
▶ It is designed to optimize the power usage of a system
▶ Useful to undertstand which C-States and OPP are being used
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 70/116
Testing and Benchmarking
Testing and
Benchmarking
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 71/116
Benchmarking vs Testing
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 72/116
ftrace - Kernel function tracer
Infrastructure that can be used for debugging or analyzing latencies and performance
issues in the kernel.
▶ Very well documented in trace/ftrace
▶ Negligible overhead when tracing is not enabled at run-time.
▶ Can be used to trace any kernel function!
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 73/116
Using ftrace
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 74/116
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 us.
▶ Check that wakeup is listed in /sys/kernel/tracing/available_tracers
▶ To select, reset and enable this tracer:
cat /sys/kernel/tracing/tracing_max_latency
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 75/116
kernelshark
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 76/116
hwlatdetect
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 77/116
cyclictest
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 78/116
hackbench
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 79/116
stress-ng
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 80/116
Testing and Benchmarking
Benchmarking an application
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 81/116
strace
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 82/116
perf
perf is a performance analysis tool that gathers kernel and hardware statistics
▶ Uses Hardware Counters and monitoring units
▶ Uses Kernel Counters and the tracing infrastructure
▶ Can profile the whole system, an application, a CPU core, etc.
▶ Very versatile, but tied to the kernel version
▶ Perf relies on various events reported by the kernel
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 83/116
Using perf, examples
▶ Lots of events measurable : hardware events, software events, cache misses, power
management
• perf list
▶ Display statistics in real-time
• perf top
• perf top -e cache-misses
• perf top -e context-switches
▶ Investigate scheduling latencies
• perf sched record
• perf sched latency
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 84/116
Other useful tools
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 85/116
Configuring the system
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 86/116
Configuration
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 87/116
CPU Pinning
▶ The Linux Kernel Scheduler allows setting constraints about the CPU cores that
are allowed to run each task
▶ This can be useful for lots of purposes :
• Make sure that a process won’t be migrated to another core
• Dedicate cores for specific tasks
• Optimize the data-path if a process deals with data handled by a specific CPU core
• Ease the job of the scheduler’s CPU load-balancer, whose complexity grows
non-linearly with the number of CPUs
▶ This mechanism is called the cpu affinity of a process
▶ The cpuset subsystem and the sched_setaffinity syscall are used to select the
CPUs
▶ Use taskset -p <mask> <cmd> to start a new process on the given CPUs
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 88/116
CPU Isolation
▶ Users can pin processes to CPU cores through the cpu affinity mechanism
▶ But the kernel might also schedule other processes on these CPUs
▶ isolcpus can be passed on the kernel commandline
▶ Isolated CPUs will not be used by the scheduler
▶ The only way to run processes on these CPUs is with cpu affinity
▶ Very useful when RT processes coexist with non-RT processes
isolcpus=0,2,3
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 89/116
IRQ affinity
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 90/116
The Linux Kernel Scheduler
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 91/116
Non-Realtime Scheduling Classes
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 92/116
Realtime Scheduling Classes
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 93/116
Changing the Scheduling Class
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 94/116
Realtime policies and CPU hogging
▶ A bug or an infinite loop in a high-priority realtime task will cause the system to
become unresponsive
▶ This can be prevented by creating an emergency shell with a higher priority to kill
the faulty programs
▶ The Kernel also provides a mechanism to make sure non-RT tasks get to run at
one point
▶ /proc/sys/kernel/sched_rt_period_us defines a window in microseconds that
the scheduler will share between RT and non-RT tasks
▶ /proc/sys/kernel/sched_rt_runtime_us defines how-much of that window is
going to be dedicated to RT tasks.
▶ The default values allocates 95% of the CPU time to RT tasks
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 95/116
System timer
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 96/116
Writing a driver
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 97/116
Practical lab - Benchmark and tweak your system
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 98/116
Application development
Application
development
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 99/116
Real-time application development
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 100/116
Initialization
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 101/116
Development and compilation
▶ No special library is needed, the POSIX real-time API is part of the standard C
library
▶ The glibc C library is recommended, as support for some real-time features is not
mature in other C libraries
• Priority inheritance mutexes or NPTL on some architectures, for example
▶ Compile a program
• ARCH-linux-gcc -o myprog myprog.c -lrt
▶ To get the documentation of the POSIX API
• Install the manpages-posix-dev package
• Run man function-name
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 102/116
Process, thread?
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 103/116
Process, thread: kernel point of view
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 104/116
Creating threads
pthread_exit(void *value_ptr);
▶ Waiting for the termination of a thread
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 105/116
Using scheduling classes (1)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 106/116
Using scheduling classes (2)
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);
▶ Then the thread can be created using pthread_create(), passing the attr
structure.
▶ Several other attributes can be defined this way: stack size, etc.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 107/116
Memory management
▶ When asking the Kernel to allocate some memory, a virtual address range is given
to the application
▶ This virtual range is mapped to a physical memory range upon accessing it
▶ This will introduce a page fault latency for the application
▶ All memory should be locked and accessed at application initialization
▶ Accessing memory at init is called prefaulting the memory
▶ This concerns the heap memory, but also the stack
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 108/116
Memory management
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 109/116
Locking
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 110/116
Synchronizing and signaling
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 111/116
timekeeping
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 112/116
Practical lab - Build, Analyse and Improve a Realtime Application
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 113/116
Last slides
Last slides
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 114/116
Last slide
Thank you!
And may the Source be with you
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 115/116
Rights to copy
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 116/116