achieving (hard) real-time with general purpose operating systems

73
Achieving (hard) Real-Time with General Purpose Operating Systems

Post on 19-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Achieving (hard) Real-Time with General Purpose Operating Systems

Achieving (hard) Real-Time with General Purpose

Operating Systems

Page 2: Achieving (hard) Real-Time with General Purpose Operating Systems

Motivation

• There is a conflict between the requirements of real-time systems and the need to run common day applications.

• Can we use a general purpose OS for real-time applications?

Page 3: Achieving (hard) Real-Time with General Purpose Operating Systems

Outline

• Basic definitions

• The challenges

• Can we use existing systems

• Proposed solutions

• Case studies of real-time oss.

Page 4: Achieving (hard) Real-Time with General Purpose Operating Systems

Basic definitions

• A task is the basic unit of execution. Each task has three important properties:1. release time: the point in time from which the task can be executed.2. deadline: the point in time by which the task must complete.3. execution time: the time the task takes to execute

• E.g. brakes controller

Page 5: Achieving (hard) Real-Time with General Purpose Operating Systems

example

• If we have the following set of tasks:

task1: release=0 deadline=3 exec time=2

task2: release=2 deadline=5 exec time=1

task3: release=6 deadline=10 exec time=3

Then they must be scheduled during these periods:

2 3 4 5 6 7 8 9 10 1110

Page 6: Achieving (hard) Real-Time with General Purpose Operating Systems

Basic definitions

• A processor is a concept used to denote a resource that is active in the execution of a task– Usually refers to the CPU.

Page 7: Achieving (hard) Real-Time with General Purpose Operating Systems

Classification of task timing constraints

• There are two types of timing constraints hard and soft.

• Definitions: • if missing a deadline is a fatal error or has

disastrous consequences it is considered hard (missing an occasional deadline is not acceptable)

• otherwise (if some percentage of missed deadlines is acceptable) it is soft.

• Classic examples: braking systems controller => hardmultimedia streaming => soft

Page 8: Achieving (hard) Real-Time with General Purpose Operating Systems

Soft real time systems

• Contain only tasks with no hard timing constraints.

• Also known as “best effort” systems• Most modern operating systems can serve as

the base for a soft real time systems.

• Examples:multimedia transmission and reception, networking, telecom (cellular) networks,web sites and services, computer games.

Page 9: Achieving (hard) Real-Time with General Purpose Operating Systems

Hard real time systems

• Contains tasks with hard timing constraints.

• Requires formal verification/guarantees of being to always meet its hard deadlines (except for fatal errors).

• Examples:air traffic control , vehicle subsystems control, medical systems

Page 10: Achieving (hard) Real-Time with General Purpose Operating Systems

Classifications of real time systems

• The type of system can be either a uni-processor , mp or distributed system.

• Moving to an mp or distributed real time systems adds lots of difficulty by requiring real time communication and synchronization mechanisms between the processors.

Page 11: Achieving (hard) Real-Time with General Purpose Operating Systems

Classifications of real time systems

• There are two different execution models:• In a preemptive model of execution a task

may be interrupted (preempted) during its execution and another task run in its place.

• In a non-preemptive model of execution after a task that starts executing no other task may execute until this task concludes or yields the CPU.

Page 12: Achieving (hard) Real-Time with General Purpose Operating Systems

Classifications of real time systems

• The task model for a real time system has two main types:

1. purely periodic: every task is cyclic and executes periodically, even IO is polled. The nature of tasks doesn’t vary much between cycles.

2. aperiodic/asynchronous: most tasks in the system aren’t periodic.

Page 13: Achieving (hard) Real-Time with General Purpose Operating Systems

Classifications of real time systems

• A static system is a system where all tasks are knows at design time including their release times (full apriory knowledge).

• A dynamic systems is a system that can dynamically create and destroy tasks at runtime (No full apriory knowledge).

Page 14: Achieving (hard) Real-Time with General Purpose Operating Systems

