TDDC 47 Realtime and Concurrent Programming Lecture 5

  • Slides: 45
Download presentation
TDDC 47: Real-time and Concurrent Programming Lecture 5: Real-time Scheduling (I) Simin Nadjm-Tehrani Real-time

TDDC 47: Real-time and Concurrent Programming Lecture 5: Real-time Scheduling (I) Simin Nadjm-Tehrani Real-time Systems Laboratory Department of Computer and Information Science Linköping university Undergraduate course on Real-time Systems Linköping 1 of 45 Autumn 2009

This lecture • Course overview • Introduction to Real-time systems • CPU as a

This lecture • Course overview • Introduction to Real-time systems • CPU as a resource: Scheduling – Cyclic scheduling – Rate monotonic scheduling Undergraduate course on Real-time Systems Linköping 2 of 45 Autumn 2009

TDDC 47: Where are we? • You have studied theory of concurrent processes, including

TDDC 47: Where are we? • You have studied theory of concurrent processes, including ME • Performed labs on control processes controller 1 Servo controller 2 • Mutual exclusion of control signals from each process Undergraduate course on Real-time Systems Linköping 3 of 45 Autumn 2009

Next part of the course Controller 1 Servo 1 Controller 2 Servo 2 Controller

Next part of the course Controller 1 Servo 1 Controller 2 Servo 2 Controller 3 Servo 3 Processor How does sharing CPU time affect the outcome? Undergraduate course on Real-time Systems Linköping 4 of 45 Autumn 2009

Evaluation actions • TDDC 47: – After muddy cards (ht 1) – Added 4

Evaluation actions • TDDC 47: – After muddy cards (ht 1) – Added 4 h more lab time – Have only received 2 answers to the test Keep working on solutions! Undergraduate course on Real-time Systems Linköping 5 of 45 Autumn 2009

This lecture • Course overview • Introduction to Real-time systems • CPU as a

This lecture • Course overview • Introduction to Real-time systems • CPU as a resource: Scheduling – Cyclic scheduling – Rate monotonic scheduling Undergraduate course on Real-time Systems Linköping 6 of 45 Autumn 2009

Real-time processes • From your operating systems course: scheduler’s role is to ensure that

Real-time processes • From your operating systems course: scheduler’s role is to ensure that each process gets a share of the CPU • With real-time systems it is not enough that processes get a share some time The time that the result of the computation is delivered is as important as the result itself • Predictability! Undergraduate course on Real-time Systems Linköping 7 of 45 Autumn 2009

Predictable is not “fast”! The film. . . Undergraduate course on Real-time Systems Linköping

Predictable is not “fast”! The film. . . Undergraduate course on Real-time Systems Linköping 8 of 45 Autumn 2009

What is meant by predictable? Release time Computation time Deadline time External event Reaction

What is meant by predictable? Release time Computation time Deadline time External event Reaction Undergraduate course on Real-time Systems Linköping 9 of 45 Autumn 2009

Order matters! t 0 p 1 p 2 time p 1 Undergraduate course on

Order matters! t 0 p 1 p 2 time p 1 Undergraduate course on Real-time Systems Linköping time 10 of 45 Autumn 2009

Deadlines • Hard: Not meeting any deadline is a failure of the system •

Deadlines • Hard: Not meeting any deadline is a failure of the system • Soft: It is desirable that deadlines are met, but OK if they are missed every now and then • Firm: It is OK that they are missed now and again, but after the deadline the result is of no use Undergraduate course on Real-time Systems Linköping 11 of 45 Autumn 2009

Typical application area • Vehicle electronics – Power train and chassis – Infotainment/telematics –

Typical application area • Vehicle electronics – Power train and chassis – Infotainment/telematics – Body electronics • A modern car configuration has over 40 ECUs, distributed over several buses • Several applications share each ECU that shares the bus Undergraduate course on Real-time Systems Linköping 12 of 45 Autumn 2009

This lecture • Course overview • Introduction to Real-time systems • CPU as a

This lecture • Course overview • Introduction to Real-time systems • CPU as a resource: Scheduling – Cyclic scheduling – Rate monotonic scheduling Undergraduate course on Real-time Systems Linköping 13 of 45 Autumn 2009

Scheduling. . . is about allocating resources, specially the CPU time, among all computational

Scheduling. . . is about allocating resources, specially the CPU time, among all computational processes such that the timeliness requirements are met. The scheduler decides which process is to be executed next. Undergraduate course on Real-time Systems Linköping 14 of 45 Autumn 2009

Scheduling • Performed off-line or on-line • With information available statically or dynamically •

Scheduling • Performed off-line or on-line • With information available statically or dynamically • Preemptive or non-preemptive Undergraduate course on Real-time Systems Linköping 15 of 45 Autumn 2009

