Upload
makya
View
31
Download
1
Embed Size (px)
DESCRIPTION
CSE 522 Real-Time Scheduling (3) . Computer Science & Engineering Department Arizona State University Tempe, AZ 85287 Dr. Yann -Hang Lee [email protected] (480) 727-7507. 1 :{…P(S1)…V(S1)…} 3 :{…P(S1)…V(S1)…}. Attempt to lock S1 (blocked). S1 locked. S1 unlocked. 2(M). 1(H). - PowerPoint PPT Presentation
Citation preview
1
CSE 522 Real-Time Scheduling (3)
Computer Science & Engineering DepartmentArizona State University
Tempe, AZ 85287
Dr. Yann-Hang [email protected](480) 727-7507
2
Priority Inversion in Synchronization
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
4
Accounting for Priority Inversion
Recall that task schedulability is affected bypreemption: two types of preemption
can occur several times per period can occur once per period
execution: once per periodblocking: 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
5
UB Test with Blocking
Include blocking while calculating effective utilization for each tasks:
Hn Preemption(can hit n times) Execution
Blocking H1 Preemption(can hit once)
1n Hk
kii
i
i
i
Hj j
ji e
p1
pB
pe
pe
f
6
Blocking is also included in the RT test
Perform test as before, including blocking effect
RT Test with Blocking
i
1jji0
j
1i
1j j
nii1n
eBa where
epaeBa
7
Consider the following examplePeriodic 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 msec30 msec
Data Structure
1
2
3
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 tabletask Period Execution
TimePriority Blocking
delayDeadline
1100 25 High 30+50 100
2200 50 Medium 0 200
3300 100 Low 0 300
9
UB Test for Example
UB test with blocking:
1n Hk
kii
i
i
i
Hj j
ji e
p1
pB
pe
pe
f
00.105.110080
10025
pB
pef
1
1
1
11
)2(U5.020050
10025
pe
pef
2
2
1
12
)3(U84.0300100
20050
10025
pe
pe
pef
3
3
2
2
1
13
Not schedulable
with additional RT test, 3 is shown to be schedulable
10
Synchronization Protocols
No preemption Basic priority inheritance Highest locker’s priority Priority ceiling
Each protocol prevents unbounded priority inversion.
11
Nonpreemption Protocol
2:{…P(S1)…V(S1)…}4:{…P(S1)…V(S1)…}
readyblocked
ready
ready
S1 locked S1 unlocked
Time
1(H)
4(L)
2
3
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(csi)
13
Basic Inheritance Protocol (BIP)
2:{…P(S1)…V(S1)…}4:{…P(S1)…V(S1)…}
ready
ready
S1 locked S1 unlocked
1(H)
4(L)
2
3
blocked
attempts to lock S1
S1 unlockedS1 locked
ready
inherits the priority of 2
after 2 is blocked
14
Some Notations
Ji is the i-th job in Task T. i = Assigned priority of Job Ji
i(t) = current priority of Ji
If the decision to change the priority of Job Ji is made at t = t1 then i(t1
-) = priority at and immediately before, i(t1
+) = priority immediately after the priority change
= nonexistent priority, lower than the lowest priority
15
Terminology and Assumptions At time t1, job Ji requests resource Rk. Rk Jl: Resource Rk is held by Job Jl
Ji Rk: Job Ji is blocked waiting for resource Rk to be released (Ji Rk Jl)
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 Ji 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
16
Priority Inheritance Rules
Scheduling Rule: same as the assumptions Allocation Rule: same as the assumptions Priority-Inheritance Rule:
if Ji Rk Jl and l(t1-) = priority of Jl at t = t1
then l(t1+) = i(t1)
until Jl releases Rk at t2 when l(t2+) = l(t1
-)
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
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 taski blocks j and j blocks k , then i can inherit the priority of k.
When taski 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).
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
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
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)
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
T2 T3 T4 T2 T3 T4 T2 T3 T4
T1 N Y Y 5 7
T2 N Y Y Y 5 7
T3 Y Y 7
22
Priority Ceiling Protocol (PCP)
2:{…P(S1)…V(S1)…} 3:{…P(S2)…V(S2)…}4:{…P(S1)…V(S1)…}
ready
ready
1(H)
2
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
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 Ji Rk Jl at t = t1 then block Ji (no change) Rk free at t1,
if i(t1) > S(t1), then Rk Ji
else (i.e. i(t1) S(t1) )if for some Rx Ji and (Rx) = S(t1), then Rk Ji [ Ji holds a resource Rx whose priority ceiling is S(t1) ]
else deny and block (Ji Rk)
24
Basic Priority Ceiling Rules (2) Priority-Inheritance Rule:
if Ji Rk at t = t1 and is blocked by Jl (and l(t1-) = priority
of Jl) either Rk Jl, (Jl holds the resource Rk)
or Jl Rx and (Rx) = S(t1) i(t1) then l(t1
+) = i(t1) (inherited priority) until Jl releases all Rx with (Rx) i(t1), l(t2
+) = l(t1-) at t
= t2.
25
Blocking in PCP
A task TH can be blocked by a lower-priority task TL in three ways: directly, i.e.
when TL inherits a priority higher than the priority H of TH.
When TH requests a resource the priority ceiling of resources held by TL is equal to or higher than H:
X TLTH
request for allocated to
X TLT TH
( > H)
Y TLTH
(H X)
X
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 locked S2 unlocked
S1 locked S1 unlocked
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
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”.
T1
Ti
Tn
no stack sharing
T1
Ti
Tn
stack sharing
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: Ji released and blocked until i(t) > S(t) When not blocked jobs are scheduled in the usual manner.
Allocation Rule: Allocate when requested
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
31
SRP + EDF
Let 1, 2, . . . , n denote the tasks, and R1,R2, . . . , Rm denote the non-preemptable resources. Each resource Rj is statically assigned a ceiling (Rj), which is
set equal to the minimum relative deadline parameter of any task that may access it:
(Rj) = min{Di | i accesses Rj} 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 Di is strictly less than the system ceiling.
(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 Ji, i(t) = i
Resource held by Ji,i(t) = max((Rx)) for all resource Rx
held by Ji
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)
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.
34
Highest Locker’s Priority Protocol
2:{…P(S1)…V(S1)…}4:{…P(S1)…V(S1)…}
ready
ready
S1 locked S1 unlocked
1(H)
4(L)
2
3
BB
run with the priority of 2
35
Summary of Synchronization ProtocolsProtocol Bounded Priority
InversionBlocked at Most
OnceDeadlock
AvoidanceNonpreemptible critical sections
Yes Yes1 Yes1
Highest locker's priority
Yes Yes1 Yes1
Basic inheritance Yes No No
Priority Ceiling Yes Yes2 Yes1
1 Only if tasks do not suspend within critical sections2 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