Classifications of real time systems

• In many real time systems the tasks have different priorities.

There are two possible models :

• static-priorities : the priorities of tasks don’t change during execution

• dynamic-priorities the priority of tasks may change during execution

Page 15: Achieving (hard) Real-Time with General Purpose Operating Systems

Classifications of real time systems

• Sets of tasks are classified according to the way they share resources:

• We will call a task set dependent if the tasks in it share resources.

• We will call a task set independent if they do not.

Page 16: Achieving (hard) Real-Time with General Purpose Operating Systems

Outline

• Basic definitions

• The challenges

• Can we use existing systems

• Proposed solutions

• Case studies of real-time oss.

Page 17: Achieving (hard) Real-Time with General Purpose Operating Systems

The main challenges

• Processor sharing – scheduling

• Resource sharing

• Handling external input / communication

Page 18: Achieving (hard) Real-Time with General Purpose Operating Systems

Scheduling

• a scheduler needs to decide when to run each task in order to meet the deadlines imposed on the tasks.

• In order to do this the scheduler needs to base its operation upon the properties of the tasks (release times, deadlines ,etc..).

Page 19: Achieving (hard) Real-Time with General Purpose Operating Systems

Scheduling

• If scheduling is done online, the overhead of scheduling needs to be reasonable (this can be hard because most of the optimal scheduling algorithms are np-hard, even for a single processor).

• If the task model is dynamic we need the scheduler to decide if adding some new task would prevent it from filling obligations to currently running tasks (admissibility testing).

Page 20: Achieving (hard) Real-Time with General Purpose Operating Systems

Resource sharing

• When tasks share resources they become dependant on one another and this limits our flexibility when scheduling, requiring us to use synchronization mechanisms to protect critical sections in the tasks.

• This raises the issues of deadlocks, process starvation and priority inversion.

Page 21: Achieving (hard) Real-Time with General Purpose Operating Systems

Handling external input / communication

• In real time system there will often be constraints on the minimal time until the system processes input/communication.

• These constraints must be reflected in the scheduling of tasks, the handling of external interrupts and the communication infrastructure and protocols chosen.

Page 22: Achieving (hard) Real-Time with General Purpose Operating Systems

Outline

• Basic definitions

• The challenges

• Deficiencies in modern operating systems

• Proposed solutions

• Case studies of real-time oss.

Page 23: Achieving (hard) Real-Time with General Purpose Operating Systems

Why most modern OSs are unsuitable

• The basic guiding concept for most operating systems and schedulers is to strive for good average performance while in real time systems meeting the deadlines is far more important than average performance.

• many modern operating features are problematic in a hard real time setting.

Page 24: Achieving (hard) Real-Time with General Purpose Operating Systems

Problematic modern features

• Protected memory: while important for the stability of the system it carries a high overhead in a real time environment because of the added cost to a context switch and the caches that need to be flushed each context switch ( the tlb for example).

• User and kernel modes: while protecting the systems from user processes adds significant overhead because each mode switch incurs a performance penalty because of the trap instructions and cache flushes.

• memory paging: can increase the cost of a context switch by several magnitudes if the memory pages we need were switched out. This causes a lot of unpredictability in the timing.

Page 25: Achieving (hard) Real-Time with General Purpose Operating Systems

Problematic modern features

• Dynamic memory allocation: this is a critical feature used by most modern application, however it creates memory fragmentation and as a result adds to timing unpredictability.

Page 26: Achieving (hard) Real-Time with General Purpose Operating Systems

Linux

• Linux Is based on UNIX which is a time sharing system designed to optimize average performance (as weighted by priorities) and Linux preserves this nature. This means that as the load increases the real time processes will suffer the most.

• Linux currently doesn’t support an interface for telling the OS that a task is real time or informing it of any timing constraints on a task.

Page 27: Achieving (hard) Real-Time with General Purpose Operating Systems