Schedulability Test • Sufficient – if test is passed, then tasks are definitely schedulable

Schedulability Test • Sufficient – if test is passed, then tasks are definitely schedulable – if test is not passed, we don’t know • Necessary – if test is passed, we don’t know – if test is not passed, tasks are definitely not schedulable • Exact test: – sufficient & necessary at the same time Undergraduate course on Real-time Systems Linköping 16 of 45 Autumn 2009

Which parameters? Scheduling policy induces an order on executions using an algorithm and a

Which parameters? Scheduling policy induces an order on executions using an algorithm and a set of parameters for the task set: • • Worst case execution time (WCET) Deadline Release time. . . Undergraduate course on Real-time Systems Linköping 17 of 45 Autumn 2009

Process parameters • How to find the maximum computation time for each process? •

Process parameters • How to find the maximum computation time for each process? • How to determine deadlines? • When (how often) is a process released? Undergraduate course on Real-time Systems Linköping 18 of 45 Autumn 2009

Release times • Reading and reacting to continuous signals – Periodicity • Recognising/reacting to

Release times • Reading and reacting to continuous signals – Periodicity • Recognising/reacting to some aperiodic events – Minimum inter-arrival time Sporadic processes Undergraduate course on Real-time Systems Linköping 19 of 45 Autumn 2009

Cyclic scheduling • An off-line schedule is created based on statically known and fixed

Cyclic scheduling • An off-line schedule is created based on statically known and fixed parameters • Off-line decision – When executing: Run the processes in pre-determined order using a table look-up • To run processes in the “right” frequency find – Minor cycle – Major cycle Undergraduate course on Real-time Systems Linköping 20 of 45 Autumn 2009

Example (1) Consider following processes: P 1 P 2 Period(Ti)/Deadline Worst case execution time

Example (1) Consider following processes: P 1 P 2 Period(Ti)/Deadline Worst case execution time (Ci) 50 10 100 30 Note: repetition! 0 50 100 150 Undergraduate course on Real-time Systems Linköping 200 250 21 of 45 Autumn 2009

