Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 522 Real-Time Scheduling (3) Computer Science & Engineering Department Arizona State University Tempe, AZ 85287 Dr. Yann-Hang Lee (480)

Similar presentations


Presentation on theme: "CSE 522 Real-Time Scheduling (3) Computer Science & Engineering Department Arizona State University Tempe, AZ 85287 Dr. Yann-Hang Lee (480)"— Presentation transcript:

1 CSE 522 Real-Time Scheduling (3) Computer Science & Engineering Department Arizona State University Tempe, AZ 85287 Dr. Yann-Hang Lee yhlee@asu.edu (480) 727-7507 1

2 Priority Inversion in Synchronization 2  1(H) Blocked  2(M)  3(L) Time  1 :{…P(S1)…V(S1)…}  3 :{…P(S1)…V(S1)…} S1 unlocked Attempt to lock S1 (blocked) S1 locked S1 unlocked S1 locked

3 Priority Inversion  Delay to a task’s execution caused by interference from lower priority tasks is known as priority inversion  Priority inversion is modeled by blocking time  Identifying and evaluating the effect of sources of priority inversion is important in schedulability analysis  Sources of priority Inversion  Synchronization and mutual exclusion  Non-preemtable regions of code  FIFO (first-in-first-out) queues 3

4 Accounting for Priority Inversion  Recall that task schedulability is affected by  preemption: two types of preemption  can occur several times per period  can occur once per period  execution: once per period  blocking: at most once per period for each request to a source  The schedulability formulas are modified to add a “blocking” or “priority inversion” term to account for inversion effects 4

5 UB Test with Blocking  Include blocking while calculating effective utilization for each tasks: H n Preemption(can hit n times) Execution Blocking H 1 Preemption(can hit once) 5

6  Blocking is also included in the RT test  Perform test as before, including blocking effect RT Test with Blocking 6

7  Consider the following example Periodic tasks What is the worst case blocking effect (priority inversion) experienced by each task ? Example: Considering Blocking 25 msec 100 msec 50 msec 200 msec 100 msec 300 msec 10 msec 30 msec Data Structure 11 22 33 7

8 Example: Adding Blocking  Task  2 does not use the data structure. Task  2 does experiences no priority inversion  Task  1 shares the data structure with  3. Task  1 could have to wait for  3 to complete its critical section. But worse, if  2 preempts while  1 is waiting for the data structure,  1 could have to wait for  2 ’s entire computation.  This is the resulting table taskPeriodExecution Time PriorityBlocking delay Deadline 11 10025High30+50100 22 20050Medium0200 33 300100Low0300 8

9 UB Test for Example  UB test with blocking: Not schedulable with additional RT test,  3 is shown to be schedulable 9

10 Synchronization Protocols  No preemption  Basic priority inheritance  Highest locker’s priority  Priority ceiling  Each protocol prevents unbounded priority inversion. 10

11 Nonpreemption Protocol  2 :{…P(S1)…V(S1)…}  4 :{…P(S1)…V(S1)…} ready blocked ready S1 locked S1 unlocked Time  1 (H)  4 (L) 22 33 11

12 Advantages and Disadvantages  Advantages:  Simplicity  Use with fixed-priority and dynamic-priority systems  No priori knowledge about resource requirement by each task  Good when all critical sections are short  Disadvantages:  Every task can be blocked by every lower priority task, even when there is no resource sharing between the tasks.  Blocking time: max(cs i ) 12

13 Basic Inheritance Protocol (BIP)  2 :{…P(S1)…V(S1)…}  4 :{…P(S1)…V(S1)…} ready S1 lockedS1 unlocked  1 (H)  4 (L) 22 33 blocked attempts to lock S1 S1 unlocked S1 locked ready inherits the priority of  2 after  2 is blocked 13

14 Some Notations  J i is the i-th job in Task T.   i = Assigned priority of Job J i   i (t) = current priority of J i  If the decision to change the priority of Job J i is made at t = t 1 then   i (t 1 - ) = priority at and immediately before,   i (t 1 + ) = priority immediately after the priority change   = nonexistent priority, lower than the lowest priority 14

15 Terminology and Assumptions  At time t1, job J i requests resource R k.  R k  J l : Resource R k is held by Job J l  J i  R k : Job J i is blocked waiting for resource R k to be released (J i  R k  J l )  Scheduling Rules:  Ready Jobs scheduled on processors preemptively in a priority driven manner according to their current priorities,  i (t).  At job release time the priority is equal to its assigned priority  if J i is release at t = t’, then  i (t’) =  i  Resource allocation:  If a resource is free then it is allocated when requested  if not free then the request is denied and the requesting job is blocked 15

