Carnegie Mellon University Software Engineering Institute Rate Monotonic Analysis Introduction Periodic tasks Extending basic theory Synchronization and priority inversion Aperiodic servers Case study: BSY-1 Trainer 1 Synchronization & Priority Inversion Carnegie Mellon University Software Engineering Institute Sample Problem: Synchronization Periodics Servers Emergency 100 msec τ1 50 msec 20 msec Data Server 2 msec 150 msec τ2 20 msec 5 msec Deadline 6 msec after arrival 40 msec Comm Server 350 msec τ3 Aperiodics 10 msec 10 msec Routine 40 msec 2 msec 100 msec Desired response 20 msec average τ2’s deadline is 20 msec before the end of each period. 2 Synchronization & Priority Inversion Carnegie Mellon University Software Engineering Institute Priority Inversion in Synchronization Legend τ1:{...P(S1)...V(S1)...} τ3:{...P(S1)...V(S1)...} Critical section (S1 locked) Executing S1 locked attempts to lock S1 (blocked) S1 unlocked Blocked τ1(H) B τ2(M) S1 locked S1 unlocked τ3(L) Time 3 Synchronization & Priority Inversion B Carnegie Mellon University Software Engineering Institute 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. 4 Synchronization & Priority Inversion Carnegie Mellon University Software Engineering Institute Sources of Priority Inversion Synchronization and mutual exclusion Non-preemptable regions of code FIFO (first-in-first-out) queues 5 Synchronization & Priority Inversion Carnegie Mellon University Software Engineering Institute Accounting for Priority Inversion Recall that task schedulability is affected by • preemption: two types of preemption - can occur several times per period - can only occur once per period • execution: once per period • blocking: at most once per period for each source The schedulability formulas are modified to add a “blocking” or “priority inversion” term to account for inversion effects. 6 Synchronization & Priority Inversion Carnegie Mellon University Software Engineering Institute UB Test with Blocking Include blocking while calculating effective utilization for each tasks: C j Ci Bi 1 f i = ∑ ------ + ----- + ----- + ----- ∑ C k T T i T i T i k ∈ H1 j ∈ Hn j Hn Preemption (can hit n times) 7 Synchronization & Priority Inversion Execution Blocking H1 Preemption (can hit once) Carnegie Mellon University Software Engineering Institute RT Test with Blocking Blocking is also included in the RT test: i–1 an + 1 = Bi + Ci + an ----- C j Tj ∑ j=1 i where a 0 = B i + ∑ Cj j=1 Perform test as before, including blocking effect. 8 Synchronization & Priority Inversion Carnegie Mellon University Software Engineering Institute Example: Considering Blocking Consider the following example: Periodics tasks τ1 τ2 τ3 100 msec 25 msec 200 msec 50 msec Data Structure 10 msec 30 msec 300 msec 100 msec What is the worst-case blocking effect (priority inversion) experienced by each task? 9 Synchronization & Priority Inversion Carnegie Mellon University Software Engineering Institute Example: Adding Blocking Task τ2 does not use the data structure. Task τ2 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: 10 Synchronization & Priority Inversion Task Period Execution Time Priority τ1 τ2 τ3 100 200 300 25 50 100 High Medium Low Blocking Deadline Delays 30+50 0 0 100 200 300 Carnegie Mellon University Software Engineering Institute UB Test for Example Recall UB test with blocking: C j Ci Bi 1 f i = ∑ ------ + ----- + ----- + ----- ∑ C k T T i T i T i k ∈ H1 j ∈ Hn j f1 C1 B1 25 80 = ------ + ------ = --------- + --------- = 1.05 > 1.00 100 100 T1 T1 f2 C1 C2 25 50 = ------ + ------ = --------- + --------- = 0.50 < U ( 2 ) 100 200 T1 T2 f3 C1 C2 C3 25 50 100 = ------ + ------ + ------ = --------- + --------- + --------- = 0.84 > U ( 3 ) 100 200 300 T1 T2 T3 RT test shows Not schedulable τ3 is schedulable 11 Synchronization & Priority Inversion Carnegie Mellon University Software Engineering Institute Synchronization Protocols No preemption Basic priority inheritance Highest locker’s priority Priority ceiling Each protocol prevents unbounded priority inversion. 12 Synchronization & Priority Inversion Carnegie Mellon University Software Engineering Institute Nonpreemption Protocol τ2:{...P(S1)...V(S1)...} τ4:{...P(S1)...V(S1)...} Ready τ1(H) Legend S1 locked Β Executing Blocked Ready τ2 Ready τ3 S1 locked S1 unlocked τ4(L) 13 Synchronization & Priority Inversion Time B Carnegie Mellon University Software Engineering Institute Basic Inheritance Protocol (BIP) τ2:{...P(S1)...V(S1)...} τ4:{...P(S1)...V(S1)...} Legend S1 locked Executing τ1(H) Blocked S1 locked S1 unlocked Attempts to lock S1 Ready τ2 Β Ready τ3 S1 locked S1 unlocked τ4(L) Time 14 Synchronization & Priority Inversion B Carnegie Mellon University Software Engineering Institute Highest Locker’s Priority Protocol τ2:{...P(S1)...V(S1)...} τ4:{...P(S1)...V(S1)...} Legend S1 locked τ1(H) Executing Blocked Ready τ2 Β Β Ready τ3 S1 locked S1 unlocked τ4(L) Time 15 Synchronization & Priority Inversion B Carnegie Mellon University Software Engineering Institute Priority Ceiling Protocol (PCP) τ2:{...P(S1)...V(S1)...} τ4:{...P(S1)...V(S1)...} Legend S1 locked τ1(H) Executing Attempts to lock S1 Blocked S1 locked S1 unlocked Ready τ2 Β Ready τ3 S1 locked S1 unlocked τ4(L) Time 16 Synchronization & Priority Inversion B Carnegie Mellon University Software Engineering Institute Example Of Chained Blocking (BIP) τ1:{...P(S1)...P(S2)...V(S2)...V(S1)...} Legend τ2:{...P(S1)...V(S1)...} τ3:{...P(S2)...V(S2)...} S2 locked Attempts to lock S2 (blocked) Attempts to lock S1 (blocked) τ1(H) S1 locked Β S1 locked Blocked Β S1 unlocked τ2(M) S2 locked S2 unlocked τ3(L) 0 1 2 3 4 5 6 7 8 9 10 11 Time 17 Synchronization & Priority Inversion 12 13 B Carnegie Mellon University Software Engineering Institute Blocked At Most Once (PCP) τ1:{...P(S1)...P(S2)...V(S2)...V(S1)...} Legend τ2:{...P(S1)...V(S1)...} τ3:{...P(S2)...V(S2)...} S2 locked S1 locked S2 locked S2 unlocked S1 locked Ceiling S1 unlocked Attempts to lock S1 (blocked) τ1(H) C S1 locked Attempts to lock S1 (blocked) S1 unlocked τ2(M) S2 unlocked S2 locked τ3(L) 0 18 Synchronization & Priority Inversion 1 2 3 4 5 6 7 8 9 10 11 Time 12 13 C Carnegie Mellon University Software Engineering Institute Deadlock: Using BIP τ1:{...P(S1)...P(S2)...V(S2)...V(S1)...} Legend τ2:{...P(S2)...P(S1)...V(S1)...V(S2)...} S1 locked S2 locked attempts to lock S2 (blocked) Blocked locks S1 τ1(H) B Attempts to lock S1 (blocked) S2 locked τ2(M) 0 1 2 3 4 5 6 7 8 9 10 11 Time 19 Synchronization & Priority Inversion 12 13 B Carnegie Mellon University Software Engineering Institute Deadlock Avoidance: Using PCP Legend τ1:{...P(S1)...P(S2)...V(S2)...V(S1)...} S1 locked τ2:{...P(S2)...P(S1)...V(S1)...V(S2)...} S2 locked Ceiling locks S2 attempts to lock S1 (blocked) locks S1 τ1(H) C locks S2 locks S1 unlocks S1 unlocks S2 τ2(M) 0 1 2 3 4 5 6 7 8 9 10 11 Time 20 Synchronization & Priority Inversion 12 13 C Carnegie Mellon University Software Engineering Institute Summary of Synchronization Protocols Protocol Nonpreemptible critical sections Highest locker’s priority Basic inheritance Priority ceiling Bounded Priority Inversion Blocked at Most Once Deadlock Avoidance Yes Yes1 Yes1 Yes Yes1 Yes1 Yes Yes No Yes2 No Yes 1 Only if tasks do not suspend within critical sections 2 PCP is not affected if tasks suspend within critical sections 21 Synchronization & Priority Inversion Carnegie Mellon University Software Engineering Institute Sample Problem with Synchronization When basic priority inheritance protocol is used: 22 Synchronization & Priority Inversion Task Period Execution Time Priority τ1 τ2 τ3 100 150 350 20 40 100 High Medium Low Blocking Deadline Delays 20+10 10 0 100 130 350 Carnegie Mellon University Software Engineering Institute UB Test for Sample Problem This format is sometimes called a schedulability model for the task set: f1 C1 B1 20 30 = ------ + ------ = --------- + --------- = 0.500 < U ( 1 ) 100 100 T1 T1 f2 C1 C2 B2 20 40 10 = ------ + ------ + ------ = --------- + --------- + --------- = 0.534 < 0.729 100 150 150 T1 T2 T2 U ( 2, .80 ) = 0.729 f3 C1 C2 C3 20 40 100 = ------ + ------ + ------ = --------- + --------- + --------- = 0.753 < U ( 3 ) 100 150 350 T1 T2 T3 23 Synchronization & Priority Inversion