Scheduling Basics
Scheduling Basics
Task 1
... ...
Scheduler
Task n
RTOS/Run-Time System
Hardware
How to schedule the Tasks such that given timing constraints are satisfied?
1
User requirements:
Timing constraints Energy consumption Economical constraints Period or frequency Execution time (resource requirement) Code size Input/output relations Precedence constraints
speed of the processor/number of CPU cycles Number of processors Memory size Memory bandwidth Communication bandwidth Caches
Design/Implementation constraints
Platform constraints
So far
RTOS features
RTOS functions
RT programming languages
Todays topic
Real-Time Scheduling:
Basic concepts and basic algorithms
My plan
Basic scheduling theory: summary Liu and Laylands classic results Response time analysis
Multiprocessor/multi-core scheduling
Task models
Precedence constraints
Resource constraints
Scheduling Problems
Given a set of tasks (ready queue)
1. 2.
3.
Check if all deadlines can be met (schedulability check) If yes, construct a feasible schedule to meet all deadlines If yes, construct an optimal schedule e.g. minimizing response times
How to find a feasible schedule? (OBS: there may be many feasible schedules)
Schedule: (2,3)(3,5)(1,10)
If EDF cannt find a feasible schedule for a task set, then no other algorithm can, which means that the task set is non schedulable.
10
Order tasks in increasing order of deadlines If C1+...+Ck <=Dk for all k, the task set is schedulable Response time for task i, Ri =C1+...+Ci Prove that EDD is optimal ?
11
EDF: Examples
(1,10)(3,3)(2,5) is schedulable
12
EDF: optimality
Assume that Ri is the finishing time of task i, i.e. response time. Let Li = Ri-Di (the lateness for task i) FACT: EDF is optimal, minimizing the maximum lateness Lmax= MAXi(Li)
Note that even a task set is non schedulable, EDF may minimize the maximal lateness (minimizes e.g. the loss for soft tasks)
13
Whenever new tasks arrive, sort the ready queue according to earlist deadlines Run the first task of the queue
At any time, order the tasks according to EDF (A1, C1, D1) ... (Ai,Ci,Di)
If C1+...+Ck <=Dk for all k=1,2...i, then the task
15
16
Assume that Ri is the finishing time/response time of task i. Let Li = Ri-Di (the lateness for task i)
FACT: preemptive EDF is optimal in minimizing the maximum lateness Lmax= MAXi(Li)
17
Run a task until its finished and then sort the queue according to EDF
However it is not optimal, it may not find the feasible schedule even it exists.
18
T1 A 0
T2 1
EDF Schedule
C D
4 7
2 5
Feasible Schedule
CPU idling
19
If we only consider non-idle algorithms (CPU waiting only if no tasks to run), is EDF is optimal? Unfortunately no! Example
T1= (0, 10, 100) T2= (0,1,101) T3= (1,4,4) Run T1,T3,T2: the 3rd task will miss its deadline Run T2,T3,T1: it is a feasible schedule
20
To construct optimal non-preemptive Schedule: complete search, NP-hard is needed The decision should be made according to all the parameters in the whole list of tasks The worst case is to test all possible combinations of n tasks (NP-hard, difficult for large n)
21
simple and easy to implement but may not find a schedule if n is too big (worst case)
22
6
2 2 T4 3 T2 5 T3 7 T1
C D
2 7
1 5
2 6
2 4
T2
23
Heuristic methods
Use heuristic function H to guide the search until a feasible schedule is found, otherwise backtrack: add a new node in the search tree if the node has smallest value according to H e.g H(task i) = Ci, Ai, Di etc [Spring alg.] However it may be difficult to find the right H
24
Example Heuristics
= = = =
Ai Ci Di Di +w*Ci
25
EDF: + and
Preemptive EDF
Simple (+) Optimal (+) No need for computing times (+) difficult to implement efficiently (-) Need a list of timers, one per task, Overheads for context switch
Non-preemptive EDF
26
Classical ones
LRT (Latest Release Time or reverse EDF) LST (Least Slack Time first)
27
Release time = arrival time Idea: no advantage to completing any hard task sooner than necessary. We may want to postpone the execution of hard tasks e.g to improve response times for soft tasks. LRT: Schedule tasks from the latest deadline to earliest deadline. Treat deadlines as release times and arrival times as Deadlines. The latest Deadline first FACT: LRT is optimal in finding feasible schedule (for preemptive tasks)
28
LRT: Example
T1 T2 T3 A C D 0 4 11 12 3 4
T1
20
18
11
20 18 17
T2
18 17
13
11
(D=absolute deadline)
T3
17 13
Reverse time: we get the schedule: T1(9,11)T2(11,13)T3(13,17)T2(17,18)T1(18,20) OBS: from 0 to 9, soft tasks may be running!
29
LRT: + and
It needs Arrival times (-) It got to be an off-line algorithm (-) Only for preemptive tasks (-) It could optimize Response times for soft tasks (+)
30
Idea: there is no point to complete a task earlier than its deadline. Other (soft) tasks may be executed first
Slack stealing
LST: order the queue with nondecreasing slack times FACT: preemptive LST is optimal in finding feasible schedules
31
LST: Example
T1 T2 T3 A C D 0 4 8 3 9 4
T1
4 S=15-9-2=4 at 9
20 15 15
T2
8 9
S=15-13-2=0 at 13
(D=absolute deadline)
S=15-9-4=2 at 9 T3
13 9 Comment: a task should run until a Slack reaches 0 (to avoid context switch) And if more than one 0-slack: nonschedulable
17
32
LST: + and
It needs Computing times (-) Only for preemptive tasks (-) Not easy to implement! (-) But it can run on-line (+) and it may improve response times?
33
Meaning that we can compute them in arbitrary orderings only if the orderings (schedules) are feasible All algorithms we have studied so far are applicable only to independent tasks
34
35
Dependent tasks
Such conditions are called precedence constraints which can be represented as Directed Acyclic Graphs (DAG) known as Precedence graphs Such graphs are also known as Task Graph
36
Some task is waiting for output of the others, data flow diagrams
T2
sampling T1
T4
T6 T7 output
T3
T5
37
B C
38
39
AND/OR-precedence graphs
AND-node, all incomming edges must be finished first OR-node: some of the incomming edges must be finished Similarly, outgoing edges may trigger tasks in a disjunctive or conjunctive manner
40
Overlapping area of blue and red is what we need Precedence constraints restrict the search area (Guiding!)
41
Assume a list of tasks: (A,C1,D1)(A,C2,D2) ...(A,Cn,Dn) In addition to the deadlines D1...Dn, the tasks are also constrained by a DAG Solution: Latest Deadline First (LDF), Lawler 1973 FACT: LDF is optimal (in finding feasible schedules)
42
Has the latest deadline and Does not precede any other tasks (a leaf!)
2.
Remove the selected task from the DAG and put it to the queue
Repeat the two steps until the DAG contains no more tasks. Then the queue is a potentilly feasible schedule. The last task selected should be run first. Note that this is similar to LRT
43
LDF: Example
T1 2
T1 T2 T3 T4 T5 T6 C
D
1
2
1
5
1
4
1
3
1
5
1
6
T4 3
T2 5
T3 4
T5 5
T6 6
44
LDF: Example
T1 T2 T3 T4 T5 T6 C D 1 2 1 5 1 4 1 3 1 5 1 6
T2 5 T1 2
T3 4
T4 3
T5 5
T6 6
LDF: T6
45
LDF: Example
T1 T2 T3 T4 T5 T6 C D 1 2 1 5 1 4 1 3 1 5 1 6
T2 5 T1 2
T3 4
T4 3
T5 5
LDF: T6
46
LDF: Example
T1 T2 T3 T4 T5 T6 C D 1 2 1 5 1 4 1 3 1 5 1 6
T2 5 T1 2
T3 4
T4 3
T5 5
LDF: T6,T5
47
LDF: Example
T1 T2 T3 T4 T5 T6 C D 1 2 1 5 1 4 1 3 1 5 1 6
T2 5 T1 2
T3 4
T4 3
LDF: T6,T5
48
LDF: Example
T1 T2 T3 T4 T5 T6 C D 1 2 1 5 1 4 1 3 1 5 1 6
T2 5 T1 2
T4 3
LDF: T6,T5,T3
49
LDF: Example
T1 T2 T3 T4 T5 T6 C D 1 2 1 5 1 4 1 3 1 5 1 6
T2 5 T1 2
LDF: T6,T5,T3,T4
50
LDF: Example
T1 T2 T3 T4 T5 T6 C D 1 2 1 5 1 4 1 3 1 5 1 6
T1 2
LDF: T6,T5,T3,T4,T2
51
LDF: Example
T1 T2 T3 T4 T5 T6 C D 1 2 1 5 1 4 1 3 1 5 1 6
LDF: T6,T5,T3,T4,T2,T1
52
LDF: Example
T1 T2 T3 T4 T5 T6 C D 1 2 1 5 1 4 1 3 1 5 1 6
LDF: T6,T5,T3,T4,T2,T1
Feasible Schedule
53
2.
Pick up a task with earlest deadline among all nodes that have no fathers (the roots) Remove the selected task from the DAG and put it to the queue
Repeat the two steps until the DAG contains no more tasks. Then the queue is a feasible schedule.
The earliest deadline now may lead to longer deadline in future (see the following example)
54
LDF: Example
T1 2
T1 T2 T3 T4 T5 T6 C
D
1
2
1
5
1
4
1
3
1
5
1
6
T4 3
T2 5
T3 4
T5 5
T6 6
55
LDF: Example
T1 2
T1 T2 T3 T4 T5 T6 C
D
1
2
1
5
1
4
1
3
1
5
1
6
T4 3
T2 5
T3 4
T5 5
T6 6
EDF: T1
56
EDF: Example
T1 T2 T3 T4 T5 T6 C
D
1
2
1
5
1
4
1
3
1
5
1
6
T4 3
T2 5
T3 4
T5 5
T6 6
EDF: T1
57
LDF: Example
T1 T2 T3 T4 T5 T6 C
D
1
2
1
5
1
4
1
3
1
5
1
6
T4 3
T2 5
T5 5
T6 6
EDF: T1,T3
58
LDF: Example
T1 T2 T3 T4 T5 T6 C
D
1
2
1
5
1
4
1
3
1
5
1
6
T4 3 T5 5 T6 6
EDF: T1,T3,T2
59
LDF: Example
T1 T2 T3 T4 T5 T6 C
D
1
2
1
5
1
4
1
3
1
5
1
6
EDF: T1,T3,T2,T4,T5,T6
60
LDF: Example
T1 T2 T3 T4 T5 T6 C
D
1
2
1
5
1
4
1
3
1
5
1
6
LDF: T6,T5,T3,T4,T2,T1
Feasible
EDF: T1,T3,T2,T4,T5,T6
Infeasible
61
Assume a list of tasks: S = (A1,C1,D1)(A2,C2,D2)...(A3,Cn,Dn) In addition to the deadlines D1...Dn, the tasks are also constrained by a DAG Solution: Complete Search
62
Better algorithms?
Assume a list of tasks: S = (A1,C1,D1)(A2,C2,D2)...(A3,Cn,Dn) In addition to the deadlines D1...Dn, the tasks are also constrained by the task graph Idea:
Transform the task set S to an Independent task set S* such that S is schedulable under DAG iff S* is schedulable
63
Idea: If Ti ->Tj is in the DAG i.e. Ti must be executed before Tj, we replace the arrival time for Tj and deadline for Ti with
Di*=min(Di,Dj-Cj)
64
Let arrival times and deadlines be absolute times Step 1: Transform the arrival times from roots to leafs
Pick up a node Tj whose fathers arrival times have been modified. If no such node, stop. Otherwise: Let Aj* =max(Aj, max{Ai*+Ci: Ti->Tj})
Pick up a node Ti all whose sons deadlines have been modified. If no such node, stop. Otherwise: Let Di* =min(Di, min{Dj*-Cj: Ti->Tj})
EDF*: optimality
FACT:
S is schedulable under a DAG iff S* is schedulable EDF* is optimal in finding a feasible schedule
66
Example
T1 2
T1 T2 T3 T4 T5 T6 C
D A
1
2 0
1
5 1
1
4 0
1
3 2
1
5 1
1
6 0
T4 3
T2 5
T3 4
T5 5
T6 6
67
EDF*: Example(1)
T1 2
T1 T2 T3 T4 T5 T6 C
D A
1
2 0
1
5 1
1
4 0
1
3 2
1
5 1
1
6 0
T4 3
T2 5
T3 4
T5 5
T6 6
68
EDF*: Example(1)
T1 2
T1 T2 T3 T4 T5 T6 C
D
1
2
1
5 1
1
4 1
1
3 2
1
5 2
1
6 2
T4 3
T2 5
T3 4
A* 0
T5 5
T6 6
69
EDF*: Example(2)
T1 2(1)
T1 T2 T3 T4 T5 T6 C
D
1
2
1
5 1
1
4 1
1
3 2
1
5 2
1
6 2
T4 3(3)
T2 5(2)
T3 4(4)
A* 0
T5 5(5)
T6 6(6)
70
EDF*: Example(2)
S*
T1 T2 T3 T4 T5 T6
T1 2(1)
1 2
1 4
1 3
1 5
1 6
D* 1
T2 5(2)
T3 4(4)
A* 0
2
T4 3(3) T5 5(5) T6 6(6)
71
EDF*: Example(3)
S*
T1 T2 T3 T4 T5 T6
T1 2(1)
1 2
1 4
1 3
1 5
1 6
D* 1
T2 5(2)
T3 4(4)
A* 0
2
T4 3(3) T5 5(5) T6 6(6)
72
EDF*: Example(3)
S*
T1 T2 T3 T4 T5 T6
1 2
1 4
1 3
1 5
1 6
D* 1
A* 0
73
EDF*: Example(3)
S*
T1 T2 T3 T4 T5 T6
1 2
1 4
1 3
1 5
1 6
D* 1
A* 0
74
EDF, Horn 74 O(n**2), Optimal LST, optimal LRT, optimal EDF* Chetto et al 90 O(n**2) optimal
Tree search Bratley71 O(n n!), optimal Spring, Stankovic et al 87 O(n**2) Heuristic As above
75
Nodes are computation tasks Edges are communication links Each node is characterized by resource requirements and local deadline Each edge may have a communication delay End-to-End deadline: root to leaf
Communication bus e.g. CAN BUS Shared memory, memory hiearchy Network-on-Chip
76
T1 T2 T3 T4 T5 T6 C
D
1
T3 4
1
2
1
5
1
4
1
3
1
5
1
6
T4 3
T5 5
T6 6
77
T1 T2 T3 T4 T5 T6 C
D
1
T3 4
1
2
1
5
1
4
1
3
1
5
1
6
T4 3
5
T5 5 T6 6
T7 13 78