16 Priority Inheritance Rules  Scheduling Rule: same as the assumptions  Allocation Rule: same as the assumptions  Priority-Inheritance Rule:  if J i  R k  J l and  l (t 1 - ) = priority of J l at t = t 1  then  l (t 1 + ) =  i (t 1 )  until J l releases R k at t 2 when  l (t 2 + ) =  l (t 1 - )  L. Sha, R. Rajkumar, J. Lehoczky, “Priority Inheritance Protocols: An Approach to Real-Time Synchronization”, IEEE Transactions on Computers, Vol. 39, No. 9, pp. 1175-1185, 1990 16

17 Properties of Priority Inheritance  For each resource (semaphore), a list of blocked tasks must be stored in a priority queue.  A task (job)  i uses its assigned priority, and uses (inherits) the highest dynamic priority of all the tasks it blocks when it is in its critical section and blocks some higher priority tasks.  Priority inheritance is transitive; that is, if task  i blocks  j and  j blocks  k, then  i can inherit the priority of  k.  When task  i releases a resource, which priority it should use?  Chained blocking if requesting multiple resources (nested mutex requests)  Direct blocking and indirect (inheritance) blocking (when the lower priority task inherits the higher priority task’s priority). 17

18 Implementation Issues of BIP  PI semaphore and basic semaphore  Priority is changed when acquiring and releasing a lock  When release a lock, can the priority be restore to the one before acquiring?  Need to maintain a list of PI semaphores locked by a task  when holding multiple locks and release one  when a PI semaphore is deleted  when a task waiting for a PI semaphore quits the waiting (due to timeout)  when the priority of a task waiting for a PI semaphore is changed  Note that for each semaphore, a queue for all waiting tasks 18

19 Example Of Chained Blocking (BIP)  1 :{…P(S1)…P(S2)…V(S2)…V(S1)…}  2 :{…P(S1)…V(S1)…}  3 :{…P(S2)…V(S2)…} S2 locked S2 unlocked  3 (L)  1 (H)  2 (M) B Attempts to lock S1(blocked) Attempts to lock S2(blocked) B S1 locked S1 unlocked B 19

20 Deadlock: Using BIP  1 :{…P(S1)…P(S2)…V(S2)…V(S1)..}  2 :{…P(S2)…P(S1)…V(S1)…V(S2)..} S2 locked Attempts to lock S1 (blocked) Attempts to lock S2 (blocked) Locks S1 B  2 (M)  1 (H) 20

21 Blocking Time Under BIP  Example T1 = {.. P(A).3. P(B).2. V(B).1. V(A)..} T2 = {.. P(C).2. V(C)..} T3 = {.. P(A).1. P(B).2. V(B).2. V(A).. } T4 = {.. P(A).1. P(C).1. P(B).3. V(B).1. V(C).1. V(A).. } direct blocking by indirect blocking by blocking time T2T3T4T2T3T4T2T3T4 T1NYY57 T2NYYY57 T3YY7 21

22 Priority Ceiling Protocol (PCP)  2 :{…P(S1)…V(S1)…}  3 :{…P(S2)…V(S2)…}  4 :{…P(S1)…V(S1)…} ready  1 (H) 22 33 B attempts to lock S1 S1 locked S1 unlocked S1 locked S1 unlocked  4 (L) attempts to lock S2 S2 locked blocked by ceiling 22

23 Basic Priority Ceiling Rules (1)   (R) = priority ceiling of resource R – the highest priority of the tasks that request R   S (t) = system priority ceiling -- the highest priority ceiling of the resources that are in use at time t  Scheduling Rule: same as the assumptions  Allocation Rule:  if J i  R k  J l at t = t 1 then block J i (no change)  R k free at t 1,  if  i (t 1 ) >  S (t 1 ), then R k  J i  else (i.e.  i (t 1 )   S (t 1 ) ) if for some R x  J i and  (R x ) =  S (t 1 ), then R k  J i [ J i holds a resource R x whose priority ceiling is  S (t 1 ) ]  else deny and block (J i  R k ) 23

24 Basic Priority Ceiling Rules (2)  Priority-Inheritance Rule:  if J i  R k at t = t 1 and is blocked by J l (and  l (t 1 - ) = priority of J l )  either R k  J l, (J l holds the resource R k ) or J l  R x and  (R x ) =  S (t 1 )   i (t 1 )  then  l (t 1 + ) =  i (t 1 ) (inherited priority)  until J l releases all R x with  (R x )   i (t 1 ),  l (t 2 + ) =  l (t 1 - ) at t = t 2. 24

25 Blocking in PCP  A task T H can be blocked by a lower-priority task T L in three ways:  directly, i.e.  when T L inherits a priority higher than the priority  H of T H.  When T H requests a resource the priority ceiling of resources held by T L is equal to or higher than  H : XTLTL THTH request for allocated to XTLTL T  T H (  >  H ) YTLTL THTH (H  X)(H  X) X 25

26 Blocked At Most Once (PCP)  1 :{…P(S1)…P(S2)…V(S2)…V(S1)…}  2 :{…P(S1)…V(S1)…}  3 :{…P(S2)…V(S2)…} S2 locked S2 unlocked  3 (L)  1 (H)  2 (M) attempts to lock S1(blocked) B S1 locked S1 unlocked attempts to lock S1(blocked) S2 lockedS2 unlocked S1 locked S1 unlocked 26

