practical real-time programming in user-space on...

84
Practical Real-Time Programming in User-Space on Linux linux.conf.au 2008 Lennart Poettering [email protected] January 2008 Lennart Poettering Practical Real-Time Programming in User-Space on Linux

Upload: duongnguyet

Post on 21-May-2018

216 views

Category:

Documents


0 download

TRANSCRIPT

Practical Real-Time Programming in User-Spaceon Linux

linux.conf.au 2008

Lennart [email protected]

January 2008

Lennart Poettering Practical Real-Time Programming in User-Space on Linux

Who Am I?

Software Engineer at Red Hat, Inc.

Developer of PulseAudio, Avahi and a few other Free Softwareprojects

http://0pointer.de/lennart/

[email protected]

IRC: mezcalero

Lennart Poettering Practical Real-Time Programming in User-Space on Linux

What is real-time (RT) programming?

Whenever an RT process is able to run, it runs.

Lennart Poettering Practical Real-Time Programming in User-Space on Linux

What is real-time (RT) programming?

Whenever an RT process is able to run, it runs.

Lennart Poettering Practical Real-Time Programming in User-Space on Linux

Where is this used?

ABS, Medical Systems, Finance, Audio, . . .

Everywhere where small latencies are required

Fixed rate playback audio/video; UI feedback

Lennart Poettering Practical Real-Time Programming in User-Space on Linux

Where is this used?

ABS, Medical Systems, Finance, Audio, . . .

Everywhere where small latencies are required

Fixed rate playback audio/video; UI feedback

Lennart Poettering Practical Real-Time Programming in User-Space on Linux

Where is this used?

ABS, Medical Systems, Finance, Audio, . . .

Everywhere where small latencies are required

Fixed rate playback audio/video; UI feedback

Lennart Poettering Practical Real-Time Programming in User-Space on Linux

Where is this used?

ABS, Medical Systems, Finance, Audio, . . .

Everywhere where small latencies are required

Fixed rate playback audio/video; UI feedback

Lennart Poettering Practical Real-Time Programming in User-Space on Linux

Our focus: Desktop and Audio on Linux

Lennart Poettering Practical Real-Time Programming in User-Space on Linux

Hard vs. Soft Real-time

Lennart Poettering Practical Real-Time Programming in User-Space on Linux

Linux as an RT kernel

Soft RT

Lennart Poettering Practical Real-Time Programming in User-Space on Linux

Linux as an RT kernel

Soft RT

Lennart Poettering Practical Real-Time Programming in User-Space on Linux

Scheduling latency: HZ, CPU Load

Lennart Poettering Practical Real-Time Programming in User-Space on Linux

Making a process real-time

Lennart Poettering Practical Real-Time Programming in User-Space on Linux

Real-time priorities

Lennart Poettering Practical Real-Time Programming in User-Space on Linux

Avoid locks!

Priority inversion

Priority inheritance, PTHREAD PRIO INHERIT

Lennart Poettering Practical Real-Time Programming in User-Space on Linux

Avoid locks!

Priority inversion

Priority inheritance, PTHREAD PRIO INHERIT

Lennart Poettering Practical Real-Time Programming in User-Space on Linux

Avoid locks!

Priority inversion

Priority inheritance, PTHREAD PRIO INHERIT

Lennart Poettering Practical Real-Time Programming in User-Space on Linux

Lock-free programming

Wait-free programming

Lock-free algorithms are difficult, wait-free even more so

Lennart Poettering Practical Real-Time Programming in User-Space on Linux

Lock-free programming

Wait-free programming

Lock-free algorithms are difficult, wait-free even more so

Lennart Poettering Practical Real-Time Programming in User-Space on Linux

Lock-free programming

Wait-free programming

Lock-free algorithms are difficult, wait-free even more so

Lennart Poettering Practical Real-Time Programming in User-Space on Linux

Lock-free reference counting

Multi-Reader, Multi-Writer lock-free queues

Lennart Poettering Practical Real-Time Programming in User-Space on Linux

Lock-free reference counting

Multi-Reader, Multi-Writer lock-free queues

Lennart Poettering Practical Real-Time Programming in User-Space on Linux

Atomic operations

APIs and portability: libatomic ops vs. glibc vs. glib vs. sync

Emulating atomic ops: In kernel easy, in user-space hard

