CSCI 1600 Embedded and Real Time Software Lecture
CSCI 1600: Embedded and Real Time Software Lecture 17: Real Time Scheduling I Steven Reiss, Fall 2019
Scheduling Problem Arduino Main Loop Round-robin scheduler (with interrupts) No preemption Assuming not that much is time critical Possibly one or two interrupt routines What happens in a more complex system We first must understand the components What happens when things change This is what a RT operating system provides Problem: given a set of tasks How can these be scheduled What is a task? 2 Lecture 26: Schedulilng I 3/3/2021
Periodic Tasks Execution time (e units) : max execution time Release time (t – when it becomes available to run) Period p: the time between releases Deadline (d – relative to release time) How quickly it needs to be processed Default = period P(p, e, d) : representation of a job P(p, e) = P(p, e, p) Goal: ensure all deadlines are met 3 Lecture 26: Schedulilng I 3/3/2021
Aperiodic Tasks Background tasks that run occasionally Arise at random (possibly with a distribution) Soft deadlines Can have priorities Release time, deadline, execution time GOAL: Execute as fast as possible Average or worst case time 4 Lecture 26: Schedulilng I 3/3/2021
Sporadic Tasks Response to interrupts or unusual events Arise randomly (possibly with a distribution) Hard deadlines Again have release time, deadline, execution time GOAL: ensure these can be serviced correctly Accept or reject the job 5 Lecture 26: Schedulilng I 3/3/2021
Simplifying Assumptions All jobs are preemptable at any time Relaxations: Not preemptable until non-runnable Preempt at “safe” times (calling OS, acquiring synchronization) Switching overhead is 0 No resource contention between jobs (no blocking) Jobs don’t suspend voluntarily Job execution time (max) is known Simple processor; single processor Jobs are perfect (i. e. always available on time, always obey limits) All of these can be taken into account, but things are more complex 6 Lecture 26: Schedulilng I 3/3/2021
Scheduling Problem Given a set of periodic tasks, can they be scheduled Can aperiodic tasks at a given rate be handled Can all sporadic tasks be handled Can we accept/reject a sporadic task when it arises Need to make this decision when job comes We first need to decide what type of solution we want What types are there? 7 Lecture 26: Schedulilng I 3/3/2021
Solution Models: Fixed Schedule Also call clock-based Single schedule Schedule is precomputed, stored in a table Time allocated for aperiodic and sporadic tasks Can be fully static (all decisions made in advance) Can be static order Order precomputed When to run decided at run time 8 Lecture 26: Schedulilng I 3/3/2021
Solution Models: Fixed Priority Assign priority to jobs as they arise Priorities don’t change once they are assigned Low numbers are higher priority Highest priority job runs Schedule reexamined as new jobs come in Aperiodic and sporadic jobs are accommodated Either directly by priority Or by creating a periodic job for them 9 Lecture 26: Schedulilng I 3/3/2021
Solution Models: Dynamic Priority Jobs are assigned priorities as they arise But job priorities can be changed over time Highest priority job runs Schedule checked as new jobs arise Schedule checked periodically Jobs can change priorities Accommodate aperiodic and sporadic jobs 10 Lecture 26: Schedulilng I 3/3/2021
Solution Models: Advanced Multiple threads/cores/processors Handling aperiodic and sporadic tasks Relaxing assumptions Resource scheduling 11 Lecture 26: Schedulilng I 3/3/2021
Example Consider three jobs P 1(4, 2), P 2(6, 2), P 3(12, 2) P[period; execution_time; deadline ] P[p, e] => deadline = period Can we schedule these? How many time units do we have to consider? 12 Lecture 26: Schedulilng I 3/3/2021
Schedule Representations P 1(4, 2), P 2(6, 2), P 3(12, 2) 13 Lecture 26: Schedulilng I 3/3/2021
Schedule Representations P 1(4, 2), P 2(6, 2), P 3(12, 2) P 1 P 2 P 3 14 Lecture 26: Schedulilng I 3/3/2021
Scheduling Example P 1(4, 1), P 2(6, 2), P 3(12, 3) 15 Lecture 26: Schedulilng I 3/3/2021
Schedule Representations P 1(4, 1), P 2(6, 2), P 3(12, 3) 16 Lecture 26: Schedulilng I 3/3/2021
Scheduling Example P 1(4, 2), P 2(6, 3), P 3(12, 1) 17 Lecture 26: Schedulilng I 3/3/2021
Clock-Based Scheduling Set up fixed schedule in advance Problem is NP-complete but can be solved Done once off-line Still need to handle aperiodic and sporadic tasks Need to leave slack in schedule We’ll get back to this Pros and Cons Simple to implement Can use an optimal schedule Assumes release times are fixed (no jitter) Everything must be known in advance Changing anything can be messy 18 Lecture 26: Schedulilng I 3/3/2021
Fixed Priority Scheduling Basic Idea Assign a priority to a job as it arrives Not necessarily based on its a priority Based on scheduling requirements Job with highest priority is run If a new job gets higher priority, it preempts running job Implementation Maintain a priority queue of jobs New jobs are inserted into the queue And may cause preemption 19 Lecture 26: Schedulilng I 3/3/2021
Fixed Priority Assignment Problem: how to assign priorities Rate Monotonic (RM) priority assignment Priority is the period Shortest task gets the highest priority Deadline Monotonic (DM) priority assignment Priority is the deadline Jobs with shortest relative deadline gets highest priority If deadline = period, these are the same 20 Lecture 26: Schedulilng I 3/3/2021
Rate Monotonic Example P 1(4, 1), P 2(5, 2), P 3(20, 5) 21 Lecture 26: Schedulilng I 3/3/2021
Feasibility Analysis How good is a RM/DM schedule? What do you want to measure? Utilization of the CPU Does the algorithm produce a feasible schedule If the jobs are schedulable Under what circumstances Necessary and Sufficient Schedule All cycles are needed to service jobs and there are enough cycles Achieving this is NP-hard. Settle for sufficient (enough cycles) 22 Lecture 26: Schedulilng I 3/3/2021
Critical Instant Theorem The critical instant for a job is the worst time to start it The release time for which response is maximized This can be formalized Theorem: The critical instant occurs (for periodic tasks with fixed priorities) when all higher priority jobs share the release time with the job in question This is fairly robust : stays true even if we remove simplifying assumptions 23 Lecture 26: Schedulilng I 3/3/2021
Checking a Schedule Notation τi – the ith task or the ith execution of task τ Di – the relative deadline for the ith task Pi – the period of the ith task ei – the execution time of the ith task 24 Lecture 26: Schedulilng I 3/3/2021
Checking a Schedule Simple necessary and sufficient test of feasibility Assume synchronous release of all jobs Release jobs from all tasks at time t 0 Simulate until lowest priority job completes or misses If job completes, then the schedule is feasible If job fails, then the schedule doesn’t work Simulate until back at the start Simulate for LCM of the periods 25 Lecture 26: Schedulilng I 3/3/2021
RM Priority Assignment If a task set can be scheduled at all by a fixed priority schedule It can be scheduled with a RM (or DM) policy This can be proven fairly easily Hence RM (or DM) is optimal in some sense Define the utility of a task ui = ei/Pi The utility of a set of tasks is their sum If sum > 1 then the jobs can’t be scheduled If sum = 1 and tasks can be scheduled, algorithm is optimal What can be done using RM/DM scheduling? 26 Lecture 26: Schedulilng I 3/3/2021
RM Scheduling 27 Lecture 26: Schedulilng I 3/3/2021
RM and DM Can Be Optimal Simply Periodic For every pair of tasks A and B with Pa < Pb, Pb is an integral multiple of Pa Then we can schedule the jobs if Utilization <= 1 Can we do better in the general case? 28 Lecture 26: Schedulilng I 3/3/2021
Rate (Deadline) Monotonic Jobs come in, are assigned a priority, highest runs Consider (8, 3), (9, 3), (15, 3) Utilization = 3/8 + 3/9 + 3/15 = 0. 908 Is this guaranteed to be schedulable? 29 Lecture 26: Schedulilng I 3/3/2021
Rate Monotonic Scheduling Consider (10, 2) , (12, 5) , (15, 4) Utilization = 0. 2 + 0. 417 + 0. 27 = 0. 887 But RM fails: (WHY) 30 Lecture 26: Schedulilng I 3/3/2021
Rate Monotonic Scheduling Consider (8, 4) , (10, 2) , (12, 3) Utilization = 0. 5 + 0. 25 = 0. 95 RM fails: (WHY) 31 Lecture 26: Schedulilng I 3/3/2021
Dynamic Priority Scheduling Priorities are changed dynamically for different tasks Otherwise same simplifying assumptions Approaches to assigning priorities Basic idea: give priority to the job that needs it the most Least laxity (least slack) first (LLF) Earliest deadline first (EDF) 32 Lecture 27: Scheduling II 3/3/2021
Least Laxity First Laxity = time until deadline – compute time remaining This is a measure of how stressed we are to finish the job Small laxity implies high priority 33 Lecture 27: Scheduling II 3/3/2021
Least Laxity Scheduling (8, 4) , (10, 2) , (12, 3) 34 6 : Laxity = X, X, 3 0 : Laxity = 4, 8, 9 7 : Laxity = X, X, 3 1 : Laxity = 4, 7, 8 8 : Laxity = 4, X, 3 2 : Laxity = 4, 6, 7 9 : Laxity = 4, X, X 3 : Laxity = 4, 5, 6 10 : Laxity = 4, 8, X 4 : Laxity = X, 4, 5 11 : Laxity = 4, 7, X 5 : Laxity = X, 4, 4 12 : Laxity = X, 7, 9 … Lecture 27: Scheduling II 3/3/2021
Least Laxity First Is optimal Can schedule as long as utility is <= 1 Is complex When do priorities change The laxity of a task changes dynamically If it is not currently executing How would you implement this Generally not used Too much context switching, bookkeeping Can we approximate it? 35 Lecture 27: Scheduling II 3/3/2021
Earliest Deadline First (EDF) Put tasks in priority queue based on actual deadline Time when job needs to be done Priorities change only when a job is added EDF is also optimal If the utility is <= 1 then EDF will create a schedule Can transform any feasible schedule into an EDF one But this is based on assumptions Perfect periodic tasks, zero overhead, independence 36 Lecture 27: Scheduling II 3/3/2021
Earliest Deadline First Scheduling (8, 4) , (10, 2) , (12, 3) 0 : [8, 10, 12] 8 : [ 10, 12, 16] 10 : [ 12, 16, 20 ] 37 Lecture 27: Scheduling II 3/3/2021
EDF Scheduling P 1(4, 1), P 2(6, 3), P 3(10, 3) Is this sufficient? Is this schedulable? 38 Lecture 27: Scheduling II 3/3/2021
EDF (LLF) Seem Ideal Easy to implement, optimal, … But only in an ideal world If the workload is briefly non-schedulable Then both fail dramatically and unpredictably Choices are based on deadlines, not importance Work hard to display MPG rather than firing airbag Work only with idealized situations We need to relax the simplifying conditions Sporadic and aperiodic tasks 39 Lecture 27: Scheduling II 3/3/2021
Handling Overloads Why bother Tasks might take longer than expected Sporadic tasks (interrupts, etc. ) Might be unavoidable In EDF if one job is late and the system is loaded Then ALL future jobs might be late Techniques What might you want to do? 40 Lecture 27: Scheduling II 3/3/2021
Techniques for Handling Overload Techniques Schedule late jobs at lower priority than on-time jobs Change non-critical jobs to optional when late jobs occur Discard jobs that cannot complete Tradeoffs What are the effects of each of these What might you use in practice Depends on application 41 Lecture 27: Scheduling II 3/3/2021
Relaxing Constraints Non-preemptability Resource Contention Self-suspension Context Switching time Sporadic and aperiodic jobs Job dependencies Multiple cores/threads/processors 42 Lecture 27: Scheduling II 3/3/2021
Relaxing Non-Preemptability Compute maximum time a task might be non-preemptable Compute maximum time a task might be delayed By a non-preemptable task Only need to consider tasks of lower priority Add this to its execution time when deriving schedule 43 Lecture 27: Scheduling II 3/3/2021
Resource Contention Recall what can happen with locks and priorities Even without deadlock and race conditions Can have priority inversions 44 Lecture 27: Scheduling II 3/3/2021
Priority Protocols Blocked! τ1 L L L τ2 τ3 L L. . . L LLLR 0 45 R Lecture 27: Scheduling II R t t+2 t+3 t+254 3/3/2021
Fixing Priority Inversions Make critical sections non-preemptable This works, but … Critical tasks can be delayed 46 Lecture 27: Scheduling II 3/3/2021
Immediate Priority Ceiling Protocol Assign priorities based on resources Associate a priority ceiling p with each resource r The highest priority task that can lock it No task with priority >= p is allowed to lock r Task acquiring r runs at priority p 47 Lecture 27: Scheduling II 3/3/2021
Priority Inheritance Protocol Priority inheritance protocol If Ti blocks Tk, execute Ti with priority min(i, k) Change priority of a task as someone is blocked by it Which should you use? 48 Lecture 27: Scheduling II 3/3/2021
Handling Self-Suspension Why would this ever happen? Wait for I/O operations, etc. Split into two tasks Second task might have a shorter deadline Take the suspension time into account when computing schedule 49 Lecture 27: Scheduling II 3/3/2021
Handling Context Switching Time Compute the max # of context switches for a task From its period and higher priority tasks Worst case Add the context switching time * max To the execution time of the task 50 Lecture 27: Scheduling II 3/3/2021
Sporadic Tasks Sporadic tasks Arise occasionally Hard deadlines Question: how to schedule these How to accommodate in RM/DM or EDF schedules Goal Tell accept/reject when job is submitted If accepted, then job will meet its deadline 51 Lecture 27: Scheduling II 3/3/2021
Sporadic Jobs and EDF Schedules EDF will find a schedule as long as U <= 1 Keep track of U for the current system Utilization of the periodic tasks Plus the U for all accepted sporadic tasks Compute U’ as current U plus U of the sporadic task If this is <= 1 then accept Otherwise reject 52 Lecture 27: Scheduling II 3/3/2021
Sporadic Jobs and RM Schedules What guarantees can we make here? U <= X where X depends on the number of tasks This allows us to use the same approach Just check with <= X for appropriate X 53 Lecture 27: Scheduling II 3/3/2021
Aperiodic Job Scheduling Aperiodic Tasks Arise occasionally Soft deadlines Might be prioritized Goal: minimize worst/average case performance 54 Lecture 28: Scheduling III 3/3/2021
Simple Aperiodic Scheduling Run aperiodic tasks in open slots How to schedule them Round robin Prioritized round robin Run tasks with higher priorities first Allocate CPU time according to priority This gives no guarantees of performance 55 Lecture 28: Scheduling III 3/3/2021
Total Bandwidth Server Make an aperiodic job look like a sporadic one Add to schedule using standard algorithm (e. g. EDF) Can reject the sporadic job if necessary Key: set the parameters for the job so that The result is schedulable (total U <= 1 or U <= ln(2)) Recall U = e/D e is known and fixed for the job This lets us vary D to accommodate the jobs 56 Lecture 28: Scheduling III 3/3/2021
Total Bandwidth Server Determine desired aperiodic load Us To still allow sporadic jobs and periodic jobs Total U <= 1 or ln(2) TBS is configured with budget Qs and period Ts Qs/Ts = Us Terminology es : budget for the server (its execution time) eq : execution time of job at the head of the queue us : size of the server (max utilization) 57 Lecture 28: Scheduling III 3/3/2021
Total Bandwidth Server Initially es = 0 and d = 0 When a new job with execution time e comes into an empty queue Set d = max(d, t) + e/us Set es = e When the server completes the current job, remove the job from the queue If there are more jobs, Set d= d+eq/us Set es= eq 58 Lecture 28: Scheduling III 3/3/2021
Example Periodic: P 1(4, 1), P 2(6, 3) Aperiodic: A 3: e=2 at time 1, A 4: e= 1 at time 2 First determine Us Up = ¼ + 3/6 = 0. 75 Us = 0. 25 (could be less) 59 Lecture 28: Scheduling III 3/3/2021
Example P 1 P 2 P 3 At Start P 1(4, 1), P 2(6, 3), es= 0, d = 0 A 3: e=2 at time 1 60 At time 1: d = max(0, 1) + 2 / 0. 25 = 9, es = 2 Lecture 28: Scheduling III 3/3/2021
Example P 1 P 2 P 3 P 1(4, 1), P 2(6, 3), Ps(9, 2) At time 3: new aperiodic job Pt (A 4: e= 1 at time 2), but Ps not done 61 At time 7: Ps finishes, P 2 has deadline 12 Lecture 28: Scheduling III 3/3/2021
Example P 1 P 2 P 3 At time 7: d = d + eq/Us = 9 + 1/0. 25 = 13, es = 1 Ps(13, 1) 62 Lecture 28: Scheduling III 3/3/2021
Example P 1 P 2 P 3 At time 7: d = d + eq/Us = 9 + 1/0. 25 = 13, es = 1 Ps(13, 1) 63 P 1 and P 2 have deadlines of 12 Lecture 28: Scheduling III 3/3/2021
Handling Task Dependencies Suppose tasks depend on one another Represent this as a precedence relation Ti -> Tj if Ti must precede Tj Why would this arise? How can we schedule the result EDF doesn’t work directly 64 Lecture 28: Scheduling III 3/3/2021
EDF* Scheduling Let Di be the deadline for task i Let ei be the execution time for task i Define D’i as the modified deadline for task i D’i = min(Di, MIN(Tj dependent on Ti)[D’j – ej]) Then use EDF on the modified deadlines 65 Lecture 28: Scheduling III 3/3/2021
Jitter Periodic jobs are not perfectly periodic Compute maximum jitter and add to execution/deadline 66 Lecture 28: Scheduling III 3/3/2021
Thread Scheduling All our schedules assume single core, single thread This might not be realistic today Can we just use these techniques for multithreaded scheduling? Using any available processor What can go wrong? 67 Lecture 28: Scheduling III 3/3/2021
Thread Scheduling Assume m processors, m+1 tasks Tasks 1. . m are (1, 2 e) for small e (2 me < 1) Task m+1 is (1+e, 1) Try: m = 4, 1 -4 (10, 2); 5: (11, 10) What happens? Using EDF/RM/DM Tasks 1. . m are all scheduled But then m+1 can’t run Even though there is lots of idle time Other anomalies are possible (esp. w/ synchronization) 68 Lecture 28: Scheduling III 3/3/2021
Thread Scheduling Can still use the basic algorithms Assign each task to a particular thread Ensure the utilization of that thread is <= 1 Then you can use EDF (RM/DM) to schedule thread Optimal assignment is NP-hard But approximations are pretty good 69 Lecture 28: Scheduling III 3/3/2021
Practical Scheduling What should be done in practice? What should a general purpose OS do? Suppose we just assign priorities to jobs Run the job with the highest priority at any time What is the result? 70 Lecture 28: Scheduling III 3/3/2021
Scheduling Exercise P 1(8, 4), P 2(12, 4), P 3(20, 4) 71 Lecture 28: Scheduling III 3/3/2021
- Slides: 71