A cyclic executive every_major_cycle do{ read all in_signals; run_minor_cycle_1_processes; wait_for_interrupt; write all out_signals; .

A cyclic executive every_major_cycle do{ read all in_signals; run_minor_cycle_1_processes; wait_for_interrupt; write all out_signals; . . . read all in_signals; run_minor_cycle_n_processes; wait_for_interrupt; write all out_signals; } Undergraduate course on Real-time Systems Linköping 22 of 45 Autumn 2009

No preemption! Undergraduate course on Real-time Systems Linköping 23 of 45 Autumn 2009

No preemption! Undergraduate course on Real-time Systems Linköping 23 of 45 Autumn 2009

Finding Minor/Major Cycle Minor cycle: greatest common devisor (sv. sgd) Major cycle: least common

Finding Minor/Major Cycle Minor cycle: greatest common devisor (sv. sgd) Major cycle: least common multiplier (sv. mgn) Example (2): process period Undergraduate course on Real-time Systems Linköping A 20 B 40 C 60 24 of 45 Autumn 2009

Iterative construction • Off-line analysis in order to fix the schedule might be iterative

Iterative construction • Off-line analysis in order to fix the schedule might be iterative – Each process Pi is assumed to be strictly periodic (i. e. should be completed once every Ti) – Check: Will the schedule work with the natural periods and computation times? – Otherwise, change the parameters! • Which parameters can we change? Undergraduate course on Real-time Systems Linköping 25 of 45 Autumn 2009

Harmonic processes Recall example 2: process period A 20 Undergraduate course on Real-time Systems

Harmonic processes Recall example 2: process period A 20 Undergraduate course on Real-time Systems Linköping B 40 C 60 26 of 45 Autumn 2009

What if periods are not harmonic? Undergraduate course on Real-time Systems Linköping 27 of

What if periods are not harmonic? Undergraduate course on Real-time Systems Linköping 27 of 45 Autumn 2009

How to construct? • If the periods are not harmonically related – change the

How to construct? • If the periods are not harmonically related – change the periods – all processes should be run at least as often as every (original) Ti • Place the processes in minor cycle and major cycle until repetition appears Undergraduate course on Real-time Systems Linköping 28 of 45 Autumn 2009

Example (3. 1) process period A 75 B 100 Alternative 1: Run process B

Example (3. 1) process period A 75 B 100 Alternative 1: Run process B more often than necessary, e. g. once every 75 time units. minor cycle 0 75 Undergraduate course on Real-time Systems Linköping Drawbacks? . . . time 29 of 45 Autumn 2009

Example (3. 2) process period A 75 B 100 Alternative 2: Choose minor cycle

Example (3. 2) process period A 75 B 100 Alternative 2: Choose minor cycle as greatest common divisor, and move processes backwards in time when they clash. Drawbacks? . . . Undergraduate course on Real-time Systems Linköping 30 of 45 Autumn 2009

Jitter control • Many applications need to minimise jitter in reading data from sensors

Jitter control • Many applications need to minimise jitter in reading data from sensors or producing output to actuators Undergraduate course on Real-time Systems Linköping 31 of 45 Autumn 2009

Example (3. 3) process period A 75 B 100 Alternative 3: A mix of

Example (3. 3) process period A 75 B 100 Alternative 3: A mix of the last two Drawbacks? minor cycle Undergraduate course on Real-time Systems Linköping 32 of 45 Autumn 2009

Schedulability test • Sum of processes’ execution times (WCET) in each minor cycle is

Schedulability test • Sum of processes’ execution times (WCET) in each minor cycle is less than the cycle’s length, and processes run at the ”right” frequency If succeeded: • the schedule, whose length corresponds to the major cycle, is repeated for all executions Undergraduate course on Real-time Systems Linköping 33 of 45 Autumn 2009

If they don’t fit? • Break some process that does not fit into two

If they don’t fit? • Break some process that does not fit into two or more processes and run the different parts in different minor cycles Creates new processes out of the old one! Drawbacks? Undergraduate course on Real-time Systems Linköping 34 of 45 Autumn 2009

What if dependent? • So far we assumed all processes are independent. • Dependence

What if dependent? • So far we assumed all processes are independent. • Dependence can be due to sharing resources or computation precedence requirements • In either case, the fixed order has to respect dependencies Undergraduate course on Real-time Systems Linköping 35 of 45 Autumn 2009

Summary • Cycles can be hard to determine and can become looong. . .

Summary • Cycles can be hard to determine and can become looong. . . • Very inflexible • Can lead to high processor utilisation • Long WCET can create problems • Sporadic processes are run periodically Undergraduate course on Real-time Systems Linköping 36 of 45 Autumn 2009

But. . . • Simple at run-time • No overheads for context switching •

But. . . • Simple at run-time • No overheads for context switching • Processes can exchange data without the need for explicit (dynamic) synchronisation Undergraduate course on Real-time Systems Linköping 37 of 45 Autumn 2009

Run-time behaviour • What is the deadline for each process? • How does one

Run-time behaviour • What is the deadline for each process? • How does one know that processes meet their deadlines? • What happens if they don’t? Undergraduate course on Real-time Systems Linköping 38 of 45 Autumn 2009

Better methods needed For: • Processes with long WCET • Sporadic events • Processes

Better methods needed For: • Processes with long WCET • Sporadic events • Processes with long period but short deadline • Process dependence – sharing resources – overruns Undergraduate course on Real-time Systems Linköping 39 of 45 Autumn 2009

Priority-based scheduling • A preemptive method where the priority of the process determines whether

Priority-based scheduling • A preemptive method where the priority of the process determines whether it continues to run or it is disrupted ”Most important process first!” Undergraduate course on Real-time Systems Linköping 40 of 45 Autumn 2009

RMS Rate Monotonic Scheduling: • On-line • Preemptive • Priority-based with fixed (static) priorities

RMS Rate Monotonic Scheduling: • On-line • Preemptive • Priority-based with fixed (static) priorities Undergraduate course on Real-time Systems Linköping 41 of 45 Autumn 2009

Priorities • Each process has a period Ti that is the shortest interval between

Priorities • Each process has a period Ti that is the shortest interval between its release times • Processes are assigned priorities dependent on length of Ti – The shorter Ti the higher the priority Undergraduate course on Real-time Systems Linköping 42 of 45 Autumn 2009

Example (4) Undergraduate course on Real-time Systems Linköping 43 of 45 Autumn 2009

Example (4) Undergraduate course on Real-time Systems Linköping 43 of 45 Autumn 2009

Consider following scenario: arrival time 0 20 30 40 50 60 process P 1,

Consider following scenario: arrival time 0 20 30 40 50 60 process P 1, P 2, P 3 P 1 P 2 P 1, P 3 preemption 0 10 20 30 40 Undergraduate course on Real-time Systems Linköping . . . 50 60 80 90 time 44 of 45 Autumn 2009

Schedulability test Theorem: (sufficient condition) For n processes, RMS will guarantee their schedulability if

Schedulability test Theorem: (sufficient condition) For n processes, RMS will guarantee their schedulability if the total utilisation U = C 1/T 1 +. . . + Cn/Tn does not exceed the guarantee level G = n (2 1/n -1) Undergraduate course on Real-time Systems Linköping 45 of 45 Autumn 2009