Lennart Poettering Practical Real-Time Programming in User-Space on Linux

Atomic operations

APIs and portability: libatomic ops vs. glibc vs. glib vs. sync

Emulating atomic ops: In kernel easy, in user-space hard

Lennart Poettering Practical Real-Time Programming in User-Space on Linux

Atomic operations

APIs and portability: libatomic ops vs. glibc vs. glib vs. sync

Emulating atomic ops: In kernel easy, in user-space hard

Lennart Poettering Practical Real-Time Programming in User-Space on Linux

Memory Barriers

Lennart Poettering Practical Real-Time Programming in User-Space on Linux

Lock-free memory allocation: difficult

Beware of thread local pools like GSlice!

Alternative: free() lists

Lennart Poettering Practical Real-Time Programming in User-Space on Linux

Lock-free memory allocation: difficult

Beware of thread local pools like GSlice!

Alternative: free() lists

Lennart Poettering Practical Real-Time Programming in User-Space on Linux

Lock-free memory allocation: difficult

Beware of thread local pools like GSlice!

Alternative: free() lists

Lennart Poettering Practical Real-Time Programming in User-Space on Linux

Reference-counted memory handling

Zero-Copy

Minimize copies, cache pressure

Lennart Poettering Practical Real-Time Programming in User-Space on Linux

Reference-counted memory handling

Zero-Copy

Minimize copies, cache pressure

Lennart Poettering Practical Real-Time Programming in User-Space on Linux

Reference-counted memory handling

Zero-Copy

Minimize copies, cache pressure

Lennart Poettering Practical Real-Time Programming in User-Space on Linux

Memory locking

To mlockall() or not mlockall()?

madvise() instead? Or temporary mlock()?

Lennart Poettering Practical Real-Time Programming in User-Space on Linux

Memory locking

To mlockall() or not mlockall()?

madvise() instead? Or temporary mlock()?

Lennart Poettering Practical Real-Time Programming in User-Space on Linux

Memory locking

To mlockall() or not mlockall()?

madvise() instead? Or temporary mlock()?

Lennart Poettering Practical Real-Time Programming in User-Space on Linux

Timers and sleeping: high-resolution timers

hrtimers on x86 only, for now

nanosleep(), itimers, POSIX Timers are fine

But what about poll()?

Combine itimers + ppoll() with POSIX real-time signals

Signals are evil!

Beware of old kernels with ppoll()!

Lennart Poettering Practical Real-Time Programming in User-Space on Linux

Timers and sleeping: high-resolution timers

hrtimers on x86 only, for now

nanosleep(), itimers, POSIX Timers are fine

But what about poll()?

Combine itimers + ppoll() with POSIX real-time signals

Signals are evil!

Beware of old kernels with ppoll()!

Lennart Poettering Practical Real-Time Programming in User-Space on Linux

Timers and sleeping: high-resolution timers

hrtimers on x86 only, for now

nanosleep(), itimers, POSIX Timers are fine

But what about poll()?

Combine itimers + ppoll() with POSIX real-time signals

Signals are evil!

Beware of old kernels with ppoll()!

Lennart Poettering Practical Real-Time Programming in User-Space on Linux

Timers and sleeping: high-resolution timers

hrtimers on x86 only, for now

nanosleep(), itimers, POSIX Timers are fine

But what about poll()?

Combine itimers + ppoll() with POSIX real-time signals

Signals are evil!

Beware of old kernels with ppoll()!

Lennart Poettering Practical Real-Time Programming in User-Space on Linux

Timers and sleeping: high-resolution timers

hrtimers on x86 only, for now

nanosleep(), itimers, POSIX Timers are fine

But what about poll()?

Combine itimers + ppoll() with POSIX real-time signals

Signals are evil!

Beware of old kernels with ppoll()!

Lennart Poettering Practical Real-Time Programming in User-Space on Linux

Timers and sleeping: high-resolution timers

hrtimers on x86 only, for now

nanosleep(), itimers, POSIX Timers are fine

But what about poll()?

Combine itimers + ppoll() with POSIX real-time signals

Signals are evil!

Beware of old kernels with ppoll()!

Lennart Poettering Practical Real-Time Programming in User-Space on Linux

Timers and sleeping: high-resolution timers

hrtimers on x86 only, for now

nanosleep(), itimers, POSIX Timers are fine

