Download presentation
Presentation is loading. Please wait.
1
Real-Time Resource Scheduling A. Mok 2016
CS 386C Real-Time Resource Scheduling A. Mok 2016
2
Why Scheduling Matters
3
Real-Time Application Scheduling
4
Task Model with Demand-driven Timing Constraints
Task set M = Mp Ms Mp is a set of periodic tasks Ms is a set of sporadic tasks Task i is a tuple (Ti , ri , di , pi ) Ti is a chain of n(i) code blocks: {Ti,1, Ti,2, ...Ti, n(i) } Code blocks are separated by communication primitives. Ti,k the kth code block of task i has an integer computation time ci,k. ci = Σk ci,k is the computation time of task i ri is the ready time of task i di is the deadline of task i pi is the period/minimum separation of task i We shall assume that ci ≤ di ≤ pi Time is discrete (time units in positive integers)
5
Demand-Driven Timing Constraints
Periodic tasks are invoked at fixed interval: k*p, k=0,1,... Sporadic tasks may be invoked at any time t = 0,1, ... However, two successive invocations must be separated by at least p time units. If a task is invoked at time t, then its execution must be completed by time t+d. Additional scheduling constraints are imposed by the communication primitives between the code blocks. An execution (run) is a mapping which assigns an integer time to the start and stop events of instances of the blocks. p d r time t t+d t+p
6
Liu and Layland Periodic Tasks
Task i is parameterized by a pair (ci , pi) Task requests arrives at t = 0, pi , 2pi , 3pi … Ready time ri = 0 Deadline di = pi Example: Task A is (1,4) Task B is (3,5) How should A and B be scheduled? A B time 1 2 3 4 5
7
The Earliest Deadline (ED) Scheduler
Schedule at every clock tick the ready task whose deadline is the nearest. Break ties arbitrarily. A “time- slot swapping” trick to show that the ED scheduler is optimal (for independent tasks).
8
The Earliest Deadline (ED) Scheduler (continued)
A well known scheduling result (Liu and Layland 1973): A periodic task is represented by a pair (c,p). Let M be a set of independent periodic tasks: M = {(c1, p1), ..., (cn, pn)} Let U = Σ1≤i≤n (ci/pi). U is called the utilization factor of the task set. Then a necessary and sufficient condition for scheduling M on a single processor is U ≤ 1
9
Proof Sketch M = {(c1, p1), (c2,p2), ... (cn, pn)}
Δ =1/Πi=1n pi In each time unit, assign to jth process cj / pj time unit i.e. (cj / pj) / Δ = cj (π pi)/pj mini-unit This schedule is “good” check: (cj / pj) pj = cj meets deadline check: Σ cj / pj ≤ 1 no overflow
10
Proof Sketch (continued)
Do time-slice swapping to convert to earliest deadline algorithm Preemption now always falls on integral boundaries. Deadlines (end of period) occur at integral boundaries.
11
Definition The achievable utilization factor (also called utilization bound) of a scheduling policy UA is a real number such that for any task set Π, Uπ ≤ UA implies the schedulability of Π The achievable utilization factor of the earliest deadline scheduler is 1.0 The achievable utilization factor of the static priority scheduler is ≈ ln2 ≈ 0.7
12
Preemptive Fixed Priority Scheduler
Let M be a set of n periodic/sporadic tasks {(C1, P1), ... (Cn, Pn)} where Ci, Pi are respectively the computation time and the minimum separation between successive requests for task Ti. Unless otherwise stated, we shall adopt the convention that task Ti has higher priority than task Tj if i < j . The preemptive fixed-priority (PFP) scheduling policy executes the task with the highest priority among all ready tasks in M. A schedule produced by a PFP scheduler is a PFP schedule.
13
How should we assign priorities to the tasks?
The Rate Monotonic Algorithm (RMA) Assign priorities in increasing order of P such that tasks with shorter periods are assigned higher priority.
14
Definition Response time (of a job request) rD D A B D C D time
A task may generate many requests each of which requires the completion of a job. If we can determine the worst-case response time of the jobs that can be generated by the task, then we know the deadline that the task can satisfy. Job response times are determined by the scheduling policy of the tasks.
15
Response Time Calculation
The response time of a task is the sum of The task’s execution time Delay caused by higher priority tasks because of priority preemption Delay caused by lower priority tasks because of resource blocking Let us consider execution time only for now. rC D C A C B C time D C A B Priority A > B > C > D D and C share a resource
16
Critical Instant Critical instant (of a task in a schedule) is the finish-time of a request which has the longest response time.
17
Critical Instant (continued)
When is the critical instant of a low priority task? We shall show that a critical instant occurs when the requests of all the tasks line up in time and all the tasks issue their requests at the maximum allowable rate as determined by the period/minimum separation parameters Consider the case of 2 tasks TH High priority task TL Low priority task
18
Case 1: TL does not run at t
A Critical Instant occurs when all tasks request simultaneously at their maximum rate Case 1: TL does not run at t Case 2: TL runs at t
19
Outstanding Computation
A task T is said to have an outstanding computation at time t in a schedule s iff a request r for T occurs at time t', t' < t, and r has not been satisfied at time t. t” t” TH has outstanding computation at time t but no outstanding computation at time t”. TL has no outstanding computation at time t and no outstanding computation at time t”. TH has no outstanding computation at time t but has outstanding computation at time t”. TL has no outstanding computation at time t but has outstanding computation at time t”.
20
N(i,s,t,t') For a schedule s, a task Ti and time values t, t' define N(i,s,t,t') to be the number of requests of Ti that appear in the interval [t,t') in s. N(i,s,t,t’) is defined to be 0 if t = t’. Notice that N(i,s,t,t') is bounded from above by ┌ (t'-t)/Pi ┐, and is exactly equal to ┌ (t'-t)/Pi ┐ if a request for Ti occurs at t and Ti issues a request every Pi time units thereafter. 1 2 3 4 5 6 7 8 9 10 Examples: N(TL,S,3,8) = N(TL,S,3,9) = N(TL,S,2,8) = 1 N(TH,S,0,4) = N(TH,S,0,5) = N(TH,S,0,6) = 2
21
Lemma 1 Suppose s is a schedule such that there is no outstanding computation for any task at time t0 ≥ 0. Suppose a request r for task Tn occurs at t0 in s, and r is satisfied at time t1. Then the assertions (I1), (I2) below must hold. (I1) for every t0<t<t1, Cn + Σ1≤in Ci·N(i,s,t0,t) > t-t0 (I2) Cn + Σ1≤in Ci · N(i,s,t0,t1) = t1-t0 t1
22
Proof Sketch Since there are no outstanding computations at time t0 and the scheduler does not idle the processor wherever there is an outstanding computation, a simple induction on t shows that Cn + Σ1≤in Ci N(i,s,t0,t) is the amount of computation time that is needed to satisfy the request of Tn at t0 plus the requests for all the higher priority tasks in the interval [t0, t) in s. QED t0 t1
23
Lemma 2 Suppose s is a schedule such that there is no outstanding computation for any task at time t0 for some t0 ≥ 0, and a request r for task Tn occurs at t0. Let s' be a schedule in which there are no outstanding computations at time t0, all tasks request simultaneously at time t0 and request at their maximum rates thereafter. If the request r in s has a response time = u and the request for task Tn at time t0 in s' has a response time = u', then u' ≥ u. t0 r t1’ u’ Requests at max rate from higher-priority tasks t1 u No outstanding computation Schedule s Schedule s’ time
24
Proof Sketch t0 r t1’ u’ Requests at maximum rate from higher-priority tasks t1 u No outstanding computation Let the completion times of the requests for Tn that occur at time t0 in s and s' be respectively t1 and t1'. Assume the contrary: u' < u, i.e. t1' < t1 . Applying (I1) of lemma 1 to schedule s yields: Cn + Σ1≤in Ci N(i,s,t0,t1') > t1'-t0 Bounding N(i,s,t0,t1') by┌ (t1'-t0)/Pi ┐, we have Cn + Σ1≤in Ci ┌ (t1'-t0)/Pi ┐ > t1' – t0 Since all tasks request at their maximum rate from time t0 in schedule s', so for every 1≤in, N(i,s',t0,t1') = ┌ (t1'-t0)/Pi ┐ Substituting N(i,s',t0,t1') for ┌ (t1'-t0)/Pi ┐, we have Cn + Σ1≤in Ci N(i,s',t0,t1') > t1'-t0 Applying (I2) of lemma 1 to s' yields the following contradiction: Cn + Σ1≤in Ci N(i,s',t0,t1') = t1'-t0
25
Critical-Instant Test
Let M be a set of sporadic tasks. Let s be a PFP schedule of M in which all tasks request simultaneously at time 0, and all tasks request at their maximum rates thereafter. A task T in M is said to pass its critical-instant test if the response time of the request for T at time 0 in the schedule s is no bigger than its minimum separation parameter.
26
Theorem 3 If Tn passes its critical-instant test, then Tn is schedulable by a PFP scheduler. Proof sketch: We show that every request of Tn must have a response time no bigger than Pn in any PFP schedule. Without loss of generality, we shall disregard the scheduling of the tasks {Ti | i > n}, i.e., we consider only the n highest priority tasks.
27
Proof Strategy For any time value t0 such that:
(P1) there is no outstanding computation for Tn at time t0 in s, and (P2) there is a request for Tn at t0 We shall show that the request at t0 must have a response time ≤ Pn
28
Details Let tx be the biggest time value, 0 ≤ tx ≤ t0 such that there is no outstanding computation for any task at time tx.. Since there is no outstanding computation at time 0, tx must exist. Notice that if tx ≠ t0 then the processor cannot idle in the interval [tx , t0] and only tasks with priority higher than n are executed in [tx , t0]. tx r t0 No outstanding computation at tx No outstanding computation for Tn at t0 Processor does not idle in this interval t1
29
tx Processor does not idle in this interval t0 t1 No outstanding computation at tx r No outstanding computation for Tn at t0 Consider a schedule s' such that there are no requests for the tasks T1, ... Tn-1 before tx in s', and the requests for these tasks at or after tx in s' occur at the same time as those in s. Also let the first request for Tn in s' occur at time tx. By construction of s', the response time of this request is equal to t0 – tx plus the response time of the request for Tn that occurs at t0 in schedule s. Since there are no outstanding computations for T1, ..., Tn at time tx in s', and Tn passes its critical-instant test, the response time of the first request of Tn in s' must be at most Pn by lemma 2, and therefore the request for Tn at t0 in s must have a response time not exceeding Pn. The arrival time of the first request of Tn trivially satisfies (P1) and (P2) , and hence the first request must have response time ≤ Pn. Suppose the first i requests of Tn have response time ≤ Pn. Then the i+1th request must satisfy (P1) and (P2), and hence must also have response time ≤ Pn QED
30
Critical Schedule Given a task set M and a priority assignment, let s be the PFP schedule of M such that all tasks request simultaneously at time 0, and all tasks request at their maximum rates thereafter. We call s the critical schedule of M. If every task in a sporadic task set M meets its first deadline in the critical schedule of M, then M is schedulable by a PFP scheduler. RMA (Rate Monotonic) Priority Assignment: Suppose M = {(C1, P1), ..., (Cn, Pn)} is a set of n sporadic tasks, and task Ti has higher priority than task Tj if i<j. Then the priority assignment of tasks in M is consistent with RMA if Pi ≤ Pj, 1≤i,j≤n
31
Optimality of RMA Theorem 4 Proof Idea:
Suppose M is a set of tasks whose priority assignment is consistent with RMA. Then M is schedulable iff its critical schedule is feasible. Proof Idea: Consider two tasks τi = (Ci,Pi ), τi+1 = (Ci+1,Pi+1 ) with adjacent priorities. Suppose Pi+1 < Pi but τi has higher priority than τi+1. Let the first jobs of both tasks together span the interval [t1,t2] in the critical schedule. If we swap their priorities, both jobs together will still span [t1,t2]. Notice that only tasks with priorities equal to or higher than τi+1 can execute in [t1,t2]. t1 t2 Pi+1 Pi time
32
Achievable Utilization Factor (Utilization Bound)
Consider the class L of all task sets that cannot be satisfied by a PFP scheduler. Let UL be the greatest lower bound of the utilization factors of the task sets in L. Any task set that has a utilization factor smaller than UL is by definition schedulable by a PFP scheduler. This gives us a quick sufficiency test for schedulability. We call UL the achievable utilization (utilization bound) of the PFP scheduling policy. L contains task sets not schedulable by PFP Task sets schedulable by PFP and U < UL Task sets schedulable by PFP and U ≥ UL Consider all unschedulable task sets. A lower bound lb on their utilization satisfies the property that if a task set π is unschedulable, then Uπ > lb UL is the greatest number that satisfies this lower bound property.
33
Maximal Task Set A task set is said to fully utilize a processor if the following two conditions hold: (1) The task set is schedulable (2) Increasing the computation of any task in the task set will render the resulting task set unschedulable. We call a task set that fully utilizes a processor a maximal task set. Example 1: {(P1=2, C1=1), (P2=3, C2=1)} Notice that [0,3] is fully occupied. So no increase in C1 or C2 is possible. Example 2: {(P1=2, C1=1), (P2=3, C2=1), (P3=6, C3=1)} Notice that the task set in example 1 has one fewer task and < 100% utilization but still fully utilizes the processor
34
Computation of Utilization Bound
We can compute UL by the following strategy. First, we consider the class B of all maximal task sets and show that any lower bound on the utilization factors of task sets in B is also a lower bound of UL. For any fixed set {P1, ..., Pn} of minimum separation parameters of n tasks, we determine among all assignments of computation times to these n tasks one that has the lowest utilization factor such that the resulting task set is a maximal task set. This enables us to express the minimum utilization factor as a function of P1, ..., Pn. Next, we compute among all combinations of n minimum separation parameters one that minimizes the utilization factor. The result is a decreasing function of n alone. UL is obtained by taking the limit of this function as n approaches ∞ For task systems with n tasks, we can show that UL (n) = n ( 21/n – 1)
35
The Task Management Problem
For the purpose of implementing the ED algorithm, there are two important parameters in the task control block: the current deadline d and the ready time r. We say that a task is not ready if its ready time has not yet arrived. Suppose the task control blocks are stored in data structure D. The relevant task management operations are: SELECT(D): Return the ready task with the smallest d. Post(π,D): Insert into D the task π which has been preempted or which has just completed execution. In the latter case, set d to be the end of the next period and set r to be the start of the next period. Wakeup(t,D): Classify any task in D as ready whose ready time r = t.
36
Stack Discipline If task A preempts task B, task A must complete before task B can resume. never the case
37
Stack Discipline (cntd)
If stack discipline is obeyed, we can “charge” the cost of preemption once to the preempting task, i.e. Just add cost of one context switch to computation time of any task that may preempt another.
38
Scheduling Overhead Interrupts may make ready (wakeup) some tasks.
Interrupt (task or timer) Context switch Interrupts may make ready (wakeup) some tasks. Context switch is needed either when a task is preempted by another or when a task completes execution
39
Priority Queue Implementation
Use an array
40
Priority Queue Implementation
Root is stored at location 1 Node stored at location i has its left descendant at location 2i, and its right descendant at location 2i+1 To find left descendant, shift address left by one bit. To find right descendant, shift address left by one bit and add 1. To find parent, shift right by one bit.
41
Priority Queue Since the ED algorithm requires a dynamic priority scheduler, a “textbook solution” is to use a priority queue with the deadlines as keys.
42
Priority Queue Solution Requires O(n)
In the worst case, O(n) operations are required to select the task with the nearest deadline where n is the number of tasks. This is because tasks that are not ready could percolate toward the root of the heap and block the tasks that are ready but have later deadlines.
43
Example
44
Priority Queue Solution Requires O(n)
Having two separate heaps for ready and not ready tasks will not help in reducing the worst- case time complexity, since in the worst case, all of the tasks in the not-ready heap may become ready at the same time, and O(n) operations are required to move all of them to the ready heap.
45
The HOH (Heap of Heap) Data Structure
A HOH is a heap whose elements can be heaps themselves A HOH supports priority queue operations: Insert (x, H): Insert x into the HOH H; x may be a simple node or a heap with two or more nodes DeleteMin(H): Remove and return from H the node with the highest priority (smallest integer). Reorganize H so that it remains a HOH
46
The HOH (Heap of Heap) Data Structure (cntd)
47
Theorem Both Insert and DeleteMin operations on a HOH have O(logn) worst-case time complexity where n is the number simple nodes.
48
A O(log n) Solution The data structure D for task management is a pair (H,T) where H is a HOH and T is a 2-3 tree. Each ready task is stored as a simple node in the HOH. The leaves of the 2-3 tree are heaps and each not ready task is stored as a node in one of the leaves. All the tasks in the same leaf (heap) have the same ready time. *An alternative is to use a timing wheel in place of the 2-3 tree.
49
The Task Management Operations
Select(D), Post(π,D), Wakeup(t,D)
50
Example of Task Management Operations
51
Priority Inversion Priority inversion occurs when a lower priority task executes on the CPU while a higher priority task has not completed execution. Server Task High-Priority Client Task Low-Priority Client Task M L H Priority Low-priority client blocks high-priority client when high-priority client is executing in the server even if low-priority client does not use the server Low-priority client blocks high-priority client when low-priority client is executing in the server even if high-priority client does not use the server
52
The Mars Pathfinder Episode
The first landing of a spacecraft on Mars was accomplished by the Mars Pathfinder on July 4, 1997 Soon after landing, the Pathfinder experienced repeated RESETs by the on-board computer Resets were caused by timing overruns reported by the watchdog timer process The root cause was a priority inversion problem that got resolved after a few days by setting a flag in the software to enable priority inheritance Reference: nder.htm
53
Basic Priority Inheritance Protocol (BPIP)
Basic Idea: When a job J blocks higher priority jobs, it executes its critical section at the highest priority level of all blocked jobs. A Job J may attempt to obtain the lock on a semaphore S but be blocked by another job that has locked S A job uses its assigned priority, unless it blocks higher priority jobs, in which case it inherits the priority of the highest priority job blocked by it directly or indirectly A job J can preempt another job JI if J is not blocked and its priority is higher than JI's
54
Example of Basic Protocol in Action
J1 has the highest priority J2 has the middle priority J3 has the lowest priority
55
Properties of the Basic Protocol
Goal is to avoid blocking of high-priority tasks: Direct blocking: a high-priority task cannot preempt the exclusive resource use by a low-priority task Indirect blocking: a high-priority task is blocked by a medium- priority task that has preempted a low-priority task holding a shared resource Theorem 4) Under BPIP, if there are n lower priority jobs, a job J can be blocked for at most the duration of n critical sections Theorem 6) Under BPIP, if there are m semaphores that can be used to block job J, then J can be blocked for at most the duration of m critical sections.
56
Chained Blocking J1 is assigned the highest priority
J2 is assigned the middle priority J3 is assigned the lowest priority
57
Deadlock J1 has higher priority than J2 deadlock
58
The Priority Ceiling Protocol
Goal: To prevent deadlocks and to prevent chained blocking. Idea: Maintain an invariant such that if job J preempts the critical section of another job and executes its own critical section, the priority at which J’s critical section executes must be strictly higher than the priorities of all the jobs executing in the preempted critical sections, taking priority inheritance into account.
59
Priority Ceiling Protocol
The ceiling priority of a protected resource is the highest priority of the task that may use the resource The priority ceiling protocol is the basic protocol plus an additional blocking rule A task cannot gain access to a protected resource if the task's priority is ≤ max. of the ceiling priorities of the resources currently in use by other tasks.
60
Example: P.C.P. Prevents Deadlocks
J0 is assigned the highest priority J1 is assigned the middle priority J2 is assigned the lowest priority
61
Example: P.C.P. Prevents Chained Blocking
J1 is assigned the highest priority J2 is assigned the middle priority J3 is assigned the lowest priority
62
Where Ri = { (k,l) : 1≤k≤i, 1≤l≤ ⌊Ti/Tk⌡}
Properties of the P.C.P. Theorem 10) The priority ceiling protocol prevents deadlocks Theorem 12) A job J can be blocked for at most the duration of one critical section of any lower priority job. Theorem 16) Schedulability. Assuming periodic tasks and deterministic execution time, a set of n tasks using P.C.P can be scheduled by the rate-monotonic algorithm for all task phasing if: j=i-1 i, 1≤i≤n min ( Uj (Tj / lTk) lTk /Tj + (Ci / lTk) + (Bi / lTk) ) ≤ 1.0 j=1 (k,l) Ri Where Ri = { (k,l) : 1≤k≤i, 1≤l≤ ⌊Ti/Tk⌡}
63
Example of using theorem 16 for schedulability analysis
Given three tasks with the following characteristics, are they schedulable? T1: C1=40; P1=100; B1=20; U1=0.4 T2: C2=40; P2=150; B2=30; U2=0.267 T3: C3=100; P3=350; B3=0; U3=0.268 T1 is assigned the highest priority, T2 the middle priority and T3 the lowest priority.
64
Solution Schedulability Checks: Check C1 + B1 ≤ 100 (40 + 20 ≤ 100 √)
T1: C1=40; P1=100; B1=20; U1=0.4 T2: C2=40; P2=150; B2=30; U2=0.267 T3: C3=100; P3=350; B3=0; U3=0.268 Schedulability Checks: Check C1 + B1 ≤ ( ≤ 100 √) Check C1 + C2 + B2 ≤ ( ≥ 100) or 2C1 + C2 + B2 ≤ ( ≤ 150 √) Check C1 + C2 + C3 ≤ ( > 100 or 2C1 + C2 + C3 ≤ ( > 150) or 2C1 + 2C2 + C3 ≤ ( > 200) or 3C1 + 2C2 + C3 ≤ ( ≤ 300 √) or 4C1 + 3C2 + C3 ≤ ( > 350) Therefore these tasks are schedulable
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.