Real-Time Scheduling Priority-Driven Scheduling Aperiodic Tasks 151 Current Assumptions � Single processor � Fixed number, n, of independent periodic tasks � Jobs can be preempted at any time and never suspend themselves � No resource contentions � Aperiodic jobs exist � They are independent of each other, and of the periodic tasks � They can be preempted at any time � There are no sporadic jobs (for now) � Jobs are scheduled using a priority driven algorithm 152 Scheduling Aperiodic Jobs Consider: � A set T = {T1, . . . , Tn} of periodic tasks � An aperiodic task A Recall that: � A schedule is feasible if all jobs with hard real-time constraints complete before their deadlines ⇒ This includes all periodic jobs � A scheduling algorithm is optimal if it always produces a feasible schedule whenever such a schedule exists, and if a cost function is given, minimizes the cost We assume that the periodic tasks are scheduled using a priority-driven algorithm 153 Background Scheduling of Aperiodic Jobs � Aperiodic jobs are scheduled and executed only at times when there are no periodic jobs ready for execution � Advantages � Clearly produces feasible schedules � Extremely simple to implement � Disadvantages � Not optimal since the execution of aperiodic jobs may be unnecessarily delayed Example: T1 = (3, 1), T2 = (10, 4) 154 Polled Execution of Aperiodic Jobs � We may use a polling server � A periodic job (ps, es) scheduled according to the periodic algorithm, generally as the highest priority job � When executed, it examines the aperiodic job queue � If an aperiodic job is in the queue, it is executed for up to es time units � If the aperiodic queue is empty, the polling server self-suspends, giving up its execution slot � The server does not wake-up once it has self-suspended, aperiodic jobs which become active during a period are not considered for execution until the next period begins � Simple to prove correctness, performance less than ideal – executes aperiodic jobs in particular timeslots 155 Polled Execution of Aperiodic Jobs Example: T1 = (3, 1), T2 = (10, 4), poller = (2.5, 0.5) Can we do better? Yes, polling server is a special case of periodic-server for aperiodic jobs. 156 Periodic Severs – Terminology periodic server = a task that behaves much like a periodic task, but is created for the purpose of executing aperiodic jobs � A periodic server, TS = (pS, eS) � pS is a period of the server � eS is the (maximal) budget of the server � The budget can be consumed and replenished; the budget is exhausted when it reaches 0 (Periodic servers differ in how they consume and replenish the budget) � A periodic server is � backlogged whenever the aperiodic job queue is non-empty � idle if the queue is empty � eligible if it is backlogged and the budget is not exhausted � When a periodic server is eligible, it is scheduled as any other periodic task with parameters (pS, eS) 157 Periodic Severs Each periodic server is thus specified by � consumption rules: How the budget is consumed � replenishment rules: When and how the budget is replenished Polling server � consumption rules: � Whenever the server executes, the budget is consumed at the rate one per unit time. � Whenever the server becomes idle, the budget gets immediately exhausted � replenishment rule: At each time instant k · pS replenish the budget to eS 158 Periodic Severs Deferrable sever � Consumption rules: � The budget is consumed at the rate of one per unit time whenever the server executes � Unused budget is retained throughout the period, to be used whenever there are aperiodic jobs to execute (i.e. instead of discarding the budget if no aperiodic job to execute at start of period, keep in the hope a job arrives) � Replenishment rule: � The budget is set to eS at multiples of the period � i.e. time instants k · pS for k = 0, 1, 2, . . . (Note that the server is not able tu cumulate the budget over periods) We consider both � Fixed-priority scheduling � Dynamic-priority scheduling (EDF) 159 Deferrable Server – RM Here the tasks are scheduled using RM. Is it possible to increase the budget of the server to 1.5 ? 160 Deferrable Server – RM Consider T1 = (3.5, 1.5), T2 = (6.5, 0.5) and TDS = (3, 1) A critical instant for T1 = (3.5, 1.5) looks as follows: i.e. increasing the budget above 1 may cause T1 to miss its deadline 161 Deferrable Server – Critical Instant Lemma 23 Assume a fixed-priority scheduling algorithm. Assume that Di ≤ pi and that the deferrable server (pS, eS) has the highest priority among all tasks. Then a critical instant of every periodic task Ti occurs at a time t0 when all of the following are true: � One of its jobs Ji,c is released at t0 � A job in every higher-priority periodic task is released at t0 � The budget of the server is eS at t0, one or more aperiodic jobs are released at t0, and they keep the server backlogged hereafter � The next replenishment time of the server is t0 + eS 162 Deferrable Server – Critical Instant Assume TDS � T1 � T2 � · · · � Tn (i.e. T1 has the highest pririty and Tn lowest) 163 Deferrable Server – Time Demand Analysis Assume that the deferrable server has the highest priority � The definition of critical instant is identical to that for the periodic tasks without the deferrable server + the worst-case requirements for the server � Thus the expression for the time-demand function becomes wi(t) = ei + i−1� k=1 � t pk � ek +eS + � t − eS pS � eS for 0 < t ≤ pi � To determine whether the task Ti is schedulable, we simply check whether wi(t) ≤ t for some t ≤ Di Note that this is a sufficient condition, not necessary. � Check whether wi(t) ≤ t for some t equal either � to Di, or � to j · pk where k = 1, 2, . . . , i and j = 1, 2, . . . , �Di/pk �, or � to eS, eS + pS, eS + 2pS, . . . , eS + � (Di − ei)/pS � pS 164 Deferrable Server – Time Demand Analysis TDS = (3, 1.0), T1 = (3.5, 1.5), T2 = (6.5, 0.5) 165 Deferrable Server – Schedulable Utilization � No maximum schedulable utilization is known in general � A special case: � A set T of n independent, preemptable periodic tasks whose periods satisfy pS < p1 < · · · < pn < 2pS and pn > pS + eS and whose relative deadlines are equal to their respective periods, can be scheduled according to RM with a deferrable server provided that UT ≤ URM/DS(n) := (n − 1)   � uS + 2 uS + 1 � 1 n−1 − 1   where uS = eS/pS 166 Deferrable Server – EDF Here the tasks are scheduled using EDF. TDS = (3, 1), T1 = (2, 3.5, 1.5), T2 = (6.5, 0.5) 167 Deferrable Server – EDF – Schedulability Theorem 24 A set of n independent, preemptable, periodic tasks satisfying pi ≤ Di for all 1 ≤ i ≤ n is schedulable with a deferrable server with period pS, execution budget eS and utilization uS = eS/pS according to the EDF algorithm if: n� k=1 uk + uS � 1 + pS − eS mini Di � ≤ 1 168 Sporadic Server – Motivation � Problem with polling server: TPS = (pS, eS) executes aperiodic tasks at the multiples of pS � Problem with deferrable server: TDS = (pS, eS) may delay lower priority jobs longer than periodic task (pS, eS) Therefore special version of time-demand analysis and utilization bounds were needed. � Sporadic server TSS = (eS, pS) � may execute jobs “in the middle” of its period � never delays periodic tasks for longer time than the periodic task (pS, eS) Thus can be tested for schedulability as an ordinary periodic task. Originally proposed by Sprunt, Sha, Lehoczky in 1989 original version contains a bug which allows longer delay of lower priority jobs Part of POSIX standard also incorrect as observed and (probably) corrected by Stanovich in 2010 169 Very Simple Sporadic Server For simplicity, we consider only fixed priority scheduling, i.e. assume T1 � T2 � · · · � Tn and consider a sporadic server TSS = (pS, eS) with the highest priority Notation: � tr = the latest replenishment time � tf = first instant after tr at which server begins to execute � nr = a variable representing the next replenishment � Consumption rule: The budget is consumed (at the rate of one per unit time) whenever the current time t satisfies t ≥ tf � Replenishment rules: At the beginning, tr = nr = 0 � Whenever the current time is equal to nr , the budget is set to eS and tr is set to the current time � At the first instant tf after tr at which the server starts executing, nr is set to tf + pS (Note that such server resembles a periodic task with the highest priority whose jobs are released at times tf and execution times are at most eS ) 170 Very Simple Sporadic/Background Server New notation: � tr = the latest replenishment time � tf = first instant after tr at which server begins to execute and at least one task of T is not idle � nr = a variable representing the next replenishment � Consumption rule: The budget is consumed (at the rate of one per unit time) whenever the current time t satisfies t ≥ tf and at least one task of T is not idle � Replenishment rules: At the beginning, tr = nr = 0 � Whenever the current time is equal to nr , the budget is set to eS and tr is set to the current time � At the beginning of an idle interval of T , the budget is set to eS and nr is set to the end of this interval � At the first instant tf after tr at which the server starts executing and T is not idle, nr is set to tf + pS This combines the very simple sporadic server with background scheduling. 171 Very Simple Sporadic Server Correctness (informally): Assuming that T never idles, the sporadic server resembles a periodic task with the highest priority whose jobs are released at times tf and execution times are at most eS Whenever T idles, the sporadic server executes in the background, i.e. does not block any periodic task, hence does not consume the budget Whenever an idle interval of T ends, we may treat this situation as a restart of the system with possibly different phases of tasks (so that it is safe to have the budget equal to eS) Note that in both versions of the sporadic server, eS units of execution time are available for aper. jobs every pS units of time This means that if the server is always backlogged, then it executes for eS time units every pS units of time 172 Real-Time Scheduling Priority-Driven Scheduling Sporadic Tasks 173 Current Assumptions � Single processor � Fixed number, n, of independent periodic tasks, T1, . . . , Tn where Ti = (ϕi, pi, ei, Di) � Jobs can be preempted at any time and never suspend themselves � No resource contentions � Sporadic tasks � Independent of the periodic tasks � Jobs can be preempted at any time � Aperiodic tasks For simplicity scheduled in the background – i.e. we may ignore them � Jobs are scheduled using a priority driven algorithm A sporadic job = a job of a sporadic task 174 Our situation � Based on the execution time and deadline of each newly arrived sporadic job, decide whether to accept or reject the job � Accepting the job implies that the job will complete within its deadline, without causing any periodic job or previously accepted sporadic job to miss its deadline � Do not accept a sporadic job if cannot guarantee it will meet its deadline 175 Scheduling Sporadic Jobs – Correctness and Optimality � A correct schedule is one where all periodic tasks, and all sporadic tasks that have been accepted, meet their deadlines � A scheduling algorithm supporting sporadic jobs is a correct algorithm if it only produces correct schedules for the system � A sporadic job scheduling algorithm is optimal if it accepts a new sporadic job, and schedules that job to complete by its deadline, iff the new job can be correctly scheduled to complete in time 176 Model for Scheduling Sporadic Jobs with EDF � Assume that all jobs in the system are scheduled by EDF � if more sporadic jobs are released at the same time their acceptance test is done in the EDF order � Definitions: � Sporadic jobs are denoted by S(r, d, e) where r is the release time, d the (absolute) deadline, and e is the maximum execution time � The density of S(r, d, e) is defined by e/(d − r) � The total density of a set of sporadic jobs is the sum of densities of these jobs � The sporadic job S(r, d, e) is active at time t iff t ∈ (r, d] Note that each job of a periodic task (ϕ, p, e, D) can be seen as a sporadic job; to simplify, we assume that always D ≤ p. This in turn means that there is always at most one job of a given task active at a given time instant. For every job of this task released at r with abs. deadline d, we obtain the density e/(d − r) = e/D 177 Schedulability of Sporadic Jobs with EDF Theorem 25 A set of independent preemptable sporadic jobs is schedulable according to EDF if at every time instant t the total density of all jobs active at time t is at most one. Proof. By contradiction, suppose that a job misses its deadline at t, no deadlines missed before t Let t−1 be the supremum of time instants before t when either the system idles, or a job with a deadline after t executes Suppose that jobs J1, . . . , Jk execute in [t−1, t] and that they are ordered w.r.t. increasing deadline (Jk misses its deadline at t) Let L be the number of releases and completions in [t−1, t], denote by ti the i-th time instant when i-th such event occurs (then t−1 = t1, we denote by tL+1 the time instant t) Denote by Xi the set of all jobs that are active during the interval (ti, ti+1] and let Δi be their total density The rest on whiteboard .... � 178 Sporadic Jobs with EDF – Example Note that the above theorem includes both the periodic as well as sporadic jobs This test is sufficient but not necessary Example 26 Three sporadic jobs: S1(0, 2, 1), S2(0.5, 2.5, 1), S3(1, 3, 1) Total density at time 1.5 is 1.5 Yet, the jobs are schedulable by EDF 179 Admission Control for Sporadic Jobs with EDF Let Δ be the total density of periodic tasks. Assume that a new sporadic job S(t, d, e) is released at time t. � At time t there are n active sporadic jobs in the system � The EDF scheduler maintains a list of the jobs, in non-decreasing order of deadline � The deadlines partition the time from t to ∞ into n + 1 discrete intervals I1, I2, . . . , In+1 � I1 begins at t and ends at the earliest sporadic job deadline � For each 1 ≤ k ≤ n, each Ik+1 begins when the interval Ik ends, and ends at the next deadline in the list (or ∞ for In+1) � The scheduler maintains the total density ΔS,k of sporadic jobs active in each interval Ik � Let I� be the interval containing the deadline d of the new sporadic job S(t, d, e) � The scheduler accepts the job if e/(d − t) + ΔS,k ≤ 1 − Δ for all k = 1, 2, . . . , � � i.e. accept if the new sporadic job can be added, without increasing density of any intervals past 1 180 181 Admission Control for Sporadic Jobs with EDF This acceptance test is not optimal: a sporadic job may be rejected even though it could be scheduled. � The test is based on the density and hence is sufficient but not necessary. � It is possible to derive a – much more complex – expression for schedulability which takes into account slack time, and is optimal. Unclear if the complexity is worthwhile. 182 Sporadic Jobs with EDF � One way to schedule sporadic jobs in a fixed-priority system is to use a sporadic server to execute them � Because the server (pS, eS) has eS units of processor time every pS units of time, the scheduler can compute the least amount of time available to every sporadic job in the system � Assume that sporadic jobs are ordered among themselves according to EDF � When first sporadic job S1(t, dS,1, eS,1) arrives, there is at least � (dS,1 − t)/pS � eS units of processor time available to the server before the deadline of the job � Therefore it accepts S1 if the slack of the job σS,1(t) = � (dS,1 − t)/pS � eS − eS,1 ≥ 0 183 Sporadic Jobs with EDF � To decide if a new job Si(t, dS,i, eS,i) is acceptable when there are n sporadic jobs in the system, the scheduler first computes the slack σS,i(t) of Si: σS,i(t) = � (dS,i − t)/pS � eS − eS,i − � dS,k