Linux

• The basic Linux scheduler is a time slice based on priorities, however it is a “fair” scheduler and favors interactive (IO bound) programs rewarding programs who haven’t used up all of their CPU share.

• This presents several problems:1. the time slice resolution may be higher than requires for the timing and so it may be impossible to meet the timing demands.2. raising the priority of real time tasks is insufficient because enough lower priority tasks will cause us to miss deadlines anyway.3.fairness is not a desirable quality in real-time systems.

Page 28: Achieving (hard) Real-Time with General Purpose Operating Systems

Linux

• Linux does not provide a reliable mechanism to wake a task up at a certain time. The sleep timer can only promise to wake a task up after a certain time.

• The kernel can’t be preempted so long system calls executed in the kernel can interfere with timing constraints.

• External interrupts are disabled at several sections in the kernel to protect OS data from corruption (saves the effort of making them reentrant). This adds unpredictability to the amount of time it takes to respond to IO.

Page 29: Achieving (hard) Real-Time with General Purpose Operating Systems

Linux – kernel 2.6 changes

• These has been some progress on some of these issues in the 2.6 Linux kernel:

1.the kernel is now preemptable although not all of it is.

2.improvements in IO handling.

• despite these changes 2.6 is still not suitable for hard real time performance.

Page 30: Achieving (hard) Real-Time with General Purpose Operating Systems

Windows

• Windows (NT based) suffers from most of the same problems Linux does.

• Windows doesn’t support the specification of timing constraints for tasks.

Page 31: Achieving (hard) Real-Time with General Purpose Operating Systems

Windows

• Interrupts may be delayed for an arbitrarily long time in critical sections of the kernel.

• Handling of interrupts is done in two parts the second part the deferred procedure call is executed later in a FIFO manner and this may cause delays if a lot of DPCs are awaiting dispatch.

• Requests for synchronization objects such as semaphores are also done in FIFO, leading to similar unpredictable delays.

Page 32: Achieving (hard) Real-Time with General Purpose Operating Systems

Windows

• The windows scheduler supports threads on an OS level. Each thread is given a priority (0-31).

• The scheduler has compensation for input bound threads and anti-starvation mechanisms.

• Windows has a “real time” priority for threads but other real-time threads and the kernel can preempt a task with real time priority (15-26).

• A “real time” priority is higher than some parts of the OS, causing most non-real-time apps to become unusable

Page 33: Achieving (hard) Real-Time with General Purpose Operating Systems

Fixing the problems

• Fixing these problems in either OS would require either some form of “hack” into the operating systems design or a massive rewrite of the kernel.

• This has been tried using both of the operating systems as we will see in later case studies (rt-linux, rialto).

Page 34: Achieving (hard) Real-Time with General Purpose Operating Systems

Outline

• Basic definitions

• The challenges

• Deficiencies in modern operating systems

• Proposed solutions

• Case studies of real-time OSs.

Page 35: Achieving (hard) Real-Time with General Purpose Operating Systems

Scheduling

• Scheduling algorithm properties:

1. An algorithm is said to be optimal if for each task set for which a feasible scheduling is possible the algorithm will find one (assumes tasks are independent).

2. Schedulable utilization is the maximal utilization of a task set such that a task load under this utilization will be scheduled successfully by the algorithm.

Page 36: Achieving (hard) Real-Time with General Purpose Operating Systems

Cyclic executive

• This is a static table driven approach to scheduling. It assumes full apriori knowledge meaning tasks are static and periodic.

• The schedule is created offline, so scheduling doesn’t load the system during runtime.

• performance guarantees can be given before system is built.

• The “algorithm”: analyze and schedule tasks offline using some algorithm which creates a static schedule for running all tasks during runtime.

Page 37: Achieving (hard) Real-Time with General Purpose Operating Systems

Cyclic executive

• The cyclic executive approach can incorporate tasks of which there is no foreknowledge by scheduling them in empty spaces in the cycle, however it can’t make any guarantees about their timing. This approach works for the other algorithms as well.