27 Deadlock Avoidance: Using PCP Locks S2 Locks S1 Unlocks S1 Unlocks S2 Attempts to lock S1 (blocked) Locks S1 Locks S2 C  1 :{…P(S1)…P(S2)…V(S2)…V(S1)..}  2 :{…P(S2)…P(S1)…V(S1)…V(S2)..}  2 (M)  1 (H) Unlocks S2 Unlocks S1 27

28 Stack Sharing  Sharing of the stack among tasks eliminates stack space fragmentation and so allows for memory savings:  However:  Once job is preempted, it can only resume when it returns to be on top of stack.  Otherwise, it may cause a deadlock.  Stack becomes a resource that allows for “one-way preemption”. T1T1 TiTi TnTn no stack sharing T1T1 TiTi TnTn stack sharing 28

29 Stack-Based Priority Ceiling Protocol  To avoid deadlocks: Once execution begins, make sure that job is not blocked due to resource access  allow preemption only if the priority is higher than the ceiling of the resources in use  Update Current Ceiling in the usual manner  If no resource allocated,  S (t) =   Scheduling Rule:  J i released and blocked until  i (t) >  S (t)  When not blocked jobs are scheduled in the usual manner.  Allocation Rule:  Allocate when requested 29

30 Stack-Based PCP (cont)  The Stack-Based Priority-Ceiling Protocol is deadlock-free:  When a job begins to execute, all the resources it will ever need are free.  Otherwise,  S (t) would be higher or equal to the priority of the job.  Whenever a job is preempted, all the resources needed by the preempting job are free.  The preempting job can complete, and the preempted job can resume.  Worst-case blocking time of Stack-Based Protocol is the same as for Basic Priority Ceiling Protocol.  Stack-Based Protocol smaller context-switch overhead  2 context switches since once execution starts, job cannot be blocked (may be preempted)  4 context switches for PCP since a job may be blocked at most once 30

31 SRP + EDF  Let  1,  2,...,  n denote the tasks, and R 1,R 2,..., R m denote the non-preemptable resources.  Each resource R j is statically assigned a ceiling  (R j ), which is set equal to the minimum relative deadline parameter of any task that may access it:  (R j ) = min{D i |  i accesses R j }  A system ceiling is computed each instant during runtime. This is set equal to the minimum ceiling of any resource that is currently being held by some job.  At any instant in time, a job generated by  i may begin execution only if it is the earliest-deadline active job, and D i is strictly less than the system ceiling. 31 (From “Resource sharing in EDF-scheduled systems: a closer look,” by Sanjoy K. Baruah, RTSS’06)

32 Ceiling-Priority Protocol  Re-formulation of stack-based priority ceiling protocol for multiple stacks (no stack-sharing)  Update Current Ceiling in the usual manner  Scheduling Rule:  No resources held by J i,  i (t) =  i  Resource held by J i,  i (t) = max(  (R x )) for all resource R x held by J i  FIFO scheduling among jobs with equal priority  Allocation Rule:  Allocate when requested  Also called: Priority ceiling emulation, highest locker protocol, Immediate priority ceiling protocol (ICPP) 32

33 Comparison  Worst case performance of stack-based and basic ceiling protocols are the same  Stack-based version  supported by the Real-Time systems Annex of Ada95  Jobs must not self-suspend  When jobs do not self-suspend, stack-based and ceiling-priority protocols yield the same schedules.  Stack-based and ceiling-priority have the same worst- case blocking time. 33

34 Highest Locker’s Priority Protocol  2 :{…P(S1)…V(S1)…}  4 :{…P(S1)…V(S1)…} ready S1 lockedS1 unlocked  1 (H)  4 (L) 22 33 B B run with the priority of  2 34

35 Summary of Synchronization Protocols ProtocolBounded Priority Inversion Blocked at Most Once Deadlock Avoidance Nonpreemptible critical sections YesYes 1 Highest locker's priority YesYes 1 Basic inheritanceYesNo Priority CeilingYesYes 2 Yes 1 35 1 Only if tasks do not suspend within critical sections 2 PCP is not affected if tasks suspend within critical sections.

36 BIP and PCP  BIP  no penalty when the locks are not contended, which covers the vast majority of time-constrained systems.  many extra context switches are avoided, and medium priority tasks are not preempted unnecessarily,  excellent average performance  CPP (highest locker protocol)  change a thread's priority twice regardless of whether there is contention for the lock or not  higher overhead and many unnecessary context switches and blocking in unrelated tasks  worst-case priority inversion control, it handles nested locks well, and can avoid deadlock in some cases  static analysis of the system to find the priority ceiling 36


Download ppt "CSE 522 Real-Time Scheduling (3) Computer Science & Engineering Department Arizona State University Tempe, AZ 85287 Dr. Yann-Hang Lee (480)"

Similar presentations


Ads by Google