Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
1
Real Time: A Definition
2
The Path to Real Time in Linux
5
Thread Level Priorities
6
Sched Policy
Thread Attribute: schedpolicy
● Select the scheduling policy for the thread: one of
SCHED_OTHER (regular, non-realtime scheduling),
SCHED_RR (realtime, round-robin) or SCHED_FIFO
(realtime, first-in first-out).
● Default value: SCHED_OTHER.
● The real time scheduling policies SCHED_RR and
SCHED_FIFO are available only to processes with
superuser privileges.
● The scheduling policy of a thread can be changed after
creation with pthread_setschedpolicy(3).
7
Sched Param
8
Inherit Sched
9
Priority Inversion
50
4. Medium Priority task preempts low
priority task and high priority task
3
1. Low Priority task takes mutex
Time
10
Priority Inheritance
11
PI-Futex
● Interface through the Fast User-space
muTEX mechanism.
– Linux 2.6 application mutex support code.
● Supports user-space mutexes only.
– Similar kernel functionality in PREEMPT_RT.
● Introduced in 2.6.18
● A patched Glibc is needed right now
– Patch merging into mainline Glibc right now.
12
How fork() seems to work
A complete copy
Father Process is created of the
father process.
Copy
Father Process Child Process
13
How fork() really works
Father RO RO Child
Memory
Process Process
14
What happens during write?
When write is
attempted,
a single memory
Original Memory
page is copied
RO
and references
updated.
This is called:
“breaking the Child
CoW”. Process
RW
RO
Father
Process
15
Locking Memory
int mlockall(int flags);
● Disables paging for all pages mapped into
the address space of the calling process.
● Flags are:
– MCL_CURRENT: Lock all pages which are currently
mapped into the address space of the process.
– MCL_FUTURE : Lock all pages which will become
mapped into the address space of the process in the
future.
– Use both for max. effect: MCL_CURRENT| MCL_FUTURE
16
Stack Pre-Faulting
● Linux user space stacks are auto expanding.
– Use more stack then allocated to a process? kernel
gets an exception and allocates more stack for you.
– Can turn stack access to multiple context switch
and memory allocation with non deterministic
latency.
● We need to pre-fault stack pages.
– Call a dummy function that allocates an automatic
variable big enough for your entire future stack
usage and write to it, after you've mlocked memory.
17
Timer frequency
18
The Effect of Timer Frequency
Requested sleep time, in ms
2 HZ=100
0
1
0
1 1 2
0 5 0
Real sleep time, in ms
19
The Effect of Timer Frequency cont.
Requested sleep time, in ms
2 HZ=1000
0
1
0
1 1 2
0 5 0
Real sleep time, in ms
20
High-Res Timers
21
Sources Of Latency
Interrupt Latency
Interrupt Context
ISR Scheduler Task
Latency Switch
Preemption Latency
22
The Linux O(1) Scheduler
● The kernel maintains 2 priority arrays:
the active and the expired array.
● Each array contains 140 entries each with a
queue of processes with the same priority.
● The arrays are implemented such that it is
possible to pick the queue with the runnable
task with the highest priority in constant time
– Whatever the number of runnable tasks is.
● Let's see how this helps us...
23
Choosing and Expiring Tasks
● The scheduler finds the highest priority with a
runnable task and
– Executes the first task with that priority.
– Non real time tasks are run until they exhaust their
time slice and moved to the expired array.
Real time tasks are run until they yields the CPU [*].
● This is done until there are no more tasks in
the array, then the two arrays are swapped
and we start over.
● Scheduling is O(1).
24
Kernel Preemption Options
● Preemption means a high priority task taking
the place of a low priority one.
– User space code is always preemptive.
● In kernel mode (when running system call):
– PREEMPT_NONE
● None preemptive kernel code.
– PREEMPT_VOLUNTARY
● Voluntary preemption points.
– PREEMPT
● Non critical section preemptive kernel.
25
Soft Real Time
26
Hard and “Ruby” Hard Real Time
27
Nano Kernel Approach
Linux RTOS
RT Tasks
RT Nano-Kernel
Hardware
28
Nano Kernel Approach
29
PREEMPT_RT
Interrupt Handlers
Interrupt
Context
SoftIRQs
Kernel
Regular
tasklets
tasklets
Timers
Space
Hi prio
Stack
Net
...
Scheduling
Process
Points User Context
Thread
31
PREEMPT_RT Linux Contexts
Process
Thread Kernel
Net Stack
Tasklets
Timers
Space
Scheduling Kernel
Points Threads
User Space
32
Interface Changes
● Spinlocks and local_irq_save() no longer
disable hardware interrupts.
● Spinlocks no longer disable preemption.
● Raw_ variants for spinlocks and
local_irq_save() preserve original meaning
for SO_NODELAY interrupts.
● Semaphores and spinlocks employ priority
inheritance
● Deferred operations API.
33
Linux RT Benchmarking
● The setup:
– Dell PowerEdge SC420 machine with a Pentium 4
2.8MHz CPU, 256 Mb of RAM.
– Kernel version for all tests: Linux 2.6.12.
– Running various load generators.
● LMbench, LTP, flood ping, dd...
– 500,000 to 650,000 interrupts via parallel port.
– Measure response time on another machine.
34
Interrupt Response Times
35
Any Questions?
Gilad BenYossef
CTO
Codefidence Ltd.
gilad@codefidence.com
36
Copyright Notice
37