Upload
mathhoang
View
270
Download
3
Embed Size (px)
Citation preview
Real-Time and Embedded
Systems
Nam Ho
Dr. Anh-Vu Dinh-Duc
8/20/2010 Embedded Systems Course: Real-Time Task Scheduling - 2010 2
Module Outline
Introduction to Real-Time Systems
Real-Time Task Scheduling
Real-Time Operating Systems (RTOS)
Measuring Execution Time and Real-Time Performance
Development of Embedded Systems
Our Research Results in Embedded Systems
8/20/2010 Embedded Systems Course: Real-Time Task Scheduling - 2010 3
Real-Time Task Scheduling
Real-Time Tasks Provide function(s) in real-time systems
Every real-time system consists of a number of real-time tasks
Classification of Real-Time Task Periodic Task
Sporadic Task
Aperiodic Task
Classification of Real-Time Task Scheduling Algorithms Clock-Driven
Event-Driven
8/20/2010 Embedded Systems Course: Real-Time Task Scheduling - 2010 4
Task Instances
Each time an event occurs, the real-time
system triggers the task that handles this
event to run. Denoted: Ti
Each time a task recurs, it is called an
instance of the task. The first time a ask
occurs, it is called the first instance of the
task, and so on. Denoted: Ti(j) for the jth
instance of a task Ti
Sometimes are called jobs
8/20/2010 Embedded Systems Course: Real-Time Task Scheduling - 2010 5
Dealine and Timing Constraints
Absolute deadline: The absolute deadline of a task is the absolute time value (counted from time 0) by which the results from the task are expected. Ф + d
Relative deadline: Relative deadline is the time interval between the arrival of a task (instance) and the corresponding deadline. d
Response time: The response time is the time duration from the occurrence of the event generating the task to the time the task (instance) produces its results.
Respond time
8/20/2010 Embedded Systems Course: Real-Time Task Scheduling - 2010 6
Periodic Task
A periodic task is one that repeats after a certain fixed time interval For example, a periodic task may perform signal processing of a radar scan once every 2
seconds
A periodic task Ti can be represented by a 4 tuple (Фi, pi, ei, di) Фi : the time that the first instance occurs from 0.
pi: the period of task
ei: the worst case execution time of the task
di: the relative deadline of the task
Example: T1 = (1, 3, 1, 2)
T2 = (2, 4, 2, 3)
T1’s priority is higher than T2’s. In other words, T2 is preempted by T1
0time
2 4 6 8 10 12 14 16 18
T1 instance T2 instance
Ф1
p1
e1
d2
8/20/2010 Embedded Systems Course: Real-Time Task Scheduling - 2010 7
Sporadic Task & Aperiodic Task
Many real-time systems are required to respond to external events. The tasks taking action against these events are called sporadic tasks & aperiodic tasks
Sporadic task A sporadic task has zero or more instances, and there is a minimum
separation between two consecutive releases of the same task. Example: In a factory, the task that handles fire conditions is a sporadic
task Represented by a 3 tuple: Ti = (ei, gi, di) gi: the minimum separation between two consecutive instances of the
task. gi > 0 Aperiodic task
Is sporadic task, except that gi can be 0 Example: A logging system in a distributed system. The logging requests
from different tasks may arrive at the logger almost at the same time A sporadic instance has a hard deadline An aperiodic instance has either a soft deadline or no deadline (a
bunching of the task instances and it might lead to a few deadlinemisses)
8/20/2010 Embedded Systems Course: Real-Time Task Scheduling - 2010 8
Schedule
A schedule is an assignment of all task instances in the system on the available processors.
A valid schedule satisfies the following conditions: Every processor is assigned to at most one task at any time
Every task is assigned at most one processor at any time
No task is scheduled before its arrival time
All the precedence and resource usage constraints are satisfied
A valid schedule is also a feasible schedule if every task meets its timing constraints.
A real-time task scheduler is called optimal, if it can feasibly schedule any task set that can be feasibly scheduled by any other scheduler
The scheduling points of a scheduler are the points on time line at which the scheduler makes decisions regarding which task is to be run next.
8/20/2010 Embedded Systems Course: Real-Time Task Scheduling - 2010 9
Classification of Real-Time
Task Scheduling Algorithms
Clock-Driven Table-driven
Cyclic
Event-Driven (Priority-Driven) Rate Monotonic Algorithm (RMA)
Deadline Monotonic Algorithm (DMA)
Earliest Deadline First (EDF)
Only concern with uniprocessor scheduling algorithms ( without multiprocessor, distributed scheduling algorithms).
Non Real-Time
Scheduling
Algorithms:
- FCFS ( First Come
First Served)
- SJF (Shortest Job
First)
- SRTF (Shortest
Remaining Time
First)
Don’t care
the task
deadline
8/20/2010 Embedded Systems Course: Real-Time Task Scheduling - 2010 10
Clock-Driven Scheduling
Clock-driven schedulers are those for which the scheduling points are determined by timer interrupts
Table-driven schedulers usually pre-compute which task would run when, and store this schedule in a table at the time the system is designed or configured
How to decide the size of the schedule table for a set of periodic real-time tasks ? Only save a major cycle of a set of tasks. lcm(p1, p2, …, pn)
Example: T1 = (1, 3, 1, 2)
T2 = (2, 4, 2, 3)
M = lcm(3, 4) = 12
Task Start time
T1 0
T2 1
T1 3
T2 5
T1 6
T2 11
T1 9
8/20/2010 Embedded Systems Course: Real-Time Task Scheduling - 2010 11
Clock-Driven Scheduling
Cyclic schedulers The major cycle is divided into one or more minor cycles
(frames) The scheduling points of a cyclic scheduler occur at frame
boundaries Each task is assigned to run in one or more frames (split case) A selected frame size should satisfy the following three
constraints Minimum Context Switching
Minimization of Table Size
Satisfaction of Task Deadline
interrupt interrupt interrupt
8/20/2010 Embedded Systems Course: Real-Time Task Scheduling - 2010 12
Clock-Driven Scheduling –
Frame Size Constraints
Minimum Context Switching To avoid preemption, want tasks to start and complete
execution within a single frame
F ≥ max({ei}) (a lower-bound) (Eq.1)
Minimization of Table Size The major cycle contains an integral number of frames
(Eq.2)
Satisfaction of Task Deadline There must exist at least one full frame between the arrival
of a task and its deadline
2*F – gcd(pi, F ) ≤ di for i = 1, 2, …, n (Eq.3)
/ /M F M F
8/20/2010 Embedded Systems Course: Real-Time Task Scheduling - 2010 13
Frame Size Constraints –
Example
Example T1 = (4, 1.0), T2 = (5, 1.8), T3 = (20, 1.0), T4 = (20, 2.0)
Фi = 0, di = pi, M = lcm(4, 5, 20, 20) = 20
Constraints Eq.1 -> F ≥ max(1.0, 1.8, 1.0, 2.0) ≥ 2
Eq.2 -> F {2, 4, 5, 10, 20 }
Eq.3 -> 2F – gcd(4,F) ≤ 4 (T1)
Eq.3 -> 2F – gcd(5,F) ≤ 5 (T2)
Eq.3 -> 2F – gcd(20,F) ≤ 20 (T3, T4)
F = 2
F = ?
8/20/2010 Embedded Systems Course: Real-Time Task Scheduling - 2010 14
Frame Size Constraints – Split
Task
Example T1 = (4, 1), T2 = (5, 2), T3 = (20, 5)
Фi = 0, di = pi, M = lcm(4, 5, 20) = 20
Constraints Eq.1 -> F ≥ 5
Eq.2 -> F {5, 10, 20 }
Eq.3 -> no value of F is suitable
Splitting T3
T3.1=(20, 1), T3.2=(20, 2), T3.3=(20,2)
F now are 2 or 4
How to identify which task is to be split ?
8/20/2010 Embedded Systems Course: Real-Time Task Scheduling - 2010 15
Comparison of Cyclic with
Table-Driven Scheduling
In table-driven scheduling, a timer has to be set every time a task starts to run (lead to overhead).
In cyclic scheduling, the timer gives an interrupt exactly at every frame boundary. This interrupt timer is set only once at the application initialization time.
Therefore, a cyclic scheduler is more efficient than a table-driven scheduler
However, a shortcoming of the cyclic schedulers is that it becomes very complex to determine a suitable frame size when the number of tasks increases
Can cyclic schedulers handle sporadic tasks and aperiodic tasks ?
8/20/2010 Embedded Systems Course: Real-Time Task Scheduling - 2010 16
Clock Driven – Scheduling
Sporadic & Aperidic Tasks
The sporadic and aperiodic tasks are scheduled in the slack times that may be available in the frames
Slack time can be used to execute
aperiodic & sporadic tasks
Sporadic tasks have hard deadlines, scheduler
can determine if they are schedulable when it
arrives
8/20/2010 Embedded Systems Course: Real-Time Task Scheduling - 2010 17
Even-Driven Scheduling
Cyclic schedulers are predominantly used in small applications
Event-driven schedulers deploy more complex scheduling algorithms
Event-driven schedulers are used in all moderate and large-sized applications having many tasks
A higher priority task becomes ready, it preempts any lower priority task that may be running (preemptive)
Fixed-priority Scheduling Rate Monotonic Algorithm (RMA) Deadline Monotonic Algorithm (DMA)
Dynamic-priority Scheduling Earliest Deadline First (EDF)
8/20/2010 Embedded Systems Course: Real-Time Task Scheduling - 2010 18
Even-Driven Scheduling
A schedulability test is used to validate that a given application
can satisfy its specified deadlines when scheduled according to a specific scheduling algorithm
A schedulability test is Sufficient, if a task set that passes is (definitely)
schedulable; but there may exist task sets that fail the test, which is schedulable
Necessary, if a task set that fails are (definitely) not schedulable; but there may exist task sets that pass the test which are not schedulable
8/20/2010 Embedded Systems Course: Real-Time Task Scheduling - 2010 19
Rate Monotonic Algorithm
(RMA) of Periodic Tasks
RM assigns priorities to tasks based on their rates of occurrence. Rate = 1/pi
Фi = 0, di = pi
A task having the highest occurrence rate (lowest period) is assigned the highest priority
Example: T1 = (4, 1), rate = 1/4
T2 = (5, 1), rate = 1/5
T3 = (20, 4), rate = 1/20
Priorities: T1 > T2 > T3
0 2 4 6 8 10 12 14 16 18 20
8/20/2010 Embedded Systems Course: Real-Time Task Scheduling - 2010 20
Rate Monotonic Algorithm
(RMA) of Periodic Tasks
Given a periodic task Ti , the ratio ui = ei / pi
is called the utilization of task Ti
The total utilization U of all tasks in a
system is the sum of the utilizations of all
individual tasks:
Schedulability Test
Necessary Condition
If U > 1, then the task set is not schedulable
1/
n
i iU e p
8/20/2010 Embedded Systems Course: Real-Time Task Scheduling - 2010 21
Rate Monotonic Algorithm
(RMA) of Periodic Tasks
Sufficient Condition (Liu and Layland’s condition) A set of n real-time periodic tasks are schedulable
under RM, if
1
( ) (2 1)nRMU U n n
If a task set passes the Liu and Layland test, then it is guaranteed to be RMA schedulable. On the other hand, even if a task set fails the Liu and Layland test, it may still be RMA schedulable
For n , URM(n) = URM= loge2 = 0.692
8/20/2010 Embedded Systems Course: Real-Time Task Scheduling - 2010 22
Rate Monotonic Algorithm
(RMA) of Periodic Tasks
Example 1: T1 = (4, 1), rate = 1/4
T2 = (5, 1), rate = 1/5
T3 = (20, 4), rate = 1/20
Priorities: T1 > T2 > T3
U = 0.65 < URM, a sufficient condition for RMA schedulability is satisfied
Example 2: T1 = (4, 1), rate = 1/4
T2 = (5, 2), rate = 1/5
T3 = (20, 5), rate = 1/20
Priorities: T1 > T2 > T3
U = 0.9 > 3(21/3 -1) = 0.78, the task set is not RMA-schedulable
But it is still schedulable using the RM policy as illustrated in the figure
0 2 4 6 8 10 12 14 16 18 20
8/20/2010 Embedded Systems Course: Real-Time Task Scheduling - 2010 23
Rate Monotonic Algorithm
(RMA) of Periodic Tasks
Lehoczsky Test Consider three tasks with priorities in the order of T1, T2, T3. Assume Фi =
0, di = pi (the first iteration of each task is arrived at time zero)
Lets observe the first iteration of each task T1 can be feasibly scheduled if e1 ≤ p1
Suppose T2 finishes at time t, t [0, p2]. The total number of iterations of task T1 that have been arrived over [0,p2] is t/p1
If we can find some t [0, p2] satisfying the following condition, we are done:
In other words, if we show that there exists some integer k such that kp1 ≥ke1+e2 and kp1 ≤ p2, we have met the necessary and sufficient conditions for T2 to be schedulable under the RM algorithm
T3 is schedulable if there is some t [0, p3] satisfying the following condition, we are done:
Generally, we can find some t [0, pi] that is a multiple of p1 and/or p2, pi and it satisfies the following condition for all tasks, we are done:
1 2
1
tt e e
p
1 2 3
1 2
t tt e e e
p p
1
1
.i
j i
j j
tt e e
p
8/20/2010 Embedded Systems Course: Real-Time Task Scheduling - 2010 24
Rate Monotonic Algorithm
(RMA) of Periodic Tasks
Lehoczsky Test
Task system X is feasible by the RMA if
and only if L = max1≤i ≤n Li ≤ 1, where
1
min ( ), ( ) . /i
i
i t S i i j
j j
tL L t L t e t
p
{ . | 1,2,..., ; 1,2,..., }ii j
j
pS k p j i k
p
8/20/2010 Embedded Systems Course: Real-Time Task Scheduling - 2010 25
Rate Monotonic Algorithm
(RMA) of Periodic Tasks
Example 1: n = 3. T1=(100,40), T2=(150, 40), T3=(350,10)
U=0.94 > 3(21/3 -1) = 0.78, the task set is not RMA-schedulable
Applying Lehoczsky Test
i = 1: S1 = {100} t = 100: L1(100) = 40/100 < 1. Thus, L1 < 1
i = 2: S2 = {100, 150} t = 100: L2(100) = 80/100 < 1. Thus, L2 < 1
i = 3: S3 = {100, 150, 200, 300, 350} t = 100: L3(100) = 180/100 > 1
t = 150: L3(150) = 220/150 > 1
t = 200: L3(200) = 260/200 > 1
t = 300: L3(300) = 300/300 = 1. Thus, the task system is feasible
Example 2: n = 3. T1=(100,60), T2=(150, 50), T3=(350,20)
i = 1: S1 = {100} t = 100: L1(100) = 60/100 < 1. Thus, L1 < 1
i = 2: S2 = {100, 150} t = 100: L2(100) = 110/100 > 1 .
t = 150: L2(150)= 170/150 > 1. Thus the whole task system is not feasible
8/20/2010 Embedded Systems Course: Real-Time Task Scheduling - 2010 26
Deadline Monotonic Algorithm
(DMA) of Periodic Tasks
The deadline monotonic algorithm assigns task priority according to relative deadlines. The shorter the relative deadline, the higher the priority
Deadline monotonic preferred to rate monotonic if deadline ≠ period
When relative deadline of every task matches its period, then rate monotonic and deadline monotonic give identical results
Rate monotonic always fails when deadline monotonic fails
8/20/2010 Embedded Systems Course: Real-Time Task Scheduling - 2010 27
Deadline Monotonic Algorithm
(DMA) of Periodic Tasks
Lehoczsky Test (An extension) [Y. Manabe
Lemma 3]
Task system X is feasible by the DMA if and
only if L = max1≤i ≤n Li ≤ 1, where
1
min ( ), ( ) . /i
i
i t S i i j
j j
tL L t L t e t
p
{ } { . | 1,2,..., ; 1,2,..., }ii i j
j
dS d k p j i k
p
8/20/2010 Embedded Systems Course: Real-Time Task Scheduling - 2010 28
Deadline Monotonic Algorithm
(DMA) of Periodic Tasks
Example: T1 = (p1,e1,d1) = (50, 10, 35), T2 = (100, 15, 20),
T3 = (200, 20, 200)
RMA Priorities: T1 > T2 > T3
Failed to Lehoczky Test
DMA: Priorities: T2 > T1 > T3
Passing Lehoczky Test
8/20/2010 Embedded Systems Course: Real-Time Task Scheduling - 2010 29
Earliest Deadline First (EDF)
Scheduling
The task priority may be changed during runtime
In EDF scheduling, at every scheduling point the task having the shortest deadline is taken up for scheduling
The EDF algorithm gives higher priority to task instances that have missed their deadlines than to task instances whose deadline is still in the future
Dynamic algorithms like EDF can produce feasible schedules in cases where RM and DM cannot
The EDF algorithm can achieve full utilization of the processor (100% CPU utilization)
8/20/2010 Embedded Systems Course: Real-Time Task Scheduling - 2010 30
Earliest Deadline First (EDF)
Scheduling
A task set of periodic real-time tasks {T1, T2, …, Tn}with pi ≤ di is schedulable under EDF, if and only if it satisfies the condition that the total processor utilization is less than 1.
1
1n
i
i i
eU
p
If di ≤ pi, a task set of periodic real-time tasks {T1, T2, …, Tn} is schedulable under EDF, if it satisfies:
Eq. 4
1
1n
i
i i
eU
dEq. 5 (only a sufficient condition)
8/20/2010 Embedded Systems Course: Real-Time Task Scheduling - 2010 31
Earliest Deadline First (EDF)
Scheduling
Example: T1 = (p1,e1) = (30, 15), T2 = (40, 15), T3 = (50, 5)
U = 0.974 > 3(21/3 -1) = 0.78, the task set is not RMA-schedulable
But the task set is EDF-schedulable (Eq.4)
0 10 20 30 40 50 60 70 80 90 100
T1
T2
T3
RMA
EDF
T1,1 T1,2 T1,3 T1,4
T2,1 T2,2T2,3
T3,1 T3,2 T3,3
T1,1 T1,2T2,1 T2,2T3,1
T1,1 T2,1 T3,1 T1,2 T2,2 T1,3 T3,2 T1,4 T2,3
8/20/2010 Embedded Systems Course: Real-Time Task Scheduling - 2010 32
Fixed- vs Dynamic-Priority
Algorithms
Dynamic-priority algorithms give high run-time overhead for deadline scheduling We need to keep the tasks ordered in terms of their
deadlines using a priority queue.
But they give low preemptions (context switch)
RMA/DMA are simple and efficient and are very commonly used for scheduling real-time tasks in practical applications
Most commercial Real-Time Operating Systems (RTOS) support real-time static priority levels for tasks
8/20/2010 Embedded Systems Course: Real-Time Task Scheduling - 2010 33
Even-Driven – Scheduling
Sporadic & Aperiodic Tasks
Based on the execution time and deadline of each newly arrived sporadic task, decide whether to accept or reject the task Accepting the task implies that the task will complete
within its deadline, without causing any periodic task or previously accepted sporadic task to miss its deadline
Do not accept a sporadic task if cannot guarantee it will meet its deadline
Aim to complete each aperiodic task as soon as possible, without causing periodic tasks or accepted sporadic tasks to miss deadlines Aperiodic tasks are always accepted
8/20/2010 Embedded Systems Course: Real-Time Task Scheduling - 2010 34
Even-Driven – Scheduling
Aperiodic Tasks
Background Scheduling Aperiodic tasks are scheduled and executed only at times when
there are no periodic or sporadic tasks ready for execution Extremely simple to implement
Long response times for the aperiodic tasks
Interrupt Scheduling
Whenever an aperiodic task arrives, the execution of periodic
tasks is interrupted, and the aperiodic task is executed
Improve the response time for aperiodic tasks
Cause periodic/sporadic tasks in the system to miss some deadlines
8/20/2010 Embedded Systems Course: Real-Time Task Scheduling - 2010 35
Even-Driven – Scheduling
Aperiodic Tasks
Slack stealing
There may be some slack time between
completion of the periodic job and its deadline
Can move the slack time earlier in the schedule,
running periodic tasks ‘just in time’ to meet their
deadlines
Execute aperiodic tasks in the slack time, ahead
of periodic tasks
Can reduces response time for aperiodic tasks
But computing available slack is difficult in practice
8/20/2010 Embedded Systems Course: Real-Time Task Scheduling - 2010 36
Even-Driven – Scheduling
Aperiodic Tasks
Handling through an aperiodic server task As any other periodic tasks
T = (ps, es), es: server capacity (or budget)
Once activated, it starts serving the pending aperiodic requests within the limit of its current capacity
8/20/2010 Embedded Systems Course: Real-Time Task Scheduling - 2010 37
Even-Driven – Scheduling
Sporadic Tasks
Strategy is the same as scheduling
aperiodic task’s
But has to perform an acceptance test
8/20/2010 Embedded Systems Course: Real-Time Task Scheduling - 2010 38
Implementation Real-Time
Systems
Based on Clock-Driven Scheduling All task time are pre-computed offline and store
in a table
Low run-time overhead
Task table can get very large
Simple, suitable for small embedded devices
Based on Even-Driven Scheduling Scheduling based on priority
Flexibility but incurring high run-time overhead
Used in all moderate and large-sized applications having many tasks
8/20/2010 Embedded Systems Course: Real-Time Task Scheduling - 2010 39
Reference
Qing Li and Carolyn Yao, Real-Time Concepts for Embedded Systems, CMP Books, 2003
http://nptel.iitm.ac.in/courses/Webcourse-contents/IIT%20Kharagpur/Embedded%20systems/New_index1.html
Lehoczky, J. Sha, L. Ding, Y., The rate monotonic scheduling algorithm: exact characterization and average case behavior, proceedings of Real Time Systems Symposium, 1989
Y. Manabe S. Aoyagi, A feasibility decision algorithm for rate monotonic scheduling of periodic real-time tasks, proceedings of the Real-Time Technology and Applications Symposium, 1995
Albert m. K. Cheng, Real-Time Systems, Scheduling, Analysis, and Verification [Chap. 3]