Page 38: Achieving (hard) Real-Time with General Purpose Operating Systems

Priority driven algorithms

• This is a family of algorithms which gives the tasks priorities according to some criterion and schedules at each point the task with the highest priority. They are efficient in that if there are tasks ready to execute the CPU is always busy.

• This approach is attractive because in case of performance degradation the most important tasks will be affected the least.

Page 39: Achieving (hard) Real-Time with General Purpose Operating Systems

Rate monotonic

• This algorithm assumes a periodic task model with preemption.

• The Algorithm:

is a priority driven algorithm where the tasks are assigned priorities proportional to their cycle length, the shorter the cycle the higher the priority.

Page 40: Achieving (hard) Real-Time with General Purpose Operating Systems

Rate monotonic

• The algorithm is optimal in a way: if there is a feasible schedule for a periodic task load where Pi<=Di on a single processor with fixed priorities then RM will find a feasible schedule.

• Liu and Leyland show that the schedulable utilization of RM is bounded by :

where N is the number of real time tasks

)12( /1

1

NN

i i

iN

period

executionU

69.0~)12(lim /1

N

n

Page 41: Achieving (hard) Real-Time with General Purpose Operating Systems

example

• If we have the following set of tasks:

task1: period=deadline=3 execution time=1

task2: period=deadline=5 execution time=3

Then the schedule will look like this:

2 3 4 5 6 7 8 9 1010

1

1

2

1

3 4

2 2

Page 42: Achieving (hard) Real-Time with General Purpose Operating Systems

The problem with static priority driven algorithms

• This simple example shows that a schedulable set of tasks can’t be scheduled based on static priorities

2 3 4 5 6 7 8 9 1010

1

1

2 3

1

task1: period=deadline=4 execution time=2

task2: period=deadline=10 execution time=5 Task 1 must have higher priority

Task 2 must have higher priority

Page 43: Achieving (hard) Real-Time with General Purpose Operating Systems

EDF algorithm

• The Earliest Deadline First algorithm is a priority driven algorithm in which the tasks get priorities according to how close their nearest deadline is. The closer the deadline the higher the priority.

Page 44: Achieving (hard) Real-Time with General Purpose Operating Systems

example

• If we have the following set of tasks:

task1: period=deadline=3 execution time=1

task2: period=deadline=5 execution time= 3

Then the schedule will look like this:

2 3 4 5 6 7 8 9 1010

1

1

2 3 4

2 2

Page 45: Achieving (hard) Real-Time with General Purpose Operating Systems

EDF properties

• The algorithm is optimal.• The schedulable utilization of the algorithm

is 1 .• Priorities must only be recalculated when

a new task arrives, as time advances equally for all tasks.

• the algorithm behaves unpredictably at overloads, and suffers more from overloads than RM.

Page 46: Achieving (hard) Real-Time with General Purpose Operating Systems

LST algorithm

• The slack time of a task is the amount of time until the task must be scheduled or miss it’s deadline.

• The least slack time algorithm is a priority driven algorithm in which the tasks receive priorities according the their slack times. The shorter the slack time the higher the priority.

• The LST algorithm is optimal

Page 47: Achieving (hard) Real-Time with General Purpose Operating Systems

Priority inversion

• The priority inversion problem exists in the preemptive model when tasks are dependant on one another (share resources). it causes a high priority task to wait for a low priority task to conclude because it is holding a critical resource.

• This problem can be at least partially solved by priority inheritance, or the priority ceiling protocol based on a similar concept.

Page 48: Achieving (hard) Real-Time with General Purpose Operating Systems

Outline

• Basic definitions

• The challenges

• Deficiencies in modern operating systems

• Proposed solutions

• Case studies of real-time OSs

Page 49: Achieving (hard) Real-Time with General Purpose Operating Systems

Case study

• We will go over how some actual implementations of real time operating systems tackled these problems.

• Rt-Linux

• Rialto

• VxWorks

Page 50: Achieving (hard) Real-Time with General Purpose Operating Systems

Rt-linux

• RT-Linux is a system developed as a way to adapt Linux to hard real time performance and still give the user most of the advantages of the Linux environment.

• The main idea: add a hard real time scheduler into (under) the kernel which will schedule the real time tasks and will run the rest of Linux as the lowest priority task.

Page 51: Achieving (hard) Real-Time with General Purpose Operating Systems

Rt-linux

• the main philosophy is this:Real time programs need to be split into parts , a small light parts with hard real time constraints and larger parts that do most of the processing.

• the light real time parts will be scheduled in the real time scheduler, while the heavier parts will run as normal processes under Linux.

• The two parts can communicate through a real-time-FIFO, a non-blocking queue which allows the real time parts to read and write data from normal processes.

Page 52: Achieving (hard) Real-Time with General Purpose Operating Systems

Rt-linux

• Real time tasks are implemented as kernel modules whose init function sends the real time scheduler all the information about their release times, periods and deadlines.

• Rt-Linux allows for swappable schedulers for the real time tasks, a scheduler based on the EDF algorithm and another based on the RM algorithm.

• Linux is run as the lowest priority process in either case and will only execute if the real time scheduler has nothing else to do.When Linux is running it uses it’s own scheduler to schedule tasks running under it.

Page 53: Achieving (hard) Real-Time with General Purpose Operating Systems

Rt-Linux

• The Linux kernel runs as a process under the real time scheduler and as a result can be preempted by it at any time, and while this allows for good sleep timers and scheduling flexibility it dictates that system calls can’t be used by real time tasks in order to preserve the consistency of the kernels internal state.

Page 54: Achieving (hard) Real-Time with General Purpose Operating Systems

Rt-Linux

• All the memory needs of the real time processes are handled by the real time subsystem (no system calls) and memory is allocated in non-pageable blocks saving the uncertainty of swap file usage.

• As all the real time tasks run in the same kernel memory space we lose the benefits of protected memory for them but context switches are much faster because we save the cost of flushing the TLB cache and using trap instructions to enter and exit kernel mode for scheduling.

Page 55: Achieving (hard) Real-Time with General Purpose Operating Systems

Rt-Linux

• A very serious problem is the disabling of interrupts by the kernel this is handled by replacing the function call to disable interrupts in the kernel source with a macro that simply masks these interrupts from the Linux process but not from the other real time processes, so interrupts are never blocked as far as the real time scheduler or tasks are concerned.

• The masking of the interrupts is done by having the real time portions of the system initially handle all interrupts and pass the ones intended to the Linux process on to it as software interrupts (in case Linux thinks the interrupts are enabled).

Page 56: Achieving (hard) Real-Time with General Purpose Operating Systems

Rt-Linux - limitations

• Doesn’t handle the problem of scheduling dependant real time tasks.

• Not all applications can be split into a light real-time parts and non-real-time parts.

• Not suited for dynamic real time systems.

Page 57: Achieving (hard) Real-Time with General Purpose Operating Systems

Rt-Linux - summary

• Demonstrates the “hack in to the architecture” approach to adding real time.

• Is a functional system used in the wild.

• It’s requirements form the real time portions of the tasks may make it unsuitable for some applications.

Page 58: Achieving (hard) Real-Time with General Purpose Operating Systems

Rialto

• Rialto is a real time system developed at Microsoft Research based on the windows NT architecture.

• The main idea: rewriting the kernel and scheduler so that they support real time deadlines.

Page 59: Achieving (hard) Real-Time with General Purpose Operating Systems

Rialto

• Unlike rt-linux it doesn’t require the separation of the hard real time functionality from the rest of the code.

• It uses the beginConstraint and endConstriant function calls that pass the parameters of the real time portion of task to the scheduler.

Page 60: Achieving (hard) Real-Time with General Purpose Operating Systems

Rialto

• The parameters passed to the scheduler:- Release time

- Deadline

- Running time estimate

- Criticality (critical=hard constraint , non-critical=soft )

• The scheduler returns the feasibility of scheduling the task. This allows the task to perform load shedding if possible, or fail gracefully if not.

Page 61: Achieving (hard) Real-Time with General Purpose Operating Systems

Rialto

• the scheduling in Rialto is quite complicated:1. Tasks can be granted CPU time reservations.

This is the average amount of CPU time they should get.

2. The scheduler is based on a variation of preemptive LST which uses the maximal amount of time before a task has to start running (deadline-running time estimate).

3. Critical tasks always run before non-critical tasks.

Page 62: Achieving (hard) Real-Time with General Purpose Operating Systems

Rialto

• The rialto system is system-resource aware.

• Tasks can negotiate with the resource manager in order to reserve the resources they need.

• The resource manager arbitrates the use of resources, and can cause an existing task to relinquish resources allocated to it.

Page 63: Achieving (hard) Real-Time with General Purpose Operating Systems

Rialto - limitations

• Rialto doesn’t handle the problem of the kernel disabling interrupts.

• The schedulability test was not fully implemented originally.

Page 64: Achieving (hard) Real-Time with General Purpose Operating Systems

Rialto -summary

• Rialto uses the approach of rewriting an existing kernel in order to allow support for real time performance.

• Built as “academic” work ,was never widely used.

• Uses a novel scheduling approach, But because it is complicated it is sensitive to many implementation details.

Page 65: Achieving (hard) Real-Time with General Purpose Operating Systems

VxWorks

• VxWorks is a commercial hard real time operating system developed by wind river systems.

• The main idea: use monolithic kernel to schedule user tasks according to user defined priorities. Maximize kernel timing predictability.

• Gives the users maximal control.

Page 66: Achieving (hard) Real-Time with General Purpose Operating Systems

VxWorks

• A dedicated real time system, not intended as a general purpose OS.

• lacks many modern os features that interfere with real time performance (flat memory model, no paging).

Page 67: Achieving (hard) Real-Time with General Purpose Operating Systems

VxWorks

• Scheduling is done using a preemptive priority driven approach, priorities are chosen arbitrarily by the user (0-256).

• Priorities can be changed by the user at runtime but this is discouraged.

• A user can lock a task so that it can’t be preempted even by higher priority tasks or interrupts.

• This allows the use of the fixed priority response time analysis to check schedulability offline.

Page 68: Achieving (hard) Real-Time with General Purpose Operating Systems

VxWorks

• Is resource sharing aware and has a priority inheritance built in.

• Optimizations in implementation of the context switch and the return from interrupts.

• The kernel never disables NMI (non-maskable interrupts) so they are always available to the user.

Page 69: Achieving (hard) Real-Time with General Purpose Operating Systems

VxWorks - limitations

• Lacks many modern OS features.• Guaranteeing the deadlines is the

responsibility of the user at design time.• Doesn’t support most modern applications

and APIs (only a small subset of POSIX).• Despite the flat memory model dynamic

memory allocation still cases memory fragmenting, which increases timing unpredictability.

Page 70: Achieving (hard) Real-Time with General Purpose Operating Systems

VxWorks - summary

• A dedicated and widely used real time system.

• Offers the user maximal control , but also passes him responsibility for the deadlines.

• Lacks many modern OS features.

Page 71: Achieving (hard) Real-Time with General Purpose Operating Systems

Summary

• This talk reviewed:

- The basic concepts of real time systems

- The challenges of building a real time system

- The use of generic OSs as real time systems

- Some Solutions to the Challenges.

- The approaches taken by some real systems

Page 72: Achieving (hard) Real-Time with General Purpose Operating Systems

questions

Page 73: Achieving (hard) Real-Time with General Purpose Operating Systems