But what about poll()?

Combine itimers + ppoll() with POSIX real-time signals

Signals are evil!

Beware of old kernels with ppoll()!

Lennart Poettering Practical Real-Time Programming in User-Space on Linux

Mutexes, Semaphores, Conditions

Futexes

Semaphores good, Conditions bad, Mutexes with PI good

Lennart Poettering Practical Real-Time Programming in User-Space on Linux

Mutexes, Semaphores, Conditions

Futexes

Semaphores good, Conditions bad, Mutexes with PI good

Lennart Poettering Practical Real-Time Programming in User-Space on Linux

Mutexes, Semaphores, Conditions

Futexes

Semaphores good

, Conditions bad, Mutexes with PI good

Lennart Poettering Practical Real-Time Programming in User-Space on Linux

Mutexes, Semaphores, Conditions

Futexes

Semaphores good, Conditions bad

, Mutexes with PI good

Lennart Poettering Practical Real-Time Programming in User-Space on Linux

Mutexes, Semaphores, Conditions

Futexes

Semaphores good, Conditions bad, Mutexes with PI good

Lennart Poettering Practical Real-Time Programming in User-Space on Linux

Using poll() and futexes?

FIFOs and eventfd() ... use locking

Compromise: Wrap eventfd()/FIFOs in atomic ops

Lennart Poettering Practical Real-Time Programming in User-Space on Linux

Using poll() and futexes?

FIFOs and eventfd()

... use locking

Compromise: Wrap eventfd()/FIFOs in atomic ops

Lennart Poettering Practical Real-Time Programming in User-Space on Linux

Using poll() and futexes?

FIFOs and eventfd() ... use locking

Compromise: Wrap eventfd()/FIFOs in atomic ops

Lennart Poettering Practical Real-Time Programming in User-Space on Linux

Using poll() and futexes?

FIFOs and eventfd() ... use locking

Compromise: Wrap eventfd()/FIFOs in atomic ops

Lennart Poettering Practical Real-Time Programming in User-Space on Linux

Better solution: kevent

(theoretically)

Allows sleeping on timers, futexes, fds and is lock-free

Lennart Poettering Practical Real-Time Programming in User-Space on Linux

Better solution: kevent (theoretically)

Allows sleeping on timers, futexes, fds and is lock-free

Lennart Poettering Practical Real-Time Programming in User-Space on Linux

Better solution: kevent (theoretically)

Allows sleeping on timers, futexes, fds

and is lock-free

Lennart Poettering Practical Real-Time Programming in User-Space on Linux

Better solution: kevent (theoretically)

Allows sleeping on timers, futexes, fds and is lock-free

Lennart Poettering Practical Real-Time Programming in User-Space on Linux

Lazy binding and relocations

RTLD NOW and $BIND NOW

Lennart Poettering Practical Real-Time Programming in User-Space on Linux

Lazy binding and relocations

RTLD NOW and $BIND NOW

Lennart Poettering Practical Real-Time Programming in User-Space on Linux

Robust RT and security

Watchdog thread vs. RLIMIT CPU and SIGXCPU

RLIMIT RTPRIO RLIMIT RTTIME

Lennart Poettering Practical Real-Time Programming in User-Space on Linux

Robust RT and security

Watchdog thread vs. RLIMIT CPU and SIGXCPU

RLIMIT RTPRIO RLIMIT RTTIME

Lennart Poettering Practical Real-Time Programming in User-Space on Linux

Robust RT and security

Watchdog thread vs. RLIMIT CPU and SIGXCPU

RLIMIT RTPRIO

RLIMIT RTTIME

Lennart Poettering Practical Real-Time Programming in User-Space on Linux

Robust RT and security

Watchdog thread vs. RLIMIT CPU and SIGXCPU

RLIMIT RTPRIO RLIMIT RTTIME

Lennart Poettering Practical Real-Time Programming in User-Space on Linux

How to partition your code best between RT threads and non RTthreads?

It’s difficult!

Lennart Poettering Practical Real-Time Programming in User-Space on Linux

How to partition your code best between RT threads and non RTthreads?

It’s difficult!

Lennart Poettering Practical Real-Time Programming in User-Space on Linux

Avoid in RT:

Disk I/O

Any other kind of blocking I/O

Unbounded algorithms, i.e. slower than O(n)

