Real-Time Scheduling Priority-Driven Scheduling Aperiodic Tasks [Some parts of this lecture are based on a real-time systems course of Colin Perkins http://csperkins.org/teaching/rtes/index.html] 1 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 An aperiodic task A Independent of the periodic tasks Can be preempted at any time There are no sporadic tasks (for now) Jobs are scheduled using a priority driven algorithm 2 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 (maximum) 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) Each periodic server is specified by consumption and replenishment rules. 3 Polling and Deferrable Servers Polling Server TPS = (eS, pS) 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 Deferrable Sever TDS = (eS, pS) Consumption rule: 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 Replenishment rule: At each time instant k · pS replenish the budget to eS 4 Deferrable Server – RM Here the tasks are scheduled using RM. Is it possible to increase the budget of the server to 1.5 ? 5 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 would cause T1 to miss its deadline 6 Deferrable Server – Critical Instant Lemma 1 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 7 Deferrable Server – Critical Instant 8 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 Remember, 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 9 Deferrable Server – Time Demand Analysis T1 = (2, 3.5, 1.5), T2 = (6.5, 0.5) and TS = (3, 1.0) 10 Deferrable Server – Schedulable Utilization No maximum schedulable utilization is known 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 11 Deferrable Server – EDF Here the tasks are scheduled using EDF. T1 = (2, 3.5, 1.5), T2 = (6.5, 0.5) and TDS = (3, 1) 12 Deferrable Server – EDF – Schedulability Theorem 2 A periodic task Ti in a set of n independent, preemptable, periodic tasks 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 ek min(Dk , pk ) + uS 1 + pS − eS Di ≤ 1 13 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 14 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) at any time t ≥ tf Replenishment rules: At the beginning, tr = nr = 0 The budget is set to eS and tr is set to the current time whenever the current time is equal to nr 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 ) 15 Very Simple Sporadic Server T1 T2 · · · Tn and TSS = (pS, eS) has the highest priority We say that T = {T1, . . . , Tn} is idle at t if no task Ti is ready for execution, or executing at t 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) at any time t ≥ tf Replenishment rules: At the beginning, tr = nr = 0 The budget is set to eS and tr is set to the current time whenever the current time is equal to nr At the first instant tf after tr at which the server starts executing, nr is set to tf + pS IMPROVEMENT: If T becomes idle before nr = tf + pS, and becomes busy again at tb , then nr is set to min{tf + pS, tb } 16 Very Simple Sporadic/Background Server Another improvement: Consumption rule: The budget is consumed (at the rate of one per unit time) at any time t ≥ tf whenever T is not idle Replenishment rules: At the beginning, tr = nr = 0 The budget is set to eS and tr is set to the current time whenever the current time is equal to nr At the first instant tf after tr at which the server starts executing and T is not idle, nr is set to tf + pS 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 This server combines the idea of the very simple sporadic server with background scheduling 17 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 all 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 18 Real-Time Scheduling Priority-Driven Scheduling Sporadic Tasks 19 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 20 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 21 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 22 Model for Scheduling Sporadic Jobs with EDF Assume that all jobs in the system are scheduled according to 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 can be seen as a sporadic job Recall that the density of a periodic task (ϕ, p, e, D) is defined by e/ min{p, D}. For D ≤ p and every job of this task released at r and with abs. deadline d we obtain the density e/(d − r) = e/D 23 Schedulability of Sporadic Jobs with EDF Theorem 3 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. 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 .... 24 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 a necessary Example 4 Three sporadic jobs: S1(0, 2, 1), S2(0.5, 2.5, 1), S3(1, 3, 1) Total density in (1, 2] is 1.5 Yet, the jobs are schedulable by EDF 25 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 tasks 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 26 27 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 28 Sporadic Jobs in Fixed-Priority Systems 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 29 Sporadic Jobs in Fixed-Priority Systems 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