Top Banner
Real-Time Linux (RT-Preempt) + SCHED DEADLINE (Linux Kernel 3.14) Josef Widder BRDS 2015
45

Real-Time Linux (RT-Preempt) + SCHED DEADLINE (Linux ...

Dec 25, 2021

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Real-Time Linux (RT-Preempt) + SCHED DEADLINE (Linux ...

Real-Time Linux (RT-Preempt)

+

SCHED DEADLINE (Linux Kernel 3.14)

Josef Widder

BRDS 2015

Page 2: Real-Time Linux (RT-Preempt) + SCHED DEADLINE (Linux ...

Misconceptions about Real-Time Computing [Sta88]

I Advances in supercomputer hardware will take care ofreal-time requiremens

I Real-time computing is equivalent to fast computing

I Real-time programming is assembly coding

I Real-time is meaningless if hardware will fail or softwarecontains bugs

I Real-time systems function in static environments

2

Page 3: Real-Time Linux (RT-Preempt) + SCHED DEADLINE (Linux ...

Misconceptions about Real-Time Computing [Sta88]

I Advances in supercomputer hardware will take care ofreal-time requiremens

I Real-time computing is equivalent to fast computing

I Real-time programming is assembly coding

I Real-time is meaningless if hardware will fail or softwarecontains bugs

I Real-time systems function in static environments

2

Page 4: Real-Time Linux (RT-Preempt) + SCHED DEADLINE (Linux ...

Misconceptions about Real-Time Computing [Sta88]

I Advances in supercomputer hardware will take care ofreal-time requiremens

I Real-time computing is equivalent to fast computing

I Real-time programming is assembly coding

I Real-time is meaningless if hardware will fail or softwarecontains bugs

I Real-time systems function in static environments

2

Page 5: Real-Time Linux (RT-Preempt) + SCHED DEADLINE (Linux ...

Misconceptions about Real-Time Computing [Sta88]

I Advances in supercomputer hardware will take care ofreal-time requiremens

I Real-time computing is equivalent to fast computing

I Real-time programming is assembly coding

I Real-time is meaningless if hardware will fail or softwarecontains bugs

I Real-time systems function in static environments

2

Page 6: Real-Time Linux (RT-Preempt) + SCHED DEADLINE (Linux ...

Misconceptions about Real-Time Computing [Sta88]

I Advances in supercomputer hardware will take care ofreal-time requiremens

I Real-time computing is equivalent to fast computing

I Real-time programming is assembly coding

I Real-time is meaningless if hardware will fail or softwarecontains bugs

I Real-time systems function in static environments

2

Page 7: Real-Time Linux (RT-Preempt) + SCHED DEADLINE (Linux ...

Real-Time vs. general-purpose OS

real-timealways predictable behavior

I timing guarantees

I under worst case behavior

general-purposemost of the time good behavior

I high throughput

I under normal (avg. case) behavior

3

Page 8: Real-Time Linux (RT-Preempt) + SCHED DEADLINE (Linux ...

Real-Time vs. general-purpose OS

real-timealways predictable behavior

I timing guarantees

I under worst case behavior

general-purposemost of the time good behavior

I high throughput

I under normal (avg. case) behavior

3

Page 9: Real-Time Linux (RT-Preempt) + SCHED DEADLINE (Linux ...

Why (Vanilla) Linux is not Real-time (bird’s eye view)

I general-purpose

I high functionality

I optimized for average case

I latency my vary and may not be bounded

(even with patch, convenient Linux features should not be touched)

4

Page 10: Real-Time Linux (RT-Preempt) + SCHED DEADLINE (Linux ...

Why (Vanilla) Linux is not Real-time (bird’s eye view)

I general-purpose

I high functionality

I optimized for average case

I latency my vary and may not be bounded

(even with patch, convenient Linux features should not be touched)

4

Page 11: Real-Time Linux (RT-Preempt) + SCHED DEADLINE (Linux ...

Why Linux is not Real-time (a closer look)

I Applications run in user space

I Hardware interaction is in kernel

I kernel not preempted by user threads ⇒

I event triggers high-priority thread

I thread cannot preempt kernel⇒ large latency possible > 100ms

I “classic” time sharing scheduling

5

Page 12: Real-Time Linux (RT-Preempt) + SCHED DEADLINE (Linux ...

CONFIG PREEMPT RT patch

to improve RT behavior, “reduce special status” of kernel

I kernel code outside of

I spinlocks protected areas

I interrupt handlers

may be preempted

I WC latency drops to (single digit) ms

Unlike semaphores, spinlocks may be used in code that cannotsleep, such as interrupt handlers. When properly used, spinlocksoffer higher performance than semaphores in general.

6

Page 13: Real-Time Linux (RT-Preempt) + SCHED DEADLINE (Linux ...

How does it work? [faq07]

The patch converts Linux into a fully preemptible kernel:

I in-kernel locking-primitives (using spinlocks) preemptiblethrough reimplementation with rtmutexes.RT-mutexes extend the semantics of simple mutexes by thepriority inheritance protocol

I citical sections protected by spinlock t and rwlock t arenow preemptible.rwlock t allows multiple readers in critical section

I priority inheritance for in-kernel spinlocks and semaphores.

I converting interrupt handlers into preemptible kernel threads

I user space POSIX timers with high resolution.

7

Page 14: Real-Time Linux (RT-Preempt) + SCHED DEADLINE (Linux ...

How does it work? [faq07]

The patch converts Linux into a fully preemptible kernel:

I in-kernel locking-primitives (using spinlocks) preemptiblethrough reimplementation with rtmutexes.RT-mutexes extend the semantics of simple mutexes by thepriority inheritance protocol

I citical sections protected by spinlock t and rwlock t arenow preemptible.rwlock t allows multiple readers in critical section

I priority inheritance for in-kernel spinlocks and semaphores.

I converting interrupt handlers into preemptible kernel threads

I user space POSIX timers with high resolution.

7

Page 15: Real-Time Linux (RT-Preempt) + SCHED DEADLINE (Linux ...

How does it work? [faq07]

The patch converts Linux into a fully preemptible kernel:

I in-kernel locking-primitives (using spinlocks) preemptiblethrough reimplementation with rtmutexes.RT-mutexes extend the semantics of simple mutexes by thepriority inheritance protocol

I citical sections protected by spinlock t and rwlock t arenow preemptible.rwlock t allows multiple readers in critical section

I priority inheritance for in-kernel spinlocks and semaphores.

I converting interrupt handlers into preemptible kernel threads

I user space POSIX timers with high resolution.

7

Page 16: Real-Time Linux (RT-Preempt) + SCHED DEADLINE (Linux ...

How does it work? [faq07]

The patch converts Linux into a fully preemptible kernel:

I in-kernel locking-primitives (using spinlocks) preemptiblethrough reimplementation with rtmutexes.RT-mutexes extend the semantics of simple mutexes by thepriority inheritance protocol

I citical sections protected by spinlock t and rwlock t arenow preemptible.rwlock t allows multiple readers in critical section

I priority inheritance for in-kernel spinlocks and semaphores.

I converting interrupt handlers into preemptible kernel threads

I user space POSIX timers with high resolution.

7

Page 17: Real-Time Linux (RT-Preempt) + SCHED DEADLINE (Linux ...

What sort of real-time? [faq07]

it depends. . .

I speed of the CPU and the architecture

I device drivers

I hardware

e.g., DMA activity can introduce significant latencies

e.g., some firmwares can stop the system for housekeeping(SMI, Service Management Interrupts)SMIs can not be trapped by the OS

. . . and it is still Linux ⇒ certain features must not be used. . .

8

Page 18: Real-Time Linux (RT-Preempt) + SCHED DEADLINE (Linux ...

What sort of real-time? [faq07]

it depends. . .

I speed of the CPU and the architecture

I device drivers

I hardware

e.g., DMA activity can introduce significant latencies

e.g., some firmwares can stop the system for housekeeping(SMI, Service Management Interrupts)SMIs can not be trapped by the OS

. . . and it is still Linux ⇒ certain features must not be used. . .

8

Page 19: Real-Time Linux (RT-Preempt) + SCHED DEADLINE (Linux ...

Guidelines for writing RT applications [faq07]

I Call mlockall() as soon as possible from main().causes all of the pages mapped by the address space of aprocess to be memory residenti.e., preventing that memory is paged to the swap area

I Create all threads at startup time of the applicationi.e., never start threads dynamically during mission time

I Touch each page of the entire stack of each thread

I Never use system calls that are known to generate page faults,such as fopen().opening files does mmap() system call ⇒ page fault

9

Page 20: Real-Time Linux (RT-Preempt) + SCHED DEADLINE (Linux ...

Guidelines for writing RT applications [faq07]

I Call mlockall() as soon as possible from main().causes all of the pages mapped by the address space of aprocess to be memory residenti.e., preventing that memory is paged to the swap area

I Create all threads at startup time of the applicationi.e., never start threads dynamically during mission time

I Touch each page of the entire stack of each thread

I Never use system calls that are known to generate page faults,such as fopen().opening files does mmap() system call ⇒ page fault

9

Page 21: Real-Time Linux (RT-Preempt) + SCHED DEADLINE (Linux ...

Guidelines for writing RT applications [faq07]

I Call mlockall() as soon as possible from main().causes all of the pages mapped by the address space of aprocess to be memory residenti.e., preventing that memory is paged to the swap area

I Create all threads at startup time of the applicationi.e., never start threads dynamically during mission time

I Touch each page of the entire stack of each thread

I Never use system calls that are known to generate page faults,such as fopen().opening files does mmap() system call ⇒ page fault

9

Page 22: Real-Time Linux (RT-Preempt) + SCHED DEADLINE (Linux ...

How to not build RT applications [how07]

Don’t use fancy stuff!

Direct memory access

I latency in microseconds by

I SATA/PATA/SCSI devices

I network adapters

I video cards

I can sometimes be controlled by device driversthroughput vs. latency

I this problem independent of OS

Power managementWatts vs. latency

Hyperthreading and out-of-order executionperformance vs. latency

10

Page 23: Real-Time Linux (RT-Preempt) + SCHED DEADLINE (Linux ...

How to not build RT applications [how07]

Don’t use fancy stuff!

Direct memory access

I latency in microseconds by

I SATA/PATA/SCSI devices

I network adapters

I video cards

I can sometimes be controlled by device driversthroughput vs. latency

I this problem independent of OS

Power managementWatts vs. latency

Hyperthreading and out-of-order executionperformance vs. latency

10

Page 24: Real-Time Linux (RT-Preempt) + SCHED DEADLINE (Linux ...

How to not build RT applications [how07]

Don’t use fancy stuff!

Direct memory access

I latency in microseconds by

I SATA/PATA/SCSI devices

I network adapters

I video cards

I can sometimes be controlled by device driversthroughput vs. latency

I this problem independent of OS

Power managementWatts vs. latency

Hyperthreading and out-of-order executionperformance vs. latency

10

Page 25: Real-Time Linux (RT-Preempt) + SCHED DEADLINE (Linux ...

More Guidelines. . . [how07]

I do not use VGA text consolevery large latency

I use serial console, ssh, telnet

I take care of page faults (swapping) during start-up

I Call directly from the main() entry the mlockall() call

I Create all threads at startup time of the application.

I Reserve a pool of memory to do new/delete or malloc/free

I Never use system calls that are known to generate pagefaults.

I do not configure application with priority 99

some watchdog threads need higher priority than appl.

I use mmap to pass data around

11

Page 26: Real-Time Linux (RT-Preempt) + SCHED DEADLINE (Linux ...

More Guidelines. . . [how07]

I do not use VGA text consolevery large latency

I use serial console, ssh, telnet

I take care of page faults (swapping) during start-up

I Call directly from the main() entry the mlockall() call

I Create all threads at startup time of the application.

I Reserve a pool of memory to do new/delete or malloc/free

I Never use system calls that are known to generate pagefaults.

I do not configure application with priority 99

some watchdog threads need higher priority than appl.

I use mmap to pass data around

11

Page 27: Real-Time Linux (RT-Preempt) + SCHED DEADLINE (Linux ...

More Guidelines. . . [how07]

I do not use VGA text consolevery large latency

I use serial console, ssh, telnet

I take care of page faults (swapping) during start-up

I Call directly from the main() entry the mlockall() call

I Create all threads at startup time of the application.

I Reserve a pool of memory to do new/delete or malloc/free

I Never use system calls that are known to generate pagefaults.

I do not configure application with priority 99

some watchdog threads need higher priority than appl.

I use mmap to pass data around

11

Page 28: Real-Time Linux (RT-Preempt) + SCHED DEADLINE (Linux ...

Install [rth06]

it is a kernel patch. . . thus something like this:

getting the sources:

# wget ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.23.1.tar.bz2

# wget http://www.kernel.org/pub/linux/kernel/projects/rt/patch-2.6.23.1-rt11.bz2

Patching the Sources

# tar xfj linux-2.6.23.1.tar.bz2

# cd linux-2.6.23.1

# bzcat ../patch-2.6.23.1-rt11.bz2 | patch -p1

configuring and build. . .

let’s see “hello, world.” and kernel detection [rth06]

12

Page 29: Real-Time Linux (RT-Preempt) + SCHED DEADLINE (Linux ...

Install [rth06]

it is a kernel patch. . . thus something like this:

getting the sources:

# wget ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.23.1.tar.bz2

# wget http://www.kernel.org/pub/linux/kernel/projects/rt/patch-2.6.23.1-rt11.bz2

Patching the Sources

# tar xfj linux-2.6.23.1.tar.bz2

# cd linux-2.6.23.1

# bzcat ../patch-2.6.23.1-rt11.bz2 | patch -p1

configuring and build. . .

let’s see “hello, world.” and kernel detection [rth06]

12

Page 30: Real-Time Linux (RT-Preempt) + SCHED DEADLINE (Linux ...

SCHED DEADLINE

Page 31: Real-Time Linux (RT-Preempt) + SCHED DEADLINE (Linux ...

“Classic” Linux Schedulers

Normal scheduling policies

I SCHED OTHER

standard round-robin time-sharing policy

I SCHED BATCH

for ”batch” style execution of processes“middle priority”

I SCHED IDLE

for running very low priority background jobs

Linux real-time schedulers with static priorities:

I SCHED FIFO: first in first outno time slicing

I SCHED RR: round robintime slices with preemption

14

Page 32: Real-Time Linux (RT-Preempt) + SCHED DEADLINE (Linux ...

“Classic” Linux Schedulers

Normal scheduling policies

I SCHED OTHER

standard round-robin time-sharing policy

I SCHED BATCH

for ”batch” style execution of processes“middle priority”

I SCHED IDLE

for running very low priority background jobs

Linux real-time schedulers with static priorities:

I SCHED FIFO: first in first outno time slicing

I SCHED RR: round robintime slices with preemption

14

Page 33: Real-Time Linux (RT-Preempt) + SCHED DEADLINE (Linux ...

SCHED DEADLINE

I real-time scheduling class in Linux

I earliest deadline first (EDF)

I dynamic priorities

I shortest deadline = highest priority

I optimal (restrictions apply)

I taskset is schedulable iff U ≤ 1

I released in kernel 3.14 on March 30, 2014.

15

Page 34: Real-Time Linux (RT-Preempt) + SCHED DEADLINE (Linux ...

There are some issues with EDF

I critical sectionpriority inheritance (deadline inheritance still a to-do

I precendence constraintsshifting deadlines w.r.t. dependent tasks

I overloadsadmission control

I context switch for free“in theory there is no differencebetween theory and practice. . . ”

. . . see RT Scheduling lecture

let’s see Linux implementation of EDF. . .

16

Page 35: Real-Time Linux (RT-Preempt) + SCHED DEADLINE (Linux ...

There are some issues with EDF

I critical sectionpriority inheritance (deadline inheritance still a to-do

I precendence constraintsshifting deadlines w.r.t. dependent tasks

I overloadsadmission control

I context switch for free“in theory there is no differencebetween theory and practice. . . ”

. . . see RT Scheduling lecture

let’s see Linux implementation of EDF. . .

16

Page 36: Real-Time Linux (RT-Preempt) + SCHED DEADLINE (Linux ...

sched-deadline.txt [EDF]

0. WARNING

===========

Fiddling with these settings can result in an

unpredictable or even unstable system behavior. As

for -rt (group) scheduling, it is assumed that root

users know what they’re doing.

17

Page 37: Real-Time Linux (RT-Preempt) + SCHED DEADLINE (Linux ...

Scheduling Guarantee

3 parameters

I runtime

I period

I deadline

The task receives runtime time units within deadline if a properadmission control strategy is used.

WCET vs. runtime?

In the example: deadline = period

18

Page 38: Real-Time Linux (RT-Preempt) + SCHED DEADLINE (Linux ...

Constant Bandwidth Server Scheduling (CBS) [EDF]

EDF-based scheduling algorithm that allows:

I periodic (sporadic) tasks with hard deadlinesutilization Up

I a CB server s with given utilization Us for “soft tasks”CBS ensures bounded utilization even in overload

I schedulable iff Us + Up ≤ 1

Tasks in CBS:

I Task cannot exceed its registered budget,

I Task cannot be unblocked when a ratio between remainingbudget and remaining deadline is higher than bandwidth.

19

Page 39: Real-Time Linux (RT-Preempt) + SCHED DEADLINE (Linux ...

Constant Bandwidth Server Scheduling (CBS) [EDF]

EDF-based scheduling algorithm that allows:

I periodic (sporadic) tasks with hard deadlinesutilization Up

I a CB server s with given utilization Us for “soft tasks”CBS ensures bounded utilization even in overload

I schedulable iff Us + Up ≤ 1

Tasks in CBS:

I Task cannot exceed its registered budget,

I Task cannot be unblocked when a ratio between remainingbudget and remaining deadline is higher than bandwidth.

19

Page 40: Real-Time Linux (RT-Preempt) + SCHED DEADLINE (Linux ...

Task interface

Specifying a periodic/sporadic task requires:

I a (maximum/typical) instance execution time,

I a minimum interval between consecutive instances,

I a time constraint by which each instance must be completed.

Therefore:

I struct sched attr with all the necessary fields

I sched setattr() and sched getattr()

scheduling related syscalls that manipulate sched attr

20

Page 41: Real-Time Linux (RT-Preempt) + SCHED DEADLINE (Linux ...

Task interface

Specifying a periodic/sporadic task requires:

I a (maximum/typical) instance execution time,

I a minimum interval between consecutive instances,

I a time constraint by which each instance must be completed.

Therefore:

I struct sched attr with all the necessary fields

I sched setattr() and sched getattr()

scheduling related syscalls that manipulate sched attr

20

Page 42: Real-Time Linux (RT-Preempt) + SCHED DEADLINE (Linux ...

Admission control

I interface parameters are only used at admission control timewhen user calls sched setattr()

I system-wide settings under proc file system

I for a root domain comprising M CPUs, -deadline tasks canbe created while the sum of their bandwidths stays below:M * (sched rt runtime us / sched rt period us)

I disable by writing -1 in/proc/sys/kernel/sched rt runtime us

21

Page 43: Real-Time Linux (RT-Preempt) + SCHED DEADLINE (Linux ...

minimal main. . . [EDF]

22

Page 44: Real-Time Linux (RT-Preempt) + SCHED DEADLINE (Linux ...

Thanks!

Page 45: Real-Time Linux (RT-Preempt) + SCHED DEADLINE (Linux ...

References I

Deadline task scheduling.https:

//www.kernel.org/doc/Documentation/scheduler/sched-deadline.txt.Accessed: Oct, 2015.

Frequently Asked Questions: RTwiki.https://rt.wiki.kernel.org/index.php/Frequently_Asked_Questions,2007.Accessed: Oct, 2015.

HOWTO: Build an RT-application Jump to: navigation, search.https://rt.wiki.kernel.org/index.php/HOWTO:_Build_an_RT-application,2007.Accessed: Oct, 2015.

RT PREEMPT HOWTO.https://rt.wiki.kernel.org/index.php/RT_PREEMPT_HOWTO, 2006.Accessed: Oct, 2015.

John A. Stankovic.Misconceptions about real-time computing.IEEE Computer, 21(10):10–19, 1988.

24