Code that needs locking – unless there is only one piece ofcode that locks it

Lennart Poettering Practical Real-Time Programming in User-Space on Linux

Avoid in RT:

Disk I/O

Any other kind of blocking I/O

Unbounded algorithms, i.e. slower than O(n)

Code that needs locking – unless there is only one piece ofcode that locks it

Lennart Poettering Practical Real-Time Programming in User-Space on Linux

Avoid in RT:

Disk I/O

Any other kind of blocking I/O

Unbounded algorithms, i.e. slower than O(n)

Code that needs locking – unless there is only one piece ofcode that locks it

Lennart Poettering Practical Real-Time Programming in User-Space on Linux

Avoid in RT:

Disk I/O

Any other kind of blocking I/O

Unbounded algorithms, i.e. slower than O(n)

Code that needs locking – unless there is only one piece ofcode that locks it

Lennart Poettering Practical Real-Time Programming in User-Space on Linux

Splitting code up into non-RT and RT threads comes at a cost:

Context switches; Latency due to buffering; Code becomes a lotmore difficult to understand.

Lennart Poettering Practical Real-Time Programming in User-Space on Linux

Splitting code up into non-RT and RT threads comes at a cost:Context switches

; Latency due to buffering; Code becomes a lotmore difficult to understand.

Lennart Poettering Practical Real-Time Programming in User-Space on Linux

Splitting code up into non-RT and RT threads comes at a cost:Context switches; Latency due to buffering

; Code becomes a lotmore difficult to understand.

Lennart Poettering Practical Real-Time Programming in User-Space on Linux

Splitting code up into non-RT and RT threads comes at a cost:Context switches; Latency due to buffering; Code becomes a lotmore difficult to understand.

Lennart Poettering Practical Real-Time Programming in User-Space on Linux

Thus: in some cases it even makes sense to do non-trivialcalculations in the RT thread. As long as it is bounded by 1/HZ incompletion time. Additional benefit: your code can decide when itis best to execute the non-trivial calculations.

But this is controversial.

Lennart Poettering Practical Real-Time Programming in User-Space on Linux

Thus: in some cases it even makes sense to do non-trivialcalculations in the RT thread. As long as it is bounded by 1/HZ incompletion time. Additional benefit: your code can decide when itis best to execute the non-trivial calculations.

But this is controversial.

Lennart Poettering Practical Real-Time Programming in User-Space on Linux

Debugging RT

Make sure to run a watchdog of some kind.

Possibly a shell with a high real-time priority

Lennart Poettering Practical Real-Time Programming in User-Space on Linux

Debugging RT

Make sure to run a watchdog of some kind.

Possibly a shell with a high real-time priority

Lennart Poettering Practical Real-Time Programming in User-Space on Linux

Debugging RT

Make sure to run a watchdog of some kind.

Possibly a shell with a high real-time priority

Lennart Poettering Practical Real-Time Programming in User-Space on Linux

Profile your code!

Logging in RT programs

Tracing memory allocations

Tracing mutexes

Idea: A module for valgrind that looks for stuff that should not bedone in RT threads

Lennart Poettering Practical Real-Time Programming in User-Space on Linux

Profile your code!

Logging in RT programs

Tracing memory allocations

Tracing mutexes

Idea: A module for valgrind that looks for stuff that should not bedone in RT threads

Lennart Poettering Practical Real-Time Programming in User-Space on Linux

Profile your code!

Logging in RT programs

Tracing memory allocations

Tracing mutexes

Idea: A module for valgrind that looks for stuff that should not bedone in RT threads

Lennart Poettering Practical Real-Time Programming in User-Space on Linux

Profile your code!

Logging in RT programs

Tracing memory allocations

Tracing mutexes

Idea: A module for valgrind that looks for stuff that should not bedone in RT threads

Lennart Poettering Practical Real-Time Programming in User-Space on Linux

Profile your code!

Logging in RT programs

Tracing memory allocations

Tracing mutexes

Idea: A module for valgrind that looks for stuff that should not bedone in RT threads

Lennart Poettering Practical Real-Time Programming in User-Space on Linux

That’s all, folks.

Any questions?

Lennart Poettering Practical Real-Time Programming in User-Space on Linux

That’s all, folks.

Any questions?

Lennart Poettering Practical Real-Time Programming in User-Space on Linux