CSCI1600: Embedded and Real Time Software Lecture 23: Real Time Scheduling I Steven Reiss, Fall 2015
Scheduling Problem Arduino Main Loop Round-robin scheduler (with interrupts) No preemption Assuming not that much is time critical Possibly one interrupt routine What happens in a more complex system We first must understand the components
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
Aperiodic Tasks Background tasks that run occassionally Arise at random (possibly with a distribution) Soft deadlines Can have priorities Again have release time, deadline, execution time GOAL: Execute these jobs as fast as possible Average or worst case time
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
Simplifying Assumptions All jobs are preemptible at any time Relaxations: Not preemptible 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 All of these can be taken into account, but things are more complex
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 We first need to decide what type of solution we want
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
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
Solution Models: Dynamic Priority Jobs are assigned priorities as they arise But job priorities can be changed over time Highest priority job runs Schedule reexamined periodically & as new jobs arise Accommodate aperiodic and sporadic jobs
Solution Models: Advanced Multiple threads/cores/processors Handling aperiodic and sporadic tasks Relaxing assumptions Resource scheduling
Example Consider three jobs P1(4,2), P2(6,2), P3(12,2) Can we schedule these How many time units do we have to consider
Schedule Representations P1(4,2), P2(6,2), P3(12,2)
Schedule Representations P1(4,2), P2(6,2), P3(12,2) P1 P2 P3
Scheduling Example P1(4,1), P2(6,2), P3(12,3)
Schedule Representations P1(4,1), P2(6,2), P3(12,3)
Scheduling Example P1(4,2), P2(6,3), P3(12,1)
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
Fixed Priority Scheduling Basic Idea Assign a priority to a job as it arrives Not necessarily based on its a priori priority Based on scheduling requirements Job with highest priority is run If a new job gets higher priority, it preempts running job Implementaiton Maintain a priority queue of jobs New jobs are inserted into the queue And may cause preemption
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
Rate Monotonic Example P1(4,1), P2(5,2), P3(20,5)
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 to service jobs Achieving this is NP-hard. Settle for sufficient (enough cycles)
Critical Instant Theorem The critical instant for a job is the worst time to start it The release time for which response is maximized 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
Checking a Schedule Notation τ i – the ith task or the i th execution of task τ D i – the relative deadline for the i th task P i – the period of the i th task e i – the execution time of the i th task
Checking a Schedule Simple necessary and sufficient test of feasibility Assuming synchronous release Release jobs from all tasks at time t Simulate until lowest priority job completes or misses Only on deadline and release points
RM Priority Assigment If a task set can be scheduled at all by a fixed priority schedule It can be scheduled with a RM policy Hence RM (or DM) is optimal in some sense Define the utility of a task u i = e i /P i 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?
RM Scheduling
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?
Rate (Deadline) Monotonic Jobs come in, are assigned a priority, highest runs Consider (8,3), (9,3), (15,3) Is this guaranteed to be schedulable?
Rate Monotonic Scheduling Consider (10,2), (12,5), (15,4) Utilization = = But RM fails: (WHY)
Rate Monotonic Scheduling Consider (8,4), (10,2), (12,3) Utilization = = 0.95 RM fails: (WHY)
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 Earliest deadline first
Homework Read