Embedded Systems 4. Aperodc and Perodc Tasks Lothar Thele 4-1
Contents of Course 1. Embedded Systems Introducton 2. Software Introducton 7. System Components 10. Models 3. Real-Tme Models 4. Perodc/Aperodc Tasks 5. Resource Sharng 8. Communcaton 9. Low Power Desgn 11. Archtecture Synthess 6. Real-Tme OS Software and Programmng 12. Model Based Desgn Processng and Communcaton Hardware 4-2
Overvew Schedulng of aperodc tasks wth real-tme constrants: Table wth some known algorthms: Equal arrval tmes non preemptve Arbtrary arrval tmes preemptve Independent tasks Dependent tasks EDD (Jackson) LDF (Lawler) EDF (Horn) EDF* (Chetto) 4-3
Earlest Deadlne Due (EDD) Jackson s rule: Gven a set of n tasks. Processng n order of non-decreasng deadlnes s optmal wth respect to mnmzng the maxmum lateness. 4-4
Earlest Deadlne Due (EDD) Example 1: 4-5
Earlest Deadlne Due (EDD) Jackson s rule: Gven a set of n tasks. Processng n order of non-decreasng deadlnes s optmal wth respect to mnmzng the maxmum lateness. Proof concept: 4-6
Earlest Deadlne Due (EDD) Example 2: 4-7
Earlest Deadlne Frst (EDF) Horn s rule: Gven a set of n ndependent tasks wth arbtrary arrval tmes, any algorthm that at any nstant executes the task wth the earlest absolute deadlne among the ready tasks s optmal wth respect to mnmzng the maxmum lateness. 4-8
Earlest Deadlne Frst (EDF) Example: 4-9
Earlest Deadlne Frst (EDF) Horn s rule: Gven a set of n ndependent tasks wth arbtrary arrval tmes, any algorthm that at any nstant executes the task wth the earlest absolute deadlne among the ready tasks s optmal wth respect to mnmzng the maxmum lateness. Concept of proof: For each tme nterval t, t 1 t s verfed, whether the actual runnng task s the one wth the earlest absolute deadlne. If ths s not the case, the task wth the earlest absolute deadlne s executed n ths nterval nstead. Ths operaton cannot ncrease the maxmum lateness. 4-10
Earlest Deadlne Frst (EDF) whch task s executng? whch task has earlest deadlne? tme slce slce for nterchange stuaton after nterchange 4-11
Earlest Deadlne Frst (EDF) Acceptance test: worst case fnshng tme of task : EDF guarantee condton: algorthm: Algorthm: EDF_guarantee (J, J new ) { J =J{J new }; /* ordered by deadlne */ t = current_tme(); f 0 = t; for (each J J ) { } f = f -1 + c (t); f (f > d ) return(infeasible); } return(feasible); f t k 1 c 1,..., n k ( t) t remanng worstcase executon tme of task k k 1 c k ( t) d 4-12
Earlest Deadlne Frst (EDF*) The problem of schedulng a set of n tasks wth precedence constrants (concurrent actvaton) can be solved n polynomal tme complexty f tasks are preemptable. The EDF* algorthm determnes a feasble schedule n the case of tasks wth precedence constrants f there exsts one. By the modfcaton t s guaranteed that f there exsts a vald schedule at all then a task starts executon not earler than ts release tme and not earler than the fnshng tmes of ts predecessors (a task cannot preempt any predecessor) all tasks fnsh ther executon wthn ther deadlnes 4-13
Earlest Deadlne Frst (EDF*) Modfcaton of deadlnes: Task must fnsh the executon tme wthn ts deadlne Task must not fnsh the executon later than the maxmum start tme of ts successor task b depends on task a: Ja J b Soluton: d * mn d, mn d * C : j j J J j 4-14
Earlest Deadlne Frst (EDF*) Modfcaton of release tmes: Task must start the executon not earler than ts release tme. Task must not start the executon earler than the mnmum fnshng tme of ts predecessor. task b depends on task a: Ja J b Soluton: r j * max r,max r * C : j J J j 4-15
Earlest Deadlne Frst (EDF*) Algorthm for modfcaton of release tmes: 1. For any ntal node of the precedence graph set r 2. Select a task j such that ts release tme has not been modfed but the release tmes of all mmedate predecessors have been modfed. If no such task exsts, ext. 3. Set r j 4. Return to step 2 Algorthm for modfcaton of deadlnes: 1. For any termnal node of the precedence graph set d 2. Select a task such that ts deadlne has not been modfed but the deadlnes of all mmedate successors j have been modfed. If no such task exsts, ext. 3. Set 4. Return to step 2 * max r,max r * C : j d * mn d, mn d * C : J j j J J j J j r * d * 4-16
Earlest Deadlne Frst (EDF*) Proof concept Show that f there exsts a feasble schedule for the modfed task set under EDF then the orgnal task set s also schedulable (gnorng precedence relatons). To ths end, show that the orgnal task set meets the tmng constrants also. Ths can be done by usng d* d, r * r. Show the reverse also. In addton, show that the precedence relatons n the orgnal task set are not volated. In partcular, show that a task cannot start before ts predecessor and a task cannot preempt ts predecessor. 4-17
Overvew Table of some known preemptve schedulng algorthms for perodc tasks: Deadlne equals perod Deadlne smaller than perod statc prorty dynamc prorty RM (rate-monotonc) EDF DM (deadlne-monotonc) EDF* 4-18
Model of Perodc Tasks Examples: sensory data acquston, low-level servong, control loops, acton plannng and system montorng. When a control applcaton conssts of several concurrent perodc tasks wth ndvdual tmng constrants, the OS has to guarantee that each perodc nstance s regularly actvated at ts proper rate and s completed wthn ts deadlne. Defntons: : denotes a set of perodc tasks : denotes a generc perodc task : denotes the jth nstance of task, j r, j, s, f, d, D, j, j j : denotes the release tme, start tme, fnshng tme, absolute deadlne of the jth nstance of task : phase of task (release tme of ts frst nstance) : relatve deadlne of task 4-19
Model of Perodc Tasks The followng hypotheses are assumed on the tasks: The nstances of a perodc task are regularly actvated at a constant rate. The nterval T between two consecutve actvatons s called perod. The release tmes satsfy r 1, j j T All nstances have the same worst case executon tme All nstances of a perodc task have the same relatve deadlne. Therefore, the absolute deadlnes satsfy D d, j 1 j T D C 4-20
Model of Perodc Tasks The followng hypotheses are assumed on the tasks cont : D Often, the relatve deadlne equals the perod and therefore d, j jt T All perodc tasks are ndependent; that s, there are no precedence relatons and no resource constrants. No task can suspend tself, for example on I/O operatons. All tasks are released as soon as they arrve. All overheads n the OS kernel are assumed to be zero. 4-21
Model of Perodc Tasks Example: T D,3 r,1 r, 2 C s,3 f, 3 4-22
Rate Monotonc Schedulng (RM) Assumptons: Task prortes are assgned to tasks before executon and do not change over tme (statc prorty assgnment). RM s ntrnscally preemptve: the currently executng task s preempted by a task wth hgher prorty. Deadlnes equal the perods D. T Algorthm: Each task s assgned a prorty. Tasks wth hgher request rates (that s wth shorter perods) wll have hgher prortes. Tasks wth hgher prorty nterrupt tasks wth lower prorty. 4-23
Perodc Tasks Example: 2 tasks, deadlne = perods, U = 97% 4-24
Rate Monotonc Schedulng (RM) Optmalty: RM s optmal among all fxed-prorty assgnments n the sense that not other fxed-prorty algorthm can schedule a task set that cannot be scheduled by RM. The proof s done by consderng several cases that may occur, but the man deas are as follows: A crtcal nstant for any task occurs whenever the task s released smultaneously wth all hgher prorty tasks. The tasks schedulablty can easly be checked at ther crtcal nstances. If all tasks are feasble at ther crtcal nstants, then the task set s schedulable n any other condton. Show that, gven two perodc tasks, f the schedule s feasble by an arbtrary prorty assgnment, then t s also feasble by RM. Extend the result to a set of n perodc tasks. 4-25
Proof of Crtcal Instance Defnton: A crtcal nstant of a task s the tme at whch the release of a task wll produce the largest response tme. Lemma: For any task, the crtcal nstant occurs f that task s smultaneously released wth all hgher prorty tasks. Proof sketch: Start wth 2 tasks 1 and 2. Response tme of 2 s delayed by tasks 1 of hgher prorty: 2 1 C 2 +2C 1 t 4-26
Proof of Crtcal Instance Delay may ncrease f 1 starts earler: 2 1 C 2 +3C 1 t Maxmum delay acheved f 2 and 1 start smultaneously. Repeatng the argument for all hgher prorty tasks of some task 2 : The worst case response tme of a task occurs when t s released smultaneously wth all hgher-prorty tasks. 4-27
Proof of RM Optmalty (2 Tasks) We have two tasks 1, 2 wth perods T 1 < T 2. Defne F= T 2 /T 1 : number of perods of 1 fully contaned n T 2 Consder two cases A and B: A: Assume RM s not used pro( 2 ) s hghest: 1 T 1 2 C 2 C 1 t Schedule s feasble f C 1 +C 2 T 1 (A) 4-28
Proof of RM Optmalty (2 Tasks) B: Assume RM s used pro( 1 ) s hghest: 1 2 C 1 T 2 FT 1 FT 1 T 2 Schedulable f FC 1 +C 2 +mn(t 2 FT 1, C 1 ) T 2 and C 1 T 1 (B) t We need to show that (A) (B): C 1 +C 2 T 1 C 1 T 1 C 1 +C 2 T 1 FC 1 +C 2 FC 1 +FC 2 FT 1 FC 1 +C 2 +mn(t 2 FT 1, C 1 ) FT 1 +mn(t 2 FT 1, C 1 ) mn(t 2, C 1 +FT 1 ) T 2 Gven tasks 1 and 2 wth T 1 < T 2, then f the schedule s feasble by an arbtrary fxed prorty assgnment, t s also feasble by RM. 4-29
Rate Monotonc Schedulng (RM) Schedulablty analyss: A set of perodc tasks s schedulable wth RM f n 1 C T n 2 1/ n 1 Ths condton s suffcent but not necessary. The term n C U T 1 denotes the processor utlzaton factor U whch s the fracton of processor tme spent n the executon of the task set. 4-30
Proof of Utlzaton Bound (2 Tasks) We have two tasks 1, 2 wth perods T 1 < T 2. Defne F= T 2 /T 1 : number of perods of 1 fully contaned n T 2 Proof procedure: Compute upper bound on utlzaton U such that the task set s stll schedulable. assgn prortes accordng to RM; compute upper bound U up by settng computaton tmes to fully utlze processor (C 2 adjusted to fully utlze processor); mnmze upper bound wth respect to other task parameters. 4-31
Proof of Utlzaton Bound (2 Tasks) As before: 1 2 C 1 Utlzaton: T 2 FT 1 FT 1 T 2 Schedulable f FC 1 +C 2 +mn(t 2 FT 1, C 1 ) T 2 and C 1 T 1 t 4-32
Proof of Utlzaton Bound (2 Tasks) Mnmze utlzaton bound w.r.t C 1 : If C 1 T 2 FT 1 then U decreases wth ncreasng C 1 If T 2 FT 1 C 1 then U decreases wth decreasng C 1 Therefore, mnmum U s obtaned wth C 1 = T 2 FT 1 : We now need to mnmze w.r.t. G =T 2 /T 1 where F = T 2 /T 1 and T 1 < T 2. As F s nteger, we frst suppose that t s ndependent of G = T 2 /T 1. We obtan 4-33
Proof of Utlzaton Bound (2 Tasks) Mnmzng U wth respect to G yelds If we set F = 1, then we obtan It can easly be checked, that all other nteger values for F lead to a larger upper bound on the utlzaton. 4-34
Deadlne Monotonc Schedulng (DM) Assumptons are as n rate monotonc schedulng, but deadlnes may be smaller than the perodc,.e. C D T Algorthm: Each task s assgned a prorty. Tasks wth smaller relatve deadlnes wll have hgher prortes. Tasks wth hgher prorty nterrupt tasks wth lower prorty. Schedulablty analyss: A set of perodc tasks s schedulable wth DM f n C 2 1/ n n 1 D 1 Ths condton s suffcent but not necessary (n general). 4-35
DM Example U = 0.874 1.08 n 2 1 0. 757 1 n 1 C D 1/ n 2 1 10 3 4 1 10 1 10 1 10 4-36
Deadlne Monotonc Schedulng (DM) There s also a necessary and suffcent schedulablty test whch s computatonally more nvolved. It s based on the followng observatons: The worst-case processor demand occurs when all tasks are released smultaneously; that s, at ther crtcal nstances. For each task, the sum of ts processng tme and the nterference (preempton) mposed by hgher prorty tasks must be less than or equal to D. A measure of the worst case nterference for task can be computed as the sum of the processng tmes of all hgher prorty tasks released before some tme t where tasks are ordered accordng to m n D m D n : 1 t I T j j 1 C j 4-37
Deadlne Monotonc Schedulng (DM) The longest response tme R of a perodc task s computed, at the crtcal nstant, as the sum of ts computaton tme and the nterference due to preempton by hgher prorty tasks R C I Hence, the schedulablty test needs to compute the smallest that satsfes R 1 R C j T j 1 for all tasks. Then, R must hold for all tasks. It can be shown that ths condton s necessary and suffcent. D R C j 4-38
Deadlne Monotonc Schedulng (DM) The longest response tmes R of the perodc tasks can be computed teratvely by the followng algorthm: Algorthm: DM_guarantee () { for (each ){ I = 0; do { R = I + C ; f (R > D ) return(unschedulable); I = j=1,,(-1) R/T j C j ; } whle (I + C > R); } return(schedulable); } 4-39
DM Example Example: Task 1: Task 2: Task 3: Task 4: C1 1; T1 4; D1 C 1; T2 5; D2 C 2; T3 6; D3 C ; T 11; D 2 3 4 1 4 4 Algorthm for task 4: Step 0: R 4 1 Step 1: R 4 5 Step 2: R 4 6 Step 3: R 4 7 Step 4: R 4 9 Step 5: R 4 10 3 4 5 10 4-40
DM Example U = 0.874 1.08 n 2 1 0. 757 1 n 1 C D 1/ n 2 1 10 3 4 1 10 1 10 1 10 4-41
EDF Schedulng (earlest deadlne frst) Assumptons: dynamc prorty assgnment ntrnscally preemptve D T Algorthm: The currently executng task s preempted whenever another perodc nstance wth earler deadlne becomes actve. d j 1 T D, j Optmalty: No other algorthm can schedule a set of perodc tasks f the set that can not be scheduled by EDF. The proof s smple and follows that of the aperodc case. 4-42
Perodc Tasks Example: 2 tasks, deadlne = perods, U = 97% 4-43
EDF Schedulng A necessary and suffcent schedulablty test f D : A set of perodc tasks s schedulable wth EDF f and only f n 1 C T U 1 T The term U n 1 C T denotes the average processor utlzaton. 4-44
EDF Schedulng If the utlzaton satsfes U 1, then there s no vald schedule: The total demand of computaton tme n nterval T T1 T2... T n s n C T UT T T 1 and therefore, t exceeds the avalable processor tme. If the utlzaton satsfes U 1, then there s a vald schedule. We wll proof ths by contradcton: Assume that deadlne s mssed at some tme t 2. Then we wll show that the utlzaton was larger than 1. 4-45
EDF Schedulng If the deadlne was mssed at t 2 then defne t 1 as the maxmal tme before t 2 where the processor s contnuously busy n [t 1, t 2 ] and the processor only executes tasks that have ther arrval tme AND deadlne n [t 1, t 2 ]. Why does such a tme t 1 exst? We fnd such a t 1 by startng at t 2 and gong backwards n tme, always ensurng that the processor only executed tasks that have ther deadlne before or at t 2 : Because of EDF, the processor wll be busy shortly before t 2 and t executes on the task that has deadlne at t 2. Suppose that we reach a tme when the processor gets dle, then we found t 1 : There s a task arrval at t 1 and the task queue s empty shortly before. Suppose that we reach a tme such that shortly before the processor works on a task wth deadlne after t 2, then we also found t 1 : Because of EDF, all tasks the processor processed n [t 1, t 2 ] arrved at or after t 1 (otherwse, the processor would not have operated before t 1 on a task wth deadlne after t 2 ). 4-46
EDF Schedulng t 1,t 2 Wthn the nterval the total computaton tme demanded by the perodc tasks s bounded by C n n t2 t 1 t2 t1 ( t1, t2) C C t 2 t 1 T 1 T p 1 number of complete perods of task I n the nterval U Snce the deadlne at tme s mssed, we must have: t t 2 t t t t U U 1 2 t1 C p 1, 2 2 1 4-47
Perodc Tasks Example: 2 tasks, deadlne = perods, U = 97% 4-48
Problem of Mxed Task Sets In many applcatons, there are as well aperodc as perodc tasks. Perodc tasks: tme-drven, execute crtcal control actvtes wth hard tmng constrants amed at guaranteeng regular actvaton rates. Aperodc tasks: event-drven, may have hard, soft, nonreal-tme requrements dependng on the specfc applcaton. Sporadc tasks: Offlne guarantee of event-drven aperodc tasks wth crtcal tmng constrants can be done only by makng proper assumptons on the envronment; that s by assumng a maxmum arrval rate for each crtcal event. Aperodc tasks characterzed by a mnmum nterarrval tme are called sporadc. 4-49
Background Schedulng Smple soluton for RM and EDF schedulng of perodc tasks: Processng of aperodc tasks n the background,.e. f there are no perodc request. Perodc tasks are not affected. Response of aperodc tasks may be prohbtvely long and there s no possblty to assgn a hgher prorty to them. 4-50
Background Schedulng Example (rate monotonc perodc schedule): 4-51
RM - Pollng Server Idea: Introduce an artfcal perodc task whose purpose s to servce aperodc requests as soon as possble (therefore, server ). Lke any perodc task, a server s characterzed by a perod and a computaton tme C s. The server s scheduled wth the same algorthm used for the perodc tasks and, once actve, t serves the aperodc requests wthn the lmt of ts server capacty. Its prorty (perod!) can be chosen to match the response tme requrement for the aperodc tasks. T s 4-52
RM - Pollng Server Functon of pollng server (PS) T s At regular ntervals equal to, a PS task s nstantated. When t has the hghest current prorty, t serves any pendng aperodc requests wthn the lmt of ts capacty C s. If no aperodc requests are pendng, PS suspends tself untl the begnnng of the next perod and the tme orgnally allocated for aperodc servce s not preserved for aperodc executon. Dsadvantage: If an aperodc requests arrves just after the server has suspended, t must wat untl the begnnng of the next pollng perod. 4-53
RM - Pollng Server Example server has current hghest prorty and checks the queue of tasks 4-54
RM - Pollng Server Schedulablty analyss of perodc tasks As n the case of RM as the nterference by a server task s the same as the one ntroduced by an equvalent perodc task. A set of perodc tasks and a server task can be executed wthn ther deadlnes f C T s s n 1 C T ( n 1) 2 1/( n1) 1 Agan, ths test s suffcent but not necessary. 4-55
RM - Pollng Server Aperodc guarantee of aperodc actvtes. Assumpton: An aperodc task s fnshed before a new aperodc request arrves. Computaton tme C, deadlne D. Suffcent schedulablty test: a Ca (1 T C ) s s D a a If the server task has the hghest prorty there s a necessary test also. The aperodc task arrves shortly after the actvaton of the server task. Maxmal number of necessary server perods. 4-56
EDF Total Bandwdth Server Total Bandwdth Server: When the kth aperodc request arrves at tme t = r k, t receves a deadlne d k C max( rk, dk 1 ) U where C k s the executon tme of the request and U s s the server utlzaton factor (that s, ts bandwdth). By defnton, d 0 =0. Once a deadlne s assgned, the request s nserted nto the ready queue of the system as any other perodc nstance. k s 4-57
EDF Total Bandwdth Server Example: U 0.75, U 0.25, U U 1 p s p s 4-58
EDF Total Bandwdth Server Schedulablty test: Gven a set of n perodc tasks wth processor utlzaton U p and a total bandwdth server wth utlzaton U s, the whole set s schedulable by EDF f and only f Proof: U p U [ t 1, t2] In each nterval of tme, f C ape s the total executon tme demanded by aperodc requests arrved at t 1 or later and served wth deadlnes less or equal to t 2, then s 1 C ) ape ( t2 t1 U s 4-59
4-60 EDF Total Bandwdth Server If ths has been proven, the proof of the schedulablty test follows closely that of the perodc case. Proof of lemma: ) ( ), max( )), max( ( 1 2 1 1 1 1 2 2 1 2 1 t t U d r d U d r d U C C s k k k s k k k k k k s k k k k ape
EDF Total Bandwdth Server Example: U 0.75, U 0.25, U U 1 p s p s 4-61